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