fix musl compatibility
[project/librpc-uclibc.git] / rcmd.c
1 /*
2 * Copyright (C) 1998 WIDE Project.
3 * All rights reserved.
4 *
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.
16 *
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.
28 */
29 /*
30 * Copyright (c) 1983, 1993, 1994
31 * The Regents of the University of California. All rights reserved.
32 *
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.
44 *
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.
56 */
57
58 #if 0
59 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
60 #endif /* LIBC_SCCS and not lint */
61
62 #define __UCLIBC_HIDE_DEPRECATED__
63 #include <features.h>
64 #include <sys/param.h>
65 #include <poll.h>
66 #include <sys/socket.h>
67 #include <sys/stat.h>
68 #include <sys/types.h>
69
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
72
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>
89
90 #ifndef _PATH_HEQUIV
91 #define _PATH_HEQUIV "/etc/hosts.equiv"
92 #endif
93
94 int rresvport(int *alport);
95
96 /* some forward declarations */
97 static int __ivaliduser2(FILE *hostf, u_int32_t raddr,
98 const char *luser, const char *ruser, const char *rhost);
99 static int iruserok2 (u_int32_t raddr, int superuser, const char *ruser,
100 const char *luser, const char *rhost);
101
102
103 int rcmd(char **ahost, u_short rport, const char *locuser, const char *remuser,
104 const char *cmd, int *fd2p)
105 {
106 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
107 int herr;
108 struct hostent hostbuf;
109 size_t hstbuflen;
110 char *tmphstbuf;
111 #endif
112 struct hostent *hp;
113 struct sockaddr_in sin, from;
114 struct pollfd pfd[2];
115 sigset_t sig, osig;
116 pid_t pid;
117 int s, lport, timo;
118 char c;
119
120 pid = getpid();
121
122 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
123 hstbuflen = 1024;
124 tmphstbuf = stack_heap_alloc(hstbuflen);
125
126 while (gethostbyname_r (*ahost, &hostbuf, tmphstbuf,
127 hstbuflen, &hp, &herr) != 0 || hp == NULL)
128 {
129 if (herr != NETDB_INTERNAL || errno != ERANGE)
130 {
131 __set_h_errno (herr);
132 stack_heap_free(tmphstbuf);
133 herror(*ahost);
134 return -1;
135 }
136 else
137 {
138 /* Enlarge the buffer. */
139 hstbuflen *= 2;
140 stack_heap_free(tmphstbuf);
141 tmphstbuf = stack_heap_alloc(hstbuflen);
142 }
143 }
144 stack_heap_free(tmphstbuf);
145 #else /* call the non-reentrant version */
146 if ((hp = gethostbyname(*ahost)) == NULL) {
147 return -1;
148 }
149 #endif
150 pfd[0].events = POLLIN;
151 pfd[1].events = POLLIN;
152
153 *ahost = hp->h_name;
154 sigemptyset(&sig);
155 sigaddset(&sig, SIGURG);
156 sigprocmask(SIG_BLOCK, &sig, &osig);
157 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
158 s = rresvport(&lport);
159 if (s < 0) {
160 if (errno == EAGAIN)
161 (void)fprintf(stderr,
162 "rcmd: socket: All ports in use\n");
163 else
164 (void)fprintf(stderr, "rcmd: socket: %m\n");
165 sigprocmask(SIG_SETMASK, &osig, NULL);
166 return -1;
167 }
168 fcntl(s, F_SETOWN, pid);
169 sin.sin_family = hp->h_addrtype;
170 memmove(&sin.sin_addr, hp->h_addr_list[0],
171 MIN (sizeof (sin.sin_addr), hp->h_length));
172 sin.sin_port = rport;
173 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) /* __connect */
174 break;
175 (void)close(s);
176 if (errno == EADDRINUSE) {
177 lport--;
178 continue;
179 }
180 if (errno == ECONNREFUSED && timo <= 16) {
181 (void)sleep(timo); /* __sleep */
182 timo *= 2;
183 continue;
184 }
185 if (hp->h_addr_list[1] != NULL) {
186 int oerrno = errno;
187
188 (void)fprintf(stderr, "connect to address %s: ",
189 inet_ntoa(sin.sin_addr));
190 __set_errno (oerrno);
191 perror(0);
192 hp->h_addr_list++;
193 memmove(&sin.sin_addr, hp->h_addr_list[0],
194 MIN (sizeof (sin.sin_addr), hp->h_length));
195 (void)fprintf(stderr, "Trying %s...\n",
196 inet_ntoa(sin.sin_addr));
197 continue;
198 }
199 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
200 sigprocmask(SIG_SETMASK, &osig, NULL);
201 return -1;
202 }
203 lport--;
204 if (fd2p == 0) {
205 write(s, "", 1);
206 lport = 0;
207 } else {
208 char num[8];
209 int s2 = rresvport(&lport), s3;
210 socklen_t len = sizeof(from);
211
212 if (s2 < 0)
213 goto bad;
214 listen(s2, 1);
215 (void)snprintf(num, sizeof(num), "%d", lport); /* __snprintf */
216 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
217 (void)fprintf(stderr,
218 "rcmd: write (setting up stderr): %m\n");
219 (void)close(s2);
220 goto bad;
221 }
222 pfd[0].fd = s;
223 pfd[1].fd = s2;
224 __set_errno (0);
225 if (poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
226 if (errno != 0)
227 (void)fprintf(stderr, "rcmd: poll (setting up stderr): %m\n");
228 else
229 (void)fprintf(stderr, "poll: protocol failure in circuit setup\n");
230 (void)close(s2);
231 goto bad;
232 }
233 s3 = accept(s2, (struct sockaddr *)&from, &len);
234 (void)close(s2);
235 if (s3 < 0) {
236 (void)fprintf(stderr,
237 "rcmd: accept: %m\n");
238 lport = 0;
239 goto bad;
240 }
241 *fd2p = s3;
242 from.sin_port = ntohs((u_short)from.sin_port);
243 if (from.sin_family != AF_INET ||
244 from.sin_port >= IPPORT_RESERVED ||
245 from.sin_port < IPPORT_RESERVED / 2) {
246 (void)fprintf(stderr,
247 "socket: protocol failure in circuit setup\n");
248 goto bad2;
249 }
250 }
251 (void)write(s, locuser, strlen(locuser)+1);
252 (void)write(s, remuser, strlen(remuser)+1);
253 (void)write(s, cmd, strlen(cmd)+1);
254 if (read(s, &c, 1) != 1) {
255 (void)fprintf(stderr,
256 "rcmd: %s: %m\n", *ahost);
257 goto bad2;
258 }
259 if (c != 0) {
260 while (read(s, &c, 1) == 1) {
261 (void)write(STDERR_FILENO, &c, 1);
262 if (c == '\n')
263 break;
264 }
265 goto bad2;
266 }
267 sigprocmask(SIG_SETMASK, &osig, NULL);
268 return s;
269 bad2:
270 if (lport)
271 (void)close(*fd2p);
272 bad:
273 (void)close(s);
274 sigprocmask(SIG_SETMASK, &osig, NULL);
275 return -1;
276 }
277
278 int rresvport(int *alport)
279 {
280 struct sockaddr_in sin;
281 int s;
282
283 sin.sin_family = AF_INET;
284 sin.sin_addr.s_addr = INADDR_ANY;
285 s = socket(AF_INET, SOCK_STREAM, 0);
286 if (s < 0)
287 return -1;
288 for (;;) {
289 sin.sin_port = htons((u_short)*alport);
290 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
291 return s;
292 if (errno != EADDRINUSE) {
293 (void)close(s);
294 return -1;
295 }
296 (*alport)--;
297 if (*alport == IPPORT_RESERVED/2) {
298 (void)close(s);
299 __set_errno (EAGAIN); /* close */
300 return -1;
301 }
302 }
303
304 return -1;
305 }
306 libc_hidden_def(rresvport)
307
308 /* This needs to be exported ... while it is not a documented interface
309 * for rcp related apps, it's a required one that is used to control the
310 * rhost behavior. Legacy sucks.
311 */
312 int __check_rhosts_file = 1;
313
314 int ruserok(const char *rhost, int superuser, const char *ruser,
315 const char *luser)
316 {
317 struct hostent *hp;
318 u_int32_t addr;
319 char **ap;
320 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
321 size_t buflen;
322 char *buffer;
323 int herr;
324 struct hostent hostbuf;
325 #endif
326
327 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
328 buflen = 1024;
329 buffer = stack_heap_alloc(buflen);
330
331 while (gethostbyname_r (rhost, &hostbuf, buffer,
332 buflen, &hp, &herr) != 0 || hp == NULL)
333 {
334 if (herr != NETDB_INTERNAL || errno != ERANGE) {
335 stack_heap_free(buffer);
336 return -1;
337 } else
338 {
339 /* Enlarge the buffer. */
340 buflen *= 2;
341 stack_heap_free(buffer);
342 buffer = stack_heap_alloc(buflen);
343 }
344 }
345 stack_heap_free(buffer);
346 #else
347 if ((hp = gethostbyname(rhost)) == NULL) {
348 return -1;
349 }
350 #endif
351 for (ap = hp->h_addr_list; *ap; ++ap) {
352 memmove(&addr, *ap, sizeof(addr));
353 if (iruserok2(addr, superuser, ruser, luser, rhost) == 0)
354 return 0;
355 }
356 return -1;
357 }
358
359
360 /* Extremely paranoid file open function. */
361 static FILE *
362 iruserfopen (const char *file, uid_t okuser)
363 {
364 struct stat st;
365 char *cp = NULL;
366 FILE *res = NULL;
367
368 /* If not a regular file, if owned by someone other than user or
369 root, if writeable by anyone but the owner, or if hardlinked
370 anywhere, quit. */
371 if (lstat (file, &st))
372 cp = "lstat failed";
373 else if (!S_ISREG (st.st_mode))
374 cp = "not regular file";
375 else
376 {
377 res = fopen (file, "r");
378 if (!res)
379 cp = "cannot open";
380 else if (fstat (fileno (res), &st) < 0)
381 cp = "fstat failed";
382 else if (st.st_uid && st.st_uid != okuser)
383 cp = "bad owner";
384 else if (st.st_mode & (S_IWGRP|S_IWOTH))
385 cp = "writeable by other than owner";
386 else if (st.st_nlink > 1)
387 cp = "hard linked somewhere";
388 }
389
390 /* If there were any problems, quit. */
391 if (cp != NULL)
392 {
393 if (res)
394 fclose (res);
395 return NULL;
396 }
397
398 return res;
399 }
400
401
402 /*
403 * New .rhosts strategy: We are passed an ip address. We spin through
404 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
405 * has ip addresses, we don't have to trust a nameserver. When it
406 * contains hostnames, we spin through the list of addresses the nameserver
407 * gives us and look for a match.
408 *
409 * Returns 0 if ok, -1 if not ok.
410 */
411 static int
412 iruserok2 (u_int32_t raddr, int superuser, const char *ruser, const char *luser,
413 const char *rhost)
414 {
415 FILE *hostf = NULL;
416 int isbad = -1;
417
418 if (!superuser)
419 hostf = iruserfopen (_PATH_HEQUIV, 0);
420
421 if (hostf) {
422 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
423 fclose (hostf);
424
425 if (!isbad)
426 return 0;
427 }
428
429 if (__check_rhosts_file || superuser) {
430 char *pbuf;
431 struct passwd *pwd;
432 size_t dirlen;
433 uid_t uid;
434
435 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
436 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
437 struct passwd pwdbuf;
438 char *buffer = stack_heap_alloc(buflen);
439
440 if (getpwnam_r (luser, &pwdbuf, buffer,
441 buflen, &pwd) != 0 || pwd == NULL)
442 {
443 stack_heap_free(buffer);
444 return -1;
445 }
446 stack_heap_free(buffer);
447 #else
448 if ((pwd = getpwnam(luser)) == NULL)
449 return -1;
450 #endif
451
452 dirlen = strlen (pwd->pw_dir);
453 pbuf = malloc (dirlen + sizeof "/.rhosts");
454 strcpy (pbuf, pwd->pw_dir);
455 strcat (pbuf, "/.rhosts");
456
457 /* Change effective uid while reading .rhosts. If root and
458 reading an NFS mounted file system, can't read files that
459 are protected read/write owner only. */
460 uid = geteuid ();
461 seteuid (pwd->pw_uid);
462 hostf = iruserfopen (pbuf, pwd->pw_uid);
463 free(pbuf);
464
465 if (hostf != NULL) {
466 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
467 fclose (hostf);
468 }
469
470 seteuid (uid);
471 return isbad;
472 }
473 return -1;
474 }
475
476 /* This is the exported version. */
477 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser);
478 int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser)
479 {
480 return iruserok2 (raddr, superuser, ruser, luser, "-");
481 }
482
483
484 /*
485 * XXX
486 * Don't make static, used by lpd(8).
487 *
488 * This function is not used anymore. It is only present because lpd(8)
489 * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
490 * argument. This means that netgroups won't work in .rhost/hosts.equiv
491 * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
492 * or PAM.
493 * Returns 0 if ok, -1 if not ok.
494 */
495 int
496 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser);
497 int
498 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
499 {
500 return __ivaliduser2(hostf, raddr, luser, ruser, "-");
501 }
502
503
504 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
505 static int
506 __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
507 {
508 struct hostent *hp;
509 u_int32_t laddr;
510 int negate=1; /* Multiply return with this to get -1 instead of 1 */
511 char **pp;
512
513 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
514 int save_errno;
515 size_t buflen;
516 char *buffer;
517 struct hostent hostbuf;
518 int herr;
519 #endif
520
521 #ifdef HAVE_NETGROUP
522 /* Check nis netgroup. */
523 if (strncmp ("+@", lhost, 2) == 0)
524 return innetgr (&lhost[2], rhost, NULL, NULL);
525
526 if (strncmp ("-@", lhost, 2) == 0)
527 return -innetgr (&lhost[2], rhost, NULL, NULL);
528 #endif /* HAVE_NETGROUP */
529
530 /* -host */
531 if (strncmp ("-", lhost,1) == 0) {
532 negate = -1;
533 lhost++;
534 } else if (strcmp ("+",lhost) == 0) {
535 return 1; /* asking for trouble, but ok.. */
536 }
537
538 /* Try for raw ip address first. */
539 if (isdigit (*lhost) && (laddr = inet_addr (lhost)) != INADDR_NONE)
540 return negate * (! (raddr ^ laddr));
541
542 /* Better be a hostname. */
543 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
544 buflen = 1024;
545 buffer = malloc(buflen);
546 save_errno = errno;
547
548 while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
549 != 0) {
550 free(buffer);
551 return (0);
552 }
553 free(buffer);
554 __set_errno (save_errno);
555 #else
556 hp = gethostbyname(lhost);
557 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */
558
559 if (hp == NULL)
560 return 0;
561
562 /* Spin through ip addresses. */
563 for (pp = hp->h_addr_list; *pp; ++pp)
564 if (!memcmp (&raddr, *pp, sizeof (u_int32_t)))
565 return negate;
566
567 /* No match. */
568 return (0);
569 }
570
571 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
572 static int
573 __icheckuser (const char *luser, const char *ruser)
574 {
575
576 /*
577 luser is user entry from .rhosts/hosts.equiv file
578 ruser is user id on remote host
579 */
580
581 #ifdef HAVE_NETGROUP
582 /* [-+]@netgroup */
583 if (strncmp ("+@", luser, 2) == 0)
584 return innetgr (&luser[2], NULL, ruser, NULL);
585
586 if (strncmp ("-@", luser,2) == 0)
587 return -innetgr (&luser[2], NULL, ruser, NULL);
588 #endif /* HAVE_NETGROUP */
589
590 /* -user */
591 if (strncmp ("-", luser, 1) == 0)
592 return -(strcmp (&luser[1], ruser) == 0);
593
594 /* + */
595 if (strcmp ("+", luser) == 0)
596 return 1;
597
598 /* simple string match */
599 return strcmp (ruser, luser) == 0;
600 }
601
602 /*
603 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
604 */
605 static int
606 __isempty(char *p)
607 {
608 while (*p && isspace (*p)) {
609 ++p;
610 }
611
612 return (*p == '\0' || *p == '#') ? 1 : 0 ;
613 }
614
615 /*
616 * Returns 0 if positive match, -1 if _not_ ok.
617 */
618 static int
619 __ivaliduser2(FILE *hostf, u_int32_t raddr, const char *luser,
620 const char *ruser, const char *rhost)
621 {
622 register const char *user;
623 register char *p;
624 int hcheck, ucheck;
625 char *buf = NULL;
626 size_t bufsize = 0;
627 int retval = -1;
628
629 while (getline (&buf, &bufsize, hostf) > 0) {
630 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
631 p = buf;
632
633 /* Skip empty or comment lines */
634 if (__isempty (p)) {
635 continue;
636 }
637
638 /* Skip lines that are too long. */
639 if (strchr (p, '\n') == NULL) {
640 int ch = getc_unlocked (hostf);
641
642 while (ch != '\n' && ch != EOF)
643 ch = getc_unlocked (hostf);
644 continue;
645 }
646
647 for (;*p && !isspace(*p); ++p) {
648 *p = tolower (*p);
649 }
650
651 /* Next we want to find the permitted name for the remote user. */
652 if (*p == ' ' || *p == '\t') {
653 /* <nul> terminate hostname and skip spaces */
654 for (*p++='\0'; *p && isspace (*p); ++p);
655
656 user = p; /* this is the user's name */
657 while (*p && !isspace (*p))
658 ++p; /* find end of user's name */
659 } else
660 user = p;
661
662 *p = '\0'; /* <nul> terminate username (+host?) */
663
664 /* buf -> host(?) ; user -> username(?) */
665
666 /* First check host part */
667 hcheck = __icheckhost (raddr, buf, rhost);
668
669 if (hcheck < 0)
670 break;
671
672 if (hcheck) {
673 /* Then check user part */
674 if (! (*user))
675 user = luser;
676
677 ucheck = __icheckuser (user, ruser);
678
679 /* Positive 'host user' match? */
680 if (ucheck > 0) {
681 retval = 0;
682 break;
683 }
684
685 /* Negative 'host -user' match? */
686 if (ucheck < 0)
687 break;
688
689 /* Neither, go on looking for match */
690 }
691 }
692
693 free (buf);
694
695 return retval;
696 }