dma: beautify queue listing output
[dragonfly.git] / libexec / dma / spool.c
CommitLineData
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
37#include <dirent.h>
38#include <err.h>
39#include <errno.h>
40#include <fcntl.h>
41#include <inttypes.h>
42#include <unistd.h>
405f48ee 43#include <syslog.h>
f4e61a9f
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 *
76ad9740 60 * Queue ids are unique, formed from the inode of the data file
f4e61a9f
SS
61 * and a unique identifier.
62 */
63
64int
1c9e6b7b 65newspoolf(struct queue *queue)
f4e61a9f 66{
f4e61a9f 67 char fn[PATH_MAX+1];
405f48ee 68 struct stat st;
f4e61a9f
SS
69 struct stritem *t;
70 struct qitem *it;
f4e61a9f 71 off_t hdrlen;
9afa363f 72 int fd;
f4e61a9f
SS
73
74 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
75 return (-1);
76
9afa363f
SS
77 fd = mkstemp(fn);
78 if (fd < 0)
f4e61a9f 79 return (-1);
9afa363f
SS
80 if (flock(fd, LOCK_EX) == -1)
81 goto fail;
f4e61a9f
SS
82 queue->tmpf = strdup(fn);
83 if (queue->tmpf == NULL)
84 goto fail;
85
9afa363f
SS
86 /*
87 * Assign queue id
88 */
89 if (fstat(fd, &st) != 0)
f4e61a9f 90 goto fail;
9afa363f 91 if (asprintf(&queue->id, "%"PRIxMAX, st.st_ino) < 0)
f4e61a9f
SS
92 goto fail;
93
9afa363f
SS
94 queue->mailf = fdopen(fd, "r+");
95 if (queue->mailf == NULL)
96 goto fail;
f4e61a9f 97
1c9e6b7b 98 if (fprintf(queue->mailf, "%s\n", queue->sender) < 0)
f4e61a9f 99 goto fail;
9afa363f
SS
100
101 hdrlen = ftello(queue->mailf);
102
f4e61a9f 103 LIST_FOREACH(it, &queue->queue, next) {
f4e61a9f
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
114fail:
9afa363f
SS
115 if (queue->mailf != NULL)
116 fclose(queue->mailf);
117 close(fd);
f4e61a9f
SS
118 unlink(fn);
119 return (-1);
120}
121
122int
1c9e6b7b 123linkspool(struct queue *queue)
f4e61a9f
SS
124{
125 char line[1000]; /* by RFC2822 */
126 struct stat st;
9afa363f 127 size_t error;
f4e61a9f
SS
128 int queuefd;
129 struct qitem *it;
130
9afa363f
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>",
1c9e6b7b 135 username, getuid(), queue->sender);
9afa363f 136
f4e61a9f 137 LIST_FOREACH(it, &queue->queue, next) {
405f48ee 138 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
f4e61a9f
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);
9afa363f 150 if ((ssize_t)error < 0 || error >= sizeof(line))
f4e61a9f 151 goto delfiles;
9afa363f 152
f4e61a9f
SS
153 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
154 if (queuefd == -1)
155 goto delfiles;
9afa363f
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)
f4e61a9f 163 goto delfiles;
f4e61a9f
SS
164
165 if (link(queue->tmpf, it->mailfn) != 0)
166 goto delfiles;
167 }
168
405f48ee
SS
169 LIST_FOREACH(it, &queue->queue, next) {
170 syslog(LOG_INFO, "mail to=<%s> queued as %s",
171 it->addr, it->queueid);
172 }
f4e61a9f
SS
173
174 unlink(queue->tmpf);
175 return (0);
176
177delfiles:
178 LIST_FOREACH(it, &queue->queue, next) {
f4e61a9f 179 unlink(it->mailfn);
1da0a9f2 180 unlink(it->queuefn);
f4e61a9f
SS
181 }
182 return (-1);
183}
184
1da0a9f2 185int
9afa363f 186load_queue(struct queue *queue)
f4e61a9f
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;
f4e61a9f 202
e50076f8 203 bzero(queue, sizeof(queue));
f4e61a9f
SS
204 LIST_INIT(&queue->queue);
205
206 spooldir = opendir(config->spooldir);
207 if (spooldir == NULL)
208 err(1, "reading queue");
209
210 while ((de = readdir(spooldir)) != NULL) {
f4e61a9f
SS
211 sender = NULL;
212 queuef = NULL;
213 mailf = NULL;
214 queueid = NULL;
215 queuefn = NULL;
f4e61a9f
SS
216 LIST_INIT(&itmqueue.queue);
217
218 /* ignore temp files */
219 if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
220 continue;
221 if (de->d_name[0] != 'Q')
222 continue;
223 if (asprintf(&queuefn, "%s/Q%s", config->spooldir, de->d_name + 1) < 0)
224 goto fail;
225 if (asprintf(&mailfn, "%s/M%s", config->spooldir, de->d_name + 1) < 0)
226 goto fail;
227
f4e61a9f
SS
228 mailf = fopen(mailfn, "r");
229 if (mailf == NULL)
230 goto skip_item;
231 if (fgets(line, sizeof(line), mailf) == NULL || line[0] == 0)
232 goto skip_item;
233 line[strlen(line) - 1] = 0; /* chop newline */
234 sender = strdup(line);
235 if (sender == NULL)
236 goto skip_item;
237
238 hdrlen = ftell(mailf);
239
9afa363f 240 queuef = fopen(queuefn, "r");
f4e61a9f
SS
241 if (queuef == NULL)
242 goto skip_item;
243
244 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
245 goto skip_item;
246 line[strlen(line) - 1] = 0;
247 queueid = strdup(line);
248 if (queueid == NULL)
249 goto skip_item;
250 addr = strchr(queueid, ' ');
251 if (addr == NULL)
252 goto skip_item;
253 *addr++ = 0;
254
1c9e6b7b 255 if (add_recp(&itmqueue, addr, 0) != 0)
f4e61a9f 256 goto skip_item;
9afa363f 257
f4e61a9f 258 it = LIST_FIRST(&itmqueue.queue);
1c9e6b7b 259 it->sender = sender;
f4e61a9f
SS
260 it->queueid = queueid;
261 it->queuefn = queuefn;
262 it->mailfn = mailfn;
263 it->hdrlen = hdrlen;
f4e61a9f
SS
264 LIST_INSERT_HEAD(&queue->queue, it, next);
265
9afa363f
SS
266 if (queuef != NULL)
267 fclose(queuef);
268 if (mailf != NULL)
269 fclose(mailf);
f4e61a9f
SS
270 continue;
271
272skip_item:
1da0a9f2 273 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
f4e61a9f
SS
274 if (sender != NULL)
275 free(sender);
276 if (queuefn != NULL)
277 free(queuefn);
278 if (mailfn != NULL)
279 free(queuefn);
280 if (queueid != NULL)
281 free(queueid);
282 if (queuef != NULL)
283 fclose(queuef);
284 if (mailf != NULL)
285 fclose(mailf);
f4e61a9f
SS
286 }
287 closedir(spooldir);
1da0a9f2 288 return (0);
f4e61a9f
SS
289
290fail:
1da0a9f2 291 return (-1);
f4e61a9f
SS
292}
293
294void
295delqueue(struct qitem *it)
296{
f4e61a9f 297 unlink(it->mailfn);
1da0a9f2 298 unlink(it->queuefn);
9afa363f
SS
299 if (it->queuef != NULL)
300 fclose(it->queuef);
301 if (it->mailf != NULL)
302 fclose(it->mailf);
f4e61a9f
SS
303 free(it);
304}
9afa363f
SS
305
306int
307aquirespool(struct qitem *it)
308{
309 int queuefd;
310
311 if (it->queuef == NULL) {
b95bffd0 312 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
9afa363f 313 if (queuefd < 0)
1da0a9f2 314 goto fail;
9afa363f
SS
315 it->queuef = fdopen(queuefd, "r+");
316 if (it->queuef == NULL)
1da0a9f2 317 goto fail;
9afa363f
SS
318 }
319
320 if (it->mailf == NULL) {
321 it->mailf = fopen(it->mailfn, "r");
322 if (it->mailf == NULL)
1da0a9f2 323 goto fail;
9afa363f
SS
324 }
325
326 return (0);
1da0a9f2
SS
327
328fail:
329 syslog(LOG_INFO, "could not aquire queue file: %m");
330 return (-1);
9afa363f
SS
331}
332
333void
334dropspool(struct queue *queue, struct qitem *keep)
335{
336 struct qitem *it;
337
338 LIST_FOREACH(it, &queue->queue, next) {
339 if (it == keep)
340 continue;
341
342 if (it->queuef != NULL)
343 fclose(it->queuef);
344 if (it->mailf != NULL)
345 fclose(it->mailf);
346 }
347}