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