summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/rcmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/rcmd.c')
-rw-r--r--src/lib/libc/net/rcmd.c687
1 files changed, 687 insertions, 0 deletions
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c
new file mode 100644
index 0000000000..e2b9199492
--- /dev/null
+++ b/src/lib/libc/net/rcmd.c
@@ -0,0 +1,687 @@
1/*
2 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
3 * Copyright (c) 1983, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/param.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34
35#include <netinet/in.h>
36#include <arpa/inet.h>
37
38#include <signal.h>
39#include <fcntl.h>
40#include <netdb.h>
41#include <unistd.h>
42#include <pwd.h>
43#include <errno.h>
44#include <stdio.h>
45#include <ctype.h>
46#include <string.h>
47#include <syslog.h>
48#include <stdlib.h>
49#include <netgroup.h>
50
51int __ivaliduser(FILE *, in_addr_t, const char *, const char *);
52int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t,
53 const char *, const char *);
54static int __icheckhost(struct sockaddr *, socklen_t, const char *);
55static char *__gethostloop(struct sockaddr *, socklen_t);
56
57int
58rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
59 const char *cmd, int *fd2p)
60{
61 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
62}
63
64int
65rcmd_af(char **ahost, int porta, const char *locuser, const char *remuser,
66 const char *cmd, int *fd2p, int af)
67{
68 static char hbuf[MAXHOSTNAMELEN];
69 char pbuf[NI_MAXSERV];
70 struct addrinfo hints, *res, *r;
71 int error;
72 struct sockaddr_storage from;
73 fd_set *readsp = NULL;
74 sigset_t oldmask, mask;
75 pid_t pid;
76 int s, lport, timo;
77 char c, *p;
78 int refused;
79 in_port_t rport = porta;
80
81 /* call rcmdsh() with specified remote shell if appropriate. */
82 if (!issetugid() && (p = getenv("RSH")) && *p) {
83 struct servent *sp = getservbyname("shell", "tcp");
84
85 if (sp && sp->s_port == rport)
86 return (rcmdsh(ahost, rport, locuser, remuser,
87 cmd, p));
88 }
89
90 /* use rsh(1) if non-root and remote port is shell. */
91 if (geteuid()) {
92 struct servent *sp = getservbyname("shell", "tcp");
93
94 if (sp && sp->s_port == rport)
95 return (rcmdsh(ahost, rport, locuser, remuser,
96 cmd, NULL));
97 }
98
99 pid = getpid();
100 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport));
101 memset(&hints, 0, sizeof(hints));
102 hints.ai_family = af;
103 hints.ai_socktype = SOCK_STREAM;
104 hints.ai_flags = AI_CANONNAME;
105 error = getaddrinfo(*ahost, pbuf, &hints, &res);
106 if (error) {
107#if 0
108 warnx("%s: %s", *ahost, gai_strerror(error));
109#endif
110 return (-1);
111 }
112 if (res->ai_canonname) {
113 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf));
114 *ahost = hbuf;
115 } else
116 ; /*XXX*/
117
118 r = res;
119 refused = 0;
120 sigemptyset(&mask);
121 sigaddset(&mask, SIGURG);
122 sigprocmask(SIG_BLOCK, &mask, &oldmask);
123 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
124 s = rresvport_af(&lport, r->ai_family);
125 if (s < 0) {
126 if (errno == EAGAIN)
127 (void)fprintf(stderr,
128 "rcmd: socket: All ports in use\n");
129 else
130 (void)fprintf(stderr, "rcmd: socket: %s\n",
131 strerror(errno));
132 if (r->ai_next) {
133 r = r->ai_next;
134 continue;
135 } else {
136 sigprocmask(SIG_SETMASK, &oldmask, NULL);
137 freeaddrinfo(res);
138 return (-1);
139 }
140 }
141 fcntl(s, F_SETOWN, pid);
142 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0)
143 break;
144 (void)close(s);
145 if (errno == EADDRINUSE) {
146 lport--;
147 continue;
148 }
149 if (errno == ECONNREFUSED)
150 refused++;
151 if (r->ai_next) {
152 int oerrno = errno;
153 char hbuf[NI_MAXHOST];
154 const int niflags = NI_NUMERICHOST;
155
156 hbuf[0] = '\0';
157 if (getnameinfo(r->ai_addr, r->ai_addrlen,
158 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
159 strlcpy(hbuf, "(invalid)", sizeof hbuf);
160 (void)fprintf(stderr, "connect to address %s: ", hbuf);
161 errno = oerrno;
162 perror(0);
163 r = r->ai_next;
164 hbuf[0] = '\0';
165 if (getnameinfo(r->ai_addr, r->ai_addrlen,
166 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
167 strlcpy(hbuf, "(invalid)", sizeof hbuf);
168 (void)fprintf(stderr, "Trying %s...\n", hbuf);
169 continue;
170 }
171 if (refused && timo <= 16) {
172 (void)sleep(timo);
173 timo *= 2;
174 r = res;
175 refused = 0;
176 continue;
177 }
178 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname,
179 strerror(errno));
180 sigprocmask(SIG_SETMASK, &oldmask, NULL);
181 freeaddrinfo(res);
182 return (-1);
183 }
184 /* given "af" can be PF_UNSPEC, we need the real af for "s" */
185 af = r->ai_family;
186 freeaddrinfo(res);
187#if 0
188 /*
189 * try to rresvport() to the same port. This will make rresvport()
190 * fail it's first bind, resulting in it choosing a random port.
191 */
192 lport--;
193#endif
194 if (fd2p == 0) {
195 write(s, "", 1);
196 lport = 0;
197 } else {
198 char num[8];
199 int s2 = rresvport_af(&lport, af), s3;
200 socklen_t len = sizeof(from);
201 int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask);
202
203 if (s2 < 0)
204 goto bad;
205 readsp = (fd_set *)malloc(fdssize);
206 if (readsp == NULL) {
207 close(s2);
208 goto bad;
209 }
210 listen(s2, 1);
211 (void)snprintf(num, sizeof(num), "%d", lport);
212 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
213 (void)fprintf(stderr,
214 "rcmd: write (setting up stderr): %s\n",
215 strerror(errno));
216 (void)close(s2);
217 goto bad;
218 }
219again:
220 bzero(readsp, fdssize);
221 FD_SET(s, readsp);
222 FD_SET(s2, readsp);
223 errno = 0;
224 if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 ||
225 !FD_ISSET(s2, readsp)) {
226 if (errno != 0)
227 (void)fprintf(stderr,
228 "rcmd: select (setting up stderr): %s\n",
229 strerror(errno));
230 else
231 (void)fprintf(stderr,
232 "select: protocol failure in circuit setup\n");
233 (void)close(s2);
234 goto bad;
235 }
236 s3 = accept(s2, (struct sockaddr *)&from, &len);
237 if (s3 < 0) {
238 (void)fprintf(stderr,
239 "rcmd: accept: %s\n", strerror(errno));
240 lport = 0;
241 close(s2);
242 goto bad;
243 }
244
245 /*
246 * XXX careful for ftp bounce attacks. If discovered, shut them
247 * down and check for the real auxiliary channel to connect.
248 */
249 switch (from.ss_family) {
250 case AF_INET:
251 case AF_INET6:
252 if (getnameinfo((struct sockaddr *)&from, len,
253 NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 &&
254 atoi(num) != 20) {
255 break;
256 }
257 close(s3);
258 goto again;
259 default:
260 break;
261 }
262 (void)close(s2);
263
264 *fd2p = s3;
265 switch (from.ss_family) {
266 case AF_INET:
267 case AF_INET6:
268 if (getnameinfo((struct sockaddr *)&from, len,
269 NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 ||
270 (atoi(num) >= IPPORT_RESERVED ||
271 atoi(num) < IPPORT_RESERVED / 2)) {
272 (void)fprintf(stderr,
273 "socket: protocol failure in circuit setup.\n");
274 goto bad2;
275 }
276 break;
277 default:
278 break;
279 }
280 }
281 (void)write(s, locuser, strlen(locuser)+1);
282 (void)write(s, remuser, strlen(remuser)+1);
283 (void)write(s, cmd, strlen(cmd)+1);
284 if (read(s, &c, 1) != 1) {
285 (void)fprintf(stderr,
286 "rcmd: %s: %s\n", *ahost, strerror(errno));
287 goto bad2;
288 }
289 if (c != 0) {
290 while (read(s, &c, 1) == 1) {
291 (void)write(STDERR_FILENO, &c, 1);
292 if (c == '\n')
293 break;
294 }
295 goto bad2;
296 }
297 sigprocmask(SIG_SETMASK, &oldmask, NULL);
298 free(readsp);
299 return (s);
300bad2:
301 if (lport)
302 (void)close(*fd2p);
303bad:
304 if (readsp)
305 free(readsp);
306 (void)close(s);
307 sigprocmask(SIG_SETMASK, &oldmask, NULL);
308 return (-1);
309}
310
311int __check_rhosts_file = 1;
312char *__rcmd_errstr;
313
314int
315ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
316{
317 struct addrinfo hints, *res, *r;
318 int error;
319
320 memset(&hints, 0, sizeof(hints));
321 hints.ai_family = PF_UNSPEC;
322 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
323 error = getaddrinfo(rhost, "0", &hints, &res);
324 if (error)
325 return (-1);
326
327 for (r = res; r; r = r->ai_next) {
328 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
329 luser) == 0) {
330 freeaddrinfo(res);
331 return (0);
332 }
333 }
334 freeaddrinfo(res);
335 return (-1);
336}
337
338/*
339 * New .rhosts strategy: We are passed an ip address. We spin through
340 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
341 * has ip addresses, we don't have to trust a nameserver. When it
342 * contains hostnames, we spin through the list of addresses the nameserver
343 * gives us and look for a match.
344 *
345 * Returns 0 if ok, -1 if not ok.
346 */
347int
348iruserok(u_int32_t raddr, int superuser, const char *ruser, const char *luser)
349{
350 struct sockaddr_in sin;
351
352 memset(&sin, 0, sizeof(sin));
353 sin.sin_family = AF_INET;
354 sin.sin_len = sizeof(struct sockaddr_in);
355 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
356 return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser,
357 luser);
358}
359
360int
361iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser,
362 const char *luser)
363{
364 struct sockaddr *sa;
365 char *cp;
366 struct stat sbuf;
367 struct passwd *pwd;
368 FILE *hostf;
369 uid_t uid;
370 int first;
371 char pbuf[MAXPATHLEN];
372
373 sa = (struct sockaddr *)raddr;
374 first = 1;
375 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
376again:
377 if (hostf) {
378 if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) {
379 (void)fclose(hostf);
380 return (0);
381 }
382 (void)fclose(hostf);
383 }
384 if (first == 1 && (__check_rhosts_file || superuser)) {
385 first = 0;
386 if ((pwd = getpwnam(luser)) == NULL)
387 return (-1);
388 snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir);
389
390 /*
391 * Change effective uid while opening .rhosts. If root and
392 * reading an NFS mounted file system, can't read files that
393 * are protected read/write owner only.
394 */
395 uid = geteuid();
396 (void)seteuid(pwd->pw_uid);
397 hostf = fopen(pbuf, "r");
398 (void)seteuid(uid);
399
400 if (hostf == NULL)
401 return (-1);
402 /*
403 * If not a regular file, or is owned by someone other than
404 * user or root or if writeable by anyone but the owner, quit.
405 */
406 cp = NULL;
407 if (lstat(pbuf, &sbuf) < 0)
408 cp = ".rhosts lstat failed";
409 else if (!S_ISREG(sbuf.st_mode))
410 cp = ".rhosts not regular file";
411 else if (fstat(fileno(hostf), &sbuf) < 0)
412 cp = ".rhosts fstat failed";
413 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
414 cp = "bad .rhosts owner";
415 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
416 cp = ".rhosts writable by other than owner";
417 /* If there were any problems, quit. */
418 if (cp) {
419 __rcmd_errstr = cp;
420 (void)fclose(hostf);
421 return (-1);
422 }
423 goto again;
424 }
425 return (-1);
426}
427
428/*
429 * XXX
430 * Don't make static, used by lpd(8).
431 *
432 * Returns 0 if ok, -1 if not ok.
433 */
434int
435__ivaliduser(FILE *hostf, in_addr_t raddrl, const char *luser,
436 const char *ruser)
437{
438 struct sockaddr_in sin;
439
440 memset(&sin, 0, sizeof(sin));
441 sin.sin_family = AF_INET;
442 sin.sin_len = sizeof(struct sockaddr_in);
443 memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr));
444 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
445 luser, ruser);
446}
447
448int
449__ivaliduser_sa(FILE *hostf, struct sockaddr *raddr, socklen_t salen,
450 const char *luser, const char *ruser)
451{
452 char *user, *p;
453 char *buf;
454 const char *auser, *ahost;
455 int hostok, userok;
456 char *rhost = (char *)-1;
457 char domain[MAXHOSTNAMELEN];
458 size_t buflen;
459
460 getdomainname(domain, sizeof(domain));
461
462 while ((buf = fgetln(hostf, &buflen))) {
463 p = buf;
464 if (*p == '#')
465 continue;
466 while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') {
467 if (!isprint(*p))
468 goto bail;
469 *p = isupper(*p) ? tolower(*p) : *p;
470 p++;
471 }
472 if (p >= buf + buflen)
473 continue;
474 if (*p == ' ' || *p == '\t') {
475 *p++ = '\0';
476 while (p < buf + buflen && (*p == ' ' || *p == '\t'))
477 p++;
478 if (p >= buf + buflen)
479 continue;
480 user = p;
481 while (p < buf + buflen && *p != '\n' && *p != ' ' &&
482 *p != '\t') {
483 if (!isprint(*p))
484 goto bail;
485 p++;
486 }
487 } else
488 user = p;
489 *p = '\0';
490
491 if (p == buf)
492 continue;
493
494 auser = *user ? user : luser;
495 ahost = buf;
496
497 if (strlen(ahost) >= MAXHOSTNAMELEN)
498 continue;
499
500 /*
501 * innetgr() must lookup a hostname (we do not attempt
502 * to change the semantics so that netgroups may have
503 * #.#.#.# addresses in the list.)
504 */
505 if (ahost[0] == '+')
506 switch (ahost[1]) {
507 case '\0':
508 hostok = 1;
509 break;
510 case '@':
511 if (rhost == (char *)-1)
512 rhost = __gethostloop(raddr, salen);
513 hostok = 0;
514 if (rhost)
515 hostok = innetgr(&ahost[2], rhost,
516 NULL, domain);
517 break;
518 default:
519 hostok = __icheckhost(raddr, salen, &ahost[1]);
520 break;
521 }
522 else if (ahost[0] == '-')
523 switch (ahost[1]) {
524 case '\0':
525 hostok = -1;
526 break;
527 case '@':
528 if (rhost == (char *)-1)
529 rhost = __gethostloop(raddr, salen);
530 hostok = 0;
531 if (rhost)
532 hostok = -innetgr(&ahost[2], rhost,
533 NULL, domain);
534 break;
535 default:
536 hostok = -__icheckhost(raddr, salen, &ahost[1]);
537 break;
538 }
539 else
540 hostok = __icheckhost(raddr, salen, ahost);
541
542
543 if (auser[0] == '+')
544 switch (auser[1]) {
545 case '\0':
546 userok = 1;
547 break;
548 case '@':
549 userok = innetgr(&auser[2], NULL, ruser,
550 domain);
551 break;
552 default:
553 userok = strcmp(ruser, &auser[1]) ? 0 : 1;
554 break;
555 }
556 else if (auser[0] == '-')
557 switch (auser[1]) {
558 case '\0':
559 userok = -1;
560 break;
561 case '@':
562 userok = -innetgr(&auser[2], NULL, ruser,
563 domain);
564 break;
565 default:
566 userok = strcmp(ruser, &auser[1]) ? 0 : -1;
567 break;
568 }
569 else
570 userok = strcmp(ruser, auser) ? 0 : 1;
571
572 /* Check if one component did not match */
573 if (hostok == 0 || userok == 0)
574 continue;
575
576 /* Check if we got a forbidden pair */
577 if (userok <= -1 || hostok <= -1)
578 return (-1);
579
580 /* Check if we got a valid pair */
581 if (hostok >= 1 && userok >= 1)
582 return (0);
583 }
584bail:
585 return (-1);
586}
587
588/*
589 * Returns "true" if match, 0 if no match. If we do not find any
590 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
591 */
592static int
593__icheckhost(struct sockaddr *raddr, socklen_t salen, const char *lhost)
594{
595 struct addrinfo hints, *res, *r;
596 char h1[NI_MAXHOST], h2[NI_MAXHOST];
597 int error;
598 const int niflags = NI_NUMERICHOST;
599
600 h1[0] = '\0';
601 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
602 niflags) != 0)
603 return (0);
604
605 /* Resolve laddr into sockaddr */
606 memset(&hints, 0, sizeof(hints));
607 hints.ai_family = raddr->sa_family;
608 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
609 res = NULL;
610 error = getaddrinfo(lhost, "0", &hints, &res);
611 if (error)
612 return (0);
613
614 /*
615 * Try string comparisons between raddr and laddr.
616 */
617 for (r = res; r; r = r->ai_next) {
618 h2[0] = '\0';
619 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
620 NULL, 0, niflags) != 0)
621 continue;
622 if (strcmp(h1, h2) == 0) {
623 freeaddrinfo(res);
624 return (1);
625 }
626 }
627
628 /* No match. */
629 freeaddrinfo(res);
630 return (0);
631}
632
633/*
634 * Return the hostname associated with the supplied address.
635 * Do a reverse lookup as well for security. If a loop cannot
636 * be found, pack the result of inet_ntoa() into the string.
637 */
638static char *
639__gethostloop(struct sockaddr *raddr, socklen_t salen)
640{
641 static char remotehost[NI_MAXHOST];
642 char h1[NI_MAXHOST], h2[NI_MAXHOST];
643 struct addrinfo hints, *res, *r;
644 int error;
645 const int niflags = NI_NUMERICHOST;
646
647 h1[0] = remotehost[0] = '\0';
648 if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost),
649 NULL, 0, NI_NAMEREQD) != 0)
650 return (NULL);
651 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
652 niflags) != 0)
653 return (NULL);
654
655 /*
656 * Look up the name and check that the supplied
657 * address is in the list
658 */
659 memset(&hints, 0, sizeof(hints));
660 hints.ai_family = raddr->sa_family;
661 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
662 hints.ai_flags = AI_CANONNAME;
663 res = NULL;
664 error = getaddrinfo(remotehost, "0", &hints, &res);
665 if (error)
666 return (NULL);
667
668 for (r = res; r; r = r->ai_next) {
669 h2[0] = '\0';
670 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
671 NULL, 0, niflags) != 0)
672 continue;
673 if (strcmp(h1, h2) == 0) {
674 freeaddrinfo(res);
675 return (remotehost);
676 }
677 }
678
679 /*
680 * either the DNS adminstrator has made a configuration
681 * mistake, or someone has attempted to spoof us
682 */
683 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s",
684 h1, res->ai_canonname ? res->ai_canonname : remotehost);
685 freeaddrinfo(res);
686 return (NULL);
687}