xref: /freebsd/lib/libc/net/rcmd.c (revision d1f32ba5df677453a04352095875882b1aa36e32)
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.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #include <sys/param.h>
3958f0484fSRodney W. Grimes #include <sys/socket.h>
4058f0484fSRodney W. Grimes #include <sys/stat.h>
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <netinet/in.h>
4358f0484fSRodney W. Grimes #include <arpa/inet.h>
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes #include <signal.h>
4658f0484fSRodney W. Grimes #include <fcntl.h>
4758f0484fSRodney W. Grimes #include <netdb.h>
4858f0484fSRodney W. Grimes #include <unistd.h>
4958f0484fSRodney W. Grimes #include <pwd.h>
5058f0484fSRodney W. Grimes #include <errno.h>
5158f0484fSRodney W. Grimes #include <stdio.h>
5258f0484fSRodney W. Grimes #include <ctype.h>
5358f0484fSRodney W. Grimes #include <string.h>
5458f0484fSRodney W. Grimes 
55d1f32ba5SGeoff Rehmet #define max(a, b)	((a > b) ? a : b)
56d1f32ba5SGeoff Rehmet 
5758f0484fSRodney W. Grimes int	__ivaliduser __P((FILE *, u_long, const char *, const char *));
5858f0484fSRodney W. Grimes static int __icheckhost __P((u_long, char *));
5958f0484fSRodney W. Grimes 
6058f0484fSRodney W. Grimes int
6158f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
6258f0484fSRodney W. Grimes 	char **ahost;
6358f0484fSRodney W. Grimes 	u_short rport;
6458f0484fSRodney W. Grimes 	const char *locuser, *remuser, *cmd;
6558f0484fSRodney W. Grimes 	int *fd2p;
6658f0484fSRodney W. Grimes {
6758f0484fSRodney W. Grimes 	struct hostent *hp;
6858f0484fSRodney W. Grimes 	struct sockaddr_in sin, from;
6958f0484fSRodney W. Grimes 	fd_set reads;
7058f0484fSRodney W. Grimes 	long oldmask;
7158f0484fSRodney W. Grimes 	pid_t pid;
7258f0484fSRodney W. Grimes 	int s, lport, timo;
7358f0484fSRodney W. Grimes 	char c;
7458f0484fSRodney W. Grimes 
7558f0484fSRodney W. Grimes 	pid = getpid();
7658f0484fSRodney W. Grimes 	hp = gethostbyname(*ahost);
7758f0484fSRodney W. Grimes 	if (hp == NULL) {
7858f0484fSRodney W. Grimes 		herror(*ahost);
7958f0484fSRodney W. Grimes 		return (-1);
8058f0484fSRodney W. Grimes 	}
8158f0484fSRodney W. Grimes 	*ahost = hp->h_name;
8258f0484fSRodney W. Grimes 	oldmask = sigblock(sigmask(SIGURG));
8358f0484fSRodney W. Grimes 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
8458f0484fSRodney W. Grimes 		s = rresvport(&lport);
8558f0484fSRodney W. Grimes 		if (s < 0) {
8658f0484fSRodney W. Grimes 			if (errno == EAGAIN)
8758f0484fSRodney W. Grimes 				(void)fprintf(stderr,
8858f0484fSRodney W. Grimes 				    "rcmd: socket: All ports in use\n");
8958f0484fSRodney W. Grimes 			else
9058f0484fSRodney W. Grimes 				(void)fprintf(stderr, "rcmd: socket: %s\n",
9158f0484fSRodney W. Grimes 				    strerror(errno));
9258f0484fSRodney W. Grimes 			sigsetmask(oldmask);
9358f0484fSRodney W. Grimes 			return (-1);
9458f0484fSRodney W. Grimes 		}
9558f0484fSRodney W. Grimes 		fcntl(s, F_SETOWN, pid);
9658f0484fSRodney W. Grimes 		sin.sin_family = hp->h_addrtype;
9758f0484fSRodney W. Grimes 		bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
9858f0484fSRodney W. Grimes 		sin.sin_port = rport;
9958f0484fSRodney W. Grimes 		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
10058f0484fSRodney W. Grimes 			break;
10158f0484fSRodney W. Grimes 		(void)close(s);
10258f0484fSRodney W. Grimes 		if (errno == EADDRINUSE) {
10358f0484fSRodney W. Grimes 			lport--;
10458f0484fSRodney W. Grimes 			continue;
10558f0484fSRodney W. Grimes 		}
10658f0484fSRodney W. Grimes 		if (errno == ECONNREFUSED && timo <= 16) {
10758f0484fSRodney W. Grimes 			(void)sleep(timo);
10858f0484fSRodney W. Grimes 			timo *= 2;
10958f0484fSRodney W. Grimes 			continue;
11058f0484fSRodney W. Grimes 		}
11158f0484fSRodney W. Grimes 		if (hp->h_addr_list[1] != NULL) {
11258f0484fSRodney W. Grimes 			int oerrno = errno;
11358f0484fSRodney W. Grimes 
11458f0484fSRodney W. Grimes 			(void)fprintf(stderr, "connect to address %s: ",
11558f0484fSRodney W. Grimes 			    inet_ntoa(sin.sin_addr));
11658f0484fSRodney W. Grimes 			errno = oerrno;
11758f0484fSRodney W. Grimes 			perror(0);
11858f0484fSRodney W. Grimes 			hp->h_addr_list++;
11958f0484fSRodney W. Grimes 			bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
12058f0484fSRodney W. Grimes 			(void)fprintf(stderr, "Trying %s...\n",
12158f0484fSRodney W. Grimes 			    inet_ntoa(sin.sin_addr));
12258f0484fSRodney W. Grimes 			continue;
12358f0484fSRodney W. Grimes 		}
12458f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
12558f0484fSRodney W. Grimes 		sigsetmask(oldmask);
12658f0484fSRodney W. Grimes 		return (-1);
12758f0484fSRodney W. Grimes 	}
12858f0484fSRodney W. Grimes 	lport--;
12958f0484fSRodney W. Grimes 	if (fd2p == 0) {
13058f0484fSRodney W. Grimes 		write(s, "", 1);
13158f0484fSRodney W. Grimes 		lport = 0;
13258f0484fSRodney W. Grimes 	} else {
13358f0484fSRodney W. Grimes 		char num[8];
13458f0484fSRodney W. Grimes 		int s2 = rresvport(&lport), s3;
13558f0484fSRodney W. Grimes 		int len = sizeof(from);
136d1f32ba5SGeoff Rehmet 		int nfds;
13758f0484fSRodney W. Grimes 
13858f0484fSRodney W. Grimes 		if (s2 < 0)
13958f0484fSRodney W. Grimes 			goto bad;
14058f0484fSRodney W. Grimes 		listen(s2, 1);
14158f0484fSRodney W. Grimes 		(void)snprintf(num, sizeof(num), "%d", lport);
14258f0484fSRodney W. Grimes 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
14358f0484fSRodney W. Grimes 			(void)fprintf(stderr,
14458f0484fSRodney W. Grimes 			    "rcmd: write (setting up stderr): %s\n",
14558f0484fSRodney W. Grimes 			    strerror(errno));
14658f0484fSRodney W. Grimes 			(void)close(s2);
14758f0484fSRodney W. Grimes 			goto bad;
14858f0484fSRodney W. Grimes 		}
149d1f32ba5SGeoff Rehmet 		nfds = max(s, s2)+1;
150d1f32ba5SGeoff Rehmet 		if(nfds > FD_SETSIZE) {
151d1f32ba5SGeoff Rehmet 			fprintf(stderr, "rcmd: too many files\n");
152d1f32ba5SGeoff Rehmet 			(void)close(s2);
153d1f32ba5SGeoff Rehmet 			goto bad;
154d1f32ba5SGeoff Rehmet 		}
15558f0484fSRodney W. Grimes 		FD_ZERO(&reads);
15658f0484fSRodney W. Grimes 		FD_SET(s, &reads);
15758f0484fSRodney W. Grimes 		FD_SET(s2, &reads);
15858f0484fSRodney W. Grimes 		errno = 0;
159d1f32ba5SGeoff Rehmet 		if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
16058f0484fSRodney W. Grimes 			if (errno != 0)
16158f0484fSRodney W. Grimes 				(void)fprintf(stderr,
16258f0484fSRodney W. Grimes 				    "rcmd: select (setting up stderr): %s\n",
16358f0484fSRodney W. Grimes 				    strerror(errno));
16458f0484fSRodney W. Grimes 			else
16558f0484fSRodney W. Grimes 				(void)fprintf(stderr,
16658f0484fSRodney W. Grimes 				"select: protocol failure in circuit setup\n");
16758f0484fSRodney W. Grimes 			(void)close(s2);
16858f0484fSRodney W. Grimes 			goto bad;
16958f0484fSRodney W. Grimes 		}
17058f0484fSRodney W. Grimes 		s3 = accept(s2, (struct sockaddr *)&from, &len);
17158f0484fSRodney W. Grimes 		(void)close(s2);
17258f0484fSRodney W. Grimes 		if (s3 < 0) {
17358f0484fSRodney W. Grimes 			(void)fprintf(stderr,
17458f0484fSRodney W. Grimes 			    "rcmd: accept: %s\n", strerror(errno));
17558f0484fSRodney W. Grimes 			lport = 0;
17658f0484fSRodney W. Grimes 			goto bad;
17758f0484fSRodney W. Grimes 		}
17858f0484fSRodney W. Grimes 		*fd2p = s3;
17958f0484fSRodney W. Grimes 		from.sin_port = ntohs((u_short)from.sin_port);
18058f0484fSRodney W. Grimes 		if (from.sin_family != AF_INET ||
18158f0484fSRodney W. Grimes 		    from.sin_port >= IPPORT_RESERVED ||
18258f0484fSRodney W. Grimes 		    from.sin_port < IPPORT_RESERVED / 2) {
18358f0484fSRodney W. Grimes 			(void)fprintf(stderr,
18458f0484fSRodney W. Grimes 			    "socket: protocol failure in circuit setup.\n");
18558f0484fSRodney W. Grimes 			goto bad2;
18658f0484fSRodney W. Grimes 		}
18758f0484fSRodney W. Grimes 	}
18858f0484fSRodney W. Grimes 	(void)write(s, locuser, strlen(locuser)+1);
18958f0484fSRodney W. Grimes 	(void)write(s, remuser, strlen(remuser)+1);
19058f0484fSRodney W. Grimes 	(void)write(s, cmd, strlen(cmd)+1);
19158f0484fSRodney W. Grimes 	if (read(s, &c, 1) != 1) {
19258f0484fSRodney W. Grimes 		(void)fprintf(stderr,
19358f0484fSRodney W. Grimes 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
19458f0484fSRodney W. Grimes 		goto bad2;
19558f0484fSRodney W. Grimes 	}
19658f0484fSRodney W. Grimes 	if (c != 0) {
19758f0484fSRodney W. Grimes 		while (read(s, &c, 1) == 1) {
19858f0484fSRodney W. Grimes 			(void)write(STDERR_FILENO, &c, 1);
19958f0484fSRodney W. Grimes 			if (c == '\n')
20058f0484fSRodney W. Grimes 				break;
20158f0484fSRodney W. Grimes 		}
20258f0484fSRodney W. Grimes 		goto bad2;
20358f0484fSRodney W. Grimes 	}
20458f0484fSRodney W. Grimes 	sigsetmask(oldmask);
20558f0484fSRodney W. Grimes 	return (s);
20658f0484fSRodney W. Grimes bad2:
20758f0484fSRodney W. Grimes 	if (lport)
20858f0484fSRodney W. Grimes 		(void)close(*fd2p);
20958f0484fSRodney W. Grimes bad:
21058f0484fSRodney W. Grimes 	(void)close(s);
21158f0484fSRodney W. Grimes 	sigsetmask(oldmask);
21258f0484fSRodney W. Grimes 	return (-1);
21358f0484fSRodney W. Grimes }
21458f0484fSRodney W. Grimes 
21558f0484fSRodney W. Grimes int
21658f0484fSRodney W. Grimes rresvport(alport)
21758f0484fSRodney W. Grimes 	int *alport;
21858f0484fSRodney W. Grimes {
21958f0484fSRodney W. Grimes 	struct sockaddr_in sin;
22058f0484fSRodney W. Grimes 	int s;
22158f0484fSRodney W. Grimes 
22258f0484fSRodney W. Grimes 	sin.sin_family = AF_INET;
22358f0484fSRodney W. Grimes 	sin.sin_addr.s_addr = INADDR_ANY;
22458f0484fSRodney W. Grimes 	s = socket(AF_INET, SOCK_STREAM, 0);
22558f0484fSRodney W. Grimes 	if (s < 0)
22658f0484fSRodney W. Grimes 		return (-1);
22758f0484fSRodney W. Grimes 	for (;;) {
22858f0484fSRodney W. Grimes 		sin.sin_port = htons((u_short)*alport);
22958f0484fSRodney W. Grimes 		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
23058f0484fSRodney W. Grimes 			return (s);
23158f0484fSRodney W. Grimes 		if (errno != EADDRINUSE) {
23258f0484fSRodney W. Grimes 			(void)close(s);
23358f0484fSRodney W. Grimes 			return (-1);
23458f0484fSRodney W. Grimes 		}
23558f0484fSRodney W. Grimes 		(*alport)--;
23658f0484fSRodney W. Grimes 		if (*alport == IPPORT_RESERVED/2) {
23758f0484fSRodney W. Grimes 			(void)close(s);
23858f0484fSRodney W. Grimes 			errno = EAGAIN;		/* close */
23958f0484fSRodney W. Grimes 			return (-1);
24058f0484fSRodney W. Grimes 		}
24158f0484fSRodney W. Grimes 	}
24258f0484fSRodney W. Grimes }
24358f0484fSRodney W. Grimes 
24458f0484fSRodney W. Grimes int	__check_rhosts_file = 1;
24558f0484fSRodney W. Grimes char	*__rcmd_errstr;
24658f0484fSRodney W. Grimes 
24758f0484fSRodney W. Grimes int
24858f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser)
24958f0484fSRodney W. Grimes 	const char *rhost, *ruser, *luser;
25058f0484fSRodney W. Grimes 	int superuser;
25158f0484fSRodney W. Grimes {
25258f0484fSRodney W. Grimes 	struct hostent *hp;
25358f0484fSRodney W. Grimes 	u_long addr;
25458f0484fSRodney W. Grimes 	char **ap;
25558f0484fSRodney W. Grimes 
25658f0484fSRodney W. Grimes 	if ((hp = gethostbyname(rhost)) == NULL)
25758f0484fSRodney W. Grimes 		return (-1);
25858f0484fSRodney W. Grimes 	for (ap = hp->h_addr_list; *ap; ++ap) {
25958f0484fSRodney W. Grimes 		bcopy(*ap, &addr, sizeof(addr));
26058f0484fSRodney W. Grimes 		if (iruserok(addr, superuser, ruser, luser) == 0)
26158f0484fSRodney W. Grimes 			return (0);
26258f0484fSRodney W. Grimes 	}
26358f0484fSRodney W. Grimes 	return (-1);
26458f0484fSRodney W. Grimes }
26558f0484fSRodney W. Grimes 
26658f0484fSRodney W. Grimes /*
26758f0484fSRodney W. Grimes  * New .rhosts strategy: We are passed an ip address. We spin through
26858f0484fSRodney W. Grimes  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
26958f0484fSRodney W. Grimes  * has ip addresses, we don't have to trust a nameserver.  When it
27058f0484fSRodney W. Grimes  * contains hostnames, we spin through the list of addresses the nameserver
27158f0484fSRodney W. Grimes  * gives us and look for a match.
27258f0484fSRodney W. Grimes  *
27358f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
27458f0484fSRodney W. Grimes  */
27558f0484fSRodney W. Grimes int
27658f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser)
27758f0484fSRodney W. Grimes 	u_long raddr;
27858f0484fSRodney W. Grimes 	int superuser;
27958f0484fSRodney W. Grimes 	const char *ruser, *luser;
28058f0484fSRodney W. Grimes {
28158f0484fSRodney W. Grimes 	register char *cp;
28258f0484fSRodney W. Grimes 	struct stat sbuf;
28358f0484fSRodney W. Grimes 	struct passwd *pwd;
28458f0484fSRodney W. Grimes 	FILE *hostf;
28558f0484fSRodney W. Grimes 	uid_t uid;
28658f0484fSRodney W. Grimes 	int first;
28758f0484fSRodney W. Grimes 	char pbuf[MAXPATHLEN];
28858f0484fSRodney W. Grimes 
28958f0484fSRodney W. Grimes 	first = 1;
29058f0484fSRodney W. Grimes 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
29158f0484fSRodney W. Grimes again:
29258f0484fSRodney W. Grimes 	if (hostf) {
29358f0484fSRodney W. Grimes 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
29458f0484fSRodney W. Grimes 			(void)fclose(hostf);
29558f0484fSRodney W. Grimes 			return (0);
29658f0484fSRodney W. Grimes 		}
29758f0484fSRodney W. Grimes 		(void)fclose(hostf);
29858f0484fSRodney W. Grimes 	}
29958f0484fSRodney W. Grimes 	if (first == 1 && (__check_rhosts_file || superuser)) {
30058f0484fSRodney W. Grimes 		first = 0;
30158f0484fSRodney W. Grimes 		if ((pwd = getpwnam(luser)) == NULL)
30258f0484fSRodney W. Grimes 			return (-1);
30358f0484fSRodney W. Grimes 		(void)strcpy(pbuf, pwd->pw_dir);
30458f0484fSRodney W. Grimes 		(void)strcat(pbuf, "/.rhosts");
30558f0484fSRodney W. Grimes 
30658f0484fSRodney W. Grimes 		/*
30758f0484fSRodney W. Grimes 		 * Change effective uid while opening .rhosts.  If root and
30858f0484fSRodney W. Grimes 		 * reading an NFS mounted file system, can't read files that
30958f0484fSRodney W. Grimes 		 * are protected read/write owner only.
31058f0484fSRodney W. Grimes 		 */
31158f0484fSRodney W. Grimes 		uid = geteuid();
31258f0484fSRodney W. Grimes 		(void)seteuid(pwd->pw_uid);
31358f0484fSRodney W. Grimes 		hostf = fopen(pbuf, "r");
31458f0484fSRodney W. Grimes 		(void)seteuid(uid);
31558f0484fSRodney W. Grimes 
31658f0484fSRodney W. Grimes 		if (hostf == NULL)
31758f0484fSRodney W. Grimes 			return (-1);
31858f0484fSRodney W. Grimes 		/*
31958f0484fSRodney W. Grimes 		 * If not a regular file, or is owned by someone other than
32058f0484fSRodney W. Grimes 		 * user or root or if writeable by anyone but the owner, quit.
32158f0484fSRodney W. Grimes 		 */
32258f0484fSRodney W. Grimes 		cp = NULL;
32358f0484fSRodney W. Grimes 		if (lstat(pbuf, &sbuf) < 0)
32458f0484fSRodney W. Grimes 			cp = ".rhosts lstat failed";
32558f0484fSRodney W. Grimes 		else if (!S_ISREG(sbuf.st_mode))
32658f0484fSRodney W. Grimes 			cp = ".rhosts not regular file";
32758f0484fSRodney W. Grimes 		else if (fstat(fileno(hostf), &sbuf) < 0)
32858f0484fSRodney W. Grimes 			cp = ".rhosts fstat failed";
32958f0484fSRodney W. Grimes 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
33058f0484fSRodney W. Grimes 			cp = "bad .rhosts owner";
33158f0484fSRodney W. Grimes 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
33258f0484fSRodney W. Grimes 			cp = ".rhosts writeable by other than owner";
33358f0484fSRodney W. Grimes 		/* If there were any problems, quit. */
33458f0484fSRodney W. Grimes 		if (cp) {
33558f0484fSRodney W. Grimes 			__rcmd_errstr = cp;
33658f0484fSRodney W. Grimes 			(void)fclose(hostf);
33758f0484fSRodney W. Grimes 			return (-1);
33858f0484fSRodney W. Grimes 		}
33958f0484fSRodney W. Grimes 		goto again;
34058f0484fSRodney W. Grimes 	}
34158f0484fSRodney W. Grimes 	return (-1);
34258f0484fSRodney W. Grimes }
34358f0484fSRodney W. Grimes 
34458f0484fSRodney W. Grimes /*
34558f0484fSRodney W. Grimes  * XXX
34658f0484fSRodney W. Grimes  * Don't make static, used by lpd(8).
34758f0484fSRodney W. Grimes  *
34858f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
34958f0484fSRodney W. Grimes  */
35058f0484fSRodney W. Grimes int
35158f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser)
35258f0484fSRodney W. Grimes 	FILE *hostf;
35358f0484fSRodney W. Grimes 	u_long raddr;
35458f0484fSRodney W. Grimes 	const char *luser, *ruser;
35558f0484fSRodney W. Grimes {
35658f0484fSRodney W. Grimes 	register char *user, *p;
35758f0484fSRodney W. Grimes 	int ch;
35858f0484fSRodney W. Grimes 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
35958f0484fSRodney W. Grimes 
36058f0484fSRodney W. Grimes 	while (fgets(buf, sizeof(buf), hostf)) {
36158f0484fSRodney W. Grimes 		p = buf;
36258f0484fSRodney W. Grimes 		/* Skip lines that are too long. */
36358f0484fSRodney W. Grimes 		if (strchr(p, '\n') == NULL) {
36458f0484fSRodney W. Grimes 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
36558f0484fSRodney W. Grimes 			continue;
36658f0484fSRodney W. Grimes 		}
36758f0484fSRodney W. Grimes 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
36858f0484fSRodney W. Grimes 			*p = isupper(*p) ? tolower(*p) : *p;
36958f0484fSRodney W. Grimes 			p++;
37058f0484fSRodney W. Grimes 		}
37158f0484fSRodney W. Grimes 		if (*p == ' ' || *p == '\t') {
37258f0484fSRodney W. Grimes 			*p++ = '\0';
37358f0484fSRodney W. Grimes 			while (*p == ' ' || *p == '\t')
37458f0484fSRodney W. Grimes 				p++;
37558f0484fSRodney W. Grimes 			user = p;
37658f0484fSRodney W. Grimes 			while (*p != '\n' && *p != ' ' &&
37758f0484fSRodney W. Grimes 			    *p != '\t' && *p != '\0')
37858f0484fSRodney W. Grimes 				p++;
37958f0484fSRodney W. Grimes 		} else
38058f0484fSRodney W. Grimes 			user = p;
38158f0484fSRodney W. Grimes 		*p = '\0';
38258f0484fSRodney W. Grimes 		if (__icheckhost(raddr, buf) &&
38358f0484fSRodney W. Grimes 		    strcmp(ruser, *user ? user : luser) == 0) {
38458f0484fSRodney W. Grimes 			return (0);
38558f0484fSRodney W. Grimes 		}
38658f0484fSRodney W. Grimes 	}
38758f0484fSRodney W. Grimes 	return (-1);
38858f0484fSRodney W. Grimes }
38958f0484fSRodney W. Grimes 
39058f0484fSRodney W. Grimes /*
39158f0484fSRodney W. Grimes  * Returns "true" if match, 0 if no match.
39258f0484fSRodney W. Grimes  */
39358f0484fSRodney W. Grimes static int
39458f0484fSRodney W. Grimes __icheckhost(raddr, lhost)
39558f0484fSRodney W. Grimes 	u_long raddr;
39658f0484fSRodney W. Grimes 	register char *lhost;
39758f0484fSRodney W. Grimes {
39858f0484fSRodney W. Grimes 	register struct hostent *hp;
39958f0484fSRodney W. Grimes 	register u_long laddr;
40058f0484fSRodney W. Grimes 	register char **pp;
40158f0484fSRodney W. Grimes 
40258f0484fSRodney W. Grimes 	/* Try for raw ip address first. */
40358f0484fSRodney W. Grimes 	if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1)
40458f0484fSRodney W. Grimes 		return (raddr == laddr);
40558f0484fSRodney W. Grimes 
40658f0484fSRodney W. Grimes 	/* Better be a hostname. */
40758f0484fSRodney W. Grimes 	if ((hp = gethostbyname(lhost)) == NULL)
40858f0484fSRodney W. Grimes 		return (0);
40958f0484fSRodney W. Grimes 
41058f0484fSRodney W. Grimes 	/* Spin through ip addresses. */
41158f0484fSRodney W. Grimes 	for (pp = hp->h_addr_list; *pp; ++pp)
41258f0484fSRodney W. Grimes 		if (!bcmp(&raddr, *pp, sizeof(u_long)))
41358f0484fSRodney W. Grimes 			return (1);
41458f0484fSRodney W. Grimes 
41558f0484fSRodney W. Grimes 	/* No match. */
41658f0484fSRodney W. Grimes 	return (0);
41758f0484fSRodney W. Grimes }
418