convert // comments to /**/; remove empty #if/#endif pairs. no code changes
[uclibc-ng.git] / libc / inet / rpc / rcmd.c
blob745ea776ae9e087cc309281950ac1b5ac8f262f6
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 <alloca.h>
73 #include <signal.h>
74 #include <fcntl.h>
75 #include <netdb.h>
76 #include <unistd.h>
77 #include <pwd.h>
78 #include <errno.h>
79 #include <stdio.h>
80 #include <stdio_ext.h>
81 #include <ctype.h>
82 #include <string.h>
83 #include <libintl.h>
84 #include <stdlib.h>
85 #ifdef __UCLIBC_HAS_WCHAR__
86 #include <wchar.h>
87 #endif
88 #include <sys/uio.h>
91 /* some forward declarations */
92 static int __ivaliduser2(FILE *hostf, u_int32_t raddr,
93 const char *luser, const char *ruser, const char *rhost);
94 static int iruserok2 (u_int32_t raddr, int superuser, const char *ruser,
95 const char *luser, const char *rhost);
98 int rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
99 char **ahost;
100 u_short rport;
101 const char *locuser, *remuser, *cmd;
102 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 #ifdef __ARCH_USE_MMU__
123 tmphstbuf = alloca (hstbuflen);
124 #else
125 tmphstbuf = malloc (hstbuflen);
126 #endif
128 while (gethostbyname_r (*ahost, &hostbuf, tmphstbuf,
129 hstbuflen, &hp, &herr) != 0 || hp == NULL)
131 if (herr != NETDB_INTERNAL || errno != ERANGE)
133 __set_h_errno (herr);
134 #ifndef __ARCH_USE_MMU__
135 free(tmphstbuf);
136 #endif
137 herror(*ahost);
138 return -1;
140 else
142 /* Enlarge the buffer. */
143 hstbuflen *= 2;
144 #ifdef __ARCH_USE_MMU__
145 tmphstbuf = alloca (hstbuflen);
146 #else
147 free(tmphstbuf);
148 tmphstbuf = malloc (hstbuflen);
149 #endif
152 #ifndef __ARCH_USE_MMU__
153 free(tmphstbuf);
154 #endif
155 #else /* call the non-reentrant version */
156 if ((hp = gethostbyname(*ahost)) == NULL) {
157 return -1;
159 #endif
160 pfd[0].events = POLLIN;
161 pfd[1].events = POLLIN;
163 *ahost = hp->h_name;
164 oldmask = sigblock(sigmask(SIGURG)); /* __sigblock */
165 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
166 s = rresvport(&lport);
167 if (s < 0) {
168 if (errno == EAGAIN)
169 (void)fprintf(stderr,
170 "rcmd: socket: All ports in use\n");
171 else
172 (void)fprintf(stderr, "rcmd: socket: %m\n");
173 sigsetmask(oldmask); /* sigsetmask */
174 return -1;
176 fcntl(s, F_SETOWN, pid);
177 sin.sin_family = hp->h_addrtype;
178 memmove(&sin.sin_addr, hp->h_addr_list[0],
179 MIN (sizeof (sin.sin_addr), hp->h_length));
180 sin.sin_port = rport;
181 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) /* __connect */
182 break;
183 (void)close(s);
184 if (errno == EADDRINUSE) {
185 lport--;
186 continue;
188 if (errno == ECONNREFUSED && timo <= 16) {
189 (void)sleep(timo); /* __sleep */
190 timo *= 2;
191 continue;
193 if (hp->h_addr_list[1] != NULL) {
194 int oerrno = errno;
196 (void)fprintf(stderr, "connect to address %s: ",
197 inet_ntoa(sin.sin_addr));
198 __set_errno (oerrno);
199 perror(0);
200 hp->h_addr_list++;
201 memmove(&sin.sin_addr, hp->h_addr_list[0],
202 MIN (sizeof (sin.sin_addr), hp->h_length));
203 (void)fprintf(stderr, "Trying %s...\n",
204 inet_ntoa(sin.sin_addr));
205 continue;
207 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
208 sigsetmask(oldmask); /* __sigsetmask */
209 return -1;
211 lport--;
212 if (fd2p == 0) {
213 write(s, "", 1);
214 lport = 0;
215 } else {
216 char num[8];
217 int s2 = rresvport(&lport), s3;
218 socklen_t len = sizeof(from);
220 if (s2 < 0)
221 goto bad;
222 listen(s2, 1);
223 (void)snprintf(num, sizeof(num), "%d", lport); /* __snprintf */
224 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
225 (void)fprintf(stderr,
226 "rcmd: write (setting up stderr): %m\n");
227 (void)close(s2);
228 goto bad;
230 pfd[0].fd = s;
231 pfd[1].fd = s2;
232 __set_errno (0);
233 if (poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
234 if (errno != 0)
235 (void)fprintf(stderr, "rcmd: poll (setting up stderr): %m\n");
236 else
237 (void)fprintf(stderr, "poll: protocol failure in circuit setup\n");
238 (void)close(s2);
239 goto bad;
241 s3 = accept(s2, (struct sockaddr *)&from, &len);
242 (void)close(s2);
243 if (s3 < 0) {
244 (void)fprintf(stderr,
245 "rcmd: accept: %m\n");
246 lport = 0;
247 goto bad;
249 *fd2p = s3;
250 from.sin_port = ntohs((u_short)from.sin_port);
251 if (from.sin_family != AF_INET ||
252 from.sin_port >= IPPORT_RESERVED ||
253 from.sin_port < IPPORT_RESERVED / 2) {
254 (void)fprintf(stderr,
255 "socket: protocol failure in circuit setup\n");
256 goto bad2;
259 (void)write(s, locuser, strlen(locuser)+1);
260 (void)write(s, remuser, strlen(remuser)+1);
261 (void)write(s, cmd, strlen(cmd)+1);
262 if (read(s, &c, 1) != 1) {
263 (void)fprintf(stderr,
264 "rcmd: %s: %m\n", *ahost);
265 goto bad2;
267 if (c != 0) {
268 while (read(s, &c, 1) == 1) {
269 (void)write(STDERR_FILENO, &c, 1);
270 if (c == '\n')
271 break;
273 goto bad2;
275 sigsetmask(oldmask);
276 return s;
277 bad2:
278 if (lport)
279 (void)close(*fd2p);
280 bad:
281 (void)close(s);
282 sigsetmask(oldmask);
283 return -1;
286 int rresvport(int *alport)
288 struct sockaddr_in sin;
289 int s;
291 sin.sin_family = AF_INET;
292 sin.sin_addr.s_addr = INADDR_ANY;
293 s = socket(AF_INET, SOCK_STREAM, 0);
294 if (s < 0)
295 return -1;
296 for (;;) {
297 sin.sin_port = htons((u_short)*alport);
298 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
299 return s;
300 if (errno != EADDRINUSE) {
301 (void)close(s);
302 return -1;
304 (*alport)--;
305 if (*alport == IPPORT_RESERVED/2) {
306 (void)close(s);
307 __set_errno (EAGAIN); /* close */
308 return -1;
312 return -1;
314 libc_hidden_def(rresvport)
316 /* This needs to be exported ... while it is not a documented interface
317 * for rcp related apps, it's a required one that is used to control the
318 * rhost behavior. Legacy sucks.
320 int __check_rhosts_file = 1;
322 int ruserok(rhost, superuser, ruser, luser)
323 const char *rhost, *ruser, *luser;
324 int superuser;
326 struct hostent *hp;
327 u_int32_t addr;
328 char **ap;
329 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
330 size_t buflen;
331 char *buffer;
332 int herr;
333 struct hostent hostbuf;
334 #endif
336 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
337 buflen = 1024;
338 #ifdef __ARCH_USE_MMU__
339 buffer = alloca (buflen);
340 #else
341 buffer = malloc (buflen);
342 #endif
344 while (gethostbyname_r (rhost, &hostbuf, buffer,
345 buflen, &hp, &herr) != 0 || hp == NULL)
347 if (herr != NETDB_INTERNAL || errno != ERANGE) {
348 #ifndef __ARCH_USE_MMU__
349 free(buffer);
350 #endif
351 return -1;
352 } else
354 /* Enlarge the buffer. */
355 buflen *= 2;
356 #ifdef __ARCH_USE_MMU__
357 buffer = alloca (buflen);
358 #else
359 free(buffer);
360 buffer = malloc (buflen);
361 #endif
364 #ifndef __ARCH_USE_MMU__
365 free(buffer);
366 #endif
367 #else
368 if ((hp = gethostbyname(rhost)) == NULL) {
369 return -1;
371 #endif
372 for (ap = hp->h_addr_list; *ap; ++ap) {
373 memmove(&addr, *ap, sizeof(addr));
374 if (iruserok2(addr, superuser, ruser, luser, rhost) == 0)
375 return 0;
377 return -1;
381 /* Extremely paranoid file open function. */
382 static FILE *
383 iruserfopen (const char *file, uid_t okuser)
385 struct stat st;
386 char *cp = NULL;
387 FILE *res = NULL;
389 /* If not a regular file, if owned by someone other than user or
390 root, if writeable by anyone but the owner, or if hardlinked
391 anywhere, quit. */
392 if (lstat (file, &st))
393 cp = "lstat failed";
394 else if (!S_ISREG (st.st_mode))
395 cp = "not regular file";
396 else
398 res = fopen (file, "r");
399 if (!res)
400 cp = "cannot open";
401 else if (fstat (fileno (res), &st) < 0)
402 cp = "fstat failed";
403 else if (st.st_uid && st.st_uid != okuser)
404 cp = "bad owner";
405 else if (st.st_mode & (S_IWGRP|S_IWOTH))
406 cp = "writeable by other than owner";
407 else if (st.st_nlink > 1)
408 cp = "hard linked somewhere";
411 /* If there were any problems, quit. */
412 if (cp != NULL)
414 if (res)
415 fclose (res);
416 return NULL;
419 return res;
424 * New .rhosts strategy: We are passed an ip address. We spin through
425 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
426 * has ip addresses, we don't have to trust a nameserver. When it
427 * contains hostnames, we spin through the list of addresses the nameserver
428 * gives us and look for a match.
430 * Returns 0 if ok, -1 if not ok.
432 static int
433 iruserok2 (raddr, superuser, ruser, luser, rhost)
434 u_int32_t raddr;
435 int superuser;
436 const char *ruser, *luser, *rhost;
438 FILE *hostf = NULL;
439 int isbad = -1;
441 if (!superuser)
442 hostf = iruserfopen (_PATH_HEQUIV, 0);
444 if (hostf) {
445 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
446 fclose (hostf);
448 if (!isbad)
449 return 0;
452 if (__check_rhosts_file || superuser) {
453 char *pbuf;
454 struct passwd *pwd;
455 size_t dirlen;
456 uid_t uid;
458 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
459 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
460 struct passwd pwdbuf;
461 #ifdef __ARCH_USE_MMU__
462 char *buffer = alloca (buflen);
463 #else
464 char *buffer = malloc (buflen);
465 #endif
467 if (getpwnam_r (luser, &pwdbuf, buffer,
468 buflen, &pwd) != 0 || pwd == NULL)
470 #ifndef __ARCH_USE_MMU__
471 free(buffer);
472 #endif
473 return -1;
475 #ifndef __ARCH_USE_MMU__
476 free(buffer);
477 #endif
478 #else
479 if ((pwd = getpwnam(luser)) == NULL)
480 return -1;
481 #endif
483 dirlen = strlen (pwd->pw_dir);
484 pbuf = malloc (dirlen + sizeof "/.rhosts");
485 strcpy (pbuf, pwd->pw_dir);
486 strcat (pbuf, "/.rhosts");
488 /* Change effective uid while reading .rhosts. If root and
489 reading an NFS mounted file system, can't read files that
490 are protected read/write owner only. */
491 uid = geteuid ();
492 seteuid (pwd->pw_uid);
493 hostf = iruserfopen (pbuf, pwd->pw_uid);
494 free(pbuf);
496 if (hostf != NULL) {
497 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
498 fclose (hostf);
501 seteuid (uid);
502 return isbad;
504 return -1;
507 /* This is the exported version. */
508 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser);
509 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser)
511 return iruserok2 (raddr, superuser, ruser, luser, "-");
516 * XXX
517 * Don't make static, used by lpd(8).
519 * This function is not used anymore. It is only present because lpd(8)
520 * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
521 * argument. This means that netgroups won't work in .rhost/hosts.equiv
522 * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
523 * or PAM.
524 * Returns 0 if ok, -1 if not ok.
527 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser);
529 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
531 return __ivaliduser2(hostf, raddr, luser, ruser, "-");
535 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
536 static int
537 __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
539 struct hostent *hp;
540 u_int32_t laddr;
541 int negate=1; /* Multiply return with this to get -1 instead of 1 */
542 char **pp;
544 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
545 int save_errno;
546 size_t buflen;
547 char *buffer;
548 struct hostent hostbuf;
549 int herr;
550 #endif
552 #ifdef HAVE_NETGROUP
553 /* Check nis netgroup. */
554 if (strncmp ("+@", lhost, 2) == 0)
555 return innetgr (&lhost[2], rhost, NULL, NULL);
557 if (strncmp ("-@", lhost, 2) == 0)
558 return -innetgr (&lhost[2], rhost, NULL, NULL);
559 #endif /* HAVE_NETGROUP */
561 /* -host */
562 if (strncmp ("-", lhost,1) == 0) {
563 negate = -1;
564 lhost++;
565 } else if (strcmp ("+",lhost) == 0) {
566 return 1; /* asking for trouble, but ok.. */
569 /* Try for raw ip address first. */
570 if (isdigit (*lhost) && (laddr = inet_addr (lhost)) != INADDR_NONE)
571 return negate * (! (raddr ^ laddr));
573 /* Better be a hostname. */
574 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
575 buflen = 1024;
576 buffer = malloc(buflen);
577 save_errno = errno;
579 while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
580 != 0) {
581 free(buffer);
582 return (0);
584 free(buffer);
585 __set_errno (save_errno);
586 #else
587 hp = gethostbyname(lhost);
588 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */
590 if (hp == NULL)
591 return 0;
593 /* Spin through ip addresses. */
594 for (pp = hp->h_addr_list; *pp; ++pp)
595 if (!memcmp (&raddr, *pp, sizeof (u_int32_t)))
596 return negate;
598 /* No match. */
599 return (0);
602 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
603 static int
604 __icheckuser (const char *luser, const char *ruser)
608 luser is user entry from .rhosts/hosts.equiv file
609 ruser is user id on remote host
612 #ifdef HAVE_NETGROUP
613 /* [-+]@netgroup */
614 if (strncmp ("+@", luser, 2) == 0)
615 return innetgr (&luser[2], NULL, ruser, NULL);
617 if (strncmp ("-@", luser,2) == 0)
618 return -innetgr (&luser[2], NULL, ruser, NULL);
619 #endif /* HAVE_NETGROUP */
621 /* -user */
622 if (strncmp ("-", luser, 1) == 0)
623 return -(strcmp (&luser[1], ruser) == 0);
625 /* + */
626 if (strcmp ("+", luser) == 0)
627 return 1;
629 /* simple string match */
630 return strcmp (ruser, luser) == 0;
634 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
636 static int
637 __isempty(char *p)
639 while (*p && isspace (*p)) {
640 ++p;
643 return (*p == '\0' || *p == '#') ? 1 : 0 ;
647 * Returns 0 if positive match, -1 if _not_ ok.
649 static int
650 __ivaliduser2(hostf, raddr, luser, ruser, rhost)
651 FILE *hostf;
652 u_int32_t raddr;
653 const char *luser, *ruser, *rhost;
655 register const char *user;
656 register char *p;
657 int hcheck, ucheck;
658 char *buf = NULL;
659 size_t bufsize = 0;
660 int retval = -1;
662 while (getline (&buf, &bufsize, hostf) > 0) {
663 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
664 p = buf;
666 /* Skip empty or comment lines */
667 if (__isempty (p)) {
668 continue;
671 /* Skip lines that are too long. */
672 if (strchr (p, '\n') == NULL) {
673 int ch = getc_unlocked (hostf);
675 while (ch != '\n' && ch != EOF)
676 ch = getc_unlocked (hostf);
677 continue;
680 for (;*p && !isspace(*p); ++p) {
681 *p = tolower (*p);
684 /* Next we want to find the permitted name for the remote user. */
685 if (*p == ' ' || *p == '\t') {
686 /* <nul> terminate hostname and skip spaces */
687 for (*p++='\0'; *p && isspace (*p); ++p);
689 user = p; /* this is the user's name */
690 while (*p && !isspace (*p))
691 ++p; /* find end of user's name */
692 } else
693 user = p;
695 *p = '\0'; /* <nul> terminate username (+host?) */
697 /* buf -> host(?) ; user -> username(?) */
699 /* First check host part */
700 hcheck = __icheckhost (raddr, buf, rhost);
702 if (hcheck < 0)
703 break;
705 if (hcheck) {
706 /* Then check user part */
707 if (! (*user))
708 user = luser;
710 ucheck = __icheckuser (user, ruser);
712 /* Positive 'host user' match? */
713 if (ucheck > 0) {
714 retval = 0;
715 break;
718 /* Negative 'host -user' match? */
719 if (ucheck < 0)
720 break;
722 /* Neither, go on looking for match */
726 free (buf);
728 return retval;