1 /* netrc.c -- parse the .netrc file to get hosts, accounts, and passwords
3 Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
5 For license terms, see the file COPYING in this directory.
7 Compile with -DSTANDALONE to test this module. */
15 #include "fetchmail.h"
20 /* Normally defined in xstrdup.c. */
21 # define xstrdup strdup
23 /* Normally defined in xmalloc.c */
24 # define xmalloc malloc
25 # define xrealloc realloc
27 char *program_name = "netrc";
30 /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is
31 set to a ready-to-use netrc_entry, in any event. */
33 maybe_add_to_list (netrc_entry **newentry, netrc_entry **list)
39 /* We need an account name in order to add the entry to the list. */
40 if (a && ! a->account)
42 /* Free any allocated space. */
52 /* Add the current machine into our list. */
57 /* Allocate a new netrc_entry structure. */
58 a = (netrc_entry *) xmalloc (sizeof (netrc_entry));
61 /* Zero the structure, so that it is ready to use. */
62 memset (a, 0, sizeof(*a));
64 /* Return the new pointers. */
71 /* Parse FILE as a .netrc file (as described in ftp(1)), and return a
72 list of entries. NULL is returned if the file could not be
79 char buf[POPBUFSIZE+1], *p, *tok;
80 const char *premature_token;
81 netrc_entry *current, *retval;
84 /* The latest token we've seen in the file. */
87 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
88 } last_token = tok_nothing;
90 current = retval = NULL;
92 fp = fopen (file, "r");
95 /* Just return NULL if we can't open the file. */
99 /* Initialize the file data. */
101 premature_token = NULL;
103 /* While there are lines in the file... */
104 while (fgets(buf, POPBUFSIZE, fp))
108 /* Strip trailing CRLF */
109 for (p = buf + strlen(buf) - 1; (p >= buf) && isspace(*p); p--)
112 /* Parse the line. */
115 /* If the line is empty... */
118 if (last_token == tok_macdef) /* end of macro */
119 last_token = tok_nothing;
121 continue; /* otherwise ignore it */
124 /* If we are defining macros, then skip parsing the line. */
125 while (*p && last_token != tok_macdef)
130 /* Skip any whitespace. */
131 while (*p && isspace (*p))
134 /* Discard end-of-line comments. */
140 /* Find the end of the token. */
141 while (*p && (quote_char || !isspace (*p)))
145 if (quote_char == *p)
159 if (*p == '"' || *p == '\'')
169 /* Null-terminate the token, if it isn't already. */
178 current->account = (char *) xstrdup (tok);
180 premature_token = "login";
184 /* Start a new machine entry. */
185 maybe_add_to_list (¤t, &retval);
186 current->host = (char *) xstrdup (tok);
191 current->password = (char *) xstrdup (tok);
193 premature_token = "password";
196 /* We handle most of tok_macdef above. */
199 premature_token = "macdef";
202 /* We don't handle the account keyword at all. */
205 premature_token = "account";
208 /* We handle tok_nothing below this switch. */
216 error_at_line (0, file, ln,
217 _("warning: found \"%s\" before any host names"),
221 _("%s:%d: warning: found \"%s\" before any host names\n"),
222 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, _("%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 (list, host, account)
298 char *host, *account;
300 /* Look for the HOST in LIST. */
303 if (list->host && !strcmp(list->host, host))
304 if (!list->account || !strcmp(list->account, account))
305 /* We found a matching entry. */
311 /* Return the matching entry, or NULL. */
317 #include <sys/types.h>
318 #include <sys/stat.h>
328 char *program_name, *file, *host, *account;
329 netrc_entry *head, *a;
331 program_name = argv[0];
336 if (stat (file, &sb))
338 fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
343 head = parse_netrc (file);
346 fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
355 printf("Host: %s, Account: %s\n", host, account);
357 a = search_netrc (head, host, account);
360 /* Print out the password (if any). */
364 fputs (a->password, stdout);
367 fputc ('\n', stdout);
372 /* Print out the entire contents of the netrc. */
376 /* Print the host name. */
378 fputs (a->host, stdout);
380 fputs ("DEFAULT", stdout);
384 /* Print the account name. */
385 fputs (a->account, stdout);
389 /* Print the password, if there is any. */
391 fputs (a->password, stdout);
394 fputc ('\n', stdout);
400 #endif /* STANDALONE */