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
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
38 #endif /* LIBC_SCCS and not lint */
40 #include <sys/param.h>
41 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
62 /* wrapper for KAME-special getnameinfo() */
63 #ifndef NI_WITHSCOPEID
64 #define NI_WITHSCOPEID 0
67 extern int innetgr __P(( const char *, const char *, const char *, const char * ));
69 #define max(a, b) ((a > b) ? a : b)
71 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
72 static int __icheckhost __P((void *, char *, int, int));
74 char paddr[INET6_ADDRSTRLEN];
77 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
80 const char *locuser, *remuser, *cmd;
83 struct addrinfo hints, *res, *ai;
84 struct sockaddr_storage from;
88 int s, aport, lport, timo, error;
95 memset(&hints, 0, sizeof(hints));
96 hints.ai_flags = AI_CANONNAME;
97 hints.ai_family = AF_UNSPEC;
98 hints.ai_socktype = SOCK_STREAM;
99 hints.ai_protocol = 0;
100 (void)snprintf(num, sizeof(num), "%d", ntohs(rport));
101 error = getaddrinfo(*ahost, num, &hints, &res);
103 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
104 gai_strerror(error));
105 if (error == EAI_SYSTEM)
106 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
110 if (res->ai_canonname)
111 *ahost = res->ai_canonname;
114 oldmask = sigblock(sigmask(SIGURG));
115 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
116 s = rresvport_af(&lport, ai->ai_family);
119 (void)fprintf(stderr,
120 "rcmd: socket: All ports in use\n");
122 (void)fprintf(stderr, "rcmd: socket: %s\n",
128 _libc_fcntl(s, F_SETOWN, pid);
129 if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
131 (void)_libc_close(s);
132 if (errno == EADDRINUSE) {
136 if (errno == ECONNREFUSED)
138 if (ai->ai_next != NULL) {
141 getnameinfo(ai->ai_addr, ai->ai_addrlen,
142 paddr, sizeof(paddr),
144 NI_NUMERICHOST|NI_WITHSCOPEID);
145 (void)fprintf(stderr, "connect to address %s: ",
150 getnameinfo(ai->ai_addr, ai->ai_addrlen,
151 paddr, sizeof(paddr),
153 NI_NUMERICHOST|NI_WITHSCOPEID);
154 fprintf(stderr, "Trying %s...\n", paddr);
157 if (refused && timo <= 16) {
158 (void)_libc_sleep(timo);
165 (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno));
171 _libc_write(s, "", 1);
175 int s2 = rresvport_af(&lport, ai->ai_family), s3;
176 int len = ai->ai_addrlen;
182 (void)snprintf(num, sizeof(num), "%d", lport);
183 if (_libc_write(s, num, strlen(num)+1) != strlen(num)+1) {
184 (void)fprintf(stderr,
185 "rcmd: write (setting up stderr): %s\n",
187 (void)_libc_close(s2);
191 if(nfds > FD_SETSIZE) {
192 fprintf(stderr, "rcmd: too many files\n");
193 (void)_libc_close(s2);
201 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
203 (void)fprintf(stderr,
204 "rcmd: select (setting up stderr): %s\n",
207 (void)fprintf(stderr,
208 "select: protocol failure in circuit setup\n");
209 (void)_libc_close(s2);
212 s3 = accept(s2, (struct sockaddr *)&from, &len);
213 switch (from.ss_family) {
215 aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
219 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
223 aport = 0; /* error */
227 * XXX careful for ftp bounce attacks. If discovered, shut them
228 * down and check for the real auxiliary channel to connect.
234 (void)_libc_close(s2);
236 (void)fprintf(stderr,
237 "rcmd: accept: %s\n", strerror(errno));
242 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
243 (void)fprintf(stderr,
244 "socket: protocol failure in circuit setup.\n");
248 (void)_libc_write(s, locuser, strlen(locuser)+1);
249 (void)_libc_write(s, remuser, strlen(remuser)+1);
250 (void)_libc_write(s, cmd, strlen(cmd)+1);
251 if (_libc_read(s, &c, 1) != 1) {
252 (void)fprintf(stderr,
253 "rcmd: %s: %s\n", *ahost, strerror(errno));
257 while (_libc_read(s, &c, 1) == 1) {
258 (void)_libc_write(STDERR_FILENO, &c, 1);
269 (void)_libc_close(*fd2p);
271 (void)_libc_close(s);
281 return rresvport_af(port, AF_INET);
285 rresvport_af(alport, family)
289 struct sockaddr_storage ss;
292 memset(&ss, 0, sizeof(ss));
293 ss.ss_family = family;
296 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
297 sport = &((struct sockaddr_in *)&ss)->sin_port;
298 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
302 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
303 sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
304 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
308 errno = EAFNOSUPPORT;
312 s = socket(ss.ss_family, SOCK_STREAM, 0);
315 #if 0 /* compat_exact_traditional_rresvport_semantics */
316 sin.sin_port = htons((u_short)*alport);
317 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
319 if (errno != EADDRINUSE) {
320 (void)_libc_close(s);
325 if (bindresvport2(s, (struct sockaddr *)&ss,
326 ((struct sockaddr *)&ss)->sa_len) == -1) {
327 (void)_libc_close(s);
330 *alport = (int)ntohs(*sport);
334 int __check_rhosts_file = 1;
338 ruserok(rhost, superuser, ruser, luser)
339 const char *rhost, *ruser, *luser;
342 return ruserok_af(rhost, superuser, ruser, luser, AF_INET);
346 ruserok_af(rhost, superuser, ruser, luser, af)
347 const char *rhost, *ruser, *luser;
352 struct in_addr addr_in;
353 struct in6_addr addr_in6;
358 if ((hp = getipnodebyname(rhost, af, AI_DEFAULT, &h_error)) == NULL)
361 for (ap = hp->h_addr_list; *ap; ++ap) {
362 bcopy(*ap, &addr, hp->h_length);
363 if (iruserok_af(&addr, superuser, ruser, luser, af) == 0) {
373 * New .rhosts strategy: We are passed an ip address. We spin through
374 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
375 * has ip addresses, we don't have to trust a nameserver. When it
376 * contains hostnames, we spin through the list of addresses the nameserver
377 * gives us and look for a match.
379 * Returns 0 if ok, -1 if not ok.
382 iruserok(raddr, superuser, ruser, luser)
385 const char *ruser, *luser;
387 return iruserok_af(&raddr, superuser, ruser, luser, AF_INET);
391 iruserok_af(raddr, superuser, ruser, luser, af)
394 const char *ruser, *luser;
403 char pbuf[MAXPATHLEN];
408 len = sizeof(struct in_addr);
412 len = sizeof(struct in6_addr);
418 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
421 if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len)
428 if (first == 1 && (__check_rhosts_file || superuser)) {
430 if ((pwd = getpwnam(luser)) == NULL)
432 (void)strcpy(pbuf, pwd->pw_dir);
433 (void)strcat(pbuf, "/.rhosts");
436 * Change effective uid while opening .rhosts. If root and
437 * reading an NFS mounted file system, can't read files that
438 * are protected read/write owner only.
441 (void)seteuid(pwd->pw_uid);
442 hostf = fopen(pbuf, "r");
448 * If not a regular file, or is owned by someone other than
449 * user or root or if writeable by anyone but the owner, quit.
452 if (lstat(pbuf, &sbuf) < 0)
453 cp = ".rhosts lstat failed";
454 else if (!S_ISREG(sbuf.st_mode))
455 cp = ".rhosts not regular file";
456 else if (fstat(fileno(hostf), &sbuf) < 0)
457 cp = ".rhosts fstat failed";
458 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
459 cp = "bad .rhosts owner";
460 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
461 cp = ".rhosts writeable by other than owner";
462 /* If there were any problems, quit. */
475 * Don't make static, used by lpd(8).
477 * Returns 0 if ok, -1 if not ok.
480 __ivaliduser(hostf, raddr, luser, ruser)
483 const char *luser, *ruser;
485 return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET,
490 __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
493 const char *luser, *ruser;
496 register char *user, *p;
498 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
499 char hname[MAXHOSTNAMELEN];
501 /* Presumed guilty until proven innocent. */
502 int userok = 0, hostok = 0;
507 if (yp_get_default_domain(&ypdomain))
510 #define ypdomain NULL
512 /* We need to get the damn hostname back for netgroup matching. */
513 if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL)
515 strncpy(hname, hp->h_name, sizeof(hname));
516 hname[sizeof(hname) - 1] = '\0';
519 while (fgets(buf, sizeof(buf), hostf)) {
521 /* Skip lines that are too long. */
522 if (strchr(p, '\n') == NULL) {
523 while ((ch = getc(hostf)) != '\n' && ch != EOF);
526 if (*p == '\n' || *p == '#') {
530 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
531 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
534 if (*p == ' ' || *p == '\t') {
536 while (*p == ' ' || *p == '\t')
539 while (*p != '\n' && *p != ' ' &&
540 *p != '\t' && *p != '\0')
546 * Do +/- and +@/-@ checking. This looks really nasty,
547 * but it matches SunOS's behavior so far as I can tell.
551 if (!buf[1]) { /* '+' matches all hosts */
555 if (buf[1] == '@') /* match a host by netgroup */
556 hostok = innetgr((char *)&buf[2],
557 (char *)&hname, NULL, ypdomain);
558 else /* match a host by addr */
559 hostok = __icheckhost(raddr,(char *)&buf[1],
562 case '-': /* reject '-' hosts and all their users */
564 if (innetgr((char *)&buf[2],
565 (char *)&hname, NULL, ypdomain))
568 if (__icheckhost(raddr,(char *)&buf[1],af,len))
572 default: /* if no '+' or '-', do a simple match */
573 hostok = __icheckhost(raddr, buf, af, len);
578 if (!*(user+1)) { /* '+' matches all users */
582 if (*(user+1) == '@') /* match a user by netgroup */
583 userok = innetgr(user+2, NULL, ruser, ypdomain);
584 else /* match a user by direct specification */
585 userok = !(strcmp(ruser, user+1));
587 case '-': /* if we matched a hostname, */
588 if (hostok) { /* check for user field rejections */
591 if (*(user+1) == '@') {
592 if (innetgr(user+2, NULL,
596 if (!strcmp(ruser, user+1))
601 default: /* no rejections: try to match the user */
603 userok = !(strcmp(ruser,*user ? user : luser));
606 if (hostok && userok)
613 * Returns "true" if match, 0 if no match.
616 __icheckhost(raddr, lhost, af, len)
618 register char *lhost;
621 register struct hostent *hp;
622 char laddr[BUFSIZ]; /* xxx */
627 /* Try for raw ip address first. */
628 if (inet_pton(af, lhost, laddr) == 1) {
629 if (memcmp(raddr, laddr, len) == 0)
635 /* Better be a hostname. */
636 if ((hp = getipnodebyname(lhost, af, AI_DEFAULT, &h_error)) == NULL)
639 /* Spin through ip addresses. */
641 for (pp = hp->h_addr_list; *pp; ++pp)
642 if (!bcmp(raddr, *pp, len)) {