X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fmodules%2Fimap%2Fserv_imap.c;h=3d5d2ac7bb8c685ae36235295c0dccf4b4fb15dc;hb=8c47559cb5ae97ec0fa35660ee16fd61a9451c72;hp=b7d44b5579310712d81e9c87422925e29a341e52;hpb=a605e70c6ecfccfe650a85ef4a32ebbf9cd4385f;p=citadel.git diff --git a/citadel/modules/imap/serv_imap.c b/citadel/modules/imap/serv_imap.c index b7d44b557..3d5d2ac7b 100644 --- a/citadel/modules/imap/serv_imap.c +++ b/citadel/modules/imap/serv_imap.c @@ -2,12 +2,26 @@ * $Id$ * * IMAP server for the Citadel system - * Copyright (C) 2000-2007 by Art Cancro and others. + * Copyright (C) 2000-2009 by Art Cancro and others. * This code is released under the terms of the GNU General Public License. * * WARNING: the IMAP protocol is badly designed. No implementation of it * is perfect. Indeed, with so much gratuitous complexity, *all* IMAP * implementations have bugs. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "sysdep.h" @@ -35,6 +49,7 @@ #include #include #include +#include #include "citadel.h" #include "server.h" #include "citserver.h" @@ -45,7 +60,6 @@ #include "policy.h" #include "database.h" #include "msgbase.h" -#include "tools.h" #include "internet_addressing.h" #include "serv_imap.h" #include "imap_tools.h" @@ -227,7 +241,7 @@ void imap_load_msgids(void) struct cdbdata *cdbfr; if (IMAP->selected == 0) { - lprintf(CTDL_ERR, + CtdlLogPrintf(CTDL_ERR, "imap_load_msgids() can't run; no room selected\n"); return; } @@ -269,7 +283,7 @@ void imap_rescan_msgids(void) int num_recent = 0; if (IMAP->selected == 0) { - lprintf(CTDL_ERR, + CtdlLogPrintf(CTDL_ERR, "imap_load_msgids() can't run; no room selected\n"); return; } @@ -290,7 +304,7 @@ void imap_rescan_msgids(void) if (cdbfr != NULL) { msglist = malloc(cdbfr->len); if (msglist == NULL) { - lprintf(CTDL_CRIT, "malloc() failed\n"); + CtdlLogPrintf(CTDL_CRIT, "malloc() failed\n"); abort(); } memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len); @@ -407,7 +421,7 @@ void imap_cleanup_function(void) imap_do_expunge(); } - lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n"); + CtdlLogPrintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n"); imap_free_msgids(); imap_free_transmitted_message(); @@ -426,7 +440,7 @@ void imap_cleanup_function(void) } free(IMAP); - lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n"); + CtdlLogPrintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n"); } @@ -435,7 +449,7 @@ void imap_cleanup_function(void) * output this stuff in other places as well) */ void imap_output_capability_string(void) { - cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=PLAIN AUTH=LOGIN"); + cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=PLAIN AUTH=LOGIN UIDPLUS"); #ifdef HAVE_OPENSSL if (!CC->redirect_ssl) cprintf(" STARTTLS"); @@ -447,11 +461,20 @@ void imap_output_capability_string(void) { /* We are building a partial implementation of METADATA for the sole purpose * of interoperating with the ical/vcard version of the Bynari Insight Connector. - * If you were expecting something else, comment out one or both of these - * extension advertisements. + * It is not a full RFC5464 implementation, but it should refuse non-Bynari + * metadata in a compatible and graceful way. */ cprintf(" METADATA"); - /* cprintf(" LIST-EXTENDED"); */ + + /* + * LIST-EXTENDED was originally going to be required by the METADATA extension. + * It was mercifully removed prior to the finalization of RFC5464. We started + * implementing this but stopped when we learned that it would not be needed. + * If you uncomment this declaration you are responsible for writing a lot of new + * code. + * + * cprintf(" LIST-EXTENDED") + */ } /* @@ -491,13 +514,19 @@ void imap_greeting(void) { strcpy(CC->cs_clientname, "IMAP session"); - IMAP = malloc(sizeof (struct citimap)); + CC->session_specific_data = malloc(sizeof(struct citimap)); memset(IMAP, 0, sizeof(struct citimap)); IMAP->authstate = imap_as_normal; IMAP->cached_rfc822_data = NULL; IMAP->cached_rfc822_msgnum = (-1); IMAP->cached_rfc822_withbody = 0; + if (CC->nologin) + { + cprintf("* BYE; Server busy, try later\r\n"); + CC->kill_me = 1; + return; + } cprintf("* OK ["); imap_output_capability_string(); cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL); @@ -557,7 +586,7 @@ void imap_authenticate(int num_parms, char *parms[]) } if (!strcasecmp(parms[2], "LOGIN")) { - CtdlEncodeBase64(buf, "Username:", 9); + CtdlEncodeBase64(buf, "Username:", 9, 0); cprintf("+ %s\r\n", buf); IMAP->authstate = imap_as_expecting_username; strcpy(IMAP->authseq, parms[0]); @@ -565,7 +594,7 @@ void imap_authenticate(int num_parms, char *parms[]) } if (!strcasecmp(parms[2], "PLAIN")) { - // CtdlEncodeBase64(buf, "Username:", 9); + // CtdlEncodeBase64(buf, "Username:", 9, 0); // cprintf("+ %s\r\n", buf); cprintf("+ \r\n"); IMAP->authstate = imap_as_expecting_plainauth; @@ -587,6 +616,7 @@ void imap_auth_plain(char *cmd) char pass[256]; int result; + memset(pass, 0, sizeof(pass)); CtdlDecodeBase64(decoded_authstring, cmd, strlen(cmd)); safestrncpy(ident, decoded_authstring, sizeof ident); safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user); @@ -616,7 +646,7 @@ void imap_auth_login_user(char *cmd) CtdlDecodeBase64(buf, cmd, SIZ); CtdlLoginExistingUser(NULL, buf); - CtdlEncodeBase64(buf, "Password:", 9); + CtdlEncodeBase64(buf, "Password:", 9, 0); cprintf("+ %s\r\n", buf); IMAP->authstate = imap_as_expecting_password; return; @@ -626,6 +656,7 @@ void imap_auth_login_pass(char *cmd) { char buf[SIZ]; + memset(buf, 0, sizeof(buf)); CtdlDecodeBase64(buf, cmd, SIZ); if (CtdlTryPassword(buf) == pass_ok) { cprintf("%s OK authentication succeeded\r\n", IMAP->authseq); @@ -741,13 +772,17 @@ void imap_select(int num_parms, char *parms[]) cprintf("* OK [UIDVALIDITY %ld] UID validity status\r\n", GLOBAL_UIDVALIDITY_VALUE); cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1); - /* Note that \Deleted is a valid flag, but not a permanent flag, + /* Technically, \Deleted is a valid flag, but not a permanent flag, * because we don't maintain its state across sessions. Citadel * automatically expunges mailboxes when they are de-selected. + * + * Unfortunately, omitting \Deleted as a PERMANENTFLAGS flag causes + * some clients (particularly Thunderbird) to misbehave -- they simply + * elect not to transmit the flag at all. So we have to advertise + * \Deleted as a PERMANENTFLAGS flag, even though it technically isn't. */ cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n"); - cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] " - "permanent flags\r\n"); + cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] permanent flags\r\n"); cprintf("%s OK [%s] %s completed\r\n", parms[0], @@ -766,7 +801,7 @@ int imap_do_expunge(void) long *delmsgs = NULL; int num_delmsgs = 0; - lprintf(CTDL_DEBUG, "imap_do_expunge() called\n"); + CtdlLogPrintf(CTDL_DEBUG, "imap_do_expunge() called\n"); if (IMAP->selected == 0) { return (0); } @@ -789,7 +824,7 @@ int imap_do_expunge(void) imap_rescan_msgids(); } - lprintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n", + CtdlLogPrintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n", num_expunged, CC->room.QRname); return (num_expunged); } @@ -877,11 +912,16 @@ void imap_create(int num_parms, char *parms[]) int flags; int newroomtype = 0; int newroomview = 0; + char *notification_message = NULL; + + if (num_parms < 3) { + cprintf("%s NO A foder name must be specified\r\n", parms[0]); + return; + } if (strchr(parms[2], '\\') != NULL) { - cprintf("%s NO Invalid character in folder name\r\n", - parms[0]); - lprintf(CTDL_DEBUG, "invalid character in folder name\n"); + cprintf("%s NO Invalid character in folder name\r\n", parms[0]); + CtdlLogPrintf(CTDL_DEBUG, "invalid character in folder name\n"); return; } @@ -889,7 +929,7 @@ void imap_create(int num_parms, char *parms[]) if (ret < 0) { cprintf("%s NO Invalid mailbox name or location\r\n", parms[0]); - lprintf(CTDL_DEBUG, "invalid mailbox name or location\n"); + CtdlLogPrintf(CTDL_DEBUG, "invalid mailbox name or location\n"); return; } floornum = (ret & 0x00ff); /* lower 8 bits = floor number */ @@ -898,7 +938,7 @@ void imap_create(int num_parms, char *parms[]) if (flags & IR_MAILBOX) { if (strncasecmp(parms[2], "INBOX/", 6)) { cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]); - lprintf(CTDL_DEBUG, "not subordinate to inbox\n"); + CtdlLogPrintf(CTDL_DEBUG, "not subordinate to inbox\n"); return; } } @@ -911,7 +951,7 @@ void imap_create(int num_parms, char *parms[]) newroomview = VIEW_BBS; } - lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n", + CtdlLogPrintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n", roomname, floornum, newroomtype); ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview); @@ -920,8 +960,20 @@ void imap_create(int num_parms, char *parms[]) cprintf("%s NO Mailbox already exists, or create failed\r\n", parms[0]); } else { cprintf("%s OK CREATE completed\r\n", parms[0]); + /* post a message in Aide> describing the new room */ + notification_message = malloc(1024); + snprintf(notification_message, 1024, + "A new room called \"%s\" has been created by %s%s%s%s\n", + roomname, + CC->user.fullname, + ((ret & QR_MAILBOX) ? " [personal]" : ""), + ((ret & QR_PRIVATE) ? " [private]" : ""), + ((ret & QR_GUESSNAME) ? " [hidden]" : "") + ); + aide_message(notification_message, "Room Creation Message"); + free(notification_message); } - lprintf(CTDL_DEBUG, "imap_create() completed\n"); + CtdlLogPrintf(CTDL_DEBUG, "imap_create() completed\n"); } @@ -993,7 +1045,7 @@ void imap_status(int num_parms, char *parms[]) char savedroom[ROOMNAMELEN]; int msgs, new; - ret = imap_grabroom(roomname, parms[2], 0); + ret = imap_grabroom(roomname, parms[2], 1); if (ret != 0) { cprintf ("%s NO Invalid mailbox name or location, or access denied\r\n", @@ -1095,7 +1147,7 @@ void imap_unsubscribe(int num_parms, char *parms[]) char savedroom[ROOMNAMELEN]; int msgs, new; - ret = imap_grabroom(roomname, parms[2], 0); + ret = imap_grabroom(roomname, parms[2], 1); if (ret != 0) { cprintf ("%s NO Invalid mailbox name or location, or access denied\r\n", @@ -1234,6 +1286,7 @@ void imap_rename(int num_parms, char *parms[]) struct irl *irl = NULL; /* the list */ struct irl *irlp = NULL; /* scratch pointer */ struct irlparms irlparms; + char buf[1024]; if (strchr(parms[3], '\\') != NULL) { cprintf("%s NO Invalid character in folder name\r\n", @@ -1253,20 +1306,20 @@ void imap_rename(int num_parms, char *parms[]) return; } if (r == crr_already_exists) { - cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]); + cprintf("%s NO '%s' already exists.\r\n", parms[0], parms[2]); return; } if (r == crr_noneditable) { - cprintf("%s This folder is not editable.\r\n", parms[0]); + cprintf("%s NO This folder is not editable.\r\n", parms[0]); return; } if (r == crr_invalid_floor) { - cprintf("%s Folder root does not exist.\r\n", parms[0]); + cprintf("%s NO Folder root does not exist.\r\n", parms[0]); return; } if (r == crr_access_denied) { - cprintf("%s You do not have permission to edit " - "this folder.\r\n", parms[0]); + cprintf("%s NO You do not have permission to edit this folder.\r\n", + parms[0]); return; } if (r != crr_ok) { @@ -1275,7 +1328,6 @@ void imap_rename(int num_parms, char *parms[]) return; } - /* If this is the INBOX, then RFC2060 says we have to just move the * contents. In a Citadel environment it's easier to rename the room * (already did that) and create a new inbox. @@ -1298,7 +1350,7 @@ void imap_rename(int num_parms, char *parms[]) irl->irl_newfloor); if (r != crr_ok) { /* FIXME handle error returns better */ - lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r); + CtdlLogPrintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r); } irlp = irl; irl = irl->next; @@ -1306,6 +1358,13 @@ void imap_rename(int num_parms, char *parms[]) } } + snprintf(buf, sizeof buf, "IMAP folder \"%s\" renamed to \"%s\" by %s\n", + parms[2], + parms[3], + CC->curr_user + ); + aide_message(buf, "IMAP folder rename"); + cprintf("%s OK RENAME completed\r\n", parms[0]); } @@ -1322,28 +1381,29 @@ void imap_command_loop(void) int num_parms; struct timeval tv1, tv2; suseconds_t total_time = 0; + int untagged_ok = 1; gettimeofday(&tv1, NULL); CC->lastcmd = time(NULL); memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */ flush_output(); if (client_getln(cmdbuf, sizeof cmdbuf) < 1) { - lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n"); + CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n"); CC->kill_me = 1; return; } if (IMAP->authstate == imap_as_expecting_password) { - lprintf(CTDL_INFO, "IMAP: \n"); + CtdlLogPrintf(CTDL_INFO, "IMAP: \n"); } else if (IMAP->authstate == imap_as_expecting_plainauth) { - lprintf(CTDL_INFO, "IMAP: \n"); + CtdlLogPrintf(CTDL_INFO, "IMAP: \n"); } else if (bmstrcasestr(cmdbuf, " LOGIN ")) { - lprintf(CTDL_INFO, "IMAP: LOGIN...\n"); + CtdlLogPrintf(CTDL_INFO, "IMAP: LOGIN...\n"); } else { - lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf); + CtdlLogPrintf(CTDL_INFO, "IMAP: %s\n", cmdbuf); } while (strlen(cmdbuf) < 5) @@ -1370,25 +1430,45 @@ void imap_command_loop(void) return; } - /* Ok, at this point we're in normal command mode. The first thing - * we do is print any incoming pages (yeah! we really do!) - */ - imap_print_instant_messages(); - /* - * Before processing the command that was just entered... if we happen - * to have a folder selected, we'd like to rescan that folder for new - * messages, and for deletions/changes of existing messages. This - * could probably be optimized somehow, but IMAP sucks... + /* Ok, at this point we're in normal command mode. + * If the command just submitted does not contain a literal, we + * might think about delivering some untagged stuff... */ - if (IMAP->selected) { - imap_rescan_msgids(); + if (cmdbuf[strlen(cmdbuf)-1] == '}') { + untagged_ok = 0; + } + + /* Grab the tag, command, and parameters. */ + num_parms = imap_parameterize(parms, cmdbuf); + + /* RFC3501 says that we cannot output untagged data during these commands */ + if (num_parms >= 2) { + if ( (!strcasecmp(parms[1], "FETCH")) + || (!strcasecmp(parms[1], "STORE")) + || (!strcasecmp(parms[1], "SEARCH")) + ) { + untagged_ok = 0; + } + } + + if (untagged_ok) { + + /* we can put any additional untagged stuff right here in the future */ + + /* + * Before processing the command that was just entered... if we happen + * to have a folder selected, we'd like to rescan that folder for new + * messages, and for deletions/changes of existing messages. This + * could probably be optimized better with some deep thought... + */ + if (IMAP->selected) { + imap_rescan_msgids(); + } } /* Now for the command set. */ - /* Grab the tag, command, and parameters. Check syntax. */ - num_parms = imap_parameterize(parms, cmdbuf); if (num_parms < 2) { cprintf("BAD syntax error\r\n"); } @@ -1408,7 +1488,7 @@ void imap_command_loop(void) else if (!strcasecmp(parms[1], "LOGOUT")) { if (IMAP->selected) { - imap_do_expunge(); /* yes, we auto-expunge */ + imap_do_expunge(); /* yes, we auto-expunge at logout */ } cprintf("* BYE %s logging out\r\n", config.c_fqdn); cprintf("%s OK Citadel IMAP session ended.\r\n", @@ -1581,7 +1661,8 @@ void imap_command_loop(void) gettimeofday(&tv2, NULL); total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000)); - lprintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n", + CtdlLogPrintf(CTDL_INFO, "IMAP: %s\n", cmdbuf); // FIXME FIXME FIXME REMOVE THIS NOW + CtdlLogPrintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n", (total_time / 1000000), (total_time % 1000000) ); @@ -1596,14 +1677,17 @@ const char *CitadelServiceIMAPS="IMAPS"; */ CTDL_MODULE_INIT(imap) { - CtdlRegisterServiceHook(config.c_imap_port, - NULL, imap_greeting, imap_command_loop, NULL, CitadelServiceIMAP); + if (!threading) + { + CtdlRegisterServiceHook(config.c_imap_port, + NULL, imap_greeting, imap_command_loop, NULL, CitadelServiceIMAP); #ifdef HAVE_OPENSSL - CtdlRegisterServiceHook(config.c_imaps_port, - NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS); + CtdlRegisterServiceHook(config.c_imaps_port, + NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS); #endif - CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP); - + CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP); + } + /* return our Subversion id for the Log */ return "$Id$"; }