]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* mini fix to imap
[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 whatever Citadel is using for the
175          * default mail room name (usually "Mail>").
176          */
177         if (!strcasecmp(towhere, "INBOX")) {
178                 strcpy(towhere, MAILROOM);
179         }
180
181         /* First try a regular match */
182         c = getroom(&QRscratch, towhere);
183
184         /* Then try a mailbox name match */
185         if (c != 0) {
186                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
187                 c = getroom(&QRscratch, augmented_roomname);
188                 if (c == 0)
189                         strcpy(towhere, augmented_roomname);
190         }
191
192         /* If the room exists, check security/access */
193         if (c == 0) {
194                 /* See if there is an existing user/room relationship */
195                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
196
197                 /* normal clients have to pass through security */
198                 if (ra & UA_KNOWN) {
199                         ok = 1;
200                 }
201         }
202
203         /* Fail here if no such room */
204         if (!ok) {
205                 cprintf("%s NO ... no such room, or access denied\r\n",
206                         parms[0]);
207                 IMAP->selected = 0;
208                 return;
209         }
210
211         /*
212          * usergoto() formally takes us to the desired room, happily returning
213          * the number of messages and number of new messages.
214          */
215         usergoto(QRscratch.QRname, 0, &msgs, &new);
216         IMAP->selected = 1;
217
218         if (!strcasecmp(parms[1], "EXAMINE")) {
219                 IMAP->readonly = 1;
220         }
221         else {
222                 IMAP->readonly = 0;
223         }
224
225         imap_load_msgids();
226
227         /* FIXME ... much more info needs to be supplied here */
228         cprintf("* %d EXISTS\r\n", msgs);
229         cprintf("* %d RECENT\r\n", new);
230         cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
231         cprintf("%s OK [%s] %s completed\r\n",
232                 parms[0],
233                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
234                 parms[1]);
235 }
236
237
238
239 /*
240  * implements the CLOSE command
241  */
242 void imap_close(int num_parms, char *parms[]) {
243         IMAP->selected = 0;
244         IMAP->readonly = 0;
245         imap_free_msgids();
246         cprintf("%s OK CLOSE completed\r\n", parms[0]);
247 }
248
249
250
251
252
253 /*
254  * Back end for imap_lsub()
255  */
256 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
257         char buf[256];
258         
259         imap_mailboxname(buf, sizeof buf, qrbuf);
260         cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
261 }
262
263
264 /*
265  * Implements the LSUB command
266  *
267  * FIXME: Handle wildcards, please.
268  * FIXME: Currently we show all rooms as subscribed folders.  Need to handle
269  *        subscriptions properly.
270  */
271 void imap_lsub(int num_parms, char *parms[]) {
272         ForEachRoom(imap_lsub_listroom, NULL);
273         cprintf("%s OK LSUB completed\r\n", parms[0]);
274 }
275
276
277
278 /*
279  * Back end for imap_list()
280  */
281 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
282         char buf[256];
283         
284         imap_mailboxname(buf, sizeof buf, qrbuf);
285         cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
286 }
287
288
289 /*
290  * Implements the LIST command
291  *
292  * FIXME: Handle wildcards, please.
293  */
294 void imap_list(int num_parms, char *parms[]) {
295         ForEachRoom(imap_list_listroom, NULL);
296         cprintf("%s OK LIST completed\r\n", parms[0]);
297 }
298
299
300 /*
301  * Do the actual work for imap_fetch().  By the time this function is called,
302  * the "lo" and "hi" sequence numbers are already validated, and the data item
303  * names are, um... something.
304  */
305 void imap_do_fetch(int lo, int hi, char *items) {
306         cprintf("* lo=%d hi=%d items=<%s>\r\n", lo, hi, items);
307 }
308
309 /*
310  * Mark Crispin is a fscking idiot.
311  */
312 void imap_fetch(int num_parms, char *parms[]) {
313         int lo = 0;
314         int hi = 0;
315         char lostr[1024], histr[1024], items[1024];
316         int i;
317
318         if (num_parms < 4) {
319                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
320                 return;
321         }
322
323         extract_token(lostr, parms[2], 0, ':');
324         lo = atoi(lostr);
325         extract_token(histr, parms[2], 1, ':');
326         hi = atoi(histr);
327
328         if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
329                 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
330                         parms[0], lo, hi);
331                 return;
332         }
333
334         strcpy(items, "");
335         for (i=3; i<num_parms; ++i) {
336                 strcat(items, parms[i]);
337                 if (i < (num_parms-1)) strcat(items, " ");
338         }
339
340         imap_extract_data_items(items);
341
342         imap_do_fetch(lo, hi, items);
343         cprintf("%s OK FETCH completed\r\n", parms[0]);
344 }
345
346
347
348
349 /* 
350  * Main command loop for IMAP sessions.
351  */
352 void imap_command_loop(void) {
353         char cmdbuf[256];
354         char *parms[16];
355         int num_parms;
356         int i;
357
358         time(&CC->lastcmd);
359         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
360         if (client_gets(cmdbuf) < 1) {
361                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
362                 CC->kill_me = 1;
363                 return;
364         }
365
366         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
367         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
368
369
370         /* strip off l/t whitespace and CRLF */
371         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
372         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
373         striplt(cmdbuf);
374
375         /* grab the tag */
376         num_parms = imap_parameterize(parms, cmdbuf);
377         for (i=0; i<num_parms; ++i) {
378                 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
379         }
380
381         /* commands which may be executed in any state */
382
383         if ( (!strcasecmp(parms[1], "NOOP"))
384            || (!strcasecmp(parms[1], "CHECK")) ) {
385                 cprintf("%s OK This command successfully did nothing.\r\n",
386                         parms[0]);
387         }
388
389         else if (!strcasecmp(parms[1], "LOGOUT")) {
390                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
391                 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
392                 CC->kill_me = 1;
393                 return;
394         }
395
396         else if (!strcasecmp(parms[1], "LOGIN")) {
397                 imap_login(num_parms, parms);
398         }
399
400         else if (!strcasecmp(parms[1], "CAPABILITY")) {
401                 imap_capability(num_parms, parms);
402         }
403
404         else if (!CC->logged_in) {
405                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
406         }
407
408         /* commands requiring the client to be logged in */
409
410         else if (!strcasecmp(parms[1], "SELECT")) {
411                 imap_select(num_parms, parms);
412         }
413
414         else if (!strcasecmp(parms[1], "EXAMINE")) {
415                 imap_select(num_parms, parms);
416         }
417
418         else if (!strcasecmp(parms[1], "LSUB")) {
419                 imap_lsub(num_parms, parms);
420         }
421
422         else if (!strcasecmp(parms[1], "LIST")) {
423                 imap_list(num_parms, parms);
424         }
425
426         else if (IMAP->selected == 0) {
427                 cprintf("%s BAD no folder selected\r\n", parms[0]);
428         }
429
430         /* commands requiring the SELECT state */
431
432         else if (!strcasecmp(parms[1], "FETCH")) {
433                 imap_fetch(num_parms, parms);
434         }
435
436         else if (!strcasecmp(parms[1], "CLOSE")) {
437                 imap_close(num_parms, parms);
438         }
439
440         /* end of commands */
441
442         else {
443                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
444         }
445
446 }
447
448
449
450 char *Dynamic_Module_Init(void)
451 {
452         SYM_IMAP = CtdlGetDynamicSymbol();
453         CtdlRegisterServiceHook(143,    /* FIXME put in config setup */
454                                 NULL,
455                                 imap_greeting,
456                                 imap_command_loop);
457         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
458         return "$Id$";
459 }