handle password file locking.
[glibc.git] / inet / ruserpass.c
blobcff9493b07f3a7d45bee548b92733b5b0ac56b31
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 <assert.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 /* #include "ftp_var.h" */
52 static int token __P((void));
53 static FILE *cfile;
55 #define DEFAULT 1
56 #define LOGIN 2
57 #define PASSWD 3
58 #define ACCOUNT 4
59 #define MACDEF 5
60 #define ID 10
61 #define MACHINE 11
63 static char tokval[100];
65 static struct toktab {
66 char *tokstr;
67 int tval;
68 } toktab[]= {
69 { "default", DEFAULT },
70 { "login", LOGIN },
71 { "password", PASSWD },
72 { "passwd", PASSWD },
73 { "account", ACCOUNT },
74 { "machine", MACHINE },
75 { "macdef", MACDEF },
76 { NULL, 0 }
81 int
82 ruserpass(host, aname, apass)
83 char *host, **aname, **apass;
85 char *hdir, *buf, *tmp;
86 char myname[1024], *mydomain;
87 int t, i, c, usedefault = 0;
88 struct stat stb;
90 hdir = getenv("HOME");
91 if (hdir == NULL)
92 hdir = ".";
94 buf = alloca (strlen (hdir) + 8);
96 (void) sprintf(buf, "%s/.netrc", hdir);
97 cfile = fopen(buf, "r");
98 if (cfile == NULL) {
99 if (errno != ENOENT)
100 warn("%s", buf);
101 return (0);
103 if (gethostname(myname, sizeof(myname)) < 0)
104 myname[0] = '\0';
105 if ((mydomain = strchr(myname, '.')) == NULL)
106 mydomain = "";
107 next:
108 while ((t = token())) switch(t) {
110 case DEFAULT:
111 usedefault = 1;
112 /* FALL THROUGH */
114 case MACHINE:
115 if (!usedefault) {
116 if (token() != ID)
117 continue;
119 * Allow match either for user's input host name
120 * or official hostname. Also allow match of
121 * incompletely-specified host in local domain.
123 if (strcasecmp(host, tokval) == 0)
124 goto match;
125 /* if (strcasecmp(hostname, tokval) == 0)
126 goto match;
127 if ((tmp = strchr(hostname, '.')) != NULL &&
128 strcasecmp(tmp, mydomain) == 0 &&
129 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
130 tokval[tmp - hostname] == '\0')
131 goto match; */
132 if ((tmp = strchr(host, '.')) != NULL &&
133 strcasecmp(tmp, mydomain) == 0 &&
134 strncasecmp(host, tokval, tmp - host) == 0 &&
135 tokval[tmp - host] == '\0')
136 goto match;
137 continue;
139 match:
140 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
142 case LOGIN:
143 if (token())
144 if (*aname == 0) {
145 *aname = malloc((unsigned) strlen(tokval) + 1);
146 (void) strcpy(*aname, tokval);
147 } else {
148 if (strcmp(*aname, tokval))
149 goto next;
151 break;
152 case PASSWD:
153 if (strcmp(*aname, "anonymous") &&
154 fstat(fileno(cfile), &stb) >= 0 &&
155 (stb.st_mode & 077) != 0) {
156 warnx(_("Error: .netrc file is readable by others."));
157 warnx(_("Remove password or make file unreadable by others."));
158 goto bad;
160 if (token() && *apass == 0) {
161 *apass = malloc((unsigned) strlen(tokval) + 1);
162 (void) strcpy(*apass, tokval);
164 break;
165 case ACCOUNT:
166 #if 0
167 if (fstat(fileno(cfile), &stb) >= 0
168 && (stb.st_mode & 077) != 0) {
169 warnx("Error: .netrc file is readable by others.");
170 warnx("Remove account or make file unreadable by others.");
171 goto bad;
173 if (token() && *aacct == 0) {
174 *aacct = malloc((unsigned) strlen(tokval) + 1);
175 (void) strcpy(*aacct, tokval);
177 #endif
178 break;
179 case MACDEF:
180 #if 0
181 if (proxy) {
182 (void) fclose(cfile);
183 return (0);
185 while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
186 if (c == EOF || c == '\n') {
187 printf("Missing macdef name argument.\n");
188 goto bad;
190 if (macnum == 16) {
191 printf("Limit of 16 macros have already been defined\n");
192 goto bad;
194 tmp = macros[macnum].mac_name;
195 *tmp++ = c;
196 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
197 !isspace(c); ++i) {
198 *tmp++ = c;
200 if (c == EOF) {
201 printf("Macro definition missing null line terminator.\n");
202 goto bad;
204 *tmp = '\0';
205 if (c != '\n') {
206 while ((c=getc(cfile)) != EOF && c != '\n');
208 if (c == EOF) {
209 printf("Macro definition missing null line terminator.\n");
210 goto bad;
212 if (macnum == 0) {
213 macros[macnum].mac_start = macbuf;
215 else {
216 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
218 tmp = macros[macnum].mac_start;
219 while (tmp != macbuf + 4096) {
220 if ((c=getc(cfile)) == EOF) {
221 printf("Macro definition missing null line terminator.\n");
222 goto bad;
224 *tmp = c;
225 if (*tmp == '\n') {
226 if (*(tmp-1) == '\0') {
227 macros[macnum++].mac_end = tmp - 1;
228 break;
230 *tmp = '\0';
232 tmp++;
234 if (tmp == macbuf + 4096) {
235 printf("4K macro buffer exceeded\n");
236 goto bad;
238 #endif
239 break;
240 default:
241 warnx(_("Unknown .netrc keyword %s"), tokval);
242 break;
244 goto done;
246 done:
247 (void) fclose(cfile);
248 return (0);
249 bad:
250 (void) fclose(cfile);
251 return (-1);
254 static int
255 token()
257 char *cp;
258 int c;
259 struct toktab *t;
261 if (feof(cfile) || ferror(cfile))
262 return (0);
263 while ((c = getc(cfile)) != EOF &&
264 (c == '\n' || c == '\t' || c == ' ' || c == ','))
265 continue;
266 if (c == EOF)
267 return (0);
268 cp = tokval;
269 if (c == '"') {
270 while ((c = getc(cfile)) != EOF && c != '"') {
271 if (c == '\\')
272 c = getc(cfile);
273 *cp++ = c;
275 } else {
276 *cp++ = c;
277 while ((c = getc(cfile)) != EOF
278 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
279 if (c == '\\')
280 c = getc(cfile);
281 *cp++ = c;
284 *cp = 0;
285 if (tokval[0] == 0)
286 return (0);
287 for (t = toktab; t->tokstr; t++)
288 if (!strcmp(t->tokstr, tokval))
289 return (t->tval);
290 return (ID);