]>
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" | |
1e608e42 | 26 | RCSID("$OpenBSD: auth2.c,v 1.85 2002/02/24 19:14:59 markus Exp $"); |
3c0ef626 | 27 | |
28 | #include <openssl/evp.h> | |
29 | ||
30 | #include "ssh2.h" | |
a2d82e42 | 31 | #include "ssh1.h" |
3c0ef626 | 32 | #include "xmalloc.h" |
33 | #include "rsa.h" | |
34 | #include "sshpty.h" | |
35 | #include "packet.h" | |
36 | #include "buffer.h" | |
37 | #include "log.h" | |
38 | #include "servconf.h" | |
39 | #include "compat.h" | |
40 | #include "channels.h" | |
41 | #include "bufaux.h" | |
42 | #include "auth.h" | |
43 | #include "session.h" | |
44 | #include "dispatch.h" | |
45 | #include "key.h" | |
46 | #include "cipher.h" | |
47 | #include "kex.h" | |
48 | #include "pathnames.h" | |
49 | #include "uidswap.h" | |
50 | #include "auth-options.h" | |
51 | #include "misc.h" | |
52 | #include "hostfile.h" | |
53 | #include "canohost.h" | |
54 | #include "match.h" | |
55 | ||
5598e598 | 56 | #ifdef GSSAPI |
57 | #include "ssh-gss.h" | |
58 | #endif | |
59 | ||
3c0ef626 | 60 | /* import */ |
61 | extern ServerOptions options; | |
62 | extern u_char *session_id2; | |
63 | extern int session_id2_len; | |
64 | ||
65 | static Authctxt *x_authctxt = NULL; | |
66 | static int one = 1; | |
67 | ||
68 | typedef struct Authmethod Authmethod; | |
69 | struct Authmethod { | |
70 | char *name; | |
71 | int (*userauth)(Authctxt *authctxt); | |
72 | int *enabled; | |
73 | }; | |
74 | ||
75 | /* protocol */ | |
76 | ||
1e608e42 | 77 | static void input_service_request(int, u_int32_t, void *); |
78 | static void input_userauth_request(int, u_int32_t, void *); | |
3c0ef626 | 79 | |
80 | /* helper */ | |
81 | static Authmethod *authmethod_lookup(const char *); | |
82 | static char *authmethods_get(void); | |
83 | static int user_key_allowed(struct passwd *, Key *); | |
84 | static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *); | |
85 | ||
86 | /* auth */ | |
87 | static void userauth_banner(void); | |
88 | static int userauth_none(Authctxt *); | |
89 | static int userauth_passwd(Authctxt *); | |
90 | static int userauth_pubkey(Authctxt *); | |
91 | static int userauth_hostbased(Authctxt *); | |
92 | static int userauth_kbdint(Authctxt *); | |
93 | ||
5598e598 | 94 | #ifdef GSSAPI |
95 | int userauth_external(Authctxt *authctxt); | |
96 | int userauth_gssapi(Authctxt *authctxt); | |
97 | #endif | |
98 | ||
3c0ef626 | 99 | Authmethod authmethods[] = { |
100 | {"none", | |
101 | userauth_none, | |
102 | &one}, | |
5598e598 | 103 | #ifdef GSSAPI |
104 | {"external-keyx", | |
105 | userauth_external, | |
106 | &options.gss_authentication}, | |
107 | {"gssapi", | |
108 | userauth_gssapi, | |
109 | &options.gss_authentication}, | |
110 | #endif | |
3c0ef626 | 111 | {"publickey", |
112 | userauth_pubkey, | |
113 | &options.pubkey_authentication}, | |
114 | {"password", | |
115 | userauth_passwd, | |
116 | &options.password_authentication}, | |
117 | {"keyboard-interactive", | |
118 | userauth_kbdint, | |
119 | &options.kbd_interactive_authentication}, | |
120 | {"hostbased", | |
121 | userauth_hostbased, | |
122 | &options.hostbased_authentication}, | |
123 | {NULL, NULL, NULL} | |
124 | }; | |
125 | ||
126 | /* | |
127 | * loop until authctxt->success == TRUE | |
128 | */ | |
129 | ||
130 | void | |
1e608e42 | 131 | do_authentication2(void) |
3c0ef626 | 132 | { |
133 | Authctxt *authctxt = authctxt_new(); | |
134 | ||
135 | x_authctxt = authctxt; /*XXX*/ | |
136 | ||
137 | /* challenge-response is implemented via keyboard interactive */ | |
138 | if (options.challenge_response_authentication) | |
139 | options.kbd_interactive_authentication = 1; | |
140 | if (options.pam_authentication_via_kbd_int) | |
141 | options.kbd_interactive_authentication = 1; | |
142 | ||
1e608e42 | 143 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 144 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
145 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
146 | do_authenticated(authctxt); | |
147 | } | |
148 | ||
149 | static void | |
1e608e42 | 150 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 151 | { |
152 | Authctxt *authctxt = ctxt; | |
153 | u_int len; | |
154 | int accept = 0; | |
155 | char *service = packet_get_string(&len); | |
1e608e42 | 156 | packet_check_eom(); |
3c0ef626 | 157 | |
158 | if (authctxt == NULL) | |
159 | fatal("input_service_request: no authctxt"); | |
160 | ||
161 | if (strcmp(service, "ssh-userauth") == 0) { | |
162 | if (!authctxt->success) { | |
163 | accept = 1; | |
164 | /* now we can handle user-auth requests */ | |
165 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
166 | } | |
167 | } | |
168 | /* XXX all other service requests are denied */ | |
169 | ||
170 | if (accept) { | |
171 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
172 | packet_put_cstring(service); | |
173 | packet_send(); | |
174 | packet_write_wait(); | |
175 | } else { | |
176 | debug("bad service request %s", service); | |
177 | packet_disconnect("bad service request %s", service); | |
178 | } | |
179 | xfree(service); | |
180 | } | |
181 | ||
182 | static void | |
1e608e42 | 183 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 184 | { |
185 | Authctxt *authctxt = ctxt; | |
186 | Authmethod *m = NULL; | |
187 | char *user, *service, *method, *style = NULL; | |
188 | int authenticated = 0; | |
189 | ||
190 | if (authctxt == NULL) | |
191 | fatal("input_userauth_request: no authctxt"); | |
192 | ||
193 | user = packet_get_string(NULL); | |
194 | service = packet_get_string(NULL); | |
195 | method = packet_get_string(NULL); | |
196 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
197 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); | |
198 | ||
199 | if ((style = strchr(user, ':')) != NULL) | |
200 | *style++ = 0; | |
201 | ||
202 | if (authctxt->attempt++ == 0) { | |
203 | /* setup auth context */ | |
204 | struct passwd *pw = NULL; | |
205 | pw = getpwnam(user); | |
206 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { | |
207 | authctxt->pw = pwcopy(pw); | |
208 | authctxt->valid = 1; | |
209 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
210 | #ifdef USE_PAM | |
211 | start_pam(pw->pw_name); | |
212 | #endif | |
213 | } else { | |
214 | log("input_userauth_request: illegal user %s", user); | |
215 | #ifdef USE_PAM | |
216 | start_pam("NOUSER"); | |
217 | #endif | |
218 | } | |
219 | setproctitle("%s", pw ? user : "unknown"); | |
220 | authctxt->user = xstrdup(user); | |
221 | authctxt->service = xstrdup(service); | |
222 | authctxt->style = style ? xstrdup(style) : NULL; | |
223 | } else if (strcmp(user, authctxt->user) != 0 || | |
224 | strcmp(service, authctxt->service) != 0) { | |
225 | packet_disconnect("Change of username or service not allowed: " | |
226 | "(%s,%s) -> (%s,%s)", | |
227 | authctxt->user, authctxt->service, user, service); | |
228 | } | |
229 | /* reset state */ | |
1e608e42 | 230 | auth2_challenge_stop(authctxt); |
5598e598 | 231 | |
232 | #ifdef GSSAPI | |
1e608e42 | 233 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
234 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
5598e598 | 235 | #endif |
236 | ||
3c0ef626 | 237 | authctxt->postponed = 0; |
3c0ef626 | 238 | |
239 | /* try to authenticate user */ | |
240 | m = authmethod_lookup(method); | |
241 | if (m != NULL) { | |
242 | debug2("input_userauth_request: try method %s", method); | |
243 | authenticated = m->userauth(authctxt); | |
244 | } | |
245 | userauth_finish(authctxt, authenticated, method); | |
246 | ||
247 | xfree(service); | |
248 | xfree(user); | |
249 | xfree(method); | |
250 | } | |
251 | ||
252 | void | |
253 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
254 | { | |
255 | char *methods; | |
256 | ||
257 | if (!authctxt->valid && authenticated) | |
258 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
259 | authctxt->user); | |
260 | ||
261 | /* Special handling for root */ | |
262 | if (authenticated && authctxt->pw->pw_uid == 0 && | |
263 | !auth_root_allowed(method)) | |
264 | authenticated = 0; | |
265 | ||
266 | #ifdef USE_PAM | |
267 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, | |
268 | NULL)) | |
269 | authenticated = 0; | |
270 | #endif /* USE_PAM */ | |
271 | ||
272 | /* Log before sending the reply */ | |
a2d82e42 | 273 | if (!compat20) |
274 | auth_log(authctxt, authenticated, method, " ssh1"); | |
275 | else | |
3c0ef626 | 276 | auth_log(authctxt, authenticated, method, " ssh2"); |
277 | ||
278 | if (authctxt->postponed) | |
279 | return; | |
280 | ||
281 | /* XXX todo: check if multiple auth methods are needed */ | |
282 | if (authenticated == 1) { | |
283 | /* turn off userauth */ | |
1e608e42 | 284 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
a2d82e42 | 285 | if (!compat20) |
286 | packet_start(SSH_SMSG_SUCCESS); | |
287 | else | |
3c0ef626 | 288 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
289 | packet_send(); | |
290 | packet_write_wait(); | |
291 | /* now we can break out */ | |
292 | authctxt->success = 1; | |
293 | } else { | |
294 | if (authctxt->failures++ > AUTH_FAIL_MAX) { | |
295 | #ifdef WITH_AIXAUTHENTICATE | |
296 | loginfailed(authctxt->user, | |
1e608e42 | 297 | get_canonical_hostname(options.verify_reverse_mapping), |
3c0ef626 | 298 | "ssh"); |
299 | #endif /* WITH_AIXAUTHENTICATE */ | |
300 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
301 | } | |
302 | methods = authmethods_get(); | |
a2d82e42 | 303 | if (!compat20) |
304 | packet_disconnect("GSSAPI authentication failed"); | |
305 | else | |
3c0ef626 | 306 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
307 | packet_put_cstring(methods); | |
308 | packet_put_char(0); /* XXX partial success, unused */ | |
309 | packet_send(); | |
310 | packet_write_wait(); | |
311 | xfree(methods); | |
312 | } | |
313 | } | |
314 | ||
315 | static void | |
316 | userauth_banner(void) | |
317 | { | |
318 | struct stat st; | |
319 | char *banner = NULL; | |
320 | off_t len, n; | |
321 | int fd; | |
322 | ||
323 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) | |
324 | return; | |
325 | if ((fd = open(options.banner, O_RDONLY)) < 0) | |
326 | return; | |
327 | if (fstat(fd, &st) < 0) | |
328 | goto done; | |
329 | len = st.st_size; | |
330 | banner = xmalloc(len + 1); | |
331 | if ((n = read(fd, banner, len)) < 0) | |
332 | goto done; | |
333 | banner[n] = '\0'; | |
334 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
335 | packet_put_cstring(banner); | |
336 | packet_put_cstring(""); /* language, unused */ | |
337 | packet_send(); | |
338 | debug("userauth_banner: sent"); | |
339 | done: | |
340 | if (banner) | |
341 | xfree(banner); | |
342 | close(fd); | |
343 | return; | |
344 | } | |
345 | ||
346 | static int | |
347 | userauth_none(Authctxt *authctxt) | |
348 | { | |
349 | /* disable method "none", only allowed one time */ | |
350 | Authmethod *m = authmethod_lookup("none"); | |
351 | if (m != NULL) | |
352 | m->enabled = NULL; | |
1e608e42 | 353 | packet_check_eom(); |
3c0ef626 | 354 | userauth_banner(); |
355 | ||
356 | if (authctxt->valid == 0) | |
357 | return(0); | |
358 | ||
359 | #ifdef HAVE_CYGWIN | |
1e608e42 | 360 | if (check_nt_auth(1, authctxt->pw) == 0) |
3c0ef626 | 361 | return(0); |
362 | #endif | |
363 | #ifdef USE_PAM | |
364 | return auth_pam_password(authctxt->pw, ""); | |
365 | #elif defined(HAVE_OSF_SIA) | |
366 | return 0; | |
367 | #else /* !HAVE_OSF_SIA && !USE_PAM */ | |
368 | return auth_password(authctxt, ""); | |
369 | #endif /* USE_PAM */ | |
370 | } | |
371 | ||
372 | static int | |
373 | userauth_passwd(Authctxt *authctxt) | |
374 | { | |
375 | char *password; | |
376 | int authenticated = 0; | |
377 | int change; | |
378 | u_int len; | |
379 | change = packet_get_char(); | |
380 | if (change) | |
381 | log("password change not supported"); | |
382 | password = packet_get_string(&len); | |
1e608e42 | 383 | packet_check_eom(); |
3c0ef626 | 384 | if (authctxt->valid && |
385 | #ifdef HAVE_CYGWIN | |
1e608e42 | 386 | check_nt_auth(1, authctxt->pw) && |
3c0ef626 | 387 | #endif |
388 | #ifdef USE_PAM | |
389 | auth_pam_password(authctxt->pw, password) == 1) | |
390 | #elif defined(HAVE_OSF_SIA) | |
391 | auth_sia_password(authctxt->user, password) == 1) | |
392 | #else /* !USE_PAM && !HAVE_OSF_SIA */ | |
393 | auth_password(authctxt, password) == 1) | |
394 | #endif /* USE_PAM */ | |
395 | authenticated = 1; | |
396 | memset(password, 0, len); | |
397 | xfree(password); | |
398 | return authenticated; | |
399 | } | |
400 | ||
401 | static int | |
402 | userauth_kbdint(Authctxt *authctxt) | |
403 | { | |
404 | int authenticated = 0; | |
405 | char *lang, *devs; | |
406 | ||
407 | lang = packet_get_string(NULL); | |
408 | devs = packet_get_string(NULL); | |
1e608e42 | 409 | packet_check_eom(); |
3c0ef626 | 410 | |
411 | debug("keyboard-interactive devs %s", devs); | |
412 | ||
413 | if (options.challenge_response_authentication) | |
414 | authenticated = auth2_challenge(authctxt, devs); | |
415 | ||
416 | #ifdef USE_PAM | |
417 | if (authenticated == 0 && options.pam_authentication_via_kbd_int) | |
418 | authenticated = auth2_pam(authctxt); | |
419 | #endif | |
420 | xfree(devs); | |
421 | xfree(lang); | |
422 | #ifdef HAVE_CYGWIN | |
1e608e42 | 423 | if (check_nt_auth(0, authctxt->pw) == 0) |
3c0ef626 | 424 | return(0); |
425 | #endif | |
426 | return authenticated; | |
427 | } | |
428 | ||
429 | static int | |
430 | userauth_pubkey(Authctxt *authctxt) | |
431 | { | |
432 | Buffer b; | |
1e608e42 | 433 | Key *key = NULL; |
434 | char *pkalg; | |
435 | u_char *pkblob, *sig; | |
3c0ef626 | 436 | u_int alen, blen, slen; |
437 | int have_sig, pktype; | |
438 | int authenticated = 0; | |
439 | ||
440 | if (!authctxt->valid) { | |
441 | debug2("userauth_pubkey: disabled because of invalid user"); | |
442 | return 0; | |
443 | } | |
444 | have_sig = packet_get_char(); | |
445 | if (datafellows & SSH_BUG_PKAUTH) { | |
446 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
447 | /* no explicit pkalg given */ | |
448 | pkblob = packet_get_string(&blen); | |
449 | buffer_init(&b); | |
450 | buffer_append(&b, pkblob, blen); | |
451 | /* so we have to extract the pkalg from the pkblob */ | |
452 | pkalg = buffer_get_string(&b, &alen); | |
453 | buffer_free(&b); | |
454 | } else { | |
455 | pkalg = packet_get_string(&alen); | |
456 | pkblob = packet_get_string(&blen); | |
457 | } | |
458 | pktype = key_type_from_name(pkalg); | |
459 | if (pktype == KEY_UNSPEC) { | |
460 | /* this is perfectly legal */ | |
1e608e42 | 461 | log("userauth_pubkey: unsupported public key algorithm: %s", |
462 | pkalg); | |
463 | goto done; | |
3c0ef626 | 464 | } |
465 | key = key_from_blob(pkblob, blen); | |
1e608e42 | 466 | if (key == NULL) { |
467 | error("userauth_pubkey: cannot decode key: %s", pkalg); | |
468 | goto done; | |
469 | } | |
470 | if (key->type != pktype) { | |
471 | error("userauth_pubkey: type mismatch for decoded key " | |
472 | "(received %d, expected %d)", key->type, pktype); | |
473 | goto done; | |
474 | } | |
475 | if (have_sig) { | |
476 | sig = packet_get_string(&slen); | |
477 | packet_check_eom(); | |
478 | buffer_init(&b); | |
479 | if (datafellows & SSH_OLD_SESSIONID) { | |
480 | buffer_append(&b, session_id2, session_id2_len); | |
481 | } else { | |
482 | buffer_put_string(&b, session_id2, session_id2_len); | |
483 | } | |
484 | /* reconstruct packet */ | |
485 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
486 | buffer_put_cstring(&b, authctxt->user); | |
487 | buffer_put_cstring(&b, | |
488 | datafellows & SSH_BUG_PKSERVICE ? | |
489 | "ssh-userauth" : | |
490 | authctxt->service); | |
491 | if (datafellows & SSH_BUG_PKAUTH) { | |
492 | buffer_put_char(&b, have_sig); | |
493 | } else { | |
494 | buffer_put_cstring(&b, "publickey"); | |
495 | buffer_put_char(&b, have_sig); | |
496 | buffer_put_cstring(&b, pkalg); | |
497 | } | |
498 | buffer_put_string(&b, pkblob, blen); | |
3c0ef626 | 499 | #ifdef DEBUG_PK |
1e608e42 | 500 | buffer_dump(&b); |
3c0ef626 | 501 | #endif |
1e608e42 | 502 | /* test for correct signature */ |
503 | if (user_key_allowed(authctxt->pw, key) && | |
504 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) | |
505 | authenticated = 1; | |
506 | buffer_clear(&b); | |
507 | xfree(sig); | |
508 | } else { | |
509 | debug("test whether pkalg/pkblob are acceptable"); | |
510 | packet_check_eom(); | |
511 | ||
512 | /* XXX fake reply and always send PK_OK ? */ | |
513 | /* | |
514 | * XXX this allows testing whether a user is allowed | |
515 | * to login: if you happen to have a valid pubkey this | |
516 | * message is sent. the message is NEVER sent at all | |
517 | * if a user is not allowed to login. is this an | |
518 | * issue? -markus | |
519 | */ | |
520 | if (user_key_allowed(authctxt->pw, key)) { | |
521 | packet_start(SSH2_MSG_USERAUTH_PK_OK); | |
522 | packet_put_string(pkalg, alen); | |
523 | packet_put_string(pkblob, blen); | |
524 | packet_send(); | |
525 | packet_write_wait(); | |
526 | authctxt->postponed = 1; | |
3c0ef626 | 527 | } |
3c0ef626 | 528 | } |
1e608e42 | 529 | if (authenticated != 1) |
530 | auth_clear_options(); | |
531 | done: | |
3c0ef626 | 532 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
1e608e42 | 533 | if (key != NULL) |
534 | key_free(key); | |
3c0ef626 | 535 | xfree(pkalg); |
536 | xfree(pkblob); | |
537 | #ifdef HAVE_CYGWIN | |
1e608e42 | 538 | if (check_nt_auth(0, authctxt->pw) == 0) |
3c0ef626 | 539 | return(0); |
540 | #endif | |
541 | return authenticated; | |
542 | } | |
543 | ||
544 | static int | |
545 | userauth_hostbased(Authctxt *authctxt) | |
546 | { | |
547 | Buffer b; | |
1e608e42 | 548 | Key *key = NULL; |
549 | char *pkalg, *cuser, *chost, *service; | |
550 | u_char *pkblob, *sig; | |
3c0ef626 | 551 | u_int alen, blen, slen; |
552 | int pktype; | |
553 | int authenticated = 0; | |
554 | ||
555 | if (!authctxt->valid) { | |
556 | debug2("userauth_hostbased: disabled because of invalid user"); | |
557 | return 0; | |
558 | } | |
559 | pkalg = packet_get_string(&alen); | |
560 | pkblob = packet_get_string(&blen); | |
561 | chost = packet_get_string(NULL); | |
562 | cuser = packet_get_string(NULL); | |
563 | sig = packet_get_string(&slen); | |
564 | ||
565 | debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d", | |
566 | cuser, chost, pkalg, slen); | |
567 | #ifdef DEBUG_PK | |
568 | debug("signature:"); | |
569 | buffer_init(&b); | |
570 | buffer_append(&b, sig, slen); | |
571 | buffer_dump(&b); | |
572 | buffer_free(&b); | |
573 | #endif | |
574 | pktype = key_type_from_name(pkalg); | |
575 | if (pktype == KEY_UNSPEC) { | |
576 | /* this is perfectly legal */ | |
577 | log("userauth_hostbased: unsupported " | |
578 | "public key algorithm: %s", pkalg); | |
579 | goto done; | |
580 | } | |
581 | key = key_from_blob(pkblob, blen); | |
582 | if (key == NULL) { | |
1e608e42 | 583 | error("userauth_hostbased: cannot decode key: %s", pkalg); |
584 | goto done; | |
585 | } | |
586 | if (key->type != pktype) { | |
587 | error("userauth_hostbased: type mismatch for decoded key " | |
588 | "(received %d, expected %d)", key->type, pktype); | |
3c0ef626 | 589 | goto done; |
590 | } | |
591 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | |
592 | authctxt->service; | |
593 | buffer_init(&b); | |
594 | buffer_put_string(&b, session_id2, session_id2_len); | |
595 | /* reconstruct packet */ | |
596 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
597 | buffer_put_cstring(&b, authctxt->user); | |
598 | buffer_put_cstring(&b, service); | |
599 | buffer_put_cstring(&b, "hostbased"); | |
600 | buffer_put_string(&b, pkalg, alen); | |
601 | buffer_put_string(&b, pkblob, blen); | |
602 | buffer_put_cstring(&b, chost); | |
603 | buffer_put_cstring(&b, cuser); | |
604 | #ifdef DEBUG_PK | |
605 | buffer_dump(&b); | |
606 | #endif | |
607 | /* test for allowed key and correct signature */ | |
608 | if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) && | |
609 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) | |
610 | authenticated = 1; | |
611 | ||
612 | buffer_clear(&b); | |
3c0ef626 | 613 | done: |
614 | debug2("userauth_hostbased: authenticated %d", authenticated); | |
1e608e42 | 615 | if (key != NULL) |
616 | key_free(key); | |
3c0ef626 | 617 | xfree(pkalg); |
618 | xfree(pkblob); | |
619 | xfree(cuser); | |
620 | xfree(chost); | |
621 | xfree(sig); | |
622 | return authenticated; | |
623 | } | |
624 | ||
625 | /* get current user */ | |
626 | ||
627 | struct passwd* | |
628 | auth_get_user(void) | |
629 | { | |
630 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; | |
631 | } | |
632 | ||
633 | #define DELIM "," | |
634 | ||
635 | static char * | |
636 | authmethods_get(void) | |
637 | { | |
638 | Authmethod *method = NULL; | |
1e608e42 | 639 | Buffer b; |
3c0ef626 | 640 | char *list; |
641 | ||
1e608e42 | 642 | buffer_init(&b); |
3c0ef626 | 643 | for (method = authmethods; method->name != NULL; method++) { |
644 | if (strcmp(method->name, "none") == 0) | |
645 | continue; | |
646 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
1e608e42 | 647 | if (buffer_len(&b) > 0) |
648 | buffer_append(&b, ",", 1); | |
649 | buffer_append(&b, method->name, strlen(method->name)); | |
3c0ef626 | 650 | } |
651 | } | |
1e608e42 | 652 | buffer_append(&b, "\0", 1); |
653 | list = xstrdup(buffer_ptr(&b)); | |
654 | buffer_free(&b); | |
3c0ef626 | 655 | return list; |
656 | } | |
657 | ||
658 | static Authmethod * | |
659 | authmethod_lookup(const char *name) | |
660 | { | |
661 | Authmethod *method = NULL; | |
662 | if (name != NULL) | |
663 | for (method = authmethods; method->name != NULL; method++) | |
664 | if (method->enabled != NULL && | |
665 | *(method->enabled) != 0 && | |
666 | strcmp(name, method->name) == 0) | |
667 | return method; | |
668 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
669 | return NULL; | |
670 | } | |
671 | ||
672 | /* return 1 if user allows given key */ | |
673 | static int | |
674 | user_key_allowed2(struct passwd *pw, Key *key, char *file) | |
675 | { | |
676 | char line[8192]; | |
677 | int found_key = 0; | |
678 | FILE *f; | |
679 | u_long linenum = 0; | |
680 | struct stat st; | |
681 | Key *found; | |
1e608e42 | 682 | char *fp; |
3c0ef626 | 683 | |
684 | if (pw == NULL) | |
685 | return 0; | |
686 | ||
687 | /* Temporarily use the user's uid. */ | |
688 | temporarily_use_uid(pw); | |
689 | ||
690 | debug("trying public key file %s", file); | |
691 | ||
692 | /* Fail quietly if file does not exist */ | |
693 | if (stat(file, &st) < 0) { | |
694 | /* Restore the privileged uid. */ | |
695 | restore_uid(); | |
696 | return 0; | |
697 | } | |
698 | /* Open the file containing the authorized keys. */ | |
699 | f = fopen(file, "r"); | |
700 | if (!f) { | |
701 | /* Restore the privileged uid. */ | |
702 | restore_uid(); | |
703 | return 0; | |
704 | } | |
705 | if (options.strict_modes && | |
706 | secure_filename(f, file, pw, line, sizeof(line)) != 0) { | |
707 | fclose(f); | |
708 | log("Authentication refused: %s", line); | |
709 | restore_uid(); | |
710 | return 0; | |
711 | } | |
712 | ||
713 | found_key = 0; | |
714 | found = key_new(key->type); | |
715 | ||
716 | while (fgets(line, sizeof(line), f)) { | |
717 | char *cp, *options = NULL; | |
718 | linenum++; | |
719 | /* Skip leading whitespace, empty and comment lines. */ | |
720 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
721 | ; | |
722 | if (!*cp || *cp == '\n' || *cp == '#') | |
723 | continue; | |
724 | ||
725 | if (key_read(found, &cp) != 1) { | |
726 | /* no key? check if there are options for this key */ | |
727 | int quoted = 0; | |
728 | debug2("user_key_allowed: check options: '%s'", cp); | |
729 | options = cp; | |
730 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
731 | if (*cp == '\\' && cp[1] == '"') | |
732 | cp++; /* Skip both */ | |
733 | else if (*cp == '"') | |
734 | quoted = !quoted; | |
735 | } | |
736 | /* Skip remaining whitespace. */ | |
737 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
738 | ; | |
739 | if (key_read(found, &cp) != 1) { | |
740 | debug2("user_key_allowed: advance: '%s'", cp); | |
741 | /* still no key? advance to next line*/ | |
742 | continue; | |
743 | } | |
744 | } | |
745 | if (key_equal(found, key) && | |
746 | auth_parse_options(pw, options, file, linenum) == 1) { | |
747 | found_key = 1; | |
748 | debug("matching key found: file %s, line %lu", | |
749 | file, linenum); | |
1e608e42 | 750 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); |
751 | verbose("Found matching %s key: %s", | |
752 | key_type(found), fp); | |
753 | xfree(fp); | |
3c0ef626 | 754 | break; |
755 | } | |
756 | } | |
757 | restore_uid(); | |
758 | fclose(f); | |
759 | key_free(found); | |
760 | if (!found_key) | |
761 | debug2("key not found"); | |
762 | return found_key; | |
763 | } | |
764 | ||
765 | /* check whether given key is in .ssh/authorized_keys* */ | |
766 | static int | |
767 | user_key_allowed(struct passwd *pw, Key *key) | |
768 | { | |
769 | int success; | |
770 | char *file; | |
771 | ||
772 | file = authorized_keys_file(pw); | |
773 | success = user_key_allowed2(pw, key, file); | |
774 | xfree(file); | |
775 | if (success) | |
776 | return success; | |
777 | ||
778 | /* try suffix "2" for backward compat, too */ | |
779 | file = authorized_keys_file2(pw); | |
780 | success = user_key_allowed2(pw, key, file); | |
781 | xfree(file); | |
782 | return success; | |
783 | } | |
784 | ||
785 | /* return 1 if given hostkey is allowed */ | |
786 | static int | |
787 | hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, | |
788 | Key *key) | |
789 | { | |
790 | const char *resolvedname, *ipaddr, *lookup; | |
1e608e42 | 791 | HostStatus host_status; |
792 | int len; | |
3c0ef626 | 793 | |
1e608e42 | 794 | resolvedname = get_canonical_hostname(options.verify_reverse_mapping); |
3c0ef626 | 795 | ipaddr = get_remote_ipaddr(); |
796 | ||
797 | debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s", | |
798 | chost, resolvedname, ipaddr); | |
799 | ||
800 | if (options.hostbased_uses_name_from_packet_only) { | |
801 | if (auth_rhosts2(pw, cuser, chost, chost) == 0) | |
802 | return 0; | |
803 | lookup = chost; | |
804 | } else { | |
805 | if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') { | |
806 | debug2("stripping trailing dot from chost %s", chost); | |
807 | chost[len - 1] = '\0'; | |
808 | } | |
809 | if (strcasecmp(resolvedname, chost) != 0) | |
810 | log("userauth_hostbased mismatch: " | |
811 | "client sends %s, but we resolve %s to %s", | |
812 | chost, ipaddr, resolvedname); | |
813 | if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) | |
814 | return 0; | |
815 | lookup = resolvedname; | |
816 | } | |
817 | debug2("userauth_hostbased: access allowed by auth_rhosts2"); | |
818 | ||
819 | host_status = check_key_in_hostfiles(pw, key, lookup, | |
820 | _PATH_SSH_SYSTEM_HOSTFILE, | |
821 | options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); | |
822 | ||
823 | /* backward compat if no key has been found. */ | |
824 | if (host_status == HOST_NEW) | |
825 | host_status = check_key_in_hostfiles(pw, key, lookup, | |
826 | _PATH_SSH_SYSTEM_HOSTFILE2, | |
827 | options.ignore_user_known_hosts ? NULL : | |
828 | _PATH_SSH_USER_HOSTFILE2); | |
829 | ||
830 | return (host_status == HOST_OK); | |
831 | } |