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 $
34 * $DragonFly: src/lib/libcompat/4.3/rexec.c,v 1.4 2008/09/04 09:08:22 hasso Exp $
36 * @(#)rexec.c 8.1 (Berkeley) 6/4/93
39 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/param.h>
45 #include <netinet/in.h>
58 char *getpass(), *getlogin();
61 * Options and other state info.
64 char mac_name[9]; /* macro name */
65 char *mac_start; /* start of macro in macbuf */
66 char *mac_end; /* end of macro in macbuf */
69 int macnum; /* number of defined macros */
70 struct macel macros[16];
83 static char tokval[100];
85 static struct toktab {
89 { "default", DEFAULT },
91 { "password", PASSWD },
93 { "account", ACCOUNT },
106 if (feof(cfile) || ferror(cfile))
108 while ((c = getc(cfile)) != EOF &&
109 (c == '\n' || c == '\t' || c == ' ' || c == ','))
115 while ((c = getc(cfile)) != EOF && c != '"') {
122 while ((c = getc(cfile)) != EOF
123 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
132 for (t = toktab; t->tokstr; t++)
133 if (!strcmp(t->tokstr, tokval))
139 ruserpass(host, aname, apass, aacct)
140 char *host, **aname, **apass, **aacct;
142 char *hdir, buf[BUFSIZ], *tmp;
143 char myname[MAXHOSTNAMELEN], *mydomain;
144 int t, i, c, usedefault = 0;
147 hdir = getenv("HOME");
150 if (strlen(hdir) + 8 > sizeof(buf))
152 (void) sprintf(buf, "%s/.netrc", hdir);
153 cfile = fopen(buf, "r");
159 if (gethostname(myname, sizeof(myname)) < 0)
161 if ((mydomain = strchr(myname, '.')) == NULL)
164 while ((t = token())) switch(t) {
175 * Allow match either for user's input host name
176 * or official hostname. Also allow match of
177 * incompletely-specified host in local domain.
179 if (strcasecmp(host, tokval) == 0)
181 if ((tmp = strchr(host, '.')) != NULL &&
182 strcasecmp(tmp, mydomain) == 0 &&
183 strncasecmp(host, tokval, tmp - host) == 0 &&
184 tokval[tmp - host] == '\0')
189 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
194 *aname = malloc((unsigned) strlen(tokval) + 1);
195 (void) strcpy(*aname, tokval);
197 if (strcmp(*aname, tokval))
202 if ((*aname == 0 || strcmp(*aname, "anonymous")) &&
203 fstat(fileno(cfile), &stb) >= 0 &&
204 (stb.st_mode & 077) != 0) {
205 warnx("Error: .netrc file is readable by others.");
206 warnx("Remove password or make file unreadable by others.");
209 if (token() && *apass == 0) {
210 *apass = malloc((unsigned) strlen(tokval) + 1);
211 (void) strcpy(*apass, tokval);
215 if (fstat(fileno(cfile), &stb) >= 0
216 && (stb.st_mode & 077) != 0) {
217 warnx("Error: .netrc file is readable by others.");
218 warnx("Remove account or make file unreadable by others.");
221 if (token() && *aacct == 0) {
222 *aacct = malloc((unsigned) strlen(tokval) + 1);
223 (void) strcpy(*aacct, tokval);
227 while ((c=getc(cfile)) != EOF &&
228 (c == ' ' || c == '\t'))
230 if (c == EOF || c == '\n') {
231 printf("Missing macdef name argument.\n");
235 printf("Limit of 16 macros have already been defined\n");
238 tmp = macros[macnum].mac_name;
240 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
245 printf("Macro definition missing null line terminator.\n");
250 while ((c=getc(cfile)) != EOF && c != '\n');
253 printf("Macro definition missing null line terminator.\n");
257 macros[macnum].mac_start = macbuf;
260 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
262 tmp = macros[macnum].mac_start;
263 while (tmp != macbuf + 4096) {
264 if ((c=getc(cfile)) == EOF) {
265 printf("Macro definition missing null line terminator.\n");
270 if (*(tmp-1) == '\0') {
271 macros[macnum++].mac_end = tmp - 1;
278 if (tmp == macbuf + 4096) {
279 printf("4K macro buffer exceeded\n");
284 warnx("Unknown .netrc keyword %s", tokval);
290 (void) fclose(cfile);
293 (void) fclose(cfile);
298 rexec_af(ahost, rport, name, pass, cmd, fd2p, af)
301 const char *name, *pass, *cmd;
305 struct sockaddr_storage sa2, from;
306 struct addrinfo hints, *res0;
307 const char *orig_name = name;
308 const char *orig_pass = pass;
309 static char *ahostbuf;
314 char servbuff[NI_MAXSERV];
316 sprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
317 servbuff[sizeof(servbuff) - 1] = '\0';
319 memset(&hints, '\0', sizeof(hints));
320 hints.ai_family = af;
321 hints.ai_socktype = SOCK_STREAM;
322 hints.ai_flags = AI_CANONNAME;
323 gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
325 /* XXX: set errno? */
329 if (res0->ai_canonname){
331 ahostbuf = strdup (res0->ai_canonname);
332 if (ahostbuf == NULL) {
333 perror ("rexec: strdup");
339 __set_errno (ENOENT);
342 ruserpass(res0->ai_canonname, &name, &pass);
344 s = socket(res0->ai_family, res0->ai_socktype, 0);
346 perror("rexec: socket");
349 if (connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
350 if (errno == ECONNREFUSED && timo <= 16) {
356 perror(res0->ai_canonname);
360 (void) write(s, "", 1);
367 s2 = socket(res0->ai_family, res0->ai_socktype, 0);
373 sa2len = sizeof (sa2);
374 if (getsockname(s2, (struct sockaddr *)&sa2, &sa2len) < 0) {
375 perror("getsockname");
378 } else if (sa2len != SA_LEN((struct sockaddr *)&sa2)) {
384 if (!getnameinfo((struct sockaddr *)&sa2, sa2len,
385 NULL, 0, servbuff, sizeof(servbuff),
387 port = atoi(servbuff);
388 (void) sprintf(num, "%u", port);
389 (void) write(s, num, strlen(num)+1);
390 { socklen_t len = sizeof (from);
391 s3 = accept(s2, (struct sockaddr *)&from,
403 (void) write(s, name, strlen(name) + 1);
404 /* should public key encypt the password here */
405 (void) write(s, pass, strlen(pass) + 1);
406 (void) write(s, cmd, strlen(cmd) + 1);
408 /* We don't need the memory allocated for the name and the password
409 in ruserpass anymore. */
410 if (name != orig_name)
411 free ((char *) name);
412 if (pass != orig_pass)
413 free ((char *) pass);
415 if (read(s, &c, 1) != 1) {
420 while (read(s, &c, 1) == 1) {
421 (void) write(2, &c, 1);
439 rexec(ahost, rport, name, pass, cmd, fd2p)
442 char *name, *pass, *cmd;
445 struct sockaddr_in sin, sin2, from;
452 hp = gethostbyname(*ahost);
458 ruserpass(hp->h_name, &name, &pass, &acct);
462 s = socket(AF_INET, SOCK_STREAM, 0);
464 perror("rexec: socket");
467 sin.sin_family = hp->h_addrtype;
468 sin.sin_port = rport;
469 bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
470 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
471 if (errno == ECONNREFUSED && timo <= 16) {
481 (void) write(s, "", 1);
487 s2 = socket(AF_INET, SOCK_STREAM, 0);
493 sin2len = sizeof (sin2);
494 if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 ||
495 sin2len != sizeof (sin2)) {
496 perror("getsockname");
500 port = ntohs((u_short)sin2.sin_port);
501 (void) sprintf(num, "%u", port);
502 (void) write(s, num, strlen(num)+1);
503 { int len = sizeof (from);
504 s3 = accept(s2, (struct sockaddr *)&from, &len);
514 (void) write(s, name, strlen(name) + 1);
515 /* should public key encypt the password here */
516 (void) write(s, pass, strlen(pass) + 1);
517 (void) write(s, cmd, strlen(cmd) + 1);
518 if (read(s, &c, 1) != 1) {
523 while (read(s, &c, 1) == 1) {
524 (void) write(2, &c, 1);