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