4 * This module is an Manage Sieve 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"
76 * http://tools.ietf.org/html/draft-martin-managesieve-06
78 * this is the draft this code tries to implement.
83 int command_state; /**< Information about the current session */
85 struct ctdluser vrfy_buffer;
90 int number_of_recipients;
92 int message_originated_locally;
93 char *transmitted_message; /* for APPEND command... */
94 size_t transmitted_length;
97 enum { /** Command states for login authentication */
106 #define MGSVE CC->MGSVE
108 /*****************************************************************************/
109 /* MANAGESIEVE Server */
110 /*****************************************************************************/
113 void goto_sieverules_room(void)
114 {// TODO: check if we're authenticated.
115 struct ctdlroom QRscratch;
117 char augmented_roomname[ROOMNAMELEN];
120 MailboxName(augmented_roomname, sizeof augmented_roomname,
121 &CC->user, SIEVERULES);
122 c = getroom(&QRscratch, augmented_roomname);
123 if (c != 0)/* something went wrong. hit it! */
129 /* move to the sieve room. */
130 memcpy(&CC->room, &QRscratch,
131 sizeof(struct ctdlroom));
132 usergoto(NULL, 1, transiently, NULL, NULL);
136 * Capability listing. Printed as greeting or on "CAPABILITIES"
137 * see Section 1.8 ; 2.4
139 void cmd_mgsve_caps(void)
141 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve v6.84\"\r\n" /* TODO: put citversion here. */
142 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
144 /* if TLS is already there, should we say that again? */
147 "\"SIEVE\" \"FILEINTO VACATION\"\r\n" /* TODO: print sieve extensions here. */
153 * Here's where our managesieve session begins its happy day.
155 void managesieve_greeting(void) {
157 strcpy(CC->cs_clientname, "Managesieve session");
159 CC->internal_pgm = 1;
160 CC->cs_flags |= CS_STEALTH;
161 MGSVE = malloc(sizeof(struct citmgsve));
162 memset(MGSVE, 0, sizeof(struct citmgsve));
166 /* AUTHENTICATE command; 2.1 */
167 void cmd_mgsve_auth(int num_parms, char **parms)
169 /* TODO: compare "digest-md5" or "gssapi" and answer with "NO" */
170 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
171 /* todo, check length*/
176 /* todo: how to do plain auth? */
178 if (parms[2][0] == '{')
183 literal_length = atol(&parms[2][1]);
184 if (literal_length < 1) {
185 cprintf("NO %s BAD Message length must be at least 1.\n",
190 MGSVE->transmitted_message = malloc(literal_length + 2);
191 if (MGSVE->transmitted_message == NULL) {
192 cprintf("NO %s Cannot allocate memory.\r\n", parms[0]);
196 MGSVE->transmitted_length = literal_length;
198 ret = client_read(MGSVE->transmitted_message, literal_length);
199 MGSVE->transmitted_message[literal_length] = 0;
202 cprintf("%s NO Read failed.\r\n", parms[0]);
206 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
210 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
211 if (login_ok == CtdlLoginExistingUser(auth))
214 pass = &(auth[strlen(auth)+1]);
215 /* for some reason the php script sends us the username twice. y? */
216 pass = &(pass[strlen(pass)+1]);
218 if (pass_ok == CtdlTryPassword(pass))
220 MGSVE->command_state = mgsve_password;
227 cprintf("NO\n");/* we just support auth plain. */
234 /* STARTTLS command chapter 2.2 */
235 void cmd_mgsve_starttls(void)
236 { /* answer with OK, and fire off tls session. */
238 CtdlStartTLS(NULL, NULL, NULL);
245 /* LOGOUT command, see chapter 2.3 */
246 void cmd_mgsve_logout(void)
247 {/* send "OK" and terminate the connection. */
249 lprintf(CTDL_NOTICE, "MgSve bye.");
254 /* HAVESPACE command. see chapter 2.5 */
255 void cmd_mgsve_havespace(void)
257 /* TODO answer NO in any case if auth is missing. */
258 /* as we don't have quotas in citadel we should always answer with OK;
259 * pherhaps we should have a max-scriptsize.
261 if (MGSVE->command_state != mgsve_password)
269 /* citadel doesn't have quotas. in case of change, please add code here. */
274 /* PUTSCRIPT command, see chapter 2.6 */
275 void cmd_mgsve_putscript(void)
277 /* "scriptname" {nnn+} */
278 /* TODO: answer with "NO" instant, if we're unauthorized. */
279 /* AFTER we have the whole script overwrite existing scripts */
280 /* spellcheck the script before overwrite old ones, and reply with "no" */
285 /** forward declaration for function in msgbase.c */
286 void headers_listing(long msgnum, void *userdata);
289 /* LISTSCRIPT command. see chapter 2.7 */
290 void cmd_mgsve_listscript(void)
292 goto_sieverules_room();/* TODO: do we need a template? */
293 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL,
294 headers_listing, NULL);
297 /* TODO: check auth, if not, answer with "no" */
298 /* do something like the sieve room indexlisting, one per row, in quotes. ACTIVE behind the active one.*/
300 /// ra = getroom(sieveroom, SIEVERULES);
301 /// /* Only list rooms to which the user has access!! */
302 /// CtdlRoomAccess(SIEVERULES, &CC->user, &ra, NULL);
303 /// if ((ra & UA_KNOWN)
304 /// || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
305 /// imap_mailboxname(buf, sizeof buf, qrbuf);
306 /// if (imap_mailbox_matches_pattern(pattern, buf)) {
307 /// cprintf("* LIST () \"/\" ");
308 /// imap_strout(buf);
317 /* SETACTIVE command. see chapter 2.8 */
318 void cmd_mgsve_setactive(void)
320 /* TODO: check auth, if not, answer with "no" */
321 /* search our room for subjects with that scriptname,
322 * if the scriptname is empty, use the default flag.
323 * if the script is not there answer "No "there is no script by that name "
331 /* GETSCRIPT command. see chapter 2.9 */
332 void cmd_mgsve_getscript(void)
334 /* TODO: check auth, if not, answer with "no" */
335 /* check first param, this is the name. look up that in the folder.
336 * answer with the size {nnn+}and spill it out, one blank line and OK
342 /* DELETESCRIPT command. see chapter 2.10 */
343 void cmd_mgsve_deletescript(void)
345 /* TODO: check auth, if not, answer with "no" */
354 void smtp_get_user(char *argbuf) {
358 CtdlDecodeBase64(username, argbuf, SIZ);
359 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", username); * /
360 if (CtdlLoginExistingUser(username) == login_ok) {
361 CtdlEncodeBase64(buf, "Password:", 9);
362 cprintf("334 %s\r\n", buf);
363 SMTP->command_state = smtp_password;
366 cprintf("500 5.7.0 No such user.\r\n");
367 SMTP->command_state = smtp_command;
375 void smtp_get_pass(char *argbuf) {
378 CtdlDecodeBase64(password, argbuf, SIZ);
379 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", password); * /
380 if (CtdlTryPassword(password) == pass_ok) {
381 smtp_auth_greeting();
384 cprintf("535 5.7.0 Authentication failed.\r\n");
386 SMTP->command_state = smtp_command;
392 * Back end for PLAIN auth method (either inline or multistate)
394 void mgsve_try_plain(char *encoded_authstring) {
395 char decoded_authstring[1024];
400 CtdlDecodeBase64(decoded_authstring,
402 strlen(encoded_authstring) );
403 safestrncpy(ident, decoded_authstring, sizeof ident);
404 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
405 safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
407 // SMTP->command_state = smtp_command;
409 if (CtdlLoginExistingUser(user) == login_ok) {
410 if (CtdlTryPassword(pass) == pass_ok) {
411 smtp_auth_greeting();
416 cprintf("504 5.7.4 Authentication failed.\r\n");
421 * Attempt to perform authenticated SMTP
423 void mgsve_auth(char *argbuf) {
424 char username_prompt[64];
426 char encoded_authstring[1024];
429 cprintf("504 5.7.4 Already logged in.\r\n");
433 extract_token(method, argbuf, 0, ' ', sizeof method);
435 if (!strncasecmp(method, "login", 5) ) {
436 if (strlen(argbuf) >= 7) {
437 // smtp_get_user(&argbuf[6]);
440 CtdlEncodeBase64(username_prompt, "Username:", 9);
441 cprintf("334 %s\r\n", username_prompt);
442 // SMTP->command_state = smtp_user;
447 if (!strncasecmp(method, "plain", 5) ) {
448 if (num_tokens(argbuf, ' ') < 2) {
450 // SMTP->command_state = smtp_plain;
454 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
456 /// smtp_try_plain(encoded_authstring);
460 if (strncasecmp(method, "login", 5) ) {
461 cprintf("504 5.7.4 Unknown authentication method.\r\n");
470 * implements the STARTTLS command (Citadel API version)
473 void _smtp_starttls(void)
475 char ok_response[SIZ];
476 char nosup_response[SIZ];
477 char error_response[SIZ];
480 "200 2.0.0 Begin TLS negotiation now\r\n");
481 sprintf(nosup_response,
482 "554 5.7.3 TLS not supported here\r\n");
483 sprintf(error_response,
484 "554 5.7.3 Internal error\r\n");
485 CtdlStartTLS(ok_response, nosup_response, error_response);
491 void mgsve_create_room(void)
494 /* Create the tasks list room if it doesn't already exist */
495 create_room(SIEVERULES, 4, "", 0, 1, 0, VIEW_SIEVE);
499 * Main command loop for manage Sieve sessions.
501 void managesieve_command_loop(void) {
508 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
509 length = client_getln(cmdbuf, sizeof cmdbuf);
511 num_parms = imap_parameterize(parms, cmdbuf);
512 /// length = client_getln(parms[0], sizeof parms[0]);
513 length = strlen(parms[0]);
516 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
520 lprintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
521 //// we have different lengths while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
522 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
523 cmd_mgsve_auth(num_parms, parms);
527 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
528 cmd_mgsve_starttls();
531 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
534 else if ((length>= 6) && (!strncasecmp(parms[0], "CAPABILITY", 10))){
536 } /* these commands need to be authenticated. throw it out if it tries. */
537 else if (MGSVE->command_state == mgsve_password)
539 if ((length>= 9) && (!strncasecmp(parms[0], "HAVESPACE", 9))){
540 cmd_mgsve_havespace();
542 else if ((length>= 6) && (!strncasecmp(parms[0], "PUTSCRIPT", 9))){
543 cmd_mgsve_putscript();
545 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
546 cmd_mgsve_listscript();
548 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
549 cmd_mgsve_setactive();
551 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
552 cmd_mgsve_getscript();
554 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
555 cmd_mgsve_deletescript();
573 char *serv_managesieve_init(void)
576 CtdlRegisterServiceHook(config.c_managesieve_port, /* MGSVE */
578 managesieve_greeting,
579 managesieve_command_loop,
582 CtdlRegisterSessionHook(mgsve_create_room, EVT_LOGIN);
583 return "$Id: serv_managesieve.c 4570 2006-08-27 02:07:18Z dothebart $";