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