]> code.citadel.org Git - citadel.git/blob - citadel/serv_pop3.c
* Completed POP3 server. All RFC1939 commands except APOP are implemented.
[citadel.git] / citadel / serv_pop3.c
1 /* $Id$ 
2  *
3  * An implementation of Post Office Protocol version 3 (RFC 1939).
4  *
5  * Copyright (C) 1998-2000 by Art Cancro and others.
6  * This code is released under the terms of the GNU General Public License.
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <string.h>
21 #include <limits.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <time.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
27 #include "support.h"
28 #include "config.h"
29 #include "dynloader.h"
30 #include "room_ops.h"
31 #include "user_ops.h"
32 #include "policy.h"
33 #include "database.h"
34 #include "msgbase.h"
35 #include "tools.h"
36 #include "internet_addressing.h"
37 #include "serv_pop3.h"
38
39
40 long SYM_POP3;
41
42
43 /*
44  * This cleanup function blows away the temporary memory and files used by
45  * the POP3 server.
46  */
47 void pop3_cleanup_function(void) {
48         int i;
49
50         /* Don't do this stuff if this is not a POP3 session! */
51         if (CC->h_command_function != pop3_command_loop) return;
52
53         lprintf(9, "Performing POP3 cleanup hook\n");
54
55         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
56                 fclose(POP3->msgs[i].temp);
57         }
58         if (POP3->msgs != NULL) phree(POP3->msgs);
59
60         lprintf(9, "Finished POP3 cleanup hook\n");
61 }
62
63
64
65 /*
66  * Here's where our POP3 session begins its happy day.
67  */
68 void pop3_greeting(void) {
69
70         strcpy(CC->cs_clientname, "POP3 session");
71         CC->internal_pgm = 1;
72         CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
73         POP3->msgs = NULL;
74         POP3->num_msgs = 0;
75
76         cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
77                 config.c_fqdn);
78 }
79
80
81 /*
82  * Specify user name (implements POP3 "USER" command)
83  */
84 void pop3_user(char *argbuf) {
85         char username[256];
86
87         if (CC->logged_in) {
88                 cprintf("-ERR You are already logged in.\r\n");
89                 return;
90         }
91
92         strcpy(username, argbuf);
93         striplt(username);
94
95         lprintf(9, "Trying <%s>\n", username);
96         if (CtdlLoginExistingUser(username) == login_ok) {
97                 cprintf("+OK Password required for %s\r\n", username);
98         }
99         else {
100                 cprintf("-ERR No such user.\r\n");
101         }
102 }
103
104
105
106 /*
107  * Back end for pop3_grab_mailbox()
108  */
109 void pop3_add_message(long msgnum) {
110         FILE *fp;
111         lprintf(9, "in pop3_add_message()\n");
112
113         ++POP3->num_msgs;
114         if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
115         else POP3->msgs = reallok(POP3->msgs, 
116                 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
117         POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
118         POP3->msgs[POP3->num_msgs-1].deleted = 0;
119         fp = tmpfile();
120         POP3->msgs[POP3->num_msgs-1].temp = fp;
121         CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, fp, 0);
122         POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
123 }
124
125
126
127 /*
128  * Open the inbox and read its contents.
129  * (This should be called only once, by pop3_pass(), and returns the number
130  * of messages in the inbox, or -1 for error)
131  */
132 int pop3_grab_mailbox(void) {
133         if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
134         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, pop3_add_message);
135         return(POP3->num_msgs);
136 }
137
138 /*
139  * Authorize with password (implements POP3 "PASS" command)
140  */
141 void pop3_pass(char *argbuf) {
142         char password[256];
143         int msgs;
144
145         strcpy(password, argbuf);
146         striplt(password);
147
148         lprintf(9, "Trying <%s>\n", password);
149         if (CtdlTryPassword(password) == pass_ok) {
150                 msgs = pop3_grab_mailbox();
151                 if (msgs >= 0) {
152                         cprintf("+OK %s is logged in (%d messages)\r\n",
153                                 CC->usersupp.fullname, msgs);
154                         lprintf(9, "POP3 password login successful\n");
155                 }
156                 else {
157                         cprintf("-ERR Can't open your mailbox\r\n");
158                 }
159         }
160         else {
161                 cprintf("-ERR That is NOT the password!  Go away!\r\n");
162         }
163 }
164
165
166
167 /*
168  * list available msgs
169  */
170 void pop3_list(char *argbuf) {
171         int i;
172         int which_one;
173
174         which_one = atoi(argbuf);
175
176         /* "list one" mode */
177         if (which_one > 0) {
178                 if (which_one > POP3->num_msgs) {
179                         cprintf("-ERR no such message, only %d are here\r\n",
180                                 POP3->num_msgs);
181                         return;
182                 }
183                 else if (POP3->msgs[which_one-1].deleted) {
184                         cprintf("-ERR Sorry, you deleted that message.\r\n");
185                         return;
186                 }
187                 else {
188                         cprintf("+OK %d %d\n",
189                                 which_one,
190                                 POP3->msgs[which_one-1].rfc822_length
191                                 );
192                         return;
193                 }
194         }
195
196         /* "list all" (scan listing) mode */
197         else {
198                 cprintf("+OK Here's your mail:\r\n");
199                 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
200                         if (! POP3->msgs[i].deleted) {
201                                 cprintf("%d %d\r\n",
202                                         i+1,
203                                         POP3->msgs[i].rfc822_length);
204                         }
205                 }
206                 cprintf(".\r\n");
207         }
208 }
209
210
211 /*
212  * STAT (tally up the total message count and byte count) command
213  */
214 void pop3_stat(char *argbuf) {
215         int total_msgs = 0;
216         size_t total_octets = 0;
217         int i;
218         
219         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
220                 if (! POP3->msgs[i].deleted) {
221                         ++total_msgs;
222                         total_octets += POP3->msgs[i].rfc822_length;
223                 }
224         }
225
226         cprintf("+OK %d %d\n", total_msgs, total_octets);
227 }
228
229
230
231 /*
232  * RETR command (fetch a message)
233  */
234 void pop3_retr(char *argbuf) {
235         int which_one;
236         int ch;
237         size_t bytes_remaining;
238
239         which_one = atoi(argbuf);
240         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
241                 cprintf("-ERR No such message.\r\n");
242                 return;
243         }
244
245         if (POP3->msgs[which_one - 1].deleted) {
246                 cprintf("-ERR Sorry, you deleted that message.\r\n");
247                 return;
248         }
249
250         cprintf("+OK Whoop, there it is:\r\n");
251         bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
252         rewind(POP3->msgs[which_one - 1].temp);
253         while (bytes_remaining-- > 0) {
254                 ch = getc(POP3->msgs[which_one - 1].temp);
255                 cprintf("%c", ch);
256         }
257         cprintf(".\r\n");
258 }
259
260
261 /*
262  * TOP command (dumb way of fetching a partial message or headers-only)
263  */
264 void pop3_top(char *argbuf) {
265         int which_one;
266         int lines_requested = 0;
267         int lines_dumped = 0;
268         char buf[1024];
269         char *ptr;
270         int in_body = 0;
271
272         sscanf(argbuf, "%d %d", &which_one, &lines_requested);
273         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
274                 cprintf("-ERR No such message.\r\n");
275                 return;
276         }
277
278         if (POP3->msgs[which_one - 1].deleted) {
279                 cprintf("-ERR Sorry, you deleted that message.\r\n");
280                 return;
281         }
282
283         cprintf("+OK Whoop, there it is:\r\n");
284         rewind(POP3->msgs[which_one - 1].temp);
285         while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
286               ( (ptr!=NULL) && (lines_dumped < lines_requested) ) ) {
287                 client_write(buf, strlen(buf));
288                 if (in_body) ++lines_dumped;
289                 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
290         }
291         if (buf[strlen(buf)-1] != 10) cprintf("\n");
292         cprintf(".\r\n");
293 }
294
295
296 /*
297  * DELE (delete message from mailbox)
298  */
299 void pop3_dele(char *argbuf) {
300         int which_one;
301
302         which_one = atoi(argbuf);
303         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
304                 cprintf("-ERR No such message.\r\n");
305                 return;
306         }
307
308         if (POP3->msgs[which_one - 1].deleted) {
309                 cprintf("-ERR You already deleted that message.\r\n");
310                 return;
311         }
312
313         /* Flag the message as deleted.  Will expunge during QUIT command. */
314         POP3->msgs[which_one - 1].deleted = 1;
315         cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
316                 which_one);
317 }
318
319
320 /* Perform "UPDATE state" stuff (remove messages marked for deletion)
321  */
322 void pop3_update(void) {
323         int i;
324
325         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
326                 if (POP3->msgs[i].deleted) {
327                         CtdlDeleteMessages(MAILROOM,
328                                 POP3->msgs[i].msgnum, NULL);
329                 }
330         }
331 }
332
333
334 /* 
335  * RSET (reset, i.e. undelete any deleted messages) command
336  */
337 void pop3_rset(char *argbuf) {
338         int i;
339
340         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
341                 if (POP3->msgs[i].deleted) {
342                         POP3->msgs[i].deleted = 0;
343                 }
344         }
345         cprintf("+OK all that has come to pass, has now gone away.\r\n");
346 }
347
348
349
350
351 /*
352  * UIDL (Universal IDentifier Listing) is easy.  Our 'unique' message
353  * identifiers are simply the Citadel message numbers in the database.
354  */
355 void pop3_uidl(char *argbuf) {
356         int i;
357         int which_one;
358
359         which_one = atoi(argbuf);
360
361         /* "list one" mode */
362         if (which_one > 0) {
363                 if (which_one > POP3->num_msgs) {
364                         cprintf("-ERR no such message, only %d are here\r\n",
365                                 POP3->num_msgs);
366                         return;
367                 }
368                 else if (POP3->msgs[which_one-1].deleted) {
369                         cprintf("-ERR Sorry, you deleted that message.\r\n");
370                         return;
371                 }
372                 else {
373                         cprintf("+OK %d %ld\n",
374                                 which_one,
375                                 POP3->msgs[which_one-1].msgnum
376                                 );
377                         return;
378                 }
379         }
380
381         /* "list all" (scan listing) mode */
382         else {
383                 cprintf("+OK Here's your mail:\r\n");
384                 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
385                         if (! POP3->msgs[i].deleted) {
386                                 cprintf("%d %ld\r\n",
387                                         i+1,
388                                         POP3->msgs[i].msgnum);
389                         }
390                 }
391                 cprintf(".\r\n");
392         }
393 }
394
395
396
397
398 /* 
399  * Main command loop for POP3 sessions.
400  */
401 void pop3_command_loop(void) {
402         char cmdbuf[256];
403
404         time(&CC->lastcmd);
405         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
406         if (client_gets(cmdbuf) < 1) {
407                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
408                 CC->kill_me = 1;
409                 return;
410         }
411         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
412         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
413
414         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
415                 cprintf("+OK This command successfully did nothing.\r\n");
416         }
417
418         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
419                 cprintf("+OK Goodbye...\r\n");
420                 pop3_update();
421                 CC->kill_me = 1;
422                 return;
423         }
424
425         else if (!strncasecmp(cmdbuf, "USER", 4)) {
426                 pop3_user(&cmdbuf[5]);
427         }
428
429         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
430                 pop3_pass(&cmdbuf[5]);
431         }
432
433         else if (!CC->logged_in) {
434                 cprintf("-ERR Not logged in.\r\n");
435         }
436
437         else if (!strncasecmp(cmdbuf, "LIST", 4)) {
438                 pop3_list(&cmdbuf[5]);
439         }
440
441         else if (!strncasecmp(cmdbuf, "STAT", 4)) {
442                 pop3_stat(&cmdbuf[5]);
443         }
444
445         else if (!strncasecmp(cmdbuf, "RETR", 4)) {
446                 pop3_retr(&cmdbuf[5]);
447         }
448
449         else if (!strncasecmp(cmdbuf, "DELE", 4)) {
450                 pop3_dele(&cmdbuf[5]);
451         }
452
453         else if (!strncasecmp(cmdbuf, "RSET", 4)) {
454                 pop3_rset(&cmdbuf[5]);
455         }
456
457         else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
458                 pop3_uidl(&cmdbuf[5]);
459         }
460
461         else if (!strncasecmp(cmdbuf, "TOP", 3)) {
462                 pop3_top(&cmdbuf[4]);
463         }
464
465         else {
466                 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
467         }
468
469 }
470
471
472
473 char *Dynamic_Module_Init(void)
474 {
475         SYM_POP3 = CtdlGetDynamicSymbol();
476         CtdlRegisterServiceHook(POP3_PORT,
477                                 pop3_greeting,
478                                 pop3_command_loop);
479         CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
480         return "$Id$";
481 }