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