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"
50 * If there is a message ID map in memory, free it
52 void imap_free_msgids(void) {
53 if (IMAP->msgids != NULL) {
58 if (IMAP->flags != NULL) {
66 * Back end for imap_load_msgids()
68 * FIXME: this should be optimized by figuring out a way to allocate memory
69 * once rather than doing a reallok() for each message.
71 void imap_add_single_msgid(long msgnum, void *userdata) {
73 IMAP->num_msgs = IMAP->num_msgs + 1;
74 if (IMAP->msgids == NULL) {
75 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
78 IMAP->msgids = reallok(IMAP->msgids,
79 IMAP->num_msgs * sizeof(long));
81 if (IMAP->flags == NULL) {
82 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
85 IMAP->flags = reallok(IMAP->flags,
86 IMAP->num_msgs * sizeof(long));
88 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
89 IMAP->flags[IMAP->num_msgs - 1] = 0;
95 * Set up a message ID map for the current room (folder)
97 void imap_load_msgids(void) {
99 if (IMAP->selected == 0) {
100 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
104 imap_free_msgids(); /* If there was already a map, free it */
106 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
107 imap_add_single_msgid, NULL);
109 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
116 * This cleanup function blows away the temporary memory and files used by
119 void imap_cleanup_function(void) {
121 /* Don't do this stuff if this is not a IMAP session! */
122 if (CC->h_command_function != imap_command_loop) return;
124 lprintf(9, "Performing IMAP cleanup hook\n");
126 lprintf(9, "Finished IMAP cleanup hook\n");
132 * Here's where our IMAP session begins its happy day.
134 void imap_greeting(void) {
136 strcpy(CC->cs_clientname, "IMAP session");
137 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
138 IMAP->authstate = imap_as_normal;
140 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
146 * implements the LOGIN command (ordinary username/password login)
148 void imap_login(int num_parms, char *parms[]) {
149 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
150 if (CtdlTryPassword(parms[3]) == pass_ok) {
151 cprintf("%s OK login successful\r\n", parms[0]);
156 cprintf("%s BAD Login incorrect\r\n", parms[0]);
161 * Implements the AYTHENTICATE command
163 void imap_authenticate(int num_parms, char *parms[]) {
166 if (num_parms != 3) {
167 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
171 if (!strcasecmp(parms[2], "LOGIN")) {
172 encode_base64(buf, "Username:");
173 cprintf("+ %s\r\n", buf);
174 IMAP->authstate = imap_as_expecting_username;
175 strcpy(IMAP->authseq, parms[0]);
180 cprintf("%s NO AUTHENTICATE %s failed\r\n",
185 void imap_auth_login_user(char *cmd) {
188 decode_base64(buf, cmd);
189 CtdlLoginExistingUser(buf);
190 encode_base64(buf, "Password:");
191 cprintf("+ %s\r\n", buf);
192 IMAP->authstate = imap_as_expecting_password;
196 void imap_auth_login_pass(char *cmd) {
199 decode_base64(buf, cmd);
200 if (CtdlTryPassword(buf) == pass_ok) {
201 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
204 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
206 IMAP->authstate = imap_as_normal;
213 * implements the CAPABILITY command
215 void imap_capability(int num_parms, char *parms[]) {
216 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
217 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
225 * implements the SELECT command
227 void imap_select(int num_parms, char *parms[]) {
229 char augmented_roomname[256];
233 struct quickroom QRscratch;
236 strcpy(towhere, parms[2]);
238 /* IMAP uses the reserved name "INBOX" for the user's default incoming
239 * mail folder. Convert this to whatever Citadel is using for the
240 * default mail room name (usually "Mail>").
242 if (!strcasecmp(towhere, "INBOX")) {
243 strcpy(towhere, MAILROOM);
246 /* First try a regular match */
247 c = getroom(&QRscratch, towhere);
249 /* Then try a mailbox name match */
251 MailboxName(augmented_roomname, &CC->usersupp, towhere);
252 c = getroom(&QRscratch, augmented_roomname);
254 strcpy(towhere, augmented_roomname);
257 /* If the room exists, check security/access */
259 /* See if there is an existing user/room relationship */
260 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
262 /* normal clients have to pass through security */
268 /* Fail here if no such room */
270 cprintf("%s NO ... no such room, or access denied\r\n",
277 * usergoto() formally takes us to the desired room, happily returning
278 * the number of messages and number of new messages.
280 usergoto(QRscratch.QRname, 0, &msgs, &new);
283 if (!strcasecmp(parms[1], "EXAMINE")) {
292 /* FIXME ... much more info needs to be supplied here */
293 cprintf("* %d EXISTS\r\n", msgs);
294 cprintf("* %d RECENT\r\n", new);
295 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
296 cprintf("%s OK [%s] %s completed\r\n",
298 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
305 * implements the CLOSE command
307 void imap_close(int num_parms, char *parms[]) {
311 cprintf("%s OK CLOSE completed\r\n", parms[0]);
319 * Back end for imap_lsub()
321 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
322 * may or may not be the desired behavior in the future.
324 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
328 /* Only list rooms to which the user has access!! */
329 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
331 imap_mailboxname(buf, sizeof buf, qrbuf);
332 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
338 * Implements the LSUB command
340 * FIXME: Handle wildcards, please.
342 void imap_lsub(int num_parms, char *parms[]) {
343 ForEachRoom(imap_lsub_listroom, NULL);
344 cprintf("%s OK LSUB completed\r\n", parms[0]);
350 * Back end for imap_list()
352 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
356 /* Only list rooms to which the user has access!! */
357 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
359 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
360 imap_mailboxname(buf, sizeof buf, qrbuf);
361 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
367 * Implements the LIST command
369 * FIXME: Handle wildcards, please.
371 void imap_list(int num_parms, char *parms[]) {
372 ForEachRoom(imap_list_listroom, NULL);
373 cprintf("%s OK LIST completed\r\n", parms[0]);
379 * Main command loop for IMAP sessions.
381 void imap_command_loop(void) {
387 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
388 if (client_gets(cmdbuf) < 1) {
389 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
394 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
395 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
398 /* strip off l/t whitespace and CRLF */
399 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
400 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
403 /* If we're in the middle of a multi-line command, handle that */
404 if (IMAP->authstate == imap_as_expecting_username) {
405 imap_auth_login_user(cmdbuf);
408 if (IMAP->authstate == imap_as_expecting_password) {
409 imap_auth_login_pass(cmdbuf);
414 /* Ok, at this point we're in normal command mode */
417 num_parms = imap_parameterize(parms, cmdbuf);
419 /* commands which may be executed in any state */
421 if ( (!strcasecmp(parms[1], "NOOP"))
422 || (!strcasecmp(parms[1], "CHECK")) ) {
423 cprintf("%s OK This command successfully did nothing.\r\n",
427 else if (!strcasecmp(parms[1], "LOGOUT")) {
428 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
429 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
434 else if (!strcasecmp(parms[1], "LOGIN")) {
435 imap_login(num_parms, parms);
438 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
439 imap_authenticate(num_parms, parms);
442 else if (!strcasecmp(parms[1], "CAPABILITY")) {
443 imap_capability(num_parms, parms);
446 else if (!CC->logged_in) {
447 cprintf("%s BAD Not logged in.\r\n", parms[0]);
450 /* commands requiring the client to be logged in */
452 else if (!strcasecmp(parms[1], "SELECT")) {
453 imap_select(num_parms, parms);
456 else if (!strcasecmp(parms[1], "EXAMINE")) {
457 imap_select(num_parms, parms);
460 else if (!strcasecmp(parms[1], "LSUB")) {
461 imap_lsub(num_parms, parms);
464 else if (!strcasecmp(parms[1], "LIST")) {
465 imap_list(num_parms, parms);
468 else if (IMAP->selected == 0) {
469 cprintf("%s BAD no folder selected\r\n", parms[0]);
472 /* commands requiring the SELECT state */
474 else if (!strcasecmp(parms[1], "FETCH")) {
475 imap_fetch(num_parms, parms);
478 else if ( (!strcasecmp(parms[1], "UID"))
479 && (!strcasecmp(parms[2], "FETCH")) ) {
480 imap_uidfetch(num_parms, parms);
483 else if (!strcasecmp(parms[1], "CLOSE")) {
484 imap_close(num_parms, parms);
487 /* end of commands */
490 cprintf("%s BAD command unrecognized\r\n", parms[0]);
497 char *Dynamic_Module_Init(void)
499 SYM_IMAP = CtdlGetDynamicSymbol();
500 CtdlRegisterServiceHook(config.c_imap_port,
504 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);