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