1 /* Read and parse the .netrc file to get hosts, accounts, and passwords.
2 Copyright (C) 1996, 2007, 2008, 2009 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 3 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, see <http://www.gnu.org/licenses/>.
19 Additional permission under GNU GPL version 3 section 7
21 If you modify this program, or any covered work, by linking or
22 combining it with the OpenSSL project's OpenSSL library (or a
23 modified version of that library), containing parts covered by the
24 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
25 grants you additional permission to convey the resulting work.
26 Corresponding Source for a non-source form of such a combination
27 shall include the source code for the parts of OpenSSL used as well
28 as that of the covered work. */
30 /* This file used to be kept in synch with the code in Fetchmail, but
31 the latter has diverged since. */
44 #define NETRC_FILE_NAME ".netrc"
48 static acc_t *parse_netrc (const char *);
50 /* Return the correct user and password, given the host, user (as
51 given in the URL), and password (as given in the URL). May return
54 If SLACK_DEFAULT is set, allow looking for a "default" account.
55 You will typically turn it off for HTTP. */
57 search_netrc (const char *host, const char **acc, const char **passwd,
61 static int processed_netrc;
72 char *path = "SYS$LOGIN:.netrc";
77 err = stat (path, &buf);
79 netrc_list = parse_netrc (path);
83 char *home = home_dir ();
91 char *path = (char *)alloca (strlen (home) + 1
92 + strlen (NETRC_FILE_NAME) + 1);
93 sprintf (path, "%s/%s", home, NETRC_FILE_NAME);
95 err = stat (path, &buf);
97 netrc_list = parse_netrc (path);
100 #endif /* def __VMS [else] */
102 /* If nothing to do... */
105 /* Acc and password found; all OK. */
108 /* Some data not given -- try finding the host. */
109 for (l = netrc_list; l; l = l->next)
113 else if (!strcasecmp (l->host, host))
120 /* Looking for password in .netrc. */
121 if (!strcmp (l->acc, *acc))
122 *passwd = l->passwd; /* usernames match; password OK */
124 *passwd = NULL; /* usernames don't match */
128 /* If password was given, use it. The account is l->acc. */
141 /* Try looking for the default account. */
142 for (l = netrc_list; l; l = l->next)
159 /* Normally, these functions would be defined by your package. */
160 # define xmalloc malloc
162 # define xstrdup strdup
164 # define xrealloc realloc
166 /* Read a line from FP. The function reallocs the storage as needed
167 to accomodate for any length of the line. Reallocs are done
168 storage exponentially, doubling the storage after each overflow to
169 minimize the number of calls to realloc() and fgets(). The newline
170 character at the end of line is retained.
172 After end-of-file is encountered without anything being read, NULL
173 is returned. NULL is also returned on error. To distinguish
174 between these two cases, use the stdio function ferror(). */
177 read_whole_line (FILE *fp)
181 char *line = xmalloc (bufsize);
183 while (fgets (line + length, bufsize - length, fp))
185 length += strlen (line + length);
187 if (line[length - 1] == '\n')
189 /* fgets() guarantees to read the whole line, or to use up the
190 space we've given it. We can double the buffer
193 line = xrealloc (line, bufsize);
195 if (length == 0 || ferror (fp))
200 if (length + 1 < bufsize)
201 /* Relieve the memory from our exponential greediness. We say
202 `length + 1' because the terminating \0 is not included in
203 LENGTH. We don't need to zero-terminate the string ourselves,
204 though, because fgets() does that. */
205 line = xrealloc (line, length + 1);
208 #endif /* STANDALONE */
210 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
211 set to a ready-to-use acc_t, in any event. */
213 maybe_add_to_list (acc_t **newentry, acc_t **list)
219 /* We need an account name in order to add the entry to the list. */
222 /* Free any allocated space. */
223 xfree_null (a->host);
225 xfree_null (a->passwd);
231 /* Add the current machine into our list. */
236 /* Allocate a new acc_t structure. */
237 a = xmalloc (sizeof (acc_t));
240 /* Zero the structure, so that it is ready to use. */
241 memset (a, 0, sizeof(*a));
243 /* Return the new pointers. */
249 /* Helper function for the parser, shifts contents of
250 null-terminated string once character to the left.
251 Used in processing \ and " constructs in the netrc file */
253 shift_left(char *string)
257 for (p=string; *p; ++p)
261 /* Parse a .netrc file (as described in the ftp(1) manual page). */
263 parse_netrc (const char *path)
266 char *line, *p, *tok;
267 const char *premature_token;
268 acc_t *current, *retval;
271 /* The latest token we've seen in the file. */
274 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
275 } last_token = tok_nothing;
277 current = retval = NULL;
279 fp = fopen (path, "r");
282 fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,
283 path, strerror (errno));
287 /* Initialize the file data. */
289 premature_token = NULL;
291 /* While there are lines in the file... */
292 while ((line = read_whole_line (fp)) != NULL)
296 /* Parse the line. */
300 /* Skip leading whitespace. */
301 while (*p && c_isspace (*p))
304 /* If the line is empty, then end any macro definition. */
305 if (last_token == tok_macdef && !*p)
306 /* End of macro if the line is empty. */
307 last_token = tok_nothing;
309 /* If we are defining macros, then skip parsing the line. */
310 while (*p && last_token != tok_macdef)
312 /* Skip any whitespace. */
313 while (*p && c_isspace (*p))
316 /* Discard end-of-line comments; also, stop processing if
317 the above `while' merely skipped trailing whitespace. */
318 if (*p == '#' || !*p)
321 /* If the token starts with quotation mark, note this fact,
322 and squash the quotation character */
330 /* Find the end of the token, handling quotes and escapes. */
331 while (*p && (qmark ? *p != '"' : !c_isspace (*p))){
337 /* If field was quoted, squash the trailing quotation mark
338 and reset qmark flag. */
345 /* Null-terminate the token, if it isn't already. */
353 current->acc = xstrdup (tok);
355 premature_token = "login";
359 /* Start a new machine entry. */
360 maybe_add_to_list (¤t, &retval);
361 current->host = xstrdup (tok);
366 current->passwd = xstrdup (tok);
368 premature_token = "password";
371 /* We handle most of tok_macdef above. */
374 premature_token = "macdef";
377 /* We don't handle the account keyword at all. */
380 premature_token = "account";
383 /* We handle tok_nothing below this switch. */
390 fprintf (stderr, _("\
391 %s: %s:%d: warning: %s token appears before any machine name\n"),
392 exec_name, path, ln, quote (premature_token));
393 premature_token = NULL;
396 if (last_token != tok_nothing)
397 /* We got a value, so reset the token state. */
398 last_token = tok_nothing;
401 /* Fetch the next token. */
402 if (!strcmp (tok, "account"))
403 last_token = tok_account;
404 else if (!strcmp (tok, "default"))
406 maybe_add_to_list (¤t, &retval);
408 else if (!strcmp (tok, "login"))
409 last_token = tok_login;
411 else if (!strcmp (tok, "macdef"))
412 last_token = tok_macdef;
414 else if (!strcmp (tok, "machine"))
415 last_token = tok_machine;
417 else if (!strcmp (tok, "password"))
418 last_token = tok_password;
421 fprintf (stderr, _("%s: %s:%d: unknown token \"%s\"\n"),
422 exec_name, path, ln, tok);
431 /* Finalize the last machine entry we found. */
432 maybe_add_to_list (¤t, &retval);
435 /* Reverse the order of the list so that it appears in file order. */
440 acc_t *saved_reference;
442 /* Change the direction of the pointers. */
443 saved_reference = current->next;
444 current->next = retval;
446 /* Advance to the next node. */
448 current = saved_reference;
455 /* Free a netrc list. */
465 xfree_null (l->passwd);
466 xfree_null (l->host);
473 #include <sys/types.h>
474 #include <sys/stat.h>
477 main (int argc, char **argv)
480 char *program_name, *file, *target;
483 if (argc < 2 || argc > 3)
485 fprintf (stderr, _("Usage: %s NETRC [HOSTNAME]\n"), argv[0]);
489 program_name = argv[0];
493 if (stat (file, &sb))
495 fprintf (stderr, _("%s: cannot stat %s: %s\n"), argv[0], file,
500 head = parse_netrc (file);
504 /* Skip if we have a target and this isn't it. */
505 if (target && a->host && strcmp (target, a->host))
513 /* Print the host name if we have no target. */
515 fputs (a->host, stdout);
517 fputs ("DEFAULT", stdout);
522 /* Print the account name. */
523 fputs (a->acc, stdout);
527 /* Print the password, if there is any. */
529 fputs (a->passwd, stdout);
532 fputc ('\n', stdout);
534 /* Exit if we found the target. */
540 /* Exit with failure if we had a target, success otherwise. */
546 #endif /* STANDALONE */