diff --git a/src/sieve-bin/Makefile.am b/src/sieve-bin/Makefile.am index c677cdd4db811810897355128b8aba29b65d2ee9..96a7a5b46ac17c2869cf4c50ff8e55a234cca853 100644 --- a/src/sieve-bin/Makefile.am +++ b/src/sieve-bin/Makefile.am @@ -51,6 +51,7 @@ common_sources = \ mail-raw.c sievec_SOURCES = \ + $(common_sources) \ sievec.c sieve_test_SOURCES = \ diff --git a/src/sieve-bin/bin-common.c b/src/sieve-bin/bin-common.c index 3b61de1b90b39f71a0715c36f496df59b68c7d57..01b9ede9ec66fb18b1be516d4455b50c1a99bfed 100644 --- a/src/sieve-bin/bin-common.c +++ b/src/sieve-bin/bin-common.c @@ -3,6 +3,7 @@ #include "lib.h" #include "lib-signals.h" #include "ioloop.h" +#include "ostream.h" #include "hostpid.h" #include "sieve.h" @@ -36,13 +37,76 @@ void bin_init(void) lib_signals_set_handler(SIGTERM, TRUE, sig_die, NULL); lib_signals_ignore(SIGPIPE, TRUE); lib_signals_ignore(SIGALRM, FALSE); + + if ( !sieve_init("") ) + i_fatal("failed to initialize sieve implementation\n"); } void bin_deinit(void) { + sieve_deinit(); + lib_signals_deinit(); io_loop_destroy(&ioloop); lib_deinit(); } + +const char *bin_get_user(void) +{ + uid_t process_euid = geteuid(); + struct passwd *pw = getpwuid(process_euid); + if (pw != NULL) { + return t_strdup(pw->pw_name); + } + + i_fatal("couldn't lookup our username (uid=%s)", dec2str(process_euid)); + return NULL; +} + +struct sieve_binary *bin_compile_sieve_script(const char *filename) +{ + int sfd; + struct sieve_binary *sbin; + + i_info("compiling sieve script '%s'...\n", filename); + + if ( (sfd = open(filename, O_RDONLY)) < 0 ) + i_info("failed to open sieve script %s: %m", filename); + + if ( (sbin = sieve_compile(sfd, FALSE)) == NULL ) + { + close(sfd); + i_fatal("failed to compile sieve script\n"); + } + + close(sfd); + return sbin; +} + +void bin_dump_sieve_binary_to(struct sieve_binary *sbin, const char *filename) +{ + int dfd = -1; + struct ostream *dumpstream; + + if ( strcmp(filename, "-") == 0 ) + dumpstream = o_stream_create_fd(1, 0, FALSE); + else { + if ( (dfd = open(filename, O_WRONLY)) < 0 ) { + i_fatal("failed to open dump-file for writing: %m"); + exit(1); + } + + dumpstream = o_stream_create_fd(dfd, 0, FALSE); + } + + if ( dumpstream != NULL ) { + (void) sieve_dump(sbin, dumpstream); + o_stream_destroy(&dumpstream); + } else { + i_fatal("failed to create stream for sieve code dump."); + } + if ( dfd != -1 ) + close(dfd); +} diff --git a/src/sieve-bin/bin-common.h b/src/sieve-bin/bin-common.h index ecacac4f41a5a75e27f430df784f6c44a2df98c6..1b75eb9fb8eafc11afa7cf4108389cb2e8d12241 100644 --- a/src/sieve-bin/bin-common.h +++ b/src/sieve-bin/bin-common.h @@ -1,7 +1,13 @@ #ifndef __BIN_COMMON_H #define __BIN_COMMON_H +#include "sieve.h" + void bin_init(void); void bin_deinit(void); +const char *bin_get_user(void); +struct sieve_binary *bin_compile_sieve_script(const char *filename); +void bin_dump_sieve_binary_to(struct sieve_binary *sbin, const char *filename); + #endif /* __BIN_COMMON_H */ diff --git a/src/sieve-bin/sieve-exec.c b/src/sieve-bin/sieve-exec.c index 51c8edb9ebce6ec24c0fa11e0625878b15fc570d..e061d97494f4a4f1b647c259d6634be8818fc884 100644 --- a/src/sieve-bin/sieve-exec.c +++ b/src/sieve-bin/sieve-exec.c @@ -23,10 +23,11 @@ static void *sieve_smtp_open(const char *destination, const char *return_path, FILE **file_r) -{getenv("HOSTNAME"); - printf("Sending mesage from <%s> to <%s>:\n\nSTART MESSAGE:\n", +{ + i_info("sending mesage from <%s> to <%s>:", return_path == NULL || *return_path == '\0' ? "" : return_path, destination); + printf("\nSTART MESSAGE:\n"); *file_r = stdout; @@ -42,7 +43,7 @@ static bool sieve_smtp_close(void *handle ATTR_UNUSED) static int duplicate_check(const void *id ATTR_UNUSED, size_t id_size ATTR_UNUSED, const char *user) { - printf("Checked duplicate for user %s.\n", user); + i_info("checked duplicate for user %s.\n", user); return 0; } @@ -50,15 +51,13 @@ static void duplicate_mark (const void *id ATTR_UNUSED, size_t id_size ATTR_UNUSED, const char *user, time_t time ATTR_UNUSED) { - printf("Marked duplicate for user %s.\n", user); + i_info("marked duplicate for user %s.\n", user); } int main(int argc, char **argv) { const char *user; - struct passwd *pw; - uid_t process_euid; - int sfd, mfd; + int mfd; pool_t namespaces_pool; struct mail_namespace *ns; struct mail_raw *mailr; @@ -73,13 +72,6 @@ int main(int argc, char **argv) exit(1); } - /* Open sieve script */ - - if ( (sfd = open(argv[1], O_RDONLY)) < 0 ) { - perror("open()"); - exit(1); - } - /* Open mail file */ if ( argc > 2 ) @@ -95,39 +87,10 @@ int main(int argc, char **argv) } else mfd = 0; - /* Compile sieve script */ - - printf("Parsing sieve script '%s'...\n", argv[1]); - - if ( !sieve_init("") ) { - printf("Failed to initialize sieve implementation\n"); - exit(1); - } - - if ( (sbin = sieve_compile(sfd, FALSE)) == NULL ) { - printf("Failed to compile sieve script\n"); - exit(1); - } - - if ( sbin != NULL ) { - struct ostream *dumpstream = o_stream_create_fd(1, 0, FALSE); - - if ( dumpstream != NULL ) { - (void) sieve_dump(sbin, dumpstream); - o_stream_unref(&dumpstream); - } - } - - close(sfd); + sbin = bin_compile_sieve_script(argv[1]); + bin_dump_sieve_binary_to(sbin, "-"); - process_euid = geteuid(); - pw = getpwuid(process_euid); - if (pw != NULL) { - user = t_strdup(pw->pw_name); - } else { - i_fatal("Couldn't lookup our username (uid=%s)", - dec2str(process_euid)); - } + user = bin_get_user(); env_put(t_strdup_printf("NAMESPACE_1=%s", "maildir:/home/stephan/Maildir")); env_put("NAMESPACE_1_INBOX=1"); @@ -165,8 +128,6 @@ int main(int argc, char **argv) /* Run */ sieve_execute(sbin, &msgdata, &mailenv); - sieve_deinit(); - mail_raw_close(mailr); close(mfd); diff --git a/src/sieve-bin/sieve-test.c b/src/sieve-bin/sieve-test.c index a8afb8471232505aa438a55fb96f68cc94b229f0..1385ba71e4119ef7a7de3398b30fc85a2ac5784a 100644 --- a/src/sieve-bin/sieve-test.c +++ b/src/sieve-bin/sieve-test.c @@ -14,34 +14,25 @@ #include <fcntl.h> #include <pwd.h> - #define DEFAULT_SENDMAIL_PATH "/usr/lib/sendmail" #define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON" int main(int argc, char **argv) { const char *user; - struct passwd *pw; - uid_t process_euid; - int sfd, mfd; + int mfd; pool_t namespaces_pool; struct mail_raw *mailr; struct sieve_binary *sbin; struct sieve_message_data msgdata; struct sieve_mail_environment mailenv; + bin_init(); if ( argc < 2 ) { printf( "Usage: sieve-test <sieve-file> [<mailfile>/-]\n"); exit(1); } - - /* Open sieve script */ - - if ( (sfd = open(argv[1], O_RDONLY)) < 0 ) { - perror("Failed to open sieve script"); - exit(1); - } /* Open mail file */ if ( argc > 2 ) @@ -58,39 +49,12 @@ int main(int argc, char **argv) mfd = 0; /* Compile sieve script */ - - printf("Parsing sieve script '%s'...\n", argv[1]); - - if ( !sieve_init("") ) { - printf("Failed to initialize sieve implementation\n"); - exit(1); - } - - if ( (sbin = sieve_compile(sfd, TRUE)) == NULL ) { - printf("Failed to compile sieve script\n"); - exit(1); - } - - if ( sbin != NULL ) { - struct ostream *dumpstream = o_stream_create_fd(1, 0, FALSE); - - if ( dumpstream != NULL ) { - (void) sieve_dump(sbin, dumpstream); - o_stream_unref(&dumpstream); - } - } - - close(sfd); + sbin = bin_compile_sieve_script(argv[1]); - process_euid = geteuid(); - pw = getpwuid(process_euid); - if (pw != NULL) { - user = t_strdup(pw->pw_name); - } else { - i_fatal("Couldn't lookup our username (uid=%s)", - dec2str(process_euid)); - } - + /* Dump script */ + bin_dump_sieve_binary_to(sbin, "-"); + + user = bin_get_user(); namespaces_pool = namespaces_init(); mail_raw_init(namespaces_pool, user); @@ -111,14 +75,13 @@ int main(int argc, char **argv) /* Run the test */ (void) sieve_test(sbin, &msgdata, &mailenv); - sieve_deinit(); - mail_raw_close(mailr); if ( mfd > 0 ) close(mfd); mail_raw_deinit(); namespaces_deinit(); + bin_deinit(); return 0; } diff --git a/src/sieve-bin/sievec.c b/src/sieve-bin/sievec.c index 4d3208ba6bac86062c51c6c2092bab10b6103935..29d91fec363ee9a706e691882ee422072daa1610 100644 --- a/src/sieve-bin/sievec.c +++ b/src/sieve-bin/sievec.c @@ -7,45 +7,22 @@ #include <stdio.h> #include "lib.h" -#include "str.h" -#include "istream.h" -#include "ostream.h" -#include "buffer.h" - #include "sieve.h" +#include "bin-common.h" + int main(int argc, char **argv) { - int fd; struct sieve_binary *sbin; + bin_init(); + if ( argc < 2 ) { printf( "Usage: sievec <filename>\n"); exit(1); } - if ( (fd = open(argv[1], O_RDONLY)) < 0 ) { - perror("open()"); - exit(1); - } - - printf("Parsing sieve script '%s'...\n", argv[1]); - - if ( sieve_init("") ) { - sbin = sieve_compile(fd, TRUE); - - if ( sbin != NULL ) { - struct ostream *dumpstream = o_stream_create_fd(1, 0, FALSE); - - if ( dumpstream != NULL ) { - (void) sieve_dump(sbin, dumpstream); - o_stream_unref(&dumpstream); - } - } - - sieve_deinit(); - } else { - printf("Failed to initialize sieve implementation."); - } + sbin = bin_compile_sieve_script(argv[1]); + bin_dump_sieve_binary_to(sbin, "-"); - close(fd); + bin_deinit(); }