From af80a74d65f30cc89d03f9d8db6787c21d996210 Mon Sep 17 00:00:00 2001 From: Simon Schubert Date: Thu, 16 Jul 2009 16:04:25 +0200 Subject: [PATCH] dma: add required headers if they are not present 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 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/libexec/dma/dma.c b/libexec/dma/dma.c index f3a6e7cb44..ee48658179 100644 --- a/libexec/dma/dma.c +++ b/libexec/dma/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) -- 2.41.0