23f6e420e0231aee09250fc7c39ef89288c6fdfe
[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, 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 || s[0] == 0)
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 malformed:
197                 errno = EINVAL;
198                 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
199                 goto out;
200         }
201
202         if (add_recp(&itmqueue, addr, 0) != 0)
203                 goto out;
204
205         it = LIST_FIRST(&itmqueue.queue);
206         it->sender = sender; sender = NULL;
207         it->queueid = queueid; queueid = NULL;
208         it->queuefn = queuefn; queuefn = NULL;
209         LIST_INSERT_HEAD(&queue->queue, it, next);
210
211 out:
212         if (sender != NULL)
213                 free(sender);
214         if (queueid != NULL)
215                 free(queueid);
216         if (addr != NULL)
217                 free(addr);
218         if (queuef != NULL)
219                 fclose(queuef);
220
221         return (it);
222 }
223
224 int
225 linkspool(struct queue *queue)
226 {
227         struct stat st;
228         struct qitem *it;
229
230         if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
231                 goto delfiles;
232
233         syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
234                username, getuid(), queue->sender);
235
236         LIST_FOREACH(it, &queue->queue, next) {
237                 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
238                         goto delfiles;
239                 if (asprintf(&it->queuefn, "%s/Q%s", config->spooldir, it->queueid) <= 0)
240                         goto delfiles;
241                 if (asprintf(&it->mailfn, "%s/M%s", config->spooldir, it->queueid) <= 0)
242                         goto delfiles;
243
244                 /* Neither file may not exist yet */
245                 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
246                         goto delfiles;
247
248                 if (writequeuef(it) != 0)
249                         goto delfiles;
250
251                 if (link(queue->tmpf, it->mailfn) != 0)
252                         goto delfiles;
253         }
254
255         LIST_FOREACH(it, &queue->queue, next) {
256                 syslog(LOG_INFO, "mail to=<%s> queued as %s",
257                        it->addr, it->queueid);
258         }
259
260         unlink(queue->tmpf);
261         return (0);
262
263 delfiles:
264         LIST_FOREACH(it, &queue->queue, next) {
265                 unlink(it->mailfn);
266                 unlink(it->queuefn);
267         }
268         return (-1);
269 }
270
271 int
272 load_queue(struct queue *queue)
273 {
274         struct stat sb;
275         struct qitem *it;
276         DIR *spooldir;
277         struct dirent *de;
278         char *queuefn;
279         char *mailfn;
280
281         bzero(queue, sizeof(queue));
282         LIST_INIT(&queue->queue);
283
284         spooldir = opendir(config->spooldir);
285         if (spooldir == NULL)
286                 err(1, "reading queue");
287
288         while ((de = readdir(spooldir)) != NULL) {
289                 queuefn = NULL;
290                 mailfn = NULL;
291
292                 /* ignore temp files */
293                 if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
294                         continue;
295                 if (de->d_name[0] != 'Q')
296                         continue;
297                 if (asprintf(&queuefn, "%s/Q%s", config->spooldir, de->d_name + 1) < 0)
298                         goto fail;
299                 if (asprintf(&mailfn, "%s/M%s", config->spooldir, de->d_name + 1) < 0)
300                         goto fail;
301
302                 if (stat(mailfn, &sb) != 0)
303                         goto skip_item;
304
305                 it = readqueuef(queue, queuefn);
306                 if (it == NULL)
307                         goto skip_item;
308
309                 it->mailfn = mailfn;
310                 continue;
311
312 skip_item:
313                 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
314                 if (queuefn != NULL)
315                         free(queuefn);
316                 if (mailfn != NULL)
317                         free(queuefn);
318         }
319         closedir(spooldir);
320         return (0);
321
322 fail:
323         return (-1);
324 }
325
326 void
327 delqueue(struct qitem *it)
328 {
329         unlink(it->mailfn);
330         unlink(it->queuefn);
331         if (it->queuef != NULL)
332                 fclose(it->queuef);
333         if (it->mailf != NULL)
334                 fclose(it->mailf);
335         free(it);
336 }
337
338 int
339 acquirespool(struct qitem *it)
340 {
341         int queuefd;
342
343         if (it->queuef == NULL) {
344                 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
345                 if (queuefd < 0)
346                         goto fail;
347                 it->queuef = fdopen(queuefd, "r+");
348                 if (it->queuef == NULL)
349                         goto fail;
350         }
351
352         if (it->mailf == NULL) {
353                 it->mailf = fopen(it->mailfn, "r");
354                 if (it->mailf == NULL)
355                         goto fail;
356         }
357
358         return (0);
359
360 fail:
361         syslog(LOG_INFO, "could not acquire queue file: %m");
362         return (-1);
363 }
364
365 void
366 dropspool(struct queue *queue, struct qitem *keep)
367 {
368         struct qitem *it;
369
370         LIST_FOREACH(it, &queue->queue, next) {
371                 if (it == keep)
372                         continue;
373
374                 if (it->queuef != NULL)
375                         fclose(it->queuef);
376                 if (it->mailf != NULL)
377                         fclose(it->mailf);
378         }
379 }