jdk/src/share/classes/com/sun/security/auth/module/Krb5LoginModule.java
author weijun
Wed, 28 Sep 2011 14:21:10 +0800
changeset 10695 08c28770f82b
parent 10336 0bb1999251f8
child 13590 f7f85d7f7a82
permissions -rw-r--r--
7089889: Krb5LoginModule.login() throws an exception if used without a keytab Reviewed-by: xuelei, valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
     2
 * Copyright (c) 2000, 2011, 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: 5457
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: 5457
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: 5457
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5457
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5457
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
package com.sun.security.auth.module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.kerberos.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.security.auth.login.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.security.auth.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.jgss.krb5.Krb5Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.krb5.Credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> This <code>LoginModule</code> authenticates users using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Kerberos protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p> The configuration entry for <code>Krb5LoginModule</code> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * several options that control the authentication process and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * additions to the <code>Subject</code>'s private credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * set. Irrespective of these options, the <code>Subject</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * principal set and private credentials set are updated only when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>commit</code> is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * When <code>commit</code> is called, the <code>KerberosPrincipal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * is added to the  <code>Subject</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * principal set and <code>KerberosTicket</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * added to the <code>Subject</code>'s private credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> If the configuration entry for <code>KerberosLoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * has the option <code>storeKey</code> set to true, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <code>KerberosKey</code> will also be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * subject's private credentials. <code>KerberosKey</code>, the principal's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * key will be either obtained from the keytab or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * derived from user's password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> This <code>LoginModule</code> recognizes the <code>doNotPrompt</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * option. If set to true the user will not be prompted for the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p> The user can  specify the location of the ticket cache by using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the option <code>ticketCache</code> in the configuration entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>The user can specify the keytab location by using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the option <code>keyTab</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * in the configuration entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p> The principal name can be specified in the configuration entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * by using the option <code>principal</code>. The principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * can either be a simple user name or a service name such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <code>host/mission.eng.sun.com</code>. The principal can also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * be set using the system property <code>sun.security.krb5.principal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * This property is checked during login. If this property is not set, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * the principal name from the configuration is used. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * case where the principal property is not set and the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * entry also does not exist, the user is prompted for the name.
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
    85
 * When this property of entry is set, and <code>useTicketCache</code>
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
    86
 * is set to true, only TGT belonging to this principal is used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p> The following is a list of configuration options supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * for <code>Krb5LoginModule</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <blockquote><dt><b><code>refreshKrb5Config</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <dd> Set this to true, if you want the configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * to be refreshed before the <code>login</code> method is called.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <dt><b><code>useTicketCache</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <dd>Set this to true, if you want the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * TGT to be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * from the ticket cache. Set this option
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * to false if you do not want this module to use the ticket cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * (Default is False).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * This module will
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   102
 * search for the ticket
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * cache in the following locations:
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   104
 * On Solaris and Linux
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * it will look for the ticket cache in /tmp/krb5cc_<code>uid</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * where the uid is numeric user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * identifier. If the ticket cache is
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   108
 * not available in the above location, or if we are on a
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   109
 * Windows platform, it will look for the cache as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * {user.home}{file.separator}krb5cc_{user.name}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * You can override the ticket cache location by using
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   112
 * <code>ticketCache</code>.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   113
 * For Windows, if a ticket cannot be retrieved from the file ticket cache,
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   114
 * it will use Local Security Authority (LSA) API to get the TGT.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <dt><b><code>ticketCache</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <dd>Set this to the name of the ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * cache that  contains user's TGT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * If this is set,  <code>useTicketCache</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * must also be set to true; Otherwise a configuration error will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * be returned.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *  <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <dt><b><code>renewTGT</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <dd>Set this to true, if you want to renew
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * the TGT. If this is set, <code>useTicketCache</code> must also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * set to true; otherwise a configuration error will be returned.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <dt><b><code>doNotPrompt</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <dd>Set this to true if you do not want to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * prompted for the password
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   131
 * if credentials can not be obtained from the cache, the keytab,
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   132
 * or through shared state.(Default is false)
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   133
 * If set to true, credential must be obtained through cache, keytab,
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   134
 * or shared state. Otherwise, authentication will fail.</dd>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <dt><b><code>useKeyTab</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <dd>Set this to true if you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * want the module to get the principal's key from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * the keytab.(default value is False)
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   140
 * If <code>keytab</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * is not set then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * the module will locate the keytab from the
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   143
 * Kerberos configuration file.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   144
 * If it is not specified in the Kerberos configuration file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * then it will look for the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <code>{user.home}{file.separator}</code>krb5.keytab.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <dt><b><code>keyTab</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <dd>Set this to the file name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * keytab to get principal's secret key.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <dt><b><code>storeKey</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <dd>Set this to true to if you want the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * principal's key to be stored in the Subject's private credentials. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <dt><b><code>principal</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <dd>The name of the principal that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * be used. The principal can be a simple username such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * "<code>testuser</code>" or a service name such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * "<code>host/testhost.eng.sun.com</code>". You can use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <code>principal</code>  option to set the principal when there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * credentials for multiple principals in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <code>keyTab</code> or when you want a specific ticket cache only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * The principal can also be set using the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <code>sun.security.krb5.principal</code>. In addition, if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * system property is defined, then it will be used. If this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * is not set, then the principal name from the configuration will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * used.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * <dt><b><code>isInitiator</code></b>:</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <dd>Set this to true, if initiator. Set this to false, if acceptor only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * (Default is true).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * Note: Do not set this value to false for initiators.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * </dl></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * <p> This <code>LoginModule</code> also recognizes the following additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <code>Configuration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * options that enable you to share username and passwords across different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * authentication modules:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *    useFirstPass   if, true, this LoginModule retrieves the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *                   username and password from the module's shared state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *                   using "javax.security.auth.login.name" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *                   "javax.security.auth.login.password" as the respective
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *                   keys. The retrieved values are used for authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *                   If authentication fails, no attempt for a retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *                   is made, and the failure is reported back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *                   calling application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *    tryFirstPass   if, true, this LoginModule retrieves the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *                   the username and password from the module's shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *                   state using "javax.security.auth.login.name" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *                   "javax.security.auth.login.password" as the respective
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *                   keys.  The retrieved values are used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *                   authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *                   If authentication fails, the module uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *                   CallbackHandler to retrieve a new username
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *                   and password, and another attempt to authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *                   is made. If the authentication fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *                   the failure is reported back to the calling application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *    storePass      if, true, this LoginModule stores the username and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *                   password obtained from the CallbackHandler in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *                   modules shared state, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *                   "javax.security.auth.login.name" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *                   "javax.security.auth.login.password" as the respective
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *                   keys.  This is not performed if existing values already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *                   exist for the username and password in the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *                   state, or if authentication fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   212
 *    clearPass      if, true, this LoginModule clears the
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   213
 *                   username and password stored in the module's shared
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   214
 *                   state  after both phases of authentication
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   215
 *                   (login and commit) have completed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * </pre>
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   217
 * <p>If the principal system property or key is already provided, the value of
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   218
 * "javax.security.auth.login.name" in the shared state is ignored.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   219
 * <p>When multiple mechanisms to retrieve a ticket or key is provided, the
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   220
 * preference order looks like this:
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   221
 * <ol>
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   222
 * <li>ticket cache
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   223
 * <li>keytab
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   224
 * <li>shared state
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   225
 * <li>user prompt
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   226
 * </ol>
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   227
 * <p>Note that if any step fails, it will fallback to the next step.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   228
 * There's only one exception, it the shared state step fails and
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   229
 * <code>useFirstPass</code>=true, no user prompt is made.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * <p>Examples of some configuration values for Krb5LoginModule in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * JAAS config file and the results are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * <p> <code>doNotPrompt</code>=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * </ul>
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   235
 * <p> This is an illegal combination since none of <code>useTicketCache</code>,
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   236
 * <code>useKeyTab</code>, <code>useFirstPass</code> and <code>tryFirstPass</code>
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   237
 * is set and the user can not be prompted for the password.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *<ul>
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   239
 * <p> <code>ticketCache</code> = &lt;filename&gt;;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * <p> This is an illegal combination since <code>useTicketCache</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * is not set to true and the ticketCache is set. A configuration error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * <p> <code>renewTGT</code>=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * <p> This is an illegal combination since <code>useTicketCache</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * not set to true and renewTGT is set. A configuration error will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * <p> <code>storeKey</code>=true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * <code>useTicketCache</code> = true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * <code>doNotPrompt</code>=true;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * <p> This is an illegal combination since  <code>storeKey</code> is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * true but the key can not be obtained either by prompting the user or from
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   256
 * the keytab, or from the shared state. A configuration error will occur.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * <ul>
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   258
 * <p>  <code>keyTab</code> = &lt;filename&gt; <code>doNotPrompt</code>=true ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * <p>This is an illegal combination since useKeyTab is not set to true and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * the keyTab is set. A configuration error will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * <p> <code>debug=true </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * <p> Prompt the user for the principal name and the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * Use the authentication exchange to get TGT from the KDC and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * populate the <code>Subject</code> with the principal and TGT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * Output debug messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * <p> <code>useTicketCache</code> = true <code>doNotPrompt</code>=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * <p>Check the default cache for TGT and populate the <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * with the principal and TGT. If the TGT is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * do not prompt the user, instead fail the authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * <ul>
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   276
 * <p><code>principal</code>=&lt;name&gt;<code>useTicketCache</code> = true
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * <code>doNotPrompt</code>=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * <p> Get the TGT from the default cache for the principal and populate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * Subject's principal and private creds set. If ticket cache is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * not available or does not contain the principal's TGT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * authentication will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * <p> <code>useTicketCache</code> = true
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   285
 * <code>ticketCache</code>=&lt;file name&gt;<code>useKeyTab</code> = true
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   286
 * <code> keyTab</code>=&lt;keytab filename&gt;
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   287
 * <code>principal</code> = &lt;principal name&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * <code>doNotPrompt</code>=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 * <p>  Search the cache for the principal's TGT. If it is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * use the key in the keytab to perform authentication exchange with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * KDC and acquire the TGT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * The Subject will be populated with the principal and the TGT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 * If the key is not available or valid then authentication will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 * <p><code>useTicketCache</code> = true
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   297
 * <code>ticketCache</code>=&lt;file name&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * <p> The TGT will be obtained from the cache specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * The Kerberos principal name used will be the principal name in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * the Ticket cache. If the TGT is not available in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * ticket cache the user will be prompted for the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * and the password. The TGT will be obtained using the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * exchange with the KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * The Subject will be populated with the TGT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * <p> <code>useKeyTab</code> = true
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   308
 * <code>keyTab</code>=&lt;keytab filename&gt;
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   309
 * <code>principal</code>= &lt;principal name&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * <code>storeKey</code>=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * <p>  The key for the principal will be retrieved from the keytab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * If the key is not available in the keytab the user will be prompted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 * for the principal's password. The Subject will be populated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * with the principal's key either from the keytab or derived from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * password entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * <p> <code>useKeyTab</code> = true
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   319
 * <code>keyTab</code>=&lt;keytabname&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 * <code>storeKey</code>=true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 * <code>doNotPrompt</code>=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * <p>The user will be prompted for the service principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * If the principal's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 * longterm key is available in the keytab , it will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 * Subject's private credentials. An authentication exchange will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * attempted with the principal name and the key from the Keytab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 * If successful the TGT will be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * Subject's private credentials set. Otherwise the authentication will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 *<ul>
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   332
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 * <code>useTicketCache</code>=true
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   334
 * <code>ticketCache</code>=&lt;file name&gt;;
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   335
 * <code>useKeyTab</code> = true
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   336
 * <code>keyTab</code>=&lt;file name&gt; <code>storeKey</code>=true
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   337
 * <code>principal</code>= &lt;principal name&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 *</ul>
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   339
 * <p>
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   340
 * The client's TGT will be retrieved from the ticket cache and added to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 * <code>Subject</code>'s private credentials. If the TGT is not available
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   342
 * in the ticket cache, or the TGT's client name does not match the principal
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   343
 * name, Java will use a secret key to obtain the TGT using the authentication
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
 * exchange and added to the Subject's private credentials.
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   345
 * This secret key will be first retrieved from the keytab. If the key
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   346
 * is not available, the user will be prompted for the password. In either
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   347
 * case, the key derived from the password will be added to the
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   348
 * Subject's private credentials set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 * <p><code>isInitiator</code> = false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 * <p>Configured to act as acceptor only, credentials are not acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 * via AS exchange. For acceptors only, set this value to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 * For initiators, do not set this value to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 * <p><code>isInitiator</code> = true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
 * <p>Configured to act as initiator, credentials are acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
 * via AS exchange. For initiators, set this value to true, or leave this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
 * option unset, in which case default value (true) will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
public class Krb5LoginModule implements LoginModule {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    // initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private CallbackHandler callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9499
diff changeset
   370
    private Map<String, Object> sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private Map<String, ?> options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    // configurable option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private boolean storeKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private boolean doNotPrompt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private boolean useTicketCache = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private boolean useKeyTab = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private String ticketCacheName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private String keyTabName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private String princName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    private boolean useFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    private boolean tryFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private boolean storePass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private boolean clearPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private boolean refreshKrb5Config = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private boolean renewTGT = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    // specify if initiator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    // perform authentication exchange if initiator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private boolean isInitiator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    // the authentication status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private boolean succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private boolean commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private String username;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   398
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   399
    // Encryption keys calculated from password. Assigned when storekey == true
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   400
    // and useKeyTab == false (or true but not found)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private EncryptionKey[] encKeys = null;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   402
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   403
    KeyTab ktab = null;
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   404
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private Credentials cred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    private PrincipalName principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private KerberosPrincipal kerbClientPrinc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private KerberosTicket kerbTicket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private KerberosKey[] kerbKeys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private StringBuffer krb5PrincName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private char[] password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private static final String NAME = "javax.security.auth.login.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private static final String PWD = "javax.security.auth.login.password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static final java.util.ResourceBundle rb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        java.util.ResourceBundle.getBundle("sun.security.util.AuthResources");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Initialize this <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param subject the <code>Subject</code> to be authenticated. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param callbackHandler a <code>CallbackHandler</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *                  communication with the end user (prompting for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *                  usernames and passwords, for example). <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param sharedState shared <code>LoginModule</code> state. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param options options specified in the login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *                  <code>Configuration</code> for this particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *                  <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9499
diff changeset
   435
    // Unchecked warning from (Map<String, Object>)sharedState is safe
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9499
diff changeset
   436
    // since javax.security.auth.login.LoginContext passes a raw HashMap.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9499
diff changeset
   437
    // Unchecked warnings from options.get(String) are safe since we are
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9499
diff changeset
   438
    // passing known keys.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9499
diff changeset
   439
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public void initialize(Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                           CallbackHandler callbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                           Map<String, ?> sharedState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                           Map<String, ?> options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        this.callbackHandler = callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9499
diff changeset
   447
        this.sharedState = (Map<String, Object>)sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // initialize any configured options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        debug = "true".equalsIgnoreCase((String)options.get("debug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        storeKey = "true".equalsIgnoreCase((String)options.get("storeKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        doNotPrompt = "true".equalsIgnoreCase((String)options.get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                              ("doNotPrompt"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        useTicketCache = "true".equalsIgnoreCase((String)options.get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                                 ("useTicketCache"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        useKeyTab = "true".equalsIgnoreCase((String)options.get("useKeyTab"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        ticketCacheName = (String)options.get("ticketCache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        keyTabName = (String)options.get("keyTab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        princName = (String)options.get("principal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        refreshKrb5Config =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            "true".equalsIgnoreCase((String)options.get("refreshKrb5Config"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        renewTGT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            "true".equalsIgnoreCase((String)options.get("renewTGT"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // check isInitiator value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        String isInitiatorValue = ((String)options.get("isInitiator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (isInitiatorValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // use default, if value not set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            isInitiator = "true".equalsIgnoreCase(isInitiatorValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        tryFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            "true".equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            ((String)options.get("tryFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        useFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            "true".equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            ((String)options.get("useFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        storePass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            "true".equalsIgnoreCase((String)options.get("storePass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        clearPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            "true".equalsIgnoreCase((String)options.get("clearPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            System.out.print("Debug is  " + debug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                             + " storeKey " + storeKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                             + " useTicketCache " + useTicketCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                             + " useKeyTab " + useKeyTab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                             + " doNotPrompt " + doNotPrompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                             + " ticketCache is " + ticketCacheName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                             + " isInitiator " + isInitiator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                             + " KeyTab is " + keyTabName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                             + " refreshKrb5Config is " + refreshKrb5Config
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                             + " principal is " + princName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                             + " tryFirstPass is " + tryFirstPass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                             + " useFirstPass is " + useFirstPass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                             + " storePass is " + storePass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                             + " clearPass is " + clearPass + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Authenticate the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @return true in all cases since this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @exception FailedLoginException if the authentication fails. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @exception LoginException if this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *          is unable to perform the authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public boolean login() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        validateConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (refreshKrb5Config) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    System.out.println("Refreshing Kerberos configuration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                sun.security.krb5.Config.refresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            } catch (KrbException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                LoginException le = new LoginException(ke.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                le.initCause(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        String principalProperty = System.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            ("sun.security.krb5.principal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (principalProperty != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            krb5PrincName = new StringBuffer(principalProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (princName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                krb5PrincName = new StringBuffer(princName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (tryFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    System.out.println("\t\t[Krb5LoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                       "authentication succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                // authentication failed -- try again below by prompting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    System.out.println("\t\t[Krb5LoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                       "tryFirstPass failed with:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                       le.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        } else if (useFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            } catch (LoginException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                // authentication failed -- clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    System.out.println("\t\t[Krb5LoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                       "authentication failed \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                       e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // attempt the authentication by getting the username and pwd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // by prompting or configuration i.e. not from shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            attemptAuthentication(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        } catch (LoginException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            // authentication failed -- clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                System.out.println("\t\t[Krb5LoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                   "authentication failed \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * process the configuration options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Get the TGT either out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * cache or from the KDC using the password entered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Check the  permission before getting the TGT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private void attemptAuthentication(boolean getPasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * Check the creds cache to see whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         * we have TGT for this client principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (krb5PrincName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                principal = new PrincipalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    (krb5PrincName.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                     PrincipalName.KRB_NT_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                LoginException le = new LoginException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if (useTicketCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                // ticketCacheName == null implies the default cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    System.out.println("Acquire TGT from Cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                cred  = Credentials.acquireTGTFromCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    (principal, ticketCacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                if (cred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    // check to renew credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    if (!isCurrent(cred)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        if (renewTGT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                            cred = renewCredentials(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                            // credentials have expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                            cred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                            if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                System.out.println("Credentials are" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                                " no longer valid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (cred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                   // get the principal name from the ticket cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                   if (principal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        principal = cred.getClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    System.out.println("Principal is " + principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                            ("null credentials from Ticket Cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            // cred = null indicates that we didn't get the creds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            // from the cache or useTicketCache was false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                // We need the principal name whether we use keytab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                // or AS Exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                if (principal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    promptForName(getPasswdFromSharedState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    principal = new PrincipalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        (krb5PrincName.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                         PrincipalName.KRB_NT_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   676
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   677
                /*
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   678
                 * Before dynamic KeyTab support (6894072), here we check if
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   679
                 * the keytab contains keys for the principal. If no, keytab
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   680
                 * will not be used and password is prompted for.
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   681
                 *
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   682
                 * After 6894072, we normally don't check it, and expect the
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   683
                 * keys can be populated until a real connection is made. The
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   684
                 * check is still done when isInitiator == true, where the keys
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   685
                 * will be used right now.
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   686
                 *
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   687
                 * Probably tricky relations:
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   688
                 *
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   689
                 * useKeyTab is config flag, but when it's true but the ktab
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   690
                 * does not contains keys for principal, we would use password
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   691
                 * and keep the flag unchanged (for reuse?). In this method,
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   692
                 * we use (ktab != null) to check whether keytab is used.
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   693
                 * After this method (and when storeKey == true), we use
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   694
                 * (encKeys == null) to check.
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   695
                 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                if (useKeyTab) {
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   697
                    ktab = (keyTabName == null)
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   698
                                ? KeyTab.getInstance()
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   699
                                : KeyTab.getInstance(new File(keyTabName));
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   700
                    if (isInitiator) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   701
                        if (Krb5Util.keysFromJavaxKeyTab(ktab, principal).length
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   702
                                == 0) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   703
                            ktab = null;
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   704
                            if (debug) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   705
                                System.out.println
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   706
                                    ("Key for the principal " +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   707
                                     principal  +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   708
                                     " not available in " +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   709
                                     ((keyTabName == null) ?
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   710
                                      "default key tab" : keyTabName));
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   711
                            }
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   712
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   715
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   716
                KrbAsReqBuilder builder;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   717
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   718
                if (ktab == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    promptForPass(getPasswdFromSharedState);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   720
                    builder = new KrbAsReqBuilder(principal, password);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    if (isInitiator) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   722
                        // XXX Even if isInitiator=false, it might be
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   723
                        // better to do an AS-REQ so that keys can be
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   724
                        // updated with PA info
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   725
                        cred = builder.action().getCreds();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   727
                    if (storeKey) {
10695
08c28770f82b 7089889: Krb5LoginModule.login() throws an exception if used without a keytab
weijun
parents: 10336
diff changeset
   728
                        encKeys = builder.getKeys(isInitiator);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   729
                        // When encKeys is empty, the login actually fails.
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   730
                        // For compatibility, exception is thrown in commit().
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   731
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                } else {
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   733
                    builder = new KrbAsReqBuilder(principal, ktab);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    if (isInitiator) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   735
                        cred = builder.action().getCreds();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7179
diff changeset
   738
                builder.destroy();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    System.out.println("principal is " + principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    HexDumpEncoder hd = new HexDumpEncoder();
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   743
                    if (ktab != null) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   744
                        System.out.println("Will use keytab");
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   745
                    } else if (storeKey) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   746
                        for (int i = 0; i < encKeys.length; i++) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   747
                            System.out.println("EncryptionKey: keyType=" +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   748
                                encKeys[i].getEType() +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   749
                                " keyBytes (hex dump)=" +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   750
                                hd.encodeBuffer(encKeys[i].getBytes()));
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   751
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                // we should hava a non-null cred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                if (isInitiator && (cred == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        ("TGT Can not be obtained from the KDC ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            LoginException le = new LoginException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            LoginException ie = new LoginException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            ie.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private void promptForName(boolean getPasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        krb5PrincName = new StringBuffer("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        if (getPasswdFromSharedState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            // use the name saved by the first module in the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            username = (String)sharedState.get(NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    ("username from shared state is " + username + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (username == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    ("username from shared state is null\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    ("Username can not be obtained from sharedstate ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    ("username from shared state is " + username + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            if (username != null && username.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                krb5PrincName.insert(0, username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (doNotPrompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                ("Unable to obtain Princpal Name for authentication ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (callbackHandler == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                throw new LoginException("No CallbackHandler "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                         + "available "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                         + "to garner authentication "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                         + "information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                String defUsername = System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                Callback[] callbacks = new Callback[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                MessageFormat form = new MessageFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                                       rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   814
                                       "Kerberos.username.defUsername."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                Object[] source =  {defUsername};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                callbacks[0] = new NameCallback(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                callbackHandler.handle(callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                username = ((NameCallback)callbacks[0]).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                if (username == null || username.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    username = defUsername;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                krb5PrincName.insert(0, username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                throw new LoginException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            } catch (UnsupportedCallbackException uce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    (uce.getMessage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                     +" not available to garner "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                     +" authentication information "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                     +" from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    private void promptForPass(boolean getPasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (getPasswdFromSharedState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            // use the password saved by the first module in the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            password = (char[])sharedState.get(PWD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        ("Password from shared state is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    ("Password can not be obtained from sharedstate ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    ("password is " + new String(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if (doNotPrompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                ("Unable to obtain password from user\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (callbackHandler == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                throw new LoginException("No CallbackHandler "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                                         + "available "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                         + "to garner authentication "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                                         + "information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                Callback[] callbacks = new Callback[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                String userName = krb5PrincName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                MessageFormat form = new MessageFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                         rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   869
                                         "Kerberos.password.for.username."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                Object[] source = {userName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                callbacks[0] = new PasswordCallback(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                                                    form.format(source),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                                                    false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                callbackHandler.handle(callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                char[] tmpPassword = ((PasswordCallback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                                      callbacks[0]).getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                if (tmpPassword == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    // treat a NULL password as an empty password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    tmpPassword = new char[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                password = new char[tmpPassword.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                System.arraycopy(tmpPassword, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                                 password, 0, tmpPassword.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                ((PasswordCallback)callbacks[0]).clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                // clear tmpPassword
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                for (int i = 0; i < tmpPassword.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                    tmpPassword[i] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                tmpPassword = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    System.out.println("\t\t[Krb5LoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                       "user entered username: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                       krb5PrincName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                throw new LoginException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            } catch (UnsupportedCallbackException uce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                throw new LoginException(uce.getMessage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                                         +" not available to garner "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                         +" authentication information "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                         + "from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    private void validateConfiguration() throws LoginException {
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   909
        if (doNotPrompt && !useTicketCache && !useKeyTab
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   910
                && !tryFirstPass && !useFirstPass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                ("Configuration Error"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                 + " - either doNotPrompt should be "
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   914
                 + " false or at least one of useTicketCache, "
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   915
                 + " useKeyTab, tryFirstPass and useFirstPass"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                 + " should be true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if (ticketCacheName != null && !useTicketCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                ("Configuration Error "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                 + " - useTicketCache should be set "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                 + "to true to use the ticket cache"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                 + ticketCacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (keyTabName != null & !useKeyTab)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                ("Configuration Error - useKeyTab should be set to true "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                 + "to use the keytab" + keyTabName);
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   927
        if (storeKey && doNotPrompt && !useKeyTab
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   928
                && !tryFirstPass && !useFirstPass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            throw new LoginException
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   930
                ("Configuration Error - either doNotPrompt should be set to "
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   931
                 + " false or at least one of tryFirstPass, useFirstPass "
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 793
diff changeset
   932
                 + "or useKeyTab must be set to true for storeKey option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        if (renewTGT && !useTicketCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                ("Configuration Error"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                 + " - either useTicketCache should be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                 + " true or renewTGT should be false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    private boolean isCurrent(Credentials creds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        Date endTime = creds.getEndTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (endTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            return (System.currentTimeMillis() <= endTime.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    private Credentials renewCredentials(Credentials creds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        Credentials lcreds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            if (!creds.isRenewable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                throw new RefreshFailedException("This ticket" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                                " is not renewable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if (System.currentTimeMillis() > cred.getRenewTill().getTime())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                throw new RefreshFailedException("This ticket is past "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                                             + "its last renewal time.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            lcreds = creds.renew();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                System.out.println("Renewed Kerberos Ticket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            lcreds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                System.out.println("Ticket could not be renewed : "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                                + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        return lcreds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * overall authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * LoginModules succeeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * succeeded (checked by retrieving the private state saved by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * <code>login</code> method), then this method associates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * <code>Krb5Principal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * with the <code>Subject</code> located in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <code>LoginModule</code>. It adds Kerberos Credentials to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *  the Subject's private credentials set. If this LoginModule's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * authentication attempted failed, then this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @exception LoginException if the commit fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @return true if this LoginModule's own login and commit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *          attempts succeeded, or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    public boolean commit() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         * Let us add the Krb5 Creds to the Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         * private credentials. The credentials are of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * KerberosKey or KerberosTicket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            if (isInitiator && (cred == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                throw new LoginException("Null Client Credential");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                cleanKerberosCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                throw new LoginException("Subject is Readonly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
             * Add the Principal (authenticated identity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
             * to the Subject's principal set and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
             * add the credentials (TGT or Service key) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
             * Subject's private credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            Set<Object> privCredSet =  subject.getPrivateCredentials();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            Set<java.security.Principal> princSet  = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            kerbClientPrinc = new KerberosPrincipal(principal.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            // create Kerberos Ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            if (isInitiator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                kerbTicket = Krb5Util.credsToTicket(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1032
            if (storeKey && encKeys != null) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1033
                if (encKeys.length == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                    succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                    throw new LoginException("Null Server Key ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                kerbKeys = new KerberosKey[encKeys.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                for (int i = 0; i < encKeys.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    Integer temp = encKeys[i].getKeyVersionNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    kerbKeys[i] = new KerberosKey(kerbClientPrinc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                                          encKeys[i].getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                                          encKeys[i].getEType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                                          (temp == null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                                          0: temp.intValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1049
            // Let us add the kerbClientPrinc,kerbTicket and KeyTab/KerbKey (if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            // storeKey is true)
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1051
            if (!princSet.contains(kerbClientPrinc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                princSet.add(kerbClientPrinc);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1053
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            // add the TGT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            if (kerbTicket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                if (!privCredSet.contains(kerbTicket))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    privCredSet.add(kerbTicket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            if (storeKey) {
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1062
                if (encKeys == null) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1063
                    if (!privCredSet.contains(ktab)) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1064
                        privCredSet.add(ktab);
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1065
                        // Compatibility; also add keys to privCredSet
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1066
                        for (KerberosKey key: ktab.getKeys(kerbClientPrinc)) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1067
                            privCredSet.add(new Krb5Util.KeysFromKeyTab(key));
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1068
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                    }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1070
                } else {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1071
                    for (int i = 0; i < kerbKeys.length; i ++) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1072
                        if (!privCredSet.contains(kerbKeys[i])) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1073
                            privCredSet.add(kerbKeys[i]);
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1074
                        }
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1075
                        encKeys[i].destroy();
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1076
                        encKeys[i] = null;
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1077
                        if (debug) {
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1078
                            System.out.println("Added server's key"
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1079
                                            + kerbKeys[i]);
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1080
                            System.out.println("\t\t[Krb5LoginModule] " +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1081
                                           "added Krb5Principal  " +
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1082
                                           kerbClientPrinc.toString()
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1083
                                           + " to Subject");
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1084
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        commitSucceeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            System.out.println("Commit Succeeded \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * overall authentication failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * LoginModules did not succeed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * succeeded (checked by retrieving the private state saved by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * <code>login</code> and <code>commit</code> methods),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * then this method cleans up any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @exception LoginException if the abort fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @return false if this LoginModule's own login and/or commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *          failed, and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    public boolean abort() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        } else if (succeeded == true && commitSucceeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            // login succeeded but overall authentication failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            cleanKerberosCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            // overall authentication succeeded and commit succeeded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            // but someone else's commit failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * Logout the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * <p> This method removes the <code>Krb5Principal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * that was added by the <code>commit</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @exception LoginException if the logout fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @return true in all cases since this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    public boolean logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            System.out.println("\t\t[Krb5LoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                "Entering logout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            cleanKerberosCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            throw new LoginException("Subject is Readonly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        subject.getPrincipals().remove(kerbClientPrinc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
           // Let us remove all Kerberos credentials stored in the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        Iterator<Object> it = subject.getPrivateCredentials().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            Object o = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            if (o instanceof KerberosTicket ||
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1160
                    o instanceof KerberosKey ||
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1161
                    o instanceof KeyTab) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        // clean the kerberos ticket and keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        cleanKerberosCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            System.out.println("\t\t[Krb5LoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                               "logged out Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * Clean Kerberos credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    private void cleanKerberosCred() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        // Clean the ticket and server key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            if (kerbTicket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                kerbTicket.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            if (kerbKeys != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                for (int i = 0; i < kerbKeys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                    kerbKeys[i].destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        } catch (DestroyFailedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                ("Destroy Failed on Kerberos Private Credentials");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        kerbTicket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        kerbKeys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        kerbClientPrinc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Clean out the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    private void cleanState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        // save input as shared state only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        if (succeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            if (storePass &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                !sharedState.containsKey(NAME) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                !sharedState.containsKey(PWD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                sharedState.put(NAME, username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                sharedState.put(PWD, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            }
793
8183c2b0985f 6716534: Krb5LoginModule has not cleaned temp info between authentication attempts
weijun
parents: 2
diff changeset
  1213
        } else {
8183c2b0985f 6716534: Krb5LoginModule has not cleaned temp info between authentication attempts
weijun
parents: 2
diff changeset
  1214
            // remove temp results for the next try
8183c2b0985f 6716534: Krb5LoginModule has not cleaned temp info between authentication attempts
weijun
parents: 2
diff changeset
  1215
            encKeys = null;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
  1216
            ktab = null;
793
8183c2b0985f 6716534: Krb5LoginModule has not cleaned temp info between authentication attempts
weijun
parents: 2
diff changeset
  1217
            principal = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        username = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        if (krb5PrincName != null && krb5PrincName.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            krb5PrincName.delete(0, krb5PrincName.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        krb5PrincName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        if (clearPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            sharedState.remove(NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            sharedState.remove(PWD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
}