1 /* $NetBSD: ruserpass.c,v 1.32 2007/04/12 01:28:13 lukem 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>
35 static char sccsid
[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
37 __RCSID("$NetBSD: ruserpass.c,v 1.32 2007/04/12 01:28:13 lukem Exp $");
41 #include <sys/types.h>
55 static int token(void);
66 static char tokval
[100];
68 static struct toktab
{
72 { "default", DEFAULT
},
74 { "password", PASSWD
},
76 { "account", ACCOUNT
},
83 ruserpass(const char *host
, char **aname
, char **apass
, char **aacct
)
87 char myname
[MAXHOSTNAMELEN
+ 1];
88 int t
, i
, c
, usedefault
= 0;
93 cfile
= fopen(netrc
, "r");
96 warn("Can't read `%s'", netrc
);
99 if (gethostname(myname
, sizeof(myname
)) < 0)
101 myname
[sizeof(myname
) - 1] = '\0';
102 if ((mydomain
= strchr(myname
, '.')) == NULL
)
105 while ((t
= token()) > 0) switch(t
) {
113 if ((t
= token()) == -1)
118 * Allow match either for user's input host name
119 * or official hostname. Also allow match of
120 * incompletely-specified host in local domain.
122 if (strcasecmp(host
, tokval
) == 0)
124 if (strcasecmp(hostname
, tokval
) == 0)
126 if ((tmp
= strchr(hostname
, '.')) != NULL
&&
127 strcasecmp(tmp
, mydomain
) == 0 &&
128 strncasecmp(hostname
, tokval
, tmp
-hostname
) == 0 &&
129 tokval
[tmp
- hostname
] == '\0')
131 if ((tmp
= strchr(host
, '.')) != NULL
&&
132 strcasecmp(tmp
, mydomain
) == 0 &&
133 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
134 tokval
[tmp
- host
] == '\0')
139 while ((t
= token()) > 0 &&
140 t
!= MACH
&& t
!= DEFAULT
) switch(t
) {
143 if ((t
= token()) == -1)
147 *aname
= ftp_strdup(tokval
);
149 if (strcmp(*aname
, tokval
))
155 if ((*aname
== NULL
|| strcmp(*aname
, "anonymous")) &&
156 fstat(fileno(cfile
), &stb
) >= 0 &&
157 (stb
.st_mode
& 077) != 0) {
158 warnx("Error: .netrc file is readable by others");
159 warnx("Remove password or make file unreadable by others");
162 if ((t
= token()) == -1)
164 if (t
&& *apass
== NULL
)
165 *apass
= ftp_strdup(tokval
);
168 if (fstat(fileno(cfile
), &stb
) >= 0
169 && (stb
.st_mode
& 077) != 0) {
170 warnx("Error: .netrc file is readable by others");
171 warnx("Remove account or make file unreadable by others");
174 if ((t
= token()) == -1)
176 if (t
&& *aacct
== NULL
)
177 *aacct
= ftp_strdup(tokval
);
184 while ((c
= getc(cfile
)) != EOF
)
185 if (c
!= ' ' && c
!= '\t')
187 if (c
== EOF
|| c
== '\n') {
188 fputs("Missing macdef name argument.\n",
194 "Limit of 16 macros have already been defined.\n",
198 tmp
= macros
[macnum
].mac_name
;
200 for (i
= 0; i
< 8 && (c
= getc(cfile
)) != EOF
&&
206 "Macro definition missing null line terminator.\n",
212 while ((c
= getc(cfile
)) != EOF
&& c
!= '\n');
216 "Macro definition missing null line terminator.\n",
221 macros
[macnum
].mac_start
= macbuf
;
224 macros
[macnum
].mac_start
=
225 macros
[macnum
-1].mac_end
+ 1;
227 tmp
= macros
[macnum
].mac_start
;
228 while (tmp
!= macbuf
+ 4096) {
229 if ((c
= getc(cfile
)) == EOF
) {
231 "Macro definition missing null line terminator.\n",
237 if (tmp
== macros
[macnum
].mac_start
) {
238 macros
[macnum
++].mac_end
= tmp
;
240 } else if (*(tmp
- 1) == '\0') {
241 macros
[macnum
++].mac_end
=
249 if (tmp
== macbuf
+ 4096) {
250 fputs("4K macro buffer exceeded.\n",
256 warnx("Unknown .netrc keyword `%s'", tokval
);
278 if (feof(cfile
) || ferror(cfile
))
280 while ((c
= getc(cfile
)) != EOF
&&
281 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
287 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
289 if ((c
= getc(cfile
)) == EOF
)
292 if (cp
== tokval
+ sizeof(tokval
)) {
293 warnx("Token in .netrc too long");
299 while ((c
= getc(cfile
)) != EOF
300 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
302 if ((c
= getc(cfile
)) == EOF
)
305 if (cp
== tokval
+ sizeof(tokval
)) {
306 warnx("Token in .netrc too long");
314 for (t
= toktab
; t
->tokstr
; t
++)
315 if (!strcmp(t
->tokstr
, tokval
))