2 * netrc.c -- parse the .netrc file to get hosts, accounts, and passwords
4 Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
5 Copyright assigned to Eric S. Raymond, October 2001.
7 For license terms, see the file COPYING in this directory.
9 Compile with -DSTANDALONE to test this module.
10 (Makefile.am should have a rule so you can just type "make netrc")
20 #include "fetchmail.h"
25 /* Normally defined in xstrdup.c. */
26 # define xstrdup strdup
28 /* Normally defined in xmalloc.c */
29 # define xmalloc malloc
30 # define xrealloc realloc
32 const char *program_name = "netrc";
35 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
36 set to a ready-to-use netrc_entry, in any event. */
38 maybe_add_to_list (netrc_entry **newentry, netrc_entry **list)
44 /* We need a login name in order to add the entry to the list. */
47 /* Free any allocated space. */
57 /* Add the current machine into our list. */
62 /* Allocate a new netrc_entry structure. */
63 a = (netrc_entry *) xmalloc (sizeof (netrc_entry));
66 /* Zero the structure, so that it is ready to use. */
67 memset (a, 0, sizeof(*a));
69 /* Return the new pointers. */
76 /* Parse FILE as a .netrc file (as described in ftp(1)), and return a
77 list of entries. NULL is returned if the file could not be
80 parse_netrc (char *file)
83 char buf[POPBUFSIZE+1], *p, *tok;
84 const char *premature_token;
85 netrc_entry *current, *retval;
88 /* The latest token we've seen in the file. */
91 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
92 } last_token = tok_nothing;
94 current = retval = NULL;
96 fp = fopen (file, "r");
99 /* Just return NULL if we can't open the file. */
103 /* Initialize the file data. */
105 premature_token = NULL;
107 /* While there are lines in the file... */
108 while (fgets(buf, sizeof(buf) - 1, fp))
112 /* Strip trailing CRLF */
113 for (p = buf + strlen(buf) - 1; (p >= buf) && isspace((unsigned char)*p); p--)
116 /* Parse the line. */
119 /* If the line is empty... */
122 if (last_token == tok_macdef) /* end of macro */
123 last_token = tok_nothing;
125 continue; /* otherwise ignore it */
128 /* If we are defining macros, then skip parsing the line. */
129 while (*p && last_token != tok_macdef)
134 /* Skip any whitespace. */
135 while (*p && isspace ((unsigned char)*p))
138 /* Discard end-of-line comments. */
144 /* Find the end of the token. */
145 while (*p && (quote_char || !isspace ((unsigned char)*p)))
149 if (quote_char == *p)
163 if (*p == '"' || *p == '\'')
173 /* Null-terminate the token, if it isn't already. */
182 current->login = (char *) xstrdup (tok);
184 premature_token = "login";
188 /* Start a new machine entry. */
189 maybe_add_to_list (¤t, &retval);
190 current->host = (char *) xstrdup (tok);
195 current->password = (char *) xstrdup (tok);
197 premature_token = "password";
200 /* We handle most of tok_macdef above. */
203 premature_token = "macdef";
206 /* We don't handle the account keyword at all. */
209 premature_token = "account";
212 /* We handle tok_nothing below this switch. */
220 GT_("%s:%d: warning: found \"%s\" before any host names\n"),
221 file, ln, premature_token);
222 premature_token = NULL;
225 if (last_token != tok_nothing)
226 /* We got a value, so reset the token state. */
227 last_token = tok_nothing;
230 /* Fetch the next token. */
231 if (!strcmp (tok, "default"))
233 maybe_add_to_list (¤t, &retval);
235 else if (!strcmp (tok, "login"))
236 last_token = tok_login;
238 else if (!strcmp (tok, "user"))
239 last_token = tok_login;
241 else if (!strcmp (tok, "macdef"))
242 last_token = tok_macdef;
244 else if (!strcmp (tok, "machine"))
245 last_token = tok_machine;
247 else if (!strcmp (tok, "password"))
248 last_token = tok_password;
250 else if (!strcmp (tok, "passwd"))
251 last_token = tok_password;
253 else if (!strcmp (tok, "account"))
254 last_token = tok_account;
258 fprintf (stderr, GT_("%s:%d: warning: unknown token \"%s\"\n"),
267 /* Finalize the last machine entry we found. */
268 maybe_add_to_list (¤t, &retval);
271 /* Reverse the order of the list so that it appears in file order. */
276 netrc_entry *saved_reference;
278 /* Change the direction of the pointers. */
279 saved_reference = current->next;
280 current->next = retval;
282 /* Advance to the next node. */
284 current = saved_reference;
291 /* Return the netrc entry from LIST corresponding to HOST. NULL is
292 returned if no such entry exists. */
294 search_netrc (netrc_entry *list, char *host, char *login)
296 /* Look for the HOST in LIST. */
299 if (list->host && !strcmp(list->host, host))
300 if (!list->login || !strcmp(list->login, login))
301 /* We found a matching entry. */
307 /* Return the matching entry, or NULL. */
312 free_netrc(netrc_entry *a) {
314 netrc_entry *n = a->next;
315 if (a->password != NULL) {
316 memset(a->password, 0x55, strlen(a->password));
327 #include <sys/types.h>
328 #include <sys/stat.h>
332 int main (int argc, char **argv)
335 char *file, *host, *login;
336 netrc_entry *head, *a;
338 program_name = argv[0];
348 fprintf (stderr, "Usage: %s <file> [<host> <login>]\n", argv[0]);
352 if (stat (file, &sb))
354 fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
359 head = parse_netrc (file);
362 fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
369 status = EXIT_SUCCESS;
371 printf("Host: %s, Login: %s\n", host, login);
373 a = search_netrc (head, host, login);
376 /* Print out the password (if any). */
379 printf("Password: %s\n", a->password);
382 status = EXIT_FAILURE;
383 fputc ('\n', stdout);
388 /* Print out the entire contents of the netrc. */
392 /* Print the host name. */
394 fputs (a->host, stdout);
396 fputs ("DEFAULT", stdout);
400 /* Print the login name. */
401 fputs (a->login, stdout);
405 /* Print the password, if there is any. */
407 fputs (a->password, stdout);
410 fputc ('\n', stdout);
418 #endif /* STANDALONE */