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