1 /* $NetBSD: auth2.c,v 1.19 2019/04/20 17:16:40 christos Exp $ */ 2 /* $OpenBSD: auth2.c,v 1.155 2019/03/25 22:34:52 djm 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.19 2019/04/20 17:16:40 christos Exp $"); 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <sys/uio.h> 32 33 #include <fcntl.h> 34 #include <limits.h> 35 #include <pwd.h> 36 #include <stdarg.h> 37 #include <string.h> 38 #include <unistd.h> 39 #include <time.h> 40 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 "compat.h" 50 #include "sshkey.h" 51 #include "hostfile.h" 52 #include "auth.h" 53 #include "dispatch.h" 54 #include "pathnames.h" 55 #include "canohost.h" 56 #include "pfilter.h" 57 58 #ifdef GSSAPI 59 #include "ssh-gss.h" 60 #endif 61 62 #include "monitor_wrap.h" 63 #include "ssherr.h" 64 #include "digest.h" 65 66 /* import */ 67 extern ServerOptions options; 68 extern u_char *session_id2; 69 extern u_int session_id2_len; 70 extern struct sshbuf *loginmsg; 71 72 /* methods */ 73 74 extern Authmethod method_none; 75 extern Authmethod method_pubkey; 76 extern Authmethod method_passwd; 77 extern Authmethod method_kbdint; 78 extern Authmethod method_hostbased; 79 #ifdef KRB5 80 extern Authmethod method_kerberos; 81 #endif 82 #ifdef GSSAPI 83 extern Authmethod method_gssapi; 84 #endif 85 86 static int log_flag = 0; 87 88 Authmethod *authmethods[] = { 89 &method_none, 90 &method_pubkey, 91 #ifdef GSSAPI 92 &method_gssapi, 93 #endif 94 &method_passwd, 95 &method_kbdint, 96 &method_hostbased, 97 #ifdef KRB5 98 &method_kerberos, 99 #endif 100 NULL 101 }; 102 103 /* protocol */ 104 105 static int input_service_request(int, u_int32_t, struct ssh *); 106 static int input_userauth_request(int, u_int32_t, struct ssh *); 107 108 /* helper */ 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("%s: %s", __func__, ssh_err(r)); 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 /*ARGSUSED*/ 195 static int 196 input_service_request(int type, u_int32_t seq, struct ssh *ssh) 197 { 198 Authctxt *authctxt = ssh->authctxt; 199 char *service = NULL; 200 int r, acceptit = 0; 201 202 if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 || 203 (r = sshpkt_get_end(ssh)) != 0) 204 goto out; 205 206 if (authctxt == NULL) 207 fatal("input_service_request: no authctxt"); 208 209 if (strcmp(service, "ssh-userauth") == 0) { 210 if (!authctxt->success) { 211 acceptit = 1; 212 /* now we can handle user-auth requests */ 213 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST, 214 &input_userauth_request); 215 } 216 } 217 /* XXX all other service requests are denied */ 218 219 if (acceptit) { 220 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 || 221 (r = sshpkt_put_cstring(ssh, service)) != 0 || 222 (r = sshpkt_send(ssh)) != 0 || 223 (r = ssh_packet_write_wait(ssh)) < 0) 224 goto out; 225 } else { 226 debug("bad service request %s", service); 227 ssh_packet_disconnect(ssh, "bad service request %s", service); 228 } 229 r = 0; 230 out: 231 free(service); 232 return 0; 233 } 234 235 #define MIN_FAIL_DELAY_SECONDS 0.005 236 static double 237 user_specific_delay(const char *user) 238 { 239 char b[512]; 240 size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512); 241 u_char *hash = xmalloc(len); 242 double delay; 243 244 (void)snprintf(b, sizeof b, "%llu%s", 245 (unsigned long long)options.timing_secret, user); 246 if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0) 247 fatal("%s: ssh_digest_memory", __func__); 248 /* 0-4.2 ms of delay */ 249 delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000; 250 freezero(hash, len); 251 debug3("%s: user specific delay %0.3lfms", __func__, delay/1000); 252 return MIN_FAIL_DELAY_SECONDS + delay; 253 } 254 255 static void 256 ensure_minimum_time_since(double start, double seconds) 257 { 258 struct timespec ts; 259 double elapsed = monotime_double() - start, req = seconds, remain; 260 261 /* if we've already passed the requested time, scale up */ 262 while ((remain = seconds - elapsed) < 0.0) 263 seconds *= 2; 264 265 ts.tv_sec = remain; 266 ts.tv_nsec = (remain - ts.tv_sec) * 1000000000; 267 debug3("%s: elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)", 268 __func__, elapsed*1000, remain*1000, req*1000); 269 nanosleep(&ts, NULL); 270 } 271 272 /*ARGSUSED*/ 273 static int 274 input_userauth_request(int type, u_int32_t seq, struct ssh *ssh) 275 { 276 Authctxt *authctxt = ssh->authctxt; 277 Authmethod *m = NULL; 278 char *user = NULL, *service = NULL, *method = NULL, *style = NULL; 279 int r, authenticated = 0; 280 double tstart = monotime_double(); 281 282 if (authctxt == NULL) 283 fatal("input_userauth_request: no authctxt"); 284 285 if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 || 286 (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 || 287 (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0) 288 goto out; 289 debug("userauth-request for user %s service %s method %s", user, service, method); 290 if (!log_flag) { 291 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s", 292 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), user); 293 log_flag = 1; 294 } 295 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); 296 297 if ((style = strchr(user, ':')) != NULL) 298 *style++ = 0; 299 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("%s: setting up authctxt for %s", 307 __func__, user); 308 } else { 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); 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 *method, 368 const char *submethod) 369 { 370 Authctxt *authctxt = ssh->authctxt; 371 char *methods; 372 int r, partial = 0; 373 374 if (!authctxt->valid && authenticated) 375 fatal("INTERNAL ERROR: authenticated invalid user %s", 376 authctxt->user); 377 if (authenticated && authctxt->postponed) 378 fatal("INTERNAL ERROR: authenticated and postponed"); 379 380 /* Special handling for root */ 381 if (authenticated && authctxt->pw->pw_uid == 0 && 382 !auth_root_allowed(ssh, method)) { 383 authenticated = 0; 384 #ifdef SSH_AUDIT_EVENTS 385 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); 386 #endif 387 } 388 389 #ifdef USE_PAM 390 if (options.use_pam && authenticated) { 391 if (!PRIVSEP(do_pam_account())) { 392 /* if PAM returned a message, send it to the user */ 393 if (sshbuf_len(loginmsg) > 0) { 394 if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0) 395 fatal("%s: buffer error: %s", 396 __func__, ssh_err(r)); 397 userauth_send_banner(ssh, 398 (const char *)sshbuf_ptr(loginmsg)); 399 if ((r = ssh_packet_write_wait(ssh)) < 0) { 400 sshpkt_fatal(ssh, r, 401 "%s: send PAM banner", __func__); 402 } 403 } 404 fatal("Access denied for user %s by PAM account " 405 "configuration", authctxt->user); 406 } 407 } 408 #endif 409 410 if (authenticated && options.num_auth_methods != 0) { 411 if (!auth2_update_methods_lists(authctxt, method, submethod)) { 412 authenticated = 0; 413 partial = 1; 414 } 415 } 416 417 /* Log before sending the reply */ 418 auth_log(ssh, authenticated, partial, method, submethod); 419 420 /* Update information exposed to session */ 421 if (authenticated || partial) 422 auth2_update_session_info(authctxt, method, submethod); 423 424 if (authctxt->postponed) 425 return; 426 427 if (authenticated == 1) { 428 /* turn off userauth */ 429 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST, 430 &dispatch_protocol_ignore); 431 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 || 432 (r = sshpkt_send(ssh)) != 0 || 433 (r = ssh_packet_write_wait(ssh)) < 0) 434 fatal("%s: %s", __func__, ssh_err(r)); 435 /* now we can break out */ 436 authctxt->success = 1; 437 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); 438 } else { 439 /* Allow initial try of "none" auth without failure penalty */ 440 if (!partial && !authctxt->server_caused_failure && 441 (authctxt->attempt > 1 || strcmp(method, "none") != 0)) 442 authctxt->failures++; 443 if (authctxt->failures >= options.max_authtries) 444 auth_maxtries_exceeded(ssh); 445 methods = authmethods_get(authctxt); 446 debug3("%s: failure partial=%d next methods=\"%s\"", __func__, 447 partial, methods); 448 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 || 449 (r = sshpkt_put_cstring(ssh, methods)) != 0 || 450 (r = sshpkt_put_u8(ssh, partial)) != 0 || 451 (r = sshpkt_send(ssh)) != 0 || 452 (r = ssh_packet_write_wait(ssh)) < 0) 453 fatal("%s: %s", __func__, ssh_err(r)); 454 free(methods); 455 } 456 } 457 458 /* 459 * Checks whether method is allowed by at least one AuthenticationMethods 460 * methods list. Returns 1 if allowed, or no methods lists configured. 461 * 0 otherwise. 462 */ 463 int 464 auth2_method_allowed(Authctxt *authctxt, const char *method, 465 const char *submethod) 466 { 467 u_int i; 468 469 /* 470 * NB. authctxt->num_auth_methods might be zero as a result of 471 * auth2_setup_methods_lists(), so check the configuration. 472 */ 473 if (options.num_auth_methods == 0) 474 return 1; 475 for (i = 0; i < authctxt->num_auth_methods; i++) { 476 if (list_starts_with(authctxt->auth_methods[i], method, 477 submethod) != MATCH_NONE) 478 return 1; 479 } 480 return 0; 481 } 482 483 static char * 484 authmethods_get(Authctxt *authctxt) 485 { 486 struct sshbuf *b; 487 char *list; 488 int i, r; 489 490 if ((b = sshbuf_new()) == NULL) 491 fatal("%s: sshbuf_new failed", __func__); 492 for (i = 0; authmethods[i] != NULL; i++) { 493 if (strcmp(authmethods[i]->name, "none") == 0) 494 continue; 495 if (authmethods[i]->enabled == NULL || 496 *(authmethods[i]->enabled) == 0) 497 continue; 498 if (!auth2_method_allowed(authctxt, authmethods[i]->name, 499 NULL)) 500 continue; 501 if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "", 502 authmethods[i]->name)) != 0) 503 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 504 } 505 if ((list = sshbuf_dup_string(b)) == NULL) 506 fatal("%s: sshbuf_dup_string failed", __func__); 507 sshbuf_free(b); 508 return list; 509 } 510 511 static Authmethod * 512 authmethod_lookup(Authctxt *authctxt, const char *name) 513 { 514 int i; 515 516 if (name != NULL) 517 for (i = 0; authmethods[i] != NULL; i++) 518 if (authmethods[i]->enabled != NULL && 519 *(authmethods[i]->enabled) != 0 && 520 strcmp(name, authmethods[i]->name) == 0 && 521 auth2_method_allowed(authctxt, 522 authmethods[i]->name, NULL)) 523 return authmethods[i]; 524 debug2("Unrecognized authentication method name: %s", 525 name ? name : "NULL"); 526 return NULL; 527 } 528 529 /* 530 * Check a comma-separated list of methods for validity. Is need_enable is 531 * non-zero, then also require that the methods are enabled. 532 * Returns 0 on success or -1 if the methods list is invalid. 533 */ 534 int 535 auth2_methods_valid(const char *_methods, int need_enable) 536 { 537 char *methods, *omethods, *method, *p; 538 u_int i, found; 539 int ret = -1; 540 541 if (*_methods == '\0') { 542 error("empty authentication method list"); 543 return -1; 544 } 545 omethods = methods = xstrdup(_methods); 546 while ((method = strsep(&methods, ",")) != NULL) { 547 for (found = i = 0; !found && authmethods[i] != NULL; i++) { 548 if ((p = strchr(method, ':')) != NULL) 549 *p = '\0'; 550 if (strcmp(method, authmethods[i]->name) != 0) 551 continue; 552 if (need_enable) { 553 if (authmethods[i]->enabled == NULL || 554 *(authmethods[i]->enabled) == 0) { 555 error("Disabled method \"%s\" in " 556 "AuthenticationMethods list \"%s\"", 557 method, _methods); 558 goto out; 559 } 560 } 561 found = 1; 562 break; 563 } 564 if (!found) { 565 error("Unknown authentication method \"%s\" in list", 566 method); 567 goto out; 568 } 569 } 570 ret = 0; 571 out: 572 free(omethods); 573 return ret; 574 } 575 576 /* 577 * Prune the AuthenticationMethods supplied in the configuration, removing 578 * any methods lists that include disabled methods. Note that this might 579 * leave authctxt->num_auth_methods == 0, even when multiple required auth 580 * has been requested. For this reason, all tests for whether multiple is 581 * enabled should consult options.num_auth_methods directly. 582 */ 583 int 584 auth2_setup_methods_lists(Authctxt *authctxt) 585 { 586 u_int i; 587 588 /* First, normalise away the "any" pseudo-method */ 589 if (options.num_auth_methods == 1 && 590 strcmp(options.auth_methods[0], "any") == 0) { 591 free(options.auth_methods[0]); 592 options.auth_methods[0] = NULL; 593 options.num_auth_methods = 0; 594 } 595 596 if (options.num_auth_methods == 0) 597 return 0; 598 debug3("%s: checking methods", __func__); 599 authctxt->auth_methods = xcalloc(options.num_auth_methods, 600 sizeof(*authctxt->auth_methods)); 601 authctxt->num_auth_methods = 0; 602 for (i = 0; i < options.num_auth_methods; i++) { 603 if (auth2_methods_valid(options.auth_methods[i], 1) != 0) { 604 logit("Authentication methods list \"%s\" contains " 605 "disabled method, skipping", 606 options.auth_methods[i]); 607 continue; 608 } 609 debug("authentication methods list %d: %s", 610 authctxt->num_auth_methods, options.auth_methods[i]); 611 authctxt->auth_methods[authctxt->num_auth_methods++] = 612 xstrdup(options.auth_methods[i]); 613 } 614 if (authctxt->num_auth_methods == 0) { 615 error("No AuthenticationMethods left after eliminating " 616 "disabled methods"); 617 return -1; 618 } 619 return 0; 620 } 621 622 static int 623 list_starts_with(const char *methods, const char *method, 624 const char *submethod) 625 { 626 size_t l = strlen(method); 627 int match; 628 const char *p; 629 630 if (strncmp(methods, method, l) != 0) 631 return MATCH_NONE; 632 p = methods + l; 633 match = MATCH_METHOD; 634 if (*p == ':') { 635 if (!submethod) 636 return MATCH_PARTIAL; 637 l = strlen(submethod); 638 p += 1; 639 if (strncmp(submethod, p, l)) 640 return MATCH_NONE; 641 p += l; 642 match = MATCH_BOTH; 643 } 644 if (*p != ',' && *p != '\0') 645 return MATCH_NONE; 646 return match; 647 } 648 649 /* 650 * Remove method from the start of a comma-separated list of methods. 651 * Returns 0 if the list of methods did not start with that method or 1 652 * if it did. 653 */ 654 static int 655 remove_method(char **methods, const char *method, const char *submethod) 656 { 657 char *omethods = *methods, *p; 658 size_t l = strlen(method); 659 int match; 660 661 match = list_starts_with(omethods, method, submethod); 662 if (match != MATCH_METHOD && match != MATCH_BOTH) 663 return 0; 664 p = omethods + l; 665 if (submethod && match == MATCH_BOTH) 666 p += 1 + strlen(submethod); /* include colon */ 667 if (*p == ',') 668 p++; 669 *methods = xstrdup(p); 670 free(omethods); 671 return 1; 672 } 673 674 /* 675 * Called after successful authentication. Will remove the successful method 676 * from the start of each list in which it occurs. If it was the last method 677 * in any list, then authentication is deemed successful. 678 * Returns 1 if the method completed any authentication list or 0 otherwise. 679 */ 680 int 681 auth2_update_methods_lists(Authctxt *authctxt, const char *method, 682 const char *submethod) 683 { 684 u_int i, found = 0; 685 686 debug3("%s: updating methods list after \"%s\"", __func__, method); 687 for (i = 0; i < authctxt->num_auth_methods; i++) { 688 if (!remove_method(&(authctxt->auth_methods[i]), method, 689 submethod)) 690 continue; 691 found = 1; 692 if (*authctxt->auth_methods[i] == '\0') { 693 debug2("authentication methods list %d complete", i); 694 return 1; 695 } 696 debug3("authentication methods list %d remaining: \"%s\"", 697 i, authctxt->auth_methods[i]); 698 } 699 /* This should not happen, but would be bad if it did */ 700 if (!found) 701 fatal("%s: method not in AuthenticationMethods", __func__); 702 return 0; 703 } 704 705 /* Reset method-specific information */ 706 void auth2_authctxt_reset_info(Authctxt *authctxt) 707 { 708 sshkey_free(authctxt->auth_method_key); 709 free(authctxt->auth_method_info); 710 authctxt->auth_method_key = NULL; 711 authctxt->auth_method_info = NULL; 712 } 713 714 /* Record auth method-specific information for logs */ 715 void 716 auth2_record_info(Authctxt *authctxt, const char *fmt, ...) 717 { 718 va_list ap; 719 int i; 720 721 free(authctxt->auth_method_info); 722 authctxt->auth_method_info = NULL; 723 724 va_start(ap, fmt); 725 i = vasprintf(&authctxt->auth_method_info, fmt, ap); 726 va_end(ap); 727 728 if (i < 0 || authctxt->auth_method_info == NULL) 729 fatal("%s: vasprintf failed", __func__); 730 } 731 732 /* 733 * Records a public key used in authentication. This is used for logging 734 * and to ensure that the same key is not subsequently accepted again for 735 * multiple authentication. 736 */ 737 void 738 auth2_record_key(Authctxt *authctxt, int authenticated, 739 const struct sshkey *key) 740 { 741 struct sshkey **tmp, *dup; 742 int r; 743 744 if ((r = sshkey_from_private(key, &dup)) != 0) 745 fatal("%s: copy key: %s", __func__, ssh_err(r)); 746 sshkey_free(authctxt->auth_method_key); 747 authctxt->auth_method_key = dup; 748 749 if (!authenticated) 750 return; 751 752 /* If authenticated, make sure we don't accept this key again */ 753 if ((r = sshkey_from_private(key, &dup)) != 0) 754 fatal("%s: copy key: %s", __func__, ssh_err(r)); 755 if (authctxt->nprev_keys >= INT_MAX || 756 (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys, 757 authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL) 758 fatal("%s: reallocarray failed", __func__); 759 authctxt->prev_keys = tmp; 760 authctxt->prev_keys[authctxt->nprev_keys] = dup; 761 authctxt->nprev_keys++; 762 763 } 764 765 /* Checks whether a key has already been previously used for authentication */ 766 int 767 auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key) 768 { 769 u_int i; 770 char *fp; 771 772 for (i = 0; i < authctxt->nprev_keys; i++) { 773 if (sshkey_equal_public(key, authctxt->prev_keys[i])) { 774 fp = sshkey_fingerprint(authctxt->prev_keys[i], 775 options.fingerprint_hash, SSH_FP_DEFAULT); 776 debug3("%s: key already used: %s %s", __func__, 777 sshkey_type(authctxt->prev_keys[i]), 778 fp == NULL ? "UNKNOWN" : fp); 779 free(fp); 780 return 1; 781 } 782 } 783 return 0; 784 } 785 786 /* 787 * Updates authctxt->session_info with details of authentication. Should be 788 * whenever an authentication method succeeds. 789 */ 790 void 791 auth2_update_session_info(Authctxt *authctxt, const char *method, 792 const char *submethod) 793 { 794 int r; 795 796 if (authctxt->session_info == NULL) { 797 if ((authctxt->session_info = sshbuf_new()) == NULL) 798 fatal("%s: sshbuf_new", __func__); 799 } 800 801 /* Append method[/submethod] */ 802 if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s", 803 method, submethod == NULL ? "" : "/", 804 submethod == NULL ? "" : submethod)) != 0) 805 fatal("%s: append method: %s", __func__, ssh_err(r)); 806 807 /* Append key if present */ 808 if (authctxt->auth_method_key != NULL) { 809 if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 || 810 (r = sshkey_format_text(authctxt->auth_method_key, 811 authctxt->session_info)) != 0) 812 fatal("%s: append key: %s", __func__, ssh_err(r)); 813 } 814 815 if (authctxt->auth_method_info != NULL) { 816 /* Ensure no ambiguity here */ 817 if (strchr(authctxt->auth_method_info, '\n') != NULL) 818 fatal("%s: auth_method_info contains \\n", __func__); 819 if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 || 820 (r = sshbuf_putf(authctxt->session_info, "%s", 821 authctxt->auth_method_info)) != 0) { 822 fatal("%s: append method info: %s", 823 __func__, ssh_err(r)); 824 } 825 } 826 if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0) 827 fatal("%s: append: %s", __func__, ssh_err(r)); 828 } 829 830