X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=netrc.c;h=5af542735349214d901c9f0f0f0ad5f366ede832;hb=33cddbff323efcbae1503e91e6e65b2733da80c7;hp=70f3361db6472a378a87a922b3d454e4762bcb84;hpb=b5e4dc5abc12941cb0695e4d69d5ac2a2f21406f;p=~andy%2Ffetchmail diff --git a/netrc.c b/netrc.c index 70f3361d..5af54273 100644 --- a/netrc.c +++ b/netrc.c @@ -1,20 +1,27 @@ -/* netrc.c -- parse the .netrc file to get hosts, accounts, and passwords - +/* + * netrc.c -- parse the .netrc file to get hosts, accounts, and passwords + * Gordon Matzigkeit , 1996 + Copyright assigned to Eric S. Raymond, October 2001. For license terms, see the file COPYING in this directory. - Compile with -DSTANDALONE to test this module. */ + Compile with -DSTANDALONE to test this module. + (Makefile.am should have a rule so you can just type "make netrc") +*/ + +#define _XOPEN_SOURCE 600 + +#include "config.h" #include #include #include #include -#include "config.h" #include "fetchmail.h" #include "netrc.h" -#include "i18n.h" +#include "gettext.h" #ifdef STANDALONE /* Normally defined in xstrdup.c. */ @@ -24,7 +31,7 @@ # define xmalloc malloc # define xrealloc realloc -char *program_name = "netrc"; +const char *program_name = "netrc"; #endif /* Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is @@ -36,8 +43,8 @@ maybe_add_to_list (netrc_entry **newentry, netrc_entry **list) a = *newentry; l = *list; - /* We need an account name in order to add the entry to the list. */ - if (a && ! a->account) + /* We need a login name in order to add the entry to the list. */ + if (a && ! a->login) { /* Free any allocated space. */ if (a->host) @@ -72,8 +79,7 @@ maybe_add_to_list (netrc_entry **newentry, netrc_entry **list) list of entries. NULL is returned if the file could not be parsed. */ netrc_entry * -parse_netrc (file) - char *file; +parse_netrc (char *file) { FILE *fp; char buf[POPBUFSIZE+1], *p, *tok; @@ -101,12 +107,12 @@ parse_netrc (file) premature_token = NULL; /* While there are lines in the file... */ - while (fgets(buf, POPBUFSIZE, fp)) + while (fgets(buf, sizeof(buf) - 1, fp)) { ln++; /* Strip trailing CRLF */ - for (p = buf + strlen(buf) - 1; (p >= buf) && isspace(*p); p--) + for (p = buf + strlen(buf) - 1; (p >= buf) && isspace((unsigned char)*p); p--) *p = '\0'; /* Parse the line. */ @@ -128,7 +134,7 @@ parse_netrc (file) char *pp; /* Skip any whitespace. */ - while (*p && isspace (*p)) + while (*p && isspace ((unsigned char)*p)) p++; /* Discard end-of-line comments. */ @@ -138,7 +144,7 @@ parse_netrc (file) tok = pp = p; /* Find the end of the token. */ - while (*p && (quote_char || !isspace (*p))) + while (*p && (quote_char || !isspace ((unsigned char)*p))) { if (quote_char) { @@ -175,7 +181,7 @@ parse_netrc (file) { case tok_login: if (current) - current->account = (char *) xstrdup (tok); + current->login = (char *) xstrdup (tok); else premature_token = "login"; break; @@ -212,15 +218,9 @@ parse_netrc (file) if (premature_token) { -#ifdef HAVE_ERROR - error_at_line (0, file, ln, - _("warning: found \"%s\" before any host names"), - premature_token); -#else fprintf (stderr, - _("%s:%d: warning: found \"%s\" before any host names\n"), + GT_("%s:%d: warning: found \"%s\" before any host names\n"), file, ln, premature_token); -#endif premature_token = NULL; } @@ -257,7 +257,7 @@ parse_netrc (file) else { - fprintf (stderr, _("%s:%d: warning: unknown token \"%s\"\n"), + fprintf (stderr, GT_("%s:%d: warning: unknown token \"%s\"\n"), file, ln, tok); } } @@ -293,15 +293,13 @@ parse_netrc (file) /* Return the netrc entry from LIST corresponding to HOST. NULL is returned if no such entry exists. */ netrc_entry * -search_netrc (list, host, account) - netrc_entry *list; - char *host, *account; +search_netrc (netrc_entry *list, char *host, char *login) { /* Look for the HOST in LIST. */ while (list) { if (list->host && !strcmp(list->host, host)) - if (!list->account || !strcmp(list->account, account)) + if (!list->login || !strcmp(list->login, login)) /* We found a matching entry. */ break; @@ -312,26 +310,46 @@ search_netrc (list, host, account) return list; } +void +free_netrc(netrc_entry *a) { + while(a) { + netrc_entry *n = a->next; + if (a->password != NULL) { + memset(a->password, 0x55, strlen(a->password)); + free(a->password); + } + xfree(a->login); + xfree(a->host); + xfree(a); + a = n; + } +} #ifdef STANDALONE #include #include -extern int errno; +#include -int -main (argc, argv) - int argc; - char **argv; +int main (int argc, char **argv) { struct stat sb; - char *program_name, *file, *host, *account; + char *file, *host, *login; netrc_entry *head, *a; program_name = argv[0]; file = argv[1]; host = argv[2]; - account = argv[3]; + login = argv[3]; + + switch (argc) { + case 2: + case 4: + break; + default: + fprintf (stderr, "Usage: %s [ ]\n", argv[0]); + exit(EXIT_FAILURE); + } if (stat (file, &sb)) { @@ -347,23 +365,23 @@ main (argc, argv) exit (1); } - if (host && account) + if (host && login) { - int i, status; - status = 0; + int status; + status = EXIT_SUCCESS; - printf("Host: %s, Account: %s\n", host, account); + printf("Host: %s, Login: %s\n", host, login); - a = search_netrc (head, host, account); + a = search_netrc (head, host, login); if (a) { /* Print out the password (if any). */ if (a->password) { - fputc (' ', stdout); - fputs (a->password, stdout); + printf("Password: %s\n", a->password); } - } + } else + status = EXIT_FAILURE; fputc ('\n', stdout); exit (status); @@ -381,8 +399,8 @@ main (argc, argv) fputc (' ', stdout); - /* Print the account name. */ - fputs (a->account, stdout); + /* Print the login name. */ + fputs (a->login, stdout); if (a->password) { @@ -395,6 +413,8 @@ main (argc, argv) a = a->next; } + free_netrc(head); + exit (0); } #endif /* STANDALONE */