]> Pileus Git - ~andy/fetchmail/blobdiff - netrc.c
Credit John Beck's fixes.
[~andy/fetchmail] / netrc.c
diff --git a/netrc.c b/netrc.c
index 7c5007026492b78931540f1b84d08a666bc4538e..a585e1a0be37c236336713b949f15e7f9b3926e6 100644 (file)
--- a/netrc.c
+++ b/netrc.c
@@ -1,17 +1,22 @@
-/* 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 <gord@gnu.ai.mit.edu>, 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")
+*/
+
+#include "config.h"
 
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "config.h"
 #include "fetchmail.h"
 #include "netrc.h"
 #include "i18n.h"
@@ -24,7 +29,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 +41,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 +77,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 +105,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. */
@@ -114,10 +118,12 @@ parse_netrc (file)
 
        /* If the line is empty... */
        if (!*p)
+       {
            if (last_token == tok_macdef)       /* end of macro */
                last_token = tok_nothing;
            else
                continue;                       /* otherwise ignore it */
+       }
 
        /* If we are defining macros, then skip parsing the line. */
        while (*p && last_token != tok_macdef)
@@ -126,7 +132,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. */
@@ -136,7 +142,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)
                {
@@ -173,7 +179,7 @@ parse_netrc (file)
            {
            case tok_login:
                if (current)
-                   current->account = (char *) xstrdup (tok);
+                   current->login = (char *) xstrdup (tok);
                else
                    premature_token = "login";
                break;
@@ -210,15 +216,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;
            }
 
@@ -255,7 +255,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);
                }
            }
@@ -291,19 +291,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)
-           /* We hit the default entry. */
-           break;
-
-       else if (!strcmp(list->host, host))
-           if (!list->account || !strcmp(list->account, account))
+       if (list->host && !strcmp(list->host, host))
+           if (!list->login || !strcmp(list->login, login))
                /* We found a matching entry. */
                break;
 
@@ -314,26 +308,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 <sys/types.h>
 #include <sys/stat.h>
 
-extern int errno;
+#include <errno.h>
 
-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 <file> [<host> <login>]\n", argv[0]);
+           exit(EXIT_FAILURE);
+    }
 
     if (stat (file, &sb))
     {
@@ -349,23 +363,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);
@@ -383,8 +397,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)
        {
@@ -397,6 +411,8 @@ main (argc, argv)
        a = a->next;
     }
 
+    free_netrc(head);
+
     exit (0);
 }
 #endif /* STANDALONE */