xref: /freebsd/lib/libc/net/rcmd.c (revision 68ca8363c7a19d5351dc2b10568cbf2403e07e33)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1983, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
3258f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3358f0484fSRodney W. Grimes static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
3458f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
35333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
36333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3758f0484fSRodney W. Grimes 
38d201fe46SDaniel Eischen #include "namespace.h"
3958f0484fSRodney W. Grimes #include <sys/param.h>
4058f0484fSRodney W. Grimes #include <sys/socket.h>
4158f0484fSRodney W. Grimes #include <sys/stat.h>
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <netinet/in.h>
4458f0484fSRodney W. Grimes #include <arpa/inet.h>
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes #include <signal.h>
4758f0484fSRodney W. Grimes #include <fcntl.h>
4858f0484fSRodney W. Grimes #include <netdb.h>
49d4567212SWarner Losh #include <stdlib.h>
5058f0484fSRodney W. Grimes #include <unistd.h>
5158f0484fSRodney W. Grimes #include <pwd.h>
5258f0484fSRodney W. Grimes #include <errno.h>
5358f0484fSRodney W. Grimes #include <stdio.h>
5458f0484fSRodney W. Grimes #include <ctype.h>
5558f0484fSRodney W. Grimes #include <string.h>
561e890b05SBill Paul #include <rpc/rpc.h>
576c58990dSBjoern A. Zeeb #ifdef YP
581e890b05SBill Paul #include <rpcsvc/yp_prot.h>
591e890b05SBill Paul #include <rpcsvc/ypclnt.h>
601e890b05SBill Paul #endif
61c98e299eSHajimu UMEMOTO #include <arpa/nameser.h>
62d201fe46SDaniel Eischen #include "un-namespace.h"
63bd6060a1SKonstantin Belousov #include "libc_private.h"
6458f0484fSRodney W. Grimes 
65c05ac53bSDavid E. O'Brien extern int innetgr( const char *, const char *, const char *, const char * );
6651295a4dSJordan K. Hubbard 
67d1f32ba5SGeoff Rehmet #define max(a, b)	((a > b) ? a : b)
68d1f32ba5SGeoff Rehmet 
69c05ac53bSDavid E. O'Brien int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
701372519bSDavid E. O'Brien int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
711372519bSDavid E. O'Brien int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
721372519bSDavid E. O'Brien     const char *);
731372519bSDavid E. O'Brien static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
7442b4f28eSYoshinobu Inoue 
75eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST];
7658f0484fSRodney W. Grimes 
7758f0484fSRodney W. Grimes int
783ba5ea24SCraig Rodrigues rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
793ba5ea24SCraig Rodrigues     const char *cmd, int *fd2p)
8058f0484fSRodney W. Grimes {
810cac72f4SYoshinobu Inoue 	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
820cac72f4SYoshinobu Inoue }
830cac72f4SYoshinobu Inoue 
840cac72f4SYoshinobu Inoue int
853ba5ea24SCraig Rodrigues rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser,
863ba5ea24SCraig Rodrigues     const char *cmd, int *fd2p, int af)
870cac72f4SYoshinobu Inoue {
8842b4f28eSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
8942b4f28eSYoshinobu Inoue 	struct sockaddr_storage from;
9058f0484fSRodney W. Grimes 	fd_set reads;
91d201fe46SDaniel Eischen 	sigset_t oldmask, newmask;
9258f0484fSRodney W. Grimes 	pid_t pid;
9342b4f28eSYoshinobu Inoue 	int s, aport, lport, timo, error;
94d4567212SWarner Losh 	char c, *p;
95eb74b996SHajimu UMEMOTO 	int refused, nres;
96c98e299eSHajimu UMEMOTO 	char num[8];
97c98e299eSHajimu UMEMOTO 	static char canonnamebuf[MAXDNAME];	/* is it proper here? */
9858f0484fSRodney W. Grimes 
99d4567212SWarner Losh 	/* call rcmdsh() with specified remote shell if appropriate. */
100*68ca8363SMark Johnston 	if ((p = secure_getenv("RSH")) != NULL) {
101d4567212SWarner Losh 		struct servent *sp = getservbyname("shell", "tcp");
102d4567212SWarner Losh 
103d4567212SWarner Losh 		if (sp && sp->s_port == rport)
104d4567212SWarner Losh 			return (rcmdsh(ahost, rport, locuser, remuser,
105d4567212SWarner Losh 			    cmd, p));
106d4567212SWarner Losh 	}
107d4567212SWarner Losh 
108d4567212SWarner Losh 	/* use rsh(1) if non-root and remote port is shell. */
109d4567212SWarner Losh 	if (geteuid()) {
110d4567212SWarner Losh 		struct servent *sp = getservbyname("shell", "tcp");
111d4567212SWarner Losh 
112d4567212SWarner Losh 		if (sp && sp->s_port == rport)
113d4567212SWarner Losh 			return (rcmdsh(ahost, rport, locuser, remuser,
114d4567212SWarner Losh 			    cmd, NULL));
115d4567212SWarner Losh 	}
116d4567212SWarner Losh 
11758f0484fSRodney W. Grimes 	pid = getpid();
11842b4f28eSYoshinobu Inoue 
11942b4f28eSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
12042b4f28eSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
1210cac72f4SYoshinobu Inoue 	hints.ai_family = af;
12242b4f28eSYoshinobu Inoue 	hints.ai_socktype = SOCK_STREAM;
12342b4f28eSYoshinobu Inoue 	hints.ai_protocol = 0;
12442b4f28eSYoshinobu Inoue 	(void)snprintf(num, sizeof(num), "%d", ntohs(rport));
12542b4f28eSYoshinobu Inoue 	error = getaddrinfo(*ahost, num, &hints, &res);
12642b4f28eSYoshinobu Inoue 	if (error) {
12742b4f28eSYoshinobu Inoue 		fprintf(stderr, "rcmd: getaddrinfo: %s\n",
12842b4f28eSYoshinobu Inoue 			gai_strerror(error));
12942b4f28eSYoshinobu Inoue 		if (error == EAI_SYSTEM)
13042b4f28eSYoshinobu Inoue 			fprintf(stderr, "rcmd: getaddrinfo: %s\n",
13142b4f28eSYoshinobu Inoue 				strerror(errno));
13258f0484fSRodney W. Grimes 		return (-1);
13358f0484fSRodney W. Grimes 	}
134c98e299eSHajimu UMEMOTO 
135c98e299eSHajimu UMEMOTO 	if (res->ai_canonname
136c98e299eSHajimu UMEMOTO 	 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
137c98e299eSHajimu UMEMOTO 		strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
138c98e299eSHajimu UMEMOTO 		*ahost = canonnamebuf;
139c98e299eSHajimu UMEMOTO 	}
140eb74b996SHajimu UMEMOTO 	nres = 0;
141eb74b996SHajimu UMEMOTO 	for (ai = res; ai; ai = ai->ai_next)
142eb74b996SHajimu UMEMOTO 		nres++;
14342b4f28eSYoshinobu Inoue 	ai = res;
144eb74b996SHajimu UMEMOTO 	refused = 0;
145d201fe46SDaniel Eischen 	sigemptyset(&newmask);
146d201fe46SDaniel Eischen 	sigaddset(&newmask, SIGURG);
147bd6060a1SKonstantin Belousov 	__libc_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
14858f0484fSRodney W. Grimes 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
14942b4f28eSYoshinobu Inoue 		s = rresvport_af(&lport, ai->ai_family);
15058f0484fSRodney W. Grimes 		if (s < 0) {
151474ce1d1SYoshinobu Inoue 			if (errno != EAGAIN && ai->ai_next) {
152474ce1d1SYoshinobu Inoue 				ai = ai->ai_next;
153474ce1d1SYoshinobu Inoue 				continue;
154474ce1d1SYoshinobu Inoue 			}
15558f0484fSRodney W. Grimes 			if (errno == EAGAIN)
15658f0484fSRodney W. Grimes 				(void)fprintf(stderr,
15758f0484fSRodney W. Grimes 				    "rcmd: socket: All ports in use\n");
15858f0484fSRodney W. Grimes 			else
15958f0484fSRodney W. Grimes 				(void)fprintf(stderr, "rcmd: socket: %s\n",
16058f0484fSRodney W. Grimes 				    strerror(errno));
16142b4f28eSYoshinobu Inoue 			freeaddrinfo(res);
162bd6060a1SKonstantin Belousov 			__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
163d201fe46SDaniel Eischen 			    NULL);
16458f0484fSRodney W. Grimes 			return (-1);
16558f0484fSRodney W. Grimes 		}
1669233c4d9SJason Evans 		_fcntl(s, F_SETOWN, pid);
167d201fe46SDaniel Eischen 		if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
16858f0484fSRodney W. Grimes 			break;
1699233c4d9SJason Evans 		(void)_close(s);
17058f0484fSRodney W. Grimes 		if (errno == EADDRINUSE) {
17158f0484fSRodney W. Grimes 			lport--;
17258f0484fSRodney W. Grimes 			continue;
17358f0484fSRodney W. Grimes 		}
174eb74b996SHajimu UMEMOTO 		if (errno == ECONNREFUSED)
175eb74b996SHajimu UMEMOTO 			refused = 1;
176eb74b996SHajimu UMEMOTO 		if (ai->ai_next == NULL && (!refused || timo > 16)) {
177eb74b996SHajimu UMEMOTO 			(void)fprintf(stderr, "%s: %s\n",
178eb74b996SHajimu UMEMOTO 				      *ahost, strerror(errno));
179eb74b996SHajimu UMEMOTO 			freeaddrinfo(res);
180bd6060a1SKonstantin Belousov 			__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
181d201fe46SDaniel Eischen 			    NULL);
182eb74b996SHajimu UMEMOTO 			return (-1);
1832368b03bSHajimu UMEMOTO 		}
184eb74b996SHajimu UMEMOTO 		if (nres > 1) {
18558f0484fSRodney W. Grimes 			int oerrno = errno;
18658f0484fSRodney W. Grimes 
1874f101318SHajimu UMEMOTO 			getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
1884f101318SHajimu UMEMOTO 			    sizeof(paddr), NULL, 0, NI_NUMERICHOST);
18958f0484fSRodney W. Grimes 			(void)fprintf(stderr, "connect to address %s: ",
19042b4f28eSYoshinobu Inoue 				      paddr);
19158f0484fSRodney W. Grimes 			errno = oerrno;
19258f0484fSRodney W. Grimes 			perror(0);
193eb74b996SHajimu UMEMOTO 		}
194eb74b996SHajimu UMEMOTO 		if ((ai = ai->ai_next) == NULL) {
195eb74b996SHajimu UMEMOTO 			/* refused && timo <= 16 */
196eb74b996SHajimu UMEMOTO 			struct timespec time_to_sleep, time_remaining;
197eb74b996SHajimu UMEMOTO 
198eb74b996SHajimu UMEMOTO 			time_to_sleep.tv_sec = timo;
199eb74b996SHajimu UMEMOTO 			time_to_sleep.tv_nsec = 0;
200eb74b996SHajimu UMEMOTO 			(void)_nanosleep(&time_to_sleep, &time_remaining);
201eb74b996SHajimu UMEMOTO 			timo *= 2;
202eb74b996SHajimu UMEMOTO 			ai = res;
203eb74b996SHajimu UMEMOTO 			refused = 0;
204eb74b996SHajimu UMEMOTO 		}
205eb74b996SHajimu UMEMOTO 		if (nres > 1) {
2064f101318SHajimu UMEMOTO 			getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
2074f101318SHajimu UMEMOTO 			    sizeof(paddr), NULL, 0, NI_NUMERICHOST);
20842b4f28eSYoshinobu Inoue 			fprintf(stderr, "Trying %s...\n", paddr);
20958f0484fSRodney W. Grimes 		}
21058f0484fSRodney W. Grimes 	}
21158f0484fSRodney W. Grimes 	lport--;
212513004a2SPedro F. Giffuni 	if (fd2p == NULL) {
2139233c4d9SJason Evans 		_write(s, "", 1);
21458f0484fSRodney W. Grimes 		lport = 0;
21558f0484fSRodney W. Grimes 	} else {
21642b4f28eSYoshinobu Inoue 		int s2 = rresvport_af(&lport, ai->ai_family), s3;
21710248e3aSStefan Farfeleder 		socklen_t len = ai->ai_addrlen;
218d1f32ba5SGeoff Rehmet 		int nfds;
21958f0484fSRodney W. Grimes 
22058f0484fSRodney W. Grimes 		if (s2 < 0)
22158f0484fSRodney W. Grimes 			goto bad;
222d201fe46SDaniel Eischen 		_listen(s2, 1);
22358f0484fSRodney W. Grimes 		(void)snprintf(num, sizeof(num), "%d", lport);
2249233c4d9SJason Evans 		if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
22558f0484fSRodney W. Grimes 			(void)fprintf(stderr,
22658f0484fSRodney W. Grimes 			    "rcmd: write (setting up stderr): %s\n",
22758f0484fSRodney W. Grimes 			    strerror(errno));
2289233c4d9SJason Evans 			(void)_close(s2);
22958f0484fSRodney W. Grimes 			goto bad;
23058f0484fSRodney W. Grimes 		}
231d1f32ba5SGeoff Rehmet 		nfds = max(s, s2)+1;
232d1f32ba5SGeoff Rehmet 		if(nfds > FD_SETSIZE) {
233d1f32ba5SGeoff Rehmet 			fprintf(stderr, "rcmd: too many files\n");
2349233c4d9SJason Evans 			(void)_close(s2);
235d1f32ba5SGeoff Rehmet 			goto bad;
236d1f32ba5SGeoff Rehmet 		}
237ce70b6caSPeter Wemm again:
23858f0484fSRodney W. Grimes 		FD_ZERO(&reads);
23958f0484fSRodney W. Grimes 		FD_SET(s, &reads);
24058f0484fSRodney W. Grimes 		FD_SET(s2, &reads);
24158f0484fSRodney W. Grimes 		errno = 0;
242d201fe46SDaniel Eischen 		if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
24358f0484fSRodney W. Grimes 			if (errno != 0)
24458f0484fSRodney W. Grimes 				(void)fprintf(stderr,
24558f0484fSRodney W. Grimes 				    "rcmd: select (setting up stderr): %s\n",
24658f0484fSRodney W. Grimes 				    strerror(errno));
24758f0484fSRodney W. Grimes 			else
24858f0484fSRodney W. Grimes 				(void)fprintf(stderr,
24958f0484fSRodney W. Grimes 				"select: protocol failure in circuit setup\n");
2509233c4d9SJason Evans 			(void)_close(s2);
25158f0484fSRodney W. Grimes 			goto bad;
25258f0484fSRodney W. Grimes 		}
253d201fe46SDaniel Eischen 		s3 = _accept(s2, (struct sockaddr *)&from, &len);
25442b4f28eSYoshinobu Inoue 		switch (from.ss_family) {
25542b4f28eSYoshinobu Inoue 		case AF_INET:
25642b4f28eSYoshinobu Inoue 			aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
25742b4f28eSYoshinobu Inoue 			break;
25842b4f28eSYoshinobu Inoue #ifdef INET6
25942b4f28eSYoshinobu Inoue 		case AF_INET6:
26042b4f28eSYoshinobu Inoue 			aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
26142b4f28eSYoshinobu Inoue 			break;
26242b4f28eSYoshinobu Inoue #endif
26342b4f28eSYoshinobu Inoue 		default:
26442b4f28eSYoshinobu Inoue 			aport = 0;	/* error */
26542b4f28eSYoshinobu Inoue 			break;
26642b4f28eSYoshinobu Inoue 		}
267ce70b6caSPeter Wemm 		/*
268ce70b6caSPeter Wemm 		 * XXX careful for ftp bounce attacks. If discovered, shut them
269ce70b6caSPeter Wemm 		 * down and check for the real auxiliary channel to connect.
270ce70b6caSPeter Wemm 		 */
27142b4f28eSYoshinobu Inoue 		if (aport == 20) {
2729233c4d9SJason Evans 			_close(s3);
273ce70b6caSPeter Wemm 			goto again;
274ce70b6caSPeter Wemm 		}
2759233c4d9SJason Evans 		(void)_close(s2);
27658f0484fSRodney W. Grimes 		if (s3 < 0) {
27758f0484fSRodney W. Grimes 			(void)fprintf(stderr,
27858f0484fSRodney W. Grimes 			    "rcmd: accept: %s\n", strerror(errno));
27958f0484fSRodney W. Grimes 			lport = 0;
28058f0484fSRodney W. Grimes 			goto bad;
28158f0484fSRodney W. Grimes 		}
28258f0484fSRodney W. Grimes 		*fd2p = s3;
28342b4f28eSYoshinobu Inoue 		if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
28458f0484fSRodney W. Grimes 			(void)fprintf(stderr,
28558f0484fSRodney W. Grimes 			    "socket: protocol failure in circuit setup.\n");
28658f0484fSRodney W. Grimes 			goto bad2;
28758f0484fSRodney W. Grimes 		}
28858f0484fSRodney W. Grimes 	}
2899233c4d9SJason Evans 	(void)_write(s, locuser, strlen(locuser)+1);
2909233c4d9SJason Evans 	(void)_write(s, remuser, strlen(remuser)+1);
2919233c4d9SJason Evans 	(void)_write(s, cmd, strlen(cmd)+1);
2929233c4d9SJason Evans 	if (_read(s, &c, 1) != 1) {
29358f0484fSRodney W. Grimes 		(void)fprintf(stderr,
29458f0484fSRodney W. Grimes 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
29558f0484fSRodney W. Grimes 		goto bad2;
29658f0484fSRodney W. Grimes 	}
29758f0484fSRodney W. Grimes 	if (c != 0) {
2989233c4d9SJason Evans 		while (_read(s, &c, 1) == 1) {
2999233c4d9SJason Evans 			(void)_write(STDERR_FILENO, &c, 1);
30058f0484fSRodney W. Grimes 			if (c == '\n')
30158f0484fSRodney W. Grimes 				break;
30258f0484fSRodney W. Grimes 		}
30358f0484fSRodney W. Grimes 		goto bad2;
30458f0484fSRodney W. Grimes 	}
305bd6060a1SKonstantin Belousov 	__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
30642b4f28eSYoshinobu Inoue 	freeaddrinfo(res);
30758f0484fSRodney W. Grimes 	return (s);
30858f0484fSRodney W. Grimes bad2:
30958f0484fSRodney W. Grimes 	if (lport)
3109233c4d9SJason Evans 		(void)_close(*fd2p);
31158f0484fSRodney W. Grimes bad:
3129233c4d9SJason Evans 	(void)_close(s);
313bd6060a1SKonstantin Belousov 	__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
31442b4f28eSYoshinobu Inoue 	freeaddrinfo(res);
31558f0484fSRodney W. Grimes 	return (-1);
31658f0484fSRodney W. Grimes }
31758f0484fSRodney W. Grimes 
31858f0484fSRodney W. Grimes int
3193ba5ea24SCraig Rodrigues rresvport(int *port)
32058f0484fSRodney W. Grimes {
32142b4f28eSYoshinobu Inoue 	return rresvport_af(port, AF_INET);
32242b4f28eSYoshinobu Inoue }
32358f0484fSRodney W. Grimes 
32442b4f28eSYoshinobu Inoue int
3253ba5ea24SCraig Rodrigues rresvport_af(int *alport, int family)
32642b4f28eSYoshinobu Inoue {
3276d7bd75aSJacques Vidrine 	int s;
32842b4f28eSYoshinobu Inoue 	struct sockaddr_storage ss;
32942b4f28eSYoshinobu Inoue 	u_short *sport;
33042b4f28eSYoshinobu Inoue 
33142b4f28eSYoshinobu Inoue 	memset(&ss, 0, sizeof(ss));
33242b4f28eSYoshinobu Inoue 	ss.ss_family = family;
33342b4f28eSYoshinobu Inoue 	switch (family) {
33442b4f28eSYoshinobu Inoue 	case AF_INET:
33542b4f28eSYoshinobu Inoue 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
33642b4f28eSYoshinobu Inoue 		sport = &((struct sockaddr_in *)&ss)->sin_port;
33742b4f28eSYoshinobu Inoue 		((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
33842b4f28eSYoshinobu Inoue 		break;
33942b4f28eSYoshinobu Inoue #ifdef INET6
34042b4f28eSYoshinobu Inoue 	case AF_INET6:
34142b4f28eSYoshinobu Inoue 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
34242b4f28eSYoshinobu Inoue 		sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
34342b4f28eSYoshinobu Inoue 		((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
34442b4f28eSYoshinobu Inoue 		break;
34542b4f28eSYoshinobu Inoue #endif
34642b4f28eSYoshinobu Inoue 	default:
34742b4f28eSYoshinobu Inoue 		errno = EAFNOSUPPORT;
34842b4f28eSYoshinobu Inoue 		return -1;
34942b4f28eSYoshinobu Inoue 	}
35042b4f28eSYoshinobu Inoue 
351d201fe46SDaniel Eischen 	s = _socket(ss.ss_family, SOCK_STREAM, 0);
35258f0484fSRodney W. Grimes 	if (s < 0)
35358f0484fSRodney W. Grimes 		return (-1);
354ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */
355ce70b6caSPeter Wemm 	sin.sin_port = htons((u_short)*alport);
356d201fe46SDaniel Eischen 	if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
35758f0484fSRodney W. Grimes 		return (s);
358ce70b6caSPeter Wemm 	if (errno != EADDRINUSE) {
3599233c4d9SJason Evans 		(void)_close(s);
36058f0484fSRodney W. Grimes 		return (-1);
36158f0484fSRodney W. Grimes 	}
362ce70b6caSPeter Wemm #endif
36342b4f28eSYoshinobu Inoue 	*sport = 0;
364ae42b666SYoshinobu Inoue 	if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
3659233c4d9SJason Evans 		(void)_close(s);
366ce70b6caSPeter Wemm 		return (-1);
367ce70b6caSPeter Wemm 	}
36842b4f28eSYoshinobu Inoue 	*alport = (int)ntohs(*sport);
369ce70b6caSPeter Wemm 	return (s);
370ce70b6caSPeter Wemm }
37158f0484fSRodney W. Grimes 
37258f0484fSRodney W. Grimes int	__check_rhosts_file = 1;
37358f0484fSRodney W. Grimes char	*__rcmd_errstr;
37458f0484fSRodney W. Grimes 
37558f0484fSRodney W. Grimes int
3763ba5ea24SCraig Rodrigues ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
37758f0484fSRodney W. Grimes {
378e3be4d7bSYoshinobu Inoue 	struct addrinfo hints, *res, *r;
379e3be4d7bSYoshinobu Inoue 	int error;
38042b4f28eSYoshinobu Inoue 
381e3be4d7bSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
382e3be4d7bSYoshinobu Inoue 	hints.ai_family = PF_UNSPEC;
383e3be4d7bSYoshinobu Inoue 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
384e3be4d7bSYoshinobu Inoue 	error = getaddrinfo(rhost, "0", &hints, &res);
385e3be4d7bSYoshinobu Inoue 	if (error)
38658f0484fSRodney W. Grimes 		return (-1);
387e3be4d7bSYoshinobu Inoue 
388e3be4d7bSYoshinobu Inoue 	for (r = res; r; r = r->ai_next) {
389e3be4d7bSYoshinobu Inoue 		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
390e3be4d7bSYoshinobu Inoue 		    luser) == 0) {
391e3be4d7bSYoshinobu Inoue 			freeaddrinfo(res);
392e3be4d7bSYoshinobu Inoue 			return (0);
39342b4f28eSYoshinobu Inoue 		}
39442b4f28eSYoshinobu Inoue 	}
395e3be4d7bSYoshinobu Inoue 	freeaddrinfo(res);
396e3be4d7bSYoshinobu Inoue 	return (-1);
39758f0484fSRodney W. Grimes }
39858f0484fSRodney W. Grimes 
39958f0484fSRodney W. Grimes /*
40058f0484fSRodney W. Grimes  * New .rhosts strategy: We are passed an ip address. We spin through
40158f0484fSRodney W. Grimes  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
40258f0484fSRodney W. Grimes  * has ip addresses, we don't have to trust a nameserver.  When it
40358f0484fSRodney W. Grimes  * contains hostnames, we spin through the list of addresses the nameserver
40458f0484fSRodney W. Grimes  * gives us and look for a match.
40558f0484fSRodney W. Grimes  *
40658f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
40758f0484fSRodney W. Grimes  */
40858f0484fSRodney W. Grimes int
4093ba5ea24SCraig Rodrigues iruserok(unsigned long raddr, int superuser, const char *ruser, const char *luser)
41058f0484fSRodney W. Grimes {
411eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
412eb74b996SHajimu UMEMOTO 
413eb74b996SHajimu UMEMOTO 	memset(&sin, 0, sizeof(sin));
414eb74b996SHajimu UMEMOTO 	sin.sin_family = AF_INET;
415eb74b996SHajimu UMEMOTO 	sin.sin_len = sizeof(struct sockaddr_in);
416eb74b996SHajimu UMEMOTO 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
417eb74b996SHajimu UMEMOTO 	return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
418eb74b996SHajimu UMEMOTO 		ruser, luser);
41942b4f28eSYoshinobu Inoue }
42042b4f28eSYoshinobu Inoue 
421eb74b996SHajimu UMEMOTO /*
422eb74b996SHajimu UMEMOTO  * AF independent extension of iruserok.
423eb74b996SHajimu UMEMOTO  *
424eb74b996SHajimu UMEMOTO  * Returns 0 if ok, -1 if not ok.
425eb74b996SHajimu UMEMOTO  */
426eb74b996SHajimu UMEMOTO int
4273ba5ea24SCraig Rodrigues iruserok_sa(const void *ra, int rlen, int superuser, const char *ruser,
4283ba5ea24SCraig Rodrigues     const char *luser)
42942b4f28eSYoshinobu Inoue {
4308fb3f3f6SDavid E. O'Brien 	char *cp;
43158f0484fSRodney W. Grimes 	struct stat sbuf;
43258f0484fSRodney W. Grimes 	struct passwd *pwd;
43358f0484fSRodney W. Grimes 	FILE *hostf;
43458f0484fSRodney W. Grimes 	uid_t uid;
43558f0484fSRodney W. Grimes 	int first;
43658f0484fSRodney W. Grimes 	char pbuf[MAXPATHLEN];
437eb74b996SHajimu UMEMOTO 	const struct sockaddr *raddr;
438eb74b996SHajimu UMEMOTO 	struct sockaddr_storage ss;
43942b4f28eSYoshinobu Inoue 
440eb74b996SHajimu UMEMOTO 	/* avoid alignment issue */
44151d7f2edSMark Johnston 	if (rlen <= 0 || rlen > sizeof(ss))
442eb74b996SHajimu UMEMOTO 		return (-1);
443eb74b996SHajimu UMEMOTO 	memcpy(&ss, ra, rlen);
444eb74b996SHajimu UMEMOTO 	raddr = (struct sockaddr *)&ss;
44558f0484fSRodney W. Grimes 
44658f0484fSRodney W. Grimes 	first = 1;
447a93705b0SJilles Tjoelker 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re");
44858f0484fSRodney W. Grimes again:
44958f0484fSRodney W. Grimes 	if (hostf) {
450eb74b996SHajimu UMEMOTO 		if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
45158f0484fSRodney W. Grimes 			(void)fclose(hostf);
45258f0484fSRodney W. Grimes 			return (0);
45358f0484fSRodney W. Grimes 		}
45458f0484fSRodney W. Grimes 		(void)fclose(hostf);
45558f0484fSRodney W. Grimes 	}
45658f0484fSRodney W. Grimes 	if (first == 1 && (__check_rhosts_file || superuser)) {
45758f0484fSRodney W. Grimes 		first = 0;
45858f0484fSRodney W. Grimes 		if ((pwd = getpwnam(luser)) == NULL)
45958f0484fSRodney W. Grimes 			return (-1);
46090ceddb1SWarner Losh 		(void)strlcpy(pbuf, pwd->pw_dir, sizeof(pbuf));
46190ceddb1SWarner Losh 		(void)strlcat(pbuf, "/.rhosts", sizeof(pbuf));
46258f0484fSRodney W. Grimes 
46358f0484fSRodney W. Grimes 		/*
46458f0484fSRodney W. Grimes 		 * Change effective uid while opening .rhosts.  If root and
46558f0484fSRodney W. Grimes 		 * reading an NFS mounted file system, can't read files that
46658f0484fSRodney W. Grimes 		 * are protected read/write owner only.
46758f0484fSRodney W. Grimes 		 */
46858f0484fSRodney W. Grimes 		uid = geteuid();
46958f0484fSRodney W. Grimes 		(void)seteuid(pwd->pw_uid);
470a93705b0SJilles Tjoelker 		hostf = fopen(pbuf, "re");
47158f0484fSRodney W. Grimes 		(void)seteuid(uid);
47258f0484fSRodney W. Grimes 
47358f0484fSRodney W. Grimes 		if (hostf == NULL)
47458f0484fSRodney W. Grimes 			return (-1);
47558f0484fSRodney W. Grimes 		/*
47658f0484fSRodney W. Grimes 		 * If not a regular file, or is owned by someone other than
47758f0484fSRodney W. Grimes 		 * user or root or if writeable by anyone but the owner, quit.
47858f0484fSRodney W. Grimes 		 */
47958f0484fSRodney W. Grimes 		cp = NULL;
48058f0484fSRodney W. Grimes 		if (lstat(pbuf, &sbuf) < 0)
48158f0484fSRodney W. Grimes 			cp = ".rhosts lstat failed";
48258f0484fSRodney W. Grimes 		else if (!S_ISREG(sbuf.st_mode))
48358f0484fSRodney W. Grimes 			cp = ".rhosts not regular file";
484d201fe46SDaniel Eischen 		else if (_fstat(fileno(hostf), &sbuf) < 0)
48558f0484fSRodney W. Grimes 			cp = ".rhosts fstat failed";
48658f0484fSRodney W. Grimes 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
48758f0484fSRodney W. Grimes 			cp = "bad .rhosts owner";
48858f0484fSRodney W. Grimes 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
48958f0484fSRodney W. Grimes 			cp = ".rhosts writeable by other than owner";
49058f0484fSRodney W. Grimes 		/* If there were any problems, quit. */
49158f0484fSRodney W. Grimes 		if (cp) {
49258f0484fSRodney W. Grimes 			__rcmd_errstr = cp;
49358f0484fSRodney W. Grimes 			(void)fclose(hostf);
49458f0484fSRodney W. Grimes 			return (-1);
49558f0484fSRodney W. Grimes 		}
49658f0484fSRodney W. Grimes 		goto again;
49758f0484fSRodney W. Grimes 	}
49858f0484fSRodney W. Grimes 	return (-1);
49958f0484fSRodney W. Grimes }
50058f0484fSRodney W. Grimes 
50158f0484fSRodney W. Grimes /*
50258f0484fSRodney W. Grimes  * XXX
50358f0484fSRodney W. Grimes  * Don't make static, used by lpd(8).
50458f0484fSRodney W. Grimes  *
50558f0484fSRodney W. Grimes  * Returns 0 if ok, -1 if not ok.
50658f0484fSRodney W. Grimes  */
50758f0484fSRodney W. Grimes int
5083ba5ea24SCraig Rodrigues __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
50958f0484fSRodney W. Grimes {
510eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
511eb74b996SHajimu UMEMOTO 
512eb74b996SHajimu UMEMOTO 	memset(&sin, 0, sizeof(sin));
513eb74b996SHajimu UMEMOTO 	sin.sin_family = AF_INET;
514eb74b996SHajimu UMEMOTO 	sin.sin_len = sizeof(struct sockaddr_in);
515eb74b996SHajimu UMEMOTO 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
516eb74b996SHajimu UMEMOTO 	return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
517eb74b996SHajimu UMEMOTO 		luser, ruser);
51842b4f28eSYoshinobu Inoue }
51942b4f28eSYoshinobu Inoue 
520eb74b996SHajimu UMEMOTO /*
521eb74b996SHajimu UMEMOTO  * Returns 0 if ok, -1 if not ok.
522eb74b996SHajimu UMEMOTO  *
523eb74b996SHajimu UMEMOTO  * XXX obsolete API.
524eb74b996SHajimu UMEMOTO  */
52542b4f28eSYoshinobu Inoue int
5263ba5ea24SCraig Rodrigues __ivaliduser_af(FILE *hostf, const void *raddr, const char *luser,
5273ba5ea24SCraig Rodrigues     const char *ruser, int af, int len)
52842b4f28eSYoshinobu Inoue {
529eb74b996SHajimu UMEMOTO 	struct sockaddr *sa = NULL;
530eb74b996SHajimu UMEMOTO 	struct sockaddr_in *sin = NULL;
531eb74b996SHajimu UMEMOTO #ifdef INET6
532eb74b996SHajimu UMEMOTO 	struct sockaddr_in6 *sin6 = NULL;
533eb74b996SHajimu UMEMOTO #endif
534eb74b996SHajimu UMEMOTO 	struct sockaddr_storage ss;
535eb74b996SHajimu UMEMOTO 
536eb74b996SHajimu UMEMOTO 	memset(&ss, 0, sizeof(ss));
537eb74b996SHajimu UMEMOTO 	switch (af) {
538eb74b996SHajimu UMEMOTO 	case AF_INET:
539eb74b996SHajimu UMEMOTO 		if (len != sizeof(sin->sin_addr))
540eb74b996SHajimu UMEMOTO 			return -1;
541eb74b996SHajimu UMEMOTO 		sin = (struct sockaddr_in *)&ss;
542eb74b996SHajimu UMEMOTO 		sin->sin_family = AF_INET;
543eb74b996SHajimu UMEMOTO 		sin->sin_len = sizeof(struct sockaddr_in);
544eb74b996SHajimu UMEMOTO 		memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
545eb74b996SHajimu UMEMOTO 		break;
546eb74b996SHajimu UMEMOTO #ifdef INET6
547eb74b996SHajimu UMEMOTO 	case AF_INET6:
548eb74b996SHajimu UMEMOTO 		if (len != sizeof(sin6->sin6_addr))
549eb74b996SHajimu UMEMOTO 			return -1;
550eb74b996SHajimu UMEMOTO 		/* you will lose scope info */
551eb74b996SHajimu UMEMOTO 		sin6 = (struct sockaddr_in6 *)&ss;
552eb74b996SHajimu UMEMOTO 		sin6->sin6_family = AF_INET6;
553eb74b996SHajimu UMEMOTO 		sin6->sin6_len = sizeof(struct sockaddr_in6);
554eb74b996SHajimu UMEMOTO 		memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
555eb74b996SHajimu UMEMOTO 		break;
556eb74b996SHajimu UMEMOTO #endif
557eb74b996SHajimu UMEMOTO 	default:
558eb74b996SHajimu UMEMOTO 		return -1;
559eb74b996SHajimu UMEMOTO 	}
560eb74b996SHajimu UMEMOTO 
561eb74b996SHajimu UMEMOTO 	sa = (struct sockaddr *)&ss;
562eb74b996SHajimu UMEMOTO 	return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
563eb74b996SHajimu UMEMOTO }
564eb74b996SHajimu UMEMOTO 
565eb74b996SHajimu UMEMOTO int
5663ba5ea24SCraig Rodrigues __ivaliduser_sa(FILE *hostf, const struct sockaddr *raddr, socklen_t salen,
5673ba5ea24SCraig Rodrigues     const char *luser, const char *ruser)
568eb74b996SHajimu UMEMOTO {
5698fb3f3f6SDavid E. O'Brien 	char *user, *p;
57058f0484fSRodney W. Grimes 	int ch;
57158f0484fSRodney W. Grimes 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
57297cb5094SBill Paul 	char hname[MAXHOSTNAMELEN];
5738538335fSBill Paul 	/* Presumed guilty until proven innocent. */
5748538335fSBill Paul 	int userok = 0, hostok = 0;
5751e890b05SBill Paul #ifdef YP
5761e890b05SBill Paul 	char *ypdomain;
5778538335fSBill Paul 
5781e890b05SBill Paul 	if (yp_get_default_domain(&ypdomain))
5791e890b05SBill Paul 		ypdomain = NULL;
5801e890b05SBill Paul #else
5811e890b05SBill Paul #define	ypdomain NULL
5821e890b05SBill Paul #endif
5838538335fSBill Paul 	/* We need to get the damn hostname back for netgroup matching. */
584eb74b996SHajimu UMEMOTO 	if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
585eb74b996SHajimu UMEMOTO 			NI_NAMEREQD) != 0)
586c97c8f4aSJohn Polstra 		hname[0] = '\0';
58758f0484fSRodney W. Grimes 
58858f0484fSRodney W. Grimes 	while (fgets(buf, sizeof(buf), hostf)) {
58958f0484fSRodney W. Grimes 		p = buf;
59058f0484fSRodney W. Grimes 		/* Skip lines that are too long. */
591a9f9141cSBrian S. Dean 		if (strchr(p, '\n') == NULL) {
59258f0484fSRodney W. Grimes 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
59358f0484fSRodney W. Grimes 			continue;
59458f0484fSRodney W. Grimes 		}
595acc7e87cSPeter Wemm 		if (*p == '\n' || *p == '#') {
596acc7e87cSPeter Wemm 			/* comment... */
597acc7e87cSPeter Wemm 			continue;
598acc7e87cSPeter Wemm 		}
59958f0484fSRodney W. Grimes 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
6003fb85bffSAndrey A. Chernov 			*p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
60158f0484fSRodney W. Grimes 			p++;
60258f0484fSRodney W. Grimes 		}
60358f0484fSRodney W. Grimes 		if (*p == ' ' || *p == '\t') {
60458f0484fSRodney W. Grimes 			*p++ = '\0';
60558f0484fSRodney W. Grimes 			while (*p == ' ' || *p == '\t')
60658f0484fSRodney W. Grimes 				p++;
60758f0484fSRodney W. Grimes 			user = p;
60858f0484fSRodney W. Grimes 			while (*p != '\n' && *p != ' ' &&
60958f0484fSRodney W. Grimes 			    *p != '\t' && *p != '\0')
61058f0484fSRodney W. Grimes 				p++;
61158f0484fSRodney W. Grimes 		} else
61258f0484fSRodney W. Grimes 			user = p;
61358f0484fSRodney W. Grimes 		*p = '\0';
6148538335fSBill Paul 		/*
6158538335fSBill Paul 		 * Do +/- and +@/-@ checking. This looks really nasty,
6168538335fSBill Paul 		 * but it matches SunOS's behavior so far as I can tell.
6178538335fSBill Paul 		 */
6188538335fSBill Paul 		switch(buf[0]) {
6198538335fSBill Paul 		case '+':
6208538335fSBill Paul 			if (!buf[1]) {     /* '+' matches all hosts */
6218538335fSBill Paul 				hostok = 1;
6228538335fSBill Paul 				break;
62358f0484fSRodney W. Grimes 			}
6248538335fSBill Paul 			if (buf[1] == '@')  /* match a host by netgroup */
625c97c8f4aSJohn Polstra 				hostok = hname[0] != '\0' &&
626c97c8f4aSJohn Polstra 				    innetgr(&buf[2], hname, NULL, ypdomain);
6278538335fSBill Paul 			else		/* match a host by addr */
628eb74b996SHajimu UMEMOTO 				hostok = __icheckhost(raddr, salen,
629eb74b996SHajimu UMEMOTO 						      (char *)&buf[1]);
6308538335fSBill Paul 			break;
6318538335fSBill Paul 		case '-':     /* reject '-' hosts and all their users */
6328538335fSBill Paul 			if (buf[1] == '@') {
633c97c8f4aSJohn Polstra 				if (hname[0] == '\0' ||
634c97c8f4aSJohn Polstra 				    innetgr(&buf[2], hname, NULL, ypdomain))
6358538335fSBill Paul 					return(-1);
6368538335fSBill Paul 			} else {
637eb74b996SHajimu UMEMOTO 				if (__icheckhost(raddr, salen,
638eb74b996SHajimu UMEMOTO 						 (char *)&buf[1]))
6398538335fSBill Paul 					return(-1);
6408538335fSBill Paul 			}
6418538335fSBill Paul 			break;
6428538335fSBill Paul 		default:  /* if no '+' or '-', do a simple match */
643eb74b996SHajimu UMEMOTO 			hostok = __icheckhost(raddr, salen, buf);
6448538335fSBill Paul 			break;
6458538335fSBill Paul 		}
6468538335fSBill Paul 		switch(*user) {
6478538335fSBill Paul 		case '+':
6488538335fSBill Paul 			if (!*(user+1)) {      /* '+' matches all users */
6498538335fSBill Paul 				userok = 1;
6508538335fSBill Paul 				break;
6518538335fSBill Paul 			}
6528538335fSBill Paul 			if (*(user+1) == '@')  /* match a user by netgroup */
6531e890b05SBill Paul 				userok = innetgr(user+2, NULL, ruser, ypdomain);
6548538335fSBill Paul 			else	   /* match a user by direct specification */
6558538335fSBill Paul 				userok = !(strcmp(ruser, user+1));
6568538335fSBill Paul 			break;
6578538335fSBill Paul 		case '-': 		/* if we matched a hostname, */
6588538335fSBill Paul 			if (hostok) {   /* check for user field rejections */
6598538335fSBill Paul 				if (!*(user+1))
6608538335fSBill Paul 					return(-1);
6618538335fSBill Paul 				if (*(user+1) == '@') {
6628538335fSBill Paul 					if (innetgr(user+2, NULL,
6631e890b05SBill Paul 							ruser, ypdomain))
6648538335fSBill Paul 						return(-1);
6658538335fSBill Paul 				} else {
6668538335fSBill Paul 					if (!strcmp(ruser, user+1))
6678538335fSBill Paul 						return(-1);
6688538335fSBill Paul 				}
6698538335fSBill Paul 			}
6708538335fSBill Paul 			break;
6718538335fSBill Paul 		default:	/* no rejections: try to match the user */
6728538335fSBill Paul 			if (hostok)
6738538335fSBill Paul 				userok = !(strcmp(ruser,*user ? user : luser));
6748538335fSBill Paul 			break;
6758538335fSBill Paul 		}
6768538335fSBill Paul 		if (hostok && userok)
6778538335fSBill Paul 			return(0);
67858f0484fSRodney W. Grimes 	}
67958f0484fSRodney W. Grimes 	return (-1);
68058f0484fSRodney W. Grimes }
68158f0484fSRodney W. Grimes 
68258f0484fSRodney W. Grimes /*
68358f0484fSRodney W. Grimes  * Returns "true" if match, 0 if no match.
68458f0484fSRodney W. Grimes  */
68558f0484fSRodney W. Grimes static int
6863ba5ea24SCraig Rodrigues __icheckhost(const struct sockaddr *raddr, socklen_t salen, const char *lhost)
68758f0484fSRodney W. Grimes {
688eb74b996SHajimu UMEMOTO 	struct sockaddr_in sin;
689eb74b996SHajimu UMEMOTO 	struct sockaddr_in6 *sin6;
690eb74b996SHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
691eb74b996SHajimu UMEMOTO 	int error;
692eb74b996SHajimu UMEMOTO 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
69358f0484fSRodney W. Grimes 
694eb74b996SHajimu UMEMOTO 	if (raddr->sa_family == AF_INET6) {
695eb74b996SHajimu UMEMOTO 		sin6 = (struct sockaddr_in6 *)raddr;
696eb74b996SHajimu UMEMOTO 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
697eb74b996SHajimu UMEMOTO 			memset(&sin, 0, sizeof(sin));
698eb74b996SHajimu UMEMOTO 			sin.sin_family = AF_INET;
699eb74b996SHajimu UMEMOTO 			sin.sin_len = sizeof(struct sockaddr_in);
700eb74b996SHajimu UMEMOTO 			memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
701eb74b996SHajimu UMEMOTO 			       sizeof(sin.sin_addr));
702eb74b996SHajimu UMEMOTO 			raddr = (struct sockaddr *)&sin;
703eb74b996SHajimu UMEMOTO 			salen = sin.sin_len;
704eb74b996SHajimu UMEMOTO 		}
705eb74b996SHajimu UMEMOTO 	}
706eb74b996SHajimu UMEMOTO 
707eb74b996SHajimu UMEMOTO 	h1[0] = '\0';
708eb74b996SHajimu UMEMOTO 	if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
7094f101318SHajimu UMEMOTO 			NI_NUMERICHOST) != 0)
710eb74b996SHajimu UMEMOTO 		return (0);
711eb74b996SHajimu UMEMOTO 
712eb74b996SHajimu UMEMOTO 	/* Resolve laddr into sockaddr */
713eb74b996SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
714eb74b996SHajimu UMEMOTO 	hints.ai_family = raddr->sa_family;
715eb74b996SHajimu UMEMOTO 	hints.ai_socktype = SOCK_DGRAM;	/*XXX dummy*/
716eb74b996SHajimu UMEMOTO 	res = NULL;
717eb74b996SHajimu UMEMOTO 	error = getaddrinfo(lhost, "0", &hints, &res);
718eb74b996SHajimu UMEMOTO 	if (error)
719eb74b996SHajimu UMEMOTO 		return (0);
720eb74b996SHajimu UMEMOTO 
721eb74b996SHajimu UMEMOTO 	for (r = res; r ; r = r->ai_next) {
722eb74b996SHajimu UMEMOTO 		h2[0] = '\0';
723eb74b996SHajimu UMEMOTO 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
7244f101318SHajimu UMEMOTO 				NULL, 0, NI_NUMERICHOST) != 0)
725eb74b996SHajimu UMEMOTO 			continue;
726eb74b996SHajimu UMEMOTO 		if (strcmp(h1, h2) == 0) {
727eb74b996SHajimu UMEMOTO 			freeaddrinfo(res);
72842b4f28eSYoshinobu Inoue 			return (1);
729eb74b996SHajimu UMEMOTO 		}
73042b4f28eSYoshinobu Inoue 	}
73158f0484fSRodney W. Grimes 
732eb74b996SHajimu UMEMOTO 	/* No match. */
733eb74b996SHajimu UMEMOTO 	freeaddrinfo(res);
73458f0484fSRodney W. Grimes 	return (0);
73558f0484fSRodney W. Grimes }
736