2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon Schubert <2@0x2c.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * 'Q'id files (queue):
57 * Organized like an RFC822 header, field: value. Ignores unknown fields.
59 * Sender: envelope-from
60 * Recipient: envelope-to
65 * Each queue file needs to have a corresponding data file.
66 * One data file might be shared by linking it several times.
68 * Queue ids are unique, formed from the inode of the data file
69 * and a unique identifier.
73 newspoolf(struct queue *queue)
80 if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
86 /* XXX group rights */
87 if (fchmod(fd, 0660) < 0)
89 if (flock(fd, LOCK_EX) == -1)
91 queue->tmpf = strdup(fn);
92 if (queue->tmpf == NULL)
98 if (fstat(fd, &st) != 0)
100 if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
103 queue->mailf = fdopen(fd, "r+");
104 if (queue->mailf == NULL)
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 writequeuef(struct qitem *it)
128 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660);
131 if (fchmod(queuefd, 0660) < 0)
133 it->queuef = fdopen(queuefd, "w+");
134 if (it->queuef == NULL)
137 error = fprintf(it->queuef,
148 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
154 static struct qitem *
155 readqueuef(struct queue *queue, char *queuefn)
158 struct queue itmqueue;
161 char *queueid = NULL, *sender = NULL, *addr = NULL;
162 struct qitem *it = NULL;
164 bzero(&itmqueue, sizeof(itmqueue));
165 LIST_INIT(&itmqueue.queue);
167 queuef = fopen(queuefn, "r");
171 while (!feof(queuef)) {
172 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
174 line[strlen(line) - 1] = 0; /* chop newline */
176 s = strchr(line, ':');
189 if (strcmp(line, "ID") == 0) {
191 } else if (strcmp(line, "Sender") == 0) {
193 } else if (strcmp(line, "Recipient") == 0) {
196 syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
202 if (queueid == NULL || sender == NULL || addr == NULL ||
203 *queueid == 0 || *addr == 0) {
206 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
210 if (add_recp(&itmqueue, addr, 0) != 0)
213 it = LIST_FIRST(&itmqueue.queue);
214 it->sender = sender; sender = NULL;
215 it->queueid = queueid; queueid = NULL;
216 it->queuefn = queuefn; queuefn = NULL;
217 LIST_INSERT_HEAD(&queue->queue, it, next);
233 linkspool(struct queue *queue)
238 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
241 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
242 username, getuid(), queue->sender);
244 LIST_FOREACH(it, &queue->queue, next) {
245 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
247 if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
249 if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
252 /* Neither file may not exist yet */
253 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
256 if (writequeuef(it) != 0)
259 if (link(queue->tmpf, it->mailfn) != 0)
263 LIST_FOREACH(it, &queue->queue, next) {
264 syslog(LOG_INFO, "mail to=<%s> queued as %s",
265 it->addr, it->queueid);
272 LIST_FOREACH(it, &queue->queue, next) {
280 load_queue(struct queue *queue)
289 bzero(queue, sizeof(*queue));
290 LIST_INIT(&queue->queue);
292 spooldir = opendir(config.spooldir);
293 if (spooldir == NULL)
294 err(EX_NOINPUT, "reading queue");
296 while ((de = readdir(spooldir)) != NULL) {
300 /* ignore non-queue files */
301 if (de->d_name[0] != 'Q')
303 if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
305 if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
309 * Some file systems don't provide a de->d_type, so we have to
310 * do an explicit stat on the queue file.
311 * Move on if it turns out to be something else than a file.
313 if (stat(queuefn, &sb) != 0)
315 if (!S_ISREG(sb.st_mode)) {
320 if (stat(mailfn, &sb) != 0)
323 it = readqueuef(queue, queuefn);
331 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
345 delqueue(struct qitem *it)
349 if (it->queuef != NULL)
351 if (it->mailf != NULL)
357 acquirespool(struct qitem *it)
361 if (it->queuef == NULL) {
362 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
365 it->queuef = fdopen(queuefd, "r+");
366 if (it->queuef == NULL)
370 if (it->mailf == NULL) {
371 it->mailf = fopen(it->mailfn, "r");
372 if (it->mailf == NULL)
379 if (errno == EWOULDBLOCK)
381 syslog(LOG_INFO, "could not acquire queue file: %m");
386 dropspool(struct queue *queue, struct qitem *keep)
390 LIST_FOREACH(it, &queue->queue, next) {
394 if (it->queuef != NULL)
396 if (it->mailf != NULL)
402 flushqueue_since(unsigned int period)
406 char *flushfn = NULL;
408 if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
410 if (stat(flushfn, &st) < 0) {
416 if (gettimeofday(&now, 0) != 0)
419 /* Did the flush file get touched within the last period seconds? */
420 if (st.st_mtim.tv_sec + (int)period >= now.tv_sec)
427 flushqueue_signal(void)
429 char *flushfn = NULL;
432 if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
434 fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660);
437 syslog(LOG_ERR, "could not open flush file: %m");