xref: /netbsd-src/lib/libc/net/rcmd.c (revision f92ccee2e2541427344391e3aaa9345b4bb724a6)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * 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 /*static char *sccsid = "from: @(#)rcmd.c	5.24 (Berkeley) 2/24/91";*/
36 static char *rcsid = "$Id: rcmd.c,v 1.4 1993/12/05 14:42:26 deraadt 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 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <signal.h>
45 #include <fcntl.h>
46 #include <netdb.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <ctype.h>
51 #include <unistd.h>
52 #include <string.h>
53 
54 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
55 	char **ahost;
56 	u_short rport;
57 	const char *locuser, *remuser, *cmd;
58 	int *fd2p;
59 {
60 	int s, timo = 1, pid;
61 	long oldmask;
62 	struct sockaddr_in sin, from;
63 	char c;
64 	int lport = IPPORT_RESERVED - 1;
65 	struct hostent *hp;
66 	fd_set reads;
67 
68 	pid = getpid();
69 	hp = gethostbyname(*ahost);
70 	if (hp == 0) {
71 		herror(*ahost);
72 		return (-1);
73 	}
74 	*ahost = hp->h_name;
75 	oldmask = sigblock(sigmask(SIGURG));
76 	for (;;) {
77 		s = rresvport(&lport);
78 		if (s < 0) {
79 			if (errno == EAGAIN)
80 				fprintf(stderr, "socket: All ports in use\n");
81 			else
82 				perror("rcmd: socket");
83 			sigsetmask(oldmask);
84 			return (-1);
85 		}
86 		fcntl(s, F_SETOWN, pid);
87 		bzero((char *)&sin, sizeof sin);
88 		sin.sin_family = hp->h_addrtype;
89 		bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length);
90 		sin.sin_port = rport;
91 		if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
92 			break;
93 		(void) close(s);
94 		if (errno == EADDRINUSE) {
95 			lport--;
96 			continue;
97 		}
98 		if (errno == ECONNREFUSED && timo <= 16) {
99 			sleep(timo);
100 			timo *= 2;
101 			continue;
102 		}
103 		if (hp->h_addr_list[1] != NULL) {
104 			int oerrno = errno;
105 
106 			fprintf(stderr,
107 			    "connect to address %s: ", inet_ntoa(sin.sin_addr));
108 			errno = oerrno;
109 			perror(0);
110 			hp->h_addr_list++;
111 			bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr,
112 			    hp->h_length);
113 			fprintf(stderr, "Trying %s...\n",
114 				inet_ntoa(sin.sin_addr));
115 			continue;
116 		}
117 		perror(hp->h_name);
118 		sigsetmask(oldmask);
119 		return (-1);
120 	}
121 	lport--;
122 	if (fd2p == 0) {
123 		write(s, "", 1);
124 		lport = 0;
125 	} else {
126 		char num[8];
127 		int s2 = rresvport(&lport), s3;
128 		int len = sizeof (from);
129 
130 		if (s2 < 0)
131 			goto bad;
132 		listen(s2, 1);
133 		(void) sprintf(num, "%d", lport);
134 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
135 			perror("write: setting up stderr");
136 			(void) close(s2);
137 			goto bad;
138 		}
139 		FD_ZERO(&reads);
140 		FD_SET(s, &reads);
141 		FD_SET(s2, &reads);
142 		errno = 0;
143 		if (select(32, &reads, 0, 0, 0) < 1 ||
144 		    !FD_ISSET(s2, &reads)) {
145 			if (errno != 0)
146 				perror("select: setting up stderr");
147 			else
148 			    fprintf(stderr,
149 				"select: protocol failure in circuit setup.\n");
150 			(void) close(s2);
151 			goto bad;
152 		}
153 		s3 = accept(s2, (struct sockaddr *)&from, &len);
154 		(void) close(s2);
155 		if (s3 < 0) {
156 			perror("accept");
157 			lport = 0;
158 			goto bad;
159 		}
160 		*fd2p = s3;
161 		from.sin_port = ntohs((u_short)from.sin_port);
162 		if (from.sin_family != AF_INET ||
163 		    from.sin_port >= IPPORT_RESERVED ||
164 		    from.sin_port < IPPORT_RESERVED / 2) {
165 			fprintf(stderr,
166 			    "socket: protocol failure in circuit setup.\n");
167 			goto bad2;
168 		}
169 	}
170 	(void) write(s, locuser, strlen(locuser)+1);
171 	(void) write(s, remuser, strlen(remuser)+1);
172 	(void) write(s, cmd, strlen(cmd)+1);
173 	if (read(s, &c, 1) != 1) {
174 		perror(*ahost);
175 		goto bad2;
176 	}
177 	if (c != 0) {
178 		while (read(s, &c, 1) == 1) {
179 			(void) write(2, &c, 1);
180 			if (c == '\n')
181 				break;
182 		}
183 		goto bad2;
184 	}
185 	sigsetmask(oldmask);
186 	return (s);
187 bad2:
188 	if (lport)
189 		(void) close(*fd2p);
190 bad:
191 	(void) close(s);
192 	sigsetmask(oldmask);
193 	return (-1);
194 }
195 
196 rresvport(alport)
197 	int *alport;
198 {
199 	struct sockaddr_in sin;
200 	int s;
201 
202 	sin.sin_family = AF_INET;
203 	sin.sin_addr.s_addr = INADDR_ANY;
204 	s = socket(AF_INET, SOCK_STREAM, 0);
205 	if (s < 0)
206 		return (-1);
207 	for (;;) {
208 		sin.sin_port = htons((u_short)*alport);
209 		if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
210 			return (s);
211 		if (errno != EADDRINUSE) {
212 			(void) close(s);
213 			return (-1);
214 		}
215 		(*alport)--;
216 		if (*alport == IPPORT_RESERVED/2) {
217 			(void) close(s);
218 			errno = EAGAIN;		/* close */
219 			return (-1);
220 		}
221 	}
222 }
223 
224 int	_check_rhosts_file = 1;
225 
226 ruserok(rhost, superuser, ruser, luser)
227 	const char *rhost, *ruser, *luser;
228 	int superuser;
229 {
230 	FILE *hostf;
231 	char fhost[MAXHOSTNAMELEN];
232 	int first = 1;
233 	register char *sp, *p;
234 	int baselen = -1;
235 
236 	sp = (char *)rhost;
237 	p = fhost;
238 	while (*sp) {
239 		if (*sp == '.') {
240 			if (baselen == -1)
241 				baselen = sp - rhost;
242 			*p++ = *sp++;
243 		} else {
244 			*p++ = isupper(*sp) ? tolower(*sp++) : *sp++;
245 		}
246 	}
247 	*p = '\0';
248 	hostf = superuser ? (FILE *)0 : fopen(_PATH_HEQUIV, "r");
249 again:
250 	if (hostf) {
251 		if (!_validuser(hostf, fhost, luser, ruser, baselen)) {
252 			(void) fclose(hostf);
253 			return(0);
254 		}
255 		(void) fclose(hostf);
256 	}
257 	if (first == 1 && (_check_rhosts_file || superuser)) {
258 		struct stat sbuf;
259 		struct passwd *pwd;
260 		char pbuf[MAXPATHLEN];
261 
262 		first = 0;
263 		if ((pwd = getpwnam(luser)) == NULL)
264 			return(-1);
265 		(void)strcpy(pbuf, pwd->pw_dir);
266 		(void)strcat(pbuf, "/.rhosts");
267 		if ((hostf = fopen(pbuf, "r")) == NULL)
268 			return(-1);
269 		/*
270 		 * if owned by someone other than user or root or if
271 		 * writeable by anyone but the owner, quit
272 		 */
273 		if (fstat(fileno(hostf), &sbuf) ||
274 		    sbuf.st_uid && sbuf.st_uid != pwd->pw_uid ||
275 		    sbuf.st_mode&022) {
276 			fclose(hostf);
277 			return(-1);
278 		}
279 		goto again;
280 	}
281 	return (-1);
282 }
283 
284 /* don't make static, used by lpd(8) */
285 _validuser(hostf, rhost, luser, ruser, baselen)
286 	char *rhost, *luser, *ruser;
287 	FILE *hostf;
288 	int baselen;
289 {
290 	register char *p;
291 	char *user, ahost[MAXHOSTNAMELEN];
292 	static int _checkhost();
293 
294 	while (fgets(ahost, sizeof (ahost), hostf)) {
295 		p = ahost;
296 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
297 			*p = isupper(*p) ? tolower(*p) : *p;
298 			p++;
299 		}
300 		if (*p == ' ' || *p == '\t') {
301 			*p++ = '\0';
302 			while (*p == ' ' || *p == '\t')
303 				p++;
304 			user = p;
305 			while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0')
306 				p++;
307 		} else
308 			user = p;
309 		*p = '\0';
310 		if (_checkhost(rhost, ahost, baselen) &&
311 		    !strcmp(ruser, *user ? user : luser)) {
312 			return (0);
313 		}
314 	}
315 	return (-1);
316 }
317 
318 static
319 _checkhost(rhost, lhost, len)
320 	char *rhost, *lhost;
321 	int len;
322 {
323 	static char ldomain[MAXHOSTNAMELEN + 1];
324 	static char *domainp = NULL;
325 	static int nodomain = 0;
326 	register char *cp;
327 
328 	if (len == -1)
329 		return(!strcmp(rhost, lhost));
330 	if (strncmp(rhost, lhost, len))
331 		return(0);
332 	if (!strcmp(rhost, lhost))
333 		return(1);
334 	if (*(lhost + len) != '\0')
335 		return(0);
336 	if (nodomain)
337 		return(0);
338 	if (!domainp) {
339 		if (gethostname(ldomain, sizeof(ldomain)) == -1) {
340 			nodomain = 1;
341 			return(0);
342 		}
343 		ldomain[MAXHOSTNAMELEN] = NULL;
344 		if ((domainp = index(ldomain, '.')) == (char *)NULL) {
345 			nodomain = 1;
346 			return(0);
347 		}
348 		for (cp = ++domainp; *cp; ++cp)
349 			if (isupper(*cp))
350 				*cp = tolower(*cp);
351 	}
352 	return(!strcmp(domainp, rhost + len +1));
353 }
354