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