Annotation of src/usr.bin/ftp/ruserpass.c, Revision 1.21
1.21 ! moritz 1: /* $OpenBSD: ruserpass.c,v 1.20 2006/05/16 23:43:16 ray 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
1.21 ! moritz 38: static const char rcsid[] = "$OpenBSD: ruserpass.c,v 1.20 2006/05/16 23:43:16 ray Exp $";
1.17 deraadt 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
1.18 deraadt 84: ruserpass(const char *host, char **aname, char **apass, char **aacct)
1.1 deraadt 85: {
86: char *hdir, buf[BUFSIZ], *tmp;
87: char myname[MAXHOSTNAMELEN], *mydomain;
88: int t, i, c, usedefault = 0;
89: struct stat stb;
90:
91: hdir = getenv("HOME");
1.14 millert 92: if (hdir == NULL || *hdir == '\0')
1.12 millert 93: return (0);
1.4 millert 94: if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
1.15 deraadt 95: (void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
1.3 millert 96: } else {
97: warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
98: return (0);
99: }
1.1 deraadt 100: cfile = fopen(buf, "r");
101: if (cfile == NULL) {
102: if (errno != ENOENT)
103: warn("%s", buf);
104: return (0);
105: }
106: if (gethostname(myname, sizeof(myname)) < 0)
107: myname[0] = '\0';
108: if ((mydomain = strchr(myname, '.')) == NULL)
109: mydomain = "";
110: next:
1.21 ! moritz 111: while ((t = token()) > 0) switch(t) {
1.1 deraadt 112:
113: case DEFAULT:
114: usedefault = 1;
1.20 ray 115: /* FALLTHROUGH */
1.1 deraadt 116:
117: case MACH:
118: if (!usedefault) {
1.21 ! moritz 119: if ((t = token()) == -1)
! 120: goto bad;
! 121: if (t != ID)
1.1 deraadt 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:
1.21 ! moritz 147: while ((t = token()) > 0 &&
! 148: t != MACH && t != DEFAULT) switch(t) {
1.1 deraadt 149:
150: case LOGIN:
1.21 ! moritz 151: if ((t = token()) == -1)
! 152: goto bad;
! 153: if (t) {
1.15 deraadt 154: if (*aname == 0)
155: *aname = strdup(tokval);
156: else {
1.1 deraadt 157: if (strcmp(*aname, tokval))
158: goto next;
159: }
1.11 deraadt 160: }
1.1 deraadt 161: break;
162: case PASSWD:
163: if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
164: fstat(fileno(cfile), &stb) >= 0 &&
165: (stb.st_mode & 077) != 0) {
166: warnx("Error: .netrc file is readable by others.");
167: warnx("Remove password or make file unreadable by others.");
168: goto bad;
169: }
1.21 ! moritz 170: if ((t = token()) == -1)
! 171: goto bad;
! 172: if (t && *apass == 0)
1.15 deraadt 173: *apass = strdup(tokval);
1.1 deraadt 174: break;
175: case ACCOUNT:
176: if (fstat(fileno(cfile), &stb) >= 0
177: && (stb.st_mode & 077) != 0) {
178: warnx("Error: .netrc file is readable by others.");
179: warnx("Remove account or make file unreadable by others.");
180: goto bad;
181: }
1.21 ! moritz 182: if ((t = token()) == -1)
! 183: goto bad;
! 184: if (t && *aacct == 0)
1.15 deraadt 185: *aacct = strdup(tokval);
1.1 deraadt 186: break;
187: case MACDEF:
188: if (proxy) {
1.5 millert 189: (void)fclose(cfile);
1.1 deraadt 190: return (0);
191: }
1.10 millert 192: while ((c = fgetc(cfile)) != EOF)
1.4 millert 193: if (c != ' ' && c != '\t')
194: break;
1.1 deraadt 195: if (c == EOF || c == '\n') {
1.8 deraadt 196: fputs("Missing macdef name argument.\n", ttyout);
1.1 deraadt 197: goto bad;
198: }
199: if (macnum == 16) {
1.8 deraadt 200: fputs(
201: "Limit of 16 macros have already been defined.\n", ttyout);
1.1 deraadt 202: goto bad;
203: }
204: tmp = macros[macnum].mac_name;
205: *tmp++ = c;
1.10 millert 206: for (i=0; i < 8 && (c = fgetc(cfile)) != EOF &&
1.1 deraadt 207: !isspace(c); ++i) {
208: *tmp++ = c;
209: }
210: if (c == EOF) {
1.8 deraadt 211: fputs(
212: "Macro definition missing null line terminator.\n", ttyout);
1.1 deraadt 213: goto bad;
214: }
215: *tmp = '\0';
216: if (c != '\n') {
1.10 millert 217: while ((c = fgetc(cfile)) != EOF && c != '\n');
1.1 deraadt 218: }
219: if (c == EOF) {
1.8 deraadt 220: fputs(
221: "Macro definition missing null line terminator.\n", ttyout);
1.1 deraadt 222: goto bad;
223: }
224: if (macnum == 0) {
225: macros[macnum].mac_start = macbuf;
226: }
227: else {
1.4 millert 228: macros[macnum].mac_start =
229: macros[macnum-1].mac_end + 1;
1.1 deraadt 230: }
231: tmp = macros[macnum].mac_start;
232: while (tmp != macbuf + 4096) {
1.10 millert 233: if ((c = fgetc(cfile)) == EOF) {
1.8 deraadt 234: fputs(
235: "Macro definition missing null line terminator.\n", ttyout);
1.1 deraadt 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) {
1.8 deraadt 249: fputs("4K macro buffer exceeded.\n", ttyout);
1.1 deraadt 250: goto bad;
251: }
252: break;
253: default:
254: warnx("Unknown .netrc keyword %s", tokval);
255: break;
256: }
257: goto done;
258: }
259: done:
1.21 ! moritz 260: if (t == -1)
! 261: goto bad;
1.5 millert 262: (void)fclose(cfile);
1.1 deraadt 263: return (0);
264: bad:
1.5 millert 265: (void)fclose(cfile);
1.1 deraadt 266: return (-1);
267: }
268:
269: static int
1.18 deraadt 270: token(void)
1.1 deraadt 271: {
272: char *cp;
273: int c;
274: struct toktab *t;
275:
276: if (feof(cfile) || ferror(cfile))
277: return (0);
1.10 millert 278: while ((c = fgetc(cfile)) != EOF &&
1.1 deraadt 279: (c == '\n' || c == '\t' || c == ' ' || c == ','))
280: continue;
281: if (c == EOF)
282: return (0);
283: cp = tokval;
284: if (c == '"') {
1.10 millert 285: while ((c = fgetc(cfile)) != EOF && c != '"') {
1.1 deraadt 286: if (c == '\\')
1.10 millert 287: c = fgetc(cfile);
1.1 deraadt 288: *cp++ = c;
1.21 ! moritz 289: if (cp == tokval + sizeof(tokval)) {
! 290: warnx("Token in .netrc too long");
! 291: return (-1);
! 292: }
1.1 deraadt 293: }
294: } else {
295: *cp++ = c;
1.10 millert 296: while ((c = fgetc(cfile)) != EOF
1.1 deraadt 297: && c != '\n' && c != '\t' && c != ' ' && c != ',') {
298: if (c == '\\')
1.10 millert 299: c = fgetc(cfile);
1.1 deraadt 300: *cp++ = c;
1.21 ! moritz 301: if (cp == tokval + sizeof(tokval)) {
! 302: warnx("Token in .netrc too long");
! 303: return (-1);
! 304: }
1.1 deraadt 305: }
306: }
307: *cp = 0;
308: if (tokval[0] == 0)
309: return (0);
310: for (t = toktab; t->tokstr; t++)
311: if (!strcmp(t->tokstr, tokval))
312: return (t->tval);
313: return (ID);
314: }