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