rcmd.c revision 1573
1169691Skan/*
2169691Skan * Copyright (c) 1983, 1993, 1994
3169691Skan *	The Regents of the University of California.  All rights reserved.
4169691Skan *
5169691Skan * Redistribution and use in source and binary forms, with or without
6169691Skan * modification, are permitted provided that the following conditions
7169691Skan * are met:
8169691Skan * 1. Redistributions of source code must retain the above copyright
9169691Skan *    notice, this list of conditions and the following disclaimer.
10169691Skan * 2. Redistributions in binary form must reproduce the above copyright
11169691Skan *    notice, this list of conditions and the following disclaimer in the
12169691Skan *    documentation and/or other materials provided with the distribution.
13169691Skan * 3. All advertising materials mentioning features or use of this software
14169691Skan *    must display the following acknowledgement:
15169691Skan *	This product includes software developed by the University of
16169691Skan *	California, Berkeley and its contributors.
17169691Skan * 4. Neither the name of the University nor the names of its contributors
18169691Skan *    may be used to endorse or promote products derived from this software
19169691Skan *    without specific prior written permission.
20169691Skan *
21169691Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24169691Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31169691Skan * SUCH DAMAGE.
32169691Skan */
33169691Skan
34169691Skan#if defined(LIBC_SCCS) && !defined(lint)
35169691Skanstatic char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
36169691Skan#endif /* LIBC_SCCS and not lint */
37169691Skan
38169691Skan#include <sys/param.h>
39169691Skan#include <sys/socket.h>
40169691Skan#include <sys/stat.h>
41169691Skan
42169691Skan#include <netinet/in.h>
43169691Skan#include <arpa/inet.h>
44169691Skan
45169691Skan#include <signal.h>
46169691Skan#include <fcntl.h>
47169691Skan#include <netdb.h>
48169691Skan#include <unistd.h>
49169691Skan#include <pwd.h>
50169691Skan#include <errno.h>
51169691Skan#include <stdio.h>
52169691Skan#include <ctype.h>
53169691Skan#include <string.h>
54169691Skan
55169691Skanint	__ivaliduser __P((FILE *, u_long, const char *, const char *));
56169691Skanstatic int __icheckhost __P((u_long, char *));
57169691Skan
58169691Skanint
59169691Skanrcmd(ahost, rport, locuser, remuser, cmd, fd2p)
60169691Skan	char **ahost;
61169691Skan	u_short rport;
62169691Skan	const char *locuser, *remuser, *cmd;
63169691Skan	int *fd2p;
64169691Skan{
65169691Skan	struct hostent *hp;
66169691Skan	struct sockaddr_in sin, from;
67169691Skan	fd_set reads;
68169691Skan	long oldmask;
69169691Skan	pid_t pid;
70169691Skan	int s, lport, timo;
71169691Skan	char c;
72169691Skan
73169691Skan	pid = getpid();
74169691Skan	hp = gethostbyname(*ahost);
75169691Skan	if (hp == NULL) {
76169691Skan		herror(*ahost);
77169691Skan		return (-1);
78169691Skan	}
79169691Skan	*ahost = hp->h_name;
80169691Skan	oldmask = sigblock(sigmask(SIGURG));
81169691Skan	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
82169691Skan		s = rresvport(&lport);
83169691Skan		if (s < 0) {
84169691Skan			if (errno == EAGAIN)
85169691Skan				(void)fprintf(stderr,
86169691Skan				    "rcmd: socket: All ports in use\n");
87169691Skan			else
88169691Skan				(void)fprintf(stderr, "rcmd: socket: %s\n",
89169691Skan				    strerror(errno));
90169691Skan			sigsetmask(oldmask);
91169691Skan			return (-1);
92169691Skan		}
93169691Skan		fcntl(s, F_SETOWN, pid);
94169691Skan		sin.sin_family = hp->h_addrtype;
95169691Skan		bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
96169691Skan		sin.sin_port = rport;
97169691Skan		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
98169691Skan			break;
99169691Skan		(void)close(s);
100169691Skan		if (errno == EADDRINUSE) {
101169691Skan			lport--;
102169691Skan			continue;
103169691Skan		}
104169691Skan		if (errno == ECONNREFUSED && timo <= 16) {
105169691Skan			(void)sleep(timo);
106			timo *= 2;
107			continue;
108		}
109		if (hp->h_addr_list[1] != NULL) {
110			int oerrno = errno;
111
112			(void)fprintf(stderr, "connect to address %s: ",
113			    inet_ntoa(sin.sin_addr));
114			errno = oerrno;
115			perror(0);
116			hp->h_addr_list++;
117			bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
118			(void)fprintf(stderr, "Trying %s...\n",
119			    inet_ntoa(sin.sin_addr));
120			continue;
121		}
122		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
123		sigsetmask(oldmask);
124		return (-1);
125	}
126	lport--;
127	if (fd2p == 0) {
128		write(s, "", 1);
129		lport = 0;
130	} else {
131		char num[8];
132		int s2 = rresvport(&lport), s3;
133		int len = sizeof(from);
134
135		if (s2 < 0)
136			goto bad;
137		listen(s2, 1);
138		(void)snprintf(num, sizeof(num), "%d", lport);
139		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
140			(void)fprintf(stderr,
141			    "rcmd: write (setting up stderr): %s\n",
142			    strerror(errno));
143			(void)close(s2);
144			goto bad;
145		}
146		FD_ZERO(&reads);
147		FD_SET(s, &reads);
148		FD_SET(s2, &reads);
149		errno = 0;
150		if (select(32, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)) {
151			if (errno != 0)
152				(void)fprintf(stderr,
153				    "rcmd: select (setting up stderr): %s\n",
154				    strerror(errno));
155			else
156				(void)fprintf(stderr,
157				"select: protocol failure in circuit setup\n");
158			(void)close(s2);
159			goto bad;
160		}
161		s3 = accept(s2, (struct sockaddr *)&from, &len);
162		(void)close(s2);
163		if (s3 < 0) {
164			(void)fprintf(stderr,
165			    "rcmd: accept: %s\n", strerror(errno));
166			lport = 0;
167			goto bad;
168		}
169		*fd2p = s3;
170		from.sin_port = ntohs((u_short)from.sin_port);
171		if (from.sin_family != AF_INET ||
172		    from.sin_port >= IPPORT_RESERVED ||
173		    from.sin_port < IPPORT_RESERVED / 2) {
174			(void)fprintf(stderr,
175			    "socket: protocol failure in circuit setup.\n");
176			goto bad2;
177		}
178	}
179	(void)write(s, locuser, strlen(locuser)+1);
180	(void)write(s, remuser, strlen(remuser)+1);
181	(void)write(s, cmd, strlen(cmd)+1);
182	if (read(s, &c, 1) != 1) {
183		(void)fprintf(stderr,
184		    "rcmd: %s: %s\n", *ahost, strerror(errno));
185		goto bad2;
186	}
187	if (c != 0) {
188		while (read(s, &c, 1) == 1) {
189			(void)write(STDERR_FILENO, &c, 1);
190			if (c == '\n')
191				break;
192		}
193		goto bad2;
194	}
195	sigsetmask(oldmask);
196	return (s);
197bad2:
198	if (lport)
199		(void)close(*fd2p);
200bad:
201	(void)close(s);
202	sigsetmask(oldmask);
203	return (-1);
204}
205
206int
207rresvport(alport)
208	int *alport;
209{
210	struct sockaddr_in sin;
211	int s;
212
213	sin.sin_family = AF_INET;
214	sin.sin_addr.s_addr = INADDR_ANY;
215	s = socket(AF_INET, SOCK_STREAM, 0);
216	if (s < 0)
217		return (-1);
218	for (;;) {
219		sin.sin_port = htons((u_short)*alport);
220		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
221			return (s);
222		if (errno != EADDRINUSE) {
223			(void)close(s);
224			return (-1);
225		}
226		(*alport)--;
227		if (*alport == IPPORT_RESERVED/2) {
228			(void)close(s);
229			errno = EAGAIN;		/* close */
230			return (-1);
231		}
232	}
233}
234
235int	__check_rhosts_file = 1;
236char	*__rcmd_errstr;
237
238int
239ruserok(rhost, superuser, ruser, luser)
240	const char *rhost, *ruser, *luser;
241	int superuser;
242{
243	struct hostent *hp;
244	u_long addr;
245	char **ap;
246
247	if ((hp = gethostbyname(rhost)) == NULL)
248		return (-1);
249	for (ap = hp->h_addr_list; *ap; ++ap) {
250		bcopy(*ap, &addr, sizeof(addr));
251		if (iruserok(addr, superuser, ruser, luser) == 0)
252			return (0);
253	}
254	return (-1);
255}
256
257/*
258 * New .rhosts strategy: We are passed an ip address. We spin through
259 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
260 * has ip addresses, we don't have to trust a nameserver.  When it
261 * contains hostnames, we spin through the list of addresses the nameserver
262 * gives us and look for a match.
263 *
264 * Returns 0 if ok, -1 if not ok.
265 */
266int
267iruserok(raddr, superuser, ruser, luser)
268	u_long raddr;
269	int superuser;
270	const char *ruser, *luser;
271{
272	register char *cp;
273	struct stat sbuf;
274	struct passwd *pwd;
275	FILE *hostf;
276	uid_t uid;
277	int first;
278	char pbuf[MAXPATHLEN];
279
280	first = 1;
281	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
282again:
283	if (hostf) {
284		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
285			(void)fclose(hostf);
286			return (0);
287		}
288		(void)fclose(hostf);
289	}
290	if (first == 1 && (__check_rhosts_file || superuser)) {
291		first = 0;
292		if ((pwd = getpwnam(luser)) == NULL)
293			return (-1);
294		(void)strcpy(pbuf, pwd->pw_dir);
295		(void)strcat(pbuf, "/.rhosts");
296
297		/*
298		 * Change effective uid while opening .rhosts.  If root and
299		 * reading an NFS mounted file system, can't read files that
300		 * are protected read/write owner only.
301		 */
302		uid = geteuid();
303		(void)seteuid(pwd->pw_uid);
304		hostf = fopen(pbuf, "r");
305		(void)seteuid(uid);
306
307		if (hostf == NULL)
308			return (-1);
309		/*
310		 * If not a regular file, or is owned by someone other than
311		 * user or root or if writeable by anyone but the owner, quit.
312		 */
313		cp = NULL;
314		if (lstat(pbuf, &sbuf) < 0)
315			cp = ".rhosts lstat failed";
316		else if (!S_ISREG(sbuf.st_mode))
317			cp = ".rhosts not regular file";
318		else if (fstat(fileno(hostf), &sbuf) < 0)
319			cp = ".rhosts fstat failed";
320		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
321			cp = "bad .rhosts owner";
322		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
323			cp = ".rhosts writeable by other than owner";
324		/* If there were any problems, quit. */
325		if (cp) {
326			__rcmd_errstr = cp;
327			(void)fclose(hostf);
328			return (-1);
329		}
330		goto again;
331	}
332	return (-1);
333}
334
335/*
336 * XXX
337 * Don't make static, used by lpd(8).
338 *
339 * Returns 0 if ok, -1 if not ok.
340 */
341int
342__ivaliduser(hostf, raddr, luser, ruser)
343	FILE *hostf;
344	u_long raddr;
345	const char *luser, *ruser;
346{
347	register char *user, *p;
348	int ch;
349	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
350
351	while (fgets(buf, sizeof(buf), hostf)) {
352		p = buf;
353		/* Skip lines that are too long. */
354		if (strchr(p, '\n') == NULL) {
355			while ((ch = getc(hostf)) != '\n' && ch != EOF);
356			continue;
357		}
358		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
359			*p = isupper(*p) ? tolower(*p) : *p;
360			p++;
361		}
362		if (*p == ' ' || *p == '\t') {
363			*p++ = '\0';
364			while (*p == ' ' || *p == '\t')
365				p++;
366			user = p;
367			while (*p != '\n' && *p != ' ' &&
368			    *p != '\t' && *p != '\0')
369				p++;
370		} else
371			user = p;
372		*p = '\0';
373		if (__icheckhost(raddr, buf) &&
374		    strcmp(ruser, *user ? user : luser) == 0) {
375			return (0);
376		}
377	}
378	return (-1);
379}
380
381/*
382 * Returns "true" if match, 0 if no match.
383 */
384static int
385__icheckhost(raddr, lhost)
386	u_long raddr;
387	register char *lhost;
388{
389	register struct hostent *hp;
390	register u_long laddr;
391	register char **pp;
392
393	/* Try for raw ip address first. */
394	if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1)
395		return (raddr == laddr);
396
397	/* Better be a hostname. */
398	if ((hp = gethostbyname(lhost)) == NULL)
399		return (0);
400
401	/* Spin through ip addresses. */
402	for (pp = hp->h_addr_list; *pp; ++pp)
403		if (!bcmp(&raddr, *pp, sizeof(u_long)))
404			return (1);
405
406	/* No match. */
407	return (0);
408}
409