1 /* $OpenBSD: auth2.c,v 1.155 2019/03/25 22:34:52 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 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 #include <sys/uio.h> 30 31 #include <fcntl.h> 32 #include <limits.h> 33 #include <pwd.h> 34 #include <stdarg.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <time.h> 38 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 extern u_char *session_id2; 63 extern u_int session_id2_len; 64 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; 72 #ifdef GSSAPI 73 extern Authmethod method_gssapi; 74 #endif 75 76 Authmethod *authmethods[] = { 77 &method_none, 78 &method_pubkey, 79 #ifdef GSSAPI 80 &method_gssapi, 81 #endif 82 &method_passwd, 83 &method_kbdint, 84 &method_hostbased, 85 NULL 86 }; 87 88 /* protocol */ 89 90 static int input_service_request(int, u_int32_t, struct ssh *); 91 static int input_userauth_request(int, u_int32_t, struct ssh *); 92 93 /* helper */ 94 static Authmethod *authmethod_lookup(Authctxt *, const char *); 95 static char *authmethods_get(Authctxt *authctxt); 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 *); 102 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 } 117 if (st.st_size <= 0 || st.st_size > 1*1024*1024) { 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) { 128 free(banner); 129 return (NULL); 130 } 131 banner[n] = '\0'; 132 133 return (banner); 134 } 135 136 static void 137 userauth_banner(struct ssh *ssh) 138 { 139 char *banner = NULL; 140 int r; 141 142 if (options.banner == NULL) 143 return; 144 145 if ((banner = PRIVSEP(auth2_read_banner())) == NULL) 146 goto done; 147 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)); 153 debug("userauth_banner: sent"); 154 done: 155 free(banner); 156 } 157 158 /* 159 * loop until authctxt->success == TRUE 160 */ 161 void 162 do_authentication2(struct ssh *ssh) 163 { 164 Authctxt *authctxt = ssh->authctxt; 165 166 ssh_dispatch_init(ssh, &dispatch_protocol_error); 167 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request); 168 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success); 169 ssh->authctxt = NULL; 170 } 171 172 /*ARGSUSED*/ 173 static int 174 input_service_request(int type, u_int32_t seq, struct ssh *ssh) 175 { 176 Authctxt *authctxt = ssh->authctxt; 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; 183 184 if (authctxt == NULL) 185 fatal("input_service_request: no authctxt"); 186 187 if (strcmp(service, "ssh-userauth") == 0) { 188 if (!authctxt->success) { 189 acceptit = 1; 190 /* now we can handle user-auth requests */ 191 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST, 192 &input_userauth_request); 193 } 194 } 195 /* XXX all other service requests are denied */ 196 197 if (acceptit) { 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; 203 } else { 204 debug("bad service request %s", service); 205 ssh_packet_disconnect(ssh, "bad service request %s", service); 206 } 207 r = 0; 208 out: 209 free(service); 210 return 0; 211 } 212 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 222 (void)snprintf(b, sizeof b, "%llu%s", 223 (unsigned long long)options.timing_secret, user); 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 250 /*ARGSUSED*/ 251 static int 252 input_userauth_request(int type, u_int32_t seq, struct ssh *ssh) 253 { 254 Authctxt *authctxt = ssh->authctxt; 255 Authmethod *m = NULL; 256 char *user = NULL, *service = NULL, *method = NULL, *style = NULL; 257 int r, authenticated = 0; 258 double tstart = monotime_double(); 259 260 if (authctxt == NULL) 261 fatal("input_userauth_request: no authctxt"); 262 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; 267 debug("userauth-request for user %s service %s method %s", user, service, method); 268 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); 269 270 if ((style = strchr(user, ':')) != NULL) 271 *style++ = 0; 272 273 if (authctxt->attempt++ == 0) { 274 /* setup auth context */ 275 authctxt->pw = PRIVSEP(getpwnamallow(ssh, user)); 276 if (authctxt->pw && strcmp(service, "ssh-connection")==0) { 277 authctxt->valid = 1; 278 debug2("%s: setting up authctxt for %s", 279 __func__, user); 280 } else { 281 /* Invalid user, fake password information */ 282 authctxt->pw = fakepw(); 283 } 284 ssh_packet_set_log_preamble(ssh, "%suser %s", 285 authctxt->valid ? "authenticating " : "invalid ", user); 286 setproctitle("%s%s", authctxt->valid ? user : "unknown", 287 use_privsep ? " [net]" : ""); 288 authctxt->user = xstrdup(user); 289 authctxt->service = xstrdup(service); 290 authctxt->style = style ? xstrdup(style) : NULL; 291 if (use_privsep) 292 mm_inform_authserv(service, style); 293 userauth_banner(ssh); 294 if (auth2_setup_methods_lists(authctxt) != 0) 295 ssh_packet_disconnect(ssh, 296 "no authentication methods enabled"); 297 } else if (strcmp(user, authctxt->user) != 0 || 298 strcmp(service, authctxt->service) != 0) { 299 ssh_packet_disconnect(ssh, "Change of username or service " 300 "not allowed: (%s,%s) -> (%s,%s)", 301 authctxt->user, authctxt->service, user, service); 302 } 303 /* reset state */ 304 auth2_challenge_stop(ssh); 305 306 #ifdef GSSAPI 307 /* XXX move to auth2_gssapi_stop() */ 308 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 309 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 310 #endif 311 312 auth2_authctxt_reset_info(authctxt); 313 authctxt->postponed = 0; 314 authctxt->server_caused_failure = 0; 315 316 /* try to authenticate user */ 317 m = authmethod_lookup(authctxt, method); 318 if (m != NULL && authctxt->failures < options.max_authtries) { 319 debug2("input_userauth_request: try method %s", method); 320 authenticated = m->userauth(ssh); 321 } 322 if (!authctxt->authenticated) 323 ensure_minimum_time_since(tstart, 324 user_specific_delay(authctxt->user)); 325 userauth_finish(ssh, authenticated, method, NULL); 326 r = 0; 327 out: 328 free(service); 329 free(user); 330 free(method); 331 return r; 332 } 333 334 void 335 userauth_finish(struct ssh *ssh, int authenticated, const char *method, 336 const char *submethod) 337 { 338 Authctxt *authctxt = ssh->authctxt; 339 char *methods; 340 int r, partial = 0; 341 342 if (!authctxt->valid && authenticated) 343 fatal("INTERNAL ERROR: authenticated invalid user %s", 344 authctxt->user); 345 if (authenticated && authctxt->postponed) 346 fatal("INTERNAL ERROR: authenticated and postponed"); 347 348 /* Special handling for root */ 349 if (authenticated && authctxt->pw->pw_uid == 0 && 350 !auth_root_allowed(ssh, method)) 351 authenticated = 0; 352 353 if (authenticated && options.num_auth_methods != 0) { 354 if (!auth2_update_methods_lists(authctxt, method, submethod)) { 355 authenticated = 0; 356 partial = 1; 357 } 358 } 359 360 /* Log before sending the reply */ 361 auth_log(ssh, authenticated, partial, method, submethod); 362 363 /* Update information exposed to session */ 364 if (authenticated || partial) 365 auth2_update_session_info(authctxt, method, submethod); 366 367 if (authctxt->postponed) 368 return; 369 370 if (authenticated == 1) { 371 /* turn off userauth */ 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)); 378 /* now we can break out */ 379 authctxt->success = 1; 380 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); 381 } else { 382 /* Allow initial try of "none" auth without failure penalty */ 383 if (!partial && !authctxt->server_caused_failure && 384 (authctxt->attempt > 1 || strcmp(method, "none") != 0)) 385 authctxt->failures++; 386 if (authctxt->failures >= options.max_authtries) 387 auth_maxtries_exceeded(ssh); 388 methods = authmethods_get(authctxt); 389 debug3("%s: failure partial=%d next methods=\"%s\"", __func__, 390 partial, methods); 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)); 397 free(methods); 398 } 399 } 400 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 */ 406 int 407 auth2_method_allowed(Authctxt *authctxt, const char *method, 408 const char *submethod) 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++) { 419 if (list_starts_with(authctxt->auth_methods[i], method, 420 submethod) != MATCH_NONE) 421 return 1; 422 } 423 return 0; 424 } 425 426 static char * 427 authmethods_get(Authctxt *authctxt) 428 { 429 struct sshbuf *b; 430 char *list; 431 int i, r; 432 433 if ((b = sshbuf_new()) == NULL) 434 fatal("%s: sshbuf_new failed", __func__); 435 for (i = 0; authmethods[i] != NULL; i++) { 436 if (strcmp(authmethods[i]->name, "none") == 0) 437 continue; 438 if (authmethods[i]->enabled == NULL || 439 *(authmethods[i]->enabled) == 0) 440 continue; 441 if (!auth2_method_allowed(authctxt, authmethods[i]->name, 442 NULL)) 443 continue; 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)); 447 } 448 if ((list = sshbuf_dup_string(b)) == NULL) 449 fatal("%s: sshbuf_dup_string failed", __func__); 450 sshbuf_free(b); 451 return list; 452 } 453 454 static Authmethod * 455 authmethod_lookup(Authctxt *authctxt, const char *name) 456 { 457 int i; 458 459 if (name != NULL) 460 for (i = 0; authmethods[i] != NULL; i++) 461 if (authmethods[i]->enabled != NULL && 462 *(authmethods[i]->enabled) != 0 && 463 strcmp(name, authmethods[i]->name) == 0 && 464 auth2_method_allowed(authctxt, 465 authmethods[i]->name, NULL)) 466 return authmethods[i]; 467 debug2("Unrecognized authentication method name: %s", 468 name ? name : "NULL"); 469 return NULL; 470 } 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 { 480 char *methods, *omethods, *method, *p; 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++) { 491 if ((p = strchr(method, ':')) != NULL) 492 *p = '\0'; 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; 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 } 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 566 list_starts_with(const char *methods, const char *method, 567 const char *submethod) 568 { 569 size_t l = strlen(method); 570 int match; 571 const char *p; 572 573 if (strncmp(methods, method, l) != 0) 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; 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 598 remove_method(char **methods, const char *method, const char *submethod) 599 { 600 char *omethods = *methods, *p; 601 size_t l = strlen(method); 602 int match; 603 604 match = list_starts_with(omethods, method, submethod); 605 if (match != MATCH_METHOD && match != MATCH_BOTH) 606 return 0; 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); 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 624 auth2_update_methods_lists(Authctxt *authctxt, const char *method, 625 const char *submethod) 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++) { 631 if (!remove_method(&(authctxt->auth_methods[i]), method, 632 submethod)) 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 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 671 if (i < 0 || authctxt->auth_method_info == NULL) 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 687 if ((r = sshkey_from_private(key, &dup)) != 0) 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 */ 696 if ((r = sshkey_from_private(key, &dup)) != 0) 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 } 772 773