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"
66 #include "imap_tools.h" /* Needed for imap_parameterize */
69 #include "clientsocket.h"
70 #include "locate_host.h"
71 #include "citadel_dirs.h"
78 #include "ctdl_module.h"
79 #include "serv_sieve.h"
83 * http://tools.ietf.org/html/draft-martin-managesieve-06
85 * this is the draft this code tries to implement.
90 int command_state; /**< Information about the current session */
91 char *transmitted_message;
92 size_t transmitted_length;
93 char *imap_format_outstring;
94 int imap_outstring_length;
97 enum { /** Command states for login authentication */
105 #define MGSVE ((struct citmgsve *)CC->session_specific_data)
107 /*****************************************************************************/
108 /* MANAGESIEVE Server */
109 /*****************************************************************************/
112 void sieve_outbuf_append(char *str)
114 size_t newlen = strlen(str)+1;
115 size_t oldlen = (MGSVE->imap_format_outstring==NULL)? 0 : strlen(MGSVE->imap_format_outstring)+2;
116 char *buf = malloc ( newlen + oldlen + 10 );
120 sprintf(buf,"%s%s",MGSVE->imap_format_outstring, str);
122 memcpy(buf, str, newlen);
124 if (oldlen != 0) free (MGSVE->imap_format_outstring);
125 MGSVE->imap_format_outstring = buf;
130 * Capability listing. Printed as greeting or on "CAPABILITIES"
131 * see Section 1.8 ; 2.4
133 void cmd_mgsve_caps(void)
135 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve " PACKAGE_VERSION "\"\r\n"
136 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
138 /* if TLS is already there, should we say that again? */
141 "\"SIEVE\" \"%s\"\r\n"
142 "OK\r\n", msiv_extensions);
147 * Here's where our managesieve session begins its happy day.
149 void managesieve_greeting(void) {
151 strcpy(CC->cs_clientname, "Managesieve session");
153 CC->internal_pgm = 0;
154 CC->cs_flags |= CS_STEALTH;
155 CC->session_specific_data = malloc(sizeof(struct citmgsve));
156 memset(MGSVE, 0, sizeof(struct citmgsve));
161 long GetSizeToken(char * token)
163 char *cursor = token;
166 while (!IsEmptyStr(cursor) &&
171 if (IsEmptyStr(cursor))
174 while ((*cursor != '\0') &&
180 if (cursor[-1] == '+')
183 if (IsEmptyStr(cursor))
189 char *ReadString(long size, char *command)
193 cprintf("NO %s: %ld BAD Message length must be at least 1.\r\n",
198 MGSVE->transmitted_message = malloc(size + 2);
199 if (MGSVE->transmitted_message == NULL) {
200 cprintf("NO %s Cannot allocate memory.\r\n", command);
204 MGSVE->transmitted_length = size;
206 ret = client_read(MGSVE->transmitted_message, size);
207 MGSVE->transmitted_message[size] = '\0';
210 cprintf("%s NO Read failed.\r\n", command);
213 return MGSVE->transmitted_message;
216 /* AUTHENTICATE command; 2.1 */
217 void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u)
219 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
220 /* todo, check length*/
228 memset (auth, 0, SIZ);
229 if (parms[2][0] == '{')
230 message = ReadString(GetSizeToken(parms[2]), parms[0]);
232 if (message != NULL) {/**< do we have tokenized login? */
233 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
236 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
238 if ((*username == '\0') && (*(username + 1) != '\0'))
241 if (login_ok == CtdlLoginExistingUser(NULL, username))
245 pass = &(auth[strlen(auth)+1]);
246 /* for some reason the php script sends us the username twice. y? */
247 pass = &(pass[strlen(pass)+1]);
249 if (pass_ok == CtdlTryPassword(pass, strlen(pass)))
251 MGSVE->command_state = mgsve_password;
257 cprintf("NO \"Authentication Failure.\"\r\n");/* we just support auth plain. */
263 * STARTTLS command chapter 2.2
265 void cmd_mgsve_starttls(void)
266 { /** answer with OK, and fire off tls session. */
268 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
275 *LOGOUT command, see chapter 2.3
277 void cmd_mgsve_logout(struct sdm_userdata *u)
280 CtdlLogPrintf(CTDL_NOTICE, "MgSve bye.");
286 * HAVESPACE command. see chapter 2.5
288 void cmd_mgsve_havespace(void)
290 /* as we don't have quotas in citadel we should always answer with OK;
291 * pherhaps we should have a max-scriptsize.
293 if (MGSVE->command_state != mgsve_password)
301 /* citadel doesn't have quotas. in case of change, please add code here. */
307 * PUTSCRIPT command, see chapter 2.6
309 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
311 /* "scriptname" {nnn+} */
312 /* AFTER we have the whole script overwrite existing scripts */
313 /* spellcheck the script before overwrite old ones, and reply with "no" */
320 if (parms[1][0]=='"')
321 ScriptName = &parms[1][1];
323 ScriptName = parms[1];
325 slength = strlen (ScriptName);
327 if (ScriptName[slength] == '"')
328 ScriptName[slength] = '\0';
330 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
332 if (Script == NULL) return;
334 // TODO: do we spellcheck?
335 msiv_putscript(u, ScriptName, Script);
339 cprintf("%s NO Read failed.\r\n", parms[0]);
352 * LISTSCRIPT command. see chapter 2.7
354 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
357 struct sdm_script *s;
360 MGSVE->imap_format_outstring = NULL;
361 for (s=u->first_script; s!=NULL; s=s->next) {
362 if (s->script_content != NULL) {
363 cprintf("\"%s\"%s\r\n",
365 (s->script_active)?" ACTIVE":"");
374 * \brief SETACTIVE command. see chapter 2.8
376 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
380 if (msiv_setactive(u, parms[1]) == 0) {
384 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
387 cprintf("NO \"unexpected parameters.\"\r\n");
393 * \brief GETSCRIPT command. see chapter 2.9
395 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
398 char *script_content;
401 script_content = msiv_getscript(u, parms[1]);
402 if (script_content != NULL){
405 slen = strlen(script_content);
406 outbuf = malloc (slen + 64);
407 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
411 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
414 cprintf("NO \"unexpected parameters.\"\r\n");
419 * \brief DELETESCRIPT command. see chapter 2.10
421 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
426 i = msiv_deletescript(u, parms[1]);
432 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
435 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
439 cprintf("NO \"unexpected parameters.\"\r\n");
446 * \brief Attempt to perform authenticated managesieve
448 void mgsve_auth(char *argbuf) {
449 char username_prompt[64];
451 char encoded_authstring[1024];
454 cprintf("NO \"Already logged in.\"\r\n");
458 extract_token(method, argbuf, 0, ' ', sizeof method);
460 if (!strncasecmp(method, "login", 5) ) {
461 if (strlen(argbuf) >= 7) {
464 CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
465 cprintf("334 %s\r\n", username_prompt);
470 if (!strncasecmp(method, "plain", 5) ) {
471 if (num_tokens(argbuf, ' ') < 2) {
475 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
479 if (strncasecmp(method, "login", 5) ) {
480 cprintf("NO \"Unknown authentication method.\"\r\n");
489 * implements the STARTTLS command (Citadel API version)
491 void _mgsve_starttls(void)
493 char ok_response[SIZ];
494 char nosup_response[SIZ];
495 char error_response[SIZ];
498 "200 2.0.0 Begin TLS negotiation now\r\n");
499 sprintf(nosup_response,
500 "554 5.7.3 TLS not supported here\r\n");
501 sprintf(error_response,
502 "554 5.7.3 Internal error\r\n");
503 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
508 * Main command loop for managesieve sessions.
510 void managesieve_command_loop(void) {
515 struct sdm_userdata u;
516 int changes_made = 0;
518 memset(&u, 0, sizeof(struct sdm_userdata));
521 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
522 length = client_getln(cmdbuf, sizeof cmdbuf);
524 num_parms = old_imap_parameterize(parms, cmdbuf);
525 if (num_parms == 0) return;
526 length = strlen(parms[0]);
529 CtdlLogPrintf(CTDL_CRIT, "Client disconnected: ending session.\n");
533 CtdlLogPrintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
534 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
535 cmd_mgsve_auth(num_parms, parms, &u);
539 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
540 cmd_mgsve_starttls();
543 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
544 cmd_mgsve_logout(&u);
546 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
549 /** these commands need to be authenticated. throw it out if it tries. */
550 else if (CC->logged_in != 0)
553 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
554 cmd_mgsve_havespace();
556 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
557 cmd_mgsve_putscript(num_parms, parms, &u);
560 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
561 cmd_mgsve_listscript(num_parms, parms,&u);
563 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
564 cmd_mgsve_setactive(num_parms, parms,&u);
567 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
568 cmd_mgsve_getscript(num_parms, parms, &u);
570 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
571 cmd_mgsve_deletescript(num_parms, parms, &u);
574 msiv_store(&u, changes_made);
577 cprintf("No Invalid access or command.\r\n");
578 CtdlLogPrintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
586 * This cleanup function blows away the temporary memory and files used by
589 void managesieve_cleanup_function(void) {
591 /* Don't do this stuff if this is not a managesieve session! */
592 if (CC->h_command_function != managesieve_command_loop) return;
594 CtdlLogPrintf(CTDL_DEBUG, "Performing managesieve cleanup hook\n");
600 const char* CitadelServiceManageSieve = "ManageSieve";
601 CTDL_MODULE_INIT(managesieve)
605 CtdlRegisterServiceHook(config.c_managesieve_port,
607 managesieve_greeting,
608 managesieve_command_loop,
610 CitadelServiceManageSieve);
611 CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
614 /* return our Subversion id for the Log */
615 return "managesieve";