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 "citserver.h"
53 #include "internet_addressing.h"
54 #include "imap_tools.h"
57 #include "clientsocket.h"
58 #include "locate_host.h"
59 #include "citadel_dirs.h"
62 #include "serv_crypto.h"
70 #include "ctdl_module.h"
76 #include "serv_sieve.h"
80 * http://tools.ietf.org/html/draft-martin-managesieve-06
82 * this is the draft this code tries to implement.
87 int command_state; /**< Information about the current session */
88 char *transmitted_message;
89 size_t transmitted_length;
90 char *imap_format_outstring;
91 int imap_outstring_length;
94 enum { /** Command states for login authentication */
102 #define MGSVE CC->MGSVE
104 /*****************************************************************************/
105 /* MANAGESIEVE Server */
106 /*****************************************************************************/
108 void sieve_outbuf_append(char *str)
110 size_t newlen = strlen(str)+1;
111 size_t oldlen = (MGSVE->imap_format_outstring==NULL)? 0 : strlen(MGSVE->imap_format_outstring)+2;
112 char *buf = malloc ( newlen + oldlen + 10 );
116 sprintf(buf,"%s%s",MGSVE->imap_format_outstring, str);
118 memcpy(buf, str, newlen);
120 if (oldlen != 0) free (MGSVE->imap_format_outstring);
121 MGSVE->imap_format_outstring = buf;
126 * Capability listing. Printed as greeting or on "CAPABILITIES"
127 * see Section 1.8 ; 2.4
129 void cmd_mgsve_caps(void)
131 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve v6.84\"\r\n" /* TODO: put citversion here. */
132 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
134 /* if TLS is already there, should we say that again? */
137 "\"SIEVE\" \"%s\"\r\n"
138 "OK\r\n", msiv_extensions);
143 * Here's where our managesieve session begins its happy day.
145 void managesieve_greeting(void) {
147 strcpy(CC->cs_clientname, "Managesieve session");
149 CC->internal_pgm = 1;
150 CC->cs_flags |= CS_STEALTH;
151 MGSVE = malloc(sizeof(struct citmgsve));
152 memset(MGSVE, 0, sizeof(struct citmgsve));
157 long GetSizeToken(char * token)
159 char *cursor = token;
162 while ((*cursor != '\0') &&
170 while ((*cursor != '\0') &&
176 if (cursor[-1] == '+')
185 char *ReadString(long size, char *command)
189 cprintf("NO %s: %ld BAD Message length must be at least 1.\r\n",
194 MGSVE->transmitted_message = malloc(size + 2);
195 if (MGSVE->transmitted_message == NULL) {
196 cprintf("NO %s Cannot allocate memory.\r\n", command);
200 MGSVE->transmitted_length = size;
202 ret = client_read(MGSVE->transmitted_message, size);
203 MGSVE->transmitted_message[size] = '\0';
206 cprintf("%s NO Read failed.\r\n", command);
209 return MGSVE->transmitted_message;
212 /* AUTHENTICATE command; 2.1 */
213 void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u)
215 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
216 /* todo, check length*/
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(NULL, 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 \"Authentication Failure.\"\r\n");/* we just support auth plain. */
251 * STARTTLS command chapter 2.2
253 void cmd_mgsve_starttls(void)
254 { /** answer with OK, and fire off tls session. */
256 CtdlStartTLS(NULL, NULL, NULL);
264 *LOGOUT command, see chapter 2.3
266 void cmd_mgsve_logout(struct sdm_userdata *u)
269 lprintf(CTDL_NOTICE, "MgSve bye.");
275 * HAVESPACE command. see chapter 2.5
277 void cmd_mgsve_havespace(void)
279 /* as we don't have quotas in citadel we should always answer with OK;
280 * pherhaps we should have a max-scriptsize.
282 if (MGSVE->command_state != mgsve_password)
290 /* citadel doesn't have quotas. in case of change, please add code here. */
296 * PUTSCRIPT command, see chapter 2.6
298 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
300 /* "scriptname" {nnn+} */
301 /* AFTER we have the whole script overwrite existing scripts */
302 /* spellcheck the script before overwrite old ones, and reply with "no" */
309 if (parms[1][0]=='"')
310 ScriptName = &parms[1][1];
312 ScriptName = parms[1];
314 slength = strlen (ScriptName);
316 if (ScriptName[slength] == '"')
317 ScriptName[slength] = '\0';
319 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
321 if (Script == NULL) return;
323 // TODO: do we spellcheck?
324 msiv_putscript(u, ScriptName, Script);
328 cprintf("%s NO Read failed.\r\n", parms[0]);
341 * LISTSCRIPT command. see chapter 2.7
343 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
346 struct sdm_script *s;
349 MGSVE->imap_format_outstring = NULL;
350 for (s=u->first_script; s!=NULL; s=s->next) {
351 if (s->script_content != NULL) {
352 cprintf("\"%s\"%s\r\n",
354 (s->script_active)?" ACTIVE":"");
363 * \brief SETACTIVE command. see chapter 2.8
365 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
369 if (msiv_setactive(u, parms[1]) == 0) {
373 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
376 cprintf("NO \"unexpected parameters.\"\r\n");
382 * \brief GETSCRIPT command. see chapter 2.9
384 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
387 char *script_content;
390 script_content = msiv_getscript(u, parms[1]);
391 if (script_content != NULL){
394 slen = strlen(script_content);
395 outbuf = malloc (slen + 64);
396 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
400 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
403 cprintf("NO \"unexpected parameters.\"\r\n");
408 * \brief DELETESCRIPT command. see chapter 2.10
410 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
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");
435 * \brief Attempt to perform authenticated managesieve
437 void mgsve_auth(char *argbuf) {
438 char username_prompt[64];
440 char encoded_authstring[1024];
443 cprintf("NO \"Already logged in.\"\r\n");
447 extract_token(method, argbuf, 0, ' ', sizeof method);
449 if (!strncasecmp(method, "login", 5) ) {
450 if (strlen(argbuf) >= 7) {
453 CtdlEncodeBase64(username_prompt, "Username:", 9);
454 cprintf("334 %s\r\n", username_prompt);
459 if (!strncasecmp(method, "plain", 5) ) {
460 if (num_tokens(argbuf, ' ') < 2) {
464 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
468 if (strncasecmp(method, "login", 5) ) {
469 cprintf("NO \"Unknown authentication method.\"\r\n");
478 * implements the STARTTLS command (Citadel API version)
481 void _mgsve_starttls(void)
483 char ok_response[SIZ];
484 char nosup_response[SIZ];
485 char error_response[SIZ];
488 "200 2.0.0 Begin TLS negotiation now\r\n");
489 sprintf(nosup_response,
490 "554 5.7.3 TLS not supported here\r\n");
491 sprintf(error_response,
492 "554 5.7.3 Internal error\r\n");
493 CtdlStartTLS(ok_response, nosup_response, error_response);
499 * Main command loop for managesieve sessions.
501 void managesieve_command_loop(void) {
506 struct sdm_userdata u;
507 int changes_made = 0;
509 memset(&u, 0, sizeof(struct sdm_userdata));
512 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
513 length = client_getln(cmdbuf, sizeof cmdbuf);
515 num_parms = imap_parameterize(parms, cmdbuf);
516 if (num_parms == 0) return;
517 length = strlen(parms[0]);
520 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
524 lprintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
525 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
526 cmd_mgsve_auth(num_parms, parms, &u);
530 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
531 cmd_mgsve_starttls();
534 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
535 cmd_mgsve_logout(&u);
537 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
540 /** these commands need to be authenticated. throw it out if it tries. */
541 else if (!CtdlAccessCheck(ac_logged_in))
544 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
545 cmd_mgsve_havespace();
547 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
548 cmd_mgsve_putscript(num_parms, parms, &u);
551 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
552 cmd_mgsve_listscript(num_parms, parms,&u);
554 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
555 cmd_mgsve_setactive(num_parms, parms,&u);
558 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
559 cmd_mgsve_getscript(num_parms, parms, &u);
561 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
562 cmd_mgsve_deletescript(num_parms, parms, &u);
565 msiv_store(&u, changes_made);
569 lprintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
577 #endif /* HAVE_LIBSIEVE */
579 CTDL_MODULE_INIT(managesieve)
584 CtdlRegisterServiceHook(config.c_managesieve_port, /* MGSVE */
586 managesieve_greeting,
587 managesieve_command_loop,
590 #else /* HAVE_LIBSIEVE */
592 lprintf(CTDL_INFO, "This server is missing libsieve. Managesieve protocol is disabled..\n");
594 #endif /* HAVE_LIBSIEVE */
596 /* return our Subversion id for the Log */