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