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 #include <sys/types.h>
38 #ifdef __UCLIBC_HAS_THREADS__
39 # include <stdio_ext.h>
46 /* #include "ftp_var.h" */
48 static int token (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 }
93 /* ruserpass - remote password check.
94 This function also exists in glibc but is undocumented */
95 int ruserpass(const char *host, const char **aname, const char **apass)
97 char *hdir, *buf, *tmp;
98 char myname[1024], *mydomain;
99 int t, usedefault = 0;
102 /* Give up when running a setuid or setgid app. */
103 if ((getuid() != geteuid()) || getgid() != getegid())
105 hdir = getenv("HOME");
107 /* If we can't get HOME, fail instead of trying ".",
108 which is no improvement. */
112 buf = alloca (strlen(hdir) + 8);
114 strcat(buf, "/.netrc");
115 cfile = fopen(buf, "r");
121 /* No threads use this stream. */
122 #ifdef __UCLIBC_HAS_THREADS__
123 __fsetlocking (cfile, FSETLOCKING_BYCALLER);
125 if (gethostname(myname, sizeof(myname)) < 0)
127 mydomain = strchr(myname, '.');
128 if (mydomain==NULL) {
129 mydomain=myname + strlen(myname);
132 while ((t = token())) switch(t) {
143 * Allow match either for user's input host name
144 * or official hostname. Also allow match of
145 * incompletely-specified host in local domain.
147 if (strcasecmp(host, tokval) == 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) {
163 newp = malloc((unsigned) strlen(tokval) + 1);
166 printf(_("out of memory"));
169 *aname = strcpy(newp, tokval);
171 if (strcmp(*aname, tokval))
177 if (strcmp(*aname, "anonymous") &&
178 fstat(fileno(cfile), &stb) >= 0 &&
179 (stb.st_mode & 077) != 0) {
180 printf(_("Error: .netrc file is readable by others."));
181 printf(_("Remove password or make file unreadable by others."));
184 if (token() && *apass == 0) {
186 newp = malloc((unsigned) strlen(tokval) + 1);
189 printf(_("out of memory"));
192 *apass = strcpy(newp, tokval);
197 if (fstat(fileno(cfile), &stb) >= 0
198 && (stb.st_mode & 077) != 0) {
199 printf("Error: .netrc file is readable by others.");
200 printf("Remove account or make file unreadable by others.");
203 if (token() && *aacct == 0) {
204 *aacct = malloc((unsigned) strlen(tokval) + 1);
205 (void) strcpy(*aacct, tokval);
212 (void) fclose(cfile);
215 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
217 if (c == EOF || c == '\n') {
218 printf("Missing macdef name argument.\n");
222 printf("Limit of 16 macros have already been defined\n");
225 tmp = macros[macnum].mac_name;
227 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
232 printf("Macro definition missing null line terminator.\n");
237 while ((c=getc_unlocked(cfile)) != EOF
241 printf("Macro definition missing null line terminator.\n");
245 macros[macnum].mac_start = macbuf;
248 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
250 tmp = macros[macnum].mac_start;
251 while (tmp != macbuf + 4096) {
252 if ((c=getc_unlocked(cfile)) == EOF) {
253 printf("Macro definition missing null line terminator.\n");
258 if (*(tmp-1) == '\0') {
259 macros[macnum++].mac_end = tmp - 1;
266 if (tmp == macbuf + 4096) {
267 printf("4K macro buffer exceeded\n");
273 printf(_("Unknown .netrc keyword %s"), tokval);
279 (void) fclose(cfile);
282 (void) fclose(cfile);
285 libc_hidden_def(ruserpass)
294 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
296 while ((c = getc_unlocked(cfile)) != EOF &&
297 (c == '\n' || c == '\t' || c == ' ' || c == ','))
303 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
305 c = getc_unlocked(cfile);
310 while ((c = getc_unlocked(cfile)) != EOF
311 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
313 c = getc_unlocked(cfile);
320 for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
321 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
322 return toktab[i].tval;