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 * 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
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid
[] = "@(#)ruserpass.c 8.3 (Berkeley) 4/2/94";
34 #include <sys/types.h>
46 /* #include "ftp_var.h" */
48 static int token
__P((void));
59 static char tokval
[100];
61 static const char tokstr
[] =
63 #define TOK_DEFAULT_IDX 0
65 #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
67 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
69 #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
71 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
73 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
75 #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
79 static const struct toktab
{
83 { TOK_DEFAULT_IDX
, DEFAULT
},
84 { TOK_LOGIN_IDX
, LOGIN
},
85 { TOK_PASSWORD_IDX
, PASSWD
},
86 { TOK_PASSWD_IDX
, PASSWD
},
87 { TOK_ACCOUNT_IDX
, ACCOUNT
},
88 { TOK_MACHINE_IDX
, MACHINE
},
89 { TOK_MACDEF_IDX
, MACDEF
}
95 ruserpass(host
, aname
, apass
)
96 char *host
, **aname
, **apass
;
98 char *hdir
, *buf
, *tmp
;
99 char myname
[1024], *mydomain
;
100 int t
, i
, c
, usedefault
= 0;
103 hdir
= __secure_getenv("HOME");
105 /* If we can't get HOME, fail instead of trying ".",
106 which is no improvement. This really should call
107 getpwuid(getuid()). */
112 buf
= alloca (strlen (hdir
) + 8);
114 __stpcpy (__stpcpy (buf
, hdir
), "/.netrc");
115 cfile
= fopen(buf
, "r");
121 if (__gethostname(myname
, sizeof(myname
)) < 0)
123 mydomain
= __strchrnul(myname
, '.');
125 while ((t
= token())) switch(t
) {
136 * Allow match either for user's input host name
137 * or official hostname. Also allow match of
138 * incompletely-specified host in local domain.
140 if (__strcasecmp(host
, tokval
) == 0)
142 /* if (__strcasecmp(hostname, tokval) == 0)
144 if ((tmp = strchr(hostname, '.')) != NULL &&
145 __strcasecmp(tmp, mydomain) == 0 &&
146 __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
147 tokval[tmp - hostname] == '\0')
149 if ((tmp
= strchr(host
, '.')) != NULL
&&
150 __strcasecmp(tmp
, mydomain
) == 0 &&
151 __strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
152 tokval
[tmp
- host
] == '\0')
157 while ((t
= token()) && t
!= MACHINE
&& t
!= DEFAULT
) switch(t
) {
162 *aname
= malloc((unsigned) strlen(tokval
) + 1);
163 (void) strcpy(*aname
, tokval
);
165 if (strcmp(*aname
, tokval
))
170 if (strcmp(*aname
, "anonymous") &&
171 fstat(fileno(cfile
), &stb
) >= 0 &&
172 (stb
.st_mode
& 077) != 0) {
173 warnx(_("Error: .netrc file is readable by others."));
174 warnx(_("Remove password or make file unreadable by others."));
177 if (token() && *apass
== 0) {
178 *apass
= malloc((unsigned) strlen(tokval
) + 1);
179 (void) strcpy(*apass
, tokval
);
184 if (fstat(fileno(cfile
), &stb
) >= 0
185 && (stb
.st_mode
& 077) != 0) {
186 warnx("Error: .netrc file is readable by others.");
187 warnx("Remove account or make file unreadable by others.");
190 if (token() && *aacct
== 0) {
191 *aacct
= malloc((unsigned) strlen(tokval
) + 1);
192 (void) strcpy(*aacct
, tokval
);
199 (void) fclose(cfile
);
202 while ((c
=getc_unlocked(cfile
)) != EOF
&& c
== ' '
204 if (c
== EOF
|| c
== '\n') {
205 printf("Missing macdef name argument.\n");
209 printf("Limit of 16 macros have already been defined\n");
212 tmp
= macros
[macnum
].mac_name
;
214 for (i
=0; i
< 8 && (c
=getc_unlocked(cfile
)) != EOF
&&
219 printf("Macro definition missing null line terminator.\n");
224 while ((c
=getc_unlocked(cfile
)) != EOF
228 printf("Macro definition missing null line terminator.\n");
232 macros
[macnum
].mac_start
= macbuf
;
235 macros
[macnum
].mac_start
= macros
[macnum
-1].mac_end
+ 1;
237 tmp
= macros
[macnum
].mac_start
;
238 while (tmp
!= macbuf
+ 4096) {
239 if ((c
=getc_unlocked(cfile
)) == EOF
) {
240 printf("Macro definition missing null line terminator.\n");
245 if (*(tmp
-1) == '\0') {
246 macros
[macnum
++].mac_end
= tmp
- 1;
253 if (tmp
== macbuf
+ 4096) {
254 printf("4K macro buffer exceeded\n");
260 warnx(_("Unknown .netrc keyword %s"), tokval
);
266 (void) fclose(cfile
);
269 (void) fclose(cfile
);
280 if (feof_unlocked(cfile
) || ferror_unlocked(cfile
))
282 while ((c
= getc_unlocked(cfile
)) != EOF
&&
283 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
289 while ((c
= getc_unlocked(cfile
)) != EOF
&& c
!= '"') {
291 c
= getc_unlocked(cfile
);
296 while ((c
= getc_unlocked(cfile
)) != EOF
297 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
299 c
= getc_unlocked(cfile
);
306 for (i
= 0; i
< sizeof (toktab
) / sizeof (toktab
[0]); ++i
)
307 if (!strcmp(&tokstr
[toktab
[i
].tokstr_off
], tokval
))
308 return toktab
[i
].tval
;