Update.
[glibc.git] / inet / ruserpass.c
blobd8ea79a04613e16e5fd297f968164561b84b2d7d
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 #ifndef 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 struct toktab {
65 const char *tokstr;
66 int tval;
67 } toktab[]= {
68 { "default", DEFAULT },
69 { "login", LOGIN },
70 { "password", PASSWD },
71 { "passwd", PASSWD },
72 { "account", ACCOUNT },
73 { "machine", MACHINE },
74 { "macdef", MACDEF },
75 { NULL, 0 }
80 int
81 ruserpass(host, aname, apass)
82 char *host, **aname, **apass;
84 char *hdir, *buf, *tmp;
85 char myname[1024], *mydomain;
86 int t, i, c, usedefault = 0;
87 struct stat stb;
89 hdir = __secure_getenv("HOME");
90 if (hdir == NULL) {
91 /* If we can't get HOME, fail instead of trying ".",
92 which is no improvement. This really should call
93 getpwuid(getuid()). */
94 /*hdir = ".";*/
95 return -1;
98 buf = alloca (strlen (hdir) + 8);
100 __stpcpy (__stpcpy (buf, hdir), "/.netrc");
101 cfile = fopen(buf, "r");
102 if (cfile == NULL) {
103 if (errno != ENOENT)
104 warn("%s", buf);
105 return (0);
107 if (__gethostname(myname, sizeof(myname)) < 0)
108 myname[0] = '\0';
109 if ((mydomain = strchr(myname, '.')) == NULL)
110 mydomain = "";
111 next:
112 while ((t = token())) switch(t) {
114 case DEFAULT:
115 usedefault = 1;
116 /* FALL THROUGH */
118 case MACHINE:
119 if (!usedefault) {
120 if (token() != ID)
121 continue;
123 * Allow match either for user's input host name
124 * or official hostname. Also allow match of
125 * incompletely-specified host in local domain.
127 if (__strcasecmp(host, tokval) == 0)
128 goto match;
129 /* if (__strcasecmp(hostname, tokval) == 0)
130 goto match;
131 if ((tmp = strchr(hostname, '.')) != NULL &&
132 __strcasecmp(tmp, mydomain) == 0 &&
133 __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
134 tokval[tmp - hostname] == '\0')
135 goto match; */
136 if ((tmp = strchr(host, '.')) != NULL &&
137 __strcasecmp(tmp, mydomain) == 0 &&
138 __strncasecmp(host, tokval, tmp - host) == 0 &&
139 tokval[tmp - host] == '\0')
140 goto match;
141 continue;
143 match:
144 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
146 case LOGIN:
147 if (token())
148 if (*aname == 0) {
149 *aname = malloc((unsigned) strlen(tokval) + 1);
150 (void) strcpy(*aname, tokval);
151 } else {
152 if (strcmp(*aname, tokval))
153 goto next;
155 break;
156 case PASSWD:
157 if (strcmp(*aname, "anonymous") &&
158 fstat(fileno(cfile), &stb) >= 0 &&
159 (stb.st_mode & 077) != 0) {
160 warnx(_("Error: .netrc file is readable by others."));
161 warnx(_("Remove password or make file unreadable by others."));
162 goto bad;
164 if (token() && *apass == 0) {
165 *apass = malloc((unsigned) strlen(tokval) + 1);
166 (void) strcpy(*apass, tokval);
168 break;
169 case ACCOUNT:
170 #if 0
171 if (fstat(fileno(cfile), &stb) >= 0
172 && (stb.st_mode & 077) != 0) {
173 warnx("Error: .netrc file is readable by others.");
174 warnx("Remove account or make file unreadable by others.");
175 goto bad;
177 if (token() && *aacct == 0) {
178 *aacct = malloc((unsigned) strlen(tokval) + 1);
179 (void) strcpy(*aacct, tokval);
181 #endif
182 break;
183 case MACDEF:
184 #if 0
185 if (proxy) {
186 (void) fclose(cfile);
187 return (0);
189 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
190 || c == '\t');
191 if (c == EOF || c == '\n') {
192 printf("Missing macdef name argument.\n");
193 goto bad;
195 if (macnum == 16) {
196 printf("Limit of 16 macros have already been defined\n");
197 goto bad;
199 tmp = macros[macnum].mac_name;
200 *tmp++ = c;
201 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
202 !isspace(c); ++i) {
203 *tmp++ = c;
205 if (c == EOF) {
206 printf("Macro definition missing null line terminator.\n");
207 goto bad;
209 *tmp = '\0';
210 if (c != '\n') {
211 while ((c=getc_unlocked(cfile)) != EOF
212 && c != '\n');
214 if (c == EOF) {
215 printf("Macro definition missing null line terminator.\n");
216 goto bad;
218 if (macnum == 0) {
219 macros[macnum].mac_start = macbuf;
221 else {
222 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
224 tmp = macros[macnum].mac_start;
225 while (tmp != macbuf + 4096) {
226 if ((c=getc_unlocked(cfile)) == EOF) {
227 printf("Macro definition missing null line terminator.\n");
228 goto bad;
230 *tmp = c;
231 if (*tmp == '\n') {
232 if (*(tmp-1) == '\0') {
233 macros[macnum++].mac_end = tmp - 1;
234 break;
236 *tmp = '\0';
238 tmp++;
240 if (tmp == macbuf + 4096) {
241 printf("4K macro buffer exceeded\n");
242 goto bad;
244 #endif
245 break;
246 default:
247 warnx(_("Unknown .netrc keyword %s"), tokval);
248 break;
250 goto done;
252 done:
253 (void) fclose(cfile);
254 return (0);
255 bad:
256 (void) fclose(cfile);
257 return (-1);
260 static int
261 token()
263 char *cp;
264 int c;
265 struct toktab *t;
267 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
268 return (0);
269 while ((c = getc_unlocked(cfile)) != EOF &&
270 (c == '\n' || c == '\t' || c == ' ' || c == ','))
271 continue;
272 if (c == EOF)
273 return (0);
274 cp = tokval;
275 if (c == '"') {
276 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
277 if (c == '\\')
278 c = getc_unlocked(cfile);
279 *cp++ = c;
281 } else {
282 *cp++ = c;
283 while ((c = getc_unlocked(cfile)) != EOF
284 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
285 if (c == '\\')
286 c = getc_unlocked(cfile);
287 *cp++ = c;
290 *cp = 0;
291 if (tokval[0] == 0)
292 return (0);
293 for (t = toktab; t->tokstr; t++)
294 if (!strcmp(t->tokstr, tokval))
295 return (t->tval);
296 return (ID);