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, 0660);
129 if (fchmod(queuefd, 0660) < 0)
131 it->queuef = fdopen(queuefd, "w+");
132 if (it->queuef == NULL)
135 error = fprintf(it->queuef,
146 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
152 static struct qitem *
153 readqueuef(struct queue *queue, char *queuefn)
156 struct queue itmqueue;
159 char *queueid = NULL, *sender = NULL, *addr = NULL;
160 struct qitem *it = NULL;
162 bzero(&itmqueue, sizeof(itmqueue));
163 LIST_INIT(&itmqueue.queue);
165 queuef = fopen(queuefn, "r");
169 while (!feof(queuef)) {
170 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
172 line[strlen(line) - 1] = 0; /* chop newline */
174 s = strchr(line, ':');
187 if (strcmp(line, "ID") == 0) {
189 } else if (strcmp(line, "Sender") == 0) {
191 } else if (strcmp(line, "Recipient") == 0) {
194 syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
200 if (queueid == NULL || sender == NULL || addr == NULL ||
201 *queueid == 0 || *addr == 0) {
204 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
208 if (add_recp(&itmqueue, addr, 0) != 0)
211 it = LIST_FIRST(&itmqueue.queue);
212 it->sender = sender; sender = NULL;
213 it->queueid = queueid; queueid = NULL;
214 it->queuefn = queuefn; queuefn = NULL;
215 LIST_INSERT_HEAD(&queue->queue, it, next);
231 linkspool(struct queue *queue)
236 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
239 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
240 username, getuid(), queue->sender);
242 LIST_FOREACH(it, &queue->queue, next) {
243 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
245 if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
247 if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
250 /* Neither file may not exist yet */
251 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
254 if (writequeuef(it) != 0)
257 if (link(queue->tmpf, it->mailfn) != 0)
261 LIST_FOREACH(it, &queue->queue, next) {
262 syslog(LOG_INFO, "mail to=<%s> queued as %s",
263 it->addr, it->queueid);
270 LIST_FOREACH(it, &queue->queue, next) {
278 load_queue(struct queue *queue)
287 bzero(queue, sizeof(*queue));
288 LIST_INIT(&queue->queue);
290 spooldir = opendir(config.spooldir);
291 if (spooldir == NULL)
292 err(1, "reading queue");
294 while ((de = readdir(spooldir)) != NULL) {
298 /* ignore non-queue files */
299 if (de->d_name[0] != 'Q')
301 if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
303 if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
307 * Some file systems don't provide a de->d_type, so we have to
308 * do an explicit stat on the queue file.
309 * Move on if it turns out to be something else than a file.
311 if (stat(queuefn, &sb) != 0)
313 if (!S_ISREG(sb.st_mode)) {
318 if (stat(mailfn, &sb) != 0)
321 it = readqueuef(queue, queuefn);
329 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
343 delqueue(struct qitem *it)
347 if (it->queuef != NULL)
349 if (it->mailf != NULL)
355 acquirespool(struct qitem *it)
359 if (it->queuef == NULL) {
360 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
363 it->queuef = fdopen(queuefd, "r+");
364 if (it->queuef == NULL)
368 if (it->mailf == NULL) {
369 it->mailf = fopen(it->mailfn, "r");
370 if (it->mailf == NULL)
377 syslog(LOG_INFO, "could not acquire queue file: %m");
382 dropspool(struct queue *queue, struct qitem *keep)
386 LIST_FOREACH(it, &queue->queue, next) {
390 if (it->queuef != NULL)
392 if (it->mailf != NULL)