4 * IMAP server for the Citadel/UX system
5 * Copyright (C) 2000-2001 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * WARNING: this is an incomplete implementation, still in progress. Parts of
9 * it work, but it's not really usable yet from a user perspective.
11 * WARNING: Mark Crispin is an idiot. IMAP is the most brain-damaged protocol
12 * you will ever have the profound lack of pleasure to encounter.
24 #include <sys/types.h>
33 #include "sysdep_decls.h"
34 #include "citserver.h"
37 #include "dynloader.h"
44 #include "internet_addressing.h"
45 #include "serv_imap.h"
46 #include "imap_tools.h"
47 #include "imap_fetch.h"
48 #include "imap_search.h"
49 #include "imap_store.h"
56 * If there is a message ID map in memory, free it
58 void imap_free_msgids(void) {
59 if (IMAP->msgids != NULL) {
64 if (IMAP->flags != NULL) {
72 * Back end for imap_load_msgids()
74 * FIXME: this should be optimized by figuring out a way to allocate memory
75 * once rather than doing a reallok() for each message.
77 void imap_add_single_msgid(long msgnum, void *userdata) {
79 IMAP->num_msgs = IMAP->num_msgs + 1;
80 if (IMAP->msgids == NULL) {
81 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
84 IMAP->msgids = reallok(IMAP->msgids,
85 IMAP->num_msgs * sizeof(long));
87 if (IMAP->flags == NULL) {
88 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
91 IMAP->flags = reallok(IMAP->flags,
92 IMAP->num_msgs * sizeof(long));
94 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
95 IMAP->flags[IMAP->num_msgs - 1] = 0;
101 * Set up a message ID map for the current room (folder)
103 void imap_load_msgids(void) {
105 if (IMAP->selected == 0) {
106 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
110 imap_free_msgids(); /* If there was already a map, free it */
112 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
113 imap_add_single_msgid, NULL);
115 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
120 * Re-scan the selected room (folder) and see if it's been changed at all
122 void imap_rescan_msgids(void) {
124 int original_num_msgs = 0;
125 long original_highest = 0L;
129 if (IMAP->selected == 0) {
130 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
136 * Check to see if any of the messages we know about have been expunged
138 if (IMAP->num_msgs > 0)
139 for (i=0; i<IMAP->num_msgs; ++i)
140 if ((IMAP->flags[i] & IMAP_EXPUNGED) == 0) {
142 count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
143 (-63), NULL, NULL, NULL, NULL);
146 IMAP->flags[i] = IMAP->flags[i] | IMAP_EXPUNGED;
147 cprintf("* %d EXPUNGE\r\n", i+1);
153 * Remember how many messages were here before we re-scanned.
155 original_num_msgs = IMAP->num_msgs;
156 if (IMAP->num_msgs > 0) {
157 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
160 original_highest = 0L;
164 * Now peruse the room for *new* messages only.
166 CtdlForEachMessage(MSGS_GT, original_highest, (-63), NULL, NULL,
167 imap_add_single_msgid, NULL);
170 * If new messages have arrived, tell the client about them.
172 if (IMAP->num_msgs > original_num_msgs) {
173 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
185 * This cleanup function blows away the temporary memory and files used by
188 void imap_cleanup_function(void) {
190 /* Don't do this stuff if this is not a IMAP session! */
191 if (CC->h_command_function != imap_command_loop) return;
193 lprintf(9, "Performing IMAP cleanup hook\n");
195 lprintf(9, "Finished IMAP cleanup hook\n");
201 * Here's where our IMAP session begins its happy day.
203 void imap_greeting(void) {
205 strcpy(CC->cs_clientname, "IMAP session");
206 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
207 IMAP->authstate = imap_as_normal;
209 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
215 * implements the LOGIN command (ordinary username/password login)
217 void imap_login(int num_parms, char *parms[]) {
218 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
219 if (CtdlTryPassword(parms[3]) == pass_ok) {
220 cprintf("%s OK login successful\r\n", parms[0]);
225 cprintf("%s BAD Login incorrect\r\n", parms[0]);
230 * Implements the AUTHENTICATE command
232 void imap_authenticate(int num_parms, char *parms[]) {
235 if (num_parms != 3) {
236 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
240 if (!strcasecmp(parms[2], "LOGIN")) {
241 encode_base64(buf, "Username:");
242 cprintf("+ %s\r\n", buf);
243 IMAP->authstate = imap_as_expecting_username;
244 strcpy(IMAP->authseq, parms[0]);
249 cprintf("%s NO AUTHENTICATE %s failed\r\n",
254 void imap_auth_login_user(char *cmd) {
257 decode_base64(buf, cmd);
258 CtdlLoginExistingUser(buf);
259 encode_base64(buf, "Password:");
260 cprintf("+ %s\r\n", buf);
261 IMAP->authstate = imap_as_expecting_password;
265 void imap_auth_login_pass(char *cmd) {
268 decode_base64(buf, cmd);
269 if (CtdlTryPassword(buf) == pass_ok) {
270 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
273 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
275 IMAP->authstate = imap_as_normal;
282 * implements the CAPABILITY command
284 void imap_capability(int num_parms, char *parms[]) {
285 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
286 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
294 * implements the SELECT command
296 void imap_select(int num_parms, char *parms[]) {
298 char augmented_roomname[ROOMNAMELEN];
302 struct quickroom QRscratch;
308 /* Convert the supplied folder name to a roomname */
309 i = imap_roomname(towhere, sizeof towhere, parms[2]);
311 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
315 floornum = (i & 0x00ff);
316 roomflags = (i & 0xff00);
318 /* First try a regular match */
319 c = getroom(&QRscratch, towhere);
321 /* Then try a mailbox name match */
323 MailboxName(augmented_roomname, &CC->usersupp, towhere);
324 c = getroom(&QRscratch, augmented_roomname);
326 strcpy(towhere, augmented_roomname);
329 /* If the room exists, check security/access */
331 /* See if there is an existing user/room relationship */
332 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
334 /* normal clients have to pass through security */
340 /* Fail here if no such room */
342 cprintf("%s NO ... no such room, or access denied\r\n",
349 * usergoto() formally takes us to the desired room, happily returning
350 * the number of messages and number of new messages.
352 usergoto(QRscratch.QRname, 0, &msgs, &new);
355 if (!strcasecmp(parms[1], "EXAMINE")) {
364 /* FIXME ... much more info needs to be supplied here */
365 cprintf("* %d EXISTS\r\n", msgs);
366 cprintf("* %d RECENT\r\n", new);
367 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
368 cprintf("%s OK [%s] %s completed\r\n",
370 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
377 * implements the CLOSE command
379 void imap_close(int num_parms, char *parms[]) {
383 cprintf("%s OK CLOSE completed\r\n", parms[0]);
390 * Used by LIST and LSUB to show the floors in the listing
392 void imap_list_floors(char *cmd) {
396 for (i=0; i<MAXFLOORS; ++i) {
398 if (fl->f_flags & F_INUSE) {
399 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
400 imap_strout(fl->f_name);
409 * Back end for imap_lsub()
411 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
412 * may or may not be the desired behavior in the future.
414 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
418 /* Only list rooms to which the user has access!! */
419 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
421 imap_mailboxname(buf, sizeof buf, qrbuf);
422 cprintf("* LSUB () \"|\" ");
430 * Implements the LSUB command
432 * FIXME: Handle wildcards, please.
434 void imap_lsub(int num_parms, char *parms[]) {
435 imap_list_floors("LSUB");
436 ForEachRoom(imap_lsub_listroom, NULL);
437 cprintf("%s OK LSUB completed\r\n", parms[0]);
443 * Back end for imap_list()
445 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
449 /* Only list rooms to which the user has access!! */
450 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
452 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
453 imap_mailboxname(buf, sizeof buf, qrbuf);
454 cprintf("* LIST () \"|\" ");
462 * Implements the LIST command
464 * FIXME: Handle wildcards, please.
466 void imap_list(int num_parms, char *parms[]) {
467 imap_list_floors("LIST");
468 ForEachRoom(imap_list_listroom, NULL);
469 cprintf("%s OK LIST completed\r\n", parms[0]);
475 * Implements the CREATE command
478 void imap_create(int num_parms, char *parms[]) {
480 char roomname[ROOMNAMELEN];
485 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
487 cprintf("%s NO Invalid mailbox name or location\r\n",
491 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
492 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
494 if (flags & IR_MAILBOX) {
495 newroomtype = 4; /* private mailbox */
498 newroomtype = 0; /* public folder */
501 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
502 roomname, floornum, newroomtype);
504 ret = create_room(roomname, newroomtype, "", floornum, 1);
506 cprintf("%s NO Mailbox already exists, or create failed\r\n",
510 cprintf("%s OK CREATE completed\r\n", parms[0]);
516 * Implements the STATUS command (sort of)
519 void imap_status(int num_parms, char *parms[]) {
521 char augmented_roomname[ROOMNAMELEN];
522 char roomname[ROOMNAMELEN];
525 struct quickroom QRscratch;
528 char savedroom[ROOMNAMELEN];
531 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
533 cprintf("%s NO Invalid mailbox name or location\r\n",
538 /* First try a regular match */
539 c = getroom(&QRscratch, roomname);
541 /* Then try a mailbox name match */
543 MailboxName(augmented_roomname, &CC->usersupp, roomname);
544 c = getroom(&QRscratch, augmented_roomname);
546 strcpy(roomname, augmented_roomname);
549 /* If the room exists, check security/access */
551 /* See if there is an existing user/room relationship */
552 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
554 /* normal clients have to pass through security */
560 /* Fail here if no such room */
562 cprintf("%s NO ... no such room, or access denied\r\n",
568 * usergoto() formally takes us to the desired room, happily returning
569 * the number of messages and number of new messages. (If another
570 * folder is selected, save its name so we can return there!!!!!)
572 if (IMAP->selected) {
573 strcpy(savedroom, CC->quickroom.QRname);
575 usergoto(QRscratch.QRname, 0, &msgs, &new);
578 * Tell the client what it wants to know. In fact, tell it *more* than
579 * it wants to know. We happily IGnore the supplied status data item
580 * names and simply spew all possible data items. It's far easier to
581 * code and probably saves us some processing time too.
583 * FIXME we need to implement RECENT and UNSEEN eventually...
586 imap_mailboxname(buf, sizeof buf, &QRscratch);
587 cprintf("* STATUS ");
589 cprintf(" (MESSAGES %d RECENT 0 UIDNEXT %ld "
590 "UIDVALIDITY 0 UNSEEN 0)\r\n",
592 CitControl.MMhighest + 1
596 * If another folder is selected, go back to that room so we can resume
597 * our happy day without violent explosions.
599 if (IMAP->selected) {
600 usergoto(savedroom, 0, &msgs, &new);
604 * Oooh, look, we're done!
606 cprintf("%s OK STATUS completed\r\n", parms[0]);
613 * Main command loop for IMAP sessions.
615 void imap_command_loop(void) {
621 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
622 if (client_gets(cmdbuf) < 1) {
623 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
628 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
629 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
632 /* strip off l/t whitespace and CRLF */
633 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
634 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
637 /* If we're in the middle of a multi-line command, handle that */
638 if (IMAP->authstate == imap_as_expecting_username) {
639 imap_auth_login_user(cmdbuf);
642 if (IMAP->authstate == imap_as_expecting_password) {
643 imap_auth_login_pass(cmdbuf);
648 /* Ok, at this point we're in normal command mode */
651 * Before processing the command that was just entered... if we happen
652 * to have a folder selected, we'd like to rescan that folder for new
653 * messages, and for deletions/changes of existing messages. This
654 * could probably be optimized somehow, but IMAP sucks...
656 if (IMAP->selected) {
657 imap_rescan_msgids();
660 /* Now for the command set. */
662 /* Grab the tag, command, and parameters. Check syntax. */
663 num_parms = imap_parameterize(parms, cmdbuf);
665 cprintf("BAD syntax error\r\n");
668 /* The commands below may be executed in any state */
670 else if ( (!strcasecmp(parms[1], "NOOP"))
671 || (!strcasecmp(parms[1], "CHECK")) ) {
672 cprintf("%s OK This command successfully did nothing.\r\n",
676 else if (!strcasecmp(parms[1], "LOGOUT")) {
677 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
678 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
683 else if (!strcasecmp(parms[1], "LOGIN")) {
684 imap_login(num_parms, parms);
687 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
688 imap_authenticate(num_parms, parms);
691 else if (!strcasecmp(parms[1], "CAPABILITY")) {
692 imap_capability(num_parms, parms);
695 else if (!CC->logged_in) {
696 cprintf("%s BAD Not logged in.\r\n", parms[0]);
699 /* The commans below require a logged-in state */
701 else if (!strcasecmp(parms[1], "SELECT")) {
702 imap_select(num_parms, parms);
705 else if (!strcasecmp(parms[1], "EXAMINE")) {
706 imap_select(num_parms, parms);
709 else if (!strcasecmp(parms[1], "LSUB")) {
710 imap_lsub(num_parms, parms);
713 else if (!strcasecmp(parms[1], "LIST")) {
714 imap_list(num_parms, parms);
717 else if (!strcasecmp(parms[1], "CREATE")) {
718 imap_create(num_parms, parms);
721 else if (!strcasecmp(parms[1], "STATUS")) {
722 imap_status(num_parms, parms);
725 else if (IMAP->selected == 0) {
726 cprintf("%s BAD no folder selected\r\n", parms[0]);
729 /* The commands below require the SELECT state on a mailbox */
731 else if (!strcasecmp(parms[1], "FETCH")) {
732 imap_fetch(num_parms, parms);
735 else if ( (!strcasecmp(parms[1], "UID"))
736 && (!strcasecmp(parms[2], "FETCH")) ) {
737 imap_uidfetch(num_parms, parms);
740 else if (!strcasecmp(parms[1], "SEARCH")) {
741 imap_search(num_parms, parms);
744 else if ( (!strcasecmp(parms[1], "UID"))
745 && (!strcasecmp(parms[2], "SEARCH")) ) {
746 imap_uidsearch(num_parms, parms);
749 else if (!strcasecmp(parms[1], "STORE")) {
750 imap_store(num_parms, parms);
753 else if ( (!strcasecmp(parms[1], "UID"))
754 && (!strcasecmp(parms[2], "STORE")) ) {
755 imap_uidstore(num_parms, parms);
758 else if (!strcasecmp(parms[1], "CLOSE")) {
759 imap_close(num_parms, parms);
762 /* End of commands. If we get here, the command is either invalid
767 cprintf("%s BAD command unrecognized\r\n", parms[0]);
775 * This function is called by dynloader.c to register the IMAP module
776 * with the Citadel server.
778 char *Dynamic_Module_Init(void)
780 SYM_IMAP = CtdlGetDynamicSymbol();
781 CtdlRegisterServiceHook(config.c_imap_port,
785 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);