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