xref: /netbsd-src/lib/libc/net/rcmd.c (revision 816feb25c5b93ee8552c4f782f85679db64c3d1c)
1 /*
2  * Copyright (c) 1983, 1993, 1994
3  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 /* from: static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94"; */
36 static char *rcsid = "$Id: rcmd.c,v 1.9 1994/06/01 19:32:43 pk Exp $";
37 #endif /* LIBC_SCCS and not lint */
38 
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 
46 #include <signal.h>
47 #include <fcntl.h>
48 #include <netdb.h>
49 #include <unistd.h>
50 #include <pwd.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <string.h>
55 
56 int	__ivaliduser __P((FILE *, u_long, const char *, const char *));
57 static int __icheckhost __P((u_long, char *));
58 
59 int
60 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
61 	char **ahost;
62 	u_short rport;
63 	const char *locuser, *remuser, *cmd;
64 	int *fd2p;
65 {
66 	struct hostent *hp;
67 	struct sockaddr_in sin, from;
68 	fd_set reads;
69 	long oldmask;
70 	pid_t pid;
71 	int s, lport, timo;
72 	char c;
73 
74 	pid = getpid();
75 	hp = gethostbyname(*ahost);
76 	if (hp == NULL) {
77 		herror(*ahost);
78 		return (-1);
79 	}
80 	*ahost = hp->h_name;
81 	oldmask = sigblock(sigmask(SIGURG));
82 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
83 		s = rresvport(&lport);
84 		if (s < 0) {
85 			if (errno == EAGAIN)
86 				(void)fprintf(stderr,
87 				    "rcmd: socket: All ports in use\n");
88 			else
89 				(void)fprintf(stderr, "rcmd: socket: %s\n",
90 				    strerror(errno));
91 			sigsetmask(oldmask);
92 			return (-1);
93 		}
94 		fcntl(s, F_SETOWN, pid);
95 		sin.sin_family = hp->h_addrtype;
96 		bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
97 		sin.sin_port = rport;
98 		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
99 			break;
100 		(void)close(s);
101 		if (errno == EADDRINUSE) {
102 			lport--;
103 			continue;
104 		}
105 		if (errno == ECONNREFUSED && timo <= 16) {
106 			(void)sleep(timo);
107 			timo *= 2;
108 			continue;
109 		}
110 		if (hp->h_addr_list[1] != NULL) {
111 			int oerrno = errno;
112 
113 			(void)fprintf(stderr, "connect to address %s: ",
114 			    inet_ntoa(sin.sin_addr));
115 			errno = oerrno;
116 			perror(0);
117 			hp->h_addr_list++;
118 			bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
119 			(void)fprintf(stderr, "Trying %s...\n",
120 			    inet_ntoa(sin.sin_addr));
121 			continue;
122 		}
123 		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
124 		sigsetmask(oldmask);
125 		return (-1);
126 	}
127 	lport--;
128 	if (fd2p == 0) {
129 		write(s, "", 1);
130 		lport = 0;
131 	} else {
132 		char num[8];
133 		int s2 = rresvport(&lport), s3;
134 		int len = sizeof(from);
135 
136 		if (s2 < 0)
137 			goto bad;
138 		listen(s2, 1);
139 		(void)snprintf(num, sizeof(num), "%d", lport);
140 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
141 			(void)fprintf(stderr,
142 			    "rcmd: write (setting up stderr): %s\n",
143 			    strerror(errno));
144 			(void)close(s2);
145 			goto bad;
146 		}
147 		FD_ZERO(&reads);
148 		FD_SET(s, &reads);
149 		FD_SET(s2, &reads);
150 		errno = 0;
151 		if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 ||
152 		    !FD_ISSET(s2, &reads)) {
153 			if (errno != 0)
154 				(void)fprintf(stderr,
155 				    "rcmd: select (setting up stderr): %s\n",
156 				    strerror(errno));
157 			else
158 				(void)fprintf(stderr,
159 				"select: protocol failure in circuit setup\n");
160 			(void)close(s2);
161 			goto bad;
162 		}
163 		s3 = accept(s2, (struct sockaddr *)&from, &len);
164 		(void)close(s2);
165 		if (s3 < 0) {
166 			(void)fprintf(stderr,
167 			    "rcmd: accept: %s\n", strerror(errno));
168 			lport = 0;
169 			goto bad;
170 		}
171 		*fd2p = s3;
172 		from.sin_port = ntohs((u_short)from.sin_port);
173 		if (from.sin_family != AF_INET ||
174 		    from.sin_port >= IPPORT_RESERVED ||
175 		    from.sin_port < IPPORT_RESERVED / 2) {
176 			(void)fprintf(stderr,
177 			    "socket: protocol failure in circuit setup.\n");
178 			goto bad2;
179 		}
180 	}
181 	(void)write(s, locuser, strlen(locuser)+1);
182 	(void)write(s, remuser, strlen(remuser)+1);
183 	(void)write(s, cmd, strlen(cmd)+1);
184 	if (read(s, &c, 1) != 1) {
185 		(void)fprintf(stderr,
186 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
187 		goto bad2;
188 	}
189 	if (c != 0) {
190 		while (read(s, &c, 1) == 1) {
191 			(void)write(STDERR_FILENO, &c, 1);
192 			if (c == '\n')
193 				break;
194 		}
195 		goto bad2;
196 	}
197 	sigsetmask(oldmask);
198 	return (s);
199 bad2:
200 	if (lport)
201 		(void)close(*fd2p);
202 bad:
203 	(void)close(s);
204 	sigsetmask(oldmask);
205 	return (-1);
206 }
207 
208 int
209 rresvport(alport)
210 	int *alport;
211 {
212 	struct sockaddr_in sin;
213 	int s;
214 
215 	sin.sin_family = AF_INET;
216 	sin.sin_addr.s_addr = INADDR_ANY;
217 	s = socket(AF_INET, SOCK_STREAM, 0);
218 	if (s < 0)
219 		return (-1);
220 	for (;;) {
221 		sin.sin_port = htons((u_short)*alport);
222 		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
223 			return (s);
224 		if (errno != EADDRINUSE) {
225 			(void)close(s);
226 			return (-1);
227 		}
228 		(*alport)--;
229 		if (*alport == IPPORT_RESERVED/2) {
230 			(void)close(s);
231 			errno = EAGAIN;		/* close */
232 			return (-1);
233 		}
234 	}
235 }
236 
237 int	__check_rhosts_file = 1;
238 char	*__rcmd_errstr;
239 
240 int
241 ruserok(rhost, superuser, ruser, luser)
242 	const char *rhost, *ruser, *luser;
243 	int superuser;
244 {
245 	struct hostent *hp;
246 	char **ap;
247 	int i;
248 #define MAXADDRS	35
249 	u_long addrs[MAXADDRS + 1];
250 
251 	if ((hp = gethostbyname(rhost)) == NULL)
252 		return (-1);
253 	for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i)
254 		bcopy(*ap, &addrs[i], sizeof(addrs[i]));
255 	addrs[i] = 0;
256 
257 	for (i = 0; i < MAXADDRS && addrs[i]; i++)
258 		if (iruserok(addrs[i], superuser, ruser, luser) == 0)
259 			return (0);
260 	return (-1);
261 }
262 
263 /*
264  * New .rhosts strategy: We are passed an ip address. We spin through
265  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
266  * has ip addresses, we don't have to trust a nameserver.  When it
267  * contains hostnames, we spin through the list of addresses the nameserver
268  * gives us and look for a match.
269  *
270  * Returns 0 if ok, -1 if not ok.
271  */
272 int
273 iruserok(raddr, superuser, ruser, luser)
274 	u_long raddr;
275 	int superuser;
276 	const char *ruser, *luser;
277 {
278 	register char *cp;
279 	struct stat sbuf;
280 	struct passwd *pwd;
281 	FILE *hostf;
282 	uid_t uid;
283 	int first;
284 	char pbuf[MAXPATHLEN];
285 
286 	first = 1;
287 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
288 again:
289 	if (hostf) {
290 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
291 			(void)fclose(hostf);
292 			return (0);
293 		}
294 		(void)fclose(hostf);
295 	}
296 	if (first == 1 && (__check_rhosts_file || superuser)) {
297 		first = 0;
298 		if ((pwd = getpwnam(luser)) == NULL)
299 			return (-1);
300 		(void)strcpy(pbuf, pwd->pw_dir);
301 		(void)strcat(pbuf, "/.rhosts");
302 
303 		/*
304 		 * Change effective uid while opening .rhosts.  If root and
305 		 * reading an NFS mounted file system, can't read files that
306 		 * are protected read/write owner only.
307 		 */
308 		uid = geteuid();
309 		(void)seteuid(pwd->pw_uid);
310 		hostf = fopen(pbuf, "r");
311 		(void)seteuid(uid);
312 
313 		if (hostf == NULL)
314 			return (-1);
315 		/*
316 		 * If not a regular file, or is owned by someone other than
317 		 * user or root or if writeable by anyone but the owner, quit.
318 		 */
319 		cp = NULL;
320 		if (lstat(pbuf, &sbuf) < 0)
321 			cp = ".rhosts lstat failed";
322 		else if (!S_ISREG(sbuf.st_mode))
323 			cp = ".rhosts not regular file";
324 		else if (fstat(fileno(hostf), &sbuf) < 0)
325 			cp = ".rhosts fstat failed";
326 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
327 			cp = "bad .rhosts owner";
328 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
329 			cp = ".rhosts writeable by other than owner";
330 		/* If there were any problems, quit. */
331 		if (cp) {
332 			__rcmd_errstr = cp;
333 			(void)fclose(hostf);
334 			return (-1);
335 		}
336 		goto again;
337 	}
338 	return (-1);
339 }
340 
341 /*
342  * XXX
343  * Don't make static, used by lpd(8).
344  *
345  * Returns 0 if ok, -1 if not ok.
346  */
347 int
348 __ivaliduser(hostf, raddr, luser, ruser)
349 	FILE *hostf;
350 	u_long raddr;
351 	const char *luser, *ruser;
352 {
353 	register char *user, *p;
354 	int ch;
355 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
356 
357 	while (fgets(buf, sizeof(buf), hostf)) {
358 		p = buf;
359 		/* Skip lines that are too long. */
360 		if (strchr(p, '\n') == NULL) {
361 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
362 			continue;
363 		}
364 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
365 			*p = isupper(*p) ? tolower(*p) : *p;
366 			p++;
367 		}
368 		if (*p == ' ' || *p == '\t') {
369 			*p++ = '\0';
370 			while (*p == ' ' || *p == '\t')
371 				p++;
372 			user = p;
373 			while (*p != '\n' && *p != ' ' &&
374 			    *p != '\t' && *p != '\0')
375 				p++;
376 		} else
377 			user = p;
378 		*p = '\0';
379 		if (p == buf)
380 			continue;
381 		if (__icheckhost(raddr, buf) &&
382 		    strcmp(ruser, *user ? user : luser) == 0) {
383 			return (0);
384 		}
385 	}
386 	return (-1);
387 }
388 
389 /*
390  * Returns "true" if match, 0 if no match.
391  */
392 static int
393 __icheckhost(raddr, lhost)
394 	u_long raddr;
395 	register char *lhost;
396 {
397 	register struct hostent *hp;
398 	register u_long laddr;
399 	register char **pp;
400 
401 	/* Try for raw ip address first. */
402 	if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1)
403 		return (raddr == laddr);
404 
405 	/* Better be a hostname. */
406 	if ((hp = gethostbyname(lhost)) == NULL)
407 		return (0);
408 
409 	/* Spin through ip addresses. */
410 	for (pp = hp->h_addr_list; *pp; ++pp)
411 		if (!bcmp(&raddr, *pp, sizeof(u_long)))
412 			return (1);
413 
414 	/* No match. */
415 	return (0);
416 }
417