]> Pileus Git - ~andy/fetchmail/blobdiff - rfc822.c
Merge branch 'legacy_63'
[~andy/fetchmail] / rfc822.c
index 1957c03774cbdc9005f9f25db59efd41f0c0d546..4efba8349eb6a79c494cae0d95aa54e9b72fb7dc 100644 (file)
--- a/rfc822.c
+++ b/rfc822.c
@@ -19,17 +19,27 @@ is part of fetchmail and the Unix Cookbook, and are released under the
 MIT license.  Compile with -DMAIN to build the demonstrator.
 
 ******************************************************************************/
+
+#define _XOPEN_SOURCE 600
+#define __BSD_VISIBLE 1
+
+#include "config.h"
+#include "fetchmail.h"
+
 #include  <stdio.h>
 #include  <ctype.h>
 #include  <string.h>
+#include  <strings.h>
 #include  <stdlib.h>
 
+#include "sdump.h"
+
 #ifndef MAIN
-#include "fetchmail.h"
-#include "i18n.h"
+#include "gettext.h"
 #else
+#include  <unistd.h>
 static int verbose;
-char *program_name = "rfc822";
+const char *program_name = "rfc822";
 #endif /* MAIN */
 
 #ifndef TRUE
@@ -39,12 +49,15 @@ char *program_name = "rfc822";
 
 #define HEADER_END(p)  ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
 
-unsigned char *reply_hack(buf, host)
+#define BEFORE_EOL(s)  (strcspn((s), "\r\n"))
+
+char *reply_hack(
+       char *buf               /* header to be hacked */,
+       const char *host        /* server hostname */,
+       size_t *length)
 /* hack message headers so replies will work properly */
-unsigned char *buf;            /* header to be hacked */
-const unsigned char *host;     /* server hostname */
 {
-    unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL;
+    char *from, *cp, last_nws = '\0', *parens_from = NULL;
     int parendepth, state, has_bare_name_part, has_host_part;
 #ifndef MAIN
     int addresscount = 1;
@@ -69,14 +82,16 @@ const unsigned char *host;  /* server hostname */
     }
 
 #ifndef MAIN
-    if (outlevel >= O_DEBUG)
-       report_build(stdout, GT_("About to rewrite %s"), buf);
+    if (outlevel >= O_DEBUG) {
+       report_build(stdout, GT_("About to rewrite %s...\n"), (cp = sdump(buf, BEFORE_EOL(buf))));
+       xfree(cp);
+    }
 
     /* make room to hack the address; buf must be malloced */
     for (cp = buf; *cp; cp++)
-       if (*cp == ',' || isspace(*cp))
+       if (*cp == ',' || isspace((unsigned char)*cp))
            addresscount++;
-    buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
+    buf = (char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
 #endif /* MAIN */
 
     /*
@@ -93,7 +108,7 @@ const unsigned char *host;   /* server hostname */
        if (verbose)
        {
            printf("state %d: %s", state, buf);
-           printf("%*s^\n", from - buf + 10, " ");
+           printf("%*s^\n", (int)(from - buf + 10), " ");
        }
 #endif /* MAIN */
        if (state != 2)
@@ -113,7 +128,7 @@ const unsigned char *host;  /* server hostname */
                break;
 
            case 1:     /* we've seen the colon, we're looking for addresses */
-               if (!isspace(*from))
+               if (!isspace((unsigned char)*from))
                    last_nws = *from;
                if (*from == '<')
                    state = 3;
@@ -132,12 +147,12 @@ const unsigned char *host;        /* server hostname */
                         && last_nws != ';')
                {
                    int hostlen;
-                   unsigned char *p;
+                   char *p;
 
                    p = from;
                    if (parens_from)
                        from = parens_from;
-                   while (isspace(*from) || (*from == ','))
+                   while (isspace((unsigned char)*from) || (*from == ','))
                        --from;
                    from++;
                    hostlen = strlen(host);
@@ -155,7 +170,7 @@ const unsigned char *host;  /* server hostname */
                {
                    parens_from = from;
                } 
-               else if (!isspace(*from))
+               else if (!isspace((unsigned char)*from))
                    has_bare_name_part = TRUE;
                break;
 
@@ -176,7 +191,7 @@ const unsigned char *host;  /* server hostname */
            case 3:     /* we're in a <>-enclosed address */
                if (*from == '@' || *from == '!')
                    has_host_part = TRUE;
-               else if (*from == '>' && from[-1] != '<')
+               else if (*from == '>' && (from > buf && from[-1] != '<'))
                {
                    state = 1;
                    if (!has_host_part)
@@ -198,29 +213,33 @@ const unsigned char *host;        /* server hostname */
        /*
         * If we passed a comma, reset everything.
         */
-       if (from[-1] == ',' && !parendepth) {
+       if ((from > buf && from[-1] == ',') && !parendepth) {
          has_host_part = has_bare_name_part = FALSE;
          parens_from = NULL;
        }
     }
 
 #ifndef MAIN
-    if (outlevel >= O_DEBUG)
-       report_complete(stdout, GT_("Rewritten version is %s\n"), buf);
+    if (outlevel >= O_DEBUG) {
+       report_complete(stdout, GT_("...rewritten version is %s.\n"),
+                       (cp = sdump(buf, BEFORE_EOL(buf))));
+       xfree(cp)
+    }
+
 #endif /* MAIN */
+    *length = strlen(buf);
     return(buf);
 }
 
-unsigned char *nxtaddr(hdr)
+char *nxtaddr(const char *hdr /* header to be parsed, NUL to continue previous hdr */)
 /* parse addresses in succession out of a specified RFC822 header */
-const unsigned char *hdr;      /* header to be parsed, NUL to continue previous hdr */
 {
-    static unsigned char address[BUFSIZ];
-    static int tp;
-    static const unsigned char *hp;
+    static char address[BUFSIZ];
+    static size_t tp;
+    static const char *hp;
     static int state, oldstate;
 #ifdef MAIN
-    static const unsigned char *orighdr;
+    static const char *orighdr;
 #endif /* MAIN */
     int parendepth = 0;
 
@@ -244,13 +263,15 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr *
        tp = 0;
     }
 
+    if (!hp) return NULL;
+
     for (; *hp; hp++)
     {
 #ifdef MAIN
        if (verbose)
        {
            printf("state %d: %s", state, orighdr);
-           printf("%*s^\n", hp - orighdr + 10, " ");
+           printf("%*s^\n", (int)(hp - orighdr + 10), " ");
        }
 #endif /* MAIN */
 
@@ -261,13 +282,13 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr *
            state = ENDIT_ALL;
            if (tp)
            {
-               while (isspace(address[--tp]))
-                   continue;
-               address[++tp] = '\0';
+               while (tp > 0 && isspace((unsigned char)address[tp - 1]))
+                   tp--;
+               address[tp] = '\0';
                tp = 0;
                return (address);
            }
-           return((unsigned char *)NULL);
+           return(NULL);
        }
        else if (*hp == '\\')           /* handle RFC822 escaping */
        {
@@ -302,7 +323,7 @@ const unsigned char *hdr;   /* header to be parsed, NUL to continue previous hdr *
                state = INSIDE_BRACKETS;
                tp = 0;
            }
-           else if (*hp != ',' && !isspace(*hp))
+           else if (*hp != ',' && !isspace((unsigned char)*hp))
            {
                --hp;
                state = BARE_ADDRESS;
@@ -337,18 +358,14 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr *
                state = INSIDE_DQUOTE;
                 address[NEXTTP()] = *hp;
             }
-           else if (!isspace(*hp))     /* just take it, ignoring whitespace */
+           else if (!isspace((unsigned char)*hp))      /* just take it, ignoring whitespace */
                address[NEXTTP()] = *hp;
            break;
 
        case INSIDE_DQUOTE:     /* we're in a quoted string, copy verbatim */
-           if (*hp != '"')
-               address[NEXTTP()] = *hp;
-           else
-           {
-               address[NEXTTP()] = *hp;
+           address[NEXTTP()] = *hp;
+           if (*hp == '"')
                state = oldstate;
-           }
            break;
 
        case INSIDE_PARENS:     /* we're in a parenthesized comment, ignore */
@@ -387,29 +404,30 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr *
 }
 
 #ifdef MAIN
-static void parsebuf(unsigned char *longbuf, int reply)
+static void parsebuf(char *longbuf, int reply)
 {
-    unsigned char      *cp;
+    char       *cp;
+    size_t     dummy;
 
     if (reply)
     {
-       reply_hack(longbuf, "HOSTNAME.NET");
-       printf("Rewritten buffer: %s", longbuf);
+       reply_hack(longbuf, "HOSTNAME.NET", &dummy);
+       printf("Rewritten buffer: %s", (char *)longbuf);
     }
     else
-       if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
+       if ((cp = nxtaddr(longbuf)) != (char *)NULL)
            do {
-               printf("\t-> \"%s\"\n", cp);
+               printf("\t-> \"%s\"\n", (char *)cp);
            } while
-               ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
+               ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
 }
 
 
 
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
-    unsigned char      buf[BUFSIZ], longbuf[BUFSIZ];
-    int                        ch, reply;
+    char       buf[BUFSIZ], longbuf[BUFSIZ];
+    int                ch, reply;
     
     verbose = reply = FALSE;
     while ((ch = getopt(argc, argv, "rv")) != EOF)
@@ -424,16 +442,18 @@ main(int argc, char *argv[])
            break;
        }
 
+    longbuf[0] = '\0';
+
     while (fgets(buf, sizeof(buf)-1, stdin))
     {
        if (buf[0] == ' ' || buf[0] == '\t')
-           strcat(longbuf, buf);
+           strlcat(longbuf, buf, sizeof(longbuf));
        else if (!strncasecmp("From: ", buf, 6)
                    || !strncasecmp("To: ", buf, 4)
                    || !strncasecmp("Reply-", buf, 6)
                    || !strncasecmp("Cc: ", buf, 4)
                    || !strncasecmp("Bcc: ", buf, 5))
-           strcpy(longbuf, buf);       
+           strlcpy(longbuf, buf, sizeof(longbuf));
        else if (longbuf[0])
        {
            if (verbose)
@@ -448,6 +468,7 @@ main(int argc, char *argv[])
            fputs(longbuf, stdout);
        parsebuf(longbuf, reply);
     }
+    exit(0);
 }
 #endif /* MAIN */