1 /*
2 * Copyright (c) 1985, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <stdio.h>
38 #ifdef __UCLIBC_HAS_THREADS__
39 # include <stdio_ext.h>
40 #endif
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <libintl.h>
45
46 /* #include "ftp_var.h" */
47
48 static int token (void);
49 static FILE *cfile;
50
51 #define DEFAULT 1
52 #define LOGIN 2
53 #define PASSWD 3
54 #define ACCOUNT 4
55 #define MACDEF 5
56 #define ID 10
57 #define MACHINE 11
58
59 static char tokval[100];
60
61 static const char tokstr[] =
62 {
63 #define TOK_DEFAULT_IDX 0
64 "default\0"
65 #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
66 "login\0"
67 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
68 "password\0"
69 #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
70 "passwd\0"
71 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
72 "account\0"
73 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
74 "machine\0"
75 #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
76 "macdef"
77 };
78
79 static const struct toktab {
80 int tokstr_off;
81 int tval;
82 } 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 }
90 };
91
92
93 /* ruserpass - remote password check.
94 This function also exists in glibc but is undocumented */
ruserpass(const char * host,const char ** aname,const char ** apass)95 int ruserpass(const char *host, const char **aname, const char **apass)
96 {
97 char *hdir, *buf, *tmp;
98 char myname[1024], *mydomain;
99 int t, usedefault = 0;
100 struct stat stb;
101
102 /* Give up when running a setuid or setgid app. */
103 if ((getuid() != geteuid()) || getgid() != getegid())
104 return -1;
105 hdir = getenv("HOME");
106 if (hdir == NULL) {
107 /* If we can't get HOME, fail instead of trying ".",
108 which is no improvement. */
109 return -1;
110 }
111
112 buf = alloca (strlen(hdir) + 8);
113 strcpy(buf, hdir);
114 strcat(buf, "/.netrc");
115 cfile = fopen(buf, "r");
116 if (cfile == NULL) {
117 if (errno != ENOENT)
118 printf("%s", buf);
119 return (0);
120 }
121 /* No threads use this stream. */
122 #ifdef __UCLIBC_HAS_THREADS__
123 __fsetlocking (cfile, FSETLOCKING_BYCALLER);
124 #endif
125 if (gethostname(myname, sizeof(myname)) < 0)
126 myname[0] = '\0';
127 mydomain = strchr(myname, '.');
128 if (mydomain==NULL) {
129 mydomain=myname + strlen(myname);
130 }
131 next:
132 while ((t = token())) switch(t) {
133
134 case DEFAULT:
135 usedefault = 1;
136 /* FALL THROUGH */
137
138 case MACHINE:
139 if (!usedefault) {
140 if (token() != ID)
141 continue;
142 /*
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.
146 */
147 if (strcasecmp(host, tokval) == 0)
148 goto match;
149 if ((tmp = strchr(host, '.')) != NULL &&
150 strcasecmp(tmp, mydomain) == 0 &&
151 strncasecmp(host, tokval, tmp - host) == 0 &&
152 tokval[tmp - host] == '\0')
153 goto match;
154 continue;
155 }
156 match:
157 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
158
159 case LOGIN:
160 if (token()) {
161 if (*aname == 0) {
162 char *newp;
163 newp = malloc((unsigned) strlen(tokval) + 1);
164 if (newp == NULL)
165 {
166 printf(_("out of memory"));
167 goto bad;
168 }
169 *aname = strcpy(newp, tokval);
170 } else {
171 if (strcmp(*aname, tokval))
172 goto next;
173 }
174 }
175 break;
176 case PASSWD:
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."));
182 goto bad;
183 }
184 if (token() && *apass == 0) {
185 char *newp;
186 newp = malloc((unsigned) strlen(tokval) + 1);
187 if (newp == NULL)
188 {
189 printf(_("out of memory"));
190 goto bad;
191 }
192 *apass = strcpy(newp, tokval);
193 }
194 break;
195 case ACCOUNT:
196 #if 0
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.");
201 goto bad;
202 }
203 if (token() && *aacct == 0) {
204 *aacct = malloc((unsigned) strlen(tokval) + 1);
205 (void) strcpy(*aacct, tokval);
206 }
207 #endif
208 break;
209 case MACDEF:
210 #if 0
211 if (proxy) {
212 (void) fclose(cfile);
213 return (0);
214 }
215 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
216 || c == '\t');
217 if (c == EOF || c == '\n') {
218 printf("Missing macdef name argument.\n");
219 goto bad;
220 }
221 if (macnum == 16) {
222 printf("Limit of 16 macros have already been defined\n");
223 goto bad;
224 }
225 tmp = macros[macnum].mac_name;
226 *tmp++ = c;
227 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
228 !isspace(c); ++i) {
229 *tmp++ = c;
230 }
231 if (c == EOF) {
232 printf("Macro definition missing null line terminator.\n");
233 goto bad;
234 }
235 *tmp = '\0';
236 if (c != '\n') {
237 while ((c=getc_unlocked(cfile)) != EOF
238 && c != '\n');
239 }
240 if (c == EOF) {
241 printf("Macro definition missing null line terminator.\n");
242 goto bad;
243 }
244 if (macnum == 0) {
245 macros[macnum].mac_start = macbuf;
246 }
247 else {
248 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
249 }
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");
254 goto bad;
255 }
256 *tmp = c;
257 if (*tmp == '\n') {
258 if (*(tmp-1) == '\0') {
259 macros[macnum++].mac_end = tmp - 1;
260 break;
261 }
262 *tmp = '\0';
263 }
264 tmp++;
265 }
266 if (tmp == macbuf + 4096) {
267 printf("4K macro buffer exceeded\n");
268 goto bad;
269 }
270 #endif
271 break;
272 default:
273 printf(_("Unknown .netrc keyword %s"), tokval);
274 break;
275 }
276 goto done;
277 }
278 done:
279 (void) fclose(cfile);
280 return (0);
281 bad:
282 (void) fclose(cfile);
283 return (-1);
284 }
libc_hidden_def(ruserpass)285 libc_hidden_def(ruserpass)
286
287 static int
288 token(void)
289 {
290 char *cp;
291 int c;
292 int i;
293
294 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
295 return (0);
296 while ((c = getc_unlocked(cfile)) != EOF &&
297 (c == '\n' || c == '\t' || c == ' ' || c == ','))
298 continue;
299 if (c == EOF)
300 return (0);
301 cp = tokval;
302 if (c == '"') {
303 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
304 if (c == '\\')
305 c = getc_unlocked(cfile);
306 *cp++ = c;
307 }
308 } else {
309 *cp++ = c;
310 while ((c = getc_unlocked(cfile)) != EOF
311 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
312 if (c == '\\')
313 c = getc_unlocked(cfile);
314 *cp++ = c;
315 }
316 }
317 *cp = 0;
318 if (tokval[0] == 0)
319 return (0);
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;
323 return (ID);
324 }
325