jdk/src/jdk.security.auth/share/classes/com/sun/security/auth/module/UnixLoginModule.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 24271 jdk/src/share/classes/com/sun/security/auth/module/UnixLoginModule.java@19000122bb5e
child 30044 bab15bbe2ca3
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20742
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
package com.sun.security.auth.module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.security.auth.login.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.security.auth.UnixPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.security.auth.UnixNumericUserPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.security.auth.UnixNumericGroupPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p> This <code>LoginModule</code> imports a user's Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>Principal</code> information (<code>UnixPrincipal</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>UnixNumericUserPrincipal</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * and <code>UnixNumericGroupPrincipal</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and associates them with the current <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> This LoginModule recognizes the debug option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * If set to true in the login Configuration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * debug messages will be output to the output stream, System.out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
20742
4ae78e8060d6 8008662: Add @jdk.Exported to JDK-specific/exported APIs
alanb
parents: 9035
diff changeset
    50
@jdk.Exported
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class UnixLoginModule implements LoginModule {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private CallbackHandler callbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Map<String, ?> sharedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Map<String, ?> options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // configurable option
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // UnixSystem to retrieve underlying system info
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private UnixSystem ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // the authentication status
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Underlying system info
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private UnixPrincipal userPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private UnixNumericUserPrincipal UIDPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private UnixNumericGroupPrincipal GIDPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private LinkedList<UnixNumericGroupPrincipal> supplementaryGroups =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
    74
                new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Initialize this <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param subject the <code>Subject</code> to be authenticated. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param callbackHandler a <code>CallbackHandler</code> for communicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *                  with the end user (prompting for usernames and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *                  passwords, for example). <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param sharedState shared <code>LoginModule</code> state. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param options options specified in the login
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *                  <code>Configuration</code> for this particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *                  <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void initialize(Subject subject, CallbackHandler callbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                           Map<String,?> sharedState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                           Map<String,?> options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.callbackHandler = callbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.sharedState = sharedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // initialize any configured options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        debug = "true".equalsIgnoreCase((String)options.get("debug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Authenticate the user (first phase).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p> The implementation of this method attempts to retrieve the user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Unix <code>Subject</code> information by making a native Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * system call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @exception FailedLoginException if attempts to retrieve the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *          system information fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return true in all cases (this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *          should not be ignored).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public boolean login() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        long[] unixGroups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
24271
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   125
        try {
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   126
            ss = new UnixSystem();
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   127
        } catch (UnsatisfiedLinkError ule) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new FailedLoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                ("Failed in attempt to import " +
24271
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   131
                                "the underlying system identity information" +
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   132
                                " on " + System.getProperty("os.name"));
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   133
        }
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   134
        userPrincipal = new UnixPrincipal(ss.getUsername());
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   135
        UIDPrincipal = new UnixNumericUserPrincipal(ss.getUid());
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   136
        GIDPrincipal = new UnixNumericGroupPrincipal(ss.getGid(), true);
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   137
        if (ss.getGroups() != null && ss.getGroups().length > 0) {
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   138
            unixGroups = ss.getGroups();
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   139
            for (int i = 0; i < unixGroups.length; i++) {
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   140
                UnixNumericGroupPrincipal ngp =
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   141
                    new UnixNumericGroupPrincipal
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   142
                    (unixGroups[i], false);
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   143
                if (!ngp.getName().equals(GIDPrincipal.getName()))
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   144
                    supplementaryGroups.add(ngp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
24271
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   146
        }
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   147
        if (debug) {
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   148
            System.out.println("\t\t[UnixLoginModule]: " +
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   149
                    "succeeded importing info: ");
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   150
            System.out.println("\t\t\tuid = " + ss.getUid());
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   151
            System.out.println("\t\t\tgid = " + ss.getGid());
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   152
            unixGroups = ss.getGroups();
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   153
            for (int i = 0; i < unixGroups.length; i++) {
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   154
                System.out.println("\t\t\tsupp gid = " + unixGroups[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
24271
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   157
        succeeded = true;
19000122bb5e 8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents: 23010
diff changeset
   158
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Commit the authentication (second phase).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * overall authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * succeeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * succeeded (the importing of the Unix authentication information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * succeeded), then this method associates the Unix Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * with the <code>Subject</code> currently tied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <code>LoginModule</code>.  If this LoginModule's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * authentication attempted failed, then this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception LoginException if the commit fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return true if this LoginModule's own login and commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *          succeeded, or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public boolean commit() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    "did not add any Principals to Subject " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    "because own authentication failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    ("commit Failed: Subject is Readonly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (!subject.getPrincipals().contains(userPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                subject.getPrincipals().add(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (!subject.getPrincipals().contains(UIDPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                subject.getPrincipals().add(UIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (!subject.getPrincipals().contains(GIDPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                subject.getPrincipals().add(GIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            for (int i = 0; i < supplementaryGroups.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (!subject.getPrincipals().contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    (supplementaryGroups.get(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    subject.getPrincipals().add(supplementaryGroups.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    "added UnixPrincipal,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                System.out.println("\t\t\t\tUnixNumericUserPrincipal,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                System.out.println("\t\t\t\tUnixNumericGroupPrincipal(s),");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                System.out.println("\t\t\t to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            commitSucceeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Abort the authentication (second phase).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * overall authentication failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * did not succeed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <p> This method cleans up any state that was originally saved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * as part of the authentication attempt from the <code>login</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * and <code>commit</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception LoginException if the abort fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return false if this LoginModule's own login and/or commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          failed, and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public boolean abort() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                "aborted authentication attempt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else if (succeeded == true && commitSucceeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // Clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            UIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            GIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            supplementaryGroups = new LinkedList<UnixNumericGroupPrincipal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // overall authentication succeeded and commit succeeded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // but someone else's commit failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Logout the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p> This method removes the Principals associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * with the <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception LoginException if the logout fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return true in all cases (this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *          should not be ignored).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public boolean logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    ("logout Failed: Subject is Readonly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // remove the added Principals from the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        subject.getPrincipals().remove(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        subject.getPrincipals().remove(UIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        subject.getPrincipals().remove(GIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (int i = 0; i < supplementaryGroups.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            subject.getPrincipals().remove(supplementaryGroups.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        UIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        GIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        supplementaryGroups = new LinkedList<UnixNumericGroupPrincipal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                "logged out Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}