4c712fa74a20bcdc5854546e9ce048e76c66606f
[citadel.git] / citadel / serv_imap.c
1 /* $Id$ 
2  *
3  * IMAP server for the Citadel/UX system
4  * Copyright (C) 2000 by Art Cancro and others.
5  * This code is released under the terms of the GNU General Public License.
6  *
7  * Current status of standards conformance:
8  *
9  *               ***  ABSOLUTELY NOTHING WORKS  ***
10  * 
11  */
12
13 #include "sysdep.h"
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <sys/wait.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
43
44 long SYM_IMAP;
45
46
47 /*
48  * This cleanup function blows away the temporary memory and files used by
49  * the IMAP server.
50  */
51 void imap_cleanup_function(void) {
52
53         /* Don't do this stuff if this is not a IMAP session! */
54         if (CC->h_command_function != imap_command_loop) return;
55
56         lprintf(9, "Performing IMAP cleanup hook\n");
57
58
59         lprintf(9, "Finished IMAP cleanup hook\n");
60 }
61
62
63
64 /*
65  * Here's where our IMAP session begins its happy day.
66  */
67 void imap_greeting(void) {
68
69         strcpy(CC->cs_clientname, "IMAP session");
70         CC->internal_pgm = 1;
71         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
72
73         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
74                 config.c_fqdn);
75 }
76
77
78 /*
79  * implements the LOGIN command (ordinary username/password login)
80  */
81 void imap_login(char *tag, char *cmd, char *parms) {
82         char username[256];
83         char password[256];
84
85         extract_token(username, parms, 0, ' ');
86         extract_token(password, parms, 1, ' ');
87
88         if (CtdlLoginExistingUser(username) == login_ok) {
89                 if (CtdlTryPassword(password) == pass_ok) {
90                         cprintf("%s OK login successful\r\n", tag);
91                         return;
92                 }
93         }
94
95         cprintf("%s BAD Login incorrect\r\n", tag);
96 }
97
98
99 /*
100  * implements the CAPABILITY command
101  */
102 void imap_capability(char *tag, char *cmd, char *parms) {
103         cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
104         cprintf("%s OK CAPABILITY completed\r\n", tag);
105 }
106
107
108
109
110
111 /*
112  * implements the SELECT command
113  */
114 void imap_select(char *tag, char *cmd, char *parms) {
115         char towhere[256];
116         char augmented_roomname[256];
117         int c = 0;
118         int ok = 0;
119         int ra = 0;
120         struct quickroom QRscratch;
121         int msgs, new;
122
123         extract_token(towhere, parms, 0, ' ');
124
125         /* IMAP uses the reserved name "INBOX" for the user's default incoming
126          * mail folder.  Convert this to Citadel's reserved name "_MAIL_".
127          */
128         if (!strcasecmp(towhere, "INBOX"))
129                 strcpy(towhere, MAILROOM);
130
131         /* First try a regular match */
132         c = getroom(&QRscratch, towhere);
133
134         /* Then try a mailbox name match */
135         if (c != 0) {
136                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
137                 c = getroom(&QRscratch, augmented_roomname);
138                 if (c == 0)
139                         strcpy(towhere, augmented_roomname);
140         }
141
142         /* If the room exists, check security/access */
143         if (c == 0) {
144                 /* See if there is an existing user/room relationship */
145                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
146
147                 /* normal clients have to pass through security */
148                 if (ra & UA_KNOWN)
149                         ok = 1;
150         }
151
152         /* Fail here if no such room */
153         if (!ok) {
154                 cprintf("%s NO ... no such room, or access denied\r\n", tag);
155                 IMAP->selected = 0;
156                 return;
157         }
158
159         /*
160          * usergoto() formally takes us to the desired room, happily returning
161          * the number of messages and number of new messages.
162          */
163         usergoto(QRscratch.QRname, 0, &msgs, &new);
164
165         cprintf("* %d EXISTS\r\n", msgs);
166         cprintf("* %d RECENT\r\n", new);
167         cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
168         cprintf("%s OK [FIXME] SELECT completed\r\n", tag);
169 }
170
171
172 /* 
173  * Main command loop for IMAP sessions.
174  */
175 void imap_command_loop(void) {
176         char cmdbuf[256];
177         char tag[256];
178         char cmd[256];
179
180         time(&CC->lastcmd);
181         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
182         if (client_gets(cmdbuf) < 1) {
183                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
184                 CC->kill_me = 1;
185                 return;
186         }
187
188         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
189         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
190
191
192         /* strip off l/t whitespace and CRLF */
193         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
194         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
195         striplt(cmdbuf);
196
197         /* grab the tag */
198         extract_token(tag, cmdbuf, 0, ' ');
199         extract_token(cmd, cmdbuf, 1, ' ');
200         remove_token(cmdbuf, 0, ' ');
201         remove_token(cmdbuf, 0, ' ');
202         lprintf(9, "tag=<%s> cmd=<%s> parms=<%s>\n", tag, cmd, cmdbuf);
203
204         /* commands which may be executed in any state */
205
206         if (!strcasecmp(cmd, "NOOP")) {
207                 cprintf("%s OK This command successfully did nothing.\r\n",
208                         tag);
209         }
210
211         else if (!strcasecmp(cmd, "LOGOUT")) {
212                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
213                 cprintf("%s OK thank you for using Citadel IMAP\r\n", tag);
214                 CC->kill_me = 1;
215                 return;
216         }
217
218         else if (!strcasecmp(cmd, "LOGIN")) {
219                 imap_login(tag, cmd, cmdbuf);
220         }
221
222         else if (!strcasecmp(cmd, "CAPABILITY")) {
223                 imap_capability(tag, cmd, cmdbuf);
224         }
225
226         else if (!CC->logged_in) {
227                 cprintf("%s BAD Not logged in.\r\n", tag);
228         }
229
230         /*  commands requiring the client to be logged in */
231
232         else if (!strcasecmp(cmd, "SELECT")) {
233                 imap_select(tag, cmd, cmdbuf);
234         }
235
236         /* end of commands */
237
238         else {
239                 cprintf("%s BAD command unrecognized\r\n", tag);
240         }
241
242 }
243
244
245
246 char *Dynamic_Module_Init(void)
247 {
248         SYM_IMAP = CtdlGetDynamicSymbol();
249         CtdlRegisterServiceHook(2243,   /* FIXME put in config setup */
250                                 NULL,
251                                 imap_greeting,
252                                 imap_command_loop);
253         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
254         return "$Id$";
255 }