src/java.security.jgss/windows/classes/sun/security/krb5/internal/tools/Kinit.java
author mgronlun
Sun, 15 Sep 2019 15:31:04 +0200
branchJEP-349-branch
changeset 58158 369ebc2a11c2
parent 47216 71c04702a3d5
permissions -rw-r--r--
updates after merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13247
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    23
 * questions.
2
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
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
    33
import java.io.File;
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 java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.Password;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
    40
import javax.security.auth.kerberos.KeyTab;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Kinit tool for obtaining Kerberos v5 tickets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class Kinit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private KinitOptions options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The main method is used to accept user command line input for ticket
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
    55
     * request. Read {@link KinitOptions#printHelp} for usages or call
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
    56
     *    java sun.security.krb5.internal.tools.Kinit -help
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
    57
     * to bring up help menu.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * We currently support only file-based credentials cache to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * store the tickets obtained from the KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * By default, for all Unix platforms a cache file named
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    62
     * {@code /tmp/krb5cc_<uid>} will be generated. The {@code <uid>} is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * numeric user identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * For all other platforms, a cache file named
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    65
     * {@code <USER_HOME>/krb5cc_<USER_NAME>} would be generated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <p>
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    67
     * {@code <USER_HOME>} is obtained from {@code java.lang.System}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * property <i>user.home</i>.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    69
     * {@code <USER_NAME>} is obtained from {@code java.lang.System}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * property <i>user.name</i>.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    71
     * If {@code <USER_HOME>} is null the cache file would be stored in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * the current directory that the program is running from.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    73
     * {@code <USER_NAME>} is operating system's login username.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * It could be different from user's principal name.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    75
     * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * For instance, on Windows NT, it could be
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    77
     * {@code c:\winnt\profiles\duke\krb5cc_duke}, in
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    78
     * which duke is the {@code <USER_NAME>} and {@code c:\winnt\profile\duke} is the
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    79
     * {@code <USER_HOME>}.
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    80
     * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * A single user could have multiple principal names,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * but the primary principal of the credentials cache could only be one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * which means one cache file could only store tickets for one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * specific user principal. If the user switches
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * the principal name at the next Kinit, the cache file generated for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * new ticket would overwrite the old cache file by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * To avoid overwriting, you need to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * a different cache file name when you request a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * new ticket.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 27946
diff changeset
    90
     * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * You can specify the location of the cache file by using the -c option
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Kinit self = new Kinit(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            String msg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (e instanceof KrbException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                msg = ((KrbException)e).krbErrorMessage() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    ((KrbException)e).returnCodeMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                msg = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (msg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                System.err.println("Exception: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.out.println("Exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Constructs a new Kinit object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param args array of ticket request options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Avaiable options are: -f, -p, -c, principal, password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @exception RealmException if the Realm could not be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception KrbException if error occurs during Kerberos operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private Kinit(String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws IOException, RealmException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (args == null || args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            options = new KinitOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            options = new KinitOptions(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   132
        switch (options.action) {
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   133
            case 1:
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   134
                acquire();
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   135
                break;
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   136
            case 2:
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   137
                renew();
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   138
                break;
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   139
            default:
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   140
                throw new KrbException("kinit does not support action "
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   141
                        + options.action);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   142
        }
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   143
    }
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   144
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   145
    private void renew()
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   146
            throws IOException, RealmException, KrbException {
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   147
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   148
        PrincipalName principal = options.getPrincipal();
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   149
        String realm = principal.getRealmAsString();
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   150
        CredentialsCache cache = CredentialsCache.getInstance(options.cachename);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   151
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   152
        if (cache == null) {
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   153
            throw new IOException("Unable to find existing cache file " +
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   154
                    options.cachename);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   155
        }
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   156
        sun.security.krb5.internal.ccache.Credentials credentials =
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   157
                cache.getCreds(PrincipalName.tgsService(realm, realm));
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   158
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   159
        credentials = credentials.setKrbCreds()
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   160
                .renew()
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   161
                .toCCacheCreds();
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   162
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   163
        cache = CredentialsCache.create(principal, options.cachename);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   164
        if (cache == null) {
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   165
            throw new IOException("Unable to create the cache file " +
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   166
                    options.cachename);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   167
        }
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   168
        cache.update(credentials);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   169
        cache.save();
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   170
    }
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   171
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   172
    private void acquire()
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   173
            throws IOException, RealmException, KrbException {
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   174
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        String princName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        PrincipalName principal = options.getPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (principal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            princName = principal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   180
        KrbAsReqBuilder builder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            System.out.println("Principal is " + principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        char[] psswd = options.password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        boolean useKeytab = options.useKeytabFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (!useKeytab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (princName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    (" Can not obtain principal name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (psswd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                System.out.print("Password for " + princName + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                psswd = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    System.out.println(">>> Kinit console input " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        new String(psswd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   200
            builder = new KrbAsReqBuilder(principal, psswd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                System.out.println(">>> Kinit using keytab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (princName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    ("Principal name must be specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            String ktabName = options.keytabFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (ktabName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                       ">>> Kinit keytab file name: " + ktabName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   217
            builder = new KrbAsReqBuilder(principal, ktabName == null
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   218
                    ? KeyTab.getInstance()
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   219
                    : KeyTab.getInstance(new File(ktabName)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        KDCOptions opt = new KDCOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        setOptions(KDCOptions.FORWARDABLE, options.forwardable, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        setOptions(KDCOptions.PROXIABLE, options.proxiable, opt);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   225
        builder.setOptions(opt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        String realm = options.getKDCRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (realm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            realm = Config.getInstance().getDefaultRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            System.out.println(">>> Kinit realm name is " + realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 9499
diff changeset
   235
        PrincipalName sname = PrincipalName.tgsService(realm, realm);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   236
        builder.setTarget(sname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            System.out.println(">>> Creating KrbAsReq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   242
        if (options.getAddressOption())
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   243
            builder.setAddresses(HostAddresses.getLocalAddresses());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   245
        builder.setTill(options.lifetime);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   246
        builder.setRTime(options.renewable_lifetime);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   247
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   248
        builder.action();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        sun.security.krb5.internal.ccache.Credentials credentials =
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   251
            builder.getCCreds();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   252
        builder.destroy();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   253
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // we always create a new cache and store the ticket we get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        CredentialsCache cache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            CredentialsCache.create(principal, options.cachename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
           throw new IOException("Unable to create the cache file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                 options.cachename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        cache.update(credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        cache.save();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (options.password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // Assume we're running interactively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            System.out.println("New ticket is stored in cache file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                               options.cachename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
             Arrays.fill(options.password, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // clear the password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (psswd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            Arrays.fill(psswd, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        options = null; // release reference to options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static void setOptions(int flag, int option, KDCOptions opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        switch (option) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            opt.set(flag, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            opt.set(flag, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}