8cd9d62a3a3a4483911ae38b809d9105928b4fbf
[citadel.git] / citadel / serv_pop3.c
1 /*
2  * $Id$ 
3  *
4  * POP3 service for the Citadel/UX system
5  * Copyright (C) 1998-2001 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  * -> All required POP3 commands described in RFC1939 are implemented.
11  *
12  * -> All optional POP3 commands described in RFC1939 are also implemented.
13  * 
14  * -> The deprecated "LAST" command is included in this implementation, because
15  *    there exist mail clients which insist on using it (such as Bynari
16  *    TradeMail, and certain versions of Eudora).
17  * 
18  */
19
20 #include "sysdep.h"
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
32 # include <time.h>
33 #else
34 # if HAVE_SYS_TIME_H
35 #  include <sys/time.h>
36 # else
37 #  include <time.h>
38 # endif
39 #endif
40
41 #include <sys/wait.h>
42 #include <string.h>
43 #include <limits.h>
44 #include <ctype.h>
45 #include "citadel.h"
46 #include "server.h"
47 #include "sysdep_decls.h"
48 #include "citserver.h"
49 #include "support.h"
50 #include "config.h"
51 #include "dynloader.h"
52 #include "room_ops.h"
53 #include "user_ops.h"
54 #include "policy.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "tools.h"
58 #include "internet_addressing.h"
59 #include "serv_pop3.h"
60 #include "md5.h"
61
62 long SYM_POP3;
63
64
65 /*
66  * This cleanup function blows away the temporary memory and files used by
67  * the POP3 server.
68  */
69 void pop3_cleanup_function(void) {
70         int i;
71
72         /* Don't do this stuff if this is not a POP3 session! */
73         if (CC->h_command_function != pop3_command_loop) return;
74
75         lprintf(9, "Performing POP3 cleanup hook\n");
76
77         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
78                 if (POP3->msgs[i].temp != NULL) {
79                         fclose(POP3->msgs[i].temp);
80                         POP3->msgs[i].temp = NULL;
81                 }
82         }
83         if (POP3->msgs != NULL) phree(POP3->msgs);
84
85         lprintf(9, "Finished POP3 cleanup hook\n");
86 }
87
88
89
90 /*
91  * Here's where our POP3 session begins its happy day.
92  */
93 void pop3_greeting(void) {
94         strcpy(CC->cs_clientname, "POP3 session");
95         CC->internal_pgm = 1;
96         CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
97         POP3->msgs = NULL;
98         POP3->num_msgs = 0;
99
100         cprintf("+OK Citadel/UX POP3 server %s\r\n",
101                 CC->cs_nonce);
102 }
103
104
105 /*
106  * Specify user name (implements POP3 "USER" command)
107  */
108 void pop3_user(char *argbuf) {
109         char username[SIZ];
110
111         if (CC->logged_in) {
112                 cprintf("-ERR You are already logged in.\r\n");
113                 return;
114         }
115
116         strcpy(username, argbuf);
117         striplt(username);
118
119         lprintf(9, "Trying <%s>\n", username);
120         if (CtdlLoginExistingUser(username) == login_ok) {
121                 cprintf("+OK Password required for %s\r\n", username);
122         }
123         else {
124                 cprintf("-ERR No such user.\r\n");
125         }
126 }
127
128
129
130 /*
131  * Back end for pop3_grab_mailbox()
132  */
133 void pop3_add_message(long msgnum, void *userdata) {
134         FILE *fp;
135         lprintf(9, "in pop3_add_message()\n");
136
137         ++POP3->num_msgs;
138         if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
139         else POP3->msgs = reallok(POP3->msgs, 
140                 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
141         POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
142         POP3->msgs[POP3->num_msgs-1].deleted = 0;
143         fp = tmpfile();
144         POP3->msgs[POP3->num_msgs-1].temp = fp;
145
146         CtdlRedirectOutput(fp, -1);
147         CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
148         CtdlRedirectOutput(NULL, -1);
149
150         POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
151 }
152
153
154
155 /*
156  * Open the inbox and read its contents.
157  * (This should be called only once, by pop3_pass(), and returns the number
158  * of messages in the inbox, or -1 for error)
159  */
160 int pop3_grab_mailbox(void) {
161         struct visit vbuf;
162         int i;
163
164         if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
165
166         /* Load up the messages */
167         CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
168                 pop3_add_message, NULL);
169
170         /* Figure out which are old and which are new */
171         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
172         POP3->lastseen = (-1);
173         if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
174                 if (is_msg_in_mset(vbuf.v_seen,
175                    (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
176                         POP3->lastseen = i;
177                 }
178         }
179
180         return(POP3->num_msgs);
181 }
182
183 void pop3_login(void)
184 {
185         int msgs;
186         
187         msgs = pop3_grab_mailbox();
188         if (msgs >= 0) {
189                 cprintf("+OK %s is logged in (%d messages)\r\n",
190                         CC->usersupp.fullname, msgs);
191                 lprintf(9, "POP3 password login successful\n");
192         }
193         else {
194                 cprintf("-ERR Can't open your mailbox\r\n");
195         }
196         
197 }
198
199 void pop3_apop(char *argbuf)
200 {
201    char username[SIZ];
202    char userdigest[MD5_HEXSTRING_SIZE];
203    char realdigest[MD5_HEXSTRING_SIZE];
204    char *sptr;
205    
206    if (CC->logged_in)
207    {
208         cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
209         return;
210    }
211    
212    if ((sptr = strchr(argbuf, ' ')) == NULL)
213    {
214         cprintf("-ERR Invalid APOP line.\r\n");
215         return;
216    }
217    
218    *sptr++ = '\0';
219    
220    while ((*sptr) && isspace(*sptr))
221       sptr++;
222    
223    strncpy(username, argbuf, sizeof(username)-1);
224    username[sizeof(username)-1] = '\0';
225    
226    memset(userdigest, MD5_HEXSTRING_SIZE, 0);
227    strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
228    
229    if (CtdlLoginExistingUser(username) != login_ok)
230    {
231         cprintf("-ERR No such user.\r\n");
232         return;
233    }
234    
235    if (getuser(&CC->usersupp, CC->curr_user))
236    {
237         cprintf("-ERR No such user.\r\n");
238         return;
239    }
240    
241    make_apop_string(CC->usersupp.password, CC->cs_nonce, realdigest);
242    if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
243    {
244         do_login();
245         pop3_login();
246    }
247    else
248    {
249         cprintf("-ERR That is NOT the password.\r\n");
250    }
251 }
252
253
254 /*
255  * Authorize with password (implements POP3 "PASS" command)
256  */
257 void pop3_pass(char *argbuf) {
258         char password[SIZ];
259
260         strcpy(password, argbuf);
261         striplt(password);
262
263         lprintf(9, "Trying <%s>\n", password);
264         if (CtdlTryPassword(password) == pass_ok) {
265                 pop3_login();
266         }
267         else {
268                 cprintf("-ERR That is NOT the password.\r\n");
269         }
270 }
271
272
273
274 /*
275  * list available msgs
276  */
277 void pop3_list(char *argbuf) {
278         int i;
279         int which_one;
280
281         which_one = atoi(argbuf);
282
283         /* "list one" mode */
284         if (which_one > 0) {
285                 if (which_one > POP3->num_msgs) {
286                         cprintf("-ERR no such message, only %d are here\r\n",
287                                 POP3->num_msgs);
288                         return;
289                 }
290                 else if (POP3->msgs[which_one-1].deleted) {
291                         cprintf("-ERR Sorry, you deleted that message.\r\n");
292                         return;
293                 }
294                 else {
295                         cprintf("+OK %d %ld\r\n",
296                                 which_one,
297                                 (long)POP3->msgs[which_one-1].rfc822_length
298                                 );
299                         return;
300                 }
301         }
302
303         /* "list all" (scan listing) mode */
304         else {
305                 cprintf("+OK Here's your mail:\r\n");
306                 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
307                         if (! POP3->msgs[i].deleted) {
308                                 cprintf("%d %ld\r\n",
309                                         i+1,
310                                         (long)POP3->msgs[i].rfc822_length);
311                         }
312                 }
313                 cprintf(".\r\n");
314         }
315 }
316
317
318 /*
319  * STAT (tally up the total message count and byte count) command
320  */
321 void pop3_stat(char *argbuf) {
322         int total_msgs = 0;
323         size_t total_octets = 0;
324         int i;
325         
326         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
327                 if (! POP3->msgs[i].deleted) {
328                         ++total_msgs;
329                         total_octets += POP3->msgs[i].rfc822_length;
330                 }
331         }
332
333         cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
334 }
335
336
337
338 /*
339  * RETR command (fetch a message)
340  */
341 void pop3_retr(char *argbuf) {
342         int which_one;
343         int ch = 0;
344         size_t bytes_remaining;
345
346         which_one = atoi(argbuf);
347         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
348                 cprintf("-ERR No such message.\r\n");
349                 return;
350         }
351
352         if (POP3->msgs[which_one - 1].deleted) {
353                 cprintf("-ERR Sorry, you deleted that message.\r\n");
354                 return;
355         }
356
357         cprintf("+OK Message %d:\r\n", which_one);
358         bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
359         rewind(POP3->msgs[which_one - 1].temp);
360         while (bytes_remaining-- > 0) {
361                 ch = getc(POP3->msgs[which_one - 1].temp);
362                 cprintf("%c", ch);
363         }
364         if (ch != 10) {
365                 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
366         }
367         cprintf(".\r\n");
368 }
369
370
371 /*
372  * TOP command (dumb way of fetching a partial message or headers-only)
373  */
374 void pop3_top(char *argbuf) {
375         int which_one;
376         int lines_requested = 0;
377         int lines_dumped = 0;
378         char buf[1024];
379         char *ptr;
380         int in_body = 0;
381         int done = 0;
382
383         sscanf(argbuf, "%d %d", &which_one, &lines_requested);
384         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
385                 cprintf("-ERR No such message.\r\n");
386                 return;
387         }
388
389         if (POP3->msgs[which_one - 1].deleted) {
390                 cprintf("-ERR Sorry, you deleted that message.\r\n");
391                 return;
392         }
393
394         cprintf("+OK Message %d:\r\n", which_one);
395         rewind(POP3->msgs[which_one - 1].temp);
396         while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
397               ( (ptr!=NULL) && (done == 0))) {
398                 if (in_body == 1)
399                         if (lines_dumped >= lines_requested) done = 1;
400                 if ((in_body == 0) || (done == 0))
401                         client_write(buf, strlen(buf));
402                 if (in_body) ++lines_dumped;
403                 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
404         }
405         if (buf[strlen(buf)-1] != 10) cprintf("\n");
406         cprintf(".\r\n");
407 }
408
409
410 /*
411  * DELE (delete message from mailbox)
412  */
413 void pop3_dele(char *argbuf) {
414         int which_one;
415
416         which_one = atoi(argbuf);
417         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
418                 cprintf("-ERR No such message.\r\n");
419                 return;
420         }
421
422         if (POP3->msgs[which_one - 1].deleted) {
423                 cprintf("-ERR You already deleted that message.\r\n");
424                 return;
425         }
426
427         /* Flag the message as deleted.  Will expunge during QUIT command. */
428         POP3->msgs[which_one - 1].deleted = 1;
429         cprintf("+OK Message %d deleted.\r\n",
430                 which_one);
431 }
432
433
434 /* Perform "UPDATE state" stuff
435  */
436 void pop3_update(void) {
437         int i;
438         struct visit vbuf;
439
440         /* Remove messages marked for deletion */
441         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
442                 if (POP3->msgs[i].deleted) {
443                         CtdlDeleteMessages(MAILROOM,
444                                 POP3->msgs[i].msgnum, "");
445                 }
446         }
447
448         /* Set last read pointer */
449         if (POP3->num_msgs > 0) {
450                 lgetuser(&CC->usersupp, CC->curr_user);
451
452                 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
453                 sprintf(vbuf.v_seen, "*:%ld",
454                         POP3->msgs[POP3->num_msgs-1].msgnum);
455                 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
456
457                 lputuser(&CC->usersupp);
458         }
459
460 }
461
462
463 /* 
464  * RSET (reset, i.e. undelete any deleted messages) command
465  */
466 void pop3_rset(char *argbuf) {
467         int i;
468
469         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
470                 if (POP3->msgs[i].deleted) {
471                         POP3->msgs[i].deleted = 0;
472                 }
473         }
474         cprintf("+OK Reset completed.\r\n");
475 }
476
477
478
479 /* 
480  * LAST (Determine which message is the last unread message)
481  */
482 void pop3_last(char *argbuf) {
483         cprintf("+OK %d\r\n", POP3->lastseen + 1);
484 }
485
486
487
488 /*
489  * UIDL (Universal IDentifier Listing) is easy.  Our 'unique' message
490  * identifiers are simply the Citadel message numbers in the database.
491  */
492 void pop3_uidl(char *argbuf) {
493         int i;
494         int which_one;
495
496         which_one = atoi(argbuf);
497
498         /* "list one" mode */
499         if (which_one > 0) {
500                 if (which_one > POP3->num_msgs) {
501                         cprintf("-ERR no such message, only %d are here\r\n",
502                                 POP3->num_msgs);
503                         return;
504                 }
505                 else if (POP3->msgs[which_one-1].deleted) {
506                         cprintf("-ERR Sorry, you deleted that message.\r\n");
507                         return;
508                 }
509                 else {
510                         cprintf("+OK %d %ld\r\n",
511                                 which_one,
512                                 POP3->msgs[which_one-1].msgnum
513                                 );
514                         return;
515                 }
516         }
517
518         /* "list all" (scan listing) mode */
519         else {
520                 cprintf("+OK Here's your mail:\r\n");
521                 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
522                         if (! POP3->msgs[i].deleted) {
523                                 cprintf("%d %ld\r\n",
524                                         i+1,
525                                         POP3->msgs[i].msgnum);
526                         }
527                 }
528                 cprintf(".\r\n");
529         }
530 }
531
532
533
534
535 /* 
536  * Main command loop for POP3 sessions.
537  */
538 void pop3_command_loop(void) {
539         char cmdbuf[SIZ];
540
541         time(&CC->lastcmd);
542         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
543         if (client_gets(cmdbuf) < 1) {
544                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
545                 CC->kill_me = 1;
546                 return;
547         }
548         lprintf(5, "POP3: %s\r\n", cmdbuf);
549         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
550
551         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
552                 cprintf("+OK No operation.\r\n");
553         }
554
555         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
556                 cprintf("+OK Goodbye...\r\n");
557                 pop3_update();
558                 CC->kill_me = 1;
559                 return;
560         }
561
562         else if (!strncasecmp(cmdbuf, "USER", 4)) {
563                 pop3_user(&cmdbuf[5]);
564         }
565
566         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
567                 pop3_pass(&cmdbuf[5]);
568         }
569
570         else if (!strncasecmp(cmdbuf, "APOP", 4))
571         {
572                 pop3_apop(&cmdbuf[5]);
573         }
574
575         else if (!CC->logged_in) {
576                 cprintf("-ERR Not logged in.\r\n");
577         }
578
579         else if (!strncasecmp(cmdbuf, "LIST", 4)) {
580                 pop3_list(&cmdbuf[5]);
581         }
582
583         else if (!strncasecmp(cmdbuf, "STAT", 4)) {
584                 pop3_stat(&cmdbuf[5]);
585         }
586
587         else if (!strncasecmp(cmdbuf, "RETR", 4)) {
588                 pop3_retr(&cmdbuf[5]);
589         }
590
591         else if (!strncasecmp(cmdbuf, "DELE", 4)) {
592                 pop3_dele(&cmdbuf[5]);
593         }
594
595         else if (!strncasecmp(cmdbuf, "RSET", 4)) {
596                 pop3_rset(&cmdbuf[5]);
597         }
598
599         else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
600                 pop3_uidl(&cmdbuf[5]);
601         }
602
603         else if (!strncasecmp(cmdbuf, "TOP", 3)) {
604                 pop3_top(&cmdbuf[4]);
605         }
606
607         else if (!strncasecmp(cmdbuf, "LAST", 4)) {
608                 pop3_last(&cmdbuf[4]);
609         }
610
611         else {
612                 cprintf("-ERR I'm afraid I can't do that.\r\n");
613         }
614
615 }
616
617
618
619 char *Dynamic_Module_Init(void)
620 {
621         SYM_POP3 = CtdlGetDynamicSymbol();
622         CtdlRegisterServiceHook(config.c_pop3_port,
623                                 NULL,
624                                 pop3_greeting,
625                                 pop3_command_loop);
626         CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
627         return "$Id$";
628 }