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\" \"FILEINTO VACATION\"\r\n" /* TODO: print sieve extensions here. */
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 /* TODO: compare "digest-md5" or "gssapi" and answer with "NO" */
213 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
214 /* todo, check length*/
219 /* todo: how to do plain auth? */
220 char *message = ReadString(GetSizeToken(parms[2]), parms[0]);
222 if (message != NULL) {/* do we have tokenized login? */
223 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
226 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
228 if (login_ok == CtdlLoginExistingUser(auth))
231 pass = &(auth[strlen(auth)+1]);
232 /* for some reason the php script sends us the username twice. y? */
233 pass = &(pass[strlen(pass)+1]);
235 if (pass_ok == CtdlTryPassword(pass))
237 MGSVE->command_state = mgsve_password;
244 cprintf("NO\r\n");/* we just support auth plain. */
250 /* STARTTLS command chapter 2.2 */
251 void cmd_mgsve_starttls(void)
252 { /* answer with OK, and fire off tls session. */
254 CtdlStartTLS(NULL, NULL, NULL);
261 /* LOGOUT command, see chapter 2.3 */
262 void cmd_mgsve_logout(struct sdm_userdata *u)
263 {/* send "OK" and terminate the connection. */
265 lprintf(CTDL_NOTICE, "MgSve bye.");
270 /* HAVESPACE command. see chapter 2.5 */
271 void cmd_mgsve_havespace(void)
273 /* TODO answer NO in any case if auth is missing. */
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. */
290 /* PUTSCRIPT command, see chapter 2.6 */
291 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
293 /* "scriptname" {nnn+} */
294 /* TODO: answer with "NO" instant, if we're unauthorized. */
295 /* AFTER we have the whole script overwrite existing scripts */
296 /* spellcheck the script before overwrite old ones, and reply with "no" */
303 if (parms[1][0]=='"')
304 ScriptName = &parms[1][1];
306 ScriptName = parms[1];
308 slength = strlen (ScriptName);
310 if (ScriptName[slength] == '"')
311 ScriptName[slength] = '\0';
313 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
315 if (Script == NULL) return;
317 // TODO: do we spellcheck?
318 msiv_putscript(u, ScriptName, Script);
322 cprintf("%s NO Read failed.\r\n", parms[0]);
334 /* LISTSCRIPT command. see chapter 2.7 */
335 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
338 struct sdm_script *s;
341 MGSVE->imap_format_outstring = NULL;
342 for (s=u->first_script; s!=NULL; s=s->next) {
343 if (s->script_content != NULL) {
344 cprintf("\"%s\"%s\r\n",
346 (s->script_active)?" ACTIVE":"");
354 //cprintf("NO \"No scripts found.\"\r\n");
358 /* SETACTIVE command. see chapter 2.8 */
359 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
361 /* TODO: check auth, if not, answer with "no" */
362 /* search our room for subjects with that scriptname,
363 * if the scriptname is empty, use the default flag.
364 * if the script is not there answer "No "there is no script by that name "
368 if (msiv_setactive(u, parms[1]) == 0) {
372 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
375 cprintf("NO \"unexpected parameters.\"\r\n");
380 /* GETSCRIPT command. see chapter 2.9 */
381 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
383 /* check first param, this is the name. look up that in the folder.
384 * answer with the size {nnn+}and spill it out, one blank line and OK
388 char *script_content;
391 script_content = msiv_getscript(u, parms[1]);
392 if (script_content != NULL){
395 slen = strlen(script_content);
396 outbuf = malloc (slen + 64);
397 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
401 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
404 cprintf("NO \"unexpected parameters.\"\r\n");
408 /* DELETESCRIPT command. see chapter 2.10 */
409 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
411 /* TODO: check auth, if not, answer with "no" */
415 i = msiv_deletescript(u, parms[1]);
421 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
424 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
428 cprintf("NO \"unexpected parameters.\"\r\n");
437 void mgsve_get_user(char *argbuf) {
441 CtdlDecodeBase64(username, argbuf, SIZ);
442 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", username); * /
443 if (CtdlLoginExistingUser(username) == login_ok) {
444 CtdlEncodeBase64(buf, "Password:", 9);
445 cprintf("334 %s\r\n", buf);
446 MGSVE->command_state = mgsve_password;
449 cprintf("500 5.7.0 No such user.\r\n");
450 MGSVE->command_state = mgsve_command;
458 void mgsve_get_pass(char *argbuf) {
461 CtdlDecodeBase64(password, argbuf, SIZ);
462 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", password); * /
463 if (CtdlTryPassword(password) == pass_ok) {
464 mgsve_auth_greeting();
467 cprintf("535 5.7.0 Authentication failed.\r\n");
469 MGSVE->command_state = mgsve_command;
475 * Back end for PLAIN auth method (either inline or multistate)
477 void mgsve_try_plain(char *encoded_authstring) {
478 char decoded_authstring[1024];
483 CtdlDecodeBase64(decoded_authstring,
485 strlen(encoded_authstring) );
486 safestrncpy(ident, decoded_authstring, sizeof ident);
487 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
488 safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
490 // MGSVE->command_state = mgsve_command;
492 if (CtdlLoginExistingUser(user) == login_ok) {
493 if (CtdlTryPassword(pass) == pass_ok) {
494 mgsve_auth_greeting();
499 cprintf("504 5.7.4 Authentication failed.\r\n");
504 * Attempt to perform authenticated managesieve
506 void mgsve_auth(char *argbuf) {
507 char username_prompt[64];
509 char encoded_authstring[1024];
512 cprintf("504 5.7.4 Already logged in.\r\n");
516 extract_token(method, argbuf, 0, ' ', sizeof method);
518 if (!strncasecmp(method, "login", 5) ) {
519 if (strlen(argbuf) >= 7) {
520 // mgsve_get_user(&argbuf[6]);
523 CtdlEncodeBase64(username_prompt, "Username:", 9);
524 cprintf("334 %s\r\n", username_prompt);
525 // MGSVE->command_state = mgsve_user;
530 if (!strncasecmp(method, "plain", 5) ) {
531 if (num_tokens(argbuf, ' ') < 2) {
533 // MGSVE->command_state = mgsve_plain;
537 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
539 /// mgsve_try_plain(encoded_authstring);
543 if (strncasecmp(method, "login", 5) ) {
544 cprintf("504 5.7.4 Unknown authentication method.\r\n");
553 * implements the STARTTLS command (Citadel API version)
556 void _mgsve_starttls(void)
558 char ok_response[SIZ];
559 char nosup_response[SIZ];
560 char error_response[SIZ];
563 "200 2.0.0 Begin TLS negotiation now\r\n");
564 sprintf(nosup_response,
565 "554 5.7.3 TLS not supported here\r\n");
566 sprintf(error_response,
567 "554 5.7.3 Internal error\r\n");
568 CtdlStartTLS(ok_response, nosup_response, error_response);
574 * Main command loop for managesieve sessions.
576 void managesieve_command_loop(void) {
581 struct sdm_userdata u;
584 memset(&u, 0, sizeof(struct sdm_userdata));
587 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
588 length = client_getln(cmdbuf, sizeof cmdbuf);
590 num_parms = imap_parameterize(parms, cmdbuf);
591 /// length = client_getln(parms[0], sizeof parms[0]);
592 if (num_parms == 0) return;
593 length = strlen(parms[0]);
596 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
600 lprintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
601 //// we have different lengths while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
602 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
603 cmd_mgsve_auth(num_parms, parms, &u);
607 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
608 cmd_mgsve_starttls();
611 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
612 cmd_mgsve_logout(&u);
614 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
616 } /* these commands need to be authenticated. throw it out if it tries. */
617 else if (!CtdlAccessCheck(ac_logged_in))
620 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
621 cmd_mgsve_havespace();
623 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
624 cmd_mgsve_putscript(num_parms, parms, &u);
626 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
627 cmd_mgsve_listscript(num_parms, parms,&u);
629 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
630 cmd_mgsve_setactive(num_parms, parms,&u);
632 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
633 cmd_mgsve_getscript(num_parms, parms, &u);
635 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
636 cmd_mgsve_deletescript(num_parms, parms, &u);
651 char *serv_managesieve_init(void)
654 CtdlRegisterServiceHook(config.c_managesieve_port, /* MGSVE */
656 managesieve_greeting,
657 managesieve_command_loop,
660 return "$Id: serv_managesieve.c 4570 2006-08-27 02:07:18Z dothebart $";
663 #else /* HAVE_LIBSIEVE */
665 char *serv_managesieve_init(void)
667 lprintf(CTDL_INFO, "This server is missing libsieve. Managesieve protocol is disabled..\n");
671 #endif /* HAVE_LIBSIEVE */