xref: /openbsd-src/lib/libc/net/rcmd.c (revision efa7ea8e4a2045e2c9ec25a0927705f584752b63)
1 /*	$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
39 #else
40 static char *rcsid = "$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 
51 #include <signal.h>
52 #include <fcntl.h>
53 #include <netdb.h>
54 #include <unistd.h>
55 #include <pwd.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <ctype.h>
59 #include <string.h>
60 #include <syslog.h>
61 
62 int	__ivaliduser __P((FILE *, u_long, const char *, const char *));
63 static int __icheckhost __P((u_int32_t, const char *));
64 static char *__gethostloop __P((u_int32_t));
65 
66 int
67 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
68 	char **ahost;
69 	u_short rport;
70 	const char *locuser, *remuser, *cmd;
71 	int *fd2p;
72 {
73 	struct hostent *hp;
74 	struct sockaddr_in sin, from;
75 	fd_set reads;
76 	int oldmask;
77 	pid_t pid;
78 	int s, lport, timo;
79 	char c;
80 
81 	pid = getpid();
82 	hp = gethostbyname(*ahost);
83 	if (hp == NULL) {
84 		herror(*ahost);
85 		return (-1);
86 	}
87 	*ahost = hp->h_name;
88 	oldmask = sigblock(sigmask(SIGURG));
89 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
90 		s = rresvport(&lport);
91 		if (s < 0) {
92 			if (errno == EAGAIN)
93 				(void)fprintf(stderr,
94 				    "rcmd: socket: All ports in use\n");
95 			else
96 				(void)fprintf(stderr, "rcmd: socket: %s\n",
97 				    strerror(errno));
98 			sigsetmask(oldmask);
99 			return (-1);
100 		}
101 		fcntl(s, F_SETOWN, pid);
102 		bzero(&sin, sizeof sin);
103 		sin.sin_len = sizeof(struct sockaddr_in);
104 		sin.sin_family = hp->h_addrtype;
105 		sin.sin_port = rport;
106 		bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
107 		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
108 			break;
109 		(void)close(s);
110 		if (errno == EADDRINUSE) {
111 			lport--;
112 			continue;
113 		}
114 		if (errno == ECONNREFUSED && timo <= 16) {
115 			(void)sleep(timo);
116 			timo *= 2;
117 			continue;
118 		}
119 		if (hp->h_addr_list[1] != NULL) {
120 			int oerrno = errno;
121 
122 			(void)fprintf(stderr, "connect to address %s: ",
123 			    inet_ntoa(sin.sin_addr));
124 			errno = oerrno;
125 			perror(0);
126 			hp->h_addr_list++;
127 			bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
128 			(void)fprintf(stderr, "Trying %s...\n",
129 			    inet_ntoa(sin.sin_addr));
130 			continue;
131 		}
132 		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
133 		sigsetmask(oldmask);
134 		return (-1);
135 	}
136 	lport--;
137 	if (fd2p == 0) {
138 		write(s, "", 1);
139 		lport = 0;
140 	} else {
141 		char num[8];
142 		int s2 = rresvport(&lport), s3;
143 		int len = sizeof(from);
144 
145 		if (s2 < 0)
146 			goto bad;
147 		listen(s2, 1);
148 		(void)snprintf(num, sizeof(num), "%d", lport);
149 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
150 			(void)fprintf(stderr,
151 			    "rcmd: write (setting up stderr): %s\n",
152 			    strerror(errno));
153 			(void)close(s2);
154 			goto bad;
155 		}
156 again:
157 		FD_ZERO(&reads);
158 		FD_SET(s, &reads);
159 		FD_SET(s2, &reads);
160 		errno = 0;
161 		if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 ||
162 		    !FD_ISSET(s2, &reads)) {
163 			if (errno != 0)
164 				(void)fprintf(stderr,
165 				    "rcmd: select (setting up stderr): %s\n",
166 				    strerror(errno));
167 			else
168 				(void)fprintf(stderr,
169 				"select: protocol failure in circuit setup\n");
170 			(void)close(s2);
171 			goto bad;
172 		}
173 		s3 = accept(s2, (struct sockaddr *)&from, &len);
174 		/*
175 		 * XXX careful for ftp bounce attacks. If discovered, shut them
176 		 * down and check for the real auxiliary channel to connect.
177 		 */
178 		if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
179 			close(s3);
180 			goto again;
181 		}
182 		(void)close(s2);
183 		if (s3 < 0) {
184 			(void)fprintf(stderr,
185 			    "rcmd: accept: %s\n", strerror(errno));
186 			lport = 0;
187 			goto bad;
188 		}
189 		*fd2p = s3;
190 		from.sin_port = ntohs(from.sin_port);
191 		if (from.sin_family != AF_INET ||
192 		    from.sin_port >= IPPORT_RESERVED ||
193 		    from.sin_port < IPPORT_RESERVED / 2) {
194 			(void)fprintf(stderr,
195 			    "socket: protocol failure in circuit setup.\n");
196 			goto bad2;
197 		}
198 	}
199 	(void)write(s, locuser, strlen(locuser)+1);
200 	(void)write(s, remuser, strlen(remuser)+1);
201 	(void)write(s, cmd, strlen(cmd)+1);
202 	if (read(s, &c, 1) != 1) {
203 		(void)fprintf(stderr,
204 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
205 		goto bad2;
206 	}
207 	if (c != 0) {
208 		while (read(s, &c, 1) == 1) {
209 			(void)write(STDERR_FILENO, &c, 1);
210 			if (c == '\n')
211 				break;
212 		}
213 		goto bad2;
214 	}
215 	sigsetmask(oldmask);
216 	return (s);
217 bad2:
218 	if (lport)
219 		(void)close(*fd2p);
220 bad:
221 	(void)close(s);
222 	sigsetmask(oldmask);
223 	return (-1);
224 }
225 
226 int
227 rresvport(alport)
228 	int *alport;
229 {
230 	struct sockaddr_in sin;
231 	int s;
232 
233 	bzero(&sin, sizeof sin);
234 	sin.sin_len = sizeof(struct sockaddr_in);
235 	sin.sin_family = AF_INET;
236 	sin.sin_addr.s_addr = INADDR_ANY;
237 	s = socket(AF_INET, SOCK_STREAM, 0);
238 	if (s < 0)
239 		return (-1);
240 	sin.sin_port = htons((u_short)*alport);
241 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
242 		return (s);
243 	if (errno != EADDRINUSE) {
244 		(void)close(s);
245 		return (-1);
246 	}
247 	sin.sin_port = 0;
248 	if (bindresvport(s, &sin) == -1) {
249 		(void)close(s);
250 		return (-1);
251 	}
252 	*alport = (int)ntohs(sin.sin_port);
253 	return (s);
254 }
255 
256 int	__check_rhosts_file = 1;
257 char	*__rcmd_errstr;
258 
259 int
260 ruserok(rhost, superuser, ruser, luser)
261 	const char *rhost, *ruser, *luser;
262 	int superuser;
263 {
264 	struct hostent *hp;
265 	char **ap;
266 	int i;
267 #define MAXADDRS	35
268 	u_int32_t addrs[MAXADDRS + 1];
269 
270 	if ((hp = gethostbyname(rhost)) == NULL)
271 		return (-1);
272 	for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i)
273 		bcopy(*ap, &addrs[i], sizeof(addrs[i]));
274 	addrs[i] = 0;
275 
276 	for (i = 0; i < MAXADDRS && addrs[i]; i++)
277 		if (iruserok((u_long)addrs[i], superuser, ruser, luser) == 0)
278 			return (0);
279 	return (-1);
280 }
281 
282 /*
283  * New .rhosts strategy: We are passed an ip address. We spin through
284  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
285  * has ip addresses, we don't have to trust a nameserver.  When it
286  * contains hostnames, we spin through the list of addresses the nameserver
287  * gives us and look for a match.
288  *
289  * Returns 0 if ok, -1 if not ok.
290  */
291 int
292 iruserok(raddr, superuser, ruser, luser)
293 	u_int32_t raddr;
294 	int superuser;
295 	const char *ruser, *luser;
296 {
297 	register char *cp;
298 	struct stat sbuf;
299 	struct passwd *pwd;
300 	FILE *hostf;
301 	uid_t uid;
302 	int first;
303 	char pbuf[MAXPATHLEN];
304 
305 	first = 1;
306 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
307 again:
308 	if (hostf) {
309 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
310 			(void)fclose(hostf);
311 			return (0);
312 		}
313 		(void)fclose(hostf);
314 	}
315 	if (first == 1 && (__check_rhosts_file || superuser)) {
316 		first = 0;
317 		if ((pwd = getpwnam(luser)) == NULL)
318 			return (-1);
319 		(void)strcpy(pbuf, pwd->pw_dir);
320 		(void)strcat(pbuf, "/.rhosts");
321 
322 		/*
323 		 * Change effective uid while opening .rhosts.  If root and
324 		 * reading an NFS mounted file system, can't read files that
325 		 * are protected read/write owner only.
326 		 */
327 		uid = geteuid();
328 		(void)seteuid(pwd->pw_uid);
329 		hostf = fopen(pbuf, "r");
330 		(void)seteuid(uid);
331 
332 		if (hostf == NULL)
333 			return (-1);
334 		/*
335 		 * If not a regular file, or is owned by someone other than
336 		 * user or root or if writeable by anyone but the owner, quit.
337 		 */
338 		cp = NULL;
339 		if (lstat(pbuf, &sbuf) < 0)
340 			cp = ".rhosts lstat failed";
341 		else if (!S_ISREG(sbuf.st_mode))
342 			cp = ".rhosts not regular file";
343 		else if (fstat(fileno(hostf), &sbuf) < 0)
344 			cp = ".rhosts fstat failed";
345 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
346 			cp = "bad .rhosts owner";
347 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
348 			cp = ".rhosts writeable by other than owner";
349 		/* If there were any problems, quit. */
350 		if (cp) {
351 			__rcmd_errstr = cp;
352 			(void)fclose(hostf);
353 			return (-1);
354 		}
355 		goto again;
356 	}
357 	return (-1);
358 }
359 
360 /*
361  * XXX
362  * Don't make static, used by lpd(8).
363  *
364  * Returns 0 if ok, -1 if not ok.
365  */
366 int
367 __ivaliduser(hostf, raddrl, luser, ruser)
368 	FILE *hostf;
369 	u_long raddrl;
370 	const char *luser, *ruser;
371 {
372 	register char *user, *p;
373 	int ch;
374 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
375 	const char *auser, *ahost;
376 	int hostok, userok;
377 	char *rhost = (char *)-1;
378 	char domain[MAXHOSTNAMELEN];
379 	u_int32_t raddr = (u_int32_t)raddrl;
380 
381 	getdomainname(domain, sizeof(domain));
382 
383 	while (fgets(buf, sizeof(buf), hostf)) {
384 		p = buf;
385 		/* Skip lines that are too long. */
386 		if (strchr(p, '\n') == NULL) {
387 			while ((ch = getc(hostf)) != '\n' && ch != EOF)
388 				;
389 			continue;
390 		}
391 		if (*p == '#')
392 			continue;
393 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
394 			*p = isupper(*p) ? tolower(*p) : *p;
395 			p++;
396 		}
397 		if (*p == ' ' || *p == '\t') {
398 			*p++ = '\0';
399 			while (*p == ' ' || *p == '\t')
400 				p++;
401 			user = p;
402 			while (*p != '\n' && *p != ' ' &&
403 			    *p != '\t' && *p != '\0')
404 				p++;
405 		} else
406 			user = p;
407 		*p = '\0';
408 
409 		if (p == buf)
410 			continue;
411 
412 		auser = *user ? user : luser;
413 		ahost = buf;
414 
415 		/*
416 		 * innetgr() must lookup a hostname (we do not attempt
417 		 * to change the semantics so that netgroups may have
418 		 * #.#.#.# addresses in the list.)
419 		 */
420 		if (ahost[0] == '+')
421 			switch (ahost[1]) {
422 			case '\0':
423 				hostok = 1;
424 				break;
425 			case '@':
426 				if (rhost == (char *)-1)
427 					rhost = __gethostloop(raddr);
428 				hostok = 0;
429 				if (rhost)
430 					hostok = innetgr(&ahost[2], rhost,
431 					    NULL, domain);
432 				break;
433 			default:
434 				hostok = __icheckhost(raddr, &ahost[1]);
435 				break;
436 			}
437 		else if (ahost[0] == '-')
438 			switch (ahost[1]) {
439 			case '\0':
440 				hostok = -1;
441 				break;
442 			case '@':
443 				if (rhost == (char *)-1)
444 					rhost = __gethostloop(raddr);
445 				hostok = 0;
446 				if (rhost)
447 					hostok = -innetgr(&ahost[2], rhost,
448 					    NULL, domain);
449 				break;
450 			default:
451 				hostok = -__icheckhost(raddr, &ahost[1]);
452 				break;
453 			}
454 		else
455 			hostok = __icheckhost(raddr, ahost);
456 
457 
458 		if (auser[0] == '+')
459 			switch (auser[1]) {
460 			case '\0':
461 				userok = 1;
462 				break;
463 			case '@':
464 				userok = innetgr(&auser[2], NULL, ruser,
465 				    domain);
466 				break;
467 			default:
468 				userok = strcmp(ruser, &auser[1]) ? 0 : 1;
469 				break;
470 			}
471 		else if (auser[0] == '-')
472 			switch (auser[1]) {
473 			case '\0':
474 				userok = -1;
475 				break;
476 			case '@':
477 				userok = -innetgr(&auser[2], NULL, ruser,
478 				    domain);
479 				break;
480 			default:
481 				userok = strcmp(ruser, &auser[1]) ? 0 : -1;
482 				break;
483 			}
484 		else
485 			userok = strcmp(ruser, auser) ? 0 : 1;
486 
487 		/* Check if one component did not match */
488 		if (hostok == 0 || userok == 0)
489 			continue;
490 
491 		/* Check if we got a forbidden pair */
492 		if (userok <= -1 || hostok <= -1)
493 			return (-1);
494 
495 		/* Check if we got a valid pair */
496 		if (hostok >= 1 && userok >= 1)
497 			return (0);
498 	}
499 	return (-1);
500 }
501 
502 /*
503  * Returns "true" if match, 0 if no match.  If we do not find any
504  * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
505  */
506 static int
507 __icheckhost(raddr, lhost)
508 	u_int32_t raddr;
509 	const char *lhost;
510 {
511 	register struct hostent *hp;
512 	register char **pp;
513 	struct in_addr in;
514 
515 	hp = gethostbyname(lhost);
516 	if (hp != NULL) {
517 		/* Spin through ip addresses. */
518 		for (pp = hp->h_addr_list; *pp; ++pp)
519 			if (!bcmp(&raddr, *pp, sizeof(raddr)))
520 				return (1);
521 	}
522 
523 	in.s_addr = raddr;
524 	if (strcmp(lhost, inet_ntoa(in)) == 0)
525 		return (1);
526 	return (0);
527 }
528 
529 /*
530  * Return the hostname associated with the supplied address.
531  * Do a reverse lookup as well for security. If a loop cannot
532  * be found, pack the result of inet_ntoa() into the string.
533  */
534 static char *
535 __gethostloop(raddr)
536 	u_int32_t raddr;
537 {
538 	static char remotehost[MAXHOSTNAMELEN];
539 	struct hostent *hp;
540 	struct in_addr in;
541 
542 	hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET);
543 	if (hp == NULL)
544 		return (NULL);
545 
546 	/*
547 	 * Look up the name and check that the supplied
548 	 * address is in the list
549 	 */
550 	strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
551 	remotehost[sizeof(remotehost) - 1] = '\0';
552 	hp = gethostbyname(remotehost);
553 	if (hp == NULL)
554 		return (NULL);
555 
556 	for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++)
557 		if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr)))
558 			return (remotehost);
559 
560 	/*
561 	 * either the DNS adminstrator has made a configuration
562 	 * mistake, or someone has attempted to spoof us
563 	 */
564 	in.s_addr = raddr;
565 	syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s",
566 	    inet_ntoa(in), hp->h_name);
567 	return (NULL);
568 }
569