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