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"
55 * If there is a message ID map in memory, free it
57 void imap_free_msgids(void) {
58 if (IMAP->msgids != NULL) {
63 if (IMAP->flags != NULL) {
71 * Back end for imap_load_msgids()
73 * FIXME: this should be optimized by figuring out a way to allocate memory
74 * once rather than doing a reallok() for each message.
76 void imap_add_single_msgid(long msgnum, void *userdata) {
78 IMAP->num_msgs = IMAP->num_msgs + 1;
79 if (IMAP->msgids == NULL) {
80 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
83 IMAP->msgids = reallok(IMAP->msgids,
84 IMAP->num_msgs * sizeof(long));
86 if (IMAP->flags == NULL) {
87 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
90 IMAP->flags = reallok(IMAP->flags,
91 IMAP->num_msgs * sizeof(long));
93 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
94 IMAP->flags[IMAP->num_msgs - 1] = 0;
100 * Set up a message ID map for the current room (folder)
102 void imap_load_msgids(void) {
104 if (IMAP->selected == 0) {
105 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
109 imap_free_msgids(); /* If there was already a map, free it */
111 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
112 imap_add_single_msgid, NULL);
114 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
119 * Re-scan the selected room (folder) and see if it's been changed at all
121 void imap_rescan_msgids(void) {
123 int original_num_msgs = 0;
124 long original_highest = 0L;
128 if (IMAP->selected == 0) {
129 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
135 * Check to see if any of the messages we know about have been expunged
137 if (IMAP->num_msgs > 0)
138 for (i=0; i<IMAP->num_msgs; ++i)
139 if ((IMAP->flags[i] & IMAP_EXPUNGED) == 0) {
141 count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
142 (-63), NULL, NULL, NULL, NULL);
145 IMAP->flags[i] = IMAP->flags[i] | IMAP_EXPUNGED;
146 cprintf("* %d EXPUNGE\r\n", i+1);
152 * Remember how many messages were here before we re-scanned.
154 original_num_msgs = IMAP->num_msgs;
155 if (IMAP->num_msgs > 0) {
156 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
159 original_highest = 0L;
163 * Now peruse the room for *new* messages only.
165 CtdlForEachMessage(MSGS_GT, original_highest, (-63), NULL, NULL,
166 imap_add_single_msgid, NULL);
169 * If new messages have arrived, tell the client about them.
171 if (IMAP->num_msgs > original_num_msgs) {
172 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
184 * This cleanup function blows away the temporary memory and files used by
187 void imap_cleanup_function(void) {
189 /* Don't do this stuff if this is not a IMAP session! */
190 if (CC->h_command_function != imap_command_loop) return;
192 lprintf(9, "Performing IMAP cleanup hook\n");
194 lprintf(9, "Finished IMAP cleanup hook\n");
200 * Here's where our IMAP session begins its happy day.
202 void imap_greeting(void) {
204 strcpy(CC->cs_clientname, "IMAP session");
205 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
206 IMAP->authstate = imap_as_normal;
208 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
214 * implements the LOGIN command (ordinary username/password login)
216 void imap_login(int num_parms, char *parms[]) {
217 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
218 if (CtdlTryPassword(parms[3]) == pass_ok) {
219 cprintf("%s OK login successful\r\n", parms[0]);
224 cprintf("%s BAD Login incorrect\r\n", parms[0]);
229 * Implements the AUTHENTICATE command
231 void imap_authenticate(int num_parms, char *parms[]) {
234 if (num_parms != 3) {
235 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
239 if (!strcasecmp(parms[2], "LOGIN")) {
240 encode_base64(buf, "Username:");
241 cprintf("+ %s\r\n", buf);
242 IMAP->authstate = imap_as_expecting_username;
243 strcpy(IMAP->authseq, parms[0]);
248 cprintf("%s NO AUTHENTICATE %s failed\r\n",
253 void imap_auth_login_user(char *cmd) {
256 decode_base64(buf, cmd);
257 CtdlLoginExistingUser(buf);
258 encode_base64(buf, "Password:");
259 cprintf("+ %s\r\n", buf);
260 IMAP->authstate = imap_as_expecting_password;
264 void imap_auth_login_pass(char *cmd) {
267 decode_base64(buf, cmd);
268 if (CtdlTryPassword(buf) == pass_ok) {
269 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
272 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
274 IMAP->authstate = imap_as_normal;
281 * implements the CAPABILITY command
283 void imap_capability(int num_parms, char *parms[]) {
284 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
285 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
293 * implements the SELECT command
295 void imap_select(int num_parms, char *parms[]) {
297 char augmented_roomname[SIZ];
301 struct quickroom QRscratch;
307 /* Convert the supplied folder name to a roomname */
308 i = imap_roomname(towhere, sizeof towhere, parms[2]);
310 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
314 floornum = (i & 0x00ff);
315 roomflags = (i & 0xff00);
317 /* First try a regular match */
318 c = getroom(&QRscratch, towhere);
320 /* Then try a mailbox name match */
322 MailboxName(augmented_roomname, &CC->usersupp, towhere);
323 c = getroom(&QRscratch, augmented_roomname);
325 strcpy(towhere, augmented_roomname);
328 /* If the room exists, check security/access */
330 /* See if there is an existing user/room relationship */
331 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
333 /* normal clients have to pass through security */
339 /* Fail here if no such room */
341 cprintf("%s NO ... no such room, or access denied\r\n",
348 * usergoto() formally takes us to the desired room, happily returning
349 * the number of messages and number of new messages.
351 usergoto(QRscratch.QRname, 0, &msgs, &new);
354 if (!strcasecmp(parms[1], "EXAMINE")) {
363 /* FIXME ... much more info needs to be supplied here */
364 cprintf("* %d EXISTS\r\n", msgs);
365 cprintf("* %d RECENT\r\n", new);
366 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
367 cprintf("%s OK [%s] %s completed\r\n",
369 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
376 * implements the CLOSE command
378 void imap_close(int num_parms, char *parms[]) {
382 cprintf("%s OK CLOSE completed\r\n", parms[0]);
389 * Used by LIST and LSUB to show the floors in the listing
391 void imap_list_floors(char *cmd) {
395 for (i=0; i<MAXFLOORS; ++i) {
397 if (fl->f_flags & F_INUSE) {
398 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
399 imap_strout(fl->f_name);
408 * Back end for imap_lsub()
410 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
411 * may or may not be the desired behavior in the future.
413 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
417 /* Only list rooms to which the user has access!! */
418 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
420 imap_mailboxname(buf, sizeof buf, qrbuf);
421 cprintf("* LSUB () \"|\" ");
429 * Implements the LSUB command
431 * FIXME: Handle wildcards, please.
433 void imap_lsub(int num_parms, char *parms[]) {
434 imap_list_floors("LSUB");
435 ForEachRoom(imap_lsub_listroom, NULL);
436 cprintf("%s OK LSUB completed\r\n", parms[0]);
442 * Back end for imap_list()
444 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
448 /* Only list rooms to which the user has access!! */
449 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
451 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
452 imap_mailboxname(buf, sizeof buf, qrbuf);
453 cprintf("* LIST () \"|\" ");
461 * Implements the LIST command
463 * FIXME: Handle wildcards, please.
465 void imap_list(int num_parms, char *parms[]) {
466 imap_list_floors("LIST");
467 ForEachRoom(imap_list_listroom, NULL);
468 cprintf("%s OK LIST completed\r\n", parms[0]);
474 * Implements the CREATE command
477 void imap_create(int num_parms, char *parms[]) {
484 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
486 cprintf("%s NO Invalid mailbox name or location\r\n",
490 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
491 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
493 if (flags & IR_MAILBOX) {
494 newroomtype = 4; /* private mailbox */
497 newroomtype = 0; /* public folder */
500 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
501 roomname, floornum, newroomtype);
503 ret = create_room(roomname, newroomtype, "", floornum, 1);
505 cprintf("%s NO Mailbox already exists, or create failed\r\n",
509 cprintf("%s OK CREATE completed\r\n", parms[0]);
516 * Main command loop for IMAP sessions.
518 void imap_command_loop(void) {
524 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
525 if (client_gets(cmdbuf) < 1) {
526 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
531 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
532 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
535 /* strip off l/t whitespace and CRLF */
536 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
537 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
540 /* If we're in the middle of a multi-line command, handle that */
541 if (IMAP->authstate == imap_as_expecting_username) {
542 imap_auth_login_user(cmdbuf);
545 if (IMAP->authstate == imap_as_expecting_password) {
546 imap_auth_login_pass(cmdbuf);
551 /* Ok, at this point we're in normal command mode */
554 * Before processing the command that was just entered... if we happen
555 * to have a folder selected, we'd like to rescan that folder for new
556 * messages, and for deletions/changes of existing messages. This
557 * could probably be optimized somehow, but IMAP sucks...
559 if (IMAP->selected) {
560 imap_rescan_msgids();
563 /* Now for the command set. */
565 /* Grab the tag, command, and parameters. Check syntax. */
566 num_parms = imap_parameterize(parms, cmdbuf);
568 cprintf("BAD syntax error\r\n");
571 /* The commands below may be executed in any state */
573 else if ( (!strcasecmp(parms[1], "NOOP"))
574 || (!strcasecmp(parms[1], "CHECK")) ) {
575 cprintf("%s OK This command successfully did nothing.\r\n",
579 else if (!strcasecmp(parms[1], "LOGOUT")) {
580 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
581 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
586 else if (!strcasecmp(parms[1], "LOGIN")) {
587 imap_login(num_parms, parms);
590 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
591 imap_authenticate(num_parms, parms);
594 else if (!strcasecmp(parms[1], "CAPABILITY")) {
595 imap_capability(num_parms, parms);
598 else if (!CC->logged_in) {
599 cprintf("%s BAD Not logged in.\r\n", parms[0]);
602 /* The commans below require a logged-in state */
604 else if (!strcasecmp(parms[1], "SELECT")) {
605 imap_select(num_parms, parms);
608 else if (!strcasecmp(parms[1], "EXAMINE")) {
609 imap_select(num_parms, parms);
612 else if (!strcasecmp(parms[1], "LSUB")) {
613 imap_lsub(num_parms, parms);
616 else if (!strcasecmp(parms[1], "LIST")) {
617 imap_list(num_parms, parms);
620 else if (!strcasecmp(parms[1], "CREATE")) {
621 imap_create(num_parms, parms);
624 else if (IMAP->selected == 0) {
625 cprintf("%s BAD no folder selected\r\n", parms[0]);
628 /* The commands below require the SELECT state on a mailbox */
630 else if (!strcasecmp(parms[1], "FETCH")) {
631 imap_fetch(num_parms, parms);
634 else if ( (!strcasecmp(parms[1], "UID"))
635 && (!strcasecmp(parms[2], "FETCH")) ) {
636 imap_uidfetch(num_parms, parms);
639 else if (!strcasecmp(parms[1], "SEARCH")) {
640 imap_search(num_parms, parms);
643 else if ( (!strcasecmp(parms[1], "UID"))
644 && (!strcasecmp(parms[2], "SEARCH")) ) {
645 imap_uidsearch(num_parms, parms);
648 else if (!strcasecmp(parms[1], "CLOSE")) {
649 imap_close(num_parms, parms);
652 /* End of commands. If we get here, the command is either invalid
657 cprintf("%s BAD command unrecognized\r\n", parms[0]);
665 * This function is called by dynloader.c to register the IMAP module
666 * with the Citadel server.
668 char *Dynamic_Module_Init(void)
670 SYM_IMAP = CtdlGetDynamicSymbol();
671 CtdlRegisterServiceHook(config.c_imap_port,
675 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);