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
51 * 'Q'id files (queue):
52 * Organized like an RFC822 header, field: value. Ignores unknown fields.
54 * Sender: envelope-from
55 * Recipient: envelope-to
60 * Each queue file needs to have a corresponding data file.
61 * One data file might be shared by linking it several times.
63 * Queue ids are unique, formed from the inode of the data file
64 * and a unique identifier.
68 newspoolf(struct queue *queue)
75 if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
81 /* XXX group rights */
82 if (fchmod(fd, 0660) < 0)
84 if (flock(fd, LOCK_EX) == -1)
86 queue->tmpf = strdup(fn);
87 if (queue->tmpf == NULL)
93 if (fstat(fd, &st) != 0)
95 if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
98 queue->mailf = fdopen(fd, "r+");
99 if (queue->mailf == NULL)
102 t = malloc(sizeof(*t));
104 t->str = queue->tmpf;
105 SLIST_INSERT_HEAD(&tmpfs, t, next);
110 if (queue->mailf != NULL)
111 fclose(queue->mailf);
118 writequeuef(struct qitem *it)
123 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
126 it->queuef = fdopen(queuefd, "w+");
127 if (it->queuef == NULL)
130 error = fprintf(it->queuef,
141 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
147 static struct qitem *
148 readqueuef(struct queue *queue, char *queuefn)
151 struct queue itmqueue;
154 char *queueid = NULL, *sender = NULL, *addr = NULL;
155 struct qitem *it = NULL;
157 bzero(&itmqueue, sizeof(itmqueue));
158 LIST_INIT(&itmqueue.queue);
160 queuef = fopen(queuefn, "r");
164 while (!feof(queuef)) {
165 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
167 line[strlen(line) - 1] = 0; /* chop newline */
169 s = strchr(line, ':');
182 if (strcmp(line, "ID") == 0) {
184 } else if (strcmp(line, "Sender") == 0) {
186 } else if (strcmp(line, "Recipient") == 0) {
189 syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
195 if (queueid == NULL || sender == NULL || addr == NULL ||
196 *queueid == 0 || *addr == 0) {
199 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
203 if (add_recp(&itmqueue, addr, 0) != 0)
206 it = LIST_FIRST(&itmqueue.queue);
207 it->sender = sender; sender = NULL;
208 it->queueid = queueid; queueid = NULL;
209 it->queuefn = queuefn; queuefn = NULL;
210 LIST_INSERT_HEAD(&queue->queue, it, next);
226 linkspool(struct queue *queue)
231 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
234 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
235 username, getuid(), queue->sender);
237 LIST_FOREACH(it, &queue->queue, next) {
238 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
240 if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
242 if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
245 /* Neither file may not exist yet */
246 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
249 if (writequeuef(it) != 0)
252 if (link(queue->tmpf, it->mailfn) != 0)
256 LIST_FOREACH(it, &queue->queue, next) {
257 syslog(LOG_INFO, "mail to=<%s> queued as %s",
258 it->addr, it->queueid);
265 LIST_FOREACH(it, &queue->queue, next) {
273 load_queue(struct queue *queue)
282 bzero(queue, sizeof(queue));
283 LIST_INIT(&queue->queue);
285 spooldir = opendir(config.spooldir);
286 if (spooldir == NULL)
287 err(1, "reading queue");
289 while ((de = readdir(spooldir)) != NULL) {
293 /* ignore temp files */
294 if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
296 if (de->d_name[0] != 'Q')
298 if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
300 if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
303 if (stat(mailfn, &sb) != 0)
306 it = readqueuef(queue, queuefn);
314 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
328 delqueue(struct qitem *it)
332 if (it->queuef != NULL)
334 if (it->mailf != NULL)
340 acquirespool(struct qitem *it)
344 if (it->queuef == NULL) {
345 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
348 it->queuef = fdopen(queuefd, "r+");
349 if (it->queuef == NULL)
353 if (it->mailf == NULL) {
354 it->mailf = fopen(it->mailfn, "r");
355 if (it->mailf == NULL)
362 syslog(LOG_INFO, "could not acquire queue file: %m");
367 dropspool(struct queue *queue, struct qitem *keep)
371 LIST_FOREACH(it, &queue->queue, next) {
375 if (it->queuef != NULL)
377 if (it->mailf != NULL)