jdk/src/windows/classes/sun/security/krb5/internal/tools/Kinit.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3949 00603a93b589
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
 * Portions Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.ccache.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.util.Password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Kinit tool for obtaining Kerberos v5 tickets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class Kinit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private KinitOptions options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The main method is used to accept user command line input for ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Usage: kinit [-A] [-f] [-p] [-c cachename] [[-k [-t keytab_file_name]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * [principal] [password]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <li>    -A        do not include addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <li>    -f        forwardable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <li>    -p        proxiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <li>    -c        cache name (i.e., FILE://c:\temp\mykrb5cc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <li>    -k        use keytab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <li>    -t        keytab file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <li>    principal the principal name (i.e., duke@java.sun.com)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <li>    password  the principal's Kerberos password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Use java sun.security.krb5.tools.Kinit -help to bring up help menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * We currently support only file-based credentials cache to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * store the tickets obtained from the KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * By default, for all Unix platforms a cache file named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * /tmp/krb5cc_&lt;uid&gt will be generated. The &lt;uid&gt is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * numeric user identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * For all other platforms, a cache file named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * &lt;USER_HOME&gt/krb5cc_&lt;USER_NAME&gt would be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * &lt;USER_HOME&gt is obtained from <code>java.lang.System</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * property <i>user.home</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * &lt;USER_NAME&gt is obtained from <code>java.lang.System</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * property <i>user.name</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * If &lt;USER_HOME&gt is null the cache file would be stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * the current directory that the program is running from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * &lt;USER_NAME&gt is operating system's login username.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * It could be different from user's principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * For instance, on Windows NT, it could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * c:\winnt\profiles\duke\krb5cc_duke, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * which duke is the &lt;USER_NAME&gt, and c:\winnt\profile\duke is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * &lt;USER_HOME&gt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * A single user could have multiple principal names,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * but the primary principal of the credentials cache could only be one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * which means one cache file could only store tickets for one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * specific user principal. If the user switches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the principal name at the next Kinit, the cache file generated for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * new ticket would overwrite the old cache file by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * To avoid overwriting, you need to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * a different cache file name when you request a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * new ticket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * You can specify the location of the cache file by using the -c option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Kinit self = new Kinit(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            String msg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (e instanceof KrbException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                msg = ((KrbException)e).krbErrorMessage() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    ((KrbException)e).returnCodeMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                msg = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (msg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                System.err.println("Exception: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                System.out.println("Exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Constructs a new Kinit object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param args array of ticket request options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Avaiable options are: -f, -p, -c, principal, password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception RealmException if the Realm could not be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception KrbException if error occurs during Kerberos operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private Kinit(String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws IOException, RealmException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (args == null || args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            options = new KinitOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            options = new KinitOptions(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        String princName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        PrincipalName principal = options.getPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (principal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            princName = principal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            System.out.println("Principal is " + principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        char[] psswd = options.password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        EncryptionKey[] skeys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        boolean useKeytab = options.useKeytabFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (!useKeytab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (princName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    (" Can not obtain principal name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (psswd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                System.out.print("Password for " + princName + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                psswd = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    System.out.println(">>> Kinit console input " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        new String(psswd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                System.out.println(">>> Kinit using keytab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (princName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    ("Principal name must be specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            String ktabName = options.keytabFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (ktabName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                       ">>> Kinit keytab file name: " + ktabName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            // assert princName and principal are nonnull
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            skeys = EncryptionKey.acquireSecretKeys(principal, ktabName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (skeys == null || skeys.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                String msg = "No supported key found in keytab";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (princName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    msg += " for principal " + princName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw new KrbException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        KDCOptions opt = new KDCOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        setOptions(KDCOptions.FORWARDABLE, options.forwardable, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        setOptions(KDCOptions.PROXIABLE, options.proxiable, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        String realm = options.getKDCRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (realm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            realm = Config.getInstance().getDefaultRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            System.out.println(">>> Kinit realm name is " + realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        PrincipalName sname = new PrincipalName("krbtgt" + "/" + realm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                        PrincipalName.KRB_NT_SRV_INST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        sname.setRealm(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            System.out.println(">>> Creating KrbAsReq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        KrbAsReq as_req = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        HostAddresses addresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (options.getAddressOption())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                addresses = HostAddresses.getLocalAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (useKeytab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                as_req = new KrbAsReq(skeys, opt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                      principal, sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                      null, null, null, null, addresses, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                as_req = new KrbAsReq(psswd, opt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                      principal, sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                      null, null, null, null, addresses, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (KrbException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new KrbException(exc.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        KrbAsRep as_rep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            as_rep = sendASRequest(as_req, useKeytab, realm, psswd, skeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } catch (KrbException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if ((ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    System.out.println("Kinit: PREAUTH FAILED/REQ, re-send AS-REQ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                KRBError error = ke.getError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                int etype = error.getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                byte[] salt = error.getSalt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                byte[] s2kparams = error.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (useKeytab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    as_req = new KrbAsReq(skeys, true, etype, salt, s2kparams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                        opt, principal, sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                        null, null, null, null, addresses, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    as_req = new KrbAsReq(psswd, true, etype, salt, s2kparams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                        opt, principal, sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                        null, null, null, null, addresses, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                as_rep = sendASRequest(as_req, useKeytab, realm, psswd, skeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        sun.security.krb5.internal.ccache.Credentials credentials =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            as_rep.setCredentials();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // we always create a new cache and store the ticket we get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        CredentialsCache cache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            CredentialsCache.create(principal, options.cachename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
           throw new IOException("Unable to create the cache file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                 options.cachename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        cache.update(credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        cache.save();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (options.password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // Assume we're running interactively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            System.out.println("New ticket is stored in cache file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                               options.cachename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
             Arrays.fill(options.password, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // clear the password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (psswd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            Arrays.fill(psswd, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        options = null; // release reference to options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private static KrbAsRep sendASRequest(KrbAsReq as_req, boolean useKeytab,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                String realm, char[] passwd, EncryptionKey[] skeys)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        throws IOException, RealmException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            System.out.println(">>> Kinit: sending as_req to realm " + realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        String kdc = as_req.send(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            System.out.println(">>> reading response from kdc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        KrbAsRep as_rep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (useKeytab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                as_rep = as_req.getReply(skeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                as_rep = as_req.getReply(passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } catch (KrbException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (ke.returnCode() == Krb5.KRB_ERR_RESPONSE_TOO_BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                as_req.send(realm, kdc, true); // useTCP is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                if (useKeytab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    as_rep = as_req.getReply(skeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    as_rep = as_req.getReply(passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return as_rep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private static void setOptions(int flag, int option, KDCOptions opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        switch (option) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            opt.set(flag, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            opt.set(flag, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
}