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