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"
67 #include "internet_addressing.h"
68 #include "imap_tools.h" /* Needed for imap_parameterize */
71 #include "clientsocket.h"
72 #include "locate_host.h"
73 #include "citadel_dirs.h"
80 #include "ctdl_module.h"
81 #include "serv_sieve.h"
85 * http://tools.ietf.org/html/draft-martin-managesieve-06
87 * this is the draft this code tries to implement.
92 int command_state; /**< Information about the current session */
93 char *transmitted_message;
94 size_t transmitted_length;
95 char *imap_format_outstring;
96 int imap_outstring_length;
99 enum { /** Command states for login authentication */
107 #define MGSVE ((struct citmgsve *)CC->session_specific_data)
109 /*****************************************************************************/
110 /* MANAGESIEVE Server */
111 /*****************************************************************************/
114 void sieve_outbuf_append(char *str)
116 size_t newlen = strlen(str)+1;
117 size_t oldlen = (MGSVE->imap_format_outstring==NULL)? 0 : strlen(MGSVE->imap_format_outstring)+2;
118 char *buf = malloc ( newlen + oldlen + 10 );
122 sprintf(buf,"%s%s",MGSVE->imap_format_outstring, str);
124 memcpy(buf, str, newlen);
126 if (oldlen != 0) free (MGSVE->imap_format_outstring);
127 MGSVE->imap_format_outstring = buf;
132 * Capability listing. Printed as greeting or on "CAPABILITIES"
133 * see Section 1.8 ; 2.4
135 void cmd_mgsve_caps(void)
137 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve " PACKAGE_VERSION "\"\r\n"
138 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
140 /* if TLS is already there, should we say that again? */
143 "\"SIEVE\" \"%s\"\r\n"
144 "OK\r\n", msiv_extensions);
149 * Here's where our managesieve session begins its happy day.
151 void managesieve_greeting(void) {
153 strcpy(CC->cs_clientname, "Managesieve session");
155 CC->internal_pgm = 0;
156 CC->cs_flags |= CS_STEALTH;
157 CC->session_specific_data = malloc(sizeof(struct citmgsve));
158 memset(MGSVE, 0, sizeof(struct citmgsve));
163 long GetSizeToken(char * token)
165 char *cursor = token;
168 while (!IsEmptyStr(cursor) &&
173 if (IsEmptyStr(cursor))
176 while ((*cursor != '\0') &&
182 if (cursor[-1] == '+')
185 if (IsEmptyStr(cursor))
191 char *ReadString(long size, char *command)
195 cprintf("NO %s: %ld BAD Message length must be at least 1.\r\n",
200 MGSVE->transmitted_message = malloc(size + 2);
201 if (MGSVE->transmitted_message == NULL) {
202 cprintf("NO %s Cannot allocate memory.\r\n", command);
206 MGSVE->transmitted_length = size;
208 ret = client_read(MGSVE->transmitted_message, size);
209 MGSVE->transmitted_message[size] = '\0';
212 cprintf("%s NO Read failed.\r\n", command);
215 return MGSVE->transmitted_message;
218 /* AUTHENTICATE command; 2.1 */
219 void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u)
221 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
222 /* todo, check length*/
230 memset (auth, 0, SIZ);
231 if (parms[2][0] == '{')
232 message = ReadString(GetSizeToken(parms[2]), parms[0]);
234 if (message != NULL) {/**< do we have tokenized login? */
235 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
238 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
240 if ((*username == '\0') && (*(username + 1) != '\0'))
243 if (login_ok == CtdlLoginExistingUser(NULL, username))
247 pass = &(auth[strlen(auth)+1]);
248 /* for some reason the php script sends us the username twice. y? */
249 pass = &(pass[strlen(pass)+1]);
251 if (pass_ok == CtdlTryPassword(pass, strlen(pass)))
253 MGSVE->command_state = mgsve_password;
259 cprintf("NO \"Authentication Failure.\"\r\n");/* we just support auth plain. */
265 * STARTTLS command chapter 2.2
267 void cmd_mgsve_starttls(void)
268 { /** answer with OK, and fire off tls session. */
270 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
277 *LOGOUT command, see chapter 2.3
279 void cmd_mgsve_logout(struct sdm_userdata *u)
282 CtdlLogPrintf(CTDL_NOTICE, "MgSve bye.");
288 * HAVESPACE command. see chapter 2.5
290 void cmd_mgsve_havespace(void)
292 /* as we don't have quotas in citadel we should always answer with OK;
293 * pherhaps we should have a max-scriptsize.
295 if (MGSVE->command_state != mgsve_password)
303 /* citadel doesn't have quotas. in case of change, please add code here. */
309 * PUTSCRIPT command, see chapter 2.6
311 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
313 /* "scriptname" {nnn+} */
314 /* AFTER we have the whole script overwrite existing scripts */
315 /* spellcheck the script before overwrite old ones, and reply with "no" */
322 if (parms[1][0]=='"')
323 ScriptName = &parms[1][1];
325 ScriptName = parms[1];
327 slength = strlen (ScriptName);
329 if (ScriptName[slength] == '"')
330 ScriptName[slength] = '\0';
332 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
334 if (Script == NULL) return;
336 // TODO: do we spellcheck?
337 msiv_putscript(u, ScriptName, Script);
341 cprintf("%s NO Read failed.\r\n", parms[0]);
354 * LISTSCRIPT command. see chapter 2.7
356 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
359 struct sdm_script *s;
362 MGSVE->imap_format_outstring = NULL;
363 for (s=u->first_script; s!=NULL; s=s->next) {
364 if (s->script_content != NULL) {
365 cprintf("\"%s\"%s\r\n",
367 (s->script_active)?" ACTIVE":"");
376 * \brief SETACTIVE command. see chapter 2.8
378 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
382 if (msiv_setactive(u, parms[1]) == 0) {
386 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
389 cprintf("NO \"unexpected parameters.\"\r\n");
395 * \brief GETSCRIPT command. see chapter 2.9
397 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
400 char *script_content;
403 script_content = msiv_getscript(u, parms[1]);
404 if (script_content != NULL){
407 slen = strlen(script_content);
408 outbuf = malloc (slen + 64);
409 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
413 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
416 cprintf("NO \"unexpected parameters.\"\r\n");
421 * \brief DELETESCRIPT command. see chapter 2.10
423 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
428 i = msiv_deletescript(u, parms[1]);
434 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
437 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
441 cprintf("NO \"unexpected parameters.\"\r\n");
448 * \brief Attempt to perform authenticated managesieve
450 void mgsve_auth(char *argbuf) {
451 char username_prompt[64];
453 char encoded_authstring[1024];
456 cprintf("NO \"Already logged in.\"\r\n");
460 extract_token(method, argbuf, 0, ' ', sizeof method);
462 if (!strncasecmp(method, "login", 5) ) {
463 if (strlen(argbuf) >= 7) {
466 CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
467 cprintf("334 %s\r\n", username_prompt);
472 if (!strncasecmp(method, "plain", 5) ) {
473 if (num_tokens(argbuf, ' ') < 2) {
477 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
481 if (strncasecmp(method, "login", 5) ) {
482 cprintf("NO \"Unknown authentication method.\"\r\n");
491 * implements the STARTTLS command (Citadel API version)
493 void _mgsve_starttls(void)
495 char ok_response[SIZ];
496 char nosup_response[SIZ];
497 char error_response[SIZ];
500 "200 2.0.0 Begin TLS negotiation now\r\n");
501 sprintf(nosup_response,
502 "554 5.7.3 TLS not supported here\r\n");
503 sprintf(error_response,
504 "554 5.7.3 Internal error\r\n");
505 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
510 * Main command loop for managesieve sessions.
512 void managesieve_command_loop(void) {
517 struct sdm_userdata u;
518 int changes_made = 0;
520 memset(&u, 0, sizeof(struct sdm_userdata));
523 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
524 length = client_getln(cmdbuf, sizeof cmdbuf);
526 num_parms = old_imap_parameterize(parms, cmdbuf);
527 if (num_parms == 0) return;
528 length = strlen(parms[0]);
531 CtdlLogPrintf(CTDL_CRIT, "Client disconnected: ending session.\n");
535 CtdlLogPrintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
536 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
537 cmd_mgsve_auth(num_parms, parms, &u);
541 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
542 cmd_mgsve_starttls();
545 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
546 cmd_mgsve_logout(&u);
548 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
551 /** these commands need to be authenticated. throw it out if it tries. */
552 else if (CC->logged_in != 0)
555 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
556 cmd_mgsve_havespace();
558 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
559 cmd_mgsve_putscript(num_parms, parms, &u);
562 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
563 cmd_mgsve_listscript(num_parms, parms,&u);
565 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
566 cmd_mgsve_setactive(num_parms, parms,&u);
569 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
570 cmd_mgsve_getscript(num_parms, parms, &u);
572 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
573 cmd_mgsve_deletescript(num_parms, parms, &u);
576 msiv_store(&u, changes_made);
579 cprintf("No Invalid access or command.\r\n");
580 CtdlLogPrintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
588 * This cleanup function blows away the temporary memory and files used by
591 void managesieve_cleanup_function(void) {
593 /* Don't do this stuff if this is not a managesieve session! */
594 if (CC->h_command_function != managesieve_command_loop) return;
596 CtdlLogPrintf(CTDL_DEBUG, "Performing managesieve cleanup hook\n");
602 const char* CitadelServiceManageSieve = "ManageSieve";
603 CTDL_MODULE_INIT(managesieve)
607 CtdlRegisterServiceHook(config.c_managesieve_port,
609 managesieve_greeting,
610 managesieve_command_loop,
612 CitadelServiceManageSieve);
613 CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
616 /* return our Subversion id for the Log */