Annotation of src/usr.bin/ftp/ruserpass.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: ruserpass.c,v 1.14 2003/03/31 23:04:07 millert Exp $ */
1.9 millert 2: /* $NetBSD: ruserpass.c,v 1.14 1997/07/20 09:46:01 lukem Exp $ */
1.1 deraadt 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
1.4 millert 38: #if 0
1.1 deraadt 39: static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
1.4 millert 40: #else
1.15 ! deraadt 41: static char rcsid[] = "$OpenBSD: ruserpass.c,v 1.14 2003/03/31 23:04:07 millert Exp $";
1.4 millert 42: #endif
1.1 deraadt 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:
1.13 millert 58: static int token(void);
1.1 deraadt 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)
1.4 millert 87: const char *host;
88: char **aname, **apass, **aacct;
1.1 deraadt 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");
1.14 millert 96: if (hdir == NULL || *hdir == '\0')
1.12 millert 97: return (0);
1.4 millert 98: if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
1.15 ! deraadt 99: (void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
1.3 millert 100: } else {
101: warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
102: return (0);
103: }
1.1 deraadt 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
1.4 millert 127: * or official hostname. Also allow match of
1.1 deraadt 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 &&
1.9 millert 136: strncasecmp(hostname, tokval,
137: (size_t)(tmp - hostname)) == 0 &&
1.1 deraadt 138: tokval[tmp - hostname] == '\0')
139: goto match;
140: if ((tmp = strchr(host, '.')) != NULL &&
141: strcasecmp(tmp, mydomain) == 0 &&
1.9 millert 142: strncasecmp(host, tokval,
143: (size_t)(tmp - host)) == 0 &&
1.1 deraadt 144: tokval[tmp - host] == '\0')
145: goto match;
146: continue;
147: }
148: match:
149: while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
150:
151: case LOGIN:
1.11 deraadt 152: if (token()) {
1.15 ! deraadt 153: if (*aname == 0)
! 154: *aname = strdup(tokval);
! 155: else {
1.1 deraadt 156: if (strcmp(*aname, tokval))
157: goto next;
158: }
1.11 deraadt 159: }
1.1 deraadt 160: break;
161: case PASSWD:
162: if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
163: fstat(fileno(cfile), &stb) >= 0 &&
164: (stb.st_mode & 077) != 0) {
165: warnx("Error: .netrc file is readable by others.");
166: warnx("Remove password or make file unreadable by others.");
167: goto bad;
168: }
1.15 ! deraadt 169: if (token() && *apass == 0)
! 170: *apass = strdup(tokval);
1.1 deraadt 171: break;
172: case ACCOUNT:
173: if (fstat(fileno(cfile), &stb) >= 0
174: && (stb.st_mode & 077) != 0) {
175: warnx("Error: .netrc file is readable by others.");
176: warnx("Remove account or make file unreadable by others.");
177: goto bad;
178: }
1.15 ! deraadt 179: if (token() && *aacct == 0)
! 180: *aacct = strdup(tokval);
1.1 deraadt 181: break;
182: case MACDEF:
183: if (proxy) {
1.5 millert 184: (void)fclose(cfile);
1.1 deraadt 185: return (0);
186: }
1.10 millert 187: while ((c = fgetc(cfile)) != EOF)
1.4 millert 188: if (c != ' ' && c != '\t')
189: break;
1.1 deraadt 190: if (c == EOF || c == '\n') {
1.8 deraadt 191: fputs("Missing macdef name argument.\n", ttyout);
1.1 deraadt 192: goto bad;
193: }
194: if (macnum == 16) {
1.8 deraadt 195: fputs(
196: "Limit of 16 macros have already been defined.\n", ttyout);
1.1 deraadt 197: goto bad;
198: }
199: tmp = macros[macnum].mac_name;
200: *tmp++ = c;
1.10 millert 201: for (i=0; i < 8 && (c = fgetc(cfile)) != EOF &&
1.1 deraadt 202: !isspace(c); ++i) {
203: *tmp++ = c;
204: }
205: if (c == EOF) {
1.8 deraadt 206: fputs(
207: "Macro definition missing null line terminator.\n", ttyout);
1.1 deraadt 208: goto bad;
209: }
210: *tmp = '\0';
211: if (c != '\n') {
1.10 millert 212: while ((c = fgetc(cfile)) != EOF && c != '\n');
1.1 deraadt 213: }
214: if (c == EOF) {
1.8 deraadt 215: fputs(
216: "Macro definition missing null line terminator.\n", ttyout);
1.1 deraadt 217: goto bad;
218: }
219: if (macnum == 0) {
220: macros[macnum].mac_start = macbuf;
221: }
222: else {
1.4 millert 223: macros[macnum].mac_start =
224: macros[macnum-1].mac_end + 1;
1.1 deraadt 225: }
226: tmp = macros[macnum].mac_start;
227: while (tmp != macbuf + 4096) {
1.10 millert 228: if ((c = fgetc(cfile)) == EOF) {
1.8 deraadt 229: fputs(
230: "Macro definition missing null line terminator.\n", ttyout);
1.1 deraadt 231: goto bad;
232: }
233: *tmp = c;
234: if (*tmp == '\n') {
235: if (*(tmp-1) == '\0') {
236: macros[macnum++].mac_end = tmp - 1;
237: break;
238: }
239: *tmp = '\0';
240: }
241: tmp++;
242: }
243: if (tmp == macbuf + 4096) {
1.8 deraadt 244: fputs("4K macro buffer exceeded.\n", ttyout);
1.1 deraadt 245: goto bad;
246: }
247: break;
248: default:
249: warnx("Unknown .netrc keyword %s", tokval);
250: break;
251: }
252: goto done;
253: }
254: done:
1.5 millert 255: (void)fclose(cfile);
1.1 deraadt 256: return (0);
257: bad:
1.5 millert 258: (void)fclose(cfile);
1.1 deraadt 259: return (-1);
260: }
261:
262: static int
263: token()
264: {
265: char *cp;
266: int c;
267: struct toktab *t;
268:
269: if (feof(cfile) || ferror(cfile))
270: return (0);
1.10 millert 271: while ((c = fgetc(cfile)) != EOF &&
1.1 deraadt 272: (c == '\n' || c == '\t' || c == ' ' || c == ','))
273: continue;
274: if (c == EOF)
275: return (0);
276: cp = tokval;
277: if (c == '"') {
1.10 millert 278: while ((c = fgetc(cfile)) != EOF && c != '"') {
1.1 deraadt 279: if (c == '\\')
1.10 millert 280: c = fgetc(cfile);
1.1 deraadt 281: *cp++ = c;
282: }
283: } else {
284: *cp++ = c;
1.10 millert 285: while ((c = fgetc(cfile)) != EOF
1.1 deraadt 286: && c != '\n' && c != '\t' && c != ' ' && c != ',') {
287: if (c == '\\')
1.10 millert 288: c = fgetc(cfile);
1.1 deraadt 289: *cp++ = c;
290: }
291: }
292: *cp = 0;
293: if (tokval[0] == 0)
294: return (0);
295: for (t = toktab; t->tokstr; t++)
296: if (!strcmp(t->tokstr, tokval))
297: return (t->tval);
298: return (ID);
299: }