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 * 4. 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 /* CV 2006-07-04: Tweaked for inclusion into Cygwin. */
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)rexec.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
45 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/param.h>
51 #include <netinet/in.h>
64 int cygwin_accept (int, struct sockaddr
*, socklen_t
*);
65 int cygwin_connect (int, const struct sockaddr
*, socklen_t
);
66 int cygwin_getsockname (int, struct sockaddr
*, socklen_t
*);
67 void cygwin_herror (const char *);
68 int cygwin_listen (int, int);
69 int cygwin_socket (int, int, int);
73 * Options and other state info.
76 char mac_name
[9]; /* macro name */
77 char *mac_start
; /* start of macro in macbuf */
78 char *mac_end
; /* end of macro in macbuf */
81 int macnum
; /* number of defined macros */
82 struct macel macros
[16];
95 static char tokval
[100];
97 static struct toktab
{
101 { "default", DEFAULT
},
103 { "password", PASSWD
},
104 { "passwd", PASSWD
},
105 { "account", ACCOUNT
},
107 { "macdef", MACDEF
},
118 if (feof(cfile
) || ferror(cfile
))
120 while ((c
= getc(cfile
)) != EOF
&&
121 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
127 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
134 while ((c
= getc(cfile
)) != EOF
135 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
144 for (t
= toktab
; t
->tokstr
; t
++)
145 if (!strcmp(t
->tokstr
, tokval
))
151 ruserpass(const char *host
, char **aname
, char **apass
, char **aacct
)
154 char buf
[BUFSIZ
], *tmp
;
155 char myname
[INTERNET_MAX_HOST_NAME_LENGTH
+ 1];
156 const char *mydomain
;
157 int t
, i
, c
, usedefault
= 0;
160 hdir
= getenv("HOME");
163 if (strlen(hdir
) + 8 > sizeof(buf
))
165 (void) sprintf(buf
, "%s/.netrc", hdir
);
166 cfile
= fopen(buf
, "r");
172 if (cygwin_gethostname(myname
, sizeof(myname
)) < 0)
174 if ((mydomain
= strchr(myname
, '.')) == NULL
)
177 while ((t
= token())) switch(t
) {
188 * Allow match either for user's input host name
189 * or official hostname. Also allow match of
190 * incompletely-specified host in local domain.
192 if (strcasecmp(host
, tokval
) == 0)
194 if ((tmp
= strchr(host
, '.')) != NULL
&&
195 strcasecmp(tmp
, mydomain
) == 0 &&
196 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
197 tokval
[tmp
- host
] == '\0')
202 while ((t
= token()) && t
!= MACH
&& t
!= DEFAULT
) switch(t
) {
207 *aname
= (char *) malloc((unsigned) strlen(tokval
) + 1);
208 (void) strcpy(*aname
, tokval
);
210 if (strcmp(*aname
, tokval
))
216 if ((*aname
== 0 || strcmp(*aname
, "anonymous")) &&
217 fstat64(fileno(cfile
), &stb
) >= 0 &&
218 (stb
.st_mode
& 077) != 0) {
219 warnx("Error: .netrc file is readable by others.");
220 warnx("Remove password or make file unreadable by others.");
223 if (token() && *apass
== 0) {
224 *apass
= (char *) malloc((unsigned) strlen(tokval
) + 1);
225 (void) strcpy(*apass
, tokval
);
229 if (fstat64(fileno(cfile
), &stb
) >= 0
230 && (stb
.st_mode
& 077) != 0) {
231 warnx("Error: .netrc file is readable by others.");
232 warnx("Remove account or make file unreadable by others.");
235 if (token() && aacct
&& *aacct
== 0) {
236 *aacct
= (char *) malloc((unsigned) strlen(tokval
) + 1);
237 (void) strcpy(*aacct
, tokval
);
241 while ((c
=getc(cfile
)) != EOF
&&
242 (c
== ' ' || c
== '\t'))
244 if (c
== EOF
|| c
== '\n') {
245 printf("Missing macdef name argument.\n");
249 printf("Limit of 16 macros have already been defined\n");
252 tmp
= macros
[macnum
].mac_name
;
254 for (i
=0; i
< 8 && (c
=getc(cfile
)) != EOF
&&
259 printf("Macro definition missing null line terminator.\n");
264 while ((c
=getc(cfile
)) != EOF
&& c
!= '\n');
267 printf("Macro definition missing null line terminator.\n");
271 macros
[macnum
].mac_start
= macbuf
;
274 macros
[macnum
].mac_start
= macros
[macnum
-1].mac_end
+ 1;
276 tmp
= macros
[macnum
].mac_start
;
277 while (tmp
!= macbuf
+ 4096) {
278 if ((c
=getc(cfile
)) == EOF
) {
279 printf("Macro definition missing null line terminator.\n");
284 if (*(tmp
-1) == '\0') {
285 macros
[macnum
++].mac_end
= tmp
- 1;
292 if (tmp
== macbuf
+ 4096) {
293 printf("4K macro buffer exceeded\n");
298 warnx("Unknown .netrc keyword %s", tokval
);
304 (void) fclose(cfile
);
307 (void) fclose(cfile
);
312 cygwin_rexec (char **ahost
, unsigned short rport
, char *name
, char *pass
,
313 char *cmd
, int *fd2p
)
315 struct sockaddr_in sin
, sin2
, from
;
320 char ahostbuf
[INTERNET_MAX_HOST_NAME_LENGTH
+ 1];
323 if (efault
.faulted (EFAULT
))
326 hp
= cygwin_gethostbyname(*ahost
);
328 cygwin_herror(*ahost
);
331 *ahost
= strcpy (ahostbuf
, hp
->h_name
);
332 ruserpass(hp
->h_name
, &name
, &pass
, NULL
);
338 s
= cygwin_socket(AF_INET
, SOCK_STREAM
, 0);
340 perror("rexec: socket");
343 sin
.sin_family
= hp
->h_addrtype
;
344 sin
.sin_port
= rport
;
345 bcopy(hp
->h_addr
, (caddr_t
)&sin
.sin_addr
, hp
->h_length
);
346 if (cygwin_connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) < 0) {
347 if (errno
== ECONNREFUSED
&& timo
<= 16) {
357 (void) write(s
, "", 1);
362 s2
= cygwin_socket(AF_INET
, SOCK_STREAM
, 0);
367 cygwin_listen(s2
, 1);
368 sin2len
= sizeof (sin2
);
369 if (cygwin_getsockname(s2
, (struct sockaddr
*)&sin2
, &sin2len
) < 0 ||
370 sin2len
!= sizeof (sin2
)) {
371 perror("getsockname");
375 port
= ntohs((u_short
)sin2
.sin_port
);
376 (void) sprintf(num
, "%u", port
);
377 (void) write(s
, num
, strlen(num
)+1);
378 { int len
= sizeof (from
);
379 s3
= cygwin_accept(s2
, (struct sockaddr
*)&from
, &len
);
389 (void) write(s
, name
, strlen(name
) + 1);
390 /* should public key encypt the password here */
391 (void) write(s
, pass
, strlen(pass
) + 1);
392 (void) write(s
, cmd
, strlen(cmd
) + 1);
393 if (read(s
, &c
, 1) != 1) {
398 while (read(s
, &c
, 1) == 1) {
399 (void) write(2, &c
, 1);