2 * This module is an managesieve implementation for the Citadel system.
3 * It is compliant with all of the following:
5 * http://tools.ietf.org/html/draft-martin-managesieve-06
6 * as this draft expires with this writing, you might need to search for
9 * Copyright (c) 2007-2009 by the citadel.org team
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <libcitadel.h>
58 #include "citserver.h"
65 #include "internet_addressing.h"
68 #include "clientsocket.h"
69 #include "locate_host.h"
70 #include "citadel_dirs.h"
77 #include "ctdl_module.h"
78 #include "serv_sieve.h"
82 * http://tools.ietf.org/html/draft-martin-managesieve-06
84 * this is the draft this code tries to implement.
89 int command_state; /**< Information about the current session */
90 char *transmitted_message;
91 size_t transmitted_length;
92 char *imap_format_outstring;
93 int imap_outstring_length;
96 enum { /** Command states for login authentication */
104 #define MGSVE ((struct citmgsve *)CC->session_specific_data)
106 int old_imap_parameterize(char** args, char *in)
113 /* Skip whitespace. */
120 /* Found the start of a token. */
124 /* Read in the token. */
134 /* Found a quoted section. */
166 /*****************************************************************************/
167 /* MANAGESIEVE Server */
168 /*****************************************************************************/
171 void sieve_outbuf_append(char *str)
173 size_t newlen = strlen(str)+1;
174 size_t oldlen = (MGSVE->imap_format_outstring==NULL)? 0 : strlen(MGSVE->imap_format_outstring)+2;
175 char *buf = malloc ( newlen + oldlen + 10 );
179 sprintf(buf,"%s%s",MGSVE->imap_format_outstring, str);
181 memcpy(buf, str, newlen);
183 if (oldlen != 0) free (MGSVE->imap_format_outstring);
184 MGSVE->imap_format_outstring = buf;
189 * Capability listing. Printed as greeting or on "CAPABILITIES"
190 * see Section 1.8 ; 2.4
192 void cmd_mgsve_caps(void)
194 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve " PACKAGE_VERSION "\"\r\n"
195 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
197 /* if TLS is already there, should we say that again? */
200 "\"SIEVE\" \"%s\"\r\n"
201 "OK\r\n", msiv_extensions);
206 * Here's where our managesieve session begins its happy day.
208 void managesieve_greeting(void) {
210 strcpy(CC->cs_clientname, "Managesieve session");
212 CC->internal_pgm = 0;
213 CC->cs_flags |= CS_STEALTH;
214 CC->session_specific_data = malloc(sizeof(struct citmgsve));
215 memset(MGSVE, 0, sizeof(struct citmgsve));
220 long GetSizeToken(char * token)
222 char *cursor = token;
225 while (!IsEmptyStr(cursor) &&
230 if (IsEmptyStr(cursor))
233 while ((*cursor != '\0') &&
239 if (cursor[-1] == '+')
242 if (IsEmptyStr(cursor))
248 char *ReadString(long size, char *command)
252 cprintf("NO %s: %ld BAD Message length must be at least 1.\r\n",
257 MGSVE->transmitted_message = malloc(size + 2);
258 if (MGSVE->transmitted_message == NULL) {
259 cprintf("NO %s Cannot allocate memory.\r\n", command);
263 MGSVE->transmitted_length = size;
265 ret = client_read(MGSVE->transmitted_message, size);
266 MGSVE->transmitted_message[size] = '\0';
269 cprintf("%s NO Read failed.\r\n", command);
272 return MGSVE->transmitted_message;
275 /* AUTHENTICATE command; 2.1 */
276 void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u)
278 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
279 /* todo, check length*/
287 memset (auth, 0, SIZ);
288 if (parms[2][0] == '{')
289 message = ReadString(GetSizeToken(parms[2]), parms[0]);
291 if (message != NULL) {/**< do we have tokenized login? */
292 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
295 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
297 if ((*username == '\0') && (*(username + 1) != '\0'))
300 if (login_ok == CtdlLoginExistingUser(NULL, username))
304 pass = &(auth[strlen(auth)+1]);
305 /* for some reason the php script sends us the username twice. y? */
306 pass = &(pass[strlen(pass)+1]);
308 if (pass_ok == CtdlTryPassword(pass, strlen(pass)))
310 MGSVE->command_state = mgsve_password;
316 cprintf("NO \"Authentication Failure.\"\r\n");/* we just support auth plain. */
322 * STARTTLS command chapter 2.2
324 void cmd_mgsve_starttls(void)
325 { /** answer with OK, and fire off tls session. */
327 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
334 *LOGOUT command, see chapter 2.3
336 void cmd_mgsve_logout(struct sdm_userdata *u)
339 CtdlLogPrintf(CTDL_NOTICE, "MgSve bye.");
345 * HAVESPACE command. see chapter 2.5
347 void cmd_mgsve_havespace(void)
349 /* as we don't have quotas in citadel we should always answer with OK;
350 * pherhaps we should have a max-scriptsize.
352 if (MGSVE->command_state != mgsve_password)
360 /* citadel doesn't have quotas. in case of change, please add code here. */
366 * PUTSCRIPT command, see chapter 2.6
368 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
370 /* "scriptname" {nnn+} */
371 /* AFTER we have the whole script overwrite existing scripts */
372 /* spellcheck the script before overwrite old ones, and reply with "no" */
379 if (parms[1][0]=='"')
380 ScriptName = &parms[1][1];
382 ScriptName = parms[1];
384 slength = strlen (ScriptName);
386 if (ScriptName[slength] == '"')
387 ScriptName[slength] = '\0';
389 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
391 if (Script == NULL) return;
393 // TODO: do we spellcheck?
394 msiv_putscript(u, ScriptName, Script);
398 cprintf("%s NO Read failed.\r\n", parms[0]);
411 * LISTSCRIPT command. see chapter 2.7
413 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
416 struct sdm_script *s;
419 MGSVE->imap_format_outstring = NULL;
420 for (s=u->first_script; s!=NULL; s=s->next) {
421 if (s->script_content != NULL) {
422 cprintf("\"%s\"%s\r\n",
424 (s->script_active)?" ACTIVE":"");
433 * \brief SETACTIVE command. see chapter 2.8
435 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
439 if (msiv_setactive(u, parms[1]) == 0) {
443 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
446 cprintf("NO \"unexpected parameters.\"\r\n");
452 * \brief GETSCRIPT command. see chapter 2.9
454 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
457 char *script_content;
460 script_content = msiv_getscript(u, parms[1]);
461 if (script_content != NULL){
464 slen = strlen(script_content);
465 outbuf = malloc (slen + 64);
466 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
470 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
473 cprintf("NO \"unexpected parameters.\"\r\n");
478 * \brief DELETESCRIPT command. see chapter 2.10
480 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
485 i = msiv_deletescript(u, parms[1]);
491 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
494 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
498 cprintf("NO \"unexpected parameters.\"\r\n");
505 * \brief Attempt to perform authenticated managesieve
507 void mgsve_auth(char *argbuf) {
508 char username_prompt[64];
510 char encoded_authstring[1024];
513 cprintf("NO \"Already logged in.\"\r\n");
517 extract_token(method, argbuf, 0, ' ', sizeof method);
519 if (!strncasecmp(method, "login", 5) ) {
520 if (strlen(argbuf) >= 7) {
523 CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
524 cprintf("334 %s\r\n", username_prompt);
529 if (!strncasecmp(method, "plain", 5) ) {
530 if (num_tokens(argbuf, ' ') < 2) {
534 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
538 if (strncasecmp(method, "login", 5) ) {
539 cprintf("NO \"Unknown authentication method.\"\r\n");
548 * implements the STARTTLS command (Citadel API version)
550 void _mgsve_starttls(void)
552 char ok_response[SIZ];
553 char nosup_response[SIZ];
554 char error_response[SIZ];
557 "200 2.0.0 Begin TLS negotiation now\r\n");
558 sprintf(nosup_response,
559 "554 5.7.3 TLS not supported here\r\n");
560 sprintf(error_response,
561 "554 5.7.3 Internal error\r\n");
562 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
567 * Main command loop for managesieve sessions.
569 void managesieve_command_loop(void) {
574 struct sdm_userdata u;
575 int changes_made = 0;
577 memset(&u, 0, sizeof(struct sdm_userdata));
580 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
581 length = client_getln(cmdbuf, sizeof cmdbuf);
583 num_parms = old_imap_parameterize(parms, cmdbuf);
584 if (num_parms == 0) return;
585 length = strlen(parms[0]);
588 CtdlLogPrintf(CTDL_CRIT, "Client disconnected: ending session.\n");
592 CtdlLogPrintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
593 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
594 cmd_mgsve_auth(num_parms, parms, &u);
598 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
599 cmd_mgsve_starttls();
602 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
603 cmd_mgsve_logout(&u);
605 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
608 /** these commands need to be authenticated. throw it out if it tries. */
609 else if (CC->logged_in != 0)
612 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
613 cmd_mgsve_havespace();
615 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
616 cmd_mgsve_putscript(num_parms, parms, &u);
619 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
620 cmd_mgsve_listscript(num_parms, parms,&u);
622 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
623 cmd_mgsve_setactive(num_parms, parms,&u);
626 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
627 cmd_mgsve_getscript(num_parms, parms, &u);
629 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
630 cmd_mgsve_deletescript(num_parms, parms, &u);
633 msiv_store(&u, changes_made);
636 cprintf("No Invalid access or command.\r\n");
637 CtdlLogPrintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
645 * This cleanup function blows away the temporary memory and files used by
648 void managesieve_cleanup_function(void) {
650 /* Don't do this stuff if this is not a managesieve session! */
651 if (CC->h_command_function != managesieve_command_loop) return;
653 CtdlLogPrintf(CTDL_DEBUG, "Performing managesieve cleanup hook\n");
659 const char* CitadelServiceManageSieve = "ManageSieve";
660 CTDL_MODULE_INIT(managesieve)
664 CtdlRegisterServiceHook(config.c_managesieve_port,
666 managesieve_greeting,
667 managesieve_command_loop,
669 CitadelServiceManageSieve);
670 CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
673 /* return our Subversion id for the Log */
674 return "managesieve";