]>
Commit | Line | Data |
---|---|---|
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 |
31 | static 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> | |
4360eafd | 44 | #include <libintl.h> |
49633efb RM |
45 | |
46 | /* #include "ftp_var.h" */ | |
47 | ||
48 | static int token __P((void)); | |
49 | static FILE *cfile; | |
50 | ||
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 | |
58 | ||
59 | static char tokval[100]; | |
60 | ||
390500b1 UD |
61 | static const char tokstr[] = |
62 | { | |
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" | |
77 | }; | |
78 | ||
79 | static const struct toktab { | |
80 | int tokstr_off; | |
49633efb RM |
81 | int tval; |
82 | } toktab[]= { | |
390500b1 UD |
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 } | |
49633efb RM |
90 | }; |
91 | ||
92 | ||
93 | ||
94 | int | |
95 | ruserpass(host, aname, apass) | |
96 | char *host, **aname, **apass; | |
97 | { | |
2de99474 | 98 | char *hdir, *buf, *tmp; |
49633efb RM |
99 | char myname[1024], *mydomain; |
100 | int t, i, c, usedefault = 0; | |
101 | struct stat stb; | |
102 | ||
d68171ed UD |
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; | |
110 | } | |
2de99474 UD |
111 | |
112 | buf = alloca (strlen (hdir) + 8); | |
113 | ||
86187531 | 114 | __stpcpy (__stpcpy (buf, hdir), "/.netrc"); |
49633efb RM |
115 | cfile = fopen(buf, "r"); |
116 | if (cfile == NULL) { | |
2de99474 UD |
117 | if (errno != ENOENT) |
118 | warn("%s", buf); | |
49633efb RM |
119 | return (0); |
120 | } | |
50304ef0 | 121 | if (__gethostname(myname, sizeof(myname)) < 0) |
49633efb | 122 | myname[0] = '\0'; |
390500b1 | 123 | mydomain = __strchrnul(myname, '.'); |
49633efb RM |
124 | next: |
125 | while ((t = token())) switch(t) { | |
126 | ||
127 | case DEFAULT: | |
128 | usedefault = 1; | |
129 | /* FALL THROUGH */ | |
130 | ||
131 | case MACHINE: | |
132 | if (!usedefault) { | |
133 | if (token() != ID) | |
134 | continue; | |
135 | /* | |
136 | * Allow match either for user's input host name | |
2de99474 | 137 | * or official hostname. Also allow match of |
49633efb RM |
138 | * incompletely-specified host in local domain. |
139 | */ | |
50304ef0 | 140 | if (__strcasecmp(host, tokval) == 0) |
49633efb | 141 | goto match; |
50304ef0 | 142 | /* if (__strcasecmp(hostname, tokval) == 0) |
2de99474 | 143 | goto match; |
49633efb | 144 | if ((tmp = strchr(hostname, '.')) != NULL && |
50304ef0 UD |
145 | __strcasecmp(tmp, mydomain) == 0 && |
146 | __strncasecmp(hostname, tokval, tmp-hostname) == 0 && | |
49633efb RM |
147 | tokval[tmp - hostname] == '\0') |
148 | goto match; */ | |
149 | if ((tmp = strchr(host, '.')) != NULL && | |
50304ef0 UD |
150 | __strcasecmp(tmp, mydomain) == 0 && |
151 | __strncasecmp(host, tokval, tmp - host) == 0 && | |
49633efb RM |
152 | tokval[tmp - host] == '\0') |
153 | goto match; | |
154 | continue; | |
155 | } | |
156 | match: | |
157 | while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) { | |
158 | ||
159 | case LOGIN: | |
160 | if (token()) | |
2de99474 | 161 | if (*aname == 0) { |
49633efb RM |
162 | *aname = malloc((unsigned) strlen(tokval) + 1); |
163 | (void) strcpy(*aname, tokval); | |
164 | } else { | |
165 | if (strcmp(*aname, tokval)) | |
166 | goto next; | |
167 | } | |
168 | break; | |
169 | case PASSWD: | |
170 | if (strcmp(*aname, "anonymous") && | |
171 | fstat(fileno(cfile), &stb) >= 0 && | |
172 | (stb.st_mode & 077) != 0) { | |
2de99474 UD |
173 | warnx(_("Error: .netrc file is readable by others.")); |
174 | warnx(_("Remove password or make file unreadable by others.")); | |
49633efb RM |
175 | goto bad; |
176 | } | |
177 | if (token() && *apass == 0) { | |
178 | *apass = malloc((unsigned) strlen(tokval) + 1); | |
179 | (void) strcpy(*apass, tokval); | |
180 | } | |
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; | |
189 | } | |
190 | if (token() && *aacct == 0) { | |
191 | *aacct = malloc((unsigned) strlen(tokval) + 1); | |
192 | (void) strcpy(*aacct, tokval); | |
193 | } | |
194 | #endif | |
195 | break; | |
196 | case MACDEF: | |
197 | #if 0 | |
198 | if (proxy) { | |
199 | (void) fclose(cfile); | |
200 | return (0); | |
2de99474 | 201 | } |
eb27c43f UD |
202 | while ((c=getc_unlocked(cfile)) != EOF && c == ' ' |
203 | || c == '\t'); | |
49633efb RM |
204 | if (c == EOF || c == '\n') { |
205 | printf("Missing macdef name argument.\n"); | |
206 | goto bad; | |
207 | } | |
208 | if (macnum == 16) { | |
209 | printf("Limit of 16 macros have already been defined\n"); | |
210 | goto bad; | |
211 | } | |
212 | tmp = macros[macnum].mac_name; | |
213 | *tmp++ = c; | |
eb27c43f | 214 | for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF && |
49633efb RM |
215 | !isspace(c); ++i) { |
216 | *tmp++ = c; | |
217 | } | |
218 | if (c == EOF) { | |
219 | printf("Macro definition missing null line terminator.\n"); | |
220 | goto bad; | |
221 | } | |
222 | *tmp = '\0'; | |
223 | if (c != '\n') { | |
eb27c43f UD |
224 | while ((c=getc_unlocked(cfile)) != EOF |
225 | && c != '\n'); | |
49633efb RM |
226 | } |
227 | if (c == EOF) { | |
228 | printf("Macro definition missing null line terminator.\n"); | |
229 | goto bad; | |
230 | } | |
231 | if (macnum == 0) { | |
232 | macros[macnum].mac_start = macbuf; | |
233 | } | |
234 | else { | |
235 | macros[macnum].mac_start = macros[macnum-1].mac_end + 1; | |
236 | } | |
237 | tmp = macros[macnum].mac_start; | |
238 | while (tmp != macbuf + 4096) { | |
eb27c43f | 239 | if ((c=getc_unlocked(cfile)) == EOF) { |
49633efb RM |
240 | printf("Macro definition missing null line terminator.\n"); |
241 | goto bad; | |
242 | } | |
243 | *tmp = c; | |
244 | if (*tmp == '\n') { | |
245 | if (*(tmp-1) == '\0') { | |
246 | macros[macnum++].mac_end = tmp - 1; | |
247 | break; | |
248 | } | |
249 | *tmp = '\0'; | |
250 | } | |
251 | tmp++; | |
252 | } | |
253 | if (tmp == macbuf + 4096) { | |
254 | printf("4K macro buffer exceeded\n"); | |
255 | goto bad; | |
256 | } | |
257 | #endif | |
258 | break; | |
259 | default: | |
2de99474 | 260 | warnx(_("Unknown .netrc keyword %s"), tokval); |
49633efb RM |
261 | break; |
262 | } | |
263 | goto done; | |
264 | } | |
265 | done: | |
266 | (void) fclose(cfile); | |
267 | return (0); | |
268 | bad: | |
269 | (void) fclose(cfile); | |
270 | return (-1); | |
271 | } | |
272 | ||
273 | static int | |
274 | token() | |
275 | { | |
276 | char *cp; | |
277 | int c; | |
390500b1 | 278 | int i; |
49633efb | 279 | |
77ccaba1 | 280 | if (feof_unlocked(cfile) || ferror_unlocked(cfile)) |
49633efb | 281 | return (0); |
eb27c43f | 282 | while ((c = getc_unlocked(cfile)) != EOF && |
49633efb RM |
283 | (c == '\n' || c == '\t' || c == ' ' || c == ',')) |
284 | continue; | |
285 | if (c == EOF) | |
286 | return (0); | |
287 | cp = tokval; | |
288 | if (c == '"') { | |
eb27c43f | 289 | while ((c = getc_unlocked(cfile)) != EOF && c != '"') { |
49633efb | 290 | if (c == '\\') |
eb27c43f | 291 | c = getc_unlocked(cfile); |
49633efb RM |
292 | *cp++ = c; |
293 | } | |
294 | } else { | |
295 | *cp++ = c; | |
eb27c43f | 296 | while ((c = getc_unlocked(cfile)) != EOF |
49633efb RM |
297 | && c != '\n' && c != '\t' && c != ' ' && c != ',') { |
298 | if (c == '\\') | |
eb27c43f | 299 | c = getc_unlocked(cfile); |
49633efb RM |
300 | *cp++ = c; |
301 | } | |
302 | } | |
303 | *cp = 0; | |
304 | if (tokval[0] == 0) | |
305 | return (0); | |
390500b1 UD |
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; | |
49633efb RM |
309 | return (ID); |
310 | } |