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)
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", queue->sender) < 0)
101 hdrlen = ftello(queue->mailf);
103 LIST_FOREACH(it, &queue->queue, next) {
107 t = malloc(sizeof(*t));
109 t->str = queue->tmpf;
110 SLIST_INSERT_HEAD(&tmpfs, t, next);
115 if (queue->mailf != NULL)
116 fclose(queue->mailf);
123 linkspool(struct queue *queue)
125 char line[1000]; /* by RFC2822 */
131 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
134 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
135 username, getuid(), queue->sender);
137 LIST_FOREACH(it, &queue->queue, next) {
138 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
140 if (asprintf(&it->queuefn, "%s/Q%s", config->spooldir, it->queueid) <= 0)
142 if (asprintf(&it->mailfn, "%s/M%s", config->spooldir, it->queueid) <= 0)
145 /* Neither file may not exist yet */
146 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
149 error = snprintf(line, sizeof(line), "%s %s\n", it->queueid, it->addr);
150 if ((ssize_t)error < 0 || error >= sizeof(line))
153 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
156 it->queuef = fdopen(queuefd, "w+");
157 if (it->queuef == NULL)
160 if (fwrite(line, strlen(line), 1, it->queuef) != 1)
162 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
165 if (link(queue->tmpf, it->mailfn) != 0)
169 LIST_FOREACH(it, &queue->queue, next) {
170 syslog(LOG_INFO, "mail to=<%s> queued as %s",
171 it->addr, it->queueid);
178 LIST_FOREACH(it, &queue->queue, next) {
186 load_queue(struct queue *queue)
189 //struct queue queue, itmqueue;
190 struct queue itmqueue;
203 bzero(queue, sizeof(queue));
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, 0) != 0)
258 it = LIST_FIRST(&itmqueue.queue);
260 it->queueid = queueid;
261 it->queuefn = queuefn;
264 LIST_INSERT_HEAD(&queue->queue, it, next);
273 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
295 delqueue(struct qitem *it)
299 if (it->queuef != NULL)
301 if (it->mailf != NULL)
307 aquirespool(struct qitem *it)
311 if (it->queuef == NULL) {
312 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
315 it->queuef = fdopen(queuefd, "r+");
316 if (it->queuef == NULL)
320 if (it->mailf == NULL) {
321 it->mailf = fopen(it->mailfn, "r");
322 if (it->mailf == NULL)
329 syslog(LOG_INFO, "could not aquire queue file: %m");
334 dropspool(struct queue *queue, struct qitem *keep)
338 LIST_FOREACH(it, &queue->queue, next) {
342 if (it->queuef != NULL)
344 if (it->mailf != NULL)