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