Moved to new module init structure.
[citadel.git] / citadel / serv_sieve.c
index b8af8e574953ae2969e0bc66eb6a938357c41468..508f8d2188ce71ecf06889f181de4704f235b7db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $
+ * $Id$
  *
  * This module glues libSieve to the Citadel server in order to implement
  * the Sieve mailbox filtering language (RFC 3028).
@@ -12,7 +12,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
-#include <signal.h>
+#include <ctype.h>
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "room_ops.h"
 #include "policy.h"
 #include "database.h"
 #include "internet_addressing.h"
 #include "tools.h"
 
+
+#include "ctdl_module.h"
+
+
 #ifdef HAVE_LIBSIEVE
 
 #include "serv_sieve.h"
@@ -55,23 +57,10 @@ char *msiv_extensions = NULL;
 
 /*
  * Callback function to send libSieve trace messages to Citadel log facility
- * Set ctdl_libsieve_debug=1 to see extremely verbose libSieve trace
  */
 int ctdl_debug(sieve2_context_t *s, void *my)
 {
-       static int ctdl_libsieve_debug = 0;
-
-       if (ctdl_libsieve_debug) {
-/*
-               lprintf(CTDL_DEBUG, "Sieve: level [%d] module [%s] file [%s] function [%s]\n",
-                       sieve2_getvalue_int(s, "level"),
-                       sieve2_getvalue_string(s, "module"),
-                       sieve2_getvalue_string(s, "file"),
-                       sieve2_getvalue_string(s, "function"));
- */
-               lprintf(CTDL_DEBUG, "Sieve: %s\n",
-                       sieve2_getvalue_string(s, "message"));
-       }
+       lprintf(CTDL_DEBUG, "Sieve: %s\n", sieve2_getvalue_string(s, "message"));
        return SIEVE2_OK;
 }
 
@@ -122,20 +111,20 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
        }
        if (valid->num_error > 0) {
                lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
-               free(valid);
+               free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
 
        msg = CtdlFetchMessage(cs->msgnum, 1);
        if (msg == NULL) {
                lprintf(CTDL_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
-               free(valid);
+               free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
 
        CtdlSubmitMsg(msg, valid, NULL);
        cs->cancel_implicit_keep = 1;
-       free(valid);
+       free_recipients(valid);
        CtdlFreeMessage(msg);
        return SIEVE2_OK;
 }
@@ -184,12 +173,12 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        c = getroom(&CC->room, foldername);
 
        /* Then a regular room name match (public and private rooms) */
-       if (c < 0) {
+       if (c != 0) {
                safestrncpy(foldername, dest_folder, sizeof foldername);
                c = getroom(&CC->room, foldername);
        }
 
-       if (c < 0) {
+       if (c != 0) {
                lprintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
                return SIEVE2_ERROR_BADARGS;
        }
@@ -462,7 +451,6 @@ int ctdl_getheaders(sieve2_context_t *s, void *my) {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
 
        lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
-
        sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
        return SIEVE2_OK;
 }
@@ -483,6 +471,7 @@ void sieve_queue_room(struct ctdlroom *which_room) {
        ptr->next = sieve_list;
        sieve_list = ptr;
        end_critical_section(S_SIEVELIST);
+       lprintf(CTDL_DEBUG, "<%s> queued for Sieve processing\n", which_room->QRname);
 }
 
 
@@ -496,21 +485,39 @@ void sieve_do_msg(long msgnum, void *userdata) {
        struct ctdl_sieve my;
        int res;
        struct CtdlMessage *msg;
+       int i;
+       size_t headers_len = 0;
+       int len = 0;
 
        lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
 
        msg = CtdlFetchMessage(msgnum, 0);
        if (msg == NULL) return;
 
+       /*
+        * Grab the message headers so we can feed them to libSieve.
+        */
        CC->redirect_buffer = malloc(SIZ);
        CC->redirect_len = 0;
        CC->redirect_alloc = SIZ;
        CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
        my.rfc822headers = CC->redirect_buffer;
+       headers_len = CC->redirect_len;
        CC->redirect_buffer = NULL;
        CC->redirect_len = 0;
        CC->redirect_alloc = 0;
 
+       /*
+        * libSieve clobbers the stack if it encounters badly formed
+        * headers.  Sanitize our headers by stripping nonprintable
+        * characters.
+        */
+       for (i=0; i<headers_len; ++i) {
+               if (!isascii(my.rfc822headers[i])) {
+                       my.rfc822headers[i] = '_';
+               }
+       }
+
        my.keep = 0;                            /* Set to 1 to declare an *explicit* keep */
        my.cancel_implicit_keep = 0;            /* Some actions will cancel the implicit keep */
        my.usernum = atol(CC->room.QRname);     /* Keep track of the owner of the room's namespace */
@@ -553,15 +560,40 @@ void sieve_do_msg(long msgnum, void *userdata) {
                strcpy(my.envelope_from, "");
        }
 
-       /* Keep track of the envelope-to address */
+       len = strlen(my.envelope_from);
+       for (i=0; i<len; ++i) {
+               if (isspace(my.envelope_from[i])) my.envelope_from[i] = '_';
+       }
+       if (haschar(my.envelope_from, '@') == 0) {
+               strcat(my.envelope_from, "@");
+               strcat(my.envelope_from, config.c_fqdn);
+       }
+
+       /* Keep track of the envelope-to address (use body-to if not found) */
        if (msg->cm_fields['V'] != NULL) {
                safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
        }
+       else if (msg->cm_fields['R'] != NULL) {
+               safestrncpy(my.envelope_to, msg->cm_fields['R'], sizeof my.envelope_to);
+               if (msg->cm_fields['D'] != NULL) {
+                       strcat(my.envelope_to, "@");
+                       strcat(my.envelope_to, msg->cm_fields['D']);
+               }
+       }
        else {
                strcpy(my.envelope_to, "");
        }
 
-       free(msg);
+       len = strlen(my.envelope_to);
+       for (i=0; i<len; ++i) {
+               if (isspace(my.envelope_to[i])) my.envelope_to[i] = '_';
+       }
+       if (haschar(my.envelope_to, '@') == 0) {
+               strcat(my.envelope_to, "@");
+               strcat(my.envelope_to, config.c_fqdn);
+       }
+
+       CtdlFreeMessage(msg);
 
        sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
        
@@ -580,7 +612,7 @@ void sieve_do_msg(long msgnum, void *userdata) {
         */
        if ( (!my.keep) && (my.cancel_implicit_keep) ) {
                lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
-               CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
+               CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
        }
 
        lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
@@ -684,9 +716,10 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
        char *text;
        struct sdm_script *sptr;
        struct sdm_vacation *vptr;
-
+       size_t tsize;
 
        text = malloc(1024);
+       tsize = 1024;
        snprintf(text, 1024,
                "Content-type: application/x-citadel-sieve-config\n"
                "\n"
@@ -698,7 +731,10 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
        );
 
        while (u->first_script != NULL) {
-               text = realloc(text, strlen(text) + strlen(u->first_script->script_content) + 256);
+               size_t tlen;
+               tlen = strlen(text);
+               tsize = tlen + strlen(u->first_script->script_content) +256;
+               text = realloc(text, tsize);
                sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
                        u->first_script->script_name,
                        u->first_script->script_active,
@@ -712,11 +748,11 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
 
        if (u->first_vacation != NULL) {
 
-               size_t realloc_len = strlen(text) + 256;
+               tsize = strlen(text) + 256;
                for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
-                       realloc_len += strlen(vptr->fromaddr + 32);
+                       tsize += strlen(vptr->fromaddr + 32);
                }
-               text = realloc(text, realloc_len);
+               text = realloc(text, tsize);
 
                sprintf(&text[strlen(text)], "vacation|\n");
                while (u->first_vacation != NULL) {
@@ -739,10 +775,11 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
                        4,
                        "Sieve configuration"
        );
-
+       
+       free (text);
        /* And delete the old one */
        if (u->config_msgnum > 0) {
-               CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "", 0);
+               CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "");
        }
 
 }
@@ -1204,21 +1241,35 @@ BAIL:   res = sieve2_free(&sieve2_context);
 
 }
 
+int serv_sieve_room(struct ctdlroom *room)
+{
+       if (!strcasecmp(&room->QRname[11], MAILROOM)) {
+               sieve_queue_room(room);
+       }
+       return 0;
+}
 
+#endif /* HAVE_LIBSIEVE */
 
-char *serv_sieve_init(void)
+CTDL_MODULE_INIT(sieve)
 {
+
+#ifdef HAVE_LIBSIEVE
+
        ctdl_sieve_init();
        CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
-       return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
-}
+
+        CtdlRegisterRoomHook(serv_sieve_room);
+
+        CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE);
 
 #else  /* HAVE_LIBSIEVE */
 
-char *serv_sieve_init(void)
-{
        lprintf(CTDL_INFO, "This server is missing libsieve.  Mailbox filtering will be disabled.\n");
-       return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
-}
 
 #endif /* HAVE_LIBSIEVE */
+
+        /* return our Subversion id for the Log */
+       return "$Id$";
+}
+