author | weijun |
Fri, 02 Oct 2009 18:49:05 +0800 | |
changeset 3950 | 5e02ff54347b |
parent 3317 | a1ea2f1893f9 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
3317 | 2 |
* Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
28 |
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
29 |
*/ |
|
30 |
||
31 |
package sun.security.krb5.internal.tools; |
|
32 |
||
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
33 |
import java.net.InetAddress; |
2 | 34 |
import sun.security.krb5.*; |
35 |
import sun.security.krb5.internal.*; |
|
36 |
import sun.security.krb5.internal.ccache.*; |
|
37 |
import sun.security.krb5.internal.ktab.*; |
|
38 |
import sun.security.krb5.internal.crypto.EType; |
|
39 |
||
40 |
/** |
|
41 |
* This class can execute as a command-line tool to list entries in |
|
42 |
* credential cache and key tab. |
|
43 |
* |
|
44 |
* @author Yanni Zhang |
|
45 |
* @author Ram Marti |
|
46 |
*/ |
|
47 |
public class Klist { |
|
48 |
Object target; |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
49 |
// for credentials cache, options are 'f', 'e', 'a' and 'n'; |
2 | 50 |
// for keytab, optionsare 't' and 'K' and 'e' |
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
51 |
char[] options = new char[4]; |
2 | 52 |
String name; // the name of credentials cache and keytable. |
53 |
char action; // actions would be 'c' for credentials cache |
|
54 |
// and 'k' for keytable. |
|
55 |
private static boolean DEBUG = Krb5.DEBUG; |
|
56 |
||
57 |
/** |
|
58 |
* The main program that can be invoked at command line. |
|
59 |
* <br>Usage: klist |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
60 |
* [[-c] [-f] [-e] [-a [-n]]] [-k [-t] [-K]] [name] |
2 | 61 |
* -c specifes that credential cache is to be listed |
62 |
* -k specifies that key tab is to be listed |
|
63 |
* name name of the credentials cache or keytab |
|
64 |
* <br>available options for credential caches: |
|
65 |
* <ul> |
|
66 |
* <li><b>-f</b> shows credentials flags |
|
67 |
* <li><b>-e</b> shows the encryption type |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
68 |
* <li><b>-a</b> shows addresses |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
69 |
* <li><b>-n</b> do not reverse-resolve addresses |
2 | 70 |
* </ul> |
71 |
* available options for keytabs: |
|
72 |
* <li><b>-t</b> shows keytab entry timestamps |
|
73 |
* <li><b>-K</b> shows keytab entry DES keys |
|
74 |
*/ |
|
75 |
public static void main(String[] args) { |
|
76 |
Klist klist = new Klist(); |
|
77 |
if ((args == null) || (args.length == 0)) { |
|
78 |
klist.action = 'c'; // default will list default credentials cache. |
|
79 |
} else { |
|
80 |
klist.processArgs(args); |
|
81 |
} |
|
82 |
switch (klist.action) { |
|
83 |
case 'c': |
|
84 |
if (klist.name == null) { |
|
85 |
klist.target = CredentialsCache.getInstance(); |
|
86 |
klist.name = CredentialsCache.cacheName(); |
|
87 |
} else |
|
88 |
klist.target = CredentialsCache.getInstance(klist.name); |
|
89 |
||
90 |
if (klist.target != null) { |
|
91 |
klist.displayCache(); |
|
92 |
} else { |
|
93 |
klist.displayMessage("Credentials cache"); |
|
94 |
System.exit(-1); |
|
95 |
} |
|
96 |
break; |
|
97 |
case 'k': |
|
98 |
if (klist.name == null) { |
|
99 |
klist.target = KeyTab.getInstance(); |
|
100 |
klist.name = KeyTab.tabName(); |
|
101 |
} else klist.target = KeyTab.getInstance(klist.name); |
|
102 |
if (klist.target != null) { |
|
103 |
klist.displayTab(); |
|
104 |
} else { |
|
105 |
klist.displayMessage("KeyTab"); |
|
106 |
System.exit(-1); |
|
107 |
} |
|
108 |
break; |
|
109 |
default: |
|
110 |
if (klist.name != null) { |
|
111 |
klist.printHelp(); |
|
112 |
System.exit(-1); |
|
113 |
} else { |
|
114 |
klist.target = CredentialsCache.getInstance(); |
|
115 |
klist.name = CredentialsCache.cacheName(); |
|
116 |
if (klist.target != null) { |
|
117 |
klist.displayCache(); |
|
118 |
} else { |
|
119 |
klist.displayMessage("Credentials cache"); |
|
120 |
System.exit(-1); |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Parses the command line arguments. |
|
128 |
*/ |
|
129 |
void processArgs(String[] args) { |
|
130 |
Character arg; |
|
131 |
for (int i = 0; i < args.length; i++) { |
|
132 |
if ((args[i].length() >= 2) && (args[i].startsWith("-"))) { |
|
133 |
arg = new Character(args[i].charAt(1)); |
|
134 |
switch (arg.charValue()) { |
|
135 |
case 'c': |
|
136 |
action = 'c'; |
|
137 |
break; |
|
138 |
case 'k': |
|
139 |
action = 'k'; |
|
140 |
break; |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
141 |
case 'a': |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
142 |
options[2] = 'a'; |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
143 |
break; |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
144 |
case 'n': |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
145 |
options[3] = 'n'; |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
146 |
break; |
2 | 147 |
case 'f': |
148 |
options[1] = 'f'; |
|
149 |
break; |
|
150 |
case 'e': |
|
151 |
options[0] = 'e'; |
|
152 |
break; |
|
153 |
case 'K': |
|
154 |
options[1] = 'K'; |
|
155 |
break; |
|
156 |
case 't': |
|
157 |
options[2] = 't'; |
|
158 |
break; |
|
159 |
default: |
|
160 |
printHelp(); |
|
161 |
System.exit(-1); |
|
162 |
} |
|
163 |
||
164 |
} else { |
|
165 |
if (!args[i].startsWith("-") && (i == args.length - 1)) { |
|
166 |
// the argument is the last one. |
|
167 |
name = args[i]; |
|
168 |
arg = null; |
|
169 |
} else { |
|
170 |
printHelp(); // incorrect input format. |
|
171 |
System.exit(-1); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
void displayTab() { |
|
178 |
KeyTab table = (KeyTab)target; |
|
179 |
KeyTabEntry[] entries = table.getEntries(); |
|
180 |
if (entries.length == 0) { |
|
181 |
System.out.println("\nKey tab: " + name + |
|
182 |
", " + " 0 entries found.\n"); |
|
183 |
} else { |
|
184 |
if (entries.length == 1) |
|
185 |
System.out.println("\nKey tab: " + name + |
|
186 |
", " + entries.length + " entry found.\n"); |
|
187 |
else |
|
188 |
System.out.println("\nKey tab: " + name + ", " + |
|
189 |
entries.length + " entries found.\n"); |
|
190 |
for (int i = 0; i < entries.length; i++) { |
|
191 |
System.out.println("[" + (i + 1) + "] " + |
|
192 |
"Service principal: " + |
|
193 |
entries[i].getService().toString()); |
|
194 |
System.out.println("\t KVNO: " + |
|
195 |
entries[i].getKey().getKeyVersionNumber()); |
|
196 |
if (options[0] == 'e') { |
|
197 |
EncryptionKey key = entries[i].getKey(); |
|
198 |
System.out.println("\t Key type: " + |
|
199 |
key.getEType()); |
|
200 |
} |
|
201 |
if (options[1] == 'K') { |
|
202 |
EncryptionKey key = entries[i].getKey(); |
|
203 |
System.out.println("\t Key: " + |
|
204 |
entries[i].getKeyString()); |
|
205 |
} |
|
206 |
if (options[2] == 't') { |
|
207 |
System.out.println("\t Time stamp: " + |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
208 |
reformat(entries[i].getTimeStamp().toDate().toString())); |
2 | 209 |
} |
210 |
} |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
void displayCache() { |
|
215 |
CredentialsCache cache = (CredentialsCache)target; |
|
216 |
sun.security.krb5.internal.ccache.Credentials[] creds = |
|
217 |
cache.getCredsList(); |
|
218 |
if (creds == null) { |
|
219 |
System.out.println ("No credentials available in the cache " + |
|
220 |
name); |
|
221 |
System.exit(-1); |
|
222 |
} |
|
223 |
System.out.println("\nCredentials cache: " + name); |
|
224 |
String defaultPrincipal = cache.getPrimaryPrincipal().toString(); |
|
225 |
int num = creds.length; |
|
226 |
||
227 |
if (num == 1) |
|
228 |
System.out.println("\nDefault principal: " + |
|
229 |
defaultPrincipal + ", " + |
|
230 |
creds.length + " entry found.\n"); |
|
231 |
else |
|
232 |
System.out.println("\nDefault principal: " + |
|
233 |
defaultPrincipal + ", " + |
|
234 |
creds.length + " entries found.\n"); |
|
235 |
String starttime = null; |
|
236 |
String endtime = null; |
|
237 |
String servicePrincipal = null; |
|
238 |
String etype = null; |
|
239 |
if (creds != null) { |
|
240 |
for (int i = 0; i < creds.length; i++) { |
|
241 |
try { |
|
242 |
starttime = |
|
243 |
reformat(creds[i].getAuthTime().toDate().toString()); |
|
244 |
endtime = |
|
245 |
reformat(creds[i].getEndTime().toDate().toString()); |
|
246 |
servicePrincipal = |
|
247 |
creds[i].getServicePrincipal().toString(); |
|
248 |
System.out.println("[" + (i + 1) + "] " + |
|
249 |
" Service Principal: " + |
|
250 |
servicePrincipal); |
|
251 |
System.out.println(" Valid starting: " + starttime); |
|
252 |
System.out.println(" Expires: " + endtime); |
|
253 |
if (options[0] == 'e') { |
|
254 |
etype = EType.toString(creds[i].getEType()); |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
255 |
System.out.println(" Encryption type: " + etype); |
2 | 256 |
} |
257 |
if (options[1] == 'f') { |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
258 |
System.out.println(" Flags: " + |
2 | 259 |
creds[i].getTicketFlags().toString()); |
260 |
} |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
261 |
if (options[2] == 'a') { |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
262 |
boolean first = true; |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
263 |
InetAddress[] caddr |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
264 |
= creds[i].setKrbCreds().getClientAddresses(); |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
265 |
if (caddr != null) { |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
266 |
for (InetAddress ia: caddr) { |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
267 |
String out; |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
268 |
if (options[3] == 'n') { |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
269 |
out = ia.getHostAddress(); |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
270 |
} else { |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
271 |
out = ia.getCanonicalHostName(); |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
272 |
} |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
273 |
System.out.println(" " + |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
274 |
(first?"Addresses:":" ") + |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
275 |
" " + out); |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
276 |
first = false; |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
277 |
} |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
278 |
} else { |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
279 |
System.out.println(" [No host addresses info]"); |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
280 |
} |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
281 |
} |
2 | 282 |
} catch (RealmException e) { |
283 |
System.out.println("Error reading principal from "+ |
|
284 |
"the entry."); |
|
285 |
if (DEBUG) { |
|
286 |
e.printStackTrace(); |
|
287 |
} |
|
288 |
System.exit(-1); |
|
289 |
} |
|
290 |
} |
|
291 |
} else { |
|
292 |
System.out.println("\nNo entries found."); |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
void displayMessage(String target) { |
|
297 |
if (name == null) { |
|
298 |
name = ""; |
|
299 |
} |
|
300 |
System.out.println(target + " " + name + " not found."); |
|
301 |
} |
|
302 |
/** |
|
303 |
* Reformats the date from the form - |
|
304 |
* dow mon dd hh:mm:ss zzz yyyy to mon/dd/yyyy hh:mm |
|
305 |
* where dow is the day of the week, mon is the month, |
|
306 |
* dd is the day of the month, hh is the hour of |
|
307 |
* the day, mm is the minute within the hour, |
|
308 |
* ss is the second within the minute, zzz is the time zone, |
|
309 |
* and yyyy is the year. |
|
310 |
* @param date the string form of Date object. |
|
311 |
*/ |
|
312 |
String reformat(String date) { |
|
313 |
return (date.substring(4, 7) + " " + date.substring(8, 10) + |
|
314 |
", " + date.substring(24) |
|
315 |
+ " " + date.substring(11, 16)); |
|
316 |
} |
|
317 |
/** |
|
318 |
* Printes out the help information. |
|
319 |
*/ |
|
320 |
void printHelp() { |
|
321 |
System.out.println("\nUsage: klist " + |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
322 |
"[[-c] [-f] [-e] [-a [-n]]] [-k [-t] [-K]] [name]"); |
2 | 323 |
System.out.println(" name\t name of credentials cache or " + |
324 |
" keytab with the prefix. File-based cache or " |
|
325 |
+ "keytab's prefix is FILE:."); |
|
326 |
System.out.println(" -c specifes that credential cache is to be " + |
|
327 |
"listed"); |
|
328 |
System.out.println(" -k specifies that key tab is to be listed"); |
|
329 |
System.out.println(" options for credentials caches:"); |
|
330 |
System.out.println("\t-f \t shows credentials flags"); |
|
331 |
System.out.println("\t-e \t shows the encryption type"); |
|
3950
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
332 |
System.out.println("\t-a \t shows addresses"); |
5e02ff54347b
6874472: display address lists for tickets in klist tool
weijun
parents:
3317
diff
changeset
|
333 |
System.out.println("\t -n \t do not reverse-resolve addresses"); |
2 | 334 |
System.out.println(" options for keytabs:"); |
335 |
System.out.println("\t-t \t shows keytab entry timestamps"); |
|
336 |
System.out.println("\t-K \t shows keytab entry key value"); |
|
337 |
System.out.println("\t-e \t shows keytab entry key type"); |
|
338 |
System.out.println("\nUsage: java sun.security.krb5.tools.Klist " + |
|
339 |
"-help for help."); |
|
340 |
} |
|
341 |
} |