xref: /dflybsd-src/libexec/dma/spool.c (revision 92fe556d1644256324e534f2cbaef0e73e2d85bc)
1f4e61a9fSSimon 'corecode' Schubert /*
237d59876SJohn Marino  * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3f4e61a9fSSimon 'corecode' Schubert  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
4f4e61a9fSSimon 'corecode' Schubert  *
5f4e61a9fSSimon 'corecode' Schubert  * This code is derived from software contributed to The DragonFly Project
637d59876SJohn Marino  * by Simon Schubert <2@0x2c.org>.
7f4e61a9fSSimon 'corecode' Schubert  *
8f4e61a9fSSimon 'corecode' Schubert  * Redistribution and use in source and binary forms, with or without
9f4e61a9fSSimon 'corecode' Schubert  * modification, are permitted provided that the following conditions
10f4e61a9fSSimon 'corecode' Schubert  * are met:
11f4e61a9fSSimon 'corecode' Schubert  *
12f4e61a9fSSimon 'corecode' Schubert  * 1. Redistributions of source code must retain the above copyright
13f4e61a9fSSimon 'corecode' Schubert  *    notice, this list of conditions and the following disclaimer.
14f4e61a9fSSimon 'corecode' Schubert  * 2. Redistributions in binary form must reproduce the above copyright
15f4e61a9fSSimon 'corecode' Schubert  *    notice, this list of conditions and the following disclaimer in
16f4e61a9fSSimon 'corecode' Schubert  *    the documentation and/or other materials provided with the
17f4e61a9fSSimon 'corecode' Schubert  *    distribution.
18f4e61a9fSSimon 'corecode' Schubert  * 3. Neither the name of The DragonFly Project nor the names of its
19f4e61a9fSSimon 'corecode' Schubert  *    contributors may be used to endorse or promote products derived
20f4e61a9fSSimon 'corecode' Schubert  *    from this software without specific, prior written permission.
21f4e61a9fSSimon 'corecode' Schubert  *
22f4e61a9fSSimon 'corecode' Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f4e61a9fSSimon 'corecode' Schubert  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24f4e61a9fSSimon 'corecode' Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25f4e61a9fSSimon 'corecode' Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26f4e61a9fSSimon 'corecode' Schubert  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27f4e61a9fSSimon 'corecode' Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28f4e61a9fSSimon 'corecode' Schubert  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29f4e61a9fSSimon 'corecode' Schubert  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30f4e61a9fSSimon 'corecode' Schubert  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31f4e61a9fSSimon 'corecode' Schubert  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32f4e61a9fSSimon 'corecode' Schubert  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f4e61a9fSSimon 'corecode' Schubert  * SUCH DAMAGE.
34f4e61a9fSSimon 'corecode' Schubert  */
35f4e61a9fSSimon 'corecode' Schubert 
36c8b07ee5SSascha Wildner #include "dfcompat.h"
37c8b07ee5SSascha Wildner 
38c8b07ee5SSascha Wildner #include <sys/file.h>
39f4e61a9fSSimon 'corecode' Schubert #include <sys/stat.h>
40*92fe556dSDaniel Fojt #include <sys/time.h>
41f4e61a9fSSimon 'corecode' Schubert 
42ebffba26SSimon Schubert #include <ctype.h>
43f4e61a9fSSimon 'corecode' Schubert #include <dirent.h>
44f4e61a9fSSimon 'corecode' Schubert #include <err.h>
45f4e61a9fSSimon 'corecode' Schubert #include <errno.h>
46f4e61a9fSSimon 'corecode' Schubert #include <fcntl.h>
47f4e61a9fSSimon 'corecode' Schubert #include <inttypes.h>
48f4e61a9fSSimon 'corecode' Schubert #include <unistd.h>
49*92fe556dSDaniel Fojt #include <strings.h>
50*92fe556dSDaniel Fojt #include <string.h>
51405f48eeSSimon Schubert #include <syslog.h>
52f4e61a9fSSimon 'corecode' Schubert 
53f4e61a9fSSimon 'corecode' Schubert #include "dma.h"
54f4e61a9fSSimon 'corecode' Schubert 
55f4e61a9fSSimon 'corecode' Schubert /*
56f4e61a9fSSimon 'corecode' Schubert  * Spool file format:
57f4e61a9fSSimon 'corecode' Schubert  *
58f4e61a9fSSimon 'corecode' Schubert  * 'Q'id files (queue):
59ebffba26SSimon Schubert  *   Organized like an RFC822 header, field: value.  Ignores unknown fields.
60ebffba26SSimon Schubert  *   ID: id
61ebffba26SSimon Schubert  *   Sender: envelope-from
62ebffba26SSimon Schubert  *   Recipient: envelope-to
63f4e61a9fSSimon 'corecode' Schubert  *
64f4e61a9fSSimon 'corecode' Schubert  * 'M'id files (data):
65f4e61a9fSSimon 'corecode' Schubert  *   mail data
66f4e61a9fSSimon 'corecode' Schubert  *
67f4e61a9fSSimon 'corecode' Schubert  * Each queue file needs to have a corresponding data file.
68f4e61a9fSSimon 'corecode' Schubert  * One data file might be shared by linking it several times.
69f4e61a9fSSimon 'corecode' Schubert  *
7076ad9740SSimon Schubert  * Queue ids are unique, formed from the inode of the data file
71f4e61a9fSSimon 'corecode' Schubert  * and a unique identifier.
72f4e61a9fSSimon 'corecode' Schubert  */
73f4e61a9fSSimon 'corecode' Schubert 
74f4e61a9fSSimon 'corecode' Schubert int
newspoolf(struct queue * queue)751c9e6b7bSSimon Schubert newspoolf(struct queue *queue)
76f4e61a9fSSimon 'corecode' Schubert {
77f4e61a9fSSimon 'corecode' Schubert 	char fn[PATH_MAX+1];
78405f48eeSSimon Schubert 	struct stat st;
79f4e61a9fSSimon 'corecode' Schubert 	struct stritem *t;
809afa363fSSimon Schubert 	int fd;
81f4e61a9fSSimon 'corecode' Schubert 
82ca259d14SSimon Schubert 	if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
83f4e61a9fSSimon 'corecode' Schubert 		return (-1);
84f4e61a9fSSimon 'corecode' Schubert 
859afa363fSSimon Schubert 	fd = mkstemp(fn);
869afa363fSSimon Schubert 	if (fd < 0)
87f4e61a9fSSimon 'corecode' Schubert 		return (-1);
886e30778bSSimon Schubert 	/* XXX group rights */
896e30778bSSimon Schubert 	if (fchmod(fd, 0660) < 0)
906e30778bSSimon Schubert 		goto fail;
919afa363fSSimon Schubert 	if (flock(fd, LOCK_EX) == -1)
929afa363fSSimon Schubert 		goto fail;
93f4e61a9fSSimon 'corecode' Schubert 	queue->tmpf = strdup(fn);
94f4e61a9fSSimon 'corecode' Schubert 	if (queue->tmpf == NULL)
95f4e61a9fSSimon 'corecode' Schubert 		goto fail;
96f4e61a9fSSimon 'corecode' Schubert 
97405f48eeSSimon Schubert 	/*
98405f48eeSSimon Schubert 	 * Assign queue id
99405f48eeSSimon Schubert 	 */
1009afa363fSSimon Schubert 	if (fstat(fd, &st) != 0)
1019afa363fSSimon Schubert 		goto fail;
102a0a50d0bSMatthias Schmidt 	if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
1039afa363fSSimon Schubert 		goto fail;
1049afa363fSSimon Schubert 
1059afa363fSSimon Schubert 	queue->mailf = fdopen(fd, "r+");
1069afa363fSSimon Schubert 	if (queue->mailf == NULL)
1079afa363fSSimon Schubert 		goto fail;
1089afa363fSSimon Schubert 
109f4e61a9fSSimon 'corecode' Schubert 	t = malloc(sizeof(*t));
110f4e61a9fSSimon 'corecode' Schubert 	if (t != NULL) {
111f4e61a9fSSimon 'corecode' Schubert 		t->str = queue->tmpf;
112f4e61a9fSSimon 'corecode' Schubert 		SLIST_INSERT_HEAD(&tmpfs, t, next);
113f4e61a9fSSimon 'corecode' Schubert 	}
114f4e61a9fSSimon 'corecode' Schubert 	return (0);
115f4e61a9fSSimon 'corecode' Schubert 
116f4e61a9fSSimon 'corecode' Schubert fail:
1179afa363fSSimon Schubert 	if (queue->mailf != NULL)
1189afa363fSSimon Schubert 		fclose(queue->mailf);
1199afa363fSSimon Schubert 	close(fd);
120f4e61a9fSSimon 'corecode' Schubert 	unlink(fn);
121f4e61a9fSSimon 'corecode' Schubert 	return (-1);
122f4e61a9fSSimon 'corecode' Schubert }
123f4e61a9fSSimon 'corecode' Schubert 
124ebffba26SSimon Schubert static int
writequeuef(struct qitem * it)125ebffba26SSimon Schubert writequeuef(struct qitem *it)
126ebffba26SSimon Schubert {
127ebffba26SSimon Schubert 	int error;
128ebffba26SSimon Schubert 	int queuefd;
129ebffba26SSimon Schubert 
130c8b07ee5SSascha Wildner 	queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660);
131ebffba26SSimon Schubert 	if (queuefd == -1)
132ebffba26SSimon Schubert 		return (-1);
133c8b07ee5SSascha Wildner 	if (fchmod(queuefd, 0660) < 0)
134c8b07ee5SSascha Wildner 		return (-1);
135ebffba26SSimon Schubert 	it->queuef = fdopen(queuefd, "w+");
136ebffba26SSimon Schubert 	if (it->queuef == NULL)
137ebffba26SSimon Schubert 		return (-1);
138ebffba26SSimon Schubert 
139ebffba26SSimon Schubert 	error = fprintf(it->queuef,
140ebffba26SSimon Schubert 			"ID: %s\n"
141ebffba26SSimon Schubert 			"Sender: %s\n"
142ebffba26SSimon Schubert 			"Recipient: %s\n",
143ebffba26SSimon Schubert 			 it->queueid,
144ebffba26SSimon Schubert 			 it->sender,
145ebffba26SSimon Schubert 			 it->addr);
146ebffba26SSimon Schubert 
147ebffba26SSimon Schubert 	if (error <= 0)
148ebffba26SSimon Schubert 		return (-1);
149ebffba26SSimon Schubert 
150ebffba26SSimon Schubert 	if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
151ebffba26SSimon Schubert 		return (-1);
152ebffba26SSimon Schubert 
153ebffba26SSimon Schubert 	return (0);
154ebffba26SSimon Schubert }
155ebffba26SSimon Schubert 
156ebffba26SSimon Schubert static struct qitem *
readqueuef(struct queue * queue,char * queuefn)157ebffba26SSimon Schubert readqueuef(struct queue *queue, char *queuefn)
158ebffba26SSimon Schubert {
159ebffba26SSimon Schubert 	char line[1000];
160ebffba26SSimon Schubert 	struct queue itmqueue;
161ebffba26SSimon Schubert 	FILE *queuef = NULL;
162ebffba26SSimon Schubert 	char *s;
163ebffba26SSimon Schubert 	char *queueid = NULL, *sender = NULL, *addr = NULL;
164ebffba26SSimon Schubert 	struct qitem *it = NULL;
165ebffba26SSimon Schubert 
166ebffba26SSimon Schubert 	bzero(&itmqueue, sizeof(itmqueue));
167ebffba26SSimon Schubert 	LIST_INIT(&itmqueue.queue);
168ebffba26SSimon Schubert 
169ebffba26SSimon Schubert 	queuef = fopen(queuefn, "r");
170ebffba26SSimon Schubert 	if (queuef == NULL)
171ebffba26SSimon Schubert 		goto out;
172ebffba26SSimon Schubert 
173ebffba26SSimon Schubert 	while (!feof(queuef)) {
174ebffba26SSimon Schubert 		if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
175ebffba26SSimon Schubert 			break;
176ebffba26SSimon Schubert 		line[strlen(line) - 1] = 0;	/* chop newline */
177ebffba26SSimon Schubert 
178ebffba26SSimon Schubert 		s = strchr(line, ':');
179ebffba26SSimon Schubert 		if (s == NULL)
180ebffba26SSimon Schubert 			goto malformed;
181ebffba26SSimon Schubert 		*s = 0;
182ebffba26SSimon Schubert 
183ebffba26SSimon Schubert 		s++;
184ebffba26SSimon Schubert 		while (isspace(*s))
185ebffba26SSimon Schubert 			s++;
186ebffba26SSimon Schubert 
187ebffba26SSimon Schubert 		s = strdup(s);
1884add537eSSimon Schubert 		if (s == NULL)
189ebffba26SSimon Schubert 			goto malformed;
190ebffba26SSimon Schubert 
191ebffba26SSimon Schubert 		if (strcmp(line, "ID") == 0) {
192ebffba26SSimon Schubert 			queueid = s;
193ebffba26SSimon Schubert 		} else if (strcmp(line, "Sender") == 0) {
194ebffba26SSimon Schubert 			sender = s;
195ebffba26SSimon Schubert 		} else if (strcmp(line, "Recipient") == 0) {
196ebffba26SSimon Schubert 			addr = s;
197ebffba26SSimon Schubert 		} else {
198ebffba26SSimon Schubert 			syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
199ebffba26SSimon Schubert 			       line, queuefn);
200ebffba26SSimon Schubert 			free(s);
201ebffba26SSimon Schubert 		}
202ebffba26SSimon Schubert 	}
203ebffba26SSimon Schubert 
2044add537eSSimon Schubert 	if (queueid == NULL || sender == NULL || addr == NULL ||
2054add537eSSimon Schubert 	    *queueid == 0 || *addr == 0) {
206ebffba26SSimon Schubert malformed:
207ebffba26SSimon Schubert 		errno = EINVAL;
208ebffba26SSimon Schubert 		syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
209ebffba26SSimon Schubert 		goto out;
210ebffba26SSimon Schubert 	}
211ebffba26SSimon Schubert 
212ebffba26SSimon Schubert 	if (add_recp(&itmqueue, addr, 0) != 0)
213ebffba26SSimon Schubert 		goto out;
214ebffba26SSimon Schubert 
215ebffba26SSimon Schubert 	it = LIST_FIRST(&itmqueue.queue);
216ebffba26SSimon Schubert 	it->sender = sender; sender = NULL;
217ebffba26SSimon Schubert 	it->queueid = queueid; queueid = NULL;
218ebffba26SSimon Schubert 	it->queuefn = queuefn; queuefn = NULL;
219ebffba26SSimon Schubert 	LIST_INSERT_HEAD(&queue->queue, it, next);
220ebffba26SSimon Schubert 
221ebffba26SSimon Schubert out:
222ebffba26SSimon Schubert 	if (sender != NULL)
223ebffba26SSimon Schubert 		free(sender);
224ebffba26SSimon Schubert 	if (queueid != NULL)
225ebffba26SSimon Schubert 		free(queueid);
226ebffba26SSimon Schubert 	if (addr != NULL)
227ebffba26SSimon Schubert 		free(addr);
228ebffba26SSimon Schubert 	if (queuef != NULL)
229ebffba26SSimon Schubert 		fclose(queuef);
230ebffba26SSimon Schubert 
231ebffba26SSimon Schubert 	return (it);
232ebffba26SSimon Schubert }
233ebffba26SSimon Schubert 
234f4e61a9fSSimon 'corecode' Schubert int
linkspool(struct queue * queue)2351c9e6b7bSSimon Schubert linkspool(struct queue *queue)
236f4e61a9fSSimon 'corecode' Schubert {
237f4e61a9fSSimon 'corecode' Schubert 	struct stat st;
238f4e61a9fSSimon 'corecode' Schubert 	struct qitem *it;
239f4e61a9fSSimon 'corecode' Schubert 
2409afa363fSSimon Schubert 	if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
2419afa363fSSimon Schubert 		goto delfiles;
2429afa363fSSimon Schubert 
2439afa363fSSimon Schubert 	syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
2441c9e6b7bSSimon Schubert 	       username, getuid(), queue->sender);
2459afa363fSSimon Schubert 
246f4e61a9fSSimon 'corecode' Schubert 	LIST_FOREACH(it, &queue->queue, next) {
247405f48eeSSimon Schubert 		if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
248f4e61a9fSSimon 'corecode' Schubert 			goto delfiles;
249ca259d14SSimon Schubert 		if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
250f4e61a9fSSimon 'corecode' Schubert 			goto delfiles;
251ca259d14SSimon Schubert 		if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
252f4e61a9fSSimon 'corecode' Schubert 			goto delfiles;
253f4e61a9fSSimon 'corecode' Schubert 
254f4e61a9fSSimon 'corecode' Schubert 		/* Neither file may not exist yet */
255f4e61a9fSSimon 'corecode' Schubert 		if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
256f4e61a9fSSimon 'corecode' Schubert 			goto delfiles;
257f4e61a9fSSimon 'corecode' Schubert 
258ebffba26SSimon Schubert 		if (writequeuef(it) != 0)
2599afa363fSSimon Schubert 			goto delfiles;
260f4e61a9fSSimon 'corecode' Schubert 
261f4e61a9fSSimon 'corecode' Schubert 		if (link(queue->tmpf, it->mailfn) != 0)
262f4e61a9fSSimon 'corecode' Schubert 			goto delfiles;
263f4e61a9fSSimon 'corecode' Schubert 	}
264f4e61a9fSSimon 'corecode' Schubert 
265405f48eeSSimon Schubert 	LIST_FOREACH(it, &queue->queue, next) {
266405f48eeSSimon Schubert 		syslog(LOG_INFO, "mail to=<%s> queued as %s",
267405f48eeSSimon Schubert 		       it->addr, it->queueid);
268405f48eeSSimon Schubert 	}
269f4e61a9fSSimon 'corecode' Schubert 
270f4e61a9fSSimon 'corecode' Schubert 	unlink(queue->tmpf);
271f4e61a9fSSimon 'corecode' Schubert 	return (0);
272f4e61a9fSSimon 'corecode' Schubert 
273f4e61a9fSSimon 'corecode' Schubert delfiles:
274f4e61a9fSSimon 'corecode' Schubert 	LIST_FOREACH(it, &queue->queue, next) {
275f4e61a9fSSimon 'corecode' Schubert 		unlink(it->mailfn);
2761da0a9f2SSimon Schubert 		unlink(it->queuefn);
277f4e61a9fSSimon 'corecode' Schubert 	}
278f4e61a9fSSimon 'corecode' Schubert 	return (-1);
279f4e61a9fSSimon 'corecode' Schubert }
280f4e61a9fSSimon 'corecode' Schubert 
2811da0a9f2SSimon Schubert int
load_queue(struct queue * queue)2829afa363fSSimon Schubert load_queue(struct queue *queue)
283f4e61a9fSSimon 'corecode' Schubert {
284ebffba26SSimon Schubert 	struct stat sb;
285f4e61a9fSSimon 'corecode' Schubert 	struct qitem *it;
286f4e61a9fSSimon 'corecode' Schubert 	DIR *spooldir;
287f4e61a9fSSimon 'corecode' Schubert 	struct dirent *de;
288f4e61a9fSSimon 'corecode' Schubert 	char *queuefn;
289f4e61a9fSSimon 'corecode' Schubert 	char *mailfn;
290f4e61a9fSSimon 'corecode' Schubert 
2918075c3b8SSascha Wildner 	bzero(queue, sizeof(*queue));
292f4e61a9fSSimon 'corecode' Schubert 	LIST_INIT(&queue->queue);
293f4e61a9fSSimon 'corecode' Schubert 
294ca259d14SSimon Schubert 	spooldir = opendir(config.spooldir);
295f4e61a9fSSimon 'corecode' Schubert 	if (spooldir == NULL)
296*92fe556dSDaniel Fojt 		err(EX_NOINPUT, "reading queue");
297f4e61a9fSSimon 'corecode' Schubert 
298f4e61a9fSSimon 'corecode' Schubert 	while ((de = readdir(spooldir)) != NULL) {
299f4e61a9fSSimon 'corecode' Schubert 		queuefn = NULL;
300ebffba26SSimon Schubert 		mailfn = NULL;
301f4e61a9fSSimon 'corecode' Schubert 
302c8b07ee5SSascha Wildner 		/* ignore non-queue files */
303f4e61a9fSSimon 'corecode' Schubert 		if (de->d_name[0] != 'Q')
304f4e61a9fSSimon 'corecode' Schubert 			continue;
305ca259d14SSimon Schubert 		if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
306f4e61a9fSSimon 'corecode' Schubert 			goto fail;
307ca259d14SSimon Schubert 		if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
308f4e61a9fSSimon 'corecode' Schubert 			goto fail;
309f4e61a9fSSimon 'corecode' Schubert 
310c8b07ee5SSascha Wildner 		/*
311c8b07ee5SSascha Wildner 		 * Some file systems don't provide a de->d_type, so we have to
312c8b07ee5SSascha Wildner 		 * do an explicit stat on the queue file.
313c8b07ee5SSascha Wildner 		 * Move on if it turns out to be something else than a file.
314c8b07ee5SSascha Wildner 		 */
315c8b07ee5SSascha Wildner 		if (stat(queuefn, &sb) != 0)
316c8b07ee5SSascha Wildner 			goto skip_item;
317c8b07ee5SSascha Wildner 		if (!S_ISREG(sb.st_mode)) {
318c8b07ee5SSascha Wildner 			errno = EINVAL;
319c8b07ee5SSascha Wildner 			goto skip_item;
320c8b07ee5SSascha Wildner 		}
321c8b07ee5SSascha Wildner 
322ebffba26SSimon Schubert 		if (stat(mailfn, &sb) != 0)
323f4e61a9fSSimon 'corecode' Schubert 			goto skip_item;
324f4e61a9fSSimon 'corecode' Schubert 
325ebffba26SSimon Schubert 		it = readqueuef(queue, queuefn);
326ebffba26SSimon Schubert 		if (it == NULL)
327f4e61a9fSSimon 'corecode' Schubert 			goto skip_item;
328f4e61a9fSSimon 'corecode' Schubert 
329f4e61a9fSSimon 'corecode' Schubert 		it->mailfn = mailfn;
330f4e61a9fSSimon 'corecode' Schubert 		continue;
331f4e61a9fSSimon 'corecode' Schubert 
332f4e61a9fSSimon 'corecode' Schubert skip_item:
3331da0a9f2SSimon Schubert 		syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
334f4e61a9fSSimon 'corecode' Schubert 		if (queuefn != NULL)
335f4e61a9fSSimon 'corecode' Schubert 			free(queuefn);
336f4e61a9fSSimon 'corecode' Schubert 		if (mailfn != NULL)
337d557d463SMatthias Schmidt 			free(mailfn);
338f4e61a9fSSimon 'corecode' Schubert 	}
339f4e61a9fSSimon 'corecode' Schubert 	closedir(spooldir);
3401da0a9f2SSimon Schubert 	return (0);
341f4e61a9fSSimon 'corecode' Schubert 
342f4e61a9fSSimon 'corecode' Schubert fail:
3431da0a9f2SSimon Schubert 	return (-1);
344f4e61a9fSSimon 'corecode' Schubert }
345f4e61a9fSSimon 'corecode' Schubert 
346f4e61a9fSSimon 'corecode' Schubert void
delqueue(struct qitem * it)347f4e61a9fSSimon 'corecode' Schubert delqueue(struct qitem *it)
348f4e61a9fSSimon 'corecode' Schubert {
349f4e61a9fSSimon 'corecode' Schubert 	unlink(it->mailfn);
3501da0a9f2SSimon Schubert 	unlink(it->queuefn);
3519afa363fSSimon Schubert 	if (it->queuef != NULL)
3529afa363fSSimon Schubert 		fclose(it->queuef);
3539afa363fSSimon Schubert 	if (it->mailf != NULL)
354f4e61a9fSSimon 'corecode' Schubert 		fclose(it->mailf);
355f4e61a9fSSimon 'corecode' Schubert 	free(it);
356f4e61a9fSSimon 'corecode' Schubert }
3579afa363fSSimon Schubert 
3589afa363fSSimon Schubert int
acquirespool(struct qitem * it)35924c80b2bSSascha Wildner acquirespool(struct qitem *it)
3609afa363fSSimon Schubert {
3619afa363fSSimon Schubert 	int queuefd;
3629afa363fSSimon Schubert 
3639afa363fSSimon Schubert 	if (it->queuef == NULL) {
364b95bffd0SSimon Schubert 		queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
3659afa363fSSimon Schubert 		if (queuefd < 0)
3661da0a9f2SSimon Schubert 			goto fail;
3679afa363fSSimon Schubert 		it->queuef = fdopen(queuefd, "r+");
3689afa363fSSimon Schubert 		if (it->queuef == NULL)
3691da0a9f2SSimon Schubert 			goto fail;
3709afa363fSSimon Schubert 	}
3719afa363fSSimon Schubert 
3729afa363fSSimon Schubert 	if (it->mailf == NULL) {
3739afa363fSSimon Schubert 		it->mailf = fopen(it->mailfn, "r");
3749afa363fSSimon Schubert 		if (it->mailf == NULL)
3751da0a9f2SSimon Schubert 			goto fail;
3769afa363fSSimon Schubert 	}
3779afa363fSSimon Schubert 
3789afa363fSSimon Schubert 	return (0);
3791da0a9f2SSimon Schubert 
3801da0a9f2SSimon Schubert fail:
38114dfb991SJoris Giovannangeli 	if (errno == EWOULDBLOCK)
38214dfb991SJoris Giovannangeli 		return (1);
38324c80b2bSSascha Wildner 	syslog(LOG_INFO, "could not acquire queue file: %m");
3841da0a9f2SSimon Schubert 	return (-1);
3859afa363fSSimon Schubert }
3869afa363fSSimon Schubert 
3879afa363fSSimon Schubert void
dropspool(struct queue * queue,struct qitem * keep)3889afa363fSSimon Schubert dropspool(struct queue *queue, struct qitem *keep)
3899afa363fSSimon Schubert {
3909afa363fSSimon Schubert 	struct qitem *it;
3919afa363fSSimon Schubert 
3929afa363fSSimon Schubert 	LIST_FOREACH(it, &queue->queue, next) {
3939afa363fSSimon Schubert 		if (it == keep)
3949afa363fSSimon Schubert 			continue;
3959afa363fSSimon Schubert 
3969afa363fSSimon Schubert 		if (it->queuef != NULL)
3979afa363fSSimon Schubert 			fclose(it->queuef);
3989afa363fSSimon Schubert 		if (it->mailf != NULL)
3999afa363fSSimon Schubert 			fclose(it->mailf);
4009afa363fSSimon Schubert 	}
4019afa363fSSimon Schubert }
40214dfb991SJoris Giovannangeli 
40314dfb991SJoris Giovannangeli int
flushqueue_since(unsigned int period)40414dfb991SJoris Giovannangeli flushqueue_since(unsigned int period)
40514dfb991SJoris Giovannangeli {
40614dfb991SJoris Giovannangeli         struct stat st;
40714dfb991SJoris Giovannangeli 	struct timeval now;
40814dfb991SJoris Giovannangeli         char *flushfn = NULL;
40914dfb991SJoris Giovannangeli 
41014dfb991SJoris Giovannangeli 	if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
41114dfb991SJoris Giovannangeli 		return (0);
41214dfb991SJoris Giovannangeli 	if (stat(flushfn, &st) < 0) {
41314dfb991SJoris Giovannangeli 		free(flushfn);
41414dfb991SJoris Giovannangeli 		return (0);
41514dfb991SJoris Giovannangeli 	}
41614dfb991SJoris Giovannangeli 	free(flushfn);
41714dfb991SJoris Giovannangeli 	flushfn = NULL;
41814dfb991SJoris Giovannangeli 	if (gettimeofday(&now, 0) != 0)
41914dfb991SJoris Giovannangeli 		return (0);
42014dfb991SJoris Giovannangeli 
42114dfb991SJoris Giovannangeli 	/* Did the flush file get touched within the last period seconds? */
422efcc709cSSascha Wildner 	if (st.st_mtim.tv_sec + period >= now.tv_sec)
42314dfb991SJoris Giovannangeli 		return (1);
42414dfb991SJoris Giovannangeli 	else
42514dfb991SJoris Giovannangeli 		return (0);
42614dfb991SJoris Giovannangeli }
42714dfb991SJoris Giovannangeli 
42814dfb991SJoris Giovannangeli int
flushqueue_signal(void)42914dfb991SJoris Giovannangeli flushqueue_signal(void)
43014dfb991SJoris Giovannangeli {
43114dfb991SJoris Giovannangeli         char *flushfn = NULL;
43214dfb991SJoris Giovannangeli 	int fd;
43314dfb991SJoris Giovannangeli 
43414dfb991SJoris Giovannangeli         if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
43514dfb991SJoris Giovannangeli 		return (-1);
43614dfb991SJoris Giovannangeli 	fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660);
43714dfb991SJoris Giovannangeli 	free(flushfn);
43814dfb991SJoris Giovannangeli 	if (fd < 0) {
43914dfb991SJoris Giovannangeli 		syslog(LOG_ERR, "could not open flush file: %m");
44014dfb991SJoris Giovannangeli 		return (-1);
44114dfb991SJoris Giovannangeli 	}
44214dfb991SJoris Giovannangeli         close(fd);
44314dfb991SJoris Giovannangeli 	return (0);
44414dfb991SJoris Giovannangeli }
445