1 /* $NetBSD: ruserpass.c,v 1.29 2003/08/07 11:13:57 agc 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>
34 #include <sys/types.h>
48 static int token(void);
59 static char tokval
[100];
61 static struct toktab
{
65 { "default", DEFAULT
},
67 { "password", PASSWD
},
69 { "account", ACCOUNT
},
76 ruserpass(const char *host
, const char **aname
, const char **apass
,
80 char myname
[MAXHOSTNAMELEN
+ 1], *mydomain
;
81 int t
, i
, c
, usedefault
= 0;
86 cfile
= fopen(netrc
, "r");
92 if (gethostname(myname
, sizeof(myname
)) < 0)
94 myname
[sizeof(myname
) - 1] = '\0';
95 if ((mydomain
= strchr(myname
, '.')) == NULL
)
98 while ((t
= token())) switch(t
) {
109 * Allow match either for user's input host name
110 * or official hostname. Also allow match of
111 * incompletely-specified host in local domain.
113 if (strcasecmp(host
, tokval
) == 0)
115 if (strcasecmp(hostname
, tokval
) == 0)
117 if ((tmp
= strchr(hostname
, '.')) != NULL
&&
118 strcasecmp(tmp
, mydomain
) == 0 &&
119 strncasecmp(hostname
, tokval
, tmp
-hostname
) == 0 &&
120 tokval
[tmp
- hostname
] == '\0')
122 if ((tmp
= strchr(host
, '.')) != NULL
&&
123 strcasecmp(tmp
, mydomain
) == 0 &&
124 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
125 tokval
[tmp
- host
] == '\0')
130 while ((t
= token()) && t
!= MACH
&& t
!= DEFAULT
) switch(t
) {
135 *aname
= xstrdup(tokval
);
137 if (strcmp(*aname
, tokval
))
143 if ((*aname
== NULL
|| strcmp(*aname
, "anonymous")) &&
144 fstat(fileno(cfile
), &stb
) >= 0 &&
145 (stb
.st_mode
& 077) != 0) {
146 warnx("Error: .netrc file is readable by others.");
147 warnx("Remove password or make file unreadable by others.");
150 if (token() && *apass
== NULL
)
151 *apass
= xstrdup(tokval
);
154 if (fstat(fileno(cfile
), &stb
) >= 0
155 && (stb
.st_mode
& 077) != 0) {
156 warnx("Error: .netrc file is readable by others.");
157 warnx("Remove account or make file unreadable by others.");
160 if (token() && *aacct
== NULL
)
161 *aacct
= xstrdup(tokval
);
168 while ((c
= getc(cfile
)) != EOF
)
169 if (c
!= ' ' && c
!= '\t')
171 if (c
== EOF
|| c
== '\n') {
172 fputs("Missing macdef name argument.\n",
178 "Limit of 16 macros have already been defined.\n",
182 tmp
= macros
[macnum
].mac_name
;
184 for (i
= 0; i
< 8 && (c
= getc(cfile
)) != EOF
&&
190 "Macro definition missing null line terminator.\n",
196 while ((c
= getc(cfile
)) != EOF
&& c
!= '\n');
200 "Macro definition missing null line terminator.\n",
205 macros
[macnum
].mac_start
= macbuf
;
208 macros
[macnum
].mac_start
=
209 macros
[macnum
-1].mac_end
+ 1;
211 tmp
= macros
[macnum
].mac_start
;
212 while (tmp
!= macbuf
+ 4096) {
213 if ((c
= getc(cfile
)) == EOF
) {
215 "Macro definition missing null line terminator.\n",
221 if (*(tmp
-1) == '\0') {
222 macros
[macnum
++].mac_end
= tmp
- 1;
229 if (tmp
== macbuf
+ 4096) {
230 fputs("4K macro buffer exceeded.\n",
236 warnx("Unknown .netrc keyword %s", tokval
);
256 if (feof(cfile
) || ferror(cfile
))
258 while ((c
= getc(cfile
)) != EOF
&&
259 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
265 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
272 while ((c
= getc(cfile
)) != EOF
273 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
282 for (t
= toktab
; t
->tokstr
; t
++)
283 if (!strcmp(t
->tokstr
, tokval
))