2
* Copyright (c) 1985 Regents of the University of California.
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
* from: @(#)ruserpass.c 5.3 (Berkeley) 3/1/91
37
char ruserpass_rcsid[] =
38
"$Id: ruserpass.c,v 1.9 1999/10/02 19:12:33 dholland Exp $";
52
static int token(void);
62
static char tokval[100];
64
static struct toktab {
68
{ "default", DEFAULT },
70
{ "password", PASSWD },
72
{ "account", ACCOUNT },
79
xruserpass(const char *host, char **aname, char **apass, char **aacct)
82
char buf[BUFSIZ], *tmp;
83
char myname[MAXHOSTNAMELEN];
85
int t, i, c, usedefault = 0;
88
hdir = getenv("HOME");
91
snprintf(buf, sizeof(buf), "%s/.netrc", hdir);
92
cfile = fopen(buf, "r");
98
if (gethostname(myname, sizeof(myname)) < 0)
100
if ((mydomain = strchr(myname, '.')) == NULL)
103
while ((t = token())) switch(t) {
114
* Allow match either for user's input host name
115
* or official hostname. Also allow match of
116
* incompletely-specified host in local domain.
118
if (strcasecmp(host, tokval) == 0)
120
if (strcasecmp(hostname, tokval) == 0)
122
if ((tmp = index(hostname, '.')) != NULL &&
123
strcasecmp(tmp, mydomain) == 0 &&
124
strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
125
tokval[tmp - hostname] == '\0')
127
if ((tmp = index(host, '.')) != NULL &&
128
strcasecmp(tmp, mydomain) == 0 &&
129
strncasecmp(host, tokval, tmp - host) == 0 &&
130
tokval[tmp - host] == '\0')
135
while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
140
*aname = obstack_copy(&mainobstack, tokval, strlen(tokval) + 1);
142
if (strcmp(*aname, tokval))
149
fprintf(stderr, "Error: `password' must follow `login' in .netrc\n");
152
if (strcmp(*aname, "anonymous") &&
153
fstat(fileno(cfile), &stb) >= 0 &&
154
(stb.st_mode & 077) != 0) {
155
fprintf(stderr, "Error - .netrc file not correct permissions.\n");
156
fprintf(stderr, "Remove password or correct mode (should be 600).\n");
159
if (token() && *apass == 0) {
160
*apass = obstack_copy(&mainobstack, tokval, strlen(tokval) + 1);
164
if (fstat(fileno(cfile), &stb) >= 0
165
&& (stb.st_mode & 077) != 0) {
166
fprintf(stderr, "Error - .netrc file not correct permissions.\n");
167
fprintf(stderr, "Remove account or correct mode (should be 600).\n");
170
if (token() && *aacct == 0) {
171
*aacct = obstack_copy(&mainobstack, tokval, strlen(tokval) + 1);
176
(void) fclose(cfile);
179
while ((c=getc(cfile)) != EOF && (c == ' ' || c == '\t'));
180
if (c == EOF || c == '\n') {
181
printf("Missing macdef name argument.\n");
185
printf("Limit of 16 macros have already been defined\n");
188
tmp = macros[macnum].mac_name;
190
for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
195
printf("Macro definition missing null line terminator.\n");
200
while ((c=getc(cfile)) != EOF && c != '\n');
203
printf("Macro definition missing null line terminator.\n");
207
macros[macnum].mac_start = macbuf;
210
macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
212
tmp = macros[macnum].mac_start;
213
while (tmp != macbuf + MACBUF_SIZE) {
214
if ((c=getc(cfile)) == EOF) {
215
printf("Macro definition missing null line terminator.\n");
220
if (*(tmp-1) == '\0') {
221
macros[macnum++].mac_end = tmp - 1;
228
if (tmp == macbuf + MACBUF_SIZE) {
229
printf("4K macro buffer exceeded\n");
234
fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
240
(void) fclose(cfile);
243
(void) fclose(cfile);
257
while ((c = getc(cfile)) != EOF &&
258
(c == '\n' || c == '\t' || c == ' ' || c == ','))
264
while ((c = getc(cfile)) != EOF && c != '"') {
271
while ((c = getc(cfile)) != EOF
272
&& c != '\n' && c != '\t' && c != ' ' && c != ',') {
281
for (t = toktab; t->tokstr; t++)
282
if (!strcmp(t->tokstr, tokval))