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