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.2 2003/06/17 04:29:26 dillon 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 __P((void));
68 static char tokval[100];
70 static struct toktab {
74 { "default", DEFAULT },
76 { "password", PASSWD },
78 { "account", ACCOUNT },
85 ruserpass(host, aname, apass, aacct)
87 char **aname, **apass, **aacct;
89 char *hdir, buf[BUFSIZ], *tmp;
90 char myname[MAXHOSTNAMELEN], *mydomain;
91 int t, i, c, usedefault = 0;
94 hdir = getenv("HOME");
97 if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
98 (void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
100 warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
103 cfile = fopen(buf, "r");
109 if (gethostname(myname, sizeof(myname)) < 0)
111 if ((mydomain = strchr(myname, '.')) == NULL)
114 while ((t = token())) switch(t) {
125 * Allow match either for user's input host name
126 * or official hostname. Also allow match of
127 * incompletely-specified host in local domain.
129 if (strcasecmp(host, tokval) == 0)
131 if (strcasecmp(hostname, tokval) == 0)
133 if ((tmp = strchr(hostname, '.')) != NULL &&
134 strcasecmp(tmp, mydomain) == 0 &&
135 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
136 tokval[tmp - hostname] == '\0')
138 if ((tmp = strchr(host, '.')) != NULL &&
139 strcasecmp(tmp, mydomain) == 0 &&
140 strncasecmp(host, tokval, tmp - host) == 0 &&
141 tokval[tmp - host] == '\0')
146 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
150 if (*aname == NULL) {
151 *aname = strdup(tokval);
153 err(1, "can't strdup *aname");
155 if (strcmp(*aname, tokval))
161 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
162 fstat(fileno(cfile), &stb) >= 0 &&
163 (stb.st_mode & 077) != 0) {
164 warnx("Error: .netrc file is readable by others.");
165 warnx("Remove password or make file unreadable by others.");
168 if (token() && *apass == NULL) {
169 *apass = strdup(tokval);
171 err(1, "can't strdup *apass");
175 if (fstat(fileno(cfile), &stb) >= 0
176 && (stb.st_mode & 077) != 0) {
177 warnx("Error: .netrc file is readable by others.");
178 warnx("Remove account or make file unreadable by others.");
181 if (token() && *aacct == NULL) {
182 *aacct = strdup(tokval);
184 err(1, "can't strdup *aacct");
192 while ((c=getc(cfile)) != EOF)
193 if (c != ' ' && c != '\t')
195 if (c == EOF || c == '\n') {
196 puts("Missing macdef name argument.");
201 "Limit of 16 macros have already been defined.");
204 tmp = macros[macnum].mac_name;
206 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
207 (!isascii(c) || !isspace(c)); ++i) {
212 "Macro definition missing null line terminator.");
217 while ((c=getc(cfile)) != EOF && c != '\n');
221 "Macro definition missing null line terminator.");
225 macros[macnum].mac_start = macbuf;
228 macros[macnum].mac_start =
229 macros[macnum-1].mac_end + 1;
231 tmp = macros[macnum].mac_start;
232 while (tmp != macbuf + 4096) {
233 if ((c=getc(cfile)) == EOF) {
235 "Macro definition missing null line terminator.");
240 if (*(tmp-1) == '\0') {
241 macros[macnum++].mac_end = tmp - 1;
248 if (tmp == macbuf + 4096) {
249 puts("4K macro buffer exceeded.");
254 warnx("Unknown .netrc keyword %s", tokval);
274 if (feof(cfile) || ferror(cfile))
276 while ((c = getc(cfile)) != EOF &&
277 (c == '\n' || c == '\t' || c == ' ' || c == ','))
283 while ((c = getc(cfile)) != EOF && c != '"') {
290 while ((c = getc(cfile)) != EOF
291 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
300 for (t = toktab; t->tokstr; t++)
301 if (!strcmp(t->tokstr, tokval))