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]);
326 * Used by LIST and LSUB to show the floors in the listing
328 void imap_list_floors(char *cmd) {
332 for (i=0; i<MAXFLOORS; ++i) {
334 if (fl->f_flags & F_INUSE) {
335 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
336 imap_strout(fl->f_name);
345 * Back end for imap_lsub()
347 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
348 * may or may not be the desired behavior in the future.
350 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
354 /* Only list rooms to which the user has access!! */
355 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
357 imap_mailboxname(buf, sizeof buf, qrbuf);
358 cprintf("* LSUB () \"|\" ");
366 * Implements the LSUB command
368 * FIXME: Handle wildcards, please.
370 void imap_lsub(int num_parms, char *parms[]) {
371 imap_list_floors("LSUB");
372 ForEachRoom(imap_lsub_listroom, NULL);
373 cprintf("%s OK LSUB completed\r\n", parms[0]);
379 * Back end for imap_list()
381 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
385 /* Only list rooms to which the user has access!! */
386 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
388 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
389 imap_mailboxname(buf, sizeof buf, qrbuf);
390 cprintf("* LIST () \"|\" ");
398 * Implements the LIST command
400 * FIXME: Handle wildcards, please.
402 void imap_list(int num_parms, char *parms[]) {
403 imap_list_floors("LIST");
404 ForEachRoom(imap_list_listroom, NULL);
405 cprintf("%s OK LIST completed\r\n", parms[0]);
411 * Implements the CREATE command (FIXME not finished yet)
414 void imap_create(int num_parms, char *parms[]) {
421 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
423 cprintf("%s NO Invalid mailbox name or location\r\n",
427 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
428 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
430 if (flags & IR_MAILBOX) {
431 newroomtype = 4; /* private mailbox */
434 newroomtype = 0; /* public folder */
437 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
438 roomname, floornum, newroomtype);
440 ret = create_room(roomname, newroomtype, "", floornum, 1);
442 cprintf("%s NO Mailbox already exists, or create failed\r\n",
446 cprintf("%s OK CREATE completed\r\n", parms[0]);
453 * Main command loop for IMAP sessions.
455 void imap_command_loop(void) {
461 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
462 if (client_gets(cmdbuf) < 1) {
463 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
468 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
469 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
472 /* strip off l/t whitespace and CRLF */
473 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
474 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
477 /* If we're in the middle of a multi-line command, handle that */
478 if (IMAP->authstate == imap_as_expecting_username) {
479 imap_auth_login_user(cmdbuf);
482 if (IMAP->authstate == imap_as_expecting_password) {
483 imap_auth_login_pass(cmdbuf);
488 /* Ok, at this point we're in normal command mode */
490 /* Grab the tag, command, and parameters. Check syntax. */
491 num_parms = imap_parameterize(parms, cmdbuf);
493 cprintf("BAD syntax error\r\n");
496 /* The commands below may be executed in any state */
498 else if ( (!strcasecmp(parms[1], "NOOP"))
499 || (!strcasecmp(parms[1], "CHECK")) ) {
500 cprintf("%s OK This command successfully did nothing.\r\n",
504 else if (!strcasecmp(parms[1], "LOGOUT")) {
505 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
506 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
511 else if (!strcasecmp(parms[1], "LOGIN")) {
512 imap_login(num_parms, parms);
515 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
516 imap_authenticate(num_parms, parms);
519 else if (!strcasecmp(parms[1], "CAPABILITY")) {
520 imap_capability(num_parms, parms);
523 else if (!CC->logged_in) {
524 cprintf("%s BAD Not logged in.\r\n", parms[0]);
527 /* The commans below require a logged-in state */
529 else if (!strcasecmp(parms[1], "SELECT")) {
530 imap_select(num_parms, parms);
533 else if (!strcasecmp(parms[1], "EXAMINE")) {
534 imap_select(num_parms, parms);
537 else if (!strcasecmp(parms[1], "LSUB")) {
538 imap_lsub(num_parms, parms);
541 else if (!strcasecmp(parms[1], "LIST")) {
542 imap_list(num_parms, parms);
545 else if (!strcasecmp(parms[1], "CREATE")) {
546 imap_create(num_parms, parms);
549 else if (IMAP->selected == 0) {
550 cprintf("%s BAD no folder selected\r\n", parms[0]);
553 /* The commands below require the SELECT state on a mailbox */
555 else if (!strcasecmp(parms[1], "FETCH")) {
556 imap_fetch(num_parms, parms);
559 else if ( (!strcasecmp(parms[1], "UID"))
560 && (!strcasecmp(parms[2], "FETCH")) ) {
561 imap_uidfetch(num_parms, parms);
564 else if (!strcasecmp(parms[1], "SEARCH")) {
565 imap_search(num_parms, parms);
568 else if ( (!strcasecmp(parms[1], "UID"))
569 && (!strcasecmp(parms[2], "SEARCH")) ) {
570 imap_uidsearch(num_parms, parms);
573 else if (!strcasecmp(parms[1], "CLOSE")) {
574 imap_close(num_parms, parms);
577 /* End of commands. If we get here, the command is either invalid
582 cprintf("%s BAD command unrecognized\r\n", parms[0]);
590 * This function is called by dynloader.c to register the IMAP module
591 * with the Citadel server.
593 char *Dynamic_Module_Init(void)
595 SYM_IMAP = CtdlGetDynamicSymbol();
596 CtdlRegisterServiceHook(config.c_imap_port,
600 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);