]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* more IMAP
[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
122         extract_token(towhere, parms, 0, ' ');
123
124         /* IMAP uses the reserved name "INBOX" for the user's default incoming
125          * mail folder.  Convert this to Citadel's reserved name "_MAIL_".
126          */
127         if (!strcasecmp(towhere, "INBOX"))
128                 strcpy(towhere, MAILROOM);
129
130         /* First try a regular match */
131         c = getroom(&QRscratch, towhere);
132
133         /* Then try a mailbox name match */
134         if (c != 0) {
135                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
136                 c = getroom(&QRscratch, augmented_roomname);
137                 if (c == 0)
138                         strcpy(towhere, augmented_roomname);
139         }
140
141         /* If the room exists, check security/access */
142         if (c == 0) {
143                 /* See if there is an existing user/room relationship */
144                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
145
146                 /* normal clients have to pass through security */
147                 if (ra & UA_KNOWN)
148                         ok = 1;
149         }
150
151         /* Fail here if no such room */
152         if (!ok) {
153                 cprintf("%s NO ... no such room, or access denied\r\n", tag);
154                 IMAP->selected = 0;
155                 return;
156         }
157
158         /* FIXME */
159         cprintf("%s OK [FIXME] SELECT completed\r\n", tag);
160 }
161
162
163 /* 
164  * Main command loop for IMAP sessions.
165  */
166 void imap_command_loop(void) {
167         char cmdbuf[256];
168         char tag[256];
169         char cmd[256];
170
171         time(&CC->lastcmd);
172         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
173         if (client_gets(cmdbuf) < 1) {
174                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
175                 CC->kill_me = 1;
176                 return;
177         }
178
179         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
180         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
181
182
183         /* strip off l/t whitespace and CRLF */
184         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
185         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
186         striplt(cmdbuf);
187
188         /* grab the tag */
189         extract_token(tag, cmdbuf, 0, ' ');
190         extract_token(cmd, cmdbuf, 1, ' ');
191         remove_token(cmdbuf, 0, ' ');
192         remove_token(cmdbuf, 0, ' ');
193         lprintf(9, "tag=<%s> cmd=<%s> parms=<%s>\n", tag, cmd, cmdbuf);
194
195         /* commands which may be executed in any state */
196
197         if (!strcasecmp(cmd, "NOOP")) {
198                 cprintf("%s OK This command successfully did nothing.\r\n",
199                         tag);
200         }
201
202         else if (!strcasecmp(cmd, "LOGOUT")) {
203                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
204                 cprintf("%s OK thank you for using Citadel IMAP\r\n", tag);
205                 CC->kill_me = 1;
206                 return;
207         }
208
209         else if (!strcasecmp(cmd, "LOGIN")) {
210                 imap_login(tag, cmd, cmdbuf);
211         }
212
213         else if (!strcasecmp(cmd, "CAPABILITY")) {
214                 imap_capability(tag, cmd, cmdbuf);
215         }
216
217         else if (!CC->logged_in) {
218                 cprintf("%s BAD Not logged in.\r\n", tag);
219         }
220
221         /*  commands requiring the client to be logged in */
222
223         else if (!strcasecmp(cmd, "SELECT")) {
224                 imap_select(tag, cmd, cmdbuf);
225         }
226
227         /* end of commands */
228
229         else {
230                 cprintf("%s BAD command unrecognized\r\n", tag);
231         }
232
233 }
234
235
236
237 char *Dynamic_Module_Init(void)
238 {
239         SYM_IMAP = CtdlGetDynamicSymbol();
240         CtdlRegisterServiceHook(1143,   /* FIXME put in config setup */
241                                 NULL,
242                                 imap_greeting,
243                                 imap_command_loop);
244         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
245         return "$Id$";
246 }