Annotation of src/usr.bin/ssh/auth2.c, Revision 1.127
1.127 ! markus 1: /* $OpenBSD: auth2.c,v 1.126 2012/12/02 20:34:09 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2000 Markus Friedl. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: */
1.14 deraadt 25:
1.111 stevesk 26:
27: #include <sys/types.h>
1.117 djm 28: #include <sys/stat.h>
29: #include <sys/uio.h>
1.111 stevesk 30:
1.117 djm 31: #include <fcntl.h>
1.111 stevesk 32: #include <pwd.h>
1.121 dtucker 33: #include <stdarg.h>
1.112 stevesk 34: #include <string.h>
1.117 djm 35: #include <unistd.h>
1.1 markus 36:
1.117 djm 37: #include "atomicio.h"
1.113 deraadt 38: #include "xmalloc.h"
1.32 markus 39: #include "ssh2.h"
1.1 markus 40: #include "packet.h"
1.32 markus 41: #include "log.h"
1.113 deraadt 42: #include "buffer.h"
1.1 markus 43: #include "servconf.h"
44: #include "compat.h"
1.113 deraadt 45: #include "key.h"
46: #include "hostfile.h"
1.1 markus 47: #include "auth.h"
48: #include "dispatch.h"
1.29 markus 49: #include "pathnames.h"
1.100 markus 50: #ifdef GSSAPI
51: #include "ssh-gss.h"
52: #endif
1.113 deraadt 53: #include "monitor_wrap.h"
1.100 markus 54:
1.1 markus 55: /* import */
56: extern ServerOptions options;
1.23 markus 57: extern u_char *session_id2;
1.99 markus 58: extern u_int session_id2_len;
1.1 markus 59:
1.93 markus 60: /* methods */
61:
62: extern Authmethod method_none;
63: extern Authmethod method_pubkey;
64: extern Authmethod method_passwd;
65: extern Authmethod method_kbdint;
66: extern Authmethod method_hostbased;
1.100 markus 67: #ifdef GSSAPI
68: extern Authmethod method_gssapi;
69: #endif
1.120 djm 70: #ifdef JPAKE
71: extern Authmethod method_jpake;
72: #endif
1.93 markus 73:
74: Authmethod *authmethods[] = {
75: &method_none,
76: &method_pubkey,
1.100 markus 77: #ifdef GSSAPI
78: &method_gssapi,
79: #endif
1.120 djm 80: #ifdef JPAKE
81: &method_jpake,
82: #endif
1.93 markus 83: &method_passwd,
84: &method_kbdint,
85: &method_hostbased,
86: NULL
1.18 markus 87: };
88:
1.1 markus 89: /* protocol */
90:
1.80 markus 91: static void input_service_request(int, u_int32_t, void *);
92: static void input_userauth_request(int, u_int32_t, void *);
1.1 markus 93:
94: /* helper */
1.125 djm 95: static Authmethod *authmethod_lookup(Authctxt *, const char *);
96: static char *authmethods_get(Authctxt *authctxt);
1.127 ! markus 97:
! 98: #define MATCH_NONE 0 /* method or submethod mismatch */
! 99: #define MATCH_METHOD 1 /* method matches (no submethod specified) */
! 100: #define MATCH_BOTH 2 /* method and submethod match */
! 101: #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */
! 102: static int list_starts_with(const char *, const char *, const char *);
1.1 markus 103:
1.117 djm 104: char *
105: auth2_read_banner(void)
106: {
107: struct stat st;
108: char *banner = NULL;
109: size_t len, n;
110: int fd;
111:
112: if ((fd = open(options.banner, O_RDONLY)) == -1)
113: return (NULL);
114: if (fstat(fd, &st) == -1) {
115: close(fd);
116: return (NULL);
117: }
1.124 djm 118: if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
1.117 djm 119: close(fd);
120: return (NULL);
121: }
122:
123: len = (size_t)st.st_size; /* truncate */
124: banner = xmalloc(len + 1);
125: n = atomicio(read, fd, banner, len);
126: close(fd);
127:
128: if (n != len) {
129: xfree(banner);
130: return (NULL);
131: }
132: banner[n] = '\0';
133:
134: return (banner);
135: }
136:
137: static void
138: userauth_banner(void)
139: {
140: char *banner = NULL;
141:
142: if (options.banner == NULL ||
143: strcasecmp(options.banner, "none") == 0 ||
144: (datafellows & SSH_BUG_BANNER) != 0)
145: return;
146:
147: if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
148: goto done;
149:
150: packet_start(SSH2_MSG_USERAUTH_BANNER);
151: packet_put_cstring(banner);
152: packet_put_cstring(""); /* language, unused */
153: packet_send();
154: debug("userauth_banner: sent");
155: done:
156: if (banner)
157: xfree(banner);
158: }
159:
1.1 markus 160: /*
1.18 markus 161: * loop until authctxt->success == TRUE
1.1 markus 162: */
1.103 markus 163: void
164: do_authentication2(Authctxt *authctxt)
1.1 markus 165: {
1.81 markus 166: dispatch_init(&dispatch_protocol_error);
1.1 markus 167: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18 markus 168: dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.1 markus 169: }
170:
1.109 deraadt 171: /*ARGSUSED*/
1.66 itojun 172: static void
1.80 markus 173: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 174: {
1.18 markus 175: Authctxt *authctxt = ctxt;
1.23 markus 176: u_int len;
1.94 deraadt 177: int acceptit = 0;
1.122 djm 178: char *service = packet_get_cstring(&len);
1.79 markus 179: packet_check_eom();
1.1 markus 180:
1.18 markus 181: if (authctxt == NULL)
182: fatal("input_service_request: no authctxt");
183:
1.1 markus 184: if (strcmp(service, "ssh-userauth") == 0) {
1.18 markus 185: if (!authctxt->success) {
1.94 deraadt 186: acceptit = 1;
1.1 markus 187: /* now we can handle user-auth requests */
188: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
189: }
190: }
191: /* XXX all other service requests are denied */
192:
1.94 deraadt 193: if (acceptit) {
1.1 markus 194: packet_start(SSH2_MSG_SERVICE_ACCEPT);
195: packet_put_cstring(service);
196: packet_send();
197: packet_write_wait();
198: } else {
199: debug("bad service request %s", service);
200: packet_disconnect("bad service request %s", service);
201: }
202: xfree(service);
203: }
204:
1.109 deraadt 205: /*ARGSUSED*/
1.66 itojun 206: static void
1.80 markus 207: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 208: {
1.18 markus 209: Authctxt *authctxt = ctxt;
210: Authmethod *m = NULL;
1.28 markus 211: char *user, *service, *method, *style = NULL;
1.1 markus 212: int authenticated = 0;
213:
1.18 markus 214: if (authctxt == NULL)
215: fatal("input_userauth_request: no authctxt");
1.1 markus 216:
1.122 djm 217: user = packet_get_cstring(NULL);
218: service = packet_get_cstring(NULL);
219: method = packet_get_cstring(NULL);
1.1 markus 220: debug("userauth-request for user %s service %s method %s", user, service, method);
1.24 markus 221: debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1 markus 222:
1.28 markus 223: if ((style = strchr(user, ':')) != NULL)
224: *style++ = 0;
225:
1.36 stevesk 226: if (authctxt->attempt++ == 0) {
1.18 markus 227: /* setup auth context */
1.89 markus 228: authctxt->pw = PRIVSEP(getpwnamallow(user));
229: if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18 markus 230: authctxt->valid = 1;
231: debug2("input_userauth_request: setting up authctxt for %s", user);
232: } else {
1.107 markus 233: logit("input_userauth_request: invalid user %s", user);
1.102 markus 234: authctxt->pw = fakepw();
1.18 markus 235: }
1.106 djm 236: setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.88 provos 237: use_privsep ? " [net]" : "");
1.18 markus 238: authctxt->user = xstrdup(user);
239: authctxt->service = xstrdup(service);
1.62 markus 240: authctxt->style = style ? xstrdup(style) : NULL;
1.88 provos 241: if (use_privsep)
242: mm_inform_authserv(service, style);
1.117 djm 243: userauth_banner();
1.125 djm 244: if (auth2_setup_methods_lists(authctxt) != 0)
245: packet_disconnect("no authentication methods enabled");
1.62 markus 246: } else if (strcmp(user, authctxt->user) != 0 ||
247: strcmp(service, authctxt->service) != 0) {
248: packet_disconnect("Change of username or service not allowed: "
249: "(%s,%s) -> (%s,%s)",
250: authctxt->user, authctxt->service, user, service);
1.1 markus 251: }
1.28 markus 252: /* reset state */
1.75 markus 253: auth2_challenge_stop(authctxt);
1.120 djm 254: #ifdef JPAKE
255: auth2_jpake_stop(authctxt);
256: #endif
1.100 markus 257:
258: #ifdef GSSAPI
1.120 djm 259: /* XXX move to auth2_gssapi_stop() */
1.100 markus 260: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
261: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
262: #endif
263:
1.28 markus 264: authctxt->postponed = 0;
1.123 djm 265: authctxt->server_caused_failure = 0;
1.18 markus 266:
1.28 markus 267: /* try to authenticate user */
1.125 djm 268: m = authmethod_lookup(authctxt, method);
1.117 djm 269: if (m != NULL && authctxt->failures < options.max_authtries) {
1.18 markus 270: debug2("input_userauth_request: try method %s", method);
271: authenticated = m->userauth(authctxt);
272: }
1.126 djm 273: userauth_finish(authctxt, authenticated, method, NULL);
1.49 markus 274:
275: xfree(service);
276: xfree(user);
277: xfree(method);
278: }
279:
280: void
1.126 djm 281: userauth_finish(Authctxt *authctxt, int authenticated, const char *method,
282: const char *submethod)
1.49 markus 283: {
1.60 markus 284: char *methods;
1.125 djm 285: int partial = 0;
1.60 markus 286:
1.28 markus 287: if (!authctxt->valid && authenticated)
288: fatal("INTERNAL ERROR: authenticated invalid user %s",
289: authctxt->user);
1.126 djm 290: if (authenticated && authctxt->postponed)
291: fatal("INTERNAL ERROR: authenticated and postponed");
1.18 markus 292:
293: /* Special handling for root */
1.96 markus 294: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.41 markus 295: !auth_root_allowed(method))
1.3 markus 296: authenticated = 0;
297:
1.125 djm 298: if (authenticated && options.num_auth_methods != 0) {
1.127 ! markus 299: if (!auth2_update_methods_lists(authctxt, method, submethod)) {
1.125 djm 300: authenticated = 0;
301: partial = 1;
302: }
303: }
1.126 djm 304:
305: /* Log before sending the reply */
306: auth_log(authctxt, authenticated, partial, method, submethod, " ssh2");
307:
308: if (authctxt->postponed)
309: return;
1.125 djm 310:
1.60 markus 311: if (authenticated == 1) {
312: /* turn off userauth */
1.81 markus 313: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.60 markus 314: packet_start(SSH2_MSG_USERAUTH_SUCCESS);
315: packet_send();
316: packet_write_wait();
317: /* now we can break out */
318: authctxt->success = 1;
319: } else {
1.119 djm 320: /* Allow initial try of "none" auth without failure penalty */
1.123 djm 321: if (!authctxt->server_caused_failure &&
322: (authctxt->attempt > 1 || strcmp(method, "none") != 0))
1.119 djm 323: authctxt->failures++;
324: if (authctxt->failures >= options.max_authtries)
1.60 markus 325: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.125 djm 326: methods = authmethods_get(authctxt);
327: debug3("%s: failure partial=%d next methods=\"%s\"", __func__,
328: partial, methods);
1.60 markus 329: packet_start(SSH2_MSG_USERAUTH_FAILURE);
330: packet_put_cstring(methods);
1.125 djm 331: packet_put_char(partial);
1.60 markus 332: packet_send();
333: packet_write_wait();
334: xfree(methods);
335: }
1.1 markus 336: }
1.18 markus 337:
1.125 djm 338: /*
339: * Checks whether method is allowed by at least one AuthenticationMethods
340: * methods list. Returns 1 if allowed, or no methods lists configured.
341: * 0 otherwise.
342: */
1.127 ! markus 343: int
! 344: auth2_method_allowed(Authctxt *authctxt, const char *method,
! 345: const char *submethod)
1.125 djm 346: {
347: u_int i;
348:
349: /*
350: * NB. authctxt->num_auth_methods might be zero as a result of
351: * auth2_setup_methods_lists(), so check the configuration.
352: */
353: if (options.num_auth_methods == 0)
354: return 1;
355: for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127 ! markus 356: if (list_starts_with(authctxt->auth_methods[i], method,
! 357: submethod) != MATCH_NONE)
1.125 djm 358: return 1;
359: }
360: return 0;
361: }
362:
1.67 stevesk 363: static char *
1.125 djm 364: authmethods_get(Authctxt *authctxt)
1.1 markus 365: {
1.82 markus 366: Buffer b;
1.18 markus 367: char *list;
1.125 djm 368: u_int i;
1.1 markus 369:
1.82 markus 370: buffer_init(&b);
1.93 markus 371: for (i = 0; authmethods[i] != NULL; i++) {
372: if (strcmp(authmethods[i]->name, "none") == 0)
1.18 markus 373: continue;
1.125 djm 374: if (authmethods[i]->enabled == NULL ||
375: *(authmethods[i]->enabled) == 0)
376: continue;
1.127 ! markus 377: if (!auth2_method_allowed(authctxt, authmethods[i]->name,
! 378: NULL))
1.125 djm 379: continue;
380: if (buffer_len(&b) > 0)
381: buffer_append(&b, ",", 1);
382: buffer_append(&b, authmethods[i]->name,
383: strlen(authmethods[i]->name));
1.1 markus 384: }
1.82 markus 385: buffer_append(&b, "\0", 1);
386: list = xstrdup(buffer_ptr(&b));
387: buffer_free(&b);
1.18 markus 388: return list;
389: }
390:
1.66 itojun 391: static Authmethod *
1.125 djm 392: authmethod_lookup(Authctxt *authctxt, const char *name)
1.18 markus 393: {
1.93 markus 394: int i;
395:
1.18 markus 396: if (name != NULL)
1.93 markus 397: for (i = 0; authmethods[i] != NULL; i++)
398: if (authmethods[i]->enabled != NULL &&
399: *(authmethods[i]->enabled) != 0 &&
1.125 djm 400: strcmp(name, authmethods[i]->name) == 0 &&
1.127 ! markus 401: auth2_method_allowed(authctxt,
! 402: authmethods[i]->name, NULL))
1.93 markus 403: return authmethods[i];
404: debug2("Unrecognized authentication method name: %s",
405: name ? name : "NULL");
1.18 markus 406: return NULL;
1.1 markus 407: }
1.125 djm 408:
409: /*
410: * Check a comma-separated list of methods for validity. Is need_enable is
411: * non-zero, then also require that the methods are enabled.
412: * Returns 0 on success or -1 if the methods list is invalid.
413: */
414: int
415: auth2_methods_valid(const char *_methods, int need_enable)
416: {
1.127 ! markus 417: char *methods, *omethods, *method, *p;
1.125 djm 418: u_int i, found;
419: int ret = -1;
420:
421: if (*_methods == '\0') {
422: error("empty authentication method list");
423: return -1;
424: }
425: omethods = methods = xstrdup(_methods);
426: while ((method = strsep(&methods, ",")) != NULL) {
427: for (found = i = 0; !found && authmethods[i] != NULL; i++) {
1.127 ! markus 428: if ((p = strchr(method, ':')) != NULL)
! 429: *p = '\0';
1.125 djm 430: if (strcmp(method, authmethods[i]->name) != 0)
431: continue;
432: if (need_enable) {
433: if (authmethods[i]->enabled == NULL ||
434: *(authmethods[i]->enabled) == 0) {
435: error("Disabled method \"%s\" in "
436: "AuthenticationMethods list \"%s\"",
437: method, _methods);
438: goto out;
439: }
440: }
441: found = 1;
442: break;
443: }
444: if (!found) {
445: error("Unknown authentication method \"%s\" in list",
446: method);
447: goto out;
448: }
449: }
450: ret = 0;
451: out:
452: free(omethods);
453: return ret;
454: }
455:
456: /*
457: * Prune the AuthenticationMethods supplied in the configuration, removing
458: * any methods lists that include disabled methods. Note that this might
459: * leave authctxt->num_auth_methods == 0, even when multiple required auth
460: * has been requested. For this reason, all tests for whether multiple is
461: * enabled should consult options.num_auth_methods directly.
462: */
463: int
464: auth2_setup_methods_lists(Authctxt *authctxt)
465: {
466: u_int i;
467:
468: if (options.num_auth_methods == 0)
469: return 0;
470: debug3("%s: checking methods", __func__);
471: authctxt->auth_methods = xcalloc(options.num_auth_methods,
472: sizeof(*authctxt->auth_methods));
473: authctxt->num_auth_methods = 0;
474: for (i = 0; i < options.num_auth_methods; i++) {
475: if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
476: logit("Authentication methods list \"%s\" contains "
477: "disabled method, skipping",
478: options.auth_methods[i]);
479: continue;
480: }
481: debug("authentication methods list %d: %s",
482: authctxt->num_auth_methods, options.auth_methods[i]);
483: authctxt->auth_methods[authctxt->num_auth_methods++] =
484: xstrdup(options.auth_methods[i]);
485: }
486: if (authctxt->num_auth_methods == 0) {
487: error("No AuthenticationMethods left after eliminating "
488: "disabled methods");
489: return -1;
490: }
491: return 0;
492: }
493:
494: static int
1.127 ! markus 495: list_starts_with(const char *methods, const char *method,
! 496: const char *submethod)
1.125 djm 497: {
498: size_t l = strlen(method);
1.127 ! markus 499: int match;
! 500: const char *p;
1.125 djm 501:
502: if (strncmp(methods, method, l) != 0)
1.127 ! markus 503: return MATCH_NONE;
! 504: p = methods + l;
! 505: match = MATCH_METHOD;
! 506: if (*p == ':') {
! 507: if (!submethod)
! 508: return MATCH_PARTIAL;
! 509: l = strlen(submethod);
! 510: p += 1;
! 511: if (strncmp(submethod, p, l))
! 512: return MATCH_NONE;
! 513: p += l;
! 514: match = MATCH_BOTH;
! 515: }
! 516: if (*p != ',' && *p != '\0')
! 517: return MATCH_NONE;
! 518: return match;
1.125 djm 519: }
520:
521: /*
522: * Remove method from the start of a comma-separated list of methods.
523: * Returns 0 if the list of methods did not start with that method or 1
524: * if it did.
525: */
526: static int
1.127 ! markus 527: remove_method(char **methods, const char *method, const char *submethod)
1.125 djm 528: {
1.127 ! markus 529: char *omethods = *methods, *p;
1.125 djm 530: size_t l = strlen(method);
1.127 ! markus 531: int match;
1.125 djm 532:
1.127 ! markus 533: match = list_starts_with(omethods, method, submethod);
! 534: if (match != MATCH_METHOD && match != MATCH_BOTH)
1.125 djm 535: return 0;
1.127 ! markus 536: p = omethods + l;
! 537: if (submethod && match == MATCH_BOTH)
! 538: p += 1 + strlen(submethod); /* include colon */
! 539: if (*p == ',')
! 540: p++;
! 541: *methods = xstrdup(p);
1.125 djm 542: free(omethods);
543: return 1;
544: }
545:
546: /*
547: * Called after successful authentication. Will remove the successful method
548: * from the start of each list in which it occurs. If it was the last method
549: * in any list, then authentication is deemed successful.
550: * Returns 1 if the method completed any authentication list or 0 otherwise.
551: */
552: int
1.127 ! markus 553: auth2_update_methods_lists(Authctxt *authctxt, const char *method,
! 554: const char *submethod)
1.125 djm 555: {
556: u_int i, found = 0;
557:
558: debug3("%s: updating methods list after \"%s\"", __func__, method);
559: for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127 ! markus 560: if (!remove_method(&(authctxt->auth_methods[i]), method,
! 561: submethod))
1.125 djm 562: continue;
563: found = 1;
564: if (*authctxt->auth_methods[i] == '\0') {
565: debug2("authentication methods list %d complete", i);
566: return 1;
567: }
568: debug3("authentication methods list %d remaining: \"%s\"",
569: i, authctxt->auth_methods[i]);
570: }
571: /* This should not happen, but would be bad if it did */
572: if (!found)
573: fatal("%s: method not in AuthenticationMethods", __func__);
574: return 0;
575: }
576:
1.117 djm 577: