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