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
11 * Copyright (c) 2007-2009 by the citadel.org team
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/types.h>
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
44 # include <sys/time.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <libcitadel.h>
60 #include "citserver.h"
69 #include "internet_addressing.h"
70 #include "imap_tools.h" /* Needed for imap_parameterize */
73 #include "clientsocket.h"
74 #include "locate_host.h"
75 #include "citadel_dirs.h"
82 #include "ctdl_module.h"
83 #include "serv_sieve.h"
87 * http://tools.ietf.org/html/draft-martin-managesieve-06
89 * this is the draft this code tries to implement.
94 int command_state; /**< Information about the current session */
95 char *transmitted_message;
96 size_t transmitted_length;
97 char *imap_format_outstring;
98 int imap_outstring_length;
101 enum { /** Command states for login authentication */
109 #define MGSVE ((struct citmgsve *)CC->session_specific_data)
111 /*****************************************************************************/
112 /* MANAGESIEVE Server */
113 /*****************************************************************************/
116 void sieve_outbuf_append(char *str)
118 size_t newlen = strlen(str)+1;
119 size_t oldlen = (MGSVE->imap_format_outstring==NULL)? 0 : strlen(MGSVE->imap_format_outstring)+2;
120 char *buf = malloc ( newlen + oldlen + 10 );
124 sprintf(buf,"%s%s",MGSVE->imap_format_outstring, str);
126 memcpy(buf, str, newlen);
128 if (oldlen != 0) free (MGSVE->imap_format_outstring);
129 MGSVE->imap_format_outstring = buf;
134 * Capability listing. Printed as greeting or on "CAPABILITIES"
135 * see Section 1.8 ; 2.4
137 void cmd_mgsve_caps(void)
139 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve " PACKAGE_VERSION "\"\r\n"
140 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
142 /* if TLS is already there, should we say that again? */
145 "\"SIEVE\" \"%s\"\r\n"
146 "OK\r\n", msiv_extensions);
151 * Here's where our managesieve session begins its happy day.
153 void managesieve_greeting(void) {
155 strcpy(CC->cs_clientname, "Managesieve session");
157 CC->internal_pgm = 0;
158 CC->cs_flags |= CS_STEALTH;
159 CC->session_specific_data = malloc(sizeof(struct citmgsve));
160 memset(MGSVE, 0, sizeof(struct citmgsve));
165 long GetSizeToken(char * token)
167 char *cursor = token;
170 while (!IsEmptyStr(cursor) &&
175 if (IsEmptyStr(cursor))
178 while ((*cursor != '\0') &&
184 if (cursor[-1] == '+')
187 if (IsEmptyStr(cursor))
193 char *ReadString(long size, char *command)
197 cprintf("NO %s: %ld BAD Message length must be at least 1.\r\n",
202 MGSVE->transmitted_message = malloc(size + 2);
203 if (MGSVE->transmitted_message == NULL) {
204 cprintf("NO %s Cannot allocate memory.\r\n", command);
208 MGSVE->transmitted_length = size;
210 ret = client_read(MGSVE->transmitted_message, size);
211 MGSVE->transmitted_message[size] = '\0';
214 cprintf("%s NO Read failed.\r\n", command);
217 return MGSVE->transmitted_message;
220 /* AUTHENTICATE command; 2.1 */
221 void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u)
223 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
224 /* todo, check length*/
232 memset (auth, 0, SIZ);
233 if (parms[2][0] == '{')
234 message = ReadString(GetSizeToken(parms[2]), parms[0]);
236 if (message != NULL) {/**< do we have tokenized login? */
237 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
240 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
242 if ((*username == '\0') && (*(username + 1) != '\0'))
245 if (login_ok == CtdlLoginExistingUser(NULL, username))
248 pass = &(auth[strlen(auth)+1]);
249 /* for some reason the php script sends us the username twice. y? */
250 pass = &(pass[strlen(pass)+1]);
252 if (pass_ok == CtdlTryPassword(pass))
254 MGSVE->command_state = mgsve_password;
260 cprintf("NO \"Authentication Failure.\"\r\n");/* we just support auth plain. */
266 * STARTTLS command chapter 2.2
268 void cmd_mgsve_starttls(void)
269 { /** answer with OK, and fire off tls session. */
271 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
278 *LOGOUT command, see chapter 2.3
280 void cmd_mgsve_logout(struct sdm_userdata *u)
283 CtdlLogPrintf(CTDL_NOTICE, "MgSve bye.");
289 * HAVESPACE command. see chapter 2.5
291 void cmd_mgsve_havespace(void)
293 /* as we don't have quotas in citadel we should always answer with OK;
294 * pherhaps we should have a max-scriptsize.
296 if (MGSVE->command_state != mgsve_password)
304 /* citadel doesn't have quotas. in case of change, please add code here. */
310 * PUTSCRIPT command, see chapter 2.6
312 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
314 /* "scriptname" {nnn+} */
315 /* AFTER we have the whole script overwrite existing scripts */
316 /* spellcheck the script before overwrite old ones, and reply with "no" */
323 if (parms[1][0]=='"')
324 ScriptName = &parms[1][1];
326 ScriptName = parms[1];
328 slength = strlen (ScriptName);
330 if (ScriptName[slength] == '"')
331 ScriptName[slength] = '\0';
333 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
335 if (Script == NULL) return;
337 // TODO: do we spellcheck?
338 msiv_putscript(u, ScriptName, Script);
342 cprintf("%s NO Read failed.\r\n", parms[0]);
355 * LISTSCRIPT command. see chapter 2.7
357 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
360 struct sdm_script *s;
363 MGSVE->imap_format_outstring = NULL;
364 for (s=u->first_script; s!=NULL; s=s->next) {
365 if (s->script_content != NULL) {
366 cprintf("\"%s\"%s\r\n",
368 (s->script_active)?" ACTIVE":"");
377 * \brief SETACTIVE command. see chapter 2.8
379 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
383 if (msiv_setactive(u, parms[1]) == 0) {
387 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
390 cprintf("NO \"unexpected parameters.\"\r\n");
396 * \brief GETSCRIPT command. see chapter 2.9
398 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
401 char *script_content;
404 script_content = msiv_getscript(u, parms[1]);
405 if (script_content != NULL){
408 slen = strlen(script_content);
409 outbuf = malloc (slen + 64);
410 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
414 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
417 cprintf("NO \"unexpected parameters.\"\r\n");
422 * \brief DELETESCRIPT command. see chapter 2.10
424 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
429 i = msiv_deletescript(u, parms[1]);
435 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
438 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
442 cprintf("NO \"unexpected parameters.\"\r\n");
449 * \brief Attempt to perform authenticated managesieve
451 void mgsve_auth(char *argbuf) {
452 char username_prompt[64];
454 char encoded_authstring[1024];
457 cprintf("NO \"Already logged in.\"\r\n");
461 extract_token(method, argbuf, 0, ' ', sizeof method);
463 if (!strncasecmp(method, "login", 5) ) {
464 if (strlen(argbuf) >= 7) {
467 CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
468 cprintf("334 %s\r\n", username_prompt);
473 if (!strncasecmp(method, "plain", 5) ) {
474 if (num_tokens(argbuf, ' ') < 2) {
478 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
482 if (strncasecmp(method, "login", 5) ) {
483 cprintf("NO \"Unknown authentication method.\"\r\n");
492 * implements the STARTTLS command (Citadel API version)
494 void _mgsve_starttls(void)
496 char ok_response[SIZ];
497 char nosup_response[SIZ];
498 char error_response[SIZ];
501 "200 2.0.0 Begin TLS negotiation now\r\n");
502 sprintf(nosup_response,
503 "554 5.7.3 TLS not supported here\r\n");
504 sprintf(error_response,
505 "554 5.7.3 Internal error\r\n");
506 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
511 * Main command loop for managesieve sessions.
513 void managesieve_command_loop(void) {
518 struct sdm_userdata u;
519 int changes_made = 0;
521 memset(&u, 0, sizeof(struct sdm_userdata));
524 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
525 length = client_getln(cmdbuf, sizeof cmdbuf);
527 num_parms = imap_parameterize(parms, cmdbuf);
528 if (num_parms == 0) return;
529 length = strlen(parms[0]);
532 CtdlLogPrintf(CTDL_CRIT, "Client disconnected: ending session.\n");
536 CtdlLogPrintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
537 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
538 cmd_mgsve_auth(num_parms, parms, &u);
542 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
543 cmd_mgsve_starttls();
546 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
547 cmd_mgsve_logout(&u);
549 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
552 /** these commands need to be authenticated. throw it out if it tries. */
553 else if (CC->logged_in != 0)
556 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
557 cmd_mgsve_havespace();
559 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
560 cmd_mgsve_putscript(num_parms, parms, &u);
563 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
564 cmd_mgsve_listscript(num_parms, parms,&u);
566 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
567 cmd_mgsve_setactive(num_parms, parms,&u);
570 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
571 cmd_mgsve_getscript(num_parms, parms, &u);
573 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
574 cmd_mgsve_deletescript(num_parms, parms, &u);
577 msiv_store(&u, changes_made);
580 cprintf("No Invalid access or command.\r\n");
581 CtdlLogPrintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
589 * This cleanup function blows away the temporary memory and files used by
592 void managesieve_cleanup_function(void) {
594 /* Don't do this stuff if this is not a managesieve session! */
595 if (CC->h_command_function != managesieve_command_loop) return;
597 CtdlLogPrintf(CTDL_DEBUG, "Performing managesieve cleanup hook\n");
603 const char* CitadelServiceManageSieve = "ManageSieve";
604 CTDL_MODULE_INIT(managesieve)
608 CtdlRegisterServiceHook(config.c_managesieve_port,
610 managesieve_greeting,
611 managesieve_command_loop,
613 CitadelServiceManageSieve);
614 CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
617 /* return our Subversion id for the Log */