Fix finite-math-only lgamma functions signgam setting (bug 19211).
[glibc.git] / inet / ruserpass.c
blobff379db43b72649c1d1161f4db1547c94ef43c42
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 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)ruserpass.c 8.3 (Berkeley) 4/2/94";
32 #endif /* not lint */
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdio_ext.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <libintl.h>
48 /* #include "ftp_var.h" */
50 static int token (void);
51 static FILE *cfile;
53 #define DEFAULT 1
54 #define LOGIN 2
55 #define PASSWD 3
56 #define ACCOUNT 4
57 #define MACDEF 5
58 #define ID 10
59 #define MACHINE 11
61 static char tokval[100];
63 static const char tokstr[] =
65 #define TOK_DEFAULT_IDX 0
66 "default\0"
67 #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
68 "login\0"
69 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
70 "password\0"
71 #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
72 "passwd\0"
73 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
74 "account\0"
75 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
76 "machine\0"
77 #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
78 "macdef"
81 static const struct toktab {
82 int tokstr_off;
83 int tval;
84 } toktab[]= {
85 { TOK_DEFAULT_IDX, DEFAULT },
86 { TOK_LOGIN_IDX, LOGIN },
87 { TOK_PASSWORD_IDX, PASSWD },
88 { TOK_PASSWD_IDX, PASSWD },
89 { TOK_ACCOUNT_IDX, ACCOUNT },
90 { TOK_MACHINE_IDX, MACHINE },
91 { TOK_MACDEF_IDX, MACDEF }
96 int
97 ruserpass (const char *host, const char **aname, const char **apass)
99 char *hdir, *buf, *tmp;
100 char myname[1024], *mydomain;
101 int t, usedefault = 0;
102 struct stat64 stb;
104 hdir = __libc_secure_getenv("HOME");
105 if (hdir == NULL) {
106 /* If we can't get HOME, fail instead of trying ".",
107 which is no improvement. This really should call
108 getpwuid(getuid()). */
109 /*hdir = ".";*/
110 return -1;
113 buf = alloca (strlen (hdir) + 8);
115 __stpcpy (__stpcpy (buf, hdir), "/.netrc");
116 cfile = fopen(buf, "rce");
117 if (cfile == NULL) {
118 if (errno != ENOENT)
119 warn("%s", buf);
120 return (0);
122 /* No threads use this stream. */
123 __fsetlocking (cfile, FSETLOCKING_BYCALLER);
124 if (__gethostname(myname, sizeof(myname)) < 0)
125 myname[0] = '\0';
126 mydomain = __strchrnul(myname, '.');
127 next:
128 while ((t = token())) switch(t) {
130 case DEFAULT:
131 usedefault = 1;
132 /* FALL THROUGH */
134 case MACHINE:
135 if (!usedefault) {
136 if (token() != ID)
137 continue;
139 * Allow match either for user's input host name
140 * or official hostname. Also allow match of
141 * incompletely-specified host in local domain.
143 if (__strcasecmp(host, tokval) == 0)
144 goto match;
145 /* if (__strcasecmp(hostname, tokval) == 0)
146 goto match;
147 if ((tmp = strchr(hostname, '.')) != NULL &&
148 __strcasecmp(tmp, mydomain) == 0 &&
149 __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
150 tokval[tmp - hostname] == '\0')
151 goto match; */
152 if ((tmp = strchr(host, '.')) != NULL &&
153 __strcasecmp(tmp, mydomain) == 0 &&
154 __strncasecmp(host, tokval, tmp - host) == 0 &&
155 tokval[tmp - host] == '\0')
156 goto match;
157 continue;
159 match:
160 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
162 case LOGIN:
163 if (token()) {
164 if (*aname == 0) {
165 char *newp;
166 newp = malloc((unsigned) strlen(tokval) + 1);
167 if (newp == NULL)
169 warnx(_("out of memory"));
170 goto bad;
172 *aname = strcpy(newp, tokval);
173 } else {
174 if (strcmp(*aname, tokval))
175 goto next;
178 break;
179 case PASSWD:
180 if (strcmp(*aname, "anonymous") &&
181 fstat64(fileno(cfile), &stb) >= 0 &&
182 (stb.st_mode & 077) != 0) {
183 warnx(_("Error: .netrc file is readable by others."));
184 warnx(_("Remove password or make file unreadable by others."));
185 goto bad;
187 if (token() && *apass == 0) {
188 char *newp;
189 newp = malloc((unsigned) strlen(tokval) + 1);
190 if (newp == NULL)
192 warnx(_("out of memory"));
193 goto bad;
195 *apass = strcpy(newp, tokval);
197 break;
198 case ACCOUNT:
199 #if 0
200 if (fstat64(fileno(cfile), &stb) >= 0
201 && (stb.st_mode & 077) != 0) {
202 warnx("Error: .netrc file is readable by others.");
203 warnx("Remove account or make file unreadable by others.");
204 goto bad;
206 if (token() && *aacct == 0) {
207 *aacct = malloc((unsigned) strlen(tokval) + 1);
208 (void) strcpy(*aacct, tokval);
210 #endif
211 break;
212 case MACDEF:
213 #if 0
214 if (proxy) {
215 (void) fclose(cfile);
216 return (0);
218 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
219 || c == '\t');
220 if (c == EOF || c == '\n') {
221 printf("Missing macdef name argument.\n");
222 goto bad;
224 if (macnum == 16) {
225 printf("Limit of 16 macros have already been defined\n");
226 goto bad;
228 tmp = macros[macnum].mac_name;
229 *tmp++ = c;
230 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
231 !isspace(c); ++i) {
232 *tmp++ = c;
234 if (c == EOF) {
235 printf("Macro definition missing null line terminator.\n");
236 goto bad;
238 *tmp = '\0';
239 if (c != '\n') {
240 while ((c=getc_unlocked(cfile)) != EOF
241 && c != '\n');
243 if (c == EOF) {
244 printf("Macro definition missing null line terminator.\n");
245 goto bad;
247 if (macnum == 0) {
248 macros[macnum].mac_start = macbuf;
250 else {
251 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
253 tmp = macros[macnum].mac_start;
254 while (tmp != macbuf + 4096) {
255 if ((c=getc_unlocked(cfile)) == EOF) {
256 printf("Macro definition missing null line terminator.\n");
257 goto bad;
259 *tmp = c;
260 if (*tmp == '\n') {
261 if (*(tmp-1) == '\0') {
262 macros[macnum++].mac_end = tmp - 1;
263 break;
265 *tmp = '\0';
267 tmp++;
269 if (tmp == macbuf + 4096) {
270 printf("4K macro buffer exceeded\n");
271 goto bad;
273 #endif
274 break;
275 default:
276 warnx(_("Unknown .netrc keyword %s"), tokval);
277 break;
279 goto done;
281 done:
282 (void) fclose(cfile);
283 return (0);
284 bad:
285 (void) fclose(cfile);
286 return (-1);
288 libc_hidden_def (ruserpass)
290 static int
291 token (void)
293 char *cp;
294 int c;
295 int i;
297 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
298 return (0);
299 while ((c = getc_unlocked(cfile)) != EOF &&
300 (c == '\n' || c == '\t' || c == ' ' || c == ','))
301 continue;
302 if (c == EOF)
303 return (0);
304 cp = tokval;
305 if (c == '"') {
306 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
307 if (c == '\\')
308 c = getc_unlocked(cfile);
309 *cp++ = c;
311 } else {
312 *cp++ = c;
313 while ((c = getc_unlocked(cfile)) != EOF
314 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
315 if (c == '\\')
316 c = getc_unlocked(cfile);
317 *cp++ = c;
320 *cp = 0;
321 if (tokval[0] == 0)
322 return (0);
323 for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
324 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
325 return toktab[i].tval;
326 return (ID);