1 /* $NetBSD: ruserpass.c,v 1.31 2006/01/31 20:01:23 christos Exp $ */
4 * Copyright (c) 1985, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
37 __RCSID("$NetBSD: ruserpass.c,v 1.31 2006/01/31 20:01:23 christos Exp $");
41 #include <sys/types.h>
55 static int token(void);
66 static char tokval[100];
68 static struct toktab {
72 { "default", DEFAULT },
74 { "password", PASSWD },
76 { "account", ACCOUNT },
83 ruserpass(const char *host, char **aname, char **apass, char **aacct)
87 char myname[MAXHOSTNAMELEN + 1];
88 int t, i, c, usedefault = 0;
93 cfile = fopen(netrc, "r");
99 if (gethostname(myname, sizeof(myname)) < 0)
101 myname[sizeof(myname) - 1] = '\0';
102 if ((mydomain = strchr(myname, '.')) == NULL)
105 while ((t = token())) switch(t) {
116 * Allow match either for user's input host name
117 * or official hostname. Also allow match of
118 * incompletely-specified host in local domain.
120 if (strcasecmp(host, tokval) == 0)
122 if (strcasecmp(hostname, tokval) == 0)
124 if ((tmp = strchr(hostname, '.')) != NULL &&
125 strcasecmp(tmp, mydomain) == 0 &&
126 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
127 tokval[tmp - hostname] == '\0')
129 if ((tmp = strchr(host, '.')) != NULL &&
130 strcasecmp(tmp, mydomain) == 0 &&
131 strncasecmp(host, tokval, tmp - host) == 0 &&
132 tokval[tmp - host] == '\0')
137 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
142 *aname = ftp_strdup(tokval);
144 if (strcmp(*aname, tokval))
150 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
151 fstat(fileno(cfile), &stb) >= 0 &&
152 (stb.st_mode & 077) != 0) {
153 warnx("Error: .netrc file is readable by others.");
154 warnx("Remove password or make file unreadable by others.");
157 if (token() && *apass == NULL)
158 *apass = ftp_strdup(tokval);
161 if (fstat(fileno(cfile), &stb) >= 0
162 && (stb.st_mode & 077) != 0) {
163 warnx("Error: .netrc file is readable by others.");
164 warnx("Remove account or make file unreadable by others.");
167 if (token() && *aacct == NULL)
168 *aacct = ftp_strdup(tokval);
175 while ((c = getc(cfile)) != EOF)
176 if (c != ' ' && c != '\t')
178 if (c == EOF || c == '\n') {
179 fputs("Missing macdef name argument.\n",
185 "Limit of 16 macros have already been defined.\n",
189 tmp = macros[macnum].mac_name;
191 for (i = 0; i < 8 && (c = getc(cfile)) != EOF &&
197 "Macro definition missing null line terminator.\n",
203 while ((c = getc(cfile)) != EOF && c != '\n');
207 "Macro definition missing null line terminator.\n",
212 macros[macnum].mac_start = macbuf;
215 macros[macnum].mac_start =
216 macros[macnum-1].mac_end + 1;
218 tmp = macros[macnum].mac_start;
219 while (tmp != macbuf + 4096) {
220 if ((c = getc(cfile)) == EOF) {
222 "Macro definition missing null line terminator.\n",
228 if (*(tmp-1) == '\0') {
229 macros[macnum++].mac_end = tmp - 1;
236 if (tmp == macbuf + 4096) {
237 fputs("4K macro buffer exceeded.\n",
243 warnx("Unknown .netrc keyword %s", tokval);
263 if (feof(cfile) || ferror(cfile))
265 while ((c = getc(cfile)) != EOF &&
266 (c == '\n' || c == '\t' || c == ' ' || c == ','))
272 while ((c = getc(cfile)) != EOF && c != '"') {
279 while ((c = getc(cfile)) != EOF
280 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
289 for (t = toktab; t->tokstr; t++)
290 if (!strcmp(t->tokstr, tokval))