xref: /freebsd/lib/libc/net/rcmd.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 
40 #include <signal.h>
41 #include <fcntl.h>
42 #include <netdb.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <pwd.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <ctype.h>
49 #include <string.h>
50 #include <rpc/rpc.h>
51 #ifdef YP
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
54 #endif
55 #include <arpa/nameser.h>
56 #include "un-namespace.h"
57 #include "libc_private.h"
58 
59 extern int innetgr( const char *, const char *, const char *, const char * );
60 
61 #define max(a, b)	((a > b) ? a : b)
62 
63 int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
64 int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
65 int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
66     const char *);
67 static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
68 
69 char paddr[NI_MAXHOST];
70 
71 int
72 rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
73     const char *cmd, int *fd2p)
74 {
75 	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
76 }
77 
78 int
79 rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser,
80     const char *cmd, int *fd2p, int af)
81 {
82 	struct addrinfo hints, *res, *ai;
83 	struct sockaddr_storage from;
84 	fd_set reads;
85 	sigset_t oldmask, newmask;
86 	pid_t pid;
87 	int s, aport, lport, timo, error;
88 	char c, *p;
89 	int refused, nres;
90 	char num[8];
91 	static char canonnamebuf[MAXDNAME];	/* is it proper here? */
92 
93 	/* call rcmdsh() with specified remote shell if appropriate. */
94 	if ((p = secure_getenv("RSH")) != NULL) {
95 		struct servent *sp = getservbyname("shell", "tcp");
96 
97 		if (sp && sp->s_port == rport)
98 			return (rcmdsh(ahost, rport, locuser, remuser,
99 			    cmd, p));
100 	}
101 
102 	/* use rsh(1) if non-root and remote port is shell. */
103 	if (geteuid()) {
104 		struct servent *sp = getservbyname("shell", "tcp");
105 
106 		if (sp && sp->s_port == rport)
107 			return (rcmdsh(ahost, rport, locuser, remuser,
108 			    cmd, NULL));
109 	}
110 
111 	pid = getpid();
112 
113 	memset(&hints, 0, sizeof(hints));
114 	hints.ai_flags = AI_CANONNAME;
115 	hints.ai_family = af;
116 	hints.ai_socktype = SOCK_STREAM;
117 	hints.ai_protocol = 0;
118 	(void)snprintf(num, sizeof(num), "%d", ntohs(rport));
119 	error = getaddrinfo(*ahost, num, &hints, &res);
120 	if (error) {
121 		fprintf(stderr, "rcmd: getaddrinfo: %s\n",
122 			gai_strerror(error));
123 		if (error == EAI_SYSTEM)
124 			fprintf(stderr, "rcmd: getaddrinfo: %s\n",
125 				strerror(errno));
126 		return (-1);
127 	}
128 
129 	if (res->ai_canonname
130 	 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
131 		strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
132 		*ahost = canonnamebuf;
133 	}
134 	nres = 0;
135 	for (ai = res; ai; ai = ai->ai_next)
136 		nres++;
137 	ai = res;
138 	refused = 0;
139 	sigemptyset(&newmask);
140 	sigaddset(&newmask, SIGURG);
141 	__libc_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
142 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
143 		s = rresvport_af(&lport, ai->ai_family);
144 		if (s < 0) {
145 			if (errno != EAGAIN && ai->ai_next) {
146 				ai = ai->ai_next;
147 				continue;
148 			}
149 			if (errno == EAGAIN)
150 				(void)fprintf(stderr,
151 				    "rcmd: socket: All ports in use\n");
152 			else
153 				(void)fprintf(stderr, "rcmd: socket: %s\n",
154 				    strerror(errno));
155 			freeaddrinfo(res);
156 			__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
157 			    NULL);
158 			return (-1);
159 		}
160 		_fcntl(s, F_SETOWN, pid);
161 		if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
162 			break;
163 		(void)_close(s);
164 		if (errno == EADDRINUSE) {
165 			lport--;
166 			continue;
167 		}
168 		if (errno == ECONNREFUSED)
169 			refused = 1;
170 		if (ai->ai_next == NULL && (!refused || timo > 16)) {
171 			(void)fprintf(stderr, "%s: %s\n",
172 				      *ahost, strerror(errno));
173 			freeaddrinfo(res);
174 			__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
175 			    NULL);
176 			return (-1);
177 		}
178 		if (nres > 1) {
179 			int oerrno = errno;
180 
181 			getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
182 			    sizeof(paddr), NULL, 0, NI_NUMERICHOST);
183 			(void)fprintf(stderr, "connect to address %s: ",
184 				      paddr);
185 			errno = oerrno;
186 			perror(0);
187 		}
188 		if ((ai = ai->ai_next) == NULL) {
189 			/* refused && timo <= 16 */
190 			struct timespec time_to_sleep, time_remaining;
191 
192 			time_to_sleep.tv_sec = timo;
193 			time_to_sleep.tv_nsec = 0;
194 			(void)_nanosleep(&time_to_sleep, &time_remaining);
195 			timo *= 2;
196 			ai = res;
197 			refused = 0;
198 		}
199 		if (nres > 1) {
200 			getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
201 			    sizeof(paddr), NULL, 0, NI_NUMERICHOST);
202 			fprintf(stderr, "Trying %s...\n", paddr);
203 		}
204 	}
205 	lport--;
206 	if (fd2p == NULL) {
207 		_write(s, "", 1);
208 		lport = 0;
209 	} else {
210 		int s2 = rresvport_af(&lport, ai->ai_family), s3;
211 		socklen_t len = ai->ai_addrlen;
212 		int nfds;
213 
214 		if (s2 < 0)
215 			goto bad;
216 		_listen(s2, 1);
217 		(void)snprintf(num, sizeof(num), "%d", lport);
218 		if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
219 			(void)fprintf(stderr,
220 			    "rcmd: write (setting up stderr): %s\n",
221 			    strerror(errno));
222 			(void)_close(s2);
223 			goto bad;
224 		}
225 		nfds = max(s, s2)+1;
226 		if(nfds > FD_SETSIZE) {
227 			fprintf(stderr, "rcmd: too many files\n");
228 			(void)_close(s2);
229 			goto bad;
230 		}
231 again:
232 		FD_ZERO(&reads);
233 		FD_SET(s, &reads);
234 		FD_SET(s2, &reads);
235 		errno = 0;
236 		if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
237 			if (errno != 0)
238 				(void)fprintf(stderr,
239 				    "rcmd: select (setting up stderr): %s\n",
240 				    strerror(errno));
241 			else
242 				(void)fprintf(stderr,
243 				"select: protocol failure in circuit setup\n");
244 			(void)_close(s2);
245 			goto bad;
246 		}
247 		s3 = _accept(s2, (struct sockaddr *)&from, &len);
248 		switch (from.ss_family) {
249 		case AF_INET:
250 			aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
251 			break;
252 #ifdef INET6
253 		case AF_INET6:
254 			aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
255 			break;
256 #endif
257 		default:
258 			aport = 0;	/* error */
259 			break;
260 		}
261 		/*
262 		 * XXX careful for ftp bounce attacks. If discovered, shut them
263 		 * down and check for the real auxiliary channel to connect.
264 		 */
265 		if (aport == 20) {
266 			_close(s3);
267 			goto again;
268 		}
269 		(void)_close(s2);
270 		if (s3 < 0) {
271 			(void)fprintf(stderr,
272 			    "rcmd: accept: %s\n", strerror(errno));
273 			lport = 0;
274 			goto bad;
275 		}
276 		*fd2p = s3;
277 		if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
278 			(void)fprintf(stderr,
279 			    "socket: protocol failure in circuit setup.\n");
280 			goto bad2;
281 		}
282 	}
283 	(void)_write(s, locuser, strlen(locuser)+1);
284 	(void)_write(s, remuser, strlen(remuser)+1);
285 	(void)_write(s, cmd, strlen(cmd)+1);
286 	if (_read(s, &c, 1) != 1) {
287 		(void)fprintf(stderr,
288 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
289 		goto bad2;
290 	}
291 	if (c != 0) {
292 		while (_read(s, &c, 1) == 1) {
293 			(void)_write(STDERR_FILENO, &c, 1);
294 			if (c == '\n')
295 				break;
296 		}
297 		goto bad2;
298 	}
299 	__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
300 	freeaddrinfo(res);
301 	return (s);
302 bad2:
303 	if (lport)
304 		(void)_close(*fd2p);
305 bad:
306 	(void)_close(s);
307 	__libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
308 	freeaddrinfo(res);
309 	return (-1);
310 }
311 
312 int
313 rresvport(int *port)
314 {
315 	return rresvport_af(port, AF_INET);
316 }
317 
318 int
319 rresvport_af(int *alport, int family)
320 {
321 	int s;
322 	struct sockaddr_storage ss;
323 	u_short *sport;
324 
325 	memset(&ss, 0, sizeof(ss));
326 	ss.ss_family = family;
327 	switch (family) {
328 	case AF_INET:
329 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
330 		sport = &((struct sockaddr_in *)&ss)->sin_port;
331 		((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
332 		break;
333 #ifdef INET6
334 	case AF_INET6:
335 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
336 		sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
337 		((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
338 		break;
339 #endif
340 	default:
341 		errno = EAFNOSUPPORT;
342 		return -1;
343 	}
344 
345 	s = _socket(ss.ss_family, SOCK_STREAM, 0);
346 	if (s < 0)
347 		return (-1);
348 #if 0 /* compat_exact_traditional_rresvport_semantics */
349 	sin.sin_port = htons((u_short)*alport);
350 	if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
351 		return (s);
352 	if (errno != EADDRINUSE) {
353 		(void)_close(s);
354 		return (-1);
355 	}
356 #endif
357 	*sport = 0;
358 	if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
359 		(void)_close(s);
360 		return (-1);
361 	}
362 	*alport = (int)ntohs(*sport);
363 	return (s);
364 }
365 
366 int	__check_rhosts_file = 1;
367 char	*__rcmd_errstr;
368 
369 int
370 ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
371 {
372 	struct addrinfo hints, *res, *r;
373 	int error;
374 
375 	memset(&hints, 0, sizeof(hints));
376 	hints.ai_family = PF_UNSPEC;
377 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
378 	error = getaddrinfo(rhost, "0", &hints, &res);
379 	if (error)
380 		return (-1);
381 
382 	for (r = res; r; r = r->ai_next) {
383 		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
384 		    luser) == 0) {
385 			freeaddrinfo(res);
386 			return (0);
387 		}
388 	}
389 	freeaddrinfo(res);
390 	return (-1);
391 }
392 
393 /*
394  * New .rhosts strategy: We are passed an ip address. We spin through
395  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
396  * has ip addresses, we don't have to trust a nameserver.  When it
397  * contains hostnames, we spin through the list of addresses the nameserver
398  * gives us and look for a match.
399  *
400  * Returns 0 if ok, -1 if not ok.
401  */
402 int
403 iruserok(unsigned long raddr, int superuser, const char *ruser, const char *luser)
404 {
405 	struct sockaddr_in sin;
406 
407 	memset(&sin, 0, sizeof(sin));
408 	sin.sin_family = AF_INET;
409 	sin.sin_len = sizeof(struct sockaddr_in);
410 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
411 	return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
412 		ruser, luser);
413 }
414 
415 /*
416  * AF independent extension of iruserok.
417  *
418  * Returns 0 if ok, -1 if not ok.
419  */
420 int
421 iruserok_sa(const void *ra, int rlen, int superuser, const char *ruser,
422     const char *luser)
423 {
424 	char *cp;
425 	struct stat sbuf;
426 	struct passwd *pwd;
427 	FILE *hostf;
428 	uid_t uid;
429 	int first;
430 	char pbuf[MAXPATHLEN];
431 	const struct sockaddr *raddr;
432 	struct sockaddr_storage ss;
433 
434 	/* avoid alignment issue */
435 	if (rlen <= 0 || rlen > sizeof(ss))
436 		return (-1);
437 	memcpy(&ss, ra, rlen);
438 	raddr = (struct sockaddr *)&ss;
439 
440 	first = 1;
441 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re");
442 again:
443 	if (hostf) {
444 		if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
445 			(void)fclose(hostf);
446 			return (0);
447 		}
448 		(void)fclose(hostf);
449 	}
450 	if (first == 1 && (__check_rhosts_file || superuser)) {
451 		first = 0;
452 		if ((pwd = getpwnam(luser)) == NULL)
453 			return (-1);
454 		(void)strlcpy(pbuf, pwd->pw_dir, sizeof(pbuf));
455 		(void)strlcat(pbuf, "/.rhosts", sizeof(pbuf));
456 
457 		/*
458 		 * Change effective uid while opening .rhosts.  If root and
459 		 * reading an NFS mounted file system, can't read files that
460 		 * are protected read/write owner only.
461 		 */
462 		uid = geteuid();
463 		(void)seteuid(pwd->pw_uid);
464 		hostf = fopen(pbuf, "re");
465 		(void)seteuid(uid);
466 
467 		if (hostf == NULL)
468 			return (-1);
469 		/*
470 		 * If not a regular file, or is owned by someone other than
471 		 * user or root or if writeable by anyone but the owner, quit.
472 		 */
473 		cp = NULL;
474 		if (lstat(pbuf, &sbuf) < 0)
475 			cp = ".rhosts lstat failed";
476 		else if (!S_ISREG(sbuf.st_mode))
477 			cp = ".rhosts not regular file";
478 		else if (_fstat(fileno(hostf), &sbuf) < 0)
479 			cp = ".rhosts fstat failed";
480 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
481 			cp = "bad .rhosts owner";
482 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
483 			cp = ".rhosts writeable by other than owner";
484 		/* If there were any problems, quit. */
485 		if (cp) {
486 			__rcmd_errstr = cp;
487 			(void)fclose(hostf);
488 			return (-1);
489 		}
490 		goto again;
491 	}
492 	return (-1);
493 }
494 
495 /*
496  * XXX
497  * Don't make static, used by lpd(8).
498  *
499  * Returns 0 if ok, -1 if not ok.
500  */
501 int
502 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
503 {
504 	struct sockaddr_in sin;
505 
506 	memset(&sin, 0, sizeof(sin));
507 	sin.sin_family = AF_INET;
508 	sin.sin_len = sizeof(struct sockaddr_in);
509 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
510 	return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
511 		luser, ruser);
512 }
513 
514 /*
515  * Returns 0 if ok, -1 if not ok.
516  *
517  * XXX obsolete API.
518  */
519 int
520 __ivaliduser_af(FILE *hostf, const void *raddr, const char *luser,
521     const char *ruser, int af, int len)
522 {
523 	struct sockaddr *sa = NULL;
524 	struct sockaddr_in *sin = NULL;
525 #ifdef INET6
526 	struct sockaddr_in6 *sin6 = NULL;
527 #endif
528 	struct sockaddr_storage ss;
529 
530 	memset(&ss, 0, sizeof(ss));
531 	switch (af) {
532 	case AF_INET:
533 		if (len != sizeof(sin->sin_addr))
534 			return -1;
535 		sin = (struct sockaddr_in *)&ss;
536 		sin->sin_family = AF_INET;
537 		sin->sin_len = sizeof(struct sockaddr_in);
538 		memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
539 		break;
540 #ifdef INET6
541 	case AF_INET6:
542 		if (len != sizeof(sin6->sin6_addr))
543 			return -1;
544 		/* you will lose scope info */
545 		sin6 = (struct sockaddr_in6 *)&ss;
546 		sin6->sin6_family = AF_INET6;
547 		sin6->sin6_len = sizeof(struct sockaddr_in6);
548 		memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
549 		break;
550 #endif
551 	default:
552 		return -1;
553 	}
554 
555 	sa = (struct sockaddr *)&ss;
556 	return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
557 }
558 
559 int
560 __ivaliduser_sa(FILE *hostf, const struct sockaddr *raddr, socklen_t salen,
561     const char *luser, const char *ruser)
562 {
563 	char *user, *p;
564 	int ch;
565 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
566 	char hname[MAXHOSTNAMELEN];
567 	/* Presumed guilty until proven innocent. */
568 	int userok = 0, hostok = 0;
569 #ifdef YP
570 	char *ypdomain;
571 
572 	if (yp_get_default_domain(&ypdomain))
573 		ypdomain = NULL;
574 #else
575 #define	ypdomain NULL
576 #endif
577 	/* We need to get the damn hostname back for netgroup matching. */
578 	if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
579 			NI_NAMEREQD) != 0)
580 		hname[0] = '\0';
581 
582 	while (fgets(buf, sizeof(buf), hostf)) {
583 		p = buf;
584 		/* Skip lines that are too long. */
585 		if (strchr(p, '\n') == NULL) {
586 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
587 			continue;
588 		}
589 		if (*p == '\n' || *p == '#') {
590 			/* comment... */
591 			continue;
592 		}
593 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
594 			*p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
595 			p++;
596 		}
597 		if (*p == ' ' || *p == '\t') {
598 			*p++ = '\0';
599 			while (*p == ' ' || *p == '\t')
600 				p++;
601 			user = p;
602 			while (*p != '\n' && *p != ' ' &&
603 			    *p != '\t' && *p != '\0')
604 				p++;
605 		} else
606 			user = p;
607 		*p = '\0';
608 		/*
609 		 * Do +/- and +@/-@ checking. This looks really nasty,
610 		 * but it matches SunOS's behavior so far as I can tell.
611 		 */
612 		switch(buf[0]) {
613 		case '+':
614 			if (!buf[1]) {     /* '+' matches all hosts */
615 				hostok = 1;
616 				break;
617 			}
618 			if (buf[1] == '@')  /* match a host by netgroup */
619 				hostok = hname[0] != '\0' &&
620 				    innetgr(&buf[2], hname, NULL, ypdomain);
621 			else		/* match a host by addr */
622 				hostok = __icheckhost(raddr, salen,
623 						      (char *)&buf[1]);
624 			break;
625 		case '-':     /* reject '-' hosts and all their users */
626 			if (buf[1] == '@') {
627 				if (hname[0] == '\0' ||
628 				    innetgr(&buf[2], hname, NULL, ypdomain))
629 					return(-1);
630 			} else {
631 				if (__icheckhost(raddr, salen,
632 						 (char *)&buf[1]))
633 					return(-1);
634 			}
635 			break;
636 		default:  /* if no '+' or '-', do a simple match */
637 			hostok = __icheckhost(raddr, salen, buf);
638 			break;
639 		}
640 		switch(*user) {
641 		case '+':
642 			if (!*(user+1)) {      /* '+' matches all users */
643 				userok = 1;
644 				break;
645 			}
646 			if (*(user+1) == '@')  /* match a user by netgroup */
647 				userok = innetgr(user+2, NULL, ruser, ypdomain);
648 			else	   /* match a user by direct specification */
649 				userok = !(strcmp(ruser, user+1));
650 			break;
651 		case '-': 		/* if we matched a hostname, */
652 			if (hostok) {   /* check for user field rejections */
653 				if (!*(user+1))
654 					return(-1);
655 				if (*(user+1) == '@') {
656 					if (innetgr(user+2, NULL,
657 							ruser, ypdomain))
658 						return(-1);
659 				} else {
660 					if (!strcmp(ruser, user+1))
661 						return(-1);
662 				}
663 			}
664 			break;
665 		default:	/* no rejections: try to match the user */
666 			if (hostok)
667 				userok = !(strcmp(ruser,*user ? user : luser));
668 			break;
669 		}
670 		if (hostok && userok)
671 			return(0);
672 	}
673 	return (-1);
674 }
675 
676 /*
677  * Returns "true" if match, 0 if no match.
678  */
679 static int
680 __icheckhost(const struct sockaddr *raddr, socklen_t salen, const char *lhost)
681 {
682 	struct sockaddr_in sin;
683 	struct sockaddr_in6 *sin6;
684 	struct addrinfo hints, *res, *r;
685 	int error;
686 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
687 
688 	if (raddr->sa_family == AF_INET6) {
689 		sin6 = (struct sockaddr_in6 *)raddr;
690 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
691 			memset(&sin, 0, sizeof(sin));
692 			sin.sin_family = AF_INET;
693 			sin.sin_len = sizeof(struct sockaddr_in);
694 			memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
695 			       sizeof(sin.sin_addr));
696 			raddr = (struct sockaddr *)&sin;
697 			salen = sin.sin_len;
698 		}
699 	}
700 
701 	h1[0] = '\0';
702 	if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
703 			NI_NUMERICHOST) != 0)
704 		return (0);
705 
706 	/* Resolve laddr into sockaddr */
707 	memset(&hints, 0, sizeof(hints));
708 	hints.ai_family = raddr->sa_family;
709 	hints.ai_socktype = SOCK_DGRAM;	/*XXX dummy*/
710 	res = NULL;
711 	error = getaddrinfo(lhost, "0", &hints, &res);
712 	if (error)
713 		return (0);
714 
715 	for (r = res; r ; r = r->ai_next) {
716 		h2[0] = '\0';
717 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
718 				NULL, 0, NI_NUMERICHOST) != 0)
719 			continue;
720 		if (strcmp(h1, h2) == 0) {
721 			freeaddrinfo(res);
722 			return (1);
723 		}
724 	}
725 
726 	/* No match. */
727 	freeaddrinfo(res);
728 	return (0);
729 }
730