Debian Codegraph @

ftp/ruserpass.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1985, 1993, 1994
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <sys/types.h>
51 #include <sys/stat.h>
52
53 #include <ctype.h>
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <stdarg.h>
60 #include <error.h>
61
62 #include "ftp_var.h"
63 #include <libinetutils.h>
64 #include <xalloc.h>
65
66 static int token (void);
67 static FILE *cfile;
68
69 /* protect against os headers */
70 #undef DEFAULT
71 #define DEFAULT 1
72 #undef LOGIN
73 #define LOGIN 2
74 #undef PASSWD
75 #define PASSWD 3
76 #undef ACCOUNT
77 #define ACCOUNT 4
78 #undef MACDEF
79 #define MACDEF 5
80 #undef ID
81 #define ID 10
82 #undef MACHINE
83 #define MACHINE 11
84
85 static char tokval[BUFSIZ];
86
87 static struct toktab
88 {
89 char *tokstr;
90 int tval;
91 } toktab[] = {
92 {
93 "default", DEFAULT},
94 {
95 "login", LOGIN},
96 {
97 "password", PASSWD},
98 {
99 "passwd", PASSWD},
100 {
101 "account", ACCOUNT},
102 {
103 "machine", MACHINE},
104 {
105 "macdef", MACDEF},
106 {
107 NULL, 0}
108 };
109
110 int
111 remote_userpass (char *host, char **aname, char **apass, char **aacct)
112 {
113 char *hdir, buf[BUFSIZ], *tmp;
114 char *myname, *mydomain;
115 int t, c, usedefault = 0;
116 size_t i;
117 struct stat stb;
118
119 hdir = getenv ("HOME");
120 if (hdir == NULL)
121 hdir = ".";
122 snprintf (buf, sizeof buf, "%s/.netrc", hdir);
123
124 /* The switch `-N/--netrc' would have set this. */
125 if (!netrc)
126 netrc = getenv ("NETRC");
127
128 if (netrc && netrc[0])
129 snprintf (buf, sizeof buf, "%s", netrc);
130
131 cfile = fopen (buf, "r");
132 if (cfile == NULL)
133 {
134 if (errno != ENOENT)
135 error (0, errno, "%s", buf);
136 return (0);
137 }
138
139 /* The .netrc is now opened and is thus fixed.
140 * Check that it is a regular file, and not a
141 * soft link in particular.
142 */
143 if (lstat (buf, &stb) < 0)
144 {
145 error (0, errno, "%s", buf);
146 fclose (cfile);
147 return (-1);
148 }
149
150 if (!S_ISREG (stb.st_mode))
151 {
152 if (S_ISLNK (stb.st_mode))
153 error (0, 0, "the .netrc file is symbolic link: %s", buf);
154 else
155 error (0, 0, "the .netrc file is no regular file: %s", buf);
156
157 fclose (cfile);
158 return (-1);
159 }
160
161 myname = localhost ();
162 if (!myname)
163 myname = xstrdup ("");
164
165 mydomain = strchr (myname, '.');
166 if (mydomain == NULL)
167 mydomain = "";
168
169 next:
170 while ((t = token ()))
171 switch (t)
172 {
173 case DEFAULT:
174 usedefault = 1;
175 /* FALL THROUGH */
176
177 case MACHINE:
178 if (!usedefault)
179 {
180 if (token () != ID)
181 continue;
182 /*
183 * Allow match either for user's input host name
184 * or official hostname. Also allow match of
185 * incompletely-specified host in local domain. */
186 if (strcasecmp (host, tokval) == 0)
187 goto match;
188 if (strcasecmp (hostname, tokval) == 0)
189 goto match;
190 tmp = strchr (hostname, '.');
191 if (tmp != NULL
192 && strcasecmp (tmp, mydomain) == 0
193 && strncasecmp (hostname, tokval, tmp - hostname) == 0
194 && tokval[tmp - hostname] == '\0')
195 goto match;
196 tmp = strchr (host, '.');
197 if (tmp != NULL
198 && strcasecmp (tmp, mydomain) == 0
199 && strncasecmp (host, tokval, tmp - host) == 0
200 && tokval[tmp - host] == '\0')
201 goto match;
202 continue;
203 }
204 match:
205 while ((t = token ()) && t != MACHINE && t != DEFAULT)
206 switch (t)
207 {
208 case LOGIN:
209 if (token ())
210 {
211 if (*aname == 0)
212 {
213 *aname = xmalloc ((unsigned) strlen (tokval) + 1);
214 strcpy (*aname, tokval);
215 }
216 else
217 {
218 if (strcmp (*aname, tokval))
219 goto next;
220 }
221 }
222 break;
223 case PASSWD:
224 if ((*aname == NULL || strcmp (*aname, "anonymous"))
225 && fstat (fileno (cfile), &stb) >= 0
226 && (stb.st_mode & 077) != 0)
227 {
228 error (0, 0, "Error: .netrc file is readable by others.");
229 error (0, 0,
230 "Remove password or make file unreadable by others.");
231 goto bad;
232 }
233 if (token () && *apass == 0)
234 {
235 *apass = xmalloc ((unsigned) strlen (tokval) + 1);
236 strcpy (*apass, tokval);
237 }
238 break;
239 case ACCOUNT:
240 if (fstat (fileno (cfile), &stb) >= 0
241 && (stb.st_mode & 077) != 0)
242 {
243 error (0, 0, "Error: .netrc file is readable by others.");
244 error (0, 0,
245 "Remove account or make file unreadable by others.");
246 goto bad;
247 }
248 if (token () && *aacct == 0)
249 {
250 *aacct = xmalloc ((unsigned) strlen (tokval) + 1);
251 strcpy (*aacct, tokval);
252 }
253 break;
254 case MACDEF:
255 if (proxy)
256 goto done;
257
258 while (((c = getc (cfile)) != EOF && c == ' ') || c == '\t')
259 ;
260 if (c == EOF || c == '\n')
261 {
262 printf ("Missing macdef name argument.\n");
263 goto bad;
264 }
265 if (macnum == 16)
266 {
267 printf ("Limit of 16 macros have already been defined\n");
268 goto bad;
269 }
270 tmp = macros[macnum].mac_name;
271 *tmp++ = c;
272 for (i = 0; i < (sizeof (macros[macnum].mac_name) - 1)
273 && (c = getc (cfile)) != EOF && !isspace (c); ++i)
274 {
275 *tmp++ = c;
276 }
277 if (c == EOF)
278 {
279 printf ("Macro definition missing null line terminator.\n");
280 goto bad;
281 }
282 *tmp = '\0';
283 if (c != '\n')
284 {
285 while ((c = getc (cfile)) != EOF && c != '\n');
286 }
287 if (c == EOF)
288 {
289 printf ("Macro definition missing null line terminator.\n");
290 goto bad;
291 }
292 if (macnum == 0)
293 {
294 macros[macnum].mac_start = macbuf;
295 }
296 else
297 {
298 macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
299 }
300 tmp = macros[macnum].mac_start;
301 while (tmp < macbuf + sizeof (macbuf))
302 {
303 if ((c = getc (cfile)) == EOF)
304 {
305 printf
306 ("Macro definition missing null line terminator.\n");
307 goto bad;
308 }
309 *tmp = c;
310 if (*tmp == '\n')
311 {
312 if (*(tmp - 1) == '\0')
313 {
314 macros[macnum++].mac_end = tmp - 1;
315 break;
316 }
317 *tmp = '\0';
318 }
319 tmp++;
320 }
321 if (tmp == macbuf + sizeof (macbuf))
322 {
323 printf ("4K macro buffer exceeded\n");
324 goto bad;
325 }
326 break;
327 default:
328 error (0, 0, "Unknown .netrc keyword %s", tokval);
329 break;
330 }
331 goto done;
332 }
333 done:
334 fclose (cfile);
335 free (myname);
336 return (0);
337 bad:
338 fclose (cfile);
339 free (myname);
340 return (-1);
341 }
342
343 static int
344 token (void)
345 {
346 char *cp;
347 int c;
348 struct toktab *t;
349
350 if (feof (cfile) || ferror (cfile))
351 return (0);
352 while ((c = getc (cfile)) != EOF &&
353 (c == '\n' || c == '\t' || c == ' ' || c == ','))
354 continue;
355 if (c == EOF)
356 return (0);
357 cp = tokval;
358 if (c == '"')
359 {
360 while ((c = getc (cfile)) != EOF && c != '"')
361 {
362 if (c == '\\')
363 c = getc (cfile);
364 *cp++ = c;
365 }
366 }
367 else
368 {
369 *cp++ = c;
370 while ((c = getc (cfile)) != EOF
371 && c != '\n' && c != '\t' && c != ' ' && c != ',')
372 {
373 if (c == '\\')
374 c = getc (cfile);
375 *cp++ = c;
376 }
377 }
378 *cp = 0;
379 if (tokval[0] == 0)
380 return (0);
381 for (t = toktab; t->tokstr; t++)
382 if (!strcmp (t->tokstr, tokval))
383 return (t->tval);
384 return (ID);
385 }