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