2 * Copyright (c) 1985, 1993, 1994
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
35 static char sccsid
[] = "@(#)ruserpass.c 8.3 (Berkeley) 4/2/94";
38 #include <sys/types.h>
50 /* #include "ftp_var.h" */
52 static int token
__P((void));
63 static char tokval
[100];
65 static struct toktab
{
69 { "default", DEFAULT
},
71 { "password", PASSWD
},
73 { "account", ACCOUNT
},
74 { "machine", MACHINE
},
82 ruserpass(host
, aname
, apass
)
83 char *host
, **aname
, **apass
;
85 char *hdir
, *buf
, *tmp
;
86 char myname
[1024], *mydomain
;
87 int t
, i
, c
, usedefault
= 0;
90 hdir
= __secure_getenv("HOME");
92 /* If we can't get HOME, fail instead of trying ".",
93 which is no improvement. This really should call
94 getpwuid(getuid()). */
99 buf
= alloca (strlen (hdir
) + 8);
101 __stpcpy (__stpcpy (buf
, hdir
), "/.netrc");
102 cfile
= fopen(buf
, "r");
108 if (gethostname(myname
, sizeof(myname
)) < 0)
110 if ((mydomain
= strchr(myname
, '.')) == NULL
)
113 while ((t
= token())) switch(t
) {
124 * Allow match either for user's input host name
125 * or official hostname. Also allow match of
126 * incompletely-specified host in local domain.
128 if (strcasecmp(host
, tokval
) == 0)
130 /* if (strcasecmp(hostname, tokval) == 0)
132 if ((tmp = strchr(hostname, '.')) != NULL &&
133 strcasecmp(tmp, mydomain) == 0 &&
134 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
135 tokval[tmp - hostname] == '\0')
137 if ((tmp
= strchr(host
, '.')) != NULL
&&
138 strcasecmp(tmp
, mydomain
) == 0 &&
139 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
140 tokval
[tmp
- host
] == '\0')
145 while ((t
= token()) && t
!= MACHINE
&& t
!= DEFAULT
) switch(t
) {
150 *aname
= malloc((unsigned) strlen(tokval
) + 1);
151 (void) strcpy(*aname
, tokval
);
153 if (strcmp(*aname
, tokval
))
158 if (strcmp(*aname
, "anonymous") &&
159 fstat(fileno(cfile
), &stb
) >= 0 &&
160 (stb
.st_mode
& 077) != 0) {
161 warnx(_("Error: .netrc file is readable by others."));
162 warnx(_("Remove password or make file unreadable by others."));
165 if (token() && *apass
== 0) {
166 *apass
= malloc((unsigned) strlen(tokval
) + 1);
167 (void) strcpy(*apass
, tokval
);
172 if (fstat(fileno(cfile
), &stb
) >= 0
173 && (stb
.st_mode
& 077) != 0) {
174 warnx("Error: .netrc file is readable by others.");
175 warnx("Remove account or make file unreadable by others.");
178 if (token() && *aacct
== 0) {
179 *aacct
= malloc((unsigned) strlen(tokval
) + 1);
180 (void) strcpy(*aacct
, tokval
);
187 (void) fclose(cfile
);
190 while ((c
=getc(cfile
)) != EOF
&& c
== ' ' || c
== '\t');
191 if (c
== EOF
|| c
== '\n') {
192 printf("Missing macdef name argument.\n");
196 printf("Limit of 16 macros have already been defined\n");
199 tmp
= macros
[macnum
].mac_name
;
201 for (i
=0; i
< 8 && (c
=getc(cfile
)) != EOF
&&
206 printf("Macro definition missing null line terminator.\n");
211 while ((c
=getc(cfile
)) != EOF
&& c
!= '\n');
214 printf("Macro definition missing null line terminator.\n");
218 macros
[macnum
].mac_start
= macbuf
;
221 macros
[macnum
].mac_start
= macros
[macnum
-1].mac_end
+ 1;
223 tmp
= macros
[macnum
].mac_start
;
224 while (tmp
!= macbuf
+ 4096) {
225 if ((c
=getc(cfile
)) == EOF
) {
226 printf("Macro definition missing null line terminator.\n");
231 if (*(tmp
-1) == '\0') {
232 macros
[macnum
++].mac_end
= tmp
- 1;
239 if (tmp
== macbuf
+ 4096) {
240 printf("4K macro buffer exceeded\n");
246 warnx(_("Unknown .netrc keyword %s"), tokval
);
252 (void) fclose(cfile
);
255 (void) fclose(cfile
);
266 if (feof(cfile
) || ferror(cfile
))
268 while ((c
= getc(cfile
)) != EOF
&&
269 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
275 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
282 while ((c
= getc(cfile
)) != EOF
283 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
292 for (t
= toktab
; t
->tokstr
; t
++)
293 if (!strcmp(t
->tokstr
, tokval
))