xref: /dragonfly/libexec/dma/spool.c (revision e98bdfd3)
1 /*
2  * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Simon Schubert <2@0x2c.org>.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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
17  *    distribution.
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.
21  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #include "dfcompat.h"
37 
38 #include <sys/file.h>
39 #include <sys/stat.h>
40 
41 #include <ctype.h>
42 #include <dirent.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <inttypes.h>
47 #include <unistd.h>
48 #include <syslog.h>
49 
50 #include "dma.h"
51 
52 /*
53  * Spool file format:
54  *
55  * 'Q'id files (queue):
56  *   Organized like an RFC822 header, field: value.  Ignores unknown fields.
57  *   ID: id
58  *   Sender: envelope-from
59  *   Recipient: envelope-to
60  *
61  * 'M'id files (data):
62  *   mail data
63  *
64  * Each queue file needs to have a corresponding data file.
65  * One data file might be shared by linking it several times.
66  *
67  * Queue ids are unique, formed from the inode of the data file
68  * and a unique identifier.
69  */
70 
71 int
72 newspoolf(struct queue *queue)
73 {
74 	char fn[PATH_MAX+1];
75 	struct stat st;
76 	struct stritem *t;
77 	int fd;
78 
79 	if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
80 		return (-1);
81 
82 	fd = mkstemp(fn);
83 	if (fd < 0)
84 		return (-1);
85 	/* XXX group rights */
86 	if (fchmod(fd, 0660) < 0)
87 		goto fail;
88 	if (flock(fd, LOCK_EX) == -1)
89 		goto fail;
90 	queue->tmpf = strdup(fn);
91 	if (queue->tmpf == NULL)
92 		goto fail;
93 
94 	/*
95 	 * Assign queue id
96 	 */
97 	if (fstat(fd, &st) != 0)
98 		goto fail;
99 	if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
100 		goto fail;
101 
102 	queue->mailf = fdopen(fd, "r+");
103 	if (queue->mailf == NULL)
104 		goto fail;
105 
106 	t = malloc(sizeof(*t));
107 	if (t != NULL) {
108 		t->str = queue->tmpf;
109 		SLIST_INSERT_HEAD(&tmpfs, t, next);
110 	}
111 	return (0);
112 
113 fail:
114 	if (queue->mailf != NULL)
115 		fclose(queue->mailf);
116 	close(fd);
117 	unlink(fn);
118 	return (-1);
119 }
120 
121 static int
122 writequeuef(struct qitem *it)
123 {
124 	int error;
125 	int queuefd;
126 
127 	queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660);
128 	if (queuefd == -1)
129 		return (-1);
130 	if (fchmod(queuefd, 0660) < 0)
131 		return (-1);
132 	it->queuef = fdopen(queuefd, "w+");
133 	if (it->queuef == NULL)
134 		return (-1);
135 
136 	error = fprintf(it->queuef,
137 			"ID: %s\n"
138 			"Sender: %s\n"
139 			"Recipient: %s\n",
140 			 it->queueid,
141 			 it->sender,
142 			 it->addr);
143 
144 	if (error <= 0)
145 		return (-1);
146 
147 	if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
148 		return (-1);
149 
150 	return (0);
151 }
152 
153 static struct qitem *
154 readqueuef(struct queue *queue, char *queuefn)
155 {
156 	char line[1000];
157 	struct queue itmqueue;
158 	FILE *queuef = NULL;
159 	char *s;
160 	char *queueid = NULL, *sender = NULL, *addr = NULL;
161 	struct qitem *it = NULL;
162 
163 	bzero(&itmqueue, sizeof(itmqueue));
164 	LIST_INIT(&itmqueue.queue);
165 
166 	queuef = fopen(queuefn, "r");
167 	if (queuef == NULL)
168 		goto out;
169 
170 	while (!feof(queuef)) {
171 		if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
172 			break;
173 		line[strlen(line) - 1] = 0;	/* chop newline */
174 
175 		s = strchr(line, ':');
176 		if (s == NULL)
177 			goto malformed;
178 		*s = 0;
179 
180 		s++;
181 		while (isspace(*s))
182 			s++;
183 
184 		s = strdup(s);
185 		if (s == NULL)
186 			goto malformed;
187 
188 		if (strcmp(line, "ID") == 0) {
189 			queueid = s;
190 		} else if (strcmp(line, "Sender") == 0) {
191 			sender = s;
192 		} else if (strcmp(line, "Recipient") == 0) {
193 			addr = s;
194 		} else {
195 			syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
196 			       line, queuefn);
197 			free(s);
198 		}
199 	}
200 
201 	if (queueid == NULL || sender == NULL || addr == NULL ||
202 	    *queueid == 0 || *addr == 0) {
203 malformed:
204 		errno = EINVAL;
205 		syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
206 		goto out;
207 	}
208 
209 	if (add_recp(&itmqueue, addr, 0) != 0)
210 		goto out;
211 
212 	it = LIST_FIRST(&itmqueue.queue);
213 	it->sender = sender; sender = NULL;
214 	it->queueid = queueid; queueid = NULL;
215 	it->queuefn = queuefn; queuefn = NULL;
216 	LIST_INSERT_HEAD(&queue->queue, it, next);
217 
218 out:
219 	if (sender != NULL)
220 		free(sender);
221 	if (queueid != NULL)
222 		free(queueid);
223 	if (addr != NULL)
224 		free(addr);
225 	if (queuef != NULL)
226 		fclose(queuef);
227 
228 	return (it);
229 }
230 
231 int
232 linkspool(struct queue *queue)
233 {
234 	struct stat st;
235 	struct qitem *it;
236 
237 	if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
238 		goto delfiles;
239 
240 	syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
241 	       username, getuid(), queue->sender);
242 
243 	LIST_FOREACH(it, &queue->queue, next) {
244 		if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
245 			goto delfiles;
246 		if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
247 			goto delfiles;
248 		if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
249 			goto delfiles;
250 
251 		/* Neither file may not exist yet */
252 		if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
253 			goto delfiles;
254 
255 		if (writequeuef(it) != 0)
256 			goto delfiles;
257 
258 		if (link(queue->tmpf, it->mailfn) != 0)
259 			goto delfiles;
260 	}
261 
262 	LIST_FOREACH(it, &queue->queue, next) {
263 		syslog(LOG_INFO, "mail to=<%s> queued as %s",
264 		       it->addr, it->queueid);
265 	}
266 
267 	unlink(queue->tmpf);
268 	return (0);
269 
270 delfiles:
271 	LIST_FOREACH(it, &queue->queue, next) {
272 		unlink(it->mailfn);
273 		unlink(it->queuefn);
274 	}
275 	return (-1);
276 }
277 
278 int
279 load_queue(struct queue *queue)
280 {
281 	struct stat sb;
282 	struct qitem *it;
283 	DIR *spooldir;
284 	struct dirent *de;
285 	char *queuefn;
286 	char *mailfn;
287 
288 	bzero(queue, sizeof(*queue));
289 	LIST_INIT(&queue->queue);
290 
291 	spooldir = opendir(config.spooldir);
292 	if (spooldir == NULL)
293 		err(1, "reading queue");
294 
295 	while ((de = readdir(spooldir)) != NULL) {
296 		queuefn = NULL;
297 		mailfn = NULL;
298 
299 		/* ignore non-queue files */
300 		if (de->d_name[0] != 'Q')
301 			continue;
302 		if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
303 			goto fail;
304 		if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
305 			goto fail;
306 
307 		/*
308 		 * Some file systems don't provide a de->d_type, so we have to
309 		 * do an explicit stat on the queue file.
310 		 * Move on if it turns out to be something else than a file.
311 		 */
312 		if (stat(queuefn, &sb) != 0)
313 			goto skip_item;
314 		if (!S_ISREG(sb.st_mode)) {
315 			errno = EINVAL;
316 			goto skip_item;
317 		}
318 
319 		if (stat(mailfn, &sb) != 0)
320 			goto skip_item;
321 
322 		it = readqueuef(queue, queuefn);
323 		if (it == NULL)
324 			goto skip_item;
325 
326 		it->mailfn = mailfn;
327 		continue;
328 
329 skip_item:
330 		syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
331 		if (queuefn != NULL)
332 			free(queuefn);
333 		if (mailfn != NULL)
334 			free(mailfn);
335 	}
336 	closedir(spooldir);
337 	return (0);
338 
339 fail:
340 	return (-1);
341 }
342 
343 void
344 delqueue(struct qitem *it)
345 {
346 	unlink(it->mailfn);
347 	unlink(it->queuefn);
348 	if (it->queuef != NULL)
349 		fclose(it->queuef);
350 	if (it->mailf != NULL)
351 		fclose(it->mailf);
352 	free(it);
353 }
354 
355 int
356 acquirespool(struct qitem *it)
357 {
358 	int queuefd;
359 
360 	if (it->queuef == NULL) {
361 		queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
362 		if (queuefd < 0)
363 			goto fail;
364 		it->queuef = fdopen(queuefd, "r+");
365 		if (it->queuef == NULL)
366 			goto fail;
367 	}
368 
369 	if (it->mailf == NULL) {
370 		it->mailf = fopen(it->mailfn, "r");
371 		if (it->mailf == NULL)
372 			goto fail;
373 	}
374 
375 	return (0);
376 
377 fail:
378 	if (errno == EWOULDBLOCK)
379 		return (1);
380 	syslog(LOG_INFO, "could not acquire queue file: %m");
381 	return (-1);
382 }
383 
384 void
385 dropspool(struct queue *queue, struct qitem *keep)
386 {
387 	struct qitem *it;
388 
389 	LIST_FOREACH(it, &queue->queue, next) {
390 		if (it == keep)
391 			continue;
392 
393 		if (it->queuef != NULL)
394 			fclose(it->queuef);
395 		if (it->mailf != NULL)
396 			fclose(it->mailf);
397 	}
398 }
399 
400 int
401 flushqueue_since(unsigned int period)
402 {
403         struct stat st;
404 	struct timeval now;
405         char *flushfn = NULL;
406 
407 	if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
408 		return (0);
409 	if (stat(flushfn, &st) < 0) {
410 		free(flushfn);
411 		return (0);
412 	}
413 	free(flushfn);
414 	flushfn = NULL;
415 	if (gettimeofday(&now, 0) != 0)
416 		return (0);
417 
418 	/* Did the flush file get touched within the last period seconds? */
419 	if (st.st_mtim.tv_sec + (int)period >= now.tv_sec)
420 		return (1);
421 	else
422 		return (0);
423 }
424 
425 int
426 flushqueue_signal(void)
427 {
428         char *flushfn = NULL;
429 	int fd;
430 
431         if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
432 		return (-1);
433 	fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660);
434 	free(flushfn);
435 	if (fd < 0) {
436 		syslog(LOG_ERR, "could not open flush file: %m");
437 		return (-1);
438 	}
439         close(fd);
440 	return (0);
441 }
442