]> git.ipfire.org Git - thirdparty/glibc.git/blame - inet/ruserpass.c
Update.
[thirdparty/glibc.git] / inet / ruserpass.c
CommitLineData
49633efb
RM
1/*
2 * Copyright (c) 1985, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
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.
49633efb
RM
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.
16 *
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.
28 */
29
390500b1 30#if defined(LIBC_SCCS) && !defined(lint)
49633efb
RM
31static char sccsid[] = "@(#)ruserpass.c 8.3 (Berkeley) 4/2/94";
32#endif /* not lint */
33
34#include <sys/types.h>
35#include <sys/stat.h>
36
37#include <ctype.h>
2de99474 38#include <err.h>
49633efb
RM
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45/* #include "ftp_var.h" */
46
47static int token __P((void));
48static FILE *cfile;
49
50#define DEFAULT 1
51#define LOGIN 2
52#define PASSWD 3
53#define ACCOUNT 4
54#define MACDEF 5
55#define ID 10
56#define MACHINE 11
57
58static char tokval[100];
59
390500b1
UD
60static const char tokstr[] =
61{
62#define TOK_DEFAULT_IDX 0
63 "default\0"
64#define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
65 "login\0"
66#define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
67 "password\0"
68#define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
69 "passwd\0"
70#define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
71 "account\0"
72#define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
73 "machine\0"
74#define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
75 "macdef"
76};
77
78static const struct toktab {
79 int tokstr_off;
49633efb
RM
80 int tval;
81} toktab[]= {
390500b1
UD
82 { TOK_DEFAULT_IDX, DEFAULT },
83 { TOK_LOGIN_IDX, LOGIN },
84 { TOK_PASSWORD_IDX, PASSWD },
85 { TOK_PASSWD_IDX, PASSWD },
86 { TOK_ACCOUNT_IDX, ACCOUNT },
87 { TOK_MACHINE_IDX, MACHINE },
88 { TOK_MACDEF_IDX, MACDEF }
49633efb
RM
89};
90
91
92
93int
94ruserpass(host, aname, apass)
95 char *host, **aname, **apass;
96{
2de99474 97 char *hdir, *buf, *tmp;
49633efb
RM
98 char myname[1024], *mydomain;
99 int t, i, c, usedefault = 0;
100 struct stat stb;
101
d68171ed
UD
102 hdir = __secure_getenv("HOME");
103 if (hdir == NULL) {
104 /* If we can't get HOME, fail instead of trying ".",
105 which is no improvement. This really should call
106 getpwuid(getuid()). */
107 /*hdir = ".";*/
108 return -1;
109 }
2de99474
UD
110
111 buf = alloca (strlen (hdir) + 8);
112
86187531 113 __stpcpy (__stpcpy (buf, hdir), "/.netrc");
49633efb
RM
114 cfile = fopen(buf, "r");
115 if (cfile == NULL) {
2de99474
UD
116 if (errno != ENOENT)
117 warn("%s", buf);
49633efb
RM
118 return (0);
119 }
50304ef0 120 if (__gethostname(myname, sizeof(myname)) < 0)
49633efb 121 myname[0] = '\0';
390500b1 122 mydomain = __strchrnul(myname, '.');
49633efb
RM
123next:
124 while ((t = token())) switch(t) {
125
126 case DEFAULT:
127 usedefault = 1;
128 /* FALL THROUGH */
129
130 case MACHINE:
131 if (!usedefault) {
132 if (token() != ID)
133 continue;
134 /*
135 * Allow match either for user's input host name
2de99474 136 * or official hostname. Also allow match of
49633efb
RM
137 * incompletely-specified host in local domain.
138 */
50304ef0 139 if (__strcasecmp(host, tokval) == 0)
49633efb 140 goto match;
50304ef0 141/* if (__strcasecmp(hostname, tokval) == 0)
2de99474 142 goto match;
49633efb 143 if ((tmp = strchr(hostname, '.')) != NULL &&
50304ef0
UD
144 __strcasecmp(tmp, mydomain) == 0 &&
145 __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
49633efb
RM
146 tokval[tmp - hostname] == '\0')
147 goto match; */
148 if ((tmp = strchr(host, '.')) != NULL &&
50304ef0
UD
149 __strcasecmp(tmp, mydomain) == 0 &&
150 __strncasecmp(host, tokval, tmp - host) == 0 &&
49633efb
RM
151 tokval[tmp - host] == '\0')
152 goto match;
153 continue;
154 }
155 match:
156 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
157
158 case LOGIN:
159 if (token())
2de99474 160 if (*aname == 0) {
49633efb
RM
161 *aname = malloc((unsigned) strlen(tokval) + 1);
162 (void) strcpy(*aname, tokval);
163 } else {
164 if (strcmp(*aname, tokval))
165 goto next;
166 }
167 break;
168 case PASSWD:
169 if (strcmp(*aname, "anonymous") &&
170 fstat(fileno(cfile), &stb) >= 0 &&
171 (stb.st_mode & 077) != 0) {
2de99474
UD
172 warnx(_("Error: .netrc file is readable by others."));
173 warnx(_("Remove password or make file unreadable by others."));
49633efb
RM
174 goto bad;
175 }
176 if (token() && *apass == 0) {
177 *apass = malloc((unsigned) strlen(tokval) + 1);
178 (void) strcpy(*apass, tokval);
179 }
180 break;
181 case ACCOUNT:
182#if 0
183 if (fstat(fileno(cfile), &stb) >= 0
184 && (stb.st_mode & 077) != 0) {
185 warnx("Error: .netrc file is readable by others.");
186 warnx("Remove account or make file unreadable by others.");
187 goto bad;
188 }
189 if (token() && *aacct == 0) {
190 *aacct = malloc((unsigned) strlen(tokval) + 1);
191 (void) strcpy(*aacct, tokval);
192 }
193#endif
194 break;
195 case MACDEF:
196#if 0
197 if (proxy) {
198 (void) fclose(cfile);
199 return (0);
2de99474 200 }
eb27c43f
UD
201 while ((c=getc_unlocked(cfile)) != EOF && c == ' '
202 || c == '\t');
49633efb
RM
203 if (c == EOF || c == '\n') {
204 printf("Missing macdef name argument.\n");
205 goto bad;
206 }
207 if (macnum == 16) {
208 printf("Limit of 16 macros have already been defined\n");
209 goto bad;
210 }
211 tmp = macros[macnum].mac_name;
212 *tmp++ = c;
eb27c43f 213 for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
49633efb
RM
214 !isspace(c); ++i) {
215 *tmp++ = c;
216 }
217 if (c == EOF) {
218 printf("Macro definition missing null line terminator.\n");
219 goto bad;
220 }
221 *tmp = '\0';
222 if (c != '\n') {
eb27c43f
UD
223 while ((c=getc_unlocked(cfile)) != EOF
224 && c != '\n');
49633efb
RM
225 }
226 if (c == EOF) {
227 printf("Macro definition missing null line terminator.\n");
228 goto bad;
229 }
230 if (macnum == 0) {
231 macros[macnum].mac_start = macbuf;
232 }
233 else {
234 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
235 }
236 tmp = macros[macnum].mac_start;
237 while (tmp != macbuf + 4096) {
eb27c43f 238 if ((c=getc_unlocked(cfile)) == EOF) {
49633efb
RM
239 printf("Macro definition missing null line terminator.\n");
240 goto bad;
241 }
242 *tmp = c;
243 if (*tmp == '\n') {
244 if (*(tmp-1) == '\0') {
245 macros[macnum++].mac_end = tmp - 1;
246 break;
247 }
248 *tmp = '\0';
249 }
250 tmp++;
251 }
252 if (tmp == macbuf + 4096) {
253 printf("4K macro buffer exceeded\n");
254 goto bad;
255 }
256#endif
257 break;
258 default:
2de99474 259 warnx(_("Unknown .netrc keyword %s"), tokval);
49633efb
RM
260 break;
261 }
262 goto done;
263 }
264done:
265 (void) fclose(cfile);
266 return (0);
267bad:
268 (void) fclose(cfile);
269 return (-1);
270}
271
272static int
273token()
274{
275 char *cp;
276 int c;
390500b1 277 int i;
49633efb 278
77ccaba1 279 if (feof_unlocked(cfile) || ferror_unlocked(cfile))
49633efb 280 return (0);
eb27c43f 281 while ((c = getc_unlocked(cfile)) != EOF &&
49633efb
RM
282 (c == '\n' || c == '\t' || c == ' ' || c == ','))
283 continue;
284 if (c == EOF)
285 return (0);
286 cp = tokval;
287 if (c == '"') {
eb27c43f 288 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
49633efb 289 if (c == '\\')
eb27c43f 290 c = getc_unlocked(cfile);
49633efb
RM
291 *cp++ = c;
292 }
293 } else {
294 *cp++ = c;
eb27c43f 295 while ((c = getc_unlocked(cfile)) != EOF
49633efb
RM
296 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
297 if (c == '\\')
eb27c43f 298 c = getc_unlocked(cfile);
49633efb
RM
299 *cp++ = c;
300 }
301 }
302 *cp = 0;
303 if (tokval[0] == 0)
304 return (0);
390500b1
UD
305 for (i = 0; i < sizeof (toktab) / sizeof (toktab[0]); ++i)
306 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
307 return toktab[i].tval;
49633efb
RM
308 return (ID);
309}