]>
Commit | Line | Data |
---|---|---|
fd1de51a | 1 | /* |
e458e5f0 | 2 | * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>. |
fd1de51a SS |
3 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. |
4 | * | |
5 | * This code is derived from software contributed to The DragonFly Project | |
e458e5f0 | 6 | * by Simon Schubert <2@0x2c.org>. |
fd1de51a SS |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * 3. Neither the name of The DragonFly Project nor the names of its | |
19 | * contributors may be used to endorse or promote products derived | |
20 | * from this software without specific, prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
fd1de51a SS |
34 | */ |
35 | ||
9b921550 SS |
36 | #include "dfcompat.h" |
37 | ||
38 | #include <sys/file.h> | |
fd1de51a SS |
39 | #include <sys/stat.h> |
40 | ||
6ddd63e1 | 41 | #include <ctype.h> |
fd1de51a SS |
42 | #include <dirent.h> |
43 | #include <err.h> | |
44 | #include <errno.h> | |
45 | #include <fcntl.h> | |
46 | #include <inttypes.h> | |
47 | #include <unistd.h> | |
4d5af2b0 | 48 | #include <syslog.h> |
fd1de51a SS |
49 | |
50 | #include "dma.h" | |
51 | ||
52 | /* | |
53 | * Spool file format: | |
54 | * | |
55 | * 'Q'id files (queue): | |
6ddd63e1 SS |
56 | * Organized like an RFC822 header, field: value. Ignores unknown fields. |
57 | * ID: id | |
58 | * Sender: envelope-from | |
59 | * Recipient: envelope-to | |
fd1de51a SS |
60 | * |
61 | * 'M'id files (data): | |
fd1de51a SS |
62 | * mail data |
63 | * | |
64 | * Each queue file needs to have a corresponding data file. | |
65 | * One data file might be shared by linking it several times. | |
66 | * | |
24a87a55 | 67 | * Queue ids are unique, formed from the inode of the data file |
fd1de51a SS |
68 | * and a unique identifier. |
69 | */ | |
70 | ||
71 | int | |
057afad5 | 72 | newspoolf(struct queue *queue) |
fd1de51a | 73 | { |
fd1de51a | 74 | char fn[PATH_MAX+1]; |
4d5af2b0 | 75 | struct stat st; |
fd1de51a | 76 | struct stritem *t; |
87bdbc01 | 77 | int fd; |
fd1de51a | 78 | |
a0c4afa6 | 79 | if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0) |
fd1de51a SS |
80 | return (-1); |
81 | ||
87bdbc01 SS |
82 | fd = mkstemp(fn); |
83 | if (fd < 0) | |
fd1de51a | 84 | return (-1); |
0bd3531e SS |
85 | /* XXX group rights */ |
86 | if (fchmod(fd, 0660) < 0) | |
87 | goto fail; | |
87bdbc01 SS |
88 | if (flock(fd, LOCK_EX) == -1) |
89 | goto fail; | |
fd1de51a SS |
90 | queue->tmpf = strdup(fn); |
91 | if (queue->tmpf == NULL) | |
92 | goto fail; | |
93 | ||
87bdbc01 SS |
94 | /* |
95 | * Assign queue id | |
96 | */ | |
97 | if (fstat(fd, &st) != 0) | |
fd1de51a | 98 | goto fail; |
2f4f1f06 | 99 | if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0) |
fd1de51a SS |
100 | goto fail; |
101 | ||
87bdbc01 SS |
102 | queue->mailf = fdopen(fd, "r+"); |
103 | if (queue->mailf == NULL) | |
104 | goto fail; | |
fd1de51a | 105 | |
fd1de51a SS |
106 | t = malloc(sizeof(*t)); |
107 | if (t != NULL) { | |
108 | t->str = queue->tmpf; | |
109 | SLIST_INSERT_HEAD(&tmpfs, t, next); | |
110 | } | |
111 | return (0); | |
112 | ||
113 | fail: | |
87bdbc01 SS |
114 | if (queue->mailf != NULL) |
115 | fclose(queue->mailf); | |
116 | close(fd); | |
fd1de51a SS |
117 | unlink(fn); |
118 | return (-1); | |
119 | } | |
120 | ||
6ddd63e1 SS |
121 | static int |
122 | writequeuef(struct qitem *it) | |
123 | { | |
124 | int error; | |
125 | int queuefd; | |
126 | ||
17319455 | 127 | queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660); |
6ddd63e1 SS |
128 | if (queuefd == -1) |
129 | return (-1); | |
17319455 SS |
130 | if (fchmod(queuefd, 0660) < 0) |
131 | return (-1); | |
6ddd63e1 SS |
132 | it->queuef = fdopen(queuefd, "w+"); |
133 | if (it->queuef == NULL) | |
134 | return (-1); | |
135 | ||
136 | error = fprintf(it->queuef, | |
137 | "ID: %s\n" | |
138 | "Sender: %s\n" | |
139 | "Recipient: %s\n", | |
140 | it->queueid, | |
141 | it->sender, | |
142 | it->addr); | |
143 | ||
144 | if (error <= 0) | |
145 | return (-1); | |
146 | ||
147 | if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0) | |
148 | return (-1); | |
149 | ||
150 | return (0); | |
151 | } | |
152 | ||
153 | static struct qitem * | |
154 | readqueuef(struct queue *queue, char *queuefn) | |
155 | { | |
156 | char line[1000]; | |
157 | struct queue itmqueue; | |
158 | FILE *queuef = NULL; | |
159 | char *s; | |
160 | char *queueid = NULL, *sender = NULL, *addr = NULL; | |
161 | struct qitem *it = NULL; | |
162 | ||
163 | bzero(&itmqueue, sizeof(itmqueue)); | |
164 | LIST_INIT(&itmqueue.queue); | |
165 | ||
166 | queuef = fopen(queuefn, "r"); | |
167 | if (queuef == NULL) | |
168 | goto out; | |
169 | ||
170 | while (!feof(queuef)) { | |
171 | if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0) | |
172 | break; | |
173 | line[strlen(line) - 1] = 0; /* chop newline */ | |
174 | ||
175 | s = strchr(line, ':'); | |
176 | if (s == NULL) | |
177 | goto malformed; | |
178 | *s = 0; | |
179 | ||
180 | s++; | |
181 | while (isspace(*s)) | |
182 | s++; | |
183 | ||
184 | s = strdup(s); | |
7aa2b3b7 | 185 | if (s == NULL) |
6ddd63e1 SS |
186 | goto malformed; |
187 | ||
188 | if (strcmp(line, "ID") == 0) { | |
189 | queueid = s; | |
190 | } else if (strcmp(line, "Sender") == 0) { | |
191 | sender = s; | |
192 | } else if (strcmp(line, "Recipient") == 0) { | |
193 | addr = s; | |
194 | } else { | |
195 | syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'", | |
196 | line, queuefn); | |
197 | free(s); | |
198 | } | |
199 | } | |
200 | ||
7aa2b3b7 SS |
201 | if (queueid == NULL || sender == NULL || addr == NULL || |
202 | *queueid == 0 || *addr == 0) { | |
6ddd63e1 SS |
203 | malformed: |
204 | errno = EINVAL; | |
205 | syslog(LOG_ERR, "malformed queue file `%s'", queuefn); | |
206 | goto out; | |
207 | } | |
208 | ||
209 | if (add_recp(&itmqueue, addr, 0) != 0) | |
210 | goto out; | |
211 | ||
212 | it = LIST_FIRST(&itmqueue.queue); | |
213 | it->sender = sender; sender = NULL; | |
214 | it->queueid = queueid; queueid = NULL; | |
215 | it->queuefn = queuefn; queuefn = NULL; | |
216 | LIST_INSERT_HEAD(&queue->queue, it, next); | |
217 | ||
218 | out: | |
219 | if (sender != NULL) | |
220 | free(sender); | |
221 | if (queueid != NULL) | |
222 | free(queueid); | |
223 | if (addr != NULL) | |
224 | free(addr); | |
225 | if (queuef != NULL) | |
226 | fclose(queuef); | |
227 | ||
228 | return (it); | |
229 | } | |
230 | ||
fd1de51a | 231 | int |
057afad5 | 232 | linkspool(struct queue *queue) |
fd1de51a | 233 | { |
fd1de51a | 234 | struct stat st; |
fd1de51a SS |
235 | struct qitem *it; |
236 | ||
87bdbc01 SS |
237 | if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0) |
238 | goto delfiles; | |
239 | ||
240 | syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>", | |
057afad5 | 241 | username, getuid(), queue->sender); |
87bdbc01 | 242 | |
fd1de51a | 243 | LIST_FOREACH(it, &queue->queue, next) { |
4d5af2b0 | 244 | if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0) |
fd1de51a | 245 | goto delfiles; |
a0c4afa6 | 246 | if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0) |
fd1de51a | 247 | goto delfiles; |
a0c4afa6 | 248 | if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0) |
fd1de51a SS |
249 | goto delfiles; |
250 | ||
251 | /* Neither file may not exist yet */ | |
252 | if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0) | |
253 | goto delfiles; | |
254 | ||
6ddd63e1 | 255 | if (writequeuef(it) != 0) |
fd1de51a | 256 | goto delfiles; |
fd1de51a SS |
257 | |
258 | if (link(queue->tmpf, it->mailfn) != 0) | |
259 | goto delfiles; | |
260 | } | |
261 | ||
4d5af2b0 SS |
262 | LIST_FOREACH(it, &queue->queue, next) { |
263 | syslog(LOG_INFO, "mail to=<%s> queued as %s", | |
264 | it->addr, it->queueid); | |
265 | } | |
fd1de51a SS |
266 | |
267 | unlink(queue->tmpf); | |
268 | return (0); | |
269 | ||
270 | delfiles: | |
271 | LIST_FOREACH(it, &queue->queue, next) { | |
fd1de51a | 272 | unlink(it->mailfn); |
de13a881 | 273 | unlink(it->queuefn); |
fd1de51a SS |
274 | } |
275 | return (-1); | |
276 | } | |
277 | ||
de13a881 | 278 | int |
87bdbc01 | 279 | load_queue(struct queue *queue) |
fd1de51a | 280 | { |
6ddd63e1 | 281 | struct stat sb; |
fd1de51a | 282 | struct qitem *it; |
fd1de51a SS |
283 | DIR *spooldir; |
284 | struct dirent *de; | |
fd1de51a SS |
285 | char *queuefn; |
286 | char *mailfn; | |
fd1de51a | 287 | |
9f7c8589 | 288 | bzero(queue, sizeof(*queue)); |
fd1de51a SS |
289 | LIST_INIT(&queue->queue); |
290 | ||
a0c4afa6 | 291 | spooldir = opendir(config.spooldir); |
fd1de51a | 292 | if (spooldir == NULL) |
de196e33 | 293 | err(EX_NOINPUT, "reading queue"); |
fd1de51a SS |
294 | |
295 | while ((de = readdir(spooldir)) != NULL) { | |
fd1de51a | 296 | queuefn = NULL; |
6ddd63e1 | 297 | mailfn = NULL; |
fd1de51a | 298 | |
0cf8120d | 299 | /* ignore non-queue files */ |
fd1de51a SS |
300 | if (de->d_name[0] != 'Q') |
301 | continue; | |
a0c4afa6 | 302 | if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0) |
fd1de51a | 303 | goto fail; |
a0c4afa6 | 304 | if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0) |
fd1de51a SS |
305 | goto fail; |
306 | ||
0cf8120d SS |
307 | /* |
308 | * Some file systems don't provide a de->d_type, so we have to | |
309 | * do an explicit stat on the queue file. | |
310 | * Move on if it turns out to be something else than a file. | |
311 | */ | |
312 | if (stat(queuefn, &sb) != 0) | |
313 | goto skip_item; | |
314 | if (!S_ISREG(sb.st_mode)) { | |
315 | errno = EINVAL; | |
316 | goto skip_item; | |
317 | } | |
318 | ||
6ddd63e1 | 319 | if (stat(mailfn, &sb) != 0) |
fd1de51a SS |
320 | goto skip_item; |
321 | ||
6ddd63e1 SS |
322 | it = readqueuef(queue, queuefn); |
323 | if (it == NULL) | |
fd1de51a | 324 | goto skip_item; |
fd1de51a | 325 | |
fd1de51a | 326 | it->mailfn = mailfn; |
fd1de51a SS |
327 | continue; |
328 | ||
329 | skip_item: | |
de13a881 | 330 | syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn); |
fd1de51a SS |
331 | if (queuefn != NULL) |
332 | free(queuefn); | |
333 | if (mailfn != NULL) | |
94fdb70b | 334 | free(mailfn); |
fd1de51a SS |
335 | } |
336 | closedir(spooldir); | |
de13a881 | 337 | return (0); |
fd1de51a SS |
338 | |
339 | fail: | |
de13a881 | 340 | return (-1); |
fd1de51a SS |
341 | } |
342 | ||
343 | void | |
344 | delqueue(struct qitem *it) | |
345 | { | |
fd1de51a | 346 | unlink(it->mailfn); |
de13a881 | 347 | unlink(it->queuefn); |
87bdbc01 SS |
348 | if (it->queuef != NULL) |
349 | fclose(it->queuef); | |
350 | if (it->mailf != NULL) | |
351 | fclose(it->mailf); | |
fd1de51a SS |
352 | free(it); |
353 | } | |
87bdbc01 SS |
354 | |
355 | int | |
9aec6ad8 | 356 | acquirespool(struct qitem *it) |
87bdbc01 SS |
357 | { |
358 | int queuefd; | |
359 | ||
360 | if (it->queuef == NULL) { | |
8e4fa698 | 361 | queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK); |
87bdbc01 | 362 | if (queuefd < 0) |
de13a881 | 363 | goto fail; |
87bdbc01 SS |
364 | it->queuef = fdopen(queuefd, "r+"); |
365 | if (it->queuef == NULL) | |
de13a881 | 366 | goto fail; |
87bdbc01 SS |
367 | } |
368 | ||
369 | if (it->mailf == NULL) { | |
370 | it->mailf = fopen(it->mailfn, "r"); | |
371 | if (it->mailf == NULL) | |
de13a881 | 372 | goto fail; |
87bdbc01 SS |
373 | } |
374 | ||
375 | return (0); | |
de13a881 SS |
376 | |
377 | fail: | |
e33faafe SS |
378 | if (errno == EWOULDBLOCK) |
379 | return (1); | |
9aec6ad8 | 380 | syslog(LOG_INFO, "could not acquire queue file: %m"); |
de13a881 | 381 | return (-1); |
87bdbc01 SS |
382 | } |
383 | ||
384 | void | |
385 | dropspool(struct queue *queue, struct qitem *keep) | |
386 | { | |
387 | struct qitem *it; | |
388 | ||
389 | LIST_FOREACH(it, &queue->queue, next) { | |
390 | if (it == keep) | |
391 | continue; | |
392 | ||
393 | if (it->queuef != NULL) | |
394 | fclose(it->queuef); | |
395 | if (it->mailf != NULL) | |
396 | fclose(it->mailf); | |
397 | } | |
398 | } | |
3686aecc SS |
399 | |
400 | int | |
401 | flushqueue_since(unsigned int period) | |
402 | { | |
403 | struct stat st; | |
404 | struct timeval now; | |
405 | char *flushfn = NULL; | |
406 | ||
407 | if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0) | |
408 | return (0); | |
409 | if (stat(flushfn, &st) < 0) { | |
410 | free(flushfn); | |
411 | return (0); | |
412 | } | |
413 | free(flushfn); | |
414 | flushfn = NULL; | |
415 | if (gettimeofday(&now, 0) != 0) | |
416 | return (0); | |
417 | ||
418 | /* Did the flush file get touched within the last period seconds? */ | |
419 | if (st.st_mtim.tv_sec + period >= now.tv_sec) | |
420 | return (1); | |
421 | else | |
422 | return (0); | |
423 | } | |
424 | ||
425 | int | |
426 | flushqueue_signal(void) | |
427 | { | |
428 | char *flushfn = NULL; | |
429 | int fd; | |
430 | ||
431 | if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0) | |
432 | return (-1); | |
3f9fdc0f SS |
433 | fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660); |
434 | free(flushfn); | |
3686aecc SS |
435 | if (fd < 0) { |
436 | syslog(LOG_ERR, "could not open flush file: %m"); | |
3686aecc SS |
437 | return (-1); |
438 | } | |
439 | close(fd); | |
3686aecc SS |
440 | return (0); |
441 | } |