2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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 <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "namespace.h"
39 #include <sys/param.h>
40 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
61 #include <arpa/nameser.h>
62 #include "un-namespace.h"
63 #include "libc_private.h"
65 extern int innetgr( const char *, const char *, const char *, const char * );
67 #define max(a, b) ((a > b) ? a : b)
69 int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
70 int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int);
71 int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *,
73 static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
75 char paddr[NI_MAXHOST];
78 rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
79 const char *cmd, int *fd2p)
81 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
85 rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser,
86 const char *cmd, int *fd2p, int af)
88 struct addrinfo hints, *res, *ai;
89 struct sockaddr_storage from;
91 sigset_t oldmask, newmask;
93 int s, aport, lport, timo, error;
97 static char canonnamebuf[MAXDNAME]; /* is it proper here? */
99 /* call rcmdsh() with specified remote shell if appropriate. */
100 if (!issetugid() && (p = getenv("RSH"))) {
101 struct servent *sp = getservbyname("shell", "tcp");
103 if (sp && sp->s_port == rport)
104 return (rcmdsh(ahost, rport, locuser, remuser,
108 /* use rsh(1) if non-root and remote port is shell. */
110 struct servent *sp = getservbyname("shell", "tcp");
112 if (sp && sp->s_port == rport)
113 return (rcmdsh(ahost, rport, locuser, remuser,
119 memset(&hints, 0, sizeof(hints));
120 hints.ai_flags = AI_CANONNAME;
121 hints.ai_family = af;
122 hints.ai_socktype = SOCK_STREAM;
123 hints.ai_protocol = 0;
124 (void)snprintf(num, sizeof(num), "%d", ntohs(rport));
125 error = getaddrinfo(*ahost, num, &hints, &res);
127 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
128 gai_strerror(error));
129 if (error == EAI_SYSTEM)
130 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
135 if (res->ai_canonname
136 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
137 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
138 *ahost = canonnamebuf;
141 for (ai = res; ai; ai = ai->ai_next)
145 sigemptyset(&newmask);
146 sigaddset(&newmask, SIGURG);
147 __libc_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask);
148 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
149 s = rresvport_af(&lport, ai->ai_family);
151 if (errno != EAGAIN && ai->ai_next) {
156 (void)fprintf(stderr,
157 "rcmd: socket: All ports in use\n");
159 (void)fprintf(stderr, "rcmd: socket: %s\n",
162 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
166 _fcntl(s, F_SETOWN, pid);
167 if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
170 if (errno == EADDRINUSE) {
174 if (errno == ECONNREFUSED)
176 if (ai->ai_next == NULL && (!refused || timo > 16)) {
177 (void)fprintf(stderr, "%s: %s\n",
178 *ahost, strerror(errno));
180 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask,
187 getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
188 sizeof(paddr), NULL, 0, NI_NUMERICHOST);
189 (void)fprintf(stderr, "connect to address %s: ",
194 if ((ai = ai->ai_next) == NULL) {
195 /* refused && timo <= 16 */
196 struct timespec time_to_sleep, time_remaining;
198 time_to_sleep.tv_sec = timo;
199 time_to_sleep.tv_nsec = 0;
200 (void)_nanosleep(&time_to_sleep, &time_remaining);
206 getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr,
207 sizeof(paddr), NULL, 0, NI_NUMERICHOST);
208 fprintf(stderr, "Trying %s...\n", paddr);
216 int s2 = rresvport_af(&lport, ai->ai_family), s3;
217 socklen_t len = ai->ai_addrlen;
223 (void)snprintf(num, sizeof(num), "%d", lport);
224 if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
225 (void)fprintf(stderr,
226 "rcmd: write (setting up stderr): %s\n",
232 if(nfds > FD_SETSIZE) {
233 fprintf(stderr, "rcmd: too many files\n");
242 if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
244 (void)fprintf(stderr,
245 "rcmd: select (setting up stderr): %s\n",
248 (void)fprintf(stderr,
249 "select: protocol failure in circuit setup\n");
253 s3 = _accept(s2, (struct sockaddr *)&from, &len);
254 switch (from.ss_family) {
256 aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
260 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
264 aport = 0; /* error */
268 * XXX careful for ftp bounce attacks. If discovered, shut them
269 * down and check for the real auxiliary channel to connect.
277 (void)fprintf(stderr,
278 "rcmd: accept: %s\n", strerror(errno));
283 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
284 (void)fprintf(stderr,
285 "socket: protocol failure in circuit setup.\n");
289 (void)_write(s, locuser, strlen(locuser)+1);
290 (void)_write(s, remuser, strlen(remuser)+1);
291 (void)_write(s, cmd, strlen(cmd)+1);
292 if (_read(s, &c, 1) != 1) {
293 (void)fprintf(stderr,
294 "rcmd: %s: %s\n", *ahost, strerror(errno));
298 while (_read(s, &c, 1) == 1) {
299 (void)_write(STDERR_FILENO, &c, 1);
305 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
313 __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL);
321 return rresvport_af(port, AF_INET);
325 rresvport_af(int *alport, int family)
328 struct sockaddr_storage ss;
331 memset(&ss, 0, sizeof(ss));
332 ss.ss_family = family;
335 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
336 sport = &((struct sockaddr_in *)&ss)->sin_port;
337 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
341 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
342 sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
343 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
347 errno = EAFNOSUPPORT;
351 s = _socket(ss.ss_family, SOCK_STREAM, 0);
354 #if 0 /* compat_exact_traditional_rresvport_semantics */
355 sin.sin_port = htons((u_short)*alport);
356 if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
358 if (errno != EADDRINUSE) {
364 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
368 *alport = (int)ntohs(*sport);
372 int __check_rhosts_file = 1;
376 ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
378 struct addrinfo hints, *res, *r;
381 memset(&hints, 0, sizeof(hints));
382 hints.ai_family = PF_UNSPEC;
383 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
384 error = getaddrinfo(rhost, "0", &hints, &res);
388 for (r = res; r; r = r->ai_next) {
389 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
400 * New .rhosts strategy: We are passed an ip address. We spin through
401 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
402 * has ip addresses, we don't have to trust a nameserver. When it
403 * contains hostnames, we spin through the list of addresses the nameserver
404 * gives us and look for a match.
406 * Returns 0 if ok, -1 if not ok.
409 iruserok(unsigned long raddr, int superuser, const char *ruser, const char *luser)
411 struct sockaddr_in sin;
413 memset(&sin, 0, sizeof(sin));
414 sin.sin_family = AF_INET;
415 sin.sin_len = sizeof(struct sockaddr_in);
416 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
417 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
422 * AF independent extension of iruserok.
424 * Returns 0 if ok, -1 if not ok.
427 iruserok_sa(const void *ra, int rlen, int superuser, const char *ruser,
436 char pbuf[MAXPATHLEN];
437 const struct sockaddr *raddr;
438 struct sockaddr_storage ss;
440 /* avoid alignment issue */
441 if (rlen <= 0 || rlen > sizeof(ss))
443 memcpy(&ss, ra, rlen);
444 raddr = (struct sockaddr *)&ss;
447 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re");
450 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
456 if (first == 1 && (__check_rhosts_file || superuser)) {
458 if ((pwd = getpwnam(luser)) == NULL)
460 (void)strlcpy(pbuf, pwd->pw_dir, sizeof(pbuf));
461 (void)strlcat(pbuf, "/.rhosts", sizeof(pbuf));
464 * Change effective uid while opening .rhosts. If root and
465 * reading an NFS mounted file system, can't read files that
466 * are protected read/write owner only.
469 (void)seteuid(pwd->pw_uid);
470 hostf = fopen(pbuf, "re");
476 * If not a regular file, or is owned by someone other than
477 * user or root or if writeable by anyone but the owner, quit.
480 if (lstat(pbuf, &sbuf) < 0)
481 cp = ".rhosts lstat failed";
482 else if (!S_ISREG(sbuf.st_mode))
483 cp = ".rhosts not regular file";
484 else if (_fstat(fileno(hostf), &sbuf) < 0)
485 cp = ".rhosts fstat failed";
486 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
487 cp = "bad .rhosts owner";
488 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
489 cp = ".rhosts writeable by other than owner";
490 /* If there were any problems, quit. */
503 * Don't make static, used by lpd(8).
505 * Returns 0 if ok, -1 if not ok.
508 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
510 struct sockaddr_in sin;
512 memset(&sin, 0, sizeof(sin));
513 sin.sin_family = AF_INET;
514 sin.sin_len = sizeof(struct sockaddr_in);
515 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
516 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
521 * Returns 0 if ok, -1 if not ok.
526 __ivaliduser_af(FILE *hostf, const void *raddr, const char *luser,
527 const char *ruser, int af, int len)
529 struct sockaddr *sa = NULL;
530 struct sockaddr_in *sin = NULL;
532 struct sockaddr_in6 *sin6 = NULL;
534 struct sockaddr_storage ss;
536 memset(&ss, 0, sizeof(ss));
539 if (len != sizeof(sin->sin_addr))
541 sin = (struct sockaddr_in *)&ss;
542 sin->sin_family = AF_INET;
543 sin->sin_len = sizeof(struct sockaddr_in);
544 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
548 if (len != sizeof(sin6->sin6_addr))
550 /* you will lose scope info */
551 sin6 = (struct sockaddr_in6 *)&ss;
552 sin6->sin6_family = AF_INET6;
553 sin6->sin6_len = sizeof(struct sockaddr_in6);
554 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
561 sa = (struct sockaddr *)&ss;
562 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
566 __ivaliduser_sa(FILE *hostf, const struct sockaddr *raddr, socklen_t salen,
567 const char *luser, const char *ruser)
571 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
572 char hname[MAXHOSTNAMELEN];
573 /* Presumed guilty until proven innocent. */
574 int userok = 0, hostok = 0;
578 if (yp_get_default_domain(&ypdomain))
581 #define ypdomain NULL
583 /* We need to get the damn hostname back for netgroup matching. */
584 if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
588 while (fgets(buf, sizeof(buf), hostf)) {
590 /* Skip lines that are too long. */
591 if (strchr(p, '\n') == NULL) {
592 while ((ch = getc(hostf)) != '\n' && ch != EOF);
595 if (*p == '\n' || *p == '#') {
599 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
600 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
603 if (*p == ' ' || *p == '\t') {
605 while (*p == ' ' || *p == '\t')
608 while (*p != '\n' && *p != ' ' &&
609 *p != '\t' && *p != '\0')
615 * Do +/- and +@/-@ checking. This looks really nasty,
616 * but it matches SunOS's behavior so far as I can tell.
620 if (!buf[1]) { /* '+' matches all hosts */
624 if (buf[1] == '@') /* match a host by netgroup */
625 hostok = hname[0] != '\0' &&
626 innetgr(&buf[2], hname, NULL, ypdomain);
627 else /* match a host by addr */
628 hostok = __icheckhost(raddr, salen,
631 case '-': /* reject '-' hosts and all their users */
633 if (hname[0] == '\0' ||
634 innetgr(&buf[2], hname, NULL, ypdomain))
637 if (__icheckhost(raddr, salen,
642 default: /* if no '+' or '-', do a simple match */
643 hostok = __icheckhost(raddr, salen, buf);
648 if (!*(user+1)) { /* '+' matches all users */
652 if (*(user+1) == '@') /* match a user by netgroup */
653 userok = innetgr(user+2, NULL, ruser, ypdomain);
654 else /* match a user by direct specification */
655 userok = !(strcmp(ruser, user+1));
657 case '-': /* if we matched a hostname, */
658 if (hostok) { /* check for user field rejections */
661 if (*(user+1) == '@') {
662 if (innetgr(user+2, NULL,
666 if (!strcmp(ruser, user+1))
671 default: /* no rejections: try to match the user */
673 userok = !(strcmp(ruser,*user ? user : luser));
676 if (hostok && userok)
683 * Returns "true" if match, 0 if no match.
686 __icheckhost(const struct sockaddr *raddr, socklen_t salen, const char *lhost)
688 struct sockaddr_in sin;
689 struct sockaddr_in6 *sin6;
690 struct addrinfo hints, *res, *r;
692 char h1[NI_MAXHOST], h2[NI_MAXHOST];
694 if (raddr->sa_family == AF_INET6) {
695 sin6 = (struct sockaddr_in6 *)raddr;
696 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
697 memset(&sin, 0, sizeof(sin));
698 sin.sin_family = AF_INET;
699 sin.sin_len = sizeof(struct sockaddr_in);
700 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
701 sizeof(sin.sin_addr));
702 raddr = (struct sockaddr *)&sin;
708 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
709 NI_NUMERICHOST) != 0)
712 /* Resolve laddr into sockaddr */
713 memset(&hints, 0, sizeof(hints));
714 hints.ai_family = raddr->sa_family;
715 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/
717 error = getaddrinfo(lhost, "0", &hints, &res);
721 for (r = res; r ; r = r->ai_next) {
723 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
724 NULL, 0, NI_NUMERICHOST) != 0)
726 if (strcmp(h1, h2) == 0) {