ruserpass.c revision 1.14 1 /* $NetBSD: ruserpass.c,v 1.14 1997/07/20 09:46:01 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1985, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
40 #else
41 __RCSID("$NetBSD: ruserpass.c,v 1.14 1997/07/20 09:46:01 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "ftp_var.h"
57
58 static int token __P((void));
59 static FILE *cfile;
60
61 #define DEFAULT 1
62 #define LOGIN 2
63 #define PASSWD 3
64 #define ACCOUNT 4
65 #define MACDEF 5
66 #define ID 10
67 #define MACH 11
68
69 static char tokval[100];
70
71 static struct toktab {
72 char *tokstr;
73 int tval;
74 } toktab[]= {
75 { "default", DEFAULT },
76 { "login", LOGIN },
77 { "password", PASSWD },
78 { "passwd", PASSWD },
79 { "account", ACCOUNT },
80 { "machine", MACH },
81 { "macdef", MACDEF },
82 { NULL, 0 }
83 };
84
85 int
86 ruserpass(host, aname, apass, aacct)
87 const char *host;
88 char **aname, **apass, **aacct;
89 {
90 char *hdir, buf[BUFSIZ], *tmp;
91 char myname[MAXHOSTNAMELEN], *mydomain;
92 int t, i, c, usedefault = 0;
93 struct stat stb;
94
95 hdir = getenv("HOME");
96 if (hdir == NULL)
97 hdir = ".";
98 if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
99 (void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
100 } else {
101 warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
102 return (0);
103 }
104 cfile = fopen(buf, "r");
105 if (cfile == NULL) {
106 if (errno != ENOENT)
107 warn("%s", buf);
108 return (0);
109 }
110 if (gethostname(myname, sizeof(myname)) < 0)
111 myname[0] = '\0';
112 if ((mydomain = strchr(myname, '.')) == NULL)
113 mydomain = "";
114 next:
115 while ((t = token())) switch(t) {
116
117 case DEFAULT:
118 usedefault = 1;
119 /* FALL THROUGH */
120
121 case MACH:
122 if (!usedefault) {
123 if (token() != ID)
124 continue;
125 /*
126 * Allow match either for user's input host name
127 * or official hostname. Also allow match of
128 * incompletely-specified host in local domain.
129 */
130 if (strcasecmp(host, tokval) == 0)
131 goto match;
132 if (strcasecmp(hostname, tokval) == 0)
133 goto match;
134 if ((tmp = strchr(hostname, '.')) != NULL &&
135 strcasecmp(tmp, mydomain) == 0 &&
136 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
137 tokval[tmp - hostname] == '\0')
138 goto match;
139 if ((tmp = strchr(host, '.')) != NULL &&
140 strcasecmp(tmp, mydomain) == 0 &&
141 strncasecmp(host, tokval, tmp - host) == 0 &&
142 tokval[tmp - host] == '\0')
143 goto match;
144 continue;
145 }
146 match:
147 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
148
149 case LOGIN:
150 if (token())
151 if (*aname == 0) {
152 *aname = malloc((unsigned)
153 strlen(tokval) + 1);
154 (void)strcpy(*aname, tokval);
155 } else {
156 if (strcmp(*aname, tokval))
157 goto next;
158 }
159 break;
160 case PASSWD:
161 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
162 fstat(fileno(cfile), &stb) >= 0 &&
163 (stb.st_mode & 077) != 0) {
164 warnx("Error: .netrc file is readable by others.");
165 warnx("Remove password or make file unreadable by others.");
166 goto bad;
167 }
168 if (token() && *apass == 0) {
169 *apass = malloc((unsigned) strlen(tokval) + 1);
170 (void)strcpy(*apass, tokval);
171 }
172 break;
173 case ACCOUNT:
174 if (fstat(fileno(cfile), &stb) >= 0
175 && (stb.st_mode & 077) != 0) {
176 warnx("Error: .netrc file is readable by others.");
177 warnx("Remove account or make file unreadable by others.");
178 goto bad;
179 }
180 if (token() && *aacct == 0) {
181 *aacct = malloc((unsigned) strlen(tokval) + 1);
182 (void)strcpy(*aacct, tokval);
183 }
184 break;
185 case MACDEF:
186 if (proxy) {
187 (void)fclose(cfile);
188 return (0);
189 }
190 while ((c=getc(cfile)) != EOF)
191 if (c != ' ' && c != '\t')
192 break;
193 if (c == EOF || c == '\n') {
194 puts("Missing macdef name argument.");
195 goto bad;
196 }
197 if (macnum == 16) {
198 puts(
199 "Limit of 16 macros have already been defined.");
200 goto bad;
201 }
202 tmp = macros[macnum].mac_name;
203 *tmp++ = c;
204 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
205 !isspace(c); ++i) {
206 *tmp++ = c;
207 }
208 if (c == EOF) {
209 puts(
210 "Macro definition missing null line terminator.");
211 goto bad;
212 }
213 *tmp = '\0';
214 if (c != '\n') {
215 while ((c=getc(cfile)) != EOF && c != '\n');
216 }
217 if (c == EOF) {
218 puts(
219 "Macro definition missing null line terminator.");
220 goto bad;
221 }
222 if (macnum == 0) {
223 macros[macnum].mac_start = macbuf;
224 }
225 else {
226 macros[macnum].mac_start =
227 macros[macnum-1].mac_end + 1;
228 }
229 tmp = macros[macnum].mac_start;
230 while (tmp != macbuf + 4096) {
231 if ((c=getc(cfile)) == EOF) {
232 puts(
233 "Macro definition missing null line terminator.");
234 goto bad;
235 }
236 *tmp = c;
237 if (*tmp == '\n') {
238 if (*(tmp-1) == '\0') {
239 macros[macnum++].mac_end = tmp - 1;
240 break;
241 }
242 *tmp = '\0';
243 }
244 tmp++;
245 }
246 if (tmp == macbuf + 4096) {
247 puts("4K macro buffer exceeded.");
248 goto bad;
249 }
250 break;
251 default:
252 warnx("Unknown .netrc keyword %s", tokval);
253 break;
254 }
255 goto done;
256 }
257 done:
258 (void)fclose(cfile);
259 return (0);
260 bad:
261 (void)fclose(cfile);
262 return (-1);
263 }
264
265 static int
266 token()
267 {
268 char *cp;
269 int c;
270 struct toktab *t;
271
272 if (feof(cfile) || ferror(cfile))
273 return (0);
274 while ((c = getc(cfile)) != EOF &&
275 (c == '\n' || c == '\t' || c == ' ' || c == ','))
276 continue;
277 if (c == EOF)
278 return (0);
279 cp = tokval;
280 if (c == '"') {
281 while ((c = getc(cfile)) != EOF && c != '"') {
282 if (c == '\\')
283 c = getc(cfile);
284 *cp++ = c;
285 }
286 } else {
287 *cp++ = c;
288 while ((c = getc(cfile)) != EOF
289 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
290 if (c == '\\')
291 c = getc(cfile);
292 *cp++ = c;
293 }
294 }
295 *cp = 0;
296 if (tokval[0] == 0)
297 return (0);
298 for (t = toktab; t->tokstr; t++)
299 if (!strcmp(t->tokstr, tokval))
300 return (t->tval);
301 return (ID);
302 }
303