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