]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* imap_fetch.c: added support for fetch-->envelope-->from
[citadel.git] / citadel / serv_imap.c
1 /*
2  * $Id$ 
3  *
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.
7  *
8  * *** THIS IS UNDER DEVELOPMENT.  IT DOES NOT WORK.  DO NOT USE IT. ***
9  * 
10  */
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/time.h>
22 #include <sys/wait.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <limits.h>
26 #include "citadel.h"
27 #include "server.h"
28 #include <time.h>
29 #include "sysdep_decls.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "dynloader.h"
34 #include "room_ops.h"
35 #include "user_ops.h"
36 #include "policy.h"
37 #include "database.h"
38 #include "msgbase.h"
39 #include "tools.h"
40 #include "internet_addressing.h"
41 #include "serv_imap.h"
42 #include "imap_tools.h"
43 #include "imap_fetch.h"
44
45
46 long SYM_IMAP;
47
48
49 /*
50  * If there is a message ID map in memory, free it
51  */
52 void imap_free_msgids(void) {
53         if (IMAP->msgids != NULL) {
54                 phree(IMAP->msgids);
55                 IMAP->msgids = NULL;
56                 IMAP->num_msgs = 0;
57         }
58         if (IMAP->flags != NULL) {
59                 phree(IMAP->flags);
60                 IMAP->flags = NULL;
61         }
62 }
63
64
65 /*
66  * Back end for imap_load_msgids()
67  *
68  * FIXME: this should be optimized by figuring out a way to allocate memory
69  * once rather than doing a reallok() for each message.
70  */
71 void imap_add_single_msgid(long msgnum, void *userdata) {
72         
73         IMAP->num_msgs = IMAP->num_msgs + 1;
74         if (IMAP->msgids == NULL) {
75                 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
76         }
77         else {
78                 IMAP->msgids = reallok(IMAP->msgids,
79                         IMAP->num_msgs * sizeof(long));
80         }
81         if (IMAP->flags == NULL) {
82                 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
83         }
84         else {
85                 IMAP->flags = reallok(IMAP->flags,
86                         IMAP->num_msgs * sizeof(long));
87         }
88         IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
89         IMAP->flags[IMAP->num_msgs - 1] = 0;
90 }
91
92
93
94 /*
95  * Set up a message ID map for the current room (folder)
96  */
97 void imap_load_msgids(void) {
98          
99         if (IMAP->selected == 0) {
100                 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
101                 return;
102         }
103
104         imap_free_msgids();     /* If there was already a map, free it */
105
106         CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
107                 imap_add_single_msgid, NULL);
108
109         lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
110 }
111
112
113
114
115 /*
116  * This cleanup function blows away the temporary memory and files used by
117  * the IMAP server.
118  */
119 void imap_cleanup_function(void) {
120
121         /* Don't do this stuff if this is not a IMAP session! */
122         if (CC->h_command_function != imap_command_loop) return;
123
124         lprintf(9, "Performing IMAP cleanup hook\n");
125         imap_free_msgids();
126         lprintf(9, "Finished IMAP cleanup hook\n");
127 }
128
129
130
131 /*
132  * Here's where our IMAP session begins its happy day.
133  */
134 void imap_greeting(void) {
135
136         strcpy(CC->cs_clientname, "IMAP session");
137         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
138         IMAP->authstate = imap_as_normal;
139
140         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
141                 config.c_fqdn);
142 }
143
144
145 /*
146  * implements the LOGIN command (ordinary username/password login)
147  */
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]);
152                         return;
153                 }
154         }
155
156         cprintf("%s BAD Login incorrect\r\n", parms[0]);
157 }
158
159
160 /*
161  * Implements the AYTHENTICATE command
162  */
163 void imap_authenticate(int num_parms, char *parms[]) {
164         char buf[256];
165
166         if (num_parms != 3) {
167                 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
168                 return;
169         }
170
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]);
176                 return;
177         }
178
179         else {
180                 cprintf("%s NO AUTHENTICATE %s failed\r\n",
181                         parms[0], parms[1]);
182         }
183 }
184
185 void imap_auth_login_user(char *cmd) {
186         char buf[256];
187
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;
193         return;
194 }
195
196 void imap_auth_login_pass(char *cmd) {
197         char buf[256];
198
199         decode_base64(buf, cmd);
200         if (CtdlTryPassword(buf) == pass_ok) {
201                 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
202         }
203         else {
204                 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
205         }
206         IMAP->authstate = imap_as_normal;
207         return;
208 }
209
210
211
212 /*
213  * implements the CAPABILITY command
214  */
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]);
218 }
219
220
221
222
223
224 /*
225  * implements the SELECT command
226  */
227 void imap_select(int num_parms, char *parms[]) {
228         char towhere[256];
229         char augmented_roomname[256];
230         int c = 0;
231         int ok = 0;
232         int ra = 0;
233         struct quickroom QRscratch;
234         int msgs, new;
235
236         strcpy(towhere, parms[2]);
237
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>").
241          */
242         if (!strcasecmp(towhere, "INBOX")) {
243                 strcpy(towhere, MAILROOM);
244         }
245
246         /* First try a regular match */
247         c = getroom(&QRscratch, towhere);
248
249         /* Then try a mailbox name match */
250         if (c != 0) {
251                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
252                 c = getroom(&QRscratch, augmented_roomname);
253                 if (c == 0)
254                         strcpy(towhere, augmented_roomname);
255         }
256
257         /* If the room exists, check security/access */
258         if (c == 0) {
259                 /* See if there is an existing user/room relationship */
260                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
261
262                 /* normal clients have to pass through security */
263                 if (ra & UA_KNOWN) {
264                         ok = 1;
265                 }
266         }
267
268         /* Fail here if no such room */
269         if (!ok) {
270                 cprintf("%s NO ... no such room, or access denied\r\n",
271                         parms[0]);
272                 IMAP->selected = 0;
273                 return;
274         }
275
276         /*
277          * usergoto() formally takes us to the desired room, happily returning
278          * the number of messages and number of new messages.
279          */
280         usergoto(QRscratch.QRname, 0, &msgs, &new);
281         IMAP->selected = 1;
282
283         if (!strcasecmp(parms[1], "EXAMINE")) {
284                 IMAP->readonly = 1;
285         }
286         else {
287                 IMAP->readonly = 0;
288         }
289
290         imap_load_msgids();
291
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",
297                 parms[0],
298                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
299                 parms[1]);
300 }
301
302
303
304 /*
305  * implements the CLOSE command
306  */
307 void imap_close(int num_parms, char *parms[]) {
308         IMAP->selected = 0;
309         IMAP->readonly = 0;
310         imap_free_msgids();
311         cprintf("%s OK CLOSE completed\r\n", parms[0]);
312 }
313
314
315
316
317
318 /*
319  * Back end for imap_lsub()
320  *
321  * IMAP "subscribed folder" is equivocated to Citadel "known rooms."  This
322  * may or may not be the desired behavior in the future.
323  */
324 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
325         char buf[256];
326         int ra;
327
328         /* Only list rooms to which the user has access!! */
329         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
330         if (ra & UA_KNOWN) {
331                 imap_mailboxname(buf, sizeof buf, qrbuf);
332                 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
333         }
334 }
335
336
337 /*
338  * Implements the LSUB command
339  *
340  * FIXME: Handle wildcards, please.
341  */
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]);
345 }
346
347
348
349 /*
350  * Back end for imap_list()
351  */
352 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
353         char buf[256];
354         int ra;
355
356         /* Only list rooms to which the user has access!! */
357         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
358         if ( (ra & UA_KNOWN) 
359           || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
360                 imap_mailboxname(buf, sizeof buf, qrbuf);
361                 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
362         }
363 }
364
365
366 /*
367  * Implements the LIST command
368  *
369  * FIXME: Handle wildcards, please.
370  */
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]);
374 }
375
376
377
378 /* 
379  * Main command loop for IMAP sessions.
380  */
381 void imap_command_loop(void) {
382         char cmdbuf[256];
383         char *parms[256];
384         int num_parms;
385
386         time(&CC->lastcmd);
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");
390                 CC->kill_me = 1;
391                 return;
392         }
393
394         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
395         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
396
397
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;
401         striplt(cmdbuf);
402
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);
406                 return;
407         }
408         if (IMAP->authstate == imap_as_expecting_password) {
409                 imap_auth_login_pass(cmdbuf);
410                 return;
411         }
412
413
414         /* Ok, at this point we're in normal command mode */
415
416         /* grab the tag */
417         num_parms = imap_parameterize(parms, cmdbuf);
418
419         /* commands which may be executed in any state */
420
421         if ( (!strcasecmp(parms[1], "NOOP"))
422            || (!strcasecmp(parms[1], "CHECK")) ) {
423                 cprintf("%s OK This command successfully did nothing.\r\n",
424                         parms[0]);
425         }
426
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]);
430                 CC->kill_me = 1;
431                 return;
432         }
433
434         else if (!strcasecmp(parms[1], "LOGIN")) {
435                 imap_login(num_parms, parms);
436         }
437
438         else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
439                 imap_authenticate(num_parms, parms);
440         }
441
442         else if (!strcasecmp(parms[1], "CAPABILITY")) {
443                 imap_capability(num_parms, parms);
444         }
445
446         else if (!CC->logged_in) {
447                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
448         }
449
450         /* commands requiring the client to be logged in */
451
452         else if (!strcasecmp(parms[1], "SELECT")) {
453                 imap_select(num_parms, parms);
454         }
455
456         else if (!strcasecmp(parms[1], "EXAMINE")) {
457                 imap_select(num_parms, parms);
458         }
459
460         else if (!strcasecmp(parms[1], "LSUB")) {
461                 imap_lsub(num_parms, parms);
462         }
463
464         else if (!strcasecmp(parms[1], "LIST")) {
465                 imap_list(num_parms, parms);
466         }
467
468         else if (IMAP->selected == 0) {
469                 cprintf("%s BAD no folder selected\r\n", parms[0]);
470         }
471
472         /* commands requiring the SELECT state */
473
474         else if (!strcasecmp(parms[1], "FETCH")) {
475                 imap_fetch(num_parms, parms);
476         }
477
478         else if ( (!strcasecmp(parms[1], "UID"))
479                 && (!strcasecmp(parms[2], "FETCH")) ) {
480                 imap_uidfetch(num_parms, parms);
481         }
482
483         else if (!strcasecmp(parms[1], "CLOSE")) {
484                 imap_close(num_parms, parms);
485         }
486
487         /* end of commands */
488
489         else {
490                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
491         }
492
493 }
494
495
496
497 char *Dynamic_Module_Init(void)
498 {
499         SYM_IMAP = CtdlGetDynamicSymbol();
500         CtdlRegisterServiceHook(config.c_imap_port,
501                                 NULL,
502                                 imap_greeting,
503                                 imap_command_loop);
504         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
505         return "$Id$";
506 }