2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
56 #include <rpcsvc/yp_prot.h>
57 #include <rpcsvc/ypclnt.h>
59 #include <arpa/nameser.h>
60 #include "un-namespace.h"
61 #include "libc_private.h"
63 extern int innetgr( const char *, const char *, const char *, const char * );
65 #define max(a, b) ((a > b) ? a : b)
67 int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
68 int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
69 int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
71 static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
73 char paddr[NI_MAXHOST];
76 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
79 const char *locuser, *remuser, *cmd;
82 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
86 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
89 const char *locuser, *remuser, *cmd;
93 struct addrinfo hints, *res, *ai;
94 struct sockaddr_storage from;
96 sigset_t oldmask, newmask;
98 int s, aport, lport, timo, error;
102 static char canonnamebuf[MAXDNAME]; /* is it proper here? */
104 /* call rcmdsh() with specified remote shell if appropriate. */
105 if (!issetugid() && (p = getenv("RSH"))) {
106 struct servent *sp = getservbyname("shell", "tcp");
108 if (sp && sp->s_port == rport)
109 return (rcmdsh(ahost, rport, locuser, remuser,
113 /* use rsh(1) if non-root and remote port is shell. */
115 struct servent *sp = getservbyname("shell", "tcp");
117 if (sp && sp->s_port == rport)
118 return (rcmdsh(ahost, rport, locuser, remuser,
124 memset(&hints, 0, sizeof(hints));
125 hints.ai_flags = AI_CANONNAME;
126 hints.ai_family = af;
127 hints.ai_socktype = SOCK_STREAM;
128 hints.ai_protocol = 0;
129 (void)snprintf(num, sizeof(num), "%d", ntohs(rport));
130 error = getaddrinfo(*ahost, num, &hints, &res);
132 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
133 gai_strerror(error));
134 if (error == EAI_SYSTEM)
135 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
140 if (res->ai_canonname
141 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
142 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
143 *ahost = canonnamebuf;
146 for (ai = res; ai; ai = ai->ai_next)
150 sigemptyset(&newmask);
151 sigaddset(&newmask, SIGURG);
152 __libc_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
153 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
154 s = rresvport_af(&lport, ai->ai_family);
156 if (errno != EAGAIN && ai->ai_next) {
161 (void)fprintf(stderr,
162 "rcmd: socket: All ports in use\n");
164 (void)fprintf(stderr, "rcmd: socket: %s\n",
167 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
171 _fcntl(s, F_SETOWN, pid);
172 if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
175 if (errno == EADDRINUSE) {
179 if (errno == ECONNREFUSED)
181 if (ai->ai_next == NULL && (!refused || timo > 16)) {
182 (void)fprintf(stderr, "%s: %s\n",
183 *ahost, strerror(errno));
185 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
192 getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
193 sizeof(paddr), NULL, 0, NI_NUMERICHOST);
194 (void)fprintf(stderr, "connect to address %s: ",
199 if ((ai = ai->ai_next) == NULL) {
200 /* refused && timo <= 16 */
201 struct timespec time_to_sleep, time_remaining;
203 time_to_sleep.tv_sec = timo;
204 time_to_sleep.tv_nsec = 0;
205 (void)_nanosleep(&time_to_sleep, &time_remaining);
211 getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
212 sizeof(paddr), NULL, 0, NI_NUMERICHOST);
213 fprintf(stderr, "Trying %s...\n", paddr);
221 int s2 = rresvport_af(&lport, ai->ai_family), s3;
222 socklen_t len = ai->ai_addrlen;
228 (void)snprintf(num, sizeof(num), "%d", lport);
229 if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
230 (void)fprintf(stderr,
231 "rcmd: write (setting up stderr): %s\n",
237 if(nfds > FD_SETSIZE) {
238 fprintf(stderr, "rcmd: too many files\n");
247 if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
249 (void)fprintf(stderr,
250 "rcmd: select (setting up stderr): %s\n",
253 (void)fprintf(stderr,
254 "select: protocol failure in circuit setup\n");
258 s3 = _accept(s2, (struct sockaddr *)&from, &len);
259 switch (from.ss_family) {
261 aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
265 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
269 aport = 0; /* error */
273 * XXX careful for ftp bounce attacks. If discovered, shut them
274 * down and check for the real auxiliary channel to connect.
282 (void)fprintf(stderr,
283 "rcmd: accept: %s\n", strerror(errno));
288 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
289 (void)fprintf(stderr,
290 "socket: protocol failure in circuit setup.\n");
294 (void)_write(s, locuser, strlen(locuser)+1);
295 (void)_write(s, remuser, strlen(remuser)+1);
296 (void)_write(s, cmd, strlen(cmd)+1);
297 if (_read(s, &c, 1) != 1) {
298 (void)fprintf(stderr,
299 "rcmd: %s: %s\n", *ahost, strerror(errno));
303 while (_read(s, &c, 1) == 1) {
304 (void)_write(STDERR_FILENO, &c, 1);
310 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
318 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
327 return rresvport_af(port, AF_INET);
331 rresvport_af(alport, family)
335 struct sockaddr_storage ss;
338 memset(&ss, 0, sizeof(ss));
339 ss.ss_family = family;
342 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
343 sport = &((struct sockaddr_in *)&ss)->sin_port;
344 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
348 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
349 sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
350 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
354 errno = EAFNOSUPPORT;
358 s = _socket(ss.ss_family, SOCK_STREAM, 0);
361 #if 0 /* compat_exact_traditional_rresvport_semantics */
362 sin.sin_port = htons((u_short)*alport);
363 if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
365 if (errno != EADDRINUSE) {
371 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
375 *alport = (int)ntohs(*sport);
379 int __check_rhosts_file = 1;
383 ruserok(rhost, superuser, ruser, luser)
384 const char *rhost, *ruser, *luser;
387 struct addrinfo hints, *res, *r;
390 memset(&hints, 0, sizeof(hints));
391 hints.ai_family = PF_UNSPEC;
392 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
393 error = getaddrinfo(rhost, "0", &hints, &res);
397 for (r = res; r; r = r->ai_next) {
398 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
409 * New .rhosts strategy: We are passed an ip address. We spin through
410 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
411 * has ip addresses, we don't have to trust a nameserver. When it
412 * contains hostnames, we spin through the list of addresses the nameserver
413 * gives us and look for a match.
415 * Returns 0 if ok, -1 if not ok.
418 iruserok(raddr, superuser, ruser, luser)
421 const char *ruser, *luser;
423 struct sockaddr_in sin;
425 memset(&sin, 0, sizeof(sin));
426 sin.sin_family = AF_INET;
427 sin.sin_len = sizeof(struct sockaddr_in);
428 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
429 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
434 * AF independent extension of iruserok.
436 * Returns 0 if ok, -1 if not ok.
439 iruserok_sa(ra, rlen, superuser, ruser, luser)
443 const char *ruser, *luser;
451 char pbuf[MAXPATHLEN];
452 const struct sockaddr *raddr;
453 struct sockaddr_storage ss;
455 /* avoid alignment issue */
456 if (rlen > sizeof(ss))
458 memcpy(&ss, ra, rlen);
459 raddr = (struct sockaddr *)&ss;
462 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re");
465 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
471 if (first == 1 && (__check_rhosts_file || superuser)) {
473 if ((pwd = getpwnam(luser)) == NULL)
475 (void)strcpy(pbuf, pwd->pw_dir);
476 (void)strcat(pbuf, "/.rhosts");
479 * Change effective uid while opening .rhosts. If root and
480 * reading an NFS mounted file system, can't read files that
481 * are protected read/write owner only.
484 (void)seteuid(pwd->pw_uid);
485 hostf = fopen(pbuf, "re");
491 * If not a regular file, or is owned by someone other than
492 * user or root or if writeable by anyone but the owner, quit.
495 if (lstat(pbuf, &sbuf) < 0)
496 cp = ".rhosts lstat failed";
497 else if (!S_ISREG(sbuf.st_mode))
498 cp = ".rhosts not regular file";
499 else if (_fstat(fileno(hostf), &sbuf) < 0)
500 cp = ".rhosts fstat failed";
501 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
502 cp = "bad .rhosts owner";
503 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
504 cp = ".rhosts writeable by other than owner";
505 /* If there were any problems, quit. */
518 * Don't make static, used by lpd(8).
520 * Returns 0 if ok, -1 if not ok.
523 __ivaliduser(hostf, raddr, luser, ruser)
526 const char *luser, *ruser;
528 struct sockaddr_in sin;
530 memset(&sin, 0, sizeof(sin));
531 sin.sin_family = AF_INET;
532 sin.sin_len = sizeof(struct sockaddr_in);
533 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
534 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
539 * Returns 0 if ok, -1 if not ok.
544 __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
547 const char *luser, *ruser;
550 struct sockaddr *sa = NULL;
551 struct sockaddr_in *sin = NULL;
553 struct sockaddr_in6 *sin6 = NULL;
555 struct sockaddr_storage ss;
557 memset(&ss, 0, sizeof(ss));
560 if (len != sizeof(sin->sin_addr))
562 sin = (struct sockaddr_in *)&ss;
563 sin->sin_family = AF_INET;
564 sin->sin_len = sizeof(struct sockaddr_in);
565 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
569 if (len != sizeof(sin6->sin6_addr))
571 /* you will lose scope info */
572 sin6 = (struct sockaddr_in6 *)&ss;
573 sin6->sin6_family = AF_INET6;
574 sin6->sin6_len = sizeof(struct sockaddr_in6);
575 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
582 sa = (struct sockaddr *)&ss;
583 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
587 __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
589 const struct sockaddr *raddr;
591 const char *luser, *ruser;
595 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
596 char hname[MAXHOSTNAMELEN];
597 /* Presumed guilty until proven innocent. */
598 int userok = 0, hostok = 0;
602 if (yp_get_default_domain(&ypdomain))
605 #define ypdomain NULL
607 /* We need to get the damn hostname back for netgroup matching. */
608 if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
612 while (fgets(buf, sizeof(buf), hostf)) {
614 /* Skip lines that are too long. */
615 if (strchr(p, '\n') == NULL) {
616 while ((ch = getc(hostf)) != '\n' && ch != EOF);
619 if (*p == '\n' || *p == '#') {
623 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
624 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
627 if (*p == ' ' || *p == '\t') {
629 while (*p == ' ' || *p == '\t')
632 while (*p != '\n' && *p != ' ' &&
633 *p != '\t' && *p != '\0')
639 * Do +/- and +@/-@ checking. This looks really nasty,
640 * but it matches SunOS's behavior so far as I can tell.
644 if (!buf[1]) { /* '+' matches all hosts */
648 if (buf[1] == '@') /* match a host by netgroup */
649 hostok = hname[0] != '\0' &&
650 innetgr(&buf[2], hname, NULL, ypdomain);
651 else /* match a host by addr */
652 hostok = __icheckhost(raddr, salen,
655 case '-': /* reject '-' hosts and all their users */
657 if (hname[0] == '\0' ||
658 innetgr(&buf[2], hname, NULL, ypdomain))
661 if (__icheckhost(raddr, salen,
666 default: /* if no '+' or '-', do a simple match */
667 hostok = __icheckhost(raddr, salen, buf);
672 if (!*(user+1)) { /* '+' matches all users */
676 if (*(user+1) == '@') /* match a user by netgroup */
677 userok = innetgr(user+2, NULL, ruser, ypdomain);
678 else /* match a user by direct specification */
679 userok = !(strcmp(ruser, user+1));
681 case '-': /* if we matched a hostname, */
682 if (hostok) { /* check for user field rejections */
685 if (*(user+1) == '@') {
686 if (innetgr(user+2, NULL,
690 if (!strcmp(ruser, user+1))
695 default: /* no rejections: try to match the user */
697 userok = !(strcmp(ruser,*user ? user : luser));
700 if (hostok && userok)
707 * Returns "true" if match, 0 if no match.
710 __icheckhost(raddr, salen, lhost)
711 const struct sockaddr *raddr;
715 struct sockaddr_in sin;
716 struct sockaddr_in6 *sin6;
717 struct addrinfo hints, *res, *r;
719 char h1[NI_MAXHOST], h2[NI_MAXHOST];
721 if (raddr->sa_family == AF_INET6) {
722 sin6 = (struct sockaddr_in6 *)raddr;
723 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
724 memset(&sin, 0, sizeof(sin));
725 sin.sin_family = AF_INET;
726 sin.sin_len = sizeof(struct sockaddr_in);
727 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
728 sizeof(sin.sin_addr));
729 raddr = (struct sockaddr *)&sin;
735 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
736 NI_NUMERICHOST) != 0)
739 /* Resolve laddr into sockaddr */
740 memset(&hints, 0, sizeof(hints));
741 hints.ai_family = raddr->sa_family;
742 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/
744 error = getaddrinfo(lhost, "0", &hints, &res);
748 for (r = res; r ; r = r->ai_next) {
750 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
751 NULL, 0, NI_NUMERICHOST) != 0)
753 if (strcmp(h1, h2) == 0) {