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):
58 * Each queue file needs to have a corresponding data file.
59 * One data file might be shared by linking it several times.
61 * Queue ids are unique, formed from the inode of the spool file
62 * and a unique identifier.
66 newspoolf(struct queue *queue, const char *sender)
68 char line[1000]; /* by RFC2822 */
76 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
79 queue->mailfd = mkstemp(fn);
80 if (queue->mailfd < 0)
82 if (flock(queue->mailfd, LOCK_EX) == -1)
85 queue->tmpf = strdup(fn);
86 if (queue->tmpf == NULL)
89 error = snprintf(line, sizeof(line), "%s\n", sender);
90 if (error < 0 || (size_t)error >= sizeof(line)) {
94 if (write(queue->mailfd, line, error) != error)
97 hdrlen = lseek(queue->mailfd, 0, SEEK_CUR);
99 mailf = fdopen(queue->mailfd, "r+");
102 LIST_FOREACH(it, &queue->queue, next) {
107 t = malloc(sizeof(*t));
109 t->str = queue->tmpf;
110 SLIST_INSERT_HEAD(&tmpfs, t, next);
115 close(queue->mailfd);
121 linkspool(struct queue *queue)
123 char line[1000]; /* by RFC2822 */
130 * Assign queue id to each dest.
132 if (fstat(queue->mailfd, &st) != 0)
134 queue->id = st.st_ino;
136 /* XXX put this to a better place
137 syslog(LOG_INFO, "%"PRIxMAX": new mail from user=%s uid=%d envelope_from=<%s>",
138 queue->id, username, uid, sender);
141 LIST_FOREACH(it, &queue->queue, next) {
142 if (asprintf(&it->queueid, "%"PRIxMAX".%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
144 if (asprintf(&it->queuefn, "%s/Q%s", config->spooldir, it->queueid) <= 0)
146 if (asprintf(&it->mailfn, "%s/M%s", config->spooldir, it->queueid) <= 0)
149 /* Neither file may not exist yet */
150 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
153 error = snprintf(line, sizeof(line), "%s %s\n", it->queueid, it->addr);
154 if (error < 0 || (size_t)error >= sizeof(line))
156 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
159 if (write(queuefd, line, error) != error) {
163 it->queuefd = queuefd;
165 if (link(queue->tmpf, it->mailfn) != 0)
170 syslog(LOG_INFO, "%"PRIxMAX": mail to=<%s> queued as %s",
171 queue->id, it->addr, it->queueid);
178 LIST_FOREACH(it, &queue->queue, next) {
186 load_queue(struct queue *queue, int ignorelock)
189 //struct queue queue, itmqueue;
190 struct queue itmqueue;
205 LIST_INIT(&queue->queue);
207 spooldir = opendir(config->spooldir);
208 if (spooldir == NULL)
209 err(1, "reading queue");
211 while ((de = readdir(spooldir)) != NULL) {
219 LIST_INIT(&itmqueue.queue);
221 /* ignore temp files */
222 if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
224 if (de->d_name[0] != 'Q')
226 if (asprintf(&queuefn, "%s/Q%s", config->spooldir, de->d_name + 1) < 0)
228 if (asprintf(&mailfn, "%s/M%s", config->spooldir, de->d_name + 1) < 0)
231 fd = open_locked(queuefn, O_RDONLY|O_NONBLOCK);
232 if (ignorelock && fd < 0) {
233 fd = open(queuefn, O_RDONLY);
237 /* Ignore locked files */
238 if (errno == EWOULDBLOCK)
243 mailf = fopen(mailfn, "r");
246 if (fgets(line, sizeof(line), mailf) == NULL || line[0] == 0)
248 line[strlen(line) - 1] = 0; /* chop newline */
249 sender = strdup(line);
253 hdrlen = ftell(mailf);
255 queuef = fdopen(fd, "r");
259 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
261 line[strlen(line) - 1] = 0;
262 queueid = strdup(line);
265 addr = strchr(queueid, ' ');
270 if (add_recp(&itmqueue, addr, sender, 0) != 0)
272 it = LIST_FIRST(&itmqueue.queue);
275 it->queueid = queueid;
276 it->queuefn = queuefn;
280 LIST_INSERT_HEAD(&queue->queue, it, next);
285 warn("reading queue: `%s'", queuefn);
304 err(1, "reading queue");
308 delqueue(struct qitem *it)