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