From owner-svn-src-head@freebsd.org Wed Dec 6 22:08:36 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 779D1E8D8B0; Wed, 6 Dec 2017 22:08:36 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 512AE64A09; Wed, 6 Dec 2017 22:08:36 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id vB6M8Zub040409; Wed, 6 Dec 2017 22:08:35 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id vB6M8ZHe040408; Wed, 6 Dec 2017 22:08:35 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201712062208.vB6M8ZHe040408@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Wed, 6 Dec 2017 22:08:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r326641 - head/contrib/dma X-SVN-Group: head X-SVN-Commit-Author: bapt X-SVN-Commit-Paths: head/contrib/dma X-SVN-Commit-Revision: 326641 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Dec 2017 22:08:36 -0000 Author: bapt Date: Wed Dec 6 22:08:35 2017 New Revision: 326641 URL: https://svnweb.freebsd.org/changeset/base/326641 Log: Split body of mails not respecting RFC2822 For mails which has a body not respecting RFC2822 (which often happen with crontabs) try to split by words finding the last space before 1000's character If no spaces are found then consider the mail to be malformed anyway PR: 208261 Modified: head/contrib/dma/mail.c Modified: head/contrib/dma/mail.c ============================================================================== --- head/contrib/dma/mail.c Wed Dec 6 22:06:48 2017 (r326640) +++ head/contrib/dma/mail.c Wed Dec 6 22:08:35 2017 (r326641) @@ -41,6 +41,8 @@ #include "dma.h" +#define MAX_LINE_RFC822 1000 + void bounce(struct qitem *it, const char *reason) { @@ -341,19 +343,47 @@ newaddr: goto again; } +static int +writeline(struct queue *queue, const char *line, ssize_t linelen) +{ + ssize_t len; + + while (linelen > 0) { + len = linelen; + if (linelen > MAX_LINE_RFC822) { + len = MAX_LINE_RFC822 - 10; + } + + if (fwrite(line, len, 1, queue->mailf) != 1) + return (-1); + + if (linelen <= MAX_LINE_RFC822) + break; + + if (fwrite("\n", 1, 1, queue->mailf) != 1) + return (-1); + + line += MAX_LINE_RFC822 - 10; + linelen = strlen(line); + } + return (0); +} + int readmail(struct queue *queue, int nodot, int recp_from_header) { struct parse_state parse_state; - char line[1000]; /* by RFC2822 */ - size_t linelen; + char *line = NULL; + ssize_t linelen; + size_t linecap = 0; + char newline[MAX_LINE_RFC822]; size_t error; int had_headers = 0; int had_from = 0; int had_messagid = 0; int had_date = 0; - int had_last_line = 0; int nocopy = 0; + int ret = -1; parse_state.state = NONE; @@ -372,24 +402,17 @@ readmail(struct queue *queue, int nodot, int recp_from return (-1); while (!feof(stdin)) { - if (fgets(line, sizeof(line) - 1, stdin) == NULL) + newline[0] = '\0'; + if ((linelen = getline(&line, &linecap, stdin)) <= 0) break; - if (had_last_line) - errlogx(EX_DATAERR, "bad mail input format:" - " from %s (uid %d) (envelope-from %s)", - username, useruid, queue->sender); - linelen = strlen(line); - if (linelen == 0 || line[linelen - 1] != '\n') { - /* - * This line did not end with a newline character. - * If we fix it, it better be the last line of - * the file. - */ - line[linelen] = '\n'; - line[linelen + 1] = 0; - had_last_line = 1; - } + if (!had_headers) { + if (linelen > MAX_LINE_RFC822) { + /* XXX also split headers */ + errlogx(EX_DATAERR, "bad mail input format:" + " from %s (uid %d) (envelope-from %s)", + username, useruid, queue->sender); + } /* * Unless this is a continuation, switch of * the Bcc: nocopy flag. @@ -430,31 +453,39 @@ readmail(struct queue *queue, int nodot, int recp_from while (!had_date || !had_messagid || !had_from) { if (!had_date) { had_date = 1; - snprintf(line, sizeof(line), "Date: %s\n", rfc822date()); + snprintf(newline, sizeof(newline), "Date: %s\n", rfc822date()); } else if (!had_messagid) { /* XXX msgid, assign earlier and log? */ had_messagid = 1; - snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n", + snprintf(newline, sizeof(newline), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n", (uintmax_t)time(NULL), queue->id, (uintmax_t)random(), hostname()); } else if (!had_from) { had_from = 1; - snprintf(line, sizeof(line), "From: <%s>\n", queue->sender); + snprintf(newline, sizeof(newline), "From: <%s>\n", queue->sender); } - if (fwrite(line, strlen(line), 1, queue->mailf) != 1) - return (-1); + if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1) + goto fail; } - strcpy(line, "\n"); + strlcpy(newline, "\n", sizeof(newline)); } if (!nodot && linelen == 2 && line[0] == '.') break; if (!nocopy) { - if (fwrite(line, strlen(line), 1, queue->mailf) != 1) - return (-1); + if (newline[0]) { + if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1) + goto fail; + } else { + if (writeline(queue, line, linelen) != 0) + goto fail; + } } } - return (0); + ret = 0; +fail: + free(line); + return (ret); }