1 /* $NetBSD: ruserpass.c,v 1.8 2007/08/06 04:33:24 lukem Exp $ */
2 /* from NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 lukem 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * 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(" NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 lukem Exp ");
46 #include <sys/types.h>
62 static int token(void);
73 static char tokval[100];
75 static struct toktab {
79 { "default", DEFAULT },
81 { "password", PASSWD },
83 { "account", ACCOUNT },
90 ruserpass(const char *host, char **aname, char **apass, char **aacct)
94 char myname[MAXHOSTNAMELEN + 1];
95 int t, i, c, usedefault = 0;
100 cfile = fopen(netrc, "r");
103 warn("Can't read `%s'", netrc);
106 if (gethostname(myname, sizeof(myname)) < 0)
108 myname[sizeof(myname) - 1] = '\0';
109 if ((mydomain = strchr(myname, '.')) == NULL)
112 while ((t = token()) > 0) switch(t) {
120 if ((t = token()) == -1)
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()) > 0 &&
147 t != MACH && t != DEFAULT) switch(t) {
150 if ((t = token()) == -1)
154 *aname = ftp_strdup(tokval);
156 if (strcmp(*aname, tokval))
162 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
163 fstat(fileno(cfile), &stb) >= 0 &&
164 (stb.st_mode & 077) != 0) {
165 warnx("Error: .netrc file is readable by others");
166 warnx("Remove password or make file unreadable by others");
169 if ((t = token()) == -1)
171 if (t && *apass == NULL)
172 *apass = ftp_strdup(tokval);
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 ((t = token()) == -1)
183 if (t && *aacct == NULL)
184 *aacct = ftp_strdup(tokval);
191 while ((c = getc(cfile)) != EOF)
192 if (c != ' ' && c != '\t')
194 if (c == EOF || c == '\n') {
195 fputs("Missing macdef name argument.\n",
201 "Limit of 16 macros have already been defined.\n",
205 tmp = macros[macnum].mac_name;
207 for (i = 0; i < 8 && (c = getc(cfile)) != EOF &&
213 "Macro definition missing null line terminator.\n",
219 while ((c = getc(cfile)) != EOF && c != '\n');
223 "Macro definition missing null line terminator.\n",
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.\n",
244 if (tmp == macros[macnum].mac_start) {
245 macros[macnum++].mac_end = tmp;
247 } else if (*(tmp - 1) == '\0') {
248 macros[macnum++].mac_end =
256 if (tmp == macbuf + 4096) {
257 fputs("4K macro buffer exceeded.\n",
263 warnx("Unknown .netrc keyword `%s'", tokval);
285 if (feof(cfile) || ferror(cfile))
287 while ((c = getc(cfile)) != EOF &&
288 (c == '\n' || c == '\t' || c == ' ' || c == ','))
294 while ((c = getc(cfile)) != EOF && c != '"') {
296 if ((c = getc(cfile)) == EOF)
299 if (cp == tokval + sizeof(tokval)) {
300 warnx("Token in .netrc too long");
306 while ((c = getc(cfile)) != EOF
307 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
309 if ((c = getc(cfile)) == EOF)
312 if (cp == tokval + sizeof(tokval)) {
313 warnx("Token in .netrc too long");
321 for (t = toktab; t->tokstr; t++)
322 if (!strcmp(t->tokstr, tokval))