4 * This module is an managesieve implementation for the Citadel system.
5 * It is compliant with all of the following:
7 * http://tools.ietf.org/html/draft-martin-managesieve-06
8 * as this draft expires with this writing, you might need to search for
20 #include <sys/types.h>
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <libcitadel.h>
44 #include "citserver.h"
53 #include "internet_addressing.h"
54 #include "imap_tools.h" /* Needed for imap_parameterize */
57 #include "clientsocket.h"
58 #include "locate_host.h"
59 #include "citadel_dirs.h"
66 #include "ctdl_module.h"
72 #include "serv_sieve.h"
76 * http://tools.ietf.org/html/draft-martin-managesieve-06
78 * this is the draft this code tries to implement.
83 int command_state; /**< Information about the current session */
84 char *transmitted_message;
85 size_t transmitted_length;
86 char *imap_format_outstring;
87 int imap_outstring_length;
90 enum { /** Command states for login authentication */
98 #define MGSVE ((struct citmgsve *)CC->session_specific_data)
100 /*****************************************************************************/
101 /* MANAGESIEVE Server */
102 /*****************************************************************************/
105 void sieve_outbuf_append(char *str)
107 size_t newlen = strlen(str)+1;
108 size_t oldlen = (MGSVE->imap_format_outstring==NULL)? 0 : strlen(MGSVE->imap_format_outstring)+2;
109 char *buf = malloc ( newlen + oldlen + 10 );
113 sprintf(buf,"%s%s",MGSVE->imap_format_outstring, str);
115 memcpy(buf, str, newlen);
117 if (oldlen != 0) free (MGSVE->imap_format_outstring);
118 MGSVE->imap_format_outstring = buf;
123 * Capability listing. Printed as greeting or on "CAPABILITIES"
124 * see Section 1.8 ; 2.4
126 void cmd_mgsve_caps(void)
128 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve v6.84\"\r\n" /* TODO: put citversion here. */
129 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
131 /* if TLS is already there, should we say that again? */
134 "\"SIEVE\" \"%s\"\r\n"
135 "OK\r\n", msiv_extensions);
140 * Here's where our managesieve session begins its happy day.
142 void managesieve_greeting(void) {
144 strcpy(CC->cs_clientname, "Managesieve session");
146 CC->internal_pgm = 1;
147 CC->cs_flags |= CS_STEALTH;
148 CC->session_specific_data = malloc(sizeof(struct citmgsve));
149 memset(MGSVE, 0, sizeof(struct citmgsve));
154 long GetSizeToken(char * token)
156 char *cursor = token;
159 while (!IsEmptyStr(cursor) &&
164 if (IsEmptyStr(cursor))
167 while ((*cursor != '\0') &&
173 if (cursor[-1] == '+')
176 if (IsEmptyStr(cursor))
182 char *ReadString(long size, char *command)
186 cprintf("NO %s: %ld BAD Message length must be at least 1.\r\n",
191 MGSVE->transmitted_message = malloc(size + 2);
192 if (MGSVE->transmitted_message == NULL) {
193 cprintf("NO %s Cannot allocate memory.\r\n", command);
197 MGSVE->transmitted_length = size;
199 ret = client_read(MGSVE->transmitted_message, size);
200 MGSVE->transmitted_message[size] = '\0';
203 cprintf("%s NO Read failed.\r\n", command);
206 return MGSVE->transmitted_message;
209 /* AUTHENTICATE command; 2.1 */
210 void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u)
212 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
213 /* todo, check length*/
217 char *message = ReadString(GetSizeToken(parms[2]), parms[0]);
219 if (message != NULL) {/**< do we have tokenized login? */
220 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
223 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
225 if (login_ok == CtdlLoginExistingUser(NULL, auth))
228 pass = &(auth[strlen(auth)+1]);
229 /* for some reason the php script sends us the username twice. y? */
230 pass = &(pass[strlen(pass)+1]);
232 if (pass_ok == CtdlTryPassword(pass))
234 MGSVE->command_state = mgsve_password;
241 cprintf("NO \"Authentication Failure.\"\r\n");/* we just support auth plain. */
247 * STARTTLS command chapter 2.2
249 void cmd_mgsve_starttls(void)
250 { /** answer with OK, and fire off tls session. */
252 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
259 *LOGOUT command, see chapter 2.3
261 void cmd_mgsve_logout(struct sdm_userdata *u)
264 lprintf(CTDL_NOTICE, "MgSve bye.");
270 * HAVESPACE command. see chapter 2.5
272 void cmd_mgsve_havespace(void)
274 /* as we don't have quotas in citadel we should always answer with OK;
275 * pherhaps we should have a max-scriptsize.
277 if (MGSVE->command_state != mgsve_password)
285 /* citadel doesn't have quotas. in case of change, please add code here. */
291 * PUTSCRIPT command, see chapter 2.6
293 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
295 /* "scriptname" {nnn+} */
296 /* AFTER we have the whole script overwrite existing scripts */
297 /* spellcheck the script before overwrite old ones, and reply with "no" */
304 if (parms[1][0]=='"')
305 ScriptName = &parms[1][1];
307 ScriptName = parms[1];
309 slength = strlen (ScriptName);
311 if (ScriptName[slength] == '"')
312 ScriptName[slength] = '\0';
314 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
316 if (Script == NULL) return;
318 // TODO: do we spellcheck?
319 msiv_putscript(u, ScriptName, Script);
323 cprintf("%s NO Read failed.\r\n", parms[0]);
336 * LISTSCRIPT command. see chapter 2.7
338 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
341 struct sdm_script *s;
344 MGSVE->imap_format_outstring = NULL;
345 for (s=u->first_script; s!=NULL; s=s->next) {
346 if (s->script_content != NULL) {
347 cprintf("\"%s\"%s\r\n",
349 (s->script_active)?" ACTIVE":"");
358 * \brief SETACTIVE command. see chapter 2.8
360 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
364 if (msiv_setactive(u, parms[1]) == 0) {
368 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
371 cprintf("NO \"unexpected parameters.\"\r\n");
377 * \brief GETSCRIPT command. see chapter 2.9
379 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
382 char *script_content;
385 script_content = msiv_getscript(u, parms[1]);
386 if (script_content != NULL){
389 slen = strlen(script_content);
390 outbuf = malloc (slen + 64);
391 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
395 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
398 cprintf("NO \"unexpected parameters.\"\r\n");
403 * \brief DELETESCRIPT command. see chapter 2.10
405 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
410 i = msiv_deletescript(u, parms[1]);
416 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
419 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
423 cprintf("NO \"unexpected parameters.\"\r\n");
430 * \brief Attempt to perform authenticated managesieve
432 void mgsve_auth(char *argbuf) {
433 char username_prompt[64];
435 char encoded_authstring[1024];
438 cprintf("NO \"Already logged in.\"\r\n");
442 extract_token(method, argbuf, 0, ' ', sizeof method);
444 if (!strncasecmp(method, "login", 5) ) {
445 if (strlen(argbuf) >= 7) {
448 CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
449 cprintf("334 %s\r\n", username_prompt);
454 if (!strncasecmp(method, "plain", 5) ) {
455 if (num_tokens(argbuf, ' ') < 2) {
459 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
463 if (strncasecmp(method, "login", 5) ) {
464 cprintf("NO \"Unknown authentication method.\"\r\n");
473 * implements the STARTTLS command (Citadel API version)
475 void _mgsve_starttls(void)
477 char ok_response[SIZ];
478 char nosup_response[SIZ];
479 char error_response[SIZ];
482 "200 2.0.0 Begin TLS negotiation now\r\n");
483 sprintf(nosup_response,
484 "554 5.7.3 TLS not supported here\r\n");
485 sprintf(error_response,
486 "554 5.7.3 Internal error\r\n");
487 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
492 * Main command loop for managesieve sessions.
494 void managesieve_command_loop(void) {
499 struct sdm_userdata u;
500 int changes_made = 0;
502 memset(&u, 0, sizeof(struct sdm_userdata));
505 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
506 length = client_getln(cmdbuf, sizeof cmdbuf);
508 num_parms = imap_parameterize(parms, cmdbuf);
509 if (num_parms == 0) return;
510 length = strlen(parms[0]);
513 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
517 lprintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
518 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
519 cmd_mgsve_auth(num_parms, parms, &u);
523 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
524 cmd_mgsve_starttls();
527 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
528 cmd_mgsve_logout(&u);
530 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
533 /** these commands need to be authenticated. throw it out if it tries. */
534 else if (!CtdlAccessCheck(ac_logged_in))
537 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
538 cmd_mgsve_havespace();
540 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
541 cmd_mgsve_putscript(num_parms, parms, &u);
544 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
545 cmd_mgsve_listscript(num_parms, parms,&u);
547 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
548 cmd_mgsve_setactive(num_parms, parms,&u);
551 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
552 cmd_mgsve_getscript(num_parms, parms, &u);
554 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
555 cmd_mgsve_deletescript(num_parms, parms, &u);
558 msiv_store(&u, changes_made);
562 lprintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
570 * This cleanup function blows away the temporary memory and files used by
573 void managesieve_cleanup_function(void) {
575 /* Don't do this stuff if this is not a managesieve session! */
576 if (CC->h_command_function != managesieve_command_loop) return;
578 lprintf(CTDL_DEBUG, "Performing managesieve cleanup hook\n");
584 #endif /* HAVE_LIBSIEVE */
585 const char* CitadelServiceManageSieve = "ManageSieve";
586 CTDL_MODULE_INIT(managesieve)
591 CtdlRegisterServiceHook(config.c_managesieve_port,
593 managesieve_greeting,
594 managesieve_command_loop,
596 CitadelServiceManageSieve);
597 CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
599 #else /* HAVE_LIBSIEVE */
601 lprintf(CTDL_INFO, "This server is missing libsieve. Managesieve protocol is disabled..\n");
603 #endif /* HAVE_LIBSIEVE */
606 /* return our Subversion id for the Log */