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