1 /* Read and parse the .netrc file to get hosts, accounts, and passwords.
2 Copyright (C) 1996, Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget 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 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
30 /* This file used to be kept in synch with the code in Fetchmail, but
31 the latter has diverged since. */
44 #include <sys/types.h>
56 #define NETRC_FILE_NAME ".netrc"
60 static acc_t *parse_netrc PARAMS ((const char *));
62 /* Return the correct user and password, given the host, user (as
63 given in the URL), and password (as given in the URL). May return
66 If SLACK_DEFAULT is set, allow looking for a "default" account.
67 You will typically turn it off for HTTP. */
69 search_netrc (const char *host, const char **acc, const char **passwd,
73 static int processed_netrc;
80 char *home = home_dir ();
88 char *path = (char *)alloca (strlen (home) + 1
89 + strlen (NETRC_FILE_NAME) + 1);
90 sprintf (path, "%s/%s", home, NETRC_FILE_NAME);
92 err = stat (path, &buf);
94 netrc_list = parse_netrc (path);
97 /* If nothing to do... */
100 /* Acc and password found; all OK. */
103 /* Some data not given -- try finding the host. */
104 for (l = netrc_list; l; l = l->next)
108 else if (!strcasecmp (l->host, host))
115 /* Looking for password in .netrc. */
116 if (!strcmp (l->acc, *acc))
117 *passwd = l->passwd; /* usernames match; password OK */
119 *passwd = NULL; /* usernames don't match */
123 /* If password was given, use it. The account is l->acc. */
136 /* Try looking for the default account. */
137 for (l = netrc_list; l; l = l->next)
154 /* Normally, these functions would be defined by your package. */
155 # define xmalloc malloc
157 # define xstrdup strdup
159 # define xrealloc realloc
161 /* Read a line from FP. The function reallocs the storage as needed
162 to accomodate for any length of the line. Reallocs are done
163 storage exponentially, doubling the storage after each overflow to
164 minimize the number of calls to realloc() and fgets(). The newline
165 character at the end of line is retained.
167 After end-of-file is encountered without anything being read, NULL
168 is returned. NULL is also returned on error. To distinguish
169 between these two cases, use the stdio function ferror(). */
172 read_whole_line (FILE *fp)
176 char *line = (char *)xmalloc (bufsize);
178 while (fgets (line + length, bufsize - length, fp))
180 length += strlen (line + length);
182 if (line[length - 1] == '\n')
184 /* fgets() guarantees to read the whole line, or to use up the
185 space we've given it. We can double the buffer
188 line = xrealloc (line, bufsize);
190 if (length == 0 || ferror (fp))
195 if (length + 1 < bufsize)
196 /* Relieve the memory from our exponential greediness. We say
197 `length + 1' because the terminating \0 is not included in
198 LENGTH. We don't need to zero-terminate the string ourselves,
199 though, because fgets() does that. */
200 line = xrealloc (line, length + 1);
203 #endif /* STANDALONE */
205 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
206 set to a ready-to-use acc_t, in any event. */
208 maybe_add_to_list (acc_t **newentry, acc_t **list)
214 /* We need an account name in order to add the entry to the list. */
217 /* Free any allocated space. */
226 /* Add the current machine into our list. */
231 /* Allocate a new acc_t structure. */
232 a = (acc_t *)xmalloc (sizeof (acc_t));
235 /* Zero the structure, so that it is ready to use. */
236 memset (a, 0, sizeof(*a));
238 /* Return the new pointers. */
244 /* Helper function for the parser, shifts contents of
245 null-terminated string once character to the left.
246 Used in processing \ and " constructs in the netrc file */
248 shift_left(char *string)
252 for (p=string; *p; ++p)
256 /* Parse a .netrc file (as described in the ftp(1) manual page). */
258 parse_netrc (const char *path)
261 char *line, *p, *tok, *premature_token;
262 acc_t *current, *retval;
265 /* The latest token we've seen in the file. */
268 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
269 } last_token = tok_nothing;
271 current = retval = NULL;
273 fp = fopen (path, "r");
276 fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,
277 path, strerror (errno));
281 /* Initialize the file data. */
283 premature_token = NULL;
285 /* While there are lines in the file... */
286 while ((line = read_whole_line (fp)))
290 /* Parse the line. */
294 /* Skip leading whitespace. */
295 while (*p && ISSPACE (*p))
298 /* If the line is empty, then end any macro definition. */
299 if (last_token == tok_macdef && !*p)
300 /* End of macro if the line is empty. */
301 last_token = tok_nothing;
303 /* If we are defining macros, then skip parsing the line. */
304 while (*p && last_token != tok_macdef)
306 /* Skip any whitespace. */
307 while (*p && ISSPACE (*p))
310 /* Discard end-of-line comments; also, stop processing if
311 the above `while' merely skipped trailing whitespace. */
312 if (*p == '#' || !*p)
315 /* If the token starts with quotation mark, note this fact,
316 and squash the quotation character */
324 /* Find the end of the token, handling quotes and escapes. */
325 while (*p && (quote ? *p != '"' : !ISSPACE (*p))){
331 /* If field was quoted, squash the trailing quotation mark
332 and reset quote flag. */
339 /* Null-terminate the token, if it isn't already. */
347 current->acc = xstrdup (tok);
349 premature_token = "login";
353 /* Start a new machine entry. */
354 maybe_add_to_list (¤t, &retval);
355 current->host = xstrdup (tok);
360 current->passwd = xstrdup (tok);
362 premature_token = "password";
365 /* We handle most of tok_macdef above. */
368 premature_token = "macdef";
371 /* We don't handle the account keyword at all. */
374 premature_token = "account";
377 /* We handle tok_nothing below this switch. */
384 fprintf (stderr, _("\
385 %s: %s:%d: warning: \"%s\" token appears before any machine name\n"),
386 exec_name, path, ln, premature_token);
387 premature_token = NULL;
390 if (last_token != tok_nothing)
391 /* We got a value, so reset the token state. */
392 last_token = tok_nothing;
395 /* Fetch the next token. */
396 if (!strcmp (tok, "account"))
397 last_token = tok_account;
398 else if (!strcmp (tok, "default"))
400 maybe_add_to_list (¤t, &retval);
402 else if (!strcmp (tok, "login"))
403 last_token = tok_login;
405 else if (!strcmp (tok, "macdef"))
406 last_token = tok_macdef;
408 else if (!strcmp (tok, "machine"))
409 last_token = tok_machine;
411 else if (!strcmp (tok, "password"))
412 last_token = tok_password;
415 fprintf (stderr, _("%s: %s:%d: unknown token \"%s\"\n"),
416 exec_name, path, ln, tok);
425 /* Finalize the last machine entry we found. */
426 maybe_add_to_list (¤t, &retval);
429 /* Reverse the order of the list so that it appears in file order. */
434 acc_t *saved_reference;
436 /* Change the direction of the pointers. */
437 saved_reference = current->next;
438 current->next = retval;
440 /* Advance to the next node. */
442 current = saved_reference;
449 /* Free a netrc list. */
459 xfree_null (l->passwd);
460 xfree_null (l->host);
467 #include <sys/types.h>
468 #include <sys/stat.h>
471 main (int argc, char **argv)
474 char *program_name, *file, *target;
477 if (argc < 2 || argc > 3)
479 fprintf (stderr, _("Usage: %s NETRC [HOSTNAME]\n"), argv[0]);
483 program_name = argv[0];
487 if (stat (file, &sb))
489 fprintf (stderr, _("%s: cannot stat %s: %s\n"), argv[0], file,
494 head = parse_netrc (file);
498 /* Skip if we have a target and this isn't it. */
499 if (target && a->host && strcmp (target, a->host))
507 /* Print the host name if we have no target. */
509 fputs (a->host, stdout);
511 fputs ("DEFAULT", stdout);
516 /* Print the account name. */
517 fputs (a->acc, stdout);
521 /* Print the password, if there is any. */
523 fputs (a->passwd, stdout);
526 fputc ('\n', stdout);
528 /* Exit if we found the target. */
534 /* Exit with failure if we had a target, success otherwise. */
540 #endif /* STANDALONE */