xref: /netbsd-src/usr.bin/ftp/ruserpass.c (revision c41a4eebefede43f6950f838a387dc18c6a431bf)
1 /*	$NetBSD: ruserpass.c,v 1.15 1997/11/01 14:37:03 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.15 1997/11/01 14:37:03 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 == NULL) {
152 					*aname = strdup(tokval);
153 					if (*aname == NULL)
154 						err(1, "can't strdup *aname");
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 == NULL) {
169 				*apass = strdup(tokval);
170 				if (*apass == NULL)
171 					err(1, "can't strdup *apass");
172 			}
173 			break;
174 		case ACCOUNT:
175 			if (fstat(fileno(cfile), &stb) >= 0
176 			    && (stb.st_mode & 077) != 0) {
177 	warnx("Error: .netrc file is readable by others.");
178 	warnx("Remove account or make file unreadable by others.");
179 				goto bad;
180 			}
181 			if (token() && *aacct == NULL) {
182 				*aacct = strdup(tokval);
183 				if (*aacct == NULL)
184 					err(1, "can't strdup *aacct");
185 			}
186 			break;
187 		case MACDEF:
188 			if (proxy) {
189 				(void)fclose(cfile);
190 				return (0);
191 			}
192 			while ((c=getc(cfile)) != EOF)
193 				if (c != ' ' && c != '\t')
194 					break;
195 			if (c == EOF || c == '\n') {
196 				puts("Missing macdef name argument.");
197 				goto bad;
198 			}
199 			if (macnum == 16) {
200 				puts(
201 "Limit of 16 macros have already been defined.");
202 				goto bad;
203 			}
204 			tmp = macros[macnum].mac_name;
205 			*tmp++ = c;
206 			for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
207 			    !isspace(c); ++i) {
208 				*tmp++ = c;
209 			}
210 			if (c == EOF) {
211 				puts(
212 "Macro definition missing null line terminator.");
213 				goto bad;
214 			}
215 			*tmp = '\0';
216 			if (c != '\n') {
217 				while ((c=getc(cfile)) != EOF && c != '\n');
218 			}
219 			if (c == EOF) {
220 				puts(
221 "Macro definition missing null line terminator.");
222 				goto bad;
223 			}
224 			if (macnum == 0) {
225 				macros[macnum].mac_start = macbuf;
226 			}
227 			else {
228 				macros[macnum].mac_start =
229 				    macros[macnum-1].mac_end + 1;
230 			}
231 			tmp = macros[macnum].mac_start;
232 			while (tmp != macbuf + 4096) {
233 				if ((c=getc(cfile)) == EOF) {
234 				puts(
235 "Macro definition missing null line terminator.");
236 					goto bad;
237 				}
238 				*tmp = c;
239 				if (*tmp == '\n') {
240 					if (*(tmp-1) == '\0') {
241 					   macros[macnum++].mac_end = tmp - 1;
242 					   break;
243 					}
244 					*tmp = '\0';
245 				}
246 				tmp++;
247 			}
248 			if (tmp == macbuf + 4096) {
249 				puts("4K macro buffer exceeded.");
250 				goto bad;
251 			}
252 			break;
253 		default:
254 			warnx("Unknown .netrc keyword %s", tokval);
255 			break;
256 		}
257 		goto done;
258 	}
259 done:
260 	(void)fclose(cfile);
261 	return (0);
262 bad:
263 	(void)fclose(cfile);
264 	return (-1);
265 }
266 
267 static int
268 token()
269 {
270 	char *cp;
271 	int c;
272 	struct toktab *t;
273 
274 	if (feof(cfile) || ferror(cfile))
275 		return (0);
276 	while ((c = getc(cfile)) != EOF &&
277 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
278 		continue;
279 	if (c == EOF)
280 		return (0);
281 	cp = tokval;
282 	if (c == '"') {
283 		while ((c = getc(cfile)) != EOF && c != '"') {
284 			if (c == '\\')
285 				c = getc(cfile);
286 			*cp++ = c;
287 		}
288 	} else {
289 		*cp++ = c;
290 		while ((c = getc(cfile)) != EOF
291 		    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
292 			if (c == '\\')
293 				c = getc(cfile);
294 			*cp++ = c;
295 		}
296 	}
297 	*cp = 0;
298 	if (tokval[0] == 0)
299 		return (0);
300 	for (t = toktab; t->tokstr; t++)
301 		if (!strcmp(t->tokstr, tokval))
302 			return (t->tval);
303 	return (ID);
304 }
305