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 /* #include "ftp_var.h" */
49 static int token (void);
60 static char tokval[100];
62 static const char tokstr[] =
64 #define TOK_DEFAULT_IDX 0
66 #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
68 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
70 #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
72 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
74 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
76 #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
80 static const struct toktab {
84 { TOK_DEFAULT_IDX, DEFAULT },
85 { TOK_LOGIN_IDX, LOGIN },
86 { TOK_PASSWORD_IDX, PASSWD },
87 { TOK_PASSWD_IDX, PASSWD },
88 { TOK_ACCOUNT_IDX, ACCOUNT },
89 { TOK_MACHINE_IDX, MACHINE },
90 { TOK_MACDEF_IDX, MACDEF }
94 /* ruserpass - remote password check.
95 This function also exists in glibc but is undocumented */
96 int ruserpass(const char *host, const char **aname, const char **apass)
98 char *hdir, *buf, *tmp;
99 char myname[1024], *mydomain;
100 int t, usedefault = 0;
103 /* Give up when running a setuid or setgid app. */
104 if ((getuid() != geteuid()) || getgid() != getegid())
106 hdir = getenv("HOME");
108 /* If we can't get HOME, fail instead of trying ".",
109 which is no improvement. */
113 buf = alloca (strlen(hdir) + 8);
115 strcat(buf, "/.netrc");
116 cfile = fopen(buf, "r");
122 /* No threads use this stream. */
123 #ifdef __UCLIBC_HAS_THREADS__
124 __fsetlocking (cfile, FSETLOCKING_BYCALLER);
126 if (gethostname(myname, sizeof(myname)) < 0)
128 mydomain = strchr(myname, '.');
129 if (mydomain==NULL) {
130 mydomain=myname + strlen(myname);
133 while ((t = token())) switch(t) {
144 * Allow match either for user's input host name
145 * or official hostname. Also allow match of
146 * incompletely-specified host in local domain.
148 if (strcasecmp(host, tokval) == 0)
150 if ((tmp = strchr(host, '.')) != NULL &&
151 strcasecmp(tmp, mydomain) == 0 &&
152 strncasecmp(host, tokval, tmp - host) == 0 &&
153 tokval[tmp - host] == '\0')
158 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
164 newp = malloc((unsigned) strlen(tokval) + 1);
167 printf(_("out of memory"));
170 *aname = strcpy(newp, tokval);
172 if (strcmp(*aname, tokval))
178 if (strcmp(*aname, "anonymous") &&
179 fstat(fileno(cfile), &stb) >= 0 &&
180 (stb.st_mode & 077) != 0) {
181 printf(_("Error: .netrc file is readable by others."));
182 printf(_("Remove password or make file unreadable by others."));
185 if (token() && *apass == 0) {
187 newp = malloc((unsigned) strlen(tokval) + 1);
190 printf(_("out of memory"));
193 *apass = strcpy(newp, tokval);
198 if (fstat(fileno(cfile), &stb) >= 0
199 && (stb.st_mode & 077) != 0) {
200 printf("Error: .netrc file is readable by others.");
201 printf("Remove account or make file unreadable by others.");
204 if (token() && *aacct == 0) {
205 *aacct = malloc((unsigned) strlen(tokval) + 1);
206 (void) strcpy(*aacct, tokval);
213 (void) fclose(cfile);
216 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
218 if (c == EOF || c == '\n') {
219 printf("Missing macdef name argument.\n");
223 printf("Limit of 16 macros have already been defined\n");
226 tmp = macros[macnum].mac_name;
228 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
233 printf("Macro definition missing null line terminator.\n");
238 while ((c=getc_unlocked(cfile)) != EOF
242 printf("Macro definition missing null line terminator.\n");
246 macros[macnum].mac_start = macbuf;
249 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
251 tmp = macros[macnum].mac_start;
252 while (tmp != macbuf + 4096) {
253 if ((c=getc_unlocked(cfile)) == EOF) {
254 printf("Macro definition missing null line terminator.\n");
259 if (*(tmp-1) == '\0') {
260 macros[macnum++].mac_end = tmp - 1;
267 if (tmp == macbuf + 4096) {
268 printf("4K macro buffer exceeded\n");
274 printf(_("Unknown .netrc keyword %s"), tokval);
280 (void) fclose(cfile);
283 (void) fclose(cfile);
286 libc_hidden_def(ruserpass)
295 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
297 while ((c = getc_unlocked(cfile)) != EOF &&
298 (c == '\n' || c == '\t' || c == ' ' || c == ','))
304 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
306 c = getc_unlocked(cfile);
311 while ((c = getc_unlocked(cfile)) != EOF
312 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
314 c = getc_unlocked(cfile);
321 for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
322 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
323 return toktab[i].tval;