1 /* $FreeBSD: src/usr.bin/ftp/ruserpass.c,v 1.9 1999/08/28 01:01:34 peter Exp $ */
2 /* $DragonFly: src/usr.bin/ftp/Attic/ruserpass.c,v 1.3 2003/10/04 20:36:45 hmp Exp $ */
3 /* $NetBSD: ruserpass.c,v 1.14.2.1 1997/11/18 01:02:05 mellon Exp $ */
6 * Copyright (c) 1985, 1993, 1994
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)ruserpass.c 8.4 (Berkeley) 4/27/95
38 * $NetBSD: ruserpass.c,v 1.14.2.1 1997/11/18 01:02:05 mellon Exp $
39 * $FreeBSD: src/usr.bin/ftp/ruserpass.c,v 1.9 1999/08/28 01:01:34 peter Exp $
42 #include <sys/cdefs.h>
44 #include <sys/types.h>
57 static int token(void);
68 static char tokval[100];
70 static struct toktab {
74 { "default", DEFAULT },
76 { "password", PASSWD },
78 { "account", ACCOUNT },
85 ruserpass(const char *host, char **aname, char **apass, char **aacct)
87 char *hdir, buf[BUFSIZ], *tmp;
88 char myname[MAXHOSTNAMELEN], *mydomain;
89 int t, i, c, usedefault = 0;
92 hdir = getenv("HOME");
95 if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
96 (void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
98 warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
101 cfile = fopen(buf, "r");
107 if (gethostname(myname, sizeof(myname)) < 0)
109 if ((mydomain = strchr(myname, '.')) == NULL)
112 while ((t = token())) switch(t) {
123 * Allow match either for user's input host name
124 * or official hostname. Also allow match of
125 * incompletely-specified host in local domain.
127 if (strcasecmp(host, tokval) == 0)
129 if (strcasecmp(hostname, tokval) == 0)
131 if ((tmp = strchr(hostname, '.')) != NULL &&
132 strcasecmp(tmp, mydomain) == 0 &&
133 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
134 tokval[tmp - hostname] == '\0')
136 if ((tmp = strchr(host, '.')) != NULL &&
137 strcasecmp(tmp, mydomain) == 0 &&
138 strncasecmp(host, tokval, tmp - host) == 0 &&
139 tokval[tmp - host] == '\0')
144 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
148 if (*aname == NULL) {
149 *aname = strdup(tokval);
151 err(1, "can't strdup *aname");
153 if (strcmp(*aname, tokval))
159 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
160 fstat(fileno(cfile), &stb) >= 0 &&
161 (stb.st_mode & 077) != 0) {
162 warnx("Error: .netrc file is readable by others.");
163 warnx("Remove password or make file unreadable by others.");
166 if (token() && *apass == NULL) {
167 *apass = strdup(tokval);
169 err(1, "can't strdup *apass");
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.");
179 if (token() && *aacct == NULL) {
180 *aacct = strdup(tokval);
182 err(1, "can't strdup *aacct");
190 while ((c=getc(cfile)) != EOF)
191 if (c != ' ' && c != '\t')
193 if (c == EOF || c == '\n') {
194 puts("Missing macdef name argument.");
199 "Limit of 16 macros have already been defined.");
202 tmp = macros[macnum].mac_name;
204 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
205 (!isascii(c) || !isspace(c)); ++i) {
210 "Macro definition missing null line terminator.");
215 while ((c=getc(cfile)) != EOF && c != '\n');
219 "Macro definition missing null line terminator.");
223 macros[macnum].mac_start = macbuf;
226 macros[macnum].mac_start =
227 macros[macnum-1].mac_end + 1;
229 tmp = macros[macnum].mac_start;
230 while (tmp != macbuf + 4096) {
231 if ((c=getc(cfile)) == EOF) {
233 "Macro definition missing null line terminator.");
238 if (*(tmp-1) == '\0') {
239 macros[macnum++].mac_end = tmp - 1;
246 if (tmp == macbuf + 4096) {
247 puts("4K macro buffer exceeded.");
252 warnx("Unknown .netrc keyword %s", tokval);
272 if (feof(cfile) || ferror(cfile))
274 while ((c = getc(cfile)) != EOF &&
275 (c == '\n' || c == '\t' || c == ' ' || c == ','))
281 while ((c = getc(cfile)) != EOF && c != '"') {
288 while ((c = getc(cfile)) != EOF
289 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
298 for (t = toktab; t->tokstr; t++)
299 if (!strcmp(t->tokstr, tokval))