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>
43 #include "sysdep_decls.h"
44 #include "citserver.h"
48 #include "serv_extensions.h"
55 #include "internet_addressing.h"
56 #include "imap_tools.h"
59 #include "clientsocket.h"
60 #include "locate_host.h"
61 #include "citadel_dirs.h"
64 #include "serv_crypto.h"
73 #include "serv_sieve.h"
77 * http://tools.ietf.org/html/draft-martin-managesieve-06
79 * this is the draft this code tries to implement.
84 int command_state; /**< Information about the current session */
85 char *transmitted_message;
86 size_t transmitted_length;
87 char *imap_format_outstring;
88 int imap_outstring_length;
91 enum { /** Command states for login authentication */
99 #define MGSVE CC->MGSVE
101 /*****************************************************************************/
102 /* MANAGESIEVE Server */
103 /*****************************************************************************/
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 MGSVE = malloc(sizeof(struct citmgsve));
149 memset(MGSVE, 0, sizeof(struct citmgsve));
154 long GetSizeToken(char * token)
156 char *cursor = token;
159 while ((*cursor != '\0') &&
167 while ((*cursor != '\0') &&
173 if (cursor[-1] == '+')
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. */
248 * STARTTLS command chapter 2.2
250 void cmd_mgsve_starttls(void)
251 { /** answer with OK, and fire off tls session. */
253 CtdlStartTLS(NULL, NULL, NULL);
261 *LOGOUT command, see chapter 2.3
263 void cmd_mgsve_logout(struct sdm_userdata *u)
266 lprintf(CTDL_NOTICE, "MgSve bye.");
272 * HAVESPACE command. see chapter 2.5
274 void cmd_mgsve_havespace(void)
276 /* as we don't have quotas in citadel we should always answer with OK;
277 * pherhaps we should have a max-scriptsize.
279 if (MGSVE->command_state != mgsve_password)
287 /* citadel doesn't have quotas. in case of change, please add code here. */
293 * PUTSCRIPT command, see chapter 2.6
295 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
297 /* "scriptname" {nnn+} */
298 /* AFTER we have the whole script overwrite existing scripts */
299 /* spellcheck the script before overwrite old ones, and reply with "no" */
306 if (parms[1][0]=='"')
307 ScriptName = &parms[1][1];
309 ScriptName = parms[1];
311 slength = strlen (ScriptName);
313 if (ScriptName[slength] == '"')
314 ScriptName[slength] = '\0';
316 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
318 if (Script == NULL) return;
320 // TODO: do we spellcheck?
321 msiv_putscript(u, ScriptName, Script);
325 cprintf("%s NO Read failed.\r\n", parms[0]);
338 * LISTSCRIPT command. see chapter 2.7
340 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
343 struct sdm_script *s;
346 MGSVE->imap_format_outstring = NULL;
347 for (s=u->first_script; s!=NULL; s=s->next) {
348 if (s->script_content != NULL) {
349 cprintf("\"%s\"%s\r\n",
351 (s->script_active)?" ACTIVE":"");
360 * \brief SETACTIVE command. see chapter 2.8
362 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
366 if (msiv_setactive(u, parms[1]) == 0) {
370 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
373 cprintf("NO \"unexpected parameters.\"\r\n");
379 * \brief GETSCRIPT command. see chapter 2.9
381 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
384 char *script_content;
387 script_content = msiv_getscript(u, parms[1]);
388 if (script_content != NULL){
391 slen = strlen(script_content);
392 outbuf = malloc (slen + 64);
393 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
397 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
400 cprintf("NO \"unexpected parameters.\"\r\n");
405 * \brief DELETESCRIPT command. see chapter 2.10
407 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
412 i = msiv_deletescript(u, parms[1]);
418 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
421 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
425 cprintf("NO \"unexpected parameters.\"\r\n");
432 * \brief Attempt to perform authenticated managesieve
434 void mgsve_auth(char *argbuf) {
435 char username_prompt[64];
437 char encoded_authstring[1024];
440 cprintf("NO \"Already logged in.\"\r\n");
444 extract_token(method, argbuf, 0, ' ', sizeof method);
446 if (!strncasecmp(method, "login", 5) ) {
447 if (strlen(argbuf) >= 7) {
450 CtdlEncodeBase64(username_prompt, "Username:", 9);
451 cprintf("334 %s\r\n", username_prompt);
456 if (!strncasecmp(method, "plain", 5) ) {
457 if (num_tokens(argbuf, ' ') < 2) {
461 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
465 if (strncasecmp(method, "login", 5) ) {
466 cprintf("NO \"Unknown authentication method.\"\r\n");
475 * implements the STARTTLS command (Citadel API version)
478 void _mgsve_starttls(void)
480 char ok_response[SIZ];
481 char nosup_response[SIZ];
482 char error_response[SIZ];
485 "200 2.0.0 Begin TLS negotiation now\r\n");
486 sprintf(nosup_response,
487 "554 5.7.3 TLS not supported here\r\n");
488 sprintf(error_response,
489 "554 5.7.3 Internal error\r\n");
490 CtdlStartTLS(ok_response, nosup_response, error_response);
496 * Main command loop for managesieve sessions.
498 void managesieve_command_loop(void) {
503 struct sdm_userdata u;
504 int changes_made = 0;
506 memset(&u, 0, sizeof(struct sdm_userdata));
509 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
510 length = client_getln(cmdbuf, sizeof cmdbuf);
512 num_parms = imap_parameterize(parms, cmdbuf);
513 if (num_parms == 0) return;
514 length = strlen(parms[0]);
517 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
521 lprintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
522 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
523 cmd_mgsve_auth(num_parms, parms, &u);
527 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
528 cmd_mgsve_starttls();
531 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
532 cmd_mgsve_logout(&u);
534 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
537 /** these commands need to be authenticated. throw it out if it tries. */
538 else if (!CtdlAccessCheck(ac_logged_in))
541 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
542 cmd_mgsve_havespace();
544 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
545 cmd_mgsve_putscript(num_parms, parms, &u);
548 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
549 cmd_mgsve_listscript(num_parms, parms,&u);
551 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
552 cmd_mgsve_setactive(num_parms, parms,&u);
555 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
556 cmd_mgsve_getscript(num_parms, parms, &u);
558 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
559 cmd_mgsve_deletescript(num_parms, parms, &u);
562 msiv_store(&u, changes_made);
566 lprintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
575 char *serv_managesieve_init(void)
578 CtdlRegisterServiceHook(config.c_managesieve_port, /* MGSVE */
580 managesieve_greeting,
581 managesieve_command_loop,
587 #else /* HAVE_LIBSIEVE */
589 char *serv_managesieve_init(void)
591 lprintf(CTDL_INFO, "This server is missing libsieve. Managesieve protocol is disabled..\n");
595 #endif /* HAVE_LIBSIEVE */