]>
Commit | Line | Data |
---|---|---|
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 | ||
70 | int | |
057afad5 | 71 | newspoolf(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 | ||
112 | fail: | |
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 |
120 | static int |
121 | writequeuef(struct qitem *it) | |
122 | { | |
123 | int error; | |
124 | int queuefd; | |
125 | ||
17319455 | 126 | queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660); |
6ddd63e1 SS |
127 | if (queuefd == -1) |
128 | return (-1); | |
17319455 SS |
129 | if (fchmod(queuefd, 0660) < 0) |
130 | return (-1); | |
6ddd63e1 SS |
131 | it->queuef = fdopen(queuefd, "w+"); |
132 | if (it->queuef == NULL) | |
133 | return (-1); | |
134 | ||
135 | error = fprintf(it->queuef, | |
136 | "ID: %s\n" | |
137 | "Sender: %s\n" | |
138 | "Recipient: %s\n", | |
139 | it->queueid, | |
140 | it->sender, | |
141 | it->addr); | |
142 | ||
143 | if (error <= 0) | |
144 | return (-1); | |
145 | ||
146 | if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0) | |
147 | return (-1); | |
148 | ||
149 | return (0); | |
150 | } | |
151 | ||
152 | static struct qitem * | |
153 | readqueuef(struct queue *queue, char *queuefn) | |
154 | { | |
155 | char line[1000]; | |
156 | struct queue itmqueue; | |
157 | FILE *queuef = NULL; | |
158 | char *s; | |
159 | char *queueid = NULL, *sender = NULL, *addr = NULL; | |
160 | struct qitem *it = NULL; | |
161 | ||
162 | bzero(&itmqueue, sizeof(itmqueue)); | |
163 | LIST_INIT(&itmqueue.queue); | |
164 | ||
165 | queuef = fopen(queuefn, "r"); | |
166 | if (queuef == NULL) | |
167 | goto out; | |
168 | ||
169 | while (!feof(queuef)) { | |
170 | if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0) | |
171 | break; | |
172 | line[strlen(line) - 1] = 0; /* chop newline */ | |
173 | ||
174 | s = strchr(line, ':'); | |
175 | if (s == NULL) | |
176 | goto malformed; | |
177 | *s = 0; | |
178 | ||
179 | s++; | |
180 | while (isspace(*s)) | |
181 | s++; | |
182 | ||
183 | s = strdup(s); | |
7aa2b3b7 | 184 | if (s == NULL) |
6ddd63e1 SS |
185 | goto malformed; |
186 | ||
187 | if (strcmp(line, "ID") == 0) { | |
188 | queueid = s; | |
189 | } else if (strcmp(line, "Sender") == 0) { | |
190 | sender = s; | |
191 | } else if (strcmp(line, "Recipient") == 0) { | |
192 | addr = s; | |
193 | } else { | |
194 | syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'", | |
195 | line, queuefn); | |
196 | free(s); | |
197 | } | |
198 | } | |
199 | ||
7aa2b3b7 SS |
200 | if (queueid == NULL || sender == NULL || addr == NULL || |
201 | *queueid == 0 || *addr == 0) { | |
6ddd63e1 SS |
202 | malformed: |
203 | errno = EINVAL; | |
204 | syslog(LOG_ERR, "malformed queue file `%s'", queuefn); | |
205 | goto out; | |
206 | } | |
207 | ||
208 | if (add_recp(&itmqueue, addr, 0) != 0) | |
209 | goto out; | |
210 | ||
211 | it = LIST_FIRST(&itmqueue.queue); | |
212 | it->sender = sender; sender = NULL; | |
213 | it->queueid = queueid; queueid = NULL; | |
214 | it->queuefn = queuefn; queuefn = NULL; | |
215 | LIST_INSERT_HEAD(&queue->queue, it, next); | |
216 | ||
217 | out: | |
218 | if (sender != NULL) | |
219 | free(sender); | |
220 | if (queueid != NULL) | |
221 | free(queueid); | |
222 | if (addr != NULL) | |
223 | free(addr); | |
224 | if (queuef != NULL) | |
225 | fclose(queuef); | |
226 | ||
227 | return (it); | |
228 | } | |
229 | ||
fd1de51a | 230 | int |
057afad5 | 231 | linkspool(struct queue *queue) |
fd1de51a | 232 | { |
fd1de51a | 233 | struct stat st; |
fd1de51a SS |
234 | struct qitem *it; |
235 | ||
87bdbc01 SS |
236 | if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0) |
237 | goto delfiles; | |
238 | ||
239 | syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>", | |
057afad5 | 240 | username, getuid(), queue->sender); |
87bdbc01 | 241 | |
fd1de51a | 242 | LIST_FOREACH(it, &queue->queue, next) { |
4d5af2b0 | 243 | if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0) |
fd1de51a | 244 | goto delfiles; |
a0c4afa6 | 245 | if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0) |
fd1de51a | 246 | goto delfiles; |
a0c4afa6 | 247 | if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0) |
fd1de51a SS |
248 | goto delfiles; |
249 | ||
250 | /* Neither file may not exist yet */ | |
251 | if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0) | |
252 | goto delfiles; | |
253 | ||
6ddd63e1 | 254 | if (writequeuef(it) != 0) |
fd1de51a | 255 | goto delfiles; |
fd1de51a SS |
256 | |
257 | if (link(queue->tmpf, it->mailfn) != 0) | |
258 | goto delfiles; | |
259 | } | |
260 | ||
4d5af2b0 SS |
261 | LIST_FOREACH(it, &queue->queue, next) { |
262 | syslog(LOG_INFO, "mail to=<%s> queued as %s", | |
263 | it->addr, it->queueid); | |
264 | } | |
fd1de51a SS |
265 | |
266 | unlink(queue->tmpf); | |
267 | return (0); | |
268 | ||
269 | delfiles: | |
270 | LIST_FOREACH(it, &queue->queue, next) { | |
fd1de51a | 271 | unlink(it->mailfn); |
de13a881 | 272 | unlink(it->queuefn); |
fd1de51a SS |
273 | } |
274 | return (-1); | |
275 | } | |
276 | ||
de13a881 | 277 | int |
87bdbc01 | 278 | load_queue(struct queue *queue) |
fd1de51a | 279 | { |
6ddd63e1 | 280 | struct stat sb; |
fd1de51a | 281 | struct qitem *it; |
fd1de51a SS |
282 | DIR *spooldir; |
283 | struct dirent *de; | |
fd1de51a SS |
284 | char *queuefn; |
285 | char *mailfn; | |
fd1de51a | 286 | |
9f7c8589 | 287 | bzero(queue, sizeof(*queue)); |
fd1de51a SS |
288 | LIST_INIT(&queue->queue); |
289 | ||
a0c4afa6 | 290 | spooldir = opendir(config.spooldir); |
fd1de51a SS |
291 | if (spooldir == NULL) |
292 | err(1, "reading queue"); | |
293 | ||
294 | while ((de = readdir(spooldir)) != NULL) { | |
fd1de51a | 295 | queuefn = NULL; |
6ddd63e1 | 296 | mailfn = NULL; |
fd1de51a | 297 | |
0cf8120d | 298 | /* ignore non-queue files */ |
fd1de51a SS |
299 | if (de->d_name[0] != 'Q') |
300 | continue; | |
a0c4afa6 | 301 | if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0) |
fd1de51a | 302 | goto fail; |
a0c4afa6 | 303 | if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0) |
fd1de51a SS |
304 | goto fail; |
305 | ||
0cf8120d SS |
306 | /* |
307 | * Some file systems don't provide a de->d_type, so we have to | |
308 | * do an explicit stat on the queue file. | |
309 | * Move on if it turns out to be something else than a file. | |
310 | */ | |
311 | if (stat(queuefn, &sb) != 0) | |
312 | goto skip_item; | |
313 | if (!S_ISREG(sb.st_mode)) { | |
314 | errno = EINVAL; | |
315 | goto skip_item; | |
316 | } | |
317 | ||
6ddd63e1 | 318 | if (stat(mailfn, &sb) != 0) |
fd1de51a SS |
319 | goto skip_item; |
320 | ||
6ddd63e1 SS |
321 | it = readqueuef(queue, queuefn); |
322 | if (it == NULL) | |
fd1de51a | 323 | goto skip_item; |
fd1de51a | 324 | |
fd1de51a | 325 | it->mailfn = mailfn; |
fd1de51a SS |
326 | continue; |
327 | ||
328 | skip_item: | |
de13a881 | 329 | syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn); |
fd1de51a SS |
330 | if (queuefn != NULL) |
331 | free(queuefn); | |
332 | if (mailfn != NULL) | |
94fdb70b | 333 | free(mailfn); |
fd1de51a SS |
334 | } |
335 | closedir(spooldir); | |
de13a881 | 336 | return (0); |
fd1de51a SS |
337 | |
338 | fail: | |
de13a881 | 339 | return (-1); |
fd1de51a SS |
340 | } |
341 | ||
342 | void | |
343 | delqueue(struct qitem *it) | |
344 | { | |
fd1de51a | 345 | unlink(it->mailfn); |
de13a881 | 346 | unlink(it->queuefn); |
87bdbc01 SS |
347 | if (it->queuef != NULL) |
348 | fclose(it->queuef); | |
349 | if (it->mailf != NULL) | |
350 | fclose(it->mailf); | |
fd1de51a SS |
351 | free(it); |
352 | } | |
87bdbc01 SS |
353 | |
354 | int | |
9aec6ad8 | 355 | acquirespool(struct qitem *it) |
87bdbc01 SS |
356 | { |
357 | int queuefd; | |
358 | ||
359 | if (it->queuef == NULL) { | |
8e4fa698 | 360 | queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK); |
87bdbc01 | 361 | if (queuefd < 0) |
de13a881 | 362 | goto fail; |
87bdbc01 SS |
363 | it->queuef = fdopen(queuefd, "r+"); |
364 | if (it->queuef == NULL) | |
de13a881 | 365 | goto fail; |
87bdbc01 SS |
366 | } |
367 | ||
368 | if (it->mailf == NULL) { | |
369 | it->mailf = fopen(it->mailfn, "r"); | |
370 | if (it->mailf == NULL) | |
de13a881 | 371 | goto fail; |
87bdbc01 SS |
372 | } |
373 | ||
374 | return (0); | |
de13a881 SS |
375 | |
376 | fail: | |
e33faafe SS |
377 | if (errno == EWOULDBLOCK) |
378 | return (1); | |
9aec6ad8 | 379 | syslog(LOG_INFO, "could not acquire queue file: %m"); |
de13a881 | 380 | return (-1); |
87bdbc01 SS |
381 | } |
382 | ||
383 | void | |
384 | dropspool(struct queue *queue, struct qitem *keep) | |
385 | { | |
386 | struct qitem *it; | |
387 | ||
388 | LIST_FOREACH(it, &queue->queue, next) { | |
389 | if (it == keep) | |
390 | continue; | |
391 | ||
392 | if (it->queuef != NULL) | |
393 | fclose(it->queuef); | |
394 | if (it->mailf != NULL) | |
395 | fclose(it->mailf); | |
396 | } | |
397 | } | |
3686aecc SS |
398 | |
399 | int | |
400 | flushqueue_since(unsigned int period) | |
401 | { | |
402 | struct stat st; | |
403 | struct timeval now; | |
404 | char *flushfn = NULL; | |
405 | ||
406 | if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0) | |
407 | return (0); | |
408 | if (stat(flushfn, &st) < 0) { | |
409 | free(flushfn); | |
410 | return (0); | |
411 | } | |
412 | free(flushfn); | |
413 | flushfn = NULL; | |
414 | if (gettimeofday(&now, 0) != 0) | |
415 | return (0); | |
416 | ||
417 | /* Did the flush file get touched within the last period seconds? */ | |
418 | if (st.st_mtim.tv_sec + period >= now.tv_sec) | |
419 | return (1); | |
420 | else | |
421 | return (0); | |
422 | } | |
423 | ||
424 | int | |
425 | flushqueue_signal(void) | |
426 | { | |
427 | char *flushfn = NULL; | |
428 | int fd; | |
429 | ||
430 | if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0) | |
431 | return (-1); | |
432 | fd = open(flushfn, O_CREAT|O_RDONLY, 0440); | |
433 | if (fd < 0) { | |
434 | syslog(LOG_ERR, "could not open flush file: %m"); | |
435 | free(flushfn); | |
436 | return (-1); | |
437 | } | |
438 | close(fd); | |
439 | if (utimes(flushfn, NULL) < 0) { | |
440 | syslog(LOG_ERR, "could not touch flush file: %m"); | |
441 | free(flushfn); | |
442 | return (-1); | |
443 | } | |
444 | free (flushfn); | |
445 | return (0); | |
446 | } |