]>
git.ipfire.org Git - people/ms/dma.git/blob - local.c
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
51 create_mbox(const char *name
)
53 struct sigaction sa
, osa
;
54 pid_t child
, waitchild
;
62 * We need to enable SIGCHLD temporarily so that waitpid works.
64 bzero(&sa
, sizeof(sa
));
65 sa
.sa_handler
= SIG_DFL
;
66 sigaction(SIGCHLD
, &sa
, &osa
);
74 maxfd
= sysconf(_SC_OPEN_MAX
);
76 maxfd
= 1024; /* what can we do... */
78 for (i
= 3; i
<= maxfd
; ++i
)
81 execl(LIBEXEC_PATH
"/dma-mbox-create", "dma-mbox-create", name
, NULL
);
82 syslog(LOG_ERR
, "cannot execute "LIBEXEC_PATH
"/dma-mbox-create: %m");
87 waitchild
= waitpid(child
, &status
, 0);
93 if (waitchild
== -1 && e
== EINTR
) {
94 syslog(LOG_ERR
, "hung child while creating mbox `%s': %m", name
);
98 if (waitchild
== -1) {
99 syslog(LOG_ERR
, "child disappeared while creating mbox `%s': %m", name
);
103 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
104 syslog(LOG_ERR
, "error creating mbox `%s'", name
);
114 syslog(LOG_ERR
, "error creating mbox");
118 sigaction(SIGCHLD
, &osa
, NULL
);
124 deliver_local(struct qitem
*it
)
129 const char *newline
= "\n";
136 time_t now
= time(NULL
);
138 error
= snprintf(fn
, sizeof(fn
), "%s/%s", _PATH_MAILDIR
, it
->addr
);
139 if (error
< 0 || (size_t)error
>= sizeof(fn
)) {
140 syslog(LOG_NOTICE
, "local delivery deferred: %m");
145 /* wait for a maximum of 100s to get the lock to the file */
148 /* don't use O_CREAT here, because we might be running as the wrong user. */
149 mbox
= open_locked(fn
, O_WRONLY
|O_APPEND
);
159 * The file does not exist or we can't access it.
160 * Call dma-mbox-create to create it and fix permissions.
162 if (tries
> 0 || create_mbox(it
->addr
) != 0) {
163 syslog(LOG_ERR
, "local delivery deferred: can not create `%s'", fn
);
170 syslog(LOG_NOTICE
, "local delivery deferred: can not lock `%s'", fn
);
174 syslog(LOG_NOTICE
, "local delivery deferred: can not open `%s': %m", fn
);
181 mboxlen
= lseek(mbox
, 0, SEEK_END
);
183 /* New mails start with \nFrom ...., unless we're at the beginning of the mbox */
187 /* If we're bouncing a message, claim it comes from MAILER-DAEMON */
189 if (strcmp(sender
, "") == 0)
190 sender
= "MAILER-DAEMON";
192 if (fseek(it
->mailf
, 0, SEEK_SET
) != 0) {
193 syslog(LOG_NOTICE
, "local delivery deferred: can not seek: %m");
197 error
= snprintf(line
, sizeof(line
), "%sFrom %s\t%s", newline
, sender
, ctime(&now
));
198 if (error
< 0 || (size_t)error
>= sizeof(line
)) {
199 syslog(LOG_NOTICE
, "local delivery deferred: can not write header: %m");
202 if (write(mbox
, line
, error
) != error
)
205 while (!feof(it
->mailf
)) {
206 if (fgets(line
, sizeof(line
), it
->mailf
) == NULL
)
208 linelen
= strlen(line
);
209 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
210 syslog(LOG_CRIT
, "local delivery failed: corrupted queue file");
211 snprintf(errmsg
, sizeof(errmsg
), "corrupted queue file");
216 if (hadnl
&& strncmp(line
, "From ", 5) == 0) {
217 const char *gt
= ">";
219 if (write(mbox
, gt
, 1) != 1)
222 } else if (strcmp(line
, "\n") == 0) {
227 if ((size_t)write(mbox
, line
, linelen
) != linelen
)
234 syslog(LOG_ERR
, "local delivery failed: write error: %m");
237 if (ftruncate(mbox
, mboxlen
) != 0)
238 syslog(LOG_WARNING
, "error recovering mbox `%s': %m", fn
);