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