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