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