xref: /dragonfly/lib/libc/net/rcmd.c (revision a632cd2d33eae3cdf365948061576a303057c733)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)rcmd.c       8.3 (Berkeley) 3/26/94
30  * $FreeBSD: src/lib/libc/net/rcmd.c,v 1.42 2007/01/09 00:28:02 imp Exp $
31  */
32 
33 #include "namespace.h"
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 
41 #include <signal.h>
42 #include <fcntl.h>
43 #include <netdb.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <pwd.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <rpc/rpc.h>
52 #ifdef YP
53 #include <rpcsvc/yp_prot.h>
54 #include <rpcsvc/ypclnt.h>
55 #endif
56 #include <arpa/nameser.h>
57 #include "un-namespace.h"
58 
59 extern int innetgr(const char *, const char *, const char *, const char *);
60 
61 #define max(a, b)   ((a > b) ? a : b)
62 
63 int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
64 int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
65 int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
66                         const char *);
67 static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
68 
69 char paddr[NI_MAXHOST];
70 
71 int
rcmd(char ** ahost,int rport,const char * locuser,const char * remuser,const char * cmd,int * fd2p)72 rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
73      const char *cmd, int *fd2p)
74 {
75           return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
76 }
77 
78 int
rcmd_af(char ** ahost,int rport,const char * locuser,const char * remuser,const char * cmd,int * fd2p,int af)79 rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser,
80           const char *cmd, int *fd2p, int af)
81 {
82           struct addrinfo hints, *res, *ai;
83           struct sockaddr_storage from;
84           fd_set reads;
85           sigset_t oldmask, newmask;
86           pid_t pid;
87           int s, aport, lport, timo, error;
88           char c, *p;
89           int refused, nres;
90           char num[8];
91           static char canonnamebuf[MAXDNAME];     /* is it proper here? */
92 
93           /* call rcmdsh() with specified remote shell if appropriate. */
94           if (!issetugid() && (p = getenv("RSH"))) {
95                     struct servent *sp = getservbyname("shell", "tcp");
96 
97                     if (sp && sp->s_port == rport)
98                               return (rcmdsh(ahost, rport, locuser, remuser,
99                                   cmd, p));
100           }
101 
102           /* use rsh(1) if non-root and remote port is shell. */
103           if (geteuid()) {
104                     struct servent *sp = getservbyname("shell", "tcp");
105 
106                     if (sp && sp->s_port == rport)
107                               return (rcmdsh(ahost, rport, locuser, remuser,
108                                   cmd, NULL));
109           }
110 
111           pid = getpid();
112 
113           memset(&hints, 0, sizeof(hints));
114           hints.ai_flags = AI_CANONNAME;
115           hints.ai_family = af;
116           hints.ai_socktype = SOCK_STREAM;
117           hints.ai_protocol = 0;
118           snprintf(num, sizeof(num), "%d", ntohs(rport));
119           error = getaddrinfo(*ahost, num, &hints, &res);
120           if (error) {
121                     fprintf(stderr, "rcmd: getaddrinfo: %s\n",
122                               gai_strerror(error));
123                     if (error == EAI_SYSTEM)
124                               fprintf(stderr, "rcmd: getaddrinfo: %s\n",
125                                         strerror(errno));
126                     return (-1);
127           }
128 
129           if (res->ai_canonname &&
130               strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
131                     strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
132                     *ahost = canonnamebuf;
133           }
134           nres = 0;
135           for (ai = res; ai; ai = ai->ai_next)
136                     nres++;
137           ai = res;
138           refused = 0;
139           sigemptyset(&newmask);
140           sigaddset(&newmask, SIGURG);
141           _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
142           for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
143                     s = rresvport_af(&lport, ai->ai_family);
144                     if (s < 0) {
145                               if (errno != EAGAIN && ai->ai_next) {
146                                         ai = ai->ai_next;
147                                         continue;
148                               }
149                               if (errno == EAGAIN)
150                                         fprintf(stderr,
151                                             "rcmd: socket: All ports in use\n");
152                               else
153                                         fprintf(stderr, "rcmd: socket: %s\n",
154                                             strerror(errno));
155                               freeaddrinfo(res);
156                               _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
157                                   NULL);
158                               return (-1);
159                     }
160                     _fcntl(s, F_SETOWN, pid);
161                     if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
162                               break;
163                     _close(s);
164                     if (errno == EADDRINUSE) {
165                               lport--;
166                               continue;
167                     }
168                     if (errno == ECONNREFUSED)
169                               refused = 1;
170                     if (ai->ai_next == NULL && (!refused || timo > 16)) {
171                               fprintf(stderr, "%s: %s\n", *ahost, strerror(errno));
172                               freeaddrinfo(res);
173                               _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
174                                   NULL);
175                               return (-1);
176                     }
177                     if (nres > 1) {
178                               int oerrno = errno;
179 
180                               getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
181                                   sizeof(paddr), NULL, 0, NI_NUMERICHOST);
182                               fprintf(stderr, "connect to address %s: ", paddr);
183                               errno = oerrno;
184                               perror(0);
185                     }
186                     if ((ai = ai->ai_next) == NULL) {
187                               /* refused && timo <= 16 */
188                               struct timespec time_to_sleep, time_remaining;
189 
190                               time_to_sleep.tv_sec = timo;
191                               time_to_sleep.tv_nsec = 0;
192                               _nanosleep(&time_to_sleep, &time_remaining);
193                               timo *= 2;
194                               ai = res;
195                               refused = 0;
196                     }
197                     if (nres > 1) {
198                               getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
199                                   sizeof(paddr), NULL, 0, NI_NUMERICHOST);
200                               fprintf(stderr, "Trying %s...\n", paddr);
201                     }
202           }
203           lport--;
204           if (fd2p == NULL) {
205                     _write(s, "", 1);
206                     lport = 0;
207           } else {
208                     int s2 = rresvport_af(&lport, ai->ai_family), s3;
209                     socklen_t len = ai->ai_addrlen;
210                     int nfds;
211 
212                     if (s2 < 0)
213                               goto bad;
214                     _listen(s2, 1);
215                     snprintf(num, sizeof(num), "%d", lport);
216                     if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
217                               fprintf(stderr,
218                                   "rcmd: write (setting up stderr): %s\n",
219                                   strerror(errno));
220                               _close(s2);
221                               goto bad;
222                     }
223                     nfds = max(s, s2)+1;
224                     if(nfds > FD_SETSIZE) {
225                               fprintf(stderr, "rcmd: too many files\n");
226                               _close(s2);
227                               goto bad;
228                     }
229 again:
230                     FD_ZERO(&reads);
231                     FD_SET(s, &reads);
232                     FD_SET(s2, &reads);
233                     errno = 0;
234                     if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
235                               if (errno != 0)
236                                         fprintf(stderr,
237                                             "rcmd: select (setting up stderr): %s\n",
238                                             strerror(errno));
239                               else
240                                         fprintf(stderr,
241                                         "select: protocol failure in circuit setup\n");
242                               _close(s2);
243                               goto bad;
244                     }
245                     s3 = _accept(s2, (struct sockaddr *)&from, &len);
246                     switch (from.ss_family) {
247                     case AF_INET:
248                               aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
249                               break;
250 #ifdef INET6
251                     case AF_INET6:
252                               aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
253                               break;
254 #endif
255                     default:
256                               aport = 0;          /* error */
257                               break;
258                     }
259                     /*
260                      * XXX careful for ftp bounce attacks. If discovered, shut them
261                      * down and check for the real auxiliary channel to connect.
262                      */
263                     if (aport == 20) {
264                               _close(s3);
265                               goto again;
266                     }
267                     _close(s2);
268                     if (s3 < 0) {
269                               fprintf(stderr,
270                                   "rcmd: accept: %s\n", strerror(errno));
271                               lport = 0;
272                               goto bad;
273                     }
274                     *fd2p = s3;
275                     if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
276                               fprintf(stderr,
277                                   "socket: protocol failure in circuit setup.\n");
278                               goto bad2;
279                     }
280           }
281           _write(s, locuser, strlen(locuser)+1);
282           _write(s, remuser, strlen(remuser)+1);
283           _write(s, cmd, strlen(cmd)+1);
284           if (_read(s, &c, 1) != 1) {
285                     fprintf(stderr,
286                         "rcmd: %s: %s\n", *ahost, strerror(errno));
287                     goto bad2;
288           }
289           if (c != 0) {
290                     while (_read(s, &c, 1) == 1) {
291                               _write(STDERR_FILENO, &c, 1);
292                               if (c == '\n')
293                                         break;
294                     }
295                     goto bad2;
296           }
297           _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
298           freeaddrinfo(res);
299           return (s);
300 bad2:
301           if (lport)
302                     _close(*fd2p);
303 bad:
304           _close(s);
305           _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
306           freeaddrinfo(res);
307           return (-1);
308 }
309 
310 int
rresvport(int * port)311 rresvport(int *port)
312 {
313           return rresvport_af(port, AF_INET);
314 }
315 
316 int
rresvport_af(int * alport,int family)317 rresvport_af(int *alport, int family)
318 {
319           int s;
320           struct sockaddr_storage ss;
321           u_short *sport;
322 
323           memset(&ss, 0, sizeof(ss));
324           ss.ss_family = family;
325           switch (family) {
326           case AF_INET:
327                     ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
328                     sport = &((struct sockaddr_in *)&ss)->sin_port;
329                     ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
330                     break;
331 #ifdef INET6
332           case AF_INET6:
333                     ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
334                     sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
335                     ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
336                     break;
337 #endif
338           default:
339                     errno = EAFNOSUPPORT;
340                     return -1;
341           }
342 
343           s = _socket(ss.ss_family, SOCK_STREAM, 0);
344           if (s < 0)
345                     return (-1);
346 #if 0 /* compat_exact_traditional_rresvport_semantics */
347           sin.sin_port = htons((u_short)*alport);
348           if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
349                     return (s);
350           if (errno != EADDRINUSE) {
351                     _close(s);
352                     return (-1);
353           }
354 #endif
355           *sport = 0;
356           if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
357                     _close(s);
358                     return (-1);
359           }
360           *alport = (int)ntohs(*sport);
361           return (s);
362 }
363 
364 int       __check_rhosts_file = 1;
365 char      *__rcmd_errstr;
366 
367 int
ruserok(const char * rhost,int superuser,const char * ruser,const char * luser)368 ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
369 {
370           struct addrinfo hints, *res, *r;
371           int error;
372 
373           memset(&hints, 0, sizeof(hints));
374           hints.ai_family = PF_UNSPEC;
375           hints.ai_socktype = SOCK_DGRAM;         /*dummy*/
376           error = getaddrinfo(rhost, "0", &hints, &res);
377           if (error)
378                     return (-1);
379 
380           for (r = res; r; r = r->ai_next) {
381                     if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
382                         luser) == 0) {
383                               freeaddrinfo(res);
384                               return (0);
385                     }
386           }
387           freeaddrinfo(res);
388           return (-1);
389 }
390 
391 /*
392  * New .rhosts strategy: We are passed an ip address. We spin through
393  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
394  * has ip addresses, we don't have to trust a nameserver.  When it
395  * contains hostnames, we spin through the list of addresses the nameserver
396  * gives us and look for a match.
397  *
398  * Returns 0 if ok, -1 if not ok.
399  */
400 int
iruserok(unsigned long raddr,int superuser,const char * ruser,const char * luser)401 iruserok(unsigned long raddr, int superuser, const char *ruser,
402            const char *luser)
403 {
404           struct sockaddr_in sin;
405 
406           memset(&sin, 0, sizeof(sin));
407           sin.sin_family = AF_INET;
408           sin.sin_len = sizeof(struct sockaddr_in);
409           memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
410           return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
411                     ruser, luser);
412 }
413 
414 /*
415  * AF independent extension of iruserok.
416  *
417  * Returns 0 if ok, -1 if not ok.
418  */
419 int
iruserok_sa(const void * ra,int rlen,int superuser,const char * ruser,const char * luser)420 iruserok_sa(const void *ra, int rlen, int superuser, const char *ruser,
421               const char *luser)
422 {
423           char *cp;
424           struct stat sbuf;
425           struct passwd *pwd;
426           FILE *hostf;
427           uid_t uid;
428           int first;
429           char pbuf[MAXPATHLEN];
430           const struct sockaddr *raddr;
431           struct sockaddr_storage ss;
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                               fclose(hostf);
445                               return (0);
446                     }
447                     fclose(hostf);
448           }
449           if (first == 1 && (__check_rhosts_file || superuser)) {
450                     first = 0;
451                     if ((pwd = getpwnam(luser)) == NULL)
452                               return (-1);
453                     strcpy(pbuf, pwd->pw_dir);
454                     strcat(pbuf, "/.rhosts");
455 
456                     /*
457                      * Change effective uid while opening .rhosts.  If root and
458                      * reading an NFS mounted file system, can't read files that
459                      * are protected read/write owner only.
460                      */
461                     uid = geteuid();
462                     seteuid(pwd->pw_uid);
463                     hostf = fopen(pbuf, "r");
464                     seteuid(uid);
465 
466                     if (hostf == NULL)
467                               return (-1);
468                     /*
469                      * If not a regular file, or is owned by someone other than
470                      * user or root or if writeable by anyone but the owner, quit.
471                      */
472                     cp = NULL;
473                     if (lstat(pbuf, &sbuf) < 0)
474                               cp = ".rhosts lstat failed";
475                     else if (!S_ISREG(sbuf.st_mode))
476                               cp = ".rhosts not regular file";
477                     else if (_fstat(fileno(hostf), &sbuf) < 0)
478                               cp = ".rhosts fstat failed";
479                     else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
480                               cp = "bad .rhosts owner";
481                     else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
482                               cp = ".rhosts writeable by other than owner";
483                     /* If there were any problems, quit. */
484                     if (cp) {
485                               __rcmd_errstr = cp;
486                               fclose(hostf);
487                               return (-1);
488                     }
489                     goto again;
490           }
491           return (-1);
492 }
493 
494 /*
495  * XXX
496  * Don't make static, used by lpd(8).
497  *
498  * Returns 0 if ok, -1 if not ok.
499  */
500 int
__ivaliduser(FILE * hostf,u_int32_t raddr,const char * luser,const char * ruser)501 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
502 {
503           struct sockaddr_in sin;
504 
505           memset(&sin, 0, sizeof(sin));
506           sin.sin_family = AF_INET;
507           sin.sin_len = sizeof(struct sockaddr_in);
508           memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
509           return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
510                     luser, ruser);
511 }
512 
513 /*
514  * Returns 0 if ok, -1 if not ok.
515  *
516  * XXX obsolete API.
517  */
518 int
__ivaliduser_af(FILE * hostf,const void * raddr,const char * luser,const char * ruser,int af,int len)519 __ivaliduser_af(FILE *hostf, const void *raddr, const char *luser,
520                     const char *ruser, int af, int len)
521 {
522           struct sockaddr *sa = NULL;
523           struct sockaddr_in *sin = NULL;
524 #ifdef INET6
525           struct sockaddr_in6 *sin6 = NULL;
526 #endif
527           struct sockaddr_storage ss;
528 
529           memset(&ss, 0, sizeof(ss));
530           switch (af) {
531           case AF_INET:
532                     if (len != sizeof(sin->sin_addr))
533                               return -1;
534                     sin = (struct sockaddr_in *)&ss;
535                     sin->sin_family = AF_INET;
536                     sin->sin_len = sizeof(struct sockaddr_in);
537                     memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
538                     break;
539 #ifdef INET6
540           case AF_INET6:
541                     if (len != sizeof(sin6->sin6_addr))
542                               return -1;
543                     /* you will lose scope info */
544                     sin6 = (struct sockaddr_in6 *)&ss;
545                     sin6->sin6_family = AF_INET6;
546                     sin6->sin6_len = sizeof(struct sockaddr_in6);
547                     memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
548                     break;
549 #endif
550           default:
551                     return -1;
552           }
553 
554           sa = (struct sockaddr *)&ss;
555           return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
556 }
557 
558 int
__ivaliduser_sa(FILE * hostf,const struct sockaddr * raddr,socklen_t salen,const char * luser,const char * ruser)559 __ivaliduser_sa(FILE *hostf, const struct sockaddr *raddr, socklen_t salen,
560                     const char *luser, const char *ruser)
561 {
562           char *user, *p;
563           int ch;
564           char buf[MAXHOSTNAMELEN + 128];                   /* host + login */
565           char hname[MAXHOSTNAMELEN];
566           /* Presumed guilty until proven innocent. */
567           int userok = 0, hostok = 0;
568 #ifdef YP
569           char *ypdomain;
570 
571           if (yp_get_default_domain(&ypdomain))
572                     ypdomain = NULL;
573 #else
574 #define   ypdomain NULL
575 #endif
576           /* We need to get the damn hostname back for netgroup matching. */
577           if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
578                               NI_NAMEREQD) != 0)
579                     hname[0] = '\0';
580 
581           while (fgets(buf, sizeof(buf), hostf)) {
582                     p = buf;
583                     /* Skip lines that are too long. */
584                     if (strchr(p, '\n') == NULL) {
585                               while ((ch = getc(hostf)) != '\n' && ch != EOF);
586                               continue;
587                     }
588                     if (*p == '\n' || *p == '#') {
589                               /* comment... */
590                               continue;
591                     }
592                     while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
593                               *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
594                               p++;
595                     }
596                     if (*p == ' ' || *p == '\t') {
597                               *p++ = '\0';
598                               while (*p == ' ' || *p == '\t')
599                                         p++;
600                               user = p;
601                               while (*p != '\n' && *p != ' ' &&
602                                   *p != '\t' && *p != '\0')
603                                         p++;
604                     } else
605                               user = p;
606                     *p = '\0';
607                     /*
608                      * Do +/- and +@/-@ checking. This looks really nasty,
609                      * but it matches SunOS's behavior so far as I can tell.
610                      */
611                     switch(buf[0]) {
612                     case '+':
613                               if (!buf[1]) {     /* '+' matches all hosts */
614                                         hostok = 1;
615                                         break;
616                               }
617                               if (buf[1] == '@')  /* match a host by netgroup */
618                                         hostok = hname[0] != '\0' &&
619                                             innetgr(&buf[2], hname, NULL, ypdomain);
620                               else                /* match a host by addr */
621                                         hostok = __icheckhost(raddr, salen,
622                                                                   (char *)&buf[1]);
623                               break;
624                     case '-':     /* reject '-' hosts and all their users */
625                               if (buf[1] == '@') {
626                                         if (hname[0] == '\0' ||
627                                             innetgr(&buf[2], hname, NULL, ypdomain))
628                                                   return(-1);
629                               } else {
630                                         if (__icheckhost(raddr, salen,
631                                                              (char *)&buf[1]))
632                                                   return(-1);
633                               }
634                               break;
635                     default:  /* if no '+' or '-', do a simple match */
636                               hostok = __icheckhost(raddr, salen, buf);
637                               break;
638                     }
639                     switch(*user) {
640                     case '+':
641                               if (!*(user+1)) {      /* '+' matches all users */
642                                         userok = 1;
643                                         break;
644                               }
645                               if (*(user+1) == '@')  /* match a user by netgroup */
646                                         userok = innetgr(user+2, NULL, ruser, ypdomain);
647                               else         /* match a user by direct specification */
648                                         userok = !(strcmp(ruser, user+1));
649                               break;
650                     case '-':                     /* if we matched a hostname, */
651                               if (hostok) {   /* check for user field rejections */
652                                         if (!*(user+1))
653                                                   return(-1);
654                                         if (*(user+1) == '@') {
655                                                   if (innetgr(user+2, NULL,
656                                                                       ruser, ypdomain))
657                                                             return(-1);
658                                         } else {
659                                                   if (!strcmp(ruser, user+1))
660                                                             return(-1);
661                                         }
662                               }
663                               break;
664                     default:  /* no rejections: try to match the user */
665                               if (hostok)
666                                         userok = !(strcmp(ruser,*user ? user : luser));
667                               break;
668                     }
669                     if (hostok && userok)
670                               return(0);
671           }
672           return (-1);
673 }
674 
675 /*
676  * Returns "true" if match, 0 if no match.
677  */
678 static int
__icheckhost(const struct sockaddr * raddr,socklen_t salen,const char * lhost)679 __icheckhost(const struct sockaddr *raddr, socklen_t salen, const char *lhost)
680 {
681           struct sockaddr_in sin;
682           struct sockaddr_in6 *sin6;
683           struct addrinfo hints, *res, *r;
684           int error;
685           char h1[NI_MAXHOST], h2[NI_MAXHOST];
686 
687           if (raddr->sa_family == AF_INET6) {
688                     sin6 = (struct sockaddr_in6 *)raddr;
689                     if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
690                               memset(&sin, 0, sizeof(sin));
691                               sin.sin_family = AF_INET;
692                               sin.sin_len = sizeof(struct sockaddr_in);
693                               memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
694                                      sizeof(sin.sin_addr));
695                               raddr = (struct sockaddr *)&sin;
696                               salen = sin.sin_len;
697                     }
698           }
699 
700           h1[0] = '\0';
701           if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
702                               NI_NUMERICHOST) != 0)
703                     return (0);
704 
705           /* Resolve laddr into sockaddr */
706           memset(&hints, 0, sizeof(hints));
707           hints.ai_family = raddr->sa_family;
708           hints.ai_socktype = SOCK_DGRAM;         /*XXX dummy*/
709           res = NULL;
710           error = getaddrinfo(lhost, "0", &hints, &res);
711           if (error)
712                     return (0);
713 
714           for (r = res; r ; r = r->ai_next) {
715                     h2[0] = '\0';
716                     if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
717                                         NULL, 0, NI_NUMERICHOST) != 0)
718                               continue;
719                     if (strcmp(h1, h2) == 0) {
720                               freeaddrinfo(res);
721                               return (1);
722                     }
723           }
724 
725           /* No match. */
726           freeaddrinfo(res);
727           return (0);
728 }
729