4 * IMAP server for the Citadel/UX system
5 * Copyright (C) 2000 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * *** THIS IS UNDER DEVELOPMENT. IT DOES NOT WORK. DO NOT USE IT. ***
20 #include <sys/types.h>
29 #include "sysdep_decls.h"
30 #include "citserver.h"
33 #include "dynloader.h"
40 #include "internet_addressing.h"
41 #include "serv_imap.h"
42 #include "imap_tools.h"
43 #include "imap_fetch.h"
44 #include "imap_search.h"
51 * If there is a message ID map in memory, free it
53 void imap_free_msgids(void) {
54 if (IMAP->msgids != NULL) {
59 if (IMAP->flags != NULL) {
67 * Back end for imap_load_msgids()
69 * FIXME: this should be optimized by figuring out a way to allocate memory
70 * once rather than doing a reallok() for each message.
72 void imap_add_single_msgid(long msgnum, void *userdata) {
74 IMAP->num_msgs = IMAP->num_msgs + 1;
75 if (IMAP->msgids == NULL) {
76 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
79 IMAP->msgids = reallok(IMAP->msgids,
80 IMAP->num_msgs * sizeof(long));
82 if (IMAP->flags == NULL) {
83 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
86 IMAP->flags = reallok(IMAP->flags,
87 IMAP->num_msgs * sizeof(long));
89 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
90 IMAP->flags[IMAP->num_msgs - 1] = 0;
96 * Set up a message ID map for the current room (folder)
98 void imap_load_msgids(void) {
100 if (IMAP->selected == 0) {
101 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
105 imap_free_msgids(); /* If there was already a map, free it */
107 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
108 imap_add_single_msgid, NULL);
110 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
117 * This cleanup function blows away the temporary memory and files used by
120 void imap_cleanup_function(void) {
122 /* Don't do this stuff if this is not a IMAP session! */
123 if (CC->h_command_function != imap_command_loop) return;
125 lprintf(9, "Performing IMAP cleanup hook\n");
127 lprintf(9, "Finished IMAP cleanup hook\n");
133 * Here's where our IMAP session begins its happy day.
135 void imap_greeting(void) {
137 strcpy(CC->cs_clientname, "IMAP session");
138 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
139 IMAP->authstate = imap_as_normal;
141 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
147 * implements the LOGIN command (ordinary username/password login)
149 void imap_login(int num_parms, char *parms[]) {
150 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
151 if (CtdlTryPassword(parms[3]) == pass_ok) {
152 cprintf("%s OK login successful\r\n", parms[0]);
157 cprintf("%s BAD Login incorrect\r\n", parms[0]);
162 * Implements the AYTHENTICATE command
164 void imap_authenticate(int num_parms, char *parms[]) {
167 if (num_parms != 3) {
168 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
172 if (!strcasecmp(parms[2], "LOGIN")) {
173 encode_base64(buf, "Username:");
174 cprintf("+ %s\r\n", buf);
175 IMAP->authstate = imap_as_expecting_username;
176 strcpy(IMAP->authseq, parms[0]);
181 cprintf("%s NO AUTHENTICATE %s failed\r\n",
186 void imap_auth_login_user(char *cmd) {
189 decode_base64(buf, cmd);
190 CtdlLoginExistingUser(buf);
191 encode_base64(buf, "Password:");
192 cprintf("+ %s\r\n", buf);
193 IMAP->authstate = imap_as_expecting_password;
197 void imap_auth_login_pass(char *cmd) {
200 decode_base64(buf, cmd);
201 if (CtdlTryPassword(buf) == pass_ok) {
202 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
205 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
207 IMAP->authstate = imap_as_normal;
214 * implements the CAPABILITY command
216 void imap_capability(int num_parms, char *parms[]) {
217 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
218 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
226 * implements the SELECT command
228 void imap_select(int num_parms, char *parms[]) {
230 char augmented_roomname[256];
234 struct quickroom QRscratch;
237 strcpy(towhere, parms[2]);
239 /* IMAP uses the reserved name "INBOX" for the user's default incoming
240 * mail folder. Convert this to whatever Citadel is using for the
241 * default mail room name (usually "Mail>").
243 if (!strcasecmp(towhere, "INBOX")) {
244 strcpy(towhere, MAILROOM);
247 /* First try a regular match */
248 c = getroom(&QRscratch, towhere);
250 /* Then try a mailbox name match */
252 MailboxName(augmented_roomname, &CC->usersupp, towhere);
253 c = getroom(&QRscratch, augmented_roomname);
255 strcpy(towhere, augmented_roomname);
258 /* If the room exists, check security/access */
260 /* See if there is an existing user/room relationship */
261 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
263 /* normal clients have to pass through security */
269 /* Fail here if no such room */
271 cprintf("%s NO ... no such room, or access denied\r\n",
278 * usergoto() formally takes us to the desired room, happily returning
279 * the number of messages and number of new messages.
281 usergoto(QRscratch.QRname, 0, &msgs, &new);
284 if (!strcasecmp(parms[1], "EXAMINE")) {
293 /* FIXME ... much more info needs to be supplied here */
294 cprintf("* %d EXISTS\r\n", msgs);
295 cprintf("* %d RECENT\r\n", new);
296 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
297 cprintf("%s OK [%s] %s completed\r\n",
299 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
306 * implements the CLOSE command
308 void imap_close(int num_parms, char *parms[]) {
312 cprintf("%s OK CLOSE completed\r\n", parms[0]);
320 * Back end for imap_lsub()
322 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
323 * may or may not be the desired behavior in the future.
325 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
329 /* Only list rooms to which the user has access!! */
330 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
332 imap_mailboxname(buf, sizeof buf, qrbuf);
333 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
339 * Implements the LSUB command
341 * FIXME: Handle wildcards, please.
343 void imap_lsub(int num_parms, char *parms[]) {
344 ForEachRoom(imap_lsub_listroom, NULL);
345 cprintf("%s OK LSUB completed\r\n", parms[0]);
351 * Back end for imap_list()
353 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
357 /* Only list rooms to which the user has access!! */
358 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
360 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
361 imap_mailboxname(buf, sizeof buf, qrbuf);
362 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
368 * Implements the LIST command
370 * FIXME: Handle wildcards, please.
372 void imap_list(int num_parms, char *parms[]) {
373 ForEachRoom(imap_list_listroom, NULL);
374 cprintf("%s OK LIST completed\r\n", parms[0]);
380 * Main command loop for IMAP sessions.
382 void imap_command_loop(void) {
388 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
389 if (client_gets(cmdbuf) < 1) {
390 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
395 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
396 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
399 /* strip off l/t whitespace and CRLF */
400 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
401 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
404 /* If we're in the middle of a multi-line command, handle that */
405 if (IMAP->authstate == imap_as_expecting_username) {
406 imap_auth_login_user(cmdbuf);
409 if (IMAP->authstate == imap_as_expecting_password) {
410 imap_auth_login_pass(cmdbuf);
415 /* Ok, at this point we're in normal command mode */
418 num_parms = imap_parameterize(parms, cmdbuf);
420 /* commands which may be executed in any state */
422 if ( (!strcasecmp(parms[1], "NOOP"))
423 || (!strcasecmp(parms[1], "CHECK")) ) {
424 cprintf("%s OK This command successfully did nothing.\r\n",
428 else if (!strcasecmp(parms[1], "LOGOUT")) {
429 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
430 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
435 else if (!strcasecmp(parms[1], "LOGIN")) {
436 imap_login(num_parms, parms);
439 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
440 imap_authenticate(num_parms, parms);
443 else if (!strcasecmp(parms[1], "CAPABILITY")) {
444 imap_capability(num_parms, parms);
447 else if (!CC->logged_in) {
448 cprintf("%s BAD Not logged in.\r\n", parms[0]);
451 /* commands requiring the client to be logged in */
453 else if (!strcasecmp(parms[1], "SELECT")) {
454 imap_select(num_parms, parms);
457 else if (!strcasecmp(parms[1], "EXAMINE")) {
458 imap_select(num_parms, parms);
461 else if (!strcasecmp(parms[1], "LSUB")) {
462 imap_lsub(num_parms, parms);
465 else if (!strcasecmp(parms[1], "LIST")) {
466 imap_list(num_parms, parms);
469 else if (IMAP->selected == 0) {
470 cprintf("%s BAD no folder selected\r\n", parms[0]);
473 /* commands requiring the SELECT state */
475 else if (!strcasecmp(parms[1], "FETCH")) {
476 imap_fetch(num_parms, parms);
479 else if ( (!strcasecmp(parms[1], "UID"))
480 && (!strcasecmp(parms[2], "FETCH")) ) {
481 imap_uidfetch(num_parms, parms);
484 else if (!strcasecmp(parms[1], "SEARCH")) {
485 imap_search(num_parms, parms);
488 else if ( (!strcasecmp(parms[1], "UID"))
489 && (!strcasecmp(parms[2], "SEARCH")) ) {
490 imap_uidsearch(num_parms, parms);
493 else if (!strcasecmp(parms[1], "CLOSE")) {
494 imap_close(num_parms, parms);
497 /* end of commands */
500 cprintf("%s BAD command unrecognized\r\n", parms[0]);
507 char *Dynamic_Module_Init(void)
509 SYM_IMAP = CtdlGetDynamicSymbol();
510 CtdlRegisterServiceHook(config.c_imap_port,
514 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);