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
58 * 'Q'id files (queue):
59 * Organized like an RFC822 header, field: value. Ignores unknown fields.
61 * Sender: envelope-from
62 * Recipient: envelope-to
67 * Each queue file needs to have a corresponding data file.
68 * One data file might be shared by linking it several times.
70 * Queue ids are unique, formed from the inode of the data file
71 * and a unique identifier.
75 newspoolf(struct queue *queue)
82 if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
88 /* XXX group rights */
89 if (fchmod(fd, 0660) < 0)
91 if (flock(fd, LOCK_EX) == -1)
93 queue->tmpf = strdup(fn);
94 if (queue->tmpf == NULL)
100 if (fstat(fd, &st) != 0)
102 if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
105 queue->mailf = fdopen(fd, "r+");
106 if (queue->mailf == NULL)
109 t = malloc(sizeof(*t));
111 t->str = queue->tmpf;
112 SLIST_INSERT_HEAD(&tmpfs, t, next);
117 if (queue->mailf != NULL)
118 fclose(queue->mailf);
125 writequeuef(struct qitem *it)
130 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660);
133 if (fchmod(queuefd, 0660) < 0)
135 it->queuef = fdopen(queuefd, "w+");
136 if (it->queuef == NULL)
139 error = fprintf(it->queuef,
150 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
156 static struct qitem *
157 readqueuef(struct queue *queue, char *queuefn)
160 struct queue itmqueue;
163 char *queueid = NULL, *sender = NULL, *addr = NULL;
164 struct qitem *it = NULL;
166 bzero(&itmqueue, sizeof(itmqueue));
167 LIST_INIT(&itmqueue.queue);
169 queuef = fopen(queuefn, "r");
173 while (!feof(queuef)) {
174 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
176 line[strlen(line) - 1] = 0; /* chop newline */
178 s = strchr(line, ':');
191 if (strcmp(line, "ID") == 0) {
193 } else if (strcmp(line, "Sender") == 0) {
195 } else if (strcmp(line, "Recipient") == 0) {
198 syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
204 if (queueid == NULL || sender == NULL || addr == NULL ||
205 *queueid == 0 || *addr == 0) {
208 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
212 if (add_recp(&itmqueue, addr, 0) != 0)
215 it = LIST_FIRST(&itmqueue.queue);
216 it->sender = sender; sender = NULL;
217 it->queueid = queueid; queueid = NULL;
218 it->queuefn = queuefn; queuefn = NULL;
219 LIST_INSERT_HEAD(&queue->queue, it, next);
235 linkspool(struct queue *queue)
240 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
243 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
244 username, getuid(), queue->sender);
246 LIST_FOREACH(it, &queue->queue, next) {
247 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
249 if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
251 if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
254 /* Neither file may not exist yet */
255 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
258 if (writequeuef(it) != 0)
261 if (link(queue->tmpf, it->mailfn) != 0)
265 LIST_FOREACH(it, &queue->queue, next) {
266 syslog(LOG_INFO, "mail to=<%s> queued as %s",
267 it->addr, it->queueid);
274 LIST_FOREACH(it, &queue->queue, next) {
282 load_queue(struct queue *queue)
291 bzero(queue, sizeof(*queue));
292 LIST_INIT(&queue->queue);
294 spooldir = opendir(config.spooldir);
295 if (spooldir == NULL)
296 err(EX_NOINPUT, "reading queue");
298 while ((de = readdir(spooldir)) != NULL) {
302 /* ignore non-queue files */
303 if (de->d_name[0] != 'Q')
305 if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
307 if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
311 * Some file systems don't provide a de->d_type, so we have to
312 * do an explicit stat on the queue file.
313 * Move on if it turns out to be something else than a file.
315 if (stat(queuefn, &sb) != 0)
317 if (!S_ISREG(sb.st_mode)) {
322 if (stat(mailfn, &sb) != 0)
325 it = readqueuef(queue, queuefn);
333 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
347 delqueue(struct qitem *it)
351 if (it->queuef != NULL)
353 if (it->mailf != NULL)
359 acquirespool(struct qitem *it)
363 if (it->queuef == NULL) {
364 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
367 it->queuef = fdopen(queuefd, "r+");
368 if (it->queuef == NULL)
372 if (it->mailf == NULL) {
373 it->mailf = fopen(it->mailfn, "r");
374 if (it->mailf == NULL)
381 if (errno == EWOULDBLOCK)
383 syslog(LOG_INFO, "could not acquire queue file: %m");
388 dropspool(struct queue *queue, struct qitem *keep)
392 LIST_FOREACH(it, &queue->queue, next) {
396 if (it->queuef != NULL)
398 if (it->mailf != NULL)
404 flushqueue_since(unsigned int period)
408 char *flushfn = NULL;
410 if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
412 if (stat(flushfn, &st) < 0) {
418 if (gettimeofday(&now, 0) != 0)
421 /* Did the flush file get touched within the last period seconds? */
422 if (st.st_mtim.tv_sec + (int)period >= now.tv_sec)
429 flushqueue_signal(void)
431 char *flushfn = NULL;
434 if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
436 fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660);
439 syslog(LOG_ERR, "could not open flush file: %m");