jdk/src/share/classes/com/sun/security/auth/module/UnixLoginModule.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7970 af1579474d16
child 20742 4ae78e8060d6
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class UnixLoginModule implements LoginModule {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private CallbackHandler callbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Map<String, ?> sharedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Map<String, ?> options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // configurable option
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // UnixSystem to retrieve underlying system info
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private UnixSystem ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // the authentication status
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Underlying system info
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private UnixPrincipal userPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private UnixNumericUserPrincipal UIDPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private UnixNumericGroupPrincipal GIDPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private LinkedList<UnixNumericGroupPrincipal> supplementaryGroups =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
    73
                new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Initialize this <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param subject the <code>Subject</code> to be authenticated. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param callbackHandler a <code>CallbackHandler</code> for communicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *                  with the end user (prompting for usernames and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *                  passwords, for example). <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param sharedState shared <code>LoginModule</code> state. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param options options specified in the login
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *                  <code>Configuration</code> for this particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *                  <code>LoginModule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void initialize(Subject subject, CallbackHandler callbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                           Map<String,?> sharedState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                           Map<String,?> options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.callbackHandler = callbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.sharedState = sharedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // initialize any configured options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        debug = "true".equalsIgnoreCase((String)options.get("debug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Authenticate the user (first phase).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p> The implementation of this method attempts to retrieve the user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Unix <code>Subject</code> information by making a native Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * system call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @exception FailedLoginException if attempts to retrieve the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          system information fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return true in all cases (this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *          should not be ignored).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public boolean login() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        long[] unixGroups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ss = new UnixSystem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (ss == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new FailedLoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                ("Failed in attempt to import " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                "the underlying system identity information");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            userPrincipal = new UnixPrincipal(ss.getUsername());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            UIDPrincipal = new UnixNumericUserPrincipal(ss.getUid());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            GIDPrincipal = new UnixNumericGroupPrincipal(ss.getGid(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (ss.getGroups() != null && ss.getGroups().length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                unixGroups = ss.getGroups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                for (int i = 0; i < unixGroups.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    UnixNumericGroupPrincipal ngp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        new UnixNumericGroupPrincipal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        (unixGroups[i], false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    if (!ngp.getName().equals(GIDPrincipal.getName()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        supplementaryGroups.add(ngp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        "succeeded importing info: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                System.out.println("\t\t\tuid = " + ss.getUid());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                System.out.println("\t\t\tgid = " + ss.getGid());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                unixGroups = ss.getGroups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                for (int i = 0; i < unixGroups.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    System.out.println("\t\t\tsupp gid = " + unixGroups[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Commit the authentication (second phase).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * overall authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * succeeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * succeeded (the importing of the Unix authentication information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * succeeded), then this method associates the Unix Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * with the <code>Subject</code> currently tied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <code>LoginModule</code>.  If this LoginModule's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * authentication attempted failed, then this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception LoginException if the commit fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return true if this LoginModule's own login and commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *          succeeded, or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public boolean commit() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    "did not add any Principals to Subject " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    "because own authentication failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    ("commit Failed: Subject is Readonly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (!subject.getPrincipals().contains(userPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                subject.getPrincipals().add(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (!subject.getPrincipals().contains(UIDPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                subject.getPrincipals().add(UIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (!subject.getPrincipals().contains(GIDPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                subject.getPrincipals().add(GIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            for (int i = 0; i < supplementaryGroups.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (!subject.getPrincipals().contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    (supplementaryGroups.get(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    subject.getPrincipals().add(supplementaryGroups.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    "added UnixPrincipal,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                System.out.println("\t\t\t\tUnixNumericUserPrincipal,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                System.out.println("\t\t\t\tUnixNumericGroupPrincipal(s),");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                System.out.println("\t\t\t to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            commitSucceeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
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
     * Abort the authentication (second phase).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * overall authentication failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * did not succeed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p> This method cleans up any state that was originally saved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * as part of the authentication attempt from the <code>login</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * and <code>commit</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception LoginException if the abort fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return false if this LoginModule's own login and/or commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *          failed, and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public boolean abort() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                "aborted authentication attempt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else if (succeeded == true && commitSucceeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // Clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            UIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            GIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            supplementaryGroups = new LinkedList<UnixNumericGroupPrincipal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // overall authentication succeeded and commit succeeded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // but someone else's commit failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Logout the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p> This method removes the Principals associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * with the <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @exception LoginException if the logout fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return true in all cases (this <code>LoginModule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          should not be ignored).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public boolean logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    ("logout Failed: Subject is Readonly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // remove the added Principals from the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        subject.getPrincipals().remove(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        subject.getPrincipals().remove(UIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        subject.getPrincipals().remove(GIDPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        for (int i = 0; i < supplementaryGroups.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            subject.getPrincipals().remove(supplementaryGroups.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        UIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        GIDPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        supplementaryGroups = new LinkedList<UnixNumericGroupPrincipal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            System.out.println("\t\t[UnixLoginModule]: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                "logged out Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}