xref: /freebsd/lib/libcompat/4.3/rexec.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)rexec.c	8.1 (Berkeley) 6/4/93");
34 
35 #include <sys/types.h>
36 #include <sys/uio.h>
37 #include <sys/socket.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 
41 #include <netinet/in.h>
42 
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <netdb.h>
47 #include <errno.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 
53 int	rexecoptions;
54 char	*getpass(), *getlogin();
55 
56 /*
57  * Options and other state info.
58  */
59 struct macel {
60 	char mac_name[9];	/* macro name */
61 	char *mac_start;	/* start of macro in macbuf */
62 	char *mac_end;		/* end of macro in macbuf */
63 };
64 
65 int macnum;			/* number of defined macros */
66 struct macel macros[16];
67 char macbuf[4096];
68 
69 static	FILE *cfile;
70 
71 #define	DEFAULT	1
72 #define	LOGIN	2
73 #define	PASSWD	3
74 #define	ACCOUNT 4
75 #define MACDEF  5
76 #define	ID	10
77 #define	MACH	11
78 
79 static char tokval[100];
80 
81 static struct toktab {
82 	char *tokstr;
83 	int tval;
84 } toktab[]= {
85 	{ "default",	DEFAULT },
86 	{ "login",	LOGIN },
87 	{ "password",	PASSWD },
88 	{ "passwd",	PASSWD },
89 	{ "account",	ACCOUNT },
90 	{ "machine",	MACH },
91 	{ "macdef",	MACDEF },
92 	{ NULL,		0 }
93 };
94 
95 static int
96 token()
97 {
98 	char *cp;
99 	int c;
100 	struct toktab *t;
101 
102 	if (feof(cfile) || ferror(cfile))
103 		return (0);
104 	while ((c = getc(cfile)) != EOF &&
105 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
106 		continue;
107 	if (c == EOF)
108 		return (0);
109 	cp = tokval;
110 	if (c == '"') {
111 		while ((c = getc(cfile)) != EOF && c != '"') {
112 			if (c == '\\')
113 				c = getc(cfile);
114 			*cp++ = c;
115 		}
116 	} else {
117 		*cp++ = c;
118 		while ((c = getc(cfile)) != EOF
119 		    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
120 			if (c == '\\')
121 				c = getc(cfile);
122 			*cp++ = c;
123 		}
124 	}
125 	*cp = 0;
126 	if (tokval[0] == 0)
127 		return (0);
128 	for (t = toktab; t->tokstr; t++)
129 		if (!strcmp(t->tokstr, tokval))
130 			return (t->tval);
131 	return (ID);
132 }
133 
134 static int
135 ruserpass(host, aname, apass, aacct)
136 	char *host, **aname, **apass, **aacct;
137 {
138 	char *hdir, buf[BUFSIZ], *tmp;
139 	char myname[MAXHOSTNAMELEN], *mydomain;
140 	int t, i, c, usedefault = 0;
141 	struct stat stb;
142 
143 	hdir = getenv("HOME");
144 	if (hdir == NULL)
145 		hdir = ".";
146 	if (strlen(hdir) + 8 > sizeof(buf))
147 		return (0);
148 	(void) sprintf(buf, "%s/.netrc", hdir);
149 	cfile = fopen(buf, "r");
150 	if (cfile == NULL) {
151 		if (errno != ENOENT)
152 			warn("%s", buf);
153 		return (0);
154 	}
155 	if (gethostname(myname, sizeof(myname)) < 0)
156 		myname[0] = '\0';
157 	if ((mydomain = strchr(myname, '.')) == NULL)
158 		mydomain = "";
159 next:
160 	while ((t = token())) switch(t) {
161 
162 	case DEFAULT:
163 		usedefault = 1;
164 		/* FALL THROUGH */
165 
166 	case MACH:
167 		if (!usedefault) {
168 			if (token() != ID)
169 				continue;
170 			/*
171 			 * Allow match either for user's input host name
172 			 * or official hostname.  Also allow match of
173 			 * incompletely-specified host in local domain.
174 			 */
175 			if (strcasecmp(host, tokval) == 0)
176 				goto match;
177 			if ((tmp = strchr(host, '.')) != NULL &&
178 			    strcasecmp(tmp, mydomain) == 0 &&
179 			    strncasecmp(host, tokval, tmp - host) == 0 &&
180 			    tokval[tmp - host] == '\0')
181 				goto match;
182 			continue;
183 		}
184 	match:
185 		while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
186 
187 		case LOGIN:
188 			if (token())
189 				if (*aname == NULL) {
190 					*aname = malloc((unsigned) strlen(tokval) + 1);
191 					(void) strcpy(*aname, tokval);
192 				} else {
193 					if (strcmp(*aname, tokval))
194 						goto next;
195 				}
196 			break;
197 		case PASSWD:
198 			if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
199 			    fstat(fileno(cfile), &stb) >= 0 &&
200 			    (stb.st_mode & 077) != 0) {
201 	warnx("Error: .netrc file is readable by others.");
202 	warnx("Remove password or make file unreadable by others.");
203 				goto bad;
204 			}
205 			if (token() && *apass == NULL) {
206 				*apass = malloc((unsigned) strlen(tokval) + 1);
207 				(void) strcpy(*apass, tokval);
208 			}
209 			break;
210 		case ACCOUNT:
211 			if (fstat(fileno(cfile), &stb) >= 0
212 			    && (stb.st_mode & 077) != 0) {
213 	warnx("Error: .netrc file is readable by others.");
214 	warnx("Remove account or make file unreadable by others.");
215 				goto bad;
216 			}
217 			if (token() && *aacct == NULL) {
218 				*aacct = malloc((unsigned) strlen(tokval) + 1);
219 				(void) strcpy(*aacct, tokval);
220 			}
221 			break;
222 		case MACDEF:
223 			while ((c=getc(cfile)) != EOF &&
224 						(c == ' ' || c == '\t'))
225 				;
226 			if (c == EOF || c == '\n') {
227 				printf("Missing macdef name argument.\n");
228 				goto bad;
229 			}
230 			if (macnum == 16) {
231 				printf("Limit of 16 macros have already been defined\n");
232 				goto bad;
233 			}
234 			tmp = macros[macnum].mac_name;
235 			*tmp++ = c;
236 			for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
237 			    !isspace(c); ++i) {
238 				*tmp++ = c;
239 			}
240 			if (c == EOF) {
241 				printf("Macro definition missing null line terminator.\n");
242 				goto bad;
243 			}
244 			*tmp = '\0';
245 			if (c != '\n') {
246 				while ((c=getc(cfile)) != EOF && c != '\n');
247 			}
248 			if (c == EOF) {
249 				printf("Macro definition missing null line terminator.\n");
250 				goto bad;
251 			}
252 			if (macnum == 0) {
253 				macros[macnum].mac_start = macbuf;
254 			}
255 			else {
256 				macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
257 			}
258 			tmp = macros[macnum].mac_start;
259 			while (tmp != macbuf + 4096) {
260 				if ((c=getc(cfile)) == EOF) {
261 				printf("Macro definition missing null line terminator.\n");
262 					goto bad;
263 				}
264 				*tmp = c;
265 				if (*tmp == '\n') {
266 					if (*(tmp-1) == '\0') {
267 					   macros[macnum++].mac_end = tmp - 1;
268 					   break;
269 					}
270 					*tmp = '\0';
271 				}
272 				tmp++;
273 			}
274 			if (tmp == macbuf + 4096) {
275 				printf("4K macro buffer exceeded\n");
276 				goto bad;
277 			}
278 			break;
279 		default:
280 			warnx("Unknown .netrc keyword %s", tokval);
281 			break;
282 		}
283 		goto done;
284 	}
285 done:
286 	(void) fclose(cfile);
287 	return (0);
288 bad:
289 	(void) fclose(cfile);
290 	return (-1);
291 }
292 
293 int
294 rexec(ahost, rport, name, pass, cmd, fd2p)
295 	char **ahost;
296 	int rport;
297 	char *name, *pass, *cmd;
298 	int *fd2p;
299 {
300 	struct sockaddr_in sin, sin2, from;
301 	struct hostent *hp;
302 	u_short port;
303 	int s, timo = 1, s3;
304 	char c, *acct;
305 
306 	hp = gethostbyname(*ahost);
307 	if (hp == NULL) {
308 		herror(*ahost);
309 		return (-1);
310 	}
311 	*ahost = hp->h_name;
312 	acct = NULL;
313 	ruserpass(hp->h_name, &name, &pass, &acct);
314 	free(acct);
315 retry:
316 	s = socket(AF_INET, SOCK_STREAM, 0);
317 	if (s < 0) {
318 		perror("rexec: socket");
319 		return (-1);
320 	}
321 	sin.sin_family = hp->h_addrtype;
322 	sin.sin_port = rport;
323 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
324 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
325 		if (errno == ECONNREFUSED && timo <= 16) {
326 			(void) close(s);
327 			sleep(timo);
328 			timo *= 2;
329 			goto retry;
330 		}
331 		perror(hp->h_name);
332 		(void) close(s);
333 		return (-1);
334 	}
335 	port = 0;
336 	if (fd2p == 0)
337 		(void) write(s, "", 1);
338 	else {
339 		char num[8];
340 		int s2, sin2len;
341 
342 		s2 = socket(AF_INET, SOCK_STREAM, 0);
343 		if (s2 < 0) {
344 			(void) close(s);
345 			return (-1);
346 		}
347 		listen(s2, 1);
348 		sin2len = sizeof (sin2);
349 		if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 ||
350 		  sin2len != sizeof (sin2)) {
351 			perror("getsockname");
352 			(void) close(s2);
353 			goto bad;
354 		}
355 		port = ntohs((u_short)sin2.sin_port);
356 		(void) sprintf(num, "%hu", port);
357 		(void) write(s, num, strlen(num)+1);
358 		{ int len = sizeof (from);
359 		  s3 = accept(s2, (struct sockaddr *)&from, &len);
360 		  close(s2);
361 		  if (s3 < 0) {
362 			perror("accept");
363 			port = 0;
364 			goto bad;
365 		  }
366 		}
367 		*fd2p = s3;
368 	}
369 	(void) write(s, name, strlen(name) + 1);
370 	/* should public key encypt the password here */
371 	(void) write(s, pass, strlen(pass) + 1);
372 	(void) write(s, cmd, strlen(cmd) + 1);
373 	if (read(s, &c, 1) != 1) {
374 		perror(*ahost);
375 		goto bad;
376 	}
377 	if (c != 0) {
378 		while (read(s, &c, 1) == 1) {
379 			(void) write(2, &c, 1);
380 			if (c == '\n')
381 				break;
382 		}
383 		goto bad;
384 	}
385 	return (s);
386 bad:
387 	if (port)
388 		(void) close(*fd2p);
389 	(void) close(s);
390 	return (-1);
391 }
392