17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5004388ebScasper * Common Development and Distribution License (the "License"). 6004388ebScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*7257d1b4Sraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <limits.h> 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <ctype.h> 457c478bd9Sstevel@tonic-gate #include <pwd.h> 467c478bd9Sstevel@tonic-gate #include <sys/types.h> 477c478bd9Sstevel@tonic-gate #include <sys/param.h> 487c478bd9Sstevel@tonic-gate #include <sys/file.h> 497c478bd9Sstevel@tonic-gate #include <signal.h> 507c478bd9Sstevel@tonic-gate #include <libintl.h> 517c478bd9Sstevel@tonic-gate #include <sys/socket.h> 527c478bd9Sstevel@tonic-gate #include <sys/stat.h> 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #include <netinet/in.h> 557c478bd9Sstevel@tonic-gate #include <netinet/tcp.h> 567c478bd9Sstevel@tonic-gate #include <inet/common.h> 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #include <netdb.h> 597c478bd9Sstevel@tonic-gate #include <errno.h> 607c478bd9Sstevel@tonic-gate #include <fcntl.h> 617c478bd9Sstevel@tonic-gate #include <unistd.h> 627c478bd9Sstevel@tonic-gate #include <string.h> 637c478bd9Sstevel@tonic-gate #include <stdlib.h> 647c478bd9Sstevel@tonic-gate #include <grp.h> 657c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #include <priv_utils.h> 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #ifdef SYSV 707c478bd9Sstevel@tonic-gate #define bcopy(s1, s2, len) (void) memcpy(s2, s1, len) 717c478bd9Sstevel@tonic-gate #define bzero(s, len) (void) memset(s, 0, len) 727c478bd9Sstevel@tonic-gate #define index(s, c) strchr(s, c) 737c478bd9Sstevel@tonic-gate char *strchr(); 747c478bd9Sstevel@tonic-gate #else 757c478bd9Sstevel@tonic-gate char *index(); 767c478bd9Sstevel@tonic-gate #endif /* SYSV */ 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate extern int usingypmap(); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate static int _validuser(FILE *hostf, char *rhost, const char *luser, 817c478bd9Sstevel@tonic-gate const char *ruser, int baselen); 827c478bd9Sstevel@tonic-gate static int _checkhost(char *rhost, char *lhost, int len); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #ifdef NIS 867c478bd9Sstevel@tonic-gate static char *domain; 877c478bd9Sstevel@tonic-gate #endif 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate int rcmd(char **ahost, unsigned short rport, const char *locuser, 907c478bd9Sstevel@tonic-gate const char *remuser, const char *cmd, int *fd2p) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate int rcmd_ret; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate rcmd_ret = rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, 957c478bd9Sstevel@tonic-gate AF_INET); 967c478bd9Sstevel@tonic-gate return (rcmd_ret); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate int rcmd_af(char **ahost, unsigned short rport, const char *locuser, 1007c478bd9Sstevel@tonic-gate const char *remuser, const char *cmd, int *fd2p, int af) 1017c478bd9Sstevel@tonic-gate { 1027c478bd9Sstevel@tonic-gate int s, timo = 1; 1037c478bd9Sstevel@tonic-gate ssize_t retval; 1047c478bd9Sstevel@tonic-gate pid_t pid; 1057c478bd9Sstevel@tonic-gate struct sockaddr_storage caddr, faddr; 1067c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 1077c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 1087c478bd9Sstevel@tonic-gate struct addrinfo hints; 1097c478bd9Sstevel@tonic-gate struct addrinfo *res, *resp; 1107c478bd9Sstevel@tonic-gate size_t addrlen; 1117c478bd9Sstevel@tonic-gate int rc; 1127c478bd9Sstevel@tonic-gate #define MAX_SHORTSTRLEN 6 1137c478bd9Sstevel@tonic-gate char aport[MAX_SHORTSTRLEN]; 1147c478bd9Sstevel@tonic-gate char c; 1157c478bd9Sstevel@tonic-gate int lport = 0; 1167c478bd9Sstevel@tonic-gate #ifdef SYSV 1177c478bd9Sstevel@tonic-gate sigset_t oldmask; 1187c478bd9Sstevel@tonic-gate sigset_t newmask; 1197c478bd9Sstevel@tonic-gate struct sigaction oldaction; 1207c478bd9Sstevel@tonic-gate struct sigaction newaction; 1217c478bd9Sstevel@tonic-gate #else 1227c478bd9Sstevel@tonic-gate int oldmask; 1237c478bd9Sstevel@tonic-gate #endif /* SYSV */ 1247c478bd9Sstevel@tonic-gate fd_set fdset; 1257c478bd9Sstevel@tonic-gate int selret; 1267c478bd9Sstevel@tonic-gate char *addr; 1277c478bd9Sstevel@tonic-gate static char hostname[MAXHOSTNAMELEN]; 1287c478bd9Sstevel@tonic-gate socklen_t len; 1297c478bd9Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) { 1327c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT; 1337c478bd9Sstevel@tonic-gate return (-1); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate pid = getpid(); 1377c478bd9Sstevel@tonic-gate memset(&hints, 0, sizeof (hints)); 1387c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM; 1397c478bd9Sstevel@tonic-gate hints.ai_flags = AI_CANONNAME; 1407c478bd9Sstevel@tonic-gate if (af == AF_INET6) { 1417c478bd9Sstevel@tonic-gate hints.ai_flags |= AI_V4MAPPED; 1427c478bd9Sstevel@tonic-gate hints.ai_family = AF_UNSPEC; 1437c478bd9Sstevel@tonic-gate } else { 1447c478bd9Sstevel@tonic-gate hints.ai_family = af; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport)); 1477c478bd9Sstevel@tonic-gate rc = getaddrinfo(*ahost, aport, &hints, &res); 1487c478bd9Sstevel@tonic-gate if (rc != 0) { 1497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 150*7257d1b4Sraf dgettext(TEXT_DOMAIN, "%s: unknown host%s\n"), 1517c478bd9Sstevel@tonic-gate *ahost, rc == EAI_AGAIN ? " (try again later)" : ""); 1527c478bd9Sstevel@tonic-gate return (-1); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate resp = res; 1557c478bd9Sstevel@tonic-gate (void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN); 1567c478bd9Sstevel@tonic-gate *ahost = hostname; 1577c478bd9Sstevel@tonic-gate #ifdef SYSV 1587c478bd9Sstevel@tonic-gate /* ignore SIGPIPE */ 1597c478bd9Sstevel@tonic-gate bzero((char *)&newaction, sizeof (newaction)); 1607c478bd9Sstevel@tonic-gate newaction.sa_handler = SIG_IGN; 161*7257d1b4Sraf (void) sigaction(SIGPIPE, &newaction, &oldaction); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* block SIGURG */ 1647c478bd9Sstevel@tonic-gate bzero((char *)&newmask, sizeof (newmask)); 165*7257d1b4Sraf (void) sigaddset(&newmask, SIGURG); 166*7257d1b4Sraf (void) sigprocmask(SIG_BLOCK, &newmask, &oldmask); 1677c478bd9Sstevel@tonic-gate #else 1687c478bd9Sstevel@tonic-gate oldmask = _sigblock(sigmask(SIGURG)); 1697c478bd9Sstevel@tonic-gate #endif /* SYSV */ 1707c478bd9Sstevel@tonic-gate for (;;) { 1717c478bd9Sstevel@tonic-gate s = rresvport_af(&lport, res->ai_family); 1727c478bd9Sstevel@tonic-gate if (s < 0) { 1737c478bd9Sstevel@tonic-gate int af = res->ai_family; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * See if we have any addresses of a different type 1777c478bd9Sstevel@tonic-gate * to try. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate while (res != NULL && res->ai_family == af) 1807c478bd9Sstevel@tonic-gate res = res->ai_next; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (res != NULL) 1837c478bd9Sstevel@tonic-gate continue; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 1867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 187*7257d1b4Sraf dgettext(TEXT_DOMAIN, 1887c478bd9Sstevel@tonic-gate "socket: All ports in use\n")); 1897c478bd9Sstevel@tonic-gate else 1907c478bd9Sstevel@tonic-gate perror("rcmd: socket"); 1917c478bd9Sstevel@tonic-gate #ifdef SYSV 1927c478bd9Sstevel@tonic-gate /* restore original SIGPIPE handler */ 193*7257d1b4Sraf (void) sigaction(SIGPIPE, &oldaction, 1947c478bd9Sstevel@tonic-gate (struct sigaction *)0); 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* restore original signal mask */ 197*7257d1b4Sraf (void) sigprocmask(SIG_SETMASK, &oldmask, 1987c478bd9Sstevel@tonic-gate (sigset_t *)0); 1997c478bd9Sstevel@tonic-gate #else 2007c478bd9Sstevel@tonic-gate sigsetmask(oldmask); 2017c478bd9Sstevel@tonic-gate #endif /* SYSV */ 2027c478bd9Sstevel@tonic-gate freeaddrinfo(resp); 2037c478bd9Sstevel@tonic-gate return (-1); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate bzero((char *)&caddr, sizeof (caddr)); 2067c478bd9Sstevel@tonic-gate bcopy(res->ai_addr, &caddr, res->ai_addrlen); 2077c478bd9Sstevel@tonic-gate addrlen = res->ai_addrlen; 2087c478bd9Sstevel@tonic-gate if (af == AF_INET6 && res->ai_addr->sa_family == AF_INET) { 2097c478bd9Sstevel@tonic-gate struct in6_addr ia6; 2107c478bd9Sstevel@tonic-gate struct sockaddr_in6 *in6addr; 2117c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(&((struct sockaddr_in *) 2127c478bd9Sstevel@tonic-gate res->ai_addr)->sin_addr, &ia6); 2137c478bd9Sstevel@tonic-gate in6addr = (struct sockaddr_in6 *)&caddr; 2147c478bd9Sstevel@tonic-gate in6addr->sin6_addr = ia6; 2157c478bd9Sstevel@tonic-gate in6addr->sin6_family = AF_INET6; 2167c478bd9Sstevel@tonic-gate addrlen = sizeof (struct sockaddr_in6); 2177c478bd9Sstevel@tonic-gate } 218*7257d1b4Sraf (void) fcntl(s, F_SETOWN, pid); 2197c478bd9Sstevel@tonic-gate if (connect(s, (struct sockaddr *)&caddr, addrlen) >= 0) 2207c478bd9Sstevel@tonic-gate break; 2217c478bd9Sstevel@tonic-gate (void) close(s); 2227c478bd9Sstevel@tonic-gate if (errno == EADDRINUSE) { 2237c478bd9Sstevel@tonic-gate lport = 0; 2247c478bd9Sstevel@tonic-gate continue; 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate if (errno == ECONNREFUSED && timo <= 16) { 2277c478bd9Sstevel@tonic-gate (void) sleep(timo); 2287c478bd9Sstevel@tonic-gate timo *= 2; 2297c478bd9Sstevel@tonic-gate continue; 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate if (res->ai_next != NULL) { 2327c478bd9Sstevel@tonic-gate int oerrno = errno; 2337c478bd9Sstevel@tonic-gate if (res->ai_addr->sa_family == AF_INET6) 2347c478bd9Sstevel@tonic-gate addr = (char *)&((struct sockaddr_in6 *) 2357c478bd9Sstevel@tonic-gate res->ai_addr)->sin6_addr; 2367c478bd9Sstevel@tonic-gate else 2377c478bd9Sstevel@tonic-gate addr = (char *)&((struct sockaddr_in *) 2387c478bd9Sstevel@tonic-gate res->ai_addr)->sin_addr; 2397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 240*7257d1b4Sraf dgettext(TEXT_DOMAIN, "connect to address %s: "), 2417c478bd9Sstevel@tonic-gate inet_ntop(res->ai_addr->sa_family, addr, 2427c478bd9Sstevel@tonic-gate abuf, sizeof (abuf))); 2437c478bd9Sstevel@tonic-gate errno = oerrno; 2447c478bd9Sstevel@tonic-gate perror(0); 2457c478bd9Sstevel@tonic-gate res = res->ai_next; 2467c478bd9Sstevel@tonic-gate if (res->ai_addr->sa_family == AF_INET6) 2477c478bd9Sstevel@tonic-gate addr = (char *)&((struct sockaddr_in6 *) 2487c478bd9Sstevel@tonic-gate res->ai_addr)->sin6_addr; 2497c478bd9Sstevel@tonic-gate else 2507c478bd9Sstevel@tonic-gate addr = (char *)&((struct sockaddr_in *) 2517c478bd9Sstevel@tonic-gate res->ai_addr)->sin_addr; 2527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 253*7257d1b4Sraf dgettext(TEXT_DOMAIN, "Trying %s...\n"), 2547c478bd9Sstevel@tonic-gate inet_ntop(res->ai_addr->sa_family, addr, 2557c478bd9Sstevel@tonic-gate abuf, sizeof (abuf))); 2567c478bd9Sstevel@tonic-gate continue; 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate perror(*ahost); 2597c478bd9Sstevel@tonic-gate freeaddrinfo(resp); 2607c478bd9Sstevel@tonic-gate #ifdef SYSV 2617c478bd9Sstevel@tonic-gate /* restore original SIGPIPE handler */ 262*7257d1b4Sraf (void) sigaction(SIGPIPE, &oldaction, 2637c478bd9Sstevel@tonic-gate (struct sigaction *)0); 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate /* restore original signal mask */ 266*7257d1b4Sraf (void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0); 2677c478bd9Sstevel@tonic-gate #else 2687c478bd9Sstevel@tonic-gate sigsetmask(oldmask); 2697c478bd9Sstevel@tonic-gate #endif /* SYSV */ 2707c478bd9Sstevel@tonic-gate return (-1); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate lport = 0; 2737c478bd9Sstevel@tonic-gate if (fd2p == 0) { 2747c478bd9Sstevel@tonic-gate (void) write(s, "", 1); 2757c478bd9Sstevel@tonic-gate } else { 2767c478bd9Sstevel@tonic-gate int s2 = rresvport_af(&lport, res->ai_family), s3; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate len = (socklen_t)sizeof (faddr); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate if (s2 < 0) 2817c478bd9Sstevel@tonic-gate goto bad; 2827c478bd9Sstevel@tonic-gate (void) listen(s2, 1); 2837c478bd9Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%d", lport); 2847c478bd9Sstevel@tonic-gate if (write(s, aport, strlen(aport)+1) != strlen(aport)+1) { 285*7257d1b4Sraf perror(dgettext(TEXT_DOMAIN, 2867c478bd9Sstevel@tonic-gate "write: setting up stderr")); 2877c478bd9Sstevel@tonic-gate (void) close(s2); 2887c478bd9Sstevel@tonic-gate goto bad; 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate FD_ZERO(&fdset); 2917c478bd9Sstevel@tonic-gate FD_SET(s, &fdset); 2927c478bd9Sstevel@tonic-gate FD_SET(s2, &fdset); 2937c478bd9Sstevel@tonic-gate while ((selret = select(FD_SETSIZE, &fdset, (fd_set *)0, 2947c478bd9Sstevel@tonic-gate (fd_set *)0, (struct timeval *)0)) > 0) { 2957c478bd9Sstevel@tonic-gate if (FD_ISSET(s, &fdset)) { 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * Something's wrong: we should get no 2987c478bd9Sstevel@tonic-gate * data on this connection at this point, 2997c478bd9Sstevel@tonic-gate * so we assume that the connection has 3007c478bd9Sstevel@tonic-gate * gone away. 3017c478bd9Sstevel@tonic-gate */ 3027c478bd9Sstevel@tonic-gate (void) close(s2); 3037c478bd9Sstevel@tonic-gate goto bad; 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate if (FD_ISSET(s2, &fdset)) { 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * We assume this is an incoming connect 3087c478bd9Sstevel@tonic-gate * request and proceed normally. 3097c478bd9Sstevel@tonic-gate */ 3107c478bd9Sstevel@tonic-gate s3 = accept(s2, (struct sockaddr *)&faddr, 3117c478bd9Sstevel@tonic-gate &len); 3127c478bd9Sstevel@tonic-gate FD_CLR(s2, &fdset); 3137c478bd9Sstevel@tonic-gate (void) close(s2); 3147c478bd9Sstevel@tonic-gate if (s3 < 0) { 3157c478bd9Sstevel@tonic-gate perror("accept"); 3167c478bd9Sstevel@tonic-gate lport = 0; 3177c478bd9Sstevel@tonic-gate goto bad; 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate else 3207c478bd9Sstevel@tonic-gate break; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate if (selret == -1) { 3247c478bd9Sstevel@tonic-gate /* 3257c478bd9Sstevel@tonic-gate * This should not happen, and we treat it as 3267c478bd9Sstevel@tonic-gate * a fatal error. 3277c478bd9Sstevel@tonic-gate */ 3287c478bd9Sstevel@tonic-gate (void) close(s2); 3297c478bd9Sstevel@tonic-gate goto bad; 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate *fd2p = s3; 3337c478bd9Sstevel@tonic-gate switch (faddr.ss_family) { 3347c478bd9Sstevel@tonic-gate case AF_INET: 3357c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&faddr; 3367c478bd9Sstevel@tonic-gate if (ntohs(sin->sin_port) >= IPPORT_RESERVED) { 3377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 338*7257d1b4Sraf dgettext(TEXT_DOMAIN, 3397c478bd9Sstevel@tonic-gate "socket: protocol failure in circuit " 3407c478bd9Sstevel@tonic-gate "setup.\n")); 3417c478bd9Sstevel@tonic-gate goto bad2; 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate break; 3447c478bd9Sstevel@tonic-gate case AF_INET6: 3457c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&faddr; 3467c478bd9Sstevel@tonic-gate if (ntohs(sin6->sin6_port) >= IPPORT_RESERVED) { 3477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 348*7257d1b4Sraf dgettext(TEXT_DOMAIN, 3497c478bd9Sstevel@tonic-gate "socket: protocol failure in circuit " 3507c478bd9Sstevel@tonic-gate "setup.\n")); 3517c478bd9Sstevel@tonic-gate goto bad2; 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate break; 3547c478bd9Sstevel@tonic-gate default: 3557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 356*7257d1b4Sraf dgettext(TEXT_DOMAIN, 3577c478bd9Sstevel@tonic-gate "socket: protocol failure in circuit setup.\n")); 3587c478bd9Sstevel@tonic-gate goto bad2; 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate (void) write(s, locuser, strlen(locuser)+1); 3627c478bd9Sstevel@tonic-gate (void) write(s, remuser, strlen(remuser)+1); 3637c478bd9Sstevel@tonic-gate (void) write(s, cmd, strlen(cmd)+1); 3647c478bd9Sstevel@tonic-gate retval = read(s, &c, 1); 3657c478bd9Sstevel@tonic-gate if (retval != 1) { 3667c478bd9Sstevel@tonic-gate if (retval == 0) { 3677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 368*7257d1b4Sraf dgettext(TEXT_DOMAIN, 3697c478bd9Sstevel@tonic-gate "Protocol error, %s closed connection\n"), 3707c478bd9Sstevel@tonic-gate *ahost); 3717c478bd9Sstevel@tonic-gate } else if (retval < 0) { 3727c478bd9Sstevel@tonic-gate perror(*ahost); 3737c478bd9Sstevel@tonic-gate } else { 3747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 375*7257d1b4Sraf dgettext(TEXT_DOMAIN, 3767c478bd9Sstevel@tonic-gate "Protocol error, %s sent %d bytes\n"), 3777c478bd9Sstevel@tonic-gate *ahost, retval); 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate goto bad2; 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate if (c != 0) { 3827c478bd9Sstevel@tonic-gate while (read(s, &c, 1) == 1) { 3837c478bd9Sstevel@tonic-gate (void) write(2, &c, 1); 3847c478bd9Sstevel@tonic-gate if (c == '\n') 3857c478bd9Sstevel@tonic-gate break; 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate goto bad2; 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate #ifdef SYSV 3907c478bd9Sstevel@tonic-gate /* restore original SIGPIPE handler */ 391*7257d1b4Sraf (void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0); 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate /* restore original signal mask */ 394*7257d1b4Sraf (void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0); 3957c478bd9Sstevel@tonic-gate #else 3967c478bd9Sstevel@tonic-gate sigsetmask(oldmask); 3977c478bd9Sstevel@tonic-gate #endif /* SYSV */ 3987c478bd9Sstevel@tonic-gate freeaddrinfo(resp); 3997c478bd9Sstevel@tonic-gate return (s); 4007c478bd9Sstevel@tonic-gate bad2: 4017c478bd9Sstevel@tonic-gate if (lport) 4027c478bd9Sstevel@tonic-gate (void) close(*fd2p); 4037c478bd9Sstevel@tonic-gate bad: 4047c478bd9Sstevel@tonic-gate (void) close(s); 4057c478bd9Sstevel@tonic-gate #ifdef SYSV 4067c478bd9Sstevel@tonic-gate /* restore original SIGPIPE handler */ 407*7257d1b4Sraf (void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0); 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /* restore original signal mask */ 410*7257d1b4Sraf (void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0); 4117c478bd9Sstevel@tonic-gate #else 4127c478bd9Sstevel@tonic-gate sigsetmask(oldmask); 4137c478bd9Sstevel@tonic-gate #endif /* SYSV */ 4147c478bd9Sstevel@tonic-gate freeaddrinfo(resp); 4157c478bd9Sstevel@tonic-gate return (-1); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate static int 4197c478bd9Sstevel@tonic-gate _rresvport_addr(int *alport, struct sockaddr_storage *addr) 4207c478bd9Sstevel@tonic-gate { 4217c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 4227c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 4237c478bd9Sstevel@tonic-gate int s; 4247c478bd9Sstevel@tonic-gate socklen_t len; 4257c478bd9Sstevel@tonic-gate int on = 1; 4267c478bd9Sstevel@tonic-gate int off = 0; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate if (addr->ss_family == AF_INET) { 4297c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)addr; 4307c478bd9Sstevel@tonic-gate len = sizeof (struct sockaddr_in); 4317c478bd9Sstevel@tonic-gate } else if (addr->ss_family == AF_INET6) { 4327c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)addr; 4337c478bd9Sstevel@tonic-gate len = sizeof (struct sockaddr_in6); 4347c478bd9Sstevel@tonic-gate } else { 4357c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT; 4367c478bd9Sstevel@tonic-gate return (-1); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate s = socket(addr->ss_family, SOCK_STREAM, 0); 4397c478bd9Sstevel@tonic-gate if (s < 0) 4407c478bd9Sstevel@tonic-gate return (-1); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate /* 443ae347574Skcpoon * Set SO_EXCLBIND to get a "unique" port, which is not bound 4447c478bd9Sstevel@tonic-gate * to any other sockets. 4457c478bd9Sstevel@tonic-gate */ 446ae347574Skcpoon if (setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &on, sizeof (on)) < 0) { 4477c478bd9Sstevel@tonic-gate (void) close(s); 4487c478bd9Sstevel@tonic-gate return (-1); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* Try to bind() to the given port first. */ 4527c478bd9Sstevel@tonic-gate if (*alport != 0) { 4537c478bd9Sstevel@tonic-gate if (addr->ss_family == AF_INET) { 4547c478bd9Sstevel@tonic-gate sin->sin_port = htons((ushort_t)*alport); 4557c478bd9Sstevel@tonic-gate } else { 4567c478bd9Sstevel@tonic-gate sin6->sin6_port = htons((ushort_t)*alport); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate if (bind(s, (struct sockaddr *)addr, len) >= 0) { 459ae347574Skcpoon /* To be safe, need to turn off SO_EXCLBIND. */ 460ae347574Skcpoon (void) setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &off, 4617c478bd9Sstevel@tonic-gate sizeof (off)); 4627c478bd9Sstevel@tonic-gate return (s); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate if (errno != EADDRINUSE) { 4657c478bd9Sstevel@tonic-gate (void) close(s); 4667c478bd9Sstevel@tonic-gate return (-1); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * If no port is given or the above bind() does not succeed, set 4727c478bd9Sstevel@tonic-gate * TCP_ANONPRIVBIND option to ask the kernel to pick a port in the 4737c478bd9Sstevel@tonic-gate * priviledged range for us. 4747c478bd9Sstevel@tonic-gate */ 4757c478bd9Sstevel@tonic-gate if (setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &on, 4767c478bd9Sstevel@tonic-gate sizeof (on)) < 0) { 4777c478bd9Sstevel@tonic-gate (void) close(s); 4787c478bd9Sstevel@tonic-gate return (-1); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate if (addr->ss_family == AF_INET) { 4817c478bd9Sstevel@tonic-gate sin->sin_port = 0; 4827c478bd9Sstevel@tonic-gate } else { 4837c478bd9Sstevel@tonic-gate sin6->sin6_port = 0; 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate if (bind(s, (struct sockaddr *)addr, len) >= 0) { 4867c478bd9Sstevel@tonic-gate /* 4877c478bd9Sstevel@tonic-gate * We need to tell the caller what the port is. 4887c478bd9Sstevel@tonic-gate */ 4897c478bd9Sstevel@tonic-gate if (getsockname(s, (struct sockaddr *)addr, &len) < 0) { 4907c478bd9Sstevel@tonic-gate (void) close(s); 4917c478bd9Sstevel@tonic-gate return (-1); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate switch (addr->ss_family) { 4947c478bd9Sstevel@tonic-gate case AF_INET6: 4957c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)addr; 4967c478bd9Sstevel@tonic-gate *alport = ntohs(sin6->sin6_port); 4977c478bd9Sstevel@tonic-gate break; 4987c478bd9Sstevel@tonic-gate case AF_INET: 4997c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)addr; 5007c478bd9Sstevel@tonic-gate *alport = ntohs(sin->sin_port); 5017c478bd9Sstevel@tonic-gate break; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate /* 5057c478bd9Sstevel@tonic-gate * To be safe, always turn off these options when we are done. 5067c478bd9Sstevel@tonic-gate */ 5077c478bd9Sstevel@tonic-gate (void) setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &off, 5087c478bd9Sstevel@tonic-gate sizeof (off)); 509ae347574Skcpoon (void) setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &off, 5107c478bd9Sstevel@tonic-gate sizeof (off)); 5117c478bd9Sstevel@tonic-gate return (s); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate (void) close(s); 5147c478bd9Sstevel@tonic-gate return (-1); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate int 5187c478bd9Sstevel@tonic-gate rresvport_addr(int *alport, struct sockaddr_storage *addr) 5197c478bd9Sstevel@tonic-gate { 5207c478bd9Sstevel@tonic-gate int res, err; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate (void) __priv_bracket(PRIV_ON); 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate res = _rresvport_addr(alport, addr); 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate err = errno; 5277c478bd9Sstevel@tonic-gate (void) __priv_bracket(PRIV_OFF); 5287c478bd9Sstevel@tonic-gate errno = err; 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate return (res); 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate int 5347c478bd9Sstevel@tonic-gate rresvport_af(int *alport, int af) 5357c478bd9Sstevel@tonic-gate { 5367c478bd9Sstevel@tonic-gate struct sockaddr_storage laddr; 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate bzero(&laddr, sizeof (laddr)); 5397c478bd9Sstevel@tonic-gate if (af == AF_INET || af == AF_INET6) { 5407c478bd9Sstevel@tonic-gate laddr.ss_family = (sa_family_t)af; 5417c478bd9Sstevel@tonic-gate } else { 5427c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT; 5437c478bd9Sstevel@tonic-gate return (-1); 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate return (rresvport_addr(alport, &laddr)); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate int 5497c478bd9Sstevel@tonic-gate rresvport(int *alport) 5507c478bd9Sstevel@tonic-gate { 5517c478bd9Sstevel@tonic-gate return (rresvport_af(alport, AF_INET)); 5527c478bd9Sstevel@tonic-gate } 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate int 5557c478bd9Sstevel@tonic-gate ruserok(const char *rhost, int superuser, const char *ruser, const char *luser) 5567c478bd9Sstevel@tonic-gate { 5577c478bd9Sstevel@tonic-gate FILE *hostf; 5587c478bd9Sstevel@tonic-gate char fhost[MAXHOSTNAMELEN]; 5597c478bd9Sstevel@tonic-gate const char *sp; 5607c478bd9Sstevel@tonic-gate char *p; 5617c478bd9Sstevel@tonic-gate int baselen = -1; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate struct stat64 sbuf; 5647c478bd9Sstevel@tonic-gate struct passwd *pwd; 5657c478bd9Sstevel@tonic-gate char pbuf[MAXPATHLEN]; 5667c478bd9Sstevel@tonic-gate uid_t uid = (uid_t)-1; 5677c478bd9Sstevel@tonic-gate gid_t gid = (gid_t)-1; 5687c478bd9Sstevel@tonic-gate gid_t grouplist[NGROUPS_MAX]; 5697c478bd9Sstevel@tonic-gate int ngroups; 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate sp = rhost; 5727c478bd9Sstevel@tonic-gate p = fhost; 5737c478bd9Sstevel@tonic-gate while (*sp) { 5747c478bd9Sstevel@tonic-gate if (*sp == '.') { 5757c478bd9Sstevel@tonic-gate if (baselen == -1) 5767c478bd9Sstevel@tonic-gate baselen = (int)(sp - rhost); 5777c478bd9Sstevel@tonic-gate *p++ = *sp++; 5787c478bd9Sstevel@tonic-gate } else { 5797c478bd9Sstevel@tonic-gate *p++ = isupper(*sp) ? tolower(*sp++) : *sp++; 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate *p = '\0'; 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate /* check /etc/hosts.equiv */ 5857c478bd9Sstevel@tonic-gate if (!superuser) { 586004388ebScasper if ((hostf = fopen("/etc/hosts.equiv", "rF")) != NULL) { 5877c478bd9Sstevel@tonic-gate if (!_validuser(hostf, fhost, luser, ruser, baselen)) { 5887c478bd9Sstevel@tonic-gate (void) fclose(hostf); 5897c478bd9Sstevel@tonic-gate return (0); 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate (void) fclose(hostf); 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate /* check ~/.rhosts */ 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if ((pwd = getpwnam(luser)) == NULL) 5987c478bd9Sstevel@tonic-gate return (-1); 5997c478bd9Sstevel@tonic-gate (void) strcpy(pbuf, pwd->pw_dir); 6007c478bd9Sstevel@tonic-gate (void) strcat(pbuf, "/.rhosts"); 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate /* 6037c478bd9Sstevel@tonic-gate * Read .rhosts as the local user to avoid NFS mapping the root uid 6047c478bd9Sstevel@tonic-gate * to something that can't read .rhosts. 6057c478bd9Sstevel@tonic-gate */ 6067c478bd9Sstevel@tonic-gate gid = getegid(); 6077c478bd9Sstevel@tonic-gate uid = geteuid(); 6087c478bd9Sstevel@tonic-gate if ((ngroups = getgroups(NGROUPS_MAX, grouplist)) == -1) 6097c478bd9Sstevel@tonic-gate return (-1); 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate (void) setegid(pwd->pw_gid); 6127c478bd9Sstevel@tonic-gate initgroups(pwd->pw_name, pwd->pw_gid); 6137c478bd9Sstevel@tonic-gate (void) seteuid(pwd->pw_uid); 614004388ebScasper if ((hostf = fopen(pbuf, "rF")) == NULL) { 6157c478bd9Sstevel@tonic-gate if (gid != (gid_t)-1) 6167c478bd9Sstevel@tonic-gate (void) setegid(gid); 6177c478bd9Sstevel@tonic-gate if (uid != (uid_t)-1) 6187c478bd9Sstevel@tonic-gate (void) seteuid(uid); 6197c478bd9Sstevel@tonic-gate setgroups(ngroups, grouplist); 6207c478bd9Sstevel@tonic-gate return (-1); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate (void) fstat64(fileno(hostf), &sbuf); 6237c478bd9Sstevel@tonic-gate if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) { 6247c478bd9Sstevel@tonic-gate (void) fclose(hostf); 6257c478bd9Sstevel@tonic-gate if (gid != (gid_t)-1) 6267c478bd9Sstevel@tonic-gate (void) setegid(gid); 6277c478bd9Sstevel@tonic-gate if (uid != (uid_t)-1) 6287c478bd9Sstevel@tonic-gate (void) seteuid(uid); 6297c478bd9Sstevel@tonic-gate setgroups(ngroups, grouplist); 6307c478bd9Sstevel@tonic-gate return (-1); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate if (!_validuser(hostf, fhost, luser, ruser, baselen)) { 6347c478bd9Sstevel@tonic-gate (void) fclose(hostf); 6357c478bd9Sstevel@tonic-gate if (gid != (gid_t)-1) 6367c478bd9Sstevel@tonic-gate (void) setegid(gid); 6377c478bd9Sstevel@tonic-gate if (uid != (uid_t)-1) 6387c478bd9Sstevel@tonic-gate (void) seteuid(uid); 6397c478bd9Sstevel@tonic-gate setgroups(ngroups, grouplist); 6407c478bd9Sstevel@tonic-gate return (0); 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate (void) fclose(hostf); 6447c478bd9Sstevel@tonic-gate if (gid != (gid_t)-1) 6457c478bd9Sstevel@tonic-gate (void) setegid(gid); 6467c478bd9Sstevel@tonic-gate if (uid != (uid_t)-1) 6477c478bd9Sstevel@tonic-gate (void) seteuid(uid); 6487c478bd9Sstevel@tonic-gate setgroups(ngroups, grouplist); 6497c478bd9Sstevel@tonic-gate return (-1); 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate static int 6537c478bd9Sstevel@tonic-gate _validuser(FILE *hostf, char *rhost, const char *luser, 6547c478bd9Sstevel@tonic-gate const char *ruser, int baselen) 6557c478bd9Sstevel@tonic-gate { 6567c478bd9Sstevel@tonic-gate char *user; 6577c478bd9Sstevel@tonic-gate char ahost[BUFSIZ]; 6587c478bd9Sstevel@tonic-gate char *uchost = (char *)NULL; 6597c478bd9Sstevel@tonic-gate int hostmatch, usermatch; 6607c478bd9Sstevel@tonic-gate char *p; 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate #ifdef NIS 6637c478bd9Sstevel@tonic-gate if (domain == NULL) { 6647c478bd9Sstevel@tonic-gate (void) usingypmap(&domain, NULL); 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate #endif /* NIS */ 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate while (fgets(ahost, (int)sizeof (ahost), hostf)) { 6697c478bd9Sstevel@tonic-gate uchost = (char *)NULL; 6707c478bd9Sstevel@tonic-gate hostmatch = usermatch = 0; 6717c478bd9Sstevel@tonic-gate p = ahost; 6727c478bd9Sstevel@tonic-gate /* 6737c478bd9Sstevel@tonic-gate * We can get a line bigger than our buffer. If so we skip 6747c478bd9Sstevel@tonic-gate * the offending line. 6757c478bd9Sstevel@tonic-gate */ 6767c478bd9Sstevel@tonic-gate if (strchr(p, '\n') == NULL) { 6777c478bd9Sstevel@tonic-gate while (fgets(ahost, (int)sizeof (ahost), hostf) && 6787c478bd9Sstevel@tonic-gate strchr(ahost, '\n') == NULL) 6797c478bd9Sstevel@tonic-gate ; 6807c478bd9Sstevel@tonic-gate continue; 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 6837c478bd9Sstevel@tonic-gate /* 6847c478bd9Sstevel@tonic-gate * Both host and user ``names'' can be netgroups, 6857c478bd9Sstevel@tonic-gate * and must have their case preserved. Case is 6867c478bd9Sstevel@tonic-gate * preserved for user names because we break out 6877c478bd9Sstevel@tonic-gate * of this loop when finding a field separator. 6887c478bd9Sstevel@tonic-gate * To do so for host names, we must make a copy of 6897c478bd9Sstevel@tonic-gate * the host name field. 6907c478bd9Sstevel@tonic-gate */ 6917c478bd9Sstevel@tonic-gate if (isupper(*p)) { 6927c478bd9Sstevel@tonic-gate if (uchost == (char *)NULL) 6937c478bd9Sstevel@tonic-gate uchost = strdup(ahost); 6947c478bd9Sstevel@tonic-gate *p = tolower(*p); 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate p++; 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate if (*p != '\0' && uchost != (char *)NULL) 6997c478bd9Sstevel@tonic-gate uchost[p - ahost] = '\0'; 7007c478bd9Sstevel@tonic-gate if (*p == ' ' || *p == '\t') { 7017c478bd9Sstevel@tonic-gate *p++ = '\0'; 7027c478bd9Sstevel@tonic-gate while (*p == ' ' || *p == '\t') 7037c478bd9Sstevel@tonic-gate p++; 7047c478bd9Sstevel@tonic-gate user = p; 7057c478bd9Sstevel@tonic-gate while (*p != '\n' && *p != ' ' && *p != '\t' && 7067c478bd9Sstevel@tonic-gate *p != '\0') 7077c478bd9Sstevel@tonic-gate p++; 7087c478bd9Sstevel@tonic-gate } else 7097c478bd9Sstevel@tonic-gate user = p; 7107c478bd9Sstevel@tonic-gate *p = '\0'; 7117c478bd9Sstevel@tonic-gate if (ahost[0] == '+' && ahost[1] == 0) 7127c478bd9Sstevel@tonic-gate hostmatch = 1; 7137c478bd9Sstevel@tonic-gate #ifdef NIS 7147c478bd9Sstevel@tonic-gate else if (ahost[0] == '+' && ahost[1] == '@') 7157c478bd9Sstevel@tonic-gate if (uchost != (char *)NULL) 7167c478bd9Sstevel@tonic-gate hostmatch = innetgr(uchost + 2, rhost, 7177c478bd9Sstevel@tonic-gate NULL, domain); 7187c478bd9Sstevel@tonic-gate else 7197c478bd9Sstevel@tonic-gate hostmatch = innetgr(ahost + 2, rhost, 7207c478bd9Sstevel@tonic-gate NULL, domain); 7217c478bd9Sstevel@tonic-gate else if (ahost[0] == '-' && ahost[1] == '@') { 7227c478bd9Sstevel@tonic-gate if (uchost != (char *)NULL) { 7237c478bd9Sstevel@tonic-gate if (innetgr(uchost + 2, rhost, NULL, domain)) 7247c478bd9Sstevel@tonic-gate break; 7257c478bd9Sstevel@tonic-gate } else { 7267c478bd9Sstevel@tonic-gate if (innetgr(ahost + 2, rhost, NULL, domain)) 7277c478bd9Sstevel@tonic-gate break; 7287c478bd9Sstevel@tonic-gate } 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate #endif /* NIS */ 7317c478bd9Sstevel@tonic-gate else if (ahost[0] == '-') { 7327c478bd9Sstevel@tonic-gate if (_checkhost(rhost, ahost+1, baselen)) 7337c478bd9Sstevel@tonic-gate break; 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate else 7367c478bd9Sstevel@tonic-gate hostmatch = _checkhost(rhost, ahost, baselen); 7377c478bd9Sstevel@tonic-gate if (user[0]) { 7387c478bd9Sstevel@tonic-gate if (user[0] == '+' && user[1] == 0) 7397c478bd9Sstevel@tonic-gate usermatch = 1; 7407c478bd9Sstevel@tonic-gate #ifdef NIS 7417c478bd9Sstevel@tonic-gate else if (user[0] == '+' && user[1] == '@') 7427c478bd9Sstevel@tonic-gate usermatch = innetgr(user+2, NULL, 7437c478bd9Sstevel@tonic-gate ruser, domain); 7447c478bd9Sstevel@tonic-gate else if (user[0] == '-' && user[1] == '@') { 7457c478bd9Sstevel@tonic-gate if (hostmatch && 7467c478bd9Sstevel@tonic-gate innetgr(user+2, NULL, ruser, domain)) 7477c478bd9Sstevel@tonic-gate break; 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate #endif /* NIS */ 7507c478bd9Sstevel@tonic-gate else if (user[0] == '-') { 7517c478bd9Sstevel@tonic-gate if (hostmatch && (strcmp(user+1, ruser) == 0)) 7527c478bd9Sstevel@tonic-gate break; 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate else 7557c478bd9Sstevel@tonic-gate usermatch = (strcmp(user, ruser) == 0); 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate else 7587c478bd9Sstevel@tonic-gate usermatch = (strcmp(ruser, luser) == 0); 7597c478bd9Sstevel@tonic-gate if (uchost != (char *)NULL) 7607c478bd9Sstevel@tonic-gate free(uchost); 7617c478bd9Sstevel@tonic-gate if (hostmatch && usermatch) 7627c478bd9Sstevel@tonic-gate return (0); 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate if (uchost != (char *)NULL) 7667c478bd9Sstevel@tonic-gate free(uchost); 7677c478bd9Sstevel@tonic-gate return (-1); 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate static int 7717c478bd9Sstevel@tonic-gate _checkhost(char *rhost, char *lhost, int len) 7727c478bd9Sstevel@tonic-gate { 7737c478bd9Sstevel@tonic-gate static char *ldomain; 7747c478bd9Sstevel@tonic-gate static char *domainp; 7757c478bd9Sstevel@tonic-gate static int nodomain; 7767c478bd9Sstevel@tonic-gate char *cp; 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate if (ldomain == NULL) { 7797c478bd9Sstevel@tonic-gate ldomain = (char *)malloc(MAXHOSTNAMELEN+1); 7807c478bd9Sstevel@tonic-gate if (ldomain == 0) 7817c478bd9Sstevel@tonic-gate return (0); 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate if (len == -1) 7857c478bd9Sstevel@tonic-gate return (strcmp(rhost, lhost) == 0); 7867c478bd9Sstevel@tonic-gate if (strncmp(rhost, lhost, len)) 7877c478bd9Sstevel@tonic-gate return (0); 7887c478bd9Sstevel@tonic-gate if (strcmp(rhost, lhost) == 0) 7897c478bd9Sstevel@tonic-gate return (1); 7907c478bd9Sstevel@tonic-gate if (*(lhost + len) != '\0') 7917c478bd9Sstevel@tonic-gate return (0); 7927c478bd9Sstevel@tonic-gate if (nodomain) 7937c478bd9Sstevel@tonic-gate return (0); 7947c478bd9Sstevel@tonic-gate if (!domainp) { 7957c478bd9Sstevel@tonic-gate /* 7967c478bd9Sstevel@tonic-gate * "domainp" points after the first dot in the host name 7977c478bd9Sstevel@tonic-gate */ 7987c478bd9Sstevel@tonic-gate if (gethostname(ldomain, MAXHOSTNAMELEN) == -1) { 7997c478bd9Sstevel@tonic-gate nodomain = 1; 8007c478bd9Sstevel@tonic-gate return (0); 8017c478bd9Sstevel@tonic-gate } 8027c478bd9Sstevel@tonic-gate ldomain[MAXHOSTNAMELEN] = NULL; 8037c478bd9Sstevel@tonic-gate if ((domainp = index(ldomain, '.')) == (char *)NULL) { 8047c478bd9Sstevel@tonic-gate nodomain = 1; 8057c478bd9Sstevel@tonic-gate return (0); 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate domainp++; 8087c478bd9Sstevel@tonic-gate cp = domainp; 8097c478bd9Sstevel@tonic-gate while (*cp) { 8107c478bd9Sstevel@tonic-gate *cp = isupper(*cp) ? tolower(*cp) : *cp; 8117c478bd9Sstevel@tonic-gate cp++; 8127c478bd9Sstevel@tonic-gate } 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate return (strcmp(domainp, rhost + len + 1) == 0); 8157c478bd9Sstevel@tonic-gate } 816