2 * $Id: serv_smtp.c 4570 2006-08-27 02:07:18Z ajc $
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 ////int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
108 #define MGSVE CC->MGSVE
110 /*****************************************************************************/
111 /* MANAGESIEVE Server */
112 /*****************************************************************************/
115 void goto_sieverules_room(void);
116 {// TODO: check if we're authenticated.
117 struct ctdlroom QRscratch;
122 char augmented_roomname[ROOMNAMELEN];
124 MailboxName(augmented_roomname, sizeof augmented_roomname,
125 &CC->user, SIEVERULES);
126 c = getroom(&QRscratch, augmented_roomname);
127 if (c != 0)/* something went wrong. hit it! */
134 /* move to the sieve room. */
135 memcpy(&CC->room, &QRscratch,
136 sizeof(struct ctdlroom));
137 usergoto(NULL, 1, transiently, NULL, NULL);
141 * Capability listing. Printed as greeting or on "CAPABILITIES"
142 * see Section 1.8 ; 2.4
144 void cmd_mgsve_caps(void)
146 cprintf("\"IMPLEMENTATION\" \"CITADEL Sieve v6.84\"\r\n" /* TODO: put citversion here. */
147 "\"SASL\" \"PLAIN\"\r\n" /*DIGEST-MD5 GSSAPI SASL sucks.*/
149 /* if TLS is already there, should we say that again? */
152 "\"SIEVE\" \"FILEINTO VACATION\"\r\n" /* TODO: print sieve extensions here. */
158 * Here's where our SMTP session begins its happy day.
160 void managesieve_greeting(void) {
162 strcpy(CC->cs_clientname, "Managesieve session");
164 CC->internal_pgm = 1;
165 CC->cs_flags |= CS_STEALTH;
166 MGSVE = malloc(sizeof(struct citmgsve));
167 memset(MGSVE, 0, sizeof(struct citmgsve));
171 /* AUTHENTICATE command; 2.1 */
172 void cmd_mgsve_auth(int num_parms, char **parms)
174 /* TODO: compare "digest-md5" or "gssapi" and answer with "NO" */
175 if ((num_parms == 3) && !strncasecmp(parms[1], "PLAIN", 5))
176 /* todo, check length*/
181 /* todo: how to do plain auth? */
183 if (parms[2][0] == '{')
188 literal_length = atol(&parms[2][1]);
189 if (literal_length < 1) {
190 cprintf("NO %s BAD Message length must be at least 1.\n",
195 MGSVE->transmitted_message = malloc(literal_length + 1);
196 if (MGSVE->transmitted_message == NULL) {
197 cprintf("NO %s Cannot allocate memory.\r\n", parms[0]);
201 MGSVE->transmitted_length = literal_length;
203 ret = client_read(MGSVE->transmitted_message, literal_length);
204 MGSVE->transmitted_message[literal_length] = 0;
207 cprintf("%s NO Read failed.\r\n", parms[0]);
211 retval = CtdlDecodeBase64(auth, MGSVE->transmitted_message, SIZ);
215 retval = CtdlDecodeBase64(auth, parms[2], SIZ);
216 if (login_ok == CtdlLoginExistingUser(auth))
219 pass = &(auth[strlen(auth)+1]);
220 /* for some reason the php script sends us the username twice. y? */
221 pass = &(pass[strlen(pass)+1]);
223 CtdlTryPassword(pass);
225 MGSVE->command_state = mgsve_password;
235 cprintf("NO\n");/* we just support auth plain. */
241 switch (MGSVE->command_state)
245 MGSVE->command_state = plain;
252 /* STARTTLS command chapter 2.2 */
253 void cmd_mgsve_starttls(void)
254 { /* answer with OK, and fire off tls session. */
256 CtdlStartTLS(NULL, NULL, NULL);
263 /* LOGOUT command, see chapter 2.3 */
264 void cmd_mgsve_logout(void)
265 {/* send "OK" and terminate the connection. */
267 lprintf(CTDL_NOTICE, "MgSve bye.");
272 /* HAVESPACE command. see chapter 2.5 */
273 void cmd_mgsve_havespace(void)
275 /* TODO answer NO in any case if auth is missing. */
276 /* as we don't have quotas in citadel we should always answer with OK;
277 * pherhaps we should have a max-scriptsize.
279 if (MGSVE->command_state != mgsve_password)
287 /* citadel doesn't have quotas. in case of change, please add code here. */
292 /* PUTSCRIPT command, see chapter 2.6 */
293 void cmd_mgsve_putscript(void)
295 /* "scriptname" {nnn+} */
296 /* TODO: answer with "NO" instant, if we're unauthorized. */
297 /* AFTER we have the whole script overwrite existing scripts */
298 /* spellcheck the script before overwrite old ones, and reply with "no" */
303 /* LISTSCRIPT command. see chapter 2.7 */
304 void cmd_mgsve_listscript(void)
307 QRsieve = goto_sieverules_room();
313 /* TODO: check auth, if not, answer with "no" */
314 /* do something like the sieve room indexlisting, one per row, in quotes. ACTIVE behind the active one.*/
316 /// ra = getroom(sieveroom, SIEVERULES);
317 /// /* Only list rooms to which the user has access!! */
318 /// CtdlRoomAccess(SIEVERULES, &CC->user, &ra, NULL);
319 /// if ((ra & UA_KNOWN)
320 /// || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
321 /// imap_mailboxname(buf, sizeof buf, qrbuf);
322 /// if (imap_mailbox_matches_pattern(pattern, buf)) {
323 /// cprintf("* LIST () \"/\" ");
324 /// imap_strout(buf);
333 /* SETACTIVE command. see chapter 2.8 */
334 void cmd_mgsve_setactive(void)
336 /* TODO: check auth, if not, answer with "no" */
337 /* search our room for subjects with that scriptname,
338 * if the scriptname is empty, use the default flag.
339 * if the script is not there answer "No "there is no script by that name "
347 /* GETSCRIPT command. see chapter 2.9 */
348 void cmd_mgsve_getscript(void)
350 /* TODO: check auth, if not, answer with "no" */
351 /* check first param, this is the name. look up that in the folder.
352 * answer with the size {nnn+}and spill it out, one blank line and OK
358 /* DELETESCRIPT command. see chapter 2.10 */
359 void cmd_mgsve_deletescript(void)
361 /* TODO: check auth, if not, answer with "no" */
370 void smtp_get_user(char *argbuf) {
374 CtdlDecodeBase64(username, argbuf, SIZ);
375 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", username); * /
376 if (CtdlLoginExistingUser(username) == login_ok) {
377 CtdlEncodeBase64(buf, "Password:", 9);
378 cprintf("334 %s\r\n", buf);
379 SMTP->command_state = smtp_password;
382 cprintf("500 5.7.0 No such user.\r\n");
383 SMTP->command_state = smtp_command;
391 void smtp_get_pass(char *argbuf) {
394 CtdlDecodeBase64(password, argbuf, SIZ);
395 / * lprintf(CTDL_DEBUG, "Trying <%s>\n", password); * /
396 if (CtdlTryPassword(password) == pass_ok) {
397 smtp_auth_greeting();
400 cprintf("535 5.7.0 Authentication failed.\r\n");
402 SMTP->command_state = smtp_command;
408 * Back end for PLAIN auth method (either inline or multistate)
410 void mgsve_try_plain(char *encoded_authstring) {
411 char decoded_authstring[1024];
416 CtdlDecodeBase64(decoded_authstring,
418 strlen(encoded_authstring) );
419 safestrncpy(ident, decoded_authstring, sizeof ident);
420 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
421 safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
423 // SMTP->command_state = smtp_command;
425 if (CtdlLoginExistingUser(user) == login_ok) {
426 if (CtdlTryPassword(pass) == pass_ok) {
427 smtp_auth_greeting();
432 cprintf("504 5.7.4 Authentication failed.\r\n");
437 * Attempt to perform authenticated SMTP
439 void mgsve_auth(char *argbuf) {
440 char username_prompt[64];
442 char encoded_authstring[1024];
445 cprintf("504 5.7.4 Already logged in.\r\n");
449 extract_token(method, argbuf, 0, ' ', sizeof method);
451 if (!strncasecmp(method, "login", 5) ) {
452 if (strlen(argbuf) >= 7) {
453 // smtp_get_user(&argbuf[6]);
456 CtdlEncodeBase64(username_prompt, "Username:", 9);
457 cprintf("334 %s\r\n", username_prompt);
458 // SMTP->command_state = smtp_user;
463 if (!strncasecmp(method, "plain", 5) ) {
464 if (num_tokens(argbuf, ' ') < 2) {
466 // SMTP->command_state = smtp_plain;
470 extract_token(encoded_authstring, argbuf, 1, ' ', sizeof encoded_authstring);
472 /// smtp_try_plain(encoded_authstring);
476 if (strncasecmp(method, "login", 5) ) {
477 cprintf("504 5.7.4 Unknown authentication method.\r\n");
486 * implements the STARTTLS command (Citadel API version)
489 void _smtp_starttls(void)
491 char ok_response[SIZ];
492 char nosup_response[SIZ];
493 char error_response[SIZ];
496 "200 2.0.0 Begin TLS negotiation now\r\n");
497 sprintf(nosup_response,
498 "554 5.7.3 TLS not supported here\r\n");
499 sprintf(error_response,
500 "554 5.7.3 Internal error\r\n");
501 CtdlStartTLS(ok_response, nosup_response, error_response);
507 void mgsve_create_room(void)
510 /* Create the tasks list room if it doesn't already exist */
511 create_room(SIEVERULES, 4, "", 0, 1, 0, VIEW_SIEVE);
515 * Main command loop for manage Sieve sessions.
517 void managesieve_command_loop(void) {
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 /// length = client_getln(parms[0], sizeof parms[0]);
529 length = strlen(parms[0]);
532 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
536 lprintf(CTDL_INFO, "MANAGESIEVE: %s\n", cmdbuf);
537 //// we have different lengths while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
538 if ((length>= 12) && (!strncasecmp(parms[0], "AUTHENTICATE", 12))){
539 cmd_mgsve_auth(num_parms, parms);
543 else if ((length>= 8) && (!strncasecmp(parms[0], "STARTTLS", 8))){
544 cmd_mgsve_starttls();
547 else if ((length>= 6) && (!strncasecmp(parms[0], "LOGOUT", 6))){
550 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 (MGSVE->command_state == mgsve_password)
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();
561 else if ((length>= 6) && (!strncasecmp(parms[0], "LISTSCRIPT", 10))){
562 cmd_mgsve_listscript();
564 else if ((length>= 6) && (!strncasecmp(parms[0], "SETACTIVE", 9))){
565 cmd_mgsve_setactive();
567 else if ((length>= 6) && (!strncasecmp(parms[0], "GETSCRIPT", 9))){
568 cmd_mgsve_getscript();
570 else if ((length>= 6) && (!strncasecmp(parms[0], "DELETESCRIPT", 11))){
571 cmd_mgsve_deletescript();
589 char *serv_managesieve_init(void)
592 CtdlRegisterServiceHook(config.c_managesieve_port, /* MGSVE */
594 managesieve_greeting,
595 managesieve_command_loop,
598 CtdlRegisterSessionHook(mgsve_create_room, EVT_LOGIN);
599 return "$Id: serv_managesieve.c 4570 2006-08-27 02:07:18Z dothebart $";