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