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