]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* Mark Crispin is a fscking idiot. IMAP is a convoluted mess.
[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
44
45 long SYM_IMAP;
46
47
48 /*
49  * If there is a message ID map in memory, free it
50  */
51 void imap_free_msgids(void) {
52         if (IMAP->msgids != NULL) {
53                 phree(IMAP->msgids);
54                 IMAP->msgids = NULL;
55                 IMAP->num_msgs = 0;
56         }
57 }
58
59
60 /*
61  * Back end for imap_load_msgids()
62  *
63  * FIXME: this should be optimized by figuring out a way to allocate memory
64  * once rather than doing a reallok() for each message.
65  */
66 void imap_add_single_msgid(long msgnum, void *userdata) {
67         
68         IMAP->num_msgs = IMAP->num_msgs + 1;
69         if (IMAP->msgids == NULL) {
70                 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
71         }
72         else {
73                 IMAP->msgids = reallok(IMAP->msgids,
74                         IMAP->num_msgs * sizeof(long));
75         }
76         IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
77 }
78
79
80
81 /*
82  * Set up a message ID map for the current room (folder)
83  */
84 void imap_load_msgids(void) {
85          
86         if (IMAP->selected == 0) {
87                 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
88                 return;
89         }
90
91         imap_free_msgids();     /* If there was already a map, free it */
92
93         CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
94                 imap_add_single_msgid, NULL);
95
96         lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
97 }
98
99
100
101
102 /*
103  * This cleanup function blows away the temporary memory and files used by
104  * the IMAP server.
105  */
106 void imap_cleanup_function(void) {
107
108         /* Don't do this stuff if this is not a IMAP session! */
109         if (CC->h_command_function != imap_command_loop) return;
110
111         lprintf(9, "Performing IMAP cleanup hook\n");
112         imap_free_msgids();
113         lprintf(9, "Finished IMAP cleanup hook\n");
114 }
115
116
117
118 /*
119  * Here's where our IMAP session begins its happy day.
120  */
121 void imap_greeting(void) {
122
123         strcpy(CC->cs_clientname, "IMAP session");
124         CC->internal_pgm = 1;
125         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
126
127         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
128                 config.c_fqdn);
129 }
130
131
132 /*
133  * implements the LOGIN command (ordinary username/password login)
134  */
135 void imap_login(int num_parms, char *parms[]) {
136         if (CtdlLoginExistingUser(parms[2]) == login_ok) {
137                 if (CtdlTryPassword(parms[3]) == pass_ok) {
138                         cprintf("%s OK login successful\r\n", parms[0]);
139                         return;
140                 }
141         }
142
143         cprintf("%s BAD Login incorrect\r\n", parms[0]);
144 }
145
146
147 /*
148  * implements the CAPABILITY command
149  */
150 void imap_capability(int num_parms, char *parms[]) {
151         cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
152         cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
153 }
154
155
156
157
158
159 /*
160  * implements the SELECT command
161  */
162 void imap_select(int num_parms, char *parms[]) {
163         char towhere[256];
164         char augmented_roomname[256];
165         int c = 0;
166         int ok = 0;
167         int ra = 0;
168         struct quickroom QRscratch;
169         int msgs, new;
170
171         strcpy(towhere, parms[2]);
172
173         /* IMAP uses the reserved name "INBOX" for the user's default incoming
174          * mail folder.  Convert this to Citadel's reserved name "_MAIL_".
175          */
176         if (!strcasecmp(towhere, "INBOX"))
177                 strcpy(towhere, MAILROOM);
178
179         /* First try a regular match */
180         c = getroom(&QRscratch, towhere);
181
182         /* Then try a mailbox name match */
183         if (c != 0) {
184                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
185                 c = getroom(&QRscratch, augmented_roomname);
186                 if (c == 0)
187                         strcpy(towhere, augmented_roomname);
188         }
189
190         /* If the room exists, check security/access */
191         if (c == 0) {
192                 /* See if there is an existing user/room relationship */
193                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
194
195                 /* normal clients have to pass through security */
196                 if (ra & UA_KNOWN)
197                         ok = 1;
198         }
199
200         /* Fail here if no such room */
201         if (!ok) {
202                 cprintf("%s NO ... no such room, or access denied\r\n",
203                         parms[0]);
204                 IMAP->selected = 0;
205                 return;
206         }
207
208         /*
209          * usergoto() formally takes us to the desired room, happily returning
210          * the number of messages and number of new messages.
211          */
212         usergoto(QRscratch.QRname, 0, &msgs, &new);
213         IMAP->selected = 1;
214
215         if (!strcasecmp(parms[1], "EXAMINE")) {
216                 IMAP->readonly = 1;
217         }
218         else {
219                 IMAP->readonly = 0;
220         }
221
222         imap_load_msgids();
223
224         /* FIXME ... much more info needs to be supplied here */
225         cprintf("* %d EXISTS\r\n", msgs);
226         cprintf("* %d RECENT\r\n", new);
227         cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
228         cprintf("%s OK [%s] %s completed\r\n",
229                 parms[0],
230                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
231                 parms[1]);
232 }
233
234
235
236 /*
237  * implements the CLOSE command
238  */
239 void imap_close(int num_parms, char *parms[]) {
240         IMAP->selected = 0;
241         IMAP->readonly = 0;
242         imap_free_msgids();
243         cprintf("%s OK CLOSE completed\r\n", parms[0]);
244 }
245
246
247
248
249
250 /*
251  * Back end for imap_lsub()
252  */
253 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
254         char buf[256];
255         
256         imap_mailboxname(buf, sizeof buf, qrbuf);
257         cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
258 }
259
260
261 /*
262  * Implements the LSUB command
263  *
264  * FIXME: Handle wildcards, please.
265  * FIXME: Currently we show all rooms as subscribed folders.  Need to handle
266  *        subscriptions properly.
267  */
268 void imap_lsub(int num_parms, char *parms[]) {
269         ForEachRoom(imap_lsub_listroom, NULL);
270         cprintf("%s OK LSUB completed\r\n", parms[0]);
271 }
272
273
274
275 /*
276  * Back end for imap_list()
277  */
278 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
279         char buf[256];
280         
281         imap_mailboxname(buf, sizeof buf, qrbuf);
282         cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
283 }
284
285
286 /*
287  * Implements the LIST command
288  *
289  * FIXME: Handle wildcards, please.
290  */
291 void imap_list(int num_parms, char *parms[]) {
292         ForEachRoom(imap_list_listroom, NULL);
293         cprintf("%s OK LIST completed\r\n", parms[0]);
294 }
295
296
297 /*
298  * Do the actual work for imap_fetch().  By the time this function is called,
299  * the "lo" and "hi" sequence numbers are already validated, and the data item
300  * names are, um... something.
301  */
302 void imap_do_fetch(int lo, int hi, char *items) {
303         cprintf("* lo=%d hi=%d items=<%s>\r\n", lo, hi, items);
304 }
305
306
307 /*
308  * Mark Crispin is a fscking idiot.
309  */
310 void imap_fetch(int num_parms, char *parms[]) {
311         int lo = 0;
312         int hi = 0;
313         char lostr[1024], histr[1024], items[1024];
314         int i;
315
316         if (num_parms < 4) {
317                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
318                 return;
319         }
320
321         extract_token(lostr, parms[2], 0, ':');
322         lo = atoi(lostr);
323         extract_token(histr, parms[2], 1, ':');
324         hi = atoi(histr);
325
326         if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
327                 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
328                         parms[0], lo, hi);
329                 return;
330         }
331
332         strcpy(items, "");
333         for (i=3; i<num_parms; ++i) {
334                 strcat(items, parms[i]);
335                 if (i < (num_parms-1)) strcat(items, " ");
336         }
337         for (i=0; i<strlen(items); ++i) {
338                 if (isspace(items[i])) items[i] = ' ';
339         }
340
341         imap_do_fetch(lo, hi, items);
342         cprintf("%s OK FETCH completed\r\n", parms[0]);
343 }
344
345
346
347
348 /* 
349  * Main command loop for IMAP sessions.
350  */
351 void imap_command_loop(void) {
352         char cmdbuf[256];
353         char *parms[16];
354         int num_parms;
355         int i;
356
357         time(&CC->lastcmd);
358         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
359         if (client_gets(cmdbuf) < 1) {
360                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
361                 CC->kill_me = 1;
362                 return;
363         }
364
365         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
366         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
367
368
369         /* strip off l/t whitespace and CRLF */
370         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
371         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
372         striplt(cmdbuf);
373
374         /* grab the tag */
375         num_parms = imap_parameterize(parms, cmdbuf);
376         for (i=0; i<num_parms; ++i) {
377                 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
378         }
379
380         /* commands which may be executed in any state */
381
382         if ( (!strcasecmp(parms[1], "NOOP"))
383            || (!strcasecmp(parms[1], "CHECK")) ) {
384                 cprintf("%s OK This command successfully did nothing.\r\n",
385                         parms[0]);
386         }
387
388         else if (!strcasecmp(parms[1], "LOGOUT")) {
389                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
390                 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
391                 CC->kill_me = 1;
392                 return;
393         }
394
395         else if (!strcasecmp(parms[1], "LOGIN")) {
396                 imap_login(num_parms, parms);
397         }
398
399         else if (!strcasecmp(parms[1], "CAPABILITY")) {
400                 imap_capability(num_parms, parms);
401         }
402
403         else if (!CC->logged_in) {
404                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
405         }
406
407         /* commands requiring the client to be logged in */
408
409         else if (!strcasecmp(parms[1], "SELECT")) {
410                 imap_select(num_parms, parms);
411         }
412
413         else if (!strcasecmp(parms[1], "EXAMINE")) {
414                 imap_select(num_parms, parms);
415         }
416
417         else if (!strcasecmp(parms[1], "LSUB")) {
418                 imap_lsub(num_parms, parms);
419         }
420
421         else if (!strcasecmp(parms[1], "LIST")) {
422                 imap_list(num_parms, parms);
423         }
424
425         else if (IMAP->selected == 0) {
426                 cprintf("%s BAD no folder selected\r\n", parms[0]);
427         }
428
429         /* commands requiring the SELECT state */
430
431         else if (!strcasecmp(parms[1], "FETCH")) {
432                 imap_fetch(num_parms, parms);
433         }
434
435         else if (!strcasecmp(parms[1], "CLOSE")) {
436                 imap_close(num_parms, parms);
437         }
438
439         /* end of commands */
440
441         else {
442                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
443         }
444
445 }
446
447
448
449 char *Dynamic_Module_Init(void)
450 {
451         SYM_IMAP = CtdlGetDynamicSymbol();
452         CtdlRegisterServiceHook(143,    /* FIXME put in config setup */
453                                 NULL,
454                                 imap_greeting,
455                                 imap_command_loop);
456         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
457         return "$Id$";
458 }