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 Wget.
6 This program 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 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* This file used to be kept in synch with the code in Fetchmail, but
21 the latter has diverged since. */
35 #include <sys/types.h>
47 #define NETRC_FILE_NAME ".netrc"
51 static acc_t *parse_netrc PARAMS ((const char *));
53 /* Return the correct user and password, given the host, user (as
54 given in the URL), and password (as given in the URL). May return
57 If SLACK_DEFAULT is set, allow looking for a "default" account.
58 You will typically turn it off for HTTP. */
60 search_netrc (const char *host, const char **acc, const char **passwd,
64 static int processed_netrc;
71 char *home = home_dir();
79 char *path = (char *)alloca (strlen (home) + 1
80 + strlen (NETRC_FILE_NAME) + 1);
81 sprintf (path, "%s/%s", home, NETRC_FILE_NAME);
83 err = stat (path, &buf);
85 netrc_list = parse_netrc (path);
88 /* If nothing to do... */
91 /* Acc and password found; all OK. */
94 if (!*acc && !slack_default)
96 /* Some data not given -- try finding the host. */
97 for (l = netrc_list; l; l = l->next)
101 else if (!strcasecmp (l->host, host))
108 /* Looking for password in .netrc. */
109 if (!strcmp (l->acc, *acc))
110 *passwd = l->passwd; /* usernames match; password OK */
112 *passwd = NULL; /* usernames don't match */
116 /* If password was given, use it. The account is l->acc. */
129 /* Try looking for the default account. */
130 for (l = netrc_list; l; l = l->next)
144 /* Normally, these functions would be defined by your package. */
145 # define xmalloc malloc
146 # define xstrdup strdup
148 /* The function reads a whole line. It reads the line realloc-ing the
149 storage exponentially, doubling the storage after each overflow to
150 minimize the number of calls to realloc().
152 It is not an exemplary of correctness, since it kills off the
153 newline (and no, there is no way to know if there was a newline at
155 # define xrealloc realloc
156 # define DYNAMIC_LINE_BUFFER 40
159 read_whole_line (FILE *fp)
165 bufsize = DYNAMIC_LINE_BUFFER;
166 line = xmalloc(bufsize);
167 /* Construct the line. */
168 while ((c = getc(fp)) != EOF && c != '\n')
171 line = (char *)xrealloc(line, (bufsize <<= 1));
180 /* Check for overflow at zero-termination (no need to double the
181 buffer in this case. */
183 line = (char *)xrealloc(line, i + 1);
188 #endif /* STANDALONE */
190 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
191 set to a ready-to-use acc_t, in any event. */
193 maybe_add_to_list (acc_t **newentry, acc_t **list)
199 /* We need an account name in order to add the entry to the list. */
202 /* Free any allocated space. */
211 /* Add the current machine into our list. */
216 /* Allocate a new acc_t structure. */
217 a = (acc_t *)xmalloc (sizeof (acc_t));
220 /* Zero the structure, so that it is ready to use. */
221 memset (a, 0, sizeof(*a));
223 /* Return the new pointers. */
230 /* Parse a .netrc file (as described in the ftp(1) manual page). */
232 parse_netrc (const char *path)
235 char *line, *p, *tok, *premature_token;
236 acc_t *current, *retval;
239 /* The latest token we've seen in the file. */
242 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
243 } last_token = tok_nothing;
245 current = retval = NULL;
247 fp = fopen (path, "r");
250 fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,
251 path, strerror (errno));
255 /* Initialize the file data. */
257 premature_token = NULL;
259 /* While there are lines in the file... */
260 while ((line = read_whole_line (fp)))
264 /* Parse the line. */
267 /* If the line is empty, then end any macro definition. */
268 if (last_token == tok_macdef && !*p)
269 /* End of macro if the line is empty. */
270 last_token = tok_nothing;
272 /* If we are defining macros, then skip parsing the line. */
273 while (*p && last_token != tok_macdef)
275 /* Skip any whitespace. */
276 while (*p && ISSPACE (*p))
279 /* Discard end-of-line comments. */
285 /* Find the end of the token. */
286 while (*p && !ISSPACE (*p))
289 /* Null-terminate the token, if it isn't already. */
297 current->acc = xstrdup (tok);
299 premature_token = "login";
303 /* Start a new machine entry. */
304 maybe_add_to_list (¤t, &retval);
305 current->host = xstrdup (tok);
310 current->passwd = xstrdup (tok);
312 premature_token = "password";
315 /* We handle most of tok_macdef above. */
318 premature_token = "macdef";
321 /* We don't handle the account keyword at all. */
324 premature_token = "account";
327 /* We handle tok_nothing below this switch. */
334 fprintf (stderr, _("\
335 %s: %s:%d: warning: \"%s\" token appears before any machine name\n"),
336 exec_name, path, ln, premature_token);
337 premature_token = NULL;
340 if (last_token != tok_nothing)
341 /* We got a value, so reset the token state. */
342 last_token = tok_nothing;
345 /* Fetch the next token. */
346 if (!strcmp (tok, "account"))
347 last_token = tok_account;
348 else if (!strcmp (tok, "default"))
350 maybe_add_to_list (¤t, &retval);
352 else if (!strcmp (tok, "login"))
353 last_token = tok_login;
355 else if (!strcmp (tok, "macdef"))
356 last_token = tok_macdef;
358 else if (!strcmp (tok, "machine"))
359 last_token = tok_machine;
361 else if (!strcmp (tok, "password"))
362 last_token = tok_password;
365 fprintf (stderr, _("%s: %s:%d: unknown token \"%s\"\n"),
366 exec_name, path, ln, tok);
375 /* Finalize the last machine entry we found. */
376 maybe_add_to_list (¤t, &retval);
379 /* Reverse the order of the list so that it appears in file order. */
384 acc_t *saved_reference;
386 /* Change the direction of the pointers. */
387 saved_reference = current->next;
388 current->next = retval;
390 /* Advance to the next node. */
392 current = saved_reference;
399 /* Free a netrc list. */
409 FREE_MAYBE (l->passwd);
410 FREE_MAYBE (l->host);
417 #include <sys/types.h>
418 #include <sys/stat.h>
421 main (int argc, char **argv)
424 char *program_name, *file, *target;
427 if (argc < 2 || argc > 3)
429 fprintf (stderr, _("Usage: %s NETRC [HOSTNAME]\n"), argv[0]);
433 program_name = argv[0];
437 if (stat (file, &sb))
439 fprintf (stderr, _("%s: cannot stat %s: %s\n"), argv[0], file,
444 head = parse_netrc (file);
448 /* Skip if we have a target and this isn't it. */
449 if (target && a->host && strcmp (target, a->host))
457 /* Print the host name if we have no target. */
459 fputs (a->host, stdout);
461 fputs ("DEFAULT", stdout);
466 /* Print the account name. */
467 fputs (a->acc, stdout);
471 /* Print the password, if there is any. */
473 fputs (a->passwd, stdout);
476 fputc ('\n', stdout);
478 /* Exit if we found the target. */
484 /* Exit with failure if we had a target, success otherwise. */
490 #endif /* STANDALONE */