~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/juti/java/com/sun/grid/security/login/AuthUserWrapper.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package com.sun.grid.security.login;
 
33
 
 
34
import com.sun.grid.util.expect.Expect;
 
35
import com.sun.grid.util.expect.ExpectBuffer;
 
36
import com.sun.grid.util.expect.ExpectHandler;
 
37
import com.sun.grid.util.expect.ExpectPasswordHandler;
 
38
import com.sun.grid.util.expect.ExpectStringHandler;
 
39
import java.io.IOException;
 
40
import java.util.HashSet;
 
41
import java.util.Set;
 
42
import java.util.StringTokenizer;
 
43
import java.util.logging.Level;
 
44
import java.util.logging.Logger;
 
45
import javax.security.auth.login.LoginException;
 
46
 
 
47
/**
 
48
 * This class used by the <code>UnixLoginModule</code> to execute the authuser binary
 
49
 *
 
50
 */
 
51
class AuthUserWrapper {
 
52
    
 
53
    private static final Logger LOGGER = Logger.getLogger(AuthUserWrapper.class.getName(), RB.BUNDLE);
 
54
        
 
55
    private String [] command;
 
56
    
 
57
    private AuthUserWrapper(String [] command) {
 
58
        this.command = command;
 
59
    }
 
60
    
 
61
    /**
 
62
     * Create a new instance of <code>AuthUserWrapper</code> which uses
 
63
     * the PAM authentication system.
 
64
     * 
 
65
     * @param authuser    path the the authuser binary
 
66
     * @param pamServiceName  name of the user pam service
 
67
     * @return the <code>AuthUserWrapper</code>.
 
68
     */
 
69
    public static AuthUserWrapper newInstanceForPam(String authuser, String pamServiceName) {
 
70
        return new AuthUserWrapper( new String [] {
 
71
             authuser, "pam", "-s", pamServiceName
 
72
        });
 
73
    }
 
74
    
 
75
    /**
 
76
     * Create a new instance of <code>AuthUserWrapper</code> which uses
 
77
     * the authentication system of the operation system
 
78
     * 
 
79
     * @param authuser    path the the authuser binary
 
80
     * @return the <code>AuthUserWrapper</code>.
 
81
     */
 
82
    public static AuthUserWrapper newInstance(String authuser) {
 
83
        return new AuthUserWrapper( new String [] {
 
84
             authuser, "system"
 
85
        });
 
86
    }
 
87
 
 
88
    /**
 
89
     * Authenticate a user
 
90
     *
 
91
     * @param username  username
 
92
     * @param password the password
 
93
     * @throws javax.security.auth.login.LoginException <ul>
 
94
     *     <li>if the authuser binary reports and error (authuser exited with status 2)</li>
 
95
     *     <li>if the authuser can not be started</li>
 
96
     *     <li>if the authuser has been interrupted</li>
 
97
     * </ul>
 
98
     * @return <ul>
 
99
     *    <li><code>null</code> if <code>username</code> of <code>password</code> is invalid.
 
100
     *        (authuser exited with status 1)
 
101
     *    </li>
 
102
     *    <li> else a <code>Set</code> containing <ul>
 
103
     *          <li> a {@link com.sun.grid.security.login.UserPrincipal} of the authenticated user</li>
 
104
     *          <li> a {@link com.sun.grid.security.login.NumericUserPrincipal} with the user id
 
105
     *              of the authenticated user</li>
 
106
     *          <li> a {@link  com.sun.grid.security.login.NumericGroupPrincipal} for each group the authenticated
 
107
     *               user belongs too</li>
 
108
     *         </ul></li>
 
109
     *  </ul>
 
110
     */
 
111
    public Set authenticate(final String username, final char[] password) throws LoginException {
 
112
        
 
113
        Set ret = null;
 
114
        
 
115
        Expect expect = new Expect(command);
 
116
 
 
117
        expect.add(new ExpectStringHandler("username: ", username.toCharArray()));
 
118
        expect.add(new ExpectPasswordHandler("password: ", password));
 
119
        
 
120
        PrincipalHandler principalHandler = new PrincipalHandler(username);        
 
121
        expect.add(principalHandler);
 
122
        
 
123
        ErrorHandler errorHandler = new ErrorHandler();
 
124
        expect.add(errorHandler);
 
125
        
 
126
        try {            
 
127
            int exitCode = expect.exec(60*1000);
 
128
            
 
129
            // exit codes are defined in juti.h (see auth_result_t)
 
130
            // 0 means success, 1 means invalid username of password,
 
131
            // 2 means error
 
132
            switch(exitCode) {
 
133
                case 0:  // success
 
134
                    LOGGER.log(Level.FINE, "authuser.success", username);
 
135
                    return principalHandler.getPrinicipals();
 
136
                case 1: // authentication failed
 
137
                    LOGGER.log(Level.FINE, "authuser.failed", username);
 
138
                    return null;
 
139
                default:
 
140
                    if(errorHandler.getError() == null) {
 
141
                        throw RB.newLoginException("authuser.error.unknown",
 
142
                                                   new Object [] { new Integer(exitCode) });
 
143
                    } else {
 
144
                        throw RB.newLoginException("authuser.error",
 
145
                                                   new Object [] { errorHandler.getError() });
 
146
                    }
 
147
            }
 
148
        } catch (InterruptedException ex) {
 
149
            throw RB.newLoginException("authuser.error.interrupted");
 
150
        } catch (IOException ex) {
 
151
            throw RB.newLoginException("authuser.error.io", ex,
 
152
                                       new Object [] { ex.getLocalizedMessage() });
 
153
        }
 
154
        
 
155
    }
 
156
    
 
157
    /**
 
158
     *  Handles error message of the authuser
 
159
     */
 
160
    class ErrorHandler implements ExpectHandler {
 
161
        
 
162
        String error;
 
163
        public void handle(Expect expect, ExpectBuffer buffer) throws IOException {
 
164
            String msg = buffer.consumeLine("Error: ");
 
165
            if(msg != null) {
 
166
                error = msg.trim();
 
167
            }
 
168
        }
 
169
        
 
170
        public String getError() {
 
171
            return error;
 
172
        }
 
173
    }
 
174
    
 
175
    /**
 
176
     *  Handles the uid and gid output of the authuser
 
177
     */
 
178
    class PrincipalHandler implements ExpectHandler {
 
179
 
 
180
        private Set principals = new HashSet();
 
181
        private String username;
 
182
        
 
183
        public PrincipalHandler(String username) {
 
184
            this.username = username;
 
185
        }
 
186
        
 
187
        public void handle(Expect expect, ExpectBuffer buffer) throws IOException {
 
188
 
 
189
            String line = buffer.consumeLine("uid ");
 
190
            if(line != null) {
 
191
 
 
192
                UserPrincipal up = new UserPrincipal(username);
 
193
                principals.add(up);
 
194
                line = line.trim();
 
195
                NumericUserPrincipal p = new NumericUserPrincipal(line);
 
196
                principals.add(p);
 
197
            }
 
198
            
 
199
            line = buffer.consumeLine("gid ");
 
200
            if(line != null) {
 
201
                StringTokenizer st = new StringTokenizer(line.trim(), ",");
 
202
                boolean primaryGroup = true;
 
203
                while(st.hasMoreTokens()) {
 
204
                    String groupName = st.nextToken();
 
205
                    int index = groupName.indexOf('(');
 
206
                    if(index >= 0) {
 
207
                        int endIndex = groupName.indexOf(')', index);
 
208
                        if(endIndex > index) {
 
209
                            String gid = groupName.substring(index+1, endIndex);
 
210
                            groupName = groupName.substring(0,index);
 
211
                            NumericGroupPrincipal p = new NumericGroupPrincipal(gid, primaryGroup);
 
212
                            principals.add(p);
 
213
                        }
 
214
                    }
 
215
                    GroupPrincipal p = new GroupPrincipal(groupName, primaryGroup);
 
216
                    principals.add(p);
 
217
                    primaryGroup = false;
 
218
                }
 
219
            }
 
220
        }
 
221
        
 
222
        public Set getPrinicipals() {
 
223
            return principals;
 
224
        }
 
225
    }    
 
226
    
 
227
}