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>
38 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
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"
65 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
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) {
89 if (IMAP->msgids != NULL) {
94 if (IMAP->flags != NULL) {
102 * If there is a transmitted message in memory, free it
104 void imap_free_transmitted_message(void) {
105 if (IMAP->transmitted_message != NULL) {
106 phree(IMAP->transmitted_message);
107 IMAP->transmitted_message = NULL;
108 IMAP->transmitted_length = 0;
114 * Set the \\Seen flag for messages which aren't new
116 void imap_set_seen_flags(void) {
120 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
121 if (IMAP->num_msgs > 0) {
122 for (i=0; i<IMAP->num_msgs; ++i) {
123 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
124 IMAP->flags[i] |= IMAP_SEEN;
126 if (is_msg_in_mset(vbuf.v_answered, IMAP->msgids[i])) {
127 IMAP->flags[i] |= IMAP_ANSWERED;
137 * Back end for imap_load_msgids()
139 * Optimization: instead of calling realloc() to add each message, we
140 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
141 * allows the mapping to proceed much faster.
143 void imap_add_single_msgid(long msgnum, void *userdata) {
145 IMAP->num_msgs = IMAP->num_msgs + 1;
146 if (IMAP->msgids == NULL) {
147 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
148 * REALLOC_INCREMENT);
150 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
151 IMAP->msgids = reallok(IMAP->msgids,
152 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
154 if (IMAP->flags == NULL) {
155 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
156 * REALLOC_INCREMENT);
158 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
159 IMAP->flags = reallok(IMAP->flags,
160 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
162 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
163 IMAP->flags[IMAP->num_msgs - 1] = 0;
169 * Set up a message ID map for the current room (folder)
171 void imap_load_msgids(void) {
173 if (IMAP->selected == 0) {
174 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
178 imap_free_msgids(); /* If there was already a map, free it */
180 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
181 imap_add_single_msgid, NULL);
183 imap_set_seen_flags();
184 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
189 * Re-scan the selected room (folder) and see if it's been changed at all
191 void imap_rescan_msgids(void) {
193 int original_num_msgs = 0;
194 long original_highest = 0L;
196 int message_still_exists;
197 struct cdbdata *cdbfr;
198 long *msglist = NULL;
202 if (IMAP->selected == 0) {
203 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
207 /* Load the *current* message list from disk, so we can compare it
208 * to what we have in memory.
210 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
212 msglist = mallok(cdbfr->len);
213 memcpy(msglist, cdbfr->ptr, cdbfr->len);
214 num_msgs = cdbfr->len / sizeof(long);
222 * Check to see if any of the messages we know about have been expunged
224 if (IMAP->num_msgs > 0)
225 for (i=0; i<IMAP->num_msgs; ++i) {
227 message_still_exists = 0;
228 if (num_msgs > 0) for (j = 0; j < num_msgs; ++j) {
229 if (msglist[j] == IMAP->msgids[i]) {
230 message_still_exists = 1;
234 if (message_still_exists == 0) {
235 cprintf("* %d EXPUNGE\r\n", i+1);
237 /* Here's some nice stupid nonsense. When a message
238 * is expunged, we have to slide all the existing
239 * messages up in the message array.
242 memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
243 (sizeof(long)*(IMAP->num_msgs-i)) );
244 memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
245 (sizeof(long)*(IMAP->num_msgs-i)) );
253 * Remember how many messages were here before we re-scanned.
255 original_num_msgs = IMAP->num_msgs;
256 if (IMAP->num_msgs > 0) {
257 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
260 original_highest = 0L;
264 * Now peruse the room for *new* messages only.
266 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
267 if (msglist[j] > original_highest) {
268 imap_add_single_msgid(msglist[j], NULL);
271 imap_set_seen_flags();
274 * If new messages have arrived, tell the client about them.
276 if (IMAP->num_msgs > original_num_msgs) {
277 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
280 if (num_msgs != 0) phree(msglist);
290 * This cleanup function blows away the temporary memory and files used by
293 void imap_cleanup_function(void) {
295 /* Don't do this stuff if this is not a IMAP session! */
296 if (CC->h_command_function != imap_command_loop) return;
298 lprintf(9, "Performing IMAP cleanup hook\n");
300 imap_free_transmitted_message();
301 lprintf(9, "Finished IMAP cleanup hook\n");
307 * Here's where our IMAP session begins its happy day.
309 void imap_greeting(void) {
311 strcpy(CC->cs_clientname, "IMAP session");
312 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
313 IMAP->authstate = imap_as_normal;
315 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
321 * implements the LOGIN command (ordinary username/password login)
323 void imap_login(int num_parms, char *parms[]) {
324 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
325 if (CtdlTryPassword(parms[3]) == pass_ok) {
326 cprintf("%s OK login successful\r\n", parms[0]);
331 cprintf("%s BAD Login incorrect\r\n", parms[0]);
336 * Implements the AUTHENTICATE command
338 void imap_authenticate(int num_parms, char *parms[]) {
340 char plain_authident[SIZ];
341 char plain_username[SIZ];
342 char plain_password[SIZ];
344 if (!strcasecmp(parms[2], "LOGIN")) {
345 if (num_parms != 3) {
346 cprintf("%s BAD incorrect number of parameters\r\n",
350 CtdlEncodeBase64(buf, "Username:", 9);
351 cprintf("+ %s\r\n", buf);
352 IMAP->authstate = imap_as_expecting_username;
353 strcpy(IMAP->authseq, parms[0]);
357 else if (!strcasecmp(parms[2], "PLAIN")) {
358 if (num_parms != 4) {
359 cprintf("%s BAD incorrect number of parameters\r\n",
364 cprintf("%s BAD Already logged in.\r\n", parms[0]);
367 strcpy(plain_authident, parms[3]);
368 strcpy(plain_username, &parms[3][strlen(plain_authident)+2]);
369 strcpy(plain_password, &parms[3][strlen(plain_authident)+strlen(plain_username)+3]);
370 if (CtdlLoginExistingUser(plain_username) == login_ok) {
371 if (CtdlTryPassword(plain_password) == pass_ok) {
372 cprintf("%s OK login successful\r\n", parms[0]);
376 cprintf("%s BAD i=%s u=%s p=%s FIXME\r\n", parms[0],
377 plain_authident, plain_username, plain_password);
381 cprintf("%s NO AUTHENTICATE %s failed\r\n",
386 void imap_auth_login_user(char *cmd) {
389 CtdlDecodeBase64(buf, cmd, SIZ);
390 CtdlLoginExistingUser(buf);
391 CtdlEncodeBase64(buf, "Password:", 9);
392 cprintf("+ %s\r\n", buf);
393 IMAP->authstate = imap_as_expecting_password;
397 void imap_auth_login_pass(char *cmd) {
400 CtdlDecodeBase64(buf, cmd, SIZ);
401 if (CtdlTryPassword(buf) == pass_ok) {
402 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
405 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
407 IMAP->authstate = imap_as_normal;
414 * implements the CAPABILITY command
416 void imap_capability(int num_parms, char *parms[]) {
417 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=PLAIN AUTH=LOGIN");
419 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
420 cprintf(" STARTTLS");
424 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
429 * implements the STARTTLS command
431 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
432 void imap_starttls(int num_parms, char *parms[]) {
433 int retval, bits, alg_bits;
436 cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
439 if (!(CC->ssl = SSL_new(ssl_ctx))) {
440 lprintf(2, "SSL_new failed: %s\n",
441 ERR_reason_error_string(ERR_peek_error()));
442 cprintf("%s NO SSL_new: %s\r\n", parms[0],
443 ERR_reason_error_string(ERR_get_error()));
446 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
447 lprintf(2, "SSL_set_fd failed: %s\n",
448 ERR_reason_error_string(ERR_peek_error()));
451 cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
452 ERR_reason_error_string(ERR_get_error()));
455 cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
456 retval = SSL_accept(CC->ssl);
459 * Can't notify the client of an error here; they will
460 * discover the problem at the SSL layer and should
461 * revert to unencrypted communications.
465 errval = SSL_get_error(CC->ssl, retval);
466 lprintf(2, "SSL_accept failed: %s\n",
467 ERR_reason_error_string(ERR_get_error()));
472 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
473 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
474 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
475 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
476 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
478 CC->redirect_ssl = 1;
485 * implements the SELECT command
487 void imap_select(int num_parms, char *parms[]) {
489 char augmented_roomname[ROOMNAMELEN];
493 struct ctdlroom QRscratch;
499 /* Convert the supplied folder name to a roomname */
500 i = imap_roomname(towhere, sizeof towhere, parms[2]);
502 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
506 floornum = (i & 0x00ff);
507 roomflags = (i & 0xff00);
509 /* First try a regular match */
510 c = getroom(&QRscratch, towhere);
512 /* Then try a mailbox name match */
514 MailboxName(augmented_roomname, sizeof augmented_roomname,
516 c = getroom(&QRscratch, augmented_roomname);
518 strcpy(towhere, augmented_roomname);
521 /* If the room exists, check security/access */
523 /* See if there is an existing user/room relationship */
524 ra = CtdlRoomAccess(&QRscratch, &CC->user);
526 /* normal clients have to pass through security */
532 /* Fail here if no such room */
534 cprintf("%s NO ... no such room, or access denied\r\n",
536 /* IMAP->selected = 0; */
540 /* If we already had some other folder selected, auto-expunge it */
544 * usergoto() formally takes us to the desired room, happily returning
545 * the number of messages and number of new messages.
547 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
548 usergoto(NULL, 0, 0, &msgs, &new);
551 if (!strcasecmp(parms[1], "EXAMINE")) {
560 cprintf("* %d EXISTS\r\n", msgs);
561 cprintf("* %d RECENT\r\n", new);
563 /* Note that \Deleted is a valid flag, but not a permanent flag,
564 * because we don't maintain its state across sessions. Citadel
565 * automatically expunges mailboxes when they are de-selected.
567 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
568 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
569 "permanent flags\r\n");
571 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
572 cprintf("%s OK [%s] %s completed\r\n",
574 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
581 * does the real work for expunge
583 int imap_do_expunge(void) {
585 int num_expunged = 0;
587 lprintf(9, "imap_do_expunge() called\n");
588 if (IMAP->selected == 0) return(0);
590 if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
591 if (IMAP->flags[i] & IMAP_DELETED) {
592 CtdlDeleteMessages(CC->room.QRname,
593 IMAP->msgids[i], "");
595 lprintf(9, "%ld ... deleted\n", IMAP->msgids[i]);
598 lprintf(9, "%ld ... not deleted\n", IMAP->msgids[i]);
602 if (num_expunged > 0) {
603 imap_rescan_msgids();
606 return(num_expunged);
611 * implements the EXPUNGE command syntax
613 void imap_expunge(int num_parms, char *parms[]) {
614 int num_expunged = 0;
616 num_expunged = imap_do_expunge();
617 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
622 * implements the CLOSE command
624 void imap_close(int num_parms, char *parms[]) {
626 /* Yes, we always expunge on close. */
632 cprintf("%s OK CLOSE completed\r\n", parms[0]);
639 * Used by LIST and LSUB to show the floors in the listing
641 void imap_list_floors(char *cmd, char *pattern) {
645 for (i=0; i<MAXFLOORS; ++i) {
647 if (fl->f_flags & F_INUSE) {
648 if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
649 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
650 imap_strout(fl->f_name);
660 * Back end for imap_lsub()
662 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
663 * may or may not be the desired behavior in the future.
665 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data) {
670 pattern = (char *)data;
672 /* Only list rooms to which the user has access!! */
673 ra = CtdlRoomAccess(qrbuf, &CC->user);
675 imap_mailboxname(buf, sizeof buf, qrbuf);
676 if (imap_mailbox_matches_pattern(pattern, buf)) {
677 cprintf("* LSUB () \"|\" ");
686 * Implements the LSUB command
688 void imap_lsub(int num_parms, char *parms[]) {
691 cprintf("%s BAD arguments invalid\r\n", parms[0]);
694 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
696 if (strlen(parms[3])==0) {
697 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
701 imap_list_floors("LSUB", pattern);
702 ForEachRoom(imap_lsub_listroom, pattern);
705 cprintf("%s OK LSUB completed\r\n", parms[0]);
711 * Back end for imap_list()
713 void imap_list_listroom(struct ctdlroom *qrbuf, void *data) {
718 pattern = (char *)data;
720 /* Only list rooms to which the user has access!! */
721 ra = CtdlRoomAccess(qrbuf, &CC->user);
723 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
724 imap_mailboxname(buf, sizeof buf, qrbuf);
725 if (imap_mailbox_matches_pattern(pattern, buf)) {
726 cprintf("* LIST () \"|\" ");
735 * Implements the LIST command
737 void imap_list(int num_parms, char *parms[]) {
740 cprintf("%s BAD arguments invalid\r\n", parms[0]);
743 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
745 if (strlen(parms[3])==0) {
746 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
750 imap_list_floors("LIST", pattern);
751 ForEachRoom(imap_list_listroom, pattern);
754 cprintf("%s OK LIST completed\r\n", parms[0]);
760 * Implements the CREATE command
763 void imap_create(int num_parms, char *parms[]) {
765 char roomname[ROOMNAMELEN];
770 if (strchr(parms[2], '\\') != NULL) {
771 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
775 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
777 cprintf("%s NO Invalid mailbox name or location\r\n",
781 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
782 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
784 if (flags & IR_MAILBOX) {
785 newroomtype = 4; /* private mailbox */
788 newroomtype = 0; /* public folder */
791 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
792 roomname, floornum, newroomtype);
794 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
796 cprintf("%s NO Mailbox already exists, or create failed\r\n",
800 cprintf("%s OK CREATE completed\r\n", parms[0]);
806 * Locate a room by its IMAP folder name, and check access to it
808 int imap_grabroom(char *returned_roomname, char *foldername) {
810 char augmented_roomname[ROOMNAMELEN];
811 char roomname[ROOMNAMELEN];
813 struct ctdlroom QRscratch;
817 ret = imap_roomname(roomname, sizeof roomname, foldername);
822 /* First try a regular match */
823 c = getroom(&QRscratch, roomname);
825 /* Then try a mailbox name match */
827 MailboxName(augmented_roomname, sizeof augmented_roomname,
828 &CC->user, roomname);
829 c = getroom(&QRscratch, augmented_roomname);
831 strcpy(roomname, augmented_roomname);
834 /* If the room exists, check security/access */
836 /* See if there is an existing user/room relationship */
837 ra = CtdlRoomAccess(&QRscratch, &CC->user);
839 /* normal clients have to pass through security */
845 /* Fail here if no such room */
847 strcpy(returned_roomname, "");
851 strcpy(returned_roomname, QRscratch.QRname);
858 * Implements the STATUS command (sort of)
861 void imap_status(int num_parms, char *parms[]) {
863 char roomname[ROOMNAMELEN];
865 char savedroom[ROOMNAMELEN];
868 ret = imap_grabroom(roomname, parms[2]);
870 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
876 * usergoto() formally takes us to the desired room, happily returning
877 * the number of messages and number of new messages. (If another
878 * folder is selected, save its name so we can return there!!!!!)
880 if (IMAP->selected) {
881 strcpy(savedroom, CC->room.QRname);
883 usergoto(roomname, 0, 0, &msgs, &new);
886 * Tell the client what it wants to know. In fact, tell it *more* than
887 * it wants to know. We happily IGnore the supplied status data item
888 * names and simply spew all possible data items. It's far easier to
889 * code and probably saves us some processing time too.
891 imap_mailboxname(buf, sizeof buf, &CC->room);
892 cprintf("* STATUS ");
894 cprintf(" (MESSAGES %d ", msgs);
895 cprintf("RECENT 0 "); /* FIXME we need to implement this */
896 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
897 cprintf("UNSEEN %d)\r\n", new);
900 * If another folder is selected, go back to that room so we can resume
901 * our happy day without violent explosions.
903 if (IMAP->selected) {
904 usergoto(savedroom, 0, 0, &msgs, &new);
908 * Oooh, look, we're done!
910 cprintf("%s OK STATUS completed\r\n", parms[0]);
916 * Implements the SUBSCRIBE command
919 void imap_subscribe(int num_parms, char *parms[]) {
921 char roomname[ROOMNAMELEN];
922 char savedroom[ROOMNAMELEN];
925 ret = imap_grabroom(roomname, parms[2]);
927 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
933 * usergoto() formally takes us to the desired room, which has the side
934 * effect of marking the room as not-zapped ... exactly the effect
937 if (IMAP->selected) {
938 strcpy(savedroom, CC->room.QRname);
940 usergoto(roomname, 0, 0, &msgs, &new);
943 * If another folder is selected, go back to that room so we can resume
944 * our happy day without violent explosions.
946 if (IMAP->selected) {
947 usergoto(savedroom, 0, 0, &msgs, &new);
950 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
955 * Implements the UNSUBSCRIBE command
958 void imap_unsubscribe(int num_parms, char *parms[]) {
960 char roomname[ROOMNAMELEN];
961 char savedroom[ROOMNAMELEN];
964 ret = imap_grabroom(roomname, parms[2]);
966 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
972 * usergoto() formally takes us to the desired room.
974 if (IMAP->selected) {
975 strcpy(savedroom, CC->room.QRname);
977 usergoto(roomname, 0, 0, &msgs, &new);
980 * Now make the API call to zap the room
982 if (CtdlForgetThisRoom() == 0) {
983 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
986 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
991 * If another folder is selected, go back to that room so we can resume
992 * our happy day without violent explosions.
994 if (IMAP->selected) {
995 usergoto(savedroom, 0, 0, &msgs, &new);
1002 * Implements the DELETE command
1005 void imap_delete(int num_parms, char *parms[]) {
1007 char roomname[ROOMNAMELEN];
1008 char savedroom[ROOMNAMELEN];
1011 ret = imap_grabroom(roomname, parms[2]);
1013 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1019 * usergoto() formally takes us to the desired room, happily returning
1020 * the number of messages and number of new messages. (If another
1021 * folder is selected, save its name so we can return there!!!!!)
1023 if (IMAP->selected) {
1024 strcpy(savedroom, CC->room.QRname);
1026 usergoto(roomname, 0, 0, &msgs, &new);
1029 * Now delete the room.
1031 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1032 cprintf("%s OK DELETE completed\r\n", parms[0]);
1033 delete_room(&CC->room);
1036 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1040 * If another folder is selected, go back to that room so we can resume
1041 * our happy day without violent explosions.
1043 if (IMAP->selected) {
1044 usergoto(savedroom, 0, 0, &msgs, &new);
1050 * Back end function for imap_rename()
1052 void imap_rename_backend(struct ctdlroom *qrbuf, void *data) {
1053 char foldername[SIZ];
1054 char newfoldername[SIZ];
1055 char newroomname[ROOMNAMELEN];
1057 struct irl *irlp = NULL; /* scratch pointer */
1058 struct irlparms *irlparms;
1060 irlparms = (struct irlparms *)data;
1061 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1063 /* Rename subfolders */
1064 if ( (!strncasecmp(foldername, irlparms->oldname,
1065 strlen(irlparms->oldname))
1066 && (foldername[strlen(irlparms->oldname)] == '|')) ) {
1068 sprintf(newfoldername, "%s|%s",
1070 &foldername[strlen(irlparms->oldname)+1]
1073 newfloor = imap_roomname(newroomname,
1074 sizeof newroomname, newfoldername) & 0xFF;
1076 irlp = (struct irl *) mallok(sizeof(struct irl));
1077 strcpy(irlp->irl_newroom, newroomname);
1078 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1079 irlp->irl_newfloor = newfloor;
1080 irlp->next = *(irlparms->irl);
1081 *(irlparms->irl) = irlp;
1087 * Implements the RENAME command
1090 void imap_rename(int num_parms, char *parms[]) {
1091 char old_room[ROOMNAMELEN];
1092 char new_room[ROOMNAMELEN];
1096 struct irl *irl = NULL; /* the list */
1097 struct irl *irlp = NULL; /* scratch pointer */
1098 struct irlparms irlparms;
1100 if (strchr(parms[3], '\\') != NULL) {
1101 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
1105 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1106 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1107 new_floor = (newr & 0xFF);
1109 r = CtdlRenameRoom(old_room, new_room, new_floor);
1111 if (r == crr_room_not_found) {
1112 cprintf("%s NO Could not locate this folder\r\n", parms[0]);
1115 if (r == crr_already_exists) {
1116 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1119 if (r == crr_noneditable) {
1120 cprintf("%s This folder is not editable.\r\n", parms[0]);
1123 if (r == crr_invalid_floor) {
1124 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1127 if (r == crr_access_denied) {
1128 cprintf("%s You do not have permission to edit "
1129 "this folder.\r\n", parms[0]);
1133 cprintf("%s NO Rename failed - undefined error %d\r\n",
1139 /* If this is the INBOX, then RFC2060 says we have to just move the
1140 * contents. In a Citadel environment it's easier to rename the room
1141 * (already did that) and create a new inbox.
1143 if (!strcasecmp(parms[2], "INBOX")) {
1144 create_room(MAILROOM, 4, "", 0, 1, 0);
1147 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1149 irlparms.oldname = parms[2];
1150 irlparms.newname = parms[3];
1151 irlparms.irl = &irl;
1152 ForEachRoom(imap_rename_backend, (void *)&irlparms);
1154 /* ... and now rename them. */
1155 while (irl != NULL) {
1156 r = CtdlRenameRoom(irl->irl_oldroom,
1157 irl->irl_newroom, irl->irl_newfloor);
1159 /* FIXME handle error returns better */
1160 lprintf(5, "CtdlRenameRoom() error %d\n", r);
1168 cprintf("%s OK RENAME completed\r\n", parms[0]);
1175 * Main command loop for IMAP sessions.
1177 void imap_command_loop(void) {
1183 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1184 if (client_gets(cmdbuf) < 1) {
1185 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1190 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1191 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
1194 /* strip off l/t whitespace and CRLF */
1195 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
1196 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
1199 /* If we're in the middle of a multi-line command, handle that */
1200 if (IMAP->authstate == imap_as_expecting_username) {
1201 imap_auth_login_user(cmdbuf);
1204 if (IMAP->authstate == imap_as_expecting_password) {
1205 imap_auth_login_pass(cmdbuf);
1210 /* Ok, at this point we're in normal command mode. The first thing
1211 * we do is print any incoming pages (yeah! we really do!)
1213 imap_print_express_messages();
1216 * Before processing the command that was just entered... if we happen
1217 * to have a folder selected, we'd like to rescan that folder for new
1218 * messages, and for deletions/changes of existing messages. This
1219 * could probably be optimized somehow, but IMAP sucks...
1221 if (IMAP->selected) {
1222 imap_rescan_msgids();
1225 /* Now for the command set. */
1227 /* Grab the tag, command, and parameters. Check syntax. */
1228 num_parms = imap_parameterize(parms, cmdbuf);
1229 if (num_parms < 2) {
1230 cprintf("BAD syntax error\r\n");
1233 /* The commands below may be executed in any state */
1235 else if ( (!strcasecmp(parms[1], "NOOP"))
1236 || (!strcasecmp(parms[1], "CHECK")) ) {
1237 cprintf("%s OK This command successfully did nothing.\r\n",
1241 else if (!strcasecmp(parms[1], "LOGOUT")) {
1242 imap_do_expunge(); /* yes, we auto-expunge */
1243 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1244 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
1249 else if (!strcasecmp(parms[1], "LOGIN")) {
1250 imap_login(num_parms, parms);
1253 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1254 imap_authenticate(num_parms, parms);
1257 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1258 imap_capability(num_parms, parms);
1261 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
1262 else if (!strcasecmp(parms[1], "STARTTLS")) {
1263 imap_starttls(num_parms, parms);
1267 else if (!CC->logged_in) {
1268 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1271 /* The commans below require a logged-in state */
1273 else if (!strcasecmp(parms[1], "SELECT")) {
1274 imap_select(num_parms, parms);
1277 else if (!strcasecmp(parms[1], "EXAMINE")) {
1278 imap_select(num_parms, parms);
1281 else if (!strcasecmp(parms[1], "LSUB")) {
1282 imap_lsub(num_parms, parms);
1285 else if (!strcasecmp(parms[1], "LIST")) {
1286 imap_list(num_parms, parms);
1289 else if (!strcasecmp(parms[1], "CREATE")) {
1290 imap_create(num_parms, parms);
1293 else if (!strcasecmp(parms[1], "DELETE")) {
1294 imap_delete(num_parms, parms);
1297 else if (!strcasecmp(parms[1], "RENAME")) {
1298 imap_rename(num_parms, parms);
1301 else if (!strcasecmp(parms[1], "STATUS")) {
1302 imap_status(num_parms, parms);
1305 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1306 imap_subscribe(num_parms, parms);
1309 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1310 imap_unsubscribe(num_parms, parms);
1313 else if (!strcasecmp(parms[1], "APPEND")) {
1314 imap_append(num_parms, parms);
1317 else if (IMAP->selected == 0) {
1318 cprintf("%s BAD no folder selected\r\n", parms[0]);
1321 /* The commands below require the SELECT state on a mailbox */
1323 else if (!strcasecmp(parms[1], "FETCH")) {
1324 imap_fetch(num_parms, parms);
1327 else if ( (!strcasecmp(parms[1], "UID"))
1328 && (!strcasecmp(parms[2], "FETCH")) ) {
1329 imap_uidfetch(num_parms, parms);
1332 else if (!strcasecmp(parms[1], "SEARCH")) {
1333 imap_search(num_parms, parms);
1336 else if ( (!strcasecmp(parms[1], "UID"))
1337 && (!strcasecmp(parms[2], "SEARCH")) ) {
1338 imap_uidsearch(num_parms, parms);
1341 else if (!strcasecmp(parms[1], "STORE")) {
1342 imap_store(num_parms, parms);
1345 else if ( (!strcasecmp(parms[1], "UID"))
1346 && (!strcasecmp(parms[2], "STORE")) ) {
1347 imap_uidstore(num_parms, parms);
1350 else if (!strcasecmp(parms[1], "COPY")) {
1351 imap_copy(num_parms, parms);
1354 else if ( (!strcasecmp(parms[1], "UID"))
1355 && (!strcasecmp(parms[2], "COPY")) ) {
1356 imap_uidcopy(num_parms, parms);
1359 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1360 imap_expunge(num_parms, parms);
1363 else if (!strcasecmp(parms[1], "CLOSE")) {
1364 imap_close(num_parms, parms);
1367 /* End of commands. If we get here, the command is either invalid
1372 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1375 /* If the client transmitted a message we can free it now */
1376 imap_free_transmitted_message();
1382 * This function is called to register the IMAP extension with Citadel.
1384 char *serv_imap_init(void)
1386 CtdlRegisterServiceHook(config.c_imap_port,
1390 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);