xref: /csrg-svn/lib/libc/net/rcmd.c (revision 66454)
121398Sdist /*
2*66454Sbostic  * Copyright (c) 1983, 1993, 1994
361151Sbostic  *	The Regents of the University of California.  All rights reserved.
433679Sbostic  *
542626Sbostic  * %sccs.include.redist.c%
621398Sdist  */
721398Sdist 
826625Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*66454Sbostic static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 03/26/94";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
116438Swnj 
1224560Sbloom #include <sys/param.h>
136438Swnj #include <sys/socket.h>
1424662Sbloom #include <sys/stat.h>
1554576Sleres 
169191Ssam #include <netinet/in.h>
1746604Sbostic #include <arpa/inet.h>
1854576Sleres 
1946604Sbostic #include <signal.h>
2046604Sbostic #include <fcntl.h>
219191Ssam #include <netdb.h>
2254576Sleres #include <unistd.h>
2346604Sbostic #include <pwd.h>
246438Swnj #include <errno.h>
2546604Sbostic #include <stdio.h>
2646606Sbostic #include <ctype.h>
2746604Sbostic #include <string.h>
286438Swnj 
2954576Sleres int	__ivaliduser __P((FILE *, u_long, const char *, const char *));
3054576Sleres static int __icheckhost __P((u_long, char *));
3154576Sleres 
3254576Sleres int
rcmd(ahost,rport,locuser,remuser,cmd,fd2p)336438Swnj rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
346438Swnj 	char **ahost;
3527734Sbloom 	u_short rport;
3646604Sbostic 	const char *locuser, *remuser, *cmd;
376438Swnj 	int *fd2p;
386438Swnj {
3954576Sleres 	struct hostent *hp;
4054576Sleres 	struct sockaddr_in sin, from;
4154576Sleres 	fd_set reads;
4232295Sbostic 	long oldmask;
4357435Sbostic 	pid_t pid;
4457435Sbostic 	int s, lport, timo;
456438Swnj 	char c;
466438Swnj 
4726980Skarels 	pid = getpid();
488380Ssam 	hp = gethostbyname(*ahost);
4954576Sleres 	if (hp == NULL) {
5035786Sbostic 		herror(*ahost);
516438Swnj 		return (-1);
526438Swnj 	}
538380Ssam 	*ahost = hp->h_name;
5426980Skarels 	oldmask = sigblock(sigmask(SIGURG));
5554576Sleres 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
5626980Skarels 		s = rresvport(&lport);
5726980Skarels 		if (s < 0) {
5826980Skarels 			if (errno == EAGAIN)
5954576Sleres 				(void)fprintf(stderr,
6054576Sleres 				    "rcmd: socket: All ports in use\n");
6126980Skarels 			else
6254576Sleres 				(void)fprintf(stderr, "rcmd: socket: %s\n",
6354576Sleres 				    strerror(errno));
6426980Skarels 			sigsetmask(oldmask);
6526980Skarels 			return (-1);
6626980Skarels 		}
6726980Skarels 		fcntl(s, F_SETOWN, pid);
6826980Skarels 		sin.sin_family = hp->h_addrtype;
6954576Sleres 		bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
7026980Skarels 		sin.sin_port = rport;
7146604Sbostic 		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
7226980Skarels 			break;
7354576Sleres 		(void)close(s);
749191Ssam 		if (errno == EADDRINUSE) {
759191Ssam 			lport--;
7626980Skarels 			continue;
779191Ssam 		}
786438Swnj 		if (errno == ECONNREFUSED && timo <= 16) {
7954576Sleres 			(void)sleep(timo);
806438Swnj 			timo *= 2;
8126980Skarels 			continue;
826438Swnj 		}
8326980Skarels 		if (hp->h_addr_list[1] != NULL) {
8426980Skarels 			int oerrno = errno;
8526980Skarels 
8654576Sleres 			(void)fprintf(stderr, "connect to address %s: ",
8754576Sleres 			    inet_ntoa(sin.sin_addr));
8826980Skarels 			errno = oerrno;
8926980Skarels 			perror(0);
9026980Skarels 			hp->h_addr_list++;
9154576Sleres 			bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
9254576Sleres 			(void)fprintf(stderr, "Trying %s...\n",
9354576Sleres 			    inet_ntoa(sin.sin_addr));
9426980Skarels 			continue;
9526980Skarels 		}
9654576Sleres 		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
9726980Skarels 		sigsetmask(oldmask);
986438Swnj 		return (-1);
996438Swnj 	}
1009191Ssam 	lport--;
1016438Swnj 	if (fd2p == 0) {
1026438Swnj 		write(s, "", 1);
10310259Ssam 		lport = 0;
1046438Swnj 	} else {
1056438Swnj 		char num[8];
10610259Ssam 		int s2 = rresvport(&lport), s3;
10754576Sleres 		int len = sizeof(from);
1086438Swnj 
10917485Sralph 		if (s2 < 0)
11017485Sralph 			goto bad;
1119191Ssam 		listen(s2, 1);
11254576Sleres 		(void)snprintf(num, sizeof(num), "%d", lport);
11310594Ssam 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
11454576Sleres 			(void)fprintf(stderr,
11554576Sleres 			    "rcmd: write (setting up stderr): %s\n",
11654576Sleres 			    strerror(errno));
11754576Sleres 			(void)close(s2);
11810594Ssam 			goto bad;
11910594Ssam 		}
12036605Skarels 		FD_ZERO(&reads);
12136605Skarels 		FD_SET(s, &reads);
12236605Skarels 		FD_SET(s2, &reads);
12336605Skarels 		errno = 0;
12454576Sleres 		if (select(32, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)) {
12536605Skarels 			if (errno != 0)
12654576Sleres 				(void)fprintf(stderr,
12754576Sleres 				    "rcmd: select (setting up stderr): %s\n",
12854576Sleres 				    strerror(errno));
12936605Skarels 			else
13054576Sleres 				(void)fprintf(stderr,
13154576Sleres 				"select: protocol failure in circuit setup\n");
13254576Sleres 			(void)close(s2);
13336605Skarels 			goto bad;
13436605Skarels 		}
13546604Sbostic 		s3 = accept(s2, (struct sockaddr *)&from, &len);
13654576Sleres 		(void)close(s2);
13717485Sralph 		if (s3 < 0) {
13854576Sleres 			(void)fprintf(stderr,
13954576Sleres 			    "rcmd: accept: %s\n", strerror(errno));
14010259Ssam 			lport = 0;
1416438Swnj 			goto bad;
1426438Swnj 		}
1439191Ssam 		*fd2p = s3;
1449191Ssam 		from.sin_port = ntohs((u_short)from.sin_port);
1456438Swnj 		if (from.sin_family != AF_INET ||
14636605Skarels 		    from.sin_port >= IPPORT_RESERVED ||
14736605Skarels 		    from.sin_port < IPPORT_RESERVED / 2) {
14854576Sleres 			(void)fprintf(stderr,
1496438Swnj 			    "socket: protocol failure in circuit setup.\n");
1509191Ssam 			goto bad2;
1516438Swnj 		}
1526438Swnj 	}
15354576Sleres 	(void)write(s, locuser, strlen(locuser)+1);
15454576Sleres 	(void)write(s, remuser, strlen(remuser)+1);
15554576Sleres 	(void)write(s, cmd, strlen(cmd)+1);
1566438Swnj 	if (read(s, &c, 1) != 1) {
15754576Sleres 		(void)fprintf(stderr,
15854576Sleres 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
1599191Ssam 		goto bad2;
1606438Swnj 	}
1616438Swnj 	if (c != 0) {
1626438Swnj 		while (read(s, &c, 1) == 1) {
16357435Sbostic 			(void)write(STDERR_FILENO, &c, 1);
1646438Swnj 			if (c == '\n')
1656438Swnj 				break;
1666438Swnj 		}
1679191Ssam 		goto bad2;
1686438Swnj 	}
16926980Skarels 	sigsetmask(oldmask);
1706438Swnj 	return (s);
1719191Ssam bad2:
17210259Ssam 	if (lport)
17354576Sleres 		(void)close(*fd2p);
1749191Ssam bad:
17554576Sleres 	(void)close(s);
17626980Skarels 	sigsetmask(oldmask);
1776438Swnj 	return (-1);
1786438Swnj }
1796438Swnj 
18054576Sleres int
rresvport(alport)18110259Ssam rresvport(alport)
18210259Ssam 	int *alport;
1836438Swnj {
1846438Swnj 	struct sockaddr_in sin;
1856438Swnj 	int s;
1866438Swnj 
1879191Ssam 	sin.sin_family = AF_INET;
18824557Skarels 	sin.sin_addr.s_addr = INADDR_ANY;
18924557Skarels 	s = socket(AF_INET, SOCK_STREAM, 0);
1909191Ssam 	if (s < 0)
1919191Ssam 		return (-1);
1926438Swnj 	for (;;) {
1939191Ssam 		sin.sin_port = htons((u_short)*alport);
19457435Sbostic 		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
1956438Swnj 			return (s);
19624557Skarels 		if (errno != EADDRINUSE) {
19754576Sleres 			(void)close(s);
1986438Swnj 			return (-1);
1996438Swnj 		}
2009191Ssam 		(*alport)--;
2019191Ssam 		if (*alport == IPPORT_RESERVED/2) {
20254576Sleres 			(void)close(s);
20324682Skarels 			errno = EAGAIN;		/* close */
2046438Swnj 			return (-1);
2056438Swnj 		}
2066438Swnj 	}
2076438Swnj }
2086438Swnj 
20954576Sleres int	__check_rhosts_file = 1;
21054576Sleres char	*__rcmd_errstr;
21136323Sbostic 
21254576Sleres int
ruserok(rhost,superuser,ruser,luser)21311236Ssam ruserok(rhost, superuser, ruser, luser)
21446604Sbostic 	const char *rhost, *ruser, *luser;
21511236Ssam 	int superuser;
2166438Swnj {
21754576Sleres 	struct hostent *hp;
21854576Sleres 	u_long addr;
21954576Sleres 	char **ap;
22054576Sleres 
22154576Sleres 	if ((hp = gethostbyname(rhost)) == NULL)
22254576Sleres 		return (-1);
22354576Sleres 	for (ap = hp->h_addr_list; *ap; ++ap) {
22454576Sleres 		bcopy(*ap, &addr, sizeof(addr));
22554576Sleres 		if (iruserok(addr, superuser, ruser, luser) == 0)
22654576Sleres 			return (0);
22754576Sleres 	}
22854576Sleres 	return (-1);
22954576Sleres }
23054576Sleres 
23154576Sleres /*
23254576Sleres  * New .rhosts strategy: We are passed an ip address. We spin through
23354576Sleres  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
23454576Sleres  * has ip addresses, we don't have to trust a nameserver.  When it
23554576Sleres  * contains hostnames, we spin through the list of addresses the nameserver
23654576Sleres  * gives us and look for a match.
23754576Sleres  *
23854576Sleres  * Returns 0 if ok, -1 if not ok.
23954576Sleres  */
24054576Sleres int
iruserok(raddr,superuser,ruser,luser)24154576Sleres iruserok(raddr, superuser, ruser, luser)
24254576Sleres 	u_long raddr;
24354576Sleres 	int superuser;
24454576Sleres 	const char *ruser, *luser;
24554576Sleres {
24654583Sbostic 	register char *cp;
24754583Sbostic 	struct stat sbuf;
24854583Sbostic 	struct passwd *pwd;
2496438Swnj 	FILE *hostf;
25054576Sleres 	uid_t uid;
25154576Sleres 	int first;
25254583Sbostic 	char pbuf[MAXPATHLEN];
2536438Swnj 
25454576Sleres 	first = 1;
25554576Sleres 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
2566438Swnj again:
2576438Swnj 	if (hostf) {
25854576Sleres 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
25954576Sleres 			(void)fclose(hostf);
26054576Sleres 			return (0);
2616438Swnj 		}
26254576Sleres 		(void)fclose(hostf);
2636438Swnj 	}
26454576Sleres 	if (first == 1 && (__check_rhosts_file || superuser)) {
2656438Swnj 		first = 0;
26624662Sbloom 		if ((pwd = getpwnam(luser)) == NULL)
26754576Sleres 			return (-1);
26824662Sbloom 		(void)strcpy(pbuf, pwd->pw_dir);
26924662Sbloom 		(void)strcat(pbuf, "/.rhosts");
27054576Sleres 
27136323Sbostic 		/*
27254576Sleres 		 * Change effective uid while opening .rhosts.  If root and
27354576Sleres 		 * reading an NFS mounted file system, can't read files that
27454576Sleres 		 * are protected read/write owner only.
27536323Sbostic 		 */
27654576Sleres 		uid = geteuid();
27754576Sleres 		(void)seteuid(pwd->pw_uid);
27854576Sleres 		hostf = fopen(pbuf, "r");
27954576Sleres 		(void)seteuid(uid);
28054576Sleres 
28154576Sleres 		if (hostf == NULL)
28254576Sleres 			return (-1);
28354576Sleres 		/*
28454576Sleres 		 * If not a regular file, or is owned by someone other than
28554576Sleres 		 * user or root or if writeable by anyone but the owner, quit.
28654576Sleres 		 */
28754583Sbostic 		cp = NULL;
28854576Sleres 		if (lstat(pbuf, &sbuf) < 0)
28954583Sbostic 			cp = ".rhosts lstat failed";
29057970Sbostic 		else if (!S_ISREG(sbuf.st_mode))
29154583Sbostic 			cp = ".rhosts not regular file";
29254576Sleres 		else if (fstat(fileno(hostf), &sbuf) < 0)
29354583Sbostic 			cp = ".rhosts fstat failed";
29454576Sleres 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
29554583Sbostic 			cp = "bad .rhosts owner";
29657970Sbostic 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
29757970Sbostic 			cp = ".rhosts writeable by other than owner";
29854576Sleres 		/* If there were any problems, quit. */
29954583Sbostic 		if (cp) {
30054583Sbostic 			__rcmd_errstr = cp;
30154576Sleres 			(void)fclose(hostf);
30254576Sleres 			return (-1);
30324662Sbloom 		}
3046438Swnj 		goto again;
3056438Swnj 	}
3066438Swnj 	return (-1);
3076438Swnj }
30824560Sbloom 
30954576Sleres /*
31054576Sleres  * XXX
31154576Sleres  * Don't make static, used by lpd(8).
31254576Sleres  *
31354576Sleres  * Returns 0 if ok, -1 if not ok.
31454576Sleres  */
31554576Sleres int
__ivaliduser(hostf,raddr,luser,ruser)31654576Sleres __ivaliduser(hostf, raddr, luser, ruser)
31736323Sbostic 	FILE *hostf;
31854576Sleres 	u_long raddr;
31954576Sleres 	const char *luser, *ruser;
32027734Sbloom {
32154576Sleres 	register char *user, *p;
32266373Sbostic 	int ch;
323*66454Sbostic 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
32427734Sbloom 
325*66454Sbostic 	while (fgets(buf, sizeof(buf), hostf)) {
326*66454Sbostic 		p = buf;
32766373Sbostic 		/* Skip lines that are too long. */
32866373Sbostic 		if (strchr(p, '\n') == NULL) {
32966373Sbostic 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
33066373Sbostic 			continue;
33166373Sbostic 		}
33227734Sbloom 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
33327734Sbloom 			*p = isupper(*p) ? tolower(*p) : *p;
33427734Sbloom 			p++;
33527734Sbloom 		}
33627734Sbloom 		if (*p == ' ' || *p == '\t') {
33727734Sbloom 			*p++ = '\0';
33827734Sbloom 			while (*p == ' ' || *p == '\t')
33927734Sbloom 				p++;
34027734Sbloom 			user = p;
34154576Sleres 			while (*p != '\n' && *p != ' ' &&
34254576Sleres 			    *p != '\t' && *p != '\0')
34327734Sbloom 				p++;
34427734Sbloom 		} else
34527734Sbloom 			user = p;
34627734Sbloom 		*p = '\0';
347*66454Sbostic 		if (__icheckhost(raddr, buf) &&
34854576Sleres 		    strcmp(ruser, *user ? user : luser) == 0) {
34927734Sbloom 			return (0);
35027734Sbloom 		}
35127734Sbloom 	}
35227734Sbloom 	return (-1);
35327734Sbloom }
35427734Sbloom 
35554576Sleres /*
35654576Sleres  * Returns "true" if match, 0 if no match.
35754576Sleres  */
35854576Sleres static int
__icheckhost(raddr,lhost)35954576Sleres __icheckhost(raddr, lhost)
36054576Sleres 	u_long raddr;
36154576Sleres 	register char *lhost;
36224560Sbloom {
36354576Sleres 	register struct hostent *hp;
36454576Sleres 	register u_long laddr;
36554576Sleres 	register char **pp;
36624560Sbloom 
36754576Sleres 	/* Try for raw ip address first. */
36854576Sleres 	if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1)
36954576Sleres 		return (raddr == laddr);
37054576Sleres 
37154576Sleres 	/* Better be a hostname. */
37254576Sleres 	if ((hp = gethostbyname(lhost)) == NULL)
37354576Sleres 		return (0);
37454576Sleres 
37554576Sleres 	/* Spin through ip addresses. */
37654576Sleres 	for (pp = hp->h_addr_list; *pp; ++pp)
37754576Sleres 		if (!bcmp(&raddr, *pp, sizeof(u_long)))
37854576Sleres 			return (1);
37954576Sleres 
38054576Sleres 	/* No match. */
38154576Sleres 	return (0);
38224560Sbloom }
383