Libinfo-517.200.9.tar.gz
[apple/libinfo.git] / util.subproj / rcmd.c
1 /*
2 * Copyright (c) 1999-2018 Apple 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 "libinfo_common.h"
66
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <sys/stat.h>
70
71 #include <arpa/inet.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 #include <time.h>
83 #ifdef YP
84 #include <rpc/rpc.h>
85 #include <rpcsvc/yp_prot.h>
86 #include <rpcsvc/ypclnt.h>
87 #endif
88
89 #include <arpa/nameser_compat.h>
90 #include <nameser.h>
91
92 /* wrapper for KAME-special getnameinfo() */
93 #ifndef NI_WITHSCOPEID
94 #define NI_WITHSCOPEID 0
95 #endif
96
97 #ifndef socklen_t
98 #define socklen_t int
99 #endif
100
101 extern int innetgr __P(( const char *, const char *, const char *, const char * ));
102 extern int bindresvport_sa(int, struct sockaddr *);
103
104 #define max(a, b) ((a > b) ? a : b)
105
106 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
107 int __ivaliduser_af __P((FILE *,const void *, const char *, const char *,
108 int, int));
109 int __ivaliduser_sa __P((FILE *, const struct sockaddr *, socklen_t,
110 const char *,const char *));
111 static int __icheckhost __P((const struct sockaddr *, socklen_t,
112 const char *));
113
114 LIBINFO_EXPORT
115 int
116 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
117 char **ahost;
118 u_short rport;
119 const char *locuser, *remuser, *cmd;
120 int *fd2p;
121 int af;
122 {
123 struct addrinfo hints, *res, *ai;
124 struct sockaddr_storage from;
125 fd_set reads;
126 long oldmask;
127 pid_t pid;
128 int s, aport, lport, timo, error;
129 char c;
130 int refused, nres;
131 char num[8], paddr[NI_MAXHOST];
132 static char canonnamebuf[MAXDNAME]; /* is it proper here? */
133
134 pid = getpid();
135
136 memset(&hints, 0, sizeof(hints));
137 hints.ai_flags = AI_CANONNAME;
138 hints.ai_family = af;
139 hints.ai_socktype = SOCK_STREAM;
140 hints.ai_protocol = 0;
141 (void)snprintf(num, sizeof(num), "%d", ntohs(rport));
142 error = getaddrinfo(*ahost, num, &hints, &res);
143 if (error) {
144 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
145 gai_strerror(error));
146 if (error == EAI_SYSTEM)
147 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
148 strerror(errno));
149 return (-1);
150 }
151
152 if (res->ai_canonname
153 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
154 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
155 *ahost = canonnamebuf;
156 }
157 nres = 0;
158 for (ai = res; ai; ai = ai->ai_next)
159 nres++;
160 ai = res;
161 refused = 0;
162 oldmask = sigblock(sigmask(SIGURG));
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 sigsetmask(oldmask);
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 sigsetmask(oldmask);
195 return (-1);
196 }
197 if (nres > 1) {
198 int oerrno = errno;
199
200 getnameinfo(ai->ai_addr, ai->ai_addrlen,
201 paddr, sizeof(paddr),
202 NULL, 0,
203 NI_NUMERICHOST|NI_WITHSCOPEID);
204 (void)fprintf(stderr, "connect to address %s: ",
205 paddr);
206 errno = oerrno;
207 perror(0);
208 }
209 if ((ai = ai->ai_next) == NULL) {
210 /* refused && timo <= 16 */
211 struct timespec time_to_sleep, time_remaining;
212
213 time_to_sleep.tv_sec = timo;
214 time_to_sleep.tv_nsec = 0;
215 (void)nanosleep(&time_to_sleep, &time_remaining);
216 timo *= 2;
217 ai = res;
218 refused = 0;
219 }
220 if (nres > 1) {
221 getnameinfo(ai->ai_addr, ai->ai_addrlen,
222 paddr, sizeof(paddr),
223 NULL, 0,
224 NI_NUMERICHOST|NI_WITHSCOPEID);
225 fprintf(stderr, "Trying %s...\n", paddr);
226 }
227 }
228 #if 0
229 /*
230 * try to rresvport() to the same port. This will make rresvport()
231 * fail it's first bind, resulting in it choosing a random port.
232 */
233 lport--;
234 #endif
235 if (fd2p == 0) {
236 write(s, "", 1);
237 lport = 0;
238 } else {
239 char num[8];
240 int s2 = rresvport_af(&lport, ai->ai_family), s3;
241 unsigned int len = ai->ai_addrlen;
242 int nfds;
243
244 if (s2 < 0)
245 goto bad;
246 listen(s2, 1);
247 (void)snprintf(num, sizeof(num), "%d", lport);
248 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
249 (void)fprintf(stderr,
250 "rcmd: write (setting up stderr): %s\n",
251 strerror(errno));
252 (void)close(s2);
253 goto bad;
254 }
255 nfds = max(s, s2)+1;
256 if(nfds > FD_SETSIZE) {
257 fprintf(stderr, "rcmd: too many files\n");
258 (void)close(s2);
259 goto bad;
260 }
261 again:
262 FD_ZERO(&reads);
263 FD_SET(s, &reads);
264 FD_SET(s2, &reads);
265 errno = 0;
266 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
267 if (errno != 0)
268 (void)fprintf(stderr,
269 "rcmd: select (setting up stderr): %s\n",
270 strerror(errno));
271 else
272 (void)fprintf(stderr,
273 "select: protocol failure in circuit setup\n");
274 (void)close(s2);
275 goto bad;
276 }
277 s3 = accept(s2, (struct sockaddr *)&from, &len);
278 switch (from.ss_family) {
279 case AF_INET:
280 aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
281 break;
282 #ifdef INET6
283 case AF_INET6:
284 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
285 break;
286 #endif
287 default:
288 aport = 0; /* error */
289 break;
290 }
291 /*
292 * XXX careful for ftp bounce attacks. If discovered, shut them
293 * down and check for the real auxiliary channel to connect.
294 */
295 if (aport == 20) {
296 close(s3);
297 goto again;
298 }
299 (void)close(s2);
300 if (s3 < 0) {
301 (void)fprintf(stderr,
302 "rcmd: accept: %s\n", strerror(errno));
303 lport = 0;
304 goto bad;
305 }
306 *fd2p = s3;
307 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
308 (void)fprintf(stderr,
309 "socket: protocol failure in circuit setup.\n");
310 goto bad2;
311 }
312 }
313 (void)write(s, locuser, strlen(locuser)+1);
314 (void)write(s, remuser, strlen(remuser)+1);
315 (void)write(s, cmd, strlen(cmd)+1);
316 if (read(s, &c, 1) != 1) {
317 (void)fprintf(stderr,
318 "rcmd: %s: %s\n", *ahost, strerror(errno));
319 goto bad2;
320 }
321 if (c != 0) {
322 while (read(s, &c, 1) == 1) {
323 (void)write(STDERR_FILENO, &c, 1);
324 if (c == '\n')
325 break;
326 }
327 goto bad2;
328 }
329 sigsetmask(oldmask);
330 freeaddrinfo(res);
331 return (s);
332 bad2:
333 if (lport)
334 (void)close(*fd2p);
335 bad:
336 (void)close(s);
337 sigsetmask(oldmask);
338 freeaddrinfo(res);
339 return (-1);
340 }
341
342 LIBINFO_EXPORT
343 int
344 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
345 char **ahost;
346 in_port_t rport;
347 const char *locuser, *remuser, *cmd;
348 int *fd2p;
349 {
350 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
351 }
352
353 LIBINFO_EXPORT
354 int
355 rresvport(port)
356 int *port;
357 {
358 return rresvport_af(port, AF_INET);
359 }
360
361 LIBINFO_EXPORT
362 int
363 rresvport_af(alport, family)
364 int *alport, family;
365 {
366 int s;
367 struct sockaddr_storage ss;
368 u_short *sport;
369
370 memset(&ss, 0, sizeof(ss));
371 ss.ss_family = family;
372 switch (family) {
373 case AF_INET:
374 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
375 sport = &((struct sockaddr_in *)&ss)->sin_port;
376 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
377 break;
378 #ifdef INET6
379 case AF_INET6:
380 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
381 sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
382 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
383 break;
384 #endif
385 default:
386 errno = EAFNOSUPPORT;
387 return -1;
388 }
389
390 s = socket(ss.ss_family, SOCK_STREAM, 0);
391 if (s < 0)
392 return (-1);
393 #if 0 /* compat_exact_traditional_rresvport_semantics */
394 sin.sin_port = htons((u_short)*alport);
395 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
396 return (s);
397 if (errno != EADDRINUSE) {
398 (void)close(s);
399 return (-1);
400 }
401 #endif
402 *sport = 0;
403 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
404 (void)close(s);
405 return (-1);
406 }
407 *alport = (int)ntohs(*sport);
408 return (s);
409 }
410
411 int __check_rhosts_file = 1;
412 char *__rcmd_errstr;
413
414 /* Guess at the size of a password buffer for getpwnam_r (see lookup.subproj/lu_group.c) */
415 #define MAXPWBUF (MAXLOGNAME + 1 + _PASSWORD_LEN + 1 + MAXPATHLEN + 1 + MAXPATHLEN + 1 + 4098)
416
417 /*
418 * AF independent extension of iruserok.
419 *
420 * Returns 0 if ok, -1 if not ok.
421 */
422 LIBINFO_EXPORT
423 int
424 iruserok_sa(ra, rlen, superuser, ruser, luser)
425 const void *ra;
426 int rlen;
427 int superuser;
428 const char *ruser, *luser;
429 {
430 register char *cp;
431 struct stat sbuf;
432 struct passwd p, *pwd;
433 FILE *hostf;
434 uid_t uid;
435 int first, status;
436 char pbuf[MAXPATHLEN];
437 const struct sockaddr *raddr;
438 struct sockaddr_storage ss;
439 char pwbuf[MAXPWBUF];
440
441 /* avoid alignment issue */
442 if (rlen > sizeof(ss))
443 return(-1);
444 memcpy(&ss, ra, rlen);
445 raddr = (struct sockaddr *)&ss;
446
447 first = 1;
448 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
449 again:
450 if (hostf) {
451 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
452 (void)fclose(hostf);
453 return (0);
454 }
455 (void)fclose(hostf);
456 }
457 if (first == 1 && (__check_rhosts_file || superuser)) {
458 first = 0;
459
460 memset(&p, 0, sizeof(struct passwd));
461 memset(pwbuf, 0, sizeof(pwbuf));
462 pwd = NULL;
463
464 status = getpwnam_r(luser, &p, pwbuf, MAXPWBUF, &pwd);
465 if (status != 0) return -1;
466
467 (void)strcpy(pbuf, pwd->pw_dir);
468 (void)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 (void)seteuid(pwd->pw_uid);
477 hostf = fopen(pbuf, "r");
478 (void)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 (void)fclose(hostf);
501 return (-1);
502 }
503 goto again;
504 }
505 return (-1);
506 }
507
508 LIBINFO_EXPORT
509 int
510 ruserok(rhost, superuser, ruser, luser)
511 const char *rhost, *ruser, *luser;
512 int superuser;
513 {
514 struct addrinfo hints, *res, *r;
515 int error;
516
517 memset(&hints, 0, sizeof(hints));
518 hints.ai_family = PF_UNSPEC;
519 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
520 error = getaddrinfo(rhost, "0", &hints, &res);
521 if (error)
522 return (-1);
523
524 for (r = res; r; r = r->ai_next) {
525 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
526 luser) == 0) {
527 freeaddrinfo(res);
528 return (0);
529 }
530 }
531 freeaddrinfo(res);
532 return (-1);
533 }
534
535 /*
536 * New .rhosts strategy: We are passed an ip address. We spin through
537 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
538 * has ip addresses, we don't have to trust a nameserver. When it
539 * contains hostnames, we spin through the list of addresses the nameserver
540 * gives us and look for a match.
541 *
542 * Returns 0 if ok, -1 if not ok.
543 */
544 LIBINFO_EXPORT
545 int
546 iruserok(raddr, superuser, ruser, luser)
547 unsigned long raddr;
548 int superuser;
549 const char *ruser, *luser;
550 {
551 struct sockaddr_in sin;
552
553 memset(&sin, 0, sizeof(sin));
554 sin.sin_family = AF_INET;
555 sin.sin_len = sizeof(struct sockaddr_in);
556 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
557 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
558 ruser, luser);
559 }
560
561 /*
562 * XXX
563 * Don't make static, used by lpd(8).
564 *
565 * Returns 0 if ok, -1 if not ok.
566 */
567 int
568 __ivaliduser(hostf, raddr, luser, ruser)
569 FILE *hostf;
570 u_int32_t raddr;
571 const char *luser, *ruser;
572 {
573 struct sockaddr_in sin;
574
575 memset(&sin, 0, sizeof(sin));
576 sin.sin_family = AF_INET;
577 sin.sin_len = sizeof(struct sockaddr_in);
578 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
579 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
580 luser, ruser);
581 }
582
583 /*
584 * Returns 0 if ok, -1 if not ok.
585 *
586 * XXX obsolete API.
587 */
588 int
589 __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
590 FILE *hostf;
591 const void *raddr;
592 const char *luser, *ruser;
593 int af, len;
594 {
595 struct sockaddr *sa = NULL;
596 struct sockaddr_in *sin = NULL;
597 #ifdef INET6
598 struct sockaddr_in6 *sin6 = NULL;
599 #endif
600 struct sockaddr_storage ss;
601
602 memset(&ss, 0, sizeof(ss));
603 switch (af) {
604 case AF_INET:
605 if (len != sizeof(sin->sin_addr))
606 return -1;
607 sin = (struct sockaddr_in *)&ss;
608 sin->sin_family = AF_INET;
609 sin->sin_len = sizeof(struct sockaddr_in);
610 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
611 break;
612 #ifdef INET6
613 case AF_INET6:
614 if (len != sizeof(sin6->sin6_addr))
615 return -1;
616 /* you will lose scope info */
617 sin6 = (struct sockaddr_in6 *)&ss;
618 sin6->sin6_family = AF_INET6;
619 sin6->sin6_len = sizeof(struct sockaddr_in6);
620 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
621 break;
622 #endif
623 default:
624 return -1;
625 }
626
627 sa = (struct sockaddr *)&ss;
628 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
629 }
630
631 /*
632 * Returns 0 if ok, -1 if not ok.
633 */
634 int
635 __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
636 FILE *hostf;
637 const struct sockaddr *raddr;
638 socklen_t salen;
639 const char *luser, *ruser;
640 {
641 register char *user, *p;
642 int ch;
643 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
644 char hname[MAXHOSTNAMELEN];
645 /* Presumed guilty until proven innocent. */
646 int userok = 0, hostok = 0;
647 #ifdef YP
648 char *ypdomain;
649
650 if (yp_get_default_domain(&ypdomain))
651 ypdomain = NULL;
652 #else
653 #define ypdomain NULL
654 #endif
655 /*
656 * We try to get the hostname back for netgroup matching.
657 * However, the .rosts file may contain IP addresses as well
658 * as names, so a name is not always required.
659 */
660 getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, NI_NAMEREQD);
661
662 while (fgets(buf, sizeof(buf), hostf)) {
663 p = buf;
664 /* Skip lines that are too long. */
665 if (strchr(p, '\n') == NULL) {
666 while ((ch = getc(hostf)) != '\n' && ch != EOF);
667 continue;
668 }
669 if (*p == '\n' || *p == '#') {
670 /* comment... */
671 continue;
672 }
673 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
674 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
675 p++;
676 }
677 if (*p == ' ' || *p == '\t') {
678 *p++ = '\0';
679 while (*p == ' ' || *p == '\t')
680 p++;
681 user = p;
682 while (*p != '\n' && *p != ' ' &&
683 *p != '\t' && *p != '\0')
684 p++;
685 } else
686 user = p;
687 *p = '\0';
688 /*
689 * Do +/- and +@/-@ checking. This looks really nasty,
690 * but it matches SunOS's behavior so far as I can tell.
691 */
692 switch(buf[0]) {
693 case '+':
694 if (!buf[1]) { /* '+' matches all hosts */
695 hostok = 1;
696 break;
697 }
698 if (buf[1] == '@') /* match a host by netgroup */
699 hostok = innetgr((char *)&buf[2],
700 (char *)&hname, NULL, ypdomain);
701 else /* match a host by addr */
702 hostok = __icheckhost(raddr, salen,
703 (char *)&buf[1]);
704 break;
705 case '-': /* reject '-' hosts and all their users */
706 if (buf[1] == '@') {
707 if (innetgr((char *)&buf[2],
708 (char *)&hname, NULL, ypdomain))
709 return(-1);
710 } else {
711 if (__icheckhost(raddr, salen,
712 (char *)&buf[1]))
713 return(-1);
714 }
715 break;
716 default: /* if no '+' or '-', do a simple match */
717 hostok = __icheckhost(raddr, salen, buf);
718 break;
719 }
720 switch(*user) {
721 case '+':
722 if (!*(user+1)) { /* '+' matches all users */
723 userok = 1;
724 break;
725 }
726 if (*(user+1) == '@') /* match a user by netgroup */
727 userok = innetgr(user+2, NULL, ruser, ypdomain);
728 else /* match a user by direct specification */
729 userok = !(strcmp(ruser, user+1));
730 break;
731 case '-': /* if we matched a hostname, */
732 if (hostok) { /* check for user field rejections */
733 if (!*(user+1))
734 return(-1);
735 if (*(user+1) == '@') {
736 if (innetgr(user+2, NULL,
737 ruser, ypdomain))
738 return(-1);
739 } else {
740 if (!strcmp(ruser, user+1))
741 return(-1);
742 }
743 }
744 break;
745 default: /* no rejections: try to match the user */
746 if (hostok)
747 userok = !(strcmp(ruser,*user ? user : luser));
748 break;
749 }
750 if (hostok && userok)
751 return(0);
752 }
753 return (-1);
754 }
755
756 /*
757 * Returns "true" if match, 0 if no match.
758 *
759 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
760 * if af == AF_INET6.
761 */
762 static int
763 __icheckhost(raddr, salen, lhost)
764 const struct sockaddr *raddr;
765 socklen_t salen;
766 const char *lhost;
767 {
768 struct sockaddr_in sin;
769 struct sockaddr_in6 *sin6;
770 struct addrinfo hints, *res, *r;
771 int error;
772 char h1[NI_MAXHOST], h2[NI_MAXHOST];
773
774 if (raddr->sa_family == AF_INET6) {
775 sin6 = (struct sockaddr_in6 *)raddr;
776 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
777 memset(&sin, 0, sizeof(sin));
778 sin.sin_family = AF_INET;
779 sin.sin_len = sizeof(struct sockaddr_in);
780 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
781 sizeof(sin.sin_addr));
782 raddr = (struct sockaddr *)&sin;
783 salen = sin.sin_len;
784 }
785 }
786
787 h1[0] = '\0';
788 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
789 NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
790 return (0);
791
792 /* Resolve laddr into sockaddr */
793 memset(&hints, 0, sizeof(hints));
794 hints.ai_family = raddr->sa_family;
795 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/
796 res = NULL;
797 error = getaddrinfo(lhost, "0", &hints, &res);
798 if (error)
799 return (0);
800
801 for (r = res; r ; r = r->ai_next) {
802 h2[0] = '\0';
803 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
804 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
805 continue;
806 if (strcmp(h1, h2) == 0) {
807 freeaddrinfo(res);
808 return (1);
809 }
810 }
811
812 /* No match. */
813 freeaddrinfo(res);
814 return (0);
815 }