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 extern int innetgr __P(( const char *, const char *, const char *, const char * ));
64 #define max(a, b) ((a > b) ? a : b)
66 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
67 static int __icheckhost __P((u_int32_t, char *));
70 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
73 const char *locuser, *remuser, *cmd;
77 struct sockaddr_in sin, from;
85 hp = gethostbyname(*ahost);
91 oldmask = sigblock(sigmask(SIGURG));
92 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
93 s = rresvport(&lport);
97 "rcmd: socket: All ports in use\n");
99 (void)fprintf(stderr, "rcmd: socket: %s\n",
104 fcntl(s, F_SETOWN, pid);
105 bzero(&sin, sizeof sin);
106 sin.sin_len = sizeof(struct sockaddr_in);
107 sin.sin_family = hp->h_addrtype;
108 sin.sin_port = rport;
109 bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
110 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
113 if (errno == EADDRINUSE) {
117 if (errno == ECONNREFUSED && timo <= 16) {
122 if (hp->h_addr_list[1] != NULL) {
125 (void)fprintf(stderr, "connect to address %s: ",
126 inet_ntoa(sin.sin_addr));
130 bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
131 (void)fprintf(stderr, "Trying %s...\n",
132 inet_ntoa(sin.sin_addr));
135 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
145 int s2 = rresvport(&lport), s3;
146 int len = sizeof(from);
152 (void)snprintf(num, sizeof(num), "%d", lport);
153 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
154 (void)fprintf(stderr,
155 "rcmd: write (setting up stderr): %s\n",
161 if(nfds > FD_SETSIZE) {
162 fprintf(stderr, "rcmd: too many files\n");
171 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
173 (void)fprintf(stderr,
174 "rcmd: select (setting up stderr): %s\n",
177 (void)fprintf(stderr,
178 "select: protocol failure in circuit setup\n");
182 s3 = accept(s2, (struct sockaddr *)&from, &len);
184 * XXX careful for ftp bounce attacks. If discovered, shut them
185 * down and check for the real auxiliary channel to connect.
187 if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
193 (void)fprintf(stderr,
194 "rcmd: accept: %s\n", strerror(errno));
199 from.sin_port = ntohs((u_short)from.sin_port);
200 if (from.sin_family != AF_INET ||
201 from.sin_port >= IPPORT_RESERVED ||
202 from.sin_port < IPPORT_RESERVED / 2) {
203 (void)fprintf(stderr,
204 "socket: protocol failure in circuit setup.\n");
208 (void)write(s, locuser, strlen(locuser)+1);
209 (void)write(s, remuser, strlen(remuser)+1);
210 (void)write(s, cmd, strlen(cmd)+1);
211 if (read(s, &c, 1) != 1) {
212 (void)fprintf(stderr,
213 "rcmd: %s: %s\n", *ahost, strerror(errno));
217 while (read(s, &c, 1) == 1) {
218 (void)write(STDERR_FILENO, &c, 1);
239 struct sockaddr_in sin;
242 bzero(&sin, sizeof sin);
243 sin.sin_len = sizeof(struct sockaddr_in);
244 sin.sin_family = AF_INET;
245 sin.sin_addr.s_addr = INADDR_ANY;
246 s = socket(AF_INET, SOCK_STREAM, 0);
249 #if 0 /* compat_exact_traditional_rresvport_semantics */
250 sin.sin_port = htons((u_short)*alport);
251 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
253 if (errno != EADDRINUSE) {
259 if (bindresvport(s, &sin) == -1) {
263 *alport = (int)ntohs(sin.sin_port);
267 int __check_rhosts_file = 1;
271 ruserok(rhost, superuser, ruser, luser)
272 const char *rhost, *ruser, *luser;
279 if ((hp = gethostbyname(rhost)) == NULL)
281 for (ap = hp->h_addr_list; *ap; ++ap) {
282 bcopy(*ap, &addr, sizeof(addr));
283 if (iruserok(addr, superuser, ruser, luser) == 0)
290 * New .rhosts strategy: We are passed an ip address. We spin through
291 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
292 * has ip addresses, we don't have to trust a nameserver. When it
293 * contains hostnames, we spin through the list of addresses the nameserver
294 * gives us and look for a match.
296 * Returns 0 if ok, -1 if not ok.
299 iruserok(raddr, superuser, ruser, luser)
302 const char *ruser, *luser;
310 char pbuf[MAXPATHLEN];
313 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
316 if (__ivaliduser(hostf, (u_int32_t)raddr, luser, ruser) == 0) {
322 if (first == 1 && (__check_rhosts_file || superuser)) {
324 if ((pwd = getpwnam(luser)) == NULL)
326 (void)strcpy(pbuf, pwd->pw_dir);
327 (void)strcat(pbuf, "/.rhosts");
330 * Change effective uid while opening .rhosts. If root and
331 * reading an NFS mounted file system, can't read files that
332 * are protected read/write owner only.
335 (void)seteuid(pwd->pw_uid);
336 hostf = fopen(pbuf, "r");
342 * If not a regular file, or is owned by someone other than
343 * user or root or if writeable by anyone but the owner, quit.
346 if (lstat(pbuf, &sbuf) < 0)
347 cp = ".rhosts lstat failed";
348 else if (!S_ISREG(sbuf.st_mode))
349 cp = ".rhosts not regular file";
350 else if (fstat(fileno(hostf), &sbuf) < 0)
351 cp = ".rhosts fstat failed";
352 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
353 cp = "bad .rhosts owner";
354 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
355 cp = ".rhosts writeable by other than owner";
356 /* If there were any problems, quit. */
369 * Don't make static, used by lpd(8).
371 * Returns 0 if ok, -1 if not ok.
374 __ivaliduser(hostf, raddr, luser, ruser)
377 const char *luser, *ruser;
379 register char *user, *p;
381 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
382 char hname[MAXHOSTNAMELEN];
384 /* Presumed guilty until proven innocent. */
385 int userok = 0, hostok = 0;
389 if (yp_get_default_domain(&ypdomain))
392 #define ypdomain NULL
394 /* We need to get the damn hostname back for netgroup matching. */
395 if ((hp = gethostbyaddr((char *)&raddr, sizeof(u_int32_t),
398 strncpy(hname, hp->h_name, sizeof(hname));
399 hname[sizeof(hname) - 1] = '\0';
401 while (fgets(buf, sizeof(buf), hostf)) {
403 /* Skip lines that are too long. */
404 if (strchr(p, '\n') == NULL) {
405 while ((ch = getc(hostf)) != '\n' && ch != EOF);
408 if (*p == '\n' || *p == '#') {
412 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
413 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
416 if (*p == ' ' || *p == '\t') {
418 while (*p == ' ' || *p == '\t')
421 while (*p != '\n' && *p != ' ' &&
422 *p != '\t' && *p != '\0')
428 * Do +/- and +@/-@ checking. This looks really nasty,
429 * but it matches SunOS's behavior so far as I can tell.
433 if (!buf[1]) { /* '+' matches all hosts */
437 if (buf[1] == '@') /* match a host by netgroup */
438 hostok = innetgr((char *)&buf[2],
439 (char *)&hname, NULL, ypdomain);
440 else /* match a host by addr */
441 hostok = __icheckhost(raddr,(char *)&buf[1]);
443 case '-': /* reject '-' hosts and all their users */
445 if (innetgr((char *)&buf[2],
446 (char *)&hname, NULL, ypdomain))
449 if (__icheckhost(raddr,(char *)&buf[1]))
453 default: /* if no '+' or '-', do a simple match */
454 hostok = __icheckhost(raddr, buf);
459 if (!*(user+1)) { /* '+' matches all users */
463 if (*(user+1) == '@') /* match a user by netgroup */
464 userok = innetgr(user+2, NULL, ruser, ypdomain);
465 else /* match a user by direct specification */
466 userok = !(strcmp(ruser, user+1));
468 case '-': /* if we matched a hostname, */
469 if (hostok) { /* check for user field rejections */
472 if (*(user+1) == '@') {
473 if (innetgr(user+2, NULL,
477 if (!strcmp(ruser, user+1))
482 default: /* no rejections: try to match the user */
484 userok = !(strcmp(ruser,*user ? user : luser));
487 if (hostok && userok)
494 * Returns "true" if match, 0 if no match.
497 __icheckhost(raddr, lhost)
499 register char *lhost;
501 register struct hostent *hp;
502 register u_int32_t laddr;
505 /* Try for raw ip address first. */
506 if (isdigit((unsigned char)*lhost) && (u_int32_t)(laddr = inet_addr(lhost)) != -1)
507 return (raddr == laddr);
509 /* Better be a hostname. */
510 if ((hp = gethostbyname(lhost)) == NULL)
513 /* Spin through ip addresses. */
514 for (pp = hp->h_addr_list; *pp; ++pp)
515 if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))