4 * POP3 service for the Citadel system
6 * Copyright (c) 1998-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Current status of standards conformance:
24 * -> All required POP3 commands described in RFC1939 are implemented.
25 * -> All optional POP3 commands described in RFC1939 are also implemented.
26 * -> The deprecated "LAST" command is included in this implementation, because
27 * there exist mail clients which insist on using it (such as Bynari
28 * TradeMail, and certain versions of Eudora).
29 * -> Capability detection via the method described in RFC2449 is implemented.
41 #include <sys/types.h>
43 #if TIME_WITH_SYS_TIME
44 # include <sys/time.h>
48 # include <sys/time.h>
58 #include <libcitadel.h>
61 #include "citserver.h"
68 #include "internet_addressing.h"
69 #include "serv_pop3.h"
74 #include "ctdl_module.h"
79 * This cleanup function blows away the temporary memory and files used by
82 void pop3_cleanup_function(void) {
84 /* Don't do this stuff if this is not a POP3 session! */
85 if (CC->h_command_function != pop3_command_loop) return;
87 CtdlLogPrintf(CTDL_DEBUG, "Performing POP3 cleanup hook\n");
88 if (POP3->msgs != NULL) free(POP3->msgs);
96 * Here's where our POP3 session begins its happy day.
98 void pop3_greeting(void) {
99 strcpy(CC->cs_clientname, "POP3 session");
100 CC->internal_pgm = 1;
101 CC->session_specific_data = malloc(sizeof(struct citpop3));
102 memset(POP3, 0, sizeof(struct citpop3));
104 cprintf("+OK Citadel POP3 server %s\r\n",
110 * POP3S is just like POP3, except it goes crypto right away.
112 void pop3s_greeting(void) {
113 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
115 /* kill session if no crypto */
117 if (!CC->redirect_ssl) CC->kill_me = 1;
128 * Specify user name (implements POP3 "USER" command)
130 void pop3_user(char *argbuf) {
134 cprintf("-ERR You are already logged in.\r\n");
138 strcpy(username, argbuf);
141 /* CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", username); */
142 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
143 cprintf("+OK Password required for %s\r\n", username);
146 cprintf("-ERR No such user.\r\n");
153 * Back end for pop3_grab_mailbox()
155 void pop3_add_message(long msgnum, void *userdata) {
159 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
160 else POP3->msgs = realloc(POP3->msgs,
161 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
162 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
163 POP3->msgs[POP3->num_msgs-1].deleted = 0;
165 /* We need to know the length of this message when it is printed in
166 * RFC822 format. Perhaps we have cached this length in the message's
167 * metadata record. If so, great; if not, measure it and then cache
170 GetMetaData(&smi, msgnum);
171 if (smi.meta_rfc822_length <= 0L) {
172 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
173 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
174 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
175 FreeStrBuf(&CC->redirect_buffer); /* TODO: WHEW, all this for just knowing the length???? */
178 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
184 * Open the inbox and read its contents.
185 * (This should be called only once, by pop3_pass(), and returns the number
186 * of messages in the inbox, or -1 for error)
188 int pop3_grab_mailbox(void) {
192 if (CtdlGetRoom(&CC->room, MAILROOM) != 0) return(-1);
194 /* Load up the messages */
195 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
196 pop3_add_message, NULL);
198 /* Figure out which are old and which are new */
199 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
200 POP3->lastseen = (-1);
201 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
202 if (is_msg_in_sequence_set(vbuf.v_seen,
203 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
208 return(POP3->num_msgs);
211 void pop3_login(void)
215 msgs = pop3_grab_mailbox();
217 cprintf("+OK %s is logged in (%d messages)\r\n",
218 CC->user.fullname, msgs);
219 CtdlLogPrintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
222 cprintf("-ERR Can't open your mailbox\r\n");
227 void pop3_apop(char *argbuf)
230 char userdigest[MD5_HEXSTRING_SIZE];
231 char realdigest[MD5_HEXSTRING_SIZE];
236 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
240 if ((sptr = strchr(argbuf, ' ')) == NULL)
242 cprintf("-ERR Invalid APOP line.\r\n");
248 while ((*sptr) && isspace(*sptr))
251 strncpy(username, argbuf, sizeof(username)-1);
252 username[sizeof(username)-1] = '\0';
254 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
255 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
257 if (CtdlLoginExistingUser(NULL, username) != login_ok)
259 cprintf("-ERR No such user.\r\n");
263 if (CtdlGetUser(&CC->user, CC->curr_user))
265 cprintf("-ERR No such user.\r\n");
269 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
270 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
277 cprintf("-ERR That is NOT the password.\r\n");
283 * Authorize with password (implements POP3 "PASS" command)
285 void pop3_pass(char *argbuf) {
288 strcpy(password, argbuf);
291 /* CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", password); */
292 if (CtdlTryPassword(password) == pass_ok) {
296 cprintf("-ERR That is NOT the password.\r\n");
303 * list available msgs
305 void pop3_list(char *argbuf) {
309 which_one = atoi(argbuf);
311 /* "list one" mode */
313 if (which_one > POP3->num_msgs) {
314 cprintf("-ERR no such message, only %d are here\r\n",
318 else if (POP3->msgs[which_one-1].deleted) {
319 cprintf("-ERR Sorry, you deleted that message.\r\n");
323 cprintf("+OK %d %ld\r\n",
325 (long)POP3->msgs[which_one-1].rfc822_length
331 /* "list all" (scan listing) mode */
333 cprintf("+OK Here's your mail:\r\n");
334 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
335 if (! POP3->msgs[i].deleted) {
336 cprintf("%d %ld\r\n",
338 (long)POP3->msgs[i].rfc822_length);
347 * STAT (tally up the total message count and byte count) command
349 void pop3_stat(char *argbuf) {
351 size_t total_octets = 0;
354 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
355 if (! POP3->msgs[i].deleted) {
357 total_octets += POP3->msgs[i].rfc822_length;
361 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
367 * RETR command (fetch a message)
369 void pop3_retr(char *argbuf) {
372 which_one = atoi(argbuf);
373 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
374 cprintf("-ERR No such message.\r\n");
378 if (POP3->msgs[which_one - 1].deleted) {
379 cprintf("-ERR Sorry, you deleted that message.\r\n");
383 cprintf("+OK Message %d:\r\n", which_one);
384 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
385 MT_RFC822, HEADERS_ALL, 0, 1, NULL,
386 (ESC_DOT|SUPPRESS_ENV_TO)
393 * TOP command (dumb way of fetching a partial message or headers-only)
395 void pop3_top(char *argbuf) {
397 int lines_requested = 0;
398 int lines_dumped = 0;
405 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
406 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
407 cprintf("-ERR No such message.\r\n");
411 if (POP3->msgs[which_one - 1].deleted) {
412 cprintf("-ERR Sorry, you deleted that message.\r\n");
416 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
417 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
418 msgtext = CC->redirect_buffer;
419 CC->redirect_buffer = NULL;
421 cprintf("+OK Message %d:\r\n", which_one);
423 ptr = ChrPtr(msgtext);
424 while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
425 ( (*ptr != 0) && (done == 0))) {
428 if (lines_dumped >= lines_requested) {
432 if ((in_body == 0) || (done == 0)) {
433 client_write(buf, strlen(buf));
438 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
441 if (buf[strlen(buf)-1] != 10) cprintf("\n");
442 FreeStrBuf(&msgtext);
449 * DELE (delete message from mailbox)
451 void pop3_dele(char *argbuf) {
454 which_one = atoi(argbuf);
455 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
456 cprintf("-ERR No such message.\r\n");
460 if (POP3->msgs[which_one - 1].deleted) {
461 cprintf("-ERR You already deleted that message.\r\n");
465 /* Flag the message as deleted. Will expunge during QUIT command. */
466 POP3->msgs[which_one - 1].deleted = 1;
467 cprintf("+OK Message %d deleted.\r\n",
472 /* Perform "UPDATE state" stuff
474 void pop3_update(void) {
478 long *deletemsgs = NULL;
479 int num_deletemsgs = 0;
481 /* Remove messages marked for deletion */
482 if (POP3->num_msgs > 0) {
483 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
484 for (i=0; i<POP3->num_msgs; ++i) {
485 if (POP3->msgs[i].deleted) {
486 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
489 if (num_deletemsgs > 0) {
490 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
495 /* Set last read pointer */
496 if (POP3->num_msgs > 0) {
497 CtdlGetUserLock(&CC->user, CC->curr_user);
499 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
500 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
501 POP3->msgs[POP3->num_msgs-1].msgnum);
502 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
504 CtdlPutUserLock(&CC->user);
511 * RSET (reset, i.e. undelete any deleted messages) command
513 void pop3_rset(char *argbuf) {
516 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
517 if (POP3->msgs[i].deleted) {
518 POP3->msgs[i].deleted = 0;
521 cprintf("+OK Reset completed.\r\n");
527 * LAST (Determine which message is the last unread message)
529 void pop3_last(char *argbuf) {
530 cprintf("+OK %d\r\n", POP3->lastseen + 1);
535 * CAPA is a command which tells the client which POP3 extensions
538 void pop3_capa(void) {
539 cprintf("+OK Capability list follows\r\n"
543 "IMPLEMENTATION %s\r\n"
553 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
554 * identifiers are simply the Citadel message numbers in the database.
556 void pop3_uidl(char *argbuf) {
560 which_one = atoi(argbuf);
562 /* "list one" mode */
564 if (which_one > POP3->num_msgs) {
565 cprintf("-ERR no such message, only %d are here\r\n",
569 else if (POP3->msgs[which_one-1].deleted) {
570 cprintf("-ERR Sorry, you deleted that message.\r\n");
574 cprintf("+OK %d %ld\r\n",
576 POP3->msgs[which_one-1].msgnum
582 /* "list all" (scan listing) mode */
584 cprintf("+OK Here's your mail:\r\n");
585 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
586 if (! POP3->msgs[i].deleted) {
587 cprintf("%d %ld\r\n",
589 POP3->msgs[i].msgnum);
598 * implements the STLS command (Citadel API version)
602 char ok_response[SIZ];
603 char nosup_response[SIZ];
604 char error_response[SIZ];
607 "+OK Begin TLS negotiation now\r\n");
608 sprintf(nosup_response,
609 "-ERR TLS not supported here\r\n");
610 sprintf(error_response,
611 "-ERR Internal error\r\n");
612 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
622 * Main command loop for POP3 sessions.
624 void pop3_command_loop(void) {
628 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
629 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
630 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
634 if (!strncasecmp(cmdbuf, "PASS", 4)) {
635 CtdlLogPrintf(CTDL_INFO, "POP3: PASS...\r\n");
638 CtdlLogPrintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
640 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
642 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
643 cprintf("+OK No operation.\r\n");
646 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
650 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
651 cprintf("+OK Goodbye...\r\n");
657 else if (!strncasecmp(cmdbuf, "USER", 4)) {
658 pop3_user(&cmdbuf[5]);
661 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
662 pop3_pass(&cmdbuf[5]);
665 else if (!strncasecmp(cmdbuf, "APOP", 4))
667 pop3_apop(&cmdbuf[5]);
671 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
676 else if (!CC->logged_in) {
677 cprintf("-ERR Not logged in.\r\n");
680 else if (CC->nologin) {
681 cprintf("-ERR System busy, try later.\r\n");
685 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
686 pop3_list(&cmdbuf[5]);
689 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
690 pop3_stat(&cmdbuf[5]);
693 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
694 pop3_retr(&cmdbuf[5]);
697 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
698 pop3_dele(&cmdbuf[5]);
701 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
702 pop3_rset(&cmdbuf[5]);
705 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
706 pop3_uidl(&cmdbuf[5]);
709 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
710 pop3_top(&cmdbuf[4]);
713 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
714 pop3_last(&cmdbuf[4]);
718 cprintf("-ERR I'm afraid I can't do that.\r\n");
723 const char *CitadelServicePop3="POP3";
724 const char *CitadelServicePop3S="POP3S";
727 CTDL_MODULE_INIT(pop3)
731 CtdlRegisterServiceHook(config.c_pop3_port,
738 CtdlRegisterServiceHook(config.c_pop3s_port,
743 CitadelServicePop3S);
745 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
748 /* return our Subversion id for the Log */