xref: /openbsd-src/lib/libc/net/rcmd.c (revision 2233f5a3b27804b5782ddcd2dcc9a54967ae4e31)
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 		sin.sin_len = sizeof(struct sockaddr_in);
103 		sin.sin_family = hp->h_addrtype;
104 		sin.sin_port = rport;
105 		bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
106 		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
107 			break;
108 		(void)close(s);
109 		if (errno == EADDRINUSE) {
110 			lport--;
111 			continue;
112 		}
113 		if (errno == ECONNREFUSED && timo <= 16) {
114 			(void)sleep(timo);
115 			timo *= 2;
116 			continue;
117 		}
118 		if (hp->h_addr_list[1] != NULL) {
119 			int oerrno = errno;
120 
121 			(void)fprintf(stderr, "connect to address %s: ",
122 			    inet_ntoa(sin.sin_addr));
123 			errno = oerrno;
124 			perror(0);
125 			hp->h_addr_list++;
126 			bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
127 			(void)fprintf(stderr, "Trying %s...\n",
128 			    inet_ntoa(sin.sin_addr));
129 			continue;
130 		}
131 		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
132 		sigsetmask(oldmask);
133 		return (-1);
134 	}
135 	lport--;
136 	if (fd2p == 0) {
137 		write(s, "", 1);
138 		lport = 0;
139 	} else {
140 		char num[8];
141 		int s2 = rresvport(&lport), s3;
142 		int len = sizeof(from);
143 
144 		if (s2 < 0)
145 			goto bad;
146 		listen(s2, 1);
147 		(void)snprintf(num, sizeof(num), "%d", lport);
148 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
149 			(void)fprintf(stderr,
150 			    "rcmd: write (setting up stderr): %s\n",
151 			    strerror(errno));
152 			(void)close(s2);
153 			goto bad;
154 		}
155 		FD_ZERO(&reads);
156 		FD_SET(s, &reads);
157 		FD_SET(s2, &reads);
158 		errno = 0;
159 		if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 ||
160 		    !FD_ISSET(s2, &reads)) {
161 			if (errno != 0)
162 				(void)fprintf(stderr,
163 				    "rcmd: select (setting up stderr): %s\n",
164 				    strerror(errno));
165 			else
166 				(void)fprintf(stderr,
167 				"select: protocol failure in circuit setup\n");
168 			(void)close(s2);
169 			goto bad;
170 		}
171 		s3 = accept(s2, (struct sockaddr *)&from, &len);
172 		(void)close(s2);
173 		if (s3 < 0) {
174 			(void)fprintf(stderr,
175 			    "rcmd: accept: %s\n", strerror(errno));
176 			lport = 0;
177 			goto bad;
178 		}
179 		*fd2p = s3;
180 		from.sin_port = ntohs(from.sin_port);
181 		if (from.sin_family != AF_INET ||
182 		    from.sin_port >= IPPORT_RESERVED ||
183 		    from.sin_port < IPPORT_RESERVED / 2) {
184 			(void)fprintf(stderr,
185 			    "socket: protocol failure in circuit setup.\n");
186 			goto bad2;
187 		}
188 	}
189 	(void)write(s, locuser, strlen(locuser)+1);
190 	(void)write(s, remuser, strlen(remuser)+1);
191 	(void)write(s, cmd, strlen(cmd)+1);
192 	if (read(s, &c, 1) != 1) {
193 		(void)fprintf(stderr,
194 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
195 		goto bad2;
196 	}
197 	if (c != 0) {
198 		while (read(s, &c, 1) == 1) {
199 			(void)write(STDERR_FILENO, &c, 1);
200 			if (c == '\n')
201 				break;
202 		}
203 		goto bad2;
204 	}
205 	sigsetmask(oldmask);
206 	return (s);
207 bad2:
208 	if (lport)
209 		(void)close(*fd2p);
210 bad:
211 	(void)close(s);
212 	sigsetmask(oldmask);
213 	return (-1);
214 }
215 
216 int
217 rresvport(alport)
218 	int *alport;
219 {
220 	struct sockaddr_in sin;
221 	int s;
222 
223 	bzero(&sin, sizeof sin);
224 	sin.sin_len = sizeof(struct sockaddr_in);
225 	sin.sin_family = AF_INET;
226 	sin.sin_addr.s_addr = INADDR_ANY;
227 	s = socket(AF_INET, SOCK_STREAM, 0);
228 	if (s < 0)
229 		return (-1);
230 	for (;;) {
231 		sin.sin_port = htons((u_short)*alport);
232 		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
233 			return (s);
234 		if (errno != EADDRINUSE) {
235 			(void)close(s);
236 			return (-1);
237 		}
238 		(*alport)--;
239 		if (*alport == IPPORT_RESERVED/2) {
240 			(void)close(s);
241 			errno = EAGAIN;		/* close */
242 			return (-1);
243 		}
244 	}
245 }
246 
247 int	__check_rhosts_file = 1;
248 char	*__rcmd_errstr;
249 
250 int
251 ruserok(rhost, superuser, ruser, luser)
252 	const char *rhost, *ruser, *luser;
253 	int superuser;
254 {
255 	struct hostent *hp;
256 	char **ap;
257 	int i;
258 #define MAXADDRS	35
259 	u_int32_t addrs[MAXADDRS + 1];
260 
261 	if ((hp = gethostbyname(rhost)) == NULL)
262 		return (-1);
263 	for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i)
264 		bcopy(*ap, &addrs[i], sizeof(addrs[i]));
265 	addrs[i] = 0;
266 
267 	for (i = 0; i < MAXADDRS && addrs[i]; i++)
268 		if (iruserok((u_long)addrs[i], superuser, ruser, luser) == 0)
269 			return (0);
270 	return (-1);
271 }
272 
273 /*
274  * New .rhosts strategy: We are passed an ip address. We spin through
275  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
276  * has ip addresses, we don't have to trust a nameserver.  When it
277  * contains hostnames, we spin through the list of addresses the nameserver
278  * gives us and look for a match.
279  *
280  * Returns 0 if ok, -1 if not ok.
281  */
282 int
283 iruserok(raddr, superuser, ruser, luser)
284 	u_int32_t raddr;
285 	int superuser;
286 	const char *ruser, *luser;
287 {
288 	register char *cp;
289 	struct stat sbuf;
290 	struct passwd *pwd;
291 	FILE *hostf;
292 	uid_t uid;
293 	int first;
294 	char pbuf[MAXPATHLEN];
295 
296 	first = 1;
297 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
298 again:
299 	if (hostf) {
300 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
301 			(void)fclose(hostf);
302 			return (0);
303 		}
304 		(void)fclose(hostf);
305 	}
306 	if (first == 1 && (__check_rhosts_file || superuser)) {
307 		first = 0;
308 		if ((pwd = getpwnam(luser)) == NULL)
309 			return (-1);
310 		(void)strcpy(pbuf, pwd->pw_dir);
311 		(void)strcat(pbuf, "/.rhosts");
312 
313 		/*
314 		 * Change effective uid while opening .rhosts.  If root and
315 		 * reading an NFS mounted file system, can't read files that
316 		 * are protected read/write owner only.
317 		 */
318 		uid = geteuid();
319 		(void)seteuid(pwd->pw_uid);
320 		hostf = fopen(pbuf, "r");
321 		(void)seteuid(uid);
322 
323 		if (hostf == NULL)
324 			return (-1);
325 		/*
326 		 * If not a regular file, or is owned by someone other than
327 		 * user or root or if writeable by anyone but the owner, quit.
328 		 */
329 		cp = NULL;
330 		if (lstat(pbuf, &sbuf) < 0)
331 			cp = ".rhosts lstat failed";
332 		else if (!S_ISREG(sbuf.st_mode))
333 			cp = ".rhosts not regular file";
334 		else if (fstat(fileno(hostf), &sbuf) < 0)
335 			cp = ".rhosts fstat failed";
336 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
337 			cp = "bad .rhosts owner";
338 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
339 			cp = ".rhosts writeable by other than owner";
340 		/* If there were any problems, quit. */
341 		if (cp) {
342 			__rcmd_errstr = cp;
343 			(void)fclose(hostf);
344 			return (-1);
345 		}
346 		goto again;
347 	}
348 	return (-1);
349 }
350 
351 /*
352  * XXX
353  * Don't make static, used by lpd(8).
354  *
355  * Returns 0 if ok, -1 if not ok.
356  */
357 int
358 __ivaliduser(hostf, raddrl, luser, ruser)
359 	FILE *hostf;
360 	u_long raddrl;
361 	const char *luser, *ruser;
362 {
363 	register char *user, *p;
364 	int ch;
365 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
366 	const char *auser, *ahost;
367 	int hostok, userok;
368 	char *rhost = (char *)-1;
369 	char domain[MAXHOSTNAMELEN];
370 	u_int32_t raddr = (u_int32_t)raddrl;
371 
372 	getdomainname(domain, sizeof(domain));
373 
374 	while (fgets(buf, sizeof(buf), hostf)) {
375 		p = buf;
376 		/* Skip lines that are too long. */
377 		if (strchr(p, '\n') == NULL) {
378 			while ((ch = getc(hostf)) != '\n' && ch != EOF)
379 				;
380 			continue;
381 		}
382 		if (*p == '#')
383 			continue;
384 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
385 			*p = isupper(*p) ? tolower(*p) : *p;
386 			p++;
387 		}
388 		if (*p == ' ' || *p == '\t') {
389 			*p++ = '\0';
390 			while (*p == ' ' || *p == '\t')
391 				p++;
392 			user = p;
393 			while (*p != '\n' && *p != ' ' &&
394 			    *p != '\t' && *p != '\0')
395 				p++;
396 		} else
397 			user = p;
398 		*p = '\0';
399 
400 		if (p == buf)
401 			continue;
402 
403 		auser = *user ? user : luser;
404 		ahost = buf;
405 
406 		/*
407 		 * innetgr() must lookup a hostname (we do not attempt
408 		 * to change the semantics so that netgroups may have
409 		 * #.#.#.# addresses in the list.)
410 		 */
411 		if (ahost[0] == '+')
412 			switch (ahost[1]) {
413 			case '\0':
414 				hostok = 1;
415 				break;
416 			case '@':
417 				if (rhost == (char *)-1)
418 					rhost = __gethostloop(raddr);
419 				hostok = 0;
420 				if (rhost)
421 					hostok = innetgr(&ahost[2], rhost,
422 					    NULL, domain);
423 				break;
424 			default:
425 				hostok = __icheckhost(raddr, &ahost[1]);
426 				break;
427 			}
428 		else if (ahost[0] == '-')
429 			switch (ahost[1]) {
430 			case '\0':
431 				hostok = -1;
432 				break;
433 			case '@':
434 				if (rhost == (char *)-1)
435 					rhost = __gethostloop(raddr);
436 				hostok = 0;
437 				if (rhost)
438 					hostok = -innetgr(&ahost[2], rhost,
439 					    NULL, domain);
440 				break;
441 			default:
442 				hostok = -__icheckhost(raddr, &ahost[1]);
443 				break;
444 			}
445 		else
446 			hostok = __icheckhost(raddr, ahost);
447 
448 
449 		if (auser[0] == '+')
450 			switch (auser[1]) {
451 			case '\0':
452 				userok = 1;
453 				break;
454 			case '@':
455 				userok = innetgr(&auser[2], NULL, ruser,
456 				    domain);
457 				break;
458 			default:
459 				userok = strcmp(ruser, &auser[1]) ? 0 : 1;
460 				break;
461 			}
462 		else if (auser[0] == '-')
463 			switch (auser[1]) {
464 			case '\0':
465 				userok = -1;
466 				break;
467 			case '@':
468 				userok = -innetgr(&auser[2], NULL, ruser,
469 				    domain);
470 				break;
471 			default:
472 				userok = strcmp(ruser, &auser[1]) ? 0 : -1;
473 				break;
474 			}
475 		else
476 			userok = strcmp(ruser, auser) ? 0 : 1;
477 
478 		/* Check if one component did not match */
479 		if (hostok == 0 || userok == 0)
480 			continue;
481 
482 		/* Check if we got a forbidden pair */
483 		if (userok <= -1 || hostok <= -1)
484 			return (-1);
485 
486 		/* Check if we got a valid pair */
487 		if (hostok >= 1 && userok >= 1)
488 			return (0);
489 	}
490 	return (-1);
491 }
492 
493 /*
494  * Returns "true" if match, 0 if no match.  If we do not find any
495  * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
496  */
497 static int
498 __icheckhost(raddr, lhost)
499 	u_int32_t raddr;
500 	const char *lhost;
501 {
502 	register struct hostent *hp;
503 	register char **pp;
504 	struct in_addr in;
505 
506 	hp = gethostbyname(lhost);
507 	if (hp != NULL) {
508 		/* Spin through ip addresses. */
509 		for (pp = hp->h_addr_list; *pp; ++pp)
510 			if (!bcmp(&raddr, *pp, sizeof(raddr)))
511 				return (1);
512 	}
513 
514 	in.s_addr = raddr;
515 	if (strcmp(lhost, inet_ntoa(in)) == 0)
516 		return (1);
517 	return (0);
518 }
519 
520 /*
521  * Return the hostname associated with the supplied address.
522  * Do a reverse lookup as well for security. If a loop cannot
523  * be found, pack the result of inet_ntoa() into the string.
524  */
525 static char *
526 __gethostloop(raddr)
527 	u_int32_t raddr;
528 {
529 	static char remotehost[MAXHOSTNAMELEN];
530 	struct hostent *hp;
531 	struct in_addr in;
532 
533 	hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET);
534 	if (hp == NULL)
535 		return (NULL);
536 
537 	/*
538 	 * Look up the name and check that the supplied
539 	 * address is in the list
540 	 */
541 	strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
542 	remotehost[sizeof(remotehost) - 1] = '\0';
543 	hp = gethostbyname(remotehost);
544 	if (hp == NULL)
545 		return (NULL);
546 
547 	for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++)
548 		if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr)))
549 			return (remotehost);
550 
551 	/*
552 	 * either the DNS adminstrator has made a configuration
553 	 * mistake, or someone has attempted to spoof us
554 	 */
555 	in.s_addr = raddr;
556 	syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s",
557 	    inet_ntoa(in), hp->h_name);
558 	return (NULL);
559 }
560