fix eglibc 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 <sys/poll.h>
66 #include <sys/socket.h>
67 #include <sys/stat.h>
68
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71
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 <stdio_ext.h>
80 #include <ctype.h>
81 #include <string.h>
82 #include <libintl.h>
83 #include <stdlib.h>
84 #ifdef __UCLIBC_HAS_WCHAR__
85 #include <wchar.h>
86 #endif
87 #include <sys/uio.h>
88
89
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);
95
96
97 int rcmd(char **ahost, u_short rport, const char *locuser, const char *remuser,
98 const char *cmd, int *fd2p)
99 {
100 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
101 int herr;
102 struct hostent hostbuf;
103 size_t hstbuflen;
104 char *tmphstbuf;
105 #endif
106 struct hostent *hp;
107 struct sockaddr_in sin, from;
108 struct pollfd pfd[2];
109 int32_t oldmask;
110 pid_t pid;
111 int s, lport, timo;
112 char c;
113
114 pid = getpid();
115
116 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
117 hstbuflen = 1024;
118 tmphstbuf = stack_heap_alloc(hstbuflen);
119
120 while (gethostbyname_r (*ahost, &hostbuf, tmphstbuf,
121 hstbuflen, &hp, &herr) != 0 || hp == NULL)
122 {
123 if (herr != NETDB_INTERNAL || errno != ERANGE)
124 {
125 __set_h_errno (herr);
126 stack_heap_free(tmphstbuf);
127 herror(*ahost);
128 return -1;
129 }
130 else
131 {
132 /* Enlarge the buffer. */
133 hstbuflen *= 2;
134 stack_heap_free(tmphstbuf);
135 tmphstbuf = stack_heap_alloc(hstbuflen);
136 }
137 }
138 stack_heap_free(tmphstbuf);
139 #else /* call the non-reentrant version */
140 if ((hp = gethostbyname(*ahost)) == NULL) {
141 return -1;
142 }
143 #endif
144 pfd[0].events = POLLIN;
145 pfd[1].events = POLLIN;
146
147 *ahost = hp->h_name;
148 oldmask = sigblock(sigmask(SIGURG)); /* __sigblock */
149 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
150 s = rresvport(&lport);
151 if (s < 0) {
152 if (errno == EAGAIN)
153 (void)fprintf(stderr,
154 "rcmd: socket: All ports in use\n");
155 else
156 (void)fprintf(stderr, "rcmd: socket: %m\n");
157 sigsetmask(oldmask); /* sigsetmask */
158 return -1;
159 }
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 */
166 break;
167 (void)close(s);
168 if (errno == EADDRINUSE) {
169 lport--;
170 continue;
171 }
172 if (errno == ECONNREFUSED && timo <= 16) {
173 (void)sleep(timo); /* __sleep */
174 timo *= 2;
175 continue;
176 }
177 if (hp->h_addr_list[1] != NULL) {
178 int oerrno = errno;
179
180 (void)fprintf(stderr, "connect to address %s: ",
181 inet_ntoa(sin.sin_addr));
182 __set_errno (oerrno);
183 perror(0);
184 hp->h_addr_list++;
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));
189 continue;
190 }
191 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
192 sigsetmask(oldmask); /* __sigsetmask */
193 return -1;
194 }
195 lport--;
196 if (fd2p == 0) {
197 write(s, "", 1);
198 lport = 0;
199 } else {
200 char num[8];
201 int s2 = rresvport(&lport), s3;
202 socklen_t len = sizeof(from);
203
204 if (s2 < 0)
205 goto bad;
206 listen(s2, 1);
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");
211 (void)close(s2);
212 goto bad;
213 }
214 pfd[0].fd = s;
215 pfd[1].fd = s2;
216 __set_errno (0);
217 if (poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
218 if (errno != 0)
219 (void)fprintf(stderr, "rcmd: poll (setting up stderr): %m\n");
220 else
221 (void)fprintf(stderr, "poll: protocol failure in circuit setup\n");
222 (void)close(s2);
223 goto bad;
224 }
225 s3 = accept(s2, (struct sockaddr *)&from, &len);
226 (void)close(s2);
227 if (s3 < 0) {
228 (void)fprintf(stderr,
229 "rcmd: accept: %m\n");
230 lport = 0;
231 goto bad;
232 }
233 *fd2p = s3;
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");
240 goto bad2;
241 }
242 }
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);
249 goto bad2;
250 }
251 if (c != 0) {
252 while (read(s, &c, 1) == 1) {
253 (void)write(STDERR_FILENO, &c, 1);
254 if (c == '\n')
255 break;
256 }
257 goto bad2;
258 }
259 sigsetmask(oldmask);
260 return s;
261 bad2:
262 if (lport)
263 (void)close(*fd2p);
264 bad:
265 (void)close(s);
266 sigsetmask(oldmask);
267 return -1;
268 }
269
270 int rresvport(int *alport)
271 {
272 struct sockaddr_in sin;
273 int s;
274
275 sin.sin_family = AF_INET;
276 sin.sin_addr.s_addr = INADDR_ANY;
277 s = socket(AF_INET, SOCK_STREAM, 0);
278 if (s < 0)
279 return -1;
280 for (;;) {
281 sin.sin_port = htons((u_short)*alport);
282 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
283 return s;
284 if (errno != EADDRINUSE) {
285 (void)close(s);
286 return -1;
287 }
288 (*alport)--;
289 if (*alport == IPPORT_RESERVED/2) {
290 (void)close(s);
291 __set_errno (EAGAIN); /* close */
292 return -1;
293 }
294 }
295
296 return -1;
297 }
298 libc_hidden_def(rresvport)
299
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.
303 */
304 int __check_rhosts_file = 1;
305
306 int ruserok(const char *rhost, int superuser, const char *ruser,
307 const char *luser)
308 {
309 struct hostent *hp;
310 u_int32_t addr;
311 char **ap;
312 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
313 size_t buflen;
314 char *buffer;
315 int herr;
316 struct hostent hostbuf;
317 #endif
318
319 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
320 buflen = 1024;
321 buffer = stack_heap_alloc(buflen);
322
323 while (gethostbyname_r (rhost, &hostbuf, buffer,
324 buflen, &hp, &herr) != 0 || hp == NULL)
325 {
326 if (herr != NETDB_INTERNAL || errno != ERANGE) {
327 stack_heap_free(buffer);
328 return -1;
329 } else
330 {
331 /* Enlarge the buffer. */
332 buflen *= 2;
333 stack_heap_free(buffer);
334 buffer = stack_heap_alloc(buflen);
335 }
336 }
337 stack_heap_free(buffer);
338 #else
339 if ((hp = gethostbyname(rhost)) == NULL) {
340 return -1;
341 }
342 #endif
343 for (ap = hp->h_addr_list; *ap; ++ap) {
344 memmove(&addr, *ap, sizeof(addr));
345 if (iruserok2(addr, superuser, ruser, luser, rhost) == 0)
346 return 0;
347 }
348 return -1;
349 }
350
351
352 /* Extremely paranoid file open function. */
353 static FILE *
354 iruserfopen (const char *file, uid_t okuser)
355 {
356 struct stat st;
357 char *cp = NULL;
358 FILE *res = NULL;
359
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
362 anywhere, quit. */
363 if (lstat (file, &st))
364 cp = "lstat failed";
365 else if (!S_ISREG (st.st_mode))
366 cp = "not regular file";
367 else
368 {
369 res = fopen (file, "r");
370 if (!res)
371 cp = "cannot open";
372 else if (fstat (fileno (res), &st) < 0)
373 cp = "fstat failed";
374 else if (st.st_uid && st.st_uid != okuser)
375 cp = "bad owner";
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";
380 }
381
382 /* If there were any problems, quit. */
383 if (cp != NULL)
384 {
385 if (res)
386 fclose (res);
387 return NULL;
388 }
389
390 return res;
391 }
392
393
394 /*
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.
400 *
401 * Returns 0 if ok, -1 if not ok.
402 */
403 static int
404 iruserok2 (u_int32_t raddr, int superuser, const char *ruser, const char *luser,
405 const char *rhost)
406 {
407 FILE *hostf = NULL;
408 int isbad = -1;
409
410 if (!superuser)
411 hostf = iruserfopen (_PATH_HEQUIV, 0);
412
413 if (hostf) {
414 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
415 fclose (hostf);
416
417 if (!isbad)
418 return 0;
419 }
420
421 if (__check_rhosts_file || superuser) {
422 char *pbuf;
423 struct passwd *pwd;
424 size_t dirlen;
425 uid_t uid;
426
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);
431
432 if (getpwnam_r (luser, &pwdbuf, buffer,
433 buflen, &pwd) != 0 || pwd == NULL)
434 {
435 stack_heap_free(buffer);
436 return -1;
437 }
438 stack_heap_free(buffer);
439 #else
440 if ((pwd = getpwnam(luser)) == NULL)
441 return -1;
442 #endif
443
444 dirlen = strlen (pwd->pw_dir);
445 pbuf = malloc (dirlen + sizeof "/.rhosts");
446 strcpy (pbuf, pwd->pw_dir);
447 strcat (pbuf, "/.rhosts");
448
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. */
452 uid = geteuid ();
453 seteuid (pwd->pw_uid);
454 hostf = iruserfopen (pbuf, pwd->pw_uid);
455 free(pbuf);
456
457 if (hostf != NULL) {
458 isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
459 fclose (hostf);
460 }
461
462 seteuid (uid);
463 return isbad;
464 }
465 return -1;
466 }
467
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)
471 {
472 return iruserok2 (raddr, superuser, ruser, luser, "-");
473 }
474
475
476 /*
477 * XXX
478 * Don't make static, used by lpd(8).
479 *
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()
484 * or PAM.
485 * Returns 0 if ok, -1 if not ok.
486 */
487 int
488 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser);
489 int
490 __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
491 {
492 return __ivaliduser2(hostf, raddr, luser, ruser, "-");
493 }
494
495
496 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
497 static int
498 __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
499 {
500 struct hostent *hp;
501 u_int32_t laddr;
502 int negate=1; /* Multiply return with this to get -1 instead of 1 */
503 char **pp;
504
505 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
506 int save_errno;
507 size_t buflen;
508 char *buffer;
509 struct hostent hostbuf;
510 int herr;
511 #endif
512
513 #ifdef HAVE_NETGROUP
514 /* Check nis netgroup. */
515 if (strncmp ("+@", lhost, 2) == 0)
516 return innetgr (&lhost[2], rhost, NULL, NULL);
517
518 if (strncmp ("-@", lhost, 2) == 0)
519 return -innetgr (&lhost[2], rhost, NULL, NULL);
520 #endif /* HAVE_NETGROUP */
521
522 /* -host */
523 if (strncmp ("-", lhost,1) == 0) {
524 negate = -1;
525 lhost++;
526 } else if (strcmp ("+",lhost) == 0) {
527 return 1; /* asking for trouble, but ok.. */
528 }
529
530 /* Try for raw ip address first. */
531 if (isdigit (*lhost) && (laddr = inet_addr (lhost)) != INADDR_NONE)
532 return negate * (! (raddr ^ laddr));
533
534 /* Better be a hostname. */
535 #ifdef __UCLIBC_HAS_REENTRANT_RPC__
536 buflen = 1024;
537 buffer = malloc(buflen);
538 save_errno = errno;
539
540 while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
541 != 0) {
542 free(buffer);
543 return (0);
544 }
545 free(buffer);
546 __set_errno (save_errno);
547 #else
548 hp = gethostbyname(lhost);
549 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */
550
551 if (hp == NULL)
552 return 0;
553
554 /* Spin through ip addresses. */
555 for (pp = hp->h_addr_list; *pp; ++pp)
556 if (!memcmp (&raddr, *pp, sizeof (u_int32_t)))
557 return negate;
558
559 /* No match. */
560 return (0);
561 }
562
563 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
564 static int
565 __icheckuser (const char *luser, const char *ruser)
566 {
567
568 /*
569 luser is user entry from .rhosts/hosts.equiv file
570 ruser is user id on remote host
571 */
572
573 #ifdef HAVE_NETGROUP
574 /* [-+]@netgroup */
575 if (strncmp ("+@", luser, 2) == 0)
576 return innetgr (&luser[2], NULL, ruser, NULL);
577
578 if (strncmp ("-@", luser,2) == 0)
579 return -innetgr (&luser[2], NULL, ruser, NULL);
580 #endif /* HAVE_NETGROUP */
581
582 /* -user */
583 if (strncmp ("-", luser, 1) == 0)
584 return -(strcmp (&luser[1], ruser) == 0);
585
586 /* + */
587 if (strcmp ("+", luser) == 0)
588 return 1;
589
590 /* simple string match */
591 return strcmp (ruser, luser) == 0;
592 }
593
594 /*
595 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
596 */
597 static int
598 __isempty(char *p)
599 {
600 while (*p && isspace (*p)) {
601 ++p;
602 }
603
604 return (*p == '\0' || *p == '#') ? 1 : 0 ;
605 }
606
607 /*
608 * Returns 0 if positive match, -1 if _not_ ok.
609 */
610 static int
611 __ivaliduser2(FILE *hostf, u_int32_t raddr, const char *luser,
612 const char *ruser, const char *rhost)
613 {
614 register const char *user;
615 register char *p;
616 int hcheck, ucheck;
617 char *buf = NULL;
618 size_t bufsize = 0;
619 int retval = -1;
620
621 while (getline (&buf, &bufsize, hostf) > 0) {
622 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
623 p = buf;
624
625 /* Skip empty or comment lines */
626 if (__isempty (p)) {
627 continue;
628 }
629
630 /* Skip lines that are too long. */
631 if (strchr (p, '\n') == NULL) {
632 int ch = getc_unlocked (hostf);
633
634 while (ch != '\n' && ch != EOF)
635 ch = getc_unlocked (hostf);
636 continue;
637 }
638
639 for (;*p && !isspace(*p); ++p) {
640 *p = tolower (*p);
641 }
642
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);
647
648 user = p; /* this is the user's name */
649 while (*p && !isspace (*p))
650 ++p; /* find end of user's name */
651 } else
652 user = p;
653
654 *p = '\0'; /* <nul> terminate username (+host?) */
655
656 /* buf -> host(?) ; user -> username(?) */
657
658 /* First check host part */
659 hcheck = __icheckhost (raddr, buf, rhost);
660
661 if (hcheck < 0)
662 break;
663
664 if (hcheck) {
665 /* Then check user part */
666 if (! (*user))
667 user = luser;
668
669 ucheck = __icheckuser (user, ruser);
670
671 /* Positive 'host user' match? */
672 if (ucheck > 0) {
673 retval = 0;
674 break;
675 }
676
677 /* Negative 'host -user' match? */
678 if (ucheck < 0)
679 break;
680
681 /* Neither, go on looking for match */
682 }
683 }
684
685 free (buf);
686
687 return retval;
688 }