rexec.c revision 301152
126924Smsmith/*
226924Smsmith * Copyright (c) 1980, 1993
326924Smsmith *	The Regents of the University of California.  All rights reserved.
426924Smsmith *
526924Smsmith * Redistribution and use in source and binary forms, with or without
626924Smsmith * modification, are permitted provided that the following conditions
726924Smsmith * are met:
826924Smsmith * 1. Redistributions of source code must retain the above copyright
926924Smsmith *    notice, this list of conditions and the following disclaimer.
1026924Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1126924Smsmith *    notice, this list of conditions and the following disclaimer in the
1226924Smsmith *    documentation and/or other materials provided with the distribution.
1326924Smsmith * 4. Neither the name of the University nor the names of its contributors
1426924Smsmith *    may be used to endorse or promote products derived from this software
1526924Smsmith *    without specific prior written permission.
1626924Smsmith *
1726924Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1826924Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1926924Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2026924Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2126924Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2226924Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2326924Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2426924Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2526924Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2626924Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2726924Smsmith * SUCH DAMAGE.
2826924Smsmith *
2926924Smsmith * $FreeBSD: stable/10/lib/libcompat/4.3/rexec.c 301152 2016-06-01 17:41:00Z truckman $
3026924Smsmith */
3126924Smsmith
3293032Simp#if defined(LIBC_SCCS) && !defined(lint)
3393032Simpstatic char sccsid[] = "@(#)rexec.c	8.1 (Berkeley) 6/4/93";
3426924Smsmith#endif /* LIBC_SCCS and not lint */
3526924Smsmith
3626924Smsmith#include <sys/types.h>
3726924Smsmith#include <sys/uio.h>
3826924Smsmith#include <sys/socket.h>
3926924Smsmith#include <sys/param.h>
4026924Smsmith#include <sys/stat.h>
4126924Smsmith
4226924Smsmith#include <netinet/in.h>
4326924Smsmith
4426924Smsmith#include <stdio.h>
4526924Smsmith#include <unistd.h>
4626924Smsmith#include <string.h>
4726924Smsmith#include <netdb.h>
4826924Smsmith#include <errno.h>
4926924Smsmith#include <ctype.h>
5026924Smsmith#include <err.h>
5193032Simp#include <stdlib.h>
5293032Simp#include <unistd.h>
5393032Simp
5493032Simpint	rexecoptions;
5526924Smsmithchar	*getpass(), *getlogin();
5626924Smsmith
5726924Smsmith/*
58 * Options and other state info.
59 */
60struct macel {
61	char mac_name[9];	/* macro name */
62	char *mac_start;	/* start of macro in macbuf */
63	char *mac_end;		/* end of macro in macbuf */
64};
65
66int macnum;			/* number of defined macros */
67struct macel macros[16];
68char macbuf[4096];
69
70static	FILE *cfile;
71
72#define	DEFAULT	1
73#define	LOGIN	2
74#define	PASSWD	3
75#define	ACCOUNT 4
76#define MACDEF  5
77#define	ID	10
78#define	MACH	11
79
80static char tokval[100];
81
82static struct toktab {
83	char *tokstr;
84	int tval;
85} toktab[]= {
86	{ "default",	DEFAULT },
87	{ "login",	LOGIN },
88	{ "password",	PASSWD },
89	{ "passwd",	PASSWD },
90	{ "account",	ACCOUNT },
91	{ "machine",	MACH },
92	{ "macdef",	MACDEF },
93	{ NULL,		0 }
94};
95
96static int
97token()
98{
99	char *cp;
100	int c;
101	struct toktab *t;
102
103	if (feof(cfile) || ferror(cfile))
104		return (0);
105	while ((c = getc(cfile)) != EOF &&
106	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
107		continue;
108	if (c == EOF)
109		return (0);
110	cp = tokval;
111	if (c == '"') {
112		while ((c = getc(cfile)) != EOF && c != '"') {
113			if (c == '\\')
114				c = getc(cfile);
115			*cp++ = c;
116		}
117	} else {
118		*cp++ = c;
119		while ((c = getc(cfile)) != EOF
120		    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
121			if (c == '\\')
122				c = getc(cfile);
123			*cp++ = c;
124		}
125	}
126	*cp = 0;
127	if (tokval[0] == 0)
128		return (0);
129	for (t = toktab; t->tokstr; t++)
130		if (!strcmp(t->tokstr, tokval))
131			return (t->tval);
132	return (ID);
133}
134
135static int
136ruserpass(host, aname, apass, aacct)
137	char *host, **aname, **apass, **aacct;
138{
139	char *hdir, buf[BUFSIZ], *tmp;
140	char myname[MAXHOSTNAMELEN], *mydomain;
141	int t, i, c, usedefault = 0;
142	struct stat stb;
143
144	hdir = getenv("HOME");
145	if (hdir == NULL)
146		hdir = ".";
147	if (strlen(hdir) + 8 > sizeof(buf))
148		return (0);
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 = "";
160next:
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	}
286done:
287	(void) fclose(cfile);
288	return (0);
289bad:
290	(void) fclose(cfile);
291	return (-1);
292}
293
294int
295rexec(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, *acct;
306
307	hp = gethostbyname(*ahost);
308	if (hp == 0) {
309		herror(*ahost);
310		return (-1);
311	}
312	*ahost = hp->h_name;
313	acct = NULL;
314	ruserpass(hp->h_name, &name, &pass, &acct);
315	free(acct);
316retry:
317	s = socket(AF_INET, SOCK_STREAM, 0);
318	if (s < 0) {
319		perror("rexec: socket");
320		return (-1);
321	}
322	sin.sin_family = hp->h_addrtype;
323	sin.sin_port = rport;
324	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
325	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
326		if (errno == ECONNREFUSED && timo <= 16) {
327			(void) close(s);
328			sleep(timo);
329			timo *= 2;
330			goto retry;
331		}
332		perror(hp->h_name);
333		(void) close(s);
334		return (-1);
335	}
336	if (fd2p == 0) {
337		(void) write(s, "", 1);
338		port = 0;
339	} else {
340		char num[8];
341		int s2, sin2len;
342
343		s2 = socket(AF_INET, SOCK_STREAM, 0);
344		if (s2 < 0) {
345			(void) close(s);
346			return (-1);
347		}
348		listen(s2, 1);
349		sin2len = sizeof (sin2);
350		if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 ||
351		  sin2len != sizeof (sin2)) {
352			perror("getsockname");
353			(void) close(s2);
354			goto bad;
355		}
356		port = ntohs((u_short)sin2.sin_port);
357		(void) sprintf(num, "%u", port);
358		(void) write(s, num, strlen(num)+1);
359		{ int len = sizeof (from);
360		  s3 = accept(s2, (struct sockaddr *)&from, &len);
361		  close(s2);
362		  if (s3 < 0) {
363			perror("accept");
364			port = 0;
365			goto bad;
366		  }
367		}
368		*fd2p = s3;
369	}
370	(void) write(s, name, strlen(name) + 1);
371	/* should public key encypt the password here */
372	(void) write(s, pass, strlen(pass) + 1);
373	(void) write(s, cmd, strlen(cmd) + 1);
374	if (read(s, &c, 1) != 1) {
375		perror(*ahost);
376		goto bad;
377	}
378	if (c != 0) {
379		while (read(s, &c, 1) == 1) {
380			(void) write(2, &c, 1);
381			if (c == '\n')
382				break;
383		}
384		goto bad;
385	}
386	return (s);
387bad:
388	if (port)
389		(void) close(*fd2p);
390	(void) close(s);
391	return (-1);
392}
393