/*
* $Id$
*
- * IMAP server for the Citadel/UX system
+ * IMAP server for the Citadel system
* Copyright (C) 2000-2002 by Art Cancro and others.
* This code is released under the terms of the GNU General Public License.
*
#include <ctype.h>
#include <string.h>
#include <limits.h>
-
-#ifdef HAVE_OPENSSL
-#include <openssl/ssl.h>
-#include <openssl/err.h>
-#include <openssl/rand.h>
-#endif
-
#include "citadel.h"
#include "server.h"
#include "sysdep_decls.h"
void imap_free_msgids(void)
{
if (IMAP->msgids != NULL) {
- phree(IMAP->msgids);
+ free(IMAP->msgids);
IMAP->msgids = NULL;
IMAP->num_msgs = 0;
}
if (IMAP->flags != NULL) {
- phree(IMAP->flags);
+ free(IMAP->flags);
IMAP->flags = NULL;
}
}
void imap_free_transmitted_message(void)
{
if (IMAP->transmitted_message != NULL) {
- phree(IMAP->transmitted_message);
+ free(IMAP->transmitted_message);
IMAP->transmitted_message = NULL;
IMAP->transmitted_length = 0;
}
IMAP->num_msgs = IMAP->num_msgs + 1;
if (IMAP->msgids == NULL) {
- IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
+ IMAP->msgids = malloc(IMAP->num_msgs * sizeof(long)
* REALLOC_INCREMENT);
} else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
- IMAP->msgids = reallok(IMAP->msgids,
+ IMAP->msgids = realloc(IMAP->msgids,
(IMAP->num_msgs +
REALLOC_INCREMENT) * sizeof(long));
}
if (IMAP->flags == NULL) {
- IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
+ IMAP->flags = malloc(IMAP->num_msgs * sizeof(long)
* REALLOC_INCREMENT);
} else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
- IMAP->flags = reallok(IMAP->flags,
+ IMAP->flags = realloc(IMAP->flags,
(IMAP->num_msgs +
REALLOC_INCREMENT) * sizeof(long));
}
{
if (IMAP->selected == 0) {
- lprintf(5,
+ lprintf(CTDL_ERR,
"imap_load_msgids() can't run; no room selected\n");
return;
}
imap_add_single_msgid, NULL);
imap_set_seen_flags();
- lprintf(9, "imap_load_msgids() mapped %d messages\n",
+ lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
IMAP->num_msgs);
}
if (IMAP->selected == 0) {
- lprintf(5,
+ lprintf(CTDL_ERR,
"imap_load_msgids() can't run; no room selected\n");
return;
}
*/
cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
if (cdbfr != NULL) {
- msglist = mallok(cdbfr->len);
+ msglist = malloc(cdbfr->len);
memcpy(msglist, cdbfr->ptr, cdbfr->len);
num_msgs = cdbfr->len / sizeof(long);
cdb_free(cdbfr);
}
if (num_msgs != 0)
- phree(msglist);
+ free(msglist);
}
if (CC->h_command_function != imap_command_loop)
return;
- lprintf(9, "Performing IMAP cleanup hook\n");
+ /* If there is a mailbox selected, auto-expunge it. */
+ if (IMAP->selected) {
+ imap_do_expunge();
+ }
+
+ lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
imap_free_msgids();
imap_free_transmitted_message();
- lprintf(9, "Finished IMAP cleanup hook\n");
+
+ if (IMAP->cached_fetch != NULL) {
+ fclose(IMAP->cached_fetch);
+ IMAP->cached_fetch = NULL;
+ IMAP->cached_msgnum = (-1);
+ }
+
+ if (IMAP->cached_body != NULL) {
+ fclose(IMAP->cached_body);
+ IMAP->cached_body = NULL;
+ IMAP->cached_bodymsgnum = (-1);
+ }
+
+ lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
}
strcpy(CC->cs_clientname, "IMAP session");
CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
IMAP->authstate = imap_as_normal;
+ IMAP->cached_fetch = NULL;
+ IMAP->cached_msgnum = (-1);
- cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
+ cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
config.c_fqdn);
}
*/
void imap_capability(int num_parms, char *parms[])
{
- cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
+ cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
-#ifdef HAVE_OPENSSL_XXX_DISABLED
+#ifdef HAVE_OPENSSL
cprintf(" STARTTLS");
#endif
}
-/*
- * implements the STARTTLS command (lifted-from-Cyrus version)
- */
-#ifdef HAVE_OPENSSL
-void imap_starttls(int num_parms, char *parms[])
-{
- int sts;
- SSL_CIPHER *cipher;
- const char *tls_protocol = NULL;
- const char *tls_cipher_name = NULL;
- int tls_cipher_usebits = 0;
- int tls_cipher_algbits = 0;
- SSL *tls_conn;
- int r = 0;
-
- lprintf(9, "imap_starttls() called\n");
- tls_conn = (SSL *) SSL_new(ssl_ctx);
- if (tls_conn == NULL) {
- CC->ssl = NULL;
- r = -1;
- goto done;
- }
- SSL_clear(tls_conn);
-
- /* set the file descriptors for SSL to use */
- if (SSL_set_fd(tls_conn, CC->client_socket) == 0) {
- r = -1;
- goto done;
- }
-
- /*
- * This is the actual handshake routine. It will do all the negotiations
- * and will check the client cert etc.
- */
- SSL_set_accept_state(tls_conn);
-
- cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
- if ((sts = SSL_accept(tls_conn)) <= 0) {
- SSL_SESSION *session = SSL_get_session(tls_conn);
- if (session) {
- SSL_CTX_remove_session(ssl_ctx, session);
- }
- r = -1;
- goto done;
- }
-
- tls_protocol = SSL_get_version(tls_conn);
- cipher = SSL_get_current_cipher(tls_conn);
- tls_cipher_name = SSL_CIPHER_get_name(cipher);
- tls_cipher_usebits =
- SSL_CIPHER_get_bits(cipher, &tls_cipher_algbits);
-
- lprintf(9, "starttls: %s with cipher %s (%d/%d bits %s)\n",
- tls_protocol, tls_cipher_name,
- tls_cipher_usebits, tls_cipher_algbits,
- SSL_session_reused(tls_conn) ? "reused" : "new");
-
-done:
- if (r && tls_conn) {
- /* error; clean up */
- SSL_free(tls_conn);
- tls_conn = NULL;
- cprintf("%s NO negotiation failed\r\n", parms[0]);
- } else {
- CC->ssl = tls_conn;
- CC->redirect_ssl = 1;
- }
-}
-
-#endif
-
/*
- * implements the STARTTLS command (original version)
+ * implements the STARTTLS command (Citadel API version)
*/
-#ifdef HAVE_OPENSSL_XXX
+#ifdef HAVE_OPENSSL
void imap_starttls(int num_parms, char *parms[])
{
- int retval, bits, alg_bits;
- long ssloptions;
-
- if (!ssl_ctx) {
- cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
- return;
- }
- if (!(CC->ssl = SSL_new(ssl_ctx))) {
- lprintf(2, "SSL_new failed: %s\n",
- ERR_reason_error_string(ERR_peek_error()));
- cprintf("%s NO SSL_new: %s\r\n", parms[0],
- ERR_reason_error_string(ERR_get_error()));
- return;
- }
-
- /* Set the options */
- ssloptions = SSL_get_options(CC->ssl);
- ssloptions |= SSL_OP_ALL; /* Work around all known bugs */
- ssloptions |= SSL_OP_NO_SSLv2;
- ssloptions |= SSL_OP_NO_SSLv3;
- SSL_set_options(CC->ssl, ssloptions);
-
- if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
- lprintf(2, "SSL_set_fd failed: %s\n",
- ERR_reason_error_string(ERR_peek_error()));
- SSL_free(CC->ssl);
- CC->ssl = NULL;
- cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
- ERR_reason_error_string(ERR_get_error()));
- return;
- }
- cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
- retval = SSL_accept(CC->ssl);
- if (retval < 1) {
- /*
- * Can't notify the client of an error here; they will
- * discover the problem at the SSL layer and should
- * revert to unencrypted communications.
- */
- long errval;
-
- errval = SSL_get_error(CC->ssl, retval);
- lprintf(2, "SSL_accept failed: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- SSL_free(CC->ssl);
- CC->ssl = NULL;
- return;
- }
- BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
- bits =
- SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
- &alg_bits);
- lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
- SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
- SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
- bits, alg_bits);
- CC->redirect_ssl = 1;
+ char ok_response[SIZ];
+ char nosup_response[SIZ];
+ char error_response[SIZ];
+
+ sprintf(ok_response,
+ "%s OK begin TLS negotiation now\r\n",
+ parms[0]);
+ sprintf(nosup_response,
+ "%s NO TLS not supported here\r\n",
+ parms[0]);
+ sprintf(error_response,
+ "%s BAD Internal error\r\n",
+ parms[0]);
+ CtdlStartTLS(ok_response, nosup_response, error_response);
}
#endif
-
/*
* implements the SELECT command
*/
if (!ok) {
cprintf("%s NO ... no such room, or access denied\r\n",
parms[0]);
- /* IMAP->selected = 0; */
return;
}
int i;
int num_expunged = 0;
- lprintf(9, "imap_do_expunge() called\n");
- if (IMAP->selected == 0)
+ lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
+ if (IMAP->selected == 0) {
return (0);
+ }
if (IMAP->num_msgs > 0)
for (i = 0; i < IMAP->num_msgs; ++i) {
CtdlDeleteMessages(CC->room.QRname,
IMAP->msgids[i], "");
++num_expunged;
- lprintf(9, "%ld ... deleted\n",
- IMAP->msgids[i]);
- } else {
- lprintf(9, "%ld ... not deleted\n",
- IMAP->msgids[i]);
}
}
imap_rescan_msgids();
}
+ lprintf(9, "Expunged %d messages.\n", num_expunged);
return (num_expunged);
}
{
/* Yes, we always expunge on close. */
- imap_do_expunge();
+ if (IMAP->selected) {
+ imap_do_expunge();
+ }
IMAP->selected = 0;
IMAP->readonly = 0;
}
+/*
+ * Implements the NAMESPACE command.
+ */
+void imap_namespace(int num_parms, char *parms[])
+{
+ int i;
+ struct floor *fl;
+ int floors = 0;
+ char buf[SIZ];
+
+ cprintf("* NAMESPACE ");
+
+ /* All personal folders are subordinate to INBOX. */
+ cprintf("((\"INBOX/\" \"/\")) ");
+
+ /* Other users' folders ... coming soon! FIXME */
+ cprintf("NIL ");
+
+ /* Show all floors as shared namespaces. Neato! */
+ cprintf("(");
+ for (i = 0; i < MAXFLOORS; ++i) {
+ fl = cgetfloor(i);
+ if (fl->f_flags & F_INUSE) {
+ if (floors > 0) cprintf(" ");
+ cprintf("(");
+ sprintf(buf, "%s/", fl->f_name);
+ imap_strout(buf);
+ cprintf(" \"/\")");
+ ++floors;
+ }
+ }
+ cprintf(")");
+
+ /* Wind it up with a newline and a completion message. */
+ cprintf("\r\n");
+ cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
+}
+
/*
if (fl->f_flags & F_INUSE) {
if (imap_mailbox_matches_pattern
(pattern, fl->f_name)) {
- cprintf("* %s (\\NoSelect) \"|\" ", cmd);
+ cprintf("* %s (\\NoSelect) \"/\" ", cmd);
imap_strout(fl->f_name);
cprintf("\r\n");
}
if (ra & UA_KNOWN) {
imap_mailboxname(buf, sizeof buf, qrbuf);
if (imap_mailbox_matches_pattern(pattern, buf)) {
- cprintf("* LSUB () \"|\" ");
+ cprintf("* LSUB () \"/\" ");
imap_strout(buf);
cprintf("\r\n");
}
snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
if (strlen(parms[3]) == 0) {
- cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
+ cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
}
else {
|| ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
imap_mailboxname(buf, sizeof buf, qrbuf);
if (imap_mailbox_matches_pattern(pattern, buf)) {
- cprintf("* LIST () \"|\" ");
+ cprintf("* LIST () \"/\" ");
imap_strout(buf);
cprintf("\r\n");
}
snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
if (strlen(parms[3]) == 0) {
- cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
+ cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
}
else {
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");
return;
}
if (ret < 0) {
cprintf("%s NO Invalid mailbox name or location\r\n",
parms[0]);
+ lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
return;
}
floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
flags = (ret & 0xff00); /* upper 8 bits = flags */
+ 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");
+ return;
+ }
+ }
+
if (flags & IR_MAILBOX) {
newroomtype = 4; /* private mailbox */
} else {
newroomtype = 0; /* public folder */
}
- lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
+ lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
roomname, floornum, newroomtype);
ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
} else {
cprintf("%s OK CREATE completed\r\n", parms[0]);
}
+ lprintf(CTDL_DEBUG, "imap_create() completed\n");
}
/* Rename subfolders */
if ((!strncasecmp(foldername, irlparms->oldname,
strlen(irlparms->oldname))
- && (foldername[strlen(irlparms->oldname)] == '|'))) {
+ && (foldername[strlen(irlparms->oldname)] == '/'))) {
- sprintf(newfoldername, "%s|%s",
+ sprintf(newfoldername, "%s/%s",
irlparms->newname,
&foldername[strlen(irlparms->oldname) + 1]
);
sizeof newroomname,
newfoldername) & 0xFF;
- irlp = (struct irl *) mallok(sizeof(struct irl));
+ irlp = (struct irl *) malloc(sizeof(struct irl));
strcpy(irlp->irl_newroom, newroomname);
strcpy(irlp->irl_oldroom, qrbuf->QRname);
irlp->irl_newfloor = newfloor;
irl->irl_newfloor);
if (r != crr_ok) {
/* FIXME handle error returns better */
- lprintf(5, "CtdlRenameRoom() error %d\n",
+ lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
r);
}
irlp = irl;
irl = irl->next;
- phree(irlp);
+ free(irlp);
}
}
time(&CC->lastcmd);
memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
if (client_gets(cmdbuf) < 1) {
- lprintf(3, "IMAP socket is broken. Ending session.\r\n");
+ lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
CC->kill_me = 1;
return;
}
- lprintf(5, "IMAP: %s\r\n", cmdbuf);
+ lprintf(CTDL_INFO, "IMAP: %s\r\n", cmdbuf);
while (strlen(cmdbuf) < 5)
strcat(cmdbuf, " ");
/* 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_express_messages();
+ imap_print_instant_messages();
/*
* Before processing the command that was just entered... if we happen
}
else if (!strcasecmp(parms[1], "LOGOUT")) {
- imap_do_expunge(); /* yes, we auto-expunge */
+ if (IMAP->selected) {
+ imap_do_expunge(); /* yes, we auto-expunge */
+ }
cprintf("* BYE %s logging out\r\n", config.c_fqdn);
cprintf("%s OK thank you for using Citadel IMAP\r\n",
parms[0]);
imap_starttls(num_parms, parms);
}
#endif
-
else if (!CC->logged_in) {
cprintf("%s BAD Not logged in.\r\n", parms[0]);
}
imap_append(num_parms, parms);
}
+ else if (!strcasecmp(parms[1], "NAMESPACE")) {
+ imap_namespace(num_parms, parms);
+ }
+
else if (IMAP->selected == 0) {
cprintf("%s BAD no folder selected\r\n", parms[0]);
}
char *serv_imap_init(void)
{
CtdlRegisterServiceHook(config.c_imap_port,
- NULL, imap_greeting, imap_command_loop);
+ NULL, imap_greeting, imap_command_loop, NULL);
CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
return "$Id$";
}