xref: /freebsd/lib/libc/net/rcmd.c (revision 3fb85bff81d42060b798618ff4e5eb0212148f68)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1983, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
323fb85bffSAndrey A. Chernov  *
333fb85bffSAndrey A. Chernov  * $FreeBSD$
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3758f0484fSRodney W. Grimes static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #include <sys/param.h>
4158f0484fSRodney W. Grimes #include <sys/socket.h>
4258f0484fSRodney W. Grimes #include <sys/stat.h>
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes #include <netinet/in.h>
4558f0484fSRodney W. Grimes #include <arpa/inet.h>
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #include <signal.h>
4858f0484fSRodney W. Grimes #include <fcntl.h>
4958f0484fSRodney W. Grimes #include <netdb.h>
5058f0484fSRodney W. Grimes #include <unistd.h>
5158f0484fSRodney W. Grimes #include <pwd.h>
5258f0484fSRodney W. Grimes #include <errno.h>
5358f0484fSRodney W. Grimes #include <stdio.h>
5458f0484fSRodney W. Grimes #include <ctype.h>
5558f0484fSRodney W. Grimes #include <string.h>
561e890b05SBill Paul #ifdef YP
571e890b05SBill Paul #include <rpc/rpc.h>
581e890b05SBill Paul #include <rpcsvc/yp_prot.h>
591e890b05SBill Paul #include <rpcsvc/ypclnt.h>
601e890b05SBill Paul #endif
6158f0484fSRodney W. Grimes 
6251295a4dSJordan K. Hubbard extern int innetgr __P(( const char *, const char *, const char *, const char * ));
6351295a4dSJordan K. Hubbard 
64d1f32ba5SGeoff Rehmet #define max(a, b)	((a > b) ? a : b)
65d1f32ba5SGeoff Rehmet 
66e0ce825eSDoug Rabson int	__ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
67e0ce825eSDoug Rabson static int __icheckhost __P((u_int32_t, char *));
6858f0484fSRodney W. Grimes 
6958f0484fSRodney W. Grimes int
7058f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
7158f0484fSRodney W. Grimes 	char **ahost;
7258f0484fSRodney W. Grimes 	u_short rport;
7358f0484fSRodney W. Grimes 	const char *locuser, *remuser, *cmd;
7458f0484fSRodney W. Grimes 	int *fd2p;
7558f0484fSRodney W. Grimes {
7658f0484fSRodney W. Grimes 	struct hostent *hp;
7758f0484fSRodney W. Grimes 	struct sockaddr_in sin, from;
7858f0484fSRodney W. Grimes 	fd_set reads;
7958f0484fSRodney W. Grimes 	long oldmask;
8058f0484fSRodney W. Grimes 	pid_t pid;
8158f0484fSRodney W. Grimes 	int s, lport, timo;
8258f0484fSRodney W. Grimes 	char c;
8358f0484fSRodney W. Grimes 
8458f0484fSRodney W. Grimes 	pid = getpid();
8558f0484fSRodney W. Grimes 	hp = gethostbyname(*ahost);
8658f0484fSRodney W. Grimes 	if (hp == NULL) {
8758f0484fSRodney W. Grimes 		herror(*ahost);
8858f0484fSRodney W. Grimes 		return (-1);
8958f0484fSRodney W. Grimes 	}
9058f0484fSRodney W. Grimes 	*ahost = hp->h_name;
9158f0484fSRodney W. Grimes 	oldmask = sigblock(sigmask(SIGURG));
9258f0484fSRodney W. Grimes 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
9358f0484fSRodney W. Grimes 		s = rresvport(&lport);
9458f0484fSRodney W. Grimes 		if (s < 0) {
9558f0484fSRodney W. Grimes 			if (errno == EAGAIN)
9658f0484fSRodney W. Grimes 				(void)fprintf(stderr,
9758f0484fSRodney W. Grimes 				    "rcmd: socket: All ports in use\n");
9858f0484fSRodney W. Grimes 			else
9958f0484fSRodney W. Grimes 				(void)fprintf(stderr, "rcmd: socket: %s\n",
10058f0484fSRodney W. Grimes 				    strerror(errno));
10158f0484fSRodney W. Grimes 			sigsetmask(oldmask);
10258f0484fSRodney W. Grimes 			return (-1);
10358f0484fSRodney W. Grimes 		}
10458f0484fSRodney W. Grimes 		fcntl(s, F_SETOWN, pid);
105ce70b6caSPeter Wemm 		bzero(&sin, sizeof sin);
106ce70b6caSPeter Wemm 		sin.sin_len = sizeof(struct sockaddr_in);
10758f0484fSRodney W. Grimes 		sin.sin_family = hp->h_addrtype;
10858f0484fSRodney W. Grimes 		sin.sin_port = rport;
1095480d8d5SWarner Losh 		bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
11058f0484fSRodney W. Grimes 		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
11158f0484fSRodney W. Grimes 			break;
11258f0484fSRodney W. Grimes 		(void)close(s);
11358f0484fSRodney W. Grimes 		if (errno == EADDRINUSE) {
11458f0484fSRodney W. Grimes 			lport--;
11558f0484fSRodney W. Grimes 			continue;
11658f0484fSRodney W. Grimes 		}
11758f0484fSRodney W. Grimes 		if (errno == ECONNREFUSED && timo <= 16) {
11858f0484fSRodney W. Grimes 			(void)sleep(timo);
11958f0484fSRodney W. Grimes 			timo *= 2;
12058f0484fSRodney W. Grimes 			continue;
12158f0484fSRodney W. Grimes 		}
12258f0484fSRodney W. Grimes 		if (hp->h_addr_list[1] != NULL) {
12358f0484fSRodney W. Grimes 			int oerrno = errno;
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes 			(void)fprintf(stderr, "connect to address %s: ",
12658f0484fSRodney W. Grimes 			    inet_ntoa(sin.sin_addr));
12758f0484fSRodney W. Grimes 			errno = oerrno;
12858f0484fSRodney W. Grimes 			perror(0);
12958f0484fSRodney W. Grimes 			hp->h_addr_list++;
1305480d8d5SWarner Losh 			bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
13158f0484fSRodney W. Grimes 			(void)fprintf(stderr, "Trying %s...\n",
13258f0484fSRodney W. Grimes 			    inet_ntoa(sin.sin_addr));
13358f0484fSRodney W. Grimes 			continue;
13458f0484fSRodney W. Grimes 		}
13558f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
13658f0484fSRodney W. Grimes 		sigsetmask(oldmask);
13758f0484fSRodney W. Grimes 		return (-1);
13858f0484fSRodney W. Grimes 	}
13958f0484fSRodney W. Grimes 	lport--;
14058f0484fSRodney W. Grimes 	if (fd2p == 0) {
14158f0484fSRodney W. Grimes 		write(s, "", 1);
14258f0484fSRodney W. Grimes 		lport = 0;
14358f0484fSRodney W. Grimes 	} else {
14458f0484fSRodney W. Grimes 		char num[8];
14558f0484fSRodney W. Grimes 		int s2 = rresvport(&lport), s3;
14658f0484fSRodney W. Grimes 		int len = sizeof(from);
147d1f32ba5SGeoff Rehmet 		int nfds;
14858f0484fSRodney W. Grimes 
14958f0484fSRodney W. Grimes 		if (s2 < 0)
15058f0484fSRodney W. Grimes 			goto bad;
15158f0484fSRodney W. Grimes 		listen(s2, 1);
15258f0484fSRodney W. Grimes 		(void)snprintf(num, sizeof(num), "%d", lport);
15358f0484fSRodney W. Grimes 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
15458f0484fSRodney W. Grimes 			(void)fprintf(stderr,
15558f0484fSRodney W. Grimes 			    "rcmd: write (setting up stderr): %s\n",
15658f0484fSRodney W. Grimes 			    strerror(errno));
15758f0484fSRodney W. Grimes 			(void)close(s2);
15858f0484fSRodney W. Grimes 			goto bad;
15958f0484fSRodney W. Grimes 		}
160d1f32ba5SGeoff Rehmet 		nfds = max(s, s2)+1;
161d1f32ba5SGeoff Rehmet 		if(nfds > FD_SETSIZE) {
162d1f32ba5SGeoff Rehmet 			fprintf(stderr, "rcmd: too many files\n");
163d1f32ba5SGeoff Rehmet 			(void)close(s2);
164d1f32ba5SGeoff Rehmet 			goto bad;
165d1f32ba5SGeoff Rehmet 		}
166ce70b6caSPeter Wemm again:
16758f0484fSRodney W. Grimes 		FD_ZERO(&reads);
16858f0484fSRodney W. Grimes 		FD_SET(s, &reads);
16958f0484fSRodney W. Grimes 		FD_SET(s2, &reads);
17058f0484fSRodney W. Grimes 		errno = 0;
171d1f32ba5SGeoff Rehmet 		if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
17258f0484fSRodney W. Grimes 			if (errno != 0)
17358f0484fSRodney W. Grimes 				(void)fprintf(stderr,
17458f0484fSRodney W. Grimes 				    "rcmd: select (setting up stderr): %s\n",
17558f0484fSRodney W. Grimes 				    strerror(errno));
17658f0484fSRodney W. Grimes 			else
17758f0484fSRodney W. Grimes 				(void)fprintf(stderr,
17858f0484fSRodney W. Grimes 				"select: protocol failure in circuit setup\n");
17958f0484fSRodney W. Grimes 			(void)close(s2);
18058f0484fSRodney W. Grimes 			goto bad;
18158f0484fSRodney W. Grimes 		}
18258f0484fSRodney W. Grimes 		s3 = accept(s2, (struct sockaddr *)&from, &len);
183ce70b6caSPeter Wemm 		/*
184ce70b6caSPeter Wemm 		 * XXX careful for ftp bounce attacks. If discovered, shut them
185ce70b6caSPeter Wemm 		 * down and check for the real auxiliary channel to connect.
186ce70b6caSPeter Wemm 		 */
187ce70b6caSPeter Wemm 		if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
188ce70b6caSPeter Wemm 			close(s3);
189ce70b6caSPeter Wemm 			goto again;
190ce70b6caSPeter Wemm 		}
19158f0484fSRodney W. Grimes 		(void)close(s2);
19258f0484fSRodney W. Grimes 		if (s3 < 0) {
19358f0484fSRodney W. Grimes 			(void)fprintf(stderr,
19458f0484fSRodney W. Grimes 			    "rcmd: accept: %s\n", strerror(errno));
19558f0484fSRodney W. Grimes 			lport = 0;
19658f0484fSRodney W. Grimes 			goto bad;
19758f0484fSRodney W. Grimes 		}
19858f0484fSRodney W. Grimes 		*fd2p = s3;
19958f0484fSRodney W. Grimes 		from.sin_port = ntohs((u_short)from.sin_port);
20058f0484fSRodney W. Grimes 		if (from.sin_family != AF_INET ||
20158f0484fSRodney W. Grimes 		    from.sin_port >= IPPORT_RESERVED ||
20258f0484fSRodney W. Grimes 		    from.sin_port < IPPORT_RESERVED / 2) {
20358f0484fSRodney W. Grimes 			(void)fprintf(stderr,
20458f0484fSRodney W. Grimes 			    "socket: protocol failure in circuit setup.\n");
20558f0484fSRodney W. Grimes 			goto bad2;
20658f0484fSRodney W. Grimes 		}
20758f0484fSRodney W. Grimes 	}
20858f0484fSRodney W. Grimes 	(void)write(s, locuser, strlen(locuser)+1);
20958f0484fSRodney W. Grimes 	(void)write(s, remuser, strlen(remuser)+1);
21058f0484fSRodney W. Grimes 	(void)write(s, cmd, strlen(cmd)+1);
21158f0484fSRodney W. Grimes 	if (read(s, &c, 1) != 1) {
21258f0484fSRodney W. Grimes 		(void)fprintf(stderr,
21358f0484fSRodney W. Grimes 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
21458f0484fSRodney W. Grimes 		goto bad2;
21558f0484fSRodney W. Grimes 	}
21658f0484fSRodney W. Grimes 	if (c != 0) {
21758f0484fSRodney W. Grimes 		while (read(s, &c, 1) == 1) {
21858f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, &c, 1);
21958f0484fSRodney W. Grimes 			if (c == '\n')
22058f0484fSRodney W. Grimes 				break;
22158f0484fSRodney W. Grimes 		}
22258f0484fSRodney W. Grimes 		goto bad2;
22358f0484fSRodney W. Grimes 	}
22458f0484fSRodney W. Grimes 	sigsetmask(oldmask);
22558f0484fSRodney W. Grimes 	return (s);
22658f0484fSRodney W. Grimes bad2:
22758f0484fSRodney W. Grimes 	if (lport)
22858f0484fSRodney W. Grimes 		(void)close(*fd2p);
22958f0484fSRodney W. Grimes bad:
23058f0484fSRodney W. Grimes 	(void)close(s);
23158f0484fSRodney W. Grimes 	sigsetmask(oldmask);
23258f0484fSRodney W. Grimes 	return (-1);
23358f0484fSRodney W. Grimes }
23458f0484fSRodney W. Grimes 
23558f0484fSRodney W. Grimes int
23658f0484fSRodney W. Grimes rresvport(alport)
23758f0484fSRodney W. Grimes 	int *alport;
23858f0484fSRodney W. Grimes {
23958f0484fSRodney W. Grimes 	struct sockaddr_in sin;
240ce70b6caSPeter Wemm 	int s;
24158f0484fSRodney W. Grimes 
242ce70b6caSPeter Wemm 	bzero(&sin, sizeof sin);
243ce70b6caSPeter Wemm 	sin.sin_len = sizeof(struct sockaddr_in);
24458f0484fSRodney W. Grimes 	sin.sin_family = AF_INET;
24558f0484fSRodney W. Grimes 	sin.sin_addr.s_addr = INADDR_ANY;
24658f0484fSRodney W. Grimes 	s = socket(AF_INET, SOCK_STREAM, 0);
24758f0484fSRodney W. Grimes 	if (s < 0)
24858f0484fSRodney W. Grimes 		return (-1);
249ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */
250ce70b6caSPeter Wemm 	sin.sin_port = htons((u_short)*alport);
251ce70b6caSPeter Wemm 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
25258f0484fSRodney W. Grimes 		return (s);
253ce70b6caSPeter Wemm 	if (errno != EADDRINUSE) {
25458f0484fSRodney W. Grimes 		(void)close(s);
25558f0484fSRodney W. Grimes 		return (-1);
25658f0484fSRodney W. Grimes 	}
257ce70b6caSPeter Wemm #endif
258ce70b6caSPeter Wemm 	sin.sin_port = 0;
259ce70b6caSPeter Wemm 	if (bindresvport(s, &sin) == -1) {
260ce70b6caSPeter Wemm 		(void)close(s);
261ce70b6caSPeter Wemm 		return (-1);
262ce70b6caSPeter Wemm 	}
263ce70b6caSPeter Wemm 	*alport = (int)ntohs(sin.sin_port);
264ce70b6caSPeter Wemm 	return (s);
265ce70b6caSPeter Wemm }
26658f0484fSRodney W. Grimes 
26758f0484fSRodney W. Grimes int	__check_rhosts_file = 1;
26858f0484fSRodney W. Grimes char	*__rcmd_errstr;
26958f0484fSRodney W. Grimes 
27058f0484fSRodney W. Grimes int
27158f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser)
27258f0484fSRodney W. Grimes 	const char *rhost, *ruser, *luser;
27358f0484fSRodney W. Grimes 	int superuser;
27458f0484fSRodney W. Grimes {
27558f0484fSRodney W. Grimes 	struct hostent *hp;
276e0ce825eSDoug Rabson 	u_int32_t addr;
27758f0484fSRodney W. Grimes 	char **ap;
27858f0484fSRodney W. Grimes 
27958f0484fSRodney W. Grimes 	if ((hp = gethostbyname(rhost)) == NULL)
28058f0484fSRodney W. Grimes 		return (-1);
28158f0484fSRodney W. Grimes 	for (ap = hp->h_addr_list; *ap; ++ap) {
28258f0484fSRodney W. Grimes 		bcopy(*ap, &addr, sizeof(addr));
28358f0484fSRodney W. Grimes 		if (iruserok(addr, superuser, ruser, luser) == 0)
28458f0484fSRodney W. Grimes 			return (0);
28558f0484fSRodney W. Grimes 	}
28658f0484fSRodney W. Grimes 	return (-1);
28758f0484fSRodney W. Grimes }
28858f0484fSRodney W. Grimes 
28958f0484fSRodney W. Grimes /*
29058f0484fSRodney W. Grimes  * New .rhosts strategy: We are passed an ip address. We spin through
29158f0484fSRodney W. Grimes  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
29258f0484fSRodney W. Grimes  * has ip addresses, we don't have to trust a nameserver.  When it
29358f0484fSRodney W. Grimes  * contains hostnames, we spin through the list of addresses the nameserver
29458f0484fSRodney W. Grimes  * gives us and look for a match.
29558f0484fSRodney W. Grimes  *
29658f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
29758f0484fSRodney W. Grimes  */
29858f0484fSRodney W. Grimes int
29958f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser)
300e0ce825eSDoug Rabson 	unsigned long raddr;
30158f0484fSRodney W. Grimes 	int superuser;
30258f0484fSRodney W. Grimes 	const char *ruser, *luser;
30358f0484fSRodney W. Grimes {
30458f0484fSRodney W. Grimes 	register char *cp;
30558f0484fSRodney W. Grimes 	struct stat sbuf;
30658f0484fSRodney W. Grimes 	struct passwd *pwd;
30758f0484fSRodney W. Grimes 	FILE *hostf;
30858f0484fSRodney W. Grimes 	uid_t uid;
30958f0484fSRodney W. Grimes 	int first;
31058f0484fSRodney W. Grimes 	char pbuf[MAXPATHLEN];
31158f0484fSRodney W. Grimes 
31258f0484fSRodney W. Grimes 	first = 1;
31358f0484fSRodney W. Grimes 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
31458f0484fSRodney W. Grimes again:
31558f0484fSRodney W. Grimes 	if (hostf) {
316e0ce825eSDoug Rabson 		if (__ivaliduser(hostf, (u_int32_t)raddr, luser, ruser) == 0) {
31758f0484fSRodney W. Grimes 			(void)fclose(hostf);
31858f0484fSRodney W. Grimes 			return (0);
31958f0484fSRodney W. Grimes 		}
32058f0484fSRodney W. Grimes 		(void)fclose(hostf);
32158f0484fSRodney W. Grimes 	}
32258f0484fSRodney W. Grimes 	if (first == 1 && (__check_rhosts_file || superuser)) {
32358f0484fSRodney W. Grimes 		first = 0;
32458f0484fSRodney W. Grimes 		if ((pwd = getpwnam(luser)) == NULL)
32558f0484fSRodney W. Grimes 			return (-1);
32658f0484fSRodney W. Grimes 		(void)strcpy(pbuf, pwd->pw_dir);
32758f0484fSRodney W. Grimes 		(void)strcat(pbuf, "/.rhosts");
32858f0484fSRodney W. Grimes 
32958f0484fSRodney W. Grimes 		/*
33058f0484fSRodney W. Grimes 		 * Change effective uid while opening .rhosts.  If root and
33158f0484fSRodney W. Grimes 		 * reading an NFS mounted file system, can't read files that
33258f0484fSRodney W. Grimes 		 * are protected read/write owner only.
33358f0484fSRodney W. Grimes 		 */
33458f0484fSRodney W. Grimes 		uid = geteuid();
33558f0484fSRodney W. Grimes 		(void)seteuid(pwd->pw_uid);
33658f0484fSRodney W. Grimes 		hostf = fopen(pbuf, "r");
33758f0484fSRodney W. Grimes 		(void)seteuid(uid);
33858f0484fSRodney W. Grimes 
33958f0484fSRodney W. Grimes 		if (hostf == NULL)
34058f0484fSRodney W. Grimes 			return (-1);
34158f0484fSRodney W. Grimes 		/*
34258f0484fSRodney W. Grimes 		 * If not a regular file, or is owned by someone other than
34358f0484fSRodney W. Grimes 		 * user or root or if writeable by anyone but the owner, quit.
34458f0484fSRodney W. Grimes 		 */
34558f0484fSRodney W. Grimes 		cp = NULL;
34658f0484fSRodney W. Grimes 		if (lstat(pbuf, &sbuf) < 0)
34758f0484fSRodney W. Grimes 			cp = ".rhosts lstat failed";
34858f0484fSRodney W. Grimes 		else if (!S_ISREG(sbuf.st_mode))
34958f0484fSRodney W. Grimes 			cp = ".rhosts not regular file";
35058f0484fSRodney W. Grimes 		else if (fstat(fileno(hostf), &sbuf) < 0)
35158f0484fSRodney W. Grimes 			cp = ".rhosts fstat failed";
35258f0484fSRodney W. Grimes 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
35358f0484fSRodney W. Grimes 			cp = "bad .rhosts owner";
35458f0484fSRodney W. Grimes 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
35558f0484fSRodney W. Grimes 			cp = ".rhosts writeable by other than owner";
35658f0484fSRodney W. Grimes 		/* If there were any problems, quit. */
35758f0484fSRodney W. Grimes 		if (cp) {
35858f0484fSRodney W. Grimes 			__rcmd_errstr = cp;
35958f0484fSRodney W. Grimes 			(void)fclose(hostf);
36058f0484fSRodney W. Grimes 			return (-1);
36158f0484fSRodney W. Grimes 		}
36258f0484fSRodney W. Grimes 		goto again;
36358f0484fSRodney W. Grimes 	}
36458f0484fSRodney W. Grimes 	return (-1);
36558f0484fSRodney W. Grimes }
36658f0484fSRodney W. Grimes 
36758f0484fSRodney W. Grimes /*
36858f0484fSRodney W. Grimes  * XXX
36958f0484fSRodney W. Grimes  * Don't make static, used by lpd(8).
37058f0484fSRodney W. Grimes  *
37158f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
37258f0484fSRodney W. Grimes  */
37358f0484fSRodney W. Grimes int
37458f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser)
37558f0484fSRodney W. Grimes 	FILE *hostf;
376e0ce825eSDoug Rabson 	u_int32_t raddr;
37758f0484fSRodney W. Grimes 	const char *luser, *ruser;
37858f0484fSRodney W. Grimes {
37958f0484fSRodney W. Grimes 	register char *user, *p;
38058f0484fSRodney W. Grimes 	int ch;
38158f0484fSRodney W. Grimes 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
38297cb5094SBill Paul 	char hname[MAXHOSTNAMELEN];
3838538335fSBill Paul 	struct hostent *hp;
3848538335fSBill Paul 	/* Presumed guilty until proven innocent. */
3858538335fSBill Paul 	int userok = 0, hostok = 0;
3861e890b05SBill Paul #ifdef YP
3871e890b05SBill Paul 	char *ypdomain;
3888538335fSBill Paul 
3891e890b05SBill Paul 	if (yp_get_default_domain(&ypdomain))
3901e890b05SBill Paul 		ypdomain = NULL;
3911e890b05SBill Paul #else
3921e890b05SBill Paul #define	ypdomain NULL
3931e890b05SBill Paul #endif
3948538335fSBill Paul 	/* We need to get the damn hostname back for netgroup matching. */
395e0ce825eSDoug Rabson 	if ((hp = gethostbyaddr((char *)&raddr, sizeof(u_int32_t),
3968538335fSBill Paul 							AF_INET)) == NULL)
3978538335fSBill Paul 		return (-1);
3982a62f023SWarner Losh 	strncpy(hname, hp->h_name, sizeof(hname));
3992a62f023SWarner Losh 	hname[sizeof(hname) - 1] = '\0';
40058f0484fSRodney W. Grimes 
40158f0484fSRodney W. Grimes 	while (fgets(buf, sizeof(buf), hostf)) {
40258f0484fSRodney W. Grimes 		p = buf;
40358f0484fSRodney W. Grimes 		/* Skip lines that are too long. */
40458f0484fSRodney W. Grimes 		if (strchr(p, '\n') == NULL) {
40558f0484fSRodney W. Grimes 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
40658f0484fSRodney W. Grimes 			continue;
40758f0484fSRodney W. Grimes 		}
408acc7e87cSPeter Wemm 		if (*p == '\n' || *p == '#') {
409acc7e87cSPeter Wemm 			/* comment... */
410acc7e87cSPeter Wemm 			continue;
411acc7e87cSPeter Wemm 		}
41258f0484fSRodney W. Grimes 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
4133fb85bffSAndrey A. Chernov 			*p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
41458f0484fSRodney W. Grimes 			p++;
41558f0484fSRodney W. Grimes 		}
41658f0484fSRodney W. Grimes 		if (*p == ' ' || *p == '\t') {
41758f0484fSRodney W. Grimes 			*p++ = '\0';
41858f0484fSRodney W. Grimes 			while (*p == ' ' || *p == '\t')
41958f0484fSRodney W. Grimes 				p++;
42058f0484fSRodney W. Grimes 			user = p;
42158f0484fSRodney W. Grimes 			while (*p != '\n' && *p != ' ' &&
42258f0484fSRodney W. Grimes 			    *p != '\t' && *p != '\0')
42358f0484fSRodney W. Grimes 				p++;
42458f0484fSRodney W. Grimes 		} else
42558f0484fSRodney W. Grimes 			user = p;
42658f0484fSRodney W. Grimes 		*p = '\0';
4278538335fSBill Paul 		/*
4288538335fSBill Paul 		 * Do +/- and +@/-@ checking. This looks really nasty,
4298538335fSBill Paul 		 * but it matches SunOS's behavior so far as I can tell.
4308538335fSBill Paul 		 */
4318538335fSBill Paul 		switch(buf[0]) {
4328538335fSBill Paul 		case '+':
4338538335fSBill Paul 			if (!buf[1]) {     /* '+' matches all hosts */
4348538335fSBill Paul 				hostok = 1;
4358538335fSBill Paul 				break;
43658f0484fSRodney W. Grimes 			}
4378538335fSBill Paul 			if (buf[1] == '@')  /* match a host by netgroup */
43897cb5094SBill Paul 				hostok = innetgr((char *)&buf[2],
43997cb5094SBill Paul 					(char *)&hname, NULL, ypdomain);
4408538335fSBill Paul 			else		/* match a host by addr */
4418538335fSBill Paul 				hostok = __icheckhost(raddr,(char *)&buf[1]);
4428538335fSBill Paul 			break;
4438538335fSBill Paul 		case '-':     /* reject '-' hosts and all their users */
4448538335fSBill Paul 			if (buf[1] == '@') {
4458538335fSBill Paul 				if (innetgr((char *)&buf[2],
44697cb5094SBill Paul 					      (char *)&hname, NULL, ypdomain))
4478538335fSBill Paul 					return(-1);
4488538335fSBill Paul 			} else {
4498538335fSBill Paul 				if (__icheckhost(raddr,(char *)&buf[1]))
4508538335fSBill Paul 					return(-1);
4518538335fSBill Paul 			}
4528538335fSBill Paul 			break;
4538538335fSBill Paul 		default:  /* if no '+' or '-', do a simple match */
4548538335fSBill Paul 			hostok = __icheckhost(raddr, buf);
4558538335fSBill Paul 			break;
4568538335fSBill Paul 		}
4578538335fSBill Paul 		switch(*user) {
4588538335fSBill Paul 		case '+':
4598538335fSBill Paul 			if (!*(user+1)) {      /* '+' matches all users */
4608538335fSBill Paul 				userok = 1;
4618538335fSBill Paul 				break;
4628538335fSBill Paul 			}
4638538335fSBill Paul 			if (*(user+1) == '@')  /* match a user by netgroup */
4641e890b05SBill Paul 				userok = innetgr(user+2, NULL, ruser, ypdomain);
4658538335fSBill Paul 			else	   /* match a user by direct specification */
4668538335fSBill Paul 				userok = !(strcmp(ruser, user+1));
4678538335fSBill Paul 			break;
4688538335fSBill Paul 		case '-': 		/* if we matched a hostname, */
4698538335fSBill Paul 			if (hostok) {   /* check for user field rejections */
4708538335fSBill Paul 				if (!*(user+1))
4718538335fSBill Paul 					return(-1);
4728538335fSBill Paul 				if (*(user+1) == '@') {
4738538335fSBill Paul 					if (innetgr(user+2, NULL,
4741e890b05SBill Paul 							ruser, ypdomain))
4758538335fSBill Paul 						return(-1);
4768538335fSBill Paul 				} else {
4778538335fSBill Paul 					if (!strcmp(ruser, user+1))
4788538335fSBill Paul 						return(-1);
4798538335fSBill Paul 				}
4808538335fSBill Paul 			}
4818538335fSBill Paul 			break;
4828538335fSBill Paul 		default:	/* no rejections: try to match the user */
4838538335fSBill Paul 			if (hostok)
4848538335fSBill Paul 				userok = !(strcmp(ruser,*user ? user : luser));
4858538335fSBill Paul 			break;
4868538335fSBill Paul 		}
4878538335fSBill Paul 		if (hostok && userok)
4888538335fSBill Paul 			return(0);
48958f0484fSRodney W. Grimes 	}
49058f0484fSRodney W. Grimes 	return (-1);
49158f0484fSRodney W. Grimes }
49258f0484fSRodney W. Grimes 
49358f0484fSRodney W. Grimes /*
49458f0484fSRodney W. Grimes  * Returns "true" if match, 0 if no match.
49558f0484fSRodney W. Grimes  */
49658f0484fSRodney W. Grimes static int
49758f0484fSRodney W. Grimes __icheckhost(raddr, lhost)
498e0ce825eSDoug Rabson 	u_int32_t raddr;
49958f0484fSRodney W. Grimes 	register char *lhost;
50058f0484fSRodney W. Grimes {
50158f0484fSRodney W. Grimes 	register struct hostent *hp;
502e0ce825eSDoug Rabson 	register u_int32_t laddr;
50358f0484fSRodney W. Grimes 	register char **pp;
50458f0484fSRodney W. Grimes 
50558f0484fSRodney W. Grimes 	/* Try for raw ip address first. */
506e0ce825eSDoug Rabson 	if (isdigit(*lhost) && (u_int32_t)(laddr = inet_addr(lhost)) != -1)
50758f0484fSRodney W. Grimes 		return (raddr == laddr);
50858f0484fSRodney W. Grimes 
50958f0484fSRodney W. Grimes 	/* Better be a hostname. */
51058f0484fSRodney W. Grimes 	if ((hp = gethostbyname(lhost)) == NULL)
51158f0484fSRodney W. Grimes 		return (0);
51258f0484fSRodney W. Grimes 
51358f0484fSRodney W. Grimes 	/* Spin through ip addresses. */
51458f0484fSRodney W. Grimes 	for (pp = hp->h_addr_list; *pp; ++pp)
515e0ce825eSDoug Rabson 		if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))
51658f0484fSRodney W. Grimes 			return (1);
51758f0484fSRodney W. Grimes 
51858f0484fSRodney W. Grimes 	/* No match. */
51958f0484fSRodney W. Grimes 	return (0);
52058f0484fSRodney W. Grimes }
521