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);
121 * This cleanup function blows away the temporary memory and files used by
124 void imap_cleanup_function(void) {
126 /* Don't do this stuff if this is not a IMAP session! */
127 if (CC->h_command_function != imap_command_loop) return;
129 lprintf(9, "Performing IMAP cleanup hook\n");
131 lprintf(9, "Finished IMAP cleanup hook\n");
137 * Here's where our IMAP session begins its happy day.
139 void imap_greeting(void) {
141 strcpy(CC->cs_clientname, "IMAP session");
142 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
143 IMAP->authstate = imap_as_normal;
145 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
151 * implements the LOGIN command (ordinary username/password login)
153 void imap_login(int num_parms, char *parms[]) {
154 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
155 if (CtdlTryPassword(parms[3]) == pass_ok) {
156 cprintf("%s OK login successful\r\n", parms[0]);
161 cprintf("%s BAD Login incorrect\r\n", parms[0]);
166 * Implements the AUTHENTICATE command
168 void imap_authenticate(int num_parms, char *parms[]) {
171 if (num_parms != 3) {
172 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
176 if (!strcasecmp(parms[2], "LOGIN")) {
177 encode_base64(buf, "Username:");
178 cprintf("+ %s\r\n", buf);
179 IMAP->authstate = imap_as_expecting_username;
180 strcpy(IMAP->authseq, parms[0]);
185 cprintf("%s NO AUTHENTICATE %s failed\r\n",
190 void imap_auth_login_user(char *cmd) {
193 decode_base64(buf, cmd);
194 CtdlLoginExistingUser(buf);
195 encode_base64(buf, "Password:");
196 cprintf("+ %s\r\n", buf);
197 IMAP->authstate = imap_as_expecting_password;
201 void imap_auth_login_pass(char *cmd) {
204 decode_base64(buf, cmd);
205 if (CtdlTryPassword(buf) == pass_ok) {
206 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
209 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
211 IMAP->authstate = imap_as_normal;
218 * implements the CAPABILITY command
220 void imap_capability(int num_parms, char *parms[]) {
221 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
222 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
230 * implements the SELECT command
232 void imap_select(int num_parms, char *parms[]) {
234 char augmented_roomname[SIZ];
238 struct quickroom QRscratch;
242 /* Convert the supplied folder name to a roomname */
243 floornum = imap_roomname(towhere, sizeof towhere, parms[2]);
245 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
250 /* First try a regular match */
251 c = getroom(&QRscratch, towhere);
253 /* Then try a mailbox name match */
255 MailboxName(augmented_roomname, &CC->usersupp, towhere);
256 c = getroom(&QRscratch, augmented_roomname);
258 strcpy(towhere, augmented_roomname);
261 /* If the room exists, check security/access */
263 /* See if there is an existing user/room relationship */
264 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
266 /* normal clients have to pass through security */
272 /* Fail here if no such room */
274 cprintf("%s NO ... no such room, or access denied\r\n",
281 * usergoto() formally takes us to the desired room, happily returning
282 * the number of messages and number of new messages.
284 usergoto(QRscratch.QRname, 0, &msgs, &new);
287 if (!strcasecmp(parms[1], "EXAMINE")) {
296 /* FIXME ... much more info needs to be supplied here */
297 cprintf("* %d EXISTS\r\n", msgs);
298 cprintf("* %d RECENT\r\n", new);
299 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
300 cprintf("%s OK [%s] %s completed\r\n",
302 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
309 * implements the CLOSE command
311 void imap_close(int num_parms, char *parms[]) {
315 cprintf("%s OK CLOSE completed\r\n", parms[0]);
323 * Back end for imap_lsub()
325 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
326 * may or may not be the desired behavior in the future.
328 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
332 /* Only list rooms to which the user has access!! */
333 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
335 imap_mailboxname(buf, sizeof buf, qrbuf);
336 cprintf("* LSUB () \"|\" ");
344 * Implements the LSUB command
346 * FIXME: Handle wildcards, please.
348 void imap_lsub(int num_parms, char *parms[]) {
349 ForEachRoom(imap_lsub_listroom, NULL);
350 cprintf("%s OK LSUB completed\r\n", parms[0]);
356 * Back end for imap_list()
358 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
362 /* Only list rooms to which the user has access!! */
363 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
365 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
366 imap_mailboxname(buf, sizeof buf, qrbuf);
367 cprintf("* LIST () \"|\" ");
375 * Implements the LIST command
377 * FIXME: Handle wildcards, please.
379 void imap_list(int num_parms, char *parms[]) {
380 ForEachRoom(imap_list_listroom, NULL);
381 cprintf("%s OK LIST completed\r\n", parms[0]);
387 * Implements the CREATE command (FIXME not finished yet)
390 void imap_create(int num_parms, char *parms[]) {
394 floornum = imap_roomname(parms[1]);
396 cprintf("%s NO Invalid name\r\n", parms[0]);
400 cprintf("%s NO CREATE unimplemented\r\n", parms[0]);
406 * Main command loop for IMAP sessions.
408 void imap_command_loop(void) {
414 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
415 if (client_gets(cmdbuf) < 1) {
416 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
421 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
422 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
425 /* strip off l/t whitespace and CRLF */
426 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
427 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
430 /* If we're in the middle of a multi-line command, handle that */
431 if (IMAP->authstate == imap_as_expecting_username) {
432 imap_auth_login_user(cmdbuf);
435 if (IMAP->authstate == imap_as_expecting_password) {
436 imap_auth_login_pass(cmdbuf);
441 /* Ok, at this point we're in normal command mode */
443 /* Grab the tag, command, and parameters. Check syntax. */
444 num_parms = imap_parameterize(parms, cmdbuf);
446 cprintf("BAD syntax error\r\n");
449 /* The commands below may be executed in any state */
451 else if ( (!strcasecmp(parms[1], "NOOP"))
452 || (!strcasecmp(parms[1], "CHECK")) ) {
453 cprintf("%s OK This command successfully did nothing.\r\n",
457 else if (!strcasecmp(parms[1], "LOGOUT")) {
458 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
459 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
464 else if (!strcasecmp(parms[1], "LOGIN")) {
465 imap_login(num_parms, parms);
468 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
469 imap_authenticate(num_parms, parms);
472 else if (!strcasecmp(parms[1], "CAPABILITY")) {
473 imap_capability(num_parms, parms);
476 else if (!CC->logged_in) {
477 cprintf("%s BAD Not logged in.\r\n", parms[0]);
480 /* The commans below require a logged-in state */
482 else if (!strcasecmp(parms[1], "SELECT")) {
483 imap_select(num_parms, parms);
486 else if (!strcasecmp(parms[1], "EXAMINE")) {
487 imap_select(num_parms, parms);
490 else if (!strcasecmp(parms[1], "LSUB")) {
491 imap_lsub(num_parms, parms);
494 else if (!strcasecmp(parms[1], "LIST")) {
495 imap_list(num_parms, parms);
498 else if (!strcasecmp(parms[1], "CREATE")) {
499 imap_create(num_parms, parms);
502 else if (IMAP->selected == 0) {
503 cprintf("%s BAD no folder selected\r\n", parms[0]);
506 /* The commands below require the SELECT state on a mailbox */
508 else if (!strcasecmp(parms[1], "FETCH")) {
509 imap_fetch(num_parms, parms);
512 else if ( (!strcasecmp(parms[1], "UID"))
513 && (!strcasecmp(parms[2], "FETCH")) ) {
514 imap_uidfetch(num_parms, parms);
517 else if (!strcasecmp(parms[1], "SEARCH")) {
518 imap_search(num_parms, parms);
521 else if ( (!strcasecmp(parms[1], "UID"))
522 && (!strcasecmp(parms[2], "SEARCH")) ) {
523 imap_uidsearch(num_parms, parms);
526 else if (!strcasecmp(parms[1], "CLOSE")) {
527 imap_close(num_parms, parms);
530 /* End of commands. If we get here, the command is either invalid
535 cprintf("%s BAD command unrecognized\r\n", parms[0]);
543 * This function is called by dynloader.c to register the IMAP module
544 * with the Citadel server.
546 char *Dynamic_Module_Init(void)
548 SYM_IMAP = CtdlGetDynamicSymbol();
549 CtdlRegisterServiceHook(config.c_imap_port,
553 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);