2 * Copyright (c) 1985, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$Id: ruserpass.c,v 1.16 1999/09/16 20:37:31 assar Exp $");
37 static int token (void);
49 static char tokval[100];
51 static struct toktab {
55 { "default", DEFAULT },
57 { "password", PASSWD },
59 { "account", ACCOUNT },
67 * Write a copy of the hostname into `hostname, sz' and return a guess
68 * as to the `domain' of that hostname.
72 guess_domain (char *hostname, size_t sz)
79 if (gethostname (hostname, sz) < 0) {
80 strlcpy (hostname, "", sz);
83 dot = strchr (hostname, '.');
87 he = gethostbyname (hostname);
91 dot = strchr (he->h_name, '.');
93 strlcpy (hostname, he->h_name, sz);
96 for (aliases = he->h_aliases; (a = *aliases) != NULL; ++aliases) {
97 dot = strchr (a, '.');
99 strlcpy (hostname, a, sz);
107 ruserpass(char *host, char **aname, char **apass, char **aacct)
109 char *hdir, buf[BUFSIZ], *tmp;
110 int t, i, c, usedefault = 0;
113 mydomain = guess_domain (myhostname, MaxHostNameLen);
115 hdir = getenv("HOME");
118 snprintf(buf, sizeof(buf), "%s/.netrc", hdir);
119 cfile = fopen(buf, "r");
127 while ((t = token())) switch(t) {
138 * Allow match either for user's input host name
139 * or official hostname. Also allow match of
140 * incompletely-specified host in local domain.
142 if (strcasecmp(host, tokval) == 0)
144 if (strcasecmp(hostname, tokval) == 0)
146 if ((tmp = strchr(hostname, '.')) != NULL &&
148 strcasecmp(tmp, mydomain) == 0 &&
149 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
150 tokval[tmp - hostname] == '\0')
152 if ((tmp = strchr(host, '.')) != NULL &&
154 strcasecmp(tmp, mydomain) == 0 &&
155 strncasecmp(host, tokval, tmp - host) == 0 &&
156 tokval[tmp - host] == '\0')
161 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
166 *aname = strdup(tokval);
168 if (strcmp(*aname, tokval))
174 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
175 fstat(fileno(cfile), &stb) >= 0 &&
176 (stb.st_mode & 077) != 0) {
177 warnx("Error: .netrc file is readable by others.");
178 warnx("Remove password or make file unreadable by others.");
181 if (token() && *apass == 0) {
182 *apass = strdup(tokval);
186 if (fstat(fileno(cfile), &stb) >= 0
187 && (stb.st_mode & 077) != 0) {
188 warnx("Error: .netrc file is readable by others.");
189 warnx("Remove account or make file unreadable by others.");
192 if (token() && *aacct == 0) {
193 *aacct = strdup(tokval);
201 while ((c=getc(cfile)) != EOF &&
202 (c == ' ' || c == '\t'));
203 if (c == EOF || c == '\n') {
204 printf("Missing macdef name argument.\n");
208 printf("Limit of 16 macros have already been defined\n");
211 tmp = macros[macnum].mac_name;
213 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
218 printf("Macro definition missing null line terminator.\n");
223 while ((c=getc(cfile)) != EOF && c != '\n');
226 printf("Macro definition missing null line terminator.\n");
230 macros[macnum].mac_start = macbuf;
233 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
235 tmp = macros[macnum].mac_start;
236 while (tmp != macbuf + 4096) {
237 if ((c=getc(cfile)) == EOF) {
238 printf("Macro definition missing null line terminator.\n");
243 if (*(tmp-1) == '\0') {
244 macros[macnum++].mac_end = tmp - 1;
251 if (tmp == macbuf + 4096) {
252 printf("4K macro buffer exceeded\n");
258 if(sec_request_prot(tokval) < 0)
259 warnx("Unknown protection level \"%s\"", tokval);
262 warnx("Unknown .netrc keyword %s", tokval);
282 if (feof(cfile) || ferror(cfile))
284 while ((c = getc(cfile)) != EOF &&
285 (c == '\n' || c == '\t' || c == ' ' || c == ','))
291 while ((c = getc(cfile)) != EOF && c != '"') {
298 while ((c = getc(cfile)) != EOF
299 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
308 for (t = toktab; t->tokstr; t++)
309 if (!strcmp(t->tokstr, tokval))