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
50 * 'Q'id files (queue):
57 * Each queue file needs to have a corresponding data file.
58 * One data file might be shared by linking it several times.
60 * Queue ids are unique, formed from the inode of the data file
61 * and a unique identifier.
65 newspoolf(struct queue *queue, const char *sender)
74 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
80 if (flock(fd, LOCK_EX) == -1)
82 queue->tmpf = strdup(fn);
83 if (queue->tmpf == NULL)
89 if (fstat(fd, &st) != 0)
91 if (asprintf(&queue->id, "%"PRIxMAX, st.st_ino) < 0)
94 queue->mailf = fdopen(fd, "r+");
95 if (queue->mailf == NULL)
98 if (fprintf(queue->mailf, "%s\n", sender) < 0)
101 hdrlen = ftello(queue->mailf);
103 LIST_FOREACH(it, &queue->queue, next) {
104 it->mailf = queue->mailf;
108 t = malloc(sizeof(*t));
110 t->str = queue->tmpf;
111 SLIST_INSERT_HEAD(&tmpfs, t, next);
116 if (queue->mailf != NULL)
117 fclose(queue->mailf);
124 linkspool(struct queue *queue, const char *sender)
126 char line[1000]; /* by RFC2822 */
132 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
135 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
136 username, getuid(), sender);
138 LIST_FOREACH(it, &queue->queue, next) {
139 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
141 if (asprintf(&it->queuefn, "%s/Q%s", config->spooldir, it->queueid) <= 0)
143 if (asprintf(&it->mailfn, "%s/M%s", config->spooldir, it->queueid) <= 0)
146 /* Neither file may not exist yet */
147 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
150 error = snprintf(line, sizeof(line), "%s %s\n", it->queueid, it->addr);
151 if ((ssize_t)error < 0 || error >= sizeof(line))
154 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
157 it->queuef = fdopen(queuefd, "w+");
158 if (it->queuef == NULL)
161 if (fwrite(line, strlen(line), 1, it->queuef) != 1)
163 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
166 if (link(queue->tmpf, it->mailfn) != 0)
170 LIST_FOREACH(it, &queue->queue, next) {
171 syslog(LOG_INFO, "mail to=<%s> queued as %s",
172 it->addr, it->queueid);
179 LIST_FOREACH(it, &queue->queue, next) {
187 load_queue(struct queue *queue)
190 //struct queue queue, itmqueue;
191 struct queue itmqueue;
204 LIST_INIT(&queue->queue);
206 spooldir = opendir(config->spooldir);
207 if (spooldir == NULL)
208 err(1, "reading queue");
210 while ((de = readdir(spooldir)) != NULL) {
216 LIST_INIT(&itmqueue.queue);
218 /* ignore temp files */
219 if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
221 if (de->d_name[0] != 'Q')
223 if (asprintf(&queuefn, "%s/Q%s", config->spooldir, de->d_name + 1) < 0)
225 if (asprintf(&mailfn, "%s/M%s", config->spooldir, de->d_name + 1) < 0)
228 mailf = fopen(mailfn, "r");
231 if (fgets(line, sizeof(line), mailf) == NULL || line[0] == 0)
233 line[strlen(line) - 1] = 0; /* chop newline */
234 sender = strdup(line);
238 hdrlen = ftell(mailf);
240 queuef = fopen(queuefn, "r");
244 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
246 line[strlen(line) - 1] = 0;
247 queueid = strdup(line);
250 addr = strchr(queueid, ' ');
255 if (add_recp(&itmqueue, addr, sender, 0) != 0)
258 it = LIST_FIRST(&itmqueue.queue);
259 it->queueid = queueid;
260 it->queuefn = queuefn;
263 LIST_INSERT_HEAD(&queue->queue, it, next);
272 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
294 delqueue(struct qitem *it)
298 if (it->queuef != NULL)
300 if (it->mailf != NULL)
306 aquirespool(struct qitem *it)
310 if (it->queuef == NULL) {
311 queuefd = open_locked(it->queuefn, O_RDWR);
314 it->queuef = fdopen(queuefd, "r+");
315 if (it->queuef == NULL)
319 if (it->mailf == NULL) {
320 it->mailf = fopen(it->mailfn, "r");
321 if (it->mailf == NULL)
328 syslog(LOG_INFO, "could not aquire queue file: %m");
333 dropspool(struct queue *queue, struct qitem *keep)
337 LIST_FOREACH(it, &queue->queue, next) {
341 if (it->queuef != NULL)
343 if (it->mailf != NULL)