Update.
[glibc.git] / inet / ruserpass.c
blob70bfdd4c8dd2ac78c88e8df139b9170551569185
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 <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <libintl.h>
46 /* #include "ftp_var.h" */
48 static int token __P((void));
49 static FILE *cfile;
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
59 static char tokval[100];
61 static const char tokstr[] =
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"
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 }
94 int
95 ruserpass(host, aname, apass)
96 char *host, **aname, **apass;
98 char *hdir, *buf, *tmp;
99 char myname[1024], *mydomain;
100 int t, i, c, usedefault = 0;
101 struct stat stb;
103 hdir = __secure_getenv("HOME");
104 if (hdir == NULL) {
105 /* If we can't get HOME, fail instead of trying ".",
106 which is no improvement. This really should call
107 getpwuid(getuid()). */
108 /*hdir = ".";*/
109 return -1;
112 buf = alloca (strlen (hdir) + 8);
114 __stpcpy (__stpcpy (buf, hdir), "/.netrc");
115 cfile = fopen(buf, "r");
116 if (cfile == NULL) {
117 if (errno != ENOENT)
118 warn("%s", buf);
119 return (0);
121 if (__gethostname(myname, sizeof(myname)) < 0)
122 myname[0] = '\0';
123 mydomain = __strchrnul(myname, '.');
124 next:
125 while ((t = token())) switch(t) {
127 case DEFAULT:
128 usedefault = 1;
129 /* FALL THROUGH */
131 case MACHINE:
132 if (!usedefault) {
133 if (token() != ID)
134 continue;
136 * Allow match either for user's input host name
137 * or official hostname. Also allow match of
138 * incompletely-specified host in local domain.
140 if (__strcasecmp(host, tokval) == 0)
141 goto match;
142 /* if (__strcasecmp(hostname, tokval) == 0)
143 goto match;
144 if ((tmp = strchr(hostname, '.')) != NULL &&
145 __strcasecmp(tmp, mydomain) == 0 &&
146 __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
147 tokval[tmp - hostname] == '\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;
156 match:
157 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
159 case LOGIN:
160 if (token())
161 if (*aname == 0) {
162 *aname = malloc((unsigned) strlen(tokval) + 1);
163 (void) strcpy(*aname, tokval);
164 } else {
165 if (strcmp(*aname, tokval))
166 goto next;
168 break;
169 case PASSWD:
170 if (strcmp(*aname, "anonymous") &&
171 fstat(fileno(cfile), &stb) >= 0 &&
172 (stb.st_mode & 077) != 0) {
173 warnx(_("Error: .netrc file is readable by others."));
174 warnx(_("Remove password or make file unreadable by others."));
175 goto bad;
177 if (token() && *apass == 0) {
178 *apass = malloc((unsigned) strlen(tokval) + 1);
179 (void) strcpy(*apass, tokval);
181 break;
182 case ACCOUNT:
183 #if 0
184 if (fstat(fileno(cfile), &stb) >= 0
185 && (stb.st_mode & 077) != 0) {
186 warnx("Error: .netrc file is readable by others.");
187 warnx("Remove account or make file unreadable by others.");
188 goto bad;
190 if (token() && *aacct == 0) {
191 *aacct = malloc((unsigned) strlen(tokval) + 1);
192 (void) strcpy(*aacct, tokval);
194 #endif
195 break;
196 case MACDEF:
197 #if 0
198 if (proxy) {
199 (void) fclose(cfile);
200 return (0);
202 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
203 || c == '\t');
204 if (c == EOF || c == '\n') {
205 printf("Missing macdef name argument.\n");
206 goto bad;
208 if (macnum == 16) {
209 printf("Limit of 16 macros have already been defined\n");
210 goto bad;
212 tmp = macros[macnum].mac_name;
213 *tmp++ = c;
214 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
215 !isspace(c); ++i) {
216 *tmp++ = c;
218 if (c == EOF) {
219 printf("Macro definition missing null line terminator.\n");
220 goto bad;
222 *tmp = '\0';
223 if (c != '\n') {
224 while ((c=getc_unlocked(cfile)) != EOF
225 && c != '\n');
227 if (c == EOF) {
228 printf("Macro definition missing null line terminator.\n");
229 goto bad;
231 if (macnum == 0) {
232 macros[macnum].mac_start = macbuf;
234 else {
235 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
237 tmp = macros[macnum].mac_start;
238 while (tmp != macbuf + 4096) {
239 if ((c=getc_unlocked(cfile)) == EOF) {
240 printf("Macro definition missing null line terminator.\n");
241 goto bad;
243 *tmp = c;
244 if (*tmp == '\n') {
245 if (*(tmp-1) == '\0') {
246 macros[macnum++].mac_end = tmp - 1;
247 break;
249 *tmp = '\0';
251 tmp++;
253 if (tmp == macbuf + 4096) {
254 printf("4K macro buffer exceeded\n");
255 goto bad;
257 #endif
258 break;
259 default:
260 warnx(_("Unknown .netrc keyword %s"), tokval);
261 break;
263 goto done;
265 done:
266 (void) fclose(cfile);
267 return (0);
268 bad:
269 (void) fclose(cfile);
270 return (-1);
273 static int
274 token()
276 char *cp;
277 int c;
278 int i;
280 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
281 return (0);
282 while ((c = getc_unlocked(cfile)) != EOF &&
283 (c == '\n' || c == '\t' || c == ' ' || c == ','))
284 continue;
285 if (c == EOF)
286 return (0);
287 cp = tokval;
288 if (c == '"') {
289 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
290 if (c == '\\')
291 c = getc_unlocked(cfile);
292 *cp++ = c;
294 } else {
295 *cp++ = c;
296 while ((c = getc_unlocked(cfile)) != EOF
297 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
298 if (c == '\\')
299 c = getc_unlocked(cfile);
300 *cp++ = c;
303 *cp = 0;
304 if (tokval[0] == 0)
305 return (0);
306 for (i = 0; i < sizeof (toktab) / sizeof (toktab[0]); ++i)
307 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
308 return toktab[i].tval;
309 return (ID);