]>
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" |
3c0ef626 | 39 | |
5598e598 | 40 | #ifdef GSSAPI |
41 | #include "ssh-gss.h" | |
42 | #endif | |
43 | ||
3c0ef626 | 44 | /* import */ |
45 | extern ServerOptions options; | |
46 | extern u_char *session_id2; | |
47 | extern int session_id2_len; | |
48 | ||
510132b6 | 49 | Authctxt *x_authctxt = NULL; |
3c0ef626 | 50 | |
44a053a3 | 51 | /* methods */ |
52 | ||
53 | extern Authmethod method_none; | |
54 | #ifdef GSSAPI | |
55 | extern Authmethod method_external; | |
56 | extern Authmethod method_gssapi; | |
57 | #endif | |
58 | extern Authmethod method_pubkey; | |
59 | extern Authmethod method_passwd; | |
60 | extern Authmethod method_kbdint; | |
61 | extern Authmethod method_hostbased; | |
62 | ||
63 | Authmethod *authmethods[] = { | |
64 | &method_none, | |
65 | #ifdef GSSAPI | |
66 | &method_external, | |
67 | &method_gssapi, | |
68 | #endif | |
69 | &method_pubkey, | |
70 | &method_passwd, | |
71 | &method_kbdint, | |
72 | &method_hostbased, | |
73 | NULL | |
3c0ef626 | 74 | }; |
75 | ||
76 | /* protocol */ | |
77 | ||
1e608e42 | 78 | static void input_service_request(int, u_int32_t, void *); |
79 | static void input_userauth_request(int, u_int32_t, void *); | |
3c0ef626 | 80 | |
81 | /* helper */ | |
82 | static Authmethod *authmethod_lookup(const char *); | |
83 | static char *authmethods_get(void); | |
510132b6 | 84 | int user_key_allowed(struct passwd *, Key *); |
85 | int hostbased_key_allowed(struct passwd *, const char *, char *, Key *); | |
3c0ef626 | 86 | |
3c0ef626 | 87 | /* |
88 | * loop until authctxt->success == TRUE | |
89 | */ | |
90 | ||
510132b6 | 91 | Authctxt * |
1e608e42 | 92 | do_authentication2(void) |
3c0ef626 | 93 | { |
94 | Authctxt *authctxt = authctxt_new(); | |
95 | ||
96 | x_authctxt = authctxt; /*XXX*/ | |
97 | ||
98 | /* challenge-response is implemented via keyboard interactive */ | |
99 | if (options.challenge_response_authentication) | |
100 | options.kbd_interactive_authentication = 1; | |
101 | if (options.pam_authentication_via_kbd_int) | |
102 | options.kbd_interactive_authentication = 1; | |
510132b6 | 103 | if (use_privsep) |
104 | options.pam_authentication_via_kbd_int = 0; | |
3c0ef626 | 105 | |
1e608e42 | 106 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 107 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
108 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
510132b6 | 109 | |
110 | return (authctxt); | |
3c0ef626 | 111 | } |
112 | ||
113 | static void | |
1e608e42 | 114 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 115 | { |
116 | Authctxt *authctxt = ctxt; | |
117 | u_int len; | |
118 | int accept = 0; | |
119 | char *service = packet_get_string(&len); | |
1e608e42 | 120 | packet_check_eom(); |
3c0ef626 | 121 | |
122 | if (authctxt == NULL) | |
123 | fatal("input_service_request: no authctxt"); | |
124 | ||
125 | if (strcmp(service, "ssh-userauth") == 0) { | |
126 | if (!authctxt->success) { | |
127 | accept = 1; | |
128 | /* now we can handle user-auth requests */ | |
129 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
130 | } | |
131 | } | |
132 | /* XXX all other service requests are denied */ | |
133 | ||
134 | if (accept) { | |
135 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
136 | packet_put_cstring(service); | |
137 | packet_send(); | |
138 | packet_write_wait(); | |
139 | } else { | |
140 | debug("bad service request %s", service); | |
141 | packet_disconnect("bad service request %s", service); | |
142 | } | |
143 | xfree(service); | |
144 | } | |
145 | ||
146 | static void | |
1e608e42 | 147 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 148 | { |
149 | Authctxt *authctxt = ctxt; | |
150 | Authmethod *m = NULL; | |
151 | char *user, *service, *method, *style = NULL; | |
152 | int authenticated = 0; | |
153 | ||
154 | if (authctxt == NULL) | |
155 | fatal("input_userauth_request: no authctxt"); | |
156 | ||
157 | user = packet_get_string(NULL); | |
158 | service = packet_get_string(NULL); | |
159 | method = packet_get_string(NULL); | |
73a68d83 | 160 | |
305d07c0 | 161 | #ifdef GSSAPI |
b59afbfe | 162 | if (strcmp(user, "") == 0) { |
163 | char *lname = NULL; | |
164 | debug("gssapi received empty username"); | |
165 | PRIVSEP(ssh_gssapi_localname(&lname)); | |
166 | if (lname && lname[0] != '\0') { | |
167 | xfree(user); | |
168 | user = lname; | |
169 | debug("gssapi successfully set username to %s", user); | |
170 | } else if (authctxt->valid) { | |
171 | debug("failed to set username from gssapi context"); | |
172 | goto finish; | |
173 | } | |
174 | } | |
175 | #endif | |
73a68d83 | 176 | |
3c0ef626 | 177 | debug("userauth-request for user %s service %s method %s", user, service, method); |
178 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); | |
179 | ||
180 | if ((style = strchr(user, ':')) != NULL) | |
181 | *style++ = 0; | |
182 | ||
b59afbfe | 183 | if (!authctxt->user || |
184 | strcmp(user, authctxt->user) != 0) { | |
3c0ef626 | 185 | /* setup auth context */ |
b59afbfe | 186 | if (authctxt->user) { |
187 | xfree(authctxt->user); | |
188 | authctxt->user = NULL; | |
189 | } | |
190 | if (authctxt->service) { | |
191 | xfree(authctxt->service); | |
192 | authctxt->service = NULL; | |
193 | } | |
194 | if (authctxt->style) { | |
195 | xfree(authctxt->style); | |
196 | authctxt->style = NULL; | |
197 | } | |
510132b6 | 198 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
199 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { | |
3c0ef626 | 200 | authctxt->valid = 1; |
201 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
202 | #ifdef USE_PAM | |
510132b6 | 203 | PRIVSEP(start_pam(authctxt->pw->pw_name)); |
3c0ef626 | 204 | #endif |
205 | } else { | |
e964b42b | 206 | authctxt->valid = 0; |
3c0ef626 | 207 | log("input_userauth_request: illegal user %s", user); |
208 | #ifdef USE_PAM | |
510132b6 | 209 | PRIVSEP(start_pam("NOUSER")); |
3c0ef626 | 210 | #endif |
211 | } | |
510132b6 | 212 | setproctitle("%s%s", authctxt->pw ? user : "unknown", |
213 | use_privsep ? " [net]" : ""); | |
3c0ef626 | 214 | authctxt->user = xstrdup(user); |
215 | authctxt->service = xstrdup(service); | |
216 | authctxt->style = style ? xstrdup(style) : NULL; | |
510132b6 | 217 | if (use_privsep) |
218 | mm_inform_authserv(service, style); | |
3c0ef626 | 219 | } |
220 | /* reset state */ | |
1e608e42 | 221 | auth2_challenge_stop(authctxt); |
5598e598 | 222 | |
223 | #ifdef GSSAPI | |
1e608e42 | 224 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
225 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
5598e598 | 226 | #endif |
227 | ||
3c0ef626 | 228 | authctxt->postponed = 0; |
3c0ef626 | 229 | |
230 | /* try to authenticate user */ | |
231 | m = authmethod_lookup(method); | |
232 | if (m != NULL) { | |
233 | debug2("input_userauth_request: try method %s", method); | |
234 | authenticated = m->userauth(authctxt); | |
235 | } | |
b59afbfe | 236 | finish: |
3c0ef626 | 237 | userauth_finish(authctxt, authenticated, method); |
238 | ||
239 | xfree(service); | |
240 | xfree(user); | |
241 | xfree(method); | |
242 | } | |
243 | ||
244 | void | |
245 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
246 | { | |
247 | char *methods; | |
248 | ||
249 | if (!authctxt->valid && authenticated) | |
250 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
251 | authctxt->user); | |
252 | ||
253 | /* Special handling for root */ | |
254 | if (authenticated && authctxt->pw->pw_uid == 0 && | |
255 | !auth_root_allowed(method)) | |
256 | authenticated = 0; | |
257 | ||
258 | #ifdef USE_PAM | |
510132b6 | 259 | if (!use_privsep && authenticated && authctxt->user && |
260 | !do_pam_account(authctxt->user, NULL)) | |
3c0ef626 | 261 | authenticated = 0; |
262 | #endif /* USE_PAM */ | |
263 | ||
264 | /* Log before sending the reply */ | |
a2d82e42 | 265 | if (!compat20) |
266 | auth_log(authctxt, authenticated, method, " ssh1"); | |
267 | else | |
3c0ef626 | 268 | auth_log(authctxt, authenticated, method, " ssh2"); |
269 | ||
270 | if (authctxt->postponed) | |
271 | return; | |
272 | ||
273 | /* XXX todo: check if multiple auth methods are needed */ | |
274 | if (authenticated == 1) { | |
275 | /* turn off userauth */ | |
1e608e42 | 276 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
b59afbfe | 277 | if (compat20) { |
3c0ef626 | 278 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
279 | packet_send(); | |
280 | packet_write_wait(); | |
b59afbfe | 281 | } |
3c0ef626 | 282 | /* now we can break out */ |
283 | authctxt->success = 1; | |
284 | } else { | |
285 | if (authctxt->failures++ > AUTH_FAIL_MAX) { | |
286 | #ifdef WITH_AIXAUTHENTICATE | |
44a053a3 | 287 | /* XXX: privsep */ |
3c0ef626 | 288 | loginfailed(authctxt->user, |
1e608e42 | 289 | get_canonical_hostname(options.verify_reverse_mapping), |
3c0ef626 | 290 | "ssh"); |
291 | #endif /* WITH_AIXAUTHENTICATE */ | |
292 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
293 | } | |
411da244 | 294 | if (!compat20) { |
295 | /* | |
296 | * Break out of the dispatch loop now and go back to | |
297 | * SSH1 code. We need to set the 'success' flag to | |
298 | * break out of the loop. Set the 'postponed' flag to | |
299 | * tell the SSH1 code that authentication failed. The | |
300 | * SSH1 code will handle sending SSH_SMSG_FAILURE. | |
301 | */ | |
302 | authctxt->success = authctxt->postponed = 1; | |
303 | } else { | |
3c0ef626 | 304 | methods = authmethods_get(); |
305 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
306 | packet_put_cstring(methods); | |
307 | packet_put_char(0); /* XXX partial success, unused */ | |
308 | packet_send(); | |
309 | packet_write_wait(); | |
310 | xfree(methods); | |
411da244 | 311 | } |
3c0ef626 | 312 | } |
313 | } | |
314 | ||
3c0ef626 | 315 | /* get current user */ |
316 | ||
317 | struct passwd* | |
318 | auth_get_user(void) | |
319 | { | |
320 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; | |
321 | } | |
322 | ||
323 | #define DELIM "," | |
324 | ||
325 | static char * | |
326 | authmethods_get(void) | |
327 | { | |
1e608e42 | 328 | Buffer b; |
3c0ef626 | 329 | char *list; |
44a053a3 | 330 | int i; |
3c0ef626 | 331 | |
1e608e42 | 332 | buffer_init(&b); |
44a053a3 | 333 | for (i = 0; authmethods[i] != NULL; i++) { |
334 | if (strcmp(authmethods[i]->name, "none") == 0) | |
3c0ef626 | 335 | continue; |
44a053a3 | 336 | if (authmethods[i]->enabled != NULL && |
337 | *(authmethods[i]->enabled) != 0) { | |
1e608e42 | 338 | if (buffer_len(&b) > 0) |
339 | buffer_append(&b, ",", 1); | |
44a053a3 | 340 | buffer_append(&b, authmethods[i]->name, |
341 | strlen(authmethods[i]->name)); | |
3c0ef626 | 342 | } |
343 | } | |
1e608e42 | 344 | buffer_append(&b, "\0", 1); |
345 | list = xstrdup(buffer_ptr(&b)); | |
346 | buffer_free(&b); | |
3c0ef626 | 347 | return list; |
348 | } | |
349 | ||
350 | static Authmethod * | |
351 | authmethod_lookup(const char *name) | |
352 | { | |
44a053a3 | 353 | int i; |
354 | ||
3c0ef626 | 355 | if (name != NULL) |
44a053a3 | 356 | for (i = 0; authmethods[i] != NULL; i++) |
357 | if (authmethods[i]->enabled != NULL && | |
358 | *(authmethods[i]->enabled) != 0 && | |
359 | strcmp(name, authmethods[i]->name) == 0) | |
360 | return authmethods[i]; | |
361 | debug2("Unrecognized authentication method name: %s", | |
362 | name ? name : "NULL"); | |
3c0ef626 | 363 | return NULL; |
364 | } |