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