xref: /titanic_52/usr/src/lib/libsocket/inet/rcmd.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
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  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*67dbe2beSCasper H.S. Dik  * Copyright 2009 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 #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include <pwd.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/file.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <libintl.h>
497c478bd9Sstevel@tonic-gate #include <sys/socket.h>
507c478bd9Sstevel@tonic-gate #include <sys/stat.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <netinet/in.h>
537c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
547c478bd9Sstevel@tonic-gate #include <inet/common.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <netdb.h>
577c478bd9Sstevel@tonic-gate #include <errno.h>
587c478bd9Sstevel@tonic-gate #include <fcntl.h>
597c478bd9Sstevel@tonic-gate #include <unistd.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <grp.h>
63*67dbe2beSCasper H.S. Dik #include <alloca.h>
647c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <priv_utils.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifdef SYSV
697c478bd9Sstevel@tonic-gate #define	bcopy(s1, s2, len)	(void) memcpy(s2, s1, len)
707c478bd9Sstevel@tonic-gate #define	bzero(s, len)		(void) memset(s, 0, len)
717c478bd9Sstevel@tonic-gate #define	index(s, c)		strchr(s, c)
727c478bd9Sstevel@tonic-gate char	*strchr();
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate char	*index();
757c478bd9Sstevel@tonic-gate #endif /* SYSV */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate extern int  usingypmap();
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int _validuser(FILE *hostf, char *rhost, const char *luser,
807c478bd9Sstevel@tonic-gate 			const char *ruser, int baselen);
817c478bd9Sstevel@tonic-gate static int _checkhost(char *rhost, char *lhost, int len);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #ifdef NIS
857c478bd9Sstevel@tonic-gate static char *domain;
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int rcmd(char **ahost, unsigned short rport, const char *locuser,
897c478bd9Sstevel@tonic-gate     const char *remuser, const char *cmd, int *fd2p)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	int rcmd_ret;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	rcmd_ret = rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p,
947c478bd9Sstevel@tonic-gate 	    AF_INET);
957c478bd9Sstevel@tonic-gate 	return (rcmd_ret);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate int rcmd_af(char **ahost, unsigned short rport, const char *locuser,
997c478bd9Sstevel@tonic-gate     const char *remuser, const char *cmd, int *fd2p, int af)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	int s, timo = 1;
1027c478bd9Sstevel@tonic-gate 	ssize_t retval;
1037c478bd9Sstevel@tonic-gate 	pid_t pid;
1047c478bd9Sstevel@tonic-gate 	struct sockaddr_storage caddr, faddr;
1057c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
1067c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
1077c478bd9Sstevel@tonic-gate 	struct addrinfo hints;
1087c478bd9Sstevel@tonic-gate 	struct addrinfo *res, *resp;
1097c478bd9Sstevel@tonic-gate 	size_t addrlen;
1107c478bd9Sstevel@tonic-gate 	int rc;
1117c478bd9Sstevel@tonic-gate #define	MAX_SHORTSTRLEN 6
1127c478bd9Sstevel@tonic-gate 	char aport[MAX_SHORTSTRLEN];
1137c478bd9Sstevel@tonic-gate 	char c;
1147c478bd9Sstevel@tonic-gate 	int lport = 0;
1157c478bd9Sstevel@tonic-gate #ifdef SYSV
1167c478bd9Sstevel@tonic-gate 	sigset_t oldmask;
1177c478bd9Sstevel@tonic-gate 	sigset_t newmask;
1187c478bd9Sstevel@tonic-gate 	struct sigaction oldaction;
1197c478bd9Sstevel@tonic-gate 	struct sigaction newaction;
1207c478bd9Sstevel@tonic-gate #else
1217c478bd9Sstevel@tonic-gate 	int oldmask;
1227c478bd9Sstevel@tonic-gate #endif /* SYSV */
1237c478bd9Sstevel@tonic-gate 	fd_set fdset;
1247c478bd9Sstevel@tonic-gate 	int selret;
1257c478bd9Sstevel@tonic-gate 	char *addr;
1267c478bd9Sstevel@tonic-gate 	static char hostname[MAXHOSTNAMELEN];
1277c478bd9Sstevel@tonic-gate 	socklen_t len;
1287c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) {
1317c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	pid = getpid();
1367c478bd9Sstevel@tonic-gate 	memset(&hints, 0, sizeof (hints));
1377c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
1387c478bd9Sstevel@tonic-gate 	hints.ai_flags = AI_CANONNAME;
1397c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
1407c478bd9Sstevel@tonic-gate 		hints.ai_flags |= AI_V4MAPPED;
1417c478bd9Sstevel@tonic-gate 		hints.ai_family = AF_UNSPEC;
1427c478bd9Sstevel@tonic-gate 	} else {
1437c478bd9Sstevel@tonic-gate 		hints.ai_family = af;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	(void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport));
1467c478bd9Sstevel@tonic-gate 	rc = getaddrinfo(*ahost, aport, &hints, &res);
1477c478bd9Sstevel@tonic-gate 	if (rc != 0) {
1487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1497257d1b4Sraf 		    dgettext(TEXT_DOMAIN, "%s: unknown host%s\n"),
1507c478bd9Sstevel@tonic-gate 		    *ahost, rc == EAI_AGAIN ? " (try again later)" : "");
1517c478bd9Sstevel@tonic-gate 		return (-1);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	resp = res;
1547c478bd9Sstevel@tonic-gate 	(void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN);
1557c478bd9Sstevel@tonic-gate 	*ahost = hostname;
1567c478bd9Sstevel@tonic-gate #ifdef SYSV
1577c478bd9Sstevel@tonic-gate 	/* ignore SIGPIPE */
1587c478bd9Sstevel@tonic-gate 	bzero((char *)&newaction, sizeof (newaction));
1597c478bd9Sstevel@tonic-gate 	newaction.sa_handler = SIG_IGN;
1607257d1b4Sraf 	(void) sigaction(SIGPIPE, &newaction, &oldaction);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/* block SIGURG */
1637c478bd9Sstevel@tonic-gate 	bzero((char *)&newmask, sizeof (newmask));
1647257d1b4Sraf 	(void) sigaddset(&newmask, SIGURG);
1657257d1b4Sraf 	(void) sigprocmask(SIG_BLOCK, &newmask, &oldmask);
1667c478bd9Sstevel@tonic-gate #else
1677c478bd9Sstevel@tonic-gate 	oldmask = _sigblock(sigmask(SIGURG));
1687c478bd9Sstevel@tonic-gate #endif /* SYSV */
1697c478bd9Sstevel@tonic-gate 	for (;;) {
1707c478bd9Sstevel@tonic-gate 		s = rresvport_af(&lport, res->ai_family);
1717c478bd9Sstevel@tonic-gate 		if (s < 0) {
1727c478bd9Sstevel@tonic-gate 			int af = res->ai_family;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 			/*
1757c478bd9Sstevel@tonic-gate 			 * See if we have any addresses of a different type
1767c478bd9Sstevel@tonic-gate 			 * to try.
1777c478bd9Sstevel@tonic-gate 			 */
1787c478bd9Sstevel@tonic-gate 			while (res != NULL && res->ai_family == af)
1797c478bd9Sstevel@tonic-gate 				res = res->ai_next;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 			if (res != NULL)
1827c478bd9Sstevel@tonic-gate 				continue;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 			if (errno == EAGAIN)
1857c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1867257d1b4Sraf 				    dgettext(TEXT_DOMAIN,
1877c478bd9Sstevel@tonic-gate 				    "socket: All ports in use\n"));
1887c478bd9Sstevel@tonic-gate 			else
1897c478bd9Sstevel@tonic-gate 				perror("rcmd: socket");
1907c478bd9Sstevel@tonic-gate #ifdef SYSV
1917c478bd9Sstevel@tonic-gate 			/* restore original SIGPIPE handler */
1927257d1b4Sraf 			(void) sigaction(SIGPIPE, &oldaction,
1937c478bd9Sstevel@tonic-gate 			    (struct sigaction *)0);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 			/* restore original signal mask */
1967257d1b4Sraf 			(void) sigprocmask(SIG_SETMASK, &oldmask,
1977c478bd9Sstevel@tonic-gate 			    (sigset_t *)0);
1987c478bd9Sstevel@tonic-gate #else
1997c478bd9Sstevel@tonic-gate 			sigsetmask(oldmask);
2007c478bd9Sstevel@tonic-gate #endif /* SYSV */
2017c478bd9Sstevel@tonic-gate 			freeaddrinfo(resp);
2027c478bd9Sstevel@tonic-gate 			return (-1);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		bzero((char *)&caddr, sizeof (caddr));
2057c478bd9Sstevel@tonic-gate 		bcopy(res->ai_addr, &caddr, res->ai_addrlen);
2067c478bd9Sstevel@tonic-gate 		addrlen = res->ai_addrlen;
2077c478bd9Sstevel@tonic-gate 		if (af == AF_INET6 && res->ai_addr->sa_family == AF_INET) {
2087c478bd9Sstevel@tonic-gate 			struct in6_addr ia6;
2097c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *in6addr;
2107c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&((struct sockaddr_in *)
2117c478bd9Sstevel@tonic-gate 			    res->ai_addr)->sin_addr, &ia6);
2127c478bd9Sstevel@tonic-gate 			in6addr = (struct sockaddr_in6 *)&caddr;
2137c478bd9Sstevel@tonic-gate 			in6addr->sin6_addr = ia6;
2147c478bd9Sstevel@tonic-gate 			in6addr->sin6_family = AF_INET6;
2157c478bd9Sstevel@tonic-gate 			addrlen = sizeof (struct sockaddr_in6);
2167c478bd9Sstevel@tonic-gate 		}
2177257d1b4Sraf 		(void) fcntl(s, F_SETOWN, pid);
2187c478bd9Sstevel@tonic-gate 		if (connect(s, (struct sockaddr *)&caddr, addrlen) >= 0)
2197c478bd9Sstevel@tonic-gate 			break;
2207c478bd9Sstevel@tonic-gate 		(void) close(s);
2217c478bd9Sstevel@tonic-gate 		if (errno == EADDRINUSE) {
2227c478bd9Sstevel@tonic-gate 			lport = 0;
2237c478bd9Sstevel@tonic-gate 			continue;
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 		if (errno == ECONNREFUSED && timo <= 16) {
2267c478bd9Sstevel@tonic-gate 			(void) sleep(timo);
2277c478bd9Sstevel@tonic-gate 			timo *= 2;
2287c478bd9Sstevel@tonic-gate 			continue;
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 		if (res->ai_next != NULL) {
2317c478bd9Sstevel@tonic-gate 			int oerrno = errno;
2327c478bd9Sstevel@tonic-gate 			if (res->ai_addr->sa_family == AF_INET6)
2337c478bd9Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in6 *)
2347c478bd9Sstevel@tonic-gate 				    res->ai_addr)->sin6_addr;
2357c478bd9Sstevel@tonic-gate 			else
2367c478bd9Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in *)
2377c478bd9Sstevel@tonic-gate 				    res->ai_addr)->sin_addr;
2387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2397257d1b4Sraf 			    dgettext(TEXT_DOMAIN, "connect to address %s: "),
2407c478bd9Sstevel@tonic-gate 			    inet_ntop(res->ai_addr->sa_family, addr,
2417c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
2427c478bd9Sstevel@tonic-gate 			errno = oerrno;
2437c478bd9Sstevel@tonic-gate 			perror(0);
2447c478bd9Sstevel@tonic-gate 			res = res->ai_next;
2457c478bd9Sstevel@tonic-gate 			if (res->ai_addr->sa_family == AF_INET6)
2467c478bd9Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in6 *)
2477c478bd9Sstevel@tonic-gate 				    res->ai_addr)->sin6_addr;
2487c478bd9Sstevel@tonic-gate 			else
2497c478bd9Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in *)
2507c478bd9Sstevel@tonic-gate 				    res->ai_addr)->sin_addr;
2517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2527257d1b4Sraf 			    dgettext(TEXT_DOMAIN, "Trying %s...\n"),
2537c478bd9Sstevel@tonic-gate 			    inet_ntop(res->ai_addr->sa_family, addr,
2547c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
2557c478bd9Sstevel@tonic-gate 			continue;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 		perror(*ahost);
2587c478bd9Sstevel@tonic-gate 		freeaddrinfo(resp);
2597c478bd9Sstevel@tonic-gate #ifdef SYSV
2607c478bd9Sstevel@tonic-gate 		/* restore original SIGPIPE handler */
2617257d1b4Sraf 		(void) sigaction(SIGPIPE, &oldaction,
2627c478bd9Sstevel@tonic-gate 		    (struct sigaction *)0);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		/* restore original signal mask */
2657257d1b4Sraf 		(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
2667c478bd9Sstevel@tonic-gate #else
2677c478bd9Sstevel@tonic-gate 		sigsetmask(oldmask);
2687c478bd9Sstevel@tonic-gate #endif /* SYSV */
2697c478bd9Sstevel@tonic-gate 		return (-1);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	lport = 0;
2727c478bd9Sstevel@tonic-gate 	if (fd2p == 0) {
2737c478bd9Sstevel@tonic-gate 		(void) write(s, "", 1);
2747c478bd9Sstevel@tonic-gate 	} else {
2757c478bd9Sstevel@tonic-gate 		int s2 = rresvport_af(&lport, res->ai_family), s3;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		len = (socklen_t)sizeof (faddr);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		if (s2 < 0)
2807c478bd9Sstevel@tonic-gate 			goto bad;
2817c478bd9Sstevel@tonic-gate 		(void) listen(s2, 1);
2827c478bd9Sstevel@tonic-gate 		(void) snprintf(aport, MAX_SHORTSTRLEN, "%d", lport);
2837c478bd9Sstevel@tonic-gate 		if (write(s, aport, strlen(aport)+1) != strlen(aport)+1) {
2847257d1b4Sraf 			perror(dgettext(TEXT_DOMAIN,
2857c478bd9Sstevel@tonic-gate 			    "write: setting up stderr"));
2867c478bd9Sstevel@tonic-gate 			(void) close(s2);
2877c478bd9Sstevel@tonic-gate 			goto bad;
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		FD_ZERO(&fdset);
2907c478bd9Sstevel@tonic-gate 		FD_SET(s, &fdset);
2917c478bd9Sstevel@tonic-gate 		FD_SET(s2, &fdset);
2927c478bd9Sstevel@tonic-gate 		while ((selret = select(FD_SETSIZE, &fdset, (fd_set *)0,
2937c478bd9Sstevel@tonic-gate 		    (fd_set *)0, (struct timeval *)0)) > 0) {
2947c478bd9Sstevel@tonic-gate 			if (FD_ISSET(s, &fdset)) {
2957c478bd9Sstevel@tonic-gate 				/*
2967c478bd9Sstevel@tonic-gate 				 *	Something's wrong:  we should get no
2977c478bd9Sstevel@tonic-gate 				 *	data on this connection at this point,
2987c478bd9Sstevel@tonic-gate 				 *	so we assume that the connection has
2997c478bd9Sstevel@tonic-gate 				 *	gone away.
3007c478bd9Sstevel@tonic-gate 				 */
3017c478bd9Sstevel@tonic-gate 				(void) close(s2);
3027c478bd9Sstevel@tonic-gate 				goto bad;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 			if (FD_ISSET(s2, &fdset)) {
3057c478bd9Sstevel@tonic-gate 				/*
3067c478bd9Sstevel@tonic-gate 				 *	We assume this is an incoming connect
3077c478bd9Sstevel@tonic-gate 				 *	request and proceed normally.
3087c478bd9Sstevel@tonic-gate 				 */
3097c478bd9Sstevel@tonic-gate 				s3 = accept(s2, (struct sockaddr *)&faddr,
3107c478bd9Sstevel@tonic-gate 				    &len);
3117c478bd9Sstevel@tonic-gate 				FD_CLR(s2, &fdset);
3127c478bd9Sstevel@tonic-gate 				(void) close(s2);
3137c478bd9Sstevel@tonic-gate 				if (s3 < 0) {
3147c478bd9Sstevel@tonic-gate 					perror("accept");
3157c478bd9Sstevel@tonic-gate 					lport = 0;
3167c478bd9Sstevel@tonic-gate 					goto bad;
3177c478bd9Sstevel@tonic-gate 				}
3187c478bd9Sstevel@tonic-gate 				else
3197c478bd9Sstevel@tonic-gate 					break;
3207c478bd9Sstevel@tonic-gate 			}
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		if (selret == -1) {
3237c478bd9Sstevel@tonic-gate 			/*
3247c478bd9Sstevel@tonic-gate 			 *	This should not happen, and we treat it as
3257c478bd9Sstevel@tonic-gate 			 *	a fatal error.
3267c478bd9Sstevel@tonic-gate 			 */
3277c478bd9Sstevel@tonic-gate 			(void) close(s2);
3287c478bd9Sstevel@tonic-gate 			goto bad;
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		*fd2p = s3;
3327c478bd9Sstevel@tonic-gate 		switch (faddr.ss_family) {
3337c478bd9Sstevel@tonic-gate 		case AF_INET:
3347c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&faddr;
3357c478bd9Sstevel@tonic-gate 			if (ntohs(sin->sin_port) >= IPPORT_RESERVED) {
3367c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3377257d1b4Sraf 				    dgettext(TEXT_DOMAIN,
3387c478bd9Sstevel@tonic-gate 				    "socket: protocol failure in circuit "
3397c478bd9Sstevel@tonic-gate 				    "setup.\n"));
3407c478bd9Sstevel@tonic-gate 				goto bad2;
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 		case AF_INET6:
3447c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&faddr;
3457c478bd9Sstevel@tonic-gate 			if (ntohs(sin6->sin6_port) >= IPPORT_RESERVED) {
3467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3477257d1b4Sraf 				    dgettext(TEXT_DOMAIN,
3487c478bd9Sstevel@tonic-gate 				    "socket: protocol failure in circuit "
3497c478bd9Sstevel@tonic-gate 				    "setup.\n"));
3507c478bd9Sstevel@tonic-gate 				goto bad2;
3517c478bd9Sstevel@tonic-gate 			}
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 		default:
3547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3557257d1b4Sraf 			    dgettext(TEXT_DOMAIN,
3567c478bd9Sstevel@tonic-gate 			    "socket: protocol failure in circuit setup.\n"));
3577c478bd9Sstevel@tonic-gate 			goto bad2;
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	(void) write(s, locuser, strlen(locuser)+1);
3617c478bd9Sstevel@tonic-gate 	(void) write(s, remuser, strlen(remuser)+1);
3627c478bd9Sstevel@tonic-gate 	(void) write(s, cmd, strlen(cmd)+1);
3637c478bd9Sstevel@tonic-gate 	retval = read(s, &c, 1);
3647c478bd9Sstevel@tonic-gate 	if (retval != 1) {
3657c478bd9Sstevel@tonic-gate 		if (retval == 0) {
3667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3677257d1b4Sraf 			    dgettext(TEXT_DOMAIN,
3687c478bd9Sstevel@tonic-gate 			    "Protocol error, %s closed connection\n"),
3697c478bd9Sstevel@tonic-gate 			    *ahost);
3707c478bd9Sstevel@tonic-gate 		} else if (retval < 0) {
3717c478bd9Sstevel@tonic-gate 			perror(*ahost);
3727c478bd9Sstevel@tonic-gate 		} else {
3737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3747257d1b4Sraf 			    dgettext(TEXT_DOMAIN,
3757c478bd9Sstevel@tonic-gate 			    "Protocol error, %s sent %d bytes\n"),
3767c478bd9Sstevel@tonic-gate 			    *ahost, retval);
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 		goto bad2;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	if (c != 0) {
3817c478bd9Sstevel@tonic-gate 		while (read(s, &c, 1) == 1) {
3827c478bd9Sstevel@tonic-gate 			(void) write(2, &c, 1);
3837c478bd9Sstevel@tonic-gate 			if (c == '\n')
3847c478bd9Sstevel@tonic-gate 				break;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		goto bad2;
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate #ifdef SYSV
3897c478bd9Sstevel@tonic-gate 	/* restore original SIGPIPE handler */
3907257d1b4Sraf 	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* restore original signal mask */
3937257d1b4Sraf 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
3947c478bd9Sstevel@tonic-gate #else
3957c478bd9Sstevel@tonic-gate 	sigsetmask(oldmask);
3967c478bd9Sstevel@tonic-gate #endif /* SYSV */
3977c478bd9Sstevel@tonic-gate 	freeaddrinfo(resp);
3987c478bd9Sstevel@tonic-gate 	return (s);
3997c478bd9Sstevel@tonic-gate bad2:
4007c478bd9Sstevel@tonic-gate 	if (lport)
4017c478bd9Sstevel@tonic-gate 		(void) close(*fd2p);
4027c478bd9Sstevel@tonic-gate bad:
4037c478bd9Sstevel@tonic-gate 	(void) close(s);
4047c478bd9Sstevel@tonic-gate #ifdef SYSV
4057c478bd9Sstevel@tonic-gate 	/* restore original SIGPIPE handler */
4067257d1b4Sraf 	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/* restore original signal mask */
4097257d1b4Sraf 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
4107c478bd9Sstevel@tonic-gate #else
4117c478bd9Sstevel@tonic-gate 	sigsetmask(oldmask);
4127c478bd9Sstevel@tonic-gate #endif /* SYSV */
4137c478bd9Sstevel@tonic-gate 	freeaddrinfo(resp);
4147c478bd9Sstevel@tonic-gate 	return (-1);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate static int
4187c478bd9Sstevel@tonic-gate _rresvport_addr(int *alport, struct sockaddr_storage *addr)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
4217c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
4227c478bd9Sstevel@tonic-gate 	int s;
4237c478bd9Sstevel@tonic-gate 	socklen_t len;
4247c478bd9Sstevel@tonic-gate 	int on = 1;
4257c478bd9Sstevel@tonic-gate 	int off = 0;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (addr->ss_family == AF_INET) {
4287c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)addr;
4297c478bd9Sstevel@tonic-gate 		len = sizeof (struct sockaddr_in);
4307c478bd9Sstevel@tonic-gate 	} else if (addr->ss_family == AF_INET6) {
4317c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)addr;
4327c478bd9Sstevel@tonic-gate 		len = sizeof (struct sockaddr_in6);
4337c478bd9Sstevel@tonic-gate 	} else {
4347c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
4357c478bd9Sstevel@tonic-gate 		return (-1);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	s = socket(addr->ss_family, SOCK_STREAM, 0);
4387c478bd9Sstevel@tonic-gate 	if (s < 0)
4397c478bd9Sstevel@tonic-gate 		return (-1);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/*
442ae347574Skcpoon 	 * Set SO_EXCLBIND to get a "unique" port, which is not bound
4437c478bd9Sstevel@tonic-gate 	 * to any other sockets.
4447c478bd9Sstevel@tonic-gate 	 */
445ae347574Skcpoon 	if (setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &on, sizeof (on)) < 0) {
4467c478bd9Sstevel@tonic-gate 		(void) close(s);
4477c478bd9Sstevel@tonic-gate 		return (-1);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* Try to bind() to the given port first. */
4517c478bd9Sstevel@tonic-gate 	if (*alport != 0) {
4527c478bd9Sstevel@tonic-gate 		if (addr->ss_family == AF_INET) {
4537c478bd9Sstevel@tonic-gate 			sin->sin_port = htons((ushort_t)*alport);
4547c478bd9Sstevel@tonic-gate 		} else {
4557c478bd9Sstevel@tonic-gate 			sin6->sin6_port = htons((ushort_t)*alport);
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 		if (bind(s, (struct sockaddr *)addr, len) >= 0) {
458ae347574Skcpoon 			/* To be safe, need to turn off SO_EXCLBIND. */
459ae347574Skcpoon 			(void) setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &off,
4607c478bd9Sstevel@tonic-gate 			    sizeof (off));
4617c478bd9Sstevel@tonic-gate 			return (s);
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 		if (errno != EADDRINUSE) {
4647c478bd9Sstevel@tonic-gate 			(void) close(s);
4657c478bd9Sstevel@tonic-gate 			return (-1);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * If no port is given or the above bind() does not succeed, set
4717c478bd9Sstevel@tonic-gate 	 * TCP_ANONPRIVBIND option to ask the kernel to pick a port in the
4727c478bd9Sstevel@tonic-gate 	 * priviledged range for us.
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 	if (setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &on,
4757c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0) {
4767c478bd9Sstevel@tonic-gate 		(void) close(s);
4777c478bd9Sstevel@tonic-gate 		return (-1);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	if (addr->ss_family == AF_INET) {
4807c478bd9Sstevel@tonic-gate 		sin->sin_port = 0;
4817c478bd9Sstevel@tonic-gate 	} else {
4827c478bd9Sstevel@tonic-gate 		sin6->sin6_port = 0;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	if (bind(s, (struct sockaddr *)addr, len) >= 0) {
4857c478bd9Sstevel@tonic-gate 		/*
4867c478bd9Sstevel@tonic-gate 		 * We need to tell the caller what the port is.
4877c478bd9Sstevel@tonic-gate 		 */
4887c478bd9Sstevel@tonic-gate 		if (getsockname(s, (struct sockaddr *)addr, &len) < 0) {
4897c478bd9Sstevel@tonic-gate 			(void) close(s);
4907c478bd9Sstevel@tonic-gate 			return (-1);
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 		switch (addr->ss_family) {
4937c478bd9Sstevel@tonic-gate 		case AF_INET6:
4947c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addr;
4957c478bd9Sstevel@tonic-gate 			*alport = ntohs(sin6->sin6_port);
4967c478bd9Sstevel@tonic-gate 			break;
4977c478bd9Sstevel@tonic-gate 		case AF_INET:
4987c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)addr;
4997c478bd9Sstevel@tonic-gate 			*alport = ntohs(sin->sin_port);
5007c478bd9Sstevel@tonic-gate 			break;
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		/*
5047c478bd9Sstevel@tonic-gate 		 * To be safe, always turn off these options when we are done.
5057c478bd9Sstevel@tonic-gate 		 */
5067c478bd9Sstevel@tonic-gate 		(void) setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &off,
5077c478bd9Sstevel@tonic-gate 		    sizeof (off));
508ae347574Skcpoon 		(void) setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &off,
5097c478bd9Sstevel@tonic-gate 		    sizeof (off));
5107c478bd9Sstevel@tonic-gate 		return (s);
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 	(void) close(s);
5137c478bd9Sstevel@tonic-gate 	return (-1);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate int
5177c478bd9Sstevel@tonic-gate rresvport_addr(int *alport, struct sockaddr_storage *addr)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	int res, err;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_ON);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	res = _rresvport_addr(alport, addr);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	err = errno;
5267c478bd9Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_OFF);
5277c478bd9Sstevel@tonic-gate 	errno = err;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	return (res);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate int
5337c478bd9Sstevel@tonic-gate rresvport_af(int *alport, int af)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	bzero(&laddr, sizeof (laddr));
5387c478bd9Sstevel@tonic-gate 	if (af == AF_INET || af == AF_INET6) {
5397c478bd9Sstevel@tonic-gate 		laddr.ss_family = (sa_family_t)af;
5407c478bd9Sstevel@tonic-gate 	} else {
5417c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
5427c478bd9Sstevel@tonic-gate 		return (-1);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	return (rresvport_addr(alport, &laddr));
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate int
5487c478bd9Sstevel@tonic-gate rresvport(int *alport)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	return (rresvport_af(alport, AF_INET));
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate int
5547c478bd9Sstevel@tonic-gate ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	FILE *hostf;
5577c478bd9Sstevel@tonic-gate 	char fhost[MAXHOSTNAMELEN];
5587c478bd9Sstevel@tonic-gate 	const char *sp;
5597c478bd9Sstevel@tonic-gate 	char *p;
5607c478bd9Sstevel@tonic-gate 	int baselen = -1;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	struct stat64 sbuf;
5637c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
5647c478bd9Sstevel@tonic-gate 	char pbuf[MAXPATHLEN];
5657c478bd9Sstevel@tonic-gate 	uid_t uid = (uid_t)-1;
5667c478bd9Sstevel@tonic-gate 	gid_t gid = (gid_t)-1;
567*67dbe2beSCasper H.S. Dik 	int maxgrp = getgroups(0, NULL);
568*67dbe2beSCasper H.S. Dik 	gid_t *grouplist = alloca(maxgrp * sizeof (gid_t));
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();
608*67dbe2beSCasper H.S. Dik 	if ((ngroups = getgroups(maxgrp, 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