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")
13 #define _XOPEN_SOURCE 600
22 #include "fetchmail.h"
27 /* Normally defined in xstrdup.c. */
28 # define xstrdup strdup
30 /* Normally defined in xmalloc.c */
31 # define xmalloc malloc
32 # define xrealloc realloc
34 const char *program_name = "netrc";
37 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
38 set to a ready-to-use netrc_entry, in any event. */
40 maybe_add_to_list (netrc_entry **newentry, netrc_entry **list)
46 /* We need a login name in order to add the entry to the list. */
49 /* Free any allocated space. */
59 /* Add the current machine into our list. */
64 /* Allocate a new netrc_entry structure. */
65 a = (netrc_entry *) xmalloc (sizeof (netrc_entry));
68 /* Zero the structure, so that it is ready to use. */
69 memset (a, 0, sizeof(*a));
71 /* Return the new pointers. */
78 /* Parse FILE as a .netrc file (as described in ftp(1)), and return a
79 list of entries. NULL is returned if the file could not be
82 parse_netrc (char *file)
85 char buf[POPBUFSIZE+1], *p, *tok;
86 const char *premature_token;
87 netrc_entry *current, *retval;
90 /* The latest token we've seen in the file. */
93 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
94 } last_token = tok_nothing;
96 current = retval = NULL;
98 fp = fopen (file, "r");
101 /* Just return NULL if we can't open the file. */
105 /* Initialize the file data. */
107 premature_token = NULL;
109 /* While there are lines in the file... */
110 while (fgets(buf, sizeof(buf) - 1, fp))
114 /* Strip trailing CRLF */
115 for (p = buf + strlen(buf) - 1; (p >= buf) && isspace((unsigned char)*p); p--)
118 /* Parse the line. */
121 /* If the line is empty... */
124 if (last_token == tok_macdef) /* end of macro */
125 last_token = tok_nothing;
127 continue; /* otherwise ignore it */
130 /* If we are defining macros, then skip parsing the line. */
131 while (*p && last_token != tok_macdef)
136 /* Skip any whitespace. */
137 while (*p && isspace ((unsigned char)*p))
140 /* Discard end-of-line comments. */
146 /* Find the end of the token. */
147 while (*p && (quote_char || !isspace ((unsigned char)*p)))
151 if (quote_char == *p)
165 if (*p == '"' || *p == '\'')
175 /* Null-terminate the token, if it isn't already. */
184 current->login = (char *) xstrdup (tok);
186 premature_token = "login";
190 /* Start a new machine entry. */
191 maybe_add_to_list (¤t, &retval);
192 current->host = (char *) xstrdup (tok);
197 current->password = (char *) xstrdup (tok);
199 premature_token = "password";
202 /* We handle most of tok_macdef above. */
205 premature_token = "macdef";
208 /* We don't handle the account keyword at all. */
211 premature_token = "account";
214 /* We handle tok_nothing below this switch. */
222 GT_("%s:%d: warning: found \"%s\" before any host names\n"),
223 file, ln, premature_token);
224 premature_token = NULL;
227 if (last_token != tok_nothing)
228 /* We got a value, so reset the token state. */
229 last_token = tok_nothing;
232 /* Fetch the next token. */
233 if (!strcmp (tok, "default"))
235 maybe_add_to_list (¤t, &retval);
237 else if (!strcmp (tok, "login"))
238 last_token = tok_login;
240 else if (!strcmp (tok, "user"))
241 last_token = tok_login;
243 else if (!strcmp (tok, "macdef"))
244 last_token = tok_macdef;
246 else if (!strcmp (tok, "machine"))
247 last_token = tok_machine;
249 else if (!strcmp (tok, "password"))
250 last_token = tok_password;
252 else if (!strcmp (tok, "passwd"))
253 last_token = tok_password;
255 else if (!strcmp (tok, "account"))
256 last_token = tok_account;
260 fprintf (stderr, GT_("%s:%d: warning: unknown token \"%s\"\n"),
269 /* Finalize the last machine entry we found. */
270 maybe_add_to_list (¤t, &retval);
273 /* Reverse the order of the list so that it appears in file order. */
278 netrc_entry *saved_reference;
280 /* Change the direction of the pointers. */
281 saved_reference = current->next;
282 current->next = retval;
284 /* Advance to the next node. */
286 current = saved_reference;
293 /* Return the netrc entry from LIST corresponding to HOST. NULL is
294 returned if no such entry exists. */
296 search_netrc (netrc_entry *list, char *host, char *login)
298 /* Look for the HOST in LIST. */
301 if (list->host && !strcmp(list->host, host))
302 if (!list->login || !strcmp(list->login, login))
303 /* We found a matching entry. */
309 /* Return the matching entry, or NULL. */
314 free_netrc(netrc_entry *a) {
316 netrc_entry *n = a->next;
317 if (a->password != NULL) {
318 memset(a->password, 0x55, strlen(a->password));
329 #include <sys/types.h>
330 #include <sys/stat.h>
334 int main (int argc, char **argv)
337 char *file, *host, *login;
338 netrc_entry *head, *a;
340 program_name = argv[0];
350 fprintf (stderr, "Usage: %s <file> [<host> <login>]\n", argv[0]);
354 if (stat (file, &sb))
356 fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
361 head = parse_netrc (file);
364 fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
371 status = EXIT_SUCCESS;
373 printf("Host: %s, Login: %s\n", host, login);
375 a = search_netrc (head, host, login);
378 /* Print out the password (if any). */
381 printf("Password: %s\n", a->password);
384 status = EXIT_FAILURE;
385 fputc ('\n', stdout);
390 /* Print out the entire contents of the netrc. */
394 /* Print the host name. */
396 fputs (a->host, stdout);
398 fputs ("DEFAULT", stdout);
402 /* Print the login name. */
403 fputs (a->login, stdout);
407 /* Print the password, if there is any. */
409 fputs (a->password, stdout);
412 fputc ('\n', stdout);
420 #endif /* STANDALONE */