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