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