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
20 #include <sys/types.h>
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
43 #include "sysdep_decls.h"
44 #include "citserver.h"
48 #include "serv_extensions.h"
55 #include "internet_addressing.h"
56 #include "imap_tools.h"
59 #include "clientsocket.h"
60 #include "locate_host.h"
61 #include "citadel_dirs.h"
64 #include "serv_crypto.h"
74 * http://tools.ietf.org/html/draft-martin-managesieve-06
76 * this is the draft this code tries to implement.
81 int command_state; /**< Information about the current session */
82 char *transmitted_message;
83 size_t transmitted_length;
86 enum { /** Command states for login authentication */
94 #define MGSVE CC->MGSVE
96 /*****************************************************************************/
97 /* MANAGESIEVE Server */
98 /*****************************************************************************/
100 void goto_sieverules_room(void)
101 {// TODO: check if we're authenticated.
102 struct ctdlroom QRscratch;
104 char augmented_roomname[ROOMNAMELEN];
107 MailboxName(augmented_roomname, sizeof augmented_roomname,
108 &CC->user, SIEVERULES);
109 c = getroom(&QRscratch, augmented_roomname);
110 if (c != 0)/* something went wrong. hit it! */
116 /* move to the sieve room. */
117 memcpy(&CC->room, &QRscratch,
118 sizeof(struct ctdlroom));
119 usergoto(NULL, 1, transiently, NULL, NULL);
123 * Capability listing. Printed as greeting or on "CAPABILITIES"
124 * see Section 1.8 ; 2.4
126 void cmd_mgsve_caps(void)
128 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve v6.84\"\r\n" /* TODO: put citversion here. */
129 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
131 /* if TLS is already there, should we say that again? */
134 "\"SIEVE\" \"FILEINTO VACATION\"\r\n" /* TODO: print sieve extensions here. */
140 * Here's where our managesieve session begins its happy day.
142 void managesieve_greeting(void) {
144 strcpy(CC->cs_clientname, "Managesieve session");
146 CC->internal_pgm = 1;
147 CC->cs_flags |= CS_STEALTH;
148 MGSVE = malloc(sizeof(struct citmgsve));
149 memset(MGSVE, 0, sizeof(struct citmgsve));
153 /* AUTHENTICATE command; 2.1 */
154 void cmd_mgsve_auth(int num_parms, char **parms)
156 /* TODO: compare "digest-md5" or "gssapi" and answer with "NO" */
157 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
158 /* todo, check length*/
163 /* todo: how to do plain auth? */
165 if (parms[2][0] == '{')
170 literal_length = atol(&parms[2][1]);
171 if (literal_length < 1) {
172 cprintf("NO %s BAD Message length must be at least 1.\n",
177 MGSVE->transmitted_message = malloc(literal_length + 2);
178 if (MGSVE->transmitted_message == NULL) {
179 cprintf("NO %s Cannot allocate memory.\r\n", parms[0]);
183 MGSVE->transmitted_length = literal_length;
185 ret = client_read(MGSVE->transmitted_message, literal_length);
186 MGSVE->transmitted_message[literal_length] = 0;
189 cprintf("%s NO Read failed.\r\n", parms[0]);
193 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
197 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
198 if (login_ok == CtdlLoginExistingUser(auth))
201 pass = &(auth[strlen(auth)+1]);
202 /* for some reason the php script sends us the username twice. y? */
203 pass = &(pass[strlen(pass)+1]);
205 if (pass_ok == CtdlTryPassword(pass))
207 MGSVE->command_state = mgsve_password;
214 cprintf("NO\n");/* we just support auth plain. */
221 /* STARTTLS command chapter 2.2 */
222 void cmd_mgsve_starttls(void)
223 { /* answer with OK, and fire off tls session. */
225 CtdlStartTLS(NULL, NULL, NULL);
232 /* LOGOUT command, see chapter 2.3 */
233 void cmd_mgsve_logout(void)
234 {/* send "OK" and terminate the connection. */
236 lprintf(CTDL_NOTICE, "MgSve bye.");
241 /* HAVESPACE command. see chapter 2.5 */
242 void cmd_mgsve_havespace(void)
244 /* TODO answer NO in any case if auth is missing. */
245 /* as we don't have quotas in citadel we should always answer with OK;
246 * pherhaps we should have a max-scriptsize.
248 if (MGSVE->command_state != mgsve_password)
256 /* citadel doesn't have quotas. in case of change, please add code here. */
261 /* PUTSCRIPT command, see chapter 2.6 */
262 void cmd_mgsve_putscript(void)
264 /* "scriptname" {nnn+} */
265 /* TODO: answer with "NO" instant, if we're unauthorized. */
266 /* AFTER we have the whole script overwrite existing scripts */
267 /* spellcheck the script before overwrite old ones, and reply with "no" */
272 /** forward declaration for function in msgbase.c */
273 void headers_listing(long msgnum, void *userdata);
276 /* LISTSCRIPT command. see chapter 2.7 */
277 void cmd_mgsve_listscript(void)
279 goto_sieverules_room();/* TODO: do we need a template? */
280 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL,
281 headers_listing, NULL);
284 /* TODO: check auth, if not, answer with "no" */
285 /* do something like the sieve room indexlisting, one per row, in quotes. ACTIVE behind the active one.*/
287 /// ra = getroom(sieveroom, SIEVERULES);
288 /// /* Only list rooms to which the user has access!! */
289 /// CtdlRoomAccess(SIEVERULES, &CC->user, &ra, NULL);
290 /// if ((ra & UA_KNOWN)
291 /// || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
292 /// imap_mailboxname(buf, sizeof buf, qrbuf);
293 /// if (imap_mailbox_matches_pattern(pattern, buf)) {
294 /// cprintf("* LIST () \"/\" ");
295 /// imap_strout(buf);
304 /* SETACTIVE command. see chapter 2.8 */
305 void cmd_mgsve_setactive(void)
307 /* TODO: check auth, if not, answer with "no" */
308 /* search our room for subjects with that scriptname,
309 * if the scriptname is empty, use the default flag.
310 * if the script is not there answer "No "there is no script by that name "
318 /* GETSCRIPT command. see chapter 2.9 */
319 void cmd_mgsve_getscript(void)
321 /* TODO: check auth, if not, answer with "no" */
322 /* check first param, this is the name. look up that in the folder.
323 * answer with the size {nnn+}and spill it out, one blank line and OK
329 /* DELETESCRIPT command. see chapter 2.10 */
330 void cmd_mgsve_deletescript(void)
332 /* TODO: check auth, if not, answer with "no" */
341 void mgsve_get_user(char *argbuf) {
345 CtdlDecodeBase64(username, argbuf, SIZ);
346 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", username); * /
347 if (CtdlLoginExistingUser(username) == login_ok) {
348 CtdlEncodeBase64(buf, "Password:", 9);
349 cprintf("334 %s\r\n", buf);
350 MGSVE->command_state = mgsve_password;
353 cprintf("500 5.7.0 No such user.\r\n");
354 MGSVE->command_state = mgsve_command;
362 void mgsve_get_pass(char *argbuf) {
365 CtdlDecodeBase64(password, argbuf, SIZ);
366 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", password); * /
367 if (CtdlTryPassword(password) == pass_ok) {
368 mgsve_auth_greeting();
371 cprintf("535 5.7.0 Authentication failed.\r\n");
373 MGSVE->command_state = mgsve_command;
379 * Back end for PLAIN auth method (either inline or multistate)
381 void mgsve_try_plain(char *encoded_authstring) {
382 char decoded_authstring[1024];
387 CtdlDecodeBase64(decoded_authstring,
389 strlen(encoded_authstring) );
390 safestrncpy(ident, decoded_authstring, sizeof ident);
391 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
392 safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
394 // MGSVE->command_state = mgsve_command;
396 if (CtdlLoginExistingUser(user) == login_ok) {
397 if (CtdlTryPassword(pass) == pass_ok) {
398 mgsve_auth_greeting();
403 cprintf("504 5.7.4 Authentication failed.\r\n");
408 * Attempt to perform authenticated magagesieve
410 void mgsve_auth(char *argbuf) {
411 char username_prompt[64];
413 char encoded_authstring[1024];
416 cprintf("504 5.7.4 Already logged in.\r\n");
420 extract_token(method, argbuf, 0, ' ', sizeof method);
422 if (!strncasecmp(method, "login", 5) ) {
423 if (strlen(argbuf) >= 7) {
424 // mgsve_get_user(&argbuf[6]);
427 CtdlEncodeBase64(username_prompt, "Username:", 9);
428 cprintf("334 %s\r\n", username_prompt);
429 // MGSVE->command_state = mgsve_user;
434 if (!strncasecmp(method, "plain", 5) ) {
435 if (num_tokens(argbuf, ' ') < 2) {
437 // MGSVE->command_state = mgsve_plain;
441 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
443 /// mgsve_try_plain(encoded_authstring);
447 if (strncasecmp(method, "login", 5) ) {
448 cprintf("504 5.7.4 Unknown authentication method.\r\n");
457 * implements the STARTTLS command (Citadel API version)
460 void _mgsve_starttls(void)
462 char ok_response[SIZ];
463 char nosup_response[SIZ];
464 char error_response[SIZ];
467 "200 2.0.0 Begin TLS negotiation now\r\n");
468 sprintf(nosup_response,
469 "554 5.7.3 TLS not supported here\r\n");
470 sprintf(error_response,
471 "554 5.7.3 Internal error\r\n");
472 CtdlStartTLS(ok_response, nosup_response, error_response);
478 * Create the Sieve script room if it doesn't already exist
480 void mgsve_create_room(void)
482 create_room(SIEVERULES, 4, "", 0, 1, 0, VIEW_SIEVE);
487 * Main command loop for managesieve sessions.
489 void managesieve_command_loop(void) {
496 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
497 length = client_getln(cmdbuf, sizeof cmdbuf);
499 num_parms = imap_parameterize(parms, cmdbuf);
500 /// length = client_getln(parms[0], sizeof parms[0]);
501 length = strlen(parms[0]);
504 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
508 lprintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
509 //// we have different lengths while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
510 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
511 cmd_mgsve_auth(num_parms, parms);
515 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
516 cmd_mgsve_starttls();
519 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
522 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
524 } /* these commands need to be authenticated. throw it out if it tries. */
525 else if (MGSVE->command_state == mgsve_password)
527 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
528 cmd_mgsve_havespace();
530 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
531 cmd_mgsve_putscript();
533 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
534 cmd_mgsve_listscript();
536 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
537 cmd_mgsve_setactive();
539 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
540 cmd_mgsve_getscript();
542 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
543 cmd_mgsve_deletescript();
556 char *serv_managesieve_init(void)
559 CtdlRegisterServiceHook(config.c_managesieve_port, /* MGSVE */
561 managesieve_greeting,
562 managesieve_command_loop,
565 CtdlRegisterSessionHook(mgsve_create_room, EVT_LOGIN);
566 return "$Id: serv_managesieve.c 4570 2006-08-27 02:07:18Z dothebart $";