jdk/src/windows/classes/sun/security/krb5/internal/tools/Ktab.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.ktab.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.RuntimeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class can execute as a command-line tool to help the user manage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * entires in the key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Available functions include list/add/update/delete service key(s).
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class Ktab {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // KeyTabAdmin admin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    KeyTab table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    char action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    String name;   // name and directory of key table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    String principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    char[] password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The main program that can be invoked at command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <br>Usage: ktab <options>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <br>available options to Ktab:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <li><b>-l</b>  list the keytab name and entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <li><b>-a</b>  &lt;<i>principal name</i>&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * (&lt;<i>password</i>&gt;)  add an entry to the keytab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The entry is added only to the keytab. No changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Kerberos database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <li><b>-d</b>  &lt;<i>principal name</i>&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * delete an entry from the keytab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The entry is deleted only from the keytab. No changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Kerberos database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <li><b>-k</b>  &lt;<i>keytab name</i> &gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * specify keytab name and path with prefix FILE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <li><b>-help</b> display instructions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Ktab ktab = new Ktab();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if ((args.length == 1) && (args[0].equalsIgnoreCase("-help"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ktab.printHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } else if ((args == null) || (args.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ktab.action = 'l';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ktab.processArgs(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (ktab.name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                //  ktab.admin = new KeyTabAdmin();    // use the default keytab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ktab.table = KeyTab.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (ktab.table == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    if (ktab.action == 'a') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        ktab.table = KeyTab.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        System.out.println("No default key table exists.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if ((ktab.action != 'a') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    !(new File(ktab.name)).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    System.out.println("Key table " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                ktab.name + " does not exist.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    ktab.table = KeyTab.getInstance(ktab.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (ktab.table == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    if (ktab.action == 'a') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        ktab.table = KeyTab.create(ktab.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        System.out.println("The format of key table " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                ktab.name + " is incorrect.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (RealmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.err.println("Error loading key table.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.err.println("Error loading key table.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        switch (ktab.action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        case 'l':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ktab.listKt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ktab.addEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        case 'd':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            ktab.deleteEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            ktab.printHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Parses the command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void processArgs(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Character arg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if ((args[i].length() == 2) && (args[i].startsWith("-"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                arg = new Character(args[i].charAt(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                printHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            switch (arg.charValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            case 'l':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                action = 'l';    // list keytab location, name and entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            case 'A':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                action = 'a'; // add a new entry to keytab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if ((i < args.length) && (!args[i].startsWith("-"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    principal = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    System.out.println("Please specify the principal name"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                       " after -a option.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    printHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if ((i + 1 < args.length) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    (!args[i + 1].startsWith("-"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    password = args[i + 1].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    password = null; // prompt user for password later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            case 'd':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                action = 'd'; // delete an entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if ((i < args.length) && (!args[i].startsWith("-"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    principal = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    System.out.println("Please specify the principal" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                       "name of the entry you want to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                       " delete after -d option.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    printHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            case 'k':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            case 'K':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if ((i < args.length) && (!args[i].startsWith("-"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    if (args[i].length() >= 5 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        args[i].substring(0, 5).equalsIgnoreCase("FILE:")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        name = args[i].substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        name = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    System.out.println("Please specify the keytab "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                       "file name and location " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                       "after -k option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    printHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                printHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Adds a service key to key table. If the specified key table does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * exist, the program will automatically generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * a new key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    void addEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        PrincipalName pname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            pname = new PrincipalName(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (pname.getRealm() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                pname.setRealm(Config.getInstance().getDefaultRealm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            System.err.println("Failed to add " + principal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                               " to keytab.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                BufferedReader cis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    new BufferedReader(new InputStreamReader(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                System.out.print("Password for " + pname.toString() + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                password = cis.readLine().toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                System.err.println("Failed to read the password.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // admin.addEntry(pname, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            table.addEntry(pname, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            Arrays.fill(password, '0');  // clear password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // admin.save();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            table.save();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            System.out.println("Done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            System.out.println("Service key for " + principal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                               " is saved in " + table.tabName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            System.err.println("Failed to add " + principal + " to keytab.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            System.err.println("Failed to save new entry.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
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
     * Lists key table name and entries in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    void listKt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        String principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // System.out.println("Keytab name: " + admin.getKeyTabName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        System.out.println("Keytab name: " + table.tabName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // KeyTabEntry[] entries = admin.getEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        KeyTabEntry[] entries = table.getEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if ((entries != null) && (entries.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            System.out.println("KVNO    Principal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            for (int i = 0; i < entries.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                version = entries[i].getKey().getKeyVersionNumber().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                principal = entries[i].getService().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    StringBuffer separator = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    for (int j = 0; j < 9 + principal.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        separator.append("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    System.out.println(separator.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                System.out.println("  " + version + "     " + principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            System.out.println("0 entry.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Deletes an entry from the key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    void deleteEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        PrincipalName pname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            pname = new PrincipalName(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (pname.getRealm() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                pname.setRealm(Config.getInstance().getDefaultRealm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            String answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            BufferedReader cis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                new BufferedReader(new InputStreamReader(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            System.out.print("Are you sure you want to "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                             " delete service key for " + pname.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                             " in " + table.tabName() + "?(Y/N) :");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            answer = cis.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (answer.equalsIgnoreCase("Y") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                answer.equalsIgnoreCase("Yes"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // no error, the user did not want to delete the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            System.err.println("Error occured while deleting the entry. "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                               "Deletion failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            System.err.println("Error occured while deleting the entry. "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                               " Deletion failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // admin.deleteEntry(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        table.deleteEntry(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            table.save();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            System.err.println("Error occurs while saving the keytab." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                               "Deletion fails.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        System.out.println("Done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Prints out the help information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    void printHelp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        System.out.println("\nUsage: ktab " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                           "<options>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        System.out.println("available options to Ktab:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        System.out.println("-l\t\t\t\tlist the keytab name and entries");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        System.out.println("-a <principal name> (<password>)add an entry " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                           "to the keytab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        System.out.println("-d <principal name>\t\tdelete an entry from "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                           "the keytab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        System.out.println("-k <keytab name>\t\tspecify keytab name and "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                           " path with prefix FILE:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
}