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