ruserpass.c revision 1.8 1 /* $NetBSD: ruserpass.c,v 1.8 1996/11/28 03:12:42 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 #ifndef lint
37 static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
38 #endif /* not lint */
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "ftp_var.h"
52
53 static int token __P((void));
54 static FILE *cfile;
55
56 #define DEFAULT 1
57 #define LOGIN 2
58 #define PASSWD 3
59 #define ACCOUNT 4
60 #define MACDEF 5
61 #define ID 10
62 #define MACH 11
63
64 static char tokval[100];
65
66 static struct toktab {
67 char *tokstr;
68 int tval;
69 } toktab[]= {
70 { "default", DEFAULT },
71 { "login", LOGIN },
72 { "password", PASSWD },
73 { "passwd", PASSWD },
74 { "account", ACCOUNT },
75 { "machine", MACH },
76 { "macdef", MACDEF },
77 { NULL, 0 }
78 };
79
80 int
81 ruserpass(host, aname, apass, aacct)
82 const char *host;
83 char **aname, **apass, **aacct;
84 {
85 char *hdir, buf[BUFSIZ], *tmp;
86 char myname[MAXHOSTNAMELEN], *mydomain;
87 int t, i, c, usedefault = 0;
88 struct stat stb;
89
90 hdir = getenv("HOME");
91 if (hdir == NULL)
92 hdir = ".";
93 if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
94 (void) sprintf(buf, "%s/.netrc", hdir);
95 } else {
96 warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
97 return (0);
98 }
99 cfile = fopen(buf, "r");
100 if (cfile == NULL) {
101 if (errno != ENOENT)
102 warn("%s", buf);
103 return (0);
104 }
105 if (gethostname(myname, sizeof(myname)) < 0)
106 myname[0] = '\0';
107 if ((mydomain = strchr(myname, '.')) == NULL)
108 mydomain = "";
109 next:
110 while ((t = token())) switch(t) {
111
112 case DEFAULT:
113 usedefault = 1;
114 /* FALL THROUGH */
115
116 case MACH:
117 if (!usedefault) {
118 if (token() != ID)
119 continue;
120 /*
121 * Allow match either for user's input host name
122 * or official hostname. Also allow match of
123 * incompletely-specified host in local domain.
124 */
125 if (strcasecmp(host, tokval) == 0)
126 goto match;
127 if (strcasecmp(hostname, tokval) == 0)
128 goto match;
129 if ((tmp = strchr(hostname, '.')) != NULL &&
130 strcasecmp(tmp, mydomain) == 0 &&
131 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
132 tokval[tmp - hostname] == '\0')
133 goto match;
134 if ((tmp = strchr(host, '.')) != NULL &&
135 strcasecmp(tmp, mydomain) == 0 &&
136 strncasecmp(host, tokval, tmp - host) == 0 &&
137 tokval[tmp - host] == '\0')
138 goto match;
139 continue;
140 }
141 match:
142 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
143
144 case LOGIN:
145 if (token())
146 if (*aname == 0) {
147 *aname = malloc((unsigned)
148 strlen(tokval) + 1);
149 (void) strcpy(*aname, tokval);
150 } else {
151 if (strcmp(*aname, tokval))
152 goto next;
153 }
154 break;
155 case PASSWD:
156 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
157 fstat(fileno(cfile), &stb) >= 0 &&
158 (stb.st_mode & 077) != 0) {
159 warnx("Error: .netrc file is readable by others.");
160 warnx("Remove password or make file unreadable by others.");
161 goto bad;
162 }
163 if (token() && *apass == 0) {
164 *apass = malloc((unsigned) strlen(tokval) + 1);
165 (void) strcpy(*apass, tokval);
166 }
167 break;
168 case ACCOUNT:
169 if (fstat(fileno(cfile), &stb) >= 0
170 && (stb.st_mode & 077) != 0) {
171 warnx("Error: .netrc file is readable by others.");
172 warnx("Remove account or make file unreadable by others.");
173 goto bad;
174 }
175 if (token() && *aacct == 0) {
176 *aacct = malloc((unsigned) strlen(tokval) + 1);
177 (void) strcpy(*aacct, tokval);
178 }
179 break;
180 case MACDEF:
181 if (proxy) {
182 (void) fclose(cfile);
183 return (0);
184 }
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;
189 }
190 if (macnum == 16) {
191 printf(
192 "Limit of 16 macros have already been defined\n");
193 goto bad;
194 }
195 tmp = macros[macnum].mac_name;
196 *tmp++ = c;
197 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
198 !isspace(c); ++i) {
199 *tmp++ = c;
200 }
201 if (c == EOF) {
202 printf(
203 "Macro definition missing null line terminator.\n");
204 goto bad;
205 }
206 *tmp = '\0';
207 if (c != '\n') {
208 while ((c=getc(cfile)) != EOF && c != '\n');
209 }
210 if (c == EOF) {
211 printf(
212 "Macro definition missing null line terminator.\n");
213 goto bad;
214 }
215 if (macnum == 0) {
216 macros[macnum].mac_start = macbuf;
217 }
218 else {
219 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
220 }
221 tmp = macros[macnum].mac_start;
222 while (tmp != macbuf + 4096) {
223 if ((c=getc(cfile)) == EOF) {
224 printf(
225 "Macro definition missing null line terminator.\n");
226 goto bad;
227 }
228 *tmp = c;
229 if (*tmp == '\n') {
230 if (*(tmp-1) == '\0') {
231 macros[macnum++].mac_end = tmp - 1;
232 break;
233 }
234 *tmp = '\0';
235 }
236 tmp++;
237 }
238 if (tmp == macbuf + 4096) {
239 printf("4K macro buffer exceeded\n");
240 goto bad;
241 }
242 break;
243 default:
244 warnx("Unknown .netrc keyword %s", tokval);
245 break;
246 }
247 goto done;
248 }
249 done:
250 (void) fclose(cfile);
251 return (0);
252 bad:
253 (void) fclose(cfile);
254 return (-1);
255 }
256
257 static int
258 token()
259 {
260 char *cp;
261 int c;
262 struct toktab *t;
263
264 if (feof(cfile) || ferror(cfile))
265 return (0);
266 while ((c = getc(cfile)) != EOF &&
267 (c == '\n' || c == '\t' || c == ' ' || c == ','))
268 continue;
269 if (c == EOF)
270 return (0);
271 cp = tokval;
272 if (c == '"') {
273 while ((c = getc(cfile)) != EOF && c != '"') {
274 if (c == '\\')
275 c = getc(cfile);
276 *cp++ = c;
277 }
278 } else {
279 *cp++ = c;
280 while ((c = getc(cfile)) != EOF
281 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
282 if (c == '\\')
283 c = getc(cfile);
284 *cp++ = c;
285 }
286 }
287 *cp = 0;
288 if (tokval[0] == 0)
289 return (0);
290 for (t = toktab; t->tokstr; t++)
291 if (!strcmp(t->tokstr, tokval))
292 return (t->tval);
293 return (ID);
294 }
295