]> git.ipfire.org Git - people/ms/dma.git/blame - spool.c
fix wrong argument to free()
[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
9b921550
SS
35#include "dfcompat.h"
36
37#include <sys/file.h>
fd1de51a
SS
38#include <sys/stat.h>
39
6ddd63e1 40#include <ctype.h>
fd1de51a
SS
41#include <dirent.h>
42#include <err.h>
43#include <errno.h>
44#include <fcntl.h>
45#include <inttypes.h>
46#include <unistd.h>
4d5af2b0 47#include <syslog.h>
fd1de51a
SS
48
49#include "dma.h"
50
51/*
52 * Spool file format:
53 *
54 * 'Q'id files (queue):
6ddd63e1
SS
55 * Organized like an RFC822 header, field: value. Ignores unknown fields.
56 * ID: id
57 * Sender: envelope-from
58 * Recipient: envelope-to
fd1de51a
SS
59 *
60 * 'M'id files (data):
fd1de51a
SS
61 * mail data
62 *
63 * Each queue file needs to have a corresponding data file.
64 * One data file might be shared by linking it several times.
65 *
24a87a55 66 * Queue ids are unique, formed from the inode of the data file
fd1de51a
SS
67 * and a unique identifier.
68 */
69
70int
057afad5 71newspoolf(struct queue *queue)
fd1de51a 72{
fd1de51a 73 char fn[PATH_MAX+1];
4d5af2b0 74 struct stat st;
fd1de51a 75 struct stritem *t;
87bdbc01 76 int fd;
fd1de51a 77
a0c4afa6 78 if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
fd1de51a
SS
79 return (-1);
80
87bdbc01
SS
81 fd = mkstemp(fn);
82 if (fd < 0)
fd1de51a 83 return (-1);
0bd3531e
SS
84 /* XXX group rights */
85 if (fchmod(fd, 0660) < 0)
86 goto fail;
87bdbc01
SS
87 if (flock(fd, LOCK_EX) == -1)
88 goto fail;
fd1de51a
SS
89 queue->tmpf = strdup(fn);
90 if (queue->tmpf == NULL)
91 goto fail;
92
87bdbc01
SS
93 /*
94 * Assign queue id
95 */
96 if (fstat(fd, &st) != 0)
fd1de51a 97 goto fail;
2f4f1f06 98 if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
fd1de51a
SS
99 goto fail;
100
87bdbc01
SS
101 queue->mailf = fdopen(fd, "r+");
102 if (queue->mailf == NULL)
103 goto fail;
fd1de51a 104
fd1de51a
SS
105 t = malloc(sizeof(*t));
106 if (t != NULL) {
107 t->str = queue->tmpf;
108 SLIST_INSERT_HEAD(&tmpfs, t, next);
109 }
110 return (0);
111
112fail:
87bdbc01
SS
113 if (queue->mailf != NULL)
114 fclose(queue->mailf);
115 close(fd);
fd1de51a
SS
116 unlink(fn);
117 return (-1);
118}
119
6ddd63e1
SS
120static int
121writequeuef(struct qitem *it)
122{
123 int error;
124 int queuefd;
125
126 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
127 if (queuefd == -1)
128 return (-1);
129 it->queuef = fdopen(queuefd, "w+");
130 if (it->queuef == NULL)
131 return (-1);
132
133 error = fprintf(it->queuef,
134 "ID: %s\n"
135 "Sender: %s\n"
136 "Recipient: %s\n",
137 it->queueid,
138 it->sender,
139 it->addr);
140
141 if (error <= 0)
142 return (-1);
143
144 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
145 return (-1);
146
147 return (0);
148}
149
150static struct qitem *
151readqueuef(struct queue *queue, char *queuefn)
152{
153 char line[1000];
154 struct queue itmqueue;
155 FILE *queuef = NULL;
156 char *s;
157 char *queueid = NULL, *sender = NULL, *addr = NULL;
158 struct qitem *it = NULL;
159
160 bzero(&itmqueue, sizeof(itmqueue));
161 LIST_INIT(&itmqueue.queue);
162
163 queuef = fopen(queuefn, "r");
164 if (queuef == NULL)
165 goto out;
166
167 while (!feof(queuef)) {
168 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
169 break;
170 line[strlen(line) - 1] = 0; /* chop newline */
171
172 s = strchr(line, ':');
173 if (s == NULL)
174 goto malformed;
175 *s = 0;
176
177 s++;
178 while (isspace(*s))
179 s++;
180
181 s = strdup(s);
7aa2b3b7 182 if (s == NULL)
6ddd63e1
SS
183 goto malformed;
184
185 if (strcmp(line, "ID") == 0) {
186 queueid = s;
187 } else if (strcmp(line, "Sender") == 0) {
188 sender = s;
189 } else if (strcmp(line, "Recipient") == 0) {
190 addr = s;
191 } else {
192 syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
193 line, queuefn);
194 free(s);
195 }
196 }
197
7aa2b3b7
SS
198 if (queueid == NULL || sender == NULL || addr == NULL ||
199 *queueid == 0 || *addr == 0) {
6ddd63e1
SS
200malformed:
201 errno = EINVAL;
202 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
203 goto out;
204 }
205
206 if (add_recp(&itmqueue, addr, 0) != 0)
207 goto out;
208
209 it = LIST_FIRST(&itmqueue.queue);
210 it->sender = sender; sender = NULL;
211 it->queueid = queueid; queueid = NULL;
212 it->queuefn = queuefn; queuefn = NULL;
213 LIST_INSERT_HEAD(&queue->queue, it, next);
214
215out:
216 if (sender != NULL)
217 free(sender);
218 if (queueid != NULL)
219 free(queueid);
220 if (addr != NULL)
221 free(addr);
222 if (queuef != NULL)
223 fclose(queuef);
224
225 return (it);
226}
227
fd1de51a 228int
057afad5 229linkspool(struct queue *queue)
fd1de51a 230{
fd1de51a 231 struct stat st;
fd1de51a
SS
232 struct qitem *it;
233
87bdbc01
SS
234 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
235 goto delfiles;
236
237 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
057afad5 238 username, getuid(), queue->sender);
87bdbc01 239
fd1de51a 240 LIST_FOREACH(it, &queue->queue, next) {
4d5af2b0 241 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
fd1de51a 242 goto delfiles;
a0c4afa6 243 if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
fd1de51a 244 goto delfiles;
a0c4afa6 245 if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
fd1de51a
SS
246 goto delfiles;
247
248 /* Neither file may not exist yet */
249 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
250 goto delfiles;
251
6ddd63e1 252 if (writequeuef(it) != 0)
fd1de51a 253 goto delfiles;
fd1de51a
SS
254
255 if (link(queue->tmpf, it->mailfn) != 0)
256 goto delfiles;
257 }
258
4d5af2b0
SS
259 LIST_FOREACH(it, &queue->queue, next) {
260 syslog(LOG_INFO, "mail to=<%s> queued as %s",
261 it->addr, it->queueid);
262 }
fd1de51a
SS
263
264 unlink(queue->tmpf);
265 return (0);
266
267delfiles:
268 LIST_FOREACH(it, &queue->queue, next) {
fd1de51a 269 unlink(it->mailfn);
de13a881 270 unlink(it->queuefn);
fd1de51a
SS
271 }
272 return (-1);
273}
274
de13a881 275int
87bdbc01 276load_queue(struct queue *queue)
fd1de51a 277{
6ddd63e1 278 struct stat sb;
fd1de51a 279 struct qitem *it;
fd1de51a
SS
280 DIR *spooldir;
281 struct dirent *de;
fd1de51a
SS
282 char *queuefn;
283 char *mailfn;
fd1de51a 284
6f5efe4f 285 bzero(queue, sizeof(queue));
fd1de51a
SS
286 LIST_INIT(&queue->queue);
287
a0c4afa6 288 spooldir = opendir(config.spooldir);
fd1de51a
SS
289 if (spooldir == NULL)
290 err(1, "reading queue");
291
292 while ((de = readdir(spooldir)) != NULL) {
fd1de51a 293 queuefn = NULL;
6ddd63e1 294 mailfn = NULL;
fd1de51a 295
0cf8120d 296 /* ignore non-queue files */
fd1de51a
SS
297 if (de->d_name[0] != 'Q')
298 continue;
a0c4afa6 299 if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
fd1de51a 300 goto fail;
a0c4afa6 301 if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
fd1de51a
SS
302 goto fail;
303
0cf8120d
SS
304 /*
305 * Some file systems don't provide a de->d_type, so we have to
306 * do an explicit stat on the queue file.
307 * Move on if it turns out to be something else than a file.
308 */
309 if (stat(queuefn, &sb) != 0)
310 goto skip_item;
311 if (!S_ISREG(sb.st_mode)) {
312 errno = EINVAL;
313 goto skip_item;
314 }
315
6ddd63e1 316 if (stat(mailfn, &sb) != 0)
fd1de51a
SS
317 goto skip_item;
318
6ddd63e1
SS
319 it = readqueuef(queue, queuefn);
320 if (it == NULL)
fd1de51a 321 goto skip_item;
fd1de51a 322
fd1de51a 323 it->mailfn = mailfn;
fd1de51a
SS
324 continue;
325
326skip_item:
de13a881 327 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
fd1de51a
SS
328 if (queuefn != NULL)
329 free(queuefn);
330 if (mailfn != NULL)
94fdb70b 331 free(mailfn);
fd1de51a
SS
332 }
333 closedir(spooldir);
de13a881 334 return (0);
fd1de51a
SS
335
336fail:
de13a881 337 return (-1);
fd1de51a
SS
338}
339
340void
341delqueue(struct qitem *it)
342{
fd1de51a 343 unlink(it->mailfn);
de13a881 344 unlink(it->queuefn);
87bdbc01
SS
345 if (it->queuef != NULL)
346 fclose(it->queuef);
347 if (it->mailf != NULL)
348 fclose(it->mailf);
fd1de51a
SS
349 free(it);
350}
87bdbc01
SS
351
352int
9aec6ad8 353acquirespool(struct qitem *it)
87bdbc01
SS
354{
355 int queuefd;
356
357 if (it->queuef == NULL) {
8e4fa698 358 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
87bdbc01 359 if (queuefd < 0)
de13a881 360 goto fail;
87bdbc01
SS
361 it->queuef = fdopen(queuefd, "r+");
362 if (it->queuef == NULL)
de13a881 363 goto fail;
87bdbc01
SS
364 }
365
366 if (it->mailf == NULL) {
367 it->mailf = fopen(it->mailfn, "r");
368 if (it->mailf == NULL)
de13a881 369 goto fail;
87bdbc01
SS
370 }
371
372 return (0);
de13a881
SS
373
374fail:
9aec6ad8 375 syslog(LOG_INFO, "could not acquire queue file: %m");
de13a881 376 return (-1);
87bdbc01
SS
377}
378
379void
380dropspool(struct queue *queue, struct qitem *keep)
381{
382 struct qitem *it;
383
384 LIST_FOREACH(it, &queue->queue, next) {
385 if (it == keep)
386 continue;
387
388 if (it->queuef != NULL)
389 fclose(it->queuef);
390 if (it->mailf != NULL)
391 fclose(it->mailf);
392 }
393}