xref: /freebsd/lib/libc/net/rcmd.c (revision d201fe46e355212750b727061e6a7ac005267852)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1983, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
323fb85bffSAndrey A. Chernov  *
333fb85bffSAndrey A. Chernov  * $FreeBSD$
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3758f0484fSRodney W. Grimes static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3958f0484fSRodney W. Grimes 
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>
5158f0484fSRodney W. Grimes #include <unistd.h>
5258f0484fSRodney W. Grimes #include <pwd.h>
5358f0484fSRodney W. Grimes #include <errno.h>
5458f0484fSRodney W. Grimes #include <stdio.h>
5558f0484fSRodney W. Grimes #include <ctype.h>
5658f0484fSRodney W. Grimes #include <string.h>
571e890b05SBill Paul #ifdef YP
581e890b05SBill Paul #include <rpc/rpc.h>
591e890b05SBill Paul #include <rpcsvc/yp_prot.h>
601e890b05SBill Paul #include <rpcsvc/ypclnt.h>
611e890b05SBill Paul #endif
62c98e299eSHajimu UMEMOTO #include <arpa/nameser.h>
63d201fe46SDaniel Eischen #include "un-namespace.h"
6458f0484fSRodney W. Grimes 
6542b4f28eSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */
6642b4f28eSYoshinobu Inoue #ifndef NI_WITHSCOPEID
6742b4f28eSYoshinobu Inoue #define NI_WITHSCOPEID	0
6842b4f28eSYoshinobu Inoue #endif
6942b4f28eSYoshinobu Inoue 
7051295a4dSJordan K. Hubbard extern int innetgr __P(( const char *, const char *, const char *, const char * ));
7151295a4dSJordan K. Hubbard 
72d1f32ba5SGeoff Rehmet #define max(a, b)	((a > b) ? a : b)
73d1f32ba5SGeoff Rehmet 
74e0ce825eSDoug Rabson int	__ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
75eb74b996SHajimu UMEMOTO int __ivaliduser_af __P((FILE *,const void *, const char *, const char *,
76eb74b996SHajimu UMEMOTO 	int, int));
77eb74b996SHajimu UMEMOTO int __ivaliduser_sa __P((FILE *, const struct sockaddr *, socklen_t,
78eb74b996SHajimu UMEMOTO 	const char *,const char *));
79eb74b996SHajimu UMEMOTO static int __icheckhost __P((const struct sockaddr *, socklen_t,
80eb74b996SHajimu UMEMOTO 	const char *));
8142b4f28eSYoshinobu Inoue 
82eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST];
8358f0484fSRodney W. Grimes 
8458f0484fSRodney W. Grimes int
8558f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
8658f0484fSRodney W. Grimes 	char **ahost;
8758f0484fSRodney W. Grimes 	u_short rport;
8858f0484fSRodney W. Grimes 	const char *locuser, *remuser, *cmd;
8958f0484fSRodney W. Grimes 	int *fd2p;
9058f0484fSRodney W. Grimes {
910cac72f4SYoshinobu Inoue 	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
920cac72f4SYoshinobu Inoue }
930cac72f4SYoshinobu Inoue 
940cac72f4SYoshinobu Inoue int
950cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
960cac72f4SYoshinobu Inoue 	char **ahost;
970cac72f4SYoshinobu Inoue 	u_short rport;
980cac72f4SYoshinobu Inoue 	const char *locuser, *remuser, *cmd;
990cac72f4SYoshinobu Inoue 	int *fd2p;
1000cac72f4SYoshinobu Inoue 	int af;
1010cac72f4SYoshinobu Inoue {
10242b4f28eSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
10342b4f28eSYoshinobu Inoue 	struct sockaddr_storage from;
10458f0484fSRodney W. Grimes 	fd_set reads;
105d201fe46SDaniel Eischen 	sigset_t oldmask, newmask;
10658f0484fSRodney W. Grimes 	pid_t pid;
10742b4f28eSYoshinobu Inoue 	int s, aport, lport, timo, error;
10858f0484fSRodney W. Grimes 	char c;
109eb74b996SHajimu UMEMOTO 	int refused, nres;
110c98e299eSHajimu UMEMOTO 	char num[8];
111c98e299eSHajimu UMEMOTO 	static char canonnamebuf[MAXDNAME];	/* is it proper here? */
11258f0484fSRodney W. Grimes 
11358f0484fSRodney W. Grimes 	pid = getpid();
11442b4f28eSYoshinobu Inoue 
11542b4f28eSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
11642b4f28eSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
1170cac72f4SYoshinobu Inoue 	hints.ai_family = af;
11842b4f28eSYoshinobu Inoue 	hints.ai_socktype = SOCK_STREAM;
11942b4f28eSYoshinobu Inoue 	hints.ai_protocol = 0;
12042b4f28eSYoshinobu Inoue 	(void)snprintf(num, sizeof(num), "%d", ntohs(rport));
12142b4f28eSYoshinobu Inoue 	error = getaddrinfo(*ahost, num, &hints, &res);
12242b4f28eSYoshinobu Inoue 	if (error) {
12342b4f28eSYoshinobu Inoue 		fprintf(stderr, "rcmd: getaddrinfo: %s\n",
12442b4f28eSYoshinobu Inoue 			gai_strerror(error));
12542b4f28eSYoshinobu Inoue 		if (error == EAI_SYSTEM)
12642b4f28eSYoshinobu Inoue 			fprintf(stderr, "rcmd: getaddrinfo: %s\n",
12742b4f28eSYoshinobu Inoue 				strerror(errno));
12858f0484fSRodney W. Grimes 		return (-1);
12958f0484fSRodney W. Grimes 	}
130c98e299eSHajimu UMEMOTO 
131c98e299eSHajimu UMEMOTO 	if (res->ai_canonname
132c98e299eSHajimu UMEMOTO 	 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
133c98e299eSHajimu UMEMOTO 		strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
134c98e299eSHajimu UMEMOTO 		*ahost = canonnamebuf;
135c98e299eSHajimu UMEMOTO 	}
136eb74b996SHajimu UMEMOTO 	nres = 0;
137eb74b996SHajimu UMEMOTO 	for (ai = res; ai; ai = ai->ai_next)
138eb74b996SHajimu UMEMOTO 		nres++;
13942b4f28eSYoshinobu Inoue 	ai = res;
140eb74b996SHajimu UMEMOTO 	refused = 0;
141d201fe46SDaniel Eischen 	sigemptyset(&newmask);
142d201fe46SDaniel Eischen 	sigaddset(&newmask, SIGURG);
143d201fe46SDaniel Eischen 	_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
14458f0484fSRodney W. Grimes 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
14542b4f28eSYoshinobu Inoue 		s = rresvport_af(&lport, ai->ai_family);
14658f0484fSRodney W. Grimes 		if (s < 0) {
147474ce1d1SYoshinobu Inoue 			if (errno != EAGAIN && ai->ai_next) {
148474ce1d1SYoshinobu Inoue 				ai = ai->ai_next;
149474ce1d1SYoshinobu Inoue 				continue;
150474ce1d1SYoshinobu Inoue 			}
15158f0484fSRodney W. Grimes 			if (errno == EAGAIN)
15258f0484fSRodney W. Grimes 				(void)fprintf(stderr,
15358f0484fSRodney W. Grimes 				    "rcmd: socket: All ports in use\n");
15458f0484fSRodney W. Grimes 			else
15558f0484fSRodney W. Grimes 				(void)fprintf(stderr, "rcmd: socket: %s\n",
15658f0484fSRodney W. Grimes 				    strerror(errno));
15742b4f28eSYoshinobu Inoue 			freeaddrinfo(res);
158d201fe46SDaniel Eischen 			_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
159d201fe46SDaniel Eischen 			    NULL);
16058f0484fSRodney W. Grimes 			return (-1);
16158f0484fSRodney W. Grimes 		}
1629233c4d9SJason Evans 		_fcntl(s, F_SETOWN, pid);
163d201fe46SDaniel Eischen 		if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
16458f0484fSRodney W. Grimes 			break;
1659233c4d9SJason Evans 		(void)_close(s);
16658f0484fSRodney W. Grimes 		if (errno == EADDRINUSE) {
16758f0484fSRodney W. Grimes 			lport--;
16858f0484fSRodney W. Grimes 			continue;
16958f0484fSRodney W. Grimes 		}
170eb74b996SHajimu UMEMOTO 		if (errno == ECONNREFUSED)
171eb74b996SHajimu UMEMOTO 			refused = 1;
172eb74b996SHajimu UMEMOTO 		if (ai->ai_next == NULL && (!refused || timo > 16)) {
173eb74b996SHajimu UMEMOTO 			(void)fprintf(stderr, "%s: %s\n",
174eb74b996SHajimu UMEMOTO 				      *ahost, strerror(errno));
175eb74b996SHajimu UMEMOTO 			freeaddrinfo(res);
176d201fe46SDaniel Eischen 			_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
177d201fe46SDaniel Eischen 			    NULL);
178eb74b996SHajimu UMEMOTO 			return (-1);
1792368b03bSHajimu UMEMOTO 		}
180eb74b996SHajimu UMEMOTO 		if (nres > 1) {
18158f0484fSRodney W. Grimes 			int oerrno = errno;
18258f0484fSRodney W. Grimes 
18342b4f28eSYoshinobu Inoue 			getnameinfo(ai->ai_addr, ai->ai_addrlen,
18442b4f28eSYoshinobu Inoue 				    paddr, sizeof(paddr),
18542b4f28eSYoshinobu Inoue 				    NULL, 0,
18642b4f28eSYoshinobu Inoue 				    NI_NUMERICHOST|NI_WITHSCOPEID);
18758f0484fSRodney W. Grimes 			(void)fprintf(stderr, "connect to address %s: ",
18842b4f28eSYoshinobu Inoue 				      paddr);
18958f0484fSRodney W. Grimes 			errno = oerrno;
19058f0484fSRodney W. Grimes 			perror(0);
191eb74b996SHajimu UMEMOTO 		}
192eb74b996SHajimu UMEMOTO 		if ((ai = ai->ai_next) == NULL) {
193eb74b996SHajimu UMEMOTO 			/* refused && timo <= 16 */
194eb74b996SHajimu UMEMOTO 			struct timespec time_to_sleep, time_remaining;
195eb74b996SHajimu UMEMOTO 
196eb74b996SHajimu UMEMOTO 			time_to_sleep.tv_sec = timo;
197eb74b996SHajimu UMEMOTO 			time_to_sleep.tv_nsec = 0;
198eb74b996SHajimu UMEMOTO 			(void)_nanosleep(&time_to_sleep, &time_remaining);
199eb74b996SHajimu UMEMOTO 			timo *= 2;
200eb74b996SHajimu UMEMOTO 			ai = res;
201eb74b996SHajimu UMEMOTO 			refused = 0;
202eb74b996SHajimu UMEMOTO 		}
203eb74b996SHajimu UMEMOTO 		if (nres > 1) {
20442b4f28eSYoshinobu Inoue 			getnameinfo(ai->ai_addr, ai->ai_addrlen,
20542b4f28eSYoshinobu Inoue 				    paddr, sizeof(paddr),
20642b4f28eSYoshinobu Inoue 				    NULL, 0,
20742b4f28eSYoshinobu Inoue 				    NI_NUMERICHOST|NI_WITHSCOPEID);
20842b4f28eSYoshinobu Inoue 			fprintf(stderr, "Trying %s...\n", paddr);
20958f0484fSRodney W. Grimes 		}
21058f0484fSRodney W. Grimes 	}
21158f0484fSRodney W. Grimes 	lport--;
21258f0484fSRodney W. Grimes 	if (fd2p == 0) {
2139233c4d9SJason Evans 		_write(s, "", 1);
21458f0484fSRodney W. Grimes 		lport = 0;
21558f0484fSRodney W. Grimes 	} else {
21658f0484fSRodney W. Grimes 		char num[8];
21742b4f28eSYoshinobu Inoue 		int s2 = rresvport_af(&lport, ai->ai_family), s3;
21842b4f28eSYoshinobu Inoue 		int len = ai->ai_addrlen;
219d1f32ba5SGeoff Rehmet 		int nfds;
22058f0484fSRodney W. Grimes 
22158f0484fSRodney W. Grimes 		if (s2 < 0)
22258f0484fSRodney W. Grimes 			goto bad;
223d201fe46SDaniel Eischen 		_listen(s2, 1);
22458f0484fSRodney W. Grimes 		(void)snprintf(num, sizeof(num), "%d", lport);
2259233c4d9SJason Evans 		if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
22658f0484fSRodney W. Grimes 			(void)fprintf(stderr,
22758f0484fSRodney W. Grimes 			    "rcmd: write (setting up stderr): %s\n",
22858f0484fSRodney W. Grimes 			    strerror(errno));
2299233c4d9SJason Evans 			(void)_close(s2);
23058f0484fSRodney W. Grimes 			goto bad;
23158f0484fSRodney W. Grimes 		}
232d1f32ba5SGeoff Rehmet 		nfds = max(s, s2)+1;
233d1f32ba5SGeoff Rehmet 		if(nfds > FD_SETSIZE) {
234d1f32ba5SGeoff Rehmet 			fprintf(stderr, "rcmd: too many files\n");
2359233c4d9SJason Evans 			(void)_close(s2);
236d1f32ba5SGeoff Rehmet 			goto bad;
237d1f32ba5SGeoff Rehmet 		}
238ce70b6caSPeter Wemm again:
23958f0484fSRodney W. Grimes 		FD_ZERO(&reads);
24058f0484fSRodney W. Grimes 		FD_SET(s, &reads);
24158f0484fSRodney W. Grimes 		FD_SET(s2, &reads);
24258f0484fSRodney W. Grimes 		errno = 0;
243d201fe46SDaniel Eischen 		if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
24458f0484fSRodney W. Grimes 			if (errno != 0)
24558f0484fSRodney W. Grimes 				(void)fprintf(stderr,
24658f0484fSRodney W. Grimes 				    "rcmd: select (setting up stderr): %s\n",
24758f0484fSRodney W. Grimes 				    strerror(errno));
24858f0484fSRodney W. Grimes 			else
24958f0484fSRodney W. Grimes 				(void)fprintf(stderr,
25058f0484fSRodney W. Grimes 				"select: protocol failure in circuit setup\n");
2519233c4d9SJason Evans 			(void)_close(s2);
25258f0484fSRodney W. Grimes 			goto bad;
25358f0484fSRodney W. Grimes 		}
254d201fe46SDaniel Eischen 		s3 = _accept(s2, (struct sockaddr *)&from, &len);
25542b4f28eSYoshinobu Inoue 		switch (from.ss_family) {
25642b4f28eSYoshinobu Inoue 		case AF_INET:
25742b4f28eSYoshinobu Inoue 			aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
25842b4f28eSYoshinobu Inoue 			break;
25942b4f28eSYoshinobu Inoue #ifdef INET6
26042b4f28eSYoshinobu Inoue 		case AF_INET6:
26142b4f28eSYoshinobu Inoue 			aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
26242b4f28eSYoshinobu Inoue 			break;
26342b4f28eSYoshinobu Inoue #endif
26442b4f28eSYoshinobu Inoue 		default:
26542b4f28eSYoshinobu Inoue 			aport = 0;	/* error */
26642b4f28eSYoshinobu Inoue 			break;
26742b4f28eSYoshinobu Inoue 		}
268ce70b6caSPeter Wemm 		/*
269ce70b6caSPeter Wemm 		 * XXX careful for ftp bounce attacks. If discovered, shut them
270ce70b6caSPeter Wemm 		 * down and check for the real auxiliary channel to connect.
271ce70b6caSPeter Wemm 		 */
27242b4f28eSYoshinobu Inoue 		if (aport == 20) {
2739233c4d9SJason Evans 			_close(s3);
274ce70b6caSPeter Wemm 			goto again;
275ce70b6caSPeter Wemm 		}
2769233c4d9SJason Evans 		(void)_close(s2);
27758f0484fSRodney W. Grimes 		if (s3 < 0) {
27858f0484fSRodney W. Grimes 			(void)fprintf(stderr,
27958f0484fSRodney W. Grimes 			    "rcmd: accept: %s\n", strerror(errno));
28058f0484fSRodney W. Grimes 			lport = 0;
28158f0484fSRodney W. Grimes 			goto bad;
28258f0484fSRodney W. Grimes 		}
28358f0484fSRodney W. Grimes 		*fd2p = s3;
28442b4f28eSYoshinobu Inoue 		if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
28558f0484fSRodney W. Grimes 			(void)fprintf(stderr,
28658f0484fSRodney W. Grimes 			    "socket: protocol failure in circuit setup.\n");
28758f0484fSRodney W. Grimes 			goto bad2;
28858f0484fSRodney W. Grimes 		}
28958f0484fSRodney W. Grimes 	}
2909233c4d9SJason Evans 	(void)_write(s, locuser, strlen(locuser)+1);
2919233c4d9SJason Evans 	(void)_write(s, remuser, strlen(remuser)+1);
2929233c4d9SJason Evans 	(void)_write(s, cmd, strlen(cmd)+1);
2939233c4d9SJason Evans 	if (_read(s, &c, 1) != 1) {
29458f0484fSRodney W. Grimes 		(void)fprintf(stderr,
29558f0484fSRodney W. Grimes 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
29658f0484fSRodney W. Grimes 		goto bad2;
29758f0484fSRodney W. Grimes 	}
29858f0484fSRodney W. Grimes 	if (c != 0) {
2999233c4d9SJason Evans 		while (_read(s, &c, 1) == 1) {
3009233c4d9SJason Evans 			(void)_write(STDERR_FILENO, &c, 1);
30158f0484fSRodney W. Grimes 			if (c == '\n')
30258f0484fSRodney W. Grimes 				break;
30358f0484fSRodney W. Grimes 		}
30458f0484fSRodney W. Grimes 		goto bad2;
30558f0484fSRodney W. Grimes 	}
306d201fe46SDaniel Eischen 	_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
30742b4f28eSYoshinobu Inoue 	freeaddrinfo(res);
30858f0484fSRodney W. Grimes 	return (s);
30958f0484fSRodney W. Grimes bad2:
31058f0484fSRodney W. Grimes 	if (lport)
3119233c4d9SJason Evans 		(void)_close(*fd2p);
31258f0484fSRodney W. Grimes bad:
3139233c4d9SJason Evans 	(void)_close(s);
314d201fe46SDaniel Eischen 	_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
31542b4f28eSYoshinobu Inoue 	freeaddrinfo(res);
31658f0484fSRodney W. Grimes 	return (-1);
31758f0484fSRodney W. Grimes }
31858f0484fSRodney W. Grimes 
31958f0484fSRodney W. Grimes int
32042b4f28eSYoshinobu Inoue rresvport(port)
32142b4f28eSYoshinobu Inoue 	int *port;
32258f0484fSRodney W. Grimes {
32342b4f28eSYoshinobu Inoue 	return rresvport_af(port, AF_INET);
32442b4f28eSYoshinobu Inoue }
32558f0484fSRodney W. Grimes 
32642b4f28eSYoshinobu Inoue int
32742b4f28eSYoshinobu Inoue rresvport_af(alport, family)
32842b4f28eSYoshinobu Inoue 	int *alport, family;
32942b4f28eSYoshinobu Inoue {
33042b4f28eSYoshinobu Inoue 	int i, s, len, err;
33142b4f28eSYoshinobu Inoue 	struct sockaddr_storage ss;
33242b4f28eSYoshinobu Inoue 	u_short *sport;
33342b4f28eSYoshinobu Inoue 
33442b4f28eSYoshinobu Inoue 	memset(&ss, 0, sizeof(ss));
33542b4f28eSYoshinobu Inoue 	ss.ss_family = family;
33642b4f28eSYoshinobu Inoue 	switch (family) {
33742b4f28eSYoshinobu Inoue 	case AF_INET:
33842b4f28eSYoshinobu Inoue 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
33942b4f28eSYoshinobu Inoue 		sport = &((struct sockaddr_in *)&ss)->sin_port;
34042b4f28eSYoshinobu Inoue 		((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
34142b4f28eSYoshinobu Inoue 		break;
34242b4f28eSYoshinobu Inoue #ifdef INET6
34342b4f28eSYoshinobu Inoue 	case AF_INET6:
34442b4f28eSYoshinobu Inoue 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
34542b4f28eSYoshinobu Inoue 		sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
34642b4f28eSYoshinobu Inoue 		((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
34742b4f28eSYoshinobu Inoue 		break;
34842b4f28eSYoshinobu Inoue #endif
34942b4f28eSYoshinobu Inoue 	default:
35042b4f28eSYoshinobu Inoue 		errno = EAFNOSUPPORT;
35142b4f28eSYoshinobu Inoue 		return -1;
35242b4f28eSYoshinobu Inoue 	}
35342b4f28eSYoshinobu Inoue 
354d201fe46SDaniel Eischen 	s = _socket(ss.ss_family, SOCK_STREAM, 0);
35558f0484fSRodney W. Grimes 	if (s < 0)
35658f0484fSRodney W. Grimes 		return (-1);
357ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */
358ce70b6caSPeter Wemm 	sin.sin_port = htons((u_short)*alport);
359d201fe46SDaniel Eischen 	if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
36058f0484fSRodney W. Grimes 		return (s);
361ce70b6caSPeter Wemm 	if (errno != EADDRINUSE) {
3629233c4d9SJason Evans 		(void)_close(s);
36358f0484fSRodney W. Grimes 		return (-1);
36458f0484fSRodney W. Grimes 	}
365ce70b6caSPeter Wemm #endif
36642b4f28eSYoshinobu Inoue 	*sport = 0;
367ae42b666SYoshinobu Inoue 	if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
3689233c4d9SJason Evans 		(void)_close(s);
369ce70b6caSPeter Wemm 		return (-1);
370ce70b6caSPeter Wemm 	}
37142b4f28eSYoshinobu Inoue 	*alport = (int)ntohs(*sport);
372ce70b6caSPeter Wemm 	return (s);
373ce70b6caSPeter Wemm }
37458f0484fSRodney W. Grimes 
37558f0484fSRodney W. Grimes int	__check_rhosts_file = 1;
37658f0484fSRodney W. Grimes char	*__rcmd_errstr;
37758f0484fSRodney W. Grimes 
37858f0484fSRodney W. Grimes int
37958f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser)
38058f0484fSRodney W. Grimes 	const char *rhost, *ruser, *luser;
38158f0484fSRodney W. Grimes 	int superuser;
38258f0484fSRodney W. Grimes {
383e3be4d7bSYoshinobu Inoue 	struct addrinfo hints, *res, *r;
384e3be4d7bSYoshinobu Inoue 	int error;
38542b4f28eSYoshinobu Inoue 
386e3be4d7bSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
387e3be4d7bSYoshinobu Inoue 	hints.ai_family = PF_UNSPEC;
388e3be4d7bSYoshinobu Inoue 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
389e3be4d7bSYoshinobu Inoue 	error = getaddrinfo(rhost, "0", &hints, &res);
390e3be4d7bSYoshinobu Inoue 	if (error)
39158f0484fSRodney W. Grimes 		return (-1);
392e3be4d7bSYoshinobu Inoue 
393e3be4d7bSYoshinobu Inoue 	for (r = res; r; r = r->ai_next) {
394e3be4d7bSYoshinobu Inoue 		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
395e3be4d7bSYoshinobu Inoue 		    luser) == 0) {
396e3be4d7bSYoshinobu Inoue 			freeaddrinfo(res);
397e3be4d7bSYoshinobu Inoue 			return (0);
39842b4f28eSYoshinobu Inoue 		}
39942b4f28eSYoshinobu Inoue 	}
400e3be4d7bSYoshinobu Inoue 	freeaddrinfo(res);
401e3be4d7bSYoshinobu Inoue 	return (-1);
40258f0484fSRodney W. Grimes }
40358f0484fSRodney W. Grimes 
40458f0484fSRodney W. Grimes /*
40558f0484fSRodney W. Grimes  * New .rhosts strategy: We are passed an ip address. We spin through
40658f0484fSRodney W. Grimes  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
40758f0484fSRodney W. Grimes  * has ip addresses, we don't have to trust a nameserver.  When it
40858f0484fSRodney W. Grimes  * contains hostnames, we spin through the list of addresses the nameserver
40958f0484fSRodney W. Grimes  * gives us and look for a match.
41058f0484fSRodney W. Grimes  *
41158f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
41258f0484fSRodney W. Grimes  */
41358f0484fSRodney W. Grimes int
41458f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser)
415e0ce825eSDoug Rabson 	unsigned long raddr;
41658f0484fSRodney W. Grimes 	int superuser;
41758f0484fSRodney W. Grimes 	const char *ruser, *luser;
41858f0484fSRodney W. Grimes {
419eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
420eb74b996SHajimu UMEMOTO 
421eb74b996SHajimu UMEMOTO 	memset(&sin, 0, sizeof(sin));
422eb74b996SHajimu UMEMOTO 	sin.sin_family = AF_INET;
423eb74b996SHajimu UMEMOTO 	sin.sin_len = sizeof(struct sockaddr_in);
424eb74b996SHajimu UMEMOTO 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
425eb74b996SHajimu UMEMOTO 	return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
426eb74b996SHajimu UMEMOTO 		ruser, luser);
42742b4f28eSYoshinobu Inoue }
42842b4f28eSYoshinobu Inoue 
429eb74b996SHajimu UMEMOTO /*
430eb74b996SHajimu UMEMOTO  * AF independent extension of iruserok.
431eb74b996SHajimu UMEMOTO  *
432eb74b996SHajimu UMEMOTO  * Returns 0 if ok, -1 if not ok.
433eb74b996SHajimu UMEMOTO  */
434eb74b996SHajimu UMEMOTO int
435eb74b996SHajimu UMEMOTO iruserok_sa(ra, rlen, superuser, ruser, luser)
436eb74b996SHajimu UMEMOTO 	const void *ra;
437eb74b996SHajimu UMEMOTO 	int rlen;
43842b4f28eSYoshinobu Inoue 	int superuser;
43942b4f28eSYoshinobu Inoue 	const char *ruser, *luser;
44042b4f28eSYoshinobu Inoue {
44158f0484fSRodney W. Grimes 	register char *cp;
44258f0484fSRodney W. Grimes 	struct stat sbuf;
44358f0484fSRodney W. Grimes 	struct passwd *pwd;
44458f0484fSRodney W. Grimes 	FILE *hostf;
44558f0484fSRodney W. Grimes 	uid_t uid;
44658f0484fSRodney W. Grimes 	int first;
44758f0484fSRodney W. Grimes 	char pbuf[MAXPATHLEN];
448eb74b996SHajimu UMEMOTO 	const struct sockaddr *raddr;
449eb74b996SHajimu UMEMOTO 	struct sockaddr_storage ss;
45042b4f28eSYoshinobu Inoue 
451eb74b996SHajimu UMEMOTO 	/* avoid alignment issue */
452eb74b996SHajimu UMEMOTO 	if (rlen > sizeof(ss))
453eb74b996SHajimu UMEMOTO 		return(-1);
454eb74b996SHajimu UMEMOTO 	memcpy(&ss, ra, rlen);
455eb74b996SHajimu UMEMOTO 	raddr = (struct sockaddr *)&ss;
45658f0484fSRodney W. Grimes 
45758f0484fSRodney W. Grimes 	first = 1;
45858f0484fSRodney W. Grimes 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
45958f0484fSRodney W. Grimes again:
46058f0484fSRodney W. Grimes 	if (hostf) {
461eb74b996SHajimu UMEMOTO 		if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
46258f0484fSRodney W. Grimes 			(void)fclose(hostf);
46358f0484fSRodney W. Grimes 			return (0);
46458f0484fSRodney W. Grimes 		}
46558f0484fSRodney W. Grimes 		(void)fclose(hostf);
46658f0484fSRodney W. Grimes 	}
46758f0484fSRodney W. Grimes 	if (first == 1 && (__check_rhosts_file || superuser)) {
46858f0484fSRodney W. Grimes 		first = 0;
46958f0484fSRodney W. Grimes 		if ((pwd = getpwnam(luser)) == NULL)
47058f0484fSRodney W. Grimes 			return (-1);
47158f0484fSRodney W. Grimes 		(void)strcpy(pbuf, pwd->pw_dir);
47258f0484fSRodney W. Grimes 		(void)strcat(pbuf, "/.rhosts");
47358f0484fSRodney W. Grimes 
47458f0484fSRodney W. Grimes 		/*
47558f0484fSRodney W. Grimes 		 * Change effective uid while opening .rhosts.  If root and
47658f0484fSRodney W. Grimes 		 * reading an NFS mounted file system, can't read files that
47758f0484fSRodney W. Grimes 		 * are protected read/write owner only.
47858f0484fSRodney W. Grimes 		 */
47958f0484fSRodney W. Grimes 		uid = geteuid();
48058f0484fSRodney W. Grimes 		(void)seteuid(pwd->pw_uid);
48158f0484fSRodney W. Grimes 		hostf = fopen(pbuf, "r");
48258f0484fSRodney W. Grimes 		(void)seteuid(uid);
48358f0484fSRodney W. Grimes 
48458f0484fSRodney W. Grimes 		if (hostf == NULL)
48558f0484fSRodney W. Grimes 			return (-1);
48658f0484fSRodney W. Grimes 		/*
48758f0484fSRodney W. Grimes 		 * If not a regular file, or is owned by someone other than
48858f0484fSRodney W. Grimes 		 * user or root or if writeable by anyone but the owner, quit.
48958f0484fSRodney W. Grimes 		 */
49058f0484fSRodney W. Grimes 		cp = NULL;
49158f0484fSRodney W. Grimes 		if (lstat(pbuf, &sbuf) < 0)
49258f0484fSRodney W. Grimes 			cp = ".rhosts lstat failed";
49358f0484fSRodney W. Grimes 		else if (!S_ISREG(sbuf.st_mode))
49458f0484fSRodney W. Grimes 			cp = ".rhosts not regular file";
495d201fe46SDaniel Eischen 		else if (_fstat(fileno(hostf), &sbuf) < 0)
49658f0484fSRodney W. Grimes 			cp = ".rhosts fstat failed";
49758f0484fSRodney W. Grimes 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
49858f0484fSRodney W. Grimes 			cp = "bad .rhosts owner";
49958f0484fSRodney W. Grimes 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
50058f0484fSRodney W. Grimes 			cp = ".rhosts writeable by other than owner";
50158f0484fSRodney W. Grimes 		/* If there were any problems, quit. */
50258f0484fSRodney W. Grimes 		if (cp) {
50358f0484fSRodney W. Grimes 			__rcmd_errstr = cp;
50458f0484fSRodney W. Grimes 			(void)fclose(hostf);
50558f0484fSRodney W. Grimes 			return (-1);
50658f0484fSRodney W. Grimes 		}
50758f0484fSRodney W. Grimes 		goto again;
50858f0484fSRodney W. Grimes 	}
50958f0484fSRodney W. Grimes 	return (-1);
51058f0484fSRodney W. Grimes }
51158f0484fSRodney W. Grimes 
51258f0484fSRodney W. Grimes /*
51358f0484fSRodney W. Grimes  * XXX
51458f0484fSRodney W. Grimes  * Don't make static, used by lpd(8).
51558f0484fSRodney W. Grimes  *
51658f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
51758f0484fSRodney W. Grimes  */
51858f0484fSRodney W. Grimes int
51958f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser)
52058f0484fSRodney W. Grimes 	FILE *hostf;
521e0ce825eSDoug Rabson 	u_int32_t raddr;
52258f0484fSRodney W. Grimes 	const char *luser, *ruser;
52358f0484fSRodney W. Grimes {
524eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
525eb74b996SHajimu UMEMOTO 
526eb74b996SHajimu UMEMOTO 	memset(&sin, 0, sizeof(sin));
527eb74b996SHajimu UMEMOTO 	sin.sin_family = AF_INET;
528eb74b996SHajimu UMEMOTO 	sin.sin_len = sizeof(struct sockaddr_in);
529eb74b996SHajimu UMEMOTO 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
530eb74b996SHajimu UMEMOTO 	return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
531eb74b996SHajimu UMEMOTO 		luser, ruser);
53242b4f28eSYoshinobu Inoue }
53342b4f28eSYoshinobu Inoue 
534eb74b996SHajimu UMEMOTO /*
535eb74b996SHajimu UMEMOTO  * Returns 0 if ok, -1 if not ok.
536eb74b996SHajimu UMEMOTO  *
537eb74b996SHajimu UMEMOTO  * XXX obsolete API.
538eb74b996SHajimu UMEMOTO  */
53942b4f28eSYoshinobu Inoue int
54042b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
54142b4f28eSYoshinobu Inoue 	FILE *hostf;
542eb74b996SHajimu UMEMOTO 	const void *raddr;
54342b4f28eSYoshinobu Inoue 	const char *luser, *ruser;
54442b4f28eSYoshinobu Inoue 	int af, len;
54542b4f28eSYoshinobu Inoue {
546eb74b996SHajimu UMEMOTO 	struct sockaddr *sa = NULL;
547eb74b996SHajimu UMEMOTO 	struct sockaddr_in *sin = NULL;
548eb74b996SHajimu UMEMOTO #ifdef INET6
549eb74b996SHajimu UMEMOTO 	struct sockaddr_in6 *sin6 = NULL;
550eb74b996SHajimu UMEMOTO #endif
551eb74b996SHajimu UMEMOTO 	struct sockaddr_storage ss;
552eb74b996SHajimu UMEMOTO 
553eb74b996SHajimu UMEMOTO 	memset(&ss, 0, sizeof(ss));
554eb74b996SHajimu UMEMOTO 	switch (af) {
555eb74b996SHajimu UMEMOTO 	case AF_INET:
556eb74b996SHajimu UMEMOTO 		if (len != sizeof(sin->sin_addr))
557eb74b996SHajimu UMEMOTO 			return -1;
558eb74b996SHajimu UMEMOTO 		sin = (struct sockaddr_in *)&ss;
559eb74b996SHajimu UMEMOTO 		sin->sin_family = AF_INET;
560eb74b996SHajimu UMEMOTO 		sin->sin_len = sizeof(struct sockaddr_in);
561eb74b996SHajimu UMEMOTO 		memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
562eb74b996SHajimu UMEMOTO 		break;
563eb74b996SHajimu UMEMOTO #ifdef INET6
564eb74b996SHajimu UMEMOTO 	case AF_INET6:
565eb74b996SHajimu UMEMOTO 		if (len != sizeof(sin6->sin6_addr))
566eb74b996SHajimu UMEMOTO 			return -1;
567eb74b996SHajimu UMEMOTO 		/* you will lose scope info */
568eb74b996SHajimu UMEMOTO 		sin6 = (struct sockaddr_in6 *)&ss;
569eb74b996SHajimu UMEMOTO 		sin6->sin6_family = AF_INET6;
570eb74b996SHajimu UMEMOTO 		sin6->sin6_len = sizeof(struct sockaddr_in6);
571eb74b996SHajimu UMEMOTO 		memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
572eb74b996SHajimu UMEMOTO 		break;
573eb74b996SHajimu UMEMOTO #endif
574eb74b996SHajimu UMEMOTO 	default:
575eb74b996SHajimu UMEMOTO 		return -1;
576eb74b996SHajimu UMEMOTO 	}
577eb74b996SHajimu UMEMOTO 
578eb74b996SHajimu UMEMOTO 	sa = (struct sockaddr *)&ss;
579eb74b996SHajimu UMEMOTO 	return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
580eb74b996SHajimu UMEMOTO }
581eb74b996SHajimu UMEMOTO 
582eb74b996SHajimu UMEMOTO /*
583eb74b996SHajimu UMEMOTO  * Returns 0 if ok, -1 if not ok.
584eb74b996SHajimu UMEMOTO  */
585eb74b996SHajimu UMEMOTO int
586eb74b996SHajimu UMEMOTO __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
587eb74b996SHajimu UMEMOTO 	FILE *hostf;
588eb74b996SHajimu UMEMOTO 	const struct sockaddr *raddr;
589eb74b996SHajimu UMEMOTO 	socklen_t salen;
590eb74b996SHajimu UMEMOTO 	const char *luser, *ruser;
591eb74b996SHajimu UMEMOTO {
59258f0484fSRodney W. Grimes 	register char *user, *p;
59358f0484fSRodney W. Grimes 	int ch;
59458f0484fSRodney W. Grimes 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
59597cb5094SBill Paul 	char hname[MAXHOSTNAMELEN];
5968538335fSBill Paul 	/* Presumed guilty until proven innocent. */
5978538335fSBill Paul 	int userok = 0, hostok = 0;
59842b4f28eSYoshinobu Inoue 	int h_error;
5991e890b05SBill Paul #ifdef YP
6001e890b05SBill Paul 	char *ypdomain;
6018538335fSBill Paul 
6021e890b05SBill Paul 	if (yp_get_default_domain(&ypdomain))
6031e890b05SBill Paul 		ypdomain = NULL;
6041e890b05SBill Paul #else
6051e890b05SBill Paul #define	ypdomain NULL
6061e890b05SBill Paul #endif
6078538335fSBill Paul 	/* We need to get the damn hostname back for netgroup matching. */
608eb74b996SHajimu UMEMOTO 	if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
609eb74b996SHajimu UMEMOTO 			NI_NAMEREQD) != 0)
6108538335fSBill Paul 		return (-1);
61158f0484fSRodney W. Grimes 
61258f0484fSRodney W. Grimes 	while (fgets(buf, sizeof(buf), hostf)) {
61358f0484fSRodney W. Grimes 		p = buf;
61458f0484fSRodney W. Grimes 		/* Skip lines that are too long. */
615a9f9141cSBrian S. Dean 		if (strchr(p, '\n') == NULL) {
61658f0484fSRodney W. Grimes 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
61758f0484fSRodney W. Grimes 			continue;
61858f0484fSRodney W. Grimes 		}
619acc7e87cSPeter Wemm 		if (*p == '\n' || *p == '#') {
620acc7e87cSPeter Wemm 			/* comment... */
621acc7e87cSPeter Wemm 			continue;
622acc7e87cSPeter Wemm 		}
62358f0484fSRodney W. Grimes 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
6243fb85bffSAndrey A. Chernov 			*p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
62558f0484fSRodney W. Grimes 			p++;
62658f0484fSRodney W. Grimes 		}
62758f0484fSRodney W. Grimes 		if (*p == ' ' || *p == '\t') {
62858f0484fSRodney W. Grimes 			*p++ = '\0';
62958f0484fSRodney W. Grimes 			while (*p == ' ' || *p == '\t')
63058f0484fSRodney W. Grimes 				p++;
63158f0484fSRodney W. Grimes 			user = p;
63258f0484fSRodney W. Grimes 			while (*p != '\n' && *p != ' ' &&
63358f0484fSRodney W. Grimes 			    *p != '\t' && *p != '\0')
63458f0484fSRodney W. Grimes 				p++;
63558f0484fSRodney W. Grimes 		} else
63658f0484fSRodney W. Grimes 			user = p;
63758f0484fSRodney W. Grimes 		*p = '\0';
6388538335fSBill Paul 		/*
6398538335fSBill Paul 		 * Do +/- and +@/-@ checking. This looks really nasty,
6408538335fSBill Paul 		 * but it matches SunOS's behavior so far as I can tell.
6418538335fSBill Paul 		 */
6428538335fSBill Paul 		switch(buf[0]) {
6438538335fSBill Paul 		case '+':
6448538335fSBill Paul 			if (!buf[1]) {     /* '+' matches all hosts */
6458538335fSBill Paul 				hostok = 1;
6468538335fSBill Paul 				break;
64758f0484fSRodney W. Grimes 			}
6488538335fSBill Paul 			if (buf[1] == '@')  /* match a host by netgroup */
64997cb5094SBill Paul 				hostok = innetgr((char *)&buf[2],
65097cb5094SBill Paul 					(char *)&hname, NULL, ypdomain);
6518538335fSBill Paul 			else		/* match a host by addr */
652eb74b996SHajimu UMEMOTO 				hostok = __icheckhost(raddr, salen,
653eb74b996SHajimu UMEMOTO 						      (char *)&buf[1]);
6548538335fSBill Paul 			break;
6558538335fSBill Paul 		case '-':     /* reject '-' hosts and all their users */
6568538335fSBill Paul 			if (buf[1] == '@') {
6578538335fSBill Paul 				if (innetgr((char *)&buf[2],
65897cb5094SBill Paul 					      (char *)&hname, NULL, ypdomain))
6598538335fSBill Paul 					return(-1);
6608538335fSBill Paul 			} else {
661eb74b996SHajimu UMEMOTO 				if (__icheckhost(raddr, salen,
662eb74b996SHajimu UMEMOTO 						 (char *)&buf[1]))
6638538335fSBill Paul 					return(-1);
6648538335fSBill Paul 			}
6658538335fSBill Paul 			break;
6668538335fSBill Paul 		default:  /* if no '+' or '-', do a simple match */
667eb74b996SHajimu UMEMOTO 			hostok = __icheckhost(raddr, salen, buf);
6688538335fSBill Paul 			break;
6698538335fSBill Paul 		}
6708538335fSBill Paul 		switch(*user) {
6718538335fSBill Paul 		case '+':
6728538335fSBill Paul 			if (!*(user+1)) {      /* '+' matches all users */
6738538335fSBill Paul 				userok = 1;
6748538335fSBill Paul 				break;
6758538335fSBill Paul 			}
6768538335fSBill Paul 			if (*(user+1) == '@')  /* match a user by netgroup */
6771e890b05SBill Paul 				userok = innetgr(user+2, NULL, ruser, ypdomain);
6788538335fSBill Paul 			else	   /* match a user by direct specification */
6798538335fSBill Paul 				userok = !(strcmp(ruser, user+1));
6808538335fSBill Paul 			break;
6818538335fSBill Paul 		case '-': 		/* if we matched a hostname, */
6828538335fSBill Paul 			if (hostok) {   /* check for user field rejections */
6838538335fSBill Paul 				if (!*(user+1))
6848538335fSBill Paul 					return(-1);
6858538335fSBill Paul 				if (*(user+1) == '@') {
6868538335fSBill Paul 					if (innetgr(user+2, NULL,
6871e890b05SBill Paul 							ruser, ypdomain))
6888538335fSBill Paul 						return(-1);
6898538335fSBill Paul 				} else {
6908538335fSBill Paul 					if (!strcmp(ruser, user+1))
6918538335fSBill Paul 						return(-1);
6928538335fSBill Paul 				}
6938538335fSBill Paul 			}
6948538335fSBill Paul 			break;
6958538335fSBill Paul 		default:	/* no rejections: try to match the user */
6968538335fSBill Paul 			if (hostok)
6978538335fSBill Paul 				userok = !(strcmp(ruser,*user ? user : luser));
6988538335fSBill Paul 			break;
6998538335fSBill Paul 		}
7008538335fSBill Paul 		if (hostok && userok)
7018538335fSBill Paul 			return(0);
70258f0484fSRodney W. Grimes 	}
70358f0484fSRodney W. Grimes 	return (-1);
70458f0484fSRodney W. Grimes }
70558f0484fSRodney W. Grimes 
70658f0484fSRodney W. Grimes /*
70758f0484fSRodney W. Grimes  * Returns "true" if match, 0 if no match.
708eb74b996SHajimu UMEMOTO  *
709eb74b996SHajimu UMEMOTO  * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
710eb74b996SHajimu UMEMOTO  * if af == AF_INET6.
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,
739eb74b996SHajimu UMEMOTO 			NI_NUMERICHOST | NI_WITHSCOPEID) != 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),
754eb74b996SHajimu UMEMOTO 				NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 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