1 /* $NetBSD: auth.c,v 1.17 2016/08/02 13:45:12 christos Exp $ */ 2 /* $OpenBSD: auth.c,v 1.115 2016/06/15 00:40:40 dtucker 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: auth.c,v 1.17 2016/08/02 13:45:12 christos Exp $"); 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <sys/socket.h> 32 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <libgen.h> 36 #include <login_cap.h> 37 #include <paths.h> 38 #include <pwd.h> 39 #include <stdarg.h> 40 #include <stdio.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <limits.h> 44 #include <netdb.h> 45 46 #include "xmalloc.h" 47 #include "match.h" 48 #include "groupaccess.h" 49 #include "log.h" 50 #include "buffer.h" 51 #include "misc.h" 52 #include "servconf.h" 53 #include "key.h" 54 #include "hostfile.h" 55 #include "auth.h" 56 #include "auth-options.h" 57 #include "canohost.h" 58 #include "uidswap.h" 59 #include "packet.h" 60 #ifdef GSSAPI 61 #include "ssh-gss.h" 62 #endif 63 #include "authfile.h" 64 #include "monitor_wrap.h" 65 #include "authfile.h" 66 #include "ssherr.h" 67 #include "compat.h" 68 #include "pfilter.h" 69 70 #ifdef HAVE_LOGIN_CAP 71 #include <login_cap.h> 72 #endif 73 74 /* import */ 75 extern ServerOptions options; 76 extern int use_privsep; 77 78 /* Debugging messages */ 79 Buffer auth_debug; 80 int auth_debug_init; 81 82 #ifndef HOST_ONLY 83 /* 84 * Check if the user is allowed to log in via ssh. If user is listed 85 * in DenyUsers or one of user's groups is listed in DenyGroups, false 86 * will be returned. If AllowUsers isn't empty and user isn't listed 87 * there, or if AllowGroups isn't empty and one of user's groups isn't 88 * listed there, false will be returned. 89 * If the user's shell is not executable, false will be returned. 90 * Otherwise true is returned. 91 */ 92 int 93 allowed_user(struct passwd * pw) 94 { 95 #ifdef HAVE_LOGIN_CAP 96 extern login_cap_t *lc; 97 int match_name, match_ip; 98 char *cap_hlist, *hp; 99 #endif 100 struct ssh *ssh = active_state; /* XXX */ 101 struct stat st; 102 const char *hostname = NULL, *ipaddr = NULL; 103 u_int i; 104 105 /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 106 if (!pw || !pw->pw_name) 107 return 0; 108 109 #ifdef HAVE_LOGIN_CAP 110 hostname = auth_get_canonical_hostname(ssh, options.use_dns); 111 ipaddr = ssh_remote_ipaddr(ssh); 112 113 lc = login_getclass(pw->pw_class); 114 115 /* 116 * Check the deny list. 117 */ 118 cap_hlist = login_getcapstr(lc, "host.deny", NULL, NULL); 119 if (cap_hlist != NULL) { 120 hp = strtok(cap_hlist, ","); 121 while (hp != NULL) { 122 match_name = match_hostname(hostname, hp); 123 match_ip = match_hostname(ipaddr, hp); 124 /* 125 * Only a positive match here causes a "deny". 126 */ 127 if (match_name > 0 || match_ip > 0) { 128 free(cap_hlist); 129 login_close(lc); 130 return 0; 131 } 132 hp = strtok(NULL, ","); 133 } 134 free(cap_hlist); 135 } 136 137 /* 138 * Check the allow list. If the allow list exists, and the 139 * remote host is not in it, the user is implicitly denied. 140 */ 141 cap_hlist = login_getcapstr(lc, "host.allow", NULL, NULL); 142 if (cap_hlist != NULL) { 143 hp = strtok(cap_hlist, ","); 144 if (hp == NULL) { 145 /* Just in case there's an empty string... */ 146 free(cap_hlist); 147 login_close(lc); 148 return 0; 149 } 150 while (hp != NULL) { 151 match_name = match_hostname(hostname, hp); 152 match_ip = match_hostname(ipaddr, hp); 153 /* 154 * Negative match causes an immediate "deny". 155 * Positive match causes us to break out 156 * of the loop (allowing a fallthrough). 157 */ 158 if (match_name < 0 || match_ip < 0) { 159 free(cap_hlist); 160 login_close(lc); 161 return 0; 162 } 163 if (match_name > 0 || match_ip > 0) 164 break; 165 hp = strtok(NULL, ","); 166 } 167 free(cap_hlist); 168 if (hp == NULL) { 169 login_close(lc); 170 return 0; 171 } 172 } 173 174 login_close(lc); 175 #endif 176 177 #ifdef USE_PAM 178 if (!options.use_pam) { 179 #endif 180 /* 181 * password/account expiration. 182 */ 183 if (pw->pw_change || pw->pw_expire) { 184 struct timeval tv; 185 186 (void)gettimeofday(&tv, (struct timezone *)NULL); 187 if (pw->pw_expire) { 188 if (tv.tv_sec >= pw->pw_expire) { 189 logit("User %.100s not allowed because account has expired", 190 pw->pw_name); 191 return 0; /* expired */ 192 } 193 } 194 #ifdef _PASSWORD_CHGNOW 195 if (pw->pw_change == _PASSWORD_CHGNOW) { 196 logit("User %.100s not allowed because password needs to be changed", 197 pw->pw_name); 198 199 return 0; /* can't force password change (yet) */ 200 } 201 #endif 202 if (pw->pw_change) { 203 if (tv.tv_sec >= pw->pw_change) { 204 logit("User %.100s not allowed because password has expired", 205 pw->pw_name); 206 return 0; /* expired */ 207 } 208 } 209 } 210 #ifdef USE_PAM 211 } 212 #endif 213 214 /* 215 * Deny if shell does not exist or is not executable unless we 216 * are chrooting. 217 */ 218 /* 219 * XXX Should check to see if it is executable by the 220 * XXX requesting user. --thorpej 221 */ 222 if (options.chroot_directory == NULL || 223 strcasecmp(options.chroot_directory, "none") == 0) { 224 char *shell = xstrdup((pw->pw_shell[0] == '\0') ? 225 _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */ 226 227 if (stat(shell, &st) != 0) { 228 logit("User %.100s not allowed because shell %.100s " 229 "does not exist", pw->pw_name, shell); 230 free(shell); 231 return 0; 232 } 233 if (S_ISREG(st.st_mode) == 0 || 234 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) { 235 logit("User %.100s not allowed because shell %.100s " 236 "is not executable", pw->pw_name, shell); 237 free(shell); 238 return 0; 239 } 240 free(shell); 241 } 242 /* 243 * XXX Consider nuking {Allow,Deny}{Users,Groups}. We have the 244 * XXX login_cap(3) mechanism which covers all other types of 245 * XXX logins, too. 246 */ 247 248 if (options.num_deny_users > 0 || options.num_allow_users > 0 || 249 options.num_deny_groups > 0 || options.num_allow_groups > 0) { 250 hostname = auth_get_canonical_hostname(ssh, options.use_dns); 251 ipaddr = ssh_remote_ipaddr(ssh); 252 } 253 254 /* Return false if user is listed in DenyUsers */ 255 if (options.num_deny_users > 0) { 256 for (i = 0; i < options.num_deny_users; i++) 257 if (match_user(pw->pw_name, hostname, ipaddr, 258 options.deny_users[i])) { 259 logit("User %.100s from %.100s not allowed " 260 "because listed in DenyUsers", 261 pw->pw_name, hostname); 262 return 0; 263 } 264 } 265 /* Return false if AllowUsers isn't empty and user isn't listed there */ 266 if (options.num_allow_users > 0) { 267 for (i = 0; i < options.num_allow_users; i++) 268 if (match_user(pw->pw_name, hostname, ipaddr, 269 options.allow_users[i])) 270 break; 271 /* i < options.num_allow_users iff we break for loop */ 272 if (i >= options.num_allow_users) { 273 logit("User %.100s from %.100s not allowed because " 274 "not listed in AllowUsers", pw->pw_name, hostname); 275 return 0; 276 } 277 } 278 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { 279 /* Get the user's group access list (primary and supplementary) */ 280 if (ga_init(pw->pw_name, pw->pw_gid) == 0) { 281 logit("User %.100s from %.100s not allowed because " 282 "not in any group", pw->pw_name, hostname); 283 return 0; 284 } 285 286 /* Return false if one of user's groups is listed in DenyGroups */ 287 if (options.num_deny_groups > 0) 288 if (ga_match(options.deny_groups, 289 options.num_deny_groups)) { 290 ga_free(); 291 logit("User %.100s from %.100s not allowed " 292 "because a group is listed in DenyGroups", 293 pw->pw_name, hostname); 294 return 0; 295 } 296 /* 297 * Return false if AllowGroups isn't empty and one of user's groups 298 * isn't listed there 299 */ 300 if (options.num_allow_groups > 0) 301 if (!ga_match(options.allow_groups, 302 options.num_allow_groups)) { 303 ga_free(); 304 logit("User %.100s from %.100s not allowed " 305 "because none of user's groups are listed " 306 "in AllowGroups", pw->pw_name, hostname); 307 return 0; 308 } 309 ga_free(); 310 } 311 /* We found no reason not to let this user try to log on... */ 312 return 1; 313 } 314 315 void 316 auth_info(Authctxt *authctxt, const char *fmt, ...) 317 { 318 va_list ap; 319 int i; 320 321 free(authctxt->info); 322 authctxt->info = NULL; 323 324 va_start(ap, fmt); 325 i = vasprintf(&authctxt->info, fmt, ap); 326 va_end(ap); 327 328 if (i < 0 || authctxt->info == NULL) 329 fatal("vasprintf failed"); 330 } 331 332 void 333 auth_log(Authctxt *authctxt, int authenticated, int partial, 334 const char *method, const char *submethod) 335 { 336 struct ssh *ssh = active_state; /* XXX */ 337 void (*authlog) (const char *fmt,...) = verbose; 338 const char *authmsg; 339 340 if (use_privsep && !mm_is_monitor() && !authctxt->postponed) 341 return; 342 343 /* Raise logging level */ 344 if (authenticated == 1 || 345 !authctxt->valid || 346 authctxt->failures >= options.max_authtries / 2 || 347 strcmp(method, "password") == 0) 348 authlog = logit; 349 350 if (authctxt->postponed) 351 authmsg = "Postponed"; 352 else if (partial) 353 authmsg = "Partial"; 354 else 355 authmsg = authenticated ? "Accepted" : "Failed"; 356 357 authlog("%s %s%s%s for %s%.100s from %.200s port %d %s%s%s", 358 authmsg, 359 method, 360 submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod, 361 authctxt->valid ? "" : "invalid user ", 362 authctxt->user, 363 ssh_remote_ipaddr(ssh), 364 ssh_remote_port(ssh), 365 compat20 ? "ssh2" : "ssh1", 366 authctxt->info != NULL ? ": " : "", 367 authctxt->info != NULL ? authctxt->info : ""); 368 if (!authctxt->postponed) 369 pfilter_notify(!authenticated); 370 free(authctxt->info); 371 authctxt->info = NULL; 372 } 373 374 void 375 auth_maxtries_exceeded(Authctxt *authctxt) 376 { 377 struct ssh *ssh = active_state; /* XXX */ 378 379 error("maximum authentication attempts exceeded for " 380 "%s%.100s from %.200s port %d %s", 381 authctxt->valid ? "" : "invalid user ", 382 authctxt->user, 383 ssh_remote_ipaddr(ssh), 384 ssh_remote_port(ssh), 385 compat20 ? "ssh2" : "ssh1"); 386 packet_disconnect("Too many authentication failures"); 387 /* NOTREACHED */ 388 } 389 390 /* 391 * Check whether root logins are disallowed. 392 */ 393 int 394 auth_root_allowed(const char *method) 395 { 396 struct ssh *ssh = active_state; /* XXX */ 397 398 switch (options.permit_root_login) { 399 case PERMIT_YES: 400 return 1; 401 case PERMIT_NO_PASSWD: 402 if (strcmp(method, "publickey") == 0 || 403 strcmp(method, "hostbased") == 0 || 404 strcmp(method, "gssapi-with-mic") == 0) 405 return 1; 406 break; 407 case PERMIT_FORCED_ONLY: 408 if (forced_command) { 409 logit("Root login accepted for forced command."); 410 return 1; 411 } 412 break; 413 } 414 logit("ROOT LOGIN REFUSED FROM %.200s port %d", 415 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 416 return 0; 417 } 418 419 420 /* 421 * Given a template and a passwd structure, build a filename 422 * by substituting % tokenised options. Currently, %% becomes '%', 423 * %h becomes the home directory and %u the username. 424 * 425 * This returns a buffer allocated by xmalloc. 426 */ 427 char * 428 expand_authorized_keys(const char *filename, struct passwd *pw) 429 { 430 char *file, ret[PATH_MAX]; 431 int i; 432 433 file = percent_expand(filename, "h", pw->pw_dir, 434 "u", pw->pw_name, (char *)NULL); 435 436 /* 437 * Ensure that filename starts anchored. If not, be backward 438 * compatible and prepend the '%h/' 439 */ 440 if (*file == '/') 441 return (file); 442 443 i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file); 444 if (i < 0 || (size_t)i >= sizeof(ret)) 445 fatal("expand_authorized_keys: path too long"); 446 free(file); 447 return (xstrdup(ret)); 448 } 449 450 char * 451 authorized_principals_file(struct passwd *pw) 452 { 453 if (options.authorized_principals_file == NULL) 454 return NULL; 455 return expand_authorized_keys(options.authorized_principals_file, pw); 456 } 457 458 /* return ok if key exists in sysfile or userfile */ 459 HostStatus 460 check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host, 461 const char *sysfile, const char *userfile) 462 { 463 char *user_hostfile; 464 struct stat st; 465 HostStatus host_status; 466 struct hostkeys *hostkeys; 467 const struct hostkey_entry *found; 468 469 hostkeys = init_hostkeys(); 470 load_hostkeys(hostkeys, host, sysfile); 471 if (userfile != NULL) { 472 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 473 if (options.strict_modes && 474 (stat(user_hostfile, &st) == 0) && 475 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 476 (st.st_mode & 022) != 0)) { 477 logit("Authentication refused for %.100s: " 478 "bad owner or modes for %.200s", 479 pw->pw_name, user_hostfile); 480 auth_debug_add("Ignored %.200s: bad ownership or modes", 481 user_hostfile); 482 } else { 483 temporarily_use_uid(pw); 484 load_hostkeys(hostkeys, host, user_hostfile); 485 restore_uid(); 486 } 487 free(user_hostfile); 488 } 489 host_status = check_key_in_hostkeys(hostkeys, key, &found); 490 if (host_status == HOST_REVOKED) 491 error("WARNING: revoked key for %s attempted authentication", 492 found->host); 493 else if (host_status == HOST_OK) 494 debug("%s: key for %s found at %s:%ld", __func__, 495 found->host, found->file, found->line); 496 else 497 debug("%s: key for host %s not found", __func__, host); 498 499 free_hostkeys(hostkeys); 500 501 return host_status; 502 } 503 504 /* 505 * Check a given path for security. This is defined as all components 506 * of the path to the file must be owned by either the owner of 507 * of the file or root and no directories must be group or world writable. 508 * 509 * XXX Should any specific check be done for sym links ? 510 * 511 * Takes a file name, its stat information (preferably from fstat() to 512 * avoid races), the uid of the expected owner, their home directory and an 513 * error buffer plus max size as arguments. 514 * 515 * Returns 0 on success and -1 on failure 516 */ 517 int 518 auth_secure_path(const char *name, struct stat *stp, const char *pw_dir, 519 uid_t uid, char *err, size_t errlen) 520 { 521 char buf[PATH_MAX], homedir[PATH_MAX]; 522 char *cp; 523 int comparehome = 0; 524 struct stat st; 525 526 if (realpath(name, buf) == NULL) { 527 snprintf(err, errlen, "realpath %s failed: %s", name, 528 strerror(errno)); 529 return -1; 530 } 531 if (pw_dir != NULL && realpath(pw_dir, homedir) != NULL) 532 comparehome = 1; 533 534 if (!S_ISREG(stp->st_mode)) { 535 snprintf(err, errlen, "%s is not a regular file", buf); 536 return -1; 537 } 538 if ((stp->st_uid != 0 && stp->st_uid != uid) || 539 (stp->st_mode & 022) != 0) { 540 snprintf(err, errlen, "bad ownership or modes for file %s", 541 buf); 542 return -1; 543 } 544 545 /* for each component of the canonical path, walking upwards */ 546 for (;;) { 547 if ((cp = dirname(buf)) == NULL) { 548 snprintf(err, errlen, "dirname() failed"); 549 return -1; 550 } 551 strlcpy(buf, cp, sizeof(buf)); 552 553 if (stat(buf, &st) < 0 || 554 (st.st_uid != 0 && st.st_uid != uid) || 555 (st.st_mode & 022) != 0) { 556 snprintf(err, errlen, 557 "bad ownership or modes for directory %s", buf); 558 return -1; 559 } 560 561 /* If are past the homedir then we can stop */ 562 if (comparehome && strcmp(homedir, buf) == 0) 563 break; 564 565 /* 566 * dirname should always complete with a "/" path, 567 * but we can be paranoid and check for "." too 568 */ 569 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) 570 break; 571 } 572 return 0; 573 } 574 575 /* 576 * Version of secure_path() that accepts an open file descriptor to 577 * avoid races. 578 * 579 * Returns 0 on success and -1 on failure 580 */ 581 static int 582 secure_filename(FILE *f, const char *file, struct passwd *pw, 583 char *err, size_t errlen) 584 { 585 struct stat st; 586 587 /* check the open file to avoid races */ 588 if (fstat(fileno(f), &st) < 0) { 589 snprintf(err, errlen, "cannot stat file %s: %s", 590 file, strerror(errno)); 591 return -1; 592 } 593 return auth_secure_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen); 594 } 595 596 static FILE * 597 auth_openfile(const char *file, struct passwd *pw, int strict_modes, 598 int log_missing, const char *file_type) 599 { 600 char line[1024]; 601 struct stat st; 602 int fd; 603 FILE *f; 604 605 if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) { 606 if (log_missing || errno != ENOENT) 607 debug("Could not open %s '%s': %s", file_type, file, 608 strerror(errno)); 609 return NULL; 610 } 611 612 if (fstat(fd, &st) < 0) { 613 close(fd); 614 return NULL; 615 } 616 if (!S_ISREG(st.st_mode)) { 617 logit("User %s %s %s is not a regular file", 618 pw->pw_name, file_type, file); 619 close(fd); 620 return NULL; 621 } 622 unset_nonblock(fd); 623 if ((f = fdopen(fd, "r")) == NULL) { 624 close(fd); 625 return NULL; 626 } 627 if (strict_modes && 628 secure_filename(f, file, pw, line, sizeof(line)) != 0) { 629 fclose(f); 630 logit("Authentication refused: %s", line); 631 auth_debug_add("Ignored %s: %s", file_type, line); 632 return NULL; 633 } 634 635 return f; 636 } 637 638 639 FILE * 640 auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes) 641 { 642 return auth_openfile(file, pw, strict_modes, 1, "authorized keys"); 643 } 644 645 FILE * 646 auth_openprincipals(const char *file, struct passwd *pw, int strict_modes) 647 { 648 return auth_openfile(file, pw, strict_modes, 0, 649 "authorized principals"); 650 } 651 652 struct passwd * 653 getpwnamallow(const char *user) 654 { 655 #ifdef HAVE_LOGIN_CAP 656 extern login_cap_t *lc; 657 #ifdef BSD_AUTH 658 auth_session_t *as; 659 #endif 660 #endif 661 struct ssh *ssh = active_state; /* XXX */ 662 struct passwd *pw; 663 struct connection_info *ci = get_connection_info(1, options.use_dns); 664 665 ci->user = user; 666 parse_server_match_config(&options, ci); 667 668 pw = getpwnam(user); 669 if (pw == NULL) { 670 pfilter_notify(1); 671 logit("Invalid user %.100s from %.100s port %d", 672 user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 673 return (NULL); 674 } 675 if (!allowed_user(pw)) 676 return (NULL); 677 #ifdef HAVE_LOGIN_CAP 678 if ((lc = login_getclass(pw->pw_class)) == NULL) { 679 debug("unable to get login class: %s", user); 680 return (NULL); 681 } 682 #ifdef BSD_AUTH 683 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 684 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 685 debug("Approval failure for %s", user); 686 pw = NULL; 687 } 688 if (as != NULL) 689 auth_close(as); 690 #endif 691 #endif 692 if (pw != NULL) 693 return (pwcopy(pw)); 694 return (NULL); 695 } 696 697 /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */ 698 int 699 auth_key_is_revoked(Key *key) 700 { 701 char *fp = NULL; 702 int r; 703 704 if (options.revoked_keys_file == NULL) 705 return 0; 706 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, 707 SSH_FP_DEFAULT)) == NULL) { 708 r = SSH_ERR_ALLOC_FAIL; 709 error("%s: fingerprint key: %s", __func__, ssh_err(r)); 710 goto out; 711 } 712 713 r = sshkey_check_revoked(key, options.revoked_keys_file); 714 switch (r) { 715 case 0: 716 break; /* not revoked */ 717 case SSH_ERR_KEY_REVOKED: 718 error("Authentication key %s %s revoked by file %s", 719 sshkey_type(key), fp, options.revoked_keys_file); 720 goto out; 721 default: 722 error("Error checking authentication key %s %s in " 723 "revoked keys file %s: %s", sshkey_type(key), fp, 724 options.revoked_keys_file, ssh_err(r)); 725 goto out; 726 } 727 728 /* Success */ 729 r = 0; 730 731 out: 732 free(fp); 733 return r == 0 ? 0 : 1; 734 } 735 736 void 737 auth_debug_add(const char *fmt,...) 738 { 739 char buf[1024]; 740 va_list args; 741 742 if (!auth_debug_init) 743 return; 744 745 va_start(args, fmt); 746 vsnprintf(buf, sizeof(buf), fmt, args); 747 va_end(args); 748 buffer_put_cstring(&auth_debug, buf); 749 } 750 751 void 752 auth_debug_send(void) 753 { 754 char *msg; 755 756 if (!auth_debug_init) 757 return; 758 while (buffer_len(&auth_debug)) { 759 msg = buffer_get_string(&auth_debug, NULL); 760 packet_send_debug("%s", msg); 761 free(msg); 762 } 763 } 764 765 void 766 auth_debug_reset(void) 767 { 768 if (auth_debug_init) 769 buffer_clear(&auth_debug); 770 else { 771 buffer_init(&auth_debug); 772 auth_debug_init = 1; 773 } 774 } 775 776 struct passwd * 777 fakepw(void) 778 { 779 static struct passwd fake; 780 static char nouser[] = "NOUSER"; 781 static char nonexist[] = "/nonexist"; 782 783 memset(&fake, 0, sizeof(fake)); 784 fake.pw_name = nouser; 785 fake.pw_passwd = __UNCONST( 786 "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK"); 787 fake.pw_gecos = nouser; 788 fake.pw_uid = (uid_t)-1; 789 fake.pw_gid = (gid_t)-1; 790 fake.pw_class = __UNCONST(""); 791 fake.pw_dir = nonexist; 792 fake.pw_shell = nonexist; 793 794 return (&fake); 795 } 796 #endif 797 798 /* 799 * Returns the remote DNS hostname as a string. The returned string must not 800 * be freed. NB. this will usually trigger a DNS query the first time it is 801 * called. 802 * This function does additional checks on the hostname to mitigate some 803 * attacks on legacy rhosts-style authentication. 804 * XXX is RhostsRSAAuthentication vulnerable to these? 805 * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?) 806 */ 807 808 static char * 809 remote_hostname(struct ssh *ssh) 810 { 811 struct sockaddr_storage from; 812 socklen_t fromlen; 813 struct addrinfo hints, *ai, *aitop; 814 char name[NI_MAXHOST], ntop2[NI_MAXHOST]; 815 const char *ntop = ssh_remote_ipaddr(ssh); 816 817 /* Get IP address of client. */ 818 fromlen = sizeof(from); 819 memset(&from, 0, sizeof(from)); 820 if (getpeername(ssh_packet_get_connection_in(ssh), 821 (struct sockaddr *)&from, &fromlen) < 0) { 822 debug("getpeername failed: %.100s", strerror(errno)); 823 return strdup(ntop); 824 } 825 826 debug3("Trying to reverse map address %.100s.", ntop); 827 /* Map the IP address to a host name. */ 828 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), 829 NULL, 0, NI_NAMEREQD) != 0) { 830 /* Host name not found. Use ip address. */ 831 return strdup(ntop); 832 } 833 834 /* 835 * if reverse lookup result looks like a numeric hostname, 836 * someone is trying to trick us by PTR record like following: 837 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5 838 */ 839 memset(&hints, 0, sizeof(hints)); 840 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 841 hints.ai_flags = AI_NUMERICHOST; 842 if (getaddrinfo(name, NULL, &hints, &ai) == 0) { 843 logit("Nasty PTR record \"%s\" is set up for %s, ignoring", 844 name, ntop); 845 freeaddrinfo(ai); 846 return strdup(ntop); 847 } 848 849 /* Names are stored in lowercase. */ 850 lowercase(name); 851 852 /* 853 * Map it back to an IP address and check that the given 854 * address actually is an address of this host. This is 855 * necessary because anyone with access to a name server can 856 * define arbitrary names for an IP address. Mapping from 857 * name to IP address can be trusted better (but can still be 858 * fooled if the intruder has access to the name server of 859 * the domain). 860 */ 861 memset(&hints, 0, sizeof(hints)); 862 hints.ai_family = from.ss_family; 863 hints.ai_socktype = SOCK_STREAM; 864 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) { 865 logit("reverse mapping checking getaddrinfo for %.700s " 866 "[%s] failed.", name, ntop); 867 return strdup(ntop); 868 } 869 /* Look for the address from the list of addresses. */ 870 for (ai = aitop; ai; ai = ai->ai_next) { 871 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2, 872 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 && 873 (strcmp(ntop, ntop2) == 0)) 874 break; 875 } 876 freeaddrinfo(aitop); 877 /* If we reached the end of the list, the address was not there. */ 878 if (ai == NULL) { 879 /* Address not found for the host name. */ 880 logit("Address %.100s maps to %.600s, but this does not " 881 "map back to the address.", ntop, name); 882 return strdup(ntop); 883 } 884 return strdup(name); 885 } 886 887 /* 888 * Return the canonical name of the host in the other side of the current 889 * connection. The host name is cached, so it is efficient to call this 890 * several times. 891 */ 892 893 const char * 894 auth_get_canonical_hostname(struct ssh *ssh, int use_dns) 895 { 896 static char *dnsname; 897 898 if (!use_dns) 899 return ssh_remote_ipaddr(ssh); 900 else if (dnsname != NULL) 901 return dnsname; 902 else { 903 dnsname = remote_hostname(ssh); 904 return dnsname; 905 } 906 } 907