2 * Copyright (C) 1998 WIDE Project.
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. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 1983, 1993, 1994
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
60 #endif /* LIBC_SCCS and not lint */
62 #define __UCLIBC_HIDE_DEPRECATED__
64 #include <sys/param.h>
66 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
79 #include <stdio_ext.h>
84 #ifdef __UCLIBC_HAS_WCHAR__
90 /* some forward declarations */
91 static int __ivaliduser2(FILE *hostf, u_int32_t raddr,
92 const char *luser, const char *ruser, const char *rhost);
93 static int iruserok2 (u_int32_t raddr, int superuser, const char *ruser,
94 const char *luser, const char *rhost);
97 int rcmd(char **ahost, u_short rport, const char *locuser, const char *remuser,
98 const char *cmd, int *fd2p)
100 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
102 struct hostent hostbuf;
107 struct sockaddr_in sin, from;
108 struct pollfd pfd[2];
116 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
118 tmphstbuf = stack_heap_alloc(hstbuflen);
120 while (gethostbyname_r (*ahost, &hostbuf, tmphstbuf,
121 hstbuflen, &hp, &herr) != 0 || hp == NULL)
123 if (herr != NETDB_INTERNAL || errno != ERANGE)
125 __set_h_errno (herr);
126 stack_heap_free(tmphstbuf);
132 /* Enlarge the buffer. */
134 stack_heap_free(tmphstbuf);
135 tmphstbuf = stack_heap_alloc(hstbuflen);
138 stack_heap_free(tmphstbuf);
139 #else /* call the non-reentrant version */
140 if ((hp = gethostbyname(*ahost)) == NULL) {
144 pfd[0].events = POLLIN;
145 pfd[1].events = POLLIN;
148 oldmask = sigblock(sigmask(SIGURG)); /* __sigblock */
149 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
150 s = rresvport(&lport);
153 (void)fprintf(stderr,
154 "rcmd: socket: All ports in use\n");
156 (void)fprintf(stderr, "rcmd: socket: %m\n");
157 sigsetmask(oldmask); /* sigsetmask */
160 fcntl(s, F_SETOWN, pid);
161 sin.sin_family = hp->h_addrtype;
162 memmove(&sin.sin_addr, hp->h_addr_list[0],
163 MIN (sizeof (sin.sin_addr), hp->h_length));
164 sin.sin_port = rport;
165 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) /* __connect */
168 if (errno == EADDRINUSE) {
172 if (errno == ECONNREFUSED && timo <= 16) {
173 (void)sleep(timo); /* __sleep */
177 if (hp->h_addr_list[1] != NULL) {
180 (void)fprintf(stderr, "connect to address %s: ",
181 inet_ntoa(sin.sin_addr));
182 __set_errno (oerrno);
185 memmove(&sin.sin_addr, hp->h_addr_list[0],
186 MIN (sizeof (sin.sin_addr), hp->h_length));
187 (void)fprintf(stderr, "Trying %s...\n",
188 inet_ntoa(sin.sin_addr));
191 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
192 sigsetmask(oldmask); /* __sigsetmask */
201 int s2 = rresvport(&lport), s3;
202 socklen_t len = sizeof(from);
207 (void)snprintf(num, sizeof(num), "%d", lport); /* __snprintf */
208 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
209 (void)fprintf(stderr,
210 "rcmd: write (setting up stderr): %m\n");
217 if (poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
219 (void)fprintf(stderr, "rcmd: poll (setting up stderr): %m\n");
221 (void)fprintf(stderr, "poll: protocol failure in circuit setup\n");
225 s3 = accept(s2, (struct sockaddr *)&from, &len);
228 (void)fprintf(stderr,
229 "rcmd: accept: %m\n");
234 from.sin_port = ntohs((u_short)from.sin_port);
235 if (from.sin_family != AF_INET ||
236 from.sin_port >= IPPORT_RESERVED ||
237 from.sin_port < IPPORT_RESERVED / 2) {
238 (void)fprintf(stderr,
239 "socket: protocol failure in circuit setup\n");
243 (void)write(s, locuser, strlen(locuser)+1);
244 (void)write(s, remuser, strlen(remuser)+1);
245 (void)write(s, cmd, strlen(cmd)+1);
246 if (read(s, &c, 1) != 1) {
247 (void)fprintf(stderr,
248 "rcmd: %s: %m\n", *ahost);
252 while (read(s, &c, 1) == 1) {
253 (void)write(STDERR_FILENO, &c, 1);
270 int rresvport(int *alport)
272 struct sockaddr_in sin;
275 sin.sin_family = AF_INET;
276 sin.sin_addr.s_addr = INADDR_ANY;
277 s = socket(AF_INET, SOCK_STREAM, 0);
281 sin.sin_port = htons((u_short)*alport);
282 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
284 if (errno != EADDRINUSE) {
289 if (*alport == IPPORT_RESERVED/2) {
291 __set_errno (EAGAIN); /* close */
298 libc_hidden_def(rresvport)
300 /* This needs to be exported ... while it is not a documented interface
301 * for rcp related apps, it's a required one that is used to control the
302 * rhost behavior. Legacy sucks.
304 int __check_rhosts_file = 1;
306 int ruserok(const char *rhost, int superuser, const char *ruser,
312 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
316 struct hostent hostbuf;
319 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
321 buffer = stack_heap_alloc(buflen);
323 while (gethostbyname_r (rhost, &hostbuf, buffer,
324 buflen, &hp, &herr) != 0 || hp == NULL)
326 if (herr != NETDB_INTERNAL || errno != ERANGE) {
327 stack_heap_free(buffer);
331 /* Enlarge the buffer. */
333 stack_heap_free(buffer);
334 buffer = stack_heap_alloc(buflen);
337 stack_heap_free(buffer);
339 if ((hp = gethostbyname(rhost)) == NULL) {
343 for (ap = hp->h_addr_list; *ap; ++ap) {
344 memmove(&addr, *ap, sizeof(addr));
345 if (iruserok2(addr, superuser, ruser, luser, rhost) == 0)
352 /* Extremely paranoid file open function. */
354 iruserfopen (const char *file, uid_t okuser)
360 /* If not a regular file, if owned by someone other than user or
361 root, if writeable by anyone but the owner, or if hardlinked
363 if (lstat (file, &st))
365 else if (!S_ISREG (st.st_mode))
366 cp = "not regular file";
369 res = fopen (file, "r");
372 else if (fstat (fileno (res), &st) < 0)
374 else if (st.st_uid && st.st_uid != okuser)
376 else if (st.st_mode & (S_IWGRP|S_IWOTH))
377 cp = "writeable by other than owner";
378 else if (st.st_nlink > 1)
379 cp = "hard linked somewhere";
382 /* If there were any problems, quit. */
395 * New .rhosts strategy: We are passed an ip address. We spin through
396 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
397 * has ip addresses, we don't have to trust a nameserver. When it
398 * contains hostnames, we spin through the list of addresses the nameserver
399 * gives us and look for a match.
401 * Returns 0 if ok, -1 if not ok.
404 iruserok2 (u_int32_t raddr, int superuser, const char *ruser, const char *luser,
411 hostf = iruserfopen (_PATH_HEQUIV, 0);
414 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
421 if (__check_rhosts_file || superuser) {
427 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
428 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
429 struct passwd pwdbuf;
430 char *buffer = stack_heap_alloc(buflen);
432 if (getpwnam_r (luser, &pwdbuf, buffer,
433 buflen, &pwd) != 0 || pwd == NULL)
435 stack_heap_free(buffer);
438 stack_heap_free(buffer);
440 if ((pwd = getpwnam(luser)) == NULL)
444 dirlen = strlen (pwd->pw_dir);
445 pbuf = malloc (dirlen + sizeof "/.rhosts");
446 strcpy (pbuf, pwd->pw_dir);
447 strcat (pbuf, "/.rhosts");
449 /* Change effective uid while reading .rhosts. If root and
450 reading an NFS mounted file system, can't read files that
451 are protected read/write owner only. */
453 seteuid (pwd->pw_uid);
454 hostf = iruserfopen (pbuf, pwd->pw_uid);
458 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
468 /* This is the exported version. */
469 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser);
470 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser)
472 return iruserok2 (raddr, superuser, ruser, luser, "-");
478 * Don't make static, used by lpd(8).
480 * This function is not used anymore. It is only present because lpd(8)
481 * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
482 * argument. This means that netgroups won't work in .rhost/hosts.equiv
483 * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
485 * Returns 0 if ok, -1 if not ok.
488 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser);
490 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
492 return __ivaliduser2(hostf, raddr, luser, ruser, "-");
496 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
498 __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
502 int negate=1; /* Multiply return with this to get -1 instead of 1 */
505 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
509 struct hostent hostbuf;
514 /* Check nis netgroup. */
515 if (strncmp ("+@", lhost, 2) == 0)
516 return innetgr (&lhost[2], rhost, NULL, NULL);
518 if (strncmp ("-@", lhost, 2) == 0)
519 return -innetgr (&lhost[2], rhost, NULL, NULL);
520 #endif /* HAVE_NETGROUP */
523 if (strncmp ("-", lhost,1) == 0) {
526 } else if (strcmp ("+",lhost) == 0) {
527 return 1; /* asking for trouble, but ok.. */
530 /* Try for raw ip address first. */
531 if (isdigit (*lhost) && (laddr = inet_addr (lhost)) != INADDR_NONE)
532 return negate * (! (raddr ^ laddr));
534 /* Better be a hostname. */
535 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
537 buffer = malloc(buflen);
540 while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
546 __set_errno (save_errno);
548 hp = gethostbyname(lhost);
549 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */
554 /* Spin through ip addresses. */
555 for (pp = hp->h_addr_list; *pp; ++pp)
556 if (!memcmp (&raddr, *pp, sizeof (u_int32_t)))
563 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
565 __icheckuser (const char *luser, const char *ruser)
569 luser is user entry from .rhosts/hosts.equiv file
570 ruser is user id on remote host
575 if (strncmp ("+@", luser, 2) == 0)
576 return innetgr (&luser[2], NULL, ruser, NULL);
578 if (strncmp ("-@", luser,2) == 0)
579 return -innetgr (&luser[2], NULL, ruser, NULL);
580 #endif /* HAVE_NETGROUP */
583 if (strncmp ("-", luser, 1) == 0)
584 return -(strcmp (&luser[1], ruser) == 0);
587 if (strcmp ("+", luser) == 0)
590 /* simple string match */
591 return strcmp (ruser, luser) == 0;
595 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
600 while (*p && isspace (*p)) {
604 return (*p == '\0' || *p == '#') ? 1 : 0 ;
608 * Returns 0 if positive match, -1 if _not_ ok.
611 __ivaliduser2(FILE *hostf, u_int32_t raddr, const char *luser,
612 const char *ruser, const char *rhost)
614 register const char *user;
621 while (getline (&buf, &bufsize, hostf) > 0) {
622 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
625 /* Skip empty or comment lines */
630 /* Skip lines that are too long. */
631 if (strchr (p, '\n') == NULL) {
632 int ch = getc_unlocked (hostf);
634 while (ch != '\n' && ch != EOF)
635 ch = getc_unlocked (hostf);
639 for (;*p && !isspace(*p); ++p) {
643 /* Next we want to find the permitted name for the remote user. */
644 if (*p == ' ' || *p == '\t') {
645 /* <nul> terminate hostname and skip spaces */
646 for (*p++='\0'; *p && isspace (*p); ++p);
648 user = p; /* this is the user's name */
649 while (*p && !isspace (*p))
650 ++p; /* find end of user's name */
654 *p = '\0'; /* <nul> terminate username (+host?) */
656 /* buf -> host(?) ; user -> username(?) */
658 /* First check host part */
659 hcheck = __icheckhost (raddr, buf, rhost);
665 /* Then check user part */
669 ucheck = __icheckuser (user, ruser);
671 /* Positive 'host user' match? */
677 /* Negative 'host -user' match? */
681 /* Neither, go on looking for match */