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 /* This file used to be kept in synch with the code in Fetchmail, but
21 the latter has diverged since. */
34 #include <sys/types.h>
46 #define NETRC_FILE_NAME ".netrc"
50 static acc_t *parse_netrc PARAMS ((const char *));
52 /* Return the correct user and password, given the host, user (as
53 given in the URL), and password (as given in the URL). May return
56 If SLACK_DEFAULT is set, allow looking for a "default" account.
57 You will typically turn it off for HTTP. */
59 search_netrc (const char *host, const char **acc, const char **passwd,
63 static int processed_netrc;
70 char *home = home_dir ();
78 char *path = (char *)alloca (strlen (home) + 1
79 + strlen (NETRC_FILE_NAME) + 1);
80 sprintf (path, "%s/%s", home, NETRC_FILE_NAME);
82 err = stat (path, &buf);
84 netrc_list = parse_netrc (path);
87 /* If nothing to do... */
90 /* Acc and password found; all OK. */
93 /* Some data not given -- try finding the host. */
94 for (l = netrc_list; l; l = l->next)
98 else if (!strcasecmp (l->host, host))
105 /* Looking for password in .netrc. */
106 if (!strcmp (l->acc, *acc))
107 *passwd = l->passwd; /* usernames match; password OK */
109 *passwd = NULL; /* usernames don't match */
113 /* If password was given, use it. The account is l->acc. */
126 /* Try looking for the default account. */
127 for (l = netrc_list; l; l = l->next)
144 /* Normally, these functions would be defined by your package. */
145 # define xmalloc malloc
147 # define xstrdup strdup
149 # define xrealloc realloc
151 /* Read a line from FP. The function reallocs the storage as needed
152 to accomodate for any length of the line. Reallocs are done
153 storage exponentially, doubling the storage after each overflow to
154 minimize the number of calls to realloc() and fgets(). The newline
155 character at the end of line is retained.
157 After end-of-file is encountered without anything being read, NULL
158 is returned. NULL is also returned on error. To distinguish
159 between these two cases, use the stdio function ferror(). */
162 read_whole_line (FILE *fp)
166 char *line = (char *)xmalloc (bufsize);
168 while (fgets (line + length, bufsize - length, fp))
170 length += strlen (line + length);
172 if (line[length - 1] == '\n')
174 /* fgets() guarantees to read the whole line, or to use up the
175 space we've given it. We can double the buffer
178 line = xrealloc (line, bufsize);
180 if (length == 0 || ferror (fp))
185 if (length + 1 < bufsize)
186 /* Relieve the memory from our exponential greediness. We say
187 `length + 1' because the terminating \0 is not included in
188 LENGTH. We don't need to zero-terminate the string ourselves,
189 though, because fgets() does that. */
190 line = xrealloc (line, length + 1);
193 #endif /* STANDALONE */
195 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
196 set to a ready-to-use acc_t, in any event. */
198 maybe_add_to_list (acc_t **newentry, acc_t **list)
204 /* We need an account name in order to add the entry to the list. */
207 /* Free any allocated space. */
216 /* Add the current machine into our list. */
221 /* Allocate a new acc_t structure. */
222 a = (acc_t *)xmalloc (sizeof (acc_t));
225 /* Zero the structure, so that it is ready to use. */
226 memset (a, 0, sizeof(*a));
228 /* Return the new pointers. */
234 /* Helper function for the parser, shifts contents of
235 null-terminated string once character to the left.
236 Used in processing \ and " constructs in the netrc file */
238 shift_left(char *string)
242 for (p=string; *p; ++p)
246 /* Parse a .netrc file (as described in the ftp(1) manual page). */
248 parse_netrc (const char *path)
251 char *line, *p, *tok, *premature_token;
252 acc_t *current, *retval;
255 /* The latest token we've seen in the file. */
258 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
259 } last_token = tok_nothing;
261 current = retval = NULL;
263 fp = fopen (path, "r");
266 fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,
267 path, strerror (errno));
271 /* Initialize the file data. */
273 premature_token = NULL;
275 /* While there are lines in the file... */
276 while ((line = read_whole_line (fp)))
280 /* Parse the line. */
284 /* Skip leading whitespace. */
285 while (*p && ISSPACE (*p))
288 /* If the line is empty, then end any macro definition. */
289 if (last_token == tok_macdef && !*p)
290 /* End of macro if the line is empty. */
291 last_token = tok_nothing;
293 /* If we are defining macros, then skip parsing the line. */
294 while (*p && last_token != tok_macdef)
296 /* Skip any whitespace. */
297 while (*p && ISSPACE (*p))
300 /* Discard end-of-line comments; also, stop processing if
301 the above `while' merely skipped trailing whitespace. */
302 if (*p == '#' || !*p)
305 /* If the token starts with quotation mark, note this fact,
306 and squash the quotation character */
314 /* Find the end of the token, handling quotes and escapes. */
315 while (*p && (quote ? *p != '"' : !ISSPACE (*p))){
321 /* if field was quoted, squash the trailing quotation mark */
325 /* Null-terminate the token, if it isn't already. */
333 current->acc = xstrdup (tok);
335 premature_token = "login";
339 /* Start a new machine entry. */
340 maybe_add_to_list (¤t, &retval);
341 current->host = xstrdup (tok);
346 current->passwd = xstrdup (tok);
348 premature_token = "password";
351 /* We handle most of tok_macdef above. */
354 premature_token = "macdef";
357 /* We don't handle the account keyword at all. */
360 premature_token = "account";
363 /* We handle tok_nothing below this switch. */
370 fprintf (stderr, _("\
371 %s: %s:%d: warning: \"%s\" token appears before any machine name\n"),
372 exec_name, path, ln, premature_token);
373 premature_token = NULL;
376 if (last_token != tok_nothing)
377 /* We got a value, so reset the token state. */
378 last_token = tok_nothing;
381 /* Fetch the next token. */
382 if (!strcmp (tok, "account"))
383 last_token = tok_account;
384 else if (!strcmp (tok, "default"))
386 maybe_add_to_list (¤t, &retval);
388 else if (!strcmp (tok, "login"))
389 last_token = tok_login;
391 else if (!strcmp (tok, "macdef"))
392 last_token = tok_macdef;
394 else if (!strcmp (tok, "machine"))
395 last_token = tok_machine;
397 else if (!strcmp (tok, "password"))
398 last_token = tok_password;
401 fprintf (stderr, _("%s: %s:%d: unknown token \"%s\"\n"),
402 exec_name, path, ln, tok);
411 /* Finalize the last machine entry we found. */
412 maybe_add_to_list (¤t, &retval);
415 /* Reverse the order of the list so that it appears in file order. */
420 acc_t *saved_reference;
422 /* Change the direction of the pointers. */
423 saved_reference = current->next;
424 current->next = retval;
426 /* Advance to the next node. */
428 current = saved_reference;
435 /* Free a netrc list. */
445 FREE_MAYBE (l->passwd);
446 FREE_MAYBE (l->host);
453 #include <sys/types.h>
454 #include <sys/stat.h>
457 main (int argc, char **argv)
460 char *program_name, *file, *target;
463 if (argc < 2 || argc > 3)
465 fprintf (stderr, _("Usage: %s NETRC [HOSTNAME]\n"), argv[0]);
469 program_name = argv[0];
473 if (stat (file, &sb))
475 fprintf (stderr, _("%s: cannot stat %s: %s\n"), argv[0], file,
480 head = parse_netrc (file);
484 /* Skip if we have a target and this isn't it. */
485 if (target && a->host && strcmp (target, a->host))
493 /* Print the host name if we have no target. */
495 fputs (a->host, stdout);
497 fputs ("DEFAULT", stdout);
502 /* Print the account name. */
503 fputs (a->acc, stdout);
507 /* Print the password, if there is any. */
509 fputs (a->passwd, stdout);
512 fputc ('\n', stdout);
514 /* Exit if we found the target. */
520 /* Exit with failure if we had a target, success otherwise. */
526 #endif /* STANDALONE */