1*04203a83SThomas Cort /* $NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 lukem Exp $ */
2*04203a83SThomas Cort
3*04203a83SThomas Cort /*
4*04203a83SThomas Cort * Copyright (c) 1985, 1993, 1994
5*04203a83SThomas Cort * The Regents of the University of California. All rights reserved.
6*04203a83SThomas Cort *
7*04203a83SThomas Cort * Redistribution and use in source and binary forms, with or without
8*04203a83SThomas Cort * modification, are permitted provided that the following conditions
9*04203a83SThomas Cort * are met:
10*04203a83SThomas Cort * 1. Redistributions of source code must retain the above copyright
11*04203a83SThomas Cort * notice, this list of conditions and the following disclaimer.
12*04203a83SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*04203a83SThomas Cort * notice, this list of conditions and the following disclaimer in the
14*04203a83SThomas Cort * documentation and/or other materials provided with the distribution.
15*04203a83SThomas Cort * 3. Neither the name of the University nor the names of its contributors
16*04203a83SThomas Cort * may be used to endorse or promote products derived from this software
17*04203a83SThomas Cort * without specific prior written permission.
18*04203a83SThomas Cort *
19*04203a83SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*04203a83SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*04203a83SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*04203a83SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*04203a83SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*04203a83SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*04203a83SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*04203a83SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*04203a83SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*04203a83SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*04203a83SThomas Cort * SUCH DAMAGE.
30*04203a83SThomas Cort */
31*04203a83SThomas Cort
32*04203a83SThomas Cort #include <sys/cdefs.h>
33*04203a83SThomas Cort #ifndef lint
34*04203a83SThomas Cort #if 0
35*04203a83SThomas Cort static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
36*04203a83SThomas Cort #else
37*04203a83SThomas Cort __RCSID("$NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 lukem Exp $");
38*04203a83SThomas Cort #endif
39*04203a83SThomas Cort #endif /* not lint */
40*04203a83SThomas Cort
41*04203a83SThomas Cort #include <sys/types.h>
42*04203a83SThomas Cort #include <sys/stat.h>
43*04203a83SThomas Cort
44*04203a83SThomas Cort #include <ctype.h>
45*04203a83SThomas Cort #include <err.h>
46*04203a83SThomas Cort #include <errno.h>
47*04203a83SThomas Cort #include <netdb.h>
48*04203a83SThomas Cort #include <stdio.h>
49*04203a83SThomas Cort #include <stdlib.h>
50*04203a83SThomas Cort #include <string.h>
51*04203a83SThomas Cort #include <unistd.h>
52*04203a83SThomas Cort
53*04203a83SThomas Cort #include "ftp_var.h"
54*04203a83SThomas Cort
55*04203a83SThomas Cort static int token(void);
56*04203a83SThomas Cort static FILE *cfile;
57*04203a83SThomas Cort
58*04203a83SThomas Cort #define DEFAULT 1
59*04203a83SThomas Cort #define LOGIN 2
60*04203a83SThomas Cort #define PASSWD 3
61*04203a83SThomas Cort #define ACCOUNT 4
62*04203a83SThomas Cort #define MACDEF 5
63*04203a83SThomas Cort #define ID 10
64*04203a83SThomas Cort #define MACH 11
65*04203a83SThomas Cort
66*04203a83SThomas Cort static char tokval[100];
67*04203a83SThomas Cort
68*04203a83SThomas Cort static struct toktab {
69*04203a83SThomas Cort const char *tokstr;
70*04203a83SThomas Cort int tval;
71*04203a83SThomas Cort } toktab[] = {
72*04203a83SThomas Cort { "default", DEFAULT },
73*04203a83SThomas Cort { "login", LOGIN },
74*04203a83SThomas Cort { "password", PASSWD },
75*04203a83SThomas Cort { "passwd", PASSWD },
76*04203a83SThomas Cort { "account", ACCOUNT },
77*04203a83SThomas Cort { "machine", MACH },
78*04203a83SThomas Cort { "macdef", MACDEF },
79*04203a83SThomas Cort { NULL, 0 }
80*04203a83SThomas Cort };
81*04203a83SThomas Cort
82*04203a83SThomas Cort int
ruserpass(const char * host,char ** aname,char ** apass,char ** aacct)83*04203a83SThomas Cort ruserpass(const char *host, char **aname, char **apass, char **aacct)
84*04203a83SThomas Cort {
85*04203a83SThomas Cort char *tmp;
86*04203a83SThomas Cort const char *mydomain;
87*04203a83SThomas Cort char myname[MAXHOSTNAMELEN + 1];
88*04203a83SThomas Cort int t, i, c, usedefault = 0;
89*04203a83SThomas Cort struct stat stb;
90*04203a83SThomas Cort
91*04203a83SThomas Cort if (netrc[0] == '\0')
92*04203a83SThomas Cort return (0);
93*04203a83SThomas Cort cfile = fopen(netrc, "r");
94*04203a83SThomas Cort if (cfile == NULL) {
95*04203a83SThomas Cort if (errno != ENOENT)
96*04203a83SThomas Cort warn("Can't read `%s'", netrc);
97*04203a83SThomas Cort return (0);
98*04203a83SThomas Cort }
99*04203a83SThomas Cort if (gethostname(myname, sizeof(myname)) < 0)
100*04203a83SThomas Cort myname[0] = '\0';
101*04203a83SThomas Cort myname[sizeof(myname) - 1] = '\0';
102*04203a83SThomas Cort if ((mydomain = strchr(myname, '.')) == NULL)
103*04203a83SThomas Cort mydomain = "";
104*04203a83SThomas Cort next:
105*04203a83SThomas Cort while ((t = token()) > 0) switch(t) {
106*04203a83SThomas Cort
107*04203a83SThomas Cort case DEFAULT:
108*04203a83SThomas Cort usedefault = 1;
109*04203a83SThomas Cort /* FALL THROUGH */
110*04203a83SThomas Cort
111*04203a83SThomas Cort case MACH:
112*04203a83SThomas Cort if (!usedefault) {
113*04203a83SThomas Cort if ((t = token()) == -1)
114*04203a83SThomas Cort goto bad;
115*04203a83SThomas Cort if (t != ID)
116*04203a83SThomas Cort continue;
117*04203a83SThomas Cort /*
118*04203a83SThomas Cort * Allow match either for user's input host name
119*04203a83SThomas Cort * or official hostname. Also allow match of
120*04203a83SThomas Cort * incompletely-specified host in local domain.
121*04203a83SThomas Cort */
122*04203a83SThomas Cort if (strcasecmp(host, tokval) == 0)
123*04203a83SThomas Cort goto match;
124*04203a83SThomas Cort if (strcasecmp(hostname, tokval) == 0)
125*04203a83SThomas Cort goto match;
126*04203a83SThomas Cort if ((tmp = strchr(hostname, '.')) != NULL &&
127*04203a83SThomas Cort strcasecmp(tmp, mydomain) == 0 &&
128*04203a83SThomas Cort strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
129*04203a83SThomas Cort tokval[tmp - hostname] == '\0')
130*04203a83SThomas Cort goto match;
131*04203a83SThomas Cort if ((tmp = strchr(host, '.')) != NULL &&
132*04203a83SThomas Cort strcasecmp(tmp, mydomain) == 0 &&
133*04203a83SThomas Cort strncasecmp(host, tokval, tmp - host) == 0 &&
134*04203a83SThomas Cort tokval[tmp - host] == '\0')
135*04203a83SThomas Cort goto match;
136*04203a83SThomas Cort continue;
137*04203a83SThomas Cort }
138*04203a83SThomas Cort match:
139*04203a83SThomas Cort while ((t = token()) > 0 &&
140*04203a83SThomas Cort t != MACH && t != DEFAULT) switch(t) {
141*04203a83SThomas Cort
142*04203a83SThomas Cort case LOGIN:
143*04203a83SThomas Cort if ((t = token()) == -1)
144*04203a83SThomas Cort goto bad;
145*04203a83SThomas Cort if (t) {
146*04203a83SThomas Cort if (*aname == NULL)
147*04203a83SThomas Cort *aname = ftp_strdup(tokval);
148*04203a83SThomas Cort else {
149*04203a83SThomas Cort if (strcmp(*aname, tokval))
150*04203a83SThomas Cort goto next;
151*04203a83SThomas Cort }
152*04203a83SThomas Cort }
153*04203a83SThomas Cort break;
154*04203a83SThomas Cort case PASSWD:
155*04203a83SThomas Cort if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
156*04203a83SThomas Cort fstat(fileno(cfile), &stb) >= 0 &&
157*04203a83SThomas Cort (stb.st_mode & 077) != 0) {
158*04203a83SThomas Cort warnx("Error: .netrc file is readable by others");
159*04203a83SThomas Cort warnx("Remove password or make file unreadable by others");
160*04203a83SThomas Cort goto bad;
161*04203a83SThomas Cort }
162*04203a83SThomas Cort if ((t = token()) == -1)
163*04203a83SThomas Cort goto bad;
164*04203a83SThomas Cort if (t && *apass == NULL)
165*04203a83SThomas Cort *apass = ftp_strdup(tokval);
166*04203a83SThomas Cort break;
167*04203a83SThomas Cort case ACCOUNT:
168*04203a83SThomas Cort if (fstat(fileno(cfile), &stb) >= 0
169*04203a83SThomas Cort && (stb.st_mode & 077) != 0) {
170*04203a83SThomas Cort warnx("Error: .netrc file is readable by others");
171*04203a83SThomas Cort warnx("Remove account or make file unreadable by others");
172*04203a83SThomas Cort goto bad;
173*04203a83SThomas Cort }
174*04203a83SThomas Cort if ((t = token()) == -1)
175*04203a83SThomas Cort goto bad;
176*04203a83SThomas Cort if (t && *aacct == NULL)
177*04203a83SThomas Cort *aacct = ftp_strdup(tokval);
178*04203a83SThomas Cort break;
179*04203a83SThomas Cort case MACDEF:
180*04203a83SThomas Cort if (proxy) {
181*04203a83SThomas Cort (void)fclose(cfile);
182*04203a83SThomas Cort return (0);
183*04203a83SThomas Cort }
184*04203a83SThomas Cort while ((c = getc(cfile)) != EOF)
185*04203a83SThomas Cort if (c != ' ' && c != '\t')
186*04203a83SThomas Cort break;
187*04203a83SThomas Cort if (c == EOF || c == '\n') {
188*04203a83SThomas Cort fputs("Missing macdef name argument.\n",
189*04203a83SThomas Cort ttyout);
190*04203a83SThomas Cort goto bad;
191*04203a83SThomas Cort }
192*04203a83SThomas Cort if (macnum == 16) {
193*04203a83SThomas Cort fputs(
194*04203a83SThomas Cort "Limit of 16 macros have already been defined.\n",
195*04203a83SThomas Cort ttyout);
196*04203a83SThomas Cort goto bad;
197*04203a83SThomas Cort }
198*04203a83SThomas Cort tmp = macros[macnum].mac_name;
199*04203a83SThomas Cort *tmp++ = c;
200*04203a83SThomas Cort for (i = 0; i < 8 && (c = getc(cfile)) != EOF &&
201*04203a83SThomas Cort !isspace(c); ++i) {
202*04203a83SThomas Cort *tmp++ = c;
203*04203a83SThomas Cort }
204*04203a83SThomas Cort if (c == EOF) {
205*04203a83SThomas Cort fputs(
206*04203a83SThomas Cort "Macro definition missing null line terminator.\n",
207*04203a83SThomas Cort ttyout);
208*04203a83SThomas Cort goto bad;
209*04203a83SThomas Cort }
210*04203a83SThomas Cort *tmp = '\0';
211*04203a83SThomas Cort if (c != '\n') {
212*04203a83SThomas Cort while ((c = getc(cfile)) != EOF && c != '\n');
213*04203a83SThomas Cort }
214*04203a83SThomas Cort if (c == EOF) {
215*04203a83SThomas Cort fputs(
216*04203a83SThomas Cort "Macro definition missing null line terminator.\n",
217*04203a83SThomas Cort ttyout);
218*04203a83SThomas Cort goto bad;
219*04203a83SThomas Cort }
220*04203a83SThomas Cort if (macnum == 0) {
221*04203a83SThomas Cort macros[macnum].mac_start = macbuf;
222*04203a83SThomas Cort }
223*04203a83SThomas Cort else {
224*04203a83SThomas Cort macros[macnum].mac_start =
225*04203a83SThomas Cort macros[macnum-1].mac_end + 1;
226*04203a83SThomas Cort }
227*04203a83SThomas Cort tmp = macros[macnum].mac_start;
228*04203a83SThomas Cort while (tmp != macbuf + 4096) {
229*04203a83SThomas Cort if ((c = getc(cfile)) == EOF) {
230*04203a83SThomas Cort fputs(
231*04203a83SThomas Cort "Macro definition missing null line terminator.\n",
232*04203a83SThomas Cort ttyout);
233*04203a83SThomas Cort goto bad;
234*04203a83SThomas Cort }
235*04203a83SThomas Cort *tmp = c;
236*04203a83SThomas Cort if (*tmp == '\n') {
237*04203a83SThomas Cort if (tmp == macros[macnum].mac_start) {
238*04203a83SThomas Cort macros[macnum++].mac_end = tmp;
239*04203a83SThomas Cort break;
240*04203a83SThomas Cort } else if (*(tmp - 1) == '\0') {
241*04203a83SThomas Cort macros[macnum++].mac_end =
242*04203a83SThomas Cort tmp - 1;
243*04203a83SThomas Cort break;
244*04203a83SThomas Cort }
245*04203a83SThomas Cort *tmp = '\0';
246*04203a83SThomas Cort }
247*04203a83SThomas Cort tmp++;
248*04203a83SThomas Cort }
249*04203a83SThomas Cort if (tmp == macbuf + 4096) {
250*04203a83SThomas Cort fputs("4K macro buffer exceeded.\n",
251*04203a83SThomas Cort ttyout);
252*04203a83SThomas Cort goto bad;
253*04203a83SThomas Cort }
254*04203a83SThomas Cort break;
255*04203a83SThomas Cort default:
256*04203a83SThomas Cort warnx("Unknown .netrc keyword `%s'", tokval);
257*04203a83SThomas Cort break;
258*04203a83SThomas Cort }
259*04203a83SThomas Cort goto done;
260*04203a83SThomas Cort }
261*04203a83SThomas Cort done:
262*04203a83SThomas Cort if (t == -1)
263*04203a83SThomas Cort goto bad;
264*04203a83SThomas Cort (void)fclose(cfile);
265*04203a83SThomas Cort return (0);
266*04203a83SThomas Cort bad:
267*04203a83SThomas Cort (void)fclose(cfile);
268*04203a83SThomas Cort return (-1);
269*04203a83SThomas Cort }
270*04203a83SThomas Cort
271*04203a83SThomas Cort static int
token(void)272*04203a83SThomas Cort token(void)
273*04203a83SThomas Cort {
274*04203a83SThomas Cort char *cp;
275*04203a83SThomas Cort int c;
276*04203a83SThomas Cort struct toktab *t;
277*04203a83SThomas Cort
278*04203a83SThomas Cort if (feof(cfile) || ferror(cfile))
279*04203a83SThomas Cort return (0);
280*04203a83SThomas Cort while ((c = getc(cfile)) != EOF &&
281*04203a83SThomas Cort (c == '\n' || c == '\t' || c == ' ' || c == ','))
282*04203a83SThomas Cort continue;
283*04203a83SThomas Cort if (c == EOF)
284*04203a83SThomas Cort return (0);
285*04203a83SThomas Cort cp = tokval;
286*04203a83SThomas Cort if (c == '"') {
287*04203a83SThomas Cort while ((c = getc(cfile)) != EOF && c != '"') {
288*04203a83SThomas Cort if (c == '\\')
289*04203a83SThomas Cort if ((c = getc(cfile)) == EOF)
290*04203a83SThomas Cort break;
291*04203a83SThomas Cort *cp++ = c;
292*04203a83SThomas Cort if (cp == tokval + sizeof(tokval)) {
293*04203a83SThomas Cort warnx("Token in .netrc too long");
294*04203a83SThomas Cort return (-1);
295*04203a83SThomas Cort }
296*04203a83SThomas Cort }
297*04203a83SThomas Cort } else {
298*04203a83SThomas Cort *cp++ = c;
299*04203a83SThomas Cort while ((c = getc(cfile)) != EOF
300*04203a83SThomas Cort && c != '\n' && c != '\t' && c != ' ' && c != ',') {
301*04203a83SThomas Cort if (c == '\\')
302*04203a83SThomas Cort if ((c = getc(cfile)) == EOF)
303*04203a83SThomas Cort break;
304*04203a83SThomas Cort *cp++ = c;
305*04203a83SThomas Cort if (cp == tokval + sizeof(tokval)) {
306*04203a83SThomas Cort warnx("Token in .netrc too long");
307*04203a83SThomas Cort return (-1);
308*04203a83SThomas Cort }
309*04203a83SThomas Cort }
310*04203a83SThomas Cort }
311*04203a83SThomas Cort *cp = 0;
312*04203a83SThomas Cort if (tokval[0] == 0)
313*04203a83SThomas Cort return (0);
314*04203a83SThomas Cort for (t = toktab; t->tokstr; t++)
315*04203a83SThomas Cort if (!strcmp(t->tokstr, tokval))
316*04203a83SThomas Cort return (t->tval);
317*04203a83SThomas Cort return (ID);
318*04203a83SThomas Cort }
319