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