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