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