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