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