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