Refactor bug-strtod.c to better test new types.
[glibc.git] / inet / ruserpass.c
blobdafc33036a5870821f953a4b688b98d494522d80
1 /*
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
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.
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.
30 #include <sys/types.h>
31 #include <sys/stat.h>
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <stdio.h>
38 #include <stdio_ext.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <libintl.h>
44 /* #include "ftp_var.h" */
46 static int token (void);
47 static FILE *cfile;
49 #define DEFAULT 1
50 #define LOGIN 2
51 #define PASSWD 3
52 #define ACCOUNT 4
53 #define MACDEF 5
54 #define ID 10
55 #define MACHINE 11
57 static char tokval[100];
59 static const char tokstr[] =
61 #define TOK_DEFAULT_IDX 0
62 "default\0"
63 #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
64 "login\0"
65 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
66 "password\0"
67 #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
68 "passwd\0"
69 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
70 "account\0"
71 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
72 "machine\0"
73 #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
74 "macdef"
77 static const struct toktab {
78 int tokstr_off;
79 int tval;
80 } toktab[]= {
81 { TOK_DEFAULT_IDX, DEFAULT },
82 { TOK_LOGIN_IDX, LOGIN },
83 { TOK_PASSWORD_IDX, PASSWD },
84 { TOK_PASSWD_IDX, PASSWD },
85 { TOK_ACCOUNT_IDX, ACCOUNT },
86 { TOK_MACHINE_IDX, MACHINE },
87 { TOK_MACDEF_IDX, MACDEF }
92 int
93 ruserpass (const char *host, const char **aname, const char **apass)
95 char *hdir, *buf, *tmp;
96 char myname[1024], *mydomain;
97 int t, usedefault = 0;
98 struct stat64 stb;
100 hdir = __libc_secure_getenv("HOME");
101 if (hdir == NULL) {
102 /* If we can't get HOME, fail instead of trying ".",
103 which is no improvement. This really should call
104 getpwuid(getuid()). */
105 /*hdir = ".";*/
106 return -1;
109 buf = alloca (strlen (hdir) + 8);
111 __stpcpy (__stpcpy (buf, hdir), "/.netrc");
112 cfile = fopen(buf, "rce");
113 if (cfile == NULL) {
114 if (errno != ENOENT)
115 warn("%s", buf);
116 return (0);
118 /* No threads use this stream. */
119 __fsetlocking (cfile, FSETLOCKING_BYCALLER);
120 if (__gethostname(myname, sizeof(myname)) < 0)
121 myname[0] = '\0';
122 mydomain = __strchrnul(myname, '.');
123 next:
124 while ((t = token())) switch(t) {
126 case DEFAULT:
127 usedefault = 1;
128 /* FALL THROUGH */
130 case MACHINE:
131 if (!usedefault) {
132 if (token() != ID)
133 continue;
135 * Allow match either for user's input host name
136 * or official hostname. Also allow match of
137 * incompletely-specified host in local domain.
139 if (__strcasecmp(host, tokval) == 0)
140 goto match;
141 /* if (__strcasecmp(hostname, tokval) == 0)
142 goto match;
143 if ((tmp = strchr(hostname, '.')) != NULL &&
144 __strcasecmp(tmp, mydomain) == 0 &&
145 __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
146 tokval[tmp - hostname] == '\0')
147 goto match; */
148 if ((tmp = strchr(host, '.')) != NULL &&
149 __strcasecmp(tmp, mydomain) == 0 &&
150 __strncasecmp(host, tokval, tmp - host) == 0 &&
151 tokval[tmp - host] == '\0')
152 goto match;
153 continue;
155 match:
156 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
158 case LOGIN:
159 if (token()) {
160 if (*aname == 0) {
161 char *newp;
162 newp = malloc((unsigned) strlen(tokval) + 1);
163 if (newp == NULL)
165 warnx(_("out of memory"));
166 goto bad;
168 *aname = strcpy(newp, tokval);
169 } else {
170 if (strcmp(*aname, tokval))
171 goto next;
174 break;
175 case PASSWD:
176 if (strcmp(*aname, "anonymous") &&
177 fstat64(fileno(cfile), &stb) >= 0 &&
178 (stb.st_mode & 077) != 0) {
179 warnx(_("Error: .netrc file is readable by others."));
180 warnx(_("Remove password or make file unreadable by others."));
181 goto bad;
183 if (token() && *apass == 0) {
184 char *newp;
185 newp = malloc((unsigned) strlen(tokval) + 1);
186 if (newp == NULL)
188 warnx(_("out of memory"));
189 goto bad;
191 *apass = strcpy(newp, tokval);
193 break;
194 case ACCOUNT:
195 #if 0
196 if (fstat64(fileno(cfile), &stb) >= 0
197 && (stb.st_mode & 077) != 0) {
198 warnx("Error: .netrc file is readable by others.");
199 warnx("Remove account or make file unreadable by others.");
200 goto bad;
202 if (token() && *aacct == 0) {
203 *aacct = malloc((unsigned) strlen(tokval) + 1);
204 (void) strcpy(*aacct, tokval);
206 #endif
207 break;
208 case MACDEF:
209 #if 0
210 if (proxy) {
211 (void) fclose(cfile);
212 return (0);
214 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
215 || c == '\t');
216 if (c == EOF || c == '\n') {
217 printf("Missing macdef name argument.\n");
218 goto bad;
220 if (macnum == 16) {
221 printf("Limit of 16 macros have already been defined\n");
222 goto bad;
224 tmp = macros[macnum].mac_name;
225 *tmp++ = c;
226 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
227 !isspace(c); ++i) {
228 *tmp++ = c;
230 if (c == EOF) {
231 printf("Macro definition missing null line terminator.\n");
232 goto bad;
234 *tmp = '\0';
235 if (c != '\n') {
236 while ((c=getc_unlocked(cfile)) != EOF
237 && c != '\n');
239 if (c == EOF) {
240 printf("Macro definition missing null line terminator.\n");
241 goto bad;
243 if (macnum == 0) {
244 macros[macnum].mac_start = macbuf;
246 else {
247 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
249 tmp = macros[macnum].mac_start;
250 while (tmp != macbuf + 4096) {
251 if ((c=getc_unlocked(cfile)) == EOF) {
252 printf("Macro definition missing null line terminator.\n");
253 goto bad;
255 *tmp = c;
256 if (*tmp == '\n') {
257 if (*(tmp-1) == '\0') {
258 macros[macnum++].mac_end = tmp - 1;
259 break;
261 *tmp = '\0';
263 tmp++;
265 if (tmp == macbuf + 4096) {
266 printf("4K macro buffer exceeded\n");
267 goto bad;
269 #endif
270 break;
271 default:
272 warnx(_("Unknown .netrc keyword %s"), tokval);
273 break;
275 goto done;
277 done:
278 (void) fclose(cfile);
279 return (0);
280 bad:
281 (void) fclose(cfile);
282 return (-1);
284 libc_hidden_def (ruserpass)
286 static int
287 token (void)
289 char *cp;
290 int c;
291 int i;
293 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
294 return (0);
295 while ((c = getc_unlocked(cfile)) != EOF &&
296 (c == '\n' || c == '\t' || c == ' ' || c == ','))
297 continue;
298 if (c == EOF)
299 return (0);
300 cp = tokval;
301 if (c == '"') {
302 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
303 if (c == '\\')
304 c = getc_unlocked(cfile);
305 *cp++ = c;
307 } else {
308 *cp++ = c;
309 while ((c = getc_unlocked(cfile)) != EOF
310 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
311 if (c == '\\')
312 c = getc_unlocked(cfile);
313 *cp++ = c;
316 *cp = 0;
317 if (tokval[0] == 0)
318 return (0);
319 for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
320 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
321 return toktab[i].tval;
322 return (ID);