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