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