sync header with GNU libc / kernel
[uclibc-ng.git] / libc / inet / rpc / rcmd.c
blob63b0ef2e225d570d0ce39ec44e3d01930cadd089
1 /*
2 * Copyright (C) 1998 WIDE Project.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
27 * SUCH DAMAGE.
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
35 * are met:
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
55 * SUCH DAMAGE.
58 #if 0
59 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
60 #endif /* LIBC_SCCS and not lint */
62 #define __UCLIBC_HIDE_DEPRECATED__
63 #include <features.h>
64 #include <sys/param.h>
65 #include <sys/poll.h>
66 #include <sys/socket.h>
67 #include <sys/stat.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
72 #include <signal.h>
73 #include <fcntl.h>
74 #include <netdb.h>
75 #include <unistd.h>
76 #include <pwd.h>
77 #include <errno.h>
78 #include <stdio.h>
79 #include <ctype.h>
80 #include <string.h>
81 #include <stdlib.h>
82 #ifdef __UCLIBC_HAS_WCHAR__
83 #include <wchar.h>
84 #endif
85 #include <sys/uio.h>
86 #include <bits/uClibc_alloc.h>
88 /* sigsetmask and sigblock are not provided anymore, until this file is corrected,
89 * include the sources */
90 #include "../../signal/sigblock.c"
91 #include "../../signal/sigsetmask.c"
94 /* some forward declarations */
95 static int __ivaliduser2(FILE *hostf, u_int32_t raddr,
96 const char *luser, const char *ruser, const char *rhost);
97 static int iruserok2 (u_int32_t raddr, int superuser, const char *ruser,
98 const char *luser, const char *rhost);
101 int rcmd(char **ahost, u_short rport, const char *locuser, const char *remuser,
102 const char *cmd, int *fd2p)
104 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
105 int herr;
106 struct hostent hostbuf;
107 size_t hstbuflen;
108 char *tmphstbuf;
109 #endif
110 struct hostent *hp;
111 struct sockaddr_in sin, from;
112 struct pollfd pfd[2];
113 int32_t oldmask;
114 pid_t pid;
115 int s, lport, timo;
116 char c;
118 pid = getpid();
120 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
121 hstbuflen = 1024;
122 tmphstbuf = stack_heap_alloc(hstbuflen);
124 while (gethostbyname_r (*ahost, &hostbuf, tmphstbuf,
125 hstbuflen, &hp, &herr) != 0 || hp == NULL)
127 if (herr != NETDB_INTERNAL || errno != ERANGE)
129 __set_h_errno (herr);
130 stack_heap_free(tmphstbuf);
131 herror(*ahost);
132 return -1;
134 else
136 /* Enlarge the buffer. */
137 hstbuflen *= 2;
138 stack_heap_free(tmphstbuf);
139 tmphstbuf = stack_heap_alloc(hstbuflen);
142 stack_heap_free(tmphstbuf);
143 #else /* call the non-reentrant version */
144 if ((hp = gethostbyname(*ahost)) == NULL) {
145 return -1;
147 #endif
148 pfd[0].events = POLLIN;
149 pfd[1].events = POLLIN;
151 *ahost = hp->h_name;
152 oldmask = sigblock(__sigmask(SIGURG)); /* sigblock */
153 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
154 s = rresvport(&lport);
155 if (s < 0) {
156 if (errno == EAGAIN)
157 (void)fprintf(stderr,
158 "rcmd: socket: All ports in use\n");
159 else
160 (void)fprintf(stderr, "rcmd: socket: %m\n");
161 sigsetmask(oldmask); /* sigsetmask */
162 return -1;
164 fcntl(s, F_SETOWN, pid);
165 sin.sin_family = hp->h_addrtype;
166 memmove(&sin.sin_addr, hp->h_addr_list[0],
167 MIN (sizeof (sin.sin_addr), hp->h_length));
168 sin.sin_port = rport;
169 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) /* __connect */
170 break;
171 (void)close(s);
172 if (errno == EADDRINUSE) {
173 lport--;
174 continue;
176 if (errno == ECONNREFUSED && timo <= 16) {
177 (void)sleep(timo); /* __sleep */
178 timo *= 2;
179 continue;
181 if (hp->h_addr_list[1] != NULL) {
182 int oerrno = errno;
184 (void)fprintf(stderr, "connect to address %s: ",
185 inet_ntoa(sin.sin_addr));
186 __set_errno (oerrno);
187 perror(0);
188 hp->h_addr_list++;
189 memmove(&sin.sin_addr, hp->h_addr_list[0],
190 MIN (sizeof (sin.sin_addr), hp->h_length));
191 (void)fprintf(stderr, "Trying %s...\n",
192 inet_ntoa(sin.sin_addr));
193 continue;
195 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
196 sigsetmask(oldmask); /* __sigsetmask */
197 return -1;
199 lport--;
200 if (fd2p == 0) {
201 write(s, "", 1);
202 lport = 0;
203 } else {
204 char num[8];
205 int s2 = rresvport(&lport), s3;
206 socklen_t len = sizeof(from);
208 if (s2 < 0)
209 goto bad;
210 listen(s2, 1);
211 (void)snprintf(num, sizeof(num), "%d", lport); /* __snprintf */
212 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
213 (void)fprintf(stderr,
214 "rcmd: write (setting up stderr): %m\n");
215 (void)close(s2);
216 goto bad;
218 pfd[0].fd = s;
219 pfd[1].fd = s2;
220 __set_errno (0);
221 if (poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
222 if (errno != 0)
223 (void)fprintf(stderr, "rcmd: poll (setting up stderr): %m\n");
224 else
225 (void)fprintf(stderr, "poll: protocol failure in circuit setup\n");
226 (void)close(s2);
227 goto bad;
229 s3 = accept(s2, (struct sockaddr *)&from, &len);
230 (void)close(s2);
231 if (s3 < 0) {
232 (void)fprintf(stderr,
233 "rcmd: accept: %m\n");
234 lport = 0;
235 goto bad;
237 *fd2p = s3;
238 from.sin_port = ntohs((u_short)from.sin_port);
239 if (from.sin_family != AF_INET ||
240 from.sin_port >= IPPORT_RESERVED ||
241 from.sin_port < IPPORT_RESERVED / 2) {
242 (void)fprintf(stderr,
243 "socket: protocol failure in circuit setup\n");
244 goto bad2;
247 (void)write(s, locuser, strlen(locuser)+1);
248 (void)write(s, remuser, strlen(remuser)+1);
249 (void)write(s, cmd, strlen(cmd)+1);
250 if (read(s, &c, 1) != 1) {
251 (void)fprintf(stderr,
252 "rcmd: %s: %m\n", *ahost);
253 goto bad2;
255 if (c != 0) {
256 while (read(s, &c, 1) == 1) {
257 (void)write(STDERR_FILENO, &c, 1);
258 if (c == '\n')
259 break;
261 goto bad2;
263 sigsetmask(oldmask);
264 return s;
265 bad2:
266 if (lport)
267 (void)close(*fd2p);
268 bad:
269 (void)close(s);
270 sigsetmask(oldmask);
271 return -1;
274 int rresvport(int *alport)
276 struct sockaddr_in sin;
277 int s;
279 sin.sin_family = AF_INET;
280 sin.sin_addr.s_addr = INADDR_ANY;
281 s = socket(AF_INET, SOCK_STREAM, 0);
282 if (s < 0)
283 return -1;
284 for (;;) {
285 sin.sin_port = htons((u_short)*alport);
286 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
287 return s;
288 if (errno != EADDRINUSE) {
289 (void)close(s);
290 return -1;
292 (*alport)--;
293 if (*alport == IPPORT_RESERVED/2) {
294 (void)close(s);
295 __set_errno (EAGAIN); /* close */
296 return -1;
300 return -1;
302 libc_hidden_def(rresvport)
304 /* This needs to be exported ... while it is not a documented interface
305 * for rcp related apps, it's a required one that is used to control the
306 * rhost behavior. Legacy sucks.
308 int __check_rhosts_file = 1;
310 int ruserok(const char *rhost, int superuser, const char *ruser,
311 const char *luser)
313 struct hostent *hp;
314 u_int32_t addr;
315 char **ap;
316 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
317 size_t buflen;
318 char *buffer;
319 int herr;
320 struct hostent hostbuf;
321 #endif
323 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
324 buflen = 1024;
325 buffer = stack_heap_alloc(buflen);
327 while (gethostbyname_r (rhost, &hostbuf, buffer,
328 buflen, &hp, &herr) != 0 || hp == NULL)
330 if (herr != NETDB_INTERNAL || errno != ERANGE) {
331 stack_heap_free(buffer);
332 return -1;
333 } else
335 /* Enlarge the buffer. */
336 buflen *= 2;
337 stack_heap_free(buffer);
338 buffer = stack_heap_alloc(buflen);
341 stack_heap_free(buffer);
342 #else
343 if ((hp = gethostbyname(rhost)) == NULL) {
344 return -1;
346 #endif
347 for (ap = hp->h_addr_list; *ap; ++ap) {
348 memmove(&addr, *ap, sizeof(addr));
349 if (iruserok2(addr, superuser, ruser, luser, rhost) == 0)
350 return 0;
352 return -1;
356 /* Extremely paranoid file open function. */
357 static FILE *
358 iruserfopen (const char *file, uid_t okuser)
360 struct stat st;
361 char *cp = NULL;
362 FILE *res = NULL;
364 /* If not a regular file, if owned by someone other than user or
365 root, if writeable by anyone but the owner, or if hardlinked
366 anywhere, quit. */
367 if (lstat (file, &st))
368 cp = "lstat failed";
369 else if (!S_ISREG (st.st_mode))
370 cp = "not regular file";
371 else
373 res = fopen (file, "r");
374 if (!res)
375 cp = "cannot open";
376 else if (fstat (fileno (res), &st) < 0)
377 cp = "fstat failed";
378 else if (st.st_uid && st.st_uid != okuser)
379 cp = "bad owner";
380 else if (st.st_mode & (S_IWGRP|S_IWOTH))
381 cp = "writeable by other than owner";
382 else if (st.st_nlink > 1)
383 cp = "hard linked somewhere";
386 /* If there were any problems, quit. */
387 if (cp != NULL)
389 if (res)
390 fclose (res);
391 return NULL;
394 return res;
399 * New .rhosts strategy: We are passed an ip address. We spin through
400 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
401 * has ip addresses, we don't have to trust a nameserver. When it
402 * contains hostnames, we spin through the list of addresses the nameserver
403 * gives us and look for a match.
405 * Returns 0 if ok, -1 if not ok.
407 static int
408 iruserok2 (u_int32_t raddr, int superuser, const char *ruser, const char *luser,
409 const char *rhost)
411 FILE *hostf = NULL;
412 int isbad = -1;
414 if (!superuser)
415 hostf = iruserfopen (_PATH_HEQUIV, 0);
417 if (hostf) {
418 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
419 fclose (hostf);
421 if (!isbad)
422 return 0;
425 if (__check_rhosts_file || superuser) {
426 char *pbuf;
427 struct passwd *pwd;
428 size_t dirlen;
429 uid_t uid;
431 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
432 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
433 struct passwd pwdbuf;
434 char *buffer = stack_heap_alloc(buflen);
436 if (getpwnam_r (luser, &pwdbuf, buffer,
437 buflen, &pwd) != 0 || pwd == NULL)
439 stack_heap_free(buffer);
440 return -1;
442 stack_heap_free(buffer);
443 #else
444 if ((pwd = getpwnam(luser)) == NULL)
445 return -1;
446 #endif
448 dirlen = strlen (pwd->pw_dir);
449 pbuf = malloc (dirlen + sizeof "/.rhosts");
450 strcpy (pbuf, pwd->pw_dir);
451 strcat (pbuf, "/.rhosts");
453 /* Change effective uid while reading .rhosts. If root and
454 reading an NFS mounted file system, can't read files that
455 are protected read/write owner only. */
456 uid = geteuid ();
457 seteuid (pwd->pw_uid);
458 hostf = iruserfopen (pbuf, pwd->pw_uid);
459 free(pbuf);
461 if (hostf != NULL) {
462 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
463 fclose (hostf);
466 seteuid (uid);
467 return isbad;
469 return -1;
472 /* This is the exported version. */
473 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser);
474 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser)
476 return iruserok2 (raddr, superuser, ruser, luser, "-");
481 * XXX
482 * Don't make static, used by lpd(8).
484 * This function is not used anymore. It is only present because lpd(8)
485 * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
486 * argument. This means that netgroups won't work in .rhost/hosts.equiv
487 * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
488 * or PAM.
489 * Returns 0 if ok, -1 if not ok.
492 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser);
494 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
496 return __ivaliduser2(hostf, raddr, luser, ruser, "-");
500 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
501 static int
502 __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
504 struct hostent *hp;
505 u_int32_t laddr;
506 int negate=1; /* Multiply return with this to get -1 instead of 1 */
507 char **pp;
509 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
510 int save_errno;
511 size_t buflen;
512 char *buffer;
513 struct hostent hostbuf;
514 int herr;
515 #endif
517 #ifdef HAVE_NETGROUP
518 /* Check nis netgroup. */
519 if (strncmp ("+@", lhost, 2) == 0)
520 return innetgr (&lhost[2], rhost, NULL, NULL);
522 if (strncmp ("-@", lhost, 2) == 0)
523 return -innetgr (&lhost[2], rhost, NULL, NULL);
524 #endif /* HAVE_NETGROUP */
526 /* -host */
527 if (strncmp ("-", lhost,1) == 0) {
528 negate = -1;
529 lhost++;
530 } else if (strcmp ("+",lhost) == 0) {
531 return 1; /* asking for trouble, but ok.. */
534 /* Try for raw ip address first. */
535 if (isdigit (*lhost) && (laddr = inet_addr (lhost)) != INADDR_NONE)
536 return negate * (! (raddr ^ laddr));
538 /* Better be a hostname. */
539 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
540 buflen = 1024;
541 buffer = malloc(buflen);
542 save_errno = errno;
544 while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
545 != 0) {
546 free(buffer);
547 return (0);
549 free(buffer);
550 __set_errno (save_errno);
551 #else
552 hp = gethostbyname(lhost);
553 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */
555 if (hp == NULL)
556 return 0;
558 /* Spin through ip addresses. */
559 for (pp = hp->h_addr_list; *pp; ++pp)
560 if (!memcmp (&raddr, *pp, sizeof (u_int32_t)))
561 return negate;
563 /* No match. */
564 return (0);
567 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
568 static int
569 __icheckuser (const char *luser, const char *ruser)
573 luser is user entry from .rhosts/hosts.equiv file
574 ruser is user id on remote host
577 #ifdef HAVE_NETGROUP
578 /* [-+]@netgroup */
579 if (strncmp ("+@", luser, 2) == 0)
580 return innetgr (&luser[2], NULL, ruser, NULL);
582 if (strncmp ("-@", luser,2) == 0)
583 return -innetgr (&luser[2], NULL, ruser, NULL);
584 #endif /* HAVE_NETGROUP */
586 /* -user */
587 if (strncmp ("-", luser, 1) == 0)
588 return -(strcmp (&luser[1], ruser) == 0);
590 /* + */
591 if (strcmp ("+", luser) == 0)
592 return 1;
594 /* simple string match */
595 return strcmp (ruser, luser) == 0;
599 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
601 static int
602 __isempty(char *p)
604 while (*p && isspace (*p)) {
605 ++p;
608 return (*p == '\0' || *p == '#') ? 1 : 0 ;
612 * Returns 0 if positive match, -1 if _not_ ok.
614 static int
615 __ivaliduser2(FILE *hostf, u_int32_t raddr, const char *luser,
616 const char *ruser, const char *rhost)
618 register const char *user;
619 register char *p;
620 int hcheck, ucheck;
621 char *buf = NULL;
622 size_t bufsize = 0;
623 int retval = -1;
625 while (getline (&buf, &bufsize, hostf) > 0) {
626 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
627 p = buf;
629 /* Skip empty or comment lines */
630 if (__isempty (p)) {
631 continue;
634 /* Skip lines that are too long. */
635 if (strchr (p, '\n') == NULL) {
636 int ch = getc_unlocked (hostf);
638 while (ch != '\n' && ch != EOF)
639 ch = getc_unlocked (hostf);
640 continue;
643 for (;*p && !isspace(*p); ++p) {
644 *p = tolower (*p);
647 /* Next we want to find the permitted name for the remote user. */
648 if (*p == ' ' || *p == '\t') {
649 /* <nul> terminate hostname and skip spaces */
650 for (*p++='\0'; *p && isspace (*p); ++p);
652 user = p; /* this is the user's name */
653 while (*p && !isspace (*p))
654 ++p; /* find end of user's name */
655 } else
656 user = p;
658 *p = '\0'; /* <nul> terminate username (+host?) */
660 /* buf -> host(?) ; user -> username(?) */
662 /* First check host part */
663 hcheck = __icheckhost (raddr, buf, rhost);
665 if (hcheck < 0)
666 break;
668 if (hcheck) {
669 /* Then check user part */
670 if (! (*user))
671 user = luser;
673 ucheck = __icheckuser (user, ruser);
675 /* Positive 'host user' match? */
676 if (ucheck > 0) {
677 retval = 0;
678 break;
681 /* Negative 'host -user' match? */
682 if (ucheck < 0)
683 break;
685 /* Neither, go on looking for match */
689 free (buf);
691 return retval;