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