Annotation of src/usr.bin/ssh/auth2.c, Revision 1.157
1.157 ! djm 1: /* $OpenBSD: auth2.c,v 1.156 2019/06/28 05:44:09 deraadt 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: #include <sys/types.h>
1.117 djm 27: #include <sys/stat.h>
28: #include <sys/uio.h>
1.111 stevesk 29:
1.117 djm 30: #include <fcntl.h>
1.143 djm 31: #include <limits.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.151 djm 36: #include <time.h>
1.1 markus 37:
1.157 ! djm 38: #include "stdlib.h"
1.117 djm 39: #include "atomicio.h"
1.113 deraadt 40: #include "xmalloc.h"
1.32 markus 41: #include "ssh2.h"
1.1 markus 42: #include "packet.h"
1.32 markus 43: #include "log.h"
1.148 markus 44: #include "sshbuf.h"
1.132 millert 45: #include "misc.h"
1.1 markus 46: #include "servconf.h"
47: #include "compat.h"
1.149 markus 48: #include "sshkey.h"
1.113 deraadt 49: #include "hostfile.h"
1.1 markus 50: #include "auth.h"
51: #include "dispatch.h"
1.29 markus 52: #include "pathnames.h"
1.100 markus 53: #ifdef GSSAPI
54: #include "ssh-gss.h"
55: #endif
1.113 deraadt 56: #include "monitor_wrap.h"
1.143 djm 57: #include "ssherr.h"
1.146 dtucker 58: #include "digest.h"
1.152 djm 59:
1.1 markus 60: /* import */
61: extern ServerOptions options;
1.23 markus 62: extern u_char *session_id2;
1.99 markus 63: extern u_int session_id2_len;
1.1 markus 64:
1.93 markus 65: /* methods */
66:
67: extern Authmethod method_none;
68: extern Authmethod method_pubkey;
69: extern Authmethod method_passwd;
70: extern Authmethod method_kbdint;
71: extern Authmethod method_hostbased;
1.100 markus 72: #ifdef GSSAPI
73: extern Authmethod method_gssapi;
74: #endif
1.93 markus 75:
76: Authmethod *authmethods[] = {
77: &method_none,
78: &method_pubkey,
1.100 markus 79: #ifdef GSSAPI
80: &method_gssapi,
81: #endif
1.93 markus 82: &method_passwd,
83: &method_kbdint,
84: &method_hostbased,
85: NULL
1.18 markus 86: };
87:
1.1 markus 88: /* protocol */
89:
1.139 markus 90: static int input_service_request(int, u_int32_t, struct ssh *);
91: static int input_userauth_request(int, u_int32_t, struct ssh *);
1.1 markus 92:
93: /* helper */
1.125 djm 94: static Authmethod *authmethod_lookup(Authctxt *, const char *);
95: static char *authmethods_get(Authctxt *authctxt);
1.127 markus 96:
97: #define MATCH_NONE 0 /* method or submethod mismatch */
98: #define MATCH_METHOD 1 /* method matches (no submethod specified) */
99: #define MATCH_BOTH 2 /* method and submethod match */
100: #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */
101: static int list_starts_with(const char *, const char *, const char *);
1.1 markus 102:
1.117 djm 103: char *
104: auth2_read_banner(void)
105: {
106: struct stat st;
107: char *banner = NULL;
108: size_t len, n;
109: int fd;
110:
111: if ((fd = open(options.banner, O_RDONLY)) == -1)
112: return (NULL);
113: if (fstat(fd, &st) == -1) {
114: close(fd);
115: return (NULL);
116: }
1.124 djm 117: if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
1.117 djm 118: close(fd);
119: return (NULL);
120: }
121:
122: len = (size_t)st.st_size; /* truncate */
123: banner = xmalloc(len + 1);
124: n = atomicio(read, fd, banner, len);
125: close(fd);
126:
127: if (n != len) {
1.128 djm 128: free(banner);
1.117 djm 129: return (NULL);
130: }
131: banner[n] = '\0';
132:
133: return (banner);
134: }
135:
136: static void
1.153 djm 137: userauth_banner(struct ssh *ssh)
1.117 djm 138: {
139: char *banner = NULL;
1.153 djm 140: int r;
1.117 djm 141:
1.144 djm 142: if (options.banner == NULL)
1.117 djm 143: return;
144:
145: if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
146: goto done;
147:
1.153 djm 148: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
149: (r = sshpkt_put_cstring(ssh, banner)) != 0 ||
150: (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language, unused */
151: (r = sshpkt_send(ssh)) != 0)
152: fatal("%s: %s", __func__, ssh_err(r));
1.117 djm 153: debug("userauth_banner: sent");
154: done:
1.128 djm 155: free(banner);
1.117 djm 156: }
157:
1.1 markus 158: /*
1.18 markus 159: * loop until authctxt->success == TRUE
1.1 markus 160: */
1.103 markus 161: void
1.153 djm 162: do_authentication2(struct ssh *ssh)
1.1 markus 163: {
1.153 djm 164: Authctxt *authctxt = ssh->authctxt;
165:
1.140 markus 166: ssh_dispatch_init(ssh, &dispatch_protocol_error);
167: ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.142 markus 168: ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
1.138 markus 169: ssh->authctxt = NULL;
1.1 markus 170: }
171:
1.109 deraadt 172: /*ARGSUSED*/
1.135 markus 173: static int
1.139 markus 174: input_service_request(int type, u_int32_t seq, struct ssh *ssh)
1.1 markus 175: {
1.138 markus 176: Authctxt *authctxt = ssh->authctxt;
1.153 djm 177: char *service = NULL;
178: int r, acceptit = 0;
179:
180: if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
181: (r = sshpkt_get_end(ssh)) != 0)
182: goto out;
1.1 markus 183:
1.18 markus 184: if (authctxt == NULL)
185: fatal("input_service_request: no authctxt");
186:
1.1 markus 187: if (strcmp(service, "ssh-userauth") == 0) {
1.18 markus 188: if (!authctxt->success) {
1.94 deraadt 189: acceptit = 1;
1.1 markus 190: /* now we can handle user-auth requests */
1.153 djm 191: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
192: &input_userauth_request);
1.1 markus 193: }
194: }
195: /* XXX all other service requests are denied */
196:
1.94 deraadt 197: if (acceptit) {
1.153 djm 198: if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
199: (r = sshpkt_put_cstring(ssh, service)) != 0 ||
200: (r = sshpkt_send(ssh)) != 0 ||
201: (r = ssh_packet_write_wait(ssh)) != 0)
202: goto out;
1.1 markus 203: } else {
204: debug("bad service request %s", service);
1.153 djm 205: ssh_packet_disconnect(ssh, "bad service request %s", service);
1.1 markus 206: }
1.153 djm 207: r = 0;
208: out:
1.128 djm 209: free(service);
1.135 markus 210: return 0;
1.1 markus 211: }
212:
1.146 dtucker 213: #define MIN_FAIL_DELAY_SECONDS 0.005
214: static double
215: user_specific_delay(const char *user)
216: {
217: char b[512];
218: size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
219: u_char *hash = xmalloc(len);
220: double delay;
221:
1.147 dtucker 222: (void)snprintf(b, sizeof b, "%llu%s",
223: (unsigned long long)options.timing_secret, user);
1.146 dtucker 224: if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
225: fatal("%s: ssh_digest_memory", __func__);
226: /* 0-4.2 ms of delay */
227: delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
228: freezero(hash, len);
229: debug3("%s: user specific delay %0.3lfms", __func__, delay/1000);
230: return MIN_FAIL_DELAY_SECONDS + delay;
231: }
232:
233: static void
234: ensure_minimum_time_since(double start, double seconds)
235: {
236: struct timespec ts;
237: double elapsed = monotime_double() - start, req = seconds, remain;
238:
239: /* if we've already passed the requested time, scale up */
240: while ((remain = seconds - elapsed) < 0.0)
241: seconds *= 2;
242:
243: ts.tv_sec = remain;
244: ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
245: debug3("%s: elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
246: __func__, elapsed*1000, remain*1000, req*1000);
247: nanosleep(&ts, NULL);
248: }
249:
1.109 deraadt 250: /*ARGSUSED*/
1.135 markus 251: static int
1.139 markus 252: input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
1.1 markus 253: {
1.138 markus 254: Authctxt *authctxt = ssh->authctxt;
1.18 markus 255: Authmethod *m = NULL;
1.153 djm 256: char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
257: int r, authenticated = 0;
1.146 dtucker 258: double tstart = monotime_double();
1.1 markus 259:
1.18 markus 260: if (authctxt == NULL)
261: fatal("input_userauth_request: no authctxt");
1.1 markus 262:
1.153 djm 263: if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
264: (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
265: (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
266: goto out;
1.1 markus 267: debug("userauth-request for user %s service %s method %s", user, service, method);
1.24 markus 268: debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1 markus 269:
1.28 markus 270: if ((style = strchr(user, ':')) != NULL)
271: *style++ = 0;
272:
1.36 stevesk 273: if (authctxt->attempt++ == 0) {
1.18 markus 274: /* setup auth context */
1.154 djm 275: authctxt->pw = PRIVSEP(getpwnamallow(ssh, user));
1.89 markus 276: if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18 markus 277: authctxt->valid = 1;
1.137 djm 278: debug2("%s: setting up authctxt for %s",
279: __func__, user);
1.18 markus 280: } else {
1.137 djm 281: /* Invalid user, fake password information */
1.102 markus 282: authctxt->pw = fakepw();
1.18 markus 283: }
1.137 djm 284: ssh_packet_set_log_preamble(ssh, "%suser %s",
285: authctxt->valid ? "authenticating " : "invalid ", user);
1.106 djm 286: setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.88 provos 287: use_privsep ? " [net]" : "");
1.18 markus 288: authctxt->user = xstrdup(user);
289: authctxt->service = xstrdup(service);
1.62 markus 290: authctxt->style = style ? xstrdup(style) : NULL;
1.88 provos 291: if (use_privsep)
292: mm_inform_authserv(service, style);
1.153 djm 293: userauth_banner(ssh);
1.125 djm 294: if (auth2_setup_methods_lists(authctxt) != 0)
1.153 djm 295: ssh_packet_disconnect(ssh,
296: "no authentication methods enabled");
1.62 markus 297: } else if (strcmp(user, authctxt->user) != 0 ||
298: strcmp(service, authctxt->service) != 0) {
1.153 djm 299: ssh_packet_disconnect(ssh, "Change of username or service "
300: "not allowed: (%s,%s) -> (%s,%s)",
1.62 markus 301: authctxt->user, authctxt->service, user, service);
1.1 markus 302: }
1.28 markus 303: /* reset state */
1.140 markus 304: auth2_challenge_stop(ssh);
1.100 markus 305:
306: #ifdef GSSAPI
1.120 djm 307: /* XXX move to auth2_gssapi_stop() */
1.140 markus 308: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
309: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
1.100 markus 310: #endif
311:
1.143 djm 312: auth2_authctxt_reset_info(authctxt);
1.28 markus 313: authctxt->postponed = 0;
1.123 djm 314: authctxt->server_caused_failure = 0;
1.18 markus 315:
1.28 markus 316: /* try to authenticate user */
1.125 djm 317: m = authmethod_lookup(authctxt, method);
1.117 djm 318: if (m != NULL && authctxt->failures < options.max_authtries) {
1.18 markus 319: debug2("input_userauth_request: try method %s", method);
1.140 markus 320: authenticated = m->userauth(ssh);
1.18 markus 321: }
1.146 dtucker 322: if (!authctxt->authenticated)
323: ensure_minimum_time_since(tstart,
324: user_specific_delay(authctxt->user));
1.140 markus 325: userauth_finish(ssh, authenticated, method, NULL);
1.153 djm 326: r = 0;
327: out:
1.128 djm 328: free(service);
329: free(user);
330: free(method);
1.153 djm 331: return r;
1.49 markus 332: }
333:
334: void
1.140 markus 335: userauth_finish(struct ssh *ssh, int authenticated, const char *method,
1.126 djm 336: const char *submethod)
1.49 markus 337: {
1.140 markus 338: Authctxt *authctxt = ssh->authctxt;
1.60 markus 339: char *methods;
1.153 djm 340: int r, partial = 0;
1.60 markus 341:
1.28 markus 342: if (!authctxt->valid && authenticated)
343: fatal("INTERNAL ERROR: authenticated invalid user %s",
344: authctxt->user);
1.126 djm 345: if (authenticated && authctxt->postponed)
346: fatal("INTERNAL ERROR: authenticated and postponed");
1.18 markus 347:
348: /* Special handling for root */
1.96 markus 349: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.145 djm 350: !auth_root_allowed(ssh, method))
1.3 markus 351: authenticated = 0;
352:
1.125 djm 353: if (authenticated && options.num_auth_methods != 0) {
1.127 markus 354: if (!auth2_update_methods_lists(authctxt, method, submethod)) {
1.125 djm 355: authenticated = 0;
356: partial = 1;
357: }
358: }
1.126 djm 359:
360: /* Log before sending the reply */
1.154 djm 361: auth_log(ssh, authenticated, partial, method, submethod);
1.126 djm 362:
1.143 djm 363: /* Update information exposed to session */
364: if (authenticated || partial)
365: auth2_update_session_info(authctxt, method, submethod);
366:
1.126 djm 367: if (authctxt->postponed)
368: return;
1.125 djm 369:
1.60 markus 370: if (authenticated == 1) {
371: /* turn off userauth */
1.153 djm 372: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
373: &dispatch_protocol_ignore);
374: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
375: (r = sshpkt_send(ssh)) != 0 ||
376: (r = ssh_packet_write_wait(ssh)) != 0)
377: fatal("%s: %s", __func__, ssh_err(r));
1.60 markus 378: /* now we can break out */
379: authctxt->success = 1;
1.137 djm 380: ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
1.60 markus 381: } else {
1.119 djm 382: /* Allow initial try of "none" auth without failure penalty */
1.133 djm 383: if (!partial && !authctxt->server_caused_failure &&
1.123 djm 384: (authctxt->attempt > 1 || strcmp(method, "none") != 0))
1.119 djm 385: authctxt->failures++;
386: if (authctxt->failures >= options.max_authtries)
1.154 djm 387: auth_maxtries_exceeded(ssh);
1.125 djm 388: methods = authmethods_get(authctxt);
389: debug3("%s: failure partial=%d next methods=\"%s\"", __func__,
390: partial, methods);
1.153 djm 391: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 ||
392: (r = sshpkt_put_cstring(ssh, methods)) != 0 ||
393: (r = sshpkt_put_u8(ssh, partial)) != 0 ||
394: (r = sshpkt_send(ssh)) != 0 ||
395: (r = ssh_packet_write_wait(ssh)) != 0)
396: fatal("%s: %s", __func__, ssh_err(r));
1.128 djm 397: free(methods);
1.60 markus 398: }
1.1 markus 399: }
1.18 markus 400:
1.125 djm 401: /*
402: * Checks whether method is allowed by at least one AuthenticationMethods
403: * methods list. Returns 1 if allowed, or no methods lists configured.
404: * 0 otherwise.
405: */
1.127 markus 406: int
407: auth2_method_allowed(Authctxt *authctxt, const char *method,
408: const char *submethod)
1.125 djm 409: {
410: u_int i;
411:
412: /*
413: * NB. authctxt->num_auth_methods might be zero as a result of
414: * auth2_setup_methods_lists(), so check the configuration.
415: */
416: if (options.num_auth_methods == 0)
417: return 1;
418: for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127 markus 419: if (list_starts_with(authctxt->auth_methods[i], method,
420: submethod) != MATCH_NONE)
1.125 djm 421: return 1;
422: }
423: return 0;
424: }
425:
1.67 stevesk 426: static char *
1.125 djm 427: authmethods_get(Authctxt *authctxt)
1.1 markus 428: {
1.148 markus 429: struct sshbuf *b;
1.18 markus 430: char *list;
1.148 markus 431: int i, r;
1.1 markus 432:
1.148 markus 433: if ((b = sshbuf_new()) == NULL)
434: fatal("%s: sshbuf_new failed", __func__);
1.93 markus 435: for (i = 0; authmethods[i] != NULL; i++) {
436: if (strcmp(authmethods[i]->name, "none") == 0)
1.18 markus 437: continue;
1.125 djm 438: if (authmethods[i]->enabled == NULL ||
439: *(authmethods[i]->enabled) == 0)
440: continue;
1.127 markus 441: if (!auth2_method_allowed(authctxt, authmethods[i]->name,
442: NULL))
1.125 djm 443: continue;
1.148 markus 444: if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "",
445: authmethods[i]->name)) != 0)
446: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1 markus 447: }
1.148 markus 448: if ((list = sshbuf_dup_string(b)) == NULL)
1.136 djm 449: fatal("%s: sshbuf_dup_string failed", __func__);
1.148 markus 450: sshbuf_free(b);
1.18 markus 451: return list;
452: }
453:
1.66 itojun 454: static Authmethod *
1.125 djm 455: authmethod_lookup(Authctxt *authctxt, const char *name)
1.18 markus 456: {
1.93 markus 457: int i;
458:
1.18 markus 459: if (name != NULL)
1.93 markus 460: for (i = 0; authmethods[i] != NULL; i++)
461: if (authmethods[i]->enabled != NULL &&
462: *(authmethods[i]->enabled) != 0 &&
1.125 djm 463: strcmp(name, authmethods[i]->name) == 0 &&
1.127 markus 464: auth2_method_allowed(authctxt,
465: authmethods[i]->name, NULL))
1.93 markus 466: return authmethods[i];
467: debug2("Unrecognized authentication method name: %s",
468: name ? name : "NULL");
1.18 markus 469: return NULL;
1.1 markus 470: }
1.125 djm 471:
472: /*
473: * Check a comma-separated list of methods for validity. Is need_enable is
474: * non-zero, then also require that the methods are enabled.
475: * Returns 0 on success or -1 if the methods list is invalid.
476: */
477: int
478: auth2_methods_valid(const char *_methods, int need_enable)
479: {
1.127 markus 480: char *methods, *omethods, *method, *p;
1.125 djm 481: u_int i, found;
482: int ret = -1;
483:
484: if (*_methods == '\0') {
485: error("empty authentication method list");
486: return -1;
487: }
488: omethods = methods = xstrdup(_methods);
489: while ((method = strsep(&methods, ",")) != NULL) {
490: for (found = i = 0; !found && authmethods[i] != NULL; i++) {
1.127 markus 491: if ((p = strchr(method, ':')) != NULL)
492: *p = '\0';
1.125 djm 493: if (strcmp(method, authmethods[i]->name) != 0)
494: continue;
495: if (need_enable) {
496: if (authmethods[i]->enabled == NULL ||
497: *(authmethods[i]->enabled) == 0) {
498: error("Disabled method \"%s\" in "
499: "AuthenticationMethods list \"%s\"",
500: method, _methods);
501: goto out;
502: }
503: }
504: found = 1;
505: break;
506: }
507: if (!found) {
508: error("Unknown authentication method \"%s\" in list",
509: method);
510: goto out;
511: }
512: }
513: ret = 0;
514: out:
515: free(omethods);
516: return ret;
517: }
518:
519: /*
520: * Prune the AuthenticationMethods supplied in the configuration, removing
521: * any methods lists that include disabled methods. Note that this might
522: * leave authctxt->num_auth_methods == 0, even when multiple required auth
523: * has been requested. For this reason, all tests for whether multiple is
524: * enabled should consult options.num_auth_methods directly.
525: */
526: int
527: auth2_setup_methods_lists(Authctxt *authctxt)
528: {
529: u_int i;
1.155 djm 530:
531: /* First, normalise away the "any" pseudo-method */
532: if (options.num_auth_methods == 1 &&
533: strcmp(options.auth_methods[0], "any") == 0) {
534: free(options.auth_methods[0]);
535: options.auth_methods[0] = NULL;
536: options.num_auth_methods = 0;
537: }
1.125 djm 538:
539: if (options.num_auth_methods == 0)
540: return 0;
541: debug3("%s: checking methods", __func__);
542: authctxt->auth_methods = xcalloc(options.num_auth_methods,
543: sizeof(*authctxt->auth_methods));
544: authctxt->num_auth_methods = 0;
545: for (i = 0; i < options.num_auth_methods; i++) {
546: if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
547: logit("Authentication methods list \"%s\" contains "
548: "disabled method, skipping",
549: options.auth_methods[i]);
550: continue;
551: }
552: debug("authentication methods list %d: %s",
553: authctxt->num_auth_methods, options.auth_methods[i]);
554: authctxt->auth_methods[authctxt->num_auth_methods++] =
555: xstrdup(options.auth_methods[i]);
556: }
557: if (authctxt->num_auth_methods == 0) {
558: error("No AuthenticationMethods left after eliminating "
559: "disabled methods");
560: return -1;
561: }
562: return 0;
563: }
564:
565: static int
1.127 markus 566: list_starts_with(const char *methods, const char *method,
567: const char *submethod)
1.125 djm 568: {
569: size_t l = strlen(method);
1.127 markus 570: int match;
571: const char *p;
1.125 djm 572:
573: if (strncmp(methods, method, l) != 0)
1.127 markus 574: return MATCH_NONE;
575: p = methods + l;
576: match = MATCH_METHOD;
577: if (*p == ':') {
578: if (!submethod)
579: return MATCH_PARTIAL;
580: l = strlen(submethod);
581: p += 1;
582: if (strncmp(submethod, p, l))
583: return MATCH_NONE;
584: p += l;
585: match = MATCH_BOTH;
586: }
587: if (*p != ',' && *p != '\0')
588: return MATCH_NONE;
589: return match;
1.125 djm 590: }
591:
592: /*
593: * Remove method from the start of a comma-separated list of methods.
594: * Returns 0 if the list of methods did not start with that method or 1
595: * if it did.
596: */
597: static int
1.127 markus 598: remove_method(char **methods, const char *method, const char *submethod)
1.125 djm 599: {
1.127 markus 600: char *omethods = *methods, *p;
1.125 djm 601: size_t l = strlen(method);
1.127 markus 602: int match;
1.125 djm 603:
1.127 markus 604: match = list_starts_with(omethods, method, submethod);
605: if (match != MATCH_METHOD && match != MATCH_BOTH)
1.125 djm 606: return 0;
1.127 markus 607: p = omethods + l;
608: if (submethod && match == MATCH_BOTH)
609: p += 1 + strlen(submethod); /* include colon */
610: if (*p == ',')
611: p++;
612: *methods = xstrdup(p);
1.125 djm 613: free(omethods);
614: return 1;
615: }
616:
617: /*
618: * Called after successful authentication. Will remove the successful method
619: * from the start of each list in which it occurs. If it was the last method
620: * in any list, then authentication is deemed successful.
621: * Returns 1 if the method completed any authentication list or 0 otherwise.
622: */
623: int
1.127 markus 624: auth2_update_methods_lists(Authctxt *authctxt, const char *method,
625: const char *submethod)
1.125 djm 626: {
627: u_int i, found = 0;
628:
629: debug3("%s: updating methods list after \"%s\"", __func__, method);
630: for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127 markus 631: if (!remove_method(&(authctxt->auth_methods[i]), method,
632: submethod))
1.125 djm 633: continue;
634: found = 1;
635: if (*authctxt->auth_methods[i] == '\0') {
636: debug2("authentication methods list %d complete", i);
637: return 1;
638: }
639: debug3("authentication methods list %d remaining: \"%s\"",
640: i, authctxt->auth_methods[i]);
641: }
642: /* This should not happen, but would be bad if it did */
643: if (!found)
644: fatal("%s: method not in AuthenticationMethods", __func__);
645: return 0;
646: }
647:
1.143 djm 648: /* Reset method-specific information */
649: void auth2_authctxt_reset_info(Authctxt *authctxt)
650: {
651: sshkey_free(authctxt->auth_method_key);
652: free(authctxt->auth_method_info);
653: authctxt->auth_method_key = NULL;
654: authctxt->auth_method_info = NULL;
655: }
656:
657: /* Record auth method-specific information for logs */
658: void
659: auth2_record_info(Authctxt *authctxt, const char *fmt, ...)
660: {
661: va_list ap;
662: int i;
663:
664: free(authctxt->auth_method_info);
665: authctxt->auth_method_info = NULL;
666:
667: va_start(ap, fmt);
668: i = vasprintf(&authctxt->auth_method_info, fmt, ap);
669: va_end(ap);
670:
1.156 deraadt 671: if (i == -1)
1.143 djm 672: fatal("%s: vasprintf failed", __func__);
673: }
674:
675: /*
676: * Records a public key used in authentication. This is used for logging
677: * and to ensure that the same key is not subsequently accepted again for
678: * multiple authentication.
679: */
680: void
681: auth2_record_key(Authctxt *authctxt, int authenticated,
682: const struct sshkey *key)
683: {
684: struct sshkey **tmp, *dup;
685: int r;
686:
1.150 djm 687: if ((r = sshkey_from_private(key, &dup)) != 0)
1.143 djm 688: fatal("%s: copy key: %s", __func__, ssh_err(r));
689: sshkey_free(authctxt->auth_method_key);
690: authctxt->auth_method_key = dup;
691:
692: if (!authenticated)
693: return;
694:
695: /* If authenticated, make sure we don't accept this key again */
1.150 djm 696: if ((r = sshkey_from_private(key, &dup)) != 0)
1.143 djm 697: fatal("%s: copy key: %s", __func__, ssh_err(r));
698: if (authctxt->nprev_keys >= INT_MAX ||
699: (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys,
700: authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL)
701: fatal("%s: reallocarray failed", __func__);
702: authctxt->prev_keys = tmp;
703: authctxt->prev_keys[authctxt->nprev_keys] = dup;
704: authctxt->nprev_keys++;
705:
706: }
707:
708: /* Checks whether a key has already been previously used for authentication */
709: int
710: auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key)
711: {
712: u_int i;
713: char *fp;
714:
715: for (i = 0; i < authctxt->nprev_keys; i++) {
716: if (sshkey_equal_public(key, authctxt->prev_keys[i])) {
717: fp = sshkey_fingerprint(authctxt->prev_keys[i],
718: options.fingerprint_hash, SSH_FP_DEFAULT);
719: debug3("%s: key already used: %s %s", __func__,
720: sshkey_type(authctxt->prev_keys[i]),
721: fp == NULL ? "UNKNOWN" : fp);
722: free(fp);
723: return 1;
724: }
725: }
726: return 0;
727: }
728:
729: /*
730: * Updates authctxt->session_info with details of authentication. Should be
731: * whenever an authentication method succeeds.
732: */
733: void
734: auth2_update_session_info(Authctxt *authctxt, const char *method,
735: const char *submethod)
736: {
737: int r;
738:
739: if (authctxt->session_info == NULL) {
740: if ((authctxt->session_info = sshbuf_new()) == NULL)
741: fatal("%s: sshbuf_new", __func__);
742: }
743:
744: /* Append method[/submethod] */
745: if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s",
746: method, submethod == NULL ? "" : "/",
747: submethod == NULL ? "" : submethod)) != 0)
748: fatal("%s: append method: %s", __func__, ssh_err(r));
749:
750: /* Append key if present */
751: if (authctxt->auth_method_key != NULL) {
752: if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
753: (r = sshkey_format_text(authctxt->auth_method_key,
754: authctxt->session_info)) != 0)
755: fatal("%s: append key: %s", __func__, ssh_err(r));
756: }
757:
758: if (authctxt->auth_method_info != NULL) {
759: /* Ensure no ambiguity here */
760: if (strchr(authctxt->auth_method_info, '\n') != NULL)
761: fatal("%s: auth_method_info contains \\n", __func__);
762: if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
763: (r = sshbuf_putf(authctxt->session_info, "%s",
764: authctxt->auth_method_info)) != 0) {
765: fatal("%s: append method info: %s",
766: __func__, ssh_err(r));
767: }
768: }
769: if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0)
770: fatal("%s: append: %s", __func__, ssh_err(r));
771: }
1.117 djm 772: