Update.
[glibc.git] / inet / ruserpass.c
blob2673fc5bf47249c502eaaeea4999ee51558ff7cd
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)ruserpass.c 8.3 (Berkeley) 4/2/94";
36 #endif /* not lint */
38 #include <sys/types.h>
39 #include <sys/stat.h>
41 #include <ctype.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 /* #include "ftp_var.h" */
51 static int token __P((void));
52 static FILE *cfile;
54 #define DEFAULT 1
55 #define LOGIN 2
56 #define PASSWD 3
57 #define ACCOUNT 4
58 #define MACDEF 5
59 #define ID 10
60 #define MACHINE 11
62 static char tokval[100];
64 static const char tokstr[] =
66 #define TOK_DEFAULT_IDX 0
67 "default\0"
68 #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
69 "login\0"
70 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
71 "password\0"
72 #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
73 "passwd\0"
74 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
75 "account\0"
76 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
77 "machine\0"
78 #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
79 "macdef"
82 static const struct toktab {
83 int tokstr_off;
84 int tval;
85 } toktab[]= {
86 { TOK_DEFAULT_IDX, DEFAULT },
87 { TOK_LOGIN_IDX, LOGIN },
88 { TOK_PASSWORD_IDX, PASSWD },
89 { TOK_PASSWD_IDX, PASSWD },
90 { TOK_ACCOUNT_IDX, ACCOUNT },
91 { TOK_MACHINE_IDX, MACHINE },
92 { TOK_MACDEF_IDX, MACDEF }
97 int
98 ruserpass(host, aname, apass)
99 char *host, **aname, **apass;
101 char *hdir, *buf, *tmp;
102 char myname[1024], *mydomain;
103 int t, i, c, usedefault = 0;
104 struct stat stb;
106 hdir = __secure_getenv("HOME");
107 if (hdir == NULL) {
108 /* If we can't get HOME, fail instead of trying ".",
109 which is no improvement. This really should call
110 getpwuid(getuid()). */
111 /*hdir = ".";*/
112 return -1;
115 buf = alloca (strlen (hdir) + 8);
117 __stpcpy (__stpcpy (buf, hdir), "/.netrc");
118 cfile = fopen(buf, "r");
119 if (cfile == NULL) {
120 if (errno != ENOENT)
121 warn("%s", buf);
122 return (0);
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 *aname = malloc((unsigned) strlen(tokval) + 1);
166 (void) strcpy(*aname, tokval);
167 } else {
168 if (strcmp(*aname, tokval))
169 goto next;
171 break;
172 case PASSWD:
173 if (strcmp(*aname, "anonymous") &&
174 fstat(fileno(cfile), &stb) >= 0 &&
175 (stb.st_mode & 077) != 0) {
176 warnx(_("Error: .netrc file is readable by others."));
177 warnx(_("Remove password or make file unreadable by others."));
178 goto bad;
180 if (token() && *apass == 0) {
181 *apass = malloc((unsigned) strlen(tokval) + 1);
182 (void) strcpy(*apass, tokval);
184 break;
185 case ACCOUNT:
186 #if 0
187 if (fstat(fileno(cfile), &stb) >= 0
188 && (stb.st_mode & 077) != 0) {
189 warnx("Error: .netrc file is readable by others.");
190 warnx("Remove account or make file unreadable by others.");
191 goto bad;
193 if (token() && *aacct == 0) {
194 *aacct = malloc((unsigned) strlen(tokval) + 1);
195 (void) strcpy(*aacct, tokval);
197 #endif
198 break;
199 case MACDEF:
200 #if 0
201 if (proxy) {
202 (void) fclose(cfile);
203 return (0);
205 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
206 || c == '\t');
207 if (c == EOF || c == '\n') {
208 printf("Missing macdef name argument.\n");
209 goto bad;
211 if (macnum == 16) {
212 printf("Limit of 16 macros have already been defined\n");
213 goto bad;
215 tmp = macros[macnum].mac_name;
216 *tmp++ = c;
217 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
218 !isspace(c); ++i) {
219 *tmp++ = c;
221 if (c == EOF) {
222 printf("Macro definition missing null line terminator.\n");
223 goto bad;
225 *tmp = '\0';
226 if (c != '\n') {
227 while ((c=getc_unlocked(cfile)) != EOF
228 && c != '\n');
230 if (c == EOF) {
231 printf("Macro definition missing null line terminator.\n");
232 goto bad;
234 if (macnum == 0) {
235 macros[macnum].mac_start = macbuf;
237 else {
238 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
240 tmp = macros[macnum].mac_start;
241 while (tmp != macbuf + 4096) {
242 if ((c=getc_unlocked(cfile)) == EOF) {
243 printf("Macro definition missing null line terminator.\n");
244 goto bad;
246 *tmp = c;
247 if (*tmp == '\n') {
248 if (*(tmp-1) == '\0') {
249 macros[macnum++].mac_end = tmp - 1;
250 break;
252 *tmp = '\0';
254 tmp++;
256 if (tmp == macbuf + 4096) {
257 printf("4K macro buffer exceeded\n");
258 goto bad;
260 #endif
261 break;
262 default:
263 warnx(_("Unknown .netrc keyword %s"), tokval);
264 break;
266 goto done;
268 done:
269 (void) fclose(cfile);
270 return (0);
271 bad:
272 (void) fclose(cfile);
273 return (-1);
276 static int
277 token()
279 char *cp;
280 int c;
281 int i;
283 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
284 return (0);
285 while ((c = getc_unlocked(cfile)) != EOF &&
286 (c == '\n' || c == '\t' || c == ' ' || c == ','))
287 continue;
288 if (c == EOF)
289 return (0);
290 cp = tokval;
291 if (c == '"') {
292 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
293 if (c == '\\')
294 c = getc_unlocked(cfile);
295 *cp++ = c;
297 } else {
298 *cp++ = c;
299 while ((c = getc_unlocked(cfile)) != EOF
300 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
301 if (c == '\\')
302 c = getc_unlocked(cfile);
303 *cp++ = c;
306 *cp = 0;
307 if (tokval[0] == 0)
308 return (0);
309 for (i = 0; i < sizeof (toktab) / sizeof (toktab[0]); ++i)
310 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
311 return toktab[i].tval;
312 return (ID);