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 * 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.
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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
60 #include <rpcsvc/yp_prot.h>
61 #include <rpcsvc/ypclnt.h>
63 #include <arpa/nameser.h>
64 #include "un-namespace.h"
66 /* wrapper for KAME-special getnameinfo() */
67 #ifndef NI_WITHSCOPEID
68 #define NI_WITHSCOPEID 0
71 extern int innetgr( const char *, const char *, const char *, const char * );
73 #define max(a, b) ((a > b) ? a : b)
75 int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
76 int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
77 int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
79 static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
81 char paddr[NI_MAXHOST];
84 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
87 const char *locuser, *remuser, *cmd;
90 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
94 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
97 const char *locuser, *remuser, *cmd;
101 struct addrinfo hints, *res, *ai;
102 struct sockaddr_storage from;
104 sigset_t oldmask, newmask;
106 int s, aport, lport, timo, error;
110 static char canonnamebuf[MAXDNAME]; /* is it proper here? */
112 /* call rcmdsh() with specified remote shell if appropriate. */
113 if (!issetugid() && (p = getenv("RSH"))) {
114 struct servent *sp = getservbyname("shell", "tcp");
116 if (sp && sp->s_port == rport)
117 return (rcmdsh(ahost, rport, locuser, remuser,
121 /* use rsh(1) if non-root and remote port is shell. */
123 struct servent *sp = getservbyname("shell", "tcp");
125 if (sp && sp->s_port == rport)
126 return (rcmdsh(ahost, rport, locuser, remuser,
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);
140 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
141 gai_strerror(error));
142 if (error == EAI_SYSTEM)
143 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
148 if (res->ai_canonname
149 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
150 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
151 *ahost = canonnamebuf;
154 for (ai = res; ai; ai = ai->ai_next)
158 sigemptyset(&newmask);
159 sigaddset(&newmask, SIGURG);
160 _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
161 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
162 s = rresvport_af(&lport, ai->ai_family);
164 if (errno != EAGAIN && ai->ai_next) {
169 (void)fprintf(stderr,
170 "rcmd: socket: All ports in use\n");
172 (void)fprintf(stderr, "rcmd: socket: %s\n",
175 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
179 _fcntl(s, F_SETOWN, pid);
180 if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
183 if (errno == EADDRINUSE) {
187 if (errno == ECONNREFUSED)
189 if (ai->ai_next == NULL && (!refused || timo > 16)) {
190 (void)fprintf(stderr, "%s: %s\n",
191 *ahost, strerror(errno));
193 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
200 getnameinfo(ai->ai_addr, ai->ai_addrlen,
201 paddr, sizeof(paddr),
203 NI_NUMERICHOST|NI_WITHSCOPEID);
204 (void)fprintf(stderr, "connect to address %s: ",
209 if ((ai = ai->ai_next) == NULL) {
210 /* refused && timo <= 16 */
211 struct timespec time_to_sleep, time_remaining;
213 time_to_sleep.tv_sec = timo;
214 time_to_sleep.tv_nsec = 0;
215 (void)_nanosleep(&time_to_sleep, &time_remaining);
221 getnameinfo(ai->ai_addr, ai->ai_addrlen,
222 paddr, sizeof(paddr),
224 NI_NUMERICHOST|NI_WITHSCOPEID);
225 fprintf(stderr, "Trying %s...\n", paddr);
234 int s2 = rresvport_af(&lport, ai->ai_family), s3;
235 int len = ai->ai_addrlen;
241 (void)snprintf(num, sizeof(num), "%d", lport);
242 if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
243 (void)fprintf(stderr,
244 "rcmd: write (setting up stderr): %s\n",
250 if(nfds > FD_SETSIZE) {
251 fprintf(stderr, "rcmd: too many files\n");
260 if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
262 (void)fprintf(stderr,
263 "rcmd: select (setting up stderr): %s\n",
266 (void)fprintf(stderr,
267 "select: protocol failure in circuit setup\n");
271 s3 = _accept(s2, (struct sockaddr *)&from, &len);
272 switch (from.ss_family) {
274 aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
278 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
282 aport = 0; /* error */
286 * XXX careful for ftp bounce attacks. If discovered, shut them
287 * down and check for the real auxiliary channel to connect.
295 (void)fprintf(stderr,
296 "rcmd: accept: %s\n", strerror(errno));
301 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
302 (void)fprintf(stderr,
303 "socket: protocol failure in circuit setup.\n");
307 (void)_write(s, locuser, strlen(locuser)+1);
308 (void)_write(s, remuser, strlen(remuser)+1);
309 (void)_write(s, cmd, strlen(cmd)+1);
310 if (_read(s, &c, 1) != 1) {
311 (void)fprintf(stderr,
312 "rcmd: %s: %s\n", *ahost, strerror(errno));
316 while (_read(s, &c, 1) == 1) {
317 (void)_write(STDERR_FILENO, &c, 1);
323 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
331 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
340 return rresvport_af(port, AF_INET);
344 rresvport_af(alport, family)
348 struct sockaddr_storage ss;
351 memset(&ss, 0, sizeof(ss));
352 ss.ss_family = family;
355 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
356 sport = &((struct sockaddr_in *)&ss)->sin_port;
357 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
361 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
362 sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
363 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
367 errno = EAFNOSUPPORT;
371 s = _socket(ss.ss_family, SOCK_STREAM, 0);
374 #if 0 /* compat_exact_traditional_rresvport_semantics */
375 sin.sin_port = htons((u_short)*alport);
376 if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
378 if (errno != EADDRINUSE) {
384 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
388 *alport = (int)ntohs(*sport);
392 int __check_rhosts_file = 1;
396 ruserok(rhost, superuser, ruser, luser)
397 const char *rhost, *ruser, *luser;
400 struct addrinfo hints, *res, *r;
403 memset(&hints, 0, sizeof(hints));
404 hints.ai_family = PF_UNSPEC;
405 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
406 error = getaddrinfo(rhost, "0", &hints, &res);
410 for (r = res; r; r = r->ai_next) {
411 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
422 * New .rhosts strategy: We are passed an ip address. We spin through
423 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
424 * has ip addresses, we don't have to trust a nameserver. When it
425 * contains hostnames, we spin through the list of addresses the nameserver
426 * gives us and look for a match.
428 * Returns 0 if ok, -1 if not ok.
431 iruserok(raddr, superuser, ruser, luser)
434 const char *ruser, *luser;
436 struct sockaddr_in sin;
438 memset(&sin, 0, sizeof(sin));
439 sin.sin_family = AF_INET;
440 sin.sin_len = sizeof(struct sockaddr_in);
441 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
442 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
447 * AF independent extension of iruserok.
449 * Returns 0 if ok, -1 if not ok.
452 iruserok_sa(ra, rlen, superuser, ruser, luser)
456 const char *ruser, *luser;
464 char pbuf[MAXPATHLEN];
465 const struct sockaddr *raddr;
466 struct sockaddr_storage ss;
468 /* avoid alignment issue */
469 if (rlen > sizeof(ss))
471 memcpy(&ss, ra, rlen);
472 raddr = (struct sockaddr *)&ss;
475 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
478 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
484 if (first == 1 && (__check_rhosts_file || superuser)) {
486 if ((pwd = getpwnam(luser)) == NULL)
488 (void)strcpy(pbuf, pwd->pw_dir);
489 (void)strcat(pbuf, "/.rhosts");
492 * Change effective uid while opening .rhosts. If root and
493 * reading an NFS mounted file system, can't read files that
494 * are protected read/write owner only.
497 (void)seteuid(pwd->pw_uid);
498 hostf = fopen(pbuf, "r");
504 * If not a regular file, or is owned by someone other than
505 * user or root or if writeable by anyone but the owner, quit.
508 if (lstat(pbuf, &sbuf) < 0)
509 cp = ".rhosts lstat failed";
510 else if (!S_ISREG(sbuf.st_mode))
511 cp = ".rhosts not regular file";
512 else if (_fstat(fileno(hostf), &sbuf) < 0)
513 cp = ".rhosts fstat failed";
514 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
515 cp = "bad .rhosts owner";
516 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
517 cp = ".rhosts writeable by other than owner";
518 /* If there were any problems, quit. */
531 * Don't make static, used by lpd(8).
533 * Returns 0 if ok, -1 if not ok.
536 __ivaliduser(hostf, raddr, luser, ruser)
539 const char *luser, *ruser;
541 struct sockaddr_in sin;
543 memset(&sin, 0, sizeof(sin));
544 sin.sin_family = AF_INET;
545 sin.sin_len = sizeof(struct sockaddr_in);
546 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
547 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
552 * Returns 0 if ok, -1 if not ok.
557 __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
560 const char *luser, *ruser;
563 struct sockaddr *sa = NULL;
564 struct sockaddr_in *sin = NULL;
566 struct sockaddr_in6 *sin6 = NULL;
568 struct sockaddr_storage ss;
570 memset(&ss, 0, sizeof(ss));
573 if (len != sizeof(sin->sin_addr))
575 sin = (struct sockaddr_in *)&ss;
576 sin->sin_family = AF_INET;
577 sin->sin_len = sizeof(struct sockaddr_in);
578 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
582 if (len != sizeof(sin6->sin6_addr))
584 /* you will lose scope info */
585 sin6 = (struct sockaddr_in6 *)&ss;
586 sin6->sin6_family = AF_INET6;
587 sin6->sin6_len = sizeof(struct sockaddr_in6);
588 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
595 sa = (struct sockaddr *)&ss;
596 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
600 __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
602 const struct sockaddr *raddr;
604 const char *luser, *ruser;
608 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
609 char hname[MAXHOSTNAMELEN];
610 /* Presumed guilty until proven innocent. */
611 int userok = 0, hostok = 0;
616 if (yp_get_default_domain(&ypdomain))
619 #define ypdomain NULL
621 /* We need to get the damn hostname back for netgroup matching. */
622 if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
626 while (fgets(buf, sizeof(buf), hostf)) {
628 /* Skip lines that are too long. */
629 if (strchr(p, '\n') == NULL) {
630 while ((ch = getc(hostf)) != '\n' && ch != EOF);
633 if (*p == '\n' || *p == '#') {
637 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
638 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
641 if (*p == ' ' || *p == '\t') {
643 while (*p == ' ' || *p == '\t')
646 while (*p != '\n' && *p != ' ' &&
647 *p != '\t' && *p != '\0')
653 * Do +/- and +@/-@ checking. This looks really nasty,
654 * but it matches SunOS's behavior so far as I can tell.
658 if (!buf[1]) { /* '+' matches all hosts */
662 if (buf[1] == '@') /* match a host by netgroup */
663 hostok = hname[0] != '\0' &&
664 innetgr(&buf[2], hname, NULL, ypdomain);
665 else /* match a host by addr */
666 hostok = __icheckhost(raddr, salen,
669 case '-': /* reject '-' hosts and all their users */
671 if (hname[0] == '\0' ||
672 innetgr(&buf[2], hname, NULL, ypdomain))
675 if (__icheckhost(raddr, salen,
680 default: /* if no '+' or '-', do a simple match */
681 hostok = __icheckhost(raddr, salen, buf);
686 if (!*(user+1)) { /* '+' matches all users */
690 if (*(user+1) == '@') /* match a user by netgroup */
691 userok = innetgr(user+2, NULL, ruser, ypdomain);
692 else /* match a user by direct specification */
693 userok = !(strcmp(ruser, user+1));
695 case '-': /* if we matched a hostname, */
696 if (hostok) { /* check for user field rejections */
699 if (*(user+1) == '@') {
700 if (innetgr(user+2, NULL,
704 if (!strcmp(ruser, user+1))
709 default: /* no rejections: try to match the user */
711 userok = !(strcmp(ruser,*user ? user : luser));
714 if (hostok && userok)
721 * Returns "true" if match, 0 if no match.
723 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
727 __icheckhost(raddr, salen, lhost)
728 const struct sockaddr *raddr;
732 struct sockaddr_in sin;
733 struct sockaddr_in6 *sin6;
734 struct addrinfo hints, *res, *r;
736 char h1[NI_MAXHOST], h2[NI_MAXHOST];
738 if (raddr->sa_family == AF_INET6) {
739 sin6 = (struct sockaddr_in6 *)raddr;
740 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
741 memset(&sin, 0, sizeof(sin));
742 sin.sin_family = AF_INET;
743 sin.sin_len = sizeof(struct sockaddr_in);
744 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
745 sizeof(sin.sin_addr));
746 raddr = (struct sockaddr *)&sin;
752 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
753 NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
756 /* Resolve laddr into sockaddr */
757 memset(&hints, 0, sizeof(hints));
758 hints.ai_family = raddr->sa_family;
759 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/
761 error = getaddrinfo(lhost, "0", &hints, &res);
765 for (r = res; r ; r = r->ai_next) {
767 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
768 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
770 if (strcmp(h1, h2) == 0) {