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