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