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))
246 pass = &(auth[strlen(auth)+1]);
247 /* for some reason the php script sends us the username twice. y? */
248 pass = &(pass[strlen(pass)+1]);
250 if (pass_ok == CtdlTryPassword(pass))
252 MGSVE->command_state = mgsve_password;
258 cprintf("NO \"Authentication Failure.\"\r\n");/* we just support auth plain. */
264 * STARTTLS command chapter 2.2
266 void cmd_mgsve_starttls(void)
267 { /** answer with OK, and fire off tls session. */
269 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
276 *LOGOUT command, see chapter 2.3
278 void cmd_mgsve_logout(struct sdm_userdata *u)
281 CtdlLogPrintf(CTDL_NOTICE, "MgSve bye.");
287 * HAVESPACE command. see chapter 2.5
289 void cmd_mgsve_havespace(void)
291 /* as we don't have quotas in citadel we should always answer with OK;
292 * pherhaps we should have a max-scriptsize.
294 if (MGSVE->command_state != mgsve_password)
302 /* citadel doesn't have quotas. in case of change, please add code here. */
308 * PUTSCRIPT command, see chapter 2.6
310 void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u)
312 /* "scriptname" {nnn+} */
313 /* AFTER we have the whole script overwrite existing scripts */
314 /* spellcheck the script before overwrite old ones, and reply with "no" */
321 if (parms[1][0]=='"')
322 ScriptName = &parms[1][1];
324 ScriptName = parms[1];
326 slength = strlen (ScriptName);
328 if (ScriptName[slength] == '"')
329 ScriptName[slength] = '\0';
331 Script = ReadString(GetSizeToken(parms[2]),parms[0]);
333 if (Script == NULL) return;
335 // TODO: do we spellcheck?
336 msiv_putscript(u, ScriptName, Script);
340 cprintf("%s NO Read failed.\r\n", parms[0]);
353 * LISTSCRIPT command. see chapter 2.7
355 void cmd_mgsve_listscript(int num_parms, char **parms, struct sdm_userdata *u)
358 struct sdm_script *s;
361 MGSVE->imap_format_outstring = NULL;
362 for (s=u->first_script; s!=NULL; s=s->next) {
363 if (s->script_content != NULL) {
364 cprintf("\"%s\"%s\r\n",
366 (s->script_active)?" ACTIVE":"");
375 * \brief SETACTIVE command. see chapter 2.8
377 void cmd_mgsve_setactive(int num_parms, char **parms, struct sdm_userdata *u)
381 if (msiv_setactive(u, parms[1]) == 0) {
385 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
388 cprintf("NO \"unexpected parameters.\"\r\n");
394 * \brief GETSCRIPT command. see chapter 2.9
396 void cmd_mgsve_getscript(int num_parms, char **parms, struct sdm_userdata *u)
399 char *script_content;
402 script_content = msiv_getscript(u, parms[1]);
403 if (script_content != NULL){
406 slen = strlen(script_content);
407 outbuf = malloc (slen + 64);
408 snprintf(outbuf, slen + 64, "{%ld+}\r\n%s\r\nOK\r\n",slen, script_content);
412 cprintf("No \"there is no script by that name %s \"\r\n", parms[1]);
415 cprintf("NO \"unexpected parameters.\"\r\n");
420 * \brief DELETESCRIPT command. see chapter 2.10
422 void cmd_mgsve_deletescript(int num_parms, char **parms, struct sdm_userdata *u)
427 i = msiv_deletescript(u, parms[1]);
433 cprintf("NO \"no script by that name: %s\"\r\n", parms[1]);
436 cprintf("NO \"can't delete active Script: %s\"\r\n", parms[1]);
440 cprintf("NO \"unexpected parameters.\"\r\n");
447 * \brief Attempt to perform authenticated managesieve
449 void mgsve_auth(char *argbuf) {
450 char username_prompt[64];
452 char encoded_authstring[1024];
455 cprintf("NO \"Already logged in.\"\r\n");
459 extract_token(method, argbuf, 0, ' ', sizeof method);
461 if (!strncasecmp(method, "login", 5) ) {
462 if (strlen(argbuf) >= 7) {
465 CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
466 cprintf("334 %s\r\n", username_prompt);
471 if (!strncasecmp(method, "plain", 5) ) {
472 if (num_tokens(argbuf, ' ') < 2) {
476 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
480 if (strncasecmp(method, "login", 5) ) {
481 cprintf("NO \"Unknown authentication method.\"\r\n");
490 * implements the STARTTLS command (Citadel API version)
492 void _mgsve_starttls(void)
494 char ok_response[SIZ];
495 char nosup_response[SIZ];
496 char error_response[SIZ];
499 "200 2.0.0 Begin TLS negotiation now\r\n");
500 sprintf(nosup_response,
501 "554 5.7.3 TLS not supported here\r\n");
502 sprintf(error_response,
503 "554 5.7.3 Internal error\r\n");
504 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
509 * Main command loop for managesieve sessions.
511 void managesieve_command_loop(void) {
516 struct sdm_userdata u;
517 int changes_made = 0;
519 memset(&u, 0, sizeof(struct sdm_userdata));
522 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
523 length = client_getln(cmdbuf, sizeof cmdbuf);
525 num_parms = old_imap_parameterize(parms, cmdbuf);
526 if (num_parms == 0) return;
527 length = strlen(parms[0]);
530 CtdlLogPrintf(CTDL_CRIT, "Client disconnected: ending session.\n");
534 CtdlLogPrintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
535 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
536 cmd_mgsve_auth(num_parms, parms, &u);
540 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
541 cmd_mgsve_starttls();
544 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
545 cmd_mgsve_logout(&u);
547 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
550 /** these commands need to be authenticated. throw it out if it tries. */
551 else if (CC->logged_in != 0)
554 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
555 cmd_mgsve_havespace();
557 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
558 cmd_mgsve_putscript(num_parms, parms, &u);
561 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
562 cmd_mgsve_listscript(num_parms, parms,&u);
564 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
565 cmd_mgsve_setactive(num_parms, parms,&u);
568 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
569 cmd_mgsve_getscript(num_parms, parms, &u);
571 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
572 cmd_mgsve_deletescript(num_parms, parms, &u);
575 msiv_store(&u, changes_made);
578 cprintf("No Invalid access or command.\r\n");
579 CtdlLogPrintf(CTDL_INFO, "illegal Managesieve command: %s", parms[0]);
587 * This cleanup function blows away the temporary memory and files used by
590 void managesieve_cleanup_function(void) {
592 /* Don't do this stuff if this is not a managesieve session! */
593 if (CC->h_command_function != managesieve_command_loop) return;
595 CtdlLogPrintf(CTDL_DEBUG, "Performing managesieve cleanup hook\n");
601 const char* CitadelServiceManageSieve = "ManageSieve";
602 CTDL_MODULE_INIT(managesieve)
606 CtdlRegisterServiceHook(config.c_managesieve_port,
608 managesieve_greeting,
609 managesieve_command_loop,
611 CitadelServiceManageSieve);
612 CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
615 /* return our Subversion id for the Log */