File: UnixLoginModule.java

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (330 lines) | stat: -rw-r--r-- 12,356 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/
package com.sun.grid.security.login;

import com.sun.grid.util.SGEUtil;
import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

/**
 * This <code>LoginModule</code> authenticates a unix user with username
 * and password against the PAM or system authentication system.
 * 
 * The username is queried with a <code>NameCallback</code>, the password with 
 * a <code>PasswordCallback</code>
 * 
 * <p>After a successfull login this <code>LoginModule</code> adds
 * 
 * <ul>
 *    <li> a {@link com.sun.security.auth.UnixPrincipal} of the authenticated user</li>
 *    <li> a {@link com.sun.security.auth.UnixNumericUserPrincipal} with the user id
 *         of the authenticated user</li>
 *    <li> a {@link  com.sun.security.auth.UnixNumericGroupPrincipal} for each group the authenticated
 *         user belongs too</li>
 * </ul>
 * 
 * to the current subject.</p>
 * 
 * <p>This class uses a {@link java.util.logging.Logger} for log messages. The name of the <code>Logger</code>
 *    is equal to the fullqualified classname of this class.</p>
 * 
 * <H3>Options for UnixLoginModule</H3>
 * 
 * <table>
 *   <tr><th>Option</th><th>description</th></tr>
 *   <tr>
 *      <td>sge_root</td>
 *      <td>path to the gridengine distribution</td>
 *   </tr>
 *   <tr>
 *      <td>auth_method</td>
 *      <td>Autehtication method. Valid values are "pam" and "system"</td>
 *   </tr>
 *   <tr>
 *      <td>pam_service</td>
 *      <td>Name of the pam service (see man pam(5). Required for
 *          PAM authentifcation</td>
 *   </tr>
 * </table>
 * 
 * <H3>Simple jaas config file for PAM authentication</H3>
 * 
 * <pre>
 *  sample {
 *   com.sun.grid.security.login.UnixLoginModule requisite
 *         sge_root="/opt/sge",
 *         auth_method="pam";
 *         pam_service="su";
 *  };
 * </pre>
 * 
 * <H3>Simple jaas config file for system authentication</H3>
 * 
 * <pre>
 *  sample {
 *   com.sun.grid.security.login.UnixLoginModule requisite
 *         command="/opt/sge",
 *         auth_method="system";
 *  };
 * </pre>
 * 
 */
public class UnixLoginModule implements LoginModule {

    private static final String AUTHZ_IDENTITY = "authzIdentity";    
    
    private final static Logger log = Logger.getLogger(UnixLoginModule.class.getName(), RB.BUNDLE);
    private String confError;
    private String authMethod;
    private String pamService;
    private String command;
    private Subject subject;
    private boolean loginSucceded;
    private boolean commitSucceded;
    private CallbackHandler callbackHandler;
    private Set principals = new HashSet();
    private String authzIdentity;
    

    /**
     * Initialize the <code>UnixLoginModule</code>
     * @param subject   the current subject
     * @param callbackHandler the callbackhandler (must at least handle a 
     *                        <code>NameCallback</code> and a 
     *                        </code>PasswordCallback</code>).
     * @param sharedState not used
     * @param options contains the options for the <code>UnixLoginModule</code>.
     */
    public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {

        log.entering("UnixLoginModule", "initialize");

        String sgeRoot = (String) options.get("sge_root");
        if (sgeRoot != null) {
            log.log(Level.FINE, "sge_root={0}", sgeRoot);
            try {
                String arch = SGEUtil.getArch(new File(sgeRoot));
                command = sgeRoot + File.separatorChar + "utilbin" + File.separatorChar + arch + File.separatorChar + "authuser";
                if (arch.equals("win32-x86")) {
                    command += ".exe";
                }
                log.log(Level.FINE, "command={0}", command);
            } catch (Exception ex) {
                log.log(Level.WARNING, "unixlogin.error.arch", ex);
                confError = RB.getString("unixlogin.error.arch", ex.getLocalizedMessage());
                return;
            }
        } else {
            log.log(Level.WARNING, "unixlogin.error.arch", "sge_root");
            confError = RB.getString("unixlogin.error.missing", "sge_root");
            return;
        }
        authMethod = (String) options.get("auth_method");
        if (authMethod == null) {
            log.log(Level.WARNING, "unixlogin.error.missing", "auth_method");
            confError = RB.getString("unixlogin.error.missing", "auth_method");
            return;
        }
        log.log(Level.FINE, "auth_method={0}", authMethod);
        if ("pam".equals(authMethod)) {
            pamService = (String) options.get("pam_service");
            if (pamService == null) {
                log.log(Level.WARNING, "unixlogin.error.missing", "pam_service");
                confError = RB.getString("unixlogin.error.missing", "pam_service");
                return;
            } else {
                log.log(Level.FINE, "pam_service={0}", pamService);
            }
        }
        authzIdentity = (String) options.get(AUTHZ_IDENTITY);
        if(authzIdentity != null && authzIdentity.length() == 0) {
            authzIdentity = null;
        }
        
        this.subject = subject;
        this.callbackHandler = callbackHandler;

        log.entering("UnixLoginModule", "exiting");

    }

    /**
     * Perform the login.
     *
     * @throws javax.security.auth.login.LoginException <ul>
     *      <li>if the callbackhandler reports an error</li>
     *      <li>if some options are missing (please check the jass.config file)</li>
     *      <li>if the underlying authentication system report an error</li>
     *    </ul>
     * @return <code>true</code> on successfull authentication. <code>false</code>
     *         if username of password is invalid.
     */
    public boolean login() throws LoginException {

        log.entering("UnixLoginModule", "login");

        if (confError != null) {
            throw RB.newLoginException("unixlogin.error.conf", new Object[]{confError});
        }
        PasswordCallback pwCallback = new PasswordCallback(RB.getString("unixlogin.userprompt"), false);
        NameCallback nameCallback = new NameCallback(RB.getString("unixlogin.pwprompt"));

        try {
            callbackHandler.handle(new Callback[]{nameCallback, pwCallback});
        } catch (IOException ex) {
            throw RB.newLoginException("unixlogin.error.iocb", ex,
                    new Object[]{ex.getLocalizedMessage()});
        } catch (UnsupportedCallbackException ex) {
            throw RB.newLoginException("unixlogin.error.invalidcb", ex,
                    new Object[]{ex.getLocalizedMessage()});
        }

        String username = nameCallback.getName();
        if (username == null || username.length() == 0) {
            loginSucceded = false;
            log.exiting("UnixLoginModule", "login", Boolean.FALSE);
            return loginSucceded;
        }

        char[] pw = pwCallback.getPassword();
        if (pw == null) {
            loginSucceded = false;
            log.exiting("UnixLoginModule", "login", Boolean.FALSE);
            return loginSucceded;
        }

        AuthUserWrapper authuser = null;
        if (authMethod == null) {
            throw RB.newLoginException("unixlogin.error.missing",
                    new Object[]{"auth_method"});
        } else if ("pam".equals(authMethod)) {
            authuser = AuthUserWrapper.newInstanceForPam(command, pamService);
        } else if ("shadow".equals(authMethod)) {
            log.log(Level.WARNING, "unixlogin.deprecatedAuthMethod",
                    new Object[]{authMethod, "system"});
            authuser = AuthUserWrapper.newInstance(command);
        } else if ("system".equals(authMethod)) {
            authuser = AuthUserWrapper.newInstance(command);
        } else {
            throw RB.newLoginException("unixlogin.error.unknownAuthMethod",
                    new Object[]{authMethod});
        }

        try {
            Set p = authuser.authenticate(username, pw);
            if (p != null) {
                log.log(Level.FINE, "unixlogin.authuser.principal.count", new Integer(p.size()));
                if(log.isLoggable(Level.FINER)) {
                    Iterator iter = p.iterator();
                    while(iter.hasNext()) {
                        Principal pr = (Principal)iter.next();
                        log.log(Level.FINER,"unixlogin.authuser.principal", new Object [] { pr.getClass().getName(), pr.getName() });
                    }
                }
                principals.addAll(p);
                if (authzIdentity != null) {
                    principals.add(new UserPrincipal(authzIdentity));
                }
                
                loginSucceded = true;
            } else {
                log.log(Level.FINE, "unixlogin.authuser.principal.no");
                loginSucceded = false;
            }
        } catch (LoginException ex) {
            log.throwing("UnixLoginModule", "login", ex);
            throw ex;
        }
        log.exiting("UnixLoginModule", "login", Boolean.valueOf(loginSucceded));
        return loginSucceded;
    }

    /**
     * Commit the login (adds the principals to the subject)
     *
     * @return <code>true</code> of the principals has been added to the subject.
     */
    public boolean commit() {
        log.entering("UnixLoginModule", "commit");
        if (loginSucceded) {
            subject.getPrincipals().addAll(principals);
            if (log.isLoggable(Level.FINER)) {
                log.log(Level.FINE, "unixlogin.subject.principal",
                        new Integer(subject.getPrincipals().size()));
            }
            commitSucceded = true;
        }
        log.exiting("UnixLoginModule", "commit", Boolean.valueOf(commitSucceded));
        return commitSucceded;
    }

    /**
     * Abort the login.
     * @return Always <code>true</code>
     */
    public boolean abort() {
        logout();
        return true;
    }

    /**
     * Removes all previously added prinicipals from the subject.
     *
     * @return Always <code>true</code>
     */
    public boolean logout() {
        if (commitSucceded) {
            subject.getPrincipals().removeAll(principals);
        }
        subject = null;
        principals.clear();
        callbackHandler = null;
        loginSucceded = false;
        commitSucceded = false;
        return true;
    }
}