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