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
52 * 'Q'id files (queue):
59 * Each queue file needs to have a corresponding data file.
60 * One data file might be shared by linking it several times.
62 * Queue ids are unique, formed from the inode of the data file
63 * and a unique identifier.
67 newspoolf(struct queue *queue, const char *sender)
69 char line[1000]; /* by RFC2822 */
78 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
81 queue->mailfd = mkstemp(fn);
82 if (queue->mailfd < 0)
84 if (flock(queue->mailfd, LOCK_EX) == -1)
87 queue->tmpf = strdup(fn);
88 if (queue->tmpf == NULL)
91 error = snprintf(line, sizeof(line), "%s\n", sender);
92 if (error < 0 || (size_t)error >= sizeof(line)) {
96 if (write(queue->mailfd, line, error) != error)
99 hdrlen = lseek(queue->mailfd, 0, SEEK_CUR);
101 mailf = fdopen(queue->mailfd, "r+");
104 LIST_FOREACH(it, &queue->queue, next) {
112 if (fstat(queue->mailfd, &st) != 0)
114 if (asprintf(&queue->id, "%"PRIxMAX, st.st_ino) < 0)
117 t = malloc(sizeof(*t));
119 t->str = queue->tmpf;
120 SLIST_INSERT_HEAD(&tmpfs, t, next);
125 close(queue->mailfd);
131 linkspool(struct queue *queue)
133 char line[1000]; /* by RFC2822 */
139 LIST_FOREACH(it, &queue->queue, next) {
140 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
142 if (asprintf(&it->queuefn, "%s/Q%s", config->spooldir, it->queueid) <= 0)
144 if (asprintf(&it->mailfn, "%s/M%s", config->spooldir, it->queueid) <= 0)
147 /* Neither file may not exist yet */
148 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
151 error = snprintf(line, sizeof(line), "%s %s\n", it->queueid, it->addr);
152 if (error < 0 || (size_t)error >= sizeof(line))
154 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
157 if (write(queuefd, line, error) != error) {
161 it->queuefd = queuefd;
163 if (link(queue->tmpf, it->mailfn) != 0)
167 LIST_FOREACH(it, &queue->queue, next) {
168 syslog(LOG_INFO, "mail to=<%s> queued as %s",
169 it->addr, it->queueid);
176 LIST_FOREACH(it, &queue->queue, next) {
184 load_queue(struct queue *queue, int ignorelock)
187 //struct queue queue, itmqueue;
188 struct queue itmqueue;
203 LIST_INIT(&queue->queue);
205 spooldir = opendir(config->spooldir);
206 if (spooldir == NULL)
207 err(1, "reading queue");
209 while ((de = readdir(spooldir)) != NULL) {
217 LIST_INIT(&itmqueue.queue);
219 /* ignore temp files */
220 if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
222 if (de->d_name[0] != 'Q')
224 if (asprintf(&queuefn, "%s/Q%s", config->spooldir, de->d_name + 1) < 0)
226 if (asprintf(&mailfn, "%s/M%s", config->spooldir, de->d_name + 1) < 0)
229 fd = open_locked(queuefn, O_RDONLY|O_NONBLOCK);
230 if (ignorelock && fd < 0) {
231 fd = open(queuefn, O_RDONLY);
235 /* Ignore locked files */
236 if (errno == EWOULDBLOCK)
241 mailf = fopen(mailfn, "r");
244 if (fgets(line, sizeof(line), mailf) == NULL || line[0] == 0)
246 line[strlen(line) - 1] = 0; /* chop newline */
247 sender = strdup(line);
251 hdrlen = ftell(mailf);
253 queuef = fdopen(fd, "r");
257 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
259 line[strlen(line) - 1] = 0;
260 queueid = strdup(line);
263 addr = strchr(queueid, ' ');
268 if (add_recp(&itmqueue, addr, sender, 0) != 0)
270 it = LIST_FIRST(&itmqueue.queue);
273 it->queueid = queueid;
274 it->queuefn = queuefn;
278 LIST_INSERT_HEAD(&queue->queue, it, next);
283 warn("reading queue: `%s'", queuefn);
302 err(1, "reading queue");
306 delqueue(struct qitem *it)