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.

libexec/dma/dma.c

index f3a6e7c..ee48658 100644 (file)
@@ -423,11 +423,21 @@ rfc822date(void)
 }
 
 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)