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... */
117 if (last_token == tok_macdef) /* end of macro */
118 last_token = tok_nothing;
120 continue; /* otherwise ignore it */
122 /* If we are defining macros, then skip parsing the line. */
123 while (*p && last_token != tok_macdef)
128 /* Skip any whitespace. */
129 while (*p && isspace (*p))
132 /* Discard end-of-line comments. */
138 /* Find the end of the token. */
139 while (*p && (quote_char || !isspace (*p)))
143 if (quote_char == *p)
157 if (*p == '"' || *p == '\'')
167 /* Null-terminate the token, if it isn't already. */
176 current->account = (char *) xstrdup (tok);
178 premature_token = "login";
182 /* Start a new machine entry. */
183 maybe_add_to_list (¤t, &retval);
184 current->host = (char *) xstrdup (tok);
189 current->password = (char *) xstrdup (tok);
191 premature_token = "password";
194 /* We handle most of tok_macdef above. */
197 premature_token = "macdef";
200 /* We don't handle the account keyword at all. */
203 premature_token = "account";
206 /* We handle tok_nothing below this switch. */
214 error_at_line (0, file, ln,
215 _("warning: found \"%s\" before any host names"),
219 _("%s:%d: warning: found \"%s\" before any host names\n"),
220 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, _("%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 (list, host, account)
296 char *host, *account;
298 /* Look for the HOST in LIST. */
302 /* We hit the default entry. */
305 else if (!strcmp(list->host, host))
306 if (!list->account || !strcmp(list->account, account))
307 /* We found a matching entry. */
313 /* Return the matching entry, or NULL. */
319 #include <sys/types.h>
320 #include <sys/stat.h>
330 char *program_name, *file, *host, *account;
331 netrc_entry *head, *a;
333 program_name = argv[0];
338 if (stat (file, &sb))
340 fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
345 head = parse_netrc (file);
348 fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
357 printf("Host: %s, Account: %s\n", host, account);
359 a = search_netrc (head, host, account);
362 /* Print out the password (if any). */
366 fputs (a->password, stdout);
369 fputc ('\n', stdout);
374 /* Print out the entire contents of the netrc. */
378 /* Print the host name. */
380 fputs (a->host, stdout);
382 fputs ("DEFAULT", stdout);
386 /* Print the account name. */
387 fputs (a->account, stdout);
391 /* Print the password, if there is any. */
393 fputs (a->password, stdout);
396 fputc ('\n', stdout);
402 #endif /* STANDALONE */