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