]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* Still trying to fix a socket connect bug
[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  * Main command loop for IMAP sessions.
196  */
197 void imap_command_loop(void) {
198         char cmdbuf[256];
199         char *parms[16];
200         int num_parms;
201         int i;
202
203         time(&CC->lastcmd);
204         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
205         if (client_gets(cmdbuf) < 1) {
206                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
207                 CC->kill_me = 1;
208                 return;
209         }
210
211         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
212         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
213
214
215         /* strip off l/t whitespace and CRLF */
216         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
217         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
218         striplt(cmdbuf);
219
220         /* grab the tag */
221         num_parms = imap_parameterize(parms, cmdbuf);
222         for (i=0; i<num_parms; ++i) {
223                 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
224         }
225
226         /* commands which may be executed in any state */
227
228         if ( (!strcasecmp(parms[1], "NOOP"))
229            || (!strcasecmp(parms[1], "CHECK")) ) {
230                 cprintf("%s OK This command successfully did nothing.\r\n",
231                         parms[0]);
232         }
233
234         else if (!strcasecmp(parms[1], "LOGOUT")) {
235                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
236                 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
237                 CC->kill_me = 1;
238                 return;
239         }
240
241         else if (!strcasecmp(parms[1], "LOGIN")) {
242                 imap_login(num_parms, parms);
243         }
244
245         else if (!strcasecmp(parms[1], "CAPABILITY")) {
246                 imap_capability(num_parms, parms);
247         }
248
249         else if (!CC->logged_in) {
250                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
251         }
252
253         /* commands requiring the client to be logged in */
254
255         else if (!strcasecmp(parms[1], "SELECT")) {
256                 imap_select(num_parms, parms);
257         }
258
259         else if (!strcasecmp(parms[1], "EXAMINE")) {
260                 imap_select(num_parms, parms);
261         }
262
263         else if (IMAP->selected == 0) {
264                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
265         }
266
267         /* commands requiring the SELECT state */
268
269         else if (!strcasecmp(parms[1], "CLOSE")) {
270                 imap_close(num_parms, parms);
271         }
272
273         /* end of commands */
274
275         else {
276                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
277         }
278
279 }
280
281
282
283 char *Dynamic_Module_Init(void)
284 {
285         SYM_IMAP = CtdlGetDynamicSymbol();
286         CtdlRegisterServiceHook(-1,     /* FIXME put in config setup */
287                                 NULL,
288                                 imap_greeting,
289                                 imap_command_loop);
290         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
291         return "$Id$";
292 }