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
32 #include <sys/types.h>
40 #include <stdio_ext.h>
47 /* Experimentally off - libc_hidden_proto(strcat) */
48 /* Experimentally off - libc_hidden_proto(strchr) */
49 /* Experimentally off - libc_hidden_proto(strcmp) */
50 /* Experimentally off - libc_hidden_proto(strcpy) */
51 /* Experimentally off - libc_hidden_proto(strlen) */
52 /* Experimentally off - libc_hidden_proto(strcasecmp) */
53 /* Experimentally off - libc_hidden_proto(strncasecmp) */
54 libc_hidden_proto(getenv)
55 libc_hidden_proto(printf)
56 libc_hidden_proto(fstat)
57 libc_hidden_proto(__fsetlocking)
58 libc_hidden_proto(getgid)
59 libc_hidden_proto(getuid)
60 libc_hidden_proto(getegid)
61 libc_hidden_proto(geteuid)
62 libc_hidden_proto(gethostname)
63 libc_hidden_proto(fileno)
64 libc_hidden_proto(fopen)
65 libc_hidden_proto(fclose)
66 libc_hidden_proto(getc_unlocked)
67 libc_hidden_proto(__fgetc_unlocked)
68 libc_hidden_proto(__uc_malloc)
71 /* #include "ftp_var.h" */
73 static int token (void);
84 static char *tokval; /* [100] */
86 static const char tokstr[] =
88 #define TOK_DEFAULT_IDX 0
90 #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
92 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
94 #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
96 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
98 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
100 #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
104 static const struct toktab {
108 { TOK_DEFAULT_IDX, DEFAULT },
109 { TOK_LOGIN_IDX, LOGIN },
110 { TOK_PASSWORD_IDX, PASSWD },
111 { TOK_PASSWD_IDX, PASSWD },
112 { TOK_ACCOUNT_IDX, ACCOUNT },
113 { TOK_MACHINE_IDX, MACHINE },
114 { TOK_MACDEF_IDX, MACDEF }
118 /* ruserpass - remote password check.
119 This function also exists in glibc but is undocumented */
120 libc_hidden_proto(ruserpass)
121 int ruserpass(const char *host, const char **aname, const char **apass)
123 char *hdir, *buf, *tmp;
124 char myname[1024], *mydomain;
125 int t, usedefault = 0;
128 /* Give up when running a setuid or setgid app. */
129 if ((getuid() != geteuid()) || getgid() != getegid())
131 hdir = getenv("HOME");
133 /* If we can't get HOME, fail instead of trying ".",
134 which is no improvement. */
138 buf = alloca (strlen(hdir) + 8);
140 strcat(buf, "/.netrc");
141 cfile = fopen(buf, "r");
147 /* No threads use this stream. */
148 #ifdef __UCLIBC_HAS_THREADS__
149 __fsetlocking (cfile, FSETLOCKING_BYCALLER);
151 if (gethostname(myname, sizeof(myname)) < 0)
153 mydomain = strchr(myname, '.');
154 if (mydomain==NULL) {
155 mydomain=myname + strlen(myname);
159 tokval = __uc_malloc(100);
161 while ((t = token())) switch(t) {
172 * Allow match either for user's input host name
173 * or official hostname. Also allow match of
174 * incompletely-specified host in local domain.
176 if (strcasecmp(host, tokval) == 0)
178 if ((tmp = strchr(host, '.')) != NULL &&
179 strcasecmp(tmp, mydomain) == 0 &&
180 strncasecmp(host, tokval, tmp - host) == 0 &&
181 tokval[tmp - host] == '\0')
186 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
192 newp = malloc((unsigned) strlen(tokval) + 1);
195 printf(_("out of memory"));
198 *aname = strcpy(newp, tokval);
200 if (strcmp(*aname, tokval))
206 if (strcmp(*aname, "anonymous") &&
207 fstat(fileno(cfile), &stb) >= 0 &&
208 (stb.st_mode & 077) != 0) {
209 printf(_("Error: .netrc file is readable by others."));
210 printf(_("Remove password or make file unreadable by others."));
213 if (token() && *apass == 0) {
215 newp = malloc((unsigned) strlen(tokval) + 1);
218 printf(_("out of memory"));
221 *apass = strcpy(newp, tokval);
226 if (fstat(fileno(cfile), &stb) >= 0
227 && (stb.st_mode & 077) != 0) {
228 printf("Error: .netrc file is readable by others.");
229 printf("Remove account or make file unreadable by others.");
232 if (token() && *aacct == 0) {
233 *aacct = malloc((unsigned) strlen(tokval) + 1);
234 (void) strcpy(*aacct, tokval);
241 (void) fclose(cfile);
244 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
246 if (c == EOF || c == '\n') {
247 printf("Missing macdef name argument.\n");
251 printf("Limit of 16 macros have already been defined\n");
254 tmp = macros[macnum].mac_name;
256 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
261 printf("Macro definition missing null line terminator.\n");
266 while ((c=getc_unlocked(cfile)) != EOF
270 printf("Macro definition missing null line terminator.\n");
274 macros[macnum].mac_start = macbuf;
277 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
279 tmp = macros[macnum].mac_start;
280 while (tmp != macbuf + 4096) {
281 if ((c=getc_unlocked(cfile)) == EOF) {
282 printf("Macro definition missing null line terminator.\n");
287 if (*(tmp-1) == '\0') {
288 macros[macnum++].mac_end = tmp - 1;
295 if (tmp == macbuf + 4096) {
296 printf("4K macro buffer exceeded\n");
302 printf(_("Unknown .netrc keyword %s"), tokval);
308 (void) fclose(cfile);
311 (void) fclose(cfile);
314 libc_hidden_def(ruserpass)
323 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
325 while ((c = getc_unlocked(cfile)) != EOF &&
326 (c == '\n' || c == '\t' || c == ' ' || c == ','))
332 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
334 c = getc_unlocked(cfile);
339 while ((c = getc_unlocked(cfile)) != EOF
340 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
342 c = getc_unlocked(cfile);
349 for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
350 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
351 return toktab[i].tval;