]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* more imap. imap sucks. die crispin die.
[citadel.git] / citadel / serv_imap.c
1 /*
2  * $Id$ 
3  *
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.
7  *
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.
10  *
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.
13  * 
14  */
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <sys/wait.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <limits.h>
30 #include "citadel.h"
31 #include "server.h"
32 #include <time.h>
33 #include "sysdep_decls.h"
34 #include "citserver.h"
35 #include "support.h"
36 #include "config.h"
37 #include "dynloader.h"
38 #include "room_ops.h"
39 #include "user_ops.h"
40 #include "policy.h"
41 #include "database.h"
42 #include "msgbase.h"
43 #include "tools.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"
49
50
51 long SYM_IMAP;
52
53
54 /*
55  * If there is a message ID map in memory, free it
56  */
57 void imap_free_msgids(void) {
58         if (IMAP->msgids != NULL) {
59                 phree(IMAP->msgids);
60                 IMAP->msgids = NULL;
61                 IMAP->num_msgs = 0;
62         }
63         if (IMAP->flags != NULL) {
64                 phree(IMAP->flags);
65                 IMAP->flags = NULL;
66         }
67 }
68
69
70 /*
71  * Back end for imap_load_msgids()
72  *
73  * FIXME: this should be optimized by figuring out a way to allocate memory
74  * once rather than doing a reallok() for each message.
75  */
76 void imap_add_single_msgid(long msgnum, void *userdata) {
77         
78         IMAP->num_msgs = IMAP->num_msgs + 1;
79         if (IMAP->msgids == NULL) {
80                 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
81         }
82         else {
83                 IMAP->msgids = reallok(IMAP->msgids,
84                         IMAP->num_msgs * sizeof(long));
85         }
86         if (IMAP->flags == NULL) {
87                 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
88         }
89         else {
90                 IMAP->flags = reallok(IMAP->flags,
91                         IMAP->num_msgs * sizeof(long));
92         }
93         IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
94         IMAP->flags[IMAP->num_msgs - 1] = 0;
95 }
96
97
98
99 /*
100  * Set up a message ID map for the current room (folder)
101  */
102 void imap_load_msgids(void) {
103          
104         if (IMAP->selected == 0) {
105                 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
106                 return;
107         }
108
109         imap_free_msgids();     /* If there was already a map, free it */
110
111         CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
112                 imap_add_single_msgid, NULL);
113
114         lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
115 }
116
117
118
119
120 /*
121  * This cleanup function blows away the temporary memory and files used by
122  * the IMAP server.
123  */
124 void imap_cleanup_function(void) {
125
126         /* Don't do this stuff if this is not a IMAP session! */
127         if (CC->h_command_function != imap_command_loop) return;
128
129         lprintf(9, "Performing IMAP cleanup hook\n");
130         imap_free_msgids();
131         lprintf(9, "Finished IMAP cleanup hook\n");
132 }
133
134
135
136 /*
137  * Here's where our IMAP session begins its happy day.
138  */
139 void imap_greeting(void) {
140
141         strcpy(CC->cs_clientname, "IMAP session");
142         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
143         IMAP->authstate = imap_as_normal;
144
145         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
146                 config.c_fqdn);
147 }
148
149
150 /*
151  * implements the LOGIN command (ordinary username/password login)
152  */
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]);
157                         return;
158                 }
159         }
160
161         cprintf("%s BAD Login incorrect\r\n", parms[0]);
162 }
163
164
165 /*
166  * Implements the AUTHENTICATE command
167  */
168 void imap_authenticate(int num_parms, char *parms[]) {
169         char buf[SIZ];
170
171         if (num_parms != 3) {
172                 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
173                 return;
174         }
175
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]);
181                 return;
182         }
183
184         else {
185                 cprintf("%s NO AUTHENTICATE %s failed\r\n",
186                         parms[0], parms[1]);
187         }
188 }
189
190 void imap_auth_login_user(char *cmd) {
191         char buf[SIZ];
192
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;
198         return;
199 }
200
201 void imap_auth_login_pass(char *cmd) {
202         char buf[SIZ];
203
204         decode_base64(buf, cmd);
205         if (CtdlTryPassword(buf) == pass_ok) {
206                 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
207         }
208         else {
209                 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
210         }
211         IMAP->authstate = imap_as_normal;
212         return;
213 }
214
215
216
217 /*
218  * implements the CAPABILITY command
219  */
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]);
223 }
224
225
226
227
228
229 /*
230  * implements the SELECT command
231  */
232 void imap_select(int num_parms, char *parms[]) {
233         char towhere[SIZ];
234         char augmented_roomname[SIZ];
235         int c = 0;
236         int ok = 0;
237         int ra = 0;
238         struct quickroom QRscratch;
239         int msgs, new;
240         int floornum;
241
242         /* Convert the supplied folder name to a roomname */
243         floornum = imap_roomname(towhere, sizeof towhere, parms[2]);
244         if (floornum < 0) {
245                 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
246                 IMAP->selected = 0;
247                 return;
248         }
249
250         /* First try a regular match */
251         c = getroom(&QRscratch, towhere);
252
253         /* Then try a mailbox name match */
254         if (c != 0) {
255                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
256                 c = getroom(&QRscratch, augmented_roomname);
257                 if (c == 0)
258                         strcpy(towhere, augmented_roomname);
259         }
260
261         /* If the room exists, check security/access */
262         if (c == 0) {
263                 /* See if there is an existing user/room relationship */
264                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
265
266                 /* normal clients have to pass through security */
267                 if (ra & UA_KNOWN) {
268                         ok = 1;
269                 }
270         }
271
272         /* Fail here if no such room */
273         if (!ok) {
274                 cprintf("%s NO ... no such room, or access denied\r\n",
275                         parms[0]);
276                 IMAP->selected = 0;
277                 return;
278         }
279
280         /*
281          * usergoto() formally takes us to the desired room, happily returning
282          * the number of messages and number of new messages.
283          */
284         usergoto(QRscratch.QRname, 0, &msgs, &new);
285         IMAP->selected = 1;
286
287         if (!strcasecmp(parms[1], "EXAMINE")) {
288                 IMAP->readonly = 1;
289         }
290         else {
291                 IMAP->readonly = 0;
292         }
293
294         imap_load_msgids();
295
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",
301                 parms[0],
302                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
303                 parms[1]);
304 }
305
306
307
308 /*
309  * implements the CLOSE command
310  */
311 void imap_close(int num_parms, char *parms[]) {
312         IMAP->selected = 0;
313         IMAP->readonly = 0;
314         imap_free_msgids();
315         cprintf("%s OK CLOSE completed\r\n", parms[0]);
316 }
317
318
319
320
321
322 /*
323  * Back end for imap_lsub()
324  *
325  * IMAP "subscribed folder" is equivocated to Citadel "known rooms."  This
326  * may or may not be the desired behavior in the future.
327  */
328 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
329         char buf[SIZ];
330         int ra;
331
332         /* Only list rooms to which the user has access!! */
333         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
334         if (ra & UA_KNOWN) {
335                 imap_mailboxname(buf, sizeof buf, qrbuf);
336                 cprintf("* LSUB () \"|\" ");
337                 imap_strout(buf);
338                 cprintf("\r\n");
339         }
340 }
341
342
343 /*
344  * Implements the LSUB command
345  *
346  * FIXME: Handle wildcards, please.
347  */
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]);
351 }
352
353
354
355 /*
356  * Back end for imap_list()
357  */
358 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
359         char buf[SIZ];
360         int ra;
361
362         /* Only list rooms to which the user has access!! */
363         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
364         if ( (ra & UA_KNOWN) 
365           || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
366                 imap_mailboxname(buf, sizeof buf, qrbuf);
367                 cprintf("* LIST () \"|\" ");
368                 imap_strout(buf);
369                 cprintf("\r\n");
370         }
371 }
372
373
374 /*
375  * Implements the LIST command
376  *
377  * FIXME: Handle wildcards, please.
378  */
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]);
382 }
383
384
385
386 /*
387  * Implements the CREATE command (FIXME not finished yet)
388  *
389  */
390 void imap_create(int num_parms, char *parms[]) {
391         int floornum;
392         int levels;
393
394         floornum = imap_roomname(parms[1]);
395         if (floornum < 0) {
396                 cprintf("%s NO Invalid name\r\n", parms[0]);
397                 return;
398         }
399
400         cprintf("%s NO CREATE unimplemented\r\n", parms[0]);
401 }
402
403
404
405 /* 
406  * Main command loop for IMAP sessions.
407  */
408 void imap_command_loop(void) {
409         char cmdbuf[SIZ];
410         char *parms[SIZ];
411         int num_parms;
412
413         time(&CC->lastcmd);
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");
417                 CC->kill_me = 1;
418                 return;
419         }
420
421         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
422         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
423
424
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;
428         striplt(cmdbuf);
429
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);
433                 return;
434         }
435         if (IMAP->authstate == imap_as_expecting_password) {
436                 imap_auth_login_pass(cmdbuf);
437                 return;
438         }
439
440
441         /* Ok, at this point we're in normal command mode */
442
443         /* Grab the tag, command, and parameters.  Check syntax. */
444         num_parms = imap_parameterize(parms, cmdbuf);
445         if (num_parms < 2) {
446                 cprintf("BAD syntax error\r\n");
447         }
448
449         /* The commands below may be executed in any state */
450
451         else if ( (!strcasecmp(parms[1], "NOOP"))
452            || (!strcasecmp(parms[1], "CHECK")) ) {
453                 cprintf("%s OK This command successfully did nothing.\r\n",
454                         parms[0]);
455         }
456
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]);
460                 CC->kill_me = 1;
461                 return;
462         }
463
464         else if (!strcasecmp(parms[1], "LOGIN")) {
465                 imap_login(num_parms, parms);
466         }
467
468         else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
469                 imap_authenticate(num_parms, parms);
470         }
471
472         else if (!strcasecmp(parms[1], "CAPABILITY")) {
473                 imap_capability(num_parms, parms);
474         }
475
476         else if (!CC->logged_in) {
477                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
478         }
479
480         /* The commans below require a logged-in state */
481
482         else if (!strcasecmp(parms[1], "SELECT")) {
483                 imap_select(num_parms, parms);
484         }
485
486         else if (!strcasecmp(parms[1], "EXAMINE")) {
487                 imap_select(num_parms, parms);
488         }
489
490         else if (!strcasecmp(parms[1], "LSUB")) {
491                 imap_lsub(num_parms, parms);
492         }
493
494         else if (!strcasecmp(parms[1], "LIST")) {
495                 imap_list(num_parms, parms);
496         }
497
498         else if (!strcasecmp(parms[1], "CREATE")) {
499                 imap_create(num_parms, parms);
500         }
501
502         else if (IMAP->selected == 0) {
503                 cprintf("%s BAD no folder selected\r\n", parms[0]);
504         }
505
506         /* The commands below require the SELECT state on a mailbox */
507
508         else if (!strcasecmp(parms[1], "FETCH")) {
509                 imap_fetch(num_parms, parms);
510         }
511
512         else if ( (!strcasecmp(parms[1], "UID"))
513                 && (!strcasecmp(parms[2], "FETCH")) ) {
514                 imap_uidfetch(num_parms, parms);
515         }
516
517         else if (!strcasecmp(parms[1], "SEARCH")) {
518                 imap_search(num_parms, parms);
519         }
520
521         else if ( (!strcasecmp(parms[1], "UID"))
522                 && (!strcasecmp(parms[2], "SEARCH")) ) {
523                 imap_uidsearch(num_parms, parms);
524         }
525
526         else if (!strcasecmp(parms[1], "CLOSE")) {
527                 imap_close(num_parms, parms);
528         }
529
530         /* End of commands.  If we get here, the command is either invalid
531          * or unimplemented.
532          */
533
534         else {
535                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
536         }
537
538 }
539
540
541
542 /*
543  * This function is called by dynloader.c to register the IMAP module
544  * with the Citadel server.
545  */
546 char *Dynamic_Module_Init(void)
547 {
548         SYM_IMAP = CtdlGetDynamicSymbol();
549         CtdlRegisterServiceHook(config.c_imap_port,
550                                 NULL,
551                                 imap_greeting,
552                                 imap_command_loop);
553         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
554         return "$Id$";
555 }