Annotation of src/usr.bin/ssh/auth2.c, Revision 1.64
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.64 ! markus 26: RCSID("$OpenBSD: auth2.c,v 1.63 2001/06/22 21:55:49 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"
53: #include "tildexpand.h"
1.57 markus 54: #include "match.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.18 markus 61: static Authctxt *x_authctxt = NULL;
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.15 markus 73: void input_service_request(int type, int plen, void *ctxt);
74: void input_userauth_request(int type, int plen, void *ctxt);
75: void protocol_error(int type, int plen, void *ctxt);
1.1 markus 76:
77: /* helper */
1.18 markus 78: Authmethod *authmethod_lookup(const char *name);
1.52 markus 79: char *authmethods_get(void);
1.21 markus 80: int user_key_allowed(struct passwd *pw, Key *key);
1.52 markus 81: int
1.54 markus 82: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.52 markus 83: Key *key);
1.1 markus 84:
1.18 markus 85: /* auth */
1.25 markus 86: void userauth_banner(void);
1.49 markus 87: void userauth_reply(Authctxt *authctxt, int authenticated);
1.18 markus 88: int userauth_none(Authctxt *authctxt);
89: int userauth_passwd(Authctxt *authctxt);
90: int userauth_pubkey(Authctxt *authctxt);
1.52 markus 91: int userauth_hostbased(Authctxt *authctxt);
1.18 markus 92: int userauth_kbdint(Authctxt *authctxt);
93:
94: Authmethod authmethods[] = {
95: {"none",
96: userauth_none,
97: &one},
98: {"publickey",
99: userauth_pubkey,
1.21 markus 100: &options.pubkey_authentication},
1.40 markus 101: {"password",
102: userauth_passwd,
103: &options.password_authentication},
1.18 markus 104: {"keyboard-interactive",
105: userauth_kbdint,
106: &options.kbd_interactive_authentication},
1.52 markus 107: {"hostbased",
108: userauth_hostbased,
109: &options.hostbased_authentication},
1.18 markus 110: {NULL, NULL, NULL}
1.1 markus 111: };
112:
113: /*
1.18 markus 114: * loop until authctxt->success == TRUE
1.1 markus 115: */
116:
117: void
118: do_authentication2()
119: {
1.28 markus 120: Authctxt *authctxt = authctxt_new();
121:
1.18 markus 122: x_authctxt = authctxt; /*XXX*/
123:
1.34 markus 124: /* challenge-reponse is implemented via keyboard interactive */
1.57 markus 125: if (options.challenge_response_authentication)
1.34 markus 126: options.kbd_interactive_authentication = 1;
127:
1.1 markus 128: dispatch_init(&protocol_error);
129: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18 markus 130: dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.48 markus 131: do_authenticated(authctxt);
1.1 markus 132: }
133:
134: void
1.15 markus 135: protocol_error(int type, int plen, void *ctxt)
1.1 markus 136: {
137: log("auth: protocol error: type %d plen %d", type, plen);
138: packet_start(SSH2_MSG_UNIMPLEMENTED);
139: packet_put_int(0);
140: packet_send();
141: packet_write_wait();
142: }
143:
144: void
1.15 markus 145: input_service_request(int type, int plen, void *ctxt)
1.1 markus 146: {
1.18 markus 147: Authctxt *authctxt = ctxt;
1.23 markus 148: u_int len;
1.1 markus 149: int accept = 0;
150: char *service = packet_get_string(&len);
151: packet_done();
152:
1.18 markus 153: if (authctxt == NULL)
154: fatal("input_service_request: no authctxt");
155:
1.1 markus 156: if (strcmp(service, "ssh-userauth") == 0) {
1.18 markus 157: if (!authctxt->success) {
1.1 markus 158: accept = 1;
159: /* now we can handle user-auth requests */
160: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
161: }
162: }
163: /* XXX all other service requests are denied */
164:
165: if (accept) {
166: packet_start(SSH2_MSG_SERVICE_ACCEPT);
167: packet_put_cstring(service);
168: packet_send();
169: packet_write_wait();
170: } else {
171: debug("bad service request %s", service);
172: packet_disconnect("bad service request %s", service);
173: }
174: xfree(service);
175: }
176:
177: void
1.15 markus 178: input_userauth_request(int type, int plen, void *ctxt)
1.1 markus 179: {
1.18 markus 180: Authctxt *authctxt = ctxt;
181: Authmethod *m = NULL;
1.28 markus 182: char *user, *service, *method, *style = NULL;
1.1 markus 183: int authenticated = 0;
184:
1.18 markus 185: if (authctxt == NULL)
186: fatal("input_userauth_request: no authctxt");
1.1 markus 187:
1.18 markus 188: user = packet_get_string(NULL);
189: service = packet_get_string(NULL);
190: method = packet_get_string(NULL);
1.1 markus 191: debug("userauth-request for user %s service %s method %s", user, service, method);
1.24 markus 192: debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1 markus 193:
1.28 markus 194: if ((style = strchr(user, ':')) != NULL)
195: *style++ = 0;
196:
1.36 stevesk 197: if (authctxt->attempt++ == 0) {
1.18 markus 198: /* setup auth context */
199: struct passwd *pw = NULL;
200: pw = getpwnam(user);
201: if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
202: authctxt->pw = pwcopy(pw);
203: authctxt->valid = 1;
204: debug2("input_userauth_request: setting up authctxt for %s", user);
205: } else {
206: log("input_userauth_request: illegal user %s", user);
207: }
1.42 markus 208: setproctitle("%s", pw ? user : "unknown");
1.18 markus 209: authctxt->user = xstrdup(user);
210: authctxt->service = xstrdup(service);
1.62 markus 211: authctxt->style = style ? xstrdup(style) : NULL;
212: } else if (strcmp(user, authctxt->user) != 0 ||
213: strcmp(service, authctxt->service) != 0) {
214: packet_disconnect("Change of username or service not allowed: "
215: "(%s,%s) -> (%s,%s)",
216: authctxt->user, authctxt->service, user, service);
1.1 markus 217: }
1.28 markus 218: /* reset state */
219: dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
220: authctxt->postponed = 0;
1.47 markus 221: #ifdef BSD_AUTH
222: if (authctxt->as) {
223: auth_close(authctxt->as);
224: authctxt->as = NULL;
225: }
226: #endif
1.18 markus 227:
1.28 markus 228: /* try to authenticate user */
1.18 markus 229: m = authmethod_lookup(method);
230: if (m != NULL) {
231: debug2("input_userauth_request: try method %s", method);
232: authenticated = m->userauth(authctxt);
233: }
1.49 markus 234: userauth_finish(authctxt, authenticated, method);
235:
236: xfree(service);
237: xfree(user);
238: xfree(method);
239: }
240:
241: void
242: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
243: {
1.60 markus 244: char *methods;
245:
1.28 markus 246: if (!authctxt->valid && authenticated)
247: fatal("INTERNAL ERROR: authenticated invalid user %s",
248: authctxt->user);
1.18 markus 249:
250: /* Special handling for root */
1.41 markus 251: if (authenticated && authctxt->pw->pw_uid == 0 &&
252: !auth_root_allowed(method))
1.3 markus 253: authenticated = 0;
254:
1.18 markus 255: /* Log before sending the reply */
1.28 markus 256: auth_log(authctxt, authenticated, method, " ssh2");
257:
1.60 markus 258: if (authctxt->postponed)
259: return;
260:
261: /* XXX todo: check if multiple auth methods are needed */
262: if (authenticated == 1) {
263: /* turn off userauth */
264: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
265: packet_start(SSH2_MSG_USERAUTH_SUCCESS);
266: packet_send();
267: packet_write_wait();
268: /* now we can break out */
269: authctxt->success = 1;
270: } else {
271: if (authctxt->failures++ > AUTH_FAIL_MAX)
272: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
273: methods = authmethods_get();
274: packet_start(SSH2_MSG_USERAUTH_FAILURE);
275: packet_put_cstring(methods);
276: packet_put_char(0); /* XXX partial success, unused */
277: packet_send();
278: packet_write_wait();
279: xfree(methods);
280: }
1.18 markus 281: }
282:
1.25 markus 283: void
284: userauth_banner(void)
285: {
286: struct stat st;
287: char *banner = NULL;
288: off_t len, n;
289: int fd;
290:
291: if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
292: return;
1.50 markus 293: if ((fd = open(options.banner, O_RDONLY)) < 0)
1.25 markus 294: return;
295: if (fstat(fd, &st) < 0)
296: goto done;
297: len = st.st_size;
298: banner = xmalloc(len + 1);
299: if ((n = read(fd, banner, len)) < 0)
300: goto done;
301: banner[n] = '\0';
302: packet_start(SSH2_MSG_USERAUTH_BANNER);
303: packet_put_cstring(banner);
304: packet_put_cstring(""); /* language, unused */
305: packet_send();
306: debug("userauth_banner: sent");
307: done:
308: if (banner)
309: xfree(banner);
310: close(fd);
311: return;
1.1 markus 312: }
313:
314: int
1.18 markus 315: userauth_none(Authctxt *authctxt)
1.1 markus 316: {
1.18 markus 317: /* disable method "none", only allowed one time */
318: Authmethod *m = authmethod_lookup("none");
319: if (m != NULL)
320: m->enabled = NULL;
1.1 markus 321: packet_done();
1.25 markus 322: userauth_banner();
1.47 markus 323: return authctxt->valid ? auth_password(authctxt, "") : 0;
1.1 markus 324: }
1.18 markus 325:
1.1 markus 326: int
1.18 markus 327: userauth_passwd(Authctxt *authctxt)
1.1 markus 328: {
329: char *password;
330: int authenticated = 0;
331: int change;
1.23 markus 332: u_int len;
1.1 markus 333: change = packet_get_char();
334: if (change)
335: log("password change not supported");
336: password = packet_get_string(&len);
337: packet_done();
1.18 markus 338: if (authctxt->valid &&
1.47 markus 339: auth_password(authctxt, password) == 1)
1.1 markus 340: authenticated = 1;
341: memset(password, 0, len);
342: xfree(password);
343: return authenticated;
344: }
1.18 markus 345:
346: int
347: userauth_kbdint(Authctxt *authctxt)
348: {
349: int authenticated = 0;
1.57 markus 350: char *lang, *devs;
351:
1.18 markus 352: lang = packet_get_string(NULL);
353: devs = packet_get_string(NULL);
354: packet_done();
355:
1.57 markus 356: debug("keyboard-interactive devs %s", devs);
1.28 markus 357:
1.57 markus 358: if (options.challenge_response_authentication)
1.34 markus 359: authenticated = auth2_challenge(authctxt, devs);
1.28 markus 360:
1.57 markus 361: xfree(devs);
1.18 markus 362: xfree(lang);
363: return authenticated;
364: }
365:
1.1 markus 366: int
1.18 markus 367: userauth_pubkey(Authctxt *authctxt)
1.1 markus 368: {
369: Buffer b;
370: Key *key;
371: char *pkalg, *pkblob, *sig;
1.23 markus 372: u_int alen, blen, slen;
1.21 markus 373: int have_sig, pktype;
1.1 markus 374: int authenticated = 0;
375:
1.18 markus 376: if (!authctxt->valid) {
377: debug2("userauth_pubkey: disabled because of invalid user");
1.8 markus 378: return 0;
379: }
1.1 markus 380: have_sig = packet_get_char();
1.22 markus 381: if (datafellows & SSH_BUG_PKAUTH) {
382: debug2("userauth_pubkey: SSH_BUG_PKAUTH");
383: /* no explicit pkalg given */
384: pkblob = packet_get_string(&blen);
385: buffer_init(&b);
386: buffer_append(&b, pkblob, blen);
387: /* so we have to extract the pkalg from the pkblob */
388: pkalg = buffer_get_string(&b, &alen);
389: buffer_free(&b);
390: } else {
391: pkalg = packet_get_string(&alen);
392: pkblob = packet_get_string(&blen);
393: }
1.21 markus 394: pktype = key_type_from_name(pkalg);
395: if (pktype == KEY_UNSPEC) {
1.22 markus 396: /* this is perfectly legal */
397: log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
1.1 markus 398: xfree(pkalg);
1.22 markus 399: xfree(pkblob);
1.1 markus 400: return 0;
401: }
1.21 markus 402: key = key_from_blob(pkblob, blen);
1.2 markus 403: if (key != NULL) {
404: if (have_sig) {
405: sig = packet_get_string(&slen);
406: packet_done();
407: buffer_init(&b);
1.20 markus 408: if (datafellows & SSH_OLD_SESSIONID) {
409: buffer_append(&b, session_id2, session_id2_len);
410: } else {
1.11 markus 411: buffer_put_string(&b, session_id2, session_id2_len);
412: }
1.9 markus 413: /* reconstruct packet */
1.2 markus 414: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.18 markus 415: buffer_put_cstring(&b, authctxt->user);
1.9 markus 416: buffer_put_cstring(&b,
1.22 markus 417: datafellows & SSH_BUG_PKSERVICE ?
1.9 markus 418: "ssh-userauth" :
1.18 markus 419: authctxt->service);
1.22 markus 420: if (datafellows & SSH_BUG_PKAUTH) {
421: buffer_put_char(&b, have_sig);
422: } else {
423: buffer_put_cstring(&b, "publickey");
424: buffer_put_char(&b, have_sig);
1.56 markus 425: buffer_put_cstring(&b, pkalg);
1.22 markus 426: }
1.9 markus 427: buffer_put_string(&b, pkblob, blen);
1.21 markus 428: #ifdef DEBUG_PK
1.2 markus 429: buffer_dump(&b);
1.1 markus 430: #endif
1.2 markus 431: /* test for correct signature */
1.21 markus 432: if (user_key_allowed(authctxt->pw, key) &&
433: key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
1.2 markus 434: authenticated = 1;
435: buffer_clear(&b);
436: xfree(sig);
437: } else {
1.18 markus 438: debug("test whether pkalg/pkblob are acceptable");
1.2 markus 439: packet_done();
1.18 markus 440:
1.2 markus 441: /* XXX fake reply and always send PK_OK ? */
1.6 markus 442: /*
443: * XXX this allows testing whether a user is allowed
444: * to login: if you happen to have a valid pubkey this
445: * message is sent. the message is NEVER sent at all
446: * if a user is not allowed to login. is this an
447: * issue? -markus
448: */
1.21 markus 449: if (user_key_allowed(authctxt->pw, key)) {
1.2 markus 450: packet_start(SSH2_MSG_USERAUTH_PK_OK);
451: packet_put_string(pkalg, alen);
452: packet_put_string(pkblob, blen);
453: packet_send();
454: packet_write_wait();
1.38 markus 455: authctxt->postponed = 1;
1.2 markus 456: }
1.1 markus 457: }
1.17 markus 458: if (authenticated != 1)
459: auth_clear_options();
1.2 markus 460: key_free(key);
1.1 markus 461: }
1.21 markus 462: debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1.1 markus 463: xfree(pkalg);
464: xfree(pkblob);
465: return authenticated;
466: }
467:
1.52 markus 468: int
469: userauth_hostbased(Authctxt *authctxt)
470: {
471: Buffer b;
472: Key *key;
1.55 markus 473: char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
1.52 markus 474: u_int alen, blen, slen;
475: int pktype;
476: int authenticated = 0;
477:
478: if (!authctxt->valid) {
479: debug2("userauth_hostbased: disabled because of invalid user");
480: return 0;
481: }
482: pkalg = packet_get_string(&alen);
483: pkblob = packet_get_string(&blen);
484: chost = packet_get_string(NULL);
485: cuser = packet_get_string(NULL);
486: sig = packet_get_string(&slen);
487:
488: debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
489: cuser, chost, pkalg, slen);
490: #ifdef DEBUG_PK
491: debug("signature:");
492: buffer_init(&b);
493: buffer_append(&b, sig, slen);
494: buffer_dump(&b);
495: buffer_free(&b);
496: #endif
497: pktype = key_type_from_name(pkalg);
498: if (pktype == KEY_UNSPEC) {
499: /* this is perfectly legal */
500: log("userauth_hostbased: unsupported "
501: "public key algorithm: %s", pkalg);
502: goto done;
503: }
504: key = key_from_blob(pkblob, blen);
505: if (key == NULL) {
506: debug("userauth_hostbased: cannot decode key: %s", pkalg);
507: goto done;
508: }
1.55 markus 509: service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
510: authctxt->service;
1.52 markus 511: buffer_init(&b);
1.55 markus 512: buffer_put_string(&b, session_id2, session_id2_len);
1.52 markus 513: /* reconstruct packet */
514: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
515: buffer_put_cstring(&b, authctxt->user);
1.55 markus 516: buffer_put_cstring(&b, service);
1.52 markus 517: buffer_put_cstring(&b, "hostbased");
518: buffer_put_string(&b, pkalg, alen);
519: buffer_put_string(&b, pkblob, blen);
520: buffer_put_cstring(&b, chost);
521: buffer_put_cstring(&b, cuser);
522: #ifdef DEBUG_PK
523: buffer_dump(&b);
524: #endif
525: /* test for allowed key and correct signature */
526: if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
527: key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
528: authenticated = 1;
529:
530: buffer_clear(&b);
531: key_free(key);
532:
533: done:
534: debug2("userauth_hostbased: authenticated %d", authenticated);
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:
553: char *
554: authmethods_get(void)
1.1 markus 555: {
1.18 markus 556: Authmethod *method = NULL;
1.23 markus 557: u_int size = 0;
1.18 markus 558: char *list;
1.1 markus 559:
1.18 markus 560: for (method = authmethods; method->name != NULL; method++) {
561: if (strcmp(method->name, "none") == 0)
562: continue;
563: if (method->enabled != NULL && *(method->enabled) != 0) {
564: if (size != 0)
565: size += strlen(DELIM);
566: size += strlen(method->name);
1.1 markus 567: }
1.18 markus 568: }
569: size++; /* trailing '\0' */
570: list = xmalloc(size);
571: list[0] = '\0';
572:
573: for (method = authmethods; method->name != NULL; method++) {
574: if (strcmp(method->name, "none") == 0)
575: continue;
576: if (method->enabled != NULL && *(method->enabled) != 0) {
577: if (list[0] != '\0')
578: strlcat(list, DELIM, size);
579: strlcat(list, method->name, size);
1.1 markus 580: }
581: }
1.18 markus 582: return list;
583: }
584:
585: Authmethod *
586: authmethod_lookup(const char *name)
587: {
588: Authmethod *method = NULL;
589: if (name != NULL)
590: for (method = authmethods; method->name != NULL; method++)
591: if (method->enabled != NULL &&
592: *(method->enabled) != 0 &&
593: strcmp(name, method->name) == 0)
594: return method;
595: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
596: return NULL;
1.1 markus 597: }
598:
599: /* return 1 if user allows given key */
600: int
1.63 markus 601: user_key_allowed2(struct passwd *pw, Key *key, char *file)
1.1 markus 602: {
1.63 markus 603: char line[8192];
1.1 markus 604: int found_key = 0;
605: FILE *f;
1.23 markus 606: u_long linenum = 0;
1.1 markus 607: struct stat st;
608: Key *found;
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 &&
632: secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) {
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.21 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.21 markus 665: if (key_read(found, &cp) == -1) {
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;
674: debug("matching key found: file %s, line %ld",
675: file, linenum);
676: break;
677: }
678: }
679: restore_uid();
680: fclose(f);
681: key_free(found);
1.46 markus 682: if (!found_key)
683: debug2("key not found");
1.1 markus 684: return found_key;
1.63 markus 685: }
686:
687: /* check whether given key is in .ssh/authorized_keys* */
688: int
689: user_key_allowed(struct passwd *pw, Key *key)
690: {
691: int success;
692: char *file;
693:
694: file = authorized_keys_file(pw);
695: success = user_key_allowed2(pw, key, file);
696: xfree(file);
697: if (success)
698: return success;
699:
700: /* try suffix "2" for backward compat, too */
701: file = authorized_keys_file2(pw);
702: success = user_key_allowed2(pw, key, file);
703: xfree(file);
704: return success;
1.52 markus 705: }
706:
707: /* return 1 if given hostkey is allowed */
708: int
1.54 markus 709: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.52 markus 710: Key *key)
711: {
712: const char *resolvedname, *ipaddr, *lookup;
1.53 markus 713: int host_status, len;
1.52 markus 714:
715: resolvedname = get_canonical_hostname(options.reverse_mapping_check);
716: ipaddr = get_remote_ipaddr();
717:
1.53 markus 718: debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
719: chost, resolvedname, ipaddr);
1.52 markus 720:
721: if (options.hostbased_uses_name_from_packet_only) {
722: if (auth_rhosts2(pw, cuser, chost, chost) == 0)
723: return 0;
724: lookup = chost;
725: } else {
1.53 markus 726: if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
727: debug2("stripping trailing dot from chost %s", chost);
728: chost[len - 1] = '\0';
729: }
1.52 markus 730: if (strcasecmp(resolvedname, chost) != 0)
731: log("userauth_hostbased mismatch: "
732: "client sends %s, but we resolve %s to %s",
733: chost, ipaddr, resolvedname);
734: if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
735: return 0;
736: lookup = resolvedname;
737: }
738: debug2("userauth_hostbased: access allowed by auth_rhosts2");
739:
1.64 ! markus 740: host_status = check_key_in_hostfiles(pw, key, lookup,
! 741: _PATH_SSH_SYSTEM_HOSTFILE,
! 742: options.ignore_user_known_hosts ? _PATH_SSH_USER_HOSTFILE : NULL);
! 743:
! 744: /* backward compat if no key has been found. */
! 745: if (host_status == HOST_NEW)
! 746: host_status = check_key_in_hostfiles(pw, key, lookup,
! 747: _PATH_SSH_SYSTEM_HOSTFILE2,
! 748: options.ignore_user_known_hosts ? _PATH_SSH_USER_HOSTFILE2 :
! 749: NULL);
1.52 markus 750:
751: return (host_status == HOST_OK);
1.1 markus 752: }
1.64 ! markus 753: