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