2 * $Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $
4 * This module glues libSieve to the Citadel server in order to implement
5 * the Sieve mailbox filtering language (RFC 3028).
7 * This code is released under the terms of the GNU General Public License.
18 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
36 #include "sysdep_decls.h"
37 #include "citserver.h"
40 #include "serv_extensions.h"
50 #include <sieve2_error.h>
51 #include "serv_sieve.h"
53 struct RoomProcList *sieve_list = NULL;
57 int actiontaken; /* Set to 1 if the message was successfully acted upon */
58 int keep; /* Set to 1 to suppress message deletion from the inbox */
59 long usernum; /* Owner of the mailbox we're processing */
60 long msgnum; /* Message base ID of the message being processed */
65 * Callback function to send libSieve trace messages to Citadel log facility
66 * Set ctdl_libsieve_debug=1 to see extremely verbose libSieve trace
68 int ctdl_debug(sieve2_context_t *s, void *my)
70 static int ctdl_libsieve_debug = 0;
72 if (ctdl_libsieve_debug) {
73 lprintf(CTDL_DEBUG, "Sieve: level [%d] module [%s] file [%s] function [%s]\n",
74 sieve2_getvalue_int(s, "level"),
75 sieve2_getvalue_string(s, "module"),
76 sieve2_getvalue_string(s, "file"),
77 sieve2_getvalue_string(s, "function"));
78 lprintf(CTDL_DEBUG, " message [%s]\n",
79 sieve2_getvalue_string(s, "message"));
86 * Callback function to log script parsing errors
88 int ctdl_errparse(sieve2_context_t *s, void *my)
90 lprintf(CTDL_WARNING, "Error in script, line %d: %s\n",
91 sieve2_getvalue_int(s, "lineno"),
92 sieve2_getvalue_string(s, "message")
99 * Callback function to log script execution errors
101 int ctdl_errexec(sieve2_context_t *s, void *my)
103 lprintf(CTDL_WARNING, "Error executing script: %s\n",
104 sieve2_getvalue_string(s, "message")
111 * Callback function to redirect a message to a different folder
113 int ctdl_redirect(sieve2_context_t *s, void *my)
115 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
116 struct CtdlMessage *msg = NULL;
117 struct recptypes *valid = NULL;
120 safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
122 lprintf(CTDL_DEBUG, "Action is REDIRECT, recipient <%s>\n", recp);
124 valid = validate_recipients(recp);
126 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
127 return SIEVE2_ERROR_BADARGS;
129 if (valid->num_error > 0) {
130 lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
132 return SIEVE2_ERROR_BADARGS;
135 msg = CtdlFetchMessage(cs->msgnum, 1);
137 lprintf(CTDL_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
139 return SIEVE2_ERROR_BADARGS;
142 CtdlSubmitMsg(msg, valid, NULL);
145 CtdlFreeMessage(msg);
151 * Callback function to indicate that a message *will* be kept in the inbox
153 int ctdl_keep(sieve2_context_t *s, void *my)
155 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
157 lprintf(CTDL_DEBUG, "Action is KEEP\n");
166 * Callback function to file a message into a different mailbox
168 int ctdl_fileinto(sieve2_context_t *s, void *my)
170 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
171 const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
173 char foldername[256];
174 char original_room_name[ROOMNAMELEN];
176 lprintf(CTDL_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
178 /* FILEINTO 'INBOX' is the same thing as KEEP */
179 if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
185 /* Remember what room we came from */
186 safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
188 /* First try a mailbox name match (check personal mail folders first) */
189 snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
190 c = getroom(&CC->room, foldername);
192 /* Then a regular room name match (public and private rooms) */
194 safestrncpy(foldername, dest_folder, sizeof foldername);
195 c = getroom(&CC->room, foldername);
199 lprintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
200 return SIEVE2_ERROR_BADARGS;
203 /* Yes, we actually have to go there */
204 usergoto(NULL, 0, 0, NULL, NULL);
206 c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL);
208 /* Go back to the room we came from */
209 if (strcasecmp(original_room_name, CC->room.QRname)) {
210 usergoto(original_room_name, 0, 0, NULL, NULL);
218 return SIEVE2_ERROR_BADARGS;
224 * Callback function to indicate that a message should be rejected
225 * FIXME implement this
227 int ctdl_reject(sieve2_context_t *s, void *my)
229 lprintf(CTDL_DEBUG, "Action is REJECT\n");
230 return SIEVE2_ERROR_UNSUPPORTED;
235 * Callback function to indicate that the user should be notified
236 * FIXME implement this
238 int ctdl_notify(sieve2_context_t *s, void *my)
240 lprintf(CTDL_DEBUG, "Action is NOTIFY\n");
241 return SIEVE2_ERROR_UNSUPPORTED;
246 * Callback function to indicate that a vacation message should be generated
247 * FIXME implement this
249 int ctdl_vacation(sieve2_context_t *s, void *my)
251 lprintf(CTDL_DEBUG, "Action is VACATION\n");
252 return SIEVE2_ERROR_UNSUPPORTED;
257 * Callback function to parse addresses per local system convention
258 * FIXME implement this
260 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
262 return SIEVE2_ERROR_UNSUPPORTED;
267 * Callback function to parse message envelope
268 * FIXME implement this
270 int ctdl_getenvelope(sieve2_context_t *s, void *my)
272 return SIEVE2_ERROR_UNSUPPORTED;
277 * Callback function to fetch message body
278 * FIXME implement this
280 int ctdl_getbody(sieve2_context_t *s, void *my)
282 return SIEVE2_ERROR_UNSUPPORTED;
287 * Callback function to fetch message size
288 * FIXME implement this
290 int ctdl_getsize(sieve2_context_t *s, void *my)
292 return SIEVE2_ERROR_UNSUPPORTED;
297 * Callback function to indicate that a message should be discarded.
299 int ctdl_discard(sieve2_context_t *s, void *my)
301 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
303 lprintf(CTDL_DEBUG, "Action is DISCARD\n");
305 /* Yes, this is really all there is to it. Since we are not setting "keep" to 1,
306 * the message will be discarded because "some other action" was successfully taken.
315 * Callback function to retrieve the sieve script
317 int ctdl_getscript(sieve2_context_t *s, void *my) {
319 lprintf(CTDL_DEBUG, "ctdl_getscript() was called\n");
321 sieve2_setvalue_string(s, "script",
323 "require \"fileinto\"; \n"
324 " if header :contains [\"Subject\"] [\"frobnitz\"] { \n"
325 " redirect \"foo@example.com\"; \n"
326 " } elsif header :contains \"Subject\" \"XYZZY\" { \n"
327 " fileinto \"plugh\"; \n"
338 * Callback function to retrieve message headers
340 int ctdl_getheaders(sieve2_context_t *s, void *my) {
342 struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
344 lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
346 sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
353 * Add a room to the list of those rooms which potentially require sieve processing
355 void sieve_queue_room(struct ctdlroom *which_room) {
356 struct RoomProcList *ptr;
358 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
359 if (ptr == NULL) return;
361 safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
362 begin_critical_section(S_SIEVELIST);
363 ptr->next = sieve_list;
365 end_critical_section(S_SIEVELIST);
370 * We need these structs to pass a bunch of information
371 * between sieve_do_msg() and sieve_do_room()
375 struct sdm_script *next;
376 char script_name[256];
378 char *script_content;
381 struct sdm_userdata {
382 sieve2_context_t *sieve2_context; /**< for libsieve's use */
383 long config_msgnum; /**< confirms that a sieve config was located */
384 char config_roomname[ROOMNAMELEN];
385 long lastproc; /**< last message processed */
386 struct sdm_script *first_script;
391 * Perform sieve processing for one message (called by sieve_do_room() for each message)
393 void sieve_do_msg(long msgnum, void *userdata) {
394 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
395 sieve2_context_t *sieve2_context = u->sieve2_context;
396 struct ctdl_sieve my;
399 lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
401 CC->redirect_buffer = malloc(SIZ);
402 CC->redirect_len = 0;
403 CC->redirect_alloc = SIZ;
404 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ONLY, 0, 1, NULL);
405 my.rfc822headers = CC->redirect_buffer;
406 CC->redirect_buffer = NULL;
407 CC->redirect_len = 0;
408 CC->redirect_alloc = 0;
410 my.keep = 0; /* Don't keep a copy in the inbox unless a callback tells us to do so */
411 my.actiontaken = 0; /* Keep track of whether any actions were successfully taken */
412 my.usernum = atol(CC->room.QRname); /* Keep track of the owner of the room's namespace */
413 my.msgnum = msgnum; /* Keep track of the message number in our local store */
415 sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
417 lprintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
418 res = sieve2_execute(sieve2_context, &my);
419 if (res != SIEVE2_OK) {
420 lprintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
423 free(my.rfc822headers);
424 my.rfc822headers = NULL;
427 * Delete the message from the inbox unless either we were told not to, or
428 * if no other action was successfully taken.
430 if ( (!my.keep) && (my.actiontaken) ) {
431 lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
432 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
435 lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
436 u->lastproc = msgnum;
444 * Given the on-disk representation of our Sieve config, load
445 * it into an in-memory data structure.
447 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
452 while (ptr = bmstrcasestr(conf, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
454 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
456 lprintf(CTDL_DEBUG, "CONFIG: <%s>\n", c);
458 /* FIXME finish this */
464 * We found the Sieve configuration for this user.
465 * Now do something with it.
467 void get_sieve_config_backend(long msgnum, void *userdata) {
468 struct sdm_userdata *u = (struct sdm_userdata *) userdata;
469 struct CtdlMessage *msg;
472 u->config_msgnum = msgnum;
473 msg = CtdlFetchMessage(u->config_msgnum, 1);
475 u->config_msgnum = (-1) ;
479 conf = msg->cm_fields['M'];
480 msg->cm_fields['M'] = NULL;
481 CtdlFreeMessage(msg);
484 parse_sieve_config(conf, u);
491 * Write our citadel sieve config back to disk
493 void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
497 "Content-type: application/x-citadel-sieve-config\n"
501 /* Save the config */
502 quickie_message("Citadel", NULL, u->config_roomname,
505 "Sieve configuration"
508 /* And delete the old one */
509 CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "", 0);
516 * Perform sieve processing for a single room
518 void sieve_do_room(char *roomname) {
520 struct sdm_userdata u;
521 sieve2_context_t *sieve2_context = NULL; /* Context for sieve parser */
522 int res; /* Return code from libsieve calls */
523 long orig_lastproc = 0;
526 * This is our callback registration table for libSieve.
528 sieve2_callback_t ctdl_sieve_callbacks[] = {
529 { SIEVE2_ACTION_REJECT, ctdl_reject },
530 { SIEVE2_ACTION_NOTIFY, ctdl_notify },
531 { SIEVE2_ACTION_VACATION, ctdl_vacation },
532 { SIEVE2_ERRCALL_PARSE, ctdl_errparse },
533 { SIEVE2_ERRCALL_RUNTIME, ctdl_errexec },
534 { SIEVE2_ACTION_FILEINTO, ctdl_fileinto },
535 { SIEVE2_ACTION_REDIRECT, ctdl_redirect },
536 { SIEVE2_ACTION_DISCARD, ctdl_discard },
537 { SIEVE2_ACTION_KEEP, ctdl_keep },
538 { SIEVE2_SCRIPT_GETSCRIPT, ctdl_getscript },
539 { SIEVE2_DEBUG_TRACE, ctdl_debug },
540 { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders },
541 { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress },
542 { SIEVE2_MESSAGE_GETENVELOPE, ctdl_getenvelope },
543 { SIEVE2_MESSAGE_GETBODY, ctdl_getbody },
544 { SIEVE2_MESSAGE_GETSIZE, ctdl_getsize },
548 /* See if the user who owns this 'mailbox' has any Sieve scripts that
551 snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), SIEVERULES);
552 if (getroom(&CC->room, u.config_roomname) != 0) {
553 lprintf(CTDL_DEBUG, "<%s> does not exist. No processing is required.\n", u.config_roomname);
558 * Find the sieve scripts and control record and do something
560 u.config_msgnum = (-1);
561 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
562 get_sieve_config_backend, (void *)&u );
564 if (u.config_msgnum < 0) {
565 lprintf(CTDL_DEBUG, "No Sieve rules exist. No processing is required.\n");
569 lprintf(CTDL_DEBUG, "Rules found. Performing Sieve processing for <%s>\n", roomname);
571 if (getroom(&CC->room, roomname) != 0) {
572 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
576 /* Initialize the Sieve parser */
578 res = sieve2_alloc(&sieve2_context);
579 if (res != SIEVE2_OK) {
580 lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
584 res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
585 if (res != SIEVE2_OK) {
586 lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
590 /* Validate the script */
592 res = sieve2_validate(sieve2_context, NULL);
593 if (res != SIEVE2_OK) {
594 lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
598 /* Do something useful */
599 u.sieve2_context = sieve2_context;
600 orig_lastproc = u.lastproc;
601 CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL,
607 res = sieve2_free(&sieve2_context);
608 if (res != SIEVE2_OK) {
609 lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
612 /* Rewrite the config if we have to */
613 if (u.lastproc > orig_lastproc) {
614 rewrite_ctdl_sieve_config(&u);
620 * Perform sieve processing for all rooms which require it
622 void perform_sieve_processing(void) {
623 struct RoomProcList *ptr = NULL;
625 if (sieve_list != NULL) {
626 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
627 while (sieve_list != NULL) {
628 char spoolroomname[ROOMNAMELEN];
629 safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
630 begin_critical_section(S_SIEVELIST);
632 /* pop this record off the list */
634 sieve_list = sieve_list->next;
637 /* invalidate any duplicate entries to prevent double processing */
638 for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
639 if (!strcasecmp(ptr->name, spoolroomname)) {
644 end_critical_section(S_SIEVELIST);
645 if (spoolroomname[0] != 0) {
646 sieve_do_room(spoolroomname);
655 * We don't really care about dumping the entire credits to the log
656 * every time the server is initialized. The documentation will suffice
657 * for that purpose. We are making a call to sieve2_credits() in order
658 * to demonstrate that we have successfully linked in to libsieve.
660 void log_the_sieve2_credits(void) {
663 cred = strdup(sieve2_credits());
664 if (cred == NULL) return;
666 if (strlen(cred) > 60) {
667 strcpy(&cred[55], "...");
670 lprintf(CTDL_INFO, "%s\n",cred);
675 char *serv_sieve_init(void)
677 log_the_sieve2_credits();
678 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
681 #else /* HAVE_LIBSIEVE */
683 char *serv_sieve_init(void)
685 lprintf(CTDL_INFO, "This server is missing libsieve. Mailbox filtering will be disabled.\n");
686 return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
689 #endif /* HAVE_LIBSIEVE */