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 (netrc_entry **newentry, netrc_entry **list)
48 /* We need an account name in order to add the entry to the list. */
49 if (a && ! a->account)
51 /* Free any allocated space. */
61 /* Add the current machine into our list. */
66 /* Allocate a new netrc_entry structure. */
67 a = (netrc_entry *) xmalloc (sizeof (netrc_entry));
70 /* Zero the structure, so that it is ready to use. */
71 memset (a, 0, sizeof(*a));
73 /* Return the new pointers. */
80 /* Parse FILE as a .netrc file (as described in ftp(1)), and return a
81 list of entries. NULL is returned if the file could not be
88 char buf[POPBUFSIZE+1], *p, *tok;
89 const char *premature_token;
90 netrc_entry *current, *retval;
93 /* The latest token we've seen in the file. */
96 tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
97 } last_token = tok_nothing;
99 current = retval = NULL;
101 fp = fopen (file, "r");
104 /* Just return NULL if we can't open the file. */
108 /* Initialize the file data. */
110 premature_token = NULL;
112 /* While there are lines in the file... */
113 while (fgets(buf, POPBUFSIZE, fp))
117 /* Strip trailing CRLF */
118 p = buf + strlen(buf) - 1;
119 while (*p && isspace(*p))
122 /* Parse the line. */
125 /* If the line is empty... */
127 if (last_token == tok_macdef) /* end of macro */
128 last_token = tok_nothing;
130 continue; /* otherwise ignore it */
132 /* If we are defining macros, then skip parsing the line. */
133 while (*p && last_token != tok_macdef)
138 /* Skip any whitespace. */
139 while (*p && isspace (*p))
142 /* Discard end-of-line comments. */
148 /* Find the end of the token. */
149 while (*p && (quote_char || !isspace (*p)))
153 if (quote_char == *p)
167 if (*p == '"' || *p == '\'')
177 /* Null-terminate the token, if it isn't already. */
186 current->account = (char *) xstrdup (tok);
188 premature_token = "login";
192 /* Start a new machine entry. */
193 maybe_add_to_list (¤t, &retval);
194 current->host = (char *) xstrdup (tok);
199 current->password = (char *) xstrdup (tok);
201 premature_token = "password";
204 /* We handle most of tok_macdef above. */
207 premature_token = "macdef";
210 /* We don't handle the account keyword at all. */
213 premature_token = "account";
216 /* We handle tok_nothing below this switch. */
224 error_at_line (0, 0, file, ln,
225 _("warning: found \"%s\" before any host names"),
229 "%s:%d: warning: found \"%s\" before any host names\n",
230 file, ln, premature_token);
232 premature_token = NULL;
235 if (last_token != tok_nothing)
236 /* We got a value, so reset the token state. */
237 last_token = tok_nothing;
240 /* Fetch the next token. */
241 if (!strcmp (tok, "default"))
243 maybe_add_to_list (¤t, &retval);
245 else if (!strcmp (tok, "login"))
246 last_token = tok_login;
248 else if (!strcmp (tok, "user"))
249 last_token = tok_login;
251 else if (!strcmp (tok, "macdef"))
252 last_token = tok_macdef;
254 else if (!strcmp (tok, "machine"))
255 last_token = tok_machine;
257 else if (!strcmp (tok, "password"))
258 last_token = tok_password;
260 else if (!strcmp (tok, "account"))
261 last_token = tok_account;
265 fprintf (stderr, "%s:%d: warning: unknown token \"%s\"\n",
274 /* Finalize the last machine entry we found. */
275 maybe_add_to_list (¤t, &retval);
278 /* Reverse the order of the list so that it appears in file order. */
283 netrc_entry *saved_reference;
285 /* Change the direction of the pointers. */
286 saved_reference = current->next;
287 current->next = retval;
289 /* Advance to the next node. */
291 current = saved_reference;
298 /* Return the netrc entry from LIST corresponding to HOST. NULL is
299 returned if no such entry exists. */
301 search_netrc (list, host)
305 /* Look for the HOST in LIST. */
309 /* We hit the default entry. */
312 else if (!strcmp (list->host, host))
313 /* We found a matching entry. */
319 /* Return the matching entry, or NULL. */
325 #include <sys/types.h>
326 #include <sys/stat.h>
336 char *program_name, *file, *target;
337 netrc_entry *head, *a;
341 fprintf (stderr, "Usage: %s NETRC [HOSTNAME]...\n", argv[0]);
345 program_name = argv[0];
349 if (stat (file, &sb))
351 fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
356 head = parse_netrc (file);
359 fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
367 for (i = 2; i < argc; i++)
369 /* Print out the host that we are checking for. */
370 fputs (argv[i], stdout);
372 a = search_netrc (head, argv[i]);
375 /* Print out the account and password (if any). */
377 fputs (a->account, stdout);
381 fputs (a->password, stdout);
387 fputc ('\n', stdout);
392 /* Print out the entire contents of the netrc. */
396 /* Print the host name. */
398 fputs (a->host, stdout);
400 fputs ("DEFAULT", stdout);
404 /* Print the account name. */
405 fputs (a->account, stdout);
409 /* Print the password, if there is any. */
411 fputs (a->password, stdout);
414 fputc ('\n', stdout);
420 #endif /* STANDALONE */