1 /* netrc.c -- parse the .netrc file to get hosts, accounts, and passwords
2 Copyright (C) 1996, Free Software Foundation, Inc.
3 Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 /* Compile with -DSTANDALONE to test this module. */
27 #include "fetchmail.h"
31 /* Normally defined in xstrdup.c. */
32 # define xstrdup strdup
34 /* Normally defined in xmalloc.c */
35 # define xmalloc malloc
36 # define xrealloc realloc
39 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
40 set to a ready-to-use netrc_entry, in any event. */
42 maybe_add_to_list (newentry, list)
43 netrc_entry **newentry;
50 /* We need an account name in order to add the entry to the list. */
51 if (a && ! a->account)
53 /* Free any allocated space. */
63 /* Add the current machine into our list. */
68 /* Allocate a new netrc_entry structure. */
69 a = (netrc_entry *) xmalloc (sizeof (netrc_entry));
72 /* Zero the structure, so that it is ready to use. */
73 memset (a, 0, sizeof(*a));
75 /* Return the new pointers. */
82 /* Parse FILE as a .netrc file (as described in ftp(1)), and return a
83 list of entries. NULL is returned if the file could not be
90 char buf[POPBUFSIZE+1], *p, *tok, *premature_token;
91 netrc_entry *current, *retval;
94 /* The latest token we've seen in the file. */
97 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
98 } last_token = tok_nothing;
100 current = retval = NULL;
102 fp = fopen (file, "r");
105 /* Just return NULL if we can't open the file. */
109 /* Initialize the file data. */
111 premature_token = NULL;
113 /* While there are lines in the file... */
114 while (fgets(buf, POPBUFSIZE, fp))
118 /* Strip trailing CRLF */
119 p = buf + strlen(buf) - 1;
120 while (*p && isspace(*p))
123 /* Parse the line. */
126 /* If the line is empty... */
128 if (last_token == tok_macdef) /* end of macro */
129 last_token = tok_nothing;
131 continue; /* otherwise ignore it */
133 /* If we are defining macros, then skip parsing the line. */
134 while (*p && last_token != tok_macdef)
139 /* Skip any whitespace. */
140 while (*p && isspace (*p))
143 /* Discard end-of-line comments. */
149 /* Find the end of the token. */
150 while (*p && (quote_char || !isspace (*p)))
154 if (quote_char == *p)
168 if (*p == '"' || *p == '\'')
178 /* Null-terminate the token, if it isn't already. */
187 current->account = (char *) xstrdup (tok);
189 premature_token = "login";
193 /* Start a new machine entry. */
194 maybe_add_to_list (¤t, &retval);
195 current->host = (char *) xstrdup (tok);
200 current->password = (char *) xstrdup (tok);
202 premature_token = "password";
205 /* We handle most of tok_macdef above. */
208 premature_token = "macdef";
211 /* We don't handle the account keyword at all. */
214 premature_token = "account";
217 /* We handle tok_nothing below this switch. */
225 error_at_line (0, 0, file, ln,
226 _("warning: found \"%s\" before any host names"),
230 "%s:%d: warning: found \"%s\" before any host names\n",
231 file, ln, premature_token);
233 premature_token = NULL;
236 if (last_token != tok_nothing)
237 /* We got a value, so reset the token state. */
238 last_token = tok_nothing;
241 /* Fetch the next token. */
242 if (!strcmp (tok, "account"))
243 last_token = tok_account;
245 if (!strcmp (tok, "default"))
247 maybe_add_to_list (¤t, &retval);
249 else if (!strcmp (tok, "login"))
250 last_token = tok_login;
252 else if (!strcmp (tok, "user"))
253 last_token = tok_login;
255 else if (!strcmp (tok, "macdef"))
256 last_token = tok_macdef;
258 else if (!strcmp (tok, "machine"))
259 last_token = tok_machine;
261 else if (!strcmp (tok, "password"))
262 last_token = tok_password;
266 fprintf (stderr, "%s:%d: warning: unknown token \"%s\"\n",
275 /* Finalize the last machine entry we found. */
276 maybe_add_to_list (¤t, &retval);
279 /* Reverse the order of the list so that it appears in file order. */
284 netrc_entry *saved_reference;
286 /* Change the direction of the pointers. */
287 saved_reference = current->next;
288 current->next = retval;
290 /* Advance to the next node. */
292 current = saved_reference;
299 /* Return the netrc entry from LIST corresponding to HOST. NULL is
300 returned if no such entry exists. */
302 search_netrc (list, host)
306 /* Look for the HOST in LIST. */
310 /* We hit the default entry. */
313 else if (!strcmp (list->host, host))
314 /* We found a matching entry. */
320 /* Return the matching entry, or NULL. */
326 #include <sys/types.h>
327 #include <sys/stat.h>
337 char *program_name, *file, *target;
338 netrc_entry *head, *a;
342 fprintf (stderr, "Usage: %s NETRC [HOSTNAME]...\n", argv[0]);
346 program_name = argv[0];
350 if (stat (file, &sb))
352 fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
357 head = parse_netrc (file);
360 fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
368 for (i = 2; i < argc; i++)
370 /* Print out the host that we are checking for. */
371 fputs (argv[i], stdout);
373 a = search_netrc (head, argv[i]);
376 /* Print out the account and password (if any). */
378 fputs (a->account, stdout);
382 fputs (a->password, stdout);
388 fputc ('\n', stdout);
393 /* Print out the entire contents of the netrc. */
397 /* Print the host name. */
399 fputs (a->host, stdout);
401 fputs ("DEFAULT", stdout);
405 /* Print the account name. */
406 fputs (a->account, stdout);
410 /* Print the password, if there is any. */
412 fputs (a->password, stdout);
415 fputc ('\n', stdout);
421 #endif /* STANDALONE */