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