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