]> git.ipfire.org Git - people/ms/dma.git/blob - spool.c
fix wrong argument to free()
[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
35 #include "dfcompat.h"
36
37 #include <sys/file.h>
38 #include <sys/stat.h>
39
40 #include <ctype.h>
41 #include <dirent.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <inttypes.h>
46 #include <unistd.h>
47 #include <syslog.h>
48
49 #include "dma.h"
50
51 /*
52 * Spool file format:
53 *
54 * 'Q'id files (queue):
55 * Organized like an RFC822 header, field: value. Ignores unknown fields.
56 * ID: id
57 * Sender: envelope-from
58 * Recipient: envelope-to
59 *
60 * 'M'id files (data):
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 *
66 * Queue ids are unique, formed from the inode of the data file
67 * and a unique identifier.
68 */
69
70 int
71 newspoolf(struct queue *queue)
72 {
73 char fn[PATH_MAX+1];
74 struct stat st;
75 struct stritem *t;
76 int fd;
77
78 if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
79 return (-1);
80
81 fd = mkstemp(fn);
82 if (fd < 0)
83 return (-1);
84 /* XXX group rights */
85 if (fchmod(fd, 0660) < 0)
86 goto fail;
87 if (flock(fd, LOCK_EX) == -1)
88 goto fail;
89 queue->tmpf = strdup(fn);
90 if (queue->tmpf == NULL)
91 goto fail;
92
93 /*
94 * Assign queue id
95 */
96 if (fstat(fd, &st) != 0)
97 goto fail;
98 if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
99 goto fail;
100
101 queue->mailf = fdopen(fd, "r+");
102 if (queue->mailf == NULL)
103 goto fail;
104
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
112 fail:
113 if (queue->mailf != NULL)
114 fclose(queue->mailf);
115 close(fd);
116 unlink(fn);
117 return (-1);
118 }
119
120 static int
121 writequeuef(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
150 static struct qitem *
151 readqueuef(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);
182 if (s == NULL)
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
198 if (queueid == NULL || sender == NULL || addr == NULL ||
199 *queueid == 0 || *addr == 0) {
200 malformed:
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
215 out:
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
228 int
229 linkspool(struct queue *queue)
230 {
231 struct stat st;
232 struct qitem *it;
233
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>",
238 username, getuid(), queue->sender);
239
240 LIST_FOREACH(it, &queue->queue, next) {
241 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
242 goto delfiles;
243 if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
244 goto delfiles;
245 if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
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
252 if (writequeuef(it) != 0)
253 goto delfiles;
254
255 if (link(queue->tmpf, it->mailfn) != 0)
256 goto delfiles;
257 }
258
259 LIST_FOREACH(it, &queue->queue, next) {
260 syslog(LOG_INFO, "mail to=<%s> queued as %s",
261 it->addr, it->queueid);
262 }
263
264 unlink(queue->tmpf);
265 return (0);
266
267 delfiles:
268 LIST_FOREACH(it, &queue->queue, next) {
269 unlink(it->mailfn);
270 unlink(it->queuefn);
271 }
272 return (-1);
273 }
274
275 int
276 load_queue(struct queue *queue)
277 {
278 struct stat sb;
279 struct qitem *it;
280 DIR *spooldir;
281 struct dirent *de;
282 char *queuefn;
283 char *mailfn;
284
285 bzero(queue, sizeof(queue));
286 LIST_INIT(&queue->queue);
287
288 spooldir = opendir(config.spooldir);
289 if (spooldir == NULL)
290 err(1, "reading queue");
291
292 while ((de = readdir(spooldir)) != NULL) {
293 queuefn = NULL;
294 mailfn = NULL;
295
296 /* ignore non-queue files */
297 if (de->d_name[0] != 'Q')
298 continue;
299 if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
300 goto fail;
301 if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
302 goto fail;
303
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
316 if (stat(mailfn, &sb) != 0)
317 goto skip_item;
318
319 it = readqueuef(queue, queuefn);
320 if (it == NULL)
321 goto skip_item;
322
323 it->mailfn = mailfn;
324 continue;
325
326 skip_item:
327 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
328 if (queuefn != NULL)
329 free(queuefn);
330 if (mailfn != NULL)
331 free(mailfn);
332 }
333 closedir(spooldir);
334 return (0);
335
336 fail:
337 return (-1);
338 }
339
340 void
341 delqueue(struct qitem *it)
342 {
343 unlink(it->mailfn);
344 unlink(it->queuefn);
345 if (it->queuef != NULL)
346 fclose(it->queuef);
347 if (it->mailf != NULL)
348 fclose(it->mailf);
349 free(it);
350 }
351
352 int
353 acquirespool(struct qitem *it)
354 {
355 int queuefd;
356
357 if (it->queuef == NULL) {
358 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
359 if (queuefd < 0)
360 goto fail;
361 it->queuef = fdopen(queuefd, "r+");
362 if (it->queuef == NULL)
363 goto fail;
364 }
365
366 if (it->mailf == NULL) {
367 it->mailf = fopen(it->mailfn, "r");
368 if (it->mailf == NULL)
369 goto fail;
370 }
371
372 return (0);
373
374 fail:
375 syslog(LOG_INFO, "could not acquire queue file: %m");
376 return (-1);
377 }
378
379 void
380 dropspool(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 }