Libinfo-324.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 <arpa/inet.h>
70
71 #include <signal.h>
72 #include <fcntl.h>
73 #include <netdb.h>
74 #include <unistd.h>
75 #include <pwd.h>
76 #include <errno.h>
77 #include <stdio.h>
78 #include <ctype.h>
79 #include <string.h>
80 #include <time.h>
81 #ifdef YP
82 #include <rpc/rpc.h>
83 #include <rpcsvc/yp_prot.h>
84 #include <rpcsvc/ypclnt.h>
85 #endif
86
87 #include "nameser8_compat.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 extern int bindresvport_sa(int, struct sockaddr *);
100
101 #define max(a, b) ((a > b) ? a : b)
102
103 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
104 int __ivaliduser_af __P((FILE *,const void *, const char *, const char *,
105 int, int));
106 int __ivaliduser_sa __P((FILE *, const struct sockaddr *, socklen_t,
107 const char *,const char *));
108 static int __icheckhost __P((const struct sockaddr *, socklen_t,
109 const char *));
110
111 int
112 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
113 char **ahost;
114 u_short rport;
115 const char *locuser, *remuser, *cmd;
116 int *fd2p;
117 int af;
118 {
119 struct addrinfo hints, *res, *ai;
120 struct sockaddr_storage from;
121 fd_set reads;
122 long oldmask;
123 pid_t pid;
124 int s, aport, lport, timo, error;
125 char c;
126 int refused, nres;
127 char num[8], paddr[NI_MAXHOST];
128 static char canonnamebuf[MAXDNAME]; /* is it proper here? */
129
130 pid = getpid();
131
132 memset(&hints, 0, sizeof(hints));
133 hints.ai_flags = AI_CANONNAME;
134 hints.ai_family = af;
135 hints.ai_socktype = SOCK_STREAM;
136 hints.ai_protocol = 0;
137 (void)snprintf(num, sizeof(num), "%d", ntohs(rport));
138 error = getaddrinfo(*ahost, num, &hints, &res);
139 if (error) {
140 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
141 gai_strerror(error));
142 if (error == EAI_SYSTEM)
143 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
144 strerror(errno));
145 return (-1);
146 }
147
148 if (res->ai_canonname
149 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
150 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
151 *ahost = canonnamebuf;
152 }
153 nres = 0;
154 for (ai = res; ai; ai = ai->ai_next)
155 nres++;
156 ai = res;
157 refused = 0;
158 oldmask = sigblock(sigmask(SIGURG));
159 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
160 s = rresvport_af(&lport, ai->ai_family);
161 if (s < 0) {
162 if (errno != EAGAIN && ai->ai_next) {
163 ai = ai->ai_next;
164 continue;
165 }
166 if (errno == EAGAIN)
167 (void)fprintf(stderr,
168 "rcmd: socket: All ports in use\n");
169 else
170 (void)fprintf(stderr, "rcmd: socket: %s\n",
171 strerror(errno));
172 freeaddrinfo(res);
173 sigsetmask(oldmask);
174 return (-1);
175 }
176 fcntl(s, F_SETOWN, pid);
177 if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
178 break;
179 (void)close(s);
180 if (errno == EADDRINUSE) {
181 lport--;
182 continue;
183 }
184 if (errno == ECONNREFUSED)
185 refused = 1;
186 if (ai->ai_next == NULL && (!refused || timo > 16)) {
187 (void)fprintf(stderr, "%s: %s\n",
188 *ahost, strerror(errno));
189 freeaddrinfo(res);
190 sigsetmask(oldmask);
191 return (-1);
192 }
193 if (nres > 1) {
194 int oerrno = errno;
195
196 getnameinfo(ai->ai_addr, ai->ai_addrlen,
197 paddr, sizeof(paddr),
198 NULL, 0,
199 NI_NUMERICHOST|NI_WITHSCOPEID);
200 (void)fprintf(stderr, "connect to address %s: ",
201 paddr);
202 errno = oerrno;
203 perror(0);
204 }
205 if ((ai = ai->ai_next) == NULL) {
206 /* refused && timo <= 16 */
207 struct timespec time_to_sleep, time_remaining;
208
209 time_to_sleep.tv_sec = timo;
210 time_to_sleep.tv_nsec = 0;
211 (void)nanosleep(&time_to_sleep, &time_remaining);
212 timo *= 2;
213 ai = res;
214 refused = 0;
215 }
216 if (nres > 1) {
217 getnameinfo(ai->ai_addr, ai->ai_addrlen,
218 paddr, sizeof(paddr),
219 NULL, 0,
220 NI_NUMERICHOST|NI_WITHSCOPEID);
221 fprintf(stderr, "Trying %s...\n", paddr);
222 }
223 }
224 #if 0
225 /*
226 * try to rresvport() to the same port. This will make rresvport()
227 * fail it's first bind, resulting in it choosing a random port.
228 */
229 lport--;
230 #endif
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 unsigned 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 sigsetmask(oldmask);
326 freeaddrinfo(res);
327 return (s);
328 bad2:
329 if (lport)
330 (void)close(*fd2p);
331 bad:
332 (void)close(s);
333 sigsetmask(oldmask);
334 freeaddrinfo(res);
335 return (-1);
336 }
337
338 int
339 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
340 char **ahost;
341 in_port_t rport;
342 const char *locuser, *remuser, *cmd;
343 int *fd2p;
344 {
345 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
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 /* Guess at the size of a password buffer for getpwnam_r (see lookup.subproj/lu_group.c) */
408 #define MAXPWBUF (MAXLOGNAME + 1 + _PASSWORD_LEN + 1 + MAXPATHLEN + 1 + MAXPATHLEN + 1 + 4098)
409
410 /*
411 * AF independent extension of iruserok.
412 *
413 * Returns 0 if ok, -1 if not ok.
414 */
415 int
416 iruserok_sa(ra, rlen, superuser, ruser, luser)
417 const void *ra;
418 int rlen;
419 int superuser;
420 const char *ruser, *luser;
421 {
422 register char *cp;
423 struct stat sbuf;
424 struct passwd p, *pwd;
425 FILE *hostf;
426 uid_t uid;
427 int first, status;
428 char pbuf[MAXPATHLEN];
429 const struct sockaddr *raddr;
430 struct sockaddr_storage ss;
431 char pwbuf[MAXPWBUF];
432
433 /* avoid alignment issue */
434 if (rlen > sizeof(ss))
435 return(-1);
436 memcpy(&ss, ra, rlen);
437 raddr = (struct sockaddr *)&ss;
438
439 first = 1;
440 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
441 again:
442 if (hostf) {
443 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
444 (void)fclose(hostf);
445 return (0);
446 }
447 (void)fclose(hostf);
448 }
449 if (first == 1 && (__check_rhosts_file || superuser)) {
450 first = 0;
451
452 memset(&p, 0, sizeof(struct passwd));
453 memset(pwbuf, 0, sizeof(pwbuf));
454 pwd = NULL;
455
456 status = getpwnam_r(luser, &p, pwbuf, MAXPWBUF, &pwd);
457 if (status != 0) return -1;
458
459 (void)strcpy(pbuf, pwd->pw_dir);
460 (void)strcat(pbuf, "/.rhosts");
461
462 /*
463 * Change effective uid while opening .rhosts. If root and
464 * reading an NFS mounted file system, can't read files that
465 * are protected read/write owner only.
466 */
467 uid = geteuid();
468 (void)seteuid(pwd->pw_uid);
469 hostf = fopen(pbuf, "r");
470 (void)seteuid(uid);
471
472 if (hostf == NULL)
473 return (-1);
474 /*
475 * If not a regular file, or is owned by someone other than
476 * user or root or if writeable by anyone but the owner, quit.
477 */
478 cp = NULL;
479 if (lstat(pbuf, &sbuf) < 0)
480 cp = ".rhosts lstat failed";
481 else if (!S_ISREG(sbuf.st_mode))
482 cp = ".rhosts not regular file";
483 else if (fstat(fileno(hostf), &sbuf) < 0)
484 cp = ".rhosts fstat failed";
485 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
486 cp = "bad .rhosts owner";
487 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
488 cp = ".rhosts writeable by other than owner";
489 /* If there were any problems, quit. */
490 if (cp) {
491 __rcmd_errstr = cp;
492 (void)fclose(hostf);
493 return (-1);
494 }
495 goto again;
496 }
497 return (-1);
498 }
499
500 int
501 ruserok(rhost, superuser, ruser, luser)
502 const char *rhost, *ruser, *luser;
503 int superuser;
504 {
505 struct addrinfo hints, *res, *r;
506 int error;
507
508 memset(&hints, 0, sizeof(hints));
509 hints.ai_family = PF_UNSPEC;
510 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
511 error = getaddrinfo(rhost, "0", &hints, &res);
512 if (error)
513 return (-1);
514
515 for (r = res; r; r = r->ai_next) {
516 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
517 luser) == 0) {
518 freeaddrinfo(res);
519 return (0);
520 }
521 }
522 freeaddrinfo(res);
523 return (-1);
524 }
525
526 /*
527 * New .rhosts strategy: We are passed an ip address. We spin through
528 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
529 * has ip addresses, we don't have to trust a nameserver. When it
530 * contains hostnames, we spin through the list of addresses the nameserver
531 * gives us and look for a match.
532 *
533 * Returns 0 if ok, -1 if not ok.
534 */
535 int
536 iruserok(raddr, superuser, ruser, luser)
537 unsigned long raddr;
538 int superuser;
539 const char *ruser, *luser;
540 {
541 struct sockaddr_in sin;
542
543 memset(&sin, 0, sizeof(sin));
544 sin.sin_family = AF_INET;
545 sin.sin_len = sizeof(struct sockaddr_in);
546 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
547 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
548 ruser, luser);
549 }
550
551 /*
552 * XXX
553 * Don't make static, used by lpd(8).
554 *
555 * Returns 0 if ok, -1 if not ok.
556 */
557 int
558 __ivaliduser(hostf, raddr, luser, ruser)
559 FILE *hostf;
560 u_int32_t raddr;
561 const char *luser, *ruser;
562 {
563 struct sockaddr_in sin;
564
565 memset(&sin, 0, sizeof(sin));
566 sin.sin_family = AF_INET;
567 sin.sin_len = sizeof(struct sockaddr_in);
568 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
569 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
570 luser, ruser);
571 }
572
573 /*
574 * Returns 0 if ok, -1 if not ok.
575 *
576 * XXX obsolete API.
577 */
578 int
579 __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
580 FILE *hostf;
581 const void *raddr;
582 const char *luser, *ruser;
583 int af, len;
584 {
585 struct sockaddr *sa = NULL;
586 struct sockaddr_in *sin = NULL;
587 #ifdef INET6
588 struct sockaddr_in6 *sin6 = NULL;
589 #endif
590 struct sockaddr_storage ss;
591
592 memset(&ss, 0, sizeof(ss));
593 switch (af) {
594 case AF_INET:
595 if (len != sizeof(sin->sin_addr))
596 return -1;
597 sin = (struct sockaddr_in *)&ss;
598 sin->sin_family = AF_INET;
599 sin->sin_len = sizeof(struct sockaddr_in);
600 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
601 break;
602 #ifdef INET6
603 case AF_INET6:
604 if (len != sizeof(sin6->sin6_addr))
605 return -1;
606 /* you will lose scope info */
607 sin6 = (struct sockaddr_in6 *)&ss;
608 sin6->sin6_family = AF_INET6;
609 sin6->sin6_len = sizeof(struct sockaddr_in6);
610 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
611 break;
612 #endif
613 default:
614 return -1;
615 }
616
617 sa = (struct sockaddr *)&ss;
618 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
619 }
620
621 /*
622 * Returns 0 if ok, -1 if not ok.
623 */
624 int
625 __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
626 FILE *hostf;
627 const struct sockaddr *raddr;
628 socklen_t salen;
629 const char *luser, *ruser;
630 {
631 register char *user, *p;
632 int ch;
633 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
634 char hname[MAXHOSTNAMELEN];
635 /* Presumed guilty until proven innocent. */
636 int userok = 0, hostok = 0;
637 #ifdef YP
638 char *ypdomain;
639
640 if (yp_get_default_domain(&ypdomain))
641 ypdomain = NULL;
642 #else
643 #define ypdomain NULL
644 #endif
645 /*
646 * We try to get the hostname back for netgroup matching.
647 * However, the .rosts file may contain IP addresses as well
648 * as names, so a name is not always required.
649 */
650 getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, NI_NAMEREQD);
651
652 while (fgets(buf, sizeof(buf), hostf)) {
653 p = buf;
654 /* Skip lines that are too long. */
655 if (strchr(p, '\n') == NULL) {
656 while ((ch = getc(hostf)) != '\n' && ch != EOF);
657 continue;
658 }
659 if (*p == '\n' || *p == '#') {
660 /* comment... */
661 continue;
662 }
663 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
664 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
665 p++;
666 }
667 if (*p == ' ' || *p == '\t') {
668 *p++ = '\0';
669 while (*p == ' ' || *p == '\t')
670 p++;
671 user = p;
672 while (*p != '\n' && *p != ' ' &&
673 *p != '\t' && *p != '\0')
674 p++;
675 } else
676 user = p;
677 *p = '\0';
678 /*
679 * Do +/- and +@/-@ checking. This looks really nasty,
680 * but it matches SunOS's behavior so far as I can tell.
681 */
682 switch(buf[0]) {
683 case '+':
684 if (!buf[1]) { /* '+' matches all hosts */
685 hostok = 1;
686 break;
687 }
688 if (buf[1] == '@') /* match a host by netgroup */
689 hostok = innetgr((char *)&buf[2],
690 (char *)&hname, NULL, ypdomain);
691 else /* match a host by addr */
692 hostok = __icheckhost(raddr, salen,
693 (char *)&buf[1]);
694 break;
695 case '-': /* reject '-' hosts and all their users */
696 if (buf[1] == '@') {
697 if (innetgr((char *)&buf[2],
698 (char *)&hname, NULL, ypdomain))
699 return(-1);
700 } else {
701 if (__icheckhost(raddr, salen,
702 (char *)&buf[1]))
703 return(-1);
704 }
705 break;
706 default: /* if no '+' or '-', do a simple match */
707 hostok = __icheckhost(raddr, salen, buf);
708 break;
709 }
710 switch(*user) {
711 case '+':
712 if (!*(user+1)) { /* '+' matches all users */
713 userok = 1;
714 break;
715 }
716 if (*(user+1) == '@') /* match a user by netgroup */
717 userok = innetgr(user+2, NULL, ruser, ypdomain);
718 else /* match a user by direct specification */
719 userok = !(strcmp(ruser, user+1));
720 break;
721 case '-': /* if we matched a hostname, */
722 if (hostok) { /* check for user field rejections */
723 if (!*(user+1))
724 return(-1);
725 if (*(user+1) == '@') {
726 if (innetgr(user+2, NULL,
727 ruser, ypdomain))
728 return(-1);
729 } else {
730 if (!strcmp(ruser, user+1))
731 return(-1);
732 }
733 }
734 break;
735 default: /* no rejections: try to match the user */
736 if (hostok)
737 userok = !(strcmp(ruser,*user ? user : luser));
738 break;
739 }
740 if (hostok && userok)
741 return(0);
742 }
743 return (-1);
744 }
745
746 /*
747 * Returns "true" if match, 0 if no match.
748 *
749 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
750 * if af == AF_INET6.
751 */
752 static int
753 __icheckhost(raddr, salen, lhost)
754 const struct sockaddr *raddr;
755 socklen_t salen;
756 const char *lhost;
757 {
758 struct sockaddr_in sin;
759 struct sockaddr_in6 *sin6;
760 struct addrinfo hints, *res, *r;
761 int error;
762 char h1[NI_MAXHOST], h2[NI_MAXHOST];
763
764 if (raddr->sa_family == AF_INET6) {
765 sin6 = (struct sockaddr_in6 *)raddr;
766 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
767 memset(&sin, 0, sizeof(sin));
768 sin.sin_family = AF_INET;
769 sin.sin_len = sizeof(struct sockaddr_in);
770 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
771 sizeof(sin.sin_addr));
772 raddr = (struct sockaddr *)&sin;
773 salen = sin.sin_len;
774 }
775 }
776
777 h1[0] = '\0';
778 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
779 NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
780 return (0);
781
782 /* Resolve laddr into sockaddr */
783 memset(&hints, 0, sizeof(hints));
784 hints.ai_family = raddr->sa_family;
785 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/
786 res = NULL;
787 error = getaddrinfo(lhost, "0", &hints, &res);
788 if (error)
789 return (0);
790
791 for (r = res; r ; r = r->ai_next) {
792 h2[0] = '\0';
793 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
794 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
795 continue;
796 if (strcmp(h1, h2) == 0) {
797 freeaddrinfo(res);
798 return (1);
799 }
800 }
801
802 /* No match. */
803 freeaddrinfo(res);
804 return (0);
805 }