diff options
author | Gilles Chehade <gilles@cvs.openbsd.org> | 2009-01-26 22:20:32 +0000 |
---|---|---|
committer | Gilles Chehade <gilles@cvs.openbsd.org> | 2009-01-26 22:20:32 +0000 |
commit | 7312c3d09ee686b91278cfce37d2cd2a30a1f9b7 (patch) | |
tree | 712d53a2e018f93fdb902ad56bd3ed14c2a85690 /usr.sbin | |
parent | c39a0d1bc89de3368f815b27706ae4e00d66c252 (diff) |
move some queue related functions that are needed outside of smtpd to the
sharedqueue.c file, smtpctl cannot link queue.o without creating a mess
otherwise. while at it, move some prototypes to smtpd.h as they will be
needed by enqueue code
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/smtpd/queue.c | 181 | ||||
-rw-r--r-- | usr.sbin/smtpd/sharedqueue.c | 215 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd.h | 12 |
3 files changed, 227 insertions, 181 deletions
diff --git a/usr.sbin/smtpd/queue.c b/usr.sbin/smtpd/queue.c index a3130097ba8..e351628273d 100644 --- a/usr.sbin/smtpd/queue.c +++ b/usr.sbin/smtpd/queue.c @@ -1,4 +1,4 @@ -/* $OpenBSD: queue.c,v 1.46 2009/01/26 21:26:07 gilles Exp $ */ +/* $OpenBSD: queue.c,v 1.47 2009/01/26 22:20:31 gilles Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -646,174 +646,6 @@ queue_purge_incoming(void) closedir(dirp); } - -int -queue_create_layout_message(char *queuepath, char *message_id) -{ - char rootdir[MAXPATHLEN]; - char evpdir[MAXPATHLEN]; - - if (! bsnprintf(rootdir, MAXPATHLEN, "%s/%d.XXXXXXXXXXXXXXXX", - queuepath, time(NULL))) - fatalx("queue_create_message_layout: snprintf"); - - if (mkdtemp(rootdir) == NULL) { - if (errno == ENOSPC) - return 0; - fatal("queue_create_message_layout: mkdtemp"); - } - - if (strlcpy(message_id, rootdir + strlen(queuepath) + 1, MAXPATHLEN) - >= MAXPATHLEN) - fatalx("queue_create_message_layout: truncation"); - - if (! bsnprintf(evpdir, MAXPATHLEN, "%s%s", rootdir, PATH_ENVELOPES)) - fatalx("queue_create_message_layout: snprintf"); - - if (mkdir(evpdir, 0700) == -1) { - if (errno == ENOSPC) { - rmdir(rootdir); - return 0; - } - fatal("queue_create_message_layout: mkdir"); - } - - return 1; -} - -void -queue_delete_layout_message(char *queuepath, char *msgid) -{ - char rootdir[MAXPATHLEN]; - char purgedir[MAXPATHLEN]; - - if (! bsnprintf(rootdir, MAXPATHLEN, "%s/%s", queuepath, msgid)) - fatalx("snprintf"); - - if (! bsnprintf(purgedir, MAXPATHLEN, "%s/%s", PATH_PURGE, msgid)) - fatalx("snprintf"); - - if (rename(rootdir, purgedir) == -1) { - if (errno == ENOENT) - return; - fatal("queue_delete_incoming_message: rename"); - } -} - -int -queue_record_layout_envelope(char *queuepath, struct message *message) -{ - char evpname[MAXPATHLEN]; - FILE *fp; - int fd; - -again: - if (! bsnprintf(evpname, MAXPATHLEN, "%s/%s%s/%s.%qu", queuepath, - message->message_id, PATH_ENVELOPES, message->message_id, - (u_int64_t)arc4random())) - fatalx("queue_record_incoming_envelope: snprintf"); - - fd = open(evpname, O_WRONLY|O_CREAT|O_EXCL, 0600); - if (fd == -1) { - if (errno == EEXIST) - goto again; - if (errno == ENOSPC || errno == ENFILE) - goto tempfail; - fatal("queue_record_incoming_envelope: open"); - } - - fp = fdopen(fd, "w"); - if (fp == NULL) - fatal("queue_record_incoming_envelope: fdopen"); - - message->creation = time(NULL); - if (strlcpy(message->message_uid, strrchr(evpname, '/') + 1, MAXPATHLEN) - >= MAXPATHLEN) - fatalx("queue_record_incoming_envelope: truncation"); - - if (fwrite(message, sizeof (struct message), 1, fp) != 1) { - if (errno == ENOSPC) - goto tempfail; - fatal("queue_record_incoming_envelope: write"); - } - - if (! safe_fclose(fp)) - goto tempfail; - - return 1; - -tempfail: - unlink(evpname); - close(fd); - message->creation = 0; - message->message_uid[0] = '\0'; - - return 0; -} - -int -queue_remove_layout_envelope(char *queuepath, struct message *message) -{ - char pathname[MAXPATHLEN]; - - if (! bsnprintf(pathname, MAXPATHLEN, "%s/%s%s/%s", queuepath, - message->message_id, PATH_ENVELOPES, message->message_uid)) - fatal("queue_remove_incoming_envelope: snprintf"); - - if (unlink(pathname) == -1) - if (errno != ENOENT) - fatal("queue_remove_incoming_envelope: unlink"); - - return 1; -} - -int -queue_commit_layout_message(char *queuepath, struct message *messagep) -{ - char rootdir[MAXPATHLEN]; - char queuedir[MAXPATHLEN]; - - if (! bsnprintf(rootdir, MAXPATHLEN, "%s/%s", queuepath, - messagep->message_id)) - fatal("queue_commit_message_incoming: snprintf"); - - if (! bsnprintf(queuedir, MAXPATHLEN, "%s/%d", PATH_QUEUE, - queue_hash(messagep->message_id))) - fatal("queue_commit_message_incoming: snprintf"); - - if (mkdir(queuedir, 0700) == -1) { - if (errno == ENOSPC) - return 0; - if (errno != EEXIST) - fatal("queue_commit_message_incoming: mkdir"); - } - - if (strlcat(queuedir, "/", MAXPATHLEN) >= MAXPATHLEN || - strlcat(queuedir, messagep->message_id, MAXPATHLEN) >= MAXPATHLEN) - fatalx("queue_commit_incoming_message: truncation"); - - if (rename(rootdir, queuedir) == -1) { - if (errno == ENOSPC) - return 0; - fatal("queue_commit_message_incoming: rename"); - } - - return 1; -} - -int -queue_open_layout_messagefile(char *queuepath, struct message *messagep) -{ - char pathname[MAXPATHLEN]; - mode_t mode = O_CREAT|O_EXCL|O_RDWR; - - if (! bsnprintf(pathname, MAXPATHLEN, "%s/%s/message", queuepath, - messagep->message_id)) - fatal("queue_open_incoming_message_file: snprintf"); - - return open(pathname, mode, 0600); -} - int queue_create_incoming_layout(char *msgid) { @@ -1045,14 +877,3 @@ queue_message_update(struct message *messagep) /* no error, remove envelope */ queue_remove_envelope(messagep); } - -u_int16_t -queue_hash(char *msgid) -{ - u_int16_t h; - - for (h = 5381; *msgid; msgid++) - h = ((h << 5) + h) + *msgid; - - return (h % DIRHASH_BUCKETS); -} diff --git a/usr.sbin/smtpd/sharedqueue.c b/usr.sbin/smtpd/sharedqueue.c new file mode 100644 index 00000000000..082db3498b4 --- /dev/null +++ b/usr.sbin/smtpd/sharedqueue.c @@ -0,0 +1,215 @@ +/* $OpenBSD: sharedqueue.c,v 1.1 2009/01/26 22:20:31 gilles Exp $ */ + +/* + * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> + * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <sys/queue.h> +#include <sys/tree.h> +#include <sys/param.h> +#include <sys/socket.h> +#include <sys/stat.h> + +#include <dirent.h> +#include <errno.h> +#include <event.h> +#include <fcntl.h> +#include <pwd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "smtpd.h" + +int +queue_create_layout_message(char *queuepath, char *message_id) +{ + char rootdir[MAXPATHLEN]; + char evpdir[MAXPATHLEN]; + + if (! bsnprintf(rootdir, MAXPATHLEN, "%s/%d.XXXXXXXXXXXXXXXX", + queuepath, time(NULL))) + fatalx("queue_create_layout_message: snprintf"); + + if (mkdtemp(rootdir) == NULL) { + if (errno == ENOSPC) + return 0; + fatal("queue_create_layout_message: mkdtemp"); + } + + if (strlcpy(message_id, rootdir + strlen(queuepath) + 1, MAXPATHLEN) + >= MAXPATHLEN) + fatalx("queue_create_layout_message: truncation"); + + if (! bsnprintf(evpdir, MAXPATHLEN, "%s%s", rootdir, PATH_ENVELOPES)) + fatalx("queue_create_layout_message: snprintf"); + + if (mkdir(evpdir, 0700) == -1) { + if (errno == ENOSPC) { + rmdir(rootdir); + return 0; + } + fatal("queue_create_layout_message: mkdir"); + } + + return 1; +} + +void +queue_delete_layout_message(char *queuepath, char *msgid) +{ + char rootdir[MAXPATHLEN]; + char purgedir[MAXPATHLEN]; + + if (! bsnprintf(rootdir, MAXPATHLEN, "%s/%s", queuepath, msgid)) + fatalx("snprintf"); + + if (! bsnprintf(purgedir, MAXPATHLEN, "%s/%s", PATH_PURGE, msgid)) + fatalx("snprintf"); + + if (rename(rootdir, purgedir) == -1) { + if (errno == ENOENT) + return; + fatal("queue_delete_incoming_message: rename"); + } +} + +int +queue_record_layout_envelope(char *queuepath, struct message *message) +{ + char evpname[MAXPATHLEN]; + FILE *fp; + int fd; + +again: + if (! bsnprintf(evpname, MAXPATHLEN, "%s/%s%s/%s.%qu", queuepath, + message->message_id, PATH_ENVELOPES, message->message_id, + (u_int64_t)arc4random())) + fatalx("queue_record_incoming_envelope: snprintf"); + + fd = open(evpname, O_WRONLY|O_CREAT|O_EXCL, 0600); + if (fd == -1) { + if (errno == EEXIST) + goto again; + if (errno == ENOSPC || errno == ENFILE) + goto tempfail; + fatal("queue_record_incoming_envelope: open"); + } + + fp = fdopen(fd, "w"); + if (fp == NULL) + fatal("queue_record_incoming_envelope: fdopen"); + + message->creation = time(NULL); + if (strlcpy(message->message_uid, strrchr(evpname, '/') + 1, MAXPATHLEN) + >= MAXPATHLEN) + fatalx("queue_record_incoming_envelope: truncation"); + + if (fwrite(message, sizeof (struct message), 1, fp) != 1) { + if (errno == ENOSPC) + goto tempfail; + fatal("queue_record_incoming_envelope: write"); + } + + if (! safe_fclose(fp)) + goto tempfail; + + return 1; + +tempfail: + unlink(evpname); + close(fd); + message->creation = 0; + message->message_uid[0] = '\0'; + + return 0; +} + +int +queue_remove_layout_envelope(char *queuepath, struct message *message) +{ + char pathname[MAXPATHLEN]; + + if (! bsnprintf(pathname, MAXPATHLEN, "%s/%s%s/%s", queuepath, + message->message_id, PATH_ENVELOPES, message->message_uid)) + fatal("queue_remove_incoming_envelope: snprintf"); + + if (unlink(pathname) == -1) + if (errno != ENOENT) + fatal("queue_remove_incoming_envelope: unlink"); + + return 1; +} + +int +queue_commit_layout_message(char *queuepath, struct message *messagep) +{ + char rootdir[MAXPATHLEN]; + char queuedir[MAXPATHLEN]; + + if (! bsnprintf(rootdir, MAXPATHLEN, "%s/%s", queuepath, + messagep->message_id)) + fatal("queue_commit_message_incoming: snprintf"); + + if (! bsnprintf(queuedir, MAXPATHLEN, "%s/%d", PATH_QUEUE, + queue_hash(messagep->message_id))) + fatal("queue_commit_message_incoming: snprintf"); + + if (mkdir(queuedir, 0700) == -1) { + if (errno == ENOSPC) + return 0; + if (errno != EEXIST) + fatal("queue_commit_message_incoming: mkdir"); + } + + if (strlcat(queuedir, "/", MAXPATHLEN) >= MAXPATHLEN || + strlcat(queuedir, messagep->message_id, MAXPATHLEN) >= MAXPATHLEN) + fatalx("queue_commit_incoming_message: truncation"); + + if (rename(rootdir, queuedir) == -1) { + if (errno == ENOSPC) + return 0; + fatal("queue_commit_message_incoming: rename"); + } + + return 1; +} + +int +queue_open_layout_messagefile(char *queuepath, struct message *messagep) +{ + char pathname[MAXPATHLEN]; + mode_t mode = O_CREAT|O_EXCL|O_RDWR; + + if (! bsnprintf(pathname, MAXPATHLEN, "%s/%s/message", queuepath, + messagep->message_id)) + fatal("queue_open_incoming_message_file: snprintf"); + + return open(pathname, mode, 0600); +} + +u_int16_t +queue_hash(char *msgid) +{ + u_int16_t h; + + for (h = 5381; *msgid; msgid++) + h = ((h << 5) + h) + *msgid; + + return (h % DIRHASH_BUCKETS); +} diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 0312eea4218..86a31ea6933 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.46 2009/01/14 23:48:35 gilles Exp $ */ +/* $OpenBSD: smtpd.h,v 1.47 2009/01/26 22:20:31 gilles Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -46,6 +46,7 @@ #define PATH_SPOOL "/var/spool/smtpd" +#define PATH_ENQUEUE "/enqueue" #define PATH_INCOMING "/incoming" #define PATH_QUEUE "/queue" #define PATH_PURGE "/purge" @@ -721,6 +722,15 @@ struct batch *batch_by_id(struct smtpd *, u_int64_t); struct message *message_by_id(struct smtpd *, struct batch *, u_int64_t); u_int16_t queue_hash(char *); +/* sharedqueue.c */ +int queue_create_layout_message(char *, char *); +void queue_delete_layout_message(char *, char *); +int queue_record_layout_envelope(char *, struct message *); +int queue_remove_layout_envelope(char *, struct message *); +int queue_commit_layout_message(char *, struct message *); +int queue_open_layout_messagefile(char *, struct message *); +u_int16_t queue_hash(char *); + /* mda.c */ pid_t mda(struct smtpd *); int mdaproc_cmp(struct mdaproc *, struct mdaproc *); |