]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
44a053a3 | 26 | RCSID("$OpenBSD: auth2.c,v 1.93 2002/05/31 11:35:15 markus Exp $"); |
3c0ef626 | 27 | |
28 | #include "ssh2.h" | |
a2d82e42 | 29 | #include "ssh1.h" |
3c0ef626 | 30 | #include "xmalloc.h" |
3c0ef626 | 31 | #include "packet.h" |
3c0ef626 | 32 | #include "log.h" |
33 | #include "servconf.h" | |
34 | #include "compat.h" | |
3c0ef626 | 35 | #include "auth.h" |
3c0ef626 | 36 | #include "dispatch.h" |
3c0ef626 | 37 | #include "pathnames.h" |
510132b6 | 38 | #include "monitor_wrap.h" |
cf4d5580 | 39 | #include "misc.h" |
3c0ef626 | 40 | |
5598e598 | 41 | #ifdef GSSAPI |
42 | #include "ssh-gss.h" | |
7d1d5d37 | 43 | #ifdef GSI |
44 | #include "globus_gss_assist.h" | |
305d07c0 | 45 | char* olduser; |
46 | int changeuser = 0; | |
7d1d5d37 | 47 | #endif |
5598e598 | 48 | #endif |
49 | ||
3c0ef626 | 50 | /* import */ |
51 | extern ServerOptions options; | |
52 | extern u_char *session_id2; | |
53 | extern int session_id2_len; | |
54 | ||
510132b6 | 55 | Authctxt *x_authctxt = NULL; |
3c0ef626 | 56 | |
44a053a3 | 57 | /* methods */ |
58 | ||
59 | extern Authmethod method_none; | |
60 | #ifdef GSSAPI | |
61 | extern Authmethod method_external; | |
62 | extern Authmethod method_gssapi; | |
63 | #endif | |
64 | extern Authmethod method_pubkey; | |
65 | extern Authmethod method_passwd; | |
66 | extern Authmethod method_kbdint; | |
67 | extern Authmethod method_hostbased; | |
68 | ||
69 | Authmethod *authmethods[] = { | |
70 | &method_none, | |
71 | #ifdef GSSAPI | |
72 | &method_external, | |
73 | &method_gssapi, | |
74 | #endif | |
75 | &method_pubkey, | |
76 | &method_passwd, | |
77 | &method_kbdint, | |
78 | &method_hostbased, | |
79 | NULL | |
3c0ef626 | 80 | }; |
81 | ||
82 | /* protocol */ | |
83 | ||
1e608e42 | 84 | static void input_service_request(int, u_int32_t, void *); |
85 | static void input_userauth_request(int, u_int32_t, void *); | |
3c0ef626 | 86 | |
87 | /* helper */ | |
88 | static Authmethod *authmethod_lookup(const char *); | |
89 | static char *authmethods_get(void); | |
510132b6 | 90 | int user_key_allowed(struct passwd *, Key *); |
91 | int hostbased_key_allowed(struct passwd *, const char *, char *, Key *); | |
3c0ef626 | 92 | |
3c0ef626 | 93 | /* |
94 | * loop until authctxt->success == TRUE | |
95 | */ | |
96 | ||
510132b6 | 97 | Authctxt * |
1e608e42 | 98 | do_authentication2(void) |
3c0ef626 | 99 | { |
100 | Authctxt *authctxt = authctxt_new(); | |
101 | ||
102 | x_authctxt = authctxt; /*XXX*/ | |
103 | ||
104 | /* challenge-response is implemented via keyboard interactive */ | |
105 | if (options.challenge_response_authentication) | |
106 | options.kbd_interactive_authentication = 1; | |
107 | if (options.pam_authentication_via_kbd_int) | |
108 | options.kbd_interactive_authentication = 1; | |
510132b6 | 109 | if (use_privsep) |
110 | options.pam_authentication_via_kbd_int = 0; | |
3c0ef626 | 111 | |
1e608e42 | 112 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 113 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
114 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
510132b6 | 115 | |
116 | return (authctxt); | |
3c0ef626 | 117 | } |
118 | ||
119 | static void | |
1e608e42 | 120 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 121 | { |
122 | Authctxt *authctxt = ctxt; | |
123 | u_int len; | |
124 | int accept = 0; | |
125 | char *service = packet_get_string(&len); | |
1e608e42 | 126 | packet_check_eom(); |
3c0ef626 | 127 | |
128 | if (authctxt == NULL) | |
129 | fatal("input_service_request: no authctxt"); | |
130 | ||
131 | if (strcmp(service, "ssh-userauth") == 0) { | |
132 | if (!authctxt->success) { | |
133 | accept = 1; | |
134 | /* now we can handle user-auth requests */ | |
135 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
136 | } | |
137 | } | |
138 | /* XXX all other service requests are denied */ | |
139 | ||
140 | if (accept) { | |
141 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
142 | packet_put_cstring(service); | |
143 | packet_send(); | |
144 | packet_write_wait(); | |
145 | } else { | |
146 | debug("bad service request %s", service); | |
147 | packet_disconnect("bad service request %s", service); | |
148 | } | |
149 | xfree(service); | |
150 | } | |
151 | ||
152 | static void | |
1e608e42 | 153 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 154 | { |
155 | Authctxt *authctxt = ctxt; | |
156 | Authmethod *m = NULL; | |
157 | char *user, *service, *method, *style = NULL; | |
158 | int authenticated = 0; | |
159 | ||
160 | if (authctxt == NULL) | |
161 | fatal("input_userauth_request: no authctxt"); | |
162 | ||
163 | user = packet_get_string(NULL); | |
164 | service = packet_get_string(NULL); | |
165 | method = packet_get_string(NULL); | |
73a68d83 | 166 | |
305d07c0 | 167 | #ifdef GSSAPI |
168 | #ifdef GSI | |
169 | if(changeuser == 0 && (strcmp(method,"external-keyx") == 0 || strcmp(method,"gssapi") ==0) && strcmp(user,"") == 0) { | |
73a68d83 | 170 | char *gridmapped_name = NULL; |
171 | struct passwd *pw = NULL; | |
73a68d83 | 172 | if(globus_gss_assist_gridmap(gssapi_client_name.value, |
173 | &gridmapped_name) == 0) { | |
174 | user = gridmapped_name; | |
175 | debug("I gridmapped and got %s", user); | |
176 | pw = getpwnam(user); | |
177 | if (pw && allowed_user(pw)) { | |
305d07c0 | 178 | olduser = authctxt->user; |
a28539f3 | 179 | authctxt->user = xstrdup(user); |
73a68d83 | 180 | authctxt->pw = pwcopy(pw); |
181 | authctxt->valid = 1; | |
305d07c0 | 182 | changeuser = 1; |
73a68d83 | 183 | } |
305d07c0 | 184 | |
73a68d83 | 185 | } else { |
0dadc8a9 | 186 | debug("I gridmapped and got null, reverting to %s", |
187 | authctxt->user); | |
188 | xfree(user); | |
189 | user = xstrdup(authctxt->user); | |
73a68d83 | 190 | } |
191 | } | |
305d07c0 | 192 | else if(changeuser) { |
193 | struct passwd *pw = NULL; | |
194 | pw = getpwnam(user); | |
195 | if (pw && allowed_user(pw)) { | |
a28539f3 | 196 | xfree(authctxt->user); |
305d07c0 | 197 | authctxt->user = olduser; |
198 | authctxt->pw = pwcopy(pw); | |
199 | authctxt->valid = 1; | |
200 | changeuser = 0; | |
201 | } | |
202 | } | |
203 | ||
204 | #endif /* GSI */ | |
205 | #endif /* GSSAPI */ | |
73a68d83 | 206 | |
3c0ef626 | 207 | debug("userauth-request for user %s service %s method %s", user, service, method); |
208 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); | |
209 | ||
210 | if ((style = strchr(user, ':')) != NULL) | |
211 | *style++ = 0; | |
212 | ||
213 | if (authctxt->attempt++ == 0) { | |
214 | /* setup auth context */ | |
510132b6 | 215 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
216 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { | |
3c0ef626 | 217 | authctxt->valid = 1; |
218 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
219 | #ifdef USE_PAM | |
510132b6 | 220 | PRIVSEP(start_pam(authctxt->pw->pw_name)); |
3c0ef626 | 221 | #endif |
222 | } else { | |
223 | log("input_userauth_request: illegal user %s", user); | |
224 | #ifdef USE_PAM | |
510132b6 | 225 | PRIVSEP(start_pam("NOUSER")); |
3c0ef626 | 226 | #endif |
227 | } | |
510132b6 | 228 | setproctitle("%s%s", authctxt->pw ? user : "unknown", |
229 | use_privsep ? " [net]" : ""); | |
3c0ef626 | 230 | authctxt->user = xstrdup(user); |
231 | authctxt->service = xstrdup(service); | |
232 | authctxt->style = style ? xstrdup(style) : NULL; | |
510132b6 | 233 | if (use_privsep) |
234 | mm_inform_authserv(service, style); | |
3c0ef626 | 235 | } else if (strcmp(user, authctxt->user) != 0 || |
236 | strcmp(service, authctxt->service) != 0) { | |
237 | packet_disconnect("Change of username or service not allowed: " | |
238 | "(%s,%s) -> (%s,%s)", | |
239 | authctxt->user, authctxt->service, user, service); | |
240 | } | |
241 | /* reset state */ | |
1e608e42 | 242 | auth2_challenge_stop(authctxt); |
5598e598 | 243 | |
244 | #ifdef GSSAPI | |
1e608e42 | 245 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
246 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
5598e598 | 247 | #endif |
248 | ||
3c0ef626 | 249 | authctxt->postponed = 0; |
3c0ef626 | 250 | |
251 | /* try to authenticate user */ | |
252 | m = authmethod_lookup(method); | |
253 | if (m != NULL) { | |
254 | debug2("input_userauth_request: try method %s", method); | |
255 | authenticated = m->userauth(authctxt); | |
256 | } | |
257 | userauth_finish(authctxt, authenticated, method); | |
258 | ||
259 | xfree(service); | |
260 | xfree(user); | |
261 | xfree(method); | |
262 | } | |
263 | ||
264 | void | |
265 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
266 | { | |
267 | char *methods; | |
268 | ||
269 | if (!authctxt->valid && authenticated) | |
270 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
271 | authctxt->user); | |
272 | ||
273 | /* Special handling for root */ | |
274 | if (authenticated && authctxt->pw->pw_uid == 0 && | |
275 | !auth_root_allowed(method)) | |
276 | authenticated = 0; | |
277 | ||
278 | #ifdef USE_PAM | |
510132b6 | 279 | if (!use_privsep && authenticated && authctxt->user && |
280 | !do_pam_account(authctxt->user, NULL)) | |
3c0ef626 | 281 | authenticated = 0; |
282 | #endif /* USE_PAM */ | |
283 | ||
284 | /* Log before sending the reply */ | |
a2d82e42 | 285 | if (!compat20) |
286 | auth_log(authctxt, authenticated, method, " ssh1"); | |
287 | else | |
3c0ef626 | 288 | auth_log(authctxt, authenticated, method, " ssh2"); |
289 | ||
290 | if (authctxt->postponed) | |
291 | return; | |
292 | ||
293 | /* XXX todo: check if multiple auth methods are needed */ | |
294 | if (authenticated == 1) { | |
295 | /* turn off userauth */ | |
1e608e42 | 296 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
a2d82e42 | 297 | if (!compat20) |
298 | packet_start(SSH_SMSG_SUCCESS); | |
299 | else | |
3c0ef626 | 300 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
301 | packet_send(); | |
302 | packet_write_wait(); | |
303 | /* now we can break out */ | |
304 | authctxt->success = 1; | |
305 | } else { | |
306 | if (authctxt->failures++ > AUTH_FAIL_MAX) { | |
307 | #ifdef WITH_AIXAUTHENTICATE | |
44a053a3 | 308 | /* XXX: privsep */ |
3c0ef626 | 309 | loginfailed(authctxt->user, |
1e608e42 | 310 | get_canonical_hostname(options.verify_reverse_mapping), |
3c0ef626 | 311 | "ssh"); |
312 | #endif /* WITH_AIXAUTHENTICATE */ | |
313 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
314 | } | |
411da244 | 315 | if (!compat20) { |
316 | /* | |
317 | * Break out of the dispatch loop now and go back to | |
318 | * SSH1 code. We need to set the 'success' flag to | |
319 | * break out of the loop. Set the 'postponed' flag to | |
320 | * tell the SSH1 code that authentication failed. The | |
321 | * SSH1 code will handle sending SSH_SMSG_FAILURE. | |
322 | */ | |
323 | authctxt->success = authctxt->postponed = 1; | |
324 | } else { | |
3c0ef626 | 325 | methods = authmethods_get(); |
326 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
327 | packet_put_cstring(methods); | |
328 | packet_put_char(0); /* XXX partial success, unused */ | |
329 | packet_send(); | |
330 | packet_write_wait(); | |
331 | xfree(methods); | |
411da244 | 332 | } |
3c0ef626 | 333 | } |
334 | } | |
335 | ||
3c0ef626 | 336 | /* get current user */ |
337 | ||
338 | struct passwd* | |
339 | auth_get_user(void) | |
340 | { | |
341 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; | |
342 | } | |
343 | ||
344 | #define DELIM "," | |
345 | ||
346 | static char * | |
347 | authmethods_get(void) | |
348 | { | |
1e608e42 | 349 | Buffer b; |
3c0ef626 | 350 | char *list; |
44a053a3 | 351 | int i; |
3c0ef626 | 352 | |
1e608e42 | 353 | buffer_init(&b); |
44a053a3 | 354 | for (i = 0; authmethods[i] != NULL; i++) { |
355 | if (strcmp(authmethods[i]->name, "none") == 0) | |
3c0ef626 | 356 | continue; |
44a053a3 | 357 | if (authmethods[i]->enabled != NULL && |
358 | *(authmethods[i]->enabled) != 0) { | |
1e608e42 | 359 | if (buffer_len(&b) > 0) |
360 | buffer_append(&b, ",", 1); | |
44a053a3 | 361 | buffer_append(&b, authmethods[i]->name, |
362 | strlen(authmethods[i]->name)); | |
3c0ef626 | 363 | } |
364 | } | |
1e608e42 | 365 | buffer_append(&b, "\0", 1); |
366 | list = xstrdup(buffer_ptr(&b)); | |
367 | buffer_free(&b); | |
3c0ef626 | 368 | return list; |
369 | } | |
370 | ||
371 | static Authmethod * | |
372 | authmethod_lookup(const char *name) | |
373 | { | |
44a053a3 | 374 | int i; |
375 | ||
3c0ef626 | 376 | if (name != NULL) |
44a053a3 | 377 | for (i = 0; authmethods[i] != NULL; i++) |
378 | if (authmethods[i]->enabled != NULL && | |
379 | *(authmethods[i]->enabled) != 0 && | |
380 | strcmp(name, authmethods[i]->name) == 0) | |
381 | return authmethods[i]; | |
382 | debug2("Unrecognized authentication method name: %s", | |
383 | name ? name : "NULL"); | |
3c0ef626 | 384 | return NULL; |
385 | } |