xref: /dragonfly/lib/libc/net/rcmd.c (revision 9f3fc534)
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  * @(#)rcmd.c	8.3 (Berkeley) 3/26/94
30  * $FreeBSD: src/lib/libc/net/rcmd.c,v 1.42 2007/01/09 00:28:02 imp Exp $
31  * $DragonFly: src/lib/libc/net/rcmd.c,v 1.7 2005/11/13 02:04:47 swildner Exp $
32  */
33 
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 
42 #include <signal.h>
43 #include <fcntl.h>
44 #include <netdb.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <ctype.h>
51 #include <string.h>
52 #include <rpc/rpc.h>
53 #ifdef YP
54 #include <rpcsvc/yp_prot.h>
55 #include <rpcsvc/ypclnt.h>
56 #endif
57 #include <arpa/nameser.h>
58 #include "un-namespace.h"
59 
60 extern int innetgr(const char *, const char *, const char *, const char *);
61 
62 #define max(a, b)	((a > b) ? a : b)
63 
64 int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
65 int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
66 int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
67 		    const char *);
68 static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
69 
70 char paddr[NI_MAXHOST];
71 
72 int
73 rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
74      const char *cmd, int *fd2p)
75 {
76 	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
77 }
78 
79 int
80 rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser,
81 	const char *cmd, int *fd2p, int af)
82 {
83 	struct addrinfo hints, *res, *ai;
84 	struct sockaddr_storage from;
85 	fd_set reads;
86 	sigset_t oldmask, newmask;
87 	pid_t pid;
88 	int s, aport, lport, timo, error;
89 	char c, *p;
90 	int refused, nres;
91 	char num[8];
92 	static char canonnamebuf[MAXDNAME];	/* is it proper here? */
93 
94 	/* call rcmdsh() with specified remote shell if appropriate. */
95 	if (!issetugid() && (p = getenv("RSH"))) {
96 		struct servent *sp = getservbyname("shell", "tcp");
97 
98 		if (sp && sp->s_port == rport)
99 			return (rcmdsh(ahost, rport, locuser, remuser,
100 			    cmd, p));
101 	}
102 
103 	/* use rsh(1) if non-root and remote port is shell. */
104 	if (geteuid()) {
105 		struct servent *sp = getservbyname("shell", "tcp");
106 
107 		if (sp && sp->s_port == rport)
108 			return (rcmdsh(ahost, rport, locuser, remuser,
109 			    cmd, NULL));
110 	}
111 
112 	pid = getpid();
113 
114 	memset(&hints, 0, sizeof(hints));
115 	hints.ai_flags = AI_CANONNAME;
116 	hints.ai_family = af;
117 	hints.ai_socktype = SOCK_STREAM;
118 	hints.ai_protocol = 0;
119 	snprintf(num, sizeof(num), "%d", ntohs(rport));
120 	error = getaddrinfo(*ahost, num, &hints, &res);
121 	if (error) {
122 		fprintf(stderr, "rcmd: getaddrinfo: %s\n",
123 			gai_strerror(error));
124 		if (error == EAI_SYSTEM)
125 			fprintf(stderr, "rcmd: getaddrinfo: %s\n",
126 				strerror(errno));
127 		return (-1);
128 	}
129 
130 	if (res->ai_canonname &&
131 	    strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
132 		strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
133 		*ahost = canonnamebuf;
134 	}
135 	nres = 0;
136 	for (ai = res; ai; ai = ai->ai_next)
137 		nres++;
138 	ai = res;
139 	refused = 0;
140 	sigemptyset(&newmask);
141 	sigaddset(&newmask, SIGURG);
142 	_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
143 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
144 		s = rresvport_af(&lport, ai->ai_family);
145 		if (s < 0) {
146 			if (errno != EAGAIN && ai->ai_next) {
147 				ai = ai->ai_next;
148 				continue;
149 			}
150 			if (errno == EAGAIN)
151 				fprintf(stderr,
152 				    "rcmd: socket: All ports in use\n");
153 			else
154 				fprintf(stderr, "rcmd: socket: %s\n",
155 				    strerror(errno));
156 			freeaddrinfo(res);
157 			_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
158 			    NULL);
159 			return (-1);
160 		}
161 		_fcntl(s, F_SETOWN, pid);
162 		if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
163 			break;
164 		_close(s);
165 		if (errno == EADDRINUSE) {
166 			lport--;
167 			continue;
168 		}
169 		if (errno == ECONNREFUSED)
170 			refused = 1;
171 		if (ai->ai_next == NULL && (!refused || timo > 16)) {
172 			fprintf(stderr, "%s: %s\n", *ahost, strerror(errno));
173 			freeaddrinfo(res);
174 			_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 			fprintf(stderr, "connect to address %s: ", paddr);
184 			errno = oerrno;
185 			perror(0);
186 		}
187 		if ((ai = ai->ai_next) == NULL) {
188 			/* refused && timo <= 16 */
189 			struct timespec time_to_sleep, time_remaining;
190 
191 			time_to_sleep.tv_sec = timo;
192 			time_to_sleep.tv_nsec = 0;
193 			_nanosleep(&time_to_sleep, &time_remaining);
194 			timo *= 2;
195 			ai = res;
196 			refused = 0;
197 		}
198 		if (nres > 1) {
199 			getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
200 			    sizeof(paddr), NULL, 0, NI_NUMERICHOST);
201 			fprintf(stderr, "Trying %s...\n", paddr);
202 		}
203 	}
204 	lport--;
205 	if (fd2p == 0) {
206 		_write(s, "", 1);
207 		lport = 0;
208 	} else {
209 		int s2 = rresvport_af(&lport, ai->ai_family), s3;
210 		socklen_t len = ai->ai_addrlen;
211 		int nfds;
212 
213 		if (s2 < 0)
214 			goto bad;
215 		_listen(s2, 1);
216 		snprintf(num, sizeof(num), "%d", lport);
217 		if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
218 			fprintf(stderr,
219 			    "rcmd: write (setting up stderr): %s\n",
220 			    strerror(errno));
221 			_close(s2);
222 			goto bad;
223 		}
224 		nfds = max(s, s2)+1;
225 		if(nfds > FD_SETSIZE) {
226 			fprintf(stderr, "rcmd: too many files\n");
227 			_close(s2);
228 			goto bad;
229 		}
230 again:
231 		FD_ZERO(&reads);
232 		FD_SET(s, &reads);
233 		FD_SET(s2, &reads);
234 		errno = 0;
235 		if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
236 			if (errno != 0)
237 				fprintf(stderr,
238 				    "rcmd: select (setting up stderr): %s\n",
239 				    strerror(errno));
240 			else
241 				fprintf(stderr,
242 				"select: protocol failure in circuit setup\n");
243 			_close(s2);
244 			goto bad;
245 		}
246 		s3 = _accept(s2, (struct sockaddr *)&from, &len);
247 		switch (from.ss_family) {
248 		case AF_INET:
249 			aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
250 			break;
251 #ifdef INET6
252 		case AF_INET6:
253 			aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
254 			break;
255 #endif
256 		default:
257 			aport = 0;	/* error */
258 			break;
259 		}
260 		/*
261 		 * XXX careful for ftp bounce attacks. If discovered, shut them
262 		 * down and check for the real auxiliary channel to connect.
263 		 */
264 		if (aport == 20) {
265 			_close(s3);
266 			goto again;
267 		}
268 		_close(s2);
269 		if (s3 < 0) {
270 			fprintf(stderr,
271 			    "rcmd: accept: %s\n", strerror(errno));
272 			lport = 0;
273 			goto bad;
274 		}
275 		*fd2p = s3;
276 		if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
277 			fprintf(stderr,
278 			    "socket: protocol failure in circuit setup.\n");
279 			goto bad2;
280 		}
281 	}
282 	_write(s, locuser, strlen(locuser)+1);
283 	_write(s, remuser, strlen(remuser)+1);
284 	_write(s, cmd, strlen(cmd)+1);
285 	if (_read(s, &c, 1) != 1) {
286 		fprintf(stderr,
287 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
288 		goto bad2;
289 	}
290 	if (c != 0) {
291 		while (_read(s, &c, 1) == 1) {
292 			_write(STDERR_FILENO, &c, 1);
293 			if (c == '\n')
294 				break;
295 		}
296 		goto bad2;
297 	}
298 	_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
299 	freeaddrinfo(res);
300 	return (s);
301 bad2:
302 	if (lport)
303 		_close(*fd2p);
304 bad:
305 	_close(s);
306 	_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
307 	freeaddrinfo(res);
308 	return (-1);
309 }
310 
311 int
312 rresvport(int *port)
313 {
314 	return rresvport_af(port, AF_INET);
315 }
316 
317 int
318 rresvport_af(int *alport, int family)
319 {
320 	int s;
321 	struct sockaddr_storage ss;
322 	u_short *sport;
323 
324 	memset(&ss, 0, sizeof(ss));
325 	ss.ss_family = family;
326 	switch (family) {
327 	case AF_INET:
328 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
329 		sport = &((struct sockaddr_in *)&ss)->sin_port;
330 		((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
331 		break;
332 #ifdef INET6
333 	case AF_INET6:
334 		((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
335 		sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
336 		((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
337 		break;
338 #endif
339 	default:
340 		errno = EAFNOSUPPORT;
341 		return -1;
342 	}
343 
344 	s = _socket(ss.ss_family, SOCK_STREAM, 0);
345 	if (s < 0)
346 		return (-1);
347 #if 0 /* compat_exact_traditional_rresvport_semantics */
348 	sin.sin_port = htons((u_short)*alport);
349 	if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
350 		return (s);
351 	if (errno != EADDRINUSE) {
352 		_close(s);
353 		return (-1);
354 	}
355 #endif
356 	*sport = 0;
357 	if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
358 		_close(s);
359 		return (-1);
360 	}
361 	*alport = (int)ntohs(*sport);
362 	return (s);
363 }
364 
365 int	__check_rhosts_file = 1;
366 char	*__rcmd_errstr;
367 
368 int
369 ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
370 {
371 	struct addrinfo hints, *res, *r;
372 	int error;
373 
374 	memset(&hints, 0, sizeof(hints));
375 	hints.ai_family = PF_UNSPEC;
376 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
377 	error = getaddrinfo(rhost, "0", &hints, &res);
378 	if (error)
379 		return (-1);
380 
381 	for (r = res; r; r = r->ai_next) {
382 		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
383 		    luser) == 0) {
384 			freeaddrinfo(res);
385 			return (0);
386 		}
387 	}
388 	freeaddrinfo(res);
389 	return (-1);
390 }
391 
392 /*
393  * New .rhosts strategy: We are passed an ip address. We spin through
394  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
395  * has ip addresses, we don't have to trust a nameserver.  When it
396  * contains hostnames, we spin through the list of addresses the nameserver
397  * gives us and look for a match.
398  *
399  * Returns 0 if ok, -1 if not ok.
400  */
401 int
402 iruserok(unsigned long raddr, int superuser, const char *ruser,
403 	 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 > 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, "r");
442 again:
443 	if (hostf) {
444 		if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
445 			fclose(hostf);
446 			return (0);
447 		}
448 		fclose(hostf);
449 	}
450 	if (first == 1 && (__check_rhosts_file || superuser)) {
451 		first = 0;
452 		if ((pwd = getpwnam(luser)) == NULL)
453 			return (-1);
454 		strcpy(pbuf, pwd->pw_dir);
455 		strcat(pbuf, "/.rhosts");
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 		seteuid(pwd->pw_uid);
464 		hostf = fopen(pbuf, "r");
465 		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 			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