xref: /onnv-gate/usr/src/lib/libsocket/inet/rcmd.c (revision 11134:8aa0c4ca6639)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216812Sraf 
220Sstevel@tonic-gate /*
23*11134SCasper.Dik@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <limits.h>
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <ctype.h>
430Sstevel@tonic-gate #include <pwd.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <sys/file.h>
470Sstevel@tonic-gate #include <signal.h>
480Sstevel@tonic-gate #include <libintl.h>
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <sys/stat.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <netinet/in.h>
530Sstevel@tonic-gate #include <netinet/tcp.h>
540Sstevel@tonic-gate #include <inet/common.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <netdb.h>
570Sstevel@tonic-gate #include <errno.h>
580Sstevel@tonic-gate #include <fcntl.h>
590Sstevel@tonic-gate #include <unistd.h>
600Sstevel@tonic-gate #include <string.h>
610Sstevel@tonic-gate #include <stdlib.h>
620Sstevel@tonic-gate #include <grp.h>
63*11134SCasper.Dik@Sun.COM #include <alloca.h>
640Sstevel@tonic-gate #include <arpa/inet.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <priv_utils.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifdef SYSV
690Sstevel@tonic-gate #define	bcopy(s1, s2, len)	(void) memcpy(s2, s1, len)
700Sstevel@tonic-gate #define	bzero(s, len)		(void) memset(s, 0, len)
710Sstevel@tonic-gate #define	index(s, c)		strchr(s, c)
720Sstevel@tonic-gate char	*strchr();
730Sstevel@tonic-gate #else
740Sstevel@tonic-gate char	*index();
750Sstevel@tonic-gate #endif /* SYSV */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate extern int  usingypmap();
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static int _validuser(FILE *hostf, char *rhost, const char *luser,
800Sstevel@tonic-gate 			const char *ruser, int baselen);
810Sstevel@tonic-gate static int _checkhost(char *rhost, char *lhost, int len);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #ifdef NIS
850Sstevel@tonic-gate static char *domain;
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate 
rcmd(char ** ahost,unsigned short rport,const char * locuser,const char * remuser,const char * cmd,int * fd2p)880Sstevel@tonic-gate int rcmd(char **ahost, unsigned short rport, const char *locuser,
890Sstevel@tonic-gate     const char *remuser, const char *cmd, int *fd2p)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	int rcmd_ret;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	rcmd_ret = rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p,
940Sstevel@tonic-gate 	    AF_INET);
950Sstevel@tonic-gate 	return (rcmd_ret);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
rcmd_af(char ** ahost,unsigned short rport,const char * locuser,const char * remuser,const char * cmd,int * fd2p,int af)980Sstevel@tonic-gate int rcmd_af(char **ahost, unsigned short rport, const char *locuser,
990Sstevel@tonic-gate     const char *remuser, const char *cmd, int *fd2p, int af)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	int s, timo = 1;
1020Sstevel@tonic-gate 	ssize_t retval;
1030Sstevel@tonic-gate 	pid_t pid;
1040Sstevel@tonic-gate 	struct sockaddr_storage caddr, faddr;
1050Sstevel@tonic-gate 	struct sockaddr_in *sin;
1060Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
1070Sstevel@tonic-gate 	struct addrinfo hints;
1080Sstevel@tonic-gate 	struct addrinfo *res, *resp;
1090Sstevel@tonic-gate 	size_t addrlen;
1100Sstevel@tonic-gate 	int rc;
1110Sstevel@tonic-gate #define	MAX_SHORTSTRLEN 6
1120Sstevel@tonic-gate 	char aport[MAX_SHORTSTRLEN];
1130Sstevel@tonic-gate 	char c;
1140Sstevel@tonic-gate 	int lport = 0;
1150Sstevel@tonic-gate #ifdef SYSV
1160Sstevel@tonic-gate 	sigset_t oldmask;
1170Sstevel@tonic-gate 	sigset_t newmask;
1180Sstevel@tonic-gate 	struct sigaction oldaction;
1190Sstevel@tonic-gate 	struct sigaction newaction;
1200Sstevel@tonic-gate #else
1210Sstevel@tonic-gate 	int oldmask;
1220Sstevel@tonic-gate #endif /* SYSV */
1230Sstevel@tonic-gate 	fd_set fdset;
1240Sstevel@tonic-gate 	int selret;
1250Sstevel@tonic-gate 	char *addr;
1260Sstevel@tonic-gate 	static char hostname[MAXHOSTNAMELEN];
1270Sstevel@tonic-gate 	socklen_t len;
1280Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) {
1310Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
1320Sstevel@tonic-gate 		return (-1);
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	pid = getpid();
1360Sstevel@tonic-gate 	memset(&hints, 0, sizeof (hints));
1370Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
1380Sstevel@tonic-gate 	hints.ai_flags = AI_CANONNAME;
1390Sstevel@tonic-gate 	if (af == AF_INET6) {
1400Sstevel@tonic-gate 		hints.ai_flags |= AI_V4MAPPED;
1410Sstevel@tonic-gate 		hints.ai_family = AF_UNSPEC;
1420Sstevel@tonic-gate 	} else {
1430Sstevel@tonic-gate 		hints.ai_family = af;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	(void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport));
1460Sstevel@tonic-gate 	rc = getaddrinfo(*ahost, aport, &hints, &res);
1470Sstevel@tonic-gate 	if (rc != 0) {
1480Sstevel@tonic-gate 		(void) fprintf(stderr,
1496812Sraf 		    dgettext(TEXT_DOMAIN, "%s: unknown host%s\n"),
1500Sstevel@tonic-gate 		    *ahost, rc == EAI_AGAIN ? " (try again later)" : "");
1510Sstevel@tonic-gate 		return (-1);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 	resp = res;
1540Sstevel@tonic-gate 	(void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN);
1550Sstevel@tonic-gate 	*ahost = hostname;
1560Sstevel@tonic-gate #ifdef SYSV
1570Sstevel@tonic-gate 	/* ignore SIGPIPE */
1580Sstevel@tonic-gate 	bzero((char *)&newaction, sizeof (newaction));
1590Sstevel@tonic-gate 	newaction.sa_handler = SIG_IGN;
1606812Sraf 	(void) sigaction(SIGPIPE, &newaction, &oldaction);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/* block SIGURG */
1630Sstevel@tonic-gate 	bzero((char *)&newmask, sizeof (newmask));
1646812Sraf 	(void) sigaddset(&newmask, SIGURG);
1656812Sraf 	(void) sigprocmask(SIG_BLOCK, &newmask, &oldmask);
1660Sstevel@tonic-gate #else
1670Sstevel@tonic-gate 	oldmask = _sigblock(sigmask(SIGURG));
1680Sstevel@tonic-gate #endif /* SYSV */
1690Sstevel@tonic-gate 	for (;;) {
1700Sstevel@tonic-gate 		s = rresvport_af(&lport, res->ai_family);
1710Sstevel@tonic-gate 		if (s < 0) {
1720Sstevel@tonic-gate 			int af = res->ai_family;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 			/*
1750Sstevel@tonic-gate 			 * See if we have any addresses of a different type
1760Sstevel@tonic-gate 			 * to try.
1770Sstevel@tonic-gate 			 */
1780Sstevel@tonic-gate 			while (res != NULL && res->ai_family == af)
1790Sstevel@tonic-gate 				res = res->ai_next;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 			if (res != NULL)
1820Sstevel@tonic-gate 				continue;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 			if (errno == EAGAIN)
1850Sstevel@tonic-gate 				(void) fprintf(stderr,
1866812Sraf 				    dgettext(TEXT_DOMAIN,
1870Sstevel@tonic-gate 				    "socket: All ports in use\n"));
1880Sstevel@tonic-gate 			else
1890Sstevel@tonic-gate 				perror("rcmd: socket");
1900Sstevel@tonic-gate #ifdef SYSV
1910Sstevel@tonic-gate 			/* restore original SIGPIPE handler */
1926812Sraf 			(void) sigaction(SIGPIPE, &oldaction,
1930Sstevel@tonic-gate 			    (struct sigaction *)0);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 			/* restore original signal mask */
1966812Sraf 			(void) sigprocmask(SIG_SETMASK, &oldmask,
1970Sstevel@tonic-gate 			    (sigset_t *)0);
1980Sstevel@tonic-gate #else
1990Sstevel@tonic-gate 			sigsetmask(oldmask);
2000Sstevel@tonic-gate #endif /* SYSV */
2010Sstevel@tonic-gate 			freeaddrinfo(resp);
2020Sstevel@tonic-gate 			return (-1);
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		bzero((char *)&caddr, sizeof (caddr));
2050Sstevel@tonic-gate 		bcopy(res->ai_addr, &caddr, res->ai_addrlen);
2060Sstevel@tonic-gate 		addrlen = res->ai_addrlen;
2070Sstevel@tonic-gate 		if (af == AF_INET6 && res->ai_addr->sa_family == AF_INET) {
2080Sstevel@tonic-gate 			struct in6_addr ia6;
2090Sstevel@tonic-gate 			struct sockaddr_in6 *in6addr;
2100Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&((struct sockaddr_in *)
2110Sstevel@tonic-gate 			    res->ai_addr)->sin_addr, &ia6);
2120Sstevel@tonic-gate 			in6addr = (struct sockaddr_in6 *)&caddr;
2130Sstevel@tonic-gate 			in6addr->sin6_addr = ia6;
2140Sstevel@tonic-gate 			in6addr->sin6_family = AF_INET6;
2150Sstevel@tonic-gate 			addrlen = sizeof (struct sockaddr_in6);
2160Sstevel@tonic-gate 		}
2176812Sraf 		(void) fcntl(s, F_SETOWN, pid);
2180Sstevel@tonic-gate 		if (connect(s, (struct sockaddr *)&caddr, addrlen) >= 0)
2190Sstevel@tonic-gate 			break;
2200Sstevel@tonic-gate 		(void) close(s);
2210Sstevel@tonic-gate 		if (errno == EADDRINUSE) {
2220Sstevel@tonic-gate 			lport = 0;
2230Sstevel@tonic-gate 			continue;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 		if (errno == ECONNREFUSED && timo <= 16) {
2260Sstevel@tonic-gate 			(void) sleep(timo);
2270Sstevel@tonic-gate 			timo *= 2;
2280Sstevel@tonic-gate 			continue;
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 		if (res->ai_next != NULL) {
2310Sstevel@tonic-gate 			int oerrno = errno;
2320Sstevel@tonic-gate 			if (res->ai_addr->sa_family == AF_INET6)
2330Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in6 *)
2340Sstevel@tonic-gate 				    res->ai_addr)->sin6_addr;
2350Sstevel@tonic-gate 			else
2360Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in *)
2370Sstevel@tonic-gate 				    res->ai_addr)->sin_addr;
2380Sstevel@tonic-gate 			(void) fprintf(stderr,
2396812Sraf 			    dgettext(TEXT_DOMAIN, "connect to address %s: "),
2400Sstevel@tonic-gate 			    inet_ntop(res->ai_addr->sa_family, addr,
2410Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
2420Sstevel@tonic-gate 			errno = oerrno;
2430Sstevel@tonic-gate 			perror(0);
2440Sstevel@tonic-gate 			res = res->ai_next;
2450Sstevel@tonic-gate 			if (res->ai_addr->sa_family == AF_INET6)
2460Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in6 *)
2470Sstevel@tonic-gate 				    res->ai_addr)->sin6_addr;
2480Sstevel@tonic-gate 			else
2490Sstevel@tonic-gate 				addr = (char *)&((struct sockaddr_in *)
2500Sstevel@tonic-gate 				    res->ai_addr)->sin_addr;
2510Sstevel@tonic-gate 			(void) fprintf(stderr,
2526812Sraf 			    dgettext(TEXT_DOMAIN, "Trying %s...\n"),
2530Sstevel@tonic-gate 			    inet_ntop(res->ai_addr->sa_family, addr,
2540Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
2550Sstevel@tonic-gate 			continue;
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 		perror(*ahost);
2580Sstevel@tonic-gate 		freeaddrinfo(resp);
2590Sstevel@tonic-gate #ifdef SYSV
2600Sstevel@tonic-gate 		/* restore original SIGPIPE handler */
2616812Sraf 		(void) sigaction(SIGPIPE, &oldaction,
2620Sstevel@tonic-gate 		    (struct sigaction *)0);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		/* restore original signal mask */
2656812Sraf 		(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
2660Sstevel@tonic-gate #else
2670Sstevel@tonic-gate 		sigsetmask(oldmask);
2680Sstevel@tonic-gate #endif /* SYSV */
2690Sstevel@tonic-gate 		return (-1);
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 	lport = 0;
2720Sstevel@tonic-gate 	if (fd2p == 0) {
2730Sstevel@tonic-gate 		(void) write(s, "", 1);
2740Sstevel@tonic-gate 	} else {
2750Sstevel@tonic-gate 		int s2 = rresvport_af(&lport, res->ai_family), s3;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		len = (socklen_t)sizeof (faddr);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		if (s2 < 0)
2800Sstevel@tonic-gate 			goto bad;
2810Sstevel@tonic-gate 		(void) listen(s2, 1);
2820Sstevel@tonic-gate 		(void) snprintf(aport, MAX_SHORTSTRLEN, "%d", lport);
2830Sstevel@tonic-gate 		if (write(s, aport, strlen(aport)+1) != strlen(aport)+1) {
2846812Sraf 			perror(dgettext(TEXT_DOMAIN,
2850Sstevel@tonic-gate 			    "write: setting up stderr"));
2860Sstevel@tonic-gate 			(void) close(s2);
2870Sstevel@tonic-gate 			goto bad;
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 		FD_ZERO(&fdset);
2900Sstevel@tonic-gate 		FD_SET(s, &fdset);
2910Sstevel@tonic-gate 		FD_SET(s2, &fdset);
2920Sstevel@tonic-gate 		while ((selret = select(FD_SETSIZE, &fdset, (fd_set *)0,
2930Sstevel@tonic-gate 		    (fd_set *)0, (struct timeval *)0)) > 0) {
2940Sstevel@tonic-gate 			if (FD_ISSET(s, &fdset)) {
2950Sstevel@tonic-gate 				/*
2960Sstevel@tonic-gate 				 *	Something's wrong:  we should get no
2970Sstevel@tonic-gate 				 *	data on this connection at this point,
2980Sstevel@tonic-gate 				 *	so we assume that the connection has
2990Sstevel@tonic-gate 				 *	gone away.
3000Sstevel@tonic-gate 				 */
3010Sstevel@tonic-gate 				(void) close(s2);
3020Sstevel@tonic-gate 				goto bad;
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 			if (FD_ISSET(s2, &fdset)) {
3050Sstevel@tonic-gate 				/*
3060Sstevel@tonic-gate 				 *	We assume this is an incoming connect
3070Sstevel@tonic-gate 				 *	request and proceed normally.
3080Sstevel@tonic-gate 				 */
3090Sstevel@tonic-gate 				s3 = accept(s2, (struct sockaddr *)&faddr,
3100Sstevel@tonic-gate 				    &len);
3110Sstevel@tonic-gate 				FD_CLR(s2, &fdset);
3120Sstevel@tonic-gate 				(void) close(s2);
3130Sstevel@tonic-gate 				if (s3 < 0) {
3140Sstevel@tonic-gate 					perror("accept");
3150Sstevel@tonic-gate 					lport = 0;
3160Sstevel@tonic-gate 					goto bad;
3170Sstevel@tonic-gate 				}
3180Sstevel@tonic-gate 				else
3190Sstevel@tonic-gate 					break;
3200Sstevel@tonic-gate 			}
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 		if (selret == -1) {
3230Sstevel@tonic-gate 			/*
3240Sstevel@tonic-gate 			 *	This should not happen, and we treat it as
3250Sstevel@tonic-gate 			 *	a fatal error.
3260Sstevel@tonic-gate 			 */
3270Sstevel@tonic-gate 			(void) close(s2);
3280Sstevel@tonic-gate 			goto bad;
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		*fd2p = s3;
3320Sstevel@tonic-gate 		switch (faddr.ss_family) {
3330Sstevel@tonic-gate 		case AF_INET:
3340Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&faddr;
3350Sstevel@tonic-gate 			if (ntohs(sin->sin_port) >= IPPORT_RESERVED) {
3360Sstevel@tonic-gate 				(void) fprintf(stderr,
3376812Sraf 				    dgettext(TEXT_DOMAIN,
3386812Sraf 				    "socket: protocol failure in circuit "
3396812Sraf 				    "setup.\n"));
3400Sstevel@tonic-gate 				goto bad2;
3410Sstevel@tonic-gate 			}
3420Sstevel@tonic-gate 			break;
3430Sstevel@tonic-gate 		case AF_INET6:
3440Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&faddr;
3450Sstevel@tonic-gate 			if (ntohs(sin6->sin6_port) >= IPPORT_RESERVED) {
3460Sstevel@tonic-gate 				(void) fprintf(stderr,
3476812Sraf 				    dgettext(TEXT_DOMAIN,
3486812Sraf 				    "socket: protocol failure in circuit "
3496812Sraf 				    "setup.\n"));
3500Sstevel@tonic-gate 				goto bad2;
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 			break;
3530Sstevel@tonic-gate 		default:
3540Sstevel@tonic-gate 			(void) fprintf(stderr,
3556812Sraf 			    dgettext(TEXT_DOMAIN,
3560Sstevel@tonic-gate 			    "socket: protocol failure in circuit setup.\n"));
3570Sstevel@tonic-gate 			goto bad2;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	(void) write(s, locuser, strlen(locuser)+1);
3610Sstevel@tonic-gate 	(void) write(s, remuser, strlen(remuser)+1);
3620Sstevel@tonic-gate 	(void) write(s, cmd, strlen(cmd)+1);
3630Sstevel@tonic-gate 	retval = read(s, &c, 1);
3640Sstevel@tonic-gate 	if (retval != 1) {
3650Sstevel@tonic-gate 		if (retval == 0) {
3660Sstevel@tonic-gate 			(void) fprintf(stderr,
3676812Sraf 			    dgettext(TEXT_DOMAIN,
3680Sstevel@tonic-gate 			    "Protocol error, %s closed connection\n"),
3690Sstevel@tonic-gate 			    *ahost);
3700Sstevel@tonic-gate 		} else if (retval < 0) {
3710Sstevel@tonic-gate 			perror(*ahost);
3720Sstevel@tonic-gate 		} else {
3730Sstevel@tonic-gate 			(void) fprintf(stderr,
3746812Sraf 			    dgettext(TEXT_DOMAIN,
3750Sstevel@tonic-gate 			    "Protocol error, %s sent %d bytes\n"),
3760Sstevel@tonic-gate 			    *ahost, retval);
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate 		goto bad2;
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 	if (c != 0) {
3810Sstevel@tonic-gate 		while (read(s, &c, 1) == 1) {
3820Sstevel@tonic-gate 			(void) write(2, &c, 1);
3830Sstevel@tonic-gate 			if (c == '\n')
3840Sstevel@tonic-gate 				break;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		goto bad2;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate #ifdef SYSV
3890Sstevel@tonic-gate 	/* restore original SIGPIPE handler */
3906812Sraf 	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/* restore original signal mask */
3936812Sraf 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
3940Sstevel@tonic-gate #else
3950Sstevel@tonic-gate 	sigsetmask(oldmask);
3960Sstevel@tonic-gate #endif /* SYSV */
3970Sstevel@tonic-gate 	freeaddrinfo(resp);
3980Sstevel@tonic-gate 	return (s);
3990Sstevel@tonic-gate bad2:
4000Sstevel@tonic-gate 	if (lport)
4010Sstevel@tonic-gate 		(void) close(*fd2p);
4020Sstevel@tonic-gate bad:
4030Sstevel@tonic-gate 	(void) close(s);
4040Sstevel@tonic-gate #ifdef SYSV
4050Sstevel@tonic-gate 	/* restore original SIGPIPE handler */
4066812Sraf 	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/* restore original signal mask */
4096812Sraf 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
4100Sstevel@tonic-gate #else
4110Sstevel@tonic-gate 	sigsetmask(oldmask);
4120Sstevel@tonic-gate #endif /* SYSV */
4130Sstevel@tonic-gate 	freeaddrinfo(resp);
4140Sstevel@tonic-gate 	return (-1);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate static int
_rresvport_addr(int * alport,struct sockaddr_storage * addr)4180Sstevel@tonic-gate _rresvport_addr(int *alport, struct sockaddr_storage *addr)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate 	struct sockaddr_in *sin;
4210Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
4220Sstevel@tonic-gate 	int s;
4230Sstevel@tonic-gate 	socklen_t len;
4240Sstevel@tonic-gate 	int on = 1;
4250Sstevel@tonic-gate 	int off = 0;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (addr->ss_family == AF_INET) {
4280Sstevel@tonic-gate 		sin = (struct sockaddr_in *)addr;
4290Sstevel@tonic-gate 		len = sizeof (struct sockaddr_in);
4300Sstevel@tonic-gate 	} else if (addr->ss_family == AF_INET6) {
4310Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)addr;
4320Sstevel@tonic-gate 		len = sizeof (struct sockaddr_in6);
4330Sstevel@tonic-gate 	} else {
4340Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
4350Sstevel@tonic-gate 		return (-1);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 	s = socket(addr->ss_family, SOCK_STREAM, 0);
4380Sstevel@tonic-gate 	if (s < 0)
4390Sstevel@tonic-gate 		return (-1);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/*
4422429Skcpoon 	 * Set SO_EXCLBIND to get a "unique" port, which is not bound
4430Sstevel@tonic-gate 	 * to any other sockets.
4440Sstevel@tonic-gate 	 */
4452429Skcpoon 	if (setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &on, sizeof (on)) < 0) {
4460Sstevel@tonic-gate 		(void) close(s);
4470Sstevel@tonic-gate 		return (-1);
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/* Try to bind() to the given port first. */
4510Sstevel@tonic-gate 	if (*alport != 0) {
4520Sstevel@tonic-gate 		if (addr->ss_family == AF_INET) {
4530Sstevel@tonic-gate 			sin->sin_port = htons((ushort_t)*alport);
4540Sstevel@tonic-gate 		} else {
4550Sstevel@tonic-gate 			sin6->sin6_port = htons((ushort_t)*alport);
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		if (bind(s, (struct sockaddr *)addr, len) >= 0) {
4582429Skcpoon 			/* To be safe, need to turn off SO_EXCLBIND. */
4592429Skcpoon 			(void) setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &off,
4600Sstevel@tonic-gate 			    sizeof (off));
4610Sstevel@tonic-gate 			return (s);
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 		if (errno != EADDRINUSE) {
4640Sstevel@tonic-gate 			(void) close(s);
4650Sstevel@tonic-gate 			return (-1);
4660Sstevel@tonic-gate 		}
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/*
4700Sstevel@tonic-gate 	 * If no port is given or the above bind() does not succeed, set
4710Sstevel@tonic-gate 	 * TCP_ANONPRIVBIND option to ask the kernel to pick a port in the
4720Sstevel@tonic-gate 	 * priviledged range for us.
4730Sstevel@tonic-gate 	 */
4740Sstevel@tonic-gate 	if (setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &on,
4750Sstevel@tonic-gate 	    sizeof (on)) < 0) {
4760Sstevel@tonic-gate 		(void) close(s);
4770Sstevel@tonic-gate 		return (-1);
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 	if (addr->ss_family == AF_INET) {
4800Sstevel@tonic-gate 		sin->sin_port = 0;
4810Sstevel@tonic-gate 	} else {
4820Sstevel@tonic-gate 		sin6->sin6_port = 0;
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 	if (bind(s, (struct sockaddr *)addr, len) >= 0) {
4850Sstevel@tonic-gate 		/*
4860Sstevel@tonic-gate 		 * We need to tell the caller what the port is.
4870Sstevel@tonic-gate 		 */
4880Sstevel@tonic-gate 		if (getsockname(s, (struct sockaddr *)addr, &len) < 0) {
4890Sstevel@tonic-gate 			(void) close(s);
4900Sstevel@tonic-gate 			return (-1);
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 		switch (addr->ss_family) {
4930Sstevel@tonic-gate 		case AF_INET6:
4940Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addr;
4950Sstevel@tonic-gate 			*alport = ntohs(sin6->sin6_port);
4960Sstevel@tonic-gate 			break;
4970Sstevel@tonic-gate 		case AF_INET:
4980Sstevel@tonic-gate 			sin = (struct sockaddr_in *)addr;
4990Sstevel@tonic-gate 			*alport = ntohs(sin->sin_port);
5000Sstevel@tonic-gate 			break;
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		/*
5040Sstevel@tonic-gate 		 * To be safe, always turn off these options when we are done.
5050Sstevel@tonic-gate 		 */
5060Sstevel@tonic-gate 		(void) setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &off,
5070Sstevel@tonic-gate 		    sizeof (off));
5082429Skcpoon 		(void) setsockopt(s, SOL_SOCKET, SO_EXCLBIND, &off,
5090Sstevel@tonic-gate 		    sizeof (off));
5100Sstevel@tonic-gate 		return (s);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	(void) close(s);
5130Sstevel@tonic-gate 	return (-1);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate int
rresvport_addr(int * alport,struct sockaddr_storage * addr)5170Sstevel@tonic-gate rresvport_addr(int *alport, struct sockaddr_storage *addr)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	int res, err;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_ON);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	res = _rresvport_addr(alport, addr);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	err = errno;
5260Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_OFF);
5270Sstevel@tonic-gate 	errno = err;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	return (res);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate int
rresvport_af(int * alport,int af)5330Sstevel@tonic-gate rresvport_af(int *alport, int af)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	struct sockaddr_storage laddr;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	bzero(&laddr, sizeof (laddr));
5380Sstevel@tonic-gate 	if (af == AF_INET || af == AF_INET6) {
5390Sstevel@tonic-gate 		laddr.ss_family = (sa_family_t)af;
5400Sstevel@tonic-gate 	} else {
5410Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
5420Sstevel@tonic-gate 		return (-1);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	return (rresvport_addr(alport, &laddr));
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate int
rresvport(int * alport)5480Sstevel@tonic-gate rresvport(int *alport)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	return (rresvport_af(alport, AF_INET));
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate int
ruserok(const char * rhost,int superuser,const char * ruser,const char * luser)5540Sstevel@tonic-gate ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	FILE *hostf;
5570Sstevel@tonic-gate 	char fhost[MAXHOSTNAMELEN];
5580Sstevel@tonic-gate 	const char *sp;
5590Sstevel@tonic-gate 	char *p;
5600Sstevel@tonic-gate 	int baselen = -1;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	struct stat64 sbuf;
5630Sstevel@tonic-gate 	struct passwd *pwd;
5640Sstevel@tonic-gate 	char pbuf[MAXPATHLEN];
5650Sstevel@tonic-gate 	uid_t uid = (uid_t)-1;
5660Sstevel@tonic-gate 	gid_t gid = (gid_t)-1;
567*11134SCasper.Dik@Sun.COM 	int maxgrp = getgroups(0, NULL);
568*11134SCasper.Dik@Sun.COM 	gid_t *grouplist = alloca(maxgrp * sizeof (gid_t));
5690Sstevel@tonic-gate 	int ngroups;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	sp = rhost;
5720Sstevel@tonic-gate 	p = fhost;
5730Sstevel@tonic-gate 	while (*sp) {
5740Sstevel@tonic-gate 		if (*sp == '.') {
5750Sstevel@tonic-gate 			if (baselen == -1)
5760Sstevel@tonic-gate 				baselen = (int)(sp - rhost);
5770Sstevel@tonic-gate 			*p++ = *sp++;
5780Sstevel@tonic-gate 		} else {
5790Sstevel@tonic-gate 			*p++ = isupper(*sp) ? tolower(*sp++) : *sp++;
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 	*p = '\0';
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/* check /etc/hosts.equiv */
5850Sstevel@tonic-gate 	if (!superuser) {
5861914Scasper 		if ((hostf = fopen("/etc/hosts.equiv", "rF")) != NULL) {
5870Sstevel@tonic-gate 			if (!_validuser(hostf, fhost, luser, ruser, baselen)) {
5880Sstevel@tonic-gate 				(void) fclose(hostf);
5890Sstevel@tonic-gate 				return (0);
5900Sstevel@tonic-gate 			}
5910Sstevel@tonic-gate 			(void) fclose(hostf);
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	/* check ~/.rhosts */
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if ((pwd = getpwnam(luser)) == NULL)
5980Sstevel@tonic-gate 		return (-1);
5990Sstevel@tonic-gate 	(void) strcpy(pbuf, pwd->pw_dir);
6000Sstevel@tonic-gate 	(void) strcat(pbuf, "/.rhosts");
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * Read .rhosts as the local user to avoid NFS mapping the root uid
6040Sstevel@tonic-gate 	 * to something that can't read .rhosts.
6050Sstevel@tonic-gate 	 */
6060Sstevel@tonic-gate 	gid = getegid();
6070Sstevel@tonic-gate 	uid = geteuid();
608*11134SCasper.Dik@Sun.COM 	if ((ngroups = getgroups(maxgrp, grouplist)) == -1)
6090Sstevel@tonic-gate 		return (-1);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	(void) setegid(pwd->pw_gid);
6120Sstevel@tonic-gate 	initgroups(pwd->pw_name, pwd->pw_gid);
6130Sstevel@tonic-gate 	(void) seteuid(pwd->pw_uid);
6141914Scasper 	if ((hostf = fopen(pbuf, "rF")) == NULL) {
6150Sstevel@tonic-gate 		if (gid != (gid_t)-1)
6160Sstevel@tonic-gate 			(void) setegid(gid);
6170Sstevel@tonic-gate 		if (uid != (uid_t)-1)
6180Sstevel@tonic-gate 			(void) seteuid(uid);
6190Sstevel@tonic-gate 		setgroups(ngroups, grouplist);
6200Sstevel@tonic-gate 		return (-1);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 	(void) fstat64(fileno(hostf), &sbuf);
6230Sstevel@tonic-gate 	if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) {
6240Sstevel@tonic-gate 		(void) fclose(hostf);
6250Sstevel@tonic-gate 		if (gid != (gid_t)-1)
6260Sstevel@tonic-gate 			(void) setegid(gid);
6270Sstevel@tonic-gate 		if (uid != (uid_t)-1)
6280Sstevel@tonic-gate 			(void) seteuid(uid);
6290Sstevel@tonic-gate 		setgroups(ngroups, grouplist);
6300Sstevel@tonic-gate 		return (-1);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (!_validuser(hostf, fhost, luser, ruser, baselen)) {
6340Sstevel@tonic-gate 		(void) fclose(hostf);
6350Sstevel@tonic-gate 		if (gid != (gid_t)-1)
6360Sstevel@tonic-gate 			(void) setegid(gid);
6370Sstevel@tonic-gate 		if (uid != (uid_t)-1)
6380Sstevel@tonic-gate 			(void) seteuid(uid);
6390Sstevel@tonic-gate 		setgroups(ngroups, grouplist);
6400Sstevel@tonic-gate 		return (0);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	(void) fclose(hostf);
6440Sstevel@tonic-gate 	if (gid != (gid_t)-1)
6450Sstevel@tonic-gate 		(void) setegid(gid);
6460Sstevel@tonic-gate 	if (uid != (uid_t)-1)
6470Sstevel@tonic-gate 		(void) seteuid(uid);
6480Sstevel@tonic-gate 	setgroups(ngroups, grouplist);
6490Sstevel@tonic-gate 	return (-1);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate static int
_validuser(FILE * hostf,char * rhost,const char * luser,const char * ruser,int baselen)6530Sstevel@tonic-gate _validuser(FILE *hostf, char *rhost, const char *luser,
6540Sstevel@tonic-gate     const char *ruser, int baselen)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	char *user;
6570Sstevel@tonic-gate 	char ahost[BUFSIZ];
6580Sstevel@tonic-gate 	char *uchost = (char *)NULL;
6590Sstevel@tonic-gate 	int hostmatch, usermatch;
6600Sstevel@tonic-gate 	char *p;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate #ifdef NIS
6630Sstevel@tonic-gate 	if (domain == NULL) {
6640Sstevel@tonic-gate 		(void) usingypmap(&domain, NULL);
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate #endif /* NIS */
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	while (fgets(ahost, (int)sizeof (ahost), hostf)) {
6690Sstevel@tonic-gate 		uchost = (char *)NULL;
6700Sstevel@tonic-gate 		hostmatch = usermatch = 0;
6710Sstevel@tonic-gate 		p = ahost;
6720Sstevel@tonic-gate 		/*
6730Sstevel@tonic-gate 		 * We can get a line bigger than our buffer.  If so we skip
6740Sstevel@tonic-gate 		 * the offending line.
6750Sstevel@tonic-gate 		 */
6760Sstevel@tonic-gate 		if (strchr(p, '\n') == NULL) {
6770Sstevel@tonic-gate 			while (fgets(ahost, (int)sizeof (ahost), hostf) &&
6780Sstevel@tonic-gate 			    strchr(ahost, '\n') == NULL)
6790Sstevel@tonic-gate 				;
6800Sstevel@tonic-gate 			continue;
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
6830Sstevel@tonic-gate 			/*
6840Sstevel@tonic-gate 			 *	Both host and user ``names'' can be netgroups,
6850Sstevel@tonic-gate 			 *	and must have their case preserved.  Case is
6860Sstevel@tonic-gate 			 *	preserved for user names because we break out
6870Sstevel@tonic-gate 			 *	of this loop when finding a field separator.
6880Sstevel@tonic-gate 			 *	To do so for host names, we must make a copy of
6890Sstevel@tonic-gate 			 *	the host name field.
6900Sstevel@tonic-gate 			 */
6910Sstevel@tonic-gate 			if (isupper(*p)) {
6920Sstevel@tonic-gate 				if (uchost == (char *)NULL)
6930Sstevel@tonic-gate 					uchost = strdup(ahost);
6940Sstevel@tonic-gate 				*p = tolower(*p);
6950Sstevel@tonic-gate 			}
6960Sstevel@tonic-gate 			p++;
6970Sstevel@tonic-gate 		}
6980Sstevel@tonic-gate 		if (*p != '\0' && uchost != (char *)NULL)
6990Sstevel@tonic-gate 			uchost[p - ahost] = '\0';
7000Sstevel@tonic-gate 		if (*p == ' ' || *p == '\t') {
7010Sstevel@tonic-gate 			*p++ = '\0';
7020Sstevel@tonic-gate 			while (*p == ' ' || *p == '\t')
7030Sstevel@tonic-gate 				p++;
7040Sstevel@tonic-gate 			user = p;
7050Sstevel@tonic-gate 			while (*p != '\n' && *p != ' ' && *p != '\t' &&
7066812Sraf 			    *p != '\0')
7070Sstevel@tonic-gate 				p++;
7080Sstevel@tonic-gate 		} else
7090Sstevel@tonic-gate 			user = p;
7100Sstevel@tonic-gate 		*p = '\0';
7110Sstevel@tonic-gate 		if (ahost[0] == '+' && ahost[1] == 0)
7120Sstevel@tonic-gate 			hostmatch = 1;
7130Sstevel@tonic-gate #ifdef NIS
7140Sstevel@tonic-gate 		else if (ahost[0] == '+' && ahost[1] == '@')
7150Sstevel@tonic-gate 			if (uchost != (char *)NULL)
7160Sstevel@tonic-gate 				hostmatch = innetgr(uchost + 2, rhost,
7170Sstevel@tonic-gate 				    NULL, domain);
7180Sstevel@tonic-gate 			else
7190Sstevel@tonic-gate 				hostmatch = innetgr(ahost + 2, rhost,
7200Sstevel@tonic-gate 				    NULL, domain);
7210Sstevel@tonic-gate 		else if (ahost[0] == '-' && ahost[1] == '@') {
7220Sstevel@tonic-gate 			if (uchost != (char *)NULL) {
7230Sstevel@tonic-gate 				if (innetgr(uchost + 2, rhost, NULL, domain))
7240Sstevel@tonic-gate 					break;
7250Sstevel@tonic-gate 			} else {
7260Sstevel@tonic-gate 				if (innetgr(ahost + 2, rhost, NULL, domain))
7270Sstevel@tonic-gate 					break;
7280Sstevel@tonic-gate 			}
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate #endif /* NIS */
7310Sstevel@tonic-gate 		else if (ahost[0] == '-') {
7320Sstevel@tonic-gate 			if (_checkhost(rhost, ahost+1, baselen))
7330Sstevel@tonic-gate 				break;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 		else
7360Sstevel@tonic-gate 			hostmatch = _checkhost(rhost, ahost, baselen);
7370Sstevel@tonic-gate 		if (user[0]) {
7380Sstevel@tonic-gate 			if (user[0] == '+' && user[1] == 0)
7390Sstevel@tonic-gate 				usermatch = 1;
7400Sstevel@tonic-gate #ifdef NIS
7410Sstevel@tonic-gate 			else if (user[0] == '+' && user[1] == '@')
7420Sstevel@tonic-gate 				usermatch = innetgr(user+2, NULL,
7436812Sraf 				    ruser, domain);
7440Sstevel@tonic-gate 			else if (user[0] == '-' && user[1] == '@') {
7450Sstevel@tonic-gate 				if (hostmatch &&
7460Sstevel@tonic-gate 				    innetgr(user+2, NULL, ruser, domain))
7470Sstevel@tonic-gate 					break;
7480Sstevel@tonic-gate 			}
7490Sstevel@tonic-gate #endif /* NIS */
7500Sstevel@tonic-gate 			else if (user[0] == '-') {
7510Sstevel@tonic-gate 				if (hostmatch && (strcmp(user+1, ruser) == 0))
7520Sstevel@tonic-gate 					break;
7530Sstevel@tonic-gate 			}
7540Sstevel@tonic-gate 			else
7550Sstevel@tonic-gate 				usermatch = (strcmp(user, ruser) == 0);
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 		else
7580Sstevel@tonic-gate 			usermatch = (strcmp(ruser, luser) == 0);
7590Sstevel@tonic-gate 		if (uchost != (char *)NULL)
7600Sstevel@tonic-gate 			free(uchost);
7610Sstevel@tonic-gate 		if (hostmatch && usermatch)
7620Sstevel@tonic-gate 			return (0);
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if (uchost != (char *)NULL)
7660Sstevel@tonic-gate 		free(uchost);
7670Sstevel@tonic-gate 	return (-1);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate static int
_checkhost(char * rhost,char * lhost,int len)7710Sstevel@tonic-gate _checkhost(char *rhost, char *lhost, int len)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	static char *ldomain;
7740Sstevel@tonic-gate 	static char *domainp;
7750Sstevel@tonic-gate 	static int nodomain;
7760Sstevel@tonic-gate 	char *cp;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	if (ldomain == NULL) {
7790Sstevel@tonic-gate 		ldomain = (char *)malloc(MAXHOSTNAMELEN+1);
7800Sstevel@tonic-gate 		if (ldomain == 0)
7810Sstevel@tonic-gate 			return (0);
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if (len == -1)
7850Sstevel@tonic-gate 		return (strcmp(rhost, lhost) == 0);
7860Sstevel@tonic-gate 	if (strncmp(rhost, lhost, len))
7870Sstevel@tonic-gate 		return (0);
7880Sstevel@tonic-gate 	if (strcmp(rhost, lhost) == 0)
7890Sstevel@tonic-gate 		return (1);
7900Sstevel@tonic-gate 	if (*(lhost + len) != '\0')
7910Sstevel@tonic-gate 		return (0);
7920Sstevel@tonic-gate 	if (nodomain)
7930Sstevel@tonic-gate 		return (0);
7940Sstevel@tonic-gate 	if (!domainp) {
7950Sstevel@tonic-gate 		/*
7960Sstevel@tonic-gate 		 * "domainp" points after the first dot in the host name
7970Sstevel@tonic-gate 		 */
7980Sstevel@tonic-gate 		if (gethostname(ldomain, MAXHOSTNAMELEN) == -1) {
7990Sstevel@tonic-gate 			nodomain = 1;
8000Sstevel@tonic-gate 			return (0);
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 		ldomain[MAXHOSTNAMELEN] = NULL;
8030Sstevel@tonic-gate 		if ((domainp = index(ldomain, '.')) == (char *)NULL) {
8040Sstevel@tonic-gate 			nodomain = 1;
8050Sstevel@tonic-gate 			return (0);
8060Sstevel@tonic-gate 		}
8070Sstevel@tonic-gate 		domainp++;
8080Sstevel@tonic-gate 		cp = domainp;
8090Sstevel@tonic-gate 		while (*cp) {
8100Sstevel@tonic-gate 			*cp = isupper(*cp) ? tolower(*cp) : *cp;
8110Sstevel@tonic-gate 			cp++;
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 	return (strcmp(domainp, rhost + len + 1) == 0);
8150Sstevel@tonic-gate }
816