]> git.ipfire.org Git - people/ms/dma.git/commitdiff
dma: add required headers if they are not present
authorSimon Schubert <corecode@dragonflybsd.org>
Thu, 16 Jul 2009 14:04:25 +0000 (16:04 +0200)
committerSimon Schubert <corecode@dragonflybsd.org>
Thu, 16 Jul 2009 14:13:08 +0000 (16:13 +0200)
RFC2822 states that we must or should include the Date, Message-Id, and
From headers, so add them if they are not present in the message.

dma.c

diff --git a/dma.c b/dma.c
index f3a6e7cb44b19b4e6a0866a6dbd5bc9c82ef90b7..ee48658179a6430591d6f446001d82b974a8aa7c 100644 (file)
--- a/dma.c
+++ b/dma.c
@@ -422,12 +422,22 @@ rfc822date(void)
        return (str);
 }
 
+static int
+strprefixcmp(const char *str, const char *prefix)
+{
+       return (strncasecmp(str, prefix, strlen(prefix)));
+}
+
 static int
 readmail(struct queue *queue, const char *sender, int nodot)
 {
        char line[1000];        /* by RFC2822 */
        size_t linelen;
        int error;
+       int had_headers = 0;
+       int had_from = 0;
+       int had_messagid = 0;
+       int had_date = 0;
 
        error = snprintf(line, sizeof(line),
                "Received: from %s (uid %d)\n"
@@ -453,6 +463,34 @@ readmail(struct queue *queue, const char *sender, int nodot)
                        errno = EINVAL;         /* XXX mark permanent errors */
                        return (-1);
                }
+               if (!had_headers) {
+                       if (strprefixcmp(line, "Date:") == 0)
+                               had_date = 1;
+                       else if (strprefixcmp(line, "Message-Id:") == 0)
+                               had_messagid = 1;
+                       else if (strprefixcmp(line, "From:") == 0)
+                               had_from = 1;
+               }
+               if (strcmp(line, "\n") == 0 && !had_headers) {
+                       had_headers = 1;
+                       while (!had_date || !had_messagid || !had_from) {
+                               if (!had_date) {
+                                       had_date = 1;
+                                       snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
+                               } else if (!had_messagid) {
+                                       /* XXX better msgid, assign earlier and log? */
+                                       had_messagid = 1;
+                                       snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX"@%s>\n",
+                                                queue->id, hostname());
+                               } else if (!had_from) {
+                                       had_from = 1;
+                                       snprintf(line, sizeof(line), "From: <%s>\n", sender);
+                               }
+                               if ((size_t)write(queue->mailfd, line, strlen(line)) != strlen(line))
+                                       return (-1);
+                       }
+                       strcpy(line, "\n");
+               }
                if (!nodot && linelen == 2 && line[0] == '.')
                        break;
                if ((size_t)write(queue->mailfd, line, linelen) != linelen)