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
33 * $FreeBSD: src/lib/libc/net/rcmd.c,v 1.23.2.7 2002/08/26 16:17:49 jdp Exp $
34 * $DragonFly: src/lib/libc/net/rcmd.c,v 1.4 2004/10/25 19:38:01 drhodus Exp $
36 * @(#)rcmd.c 8.3 (Berkeley) 3/26/94
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>
63 /* wrapper for KAME-special getnameinfo() */
64 #ifndef NI_WITHSCOPEID
65 #define NI_WITHSCOPEID 0
68 extern int innetgr ( const char *, const char *, const char *, const char * );
70 #define max(a, b) ((a > b) ? a : b)
72 int __ivaliduser (FILE *, u_int32_t, const char *, const char *);
73 int __ivaliduser_af (FILE *,const void *, const char *, const char *,
75 int __ivaliduser_sa (FILE *, const struct sockaddr *, socklen_t,
76 const char *,const char *);
77 static int __icheckhost (const struct sockaddr *, socklen_t,
80 char paddr[NI_MAXHOST];
83 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
86 const char *locuser, *remuser, *cmd;
89 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
93 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
96 const char *locuser, *remuser, *cmd;
100 struct addrinfo hints, *res, *ai;
101 struct sockaddr_storage from;
105 int s, aport, lport, timo, error;
109 static char canonnamebuf[MAXDNAME]; /* is it proper here? */
111 /* call rcmdsh() with specified remote shell if appropriate. */
112 if (!issetugid() && (p = getenv("RSH"))) {
113 struct servent *sp = getservbyname("shell", "tcp");
115 if (sp && sp->s_port == rport)
116 return (rcmdsh(ahost, rport, locuser, remuser,
120 /* use rsh(1) if non-root and remote port is shell. */
122 struct servent *sp = getservbyname("shell", "tcp");
124 if (sp && sp->s_port == rport)
125 return (rcmdsh(ahost, rport, locuser, remuser,
131 memset(&hints, 0, sizeof(hints));
132 hints.ai_flags = AI_CANONNAME;
133 hints.ai_family = af;
134 hints.ai_socktype = SOCK_STREAM;
135 hints.ai_protocol = 0;
136 (void)snprintf(num, sizeof(num), "%d", ntohs(rport));
137 error = getaddrinfo(*ahost, num, &hints, &res);
139 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
140 gai_strerror(error));
141 if (error == EAI_SYSTEM)
142 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
147 if (res->ai_canonname
148 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) {
149 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf));
150 *ahost = canonnamebuf;
153 for (ai = res; ai; ai = ai->ai_next)
157 oldmask = sigblock(sigmask(SIGURG));
158 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
159 s = rresvport_af(&lport, ai->ai_family);
161 if (errno != EAGAIN && ai->ai_next) {
166 (void)fprintf(stderr,
167 "rcmd: socket: All ports in use\n");
169 (void)fprintf(stderr, "rcmd: socket: %s\n",
175 _fcntl(s, F_SETOWN, pid);
176 if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
179 if (errno == EADDRINUSE) {
183 if (errno == ECONNREFUSED)
185 if (ai->ai_next == NULL && (!refused || timo > 16)) {
186 (void)fprintf(stderr, "%s: %s\n",
187 *ahost, strerror(errno));
195 getnameinfo(ai->ai_addr, ai->ai_addrlen,
196 paddr, sizeof(paddr),
198 NI_NUMERICHOST|NI_WITHSCOPEID);
199 (void)fprintf(stderr, "connect to address %s: ",
204 if ((ai = ai->ai_next) == NULL) {
205 /* refused && timo <= 16 */
206 struct timespec time_to_sleep, time_remaining;
208 time_to_sleep.tv_sec = timo;
209 time_to_sleep.tv_nsec = 0;
210 (void)_nanosleep(&time_to_sleep, &time_remaining);
216 getnameinfo(ai->ai_addr, ai->ai_addrlen,
217 paddr, sizeof(paddr),
219 NI_NUMERICHOST|NI_WITHSCOPEID);
220 fprintf(stderr, "Trying %s...\n", paddr);
229 int s2 = rresvport_af(&lport, ai->ai_family), s3;
230 int len = ai->ai_addrlen;
236 (void)snprintf(num, sizeof(num), "%d", lport);
237 if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
238 (void)fprintf(stderr,
239 "rcmd: write (setting up stderr): %s\n",
245 if(nfds > FD_SETSIZE) {
246 fprintf(stderr, "rcmd: too many files\n");
255 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
257 (void)fprintf(stderr,
258 "rcmd: select (setting up stderr): %s\n",
261 (void)fprintf(stderr,
262 "select: protocol failure in circuit setup\n");
266 s3 = accept(s2, (struct sockaddr *)&from, &len);
267 switch (from.ss_family) {
269 aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
273 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
277 aport = 0; /* error */
281 * XXX careful for ftp bounce attacks. If discovered, shut them
282 * down and check for the real auxiliary channel to connect.
290 (void)fprintf(stderr,
291 "rcmd: accept: %s\n", strerror(errno));
296 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
297 (void)fprintf(stderr,
298 "socket: protocol failure in circuit setup.\n");
302 (void)_write(s, locuser, strlen(locuser)+1);
303 (void)_write(s, remuser, strlen(remuser)+1);
304 (void)_write(s, cmd, strlen(cmd)+1);
305 if (_read(s, &c, 1) != 1) {
306 (void)fprintf(stderr,
307 "rcmd: %s: %s\n", *ahost, strerror(errno));
311 while (_read(s, &c, 1) == 1) {
312 (void)_write(STDERR_FILENO, &c, 1);
335 return rresvport_af(port, AF_INET);
339 rresvport_af(alport, family)
343 struct sockaddr_storage ss;
346 memset(&ss, 0, sizeof(ss));
347 ss.ss_family = family;
350 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
351 sport = &((struct sockaddr_in *)&ss)->sin_port;
352 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
356 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
357 sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
358 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
362 errno = EAFNOSUPPORT;
366 s = socket(ss.ss_family, SOCK_STREAM, 0);
369 #if 0 /* compat_exact_traditional_rresvport_semantics */
370 sin.sin_port = htons((u_short)*alport);
371 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
373 if (errno != EADDRINUSE) {
379 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
383 *alport = (int)ntohs(*sport);
387 int __check_rhosts_file = 1;
391 ruserok(rhost, superuser, ruser, luser)
392 const char *rhost, *ruser, *luser;
395 struct addrinfo hints, *res, *r;
398 memset(&hints, 0, sizeof(hints));
399 hints.ai_family = PF_UNSPEC;
400 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
401 error = getaddrinfo(rhost, "0", &hints, &res);
405 for (r = res; r; r = r->ai_next) {
406 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
417 * New .rhosts strategy: We are passed an ip address. We spin through
418 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
419 * has ip addresses, we don't have to trust a nameserver. When it
420 * contains hostnames, we spin through the list of addresses the nameserver
421 * gives us and look for a match.
423 * Returns 0 if ok, -1 if not ok.
426 iruserok(raddr, superuser, ruser, luser)
429 const char *ruser, *luser;
431 struct sockaddr_in sin;
433 memset(&sin, 0, sizeof(sin));
434 sin.sin_family = AF_INET;
435 sin.sin_len = sizeof(struct sockaddr_in);
436 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
437 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser,
442 * AF independent extension of iruserok.
444 * Returns 0 if ok, -1 if not ok.
447 iruserok_sa(ra, rlen, superuser, ruser, luser)
451 const char *ruser, *luser;
459 char pbuf[MAXPATHLEN];
460 const struct sockaddr *raddr;
461 struct sockaddr_storage ss;
463 /* avoid alignment issue */
464 if (rlen > sizeof(ss))
466 memcpy(&ss, ra, rlen);
467 raddr = (struct sockaddr *)&ss;
470 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
473 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) {
479 if (first == 1 && (__check_rhosts_file || superuser)) {
481 if ((pwd = getpwnam(luser)) == NULL)
483 (void)strcpy(pbuf, pwd->pw_dir);
484 (void)strcat(pbuf, "/.rhosts");
487 * Change effective uid while opening .rhosts. If root and
488 * reading an NFS mounted file system, can't read files that
489 * are protected read/write owner only.
492 (void)seteuid(pwd->pw_uid);
493 hostf = fopen(pbuf, "r");
499 * If not a regular file, or is owned by someone other than
500 * user or root or if writeable by anyone but the owner, quit.
503 if (lstat(pbuf, &sbuf) < 0)
504 cp = ".rhosts lstat failed";
505 else if (!S_ISREG(sbuf.st_mode))
506 cp = ".rhosts not regular file";
507 else if (fstat(fileno(hostf), &sbuf) < 0)
508 cp = ".rhosts fstat failed";
509 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
510 cp = "bad .rhosts owner";
511 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
512 cp = ".rhosts writeable by other than owner";
513 /* If there were any problems, quit. */
526 * Don't make static, used by lpd(8).
528 * Returns 0 if ok, -1 if not ok.
531 __ivaliduser(hostf, raddr, luser, ruser)
534 const char *luser, *ruser;
536 struct sockaddr_in sin;
538 memset(&sin, 0, sizeof(sin));
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 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
547 * Returns 0 if ok, -1 if not ok.
552 __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
555 const char *luser, *ruser;
558 struct sockaddr *sa = NULL;
559 struct sockaddr_in *sin = NULL;
561 struct sockaddr_in6 *sin6 = NULL;
563 struct sockaddr_storage ss;
565 memset(&ss, 0, sizeof(ss));
568 if (len != sizeof(sin->sin_addr))
570 sin = (struct sockaddr_in *)&ss;
571 sin->sin_family = AF_INET;
572 sin->sin_len = sizeof(struct sockaddr_in);
573 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr));
577 if (len != sizeof(sin6->sin6_addr))
579 /* you will lose scope info */
580 sin6 = (struct sockaddr_in6 *)&ss;
581 sin6->sin6_family = AF_INET6;
582 sin6->sin6_len = sizeof(struct sockaddr_in6);
583 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr));
590 sa = (struct sockaddr *)&ss;
591 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser);
595 * Returns 0 if ok, -1 if not ok.
598 __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
600 const struct sockaddr *raddr;
602 const char *luser, *ruser;
606 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
607 char hname[MAXHOSTNAMELEN];
608 /* Presumed guilty until proven innocent. */
609 int userok = 0, hostok = 0;
614 if (yp_get_default_domain(&ypdomain))
617 #define ypdomain NULL
619 /* We need to get the damn hostname back for netgroup matching. */
620 if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0,
624 while (fgets(buf, sizeof(buf), hostf)) {
626 /* Skip lines that are too long. */
627 if (strchr(p, '\n') == NULL) {
628 while ((ch = getc(hostf)) != '\n' && ch != EOF);
631 if (*p == '\n' || *p == '#') {
635 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
636 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
639 if (*p == ' ' || *p == '\t') {
641 while (*p == ' ' || *p == '\t')
644 while (*p != '\n' && *p != ' ' &&
645 *p != '\t' && *p != '\0')
651 * Do +/- and +@/-@ checking. This looks really nasty,
652 * but it matches SunOS's behavior so far as I can tell.
656 if (!buf[1]) { /* '+' matches all hosts */
660 if (buf[1] == '@') /* match a host by netgroup */
661 hostok = hname[0] != '\0' &&
662 innetgr(&buf[2], hname, NULL, ypdomain);
663 else /* match a host by addr */
664 hostok = __icheckhost(raddr, salen,
667 case '-': /* reject '-' hosts and all their users */
669 if (hname[0] == '\0' ||
670 innetgr(&buf[2], hname, NULL, ypdomain))
673 if (__icheckhost(raddr, salen,
678 default: /* if no '+' or '-', do a simple match */
679 hostok = __icheckhost(raddr, salen, buf);
684 if (!*(user+1)) { /* '+' matches all users */
688 if (*(user+1) == '@') /* match a user by netgroup */
689 userok = innetgr(user+2, NULL, ruser, ypdomain);
690 else /* match a user by direct specification */
691 userok = !(strcmp(ruser, user+1));
693 case '-': /* if we matched a hostname, */
694 if (hostok) { /* check for user field rejections */
697 if (*(user+1) == '@') {
698 if (innetgr(user+2, NULL,
702 if (!strcmp(ruser, user+1))
707 default: /* no rejections: try to match the user */
709 userok = !(strcmp(ruser,*user ? user : luser));
712 if (hostok && userok)
719 * Returns "true" if match, 0 if no match.
721 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
725 __icheckhost(raddr, salen, lhost)
726 const struct sockaddr *raddr;
730 struct sockaddr_in sin;
731 struct sockaddr_in6 *sin6;
732 struct addrinfo hints, *res, *r;
734 char h1[NI_MAXHOST], h2[NI_MAXHOST];
736 if (raddr->sa_family == AF_INET6) {
737 sin6 = (struct sockaddr_in6 *)raddr;
738 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
739 memset(&sin, 0, sizeof(sin));
740 sin.sin_family = AF_INET;
741 sin.sin_len = sizeof(struct sockaddr_in);
742 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
743 sizeof(sin.sin_addr));
744 raddr = (struct sockaddr *)&sin;
750 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
751 NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
754 /* Resolve laddr into sockaddr */
755 memset(&hints, 0, sizeof(hints));
756 hints.ai_family = raddr->sa_family;
757 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/
759 error = getaddrinfo(lhost, "0", &hints, &res);
763 for (r = res; r ; r = r->ai_next) {
765 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
766 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
768 if (strcmp(h1, h2) == 0) {