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