]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* mini fix
[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  * This cleanup function blows away the temporary memory and files used by
51  * the IMAP server.
52  */
53 void imap_cleanup_function(void) {
54
55         /* Don't do this stuff if this is not a IMAP session! */
56         if (CC->h_command_function != imap_command_loop) return;
57
58         lprintf(9, "Performing IMAP cleanup hook\n");
59
60
61         lprintf(9, "Finished IMAP cleanup hook\n");
62 }
63
64
65
66 /*
67  * Here's where our IMAP session begins its happy day.
68  */
69 void imap_greeting(void) {
70
71         strcpy(CC->cs_clientname, "IMAP session");
72         CC->internal_pgm = 1;
73         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
74
75         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
76                 config.c_fqdn);
77 }
78
79
80 /*
81  * implements the LOGIN command (ordinary username/password login)
82  */
83 void imap_login(int num_parms, char *parms[]) {
84         if (CtdlLoginExistingUser(parms[2]) == login_ok) {
85                 if (CtdlTryPassword(parms[3]) == pass_ok) {
86                         cprintf("%s OK login successful\r\n", parms[0]);
87                         return;
88                 }
89         }
90
91         cprintf("%s BAD Login incorrect\r\n", parms[0]);
92 }
93
94
95 /*
96  * implements the CAPABILITY command
97  */
98 void imap_capability(int num_parms, char *parms[]) {
99         cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
100         cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
101 }
102
103
104
105
106
107 /*
108  * implements the SELECT command
109  */
110 void imap_select(int num_parms, char *parms[]) {
111         char towhere[256];
112         char augmented_roomname[256];
113         int c = 0;
114         int ok = 0;
115         int ra = 0;
116         struct quickroom QRscratch;
117         int msgs, new;
118
119         strcpy(towhere, parms[2]);
120
121         /* IMAP uses the reserved name "INBOX" for the user's default incoming
122          * mail folder.  Convert this to Citadel's reserved name "_MAIL_".
123          */
124         if (!strcasecmp(towhere, "INBOX"))
125                 strcpy(towhere, MAILROOM);
126
127         /* First try a regular match */
128         c = getroom(&QRscratch, towhere);
129
130         /* Then try a mailbox name match */
131         if (c != 0) {
132                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
133                 c = getroom(&QRscratch, augmented_roomname);
134                 if (c == 0)
135                         strcpy(towhere, augmented_roomname);
136         }
137
138         /* If the room exists, check security/access */
139         if (c == 0) {
140                 /* See if there is an existing user/room relationship */
141                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
142
143                 /* normal clients have to pass through security */
144                 if (ra & UA_KNOWN)
145                         ok = 1;
146         }
147
148         /* Fail here if no such room */
149         if (!ok) {
150                 cprintf("%s NO ... no such room, or access denied\r\n",
151                         parms[0]);
152                 IMAP->selected = 0;
153                 return;
154         }
155
156         /*
157          * usergoto() formally takes us to the desired room, happily returning
158          * the number of messages and number of new messages.
159          */
160         usergoto(QRscratch.QRname, 0, &msgs, &new);
161         IMAP->selected = 1;
162
163         if (!strcasecmp(parms[1], "EXAMINE")) {
164                 IMAP->readonly = 1;
165         }
166         else {
167                 IMAP->readonly = 0;
168         }
169
170         /* FIXME ... much more info needs to be supplied here */
171         cprintf("* %d EXISTS\r\n", msgs);
172         cprintf("* %d RECENT\r\n", new);
173         cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
174         cprintf("%s OK [%s] %s completed\r\n",
175                 parms[0],
176                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
177                 parms[1]);
178 }
179
180
181
182 /*
183  * implements the CLOSE command
184  */
185 void imap_close(int num_parms, char *parms[]) {
186         IMAP->selected = 0;
187         IMAP->readonly = 0;
188         cprintf("%s OK CLOSE completed\r\n", parms[0]);
189 }
190
191
192
193
194
195 /*
196  * Back end for imap_lsub()
197  */
198 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
199         cprintf("* LSUB () \"|\" %s\r\n", qrbuf->QRname);
200 }
201
202
203 /*
204  * Implements the LSUB command
205  *
206  * FIXME: Handle wildcards, please.  Handle subscriptions, for that matter.
207  */
208 void imap_lsub(int num_parms, char *parms[]) {
209         ForEachRoom(imap_lsub_listroom, NULL);
210         cprintf("%s OK LSUB completed\r\n", parms[0]);
211 }
212
213
214
215 /*
216  * Back end for imap_list()
217  */
218 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
219         cprintf("* LIST () \"|\" %s\r\n", qrbuf->QRname);
220 }
221
222
223 /*
224  * Implements the LIST command
225  *
226  * FIXME: Handle wildcards, please.
227  */
228 void imap_list(int num_parms, char *parms[]) {
229         ForEachRoom(imap_list_listroom, NULL);
230         cprintf("* LIST () \"|\" INBOX\r\n");
231         cprintf("%s OK LIST completed\r\n", parms[0]);
232 }
233
234
235
236 /* 
237  * Main command loop for IMAP sessions.
238  */
239 void imap_command_loop(void) {
240         char cmdbuf[256];
241         char *parms[16];
242         int num_parms;
243         int i;
244
245         time(&CC->lastcmd);
246         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
247         if (client_gets(cmdbuf) < 1) {
248                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
249                 CC->kill_me = 1;
250                 return;
251         }
252
253         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
254         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
255
256
257         /* strip off l/t whitespace and CRLF */
258         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
259         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
260         striplt(cmdbuf);
261
262         /* grab the tag */
263         num_parms = imap_parameterize(parms, cmdbuf);
264         for (i=0; i<num_parms; ++i) {
265                 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
266         }
267
268         /* commands which may be executed in any state */
269
270         if ( (!strcasecmp(parms[1], "NOOP"))
271            || (!strcasecmp(parms[1], "CHECK")) ) {
272                 cprintf("%s OK This command successfully did nothing.\r\n",
273                         parms[0]);
274         }
275
276         else if (!strcasecmp(parms[1], "LOGOUT")) {
277                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
278                 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
279                 CC->kill_me = 1;
280                 return;
281         }
282
283         else if (!strcasecmp(parms[1], "LOGIN")) {
284                 imap_login(num_parms, parms);
285         }
286
287         else if (!strcasecmp(parms[1], "CAPABILITY")) {
288                 imap_capability(num_parms, parms);
289         }
290
291         else if (!CC->logged_in) {
292                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
293         }
294
295         /* commands requiring the client to be logged in */
296
297         else if (!strcasecmp(parms[1], "SELECT")) {
298                 imap_select(num_parms, parms);
299         }
300
301         else if (!strcasecmp(parms[1], "EXAMINE")) {
302                 imap_select(num_parms, parms);
303         }
304
305         else if (!strcasecmp(parms[1], "LSUB")) {
306                 imap_lsub(num_parms, parms);
307         }
308
309         else if (!strcasecmp(parms[1], "LIST")) {
310                 imap_list(num_parms, parms);
311         }
312
313         else if (IMAP->selected == 0) {
314                 cprintf("%s BAD no folder selected\r\n", parms[0]);
315         }
316
317         /* commands requiring the SELECT state */
318
319         else if (!strcasecmp(parms[1], "CLOSE")) {
320                 imap_close(num_parms, parms);
321         }
322
323         /* end of commands */
324
325         else {
326                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
327         }
328
329 }
330
331
332
333 char *Dynamic_Module_Init(void)
334 {
335         SYM_IMAP = CtdlGetDynamicSymbol();
336         CtdlRegisterServiceHook(143,    /* FIXME put in config setup */
337                                 NULL,
338                                 imap_greeting,
339                                 imap_command_loop);
340         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
341         return "$Id$";
342 }