1 /* $FreeBSD: src/usr.bin/ftp/ruserpass.c,v 1.9 1999/08/28 01:01:34 peter Exp $ */
2 /* $NetBSD: ruserpass.c,v 1.14.2.1 1997/11/18 01:02:05 mellon Exp $ */
5 * Copyright (c) 1985, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
40 static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
42 __RCSID("$FreeBSD: src/usr.bin/ftp/ruserpass.c,v 1.9 1999/08/28 01:01:34 peter Exp $");
43 __RCSID_SOURCE("$NetBSD: ruserpass.c,v 1.14.2.1 1997/11/18 01:02:05 mellon Exp $");
47 #include <sys/types.h>
60 static int token __P((void));
71 static char tokval[100];
73 static struct toktab {
77 { "default", DEFAULT },
79 { "password", PASSWD },
81 { "account", ACCOUNT },
88 ruserpass(host, aname, apass, aacct)
90 char **aname, **apass, **aacct;
92 char *hdir, buf[BUFSIZ], *tmp;
93 char myname[MAXHOSTNAMELEN], *mydomain;
94 int t, i, c, usedefault = 0;
97 hdir = getenv("HOME");
100 if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
101 (void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
103 warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
106 cfile = fopen(buf, "r");
112 if (gethostname(myname, sizeof(myname)) < 0)
114 if ((mydomain = strchr(myname, '.')) == NULL)
117 while ((t = token())) switch(t) {
128 * Allow match either for user's input host name
129 * or official hostname. Also allow match of
130 * incompletely-specified host in local domain.
132 if (strcasecmp(host, tokval) == 0)
134 if (strcasecmp(hostname, tokval) == 0)
136 if ((tmp = strchr(hostname, '.')) != NULL &&
137 strcasecmp(tmp, mydomain) == 0 &&
138 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
139 tokval[tmp - hostname] == '\0')
141 if ((tmp = strchr(host, '.')) != NULL &&
142 strcasecmp(tmp, mydomain) == 0 &&
143 strncasecmp(host, tokval, tmp - host) == 0 &&
144 tokval[tmp - host] == '\0')
149 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
153 if (*aname == NULL) {
154 *aname = strdup(tokval);
156 err(1, "can't strdup *aname");
158 if (strcmp(*aname, tokval))
164 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
165 fstat(fileno(cfile), &stb) >= 0 &&
166 (stb.st_mode & 077) != 0) {
167 warnx("Error: .netrc file is readable by others.");
168 warnx("Remove password or make file unreadable by others.");
171 if (token() && *apass == NULL) {
172 *apass = strdup(tokval);
174 err(1, "can't strdup *apass");
178 if (fstat(fileno(cfile), &stb) >= 0
179 && (stb.st_mode & 077) != 0) {
180 warnx("Error: .netrc file is readable by others.");
181 warnx("Remove account or make file unreadable by others.");
184 if (token() && *aacct == NULL) {
185 *aacct = strdup(tokval);
187 err(1, "can't strdup *aacct");
195 while ((c=getc(cfile)) != EOF)
196 if (c != ' ' && c != '\t')
198 if (c == EOF || c == '\n') {
199 puts("Missing macdef name argument.");
204 "Limit of 16 macros have already been defined.");
207 tmp = macros[macnum].mac_name;
209 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
210 (!isascii(c) || !isspace(c)); ++i) {
215 "Macro definition missing null line terminator.");
220 while ((c=getc(cfile)) != EOF && c != '\n');
224 "Macro definition missing null line terminator.");
228 macros[macnum].mac_start = macbuf;
231 macros[macnum].mac_start =
232 macros[macnum-1].mac_end + 1;
234 tmp = macros[macnum].mac_start;
235 while (tmp != macbuf + 4096) {
236 if ((c=getc(cfile)) == EOF) {
238 "Macro definition missing null line terminator.");
243 if (*(tmp-1) == '\0') {
244 macros[macnum++].mac_end = tmp - 1;
251 if (tmp == macbuf + 4096) {
252 puts("4K macro buffer exceeded.");
257 warnx("Unknown .netrc keyword %s", tokval);
277 if (feof(cfile) || ferror(cfile))
279 while ((c = getc(cfile)) != EOF &&
280 (c == '\n' || c == '\t' || c == ' ' || c == ','))
286 while ((c = getc(cfile)) != EOF && c != '"') {
293 while ((c = getc(cfile)) != EOF
294 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
303 for (t = toktab; t->tokstr; t++)
304 if (!strcmp(t->tokstr, tokval))