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