dma: create spool files mode 660
[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
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
67int
1c9e6b7b 68newspoolf(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
109fail:
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
117static int
118writequeuef(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
147static struct qitem *
148readqueuef(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);
179 if (s == NULL || s[0] == 0)
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
195 if (queueid == NULL || sender == NULL || addr == NULL) {
196malformed:
197 errno = EINVAL;
198 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
199 goto out;
200 }
201
202 if (add_recp(&itmqueue, addr, 0) != 0)
203 goto out;
204
205 it = LIST_FIRST(&itmqueue.queue);
206 it->sender = sender; sender = NULL;
207 it->queueid = queueid; queueid = NULL;
208 it->queuefn = queuefn; queuefn = NULL;
209 LIST_INSERT_HEAD(&queue->queue, it, next);
210
211out:
212 if (sender != NULL)
213 free(sender);
214 if (queueid != NULL)
215 free(queueid);
216 if (addr != NULL)
217 free(addr);
218 if (queuef != NULL)
219 fclose(queuef);
220
221 return (it);
222}
223
f4e61a9f 224int
1c9e6b7b 225linkspool(struct queue *queue)
f4e61a9f 226{
f4e61a9f 227 struct stat st;
f4e61a9f
SS
228 struct qitem *it;
229
9afa363f
SS
230 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
231 goto delfiles;
232
233 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
1c9e6b7b 234 username, getuid(), queue->sender);
9afa363f 235
f4e61a9f 236 LIST_FOREACH(it, &queue->queue, next) {
405f48ee 237 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
f4e61a9f
SS
238 goto delfiles;
239 if (asprintf(&it->queuefn, "%s/Q%s", config->spooldir, it->queueid) <= 0)
240 goto delfiles;
241 if (asprintf(&it->mailfn, "%s/M%s", config->spooldir, it->queueid) <= 0)
242 goto delfiles;
243
244 /* Neither file may not exist yet */
245 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
246 goto delfiles;
247
ebffba26 248 if (writequeuef(it) != 0)
f4e61a9f 249 goto delfiles;
f4e61a9f
SS
250
251 if (link(queue->tmpf, it->mailfn) != 0)
252 goto delfiles;
253 }
254
405f48ee
SS
255 LIST_FOREACH(it, &queue->queue, next) {
256 syslog(LOG_INFO, "mail to=<%s> queued as %s",
257 it->addr, it->queueid);
258 }
f4e61a9f
SS
259
260 unlink(queue->tmpf);
261 return (0);
262
263delfiles:
264 LIST_FOREACH(it, &queue->queue, next) {
f4e61a9f 265 unlink(it->mailfn);
1da0a9f2 266 unlink(it->queuefn);
f4e61a9f
SS
267 }
268 return (-1);
269}
270
1da0a9f2 271int
9afa363f 272load_queue(struct queue *queue)
f4e61a9f 273{
ebffba26 274 struct stat sb;
f4e61a9f 275 struct qitem *it;
f4e61a9f
SS
276 DIR *spooldir;
277 struct dirent *de;
f4e61a9f
SS
278 char *queuefn;
279 char *mailfn;
f4e61a9f 280
e50076f8 281 bzero(queue, sizeof(queue));
f4e61a9f
SS
282 LIST_INIT(&queue->queue);
283
284 spooldir = opendir(config->spooldir);
285 if (spooldir == NULL)
286 err(1, "reading queue");
287
288 while ((de = readdir(spooldir)) != NULL) {
f4e61a9f 289 queuefn = NULL;
ebffba26 290 mailfn = NULL;
f4e61a9f
SS
291
292 /* ignore temp files */
293 if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
294 continue;
295 if (de->d_name[0] != 'Q')
296 continue;
297 if (asprintf(&queuefn, "%s/Q%s", config->spooldir, de->d_name + 1) < 0)
298 goto fail;
299 if (asprintf(&mailfn, "%s/M%s", config->spooldir, de->d_name + 1) < 0)
300 goto fail;
301
ebffba26 302 if (stat(mailfn, &sb) != 0)
f4e61a9f
SS
303 goto skip_item;
304
ebffba26
SS
305 it = readqueuef(queue, queuefn);
306 if (it == NULL)
f4e61a9f 307 goto skip_item;
f4e61a9f 308
f4e61a9f 309 it->mailfn = mailfn;
f4e61a9f
SS
310 continue;
311
312skip_item:
1da0a9f2 313 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
f4e61a9f
SS
314 if (queuefn != NULL)
315 free(queuefn);
316 if (mailfn != NULL)
317 free(queuefn);
f4e61a9f
SS
318 }
319 closedir(spooldir);
1da0a9f2 320 return (0);
f4e61a9f
SS
321
322fail:
1da0a9f2 323 return (-1);
f4e61a9f
SS
324}
325
326void
327delqueue(struct qitem *it)
328{
f4e61a9f 329 unlink(it->mailfn);
1da0a9f2 330 unlink(it->queuefn);
9afa363f
SS
331 if (it->queuef != NULL)
332 fclose(it->queuef);
333 if (it->mailf != NULL)
334 fclose(it->mailf);
f4e61a9f
SS
335 free(it);
336}
9afa363f
SS
337
338int
24c80b2b 339acquirespool(struct qitem *it)
9afa363f
SS
340{
341 int queuefd;
342
343 if (it->queuef == NULL) {
b95bffd0 344 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
9afa363f 345 if (queuefd < 0)
1da0a9f2 346 goto fail;
9afa363f
SS
347 it->queuef = fdopen(queuefd, "r+");
348 if (it->queuef == NULL)
1da0a9f2 349 goto fail;
9afa363f
SS
350 }
351
352 if (it->mailf == NULL) {
353 it->mailf = fopen(it->mailfn, "r");
354 if (it->mailf == NULL)
1da0a9f2 355 goto fail;
9afa363f
SS
356 }
357
358 return (0);
1da0a9f2
SS
359
360fail:
24c80b2b 361 syslog(LOG_INFO, "could not acquire queue file: %m");
1da0a9f2 362 return (-1);
9afa363f
SS
363}
364
365void
366dropspool(struct queue *queue, struct qitem *keep)
367{
368 struct qitem *it;
369
370 LIST_FOREACH(it, &queue->queue, next) {
371 if (it == keep)
372 continue;
373
374 if (it->queuef != NULL)
375 fclose(it->queuef);
376 if (it->mailf != NULL)
377 fclose(it->mailf);
378 }
379}