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