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