1 /* 2 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. 3 * Copyright (c) 1983, 1993, 1994 4 * The Regents of the University of California. 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 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * This product includes software developed by Theo de Raadt. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #if defined(LIBC_SCCS) && !defined(lint) 37 static char *rcsid = "$OpenBSD: rcmd.c,v 1.39 2001/09/04 23:35:58 millert Exp $"; 38 #endif /* LIBC_SCCS and not lint */ 39 40 #include <sys/param.h> 41 #include <sys/socket.h> 42 #include <sys/stat.h> 43 44 #include <netinet/in.h> 45 #include <arpa/inet.h> 46 47 #include <signal.h> 48 #include <fcntl.h> 49 #include <netdb.h> 50 #include <unistd.h> 51 #include <pwd.h> 52 #include <errno.h> 53 #include <stdio.h> 54 #include <ctype.h> 55 #include <string.h> 56 #include <syslog.h> 57 #include <stdlib.h> 58 #include <netgroup.h> 59 60 int __ivaliduser __P((FILE *, in_addr_t, const char *, const char *)); 61 int __ivaliduser_sa __P((FILE *, struct sockaddr *, socklen_t, 62 const char *, const char *)); 63 static int __icheckhost __P((struct sockaddr *, socklen_t, const char *)); 64 static char *__gethostloop __P((struct sockaddr *, socklen_t)); 65 66 int 67 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 68 char **ahost; 69 in_port_t rport; 70 const char *locuser, *remuser, *cmd; 71 int *fd2p; 72 { 73 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 74 } 75 76 int 77 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 78 char **ahost; 79 in_port_t rport; 80 const char *locuser, *remuser, *cmd; 81 int *fd2p; 82 int af; 83 { 84 static char hbuf[MAXHOSTNAMELEN]; 85 char pbuf[NI_MAXSERV]; 86 struct addrinfo hints, *res, *r; 87 int error; 88 struct sockaddr_storage from; 89 fd_set *readsp = NULL; 90 sigset_t oldmask, mask; 91 pid_t pid; 92 int s, lport, timo; 93 char c, *p; 94 int refused; 95 96 /* call rcmdsh() with specified remote shell if appropriate. */ 97 if (!issetugid() && (p = getenv("RSH")) && *p) { 98 struct servent *sp = getservbyname("shell", "tcp"); 99 100 if (sp && sp->s_port == rport) 101 return (rcmdsh(ahost, rport, locuser, remuser, 102 cmd, p)); 103 } 104 105 /* use rsh(1) if non-root and remote port is shell. */ 106 if (geteuid()) { 107 struct servent *sp = getservbyname("shell", "tcp"); 108 109 if (sp && sp->s_port == rport) 110 return (rcmdsh(ahost, rport, locuser, remuser, 111 cmd, NULL)); 112 } 113 114 pid = getpid(); 115 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 116 memset(&hints, 0, sizeof(hints)); 117 hints.ai_family = af; 118 hints.ai_socktype = SOCK_STREAM; 119 hints.ai_flags = AI_CANONNAME; 120 error = getaddrinfo(*ahost, pbuf, &hints, &res); 121 if (error) { 122 #if 0 123 warnx("%s: %s", *ahost, gai_strerror(error)); 124 #endif 125 return (-1); 126 } 127 if (res->ai_canonname) { 128 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); 129 *ahost = hbuf; 130 } else 131 ; /*XXX*/ 132 133 r = res; 134 refused = 0; 135 sigemptyset(&mask); 136 sigaddset(&mask, SIGURG); 137 oldmask = sigprocmask(SIG_BLOCK, &mask, &oldmask); 138 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 139 s = rresvport_af(&lport, r->ai_family); 140 if (s < 0) { 141 if (errno == EAGAIN) 142 (void)fprintf(stderr, 143 "rcmd: socket: All ports in use\n"); 144 else 145 (void)fprintf(stderr, "rcmd: socket: %s\n", 146 strerror(errno)); 147 if (r->ai_next) { 148 r = r->ai_next; 149 continue; 150 } else { 151 sigprocmask(SIG_SETMASK, &oldmask, NULL); 152 freeaddrinfo(res); 153 return (-1); 154 } 155 } 156 fcntl(s, F_SETOWN, pid); 157 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) 158 break; 159 (void)close(s); 160 if (errno == EADDRINUSE) { 161 lport--; 162 continue; 163 } 164 if (errno == ECONNREFUSED) 165 refused++; 166 if (r->ai_next) { 167 int oerrno = errno; 168 char hbuf[NI_MAXHOST]; 169 #ifdef NI_WITHSCOPEID 170 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 171 #else 172 const int niflags = NI_NUMERICHOST; 173 #endif 174 175 hbuf[0] = '\0'; 176 if (getnameinfo(r->ai_addr, r->ai_addrlen, 177 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 178 strcpy(hbuf, "(invalid)"); 179 (void)fprintf(stderr, "connect to address %s: ", hbuf); 180 errno = oerrno; 181 perror(0); 182 r = r->ai_next; 183 hbuf[0] = '\0'; 184 if (getnameinfo(r->ai_addr, r->ai_addrlen, 185 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 186 strcpy(hbuf, "(invalid)"); 187 (void)fprintf(stderr, "Trying %s...\n", hbuf); 188 continue; 189 } 190 if (refused && timo <= 16) { 191 (void)sleep(timo); 192 timo *= 2; 193 r = res; 194 refused = 0; 195 continue; 196 } 197 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, 198 strerror(errno)); 199 sigprocmask(SIG_SETMASK, &oldmask, NULL); 200 freeaddrinfo(res); 201 return (-1); 202 } 203 /* given "af" can be PF_UNSPEC, we need the real af for "s" */ 204 af = r->ai_family; 205 freeaddrinfo(res); 206 #if 0 207 /* 208 * try to rresvport() to the same port. This will make rresvport() 209 * fail it's first bind, resulting in it choosing a random port. 210 */ 211 lport--; 212 #endif 213 if (fd2p == 0) { 214 write(s, "", 1); 215 lport = 0; 216 } else { 217 char num[8]; 218 int s2 = rresvport_af(&lport, af), s3; 219 int len = sizeof(from); 220 int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask); 221 222 if (s2 < 0) 223 goto bad; 224 readsp = (fd_set *)malloc(fdssize); 225 if (readsp == NULL) 226 goto bad; 227 listen(s2, 1); 228 (void)snprintf(num, sizeof(num), "%d", lport); 229 if (write(s, num, strlen(num)+1) != strlen(num)+1) { 230 (void)fprintf(stderr, 231 "rcmd: write (setting up stderr): %s\n", 232 strerror(errno)); 233 (void)close(s2); 234 goto bad; 235 } 236 again: 237 bzero(readsp, fdssize); 238 FD_SET(s, readsp); 239 FD_SET(s2, readsp); 240 errno = 0; 241 if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 || 242 !FD_ISSET(s2, readsp)) { 243 if (errno != 0) 244 (void)fprintf(stderr, 245 "rcmd: select (setting up stderr): %s\n", 246 strerror(errno)); 247 else 248 (void)fprintf(stderr, 249 "select: protocol failure in circuit setup\n"); 250 (void)close(s2); 251 goto bad; 252 } 253 s3 = accept(s2, (struct sockaddr *)&from, &len); 254 /* 255 * XXX careful for ftp bounce attacks. If discovered, shut them 256 * down and check for the real auxiliary channel to connect. 257 */ 258 switch (from.ss_family) { 259 case AF_INET: 260 case AF_INET6: 261 if (getnameinfo((struct sockaddr *)&from, len, 262 NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 && 263 atoi(num) != 20) { 264 break; 265 } 266 close(s3); 267 goto again; 268 default: 269 break; 270 } 271 (void)close(s2); 272 if (s3 < 0) { 273 (void)fprintf(stderr, 274 "rcmd: accept: %s\n", strerror(errno)); 275 lport = 0; 276 goto bad; 277 } 278 *fd2p = s3; 279 switch (from.ss_family) { 280 case AF_INET: 281 case AF_INET6: 282 if (getnameinfo((struct sockaddr *)&from, len, 283 NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || 284 (atoi(num) >= IPPORT_RESERVED || 285 atoi(num) < IPPORT_RESERVED / 2)) { 286 (void)fprintf(stderr, 287 "socket: protocol failure in circuit setup.\n"); 288 goto bad2; 289 } 290 break; 291 default: 292 break; 293 } 294 } 295 (void)write(s, locuser, strlen(locuser)+1); 296 (void)write(s, remuser, strlen(remuser)+1); 297 (void)write(s, cmd, strlen(cmd)+1); 298 if (read(s, &c, 1) != 1) { 299 (void)fprintf(stderr, 300 "rcmd: %s: %s\n", *ahost, strerror(errno)); 301 goto bad2; 302 } 303 if (c != 0) { 304 while (read(s, &c, 1) == 1) { 305 (void)write(STDERR_FILENO, &c, 1); 306 if (c == '\n') 307 break; 308 } 309 goto bad2; 310 } 311 sigprocmask(SIG_SETMASK, &oldmask, NULL); 312 free(readsp); 313 return (s); 314 bad2: 315 if (lport) 316 (void)close(*fd2p); 317 bad: 318 if (readsp) 319 free(readsp); 320 (void)close(s); 321 sigprocmask(SIG_SETMASK, &oldmask, NULL); 322 return (-1); 323 } 324 325 int __check_rhosts_file = 1; 326 char *__rcmd_errstr; 327 328 int 329 ruserok(rhost, superuser, ruser, luser) 330 const char *rhost, *ruser, *luser; 331 int superuser; 332 { 333 struct addrinfo hints, *res, *r; 334 int error; 335 336 memset(&hints, 0, sizeof(hints)); 337 hints.ai_family = PF_UNSPEC; 338 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 339 error = getaddrinfo(rhost, "0", &hints, &res); 340 if (error) 341 return (-1); 342 343 for (r = res; r; r = r->ai_next) { 344 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 345 luser) == 0) { 346 freeaddrinfo(res); 347 return (0); 348 } 349 } 350 freeaddrinfo(res); 351 return (-1); 352 } 353 354 /* 355 * New .rhosts strategy: We are passed an ip address. We spin through 356 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 357 * has ip addresses, we don't have to trust a nameserver. When it 358 * contains hostnames, we spin through the list of addresses the nameserver 359 * gives us and look for a match. 360 * 361 * Returns 0 if ok, -1 if not ok. 362 */ 363 int 364 iruserok(raddr, superuser, ruser, luser) 365 u_int32_t raddr; 366 int superuser; 367 const char *ruser, *luser; 368 { 369 struct sockaddr_in sin; 370 371 memset(&sin, 0, sizeof(sin)); 372 sin.sin_family = AF_INET; 373 sin.sin_len = sizeof(struct sockaddr_in); 374 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 375 return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, 376 luser); 377 } 378 379 int 380 iruserok_sa(raddr, rlen, superuser, ruser, luser) 381 const void *raddr; 382 int rlen; 383 int superuser; 384 const char *ruser, *luser; 385 { 386 struct sockaddr *sa; 387 register char *cp; 388 struct stat sbuf; 389 struct passwd *pwd; 390 FILE *hostf; 391 uid_t uid; 392 int first; 393 char pbuf[MAXPATHLEN]; 394 395 sa = (struct sockaddr *)raddr; 396 first = 1; 397 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 398 again: 399 if (hostf) { 400 if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { 401 (void)fclose(hostf); 402 return (0); 403 } 404 (void)fclose(hostf); 405 } 406 if (first == 1 && (__check_rhosts_file || superuser)) { 407 first = 0; 408 if ((pwd = getpwnam(luser)) == NULL) 409 return (-1); 410 (void)strcpy(pbuf, pwd->pw_dir); 411 (void)strcat(pbuf, "/.rhosts"); 412 413 /* 414 * Change effective uid while opening .rhosts. If root and 415 * reading an NFS mounted file system, can't read files that 416 * are protected read/write owner only. 417 */ 418 uid = geteuid(); 419 (void)seteuid(pwd->pw_uid); 420 hostf = fopen(pbuf, "r"); 421 (void)seteuid(uid); 422 423 if (hostf == NULL) 424 return (-1); 425 /* 426 * If not a regular file, or is owned by someone other than 427 * user or root or if writeable by anyone but the owner, quit. 428 */ 429 cp = NULL; 430 if (lstat(pbuf, &sbuf) < 0) 431 cp = ".rhosts lstat failed"; 432 else if (!S_ISREG(sbuf.st_mode)) 433 cp = ".rhosts not regular file"; 434 else if (fstat(fileno(hostf), &sbuf) < 0) 435 cp = ".rhosts fstat failed"; 436 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 437 cp = "bad .rhosts owner"; 438 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 439 cp = ".rhosts writeable by other than owner"; 440 /* If there were any problems, quit. */ 441 if (cp) { 442 __rcmd_errstr = cp; 443 (void)fclose(hostf); 444 return (-1); 445 } 446 goto again; 447 } 448 return (-1); 449 } 450 451 /* 452 * XXX 453 * Don't make static, used by lpd(8). 454 * 455 * Returns 0 if ok, -1 if not ok. 456 */ 457 int 458 __ivaliduser(hostf, raddrl, luser, ruser) 459 FILE *hostf; 460 in_addr_t raddrl; 461 const char *luser, *ruser; 462 { 463 struct sockaddr_in sin; 464 465 memset(&sin, 0, sizeof(sin)); 466 sin.sin_family = AF_INET; 467 sin.sin_len = sizeof(struct sockaddr_in); 468 memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr)); 469 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 470 luser, ruser); 471 } 472 473 int 474 __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 475 FILE *hostf; 476 struct sockaddr *raddr; 477 socklen_t salen; 478 const char *luser, *ruser; 479 { 480 register char *user, *p; 481 char *buf; 482 const char *auser, *ahost; 483 int hostok, userok; 484 char *rhost = (char *)-1; 485 char domain[MAXHOSTNAMELEN]; 486 size_t buflen; 487 488 getdomainname(domain, sizeof(domain)); 489 490 while ((buf = fgetln(hostf, &buflen))) { 491 p = buf; 492 if (*p == '#') 493 continue; 494 while (*p != '\n' && *p != ' ' && *p != '\t' && p < buf + buflen) { 495 if (!isprint(*p)) 496 goto bail; 497 *p = isupper(*p) ? tolower(*p) : *p; 498 p++; 499 } 500 if (p >= buf + buflen) 501 continue; 502 if (*p == ' ' || *p == '\t') { 503 *p++ = '\0'; 504 while ((*p == ' ' || *p == '\t') && p < buf + buflen) 505 p++; 506 if (p >= buf + buflen) 507 continue; 508 user = p; 509 while (*p != '\n' && *p != ' ' && 510 *p != '\t' && p < buf + buflen) { 511 if (!isprint(*p)) 512 goto bail; 513 p++; 514 } 515 } else 516 user = p; 517 *p = '\0'; 518 519 if (p == buf) 520 continue; 521 522 auser = *user ? user : luser; 523 ahost = buf; 524 525 if (strlen(ahost) >= MAXHOSTNAMELEN) 526 continue; 527 528 /* 529 * innetgr() must lookup a hostname (we do not attempt 530 * to change the semantics so that netgroups may have 531 * #.#.#.# addresses in the list.) 532 */ 533 if (ahost[0] == '+') 534 switch (ahost[1]) { 535 case '\0': 536 hostok = 1; 537 break; 538 case '@': 539 if (rhost == (char *)-1) 540 rhost = __gethostloop(raddr, salen); 541 hostok = 0; 542 if (rhost) 543 hostok = innetgr(&ahost[2], rhost, 544 NULL, domain); 545 break; 546 default: 547 hostok = __icheckhost(raddr, salen, &ahost[1]); 548 break; 549 } 550 else if (ahost[0] == '-') 551 switch (ahost[1]) { 552 case '\0': 553 hostok = -1; 554 break; 555 case '@': 556 if (rhost == (char *)-1) 557 rhost = __gethostloop(raddr, salen); 558 hostok = 0; 559 if (rhost) 560 hostok = -innetgr(&ahost[2], rhost, 561 NULL, domain); 562 break; 563 default: 564 hostok = -__icheckhost(raddr, salen, &ahost[1]); 565 break; 566 } 567 else 568 hostok = __icheckhost(raddr, salen, ahost); 569 570 571 if (auser[0] == '+') 572 switch (auser[1]) { 573 case '\0': 574 userok = 1; 575 break; 576 case '@': 577 userok = innetgr(&auser[2], NULL, ruser, 578 domain); 579 break; 580 default: 581 userok = strcmp(ruser, &auser[1]) ? 0 : 1; 582 break; 583 } 584 else if (auser[0] == '-') 585 switch (auser[1]) { 586 case '\0': 587 userok = -1; 588 break; 589 case '@': 590 userok = -innetgr(&auser[2], NULL, ruser, 591 domain); 592 break; 593 default: 594 userok = strcmp(ruser, &auser[1]) ? 0 : -1; 595 break; 596 } 597 else 598 userok = strcmp(ruser, auser) ? 0 : 1; 599 600 /* Check if one component did not match */ 601 if (hostok == 0 || userok == 0) 602 continue; 603 604 /* Check if we got a forbidden pair */ 605 if (userok <= -1 || hostok <= -1) 606 return (-1); 607 608 /* Check if we got a valid pair */ 609 if (hostok >= 1 && userok >= 1) 610 return (0); 611 } 612 bail: 613 return (-1); 614 } 615 616 /* 617 * Returns "true" if match, 0 if no match. If we do not find any 618 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. 619 * 620 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 621 * if af == AF_INET6. 622 */ 623 static int 624 __icheckhost(raddr, salen, lhost) 625 struct sockaddr *raddr; 626 socklen_t salen; 627 const char *lhost; 628 { 629 struct addrinfo hints, *res, *r; 630 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 631 int error; 632 #ifdef NI_WITHSCOPEID 633 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 634 #else 635 const int niflags = NI_NUMERICHOST; 636 #endif 637 638 h1[0] = '\0'; 639 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 640 niflags) != 0) 641 return (0); 642 643 /* Resolve laddr into sockaddr */ 644 memset(&hints, 0, sizeof(hints)); 645 hints.ai_family = raddr->sa_family; 646 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 647 res = NULL; 648 error = getaddrinfo(lhost, "0", &hints, &res); 649 if (error) 650 return (0); 651 652 /* 653 * Try string comparisons between raddr and laddr. 654 */ 655 for (r = res; r; r = r->ai_next) { 656 h2[0] = '\0'; 657 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 658 NULL, 0, niflags) != 0) 659 continue; 660 if (strcmp(h1, h2) == 0) { 661 freeaddrinfo(res); 662 return (1); 663 } 664 } 665 666 /* No match. */ 667 freeaddrinfo(res); 668 return (0); 669 } 670 671 /* 672 * Return the hostname associated with the supplied address. 673 * Do a reverse lookup as well for security. If a loop cannot 674 * be found, pack the result of inet_ntoa() into the string. 675 * 676 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 677 * if af == AF_INET6. 678 */ 679 static char * 680 __gethostloop(raddr, salen) 681 struct sockaddr *raddr; 682 socklen_t salen; 683 { 684 static char remotehost[NI_MAXHOST]; 685 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 686 struct addrinfo hints, *res, *r; 687 int error; 688 #ifdef NI_WITHSCOPEID 689 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 690 #else 691 const int niflags = NI_NUMERICHOST; 692 #endif 693 694 h1[0] = remotehost[0] = '\0'; 695 if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), 696 NULL, 0, NI_NAMEREQD) != 0) 697 return (NULL); 698 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 699 niflags) != 0) 700 return (NULL); 701 702 /* 703 * Look up the name and check that the supplied 704 * address is in the list 705 */ 706 memset(&hints, 0, sizeof(hints)); 707 hints.ai_family = raddr->sa_family; 708 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 709 hints.ai_flags = AI_CANONNAME; 710 res = NULL; 711 error = getaddrinfo(remotehost, "0", &hints, &res); 712 if (error) 713 return (NULL); 714 715 for (r = res; r; r = r->ai_next) { 716 h2[0] = '\0'; 717 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 718 NULL, 0, niflags) != 0) 719 continue; 720 if (strcmp(h1, h2) == 0) { 721 freeaddrinfo(res); 722 return (remotehost); 723 } 724 } 725 726 /* 727 * either the DNS adminstrator has made a configuration 728 * mistake, or someone has attempted to spoof us 729 */ 730 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 731 h1, res->ai_canonname ? res->ai_canonname : remotehost); 732 freeaddrinfo(res); 733 return (NULL); 734 } 735