Annotation of src/usr.bin/ssh/auth2.c, Revision 1.129
1.129 ! djm 1: /* $OpenBSD: auth2.c,v 1.128 2013/05/17 00:13:13 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.120 djm 70: #ifdef JPAKE
71: extern Authmethod method_jpake;
72: #endif
1.93 markus 73:
74: Authmethod *authmethods[] = {
75: &method_none,
76: &method_pubkey,
1.100 markus 77: #ifdef GSSAPI
78: &method_gssapi,
79: #endif
1.120 djm 80: #ifdef JPAKE
81: &method_jpake,
82: #endif
1.93 markus 83: &method_passwd,
84: &method_kbdint,
85: &method_hostbased,
86: NULL
1.18 markus 87: };
88:
1.1 markus 89: /* protocol */
90:
1.80 markus 91: static void input_service_request(int, u_int32_t, void *);
92: static void input_userauth_request(int, u_int32_t, void *);
1.1 markus 93:
94: /* helper */
1.125 djm 95: static Authmethod *authmethod_lookup(Authctxt *, const char *);
96: static char *authmethods_get(Authctxt *authctxt);
1.127 markus 97:
98: #define MATCH_NONE 0 /* method or submethod mismatch */
99: #define MATCH_METHOD 1 /* method matches (no submethod specified) */
100: #define MATCH_BOTH 2 /* method and submethod match */
101: #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */
102: static int list_starts_with(const char *, const char *, const char *);
1.1 markus 103:
1.117 djm 104: char *
105: auth2_read_banner(void)
106: {
107: struct stat st;
108: char *banner = NULL;
109: size_t len, n;
110: int fd;
111:
112: if ((fd = open(options.banner, O_RDONLY)) == -1)
113: return (NULL);
114: if (fstat(fd, &st) == -1) {
115: close(fd);
116: return (NULL);
117: }
1.124 djm 118: if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
1.117 djm 119: close(fd);
120: return (NULL);
121: }
122:
123: len = (size_t)st.st_size; /* truncate */
124: banner = xmalloc(len + 1);
125: n = atomicio(read, fd, banner, len);
126: close(fd);
127:
128: if (n != len) {
1.128 djm 129: free(banner);
1.117 djm 130: return (NULL);
131: }
132: banner[n] = '\0';
133:
134: return (banner);
135: }
136:
137: static void
138: userauth_banner(void)
139: {
140: char *banner = NULL;
141:
142: if (options.banner == NULL ||
143: strcasecmp(options.banner, "none") == 0 ||
144: (datafellows & SSH_BUG_BANNER) != 0)
145: return;
146:
147: if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
148: goto done;
149:
150: packet_start(SSH2_MSG_USERAUTH_BANNER);
151: packet_put_cstring(banner);
152: packet_put_cstring(""); /* language, unused */
153: packet_send();
154: debug("userauth_banner: sent");
155: done:
1.128 djm 156: free(banner);
1.117 djm 157: }
158:
1.1 markus 159: /*
1.18 markus 160: * loop until authctxt->success == TRUE
1.1 markus 161: */
1.103 markus 162: void
163: do_authentication2(Authctxt *authctxt)
1.1 markus 164: {
1.81 markus 165: dispatch_init(&dispatch_protocol_error);
1.1 markus 166: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18 markus 167: dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.1 markus 168: }
169:
1.109 deraadt 170: /*ARGSUSED*/
1.66 itojun 171: static void
1.80 markus 172: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 173: {
1.18 markus 174: Authctxt *authctxt = ctxt;
1.23 markus 175: u_int len;
1.94 deraadt 176: int acceptit = 0;
1.122 djm 177: char *service = packet_get_cstring(&len);
1.79 markus 178: packet_check_eom();
1.1 markus 179:
1.18 markus 180: if (authctxt == NULL)
181: fatal("input_service_request: no authctxt");
182:
1.1 markus 183: if (strcmp(service, "ssh-userauth") == 0) {
1.18 markus 184: if (!authctxt->success) {
1.94 deraadt 185: acceptit = 1;
1.1 markus 186: /* now we can handle user-auth requests */
187: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
188: }
189: }
190: /* XXX all other service requests are denied */
191:
1.94 deraadt 192: if (acceptit) {
1.1 markus 193: packet_start(SSH2_MSG_SERVICE_ACCEPT);
194: packet_put_cstring(service);
195: packet_send();
196: packet_write_wait();
197: } else {
198: debug("bad service request %s", service);
199: packet_disconnect("bad service request %s", service);
200: }
1.128 djm 201: free(service);
1.1 markus 202: }
203:
1.109 deraadt 204: /*ARGSUSED*/
1.66 itojun 205: static void
1.80 markus 206: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 207: {
1.18 markus 208: Authctxt *authctxt = ctxt;
209: Authmethod *m = NULL;
1.28 markus 210: char *user, *service, *method, *style = NULL;
1.1 markus 211: int authenticated = 0;
212:
1.18 markus 213: if (authctxt == NULL)
214: fatal("input_userauth_request: no authctxt");
1.1 markus 215:
1.122 djm 216: user = packet_get_cstring(NULL);
217: service = packet_get_cstring(NULL);
218: method = packet_get_cstring(NULL);
1.1 markus 219: debug("userauth-request for user %s service %s method %s", user, service, method);
1.24 markus 220: debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1 markus 221:
1.28 markus 222: if ((style = strchr(user, ':')) != NULL)
223: *style++ = 0;
224:
1.36 stevesk 225: if (authctxt->attempt++ == 0) {
1.18 markus 226: /* setup auth context */
1.89 markus 227: authctxt->pw = PRIVSEP(getpwnamallow(user));
228: if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18 markus 229: authctxt->valid = 1;
230: debug2("input_userauth_request: setting up authctxt for %s", user);
231: } else {
1.107 markus 232: logit("input_userauth_request: invalid user %s", user);
1.102 markus 233: authctxt->pw = fakepw();
1.18 markus 234: }
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.75 markus 252: auth2_challenge_stop(authctxt);
1.120 djm 253: #ifdef JPAKE
254: auth2_jpake_stop(authctxt);
255: #endif
1.100 markus 256:
257: #ifdef GSSAPI
1.120 djm 258: /* XXX move to auth2_gssapi_stop() */
1.100 markus 259: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
260: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
261: #endif
262:
1.28 markus 263: authctxt->postponed = 0;
1.123 djm 264: authctxt->server_caused_failure = 0;
1.18 markus 265:
1.28 markus 266: /* try to authenticate user */
1.125 djm 267: m = authmethod_lookup(authctxt, method);
1.117 djm 268: if (m != NULL && authctxt->failures < options.max_authtries) {
1.18 markus 269: debug2("input_userauth_request: try method %s", method);
270: authenticated = m->userauth(authctxt);
271: }
1.126 djm 272: userauth_finish(authctxt, authenticated, method, NULL);
1.49 markus 273:
1.128 djm 274: free(service);
275: free(user);
276: free(method);
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.60 markus 283: char *methods;
1.125 djm 284: int partial = 0;
1.60 markus 285:
1.28 markus 286: if (!authctxt->valid && authenticated)
287: fatal("INTERNAL ERROR: authenticated invalid user %s",
288: authctxt->user);
1.126 djm 289: if (authenticated && authctxt->postponed)
290: fatal("INTERNAL ERROR: authenticated and postponed");
1.18 markus 291:
292: /* Special handling for root */
1.96 markus 293: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.41 markus 294: !auth_root_allowed(method))
1.3 markus 295: authenticated = 0;
296:
1.125 djm 297: if (authenticated && options.num_auth_methods != 0) {
1.127 markus 298: if (!auth2_update_methods_lists(authctxt, method, submethod)) {
1.125 djm 299: authenticated = 0;
300: partial = 1;
301: }
302: }
1.126 djm 303:
304: /* Log before sending the reply */
1.129 ! djm 305: auth_log(authctxt, authenticated, partial, method, submethod);
1.126 djm 306:
307: if (authctxt->postponed)
308: return;
1.125 djm 309:
1.60 markus 310: if (authenticated == 1) {
311: /* turn off userauth */
1.81 markus 312: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.60 markus 313: packet_start(SSH2_MSG_USERAUTH_SUCCESS);
314: packet_send();
315: packet_write_wait();
316: /* now we can break out */
317: authctxt->success = 1;
318: } else {
1.119 djm 319: /* Allow initial try of "none" auth without failure penalty */
1.123 djm 320: if (!authctxt->server_caused_failure &&
321: (authctxt->attempt > 1 || strcmp(method, "none") != 0))
1.119 djm 322: authctxt->failures++;
323: if (authctxt->failures >= options.max_authtries)
1.60 markus 324: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
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.82 markus 384: buffer_append(&b, "\0", 1);
385: list = xstrdup(buffer_ptr(&b));
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: