001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003: *
004: * This code is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License version 2 only, as
006: * published by the Free Software Foundation. Sun designates this
007: * particular file as subject to the "Classpath" exception as provided
008: * by Sun in the LICENSE file that accompanied this code.
009: *
010: * This code is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: * version 2 for more details (a copy is included in the LICENSE file that
014: * accompanied this code).
015: *
016: * You should have received a copy of the GNU General Public License version
017: * 2 along with this work; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021: * CA 95054 USA or visit www.sun.com if you need additional information or
022: * have any questions.
023: */
024:
025: /*
026: * (C) Copyright IBM Corp. 1999 All Rights Reserved.
027: * Copyright 1997 The Open Group Research Institute. All rights reserved.
028: */
029:
030: package sun.security.krb5.internal.tools;
031:
032: import sun.security.krb5.*;
033: import sun.security.krb5.internal.*;
034: import sun.security.krb5.internal.ccache.*;
035: import sun.security.krb5.internal.ktab.*;
036: import sun.security.krb5.internal.crypto.EType;
037: import sun.security.krb5.KrbCryptoException;
038: import java.lang.RuntimeException;
039: import java.io.IOException;
040: import java.io.BufferedReader;
041: import java.io.InputStreamReader;
042: import java.io.File;
043:
044: /**
045: * This class can execute as a command-line tool to list entries in
046: * credential cache and key tab.
047: *
048: * @author Yanni Zhang
049: * @author Ram Marti
050: * @version 1.00 10 Jul 30
051: */
052: public class Klist {
053: Object target;
054: // for credentials cache, options are 'f' and 'e';
055: // for keytab, optionsare 't' and 'K' and 'e'
056: char[] options = new char[3];
057: String name; // the name of credentials cache and keytable.
058: char action; // actions would be 'c' for credentials cache
059: // and 'k' for keytable.
060: private static boolean DEBUG = Krb5.DEBUG;
061:
062: /**
063: * The main program that can be invoked at command line.
064: * <br>Usage: klist
065: * [[-c] [-f] [-e]] [-k [-t] [-K]] [name]
066: * -c specifes that credential cache is to be listed
067: * -k specifies that key tab is to be listed
068: * name name of the credentials cache or keytab
069: * <br>available options for credential caches:
070: * <ul>
071: * <li><b>-f</b> shows credentials flags
072: * <li><b>-e</b> shows the encryption type
073: * </ul>
074: * available options for keytabs:
075: * <li><b>-t</b> shows keytab entry timestamps
076: * <li><b>-K</b> shows keytab entry DES keys
077: */
078: public static void main(String[] args) {
079: Klist klist = new Klist();
080: if ((args == null) || (args.length == 0)) {
081: klist.action = 'c'; // default will list default credentials cache.
082: } else {
083: klist.processArgs(args);
084: }
085: switch (klist.action) {
086: case 'c':
087: if (klist.name == null) {
088: klist.target = CredentialsCache.getInstance();
089: klist.name = CredentialsCache.cacheName();
090: } else
091: klist.target = CredentialsCache.getInstance(klist.name);
092:
093: if (klist.target != null) {
094: klist.displayCache();
095: } else {
096: klist.displayMessage("Credentials cache");
097: System.exit(-1);
098: }
099: break;
100: case 'k':
101: if (klist.name == null) {
102: klist.target = KeyTab.getInstance();
103: klist.name = KeyTab.tabName();
104: } else
105: klist.target = KeyTab.getInstance(klist.name);
106: if (klist.target != null) {
107: klist.displayTab();
108: } else {
109: klist.displayMessage("KeyTab");
110: System.exit(-1);
111: }
112: break;
113: default:
114: if (klist.name != null) {
115: klist.printHelp();
116: System.exit(-1);
117: } else {
118: klist.target = CredentialsCache.getInstance();
119: klist.name = CredentialsCache.cacheName();
120: if (klist.target != null) {
121: klist.displayCache();
122: } else {
123: klist.displayMessage("Credentials cache");
124: System.exit(-1);
125: }
126: }
127: }
128: }
129:
130: /**
131: * Parses the command line arguments.
132: */
133: void processArgs(String[] args) {
134: Character arg;
135: for (int i = 0; i < args.length; i++) {
136: if ((args[i].length() >= 2) && (args[i].startsWith("-"))) {
137: arg = new Character(args[i].charAt(1));
138: switch (arg.charValue()) {
139: case 'c':
140: action = 'c';
141: break;
142: case 'k':
143: action = 'k';
144: break;
145: case 'f':
146: options[1] = 'f';
147: break;
148: case 'e':
149: options[0] = 'e';
150: break;
151: case 'K':
152: options[1] = 'K';
153: break;
154: case 't':
155: options[2] = 't';
156: break;
157: default:
158: printHelp();
159: System.exit(-1);
160: }
161:
162: } else {
163: if (!args[i].startsWith("-") && (i == args.length - 1)) {
164: // the argument is the last one.
165: name = args[i];
166: arg = null;
167: } else {
168: printHelp(); // incorrect input format.
169: System.exit(-1);
170: }
171: }
172: }
173: }
174:
175: void displayTab() {
176: KeyTab table = (KeyTab) target;
177: KeyTabEntry[] entries = table.getEntries();
178: if (entries.length == 0) {
179: System.out.println("\nKey tab: " + name + ", "
180: + " 0 entries found.\n");
181: } else {
182: if (entries.length == 1)
183: System.out.println("\nKey tab: " + name + ", "
184: + entries.length + " entry found.\n");
185: else
186: System.out.println("\nKey tab: " + name + ", "
187: + entries.length + " entries found.\n");
188: for (int i = 0; i < entries.length; i++) {
189: System.out.println("[" + (i + 1) + "] "
190: + "Service principal: "
191: + entries[i].getService().toString());
192: System.out.println("\t KVNO: "
193: + entries[i].getKey().getKeyVersionNumber());
194: if (options[0] == 'e') {
195: EncryptionKey key = entries[i].getKey();
196: System.out
197: .println("\t Key type: " + key.getEType());
198: }
199: if (options[1] == 'K') {
200: EncryptionKey key = entries[i].getKey();
201: System.out.println("\t Key: "
202: + entries[i].getKeyString());
203: }
204: if (options[2] == 't') {
205: System.out.println("\t Time stamp: "
206: + reformat(entries[i].getTimeStamp()
207: .toDate().toString()));
208: }
209: }
210: }
211: }
212:
213: void displayCache() {
214: CredentialsCache cache = (CredentialsCache) target;
215: sun.security.krb5.internal.ccache.Credentials[] creds = cache
216: .getCredsList();
217: if (creds == null) {
218: System.out.println("No credentials available in the cache "
219: + name);
220: System.exit(-1);
221: }
222: System.out.println("\nCredentials cache: " + name);
223: String defaultPrincipal = cache.getPrimaryPrincipal()
224: .toString();
225: int num = creds.length;
226:
227: if (num == 1)
228: System.out.println("\nDefault principal: "
229: + defaultPrincipal + ", " + creds.length
230: + " entry found.\n");
231: else
232: System.out.println("\nDefault principal: "
233: + defaultPrincipal + ", " + creds.length
234: + " 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 = reformat(creds[i].getAuthTime()
243: .toDate().toString());
244: endtime = reformat(creds[i].getEndTime().toDate()
245: .toString());
246: servicePrincipal = creds[i].getServicePrincipal()
247: .toString();
248: System.out.println("[" + (i + 1) + "] "
249: + " Service Principal: "
250: + servicePrincipal);
251: System.out.println(" Valid starting: "
252: + starttime);
253: System.out.println(" Expires: "
254: + endtime);
255: if (options[0] == 'e') {
256: etype = EType.toString(creds[i].getEType());
257: System.out.println("\t Encryption type: "
258: + etype);
259: }
260: if (options[1] == 'f') {
261: System.out.println("\t Flags: "
262: + creds[i].getTicketFlags().toString());
263: }
264: } catch (RealmException e) {
265: System.out.println("Error reading principal from "
266: + "the entry.");
267: if (DEBUG) {
268: e.printStackTrace();
269: }
270: System.exit(-1);
271: }
272: }
273: } else {
274: System.out.println("\nNo entries found.");
275: }
276: }
277:
278: void displayMessage(String target) {
279: if (name == null) {
280: name = "";
281: }
282: System.out.println(target + " " + name + " not found.");
283: }
284:
285: /**
286: * Reformats the date from the form -
287: * dow mon dd hh:mm:ss zzz yyyy to mon/dd/yyyy hh:mm
288: * where dow is the day of the week, mon is the month,
289: * dd is the day of the month, hh is the hour of
290: * the day, mm is the minute within the hour,
291: * ss is the second within the minute, zzz is the time zone,
292: * and yyyy is the year.
293: * @param date the string form of Date object.
294: */
295: String reformat(String date) {
296: return (date.substring(4, 7) + " " + date.substring(8, 10)
297: + ", " + date.substring(24) + " " + date.substring(11,
298: 16));
299: }
300:
301: /**
302: * Printes out the help information.
303: */
304: void printHelp() {
305: System.out.println("\nUsage: klist "
306: + "[[-c] [-f] [-e]] [-k [-t] [-K]] [name]");
307: System.out.println(" name\t name of credentials cache or "
308: + " keytab with the prefix. File-based cache or "
309: + "keytab's prefix is FILE:.");
310: System.out
311: .println(" -c specifes that credential cache is to be "
312: + "listed");
313: System.out
314: .println(" -k specifies that key tab is to be listed");
315: System.out.println(" options for credentials caches:");
316: System.out.println("\t-f \t shows credentials flags");
317: System.out.println("\t-e \t shows the encryption type");
318: System.out.println(" options for keytabs:");
319: System.out.println("\t-t \t shows keytab entry timestamps");
320: System.out.println("\t-K \t shows keytab entry key value");
321: System.out.println("\t-e \t shows keytab entry key type");
322: System.out
323: .println("\nUsage: java sun.security.krb5.tools.Klist "
324: + "-help for help.");
325: }
326: }
|