2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon Schubert <2@0x2c.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
54 create_mbox(const char *name)
56 struct sigaction sa, osa;
57 pid_t child, waitchild;
65 * We need to enable SIGCHLD temporarily so that waitpid works.
67 bzero(&sa, sizeof(sa));
68 sa.sa_handler = SIG_DFL;
69 sigaction(SIGCHLD, &sa, &osa);
77 maxfd = sysconf(_SC_OPEN_MAX);
79 maxfd = 1024; /* what can we do... */
81 for (i = 3; i <= maxfd; ++i)
84 execl(LIBEXEC_PATH "/dma-mbox-create", "dma-mbox-create", name, NULL);
85 syslog(LOG_ERR, "cannot execute "LIBEXEC_PATH"/dma-mbox-create: %m");
90 waitchild = waitpid(child, &status, 0);
96 if (waitchild == -1 && e == EINTR) {
97 syslog(LOG_ERR, "hung child while creating mbox `%s': %m", name);
101 if (waitchild == -1) {
102 syslog(LOG_ERR, "child disappeared while creating mbox `%s': %m", name);
106 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
107 syslog(LOG_ERR, "error creating mbox `%s'", name);
117 syslog(LOG_ERR, "error creating mbox");
121 sigaction(SIGCHLD, &osa, NULL);
127 deliver_local(struct qitem *it)
132 const char *newline = "\n";
139 time_t now = time(NULL);
141 error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, it->addr);
142 if (error < 0 || (size_t)error >= sizeof(fn)) {
143 syslog(LOG_NOTICE, "local delivery deferred: %m");
148 /* wait for a maximum of 100s to get the lock to the file */
151 /* don't use O_CREAT here, because we might be running as the wrong user. */
152 mbox = open_locked(fn, O_WRONLY|O_APPEND);
162 * The file does not exist or we can't access it.
163 * Call dma-mbox-create to create it and fix permissions.
165 if (tries > 0 || create_mbox(it->addr) != 0) {
166 syslog(LOG_ERR, "local delivery deferred: can not create `%s'", fn);
173 syslog(LOG_NOTICE, "local delivery deferred: can not lock `%s'", fn);
177 syslog(LOG_NOTICE, "local delivery deferred: can not open `%s': %m", fn);
184 mboxlen = lseek(mbox, 0, SEEK_END);
186 /* New mails start with \nFrom ...., unless we're at the beginning of the mbox */
190 /* If we're bouncing a message, claim it comes from MAILER-DAEMON */
192 if (strcmp(sender, "") == 0)
193 sender = "MAILER-DAEMON";
195 if (fseek(it->mailf, 0, SEEK_SET) != 0) {
196 syslog(LOG_NOTICE, "local delivery deferred: can not seek: %m");
200 error = snprintf(line, sizeof(line), "%sFrom %s %s", newline, sender, ctime(&now));
201 if (error < 0 || (size_t)error >= sizeof(line)) {
202 syslog(LOG_NOTICE, "local delivery deferred: can not write header: %m");
205 if (write(mbox, line, error) != error)
208 while (!feof(it->mailf)) {
209 if (fgets(line, sizeof(line), it->mailf) == NULL)
211 linelen = strlen(line);
212 if (linelen == 0 || line[linelen - 1] != '\n') {
213 syslog(LOG_CRIT, "local delivery failed: corrupted queue file");
214 snprintf(errmsg, sizeof(errmsg), "corrupted queue file");
221 * - escape lines that start with "From " with a > sign.
222 * - be reversable by escaping lines that contain an arbitrary
223 * number of > signs, followed by "From ", i.e. />*From / in regexp.
224 * - strict mbox processing only requires escaping after empty lines,
225 * yet most MUAs seem to relax this requirement and will treat any
226 * line starting with "From " as the beginning of a new mail.
228 if ((!MBOX_STRICT || hadnl) &&
229 strncmp(&line[strspn(line, ">")], "From ", 5) == 0) {
230 const char *gt = ">";
232 if (write(mbox, gt, 1) != 1)
235 } else if (strcmp(line, "\n") == 0) {
240 if ((size_t)write(mbox, line, linelen) != linelen)
247 syslog(LOG_ERR, "local delivery failed: write error: %m");
250 if (ftruncate(mbox, mboxlen) != 0)
251 syslog(LOG_WARNING, "error recovering mbox `%s': %m", fn);