Revert "kprintf.9: Remove unneeded header."
[dragonfly.git] / libexec / dma / spool.c
CommitLineData
f4e61a9f 1/*
37d59876 2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
f4e61a9f
SS
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 *
5 * This code is derived from software contributed to The DragonFly Project
37d59876 6 * by Simon Schubert <2@0x2c.org>.
f4e61a9f
SS
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
f4e61a9f
SS
34 */
35
c8b07ee5
SW
36#include "dfcompat.h"
37
38#include <sys/file.h>
f4e61a9f
SS
39#include <sys/stat.h>
40
ebffba26 41#include <ctype.h>
f4e61a9f
SS
42#include <dirent.h>
43#include <err.h>
44#include <errno.h>
45#include <fcntl.h>
46#include <inttypes.h>
47#include <unistd.h>
405f48ee 48#include <syslog.h>
f4e61a9f
SS
49
50#include "dma.h"
51
52/*
53 * Spool file format:
54 *
55 * 'Q'id files (queue):
ebffba26
SS
56 * Organized like an RFC822 header, field: value. Ignores unknown fields.
57 * ID: id
58 * Sender: envelope-from
59 * Recipient: envelope-to
f4e61a9f
SS
60 *
61 * 'M'id files (data):
f4e61a9f
SS
62 * mail data
63 *
64 * Each queue file needs to have a corresponding data file.
65 * One data file might be shared by linking it several times.
66 *
76ad9740 67 * Queue ids are unique, formed from the inode of the data file
f4e61a9f
SS
68 * and a unique identifier.
69 */
70
71int
1c9e6b7b 72newspoolf(struct queue *queue)
f4e61a9f 73{
f4e61a9f 74 char fn[PATH_MAX+1];
405f48ee 75 struct stat st;
f4e61a9f 76 struct stritem *t;
9afa363f 77 int fd;
f4e61a9f 78
ca259d14 79 if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
f4e61a9f
SS
80 return (-1);
81
9afa363f
SS
82 fd = mkstemp(fn);
83 if (fd < 0)
f4e61a9f 84 return (-1);
6e30778b
SS
85 /* XXX group rights */
86 if (fchmod(fd, 0660) < 0)
87 goto fail;
9afa363f
SS
88 if (flock(fd, LOCK_EX) == -1)
89 goto fail;
f4e61a9f
SS
90 queue->tmpf = strdup(fn);
91 if (queue->tmpf == NULL)
92 goto fail;
93
9afa363f
SS
94 /*
95 * Assign queue id
96 */
97 if (fstat(fd, &st) != 0)
f4e61a9f 98 goto fail;
a0a50d0b 99 if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
f4e61a9f
SS
100 goto fail;
101
9afa363f
SS
102 queue->mailf = fdopen(fd, "r+");
103 if (queue->mailf == NULL)
104 goto fail;
f4e61a9f 105
f4e61a9f
SS
106 t = malloc(sizeof(*t));
107 if (t != NULL) {
108 t->str = queue->tmpf;
109 SLIST_INSERT_HEAD(&tmpfs, t, next);
110 }
111 return (0);
112
113fail:
9afa363f
SS
114 if (queue->mailf != NULL)
115 fclose(queue->mailf);
116 close(fd);
f4e61a9f
SS
117 unlink(fn);
118 return (-1);
119}
120
ebffba26
SS
121static int
122writequeuef(struct qitem *it)
123{
124 int error;
125 int queuefd;
126
c8b07ee5 127 queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660);
ebffba26
SS
128 if (queuefd == -1)
129 return (-1);
c8b07ee5
SW
130 if (fchmod(queuefd, 0660) < 0)
131 return (-1);
ebffba26
SS
132 it->queuef = fdopen(queuefd, "w+");
133 if (it->queuef == NULL)
134 return (-1);
135
136 error = fprintf(it->queuef,
137 "ID: %s\n"
138 "Sender: %s\n"
139 "Recipient: %s\n",
140 it->queueid,
141 it->sender,
142 it->addr);
143
144 if (error <= 0)
145 return (-1);
146
147 if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
148 return (-1);
149
150 return (0);
151}
152
153static struct qitem *
154readqueuef(struct queue *queue, char *queuefn)
155{
156 char line[1000];
157 struct queue itmqueue;
158 FILE *queuef = NULL;
159 char *s;
160 char *queueid = NULL, *sender = NULL, *addr = NULL;
161 struct qitem *it = NULL;
162
163 bzero(&itmqueue, sizeof(itmqueue));
164 LIST_INIT(&itmqueue.queue);
165
166 queuef = fopen(queuefn, "r");
167 if (queuef == NULL)
168 goto out;
169
170 while (!feof(queuef)) {
171 if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
172 break;
173 line[strlen(line) - 1] = 0; /* chop newline */
174
175 s = strchr(line, ':');
176 if (s == NULL)
177 goto malformed;
178 *s = 0;
179
180 s++;
181 while (isspace(*s))
182 s++;
183
184 s = strdup(s);
4add537e 185 if (s == NULL)
ebffba26
SS
186 goto malformed;
187
188 if (strcmp(line, "ID") == 0) {
189 queueid = s;
190 } else if (strcmp(line, "Sender") == 0) {
191 sender = s;
192 } else if (strcmp(line, "Recipient") == 0) {
193 addr = s;
194 } else {
195 syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
196 line, queuefn);
197 free(s);
198 }
199 }
200
4add537e
SS
201 if (queueid == NULL || sender == NULL || addr == NULL ||
202 *queueid == 0 || *addr == 0) {
ebffba26
SS
203malformed:
204 errno = EINVAL;
205 syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
206 goto out;
207 }
208
209 if (add_recp(&itmqueue, addr, 0) != 0)
210 goto out;
211
212 it = LIST_FIRST(&itmqueue.queue);
213 it->sender = sender; sender = NULL;
214 it->queueid = queueid; queueid = NULL;
215 it->queuefn = queuefn; queuefn = NULL;
216 LIST_INSERT_HEAD(&queue->queue, it, next);
217
218out:
219 if (sender != NULL)
220 free(sender);
221 if (queueid != NULL)
222 free(queueid);
223 if (addr != NULL)
224 free(addr);
225 if (queuef != NULL)
226 fclose(queuef);
227
228 return (it);
229}
230
f4e61a9f 231int
1c9e6b7b 232linkspool(struct queue *queue)
f4e61a9f 233{
f4e61a9f 234 struct stat st;
f4e61a9f
SS
235 struct qitem *it;
236
9afa363f
SS
237 if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
238 goto delfiles;
239
240 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
1c9e6b7b 241 username, getuid(), queue->sender);
9afa363f 242
f4e61a9f 243 LIST_FOREACH(it, &queue->queue, next) {
405f48ee 244 if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
f4e61a9f 245 goto delfiles;
ca259d14 246 if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
f4e61a9f 247 goto delfiles;
ca259d14 248 if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
f4e61a9f
SS
249 goto delfiles;
250
251 /* Neither file may not exist yet */
252 if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
253 goto delfiles;
254
ebffba26 255 if (writequeuef(it) != 0)
f4e61a9f 256 goto delfiles;
f4e61a9f
SS
257
258 if (link(queue->tmpf, it->mailfn) != 0)
259 goto delfiles;
260 }
261
405f48ee
SS
262 LIST_FOREACH(it, &queue->queue, next) {
263 syslog(LOG_INFO, "mail to=<%s> queued as %s",
264 it->addr, it->queueid);
265 }
f4e61a9f
SS
266
267 unlink(queue->tmpf);
268 return (0);
269
270delfiles:
271 LIST_FOREACH(it, &queue->queue, next) {
f4e61a9f 272 unlink(it->mailfn);
1da0a9f2 273 unlink(it->queuefn);
f4e61a9f
SS
274 }
275 return (-1);
276}
277
1da0a9f2 278int
9afa363f 279load_queue(struct queue *queue)
f4e61a9f 280{
ebffba26 281 struct stat sb;
f4e61a9f 282 struct qitem *it;
f4e61a9f
SS
283 DIR *spooldir;
284 struct dirent *de;
f4e61a9f
SS
285 char *queuefn;
286 char *mailfn;
f4e61a9f 287
8075c3b8 288 bzero(queue, sizeof(*queue));
f4e61a9f
SS
289 LIST_INIT(&queue->queue);
290
ca259d14 291 spooldir = opendir(config.spooldir);
f4e61a9f
SS
292 if (spooldir == NULL)
293 err(1, "reading queue");
294
295 while ((de = readdir(spooldir)) != NULL) {
f4e61a9f 296 queuefn = NULL;
ebffba26 297 mailfn = NULL;
f4e61a9f 298
c8b07ee5 299 /* ignore non-queue files */
f4e61a9f
SS
300 if (de->d_name[0] != 'Q')
301 continue;
ca259d14 302 if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
f4e61a9f 303 goto fail;
ca259d14 304 if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
f4e61a9f
SS
305 goto fail;
306
c8b07ee5
SW
307 /*
308 * Some file systems don't provide a de->d_type, so we have to
309 * do an explicit stat on the queue file.
310 * Move on if it turns out to be something else than a file.
311 */
312 if (stat(queuefn, &sb) != 0)
313 goto skip_item;
314 if (!S_ISREG(sb.st_mode)) {
315 errno = EINVAL;
316 goto skip_item;
317 }
318
ebffba26 319 if (stat(mailfn, &sb) != 0)
f4e61a9f
SS
320 goto skip_item;
321
ebffba26
SS
322 it = readqueuef(queue, queuefn);
323 if (it == NULL)
f4e61a9f 324 goto skip_item;
f4e61a9f 325
f4e61a9f 326 it->mailfn = mailfn;
f4e61a9f
SS
327 continue;
328
329skip_item:
1da0a9f2 330 syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
f4e61a9f
SS
331 if (queuefn != NULL)
332 free(queuefn);
333 if (mailfn != NULL)
d557d463 334 free(mailfn);
f4e61a9f
SS
335 }
336 closedir(spooldir);
1da0a9f2 337 return (0);
f4e61a9f
SS
338
339fail:
1da0a9f2 340 return (-1);
f4e61a9f
SS
341}
342
343void
344delqueue(struct qitem *it)
345{
f4e61a9f 346 unlink(it->mailfn);
1da0a9f2 347 unlink(it->queuefn);
9afa363f
SS
348 if (it->queuef != NULL)
349 fclose(it->queuef);
350 if (it->mailf != NULL)
351 fclose(it->mailf);
f4e61a9f
SS
352 free(it);
353}
9afa363f
SS
354
355int
24c80b2b 356acquirespool(struct qitem *it)
9afa363f
SS
357{
358 int queuefd;
359
360 if (it->queuef == NULL) {
b95bffd0 361 queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK);
9afa363f 362 if (queuefd < 0)
1da0a9f2 363 goto fail;
9afa363f
SS
364 it->queuef = fdopen(queuefd, "r+");
365 if (it->queuef == NULL)
1da0a9f2 366 goto fail;
9afa363f
SS
367 }
368
369 if (it->mailf == NULL) {
370 it->mailf = fopen(it->mailfn, "r");
371 if (it->mailf == NULL)
1da0a9f2 372 goto fail;
9afa363f
SS
373 }
374
375 return (0);
1da0a9f2
SS
376
377fail:
14dfb991
JG
378 if (errno == EWOULDBLOCK)
379 return (1);
24c80b2b 380 syslog(LOG_INFO, "could not acquire queue file: %m");
1da0a9f2 381 return (-1);
9afa363f
SS
382}
383
384void
385dropspool(struct queue *queue, struct qitem *keep)
386{
387 struct qitem *it;
388
389 LIST_FOREACH(it, &queue->queue, next) {
390 if (it == keep)
391 continue;
392
393 if (it->queuef != NULL)
394 fclose(it->queuef);
395 if (it->mailf != NULL)
396 fclose(it->mailf);
397 }
398}
14dfb991
JG
399
400int
401flushqueue_since(unsigned int period)
402{
403 struct stat st;
404 struct timeval now;
405 char *flushfn = NULL;
406
407 if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
408 return (0);
409 if (stat(flushfn, &st) < 0) {
410 free(flushfn);
411 return (0);
412 }
413 free(flushfn);
414 flushfn = NULL;
415 if (gettimeofday(&now, 0) != 0)
416 return (0);
417
418 /* Did the flush file get touched within the last period seconds? */
efcc709c 419 if (st.st_mtim.tv_sec + period >= now.tv_sec)
14dfb991
JG
420 return (1);
421 else
422 return (0);
423}
424
425int
426flushqueue_signal(void)
427{
428 char *flushfn = NULL;
429 int fd;
430
431 if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
432 return (-1);
433 fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660);
434 free(flushfn);
435 if (fd < 0) {
436 syslog(LOG_ERR, "could not open flush file: %m");
437 return (-1);
438 }
439 close(fd);
440 return (0);
441}