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