]> git.ipfire.org Git - people/ms/dma.git/blame - spool.c
dma: create spool files mode 660
[people/ms/dma.git] / spool.c
CommitLineData
fd1de51a
SS
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.
fd1de51a
SS
33 */
34
35#include <sys/stat.h>
36
6ddd63e1 37#include <ctype.h>
fd1de51a
SS
38#include <dirent.h>
39#include <err.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <inttypes.h>
43#include <unistd.h>
4d5af2b0 44#include <syslog.h>
fd1de51a
SS
45
46#include "dma.h"
47
48/*
49 * Spool file format:
50 *
51 * 'Q'id files (queue):
6ddd63e1
SS
52 * Organized like an RFC822 header, field: value. Ignores unknown fields.
53 * ID: id
54 * Sender: envelope-from
55 * Recipient: envelope-to
fd1de51a
SS
56 *
57 * 'M'id files (data):
fd1de51a
SS
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 *
24a87a55 63 * Queue ids are unique, formed from the inode of the data file
fd1de51a
SS
64 * and a unique identifier.
65 */
66
67int
057afad5 68newspoolf(struct queue *queue)
fd1de51a 69{
fd1de51a 70 char fn[PATH_MAX+1];
4d5af2b0 71 struct stat st;
fd1de51a 72 struct stritem *t;
87bdbc01 73 int fd;
fd1de51a
SS
74
75 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
76 return (-1);
77
87bdbc01
SS
78 fd = mkstemp(fn);
79 if (fd < 0)
fd1de51a 80 return (-1);
0bd3531e
SS
81 /* XXX group rights */
82 if (fchmod(fd, 0660) < 0)
83 goto fail;
87bdbc01
SS
84 if (flock(fd, LOCK_EX) == -1)
85 goto fail;
fd1de51a
SS
86 queue->tmpf = strdup(fn);
87 if (queue->tmpf == NULL)
88 goto fail;
89
87bdbc01
SS
90 /*
91 * Assign queue id
92 */
93 if (fstat(fd, &st) != 0)
fd1de51a 94 goto fail;
87bdbc01 95 if (asprintf(&queue->id, "%"PRIxMAX, st.st_ino) < 0)
fd1de51a
SS
96 goto fail;
97
87bdbc01
SS
98 queue->mailf = fdopen(fd, "r+");
99 if (queue->mailf == NULL)
100 goto fail;
fd1de51a 101
fd1de51a
SS
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
109fail:
87bdbc01
SS
110 if (queue->mailf != NULL)
111 fclose(queue->mailf);
112 close(fd);
fd1de51a
SS
113 unlink(fn);
114 return (-1);
115}
116
6ddd63e1
SS
117static int
118writequeuef(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
147static struct qitem *
148readqueuef(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) {
196malformed:
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
211out:
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
fd1de51a 224int
057afad5 225linkspool(struct queue *queue)
fd1de51a 226{
fd1de51a 227 struct stat st;
fd1de51a
SS
228 struct qitem *it;
229
87bdbc01
SS
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>",
057afad5 234 username, getuid(), queue->sender);
87bdbc01 235
fd1de51a 236 LIST_FOREACH(it, &queue->queue, next) {
4d5af2b0 237 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
fd1de51a
SS
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
6ddd63e1 248 if (writequeuef(it) != 0)
fd1de51a 249 goto delfiles;
fd1de51a
SS
250
251 if (link(queue->tmpf, it->mailfn) != 0)
252 goto delfiles;
253 }
254
4d5af2b0
SS
255 LIST_FOREACH(it, &queue->queue, next) {
256 syslog(LOG_INFO, "mail to=<%s> queued as %s",
257 it->addr, it->queueid);
258 }
fd1de51a
SS
259
260 unlink(queue->tmpf);
261 return (0);
262
263delfiles:
264 LIST_FOREACH(it, &queue->queue, next) {
fd1de51a 265 unlink(it->mailfn);
de13a881 266 unlink(it->queuefn);
fd1de51a
SS
267 }
268 return (-1);
269}
270
de13a881 271int
87bdbc01 272load_queue(struct queue *queue)
fd1de51a 273{
6ddd63e1 274 struct stat sb;
fd1de51a 275 struct qitem *it;
fd1de51a
SS
276 DIR *spooldir;
277 struct dirent *de;
fd1de51a
SS
278 char *queuefn;
279 char *mailfn;
fd1de51a 280
6f5efe4f 281 bzero(queue, sizeof(queue));
fd1de51a
SS
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) {
fd1de51a 289 queuefn = NULL;
6ddd63e1 290 mailfn = NULL;
fd1de51a
SS
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
6ddd63e1 302 if (stat(mailfn, &sb) != 0)
fd1de51a
SS
303 goto skip_item;
304
6ddd63e1
SS
305 it = readqueuef(queue, queuefn);
306 if (it == NULL)
fd1de51a 307 goto skip_item;
fd1de51a 308
fd1de51a 309 it->mailfn = mailfn;
fd1de51a
SS
310 continue;
311
312skip_item:
de13a881 313 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
fd1de51a
SS
314 if (queuefn != NULL)
315 free(queuefn);
316 if (mailfn != NULL)
317 free(queuefn);
fd1de51a
SS
318 }
319 closedir(spooldir);
de13a881 320 return (0);
fd1de51a
SS
321
322fail:
de13a881 323 return (-1);
fd1de51a
SS
324}
325
326void
327delqueue(struct qitem *it)
328{
fd1de51a 329 unlink(it->mailfn);
de13a881 330 unlink(it->queuefn);
87bdbc01
SS
331 if (it->queuef != NULL)
332 fclose(it->queuef);
333 if (it->mailf != NULL)
334 fclose(it->mailf);
fd1de51a
SS
335 free(it);
336}
87bdbc01
SS
337
338int
9aec6ad8 339acquirespool(struct qitem *it)
87bdbc01
SS
340{
341 int queuefd;
342
343 if (it->queuef == NULL) {
8e4fa698 344 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
87bdbc01 345 if (queuefd < 0)
de13a881 346 goto fail;
87bdbc01
SS
347 it->queuef = fdopen(queuefd, "r+");
348 if (it->queuef == NULL)
de13a881 349 goto fail;
87bdbc01
SS
350 }
351
352 if (it->mailf == NULL) {
353 it->mailf = fopen(it->mailfn, "r");
354 if (it->mailf == NULL)
de13a881 355 goto fail;
87bdbc01
SS
356 }
357
358 return (0);
de13a881
SS
359
360fail:
9aec6ad8 361 syslog(LOG_INFO, "could not acquire queue file: %m");
de13a881 362 return (-1);
87bdbc01
SS
363}
364
365void
366dropspool(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}