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