]>
git.ipfire.org Git - people/ms/dma.git/blob - spool.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
54 * 'Q'id files (queue):
55 * Organized like an RFC822 header, field: value. Ignores unknown fields.
57 * Sender: envelope-from
58 * Recipient: envelope-to
63 * Each queue file needs to have a corresponding data file.
64 * One data file might be shared by linking it several times.
66 * Queue ids are unique, formed from the inode of the data file
67 * and a unique identifier.
71 newspoolf(struct queue
*queue
)
78 if (snprintf(fn
, sizeof(fn
), "%s/%s", config
.spooldir
, "tmp_XXXXXXXXXX") <= 0)
84 /* XXX group rights */
85 if (fchmod(fd
, 0660) < 0)
87 if (flock(fd
, LOCK_EX
) == -1)
89 queue
->tmpf
= strdup(fn
);
90 if (queue
->tmpf
== NULL
)
96 if (fstat(fd
, &st
) != 0)
98 if (asprintf(&queue
->id
, "%"PRIxMAX
, (uintmax_t)st
.st_ino
) < 0)
101 queue
->mailf
= fdopen(fd
, "r+");
102 if (queue
->mailf
== NULL
)
105 t
= malloc(sizeof(*t
));
107 t
->str
= queue
->tmpf
;
108 SLIST_INSERT_HEAD(&tmpfs
, t
, next
);
113 if (queue
->mailf
!= NULL
)
114 fclose(queue
->mailf
);
121 writequeuef(struct qitem
*it
)
126 queuefd
= open_locked(it
->queuefn
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
129 it
->queuef
= fdopen(queuefd
, "w+");
130 if (it
->queuef
== NULL
)
133 error
= fprintf(it
->queuef
,
144 if (fflush(it
->queuef
) != 0 || fsync(fileno(it
->queuef
)) != 0)
150 static struct qitem
*
151 readqueuef(struct queue
*queue
, char *queuefn
)
154 struct queue itmqueue
;
157 char *queueid
= NULL
, *sender
= NULL
, *addr
= NULL
;
158 struct qitem
*it
= NULL
;
160 bzero(&itmqueue
, sizeof(itmqueue
));
161 LIST_INIT(&itmqueue
.queue
);
163 queuef
= fopen(queuefn
, "r");
167 while (!feof(queuef
)) {
168 if (fgets(line
, sizeof(line
), queuef
) == NULL
|| line
[0] == 0)
170 line
[strlen(line
) - 1] = 0; /* chop newline */
172 s
= strchr(line
, ':');
185 if (strcmp(line
, "ID") == 0) {
187 } else if (strcmp(line
, "Sender") == 0) {
189 } else if (strcmp(line
, "Recipient") == 0) {
192 syslog(LOG_DEBUG
, "ignoring unknown queue info `%s' in `%s'",
198 if (queueid
== NULL
|| sender
== NULL
|| addr
== NULL
||
199 *queueid
== 0 || *addr
== 0) {
202 syslog(LOG_ERR
, "malformed queue file `%s'", queuefn
);
206 if (add_recp(&itmqueue
, addr
, 0) != 0)
209 it
= LIST_FIRST(&itmqueue
.queue
);
210 it
->sender
= sender
; sender
= NULL
;
211 it
->queueid
= queueid
; queueid
= NULL
;
212 it
->queuefn
= queuefn
; queuefn
= NULL
;
213 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
229 linkspool(struct queue
*queue
)
234 if (fflush(queue
->mailf
) != 0 || fsync(fileno(queue
->mailf
)) != 0)
237 syslog(LOG_INFO
, "new mail from user=%s uid=%d envelope_from=<%s>",
238 username
, getuid(), queue
->sender
);
240 LIST_FOREACH(it
, &queue
->queue
, next
) {
241 if (asprintf(&it
->queueid
, "%s.%"PRIxPTR
, queue
->id
, (uintptr_t)it
) <= 0)
243 if (asprintf(&it
->queuefn
, "%s/Q%s", config
.spooldir
, it
->queueid
) <= 0)
245 if (asprintf(&it
->mailfn
, "%s/M%s", config
.spooldir
, it
->queueid
) <= 0)
248 /* Neither file may not exist yet */
249 if (stat(it
->queuefn
, &st
) == 0 || stat(it
->mailfn
, &st
) == 0)
252 if (writequeuef(it
) != 0)
255 if (link(queue
->tmpf
, it
->mailfn
) != 0)
259 LIST_FOREACH(it
, &queue
->queue
, next
) {
260 syslog(LOG_INFO
, "mail to=<%s> queued as %s",
261 it
->addr
, it
->queueid
);
268 LIST_FOREACH(it
, &queue
->queue
, next
) {
276 load_queue(struct queue
*queue
)
285 bzero(queue
, sizeof(queue
));
286 LIST_INIT(&queue
->queue
);
288 spooldir
= opendir(config
.spooldir
);
289 if (spooldir
== NULL
)
290 err(1, "reading queue");
292 while ((de
= readdir(spooldir
)) != NULL
) {
296 /* ignore non-queue files */
297 if (de
->d_name
[0] != 'Q')
299 if (asprintf(&queuefn
, "%s/Q%s", config
.spooldir
, de
->d_name
+ 1) < 0)
301 if (asprintf(&mailfn
, "%s/M%s", config
.spooldir
, de
->d_name
+ 1) < 0)
305 * Some file systems don't provide a de->d_type, so we have to
306 * do an explicit stat on the queue file.
307 * Move on if it turns out to be something else than a file.
309 if (stat(queuefn
, &sb
) != 0)
311 if (!S_ISREG(sb
.st_mode
)) {
316 if (stat(mailfn
, &sb
) != 0)
319 it
= readqueuef(queue
, queuefn
);
327 syslog(LOG_INFO
, "could not pick up queue file: `%s'/`%s': %m", queuefn
, mailfn
);
341 delqueue(struct qitem
*it
)
345 if (it
->queuef
!= NULL
)
347 if (it
->mailf
!= NULL
)
353 acquirespool(struct qitem
*it
)
357 if (it
->queuef
== NULL
) {
358 queuefd
= open_locked(it
->queuefn
, O_RDWR
|O_NONBLOCK
);
361 it
->queuef
= fdopen(queuefd
, "r+");
362 if (it
->queuef
== NULL
)
366 if (it
->mailf
== NULL
) {
367 it
->mailf
= fopen(it
->mailfn
, "r");
368 if (it
->mailf
== NULL
)
375 syslog(LOG_INFO
, "could not acquire queue file: %m");
380 dropspool(struct queue
*queue
, struct qitem
*keep
)
384 LIST_FOREACH(it
, &queue
->queue
, next
) {
388 if (it
->queuef
!= NULL
)
390 if (it
->mailf
!= NULL
)