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