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