4 * IMAP server for the Citadel/UX system
5 * Copyright (C) 2000-2002 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * WARNING: Mark Crispin is an idiot. IMAP is the most brain-damaged protocol
9 * you will ever have the profound lack of pleasure to encounter.
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
41 #include <openssl/rand.h>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
50 #include "serv_extensions.h"
57 #include "internet_addressing.h"
58 #include "serv_imap.h"
59 #include "imap_tools.h"
60 #include "imap_fetch.h"
61 #include "imap_search.h"
62 #include "imap_store.h"
63 #include "imap_misc.h"
66 #include "serv_crypto.h"
69 /* imap_rename() uses this struct containing list of rooms to rename */
72 char irl_oldroom[ROOMNAMELEN];
73 char irl_newroom[ROOMNAMELEN];
77 /* Data which is passed between imap_rename() and imap_rename_backend() */
86 * If there is a message ID map in memory, free it
88 void imap_free_msgids(void)
90 if (IMAP->msgids != NULL) {
95 if (IMAP->flags != NULL) {
103 * If there is a transmitted message in memory, free it
105 void imap_free_transmitted_message(void)
107 if (IMAP->transmitted_message != NULL) {
108 phree(IMAP->transmitted_message);
109 IMAP->transmitted_message = NULL;
110 IMAP->transmitted_length = 0;
116 * Set the \\Seen flag for messages which aren't new
118 void imap_set_seen_flags(void)
123 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
124 if (IMAP->num_msgs > 0) {
125 for (i = 0; i < IMAP->num_msgs; ++i) {
126 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
127 IMAP->flags[i] |= IMAP_SEEN;
130 (vbuf.v_answered, IMAP->msgids[i])) {
131 IMAP->flags[i] |= IMAP_ANSWERED;
141 * Back end for imap_load_msgids()
143 * Optimization: instead of calling realloc() to add each message, we
144 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
145 * allows the mapping to proceed much faster.
147 void imap_add_single_msgid(long msgnum, void *userdata)
150 IMAP->num_msgs = IMAP->num_msgs + 1;
151 if (IMAP->msgids == NULL) {
152 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
153 * REALLOC_INCREMENT);
154 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
155 IMAP->msgids = reallok(IMAP->msgids,
157 REALLOC_INCREMENT) * sizeof(long));
159 if (IMAP->flags == NULL) {
160 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
161 * REALLOC_INCREMENT);
162 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
163 IMAP->flags = reallok(IMAP->flags,
165 REALLOC_INCREMENT) * sizeof(long));
167 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
168 IMAP->flags[IMAP->num_msgs - 1] = 0;
174 * Set up a message ID map for the current room (folder)
176 void imap_load_msgids(void)
179 if (IMAP->selected == 0) {
181 "imap_load_msgids() can't run; no room selected\n");
185 imap_free_msgids(); /* If there was already a map, free it */
187 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
188 imap_add_single_msgid, NULL);
190 imap_set_seen_flags();
191 lprintf(9, "imap_load_msgids() mapped %d messages\n",
197 * Re-scan the selected room (folder) and see if it's been changed at all
199 void imap_rescan_msgids(void)
202 int original_num_msgs = 0;
203 long original_highest = 0L;
205 int message_still_exists;
206 struct cdbdata *cdbfr;
207 long *msglist = NULL;
211 if (IMAP->selected == 0) {
213 "imap_load_msgids() can't run; no room selected\n");
217 /* Load the *current* message list from disk, so we can compare it
218 * to what we have in memory.
220 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
222 msglist = mallok(cdbfr->len);
223 memcpy(msglist, cdbfr->ptr, cdbfr->len);
224 num_msgs = cdbfr->len / sizeof(long);
231 * Check to see if any of the messages we know about have been expunged
233 if (IMAP->num_msgs > 0)
234 for (i = 0; i < IMAP->num_msgs; ++i) {
236 message_still_exists = 0;
238 for (j = 0; j < num_msgs; ++j) {
239 if (msglist[j] == IMAP->msgids[i]) {
240 message_still_exists = 1;
244 if (message_still_exists == 0) {
245 cprintf("* %d EXPUNGE\r\n", i + 1);
247 /* Here's some nice stupid nonsense. When a message
248 * is expunged, we have to slide all the existing
249 * messages up in the message array.
252 memcpy(&IMAP->msgids[i],
253 &IMAP->msgids[i + 1],
255 (IMAP->num_msgs - i)));
256 memcpy(&IMAP->flags[i],
259 (IMAP->num_msgs - i)));
267 * Remember how many messages were here before we re-scanned.
269 original_num_msgs = IMAP->num_msgs;
270 if (IMAP->num_msgs > 0) {
271 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
273 original_highest = 0L;
277 * Now peruse the room for *new* messages only.
280 for (j = 0; j < num_msgs; ++j) {
281 if (msglist[j] > original_highest) {
282 imap_add_single_msgid(msglist[j], NULL);
285 imap_set_seen_flags();
288 * If new messages have arrived, tell the client about them.
290 if (IMAP->num_msgs > original_num_msgs) {
291 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
305 * This cleanup function blows away the temporary memory and files used by
308 void imap_cleanup_function(void)
311 /* Don't do this stuff if this is not a IMAP session! */
312 if (CC->h_command_function != imap_command_loop)
315 lprintf(9, "Performing IMAP cleanup hook\n");
317 imap_free_transmitted_message();
318 lprintf(9, "Finished IMAP cleanup hook\n");
324 * Here's where our IMAP session begins its happy day.
326 void imap_greeting(void)
329 strcpy(CC->cs_clientname, "IMAP session");
330 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
331 IMAP->authstate = imap_as_normal;
333 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
339 * implements the LOGIN command (ordinary username/password login)
341 void imap_login(int num_parms, char *parms[])
343 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
344 if (CtdlTryPassword(parms[3]) == pass_ok) {
345 cprintf("%s OK login successful\r\n", parms[0]);
350 cprintf("%s BAD Login incorrect\r\n", parms[0]);
355 * Implements the AUTHENTICATE command
357 void imap_authenticate(int num_parms, char *parms[])
361 if (num_parms != 3) {
362 cprintf("%s BAD incorrect number of parameters\r\n",
368 cprintf("%s BAD Already logged in.\r\n", parms[0]);
372 if (!strcasecmp(parms[2], "LOGIN")) {
373 CtdlEncodeBase64(buf, "Username:", 9);
374 cprintf("+ %s\r\n", buf);
375 IMAP->authstate = imap_as_expecting_username;
376 strcpy(IMAP->authseq, parms[0]);
381 cprintf("%s NO AUTHENTICATE %s failed\r\n",
386 void imap_auth_login_user(char *cmd)
390 CtdlDecodeBase64(buf, cmd, SIZ);
391 CtdlLoginExistingUser(buf);
392 CtdlEncodeBase64(buf, "Password:", 9);
393 cprintf("+ %s\r\n", buf);
394 IMAP->authstate = imap_as_expecting_password;
398 void imap_auth_login_pass(char *cmd)
402 CtdlDecodeBase64(buf, cmd, SIZ);
403 if (CtdlTryPassword(buf) == pass_ok) {
404 cprintf("%s OK authentication succeeded\r\n",
407 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
409 IMAP->authstate = imap_as_normal;
415 * implements the CAPABILITY command
417 void imap_capability(int num_parms, char *parms[])
419 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
421 #ifdef HAVE_OPENSSL_XXX
422 cprintf(" STARTTLS");
426 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
431 * implements the STARTTLS command (lifted-from-Cyrus version)
433 #ifdef HAVE_OPENSSL_XXX
434 void imap_starttls(int num_parms, char *parms[])
438 const char *tls_protocol = NULL;
439 const char *tls_cipher_name = NULL;
440 int tls_cipher_usebits = 0;
441 int tls_cipher_algbits = 0;
445 lprintf(9, "imap_starttls() called\n");
446 tls_conn = (SSL *) SSL_new(ssl_ctx);
447 if (tls_conn == NULL) {
454 /* set the file descriptors for SSL to use */
455 if (SSL_set_fd(tls_conn, CC->client_socket) == 0) {
461 * This is the actual handshake routine. It will do all the negotiations
462 * and will check the client cert etc.
464 SSL_set_accept_state(tls_conn);
466 cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
467 if ((sts = SSL_accept(tls_conn)) <= 0) {
468 SSL_SESSION *session = SSL_get_session(tls_conn);
470 SSL_CTX_remove_session(ssl_ctx, session);
476 tls_protocol = SSL_get_version(tls_conn);
477 cipher = SSL_get_current_cipher(tls_conn);
478 tls_cipher_name = SSL_CIPHER_get_name(cipher);
480 SSL_CIPHER_get_bits(cipher, &tls_cipher_algbits);
482 lprintf(9, "starttls: %s with cipher %s (%d/%d bits %s)\n",
483 tls_protocol, tls_cipher_name,
484 tls_cipher_usebits, tls_cipher_algbits,
485 SSL_session_reused(tls_conn) ? "reused" : "new");
489 /* error; clean up */
492 cprintf("%s NO negotiation failed\r\n", parms[0]);
495 CC->redirect_ssl = 1;
503 * implements the STARTTLS command (original version)
506 void imap_starttls(int num_parms, char *parms[])
508 int retval, bits, alg_bits;
512 cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
515 if (!(CC->ssl = SSL_new(ssl_ctx))) {
516 lprintf(2, "SSL_new failed: %s\n",
517 ERR_reason_error_string(ERR_peek_error()));
518 cprintf("%s NO SSL_new: %s\r\n", parms[0],
519 ERR_reason_error_string(ERR_get_error()));
523 /* Set the options */
524 ssloptions = SSL_get_options(CC->ssl);
525 ssloptions |= SSL_OP_ALL; /* Work around all known bugs */
526 ssloptions |= SSL_OP_NO_SSLv2;
527 ssloptions |= SSL_OP_NO_SSLv3;
528 SSL_set_options(CC->ssl, ssloptions);
530 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
531 lprintf(2, "SSL_set_fd failed: %s\n",
532 ERR_reason_error_string(ERR_peek_error()));
535 cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
536 ERR_reason_error_string(ERR_get_error()));
539 cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
540 retval = SSL_accept(CC->ssl);
543 * Can't notify the client of an error here; they will
544 * discover the problem at the SSL layer and should
545 * revert to unencrypted communications.
549 errval = SSL_get_error(CC->ssl, retval);
550 lprintf(2, "SSL_accept failed: %s\n",
551 ERR_reason_error_string(ERR_get_error()));
556 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
558 SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
560 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
561 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
562 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
564 CC->redirect_ssl = 1;
571 * implements the SELECT command
573 void imap_select(int num_parms, char *parms[])
576 char augmented_roomname[ROOMNAMELEN];
580 struct ctdlroom QRscratch;
586 /* Convert the supplied folder name to a roomname */
587 i = imap_roomname(towhere, sizeof towhere, parms[2]);
589 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
593 floornum = (i & 0x00ff);
594 roomflags = (i & 0xff00);
596 /* First try a regular match */
597 c = getroom(&QRscratch, towhere);
599 /* Then try a mailbox name match */
601 MailboxName(augmented_roomname, sizeof augmented_roomname,
603 c = getroom(&QRscratch, augmented_roomname);
605 strcpy(towhere, augmented_roomname);
608 /* If the room exists, check security/access */
610 /* See if there is an existing user/room relationship */
611 ra = CtdlRoomAccess(&QRscratch, &CC->user);
613 /* normal clients have to pass through security */
619 /* Fail here if no such room */
621 cprintf("%s NO ... no such room, or access denied\r\n",
623 /* IMAP->selected = 0; */
627 /* If we already had some other folder selected, auto-expunge it */
631 * usergoto() formally takes us to the desired room, happily returning
632 * the number of messages and number of new messages.
634 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
635 usergoto(NULL, 0, 0, &msgs, &new);
638 if (!strcasecmp(parms[1], "EXAMINE")) {
646 cprintf("* %d EXISTS\r\n", msgs);
647 cprintf("* %d RECENT\r\n", new);
649 /* Note that \Deleted is a valid flag, but not a permanent flag,
650 * because we don't maintain its state across sessions. Citadel
651 * automatically expunges mailboxes when they are de-selected.
653 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
654 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
655 "permanent flags\r\n");
657 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
658 cprintf("%s OK [%s] %s completed\r\n",
660 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
666 * does the real work for expunge
668 int imap_do_expunge(void)
671 int num_expunged = 0;
673 lprintf(9, "imap_do_expunge() called\n");
674 if (IMAP->selected == 0)
677 if (IMAP->num_msgs > 0)
678 for (i = 0; i < IMAP->num_msgs; ++i) {
679 if (IMAP->flags[i] & IMAP_DELETED) {
680 CtdlDeleteMessages(CC->room.QRname,
681 IMAP->msgids[i], "");
683 lprintf(9, "%ld ... deleted\n",
686 lprintf(9, "%ld ... not deleted\n",
691 if (num_expunged > 0) {
692 imap_rescan_msgids();
695 return (num_expunged);
700 * implements the EXPUNGE command syntax
702 void imap_expunge(int num_parms, char *parms[])
704 int num_expunged = 0;
706 num_expunged = imap_do_expunge();
707 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
712 * implements the CLOSE command
714 void imap_close(int num_parms, char *parms[])
717 /* Yes, we always expunge on close. */
723 cprintf("%s OK CLOSE completed\r\n", parms[0]);
730 * Used by LIST and LSUB to show the floors in the listing
732 void imap_list_floors(char *cmd, char *pattern)
737 for (i = 0; i < MAXFLOORS; ++i) {
739 if (fl->f_flags & F_INUSE) {
740 if (imap_mailbox_matches_pattern
741 (pattern, fl->f_name)) {
742 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
743 imap_strout(fl->f_name);
753 * Back end for imap_lsub()
755 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
756 * may or may not be the desired behavior in the future.
758 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
764 pattern = (char *) data;
766 /* Only list rooms to which the user has access!! */
767 ra = CtdlRoomAccess(qrbuf, &CC->user);
769 imap_mailboxname(buf, sizeof buf, qrbuf);
770 if (imap_mailbox_matches_pattern(pattern, buf)) {
771 cprintf("* LSUB () \"|\" ");
780 * Implements the LSUB command
782 void imap_lsub(int num_parms, char *parms[])
786 cprintf("%s BAD arguments invalid\r\n", parms[0]);
789 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
791 if (strlen(parms[3]) == 0) {
792 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
796 imap_list_floors("LSUB", pattern);
797 ForEachRoom(imap_lsub_listroom, pattern);
800 cprintf("%s OK LSUB completed\r\n", parms[0]);
806 * Back end for imap_list()
808 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
814 pattern = (char *) data;
816 /* Only list rooms to which the user has access!! */
817 ra = CtdlRoomAccess(qrbuf, &CC->user);
819 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
820 imap_mailboxname(buf, sizeof buf, qrbuf);
821 if (imap_mailbox_matches_pattern(pattern, buf)) {
822 cprintf("* LIST () \"|\" ");
831 * Implements the LIST command
833 void imap_list(int num_parms, char *parms[])
837 cprintf("%s BAD arguments invalid\r\n", parms[0]);
840 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
842 if (strlen(parms[3]) == 0) {
843 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
847 imap_list_floors("LIST", pattern);
848 ForEachRoom(imap_list_listroom, pattern);
851 cprintf("%s OK LIST completed\r\n", parms[0]);
857 * Implements the CREATE command
860 void imap_create(int num_parms, char *parms[])
863 char roomname[ROOMNAMELEN];
868 if (strchr(parms[2], '\\') != NULL) {
869 cprintf("%s NO Invalid character in folder name\r\n",
874 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
876 cprintf("%s NO Invalid mailbox name or location\r\n",
880 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
881 flags = (ret & 0xff00); /* upper 8 bits = flags */
883 if (flags & IR_MAILBOX) {
884 newroomtype = 4; /* private mailbox */
886 newroomtype = 0; /* public folder */
889 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
890 roomname, floornum, newroomtype);
892 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
895 ("%s NO Mailbox already exists, or create failed\r\n",
898 cprintf("%s OK CREATE completed\r\n", parms[0]);
904 * Locate a room by its IMAP folder name, and check access to it
906 int imap_grabroom(char *returned_roomname, char *foldername)
909 char augmented_roomname[ROOMNAMELEN];
910 char roomname[ROOMNAMELEN];
912 struct ctdlroom QRscratch;
916 ret = imap_roomname(roomname, sizeof roomname, foldername);
921 /* First try a regular match */
922 c = getroom(&QRscratch, roomname);
924 /* Then try a mailbox name match */
926 MailboxName(augmented_roomname, sizeof augmented_roomname,
927 &CC->user, roomname);
928 c = getroom(&QRscratch, augmented_roomname);
930 strcpy(roomname, augmented_roomname);
933 /* If the room exists, check security/access */
935 /* See if there is an existing user/room relationship */
936 ra = CtdlRoomAccess(&QRscratch, &CC->user);
938 /* normal clients have to pass through security */
944 /* Fail here if no such room */
946 strcpy(returned_roomname, "");
949 strcpy(returned_roomname, QRscratch.QRname);
956 * Implements the STATUS command (sort of)
959 void imap_status(int num_parms, char *parms[])
962 char roomname[ROOMNAMELEN];
964 char savedroom[ROOMNAMELEN];
967 ret = imap_grabroom(roomname, parms[2]);
970 ("%s NO Invalid mailbox name or location, or access denied\r\n",
976 * usergoto() formally takes us to the desired room, happily returning
977 * the number of messages and number of new messages. (If another
978 * folder is selected, save its name so we can return there!!!!!)
980 if (IMAP->selected) {
981 strcpy(savedroom, CC->room.QRname);
983 usergoto(roomname, 0, 0, &msgs, &new);
986 * Tell the client what it wants to know. In fact, tell it *more* than
987 * it wants to know. We happily IGnore the supplied status data item
988 * names and simply spew all possible data items. It's far easier to
989 * code and probably saves us some processing time too.
991 imap_mailboxname(buf, sizeof buf, &CC->room);
992 cprintf("* STATUS ");
994 cprintf(" (MESSAGES %d ", msgs);
995 cprintf("RECENT 0 "); /* FIXME we need to implement this */
996 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
997 cprintf("UNSEEN %d)\r\n", new);
1000 * If another folder is selected, go back to that room so we can resume
1001 * our happy day without violent explosions.
1003 if (IMAP->selected) {
1004 usergoto(savedroom, 0, 0, &msgs, &new);
1008 * Oooh, look, we're done!
1010 cprintf("%s OK STATUS completed\r\n", parms[0]);
1016 * Implements the SUBSCRIBE command
1019 void imap_subscribe(int num_parms, char *parms[])
1022 char roomname[ROOMNAMELEN];
1023 char savedroom[ROOMNAMELEN];
1026 ret = imap_grabroom(roomname, parms[2]);
1029 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1035 * usergoto() formally takes us to the desired room, which has the side
1036 * effect of marking the room as not-zapped ... exactly the effect
1037 * we're looking for.
1039 if (IMAP->selected) {
1040 strcpy(savedroom, CC->room.QRname);
1042 usergoto(roomname, 0, 0, &msgs, &new);
1045 * If another folder is selected, go back to that room so we can resume
1046 * our happy day without violent explosions.
1048 if (IMAP->selected) {
1049 usergoto(savedroom, 0, 0, &msgs, &new);
1052 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1057 * Implements the UNSUBSCRIBE command
1060 void imap_unsubscribe(int num_parms, char *parms[])
1063 char roomname[ROOMNAMELEN];
1064 char savedroom[ROOMNAMELEN];
1067 ret = imap_grabroom(roomname, parms[2]);
1070 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1076 * usergoto() formally takes us to the desired room.
1078 if (IMAP->selected) {
1079 strcpy(savedroom, CC->room.QRname);
1081 usergoto(roomname, 0, 0, &msgs, &new);
1084 * Now make the API call to zap the room
1086 if (CtdlForgetThisRoom() == 0) {
1087 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1090 ("%s NO You may not unsubscribe from this folder.\r\n",
1095 * If another folder is selected, go back to that room so we can resume
1096 * our happy day without violent explosions.
1098 if (IMAP->selected) {
1099 usergoto(savedroom, 0, 0, &msgs, &new);
1106 * Implements the DELETE command
1109 void imap_delete(int num_parms, char *parms[])
1112 char roomname[ROOMNAMELEN];
1113 char savedroom[ROOMNAMELEN];
1116 ret = imap_grabroom(roomname, parms[2]);
1118 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1124 * usergoto() formally takes us to the desired room, happily returning
1125 * the number of messages and number of new messages. (If another
1126 * folder is selected, save its name so we can return there!!!!!)
1128 if (IMAP->selected) {
1129 strcpy(savedroom, CC->room.QRname);
1131 usergoto(roomname, 0, 0, &msgs, &new);
1134 * Now delete the room.
1136 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1137 cprintf("%s OK DELETE completed\r\n", parms[0]);
1138 delete_room(&CC->room);
1140 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1144 * If another folder is selected, go back to that room so we can resume
1145 * our happy day without violent explosions.
1147 if (IMAP->selected) {
1148 usergoto(savedroom, 0, 0, &msgs, &new);
1154 * Back end function for imap_rename()
1156 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1158 char foldername[SIZ];
1159 char newfoldername[SIZ];
1160 char newroomname[ROOMNAMELEN];
1162 struct irl *irlp = NULL; /* scratch pointer */
1163 struct irlparms *irlparms;
1165 irlparms = (struct irlparms *) data;
1166 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1168 /* Rename subfolders */
1169 if ((!strncasecmp(foldername, irlparms->oldname,
1170 strlen(irlparms->oldname))
1171 && (foldername[strlen(irlparms->oldname)] == '|'))) {
1173 sprintf(newfoldername, "%s|%s",
1175 &foldername[strlen(irlparms->oldname) + 1]
1178 newfloor = imap_roomname(newroomname,
1180 newfoldername) & 0xFF;
1182 irlp = (struct irl *) mallok(sizeof(struct irl));
1183 strcpy(irlp->irl_newroom, newroomname);
1184 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1185 irlp->irl_newfloor = newfloor;
1186 irlp->next = *(irlparms->irl);
1187 *(irlparms->irl) = irlp;
1193 * Implements the RENAME command
1196 void imap_rename(int num_parms, char *parms[])
1198 char old_room[ROOMNAMELEN];
1199 char new_room[ROOMNAMELEN];
1203 struct irl *irl = NULL; /* the list */
1204 struct irl *irlp = NULL; /* scratch pointer */
1205 struct irlparms irlparms;
1207 if (strchr(parms[3], '\\') != NULL) {
1208 cprintf("%s NO Invalid character in folder name\r\n",
1213 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1214 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1215 new_floor = (newr & 0xFF);
1217 r = CtdlRenameRoom(old_room, new_room, new_floor);
1219 if (r == crr_room_not_found) {
1220 cprintf("%s NO Could not locate this folder\r\n",
1224 if (r == crr_already_exists) {
1225 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1228 if (r == crr_noneditable) {
1229 cprintf("%s This folder is not editable.\r\n", parms[0]);
1232 if (r == crr_invalid_floor) {
1233 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1236 if (r == crr_access_denied) {
1237 cprintf("%s You do not have permission to edit "
1238 "this folder.\r\n", parms[0]);
1242 cprintf("%s NO Rename failed - undefined error %d\r\n",
1248 /* If this is the INBOX, then RFC2060 says we have to just move the
1249 * contents. In a Citadel environment it's easier to rename the room
1250 * (already did that) and create a new inbox.
1252 if (!strcasecmp(parms[2], "INBOX")) {
1253 create_room(MAILROOM, 4, "", 0, 1, 0);
1256 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1258 irlparms.oldname = parms[2];
1259 irlparms.newname = parms[3];
1260 irlparms.irl = &irl;
1261 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1263 /* ... and now rename them. */
1264 while (irl != NULL) {
1265 r = CtdlRenameRoom(irl->irl_oldroom,
1269 /* FIXME handle error returns better */
1270 lprintf(5, "CtdlRenameRoom() error %d\n",
1279 cprintf("%s OK RENAME completed\r\n", parms[0]);
1286 * Main command loop for IMAP sessions.
1288 void imap_command_loop(void)
1295 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1296 if (client_gets(cmdbuf) < 1) {
1297 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1302 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1303 while (strlen(cmdbuf) < 5)
1304 strcat(cmdbuf, " ");
1307 /* strip off l/t whitespace and CRLF */
1308 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1309 cmdbuf[strlen(cmdbuf) - 1] = 0;
1310 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1311 cmdbuf[strlen(cmdbuf) - 1] = 0;
1314 /* If we're in the middle of a multi-line command, handle that */
1315 if (IMAP->authstate == imap_as_expecting_username) {
1316 imap_auth_login_user(cmdbuf);
1319 if (IMAP->authstate == imap_as_expecting_password) {
1320 imap_auth_login_pass(cmdbuf);
1325 /* Ok, at this point we're in normal command mode. The first thing
1326 * we do is print any incoming pages (yeah! we really do!)
1328 imap_print_express_messages();
1331 * Before processing the command that was just entered... if we happen
1332 * to have a folder selected, we'd like to rescan that folder for new
1333 * messages, and for deletions/changes of existing messages. This
1334 * could probably be optimized somehow, but IMAP sucks...
1336 if (IMAP->selected) {
1337 imap_rescan_msgids();
1340 /* Now for the command set. */
1342 /* Grab the tag, command, and parameters. Check syntax. */
1343 num_parms = imap_parameterize(parms, cmdbuf);
1344 if (num_parms < 2) {
1345 cprintf("BAD syntax error\r\n");
1348 /* The commands below may be executed in any state */
1350 else if ((!strcasecmp(parms[1], "NOOP"))
1351 || (!strcasecmp(parms[1], "CHECK"))) {
1352 cprintf("%s OK This command successfully did nothing.\r\n",
1356 else if (!strcasecmp(parms[1], "LOGOUT")) {
1357 imap_do_expunge(); /* yes, we auto-expunge */
1358 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1359 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1365 else if (!strcasecmp(parms[1], "LOGIN")) {
1366 imap_login(num_parms, parms);
1369 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1370 imap_authenticate(num_parms, parms);
1373 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1374 imap_capability(num_parms, parms);
1377 else if (!strcasecmp(parms[1], "STARTTLS")) {
1378 imap_starttls(num_parms, parms);
1382 else if (!CC->logged_in) {
1383 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1386 /* The commans below require a logged-in state */
1388 else if (!strcasecmp(parms[1], "SELECT")) {
1389 imap_select(num_parms, parms);
1392 else if (!strcasecmp(parms[1], "EXAMINE")) {
1393 imap_select(num_parms, parms);
1396 else if (!strcasecmp(parms[1], "LSUB")) {
1397 imap_lsub(num_parms, parms);
1400 else if (!strcasecmp(parms[1], "LIST")) {
1401 imap_list(num_parms, parms);
1404 else if (!strcasecmp(parms[1], "CREATE")) {
1405 imap_create(num_parms, parms);
1408 else if (!strcasecmp(parms[1], "DELETE")) {
1409 imap_delete(num_parms, parms);
1412 else if (!strcasecmp(parms[1], "RENAME")) {
1413 imap_rename(num_parms, parms);
1416 else if (!strcasecmp(parms[1], "STATUS")) {
1417 imap_status(num_parms, parms);
1420 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1421 imap_subscribe(num_parms, parms);
1424 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1425 imap_unsubscribe(num_parms, parms);
1428 else if (!strcasecmp(parms[1], "APPEND")) {
1429 imap_append(num_parms, parms);
1432 else if (IMAP->selected == 0) {
1433 cprintf("%s BAD no folder selected\r\n", parms[0]);
1436 /* The commands below require the SELECT state on a mailbox */
1438 else if (!strcasecmp(parms[1], "FETCH")) {
1439 imap_fetch(num_parms, parms);
1442 else if ((!strcasecmp(parms[1], "UID"))
1443 && (!strcasecmp(parms[2], "FETCH"))) {
1444 imap_uidfetch(num_parms, parms);
1447 else if (!strcasecmp(parms[1], "SEARCH")) {
1448 imap_search(num_parms, parms);
1451 else if ((!strcasecmp(parms[1], "UID"))
1452 && (!strcasecmp(parms[2], "SEARCH"))) {
1453 imap_uidsearch(num_parms, parms);
1456 else if (!strcasecmp(parms[1], "STORE")) {
1457 imap_store(num_parms, parms);
1460 else if ((!strcasecmp(parms[1], "UID"))
1461 && (!strcasecmp(parms[2], "STORE"))) {
1462 imap_uidstore(num_parms, parms);
1465 else if (!strcasecmp(parms[1], "COPY")) {
1466 imap_copy(num_parms, parms);
1469 else if ((!strcasecmp(parms[1], "UID"))
1470 && (!strcasecmp(parms[2], "COPY"))) {
1471 imap_uidcopy(num_parms, parms);
1474 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1475 imap_expunge(num_parms, parms);
1478 else if (!strcasecmp(parms[1], "CLOSE")) {
1479 imap_close(num_parms, parms);
1482 /* End of commands. If we get here, the command is either invalid
1487 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1490 /* If the client transmitted a message we can free it now */
1491 imap_free_transmitted_message();
1497 * This function is called to register the IMAP extension with Citadel.
1499 char *serv_imap_init(void)
1501 CtdlRegisterServiceHook(config.c_imap_port,
1502 NULL, imap_greeting, imap_command_loop);
1503 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);