Commit | Line | Data |
---|---|---|
f4e61a9f | 1 | /* |
37d59876 | 2 | * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>. |
f4e61a9f SS |
3 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. |
4 | * | |
5 | * This code is derived from software contributed to The DragonFly Project | |
37d59876 | 6 | * by Simon Schubert <2@0x2c.org>. |
f4e61a9f 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. | |
f4e61a9f SS |
34 | */ |
35 | ||
c8b07ee5 SW |
36 | #include "dfcompat.h" |
37 | ||
38 | #include <sys/file.h> | |
f4e61a9f | 39 | #include <sys/stat.h> |
92fe556d | 40 | #include <sys/time.h> |
f4e61a9f | 41 | |
ebffba26 | 42 | #include <ctype.h> |
f4e61a9f SS |
43 | #include <dirent.h> |
44 | #include <err.h> | |
45 | #include <errno.h> | |
46 | #include <fcntl.h> | |
47 | #include <inttypes.h> | |
48 | #include <unistd.h> | |
92fe556d DF |
49 | #include <strings.h> |
50 | #include <string.h> | |
405f48ee | 51 | #include <syslog.h> |
f4e61a9f SS |
52 | |
53 | #include "dma.h" | |
54 | ||
55 | /* | |
56 | * Spool file format: | |
57 | * | |
58 | * 'Q'id files (queue): | |
ebffba26 SS |
59 | * Organized like an RFC822 header, field: value. Ignores unknown fields. |
60 | * ID: id | |
61 | * Sender: envelope-from | |
62 | * Recipient: envelope-to | |
f4e61a9f SS |
63 | * |
64 | * 'M'id files (data): | |
f4e61a9f SS |
65 | * mail data |
66 | * | |
67 | * Each queue file needs to have a corresponding data file. | |
68 | * One data file might be shared by linking it several times. | |
69 | * | |
76ad9740 | 70 | * Queue ids are unique, formed from the inode of the data file |
f4e61a9f SS |
71 | * and a unique identifier. |
72 | */ | |
73 | ||
74 | int | |
1c9e6b7b | 75 | newspoolf(struct queue *queue) |
f4e61a9f | 76 | { |
f4e61a9f | 77 | char fn[PATH_MAX+1]; |
405f48ee | 78 | struct stat st; |
f4e61a9f | 79 | struct stritem *t; |
9afa363f | 80 | int fd; |
f4e61a9f | 81 | |
ca259d14 | 82 | if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0) |
f4e61a9f SS |
83 | return (-1); |
84 | ||
9afa363f SS |
85 | fd = mkstemp(fn); |
86 | if (fd < 0) | |
f4e61a9f | 87 | return (-1); |
6e30778b SS |
88 | /* XXX group rights */ |
89 | if (fchmod(fd, 0660) < 0) | |
90 | goto fail; | |
9afa363f SS |
91 | if (flock(fd, LOCK_EX) == -1) |
92 | goto fail; | |
f4e61a9f SS |
93 | queue->tmpf = strdup(fn); |
94 | if (queue->tmpf == NULL) | |
95 | goto fail; | |
96 | ||
9afa363f SS |
97 | /* |
98 | * Assign queue id | |
99 | */ | |
100 | if (fstat(fd, &st) != 0) | |
f4e61a9f | 101 | goto fail; |
a0a50d0b | 102 | if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0) |
f4e61a9f SS |
103 | goto fail; |
104 | ||
9afa363f SS |
105 | queue->mailf = fdopen(fd, "r+"); |
106 | if (queue->mailf == NULL) | |
107 | goto fail; | |
f4e61a9f | 108 | |
f4e61a9f SS |
109 | t = malloc(sizeof(*t)); |
110 | if (t != NULL) { | |
111 | t->str = queue->tmpf; | |
112 | SLIST_INSERT_HEAD(&tmpfs, t, next); | |
113 | } | |
114 | return (0); | |
115 | ||
116 | fail: | |
9afa363f SS |
117 | if (queue->mailf != NULL) |
118 | fclose(queue->mailf); | |
119 | close(fd); | |
f4e61a9f SS |
120 | unlink(fn); |
121 | return (-1); | |
122 | } | |
123 | ||
ebffba26 SS |
124 | static int |
125 | writequeuef(struct qitem *it) | |
126 | { | |
127 | int error; | |
128 | int queuefd; | |
129 | ||
c8b07ee5 | 130 | queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660); |
ebffba26 SS |
131 | if (queuefd == -1) |
132 | return (-1); | |
c8b07ee5 SW |
133 | if (fchmod(queuefd, 0660) < 0) |
134 | return (-1); | |
ebffba26 SS |
135 | it->queuef = fdopen(queuefd, "w+"); |
136 | if (it->queuef == NULL) | |
137 | return (-1); | |
138 | ||
139 | error = fprintf(it->queuef, | |
140 | "ID: %s\n" | |
141 | "Sender: %s\n" | |
142 | "Recipient: %s\n", | |
143 | it->queueid, | |
144 | it->sender, | |
145 | it->addr); | |
146 | ||
147 | if (error <= 0) | |
148 | return (-1); | |
149 | ||
150 | if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0) | |
151 | return (-1); | |
152 | ||
153 | return (0); | |
154 | } | |
155 | ||
156 | static struct qitem * | |
157 | readqueuef(struct queue *queue, char *queuefn) | |
158 | { | |
159 | char line[1000]; | |
160 | struct queue itmqueue; | |
161 | FILE *queuef = NULL; | |
162 | char *s; | |
163 | char *queueid = NULL, *sender = NULL, *addr = NULL; | |
164 | struct qitem *it = NULL; | |
165 | ||
166 | bzero(&itmqueue, sizeof(itmqueue)); | |
167 | LIST_INIT(&itmqueue.queue); | |
168 | ||
169 | queuef = fopen(queuefn, "r"); | |
170 | if (queuef == NULL) | |
171 | goto out; | |
172 | ||
173 | while (!feof(queuef)) { | |
174 | if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0) | |
175 | break; | |
176 | line[strlen(line) - 1] = 0; /* chop newline */ | |
177 | ||
178 | s = strchr(line, ':'); | |
179 | if (s == NULL) | |
180 | goto malformed; | |
181 | *s = 0; | |
182 | ||
183 | s++; | |
184 | while (isspace(*s)) | |
185 | s++; | |
186 | ||
187 | s = strdup(s); | |
4add537e | 188 | if (s == NULL) |
ebffba26 SS |
189 | goto malformed; |
190 | ||
191 | if (strcmp(line, "ID") == 0) { | |
192 | queueid = s; | |
193 | } else if (strcmp(line, "Sender") == 0) { | |
194 | sender = s; | |
195 | } else if (strcmp(line, "Recipient") == 0) { | |
196 | addr = s; | |
197 | } else { | |
198 | syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'", | |
199 | line, queuefn); | |
200 | free(s); | |
201 | } | |
202 | } | |
203 | ||
4add537e SS |
204 | if (queueid == NULL || sender == NULL || addr == NULL || |
205 | *queueid == 0 || *addr == 0) { | |
ebffba26 SS |
206 | malformed: |
207 | errno = EINVAL; | |
208 | syslog(LOG_ERR, "malformed queue file `%s'", queuefn); | |
209 | goto out; | |
210 | } | |
211 | ||
212 | if (add_recp(&itmqueue, addr, 0) != 0) | |
213 | goto out; | |
214 | ||
215 | it = LIST_FIRST(&itmqueue.queue); | |
216 | it->sender = sender; sender = NULL; | |
217 | it->queueid = queueid; queueid = NULL; | |
218 | it->queuefn = queuefn; queuefn = NULL; | |
219 | LIST_INSERT_HEAD(&queue->queue, it, next); | |
220 | ||
221 | out: | |
222 | if (sender != NULL) | |
223 | free(sender); | |
224 | if (queueid != NULL) | |
225 | free(queueid); | |
226 | if (addr != NULL) | |
227 | free(addr); | |
228 | if (queuef != NULL) | |
229 | fclose(queuef); | |
230 | ||
231 | return (it); | |
232 | } | |
233 | ||
f4e61a9f | 234 | int |
1c9e6b7b | 235 | linkspool(struct queue *queue) |
f4e61a9f | 236 | { |
f4e61a9f | 237 | struct stat st; |
f4e61a9f SS |
238 | struct qitem *it; |
239 | ||
9afa363f SS |
240 | if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0) |
241 | goto delfiles; | |
242 | ||
243 | syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>", | |
1c9e6b7b | 244 | username, getuid(), queue->sender); |
9afa363f | 245 | |
f4e61a9f | 246 | LIST_FOREACH(it, &queue->queue, next) { |
405f48ee | 247 | if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0) |
f4e61a9f | 248 | goto delfiles; |
ca259d14 | 249 | if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0) |
f4e61a9f | 250 | goto delfiles; |
ca259d14 | 251 | if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0) |
f4e61a9f SS |
252 | goto delfiles; |
253 | ||
254 | /* Neither file may not exist yet */ | |
255 | if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0) | |
256 | goto delfiles; | |
257 | ||
ebffba26 | 258 | if (writequeuef(it) != 0) |
f4e61a9f | 259 | goto delfiles; |
f4e61a9f SS |
260 | |
261 | if (link(queue->tmpf, it->mailfn) != 0) | |
262 | goto delfiles; | |
263 | } | |
264 | ||
405f48ee SS |
265 | LIST_FOREACH(it, &queue->queue, next) { |
266 | syslog(LOG_INFO, "mail to=<%s> queued as %s", | |
267 | it->addr, it->queueid); | |
268 | } | |
f4e61a9f SS |
269 | |
270 | unlink(queue->tmpf); | |
271 | return (0); | |
272 | ||
273 | delfiles: | |
274 | LIST_FOREACH(it, &queue->queue, next) { | |
f4e61a9f | 275 | unlink(it->mailfn); |
1da0a9f2 | 276 | unlink(it->queuefn); |
f4e61a9f SS |
277 | } |
278 | return (-1); | |
279 | } | |
280 | ||
1da0a9f2 | 281 | int |
9afa363f | 282 | load_queue(struct queue *queue) |
f4e61a9f | 283 | { |
ebffba26 | 284 | struct stat sb; |
f4e61a9f | 285 | struct qitem *it; |
f4e61a9f SS |
286 | DIR *spooldir; |
287 | struct dirent *de; | |
f4e61a9f SS |
288 | char *queuefn; |
289 | char *mailfn; | |
f4e61a9f | 290 | |
8075c3b8 | 291 | bzero(queue, sizeof(*queue)); |
f4e61a9f SS |
292 | LIST_INIT(&queue->queue); |
293 | ||
ca259d14 | 294 | spooldir = opendir(config.spooldir); |
f4e61a9f | 295 | if (spooldir == NULL) |
92fe556d | 296 | err(EX_NOINPUT, "reading queue"); |
f4e61a9f SS |
297 | |
298 | while ((de = readdir(spooldir)) != NULL) { | |
f4e61a9f | 299 | queuefn = NULL; |
ebffba26 | 300 | mailfn = NULL; |
f4e61a9f | 301 | |
c8b07ee5 | 302 | /* ignore non-queue files */ |
f4e61a9f SS |
303 | if (de->d_name[0] != 'Q') |
304 | continue; | |
ca259d14 | 305 | if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0) |
f4e61a9f | 306 | goto fail; |
ca259d14 | 307 | if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0) |
f4e61a9f SS |
308 | goto fail; |
309 | ||
c8b07ee5 SW |
310 | /* |
311 | * Some file systems don't provide a de->d_type, so we have to | |
312 | * do an explicit stat on the queue file. | |
313 | * Move on if it turns out to be something else than a file. | |
314 | */ | |
315 | if (stat(queuefn, &sb) != 0) | |
316 | goto skip_item; | |
317 | if (!S_ISREG(sb.st_mode)) { | |
318 | errno = EINVAL; | |
319 | goto skip_item; | |
320 | } | |
321 | ||
ebffba26 | 322 | if (stat(mailfn, &sb) != 0) |
f4e61a9f SS |
323 | goto skip_item; |
324 | ||
ebffba26 SS |
325 | it = readqueuef(queue, queuefn); |
326 | if (it == NULL) | |
f4e61a9f | 327 | goto skip_item; |
f4e61a9f | 328 | |
f4e61a9f | 329 | it->mailfn = mailfn; |
f4e61a9f SS |
330 | continue; |
331 | ||
332 | skip_item: | |
1da0a9f2 | 333 | syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn); |
f4e61a9f SS |
334 | if (queuefn != NULL) |
335 | free(queuefn); | |
336 | if (mailfn != NULL) | |
d557d463 | 337 | free(mailfn); |
f4e61a9f SS |
338 | } |
339 | closedir(spooldir); | |
1da0a9f2 | 340 | return (0); |
f4e61a9f SS |
341 | |
342 | fail: | |
1da0a9f2 | 343 | return (-1); |
f4e61a9f SS |
344 | } |
345 | ||
346 | void | |
347 | delqueue(struct qitem *it) | |
348 | { | |
f4e61a9f | 349 | unlink(it->mailfn); |
1da0a9f2 | 350 | unlink(it->queuefn); |
9afa363f SS |
351 | if (it->queuef != NULL) |
352 | fclose(it->queuef); | |
353 | if (it->mailf != NULL) | |
354 | fclose(it->mailf); | |
f4e61a9f SS |
355 | free(it); |
356 | } | |
9afa363f SS |
357 | |
358 | int | |
24c80b2b | 359 | acquirespool(struct qitem *it) |
9afa363f SS |
360 | { |
361 | int queuefd; | |
362 | ||
363 | if (it->queuef == NULL) { | |
b95bffd0 | 364 | queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK); |
9afa363f | 365 | if (queuefd < 0) |
1da0a9f2 | 366 | goto fail; |
9afa363f SS |
367 | it->queuef = fdopen(queuefd, "r+"); |
368 | if (it->queuef == NULL) | |
1da0a9f2 | 369 | goto fail; |
9afa363f SS |
370 | } |
371 | ||
372 | if (it->mailf == NULL) { | |
373 | it->mailf = fopen(it->mailfn, "r"); | |
374 | if (it->mailf == NULL) | |
1da0a9f2 | 375 | goto fail; |
9afa363f SS |
376 | } |
377 | ||
378 | return (0); | |
1da0a9f2 SS |
379 | |
380 | fail: | |
14dfb991 JG |
381 | if (errno == EWOULDBLOCK) |
382 | return (1); | |
24c80b2b | 383 | syslog(LOG_INFO, "could not acquire queue file: %m"); |
1da0a9f2 | 384 | return (-1); |
9afa363f SS |
385 | } |
386 | ||
387 | void | |
388 | dropspool(struct queue *queue, struct qitem *keep) | |
389 | { | |
390 | struct qitem *it; | |
391 | ||
392 | LIST_FOREACH(it, &queue->queue, next) { | |
393 | if (it == keep) | |
394 | continue; | |
395 | ||
396 | if (it->queuef != NULL) | |
397 | fclose(it->queuef); | |
398 | if (it->mailf != NULL) | |
399 | fclose(it->mailf); | |
400 | } | |
401 | } | |
14dfb991 JG |
402 | |
403 | int | |
404 | flushqueue_since(unsigned int period) | |
405 | { | |
406 | struct stat st; | |
407 | struct timeval now; | |
408 | char *flushfn = NULL; | |
409 | ||
410 | if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0) | |
411 | return (0); | |
412 | if (stat(flushfn, &st) < 0) { | |
413 | free(flushfn); | |
414 | return (0); | |
415 | } | |
416 | free(flushfn); | |
417 | flushfn = NULL; | |
418 | if (gettimeofday(&now, 0) != 0) | |
419 | return (0); | |
420 | ||
421 | /* Did the flush file get touched within the last period seconds? */ | |
efcc709c | 422 | if (st.st_mtim.tv_sec + period >= now.tv_sec) |
14dfb991 JG |
423 | return (1); |
424 | else | |
425 | return (0); | |
426 | } | |
427 | ||
428 | int | |
429 | flushqueue_signal(void) | |
430 | { | |
431 | char *flushfn = NULL; | |
432 | int fd; | |
433 | ||
434 | if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0) | |
435 | return (-1); | |
436 | fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660); | |
437 | free(flushfn); | |
438 | if (fd < 0) { | |
439 | syslog(LOG_ERR, "could not open flush file: %m"); | |
440 | return (-1); | |
441 | } | |
442 | close(fd); | |
443 | return (0); | |
444 | } |