2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * $FreeBSD: src/lib/libcompat/4.3/rexec.c,v 1.5.8.3 2000/11/22 13:36:00 ben Exp $
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 */
40 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/param.h>
46 #include <netinet/in.h>
59 char *getpass(), *getlogin();
62 * Options and other state info.
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 */
70 int macnum; /* number of defined macros */
71 struct macel macros[16];
84 static char tokval[100];
86 static struct toktab {
90 { "default", DEFAULT },
92 { "password", PASSWD },
94 { "account", ACCOUNT },
107 if (feof(cfile) || ferror(cfile))
109 while ((c = getc(cfile)) != EOF &&
110 (c == '\n' || c == '\t' || c == ' ' || c == ','))
116 while ((c = getc(cfile)) != EOF && c != '"') {
123 while ((c = getc(cfile)) != EOF
124 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
133 for (t = toktab; t->tokstr; t++)
134 if (!strcmp(t->tokstr, tokval))
140 ruserpass(host, aname, apass, aacct)
141 char *host, **aname, **apass, **aacct;
143 char *hdir, buf[BUFSIZ], *tmp;
144 char myname[MAXHOSTNAMELEN], *mydomain;
145 int t, i, c, usedefault = 0;
148 hdir = getenv("HOME");
151 if (strlen(hdir) + 8 > sizeof(buf))
153 (void) sprintf(buf, "%s/.netrc", hdir);
154 cfile = fopen(buf, "r");
160 if (gethostname(myname, sizeof(myname)) < 0)
162 if ((mydomain = strchr(myname, '.')) == NULL)
165 while ((t = token())) switch(t) {
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.
180 if (strcasecmp(host, tokval) == 0)
182 if ((tmp = strchr(host, '.')) != NULL &&
183 strcasecmp(tmp, mydomain) == 0 &&
184 strncasecmp(host, tokval, tmp - host) == 0 &&
185 tokval[tmp - host] == '\0')
190 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
195 *aname = malloc((unsigned) strlen(tokval) + 1);
196 (void) strcpy(*aname, tokval);
198 if (strcmp(*aname, tokval))
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.");
210 if (token() && *apass == 0) {
211 *apass = malloc((unsigned) strlen(tokval) + 1);
212 (void) strcpy(*apass, tokval);
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.");
222 if (token() && *aacct == 0) {
223 *aacct = malloc((unsigned) strlen(tokval) + 1);
224 (void) strcpy(*aacct, tokval);
228 while ((c=getc(cfile)) != EOF &&
229 (c == ' ' || c == '\t'))
231 if (c == EOF || c == '\n') {
232 printf("Missing macdef name argument.\n");
236 printf("Limit of 16 macros have already been defined\n");
239 tmp = macros[macnum].mac_name;
241 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
246 printf("Macro definition missing null line terminator.\n");
251 while ((c=getc(cfile)) != EOF && c != '\n');
254 printf("Macro definition missing null line terminator.\n");
258 macros[macnum].mac_start = macbuf;
261 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
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");
271 if (*(tmp-1) == '\0') {
272 macros[macnum++].mac_end = tmp - 1;
279 if (tmp == macbuf + 4096) {
280 printf("4K macro buffer exceeded\n");
285 warnx("Unknown .netrc keyword %s", tokval);
291 (void) fclose(cfile);
294 (void) fclose(cfile);
299 rexec(ahost, rport, name, pass, cmd, fd2p)
302 char *name, *pass, *cmd;
305 struct sockaddr_in sin, sin2, from;
311 hp = gethostbyname(*ahost);
317 ruserpass(hp->h_name, &name, &pass);
319 s = socket(AF_INET, SOCK_STREAM, 0);
321 perror("rexec: socket");
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) {
338 (void) write(s, "", 1);
344 s2 = socket(AF_INET, SOCK_STREAM, 0);
350 sin2len = sizeof (sin2);
351 if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 ||
352 sin2len != sizeof (sin2)) {
353 perror("getsockname");
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);
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) {
380 while (read(s, &c, 1) == 1) {
381 (void) write(2, &c, 1);