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.19 2000/01/08 07:45:11 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)
74 struct addrinfo *ai, *a;
75 struct addrinfo hints;
79 if (gethostname (hostname, sz) < 0) {
80 strlcpy (hostname, "", sz);
83 dot = strchr (hostname, '.');
87 memset (&hints, 0, sizeof(hints));
88 hints.ai_flags = AI_CANONNAME;
90 error = getaddrinfo (hostname, NULL, &hints, &ai);
94 for (a = ai; a != NULL; a = a->ai_next)
95 if (a->ai_canonname != NULL) {
96 strlcpy (hostname, ai->ai_canonname, sz);
100 dot = strchr (hostname, '.');
108 ruserpass(char *host, char **aname, char **apass, char **aacct)
110 char *hdir, buf[BUFSIZ], *tmp;
111 int t, i, c, usedefault = 0;
114 mydomain = guess_domain (myhostname, MaxHostNameLen);
116 hdir = getenv("HOME");
119 snprintf(buf, sizeof(buf), "%s/.netrc", hdir);
120 cfile = fopen(buf, "r");
128 while ((t = token())) switch(t) {
139 * Allow match either for user's input host name
140 * or official hostname. Also allow match of
141 * incompletely-specified host in local domain.
143 if (strcasecmp(host, tokval) == 0)
145 if (strcasecmp(hostname, tokval) == 0)
147 if ((tmp = strchr(hostname, '.')) != NULL &&
149 strcasecmp(tmp, mydomain) == 0 &&
150 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
151 tokval[tmp - hostname] == '\0')
153 if ((tmp = strchr(host, '.')) != NULL &&
155 strcasecmp(tmp, mydomain) == 0 &&
156 strncasecmp(host, tokval, tmp - host) == 0 &&
157 tokval[tmp - host] == '\0')
162 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
167 *aname = strdup(tokval);
169 if (strcmp(*aname, tokval))
175 if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
176 fstat(fileno(cfile), &stb) >= 0 &&
177 (stb.st_mode & 077) != 0) {
178 warnx("Error: .netrc file is readable by others.");
179 warnx("Remove password or make file unreadable by others.");
182 if (token() && *apass == 0) {
183 *apass = strdup(tokval);
187 if (fstat(fileno(cfile), &stb) >= 0
188 && (stb.st_mode & 077) != 0) {
189 warnx("Error: .netrc file is readable by others.");
190 warnx("Remove account or make file unreadable by others.");
193 if (token() && *aacct == 0) {
194 *aacct = strdup(tokval);
202 while ((c=getc(cfile)) != EOF &&
203 (c == ' ' || c == '\t'));
204 if (c == EOF || c == '\n') {
205 printf("Missing macdef name argument.\n");
209 printf("Limit of 16 macros have already been defined\n");
212 tmp = macros[macnum].mac_name;
214 for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
219 printf("Macro definition missing null line terminator.\n");
224 while ((c=getc(cfile)) != EOF && c != '\n');
227 printf("Macro definition missing null line terminator.\n");
231 macros[macnum].mac_start = macbuf;
234 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
236 tmp = macros[macnum].mac_start;
237 while (tmp != macbuf + 4096) {
238 if ((c=getc(cfile)) == EOF) {
239 printf("Macro definition missing null line terminator.\n");
244 if (*(tmp-1) == '\0') {
245 macros[macnum++].mac_end = tmp - 1;
252 if (tmp == macbuf + 4096) {
253 printf("4K macro buffer exceeded\n");
259 if(sec_request_prot(tokval) < 0)
260 warnx("Unknown protection level \"%s\"", tokval);
263 warnx("Unknown .netrc keyword %s", tokval);
283 if (feof(cfile) || ferror(cfile))
285 while ((c = getc(cfile)) != EOF &&
286 (c == '\n' || c == '\t' || c == ' ' || c == ','))
292 while ((c = getc(cfile)) != EOF && c != '"') {
299 while ((c = getc(cfile)) != EOF
300 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
309 for (t = toktab; t->tokstr; t++)
310 if (!strcmp(t->tokstr, tokval))