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