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";
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))
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.");
170 if (token() && *apass == NULL) {
171 *apass = strdup(tokval);
173 err(1, "can't strdup *apass");
177 if (fstat(fileno(cfile), &stb) >= 0
178 && (stb.st_mode & 077) != 0) {
179 warnx("Error: .netrc file is readable by others.");
180 warnx("Remove account or make file unreadable by others.");
183 if (token() && *aacct == NULL) {
184 *aacct = strdup(tokval);
186 err(1, "can't strdup *aacct");
194 while ((c=getc(cfile)) != EOF)
195 if (c != ' ' && c != '\t')
197 if (c == EOF || c == '\n') {
198 puts("Missing macdef name argument.");
203 "Limit of 16 macros have already been defined.");
206 tmp = macros[macnum].mac_name;
208 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
209 (!isascii(c) || !isspace(c)); ++i) {
214 "Macro definition missing null line terminator.");
219 while ((c=getc(cfile)) != EOF && c != '\n');
223 "Macro definition missing null line terminator.");
227 macros[macnum].mac_start = macbuf;
230 macros[macnum].mac_start =
231 macros[macnum-1].mac_end + 1;
233 tmp = macros[macnum].mac_start;
234 while (tmp != macbuf + 4096) {
235 if ((c=getc(cfile)) == EOF) {
237 "Macro definition missing null line terminator.");
242 if (*(tmp-1) == '\0') {
243 macros[macnum++].mac_end = tmp - 1;
250 if (tmp == macbuf + 4096) {
251 puts("4K macro buffer exceeded.");
256 warnx("Unknown .netrc keyword %s", tokval);
276 if (feof(cfile) || ferror(cfile))
278 while ((c = getc(cfile)) != EOF &&
279 (c == '\n' || c == '\t' || c == ' ' || c == ','))
285 while ((c = getc(cfile)) != EOF && c != '"') {
292 while ((c = getc(cfile)) != EOF
293 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
302 for (t = toktab; t->tokstr; t++)
303 if (!strcmp(t->tokstr, tokval))