Annotation of src/usr.bin/ssh/auth2.c, Revision 1.138
1.138 ! markus 1: /* $OpenBSD: auth2.c,v 1.137 2017/02/03 23:05:57 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.138 ! markus 158: struct ssh *ssh = active_state; /* XXX */
! 159: ssh->authctxt = authctxt; /* XXX move to caller */
1.81 markus 160: dispatch_init(&dispatch_protocol_error);
1.1 markus 161: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.138 ! markus 162: dispatch_run(DISPATCH_BLOCK, &authctxt->success, ssh);
! 163: ssh->authctxt = NULL;
1.1 markus 164: }
165:
1.109 deraadt 166: /*ARGSUSED*/
1.135 markus 167: static int
1.80 markus 168: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 169: {
1.138 ! markus 170: struct ssh *ssh = ctxt;
! 171: Authctxt *authctxt = ssh->authctxt;
1.23 markus 172: u_int len;
1.94 deraadt 173: int acceptit = 0;
1.122 djm 174: char *service = packet_get_cstring(&len);
1.79 markus 175: packet_check_eom();
1.1 markus 176:
1.18 markus 177: if (authctxt == NULL)
178: fatal("input_service_request: no authctxt");
179:
1.1 markus 180: if (strcmp(service, "ssh-userauth") == 0) {
1.18 markus 181: if (!authctxt->success) {
1.94 deraadt 182: acceptit = 1;
1.1 markus 183: /* now we can handle user-auth requests */
184: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
185: }
186: }
187: /* XXX all other service requests are denied */
188:
1.94 deraadt 189: if (acceptit) {
1.1 markus 190: packet_start(SSH2_MSG_SERVICE_ACCEPT);
191: packet_put_cstring(service);
192: packet_send();
193: packet_write_wait();
194: } else {
195: debug("bad service request %s", service);
196: packet_disconnect("bad service request %s", service);
197: }
1.128 djm 198: free(service);
1.135 markus 199: return 0;
1.1 markus 200: }
201:
1.109 deraadt 202: /*ARGSUSED*/
1.135 markus 203: static int
1.80 markus 204: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 205: {
1.138 ! markus 206: struct ssh *ssh = ctxt;
! 207: Authctxt *authctxt = ssh->authctxt;
1.18 markus 208: Authmethod *m = NULL;
1.28 markus 209: char *user, *service, *method, *style = NULL;
1.1 markus 210: int authenticated = 0;
211:
1.18 markus 212: if (authctxt == NULL)
213: fatal("input_userauth_request: no authctxt");
1.1 markus 214:
1.122 djm 215: user = packet_get_cstring(NULL);
216: service = packet_get_cstring(NULL);
217: method = packet_get_cstring(NULL);
1.1 markus 218: debug("userauth-request for user %s service %s method %s", user, service, method);
1.24 markus 219: debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1 markus 220:
1.28 markus 221: if ((style = strchr(user, ':')) != NULL)
222: *style++ = 0;
223:
1.36 stevesk 224: if (authctxt->attempt++ == 0) {
1.18 markus 225: /* setup auth context */
1.89 markus 226: authctxt->pw = PRIVSEP(getpwnamallow(user));
227: if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18 markus 228: authctxt->valid = 1;
1.137 djm 229: debug2("%s: setting up authctxt for %s",
230: __func__, user);
1.18 markus 231: } else {
1.137 djm 232: /* Invalid user, fake password information */
1.102 markus 233: authctxt->pw = fakepw();
1.18 markus 234: }
1.137 djm 235: ssh_packet_set_log_preamble(ssh, "%suser %s",
236: authctxt->valid ? "authenticating " : "invalid ", user);
1.106 djm 237: setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.88 provos 238: use_privsep ? " [net]" : "");
1.18 markus 239: authctxt->user = xstrdup(user);
240: authctxt->service = xstrdup(service);
1.62 markus 241: authctxt->style = style ? xstrdup(style) : NULL;
1.88 provos 242: if (use_privsep)
243: mm_inform_authserv(service, style);
1.117 djm 244: userauth_banner();
1.125 djm 245: if (auth2_setup_methods_lists(authctxt) != 0)
246: packet_disconnect("no authentication methods enabled");
1.62 markus 247: } else if (strcmp(user, authctxt->user) != 0 ||
248: strcmp(service, authctxt->service) != 0) {
249: packet_disconnect("Change of username or service not allowed: "
250: "(%s,%s) -> (%s,%s)",
251: authctxt->user, authctxt->service, user, service);
1.1 markus 252: }
1.28 markus 253: /* reset state */
1.75 markus 254: auth2_challenge_stop(authctxt);
1.100 markus 255:
256: #ifdef GSSAPI
1.120 djm 257: /* XXX move to auth2_gssapi_stop() */
1.100 markus 258: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
259: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
260: #endif
261:
1.28 markus 262: authctxt->postponed = 0;
1.123 djm 263: authctxt->server_caused_failure = 0;
1.18 markus 264:
1.28 markus 265: /* try to authenticate user */
1.125 djm 266: m = authmethod_lookup(authctxt, method);
1.117 djm 267: if (m != NULL && authctxt->failures < options.max_authtries) {
1.18 markus 268: debug2("input_userauth_request: try method %s", method);
269: authenticated = m->userauth(authctxt);
270: }
1.126 djm 271: userauth_finish(authctxt, authenticated, method, NULL);
1.49 markus 272:
1.128 djm 273: free(service);
274: free(user);
275: free(method);
1.135 markus 276: return 0;
1.49 markus 277: }
278:
279: void
1.126 djm 280: userauth_finish(Authctxt *authctxt, int authenticated, const char *method,
281: const char *submethod)
1.49 markus 282: {
1.137 djm 283: struct ssh *ssh = active_state; /* XXX */
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 */
1.129 djm 306: auth_log(authctxt, authenticated, partial, method, submethod);
1.126 djm 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;
1.137 djm 319: ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
1.60 markus 320: } else {
1.119 djm 321: /* Allow initial try of "none" auth without failure penalty */
1.133 djm 322: if (!partial && !authctxt->server_caused_failure &&
1.123 djm 323: (authctxt->attempt > 1 || strcmp(method, "none") != 0))
1.119 djm 324: authctxt->failures++;
325: if (authctxt->failures >= options.max_authtries)
1.131 djm 326: auth_maxtries_exceeded(authctxt);
1.125 djm 327: methods = authmethods_get(authctxt);
328: debug3("%s: failure partial=%d next methods=\"%s\"", __func__,
329: partial, methods);
1.60 markus 330: packet_start(SSH2_MSG_USERAUTH_FAILURE);
331: packet_put_cstring(methods);
1.125 djm 332: packet_put_char(partial);
1.60 markus 333: packet_send();
334: packet_write_wait();
1.128 djm 335: free(methods);
1.60 markus 336: }
1.1 markus 337: }
1.18 markus 338:
1.125 djm 339: /*
340: * Checks whether method is allowed by at least one AuthenticationMethods
341: * methods list. Returns 1 if allowed, or no methods lists configured.
342: * 0 otherwise.
343: */
1.127 markus 344: int
345: auth2_method_allowed(Authctxt *authctxt, const char *method,
346: const char *submethod)
1.125 djm 347: {
348: u_int i;
349:
350: /*
351: * NB. authctxt->num_auth_methods might be zero as a result of
352: * auth2_setup_methods_lists(), so check the configuration.
353: */
354: if (options.num_auth_methods == 0)
355: return 1;
356: for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127 markus 357: if (list_starts_with(authctxt->auth_methods[i], method,
358: submethod) != MATCH_NONE)
1.125 djm 359: return 1;
360: }
361: return 0;
362: }
363:
1.67 stevesk 364: static char *
1.125 djm 365: authmethods_get(Authctxt *authctxt)
1.1 markus 366: {
1.82 markus 367: Buffer b;
1.18 markus 368: char *list;
1.125 djm 369: u_int i;
1.1 markus 370:
1.82 markus 371: buffer_init(&b);
1.93 markus 372: for (i = 0; authmethods[i] != NULL; i++) {
373: if (strcmp(authmethods[i]->name, "none") == 0)
1.18 markus 374: continue;
1.125 djm 375: if (authmethods[i]->enabled == NULL ||
376: *(authmethods[i]->enabled) == 0)
377: continue;
1.127 markus 378: if (!auth2_method_allowed(authctxt, authmethods[i]->name,
379: NULL))
1.125 djm 380: continue;
381: if (buffer_len(&b) > 0)
382: buffer_append(&b, ",", 1);
383: buffer_append(&b, authmethods[i]->name,
384: strlen(authmethods[i]->name));
1.1 markus 385: }
1.136 djm 386: if ((list = sshbuf_dup_string(&b)) == NULL)
387: fatal("%s: sshbuf_dup_string failed", __func__);
1.82 markus 388: buffer_free(&b);
1.18 markus 389: return list;
390: }
391:
1.66 itojun 392: static Authmethod *
1.125 djm 393: authmethod_lookup(Authctxt *authctxt, const char *name)
1.18 markus 394: {
1.93 markus 395: int i;
396:
1.18 markus 397: if (name != NULL)
1.93 markus 398: for (i = 0; authmethods[i] != NULL; i++)
399: if (authmethods[i]->enabled != NULL &&
400: *(authmethods[i]->enabled) != 0 &&
1.125 djm 401: strcmp(name, authmethods[i]->name) == 0 &&
1.127 markus 402: auth2_method_allowed(authctxt,
403: authmethods[i]->name, NULL))
1.93 markus 404: return authmethods[i];
405: debug2("Unrecognized authentication method name: %s",
406: name ? name : "NULL");
1.18 markus 407: return NULL;
1.1 markus 408: }
1.125 djm 409:
410: /*
411: * Check a comma-separated list of methods for validity. Is need_enable is
412: * non-zero, then also require that the methods are enabled.
413: * Returns 0 on success or -1 if the methods list is invalid.
414: */
415: int
416: auth2_methods_valid(const char *_methods, int need_enable)
417: {
1.127 markus 418: char *methods, *omethods, *method, *p;
1.125 djm 419: u_int i, found;
420: int ret = -1;
421:
422: if (*_methods == '\0') {
423: error("empty authentication method list");
424: return -1;
425: }
426: omethods = methods = xstrdup(_methods);
427: while ((method = strsep(&methods, ",")) != NULL) {
428: for (found = i = 0; !found && authmethods[i] != NULL; i++) {
1.127 markus 429: if ((p = strchr(method, ':')) != NULL)
430: *p = '\0';
1.125 djm 431: if (strcmp(method, authmethods[i]->name) != 0)
432: continue;
433: if (need_enable) {
434: if (authmethods[i]->enabled == NULL ||
435: *(authmethods[i]->enabled) == 0) {
436: error("Disabled method \"%s\" in "
437: "AuthenticationMethods list \"%s\"",
438: method, _methods);
439: goto out;
440: }
441: }
442: found = 1;
443: break;
444: }
445: if (!found) {
446: error("Unknown authentication method \"%s\" in list",
447: method);
448: goto out;
449: }
450: }
451: ret = 0;
452: out:
453: free(omethods);
454: return ret;
455: }
456:
457: /*
458: * Prune the AuthenticationMethods supplied in the configuration, removing
459: * any methods lists that include disabled methods. Note that this might
460: * leave authctxt->num_auth_methods == 0, even when multiple required auth
461: * has been requested. For this reason, all tests for whether multiple is
462: * enabled should consult options.num_auth_methods directly.
463: */
464: int
465: auth2_setup_methods_lists(Authctxt *authctxt)
466: {
467: u_int i;
468:
469: if (options.num_auth_methods == 0)
470: return 0;
471: debug3("%s: checking methods", __func__);
472: authctxt->auth_methods = xcalloc(options.num_auth_methods,
473: sizeof(*authctxt->auth_methods));
474: authctxt->num_auth_methods = 0;
475: for (i = 0; i < options.num_auth_methods; i++) {
476: if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
477: logit("Authentication methods list \"%s\" contains "
478: "disabled method, skipping",
479: options.auth_methods[i]);
480: continue;
481: }
482: debug("authentication methods list %d: %s",
483: authctxt->num_auth_methods, options.auth_methods[i]);
484: authctxt->auth_methods[authctxt->num_auth_methods++] =
485: xstrdup(options.auth_methods[i]);
486: }
487: if (authctxt->num_auth_methods == 0) {
488: error("No AuthenticationMethods left after eliminating "
489: "disabled methods");
490: return -1;
491: }
492: return 0;
493: }
494:
495: static int
1.127 markus 496: list_starts_with(const char *methods, const char *method,
497: const char *submethod)
1.125 djm 498: {
499: size_t l = strlen(method);
1.127 markus 500: int match;
501: const char *p;
1.125 djm 502:
503: if (strncmp(methods, method, l) != 0)
1.127 markus 504: return MATCH_NONE;
505: p = methods + l;
506: match = MATCH_METHOD;
507: if (*p == ':') {
508: if (!submethod)
509: return MATCH_PARTIAL;
510: l = strlen(submethod);
511: p += 1;
512: if (strncmp(submethod, p, l))
513: return MATCH_NONE;
514: p += l;
515: match = MATCH_BOTH;
516: }
517: if (*p != ',' && *p != '\0')
518: return MATCH_NONE;
519: return match;
1.125 djm 520: }
521:
522: /*
523: * Remove method from the start of a comma-separated list of methods.
524: * Returns 0 if the list of methods did not start with that method or 1
525: * if it did.
526: */
527: static int
1.127 markus 528: remove_method(char **methods, const char *method, const char *submethod)
1.125 djm 529: {
1.127 markus 530: char *omethods = *methods, *p;
1.125 djm 531: size_t l = strlen(method);
1.127 markus 532: int match;
1.125 djm 533:
1.127 markus 534: match = list_starts_with(omethods, method, submethod);
535: if (match != MATCH_METHOD && match != MATCH_BOTH)
1.125 djm 536: return 0;
1.127 markus 537: p = omethods + l;
538: if (submethod && match == MATCH_BOTH)
539: p += 1 + strlen(submethod); /* include colon */
540: if (*p == ',')
541: p++;
542: *methods = xstrdup(p);
1.125 djm 543: free(omethods);
544: return 1;
545: }
546:
547: /*
548: * Called after successful authentication. Will remove the successful method
549: * from the start of each list in which it occurs. If it was the last method
550: * in any list, then authentication is deemed successful.
551: * Returns 1 if the method completed any authentication list or 0 otherwise.
552: */
553: int
1.127 markus 554: auth2_update_methods_lists(Authctxt *authctxt, const char *method,
555: const char *submethod)
1.125 djm 556: {
557: u_int i, found = 0;
558:
559: debug3("%s: updating methods list after \"%s\"", __func__, method);
560: for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127 markus 561: if (!remove_method(&(authctxt->auth_methods[i]), method,
562: submethod))
1.125 djm 563: continue;
564: found = 1;
565: if (*authctxt->auth_methods[i] == '\0') {
566: debug2("authentication methods list %d complete", i);
567: return 1;
568: }
569: debug3("authentication methods list %d remaining: \"%s\"",
570: i, authctxt->auth_methods[i]);
571: }
572: /* This should not happen, but would be bad if it did */
573: if (!found)
574: fatal("%s: method not in AuthenticationMethods", __func__);
575: return 0;
576: }
577:
1.117 djm 578: