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;
244 /* Convert the supplied folder name to a roomname */
245 i = imap_roomname(towhere, sizeof towhere, parms[2]);
247 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
251 floornum = (i & 0x00ff);
252 roomflags = (i & 0xff00);
254 /* First try a regular match */
255 c = getroom(&QRscratch, towhere);
257 /* Then try a mailbox name match */
259 MailboxName(augmented_roomname, &CC->usersupp, towhere);
260 c = getroom(&QRscratch, augmented_roomname);
262 strcpy(towhere, augmented_roomname);
265 /* If the room exists, check security/access */
267 /* See if there is an existing user/room relationship */
268 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
270 /* normal clients have to pass through security */
276 /* Fail here if no such room */
278 cprintf("%s NO ... no such room, or access denied\r\n",
285 * usergoto() formally takes us to the desired room, happily returning
286 * the number of messages and number of new messages.
288 usergoto(QRscratch.QRname, 0, &msgs, &new);
291 if (!strcasecmp(parms[1], "EXAMINE")) {
300 /* FIXME ... much more info needs to be supplied here */
301 cprintf("* %d EXISTS\r\n", msgs);
302 cprintf("* %d RECENT\r\n", new);
303 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
304 cprintf("%s OK [%s] %s completed\r\n",
306 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
313 * implements the CLOSE command
315 void imap_close(int num_parms, char *parms[]) {
319 cprintf("%s OK CLOSE completed\r\n", parms[0]);
327 * Back end for imap_lsub()
329 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
330 * may or may not be the desired behavior in the future.
332 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
336 /* Only list rooms to which the user has access!! */
337 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
339 imap_mailboxname(buf, sizeof buf, qrbuf);
340 cprintf("* LSUB () \"|\" ");
348 * Implements the LSUB command
350 * FIXME: Handle wildcards, please.
352 void imap_lsub(int num_parms, char *parms[]) {
353 ForEachRoom(imap_lsub_listroom, NULL);
354 cprintf("%s OK LSUB completed\r\n", parms[0]);
360 * Back end for imap_list()
362 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
366 /* Only list rooms to which the user has access!! */
367 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
369 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
370 imap_mailboxname(buf, sizeof buf, qrbuf);
371 cprintf("* LIST () \"|\" ");
379 * Implements the LIST command
381 * FIXME: Handle wildcards, please.
383 void imap_list(int num_parms, char *parms[]) {
384 ForEachRoom(imap_list_listroom, NULL);
385 cprintf("%s OK LIST completed\r\n", parms[0]);
391 * Implements the CREATE command (FIXME not finished yet)
394 void imap_create(int num_parms, char *parms[]) {
401 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
403 cprintf("%s NO Invalid mailbox name or location\r\n",
407 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
408 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
410 if (flags & IR_MAILBOX) {
411 newroomtype = 4; /* private mailbox */
414 newroomtype = 0; /* public folder */
417 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
418 roomname, floornum, newroomtype);
420 ret = create_room(roomname, newroomtype, "", floornum, 1);
422 cprintf("%s NO Mailbox already exists, or create failed\r\n",
426 cprintf("%s OK CREATE completed\r\n", parms[0]);
433 * Main command loop for IMAP sessions.
435 void imap_command_loop(void) {
441 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
442 if (client_gets(cmdbuf) < 1) {
443 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
448 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
449 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
452 /* strip off l/t whitespace and CRLF */
453 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
454 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
457 /* If we're in the middle of a multi-line command, handle that */
458 if (IMAP->authstate == imap_as_expecting_username) {
459 imap_auth_login_user(cmdbuf);
462 if (IMAP->authstate == imap_as_expecting_password) {
463 imap_auth_login_pass(cmdbuf);
468 /* Ok, at this point we're in normal command mode */
470 /* Grab the tag, command, and parameters. Check syntax. */
471 num_parms = imap_parameterize(parms, cmdbuf);
473 cprintf("BAD syntax error\r\n");
476 /* The commands below may be executed in any state */
478 else if ( (!strcasecmp(parms[1], "NOOP"))
479 || (!strcasecmp(parms[1], "CHECK")) ) {
480 cprintf("%s OK This command successfully did nothing.\r\n",
484 else if (!strcasecmp(parms[1], "LOGOUT")) {
485 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
486 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
491 else if (!strcasecmp(parms[1], "LOGIN")) {
492 imap_login(num_parms, parms);
495 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
496 imap_authenticate(num_parms, parms);
499 else if (!strcasecmp(parms[1], "CAPABILITY")) {
500 imap_capability(num_parms, parms);
503 else if (!CC->logged_in) {
504 cprintf("%s BAD Not logged in.\r\n", parms[0]);
507 /* The commans below require a logged-in state */
509 else if (!strcasecmp(parms[1], "SELECT")) {
510 imap_select(num_parms, parms);
513 else if (!strcasecmp(parms[1], "EXAMINE")) {
514 imap_select(num_parms, parms);
517 else if (!strcasecmp(parms[1], "LSUB")) {
518 imap_lsub(num_parms, parms);
521 else if (!strcasecmp(parms[1], "LIST")) {
522 imap_list(num_parms, parms);
525 else if (!strcasecmp(parms[1], "CREATE")) {
526 imap_create(num_parms, parms);
529 else if (IMAP->selected == 0) {
530 cprintf("%s BAD no folder selected\r\n", parms[0]);
533 /* The commands below require the SELECT state on a mailbox */
535 else if (!strcasecmp(parms[1], "FETCH")) {
536 imap_fetch(num_parms, parms);
539 else if ( (!strcasecmp(parms[1], "UID"))
540 && (!strcasecmp(parms[2], "FETCH")) ) {
541 imap_uidfetch(num_parms, parms);
544 else if (!strcasecmp(parms[1], "SEARCH")) {
545 imap_search(num_parms, parms);
548 else if ( (!strcasecmp(parms[1], "UID"))
549 && (!strcasecmp(parms[2], "SEARCH")) ) {
550 imap_uidsearch(num_parms, parms);
553 else if (!strcasecmp(parms[1], "CLOSE")) {
554 imap_close(num_parms, parms);
557 /* End of commands. If we get here, the command is either invalid
562 cprintf("%s BAD command unrecognized\r\n", parms[0]);
570 * This function is called by dynloader.c to register the IMAP module
571 * with the Citadel server.
573 char *Dynamic_Module_Init(void)
575 SYM_IMAP = CtdlGetDynamicSymbol();
576 CtdlRegisterServiceHook(config.c_imap_port,
580 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);