| Commit | Line | Data |
|---|---|---|
| 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 | ||
| c8b07ee5 SW |
35 | #include "dfcompat.h" |
| 36 | ||
| 37 | #include <sys/file.h> | |
| f4e61a9f SS |
38 | #include <sys/stat.h> |
| 39 | ||
| ebffba26 | 40 | #include <ctype.h> |
| f4e61a9f SS |
41 | #include <dirent.h> |
| 42 | #include <err.h> | |
| 43 | #include <errno.h> | |
| 44 | #include <fcntl.h> | |
| 45 | #include <inttypes.h> | |
| 46 | #include <unistd.h> | |
| 405f48ee | 47 | #include <syslog.h> |
| f4e61a9f SS |
48 | |
| 49 | #include "dma.h" | |
| 50 | ||
| 51 | /* | |
| 52 | * Spool file format: | |
| 53 | * | |
| 54 | * 'Q'id files (queue): | |
| ebffba26 SS |
55 | * Organized like an RFC822 header, field: value. Ignores unknown fields. |
| 56 | * ID: id | |
| 57 | * Sender: envelope-from | |
| 58 | * Recipient: envelope-to | |
| f4e61a9f SS |
59 | * |
| 60 | * 'M'id files (data): | |
| f4e61a9f SS |
61 | * mail data |
| 62 | * | |
| 63 | * Each queue file needs to have a corresponding data file. | |
| 64 | * One data file might be shared by linking it several times. | |
| 65 | * | |
| 76ad9740 | 66 | * Queue ids are unique, formed from the inode of the data file |
| f4e61a9f SS |
67 | * and a unique identifier. |
| 68 | */ | |
| 69 | ||
| 70 | int | |
| 1c9e6b7b | 71 | newspoolf(struct queue *queue) |
| f4e61a9f | 72 | { |
| f4e61a9f | 73 | char fn[PATH_MAX+1]; |
| 405f48ee | 74 | struct stat st; |
| f4e61a9f | 75 | struct stritem *t; |
| 9afa363f | 76 | int fd; |
| f4e61a9f | 77 | |
| ca259d14 | 78 | if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0) |
| f4e61a9f SS |
79 | return (-1); |
| 80 | ||
| 9afa363f SS |
81 | fd = mkstemp(fn); |
| 82 | if (fd < 0) | |
| f4e61a9f | 83 | return (-1); |
| 6e30778b SS |
84 | /* XXX group rights */ |
| 85 | if (fchmod(fd, 0660) < 0) | |
| 86 | goto fail; | |
| 9afa363f SS |
87 | if (flock(fd, LOCK_EX) == -1) |
| 88 | goto fail; | |
| f4e61a9f SS |
89 | queue->tmpf = strdup(fn); |
| 90 | if (queue->tmpf == NULL) | |
| 91 | goto fail; | |
| 92 | ||
| 9afa363f SS |
93 | /* |
| 94 | * Assign queue id | |
| 95 | */ | |
| 96 | if (fstat(fd, &st) != 0) | |
| f4e61a9f | 97 | goto fail; |
| a0a50d0b | 98 | if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0) |
| f4e61a9f SS |
99 | goto fail; |
| 100 | ||
| 9afa363f SS |
101 | queue->mailf = fdopen(fd, "r+"); |
| 102 | if (queue->mailf == NULL) | |
| 103 | goto fail; | |
| f4e61a9f | 104 | |
| f4e61a9f SS |
105 | t = malloc(sizeof(*t)); |
| 106 | if (t != NULL) { | |
| 107 | t->str = queue->tmpf; | |
| 108 | SLIST_INSERT_HEAD(&tmpfs, t, next); | |
| 109 | } | |
| 110 | return (0); | |
| 111 | ||
| 112 | fail: | |
| 9afa363f SS |
113 | if (queue->mailf != NULL) |
| 114 | fclose(queue->mailf); | |
| 115 | close(fd); | |
| f4e61a9f SS |
116 | unlink(fn); |
| 117 | return (-1); | |
| 118 | } | |
| 119 | ||
| ebffba26 SS |
120 | static int |
| 121 | writequeuef(struct qitem *it) | |
| 122 | { | |
| 123 | int error; | |
| 124 | int queuefd; | |
| 125 | ||
| c8b07ee5 | 126 | queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660); |
| ebffba26 SS |
127 | if (queuefd == -1) |
| 128 | return (-1); | |
| c8b07ee5 SW |
129 | if (fchmod(queuefd, 0660) < 0) |
| 130 | return (-1); | |
| ebffba26 SS |
131 | it->queuef = fdopen(queuefd, "w+"); |
| 132 | if (it->queuef == NULL) | |
| 133 | return (-1); | |
| 134 | ||
| 135 | error = fprintf(it->queuef, | |
| 136 | "ID: %s\n" | |
| 137 | "Sender: %s\n" | |
| 138 | "Recipient: %s\n", | |
| 139 | it->queueid, | |
| 140 | it->sender, | |
| 141 | it->addr); | |
| 142 | ||
| 143 | if (error <= 0) | |
| 144 | return (-1); | |
| 145 | ||
| 146 | if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0) | |
| 147 | return (-1); | |
| 148 | ||
| 149 | return (0); | |
| 150 | } | |
| 151 | ||
| 152 | static struct qitem * | |
| 153 | readqueuef(struct queue *queue, char *queuefn) | |
| 154 | { | |
| 155 | char line[1000]; | |
| 156 | struct queue itmqueue; | |
| 157 | FILE *queuef = NULL; | |
| 158 | char *s; | |
| 159 | char *queueid = NULL, *sender = NULL, *addr = NULL; | |
| 160 | struct qitem *it = NULL; | |
| 161 | ||
| 162 | bzero(&itmqueue, sizeof(itmqueue)); | |
| 163 | LIST_INIT(&itmqueue.queue); | |
| 164 | ||
| 165 | queuef = fopen(queuefn, "r"); | |
| 166 | if (queuef == NULL) | |
| 167 | goto out; | |
| 168 | ||
| 169 | while (!feof(queuef)) { | |
| 170 | if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0) | |
| 171 | break; | |
| 172 | line[strlen(line) - 1] = 0; /* chop newline */ | |
| 173 | ||
| 174 | s = strchr(line, ':'); | |
| 175 | if (s == NULL) | |
| 176 | goto malformed; | |
| 177 | *s = 0; | |
| 178 | ||
| 179 | s++; | |
| 180 | while (isspace(*s)) | |
| 181 | s++; | |
| 182 | ||
| 183 | s = strdup(s); | |
| 4add537e | 184 | if (s == NULL) |
| ebffba26 SS |
185 | goto malformed; |
| 186 | ||
| 187 | if (strcmp(line, "ID") == 0) { | |
| 188 | queueid = s; | |
| 189 | } else if (strcmp(line, "Sender") == 0) { | |
| 190 | sender = s; | |
| 191 | } else if (strcmp(line, "Recipient") == 0) { | |
| 192 | addr = s; | |
| 193 | } else { | |
| 194 | syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'", | |
| 195 | line, queuefn); | |
| 196 | free(s); | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 4add537e SS |
200 | if (queueid == NULL || sender == NULL || addr == NULL || |
| 201 | *queueid == 0 || *addr == 0) { | |
| ebffba26 SS |
202 | malformed: |
| 203 | errno = EINVAL; | |
| 204 | syslog(LOG_ERR, "malformed queue file `%s'", queuefn); | |
| 205 | goto out; | |
| 206 | } | |
| 207 | ||
| 208 | if (add_recp(&itmqueue, addr, 0) != 0) | |
| 209 | goto out; | |
| 210 | ||
| 211 | it = LIST_FIRST(&itmqueue.queue); | |
| 212 | it->sender = sender; sender = NULL; | |
| 213 | it->queueid = queueid; queueid = NULL; | |
| 214 | it->queuefn = queuefn; queuefn = NULL; | |
| 215 | LIST_INSERT_HEAD(&queue->queue, it, next); | |
| 216 | ||
| 217 | out: | |
| 218 | if (sender != NULL) | |
| 219 | free(sender); | |
| 220 | if (queueid != NULL) | |
| 221 | free(queueid); | |
| 222 | if (addr != NULL) | |
| 223 | free(addr); | |
| 224 | if (queuef != NULL) | |
| 225 | fclose(queuef); | |
| 226 | ||
| 227 | return (it); | |
| 228 | } | |
| 229 | ||
| f4e61a9f | 230 | int |
| 1c9e6b7b | 231 | linkspool(struct queue *queue) |
| f4e61a9f | 232 | { |
| f4e61a9f | 233 | struct stat st; |
| f4e61a9f SS |
234 | struct qitem *it; |
| 235 | ||
| 9afa363f SS |
236 | if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0) |
| 237 | goto delfiles; | |
| 238 | ||
| 239 | syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>", | |
| 1c9e6b7b | 240 | username, getuid(), queue->sender); |
| 9afa363f | 241 | |
| f4e61a9f | 242 | LIST_FOREACH(it, &queue->queue, next) { |
| 405f48ee | 243 | if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0) |
| f4e61a9f | 244 | goto delfiles; |
| ca259d14 | 245 | if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0) |
| f4e61a9f | 246 | goto delfiles; |
| ca259d14 | 247 | if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0) |
| f4e61a9f SS |
248 | goto delfiles; |
| 249 | ||
| 250 | /* Neither file may not exist yet */ | |
| 251 | if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0) | |
| 252 | goto delfiles; | |
| 253 | ||
| ebffba26 | 254 | if (writequeuef(it) != 0) |
| f4e61a9f | 255 | goto delfiles; |
| f4e61a9f SS |
256 | |
| 257 | if (link(queue->tmpf, it->mailfn) != 0) | |
| 258 | goto delfiles; | |
| 259 | } | |
| 260 | ||
| 405f48ee SS |
261 | LIST_FOREACH(it, &queue->queue, next) { |
| 262 | syslog(LOG_INFO, "mail to=<%s> queued as %s", | |
| 263 | it->addr, it->queueid); | |
| 264 | } | |
| f4e61a9f SS |
265 | |
| 266 | unlink(queue->tmpf); | |
| 267 | return (0); | |
| 268 | ||
| 269 | delfiles: | |
| 270 | LIST_FOREACH(it, &queue->queue, next) { | |
| f4e61a9f | 271 | unlink(it->mailfn); |
| 1da0a9f2 | 272 | unlink(it->queuefn); |
| f4e61a9f SS |
273 | } |
| 274 | return (-1); | |
| 275 | } | |
| 276 | ||
| 1da0a9f2 | 277 | int |
| 9afa363f | 278 | load_queue(struct queue *queue) |
| f4e61a9f | 279 | { |
| ebffba26 | 280 | struct stat sb; |
| f4e61a9f | 281 | struct qitem *it; |
| f4e61a9f SS |
282 | DIR *spooldir; |
| 283 | struct dirent *de; | |
| f4e61a9f SS |
284 | char *queuefn; |
| 285 | char *mailfn; | |
| f4e61a9f | 286 | |
| e50076f8 | 287 | bzero(queue, sizeof(queue)); |
| f4e61a9f SS |
288 | LIST_INIT(&queue->queue); |
| 289 | ||
| ca259d14 | 290 | spooldir = opendir(config.spooldir); |
| f4e61a9f SS |
291 | if (spooldir == NULL) |
| 292 | err(1, "reading queue"); | |
| 293 | ||
| 294 | while ((de = readdir(spooldir)) != NULL) { | |
| f4e61a9f | 295 | queuefn = NULL; |
| ebffba26 | 296 | mailfn = NULL; |
| f4e61a9f | 297 | |
| c8b07ee5 | 298 | /* ignore non-queue files */ |
| f4e61a9f SS |
299 | if (de->d_name[0] != 'Q') |
| 300 | continue; | |
| ca259d14 | 301 | if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0) |
| f4e61a9f | 302 | goto fail; |
| ca259d14 | 303 | if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0) |
| f4e61a9f SS |
304 | goto fail; |
| 305 | ||
| c8b07ee5 SW |
306 | /* |
| 307 | * Some file systems don't provide a de->d_type, so we have to | |
| 308 | * do an explicit stat on the queue file. | |
| 309 | * Move on if it turns out to be something else than a file. | |
| 310 | */ | |
| 311 | if (stat(queuefn, &sb) != 0) | |
| 312 | goto skip_item; | |
| 313 | if (!S_ISREG(sb.st_mode)) { | |
| 314 | errno = EINVAL; | |
| 315 | goto skip_item; | |
| 316 | } | |
| 317 | ||
| ebffba26 | 318 | if (stat(mailfn, &sb) != 0) |
| f4e61a9f SS |
319 | goto skip_item; |
| 320 | ||
| ebffba26 SS |
321 | it = readqueuef(queue, queuefn); |
| 322 | if (it == NULL) | |
| f4e61a9f | 323 | goto skip_item; |
| f4e61a9f | 324 | |
| f4e61a9f | 325 | it->mailfn = mailfn; |
| f4e61a9f SS |
326 | continue; |
| 327 | ||
| 328 | skip_item: | |
| 1da0a9f2 | 329 | syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn); |
| f4e61a9f SS |
330 | if (queuefn != NULL) |
| 331 | free(queuefn); | |
| 332 | if (mailfn != NULL) | |
| d557d463 | 333 | free(mailfn); |
| f4e61a9f SS |
334 | } |
| 335 | closedir(spooldir); | |
| 1da0a9f2 | 336 | return (0); |
| f4e61a9f SS |
337 | |
| 338 | fail: | |
| 1da0a9f2 | 339 | return (-1); |
| f4e61a9f SS |
340 | } |
| 341 | ||
| 342 | void | |
| 343 | delqueue(struct qitem *it) | |
| 344 | { | |
| f4e61a9f | 345 | unlink(it->mailfn); |
| 1da0a9f2 | 346 | unlink(it->queuefn); |
| 9afa363f SS |
347 | if (it->queuef != NULL) |
| 348 | fclose(it->queuef); | |
| 349 | if (it->mailf != NULL) | |
| 350 | fclose(it->mailf); | |
| f4e61a9f SS |
351 | free(it); |
| 352 | } | |
| 9afa363f SS |
353 | |
| 354 | int | |
| 24c80b2b | 355 | acquirespool(struct qitem *it) |
| 9afa363f SS |
356 | { |
| 357 | int queuefd; | |
| 358 | ||
| 359 | if (it->queuef == NULL) { | |
| b95bffd0 | 360 | queuefd = open_locked(it->queuefn, O_RDWR|O_NONBLOCK); |
| 9afa363f | 361 | if (queuefd < 0) |
| 1da0a9f2 | 362 | goto fail; |
| 9afa363f SS |
363 | it->queuef = fdopen(queuefd, "r+"); |
| 364 | if (it->queuef == NULL) | |
| 1da0a9f2 | 365 | goto fail; |
| 9afa363f SS |
366 | } |
| 367 | ||
| 368 | if (it->mailf == NULL) { | |
| 369 | it->mailf = fopen(it->mailfn, "r"); | |
| 370 | if (it->mailf == NULL) | |
| 1da0a9f2 | 371 | goto fail; |
| 9afa363f SS |
372 | } |
| 373 | ||
| 374 | return (0); | |
| 1da0a9f2 SS |
375 | |
| 376 | fail: | |
| 24c80b2b | 377 | syslog(LOG_INFO, "could not acquire queue file: %m"); |
| 1da0a9f2 | 378 | return (-1); |
| 9afa363f SS |
379 | } |
| 380 | ||
| 381 | void | |
| 382 | dropspool(struct queue *queue, struct qitem *keep) | |
| 383 | { | |
| 384 | struct qitem *it; | |
| 385 | ||
| 386 | LIST_FOREACH(it, &queue->queue, next) { | |
| 387 | if (it == keep) | |
| 388 | continue; | |
| 389 | ||
| 390 | if (it->queuef != NULL) | |
| 391 | fclose(it->queuef); | |
| 392 | if (it->mailf != NULL) | |
| 393 | fclose(it->mailf); | |
| 394 | } | |
| 395 | } |