xref: /freebsd/lib/libc/net/rcmd.c (revision 4f101318489d72d2eeada2463a8fc3d105855628)
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 */
37333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
38333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3958f0484fSRodney W. Grimes 
40d201fe46SDaniel Eischen #include "namespace.h"
4158f0484fSRodney W. Grimes #include <sys/param.h>
4258f0484fSRodney W. Grimes #include <sys/socket.h>
4358f0484fSRodney W. Grimes #include <sys/stat.h>
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes #include <netinet/in.h>
4658f0484fSRodney W. Grimes #include <arpa/inet.h>
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes #include <signal.h>
4958f0484fSRodney W. Grimes #include <fcntl.h>
5058f0484fSRodney W. Grimes #include <netdb.h>
51d4567212SWarner Losh #include <stdlib.h>
5258f0484fSRodney W. Grimes #include <unistd.h>
5358f0484fSRodney W. Grimes #include <pwd.h>
5458f0484fSRodney W. Grimes #include <errno.h>
5558f0484fSRodney W. Grimes #include <stdio.h>
5658f0484fSRodney W. Grimes #include <ctype.h>
5758f0484fSRodney W. Grimes #include <string.h>
581e890b05SBill Paul #include <rpc/rpc.h>
596c58990dSBjoern A. Zeeb #ifdef YP
601e890b05SBill Paul #include <rpcsvc/yp_prot.h>
611e890b05SBill Paul #include <rpcsvc/ypclnt.h>
621e890b05SBill Paul #endif
63c98e299eSHajimu UMEMOTO #include <arpa/nameser.h>
64d201fe46SDaniel Eischen #include "un-namespace.h"
6558f0484fSRodney W. Grimes 
66c05ac53bSDavid E. O'Brien extern int innetgr( const char *, const char *, const char *, const char * );
6751295a4dSJordan K. Hubbard 
68d1f32ba5SGeoff Rehmet #define max(a, b)	((a > b) ? a : b)
69d1f32ba5SGeoff Rehmet 
70c05ac53bSDavid E. O'Brien int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
711372519bSDavid E. O'Brien int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
721372519bSDavid E. O'Brien int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
731372519bSDavid E. O'Brien     const char *);
741372519bSDavid E. O'Brien static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
7542b4f28eSYoshinobu Inoue 
76eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST];
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes int
7958f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
8058f0484fSRodney W. Grimes 	char **ahost;
8158f0484fSRodney W. Grimes 	u_short rport;
8258f0484fSRodney W. Grimes 	const char *locuser, *remuser, *cmd;
8358f0484fSRodney W. Grimes 	int *fd2p;
8458f0484fSRodney W. Grimes {
850cac72f4SYoshinobu Inoue 	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
860cac72f4SYoshinobu Inoue }
870cac72f4SYoshinobu Inoue 
880cac72f4SYoshinobu Inoue int
890cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
900cac72f4SYoshinobu Inoue 	char **ahost;
910cac72f4SYoshinobu Inoue 	u_short rport;
920cac72f4SYoshinobu Inoue 	const char *locuser, *remuser, *cmd;
930cac72f4SYoshinobu Inoue 	int *fd2p;
940cac72f4SYoshinobu Inoue 	int af;
950cac72f4SYoshinobu Inoue {
9642b4f28eSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
9742b4f28eSYoshinobu Inoue 	struct sockaddr_storage from;
9858f0484fSRodney W. Grimes 	fd_set reads;
99d201fe46SDaniel Eischen 	sigset_t oldmask, newmask;
10058f0484fSRodney W. Grimes 	pid_t pid;
10142b4f28eSYoshinobu Inoue 	int s, aport, lport, timo, error;
102d4567212SWarner Losh 	char c, *p;
103eb74b996SHajimu UMEMOTO 	int refused, nres;
104c98e299eSHajimu UMEMOTO 	char num[8];
105c98e299eSHajimu UMEMOTO 	static char canonnamebuf[MAXDNAME];	/* is it proper here? */
10658f0484fSRodney W. Grimes 
107d4567212SWarner Losh 	/* call rcmdsh() with specified remote shell if appropriate. */
108d4567212SWarner Losh 	if (!issetugid() && (p = getenv("RSH"))) {
109d4567212SWarner Losh 		struct servent *sp = getservbyname("shell", "tcp");
110d4567212SWarner Losh 
111d4567212SWarner Losh 		if (sp && sp->s_port == rport)
112d4567212SWarner Losh 			return (rcmdsh(ahost, rport, locuser, remuser,
113d4567212SWarner Losh 			    cmd, p));
114d4567212SWarner Losh 	}
115d4567212SWarner Losh 
116d4567212SWarner Losh 	/* use rsh(1) if non-root and remote port is shell. */
117d4567212SWarner Losh 	if (geteuid()) {
118d4567212SWarner Losh 		struct servent *sp = getservbyname("shell", "tcp");
119d4567212SWarner Losh 
120d4567212SWarner Losh 		if (sp && sp->s_port == rport)
121d4567212SWarner Losh 			return (rcmdsh(ahost, rport, locuser, remuser,
122d4567212SWarner Losh 			    cmd, NULL));
123d4567212SWarner Losh 	}
124d4567212SWarner Losh 
12558f0484fSRodney W. Grimes 	pid = getpid();
12642b4f28eSYoshinobu Inoue 
12742b4f28eSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
12842b4f28eSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
1290cac72f4SYoshinobu Inoue 	hints.ai_family = af;
13042b4f28eSYoshinobu Inoue 	hints.ai_socktype = SOCK_STREAM;
13142b4f28eSYoshinobu Inoue 	hints.ai_protocol = 0;
13242b4f28eSYoshinobu Inoue 	(void)snprintf(num, sizeof(num), "%d", ntohs(rport));
13342b4f28eSYoshinobu Inoue 	error = getaddrinfo(*ahost, num, &hints, &res);
13442b4f28eSYoshinobu Inoue 	if (error) {
13542b4f28eSYoshinobu Inoue 		fprintf(stderr, "rcmd: getaddrinfo: %s\n",
13642b4f28eSYoshinobu Inoue 			gai_strerror(error));
13742b4f28eSYoshinobu Inoue 		if (error == EAI_SYSTEM)
13842b4f28eSYoshinobu Inoue 			fprintf(stderr, "rcmd: getaddrinfo: %s\n",
13942b4f28eSYoshinobu Inoue 				strerror(errno));
14058f0484fSRodney W. Grimes 		return (-1);
14158f0484fSRodney W. Grimes 	}
142c98e299eSHajimu UMEMOTO 
143c98e299eSHajimu UMEMOTO 	if (res->ai_canonname
144c98e299eSHajimu UMEMOTO 	 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
145c98e299eSHajimu UMEMOTO 		strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
146c98e299eSHajimu UMEMOTO 		*ahost = canonnamebuf;
147c98e299eSHajimu UMEMOTO 	}
148eb74b996SHajimu UMEMOTO 	nres = 0;
149eb74b996SHajimu UMEMOTO 	for (ai = res; ai; ai = ai->ai_next)
150eb74b996SHajimu UMEMOTO 		nres++;
15142b4f28eSYoshinobu Inoue 	ai = res;
152eb74b996SHajimu UMEMOTO 	refused = 0;
153d201fe46SDaniel Eischen 	sigemptyset(&newmask);
154d201fe46SDaniel Eischen 	sigaddset(&newmask, SIGURG);
155d201fe46SDaniel Eischen 	_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
15658f0484fSRodney W. Grimes 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
15742b4f28eSYoshinobu Inoue 		s = rresvport_af(&lport, ai->ai_family);
15858f0484fSRodney W. Grimes 		if (s < 0) {
159474ce1d1SYoshinobu Inoue 			if (errno != EAGAIN && ai->ai_next) {
160474ce1d1SYoshinobu Inoue 				ai = ai->ai_next;
161474ce1d1SYoshinobu Inoue 				continue;
162474ce1d1SYoshinobu Inoue 			}
16358f0484fSRodney W. Grimes 			if (errno == EAGAIN)
16458f0484fSRodney W. Grimes 				(void)fprintf(stderr,
16558f0484fSRodney W. Grimes 				    "rcmd: socket: All ports in use\n");
16658f0484fSRodney W. Grimes 			else
16758f0484fSRodney W. Grimes 				(void)fprintf(stderr, "rcmd: socket: %s\n",
16858f0484fSRodney W. Grimes 				    strerror(errno));
16942b4f28eSYoshinobu Inoue 			freeaddrinfo(res);
170d201fe46SDaniel Eischen 			_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
171d201fe46SDaniel Eischen 			    NULL);
17258f0484fSRodney W. Grimes 			return (-1);
17358f0484fSRodney W. Grimes 		}
1749233c4d9SJason Evans 		_fcntl(s, F_SETOWN, pid);
175d201fe46SDaniel Eischen 		if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
17658f0484fSRodney W. Grimes 			break;
1779233c4d9SJason Evans 		(void)_close(s);
17858f0484fSRodney W. Grimes 		if (errno == EADDRINUSE) {
17958f0484fSRodney W. Grimes 			lport--;
18058f0484fSRodney W. Grimes 			continue;
18158f0484fSRodney W. Grimes 		}
182eb74b996SHajimu UMEMOTO 		if (errno == ECONNREFUSED)
183eb74b996SHajimu UMEMOTO 			refused = 1;
184eb74b996SHajimu UMEMOTO 		if (ai->ai_next == NULL && (!refused || timo > 16)) {
185eb74b996SHajimu UMEMOTO 			(void)fprintf(stderr, "%s: %s\n",
186eb74b996SHajimu UMEMOTO 				      *ahost, strerror(errno));
187eb74b996SHajimu UMEMOTO 			freeaddrinfo(res);
188d201fe46SDaniel Eischen 			_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
189d201fe46SDaniel Eischen 			    NULL);
190eb74b996SHajimu UMEMOTO 			return (-1);
1912368b03bSHajimu UMEMOTO 		}
192eb74b996SHajimu UMEMOTO 		if (nres > 1) {
19358f0484fSRodney W. Grimes 			int oerrno = errno;
19458f0484fSRodney W. Grimes 
1954f101318SHajimu UMEMOTO 			getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
1964f101318SHajimu UMEMOTO 			    sizeof(paddr), NULL, 0, NI_NUMERICHOST);
19758f0484fSRodney W. Grimes 			(void)fprintf(stderr, "connect to address %s: ",
19842b4f28eSYoshinobu Inoue 				      paddr);
19958f0484fSRodney W. Grimes 			errno = oerrno;
20058f0484fSRodney W. Grimes 			perror(0);
201eb74b996SHajimu UMEMOTO 		}
202eb74b996SHajimu UMEMOTO 		if ((ai = ai->ai_next) == NULL) {
203eb74b996SHajimu UMEMOTO 			/* refused && timo <= 16 */
204eb74b996SHajimu UMEMOTO 			struct timespec time_to_sleep, time_remaining;
205eb74b996SHajimu UMEMOTO 
206eb74b996SHajimu UMEMOTO 			time_to_sleep.tv_sec = timo;
207eb74b996SHajimu UMEMOTO 			time_to_sleep.tv_nsec = 0;
208eb74b996SHajimu UMEMOTO 			(void)_nanosleep(&time_to_sleep, &time_remaining);
209eb74b996SHajimu UMEMOTO 			timo *= 2;
210eb74b996SHajimu UMEMOTO 			ai = res;
211eb74b996SHajimu UMEMOTO 			refused = 0;
212eb74b996SHajimu UMEMOTO 		}
213eb74b996SHajimu UMEMOTO 		if (nres > 1) {
2144f101318SHajimu UMEMOTO 			getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
2154f101318SHajimu UMEMOTO 			    sizeof(paddr), NULL, 0, NI_NUMERICHOST);
21642b4f28eSYoshinobu Inoue 			fprintf(stderr, "Trying %s...\n", paddr);
21758f0484fSRodney W. Grimes 		}
21858f0484fSRodney W. Grimes 	}
21958f0484fSRodney W. Grimes 	lport--;
22058f0484fSRodney W. Grimes 	if (fd2p == 0) {
2219233c4d9SJason Evans 		_write(s, "", 1);
22258f0484fSRodney W. Grimes 		lport = 0;
22358f0484fSRodney W. Grimes 	} else {
22442b4f28eSYoshinobu Inoue 		int s2 = rresvport_af(&lport, ai->ai_family), s3;
22510248e3aSStefan Farfeleder 		socklen_t len = ai->ai_addrlen;
226d1f32ba5SGeoff Rehmet 		int nfds;
22758f0484fSRodney W. Grimes 
22858f0484fSRodney W. Grimes 		if (s2 < 0)
22958f0484fSRodney W. Grimes 			goto bad;
230d201fe46SDaniel Eischen 		_listen(s2, 1);
23158f0484fSRodney W. Grimes 		(void)snprintf(num, sizeof(num), "%d", lport);
2329233c4d9SJason Evans 		if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
23358f0484fSRodney W. Grimes 			(void)fprintf(stderr,
23458f0484fSRodney W. Grimes 			    "rcmd: write (setting up stderr): %s\n",
23558f0484fSRodney W. Grimes 			    strerror(errno));
2369233c4d9SJason Evans 			(void)_close(s2);
23758f0484fSRodney W. Grimes 			goto bad;
23858f0484fSRodney W. Grimes 		}
239d1f32ba5SGeoff Rehmet 		nfds = max(s, s2)+1;
240d1f32ba5SGeoff Rehmet 		if(nfds > FD_SETSIZE) {
241d1f32ba5SGeoff Rehmet 			fprintf(stderr, "rcmd: too many files\n");
2429233c4d9SJason Evans 			(void)_close(s2);
243d1f32ba5SGeoff Rehmet 			goto bad;
244d1f32ba5SGeoff Rehmet 		}
245ce70b6caSPeter Wemm again:
24658f0484fSRodney W. Grimes 		FD_ZERO(&reads);
24758f0484fSRodney W. Grimes 		FD_SET(s, &reads);
24858f0484fSRodney W. Grimes 		FD_SET(s2, &reads);
24958f0484fSRodney W. Grimes 		errno = 0;
250d201fe46SDaniel Eischen 		if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
25158f0484fSRodney W. Grimes 			if (errno != 0)
25258f0484fSRodney W. Grimes 				(void)fprintf(stderr,
25358f0484fSRodney W. Grimes 				    "rcmd: select (setting up stderr): %s\n",
25458f0484fSRodney W. Grimes 				    strerror(errno));
25558f0484fSRodney W. Grimes 			else
25658f0484fSRodney W. Grimes 				(void)fprintf(stderr,
25758f0484fSRodney W. Grimes 				"select: protocol failure in circuit setup\n");
2589233c4d9SJason Evans 			(void)_close(s2);
25958f0484fSRodney W. Grimes 			goto bad;
26058f0484fSRodney W. Grimes 		}
261d201fe46SDaniel Eischen 		s3 = _accept(s2, (struct sockaddr *)&from, &len);
26242b4f28eSYoshinobu Inoue 		switch (from.ss_family) {
26342b4f28eSYoshinobu Inoue 		case AF_INET:
26442b4f28eSYoshinobu Inoue 			aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
26542b4f28eSYoshinobu Inoue 			break;
26642b4f28eSYoshinobu Inoue #ifdef INET6
26742b4f28eSYoshinobu Inoue 		case AF_INET6:
26842b4f28eSYoshinobu Inoue 			aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
26942b4f28eSYoshinobu Inoue 			break;
27042b4f28eSYoshinobu Inoue #endif
27142b4f28eSYoshinobu Inoue 		default:
27242b4f28eSYoshinobu Inoue 			aport = 0;	/* error */
27342b4f28eSYoshinobu Inoue 			break;
27442b4f28eSYoshinobu Inoue 		}
275ce70b6caSPeter Wemm 		/*
276ce70b6caSPeter Wemm 		 * XXX careful for ftp bounce attacks. If discovered, shut them
277ce70b6caSPeter Wemm 		 * down and check for the real auxiliary channel to connect.
278ce70b6caSPeter Wemm 		 */
27942b4f28eSYoshinobu Inoue 		if (aport == 20) {
2809233c4d9SJason Evans 			_close(s3);
281ce70b6caSPeter Wemm 			goto again;
282ce70b6caSPeter Wemm 		}
2839233c4d9SJason Evans 		(void)_close(s2);
28458f0484fSRodney W. Grimes 		if (s3 < 0) {
28558f0484fSRodney W. Grimes 			(void)fprintf(stderr,
28658f0484fSRodney W. Grimes 			    "rcmd: accept: %s\n", strerror(errno));
28758f0484fSRodney W. Grimes 			lport = 0;
28858f0484fSRodney W. Grimes 			goto bad;
28958f0484fSRodney W. Grimes 		}
29058f0484fSRodney W. Grimes 		*fd2p = s3;
29142b4f28eSYoshinobu Inoue 		if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
29258f0484fSRodney W. Grimes 			(void)fprintf(stderr,
29358f0484fSRodney W. Grimes 			    "socket: protocol failure in circuit setup.\n");
29458f0484fSRodney W. Grimes 			goto bad2;
29558f0484fSRodney W. Grimes 		}
29658f0484fSRodney W. Grimes 	}
2979233c4d9SJason Evans 	(void)_write(s, locuser, strlen(locuser)+1);
2989233c4d9SJason Evans 	(void)_write(s, remuser, strlen(remuser)+1);
2999233c4d9SJason Evans 	(void)_write(s, cmd, strlen(cmd)+1);
3009233c4d9SJason Evans 	if (_read(s, &c, 1) != 1) {
30158f0484fSRodney W. Grimes 		(void)fprintf(stderr,
30258f0484fSRodney W. Grimes 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
30358f0484fSRodney W. Grimes 		goto bad2;
30458f0484fSRodney W. Grimes 	}
30558f0484fSRodney W. Grimes 	if (c != 0) {
3069233c4d9SJason Evans 		while (_read(s, &c, 1) == 1) {
3079233c4d9SJason Evans 			(void)_write(STDERR_FILENO, &c, 1);
30858f0484fSRodney W. Grimes 			if (c == '\n')
30958f0484fSRodney W. Grimes 				break;
31058f0484fSRodney W. Grimes 		}
31158f0484fSRodney W. Grimes 		goto bad2;
31258f0484fSRodney W. Grimes 	}
313d201fe46SDaniel Eischen 	_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
31442b4f28eSYoshinobu Inoue 	freeaddrinfo(res);
31558f0484fSRodney W. Grimes 	return (s);
31658f0484fSRodney W. Grimes bad2:
31758f0484fSRodney W. Grimes 	if (lport)
3189233c4d9SJason Evans 		(void)_close(*fd2p);
31958f0484fSRodney W. Grimes bad:
3209233c4d9SJason Evans 	(void)_close(s);
321d201fe46SDaniel Eischen 	_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
32242b4f28eSYoshinobu Inoue 	freeaddrinfo(res);
32358f0484fSRodney W. Grimes 	return (-1);
32458f0484fSRodney W. Grimes }
32558f0484fSRodney W. Grimes 
32658f0484fSRodney W. Grimes int
32742b4f28eSYoshinobu Inoue rresvport(port)
32842b4f28eSYoshinobu Inoue 	int *port;
32958f0484fSRodney W. Grimes {
33042b4f28eSYoshinobu Inoue 	return rresvport_af(port, AF_INET);
33142b4f28eSYoshinobu Inoue }
33258f0484fSRodney W. Grimes 
33342b4f28eSYoshinobu Inoue int
33442b4f28eSYoshinobu Inoue rresvport_af(alport, family)
33542b4f28eSYoshinobu Inoue 	int *alport, family;
33642b4f28eSYoshinobu Inoue {
3376d7bd75aSJacques Vidrine 	int s;
33842b4f28eSYoshinobu Inoue 	struct sockaddr_storage ss;
33942b4f28eSYoshinobu Inoue 	u_short *sport;
34042b4f28eSYoshinobu Inoue 
34142b4f28eSYoshinobu Inoue 	memset(&ss, 0, sizeof(ss));
34242b4f28eSYoshinobu Inoue 	ss.ss_family = family;
34342b4f28eSYoshinobu Inoue 	switch (family) {
34442b4f28eSYoshinobu Inoue 	case AF_INET:
34542b4f28eSYoshinobu Inoue 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
34642b4f28eSYoshinobu Inoue 		sport = &((struct sockaddr_in *)&ss)->sin_port;
34742b4f28eSYoshinobu Inoue 		((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
34842b4f28eSYoshinobu Inoue 		break;
34942b4f28eSYoshinobu Inoue #ifdef INET6
35042b4f28eSYoshinobu Inoue 	case AF_INET6:
35142b4f28eSYoshinobu Inoue 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
35242b4f28eSYoshinobu Inoue 		sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
35342b4f28eSYoshinobu Inoue 		((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
35442b4f28eSYoshinobu Inoue 		break;
35542b4f28eSYoshinobu Inoue #endif
35642b4f28eSYoshinobu Inoue 	default:
35742b4f28eSYoshinobu Inoue 		errno = EAFNOSUPPORT;
35842b4f28eSYoshinobu Inoue 		return -1;
35942b4f28eSYoshinobu Inoue 	}
36042b4f28eSYoshinobu Inoue 
361d201fe46SDaniel Eischen 	s = _socket(ss.ss_family, SOCK_STREAM, 0);
36258f0484fSRodney W. Grimes 	if (s < 0)
36358f0484fSRodney W. Grimes 		return (-1);
364ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */
365ce70b6caSPeter Wemm 	sin.sin_port = htons((u_short)*alport);
366d201fe46SDaniel Eischen 	if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
36758f0484fSRodney W. Grimes 		return (s);
368ce70b6caSPeter Wemm 	if (errno != EADDRINUSE) {
3699233c4d9SJason Evans 		(void)_close(s);
37058f0484fSRodney W. Grimes 		return (-1);
37158f0484fSRodney W. Grimes 	}
372ce70b6caSPeter Wemm #endif
37342b4f28eSYoshinobu Inoue 	*sport = 0;
374ae42b666SYoshinobu Inoue 	if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
3759233c4d9SJason Evans 		(void)_close(s);
376ce70b6caSPeter Wemm 		return (-1);
377ce70b6caSPeter Wemm 	}
37842b4f28eSYoshinobu Inoue 	*alport = (int)ntohs(*sport);
379ce70b6caSPeter Wemm 	return (s);
380ce70b6caSPeter Wemm }
38158f0484fSRodney W. Grimes 
38258f0484fSRodney W. Grimes int	__check_rhosts_file = 1;
38358f0484fSRodney W. Grimes char	*__rcmd_errstr;
38458f0484fSRodney W. Grimes 
38558f0484fSRodney W. Grimes int
38658f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser)
38758f0484fSRodney W. Grimes 	const char *rhost, *ruser, *luser;
38858f0484fSRodney W. Grimes 	int superuser;
38958f0484fSRodney W. Grimes {
390e3be4d7bSYoshinobu Inoue 	struct addrinfo hints, *res, *r;
391e3be4d7bSYoshinobu Inoue 	int error;
39242b4f28eSYoshinobu Inoue 
393e3be4d7bSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
394e3be4d7bSYoshinobu Inoue 	hints.ai_family = PF_UNSPEC;
395e3be4d7bSYoshinobu Inoue 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
396e3be4d7bSYoshinobu Inoue 	error = getaddrinfo(rhost, "0", &hints, &res);
397e3be4d7bSYoshinobu Inoue 	if (error)
39858f0484fSRodney W. Grimes 		return (-1);
399e3be4d7bSYoshinobu Inoue 
400e3be4d7bSYoshinobu Inoue 	for (r = res; r; r = r->ai_next) {
401e3be4d7bSYoshinobu Inoue 		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
402e3be4d7bSYoshinobu Inoue 		    luser) == 0) {
403e3be4d7bSYoshinobu Inoue 			freeaddrinfo(res);
404e3be4d7bSYoshinobu Inoue 			return (0);
40542b4f28eSYoshinobu Inoue 		}
40642b4f28eSYoshinobu Inoue 	}
407e3be4d7bSYoshinobu Inoue 	freeaddrinfo(res);
408e3be4d7bSYoshinobu Inoue 	return (-1);
40958f0484fSRodney W. Grimes }
41058f0484fSRodney W. Grimes 
41158f0484fSRodney W. Grimes /*
41258f0484fSRodney W. Grimes  * New .rhosts strategy: We are passed an ip address. We spin through
41358f0484fSRodney W. Grimes  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
41458f0484fSRodney W. Grimes  * has ip addresses, we don't have to trust a nameserver.  When it
41558f0484fSRodney W. Grimes  * contains hostnames, we spin through the list of addresses the nameserver
41658f0484fSRodney W. Grimes  * gives us and look for a match.
41758f0484fSRodney W. Grimes  *
41858f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
41958f0484fSRodney W. Grimes  */
42058f0484fSRodney W. Grimes int
42158f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser)
422e0ce825eSDoug Rabson 	unsigned long raddr;
42358f0484fSRodney W. Grimes 	int superuser;
42458f0484fSRodney W. Grimes 	const char *ruser, *luser;
42558f0484fSRodney W. Grimes {
426eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
427eb74b996SHajimu UMEMOTO 
428eb74b996SHajimu UMEMOTO 	memset(&sin, 0, sizeof(sin));
429eb74b996SHajimu UMEMOTO 	sin.sin_family = AF_INET;
430eb74b996SHajimu UMEMOTO 	sin.sin_len = sizeof(struct sockaddr_in);
431eb74b996SHajimu UMEMOTO 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
432eb74b996SHajimu UMEMOTO 	return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
433eb74b996SHajimu UMEMOTO 		ruser, luser);
43442b4f28eSYoshinobu Inoue }
43542b4f28eSYoshinobu Inoue 
436eb74b996SHajimu UMEMOTO /*
437eb74b996SHajimu UMEMOTO  * AF independent extension of iruserok.
438eb74b996SHajimu UMEMOTO  *
439eb74b996SHajimu UMEMOTO  * Returns 0 if ok, -1 if not ok.
440eb74b996SHajimu UMEMOTO  */
441eb74b996SHajimu UMEMOTO int
442eb74b996SHajimu UMEMOTO iruserok_sa(ra, rlen, superuser, ruser, luser)
443eb74b996SHajimu UMEMOTO 	const void *ra;
444eb74b996SHajimu UMEMOTO 	int rlen;
44542b4f28eSYoshinobu Inoue 	int superuser;
44642b4f28eSYoshinobu Inoue 	const char *ruser, *luser;
44742b4f28eSYoshinobu Inoue {
4488fb3f3f6SDavid E. O'Brien 	char *cp;
44958f0484fSRodney W. Grimes 	struct stat sbuf;
45058f0484fSRodney W. Grimes 	struct passwd *pwd;
45158f0484fSRodney W. Grimes 	FILE *hostf;
45258f0484fSRodney W. Grimes 	uid_t uid;
45358f0484fSRodney W. Grimes 	int first;
45458f0484fSRodney W. Grimes 	char pbuf[MAXPATHLEN];
455eb74b996SHajimu UMEMOTO 	const struct sockaddr *raddr;
456eb74b996SHajimu UMEMOTO 	struct sockaddr_storage ss;
45742b4f28eSYoshinobu Inoue 
458eb74b996SHajimu UMEMOTO 	/* avoid alignment issue */
459eb74b996SHajimu UMEMOTO 	if (rlen > sizeof(ss))
460eb74b996SHajimu UMEMOTO 		return(-1);
461eb74b996SHajimu UMEMOTO 	memcpy(&ss, ra, rlen);
462eb74b996SHajimu UMEMOTO 	raddr = (struct sockaddr *)&ss;
46358f0484fSRodney W. Grimes 
46458f0484fSRodney W. Grimes 	first = 1;
46558f0484fSRodney W. Grimes 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
46658f0484fSRodney W. Grimes again:
46758f0484fSRodney W. Grimes 	if (hostf) {
468eb74b996SHajimu UMEMOTO 		if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
46958f0484fSRodney W. Grimes 			(void)fclose(hostf);
47058f0484fSRodney W. Grimes 			return (0);
47158f0484fSRodney W. Grimes 		}
47258f0484fSRodney W. Grimes 		(void)fclose(hostf);
47358f0484fSRodney W. Grimes 	}
47458f0484fSRodney W. Grimes 	if (first == 1 && (__check_rhosts_file || superuser)) {
47558f0484fSRodney W. Grimes 		first = 0;
47658f0484fSRodney W. Grimes 		if ((pwd = getpwnam(luser)) == NULL)
47758f0484fSRodney W. Grimes 			return (-1);
47858f0484fSRodney W. Grimes 		(void)strcpy(pbuf, pwd->pw_dir);
47958f0484fSRodney W. Grimes 		(void)strcat(pbuf, "/.rhosts");
48058f0484fSRodney W. Grimes 
48158f0484fSRodney W. Grimes 		/*
48258f0484fSRodney W. Grimes 		 * Change effective uid while opening .rhosts.  If root and
48358f0484fSRodney W. Grimes 		 * reading an NFS mounted file system, can't read files that
48458f0484fSRodney W. Grimes 		 * are protected read/write owner only.
48558f0484fSRodney W. Grimes 		 */
48658f0484fSRodney W. Grimes 		uid = geteuid();
48758f0484fSRodney W. Grimes 		(void)seteuid(pwd->pw_uid);
48858f0484fSRodney W. Grimes 		hostf = fopen(pbuf, "r");
48958f0484fSRodney W. Grimes 		(void)seteuid(uid);
49058f0484fSRodney W. Grimes 
49158f0484fSRodney W. Grimes 		if (hostf == NULL)
49258f0484fSRodney W. Grimes 			return (-1);
49358f0484fSRodney W. Grimes 		/*
49458f0484fSRodney W. Grimes 		 * If not a regular file, or is owned by someone other than
49558f0484fSRodney W. Grimes 		 * user or root or if writeable by anyone but the owner, quit.
49658f0484fSRodney W. Grimes 		 */
49758f0484fSRodney W. Grimes 		cp = NULL;
49858f0484fSRodney W. Grimes 		if (lstat(pbuf, &sbuf) < 0)
49958f0484fSRodney W. Grimes 			cp = ".rhosts lstat failed";
50058f0484fSRodney W. Grimes 		else if (!S_ISREG(sbuf.st_mode))
50158f0484fSRodney W. Grimes 			cp = ".rhosts not regular file";
502d201fe46SDaniel Eischen 		else if (_fstat(fileno(hostf), &sbuf) < 0)
50358f0484fSRodney W. Grimes 			cp = ".rhosts fstat failed";
50458f0484fSRodney W. Grimes 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
50558f0484fSRodney W. Grimes 			cp = "bad .rhosts owner";
50658f0484fSRodney W. Grimes 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
50758f0484fSRodney W. Grimes 			cp = ".rhosts writeable by other than owner";
50858f0484fSRodney W. Grimes 		/* If there were any problems, quit. */
50958f0484fSRodney W. Grimes 		if (cp) {
51058f0484fSRodney W. Grimes 			__rcmd_errstr = cp;
51158f0484fSRodney W. Grimes 			(void)fclose(hostf);
51258f0484fSRodney W. Grimes 			return (-1);
51358f0484fSRodney W. Grimes 		}
51458f0484fSRodney W. Grimes 		goto again;
51558f0484fSRodney W. Grimes 	}
51658f0484fSRodney W. Grimes 	return (-1);
51758f0484fSRodney W. Grimes }
51858f0484fSRodney W. Grimes 
51958f0484fSRodney W. Grimes /*
52058f0484fSRodney W. Grimes  * XXX
52158f0484fSRodney W. Grimes  * Don't make static, used by lpd(8).
52258f0484fSRodney W. Grimes  *
52358f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
52458f0484fSRodney W. Grimes  */
52558f0484fSRodney W. Grimes int
52658f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser)
52758f0484fSRodney W. Grimes 	FILE *hostf;
528e0ce825eSDoug Rabson 	u_int32_t raddr;
52958f0484fSRodney W. Grimes 	const char *luser, *ruser;
53058f0484fSRodney W. Grimes {
531eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
532eb74b996SHajimu UMEMOTO 
533eb74b996SHajimu UMEMOTO 	memset(&sin, 0, sizeof(sin));
534eb74b996SHajimu UMEMOTO 	sin.sin_family = AF_INET;
535eb74b996SHajimu UMEMOTO 	sin.sin_len = sizeof(struct sockaddr_in);
536eb74b996SHajimu UMEMOTO 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
537eb74b996SHajimu UMEMOTO 	return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
538eb74b996SHajimu UMEMOTO 		luser, ruser);
53942b4f28eSYoshinobu Inoue }
54042b4f28eSYoshinobu Inoue 
541eb74b996SHajimu UMEMOTO /*
542eb74b996SHajimu UMEMOTO  * Returns 0 if ok, -1 if not ok.
543eb74b996SHajimu UMEMOTO  *
544eb74b996SHajimu UMEMOTO  * XXX obsolete API.
545eb74b996SHajimu UMEMOTO  */
54642b4f28eSYoshinobu Inoue int
54742b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
54842b4f28eSYoshinobu Inoue 	FILE *hostf;
549eb74b996SHajimu UMEMOTO 	const void *raddr;
55042b4f28eSYoshinobu Inoue 	const char *luser, *ruser;
55142b4f28eSYoshinobu Inoue 	int af, len;
55242b4f28eSYoshinobu Inoue {
553eb74b996SHajimu UMEMOTO 	struct sockaddr *sa = NULL;
554eb74b996SHajimu UMEMOTO 	struct sockaddr_in *sin = NULL;
555eb74b996SHajimu UMEMOTO #ifdef INET6
556eb74b996SHajimu UMEMOTO 	struct sockaddr_in6 *sin6 = NULL;
557eb74b996SHajimu UMEMOTO #endif
558eb74b996SHajimu UMEMOTO 	struct sockaddr_storage ss;
559eb74b996SHajimu UMEMOTO 
560eb74b996SHajimu UMEMOTO 	memset(&ss, 0, sizeof(ss));
561eb74b996SHajimu UMEMOTO 	switch (af) {
562eb74b996SHajimu UMEMOTO 	case AF_INET:
563eb74b996SHajimu UMEMOTO 		if (len != sizeof(sin->sin_addr))
564eb74b996SHajimu UMEMOTO 			return -1;
565eb74b996SHajimu UMEMOTO 		sin = (struct sockaddr_in *)&ss;
566eb74b996SHajimu UMEMOTO 		sin->sin_family = AF_INET;
567eb74b996SHajimu UMEMOTO 		sin->sin_len = sizeof(struct sockaddr_in);
568eb74b996SHajimu UMEMOTO 		memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
569eb74b996SHajimu UMEMOTO 		break;
570eb74b996SHajimu UMEMOTO #ifdef INET6
571eb74b996SHajimu UMEMOTO 	case AF_INET6:
572eb74b996SHajimu UMEMOTO 		if (len != sizeof(sin6->sin6_addr))
573eb74b996SHajimu UMEMOTO 			return -1;
574eb74b996SHajimu UMEMOTO 		/* you will lose scope info */
575eb74b996SHajimu UMEMOTO 		sin6 = (struct sockaddr_in6 *)&ss;
576eb74b996SHajimu UMEMOTO 		sin6->sin6_family = AF_INET6;
577eb74b996SHajimu UMEMOTO 		sin6->sin6_len = sizeof(struct sockaddr_in6);
578eb74b996SHajimu UMEMOTO 		memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
579eb74b996SHajimu UMEMOTO 		break;
580eb74b996SHajimu UMEMOTO #endif
581eb74b996SHajimu UMEMOTO 	default:
582eb74b996SHajimu UMEMOTO 		return -1;
583eb74b996SHajimu UMEMOTO 	}
584eb74b996SHajimu UMEMOTO 
585eb74b996SHajimu UMEMOTO 	sa = (struct sockaddr *)&ss;
586eb74b996SHajimu UMEMOTO 	return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
587eb74b996SHajimu UMEMOTO }
588eb74b996SHajimu UMEMOTO 
589eb74b996SHajimu UMEMOTO int
590eb74b996SHajimu UMEMOTO __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
591eb74b996SHajimu UMEMOTO 	FILE *hostf;
592eb74b996SHajimu UMEMOTO 	const struct sockaddr *raddr;
593eb74b996SHajimu UMEMOTO 	socklen_t salen;
594eb74b996SHajimu UMEMOTO 	const char *luser, *ruser;
595eb74b996SHajimu UMEMOTO {
5968fb3f3f6SDavid E. O'Brien 	char *user, *p;
59758f0484fSRodney W. Grimes 	int ch;
59858f0484fSRodney W. Grimes 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
59997cb5094SBill Paul 	char hname[MAXHOSTNAMELEN];
6008538335fSBill Paul 	/* Presumed guilty until proven innocent. */
6018538335fSBill Paul 	int userok = 0, hostok = 0;
6021e890b05SBill Paul #ifdef YP
6031e890b05SBill Paul 	char *ypdomain;
6048538335fSBill Paul 
6051e890b05SBill Paul 	if (yp_get_default_domain(&ypdomain))
6061e890b05SBill Paul 		ypdomain = NULL;
6071e890b05SBill Paul #else
6081e890b05SBill Paul #define	ypdomain NULL
6091e890b05SBill Paul #endif
6108538335fSBill Paul 	/* We need to get the damn hostname back for netgroup matching. */
611eb74b996SHajimu UMEMOTO 	if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
612eb74b996SHajimu UMEMOTO 			NI_NAMEREQD) != 0)
613c97c8f4aSJohn Polstra 		hname[0] = '\0';
61458f0484fSRodney W. Grimes 
61558f0484fSRodney W. Grimes 	while (fgets(buf, sizeof(buf), hostf)) {
61658f0484fSRodney W. Grimes 		p = buf;
61758f0484fSRodney W. Grimes 		/* Skip lines that are too long. */
618a9f9141cSBrian S. Dean 		if (strchr(p, '\n') == NULL) {
61958f0484fSRodney W. Grimes 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
62058f0484fSRodney W. Grimes 			continue;
62158f0484fSRodney W. Grimes 		}
622acc7e87cSPeter Wemm 		if (*p == '\n' || *p == '#') {
623acc7e87cSPeter Wemm 			/* comment... */
624acc7e87cSPeter Wemm 			continue;
625acc7e87cSPeter Wemm 		}
62658f0484fSRodney W. Grimes 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
6273fb85bffSAndrey A. Chernov 			*p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
62858f0484fSRodney W. Grimes 			p++;
62958f0484fSRodney W. Grimes 		}
63058f0484fSRodney W. Grimes 		if (*p == ' ' || *p == '\t') {
63158f0484fSRodney W. Grimes 			*p++ = '\0';
63258f0484fSRodney W. Grimes 			while (*p == ' ' || *p == '\t')
63358f0484fSRodney W. Grimes 				p++;
63458f0484fSRodney W. Grimes 			user = p;
63558f0484fSRodney W. Grimes 			while (*p != '\n' && *p != ' ' &&
63658f0484fSRodney W. Grimes 			    *p != '\t' && *p != '\0')
63758f0484fSRodney W. Grimes 				p++;
63858f0484fSRodney W. Grimes 		} else
63958f0484fSRodney W. Grimes 			user = p;
64058f0484fSRodney W. Grimes 		*p = '\0';
6418538335fSBill Paul 		/*
6428538335fSBill Paul 		 * Do +/- and +@/-@ checking. This looks really nasty,
6438538335fSBill Paul 		 * but it matches SunOS's behavior so far as I can tell.
6448538335fSBill Paul 		 */
6458538335fSBill Paul 		switch(buf[0]) {
6468538335fSBill Paul 		case '+':
6478538335fSBill Paul 			if (!buf[1]) {     /* '+' matches all hosts */
6488538335fSBill Paul 				hostok = 1;
6498538335fSBill Paul 				break;
65058f0484fSRodney W. Grimes 			}
6518538335fSBill Paul 			if (buf[1] == '@')  /* match a host by netgroup */
652c97c8f4aSJohn Polstra 				hostok = hname[0] != '\0' &&
653c97c8f4aSJohn Polstra 				    innetgr(&buf[2], hname, NULL, ypdomain);
6548538335fSBill Paul 			else		/* match a host by addr */
655eb74b996SHajimu UMEMOTO 				hostok = __icheckhost(raddr, salen,
656eb74b996SHajimu UMEMOTO 						      (char *)&buf[1]);
6578538335fSBill Paul 			break;
6588538335fSBill Paul 		case '-':     /* reject '-' hosts and all their users */
6598538335fSBill Paul 			if (buf[1] == '@') {
660c97c8f4aSJohn Polstra 				if (hname[0] == '\0' ||
661c97c8f4aSJohn Polstra 				    innetgr(&buf[2], hname, NULL, ypdomain))
6628538335fSBill Paul 					return(-1);
6638538335fSBill Paul 			} else {
664eb74b996SHajimu UMEMOTO 				if (__icheckhost(raddr, salen,
665eb74b996SHajimu UMEMOTO 						 (char *)&buf[1]))
6668538335fSBill Paul 					return(-1);
6678538335fSBill Paul 			}
6688538335fSBill Paul 			break;
6698538335fSBill Paul 		default:  /* if no '+' or '-', do a simple match */
670eb74b996SHajimu UMEMOTO 			hostok = __icheckhost(raddr, salen, buf);
6718538335fSBill Paul 			break;
6728538335fSBill Paul 		}
6738538335fSBill Paul 		switch(*user) {
6748538335fSBill Paul 		case '+':
6758538335fSBill Paul 			if (!*(user+1)) {      /* '+' matches all users */
6768538335fSBill Paul 				userok = 1;
6778538335fSBill Paul 				break;
6788538335fSBill Paul 			}
6798538335fSBill Paul 			if (*(user+1) == '@')  /* match a user by netgroup */
6801e890b05SBill Paul 				userok = innetgr(user+2, NULL, ruser, ypdomain);
6818538335fSBill Paul 			else	   /* match a user by direct specification */
6828538335fSBill Paul 				userok = !(strcmp(ruser, user+1));
6838538335fSBill Paul 			break;
6848538335fSBill Paul 		case '-': 		/* if we matched a hostname, */
6858538335fSBill Paul 			if (hostok) {   /* check for user field rejections */
6868538335fSBill Paul 				if (!*(user+1))
6878538335fSBill Paul 					return(-1);
6888538335fSBill Paul 				if (*(user+1) == '@') {
6898538335fSBill Paul 					if (innetgr(user+2, NULL,
6901e890b05SBill Paul 							ruser, ypdomain))
6918538335fSBill Paul 						return(-1);
6928538335fSBill Paul 				} else {
6938538335fSBill Paul 					if (!strcmp(ruser, user+1))
6948538335fSBill Paul 						return(-1);
6958538335fSBill Paul 				}
6968538335fSBill Paul 			}
6978538335fSBill Paul 			break;
6988538335fSBill Paul 		default:	/* no rejections: try to match the user */
6998538335fSBill Paul 			if (hostok)
7008538335fSBill Paul 				userok = !(strcmp(ruser,*user ? user : luser));
7018538335fSBill Paul 			break;
7028538335fSBill Paul 		}
7038538335fSBill Paul 		if (hostok && userok)
7048538335fSBill Paul 			return(0);
70558f0484fSRodney W. Grimes 	}
70658f0484fSRodney W. Grimes 	return (-1);
70758f0484fSRodney W. Grimes }
70858f0484fSRodney W. Grimes 
70958f0484fSRodney W. Grimes /*
71058f0484fSRodney W. Grimes  * Returns "true" if match, 0 if no match.
71158f0484fSRodney W. Grimes  */
71258f0484fSRodney W. Grimes static int
713eb74b996SHajimu UMEMOTO __icheckhost(raddr, salen, lhost)
714eb74b996SHajimu UMEMOTO 	const struct sockaddr *raddr;
715eb74b996SHajimu UMEMOTO 	socklen_t salen;
716eb74b996SHajimu UMEMOTO         const char *lhost;
71758f0484fSRodney W. Grimes {
718eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
719eb74b996SHajimu UMEMOTO 	struct sockaddr_in6 *sin6;
720eb74b996SHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
721eb74b996SHajimu UMEMOTO 	int error;
722eb74b996SHajimu UMEMOTO 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
72358f0484fSRodney W. Grimes 
724eb74b996SHajimu UMEMOTO 	if (raddr->sa_family == AF_INET6) {
725eb74b996SHajimu UMEMOTO 		sin6 = (struct sockaddr_in6 *)raddr;
726eb74b996SHajimu UMEMOTO 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
727eb74b996SHajimu UMEMOTO 			memset(&sin, 0, sizeof(sin));
728eb74b996SHajimu UMEMOTO 			sin.sin_family = AF_INET;
729eb74b996SHajimu UMEMOTO 			sin.sin_len = sizeof(struct sockaddr_in);
730eb74b996SHajimu UMEMOTO 			memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
731eb74b996SHajimu UMEMOTO 			       sizeof(sin.sin_addr));
732eb74b996SHajimu UMEMOTO 			raddr = (struct sockaddr *)&sin;
733eb74b996SHajimu UMEMOTO 			salen = sin.sin_len;
734eb74b996SHajimu UMEMOTO 		}
735eb74b996SHajimu UMEMOTO 	}
736eb74b996SHajimu UMEMOTO 
737eb74b996SHajimu UMEMOTO 	h1[0] = '\0';
738eb74b996SHajimu UMEMOTO 	if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
7394f101318SHajimu UMEMOTO 			NI_NUMERICHOST) != 0)
740eb74b996SHajimu UMEMOTO 		return (0);
741eb74b996SHajimu UMEMOTO 
742eb74b996SHajimu UMEMOTO 	/* Resolve laddr into sockaddr */
743eb74b996SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
744eb74b996SHajimu UMEMOTO 	hints.ai_family = raddr->sa_family;
745eb74b996SHajimu UMEMOTO 	hints.ai_socktype = SOCK_DGRAM;	/*XXX dummy*/
746eb74b996SHajimu UMEMOTO 	res = NULL;
747eb74b996SHajimu UMEMOTO 	error = getaddrinfo(lhost, "0", &hints, &res);
748eb74b996SHajimu UMEMOTO 	if (error)
749eb74b996SHajimu UMEMOTO 		return (0);
750eb74b996SHajimu UMEMOTO 
751eb74b996SHajimu UMEMOTO 	for (r = res; r ; r = r->ai_next) {
752eb74b996SHajimu UMEMOTO 		h2[0] = '\0';
753eb74b996SHajimu UMEMOTO 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
7544f101318SHajimu UMEMOTO 				NULL, 0, NI_NUMERICHOST) != 0)
755eb74b996SHajimu UMEMOTO 			continue;
756eb74b996SHajimu UMEMOTO 		if (strcmp(h1, h2) == 0) {
757eb74b996SHajimu UMEMOTO 			freeaddrinfo(res);
75842b4f28eSYoshinobu Inoue 			return (1);
759eb74b996SHajimu UMEMOTO 		}
76042b4f28eSYoshinobu Inoue 	}
76158f0484fSRodney W. Grimes 
762eb74b996SHajimu UMEMOTO 	/* No match. */
763eb74b996SHajimu UMEMOTO 	freeaddrinfo(res);
76458f0484fSRodney W. Grimes 	return (0);
76558f0484fSRodney W. Grimes }
766