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