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