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