rcmd.c revision 63682
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1983, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
3252859Sache *
3363682Sume * $FreeBSD: head/lib/libc/net/rcmd.c 63682 2000-07-20 18:49:35Z ume $
341573Srgrimes */
351573Srgrimes
361573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
371573Srgrimesstatic char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
381573Srgrimes#endif /* LIBC_SCCS and not lint */
391573Srgrimes
401573Srgrimes#include <sys/param.h>
411573Srgrimes#include <sys/socket.h>
421573Srgrimes#include <sys/stat.h>
431573Srgrimes
441573Srgrimes#include <netinet/in.h>
451573Srgrimes#include <arpa/inet.h>
461573Srgrimes
471573Srgrimes#include <signal.h>
481573Srgrimes#include <fcntl.h>
491573Srgrimes#include <netdb.h>
501573Srgrimes#include <unistd.h>
511573Srgrimes#include <pwd.h>
521573Srgrimes#include <errno.h>
531573Srgrimes#include <stdio.h>
541573Srgrimes#include <ctype.h>
551573Srgrimes#include <string.h>
569978Swpaul#ifdef YP
579978Swpaul#include <rpc/rpc.h>
589978Swpaul#include <rpcsvc/yp_prot.h>
599978Swpaul#include <rpcsvc/ypclnt.h>
609978Swpaul#endif
6163682Sume#include <arpa/nameser.h>
621573Srgrimes
6355918Sshin/* wrapper for KAME-special getnameinfo() */
6455918Sshin#ifndef NI_WITHSCOPEID
6555918Sshin#define NI_WITHSCOPEID	0
6655918Sshin#endif
6755918Sshin
6817141Sjkhextern int innetgr __P(( const char *, const char *, const char *, const char * ));
6917141Sjkh
702592Scsgr#define max(a, b)	((a > b) ? a : b)
712592Scsgr
7256939Sshinstatic int __iruserok_af __P((void *, int, const char *, const char *, int));
7339979Sdfrint	__ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
7455918Sshinstatic int __icheckhost __P((void *, char *, int, int));
751573Srgrimes
7655918Sshinchar paddr[INET6_ADDRSTRLEN];
7755918Sshin
781573Srgrimesint
791573Srgrimesrcmd(ahost, rport, locuser, remuser, cmd, fd2p)
801573Srgrimes	char **ahost;
811573Srgrimes	u_short rport;
821573Srgrimes	const char *locuser, *remuser, *cmd;
831573Srgrimes	int *fd2p;
841573Srgrimes{
8556590Sshin	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
8656590Sshin}
8756590Sshin
8856590Sshinint
8956590Sshinrcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
9056590Sshin	char **ahost;
9156590Sshin	u_short rport;
9256590Sshin	const char *locuser, *remuser, *cmd;
9356590Sshin	int *fd2p;
9456590Sshin	int af;
9556590Sshin{
9655918Sshin	struct addrinfo hints, *res, *ai;
9755918Sshin	struct sockaddr_storage from;
981573Srgrimes	fd_set reads;
991573Srgrimes	long oldmask;
1001573Srgrimes	pid_t pid;
10155918Sshin	int s, aport, lport, timo, error;
1021573Srgrimes	char c;
10355918Sshin	int refused;
10463682Sume	char num[8];
10563682Sume	static char canonnamebuf[MAXDNAME];	/* is it proper here? */
1061573Srgrimes
1071573Srgrimes	pid = getpid();
10855918Sshin
10955918Sshin	memset(&hints, 0, sizeof(hints));
11055918Sshin	hints.ai_flags = AI_CANONNAME;
11156590Sshin	hints.ai_family = af;
11255918Sshin	hints.ai_socktype = SOCK_STREAM;
11355918Sshin	hints.ai_protocol = 0;
11455918Sshin	(void)snprintf(num, sizeof(num), "%d", ntohs(rport));
11555918Sshin	error = getaddrinfo(*ahost, num, &hints, &res);
11655918Sshin	if (error) {
11755918Sshin		fprintf(stderr, "rcmd: getaddrinfo: %s\n",
11855918Sshin			gai_strerror(error));
11955918Sshin		if (error == EAI_SYSTEM)
12055918Sshin			fprintf(stderr, "rcmd: getaddrinfo: %s\n",
12155918Sshin				strerror(errno));
1221573Srgrimes		return (-1);
1231573Srgrimes	}
12463682Sume
12563682Sume	if (res->ai_canonname
12663682Sume	 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
12763682Sume		strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
12863682Sume		*ahost = canonnamebuf;
12963682Sume	}
13055918Sshin	ai = res;
13155918Sshin	refused = 0;
1321573Srgrimes	oldmask = sigblock(sigmask(SIGURG));
1331573Srgrimes	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
13455918Sshin		s = rresvport_af(&lport, ai->ai_family);
1351573Srgrimes		if (s < 0) {
13657123Sshin			if (errno != EAGAIN && ai->ai_next) {
13757123Sshin				ai = ai->ai_next;
13857123Sshin				continue;
13957123Sshin			}
1401573Srgrimes			if (errno == EAGAIN)
1411573Srgrimes				(void)fprintf(stderr,
1421573Srgrimes				    "rcmd: socket: All ports in use\n");
1431573Srgrimes			else
1441573Srgrimes				(void)fprintf(stderr, "rcmd: socket: %s\n",
1451573Srgrimes				    strerror(errno));
14657123Sshin			freeaddrinfo(res);
1471573Srgrimes			sigsetmask(oldmask);
1481573Srgrimes			return (-1);
1491573Srgrimes		}
15056698Sjasone		_fcntl(s, F_SETOWN, pid);
15155918Sshin		if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
1521573Srgrimes			break;
15356698Sjasone		(void)_close(s);
1541573Srgrimes		if (errno == EADDRINUSE) {
1551573Srgrimes			lport--;
1561573Srgrimes			continue;
1571573Srgrimes		}
15855918Sshin		if (errno == ECONNREFUSED)
15955918Sshin			refused = 1;
16055918Sshin		if (ai->ai_next != NULL) {
1611573Srgrimes			int oerrno = errno;
1621573Srgrimes
16355918Sshin			getnameinfo(ai->ai_addr, ai->ai_addrlen,
16455918Sshin				    paddr, sizeof(paddr),
16555918Sshin				    NULL, 0,
16655918Sshin				    NI_NUMERICHOST|NI_WITHSCOPEID);
1671573Srgrimes			(void)fprintf(stderr, "connect to address %s: ",
16855918Sshin				      paddr);
1691573Srgrimes			errno = oerrno;
1701573Srgrimes			perror(0);
17155918Sshin			ai = ai->ai_next;
17255918Sshin			getnameinfo(ai->ai_addr, ai->ai_addrlen,
17355918Sshin				    paddr, sizeof(paddr),
17455918Sshin				    NULL, 0,
17555918Sshin				    NI_NUMERICHOST|NI_WITHSCOPEID);
17655918Sshin			fprintf(stderr, "Trying %s...\n", paddr);
1771573Srgrimes			continue;
1781573Srgrimes		}
17955918Sshin		if (refused && timo <= 16) {
18056698Sjasone			struct timespec time_to_sleep, time_remaining;
18156698Sjasone
18256698Sjasone			time_to_sleep.tv_sec = timo;
18356698Sjasone			time_to_sleep.tv_nsec = 0;
18456698Sjasone			(void)_nanosleep(&time_to_sleep, &time_remaining);
18556698Sjasone
18655918Sshin			timo *= 2;
18755918Sshin			ai = res;
18855918Sshin			refused = 0;
18955918Sshin			continue;
19055918Sshin		}
19162939Speter		(void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno));
19255918Sshin		freeaddrinfo(res);
1931573Srgrimes		sigsetmask(oldmask);
1941573Srgrimes		return (-1);
1951573Srgrimes	}
1961573Srgrimes	lport--;
1971573Srgrimes	if (fd2p == 0) {
19856698Sjasone		_write(s, "", 1);
1991573Srgrimes		lport = 0;
2001573Srgrimes	} else {
2011573Srgrimes		char num[8];
20255918Sshin		int s2 = rresvport_af(&lport, ai->ai_family), s3;
20355918Sshin		int len = ai->ai_addrlen;
2042592Scsgr		int nfds;
2051573Srgrimes
2061573Srgrimes		if (s2 < 0)
2071573Srgrimes			goto bad;
2081573Srgrimes		listen(s2, 1);
2091573Srgrimes		(void)snprintf(num, sizeof(num), "%d", lport);
21056698Sjasone		if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
2111573Srgrimes			(void)fprintf(stderr,
2121573Srgrimes			    "rcmd: write (setting up stderr): %s\n",
2131573Srgrimes			    strerror(errno));
21456698Sjasone			(void)_close(s2);
2151573Srgrimes			goto bad;
2161573Srgrimes		}
2172592Scsgr		nfds = max(s, s2)+1;
2182592Scsgr		if(nfds > FD_SETSIZE) {
2192592Scsgr			fprintf(stderr, "rcmd: too many files\n");
22056698Sjasone			(void)_close(s2);
2212592Scsgr			goto bad;
2222592Scsgr		}
22317543Speteragain:
2241573Srgrimes		FD_ZERO(&reads);
2251573Srgrimes		FD_SET(s, &reads);
2261573Srgrimes		FD_SET(s2, &reads);
2271573Srgrimes		errno = 0;
2282592Scsgr		if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
2291573Srgrimes			if (errno != 0)
2301573Srgrimes				(void)fprintf(stderr,
2311573Srgrimes				    "rcmd: select (setting up stderr): %s\n",
2321573Srgrimes				    strerror(errno));
2331573Srgrimes			else
2341573Srgrimes				(void)fprintf(stderr,
2351573Srgrimes				"select: protocol failure in circuit setup\n");
23656698Sjasone			(void)_close(s2);
2371573Srgrimes			goto bad;
2381573Srgrimes		}
2391573Srgrimes		s3 = accept(s2, (struct sockaddr *)&from, &len);
24055918Sshin		switch (from.ss_family) {
24155918Sshin		case AF_INET:
24255918Sshin			aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
24355918Sshin			break;
24455918Sshin#ifdef INET6
24555918Sshin		case AF_INET6:
24655918Sshin			aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
24755918Sshin			break;
24855918Sshin#endif
24955918Sshin		default:
25055918Sshin			aport = 0;	/* error */
25155918Sshin			break;
25255918Sshin		}
25317543Speter		/*
25417543Speter		 * XXX careful for ftp bounce attacks. If discovered, shut them
25517543Speter		 * down and check for the real auxiliary channel to connect.
25617543Speter		 */
25755918Sshin		if (aport == 20) {
25856698Sjasone			_close(s3);
25917543Speter			goto again;
26017543Speter		}
26156698Sjasone		(void)_close(s2);
2621573Srgrimes		if (s3 < 0) {
2631573Srgrimes			(void)fprintf(stderr,
2641573Srgrimes			    "rcmd: accept: %s\n", strerror(errno));
2651573Srgrimes			lport = 0;
2661573Srgrimes			goto bad;
2671573Srgrimes		}
2681573Srgrimes		*fd2p = s3;
26955918Sshin		if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
2701573Srgrimes			(void)fprintf(stderr,
2711573Srgrimes			    "socket: protocol failure in circuit setup.\n");
2721573Srgrimes			goto bad2;
2731573Srgrimes		}
2741573Srgrimes	}
27556698Sjasone	(void)_write(s, locuser, strlen(locuser)+1);
27656698Sjasone	(void)_write(s, remuser, strlen(remuser)+1);
27756698Sjasone	(void)_write(s, cmd, strlen(cmd)+1);
27856698Sjasone	if (_read(s, &c, 1) != 1) {
2791573Srgrimes		(void)fprintf(stderr,
2801573Srgrimes		    "rcmd: %s: %s\n", *ahost, strerror(errno));
2811573Srgrimes		goto bad2;
2821573Srgrimes	}
2831573Srgrimes	if (c != 0) {
28456698Sjasone		while (_read(s, &c, 1) == 1) {
28556698Sjasone			(void)_write(STDERR_FILENO, &c, 1);
2861573Srgrimes			if (c == '\n')
2871573Srgrimes				break;
2881573Srgrimes		}
2891573Srgrimes		goto bad2;
2901573Srgrimes	}
2911573Srgrimes	sigsetmask(oldmask);
29255918Sshin	freeaddrinfo(res);
2931573Srgrimes	return (s);
2941573Srgrimesbad2:
2951573Srgrimes	if (lport)
29656698Sjasone		(void)_close(*fd2p);
2971573Srgrimesbad:
29856698Sjasone	(void)_close(s);
2991573Srgrimes	sigsetmask(oldmask);
30055918Sshin	freeaddrinfo(res);
3011573Srgrimes	return (-1);
3021573Srgrimes}
3031573Srgrimes
3041573Srgrimesint
30555918Sshinrresvport(port)
30655918Sshin	int *port;
3071573Srgrimes{
30855918Sshin	return rresvport_af(port, AF_INET);
30955918Sshin}
3101573Srgrimes
31155918Sshinint
31255918Sshinrresvport_af(alport, family)
31355918Sshin	int *alport, family;
31455918Sshin{
31555918Sshin	int i, s, len, err;
31655918Sshin	struct sockaddr_storage ss;
31755918Sshin	u_short *sport;
31855918Sshin
31955918Sshin	memset(&ss, 0, sizeof(ss));
32055918Sshin	ss.ss_family = family;
32155918Sshin	switch (family) {
32255918Sshin	case AF_INET:
32355918Sshin		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
32455918Sshin		sport = &((struct sockaddr_in *)&ss)->sin_port;
32555918Sshin		((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
32655918Sshin		break;
32755918Sshin#ifdef INET6
32855918Sshin	case AF_INET6:
32955918Sshin		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
33055918Sshin		sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
33155918Sshin		((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
33255918Sshin		break;
33355918Sshin#endif
33455918Sshin	default:
33555918Sshin		errno = EAFNOSUPPORT;
33655918Sshin		return -1;
33755918Sshin	}
33855918Sshin
33955918Sshin	s = socket(ss.ss_family, SOCK_STREAM, 0);
3401573Srgrimes	if (s < 0)
3411573Srgrimes		return (-1);
34217543Speter#if 0 /* compat_exact_traditional_rresvport_semantics */
34317543Speter	sin.sin_port = htons((u_short)*alport);
34417543Speter	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
34517543Speter		return (s);
34617543Speter	if (errno != EADDRINUSE) {
34756698Sjasone		(void)_close(s);
34817543Speter		return (-1);
34916034Speter	}
35017543Speter#endif
35155918Sshin	*sport = 0;
35256636Sshin	if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
35356698Sjasone		(void)_close(s);
35417543Speter		return (-1);
3551573Srgrimes	}
35655918Sshin	*alport = (int)ntohs(*sport);
35717543Speter	return (s);
3581573Srgrimes}
3591573Srgrimes
3601573Srgrimesint	__check_rhosts_file = 1;
3611573Srgrimeschar	*__rcmd_errstr;
3621573Srgrimes
3631573Srgrimesint
3641573Srgrimesruserok(rhost, superuser, ruser, luser)
3651573Srgrimes	const char *rhost, *ruser, *luser;
3661573Srgrimes	int superuser;
3671573Srgrimes{
36856939Sshin	struct addrinfo hints, *res, *r;
36956939Sshin	int error;
37055918Sshin
37156939Sshin	memset(&hints, 0, sizeof(hints));
37256939Sshin	hints.ai_family = PF_UNSPEC;
37356939Sshin	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
37456939Sshin	error = getaddrinfo(rhost, "0", &hints, &res);
37556939Sshin	if (error)
37656939Sshin		return (-1);
3771573Srgrimes
37856939Sshin	for (r = res; r; r = r->ai_next) {
37956939Sshin		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
38056939Sshin		    luser) == 0) {
38156939Sshin			freeaddrinfo(res);
38256939Sshin			return (0);
38355918Sshin		}
3841573Srgrimes	}
38556939Sshin	freeaddrinfo(res);
38656939Sshin	return (-1);
3871573Srgrimes}
3881573Srgrimes
3891573Srgrimes/*
3901573Srgrimes * New .rhosts strategy: We are passed an ip address. We spin through
3911573Srgrimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only
3921573Srgrimes * has ip addresses, we don't have to trust a nameserver.  When it
3931573Srgrimes * contains hostnames, we spin through the list of addresses the nameserver
3941573Srgrimes * gives us and look for a match.
3951573Srgrimes *
3961573Srgrimes * Returns 0 if ok, -1 if not ok.
3971573Srgrimes */
3981573Srgrimesint
3991573Srgrimesiruserok(raddr, superuser, ruser, luser)
40039979Sdfr	unsigned long raddr;
4011573Srgrimes	int superuser;
4021573Srgrimes	const char *ruser, *luser;
4031573Srgrimes{
40456939Sshin	return __iruserok_af(&raddr, superuser, ruser, luser, AF_INET);
40555918Sshin}
40655918Sshin
40756939Sshin/* Other AF support extension of iruserok. */
40856939Sshinstatic int
40956939Sshin__iruserok_af(raddr, superuser, ruser, luser, af)
41055918Sshin	void *raddr;
41155918Sshin	int superuser;
41255918Sshin	const char *ruser, *luser;
41355918Sshin	int af;
41455918Sshin{
4151573Srgrimes	register char *cp;
4161573Srgrimes	struct stat sbuf;
4171573Srgrimes	struct passwd *pwd;
4181573Srgrimes	FILE *hostf;
4191573Srgrimes	uid_t uid;
4201573Srgrimes	int first;
4211573Srgrimes	char pbuf[MAXPATHLEN];
42255918Sshin	int len = 0;
4231573Srgrimes
42455918Sshin	switch (af) {
42555918Sshin	case AF_INET:
42655918Sshin		len = sizeof(struct in_addr);
42755918Sshin		break;
42855918Sshin#ifdef INET6
42955918Sshin	case AF_INET6:
43055918Sshin		len = sizeof(struct in6_addr);
43155918Sshin		break;
43255918Sshin#endif
43355918Sshin	}
43455918Sshin
4351573Srgrimes	first = 1;
4361573Srgrimes	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
4371573Srgrimesagain:
4381573Srgrimes	if (hostf) {
43955918Sshin		if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len)
44055918Sshin		    == 0) {
4411573Srgrimes			(void)fclose(hostf);
4421573Srgrimes			return (0);
4431573Srgrimes		}
4441573Srgrimes		(void)fclose(hostf);
4451573Srgrimes	}
4461573Srgrimes	if (first == 1 && (__check_rhosts_file || superuser)) {
4471573Srgrimes		first = 0;
4481573Srgrimes		if ((pwd = getpwnam(luser)) == NULL)
4491573Srgrimes			return (-1);
4501573Srgrimes		(void)strcpy(pbuf, pwd->pw_dir);
4511573Srgrimes		(void)strcat(pbuf, "/.rhosts");
4521573Srgrimes
4531573Srgrimes		/*
4541573Srgrimes		 * Change effective uid while opening .rhosts.  If root and
4551573Srgrimes		 * reading an NFS mounted file system, can't read files that
4561573Srgrimes		 * are protected read/write owner only.
4571573Srgrimes		 */
4581573Srgrimes		uid = geteuid();
4591573Srgrimes		(void)seteuid(pwd->pw_uid);
4601573Srgrimes		hostf = fopen(pbuf, "r");
4611573Srgrimes		(void)seteuid(uid);
4621573Srgrimes
4631573Srgrimes		if (hostf == NULL)
4641573Srgrimes			return (-1);
4651573Srgrimes		/*
4661573Srgrimes		 * If not a regular file, or is owned by someone other than
4671573Srgrimes		 * user or root or if writeable by anyone but the owner, quit.
4681573Srgrimes		 */
4691573Srgrimes		cp = NULL;
4701573Srgrimes		if (lstat(pbuf, &sbuf) < 0)
4711573Srgrimes			cp = ".rhosts lstat failed";
4721573Srgrimes		else if (!S_ISREG(sbuf.st_mode))
4731573Srgrimes			cp = ".rhosts not regular file";
4741573Srgrimes		else if (fstat(fileno(hostf), &sbuf) < 0)
4751573Srgrimes			cp = ".rhosts fstat failed";
4761573Srgrimes		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
4771573Srgrimes			cp = "bad .rhosts owner";
4781573Srgrimes		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
4791573Srgrimes			cp = ".rhosts writeable by other than owner";
4801573Srgrimes		/* If there were any problems, quit. */
4811573Srgrimes		if (cp) {
4821573Srgrimes			__rcmd_errstr = cp;
4831573Srgrimes			(void)fclose(hostf);
4841573Srgrimes			return (-1);
4851573Srgrimes		}
4861573Srgrimes		goto again;
4871573Srgrimes	}
4881573Srgrimes	return (-1);
4891573Srgrimes}
4901573Srgrimes
4911573Srgrimes/*
49256939Sshin * AF independent extension of iruserok. We are passed an sockaddr, and
49356939Sshin * then call iruserok_af() as the type of sockaddr.
49456939Sshin *
49556939Sshin * Returns 0 if ok, -1 if not ok.
49656939Sshin */
49756939Sshinint
49856939Sshiniruserok_sa(addr, addrlen, superuser, ruser, luser)
49956939Sshin	const void *addr;
50056939Sshin	int addrlen;
50156939Sshin	int superuser;
50256939Sshin	const char *ruser, *luser;
50356939Sshin{
50456939Sshin	struct sockaddr *sa;
50556939Sshin	void *raddr = NULL;
50656939Sshin
50756939Sshin	sa = (struct sockaddr *)addr;
50856939Sshin	switch (sa->sa_family) {
50956939Sshin	case AF_INET:
51056939Sshin		raddr = &((struct sockaddr_in *)sa)->sin_addr;
51156939Sshin		break;
51256939Sshin#ifdef INET6
51356939Sshin	case AF_INET6:
51456939Sshin		raddr = &((struct sockaddr_in6 *)sa)->sin6_addr;
51556939Sshin		break;
51656939Sshin#endif
51756939Sshin	}
51856939Sshin
51956939Sshin	__iruserok_af(raddr, superuser, ruser, luser, sa->sa_family);
52056939Sshin}
52156939Sshin
52256939Sshin/*
5231573Srgrimes * XXX
5241573Srgrimes * Don't make static, used by lpd(8).
5251573Srgrimes *
5261573Srgrimes * Returns 0 if ok, -1 if not ok.
5271573Srgrimes */
5281573Srgrimesint
5291573Srgrimes__ivaliduser(hostf, raddr, luser, ruser)
5301573Srgrimes	FILE *hostf;
53139979Sdfr	u_int32_t raddr;
5321573Srgrimes	const char *luser, *ruser;
5331573Srgrimes{
53455918Sshin	return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET,
53555918Sshin			       sizeof(raddr));
53655918Sshin}
53755918Sshin
53855918Sshinint
53955918Sshin__ivaliduser_af(hostf, raddr, luser, ruser, af, len)
54055918Sshin	FILE *hostf;
54155918Sshin	void *raddr;
54255918Sshin	const char *luser, *ruser;
54355918Sshin	int af, len;
54455918Sshin{
5451573Srgrimes	register char *user, *p;
5461573Srgrimes	int ch;
5471573Srgrimes	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
54810059Swpaul	char hname[MAXHOSTNAMELEN];
5497183Swpaul	struct hostent *hp;
5507183Swpaul	/* Presumed guilty until proven innocent. */
5517183Swpaul	int userok = 0, hostok = 0;
55255918Sshin	int h_error;
5539978Swpaul#ifdef YP
5549978Swpaul	char *ypdomain;
5551573Srgrimes
5569978Swpaul	if (yp_get_default_domain(&ypdomain))
5579978Swpaul		ypdomain = NULL;
5589978Swpaul#else
5599978Swpaul#define	ypdomain NULL
5609978Swpaul#endif
5617183Swpaul	/* We need to get the damn hostname back for netgroup matching. */
56255918Sshin	if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL)
5637183Swpaul		return (-1);
56423128Simp	strncpy(hname, hp->h_name, sizeof(hname));
56523128Simp	hname[sizeof(hname) - 1] = '\0';
56655918Sshin	freehostent(hp);
5677183Swpaul
5681573Srgrimes	while (fgets(buf, sizeof(buf), hostf)) {
5691573Srgrimes		p = buf;
5701573Srgrimes		/* Skip lines that are too long. */
57158154Sbsd		if (strchr(p, '\n') == NULL) {
5721573Srgrimes			while ((ch = getc(hostf)) != '\n' && ch != EOF);
5731573Srgrimes			continue;
5741573Srgrimes		}
5759552Speter		if (*p == '\n' || *p == '#') {
5769552Speter			/* comment... */
5779552Speter			continue;
5789552Speter		}
5791573Srgrimes		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
58052859Sache			*p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
5811573Srgrimes			p++;
5821573Srgrimes		}
5831573Srgrimes		if (*p == ' ' || *p == '\t') {
5841573Srgrimes			*p++ = '\0';
5851573Srgrimes			while (*p == ' ' || *p == '\t')
5861573Srgrimes				p++;
5871573Srgrimes			user = p;
5881573Srgrimes			while (*p != '\n' && *p != ' ' &&
5891573Srgrimes			    *p != '\t' && *p != '\0')
5901573Srgrimes				p++;
5911573Srgrimes		} else
5921573Srgrimes			user = p;
5931573Srgrimes		*p = '\0';
5947183Swpaul		/*
5957183Swpaul		 * Do +/- and +@/-@ checking. This looks really nasty,
5967183Swpaul		 * but it matches SunOS's behavior so far as I can tell.
5977183Swpaul		 */
5987183Swpaul		switch(buf[0]) {
5997183Swpaul		case '+':
6007183Swpaul			if (!buf[1]) {     /* '+' matches all hosts */
6017183Swpaul				hostok = 1;
6027183Swpaul				break;
6037183Swpaul			}
6047183Swpaul			if (buf[1] == '@')  /* match a host by netgroup */
60510059Swpaul				hostok = innetgr((char *)&buf[2],
60610059Swpaul					(char *)&hname, NULL, ypdomain);
6077183Swpaul			else		/* match a host by addr */
60855918Sshin				hostok = __icheckhost(raddr,(char *)&buf[1],
60955918Sshin						      af, len);
6107183Swpaul			break;
6117183Swpaul		case '-':     /* reject '-' hosts and all their users */
6127183Swpaul			if (buf[1] == '@') {
6137183Swpaul				if (innetgr((char *)&buf[2],
61410059Swpaul					      (char *)&hname, NULL, ypdomain))
6157183Swpaul					return(-1);
6167183Swpaul			} else {
61755918Sshin				if (__icheckhost(raddr,(char *)&buf[1],af,len))
6187183Swpaul					return(-1);
6197183Swpaul			}
6207183Swpaul			break;
6217183Swpaul		default:  /* if no '+' or '-', do a simple match */
62255918Sshin			hostok = __icheckhost(raddr, buf, af, len);
6237183Swpaul			break;
6241573Srgrimes		}
6257183Swpaul		switch(*user) {
6267183Swpaul		case '+':
6277183Swpaul			if (!*(user+1)) {      /* '+' matches all users */
6287183Swpaul				userok = 1;
6297183Swpaul				break;
6307183Swpaul			}
6317183Swpaul			if (*(user+1) == '@')  /* match a user by netgroup */
6329978Swpaul				userok = innetgr(user+2, NULL, ruser, ypdomain);
6337183Swpaul			else	   /* match a user by direct specification */
6347183Swpaul				userok = !(strcmp(ruser, user+1));
6357183Swpaul			break;
6367183Swpaul		case '-': 		/* if we matched a hostname, */
6377183Swpaul			if (hostok) {   /* check for user field rejections */
6387183Swpaul				if (!*(user+1))
6397183Swpaul					return(-1);
6407183Swpaul				if (*(user+1) == '@') {
6417183Swpaul					if (innetgr(user+2, NULL,
6429978Swpaul							ruser, ypdomain))
6437183Swpaul						return(-1);
6447183Swpaul				} else {
6457183Swpaul					if (!strcmp(ruser, user+1))
6467183Swpaul						return(-1);
6477183Swpaul				}
6487183Swpaul			}
6497183Swpaul			break;
6507183Swpaul		default:	/* no rejections: try to match the user */
6517183Swpaul			if (hostok)
6527183Swpaul				userok = !(strcmp(ruser,*user ? user : luser));
6537183Swpaul			break;
6547183Swpaul		}
6557183Swpaul		if (hostok && userok)
6567183Swpaul			return(0);
6571573Srgrimes	}
6581573Srgrimes	return (-1);
6591573Srgrimes}
6601573Srgrimes
6611573Srgrimes/*
6621573Srgrimes * Returns "true" if match, 0 if no match.
6631573Srgrimes */
6641573Srgrimesstatic int
66555918Sshin__icheckhost(raddr, lhost, af, len)
66655918Sshin	void *raddr;
6671573Srgrimes	register char *lhost;
66855918Sshin	int af, len;
6691573Srgrimes{
6701573Srgrimes	register struct hostent *hp;
67155918Sshin	char laddr[BUFSIZ]; /* xxx */
6721573Srgrimes	register char **pp;
67355918Sshin	int h_error;
67455918Sshin	int match;
6751573Srgrimes
6761573Srgrimes	/* Try for raw ip address first. */
67755918Sshin	if (inet_pton(af, lhost, laddr) == 1) {
67855918Sshin		if (memcmp(raddr, laddr, len) == 0)
67955918Sshin			return (1);
68055918Sshin		else
68155918Sshin			return (0);
68255918Sshin	}
6831573Srgrimes
6841573Srgrimes	/* Better be a hostname. */
68556939Sshin	if ((hp = getipnodebyname(lhost, af, AI_ALL|AI_DEFAULT, &h_error))
68656939Sshin	    == NULL)
6871573Srgrimes		return (0);
6881573Srgrimes
6891573Srgrimes	/* Spin through ip addresses. */
69055918Sshin	match = 0;
6911573Srgrimes	for (pp = hp->h_addr_list; *pp; ++pp)
69255918Sshin		if (!bcmp(raddr, *pp, len)) {
69355918Sshin			match = 1;
69455918Sshin			break;
69555918Sshin		}
6961573Srgrimes
69755918Sshin	freehostent(hp);
69855918Sshin	return (match);
6991573Srgrimes}
700