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)
136 /* Skip any whitespace. */
137 while (*p && isspace (*p))
140 /* Discard end-of-line comments. */
146 /* Find the end of the token. */
147 while (*p && !isspace (*p))
150 /* Null-terminate the token, if it isn't already. */
158 current->account = (char *) xstrdup (tok);
160 premature_token = "login";
164 /* Start a new machine entry. */
165 maybe_add_to_list (¤t, &retval);
166 current->host = (char *) xstrdup (tok);
171 current->password = (char *) xstrdup (tok);
173 premature_token = "password";
176 /* We handle most of tok_macdef above. */
179 premature_token = "macdef";
182 /* We don't handle the account keyword at all. */
185 premature_token = "account";
188 /* We handle tok_nothing below this switch. */
196 error_at_line (0, 0, file, ln,
197 _("warning: found \"%s\" before any host names"),
201 "%s:%d: warning: found \"%s\" before any host names\n",
202 file, ln, premature_token);
204 premature_token = NULL;
207 if (last_token != tok_nothing)
208 /* We got a value, so reset the token state. */
209 last_token = tok_nothing;
212 /* Fetch the next token. */
213 if (!strcmp (tok, "account"))
214 last_token = tok_account;
216 if (!strcmp (tok, "default"))
218 maybe_add_to_list (¤t, &retval);
220 else if (!strcmp (tok, "login"))
221 last_token = tok_login;
223 else if (!strcmp (tok, "macdef"))
224 last_token = tok_macdef;
226 else if (!strcmp (tok, "machine"))
227 last_token = tok_machine;
229 else if (!strcmp (tok, "password"))
230 last_token = tok_password;
234 fprintf (stderr, "%s:%d: warning: unknown token \"%s\"\n",
243 /* Finalize the last machine entry we found. */
244 maybe_add_to_list (¤t, &retval);
247 /* Reverse the order of the list so that it appears in file order. */
252 netrc_entry *saved_reference;
254 /* Change the direction of the pointers. */
255 saved_reference = current->next;
256 current->next = retval;
258 /* Advance to the next node. */
260 current = saved_reference;
267 /* Return the netrc entry from LIST corresponding to HOST. NULL is
268 returned if no such entry exists. */
270 search_netrc (list, host)
274 /* Look for the HOST in LIST. */
278 /* We hit the default entry. */
281 else if (!strcmp (list->host, host))
282 /* We found a matching entry. */
288 /* Return the matching entry, or NULL. */
294 #include <sys/types.h>
295 #include <sys/stat.h>
305 char *program_name, *file, *target;
306 netrc_entry *head, *a;
310 fprintf (stderr, "Usage: %s NETRC [HOSTNAME]...\n", argv[0]);
314 program_name = argv[0];
318 if (stat (file, &sb))
320 fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
325 head = parse_netrc (file);
328 fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
336 for (i = 2; i < argc; i++)
338 /* Print out the host that we are checking for. */
339 fputs (argv[i], stdout);
341 a = search_netrc (head, argv[i]);
344 /* Print out the account and password (if any). */
346 fputs (a->account, stdout);
350 fputs (a->password, stdout);
356 fputc ('\n', stdout);
361 /* Print out the entire contents of the netrc. */
365 /* Print the host name. */
367 fputs (a->host, stdout);
369 fputs ("DEFAULT", stdout);
373 /* Print the account name. */
374 fputs (a->account, stdout);
378 /* Print the password, if there is any. */
380 fputs (a->password, stdout);
383 fputc ('\n', stdout);
389 #endif /* STANDALONE */