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