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