]>
git.ipfire.org Git - people/ms/dma.git/blob - spool.c
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
52 * 'Q'id files (queue):
59 * Each queue file needs to have a corresponding data file.
60 * One data file might be shared by linking it several times.
62 * Queue ids are unique, formed from the inode of the data file
63 * and a unique identifier.
67 newspoolf(struct queue
*queue
, const char *sender
)
76 if (snprintf(fn
, sizeof(fn
), "%s/%s", config
->spooldir
, "tmp_XXXXXXXXXX") <= 0)
82 if (flock(fd
, LOCK_EX
) == -1)
84 queue
->tmpf
= strdup(fn
);
85 if (queue
->tmpf
== NULL
)
91 if (fstat(fd
, &st
) != 0)
93 if (asprintf(&queue
->id
, "%"PRIxMAX
, st
.st_ino
) < 0)
96 queue
->mailf
= fdopen(fd
, "r+");
97 if (queue
->mailf
== NULL
)
100 if (fprintf(queue
->mailf
, "%s\n", sender
) < 0)
103 hdrlen
= ftello(queue
->mailf
);
105 LIST_FOREACH(it
, &queue
->queue
, next
) {
106 it
->mailf
= queue
->mailf
;
110 t
= malloc(sizeof(*t
));
112 t
->str
= queue
->tmpf
;
113 SLIST_INSERT_HEAD(&tmpfs
, t
, next
);
118 if (queue
->mailf
!= NULL
)
119 fclose(queue
->mailf
);
126 linkspool(struct queue
*queue
, const char *sender
)
128 char line
[1000]; /* by RFC2822 */
134 if (fflush(queue
->mailf
) != 0 || fsync(fileno(queue
->mailf
)) != 0)
137 syslog(LOG_INFO
, "new mail from user=%s uid=%d envelope_from=<%s>",
138 username
, getuid(), sender
);
140 LIST_FOREACH(it
, &queue
->queue
, next
) {
141 if (asprintf(&it
->queueid
, "%s.%"PRIxPTR
, queue
->id
, (uintptr_t)it
) <= 0)
143 if (asprintf(&it
->queuefn
, "%s/Q%s", config
->spooldir
, it
->queueid
) <= 0)
145 if (asprintf(&it
->mailfn
, "%s/M%s", config
->spooldir
, it
->queueid
) <= 0)
148 /* Neither file may not exist yet */
149 if (stat(it
->queuefn
, &st
) == 0 || stat(it
->mailfn
, &st
) == 0)
152 error
= snprintf(line
, sizeof(line
), "%s %s\n", it
->queueid
, it
->addr
);
153 if ((ssize_t
)error
< 0 || error
>= sizeof(line
))
156 queuefd
= open_locked(it
->queuefn
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
159 it
->queuef
= fdopen(queuefd
, "w+");
160 if (it
->queuef
== NULL
)
163 if (fwrite(line
, strlen(line
), 1, it
->queuef
) != 1)
165 if (fflush(it
->queuef
) != 0 || fsync(fileno(it
->queuef
)) != 0)
168 if (link(queue
->tmpf
, it
->mailfn
) != 0)
172 LIST_FOREACH(it
, &queue
->queue
, next
) {
173 syslog(LOG_INFO
, "mail to=<%s> queued as %s",
174 it
->addr
, it
->queueid
);
181 LIST_FOREACH(it
, &queue
->queue
, next
) {
189 load_queue(struct queue
*queue
)
192 //struct queue queue, itmqueue;
193 struct queue itmqueue
;
206 LIST_INIT(&queue
->queue
);
208 spooldir
= opendir(config
->spooldir
);
209 if (spooldir
== NULL
)
210 err(1, "reading queue");
212 while ((de
= readdir(spooldir
)) != NULL
) {
218 LIST_INIT(&itmqueue
.queue
);
220 /* ignore temp files */
221 if (strncmp(de
->d_name
, "tmp_", 4) == 0 || de
->d_type
!= DT_REG
)
223 if (de
->d_name
[0] != 'Q')
225 if (asprintf(&queuefn
, "%s/Q%s", config
->spooldir
, de
->d_name
+ 1) < 0)
227 if (asprintf(&mailfn
, "%s/M%s", config
->spooldir
, de
->d_name
+ 1) < 0)
230 mailf
= fopen(mailfn
, "r");
233 if (fgets(line
, sizeof(line
), mailf
) == NULL
|| line
[0] == 0)
235 line
[strlen(line
) - 1] = 0; /* chop newline */
236 sender
= strdup(line
);
240 hdrlen
= ftell(mailf
);
242 queuef
= fopen(queuefn
, "r");
246 if (fgets(line
, sizeof(line
), queuef
) == NULL
|| line
[0] == 0)
248 line
[strlen(line
) - 1] = 0;
249 queueid
= strdup(line
);
252 addr
= strchr(queueid
, ' ');
257 if (add_recp(&itmqueue
, addr
, sender
, 0) != 0)
260 it
= LIST_FIRST(&itmqueue
.queue
);
261 it
->queueid
= queueid
;
262 it
->queuefn
= queuefn
;
265 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
274 warn("reading queue: `%s'", queuefn
);
292 err(1, "reading queue");
296 delqueue(struct qitem
*it
)
300 if (it
->queuef
!= NULL
)
302 if (it
->mailf
!= NULL
)
308 aquirespool(struct qitem
*it
)
312 if (it
->queuef
== NULL
) {
313 queuefd
= open_locked(it
->queuefn
, O_RDWR
);
316 it
->queuef
= fdopen(queuefd
, "r+");
317 if (it
->queuef
== NULL
)
321 if (it
->mailf
== NULL
) {
322 it
->mailf
= fopen(it
->mailfn
, "r");
323 if (it
->mailf
== NULL
)
331 dropspool(struct queue
*queue
, struct qitem
*keep
)
335 LIST_FOREACH(it
, &queue
->queue
, next
) {
339 if (it
->queuef
!= NULL
)
341 if (it
->mailf
!= NULL
)