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>
49 /* #include "ftp_var.h" */
51 static int token
__P((void));
62 static char tokval
[100];
64 static struct toktab
{
68 { "default", DEFAULT
},
70 { "password", PASSWD
},
72 { "account", ACCOUNT
},
73 { "machine", MACHINE
},
81 ruserpass(host
, aname
, apass
)
82 char *host
, **aname
, **apass
;
84 char *hdir
, buf
[BUFSIZ
], *tmp
;
85 char myname
[1024], *mydomain
;
86 int t
, i
, c
, usedefault
= 0;
89 hdir
= getenv("HOME");
92 (void) sprintf(buf
, "%s/.netrc", hdir
);
93 cfile
= fopen(buf
, "r");
95 /* if (errno != ENOENT)
99 if (gethostname(myname
, sizeof(myname
)) < 0)
101 if ((mydomain
= strchr(myname
, '.')) == NULL
)
104 while ((t
= token())) switch(t
) {
115 * Allow match either for user's input host name
116 * or official hostname. Also allow match of
117 * incompletely-specified host in local domain.
119 if (strcasecmp(host
, tokval
) == 0)
121 /* if (strcasecmp(hostname, tokval) == 0)
123 if ((tmp = strchr(hostname, '.')) != NULL &&
124 strcasecmp(tmp, mydomain) == 0 &&
125 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
126 tokval[tmp - hostname] == '\0')
128 if ((tmp
= strchr(host
, '.')) != NULL
&&
129 strcasecmp(tmp
, mydomain
) == 0 &&
130 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
131 tokval
[tmp
- host
] == '\0')
136 while ((t
= token()) && t
!= MACHINE
&& t
!= DEFAULT
) switch(t
) {
141 *aname
= malloc((unsigned) strlen(tokval
) + 1);
142 (void) strcpy(*aname
, tokval
);
144 if (strcmp(*aname
, tokval
))
149 if (strcmp(*aname
, "anonymous") &&
150 fstat(fileno(cfile
), &stb
) >= 0 &&
151 (stb
.st_mode
& 077) != 0) {
152 /* warnx("Error: .netrc file is readable by others.");
153 warnx("Remove password or make file unreadable by others.");*/
156 if (token() && *apass
== 0) {
157 *apass
= malloc((unsigned) strlen(tokval
) + 1);
158 (void) strcpy(*apass
, tokval
);
163 if (fstat(fileno(cfile
), &stb
) >= 0
164 && (stb
.st_mode
& 077) != 0) {
165 warnx("Error: .netrc file is readable by others.");
166 warnx("Remove account or make file unreadable by others.");
169 if (token() && *aacct
== 0) {
170 *aacct
= malloc((unsigned) strlen(tokval
) + 1);
171 (void) strcpy(*aacct
, tokval
);
178 (void) fclose(cfile
);
181 while ((c
=getc(cfile
)) != EOF
&& c
== ' ' || c
== '\t');
182 if (c
== EOF
|| c
== '\n') {
183 printf("Missing macdef name argument.\n");
187 printf("Limit of 16 macros have already been defined\n");
190 tmp
= macros
[macnum
].mac_name
;
192 for (i
=0; i
< 8 && (c
=getc(cfile
)) != EOF
&&
197 printf("Macro definition missing null line terminator.\n");
202 while ((c
=getc(cfile
)) != EOF
&& c
!= '\n');
205 printf("Macro definition missing null line terminator.\n");
209 macros
[macnum
].mac_start
= macbuf
;
212 macros
[macnum
].mac_start
= macros
[macnum
-1].mac_end
+ 1;
214 tmp
= macros
[macnum
].mac_start
;
215 while (tmp
!= macbuf
+ 4096) {
216 if ((c
=getc(cfile
)) == EOF
) {
217 printf("Macro definition missing null line terminator.\n");
222 if (*(tmp
-1) == '\0') {
223 macros
[macnum
++].mac_end
= tmp
- 1;
230 if (tmp
== macbuf
+ 4096) {
231 printf("4K macro buffer exceeded\n");
237 /* warnx("Unknown .netrc keyword %s", tokval);*/
243 (void) fclose(cfile
);
246 (void) fclose(cfile
);
257 if (feof(cfile
) || ferror(cfile
))
259 while ((c
= getc(cfile
)) != EOF
&&
260 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
266 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
273 while ((c
= getc(cfile
)) != EOF
274 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
283 for (t
= toktab
; t
->tokstr
; t
++)
284 if (!strcmp(t
->tokstr
, tokval
))