Annotation of src/usr.bin/ssh/auth2.c, Revision 1.89
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.89 ! markus 26: RCSID("$OpenBSD: auth2.c,v 1.88 2002/03/18 17:50:31 provos 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.66 itojun 264: static void
1.25 markus 265: userauth_banner(void)
266: {
267: struct stat st;
268: char *banner = NULL;
269: off_t len, n;
270: int fd;
271:
272: if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
273: return;
1.50 markus 274: if ((fd = open(options.banner, O_RDONLY)) < 0)
1.25 markus 275: return;
276: if (fstat(fd, &st) < 0)
277: goto done;
278: len = st.st_size;
279: banner = xmalloc(len + 1);
280: if ((n = read(fd, banner, len)) < 0)
281: goto done;
282: banner[n] = '\0';
283: packet_start(SSH2_MSG_USERAUTH_BANNER);
284: packet_put_cstring(banner);
285: packet_put_cstring(""); /* language, unused */
286: packet_send();
287: debug("userauth_banner: sent");
288: done:
289: if (banner)
290: xfree(banner);
291: close(fd);
292: return;
1.1 markus 293: }
294:
1.66 itojun 295: static int
1.18 markus 296: userauth_none(Authctxt *authctxt)
1.1 markus 297: {
1.18 markus 298: /* disable method "none", only allowed one time */
299: Authmethod *m = authmethod_lookup("none");
300: if (m != NULL)
301: m->enabled = NULL;
1.79 markus 302: packet_check_eom();
1.25 markus 303: userauth_banner();
1.88 provos 304: return (authctxt->valid ? PRIVSEP(auth_password(authctxt, "")) : 0);
1.1 markus 305: }
1.18 markus 306:
1.66 itojun 307: static int
1.18 markus 308: userauth_passwd(Authctxt *authctxt)
1.1 markus 309: {
310: char *password;
311: int authenticated = 0;
312: int change;
1.23 markus 313: u_int len;
1.1 markus 314: change = packet_get_char();
315: if (change)
316: log("password change not supported");
317: password = packet_get_string(&len);
1.79 markus 318: packet_check_eom();
1.18 markus 319: if (authctxt->valid &&
1.88 provos 320: PRIVSEP(auth_password(authctxt, password)) == 1)
1.1 markus 321: authenticated = 1;
322: memset(password, 0, len);
323: xfree(password);
324: return authenticated;
325: }
1.18 markus 326:
1.66 itojun 327: static int
1.18 markus 328: userauth_kbdint(Authctxt *authctxt)
329: {
330: int authenticated = 0;
1.57 markus 331: char *lang, *devs;
1.77 deraadt 332:
1.18 markus 333: lang = packet_get_string(NULL);
334: devs = packet_get_string(NULL);
1.79 markus 335: packet_check_eom();
1.18 markus 336:
1.57 markus 337: debug("keyboard-interactive devs %s", devs);
1.28 markus 338:
1.57 markus 339: if (options.challenge_response_authentication)
1.34 markus 340: authenticated = auth2_challenge(authctxt, devs);
1.28 markus 341:
1.57 markus 342: xfree(devs);
1.18 markus 343: xfree(lang);
344: return authenticated;
345: }
346:
1.66 itojun 347: static int
1.18 markus 348: userauth_pubkey(Authctxt *authctxt)
1.1 markus 349: {
350: Buffer b;
1.84 markus 351: Key *key = NULL;
1.85 markus 352: char *pkalg;
353: u_char *pkblob, *sig;
1.23 markus 354: u_int alen, blen, slen;
1.21 markus 355: int have_sig, pktype;
1.1 markus 356: int authenticated = 0;
357:
1.18 markus 358: if (!authctxt->valid) {
359: debug2("userauth_pubkey: disabled because of invalid user");
1.8 markus 360: return 0;
361: }
1.1 markus 362: have_sig = packet_get_char();
1.22 markus 363: if (datafellows & SSH_BUG_PKAUTH) {
364: debug2("userauth_pubkey: SSH_BUG_PKAUTH");
365: /* no explicit pkalg given */
366: pkblob = packet_get_string(&blen);
367: buffer_init(&b);
368: buffer_append(&b, pkblob, blen);
369: /* so we have to extract the pkalg from the pkblob */
370: pkalg = buffer_get_string(&b, &alen);
371: buffer_free(&b);
372: } else {
373: pkalg = packet_get_string(&alen);
374: pkblob = packet_get_string(&blen);
375: }
1.21 markus 376: pktype = key_type_from_name(pkalg);
377: if (pktype == KEY_UNSPEC) {
1.22 markus 378: /* this is perfectly legal */
1.84 markus 379: log("userauth_pubkey: unsupported public key algorithm: %s",
380: pkalg);
381: goto done;
1.1 markus 382: }
1.21 markus 383: key = key_from_blob(pkblob, blen);
1.84 markus 384: if (key == NULL) {
385: error("userauth_pubkey: cannot decode key: %s", pkalg);
386: goto done;
387: }
388: if (key->type != pktype) {
389: error("userauth_pubkey: type mismatch for decoded key "
390: "(received %d, expected %d)", key->type, pktype);
391: goto done;
392: }
393: if (have_sig) {
394: sig = packet_get_string(&slen);
395: packet_check_eom();
396: buffer_init(&b);
397: if (datafellows & SSH_OLD_SESSIONID) {
398: buffer_append(&b, session_id2, session_id2_len);
399: } else {
400: buffer_put_string(&b, session_id2, session_id2_len);
401: }
402: /* reconstruct packet */
403: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
404: buffer_put_cstring(&b, authctxt->user);
405: buffer_put_cstring(&b,
406: datafellows & SSH_BUG_PKSERVICE ?
407: "ssh-userauth" :
408: authctxt->service);
409: if (datafellows & SSH_BUG_PKAUTH) {
410: buffer_put_char(&b, have_sig);
411: } else {
412: buffer_put_cstring(&b, "publickey");
413: buffer_put_char(&b, have_sig);
414: buffer_put_cstring(&b, pkalg);
415: }
416: buffer_put_string(&b, pkblob, blen);
1.21 markus 417: #ifdef DEBUG_PK
1.84 markus 418: buffer_dump(&b);
1.1 markus 419: #endif
1.84 markus 420: /* test for correct signature */
1.88 provos 421: authenticated = 0;
422: if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
423: PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
424: buffer_len(&b))) == 1)
1.84 markus 425: authenticated = 1;
426: buffer_clear(&b);
427: xfree(sig);
428: } else {
429: debug("test whether pkalg/pkblob are acceptable");
430: packet_check_eom();
1.18 markus 431:
1.84 markus 432: /* XXX fake reply and always send PK_OK ? */
433: /*
434: * XXX this allows testing whether a user is allowed
435: * to login: if you happen to have a valid pubkey this
436: * message is sent. the message is NEVER sent at all
437: * if a user is not allowed to login. is this an
438: * issue? -markus
439: */
1.88 provos 440: if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
1.84 markus 441: packet_start(SSH2_MSG_USERAUTH_PK_OK);
442: packet_put_string(pkalg, alen);
443: packet_put_string(pkblob, blen);
444: packet_send();
445: packet_write_wait();
446: authctxt->postponed = 1;
1.1 markus 447: }
448: }
1.84 markus 449: if (authenticated != 1)
450: auth_clear_options();
451: done:
1.21 markus 452: debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1.84 markus 453: if (key != NULL)
454: key_free(key);
1.1 markus 455: xfree(pkalg);
456: xfree(pkblob);
457: return authenticated;
458: }
459:
1.66 itojun 460: static int
1.52 markus 461: userauth_hostbased(Authctxt *authctxt)
462: {
463: Buffer b;
1.84 markus 464: Key *key = NULL;
1.85 markus 465: char *pkalg, *cuser, *chost, *service;
466: u_char *pkblob, *sig;
1.52 markus 467: u_int alen, blen, slen;
468: int pktype;
469: int authenticated = 0;
470:
471: if (!authctxt->valid) {
472: debug2("userauth_hostbased: disabled because of invalid user");
473: return 0;
474: }
475: pkalg = packet_get_string(&alen);
476: pkblob = packet_get_string(&blen);
477: chost = packet_get_string(NULL);
478: cuser = packet_get_string(NULL);
479: sig = packet_get_string(&slen);
480:
481: debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
482: cuser, chost, pkalg, slen);
483: #ifdef DEBUG_PK
484: debug("signature:");
485: buffer_init(&b);
486: buffer_append(&b, sig, slen);
487: buffer_dump(&b);
488: buffer_free(&b);
489: #endif
490: pktype = key_type_from_name(pkalg);
491: if (pktype == KEY_UNSPEC) {
492: /* this is perfectly legal */
493: log("userauth_hostbased: unsupported "
494: "public key algorithm: %s", pkalg);
495: goto done;
496: }
497: key = key_from_blob(pkblob, blen);
498: if (key == NULL) {
1.84 markus 499: error("userauth_hostbased: cannot decode key: %s", pkalg);
500: goto done;
501: }
502: if (key->type != pktype) {
503: error("userauth_hostbased: type mismatch for decoded key "
504: "(received %d, expected %d)", key->type, pktype);
1.52 markus 505: goto done;
506: }
1.55 markus 507: service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
508: authctxt->service;
1.52 markus 509: buffer_init(&b);
1.55 markus 510: buffer_put_string(&b, session_id2, session_id2_len);
1.52 markus 511: /* reconstruct packet */
512: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
513: buffer_put_cstring(&b, authctxt->user);
1.55 markus 514: buffer_put_cstring(&b, service);
1.52 markus 515: buffer_put_cstring(&b, "hostbased");
516: buffer_put_string(&b, pkalg, alen);
517: buffer_put_string(&b, pkblob, blen);
518: buffer_put_cstring(&b, chost);
519: buffer_put_cstring(&b, cuser);
520: #ifdef DEBUG_PK
521: buffer_dump(&b);
522: #endif
523: /* test for allowed key and correct signature */
1.88 provos 524: authenticated = 0;
525: if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
526: PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
527: buffer_len(&b))) == 1)
1.52 markus 528: authenticated = 1;
529:
530: buffer_clear(&b);
531: done:
532: debug2("userauth_hostbased: authenticated %d", authenticated);
1.84 markus 533: if (key != NULL)
534: key_free(key);
1.52 markus 535: xfree(pkalg);
536: xfree(pkblob);
537: xfree(cuser);
538: xfree(chost);
539: xfree(sig);
540: return authenticated;
541: }
542:
1.18 markus 543: /* get current user */
1.1 markus 544:
545: struct passwd*
546: auth_get_user(void)
547: {
1.18 markus 548: return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1 markus 549: }
550:
1.18 markus 551: #define DELIM ","
552:
1.67 stevesk 553: static char *
1.18 markus 554: authmethods_get(void)
1.1 markus 555: {
1.18 markus 556: Authmethod *method = NULL;
1.82 markus 557: Buffer b;
1.18 markus 558: char *list;
1.1 markus 559:
1.82 markus 560: buffer_init(&b);
1.18 markus 561: for (method = authmethods; method->name != NULL; method++) {
562: if (strcmp(method->name, "none") == 0)
563: continue;
564: if (method->enabled != NULL && *(method->enabled) != 0) {
1.82 markus 565: if (buffer_len(&b) > 0)
566: buffer_append(&b, ",", 1);
567: buffer_append(&b, method->name, strlen(method->name));
1.1 markus 568: }
569: }
1.82 markus 570: buffer_append(&b, "\0", 1);
571: list = xstrdup(buffer_ptr(&b));
572: buffer_free(&b);
1.18 markus 573: return list;
574: }
575:
1.66 itojun 576: static Authmethod *
1.18 markus 577: authmethod_lookup(const char *name)
578: {
579: Authmethod *method = NULL;
580: if (name != NULL)
581: for (method = authmethods; method->name != NULL; method++)
582: if (method->enabled != NULL &&
583: *(method->enabled) != 0 &&
584: strcmp(name, method->name) == 0)
585: return method;
586: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
587: return NULL;
1.1 markus 588: }
589:
590: /* return 1 if user allows given key */
1.66 itojun 591: static int
1.63 markus 592: user_key_allowed2(struct passwd *pw, Key *key, char *file)
1.1 markus 593: {
1.63 markus 594: char line[8192];
1.1 markus 595: int found_key = 0;
596: FILE *f;
1.23 markus 597: u_long linenum = 0;
1.1 markus 598: struct stat st;
599: Key *found;
1.76 jakob 600: char *fp;
1.1 markus 601:
1.18 markus 602: if (pw == NULL)
603: return 0;
604:
1.1 markus 605: /* Temporarily use the user's uid. */
1.51 markus 606: temporarily_use_uid(pw);
1.1 markus 607:
1.58 markus 608: debug("trying public key file %s", file);
1.1 markus 609:
610: /* Fail quietly if file does not exist */
611: if (stat(file, &st) < 0) {
612: /* Restore the privileged uid. */
613: restore_uid();
614: return 0;
615: }
616: /* Open the file containing the authorized keys. */
617: f = fopen(file, "r");
618: if (!f) {
619: /* Restore the privileged uid. */
620: restore_uid();
621: return 0;
622: }
1.58 markus 623: if (options.strict_modes &&
1.68 markus 624: secure_filename(f, file, pw, line, sizeof(line)) != 0) {
1.58 markus 625: fclose(f);
626: log("Authentication refused: %s", line);
627: restore_uid();
628: return 0;
1.1 markus 629: }
1.58 markus 630:
1.1 markus 631: found_key = 0;
1.16 markus 632: found = key_new(key->type);
1.1 markus 633:
634: while (fgets(line, sizeof(line), f)) {
1.10 markus 635: char *cp, *options = NULL;
1.1 markus 636: linenum++;
637: /* Skip leading whitespace, empty and comment lines. */
638: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
639: ;
640: if (!*cp || *cp == '\n' || *cp == '#')
641: continue;
1.10 markus 642:
1.70 markus 643: if (key_read(found, &cp) != 1) {
1.10 markus 644: /* no key? check if there are options for this key */
645: int quoted = 0;
1.21 markus 646: debug2("user_key_allowed: check options: '%s'", cp);
1.10 markus 647: options = cp;
648: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
649: if (*cp == '\\' && cp[1] == '"')
650: cp++; /* Skip both */
651: else if (*cp == '"')
652: quoted = !quoted;
653: }
654: /* Skip remaining whitespace. */
655: for (; *cp == ' ' || *cp == '\t'; cp++)
656: ;
1.70 markus 657: if (key_read(found, &cp) != 1) {
1.21 markus 658: debug2("user_key_allowed: advance: '%s'", cp);
1.10 markus 659: /* still no key? advance to next line*/
660: continue;
661: }
662: }
663: if (key_equal(found, key) &&
1.30 markus 664: auth_parse_options(pw, options, file, linenum) == 1) {
1.1 markus 665: found_key = 1;
1.69 stevesk 666: debug("matching key found: file %s, line %lu",
1.1 markus 667: file, linenum);
1.76 jakob 668: fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
669: verbose("Found matching %s key: %s",
1.77 deraadt 670: key_type(found), fp);
1.76 jakob 671: xfree(fp);
1.1 markus 672: break;
673: }
674: }
675: restore_uid();
676: fclose(f);
677: key_free(found);
1.46 markus 678: if (!found_key)
679: debug2("key not found");
1.1 markus 680: return found_key;
1.63 markus 681: }
682:
683: /* check whether given key is in .ssh/authorized_keys* */
1.88 provos 684: int
1.63 markus 685: user_key_allowed(struct passwd *pw, Key *key)
686: {
687: int success;
688: char *file;
689:
690: file = authorized_keys_file(pw);
691: success = user_key_allowed2(pw, key, file);
692: xfree(file);
693: if (success)
694: return success;
695:
696: /* try suffix "2" for backward compat, too */
697: file = authorized_keys_file2(pw);
698: success = user_key_allowed2(pw, key, file);
699: xfree(file);
700: return success;
1.52 markus 701: }
702:
703: /* return 1 if given hostkey is allowed */
1.88 provos 704: int
1.54 markus 705: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.52 markus 706: Key *key)
707: {
708: const char *resolvedname, *ipaddr, *lookup;
1.73 stevesk 709: HostStatus host_status;
710: int len;
1.52 markus 711:
1.83 markus 712: resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
1.52 markus 713: ipaddr = get_remote_ipaddr();
714:
1.53 markus 715: debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
716: chost, resolvedname, ipaddr);
1.52 markus 717:
718: if (options.hostbased_uses_name_from_packet_only) {
719: if (auth_rhosts2(pw, cuser, chost, chost) == 0)
720: return 0;
721: lookup = chost;
722: } else {
1.53 markus 723: if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
724: debug2("stripping trailing dot from chost %s", chost);
725: chost[len - 1] = '\0';
726: }
1.52 markus 727: if (strcasecmp(resolvedname, chost) != 0)
728: log("userauth_hostbased mismatch: "
729: "client sends %s, but we resolve %s to %s",
730: chost, ipaddr, resolvedname);
731: if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
732: return 0;
733: lookup = resolvedname;
734: }
735: debug2("userauth_hostbased: access allowed by auth_rhosts2");
736:
1.64 markus 737: host_status = check_key_in_hostfiles(pw, key, lookup,
738: _PATH_SSH_SYSTEM_HOSTFILE,
1.65 markus 739: options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
1.64 markus 740:
741: /* backward compat if no key has been found. */
742: if (host_status == HOST_NEW)
743: host_status = check_key_in_hostfiles(pw, key, lookup,
744: _PATH_SSH_SYSTEM_HOSTFILE2,
1.65 markus 745: options.ignore_user_known_hosts ? NULL :
746: _PATH_SSH_USER_HOSTFILE2);
1.52 markus 747:
748: return (host_status == HOST_OK);
1.1 markus 749: }