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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)rexec.c 8.1 (Berkeley) 6/4/93";
34 #endif /* LIBC_SCCS and not lint */
36 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/param.h>
42 #include <netinet/in.h>
55 char *getpass(), *getlogin();
58 * Options and other state info.
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 */
66 int macnum; /* number of defined macros */
67 struct macel macros[16];
80 static char tokval[100];
82 static struct toktab {
86 { "default", DEFAULT },
88 { "password", PASSWD },
90 { "account", ACCOUNT },
103 if (feof(cfile) || ferror(cfile))
105 while ((c = getc(cfile)) != EOF &&
106 (c == '\n' || c == '\t' || c == ' ' || c == ','))
112 while ((c = getc(cfile)) != EOF && c != '"') {
119 while ((c = getc(cfile)) != EOF
120 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
129 for (t = toktab; t->tokstr; t++)
130 if (!strcmp(t->tokstr, tokval))
136 ruserpass(host, aname, apass, aacct)
137 char *host, **aname, **apass, **aacct;
139 char *hdir, buf[BUFSIZ], *tmp;
140 char myname[MAXHOSTNAMELEN], *mydomain;
141 int t, i, c, usedefault = 0;
144 hdir = getenv("HOME");
147 if (strlen(hdir) + 8 > sizeof(buf))
149 (void) sprintf(buf, "%s/.netrc", hdir);
150 cfile = fopen(buf, "r");
156 if (gethostname(myname, sizeof(myname)) < 0)
158 if ((mydomain = strchr(myname, '.')) == NULL)
161 while ((t = token())) switch(t) {
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.
176 if (strcasecmp(host, tokval) == 0)
178 if ((tmp = strchr(host, '.')) != NULL &&
179 strcasecmp(tmp, mydomain) == 0 &&
180 strncasecmp(host, tokval, tmp - host) == 0 &&
181 tokval[tmp - host] == '\0')
186 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
190 if (*aname == NULL) {
191 *aname = malloc((unsigned) strlen(tokval) + 1);
192 (void) strcpy(*aname, tokval);
194 if (strcmp(*aname, tokval))
199 if ((*aname == NULL || 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.");
206 if (token() && *apass == NULL) {
207 *apass = malloc((unsigned) strlen(tokval) + 1);
208 (void) strcpy(*apass, tokval);
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.");
218 if (token() && *aacct == NULL) {
219 *aacct = malloc((unsigned) strlen(tokval) + 1);
220 (void) strcpy(*aacct, tokval);
224 while ((c=getc(cfile)) != EOF &&
225 (c == ' ' || c == '\t'))
227 if (c == EOF || c == '\n') {
228 printf("Missing macdef name argument.\n");
232 printf("Limit of 16 macros have already been defined\n");
235 tmp = macros[macnum].mac_name;
237 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
242 printf("Macro definition missing null line terminator.\n");
247 while ((c=getc(cfile)) != EOF && c != '\n');
250 printf("Macro definition missing null line terminator.\n");
254 macros[macnum].mac_start = macbuf;
257 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
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");
267 if (*(tmp-1) == '\0') {
268 macros[macnum++].mac_end = tmp - 1;
275 if (tmp == macbuf + 4096) {
276 printf("4K macro buffer exceeded\n");
281 warnx("Unknown .netrc keyword %s", tokval);
287 (void) fclose(cfile);
290 (void) fclose(cfile);
295 rexec(ahost, rport, name, pass, cmd, fd2p)
298 char *name, *pass, *cmd;
301 struct sockaddr_in sin, sin2, from;
307 hp = gethostbyname(*ahost);
314 ruserpass(hp->h_name, &name, &pass, &acct);
317 s = socket(AF_INET, SOCK_STREAM, 0);
319 perror("rexec: socket");
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) {
338 (void) write(s, "", 1);
343 s2 = socket(AF_INET, SOCK_STREAM, 0);
349 sin2len = sizeof (sin2);
350 if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 ||
351 sin2len != sizeof (sin2)) {
352 perror("getsockname");
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);
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) {
379 while (read(s, &c, 1) == 1) {
380 (void) write(2, &c, 1);