2 * POP3 service for the Citadel system
4 * Copyright (c) 1998-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
20 * Current status of standards conformance:
22 * -> All required POP3 commands described in RFC1939 are implemented.
23 * -> All optional POP3 commands described in RFC1939 are also implemented.
24 * -> The deprecated "LAST" command is included in this implementation, because
25 * there exist mail clients which insist on using it (such as Bynari
26 * TradeMail, and certain versions of Eudora).
27 * -> Capability detection via the method described in RFC2449 is implemented.
39 #include <sys/types.h>
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
46 # include <sys/time.h>
56 #include <libcitadel.h>
59 #include "citserver.h"
65 #include "internet_addressing.h"
66 #include "serv_pop3.h"
71 #include "ctdl_module.h"
73 int POP3DebugEnabled = 0;
75 #define POP3DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (POP3DebugEnabled != 0))
76 #define CCCID CCC->cs_pid
77 #define POP3_syslog(LEVEL, FORMAT, ...) \
78 POP3DBGLOG(LEVEL) syslog(LEVEL, \
79 "POP3CC[%d] " FORMAT, \
82 #define POP3M_syslog(LEVEL, FORMAT) \
83 POP3DBGLOG(LEVEL) syslog(LEVEL, \
84 "POP3CC[%d] " FORMAT, \
88 * This cleanup function blows away the temporary memory and files used by
91 void pop3_cleanup_function(void)
93 struct CitContext *CCC = CC;
95 /* Don't do this stuff if this is not a POP3 session! */
96 if (CCC->h_command_function != pop3_command_loop) return;
98 POP3M_syslog(LOG_DEBUG, "Performing POP3 cleanup hook");
99 if (POP3->msgs != NULL) free(POP3->msgs);
107 * Here's where our POP3 session begins its happy day.
109 void pop3_greeting(void)
111 struct CitContext *CCC = CC;
113 strcpy(CCC->cs_clientname, "POP3 session");
114 CCC->internal_pgm = 1;
115 CCC->session_specific_data = malloc(sizeof(struct citpop3));
116 memset(POP3, 0, sizeof(struct citpop3));
118 cprintf("+OK Citadel POP3 server ready.\r\n");
123 * POP3S is just like POP3, except it goes crypto right away.
125 void pop3s_greeting(void)
127 struct CitContext *CCC = CC;
128 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
130 /* kill session if no crypto */
132 if (!CCC->redirect_ssl) CCC->kill_me = KILLME_NO_CRYPTO;
134 CCC->kill_me = KILLME_NO_CRYPTO;
143 * Specify user name (implements POP3 "USER" command)
145 void pop3_user(char *argbuf)
147 struct CitContext *CCC = CC;
150 if (CCC->logged_in) {
151 cprintf("-ERR You are already logged in.\r\n");
155 strcpy(username, argbuf);
158 /* POP3_syslog(LOG_DEBUG, "Trying <%s>", username); */
159 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
160 cprintf("+OK Password required for %s\r\n", username);
163 cprintf("-ERR No such user.\r\n");
170 * Back end for pop3_grab_mailbox()
172 void pop3_add_message(long msgnum, void *userdata)
174 struct CitContext *CCC = CC;
178 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
179 else POP3->msgs = realloc(POP3->msgs,
180 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
181 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
182 POP3->msgs[POP3->num_msgs-1].deleted = 0;
184 /* We need to know the length of this message when it is printed in
185 * RFC822 format. Perhaps we have cached this length in the message's
186 * metadata record. If so, great; if not, measure it and then cache
189 GetMetaData(&smi, msgnum);
190 if (smi.meta_rfc822_length <= 0L) {
191 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
193 CtdlOutputMsg(msgnum,
200 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
201 FreeStrBuf(&CCC->redirect_buffer); /* TODO: WHEW, all this for just knowing the length???? */
204 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
210 * Open the inbox and read its contents.
211 * (This should be called only once, by pop3_pass(), and returns the number
212 * of messages in the inbox, or -1 for error)
214 int pop3_grab_mailbox(void)
216 struct CitContext *CCC = CC;
220 if (CtdlGetRoom(&CCC->room, MAILROOM) != 0) return(-1);
222 /* Load up the messages */
223 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
224 pop3_add_message, NULL);
226 /* Figure out which are old and which are new */
227 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
228 POP3->lastseen = (-1);
229 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
230 if (is_msg_in_sequence_set(vbuf.v_seen,
231 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
236 return(POP3->num_msgs);
239 void pop3_login(void)
241 struct CitContext *CCC = CC;
244 msgs = pop3_grab_mailbox();
246 cprintf("+OK %s is logged in (%d messages)\r\n",
247 CCC->user.fullname, msgs);
248 POP3_syslog(LOG_DEBUG, "POP3 authenticated %s", CCC->user.fullname);
251 cprintf("-ERR Can't open your mailbox\r\n");
258 * Authorize with password (implements POP3 "PASS" command)
260 void pop3_pass(char *argbuf) {
263 safestrncpy(password, argbuf, sizeof password);
266 /* POP3_syslog(LOG_DEBUG, "Trying <%s>", password); */
267 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
271 cprintf("-ERR That is NOT the password.\r\n");
278 * list available msgs
280 void pop3_list(char *argbuf) {
284 which_one = atoi(argbuf);
286 /* "list one" mode */
288 if (which_one > POP3->num_msgs) {
289 cprintf("-ERR no such message, only %d are here\r\n",
293 else if (POP3->msgs[which_one-1].deleted) {
294 cprintf("-ERR Sorry, you deleted that message.\r\n");
298 cprintf("+OK %d %ld\r\n",
300 (long)POP3->msgs[which_one-1].rfc822_length
306 /* "list all" (scan listing) mode */
308 cprintf("+OK Here's your mail:\r\n");
309 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
310 if (! POP3->msgs[i].deleted) {
311 cprintf("%d %ld\r\n",
313 (long)POP3->msgs[i].rfc822_length);
322 * STAT (tally up the total message count and byte count) command
324 void pop3_stat(char *argbuf) {
326 size_t total_octets = 0;
329 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
330 if (! POP3->msgs[i].deleted) {
332 total_octets += POP3->msgs[i].rfc822_length;
336 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
342 * RETR command (fetch a message)
344 void pop3_retr(char *argbuf) {
347 which_one = atoi(argbuf);
348 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
349 cprintf("-ERR No such message.\r\n");
353 if (POP3->msgs[which_one - 1].deleted) {
354 cprintf("-ERR Sorry, you deleted that message.\r\n");
358 cprintf("+OK Message %d:\r\n", which_one);
359 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
360 MT_RFC822, HEADERS_ALL, 0, 1, NULL,
361 (ESC_DOT|SUPPRESS_ENV_TO),
368 * TOP command (dumb way of fetching a partial message or headers-only)
370 void pop3_top(char *argbuf)
372 struct CitContext *CCC = CC;
374 int lines_requested = 0;
375 int lines_dumped = 0;
382 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
383 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
384 cprintf("-ERR No such message.\r\n");
388 if (POP3->msgs[which_one - 1].deleted) {
389 cprintf("-ERR Sorry, you deleted that message.\r\n");
393 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
395 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
402 msgtext = CCC->redirect_buffer;
403 CCC->redirect_buffer = NULL;
405 cprintf("+OK Message %d:\r\n", which_one);
407 ptr = ChrPtr(msgtext);
408 while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
409 ( (*ptr != 0) && (done == 0))) {
412 if (lines_dumped >= lines_requested) {
416 if ((in_body == 0) || (done == 0)) {
417 client_write(buf, strlen(buf));
422 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
425 if (buf[strlen(buf)-1] != 10) cprintf("\n");
426 FreeStrBuf(&msgtext);
433 * DELE (delete message from mailbox)
435 void pop3_dele(char *argbuf) {
438 which_one = atoi(argbuf);
439 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
440 cprintf("-ERR No such message.\r\n");
444 if (POP3->msgs[which_one - 1].deleted) {
445 cprintf("-ERR You already deleted that message.\r\n");
449 /* Flag the message as deleted. Will expunge during QUIT command. */
450 POP3->msgs[which_one - 1].deleted = 1;
451 cprintf("+OK Message %d deleted.\r\n",
456 /* Perform "UPDATE state" stuff
458 void pop3_update(void)
460 struct CitContext *CCC = CC;
464 long *deletemsgs = NULL;
465 int num_deletemsgs = 0;
467 /* Remove messages marked for deletion */
468 if (POP3->num_msgs > 0) {
469 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
470 for (i=0; i<POP3->num_msgs; ++i) {
471 if (POP3->msgs[i].deleted) {
472 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
475 if (num_deletemsgs > 0) {
476 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
481 /* Set last read pointer */
482 if (POP3->num_msgs > 0) {
483 CtdlLockGetCurrentUser();
485 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
486 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
487 POP3->msgs[POP3->num_msgs-1].msgnum);
488 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
490 CtdlPutCurrentUserLock();
497 * RSET (reset, i.e. undelete any deleted messages) command
499 void pop3_rset(char *argbuf) {
502 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
503 if (POP3->msgs[i].deleted) {
504 POP3->msgs[i].deleted = 0;
507 cprintf("+OK Reset completed.\r\n");
513 * LAST (Determine which message is the last unread message)
515 void pop3_last(char *argbuf) {
516 cprintf("+OK %d\r\n", POP3->lastseen + 1);
521 * CAPA is a command which tells the client which POP3 extensions
524 void pop3_capa(void) {
525 cprintf("+OK Capability list follows\r\n"
529 "IMPLEMENTATION %s\r\n"
539 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
540 * identifiers are simply the Citadel message numbers in the database.
542 void pop3_uidl(char *argbuf) {
546 which_one = atoi(argbuf);
548 /* "list one" mode */
550 if (which_one > POP3->num_msgs) {
551 cprintf("-ERR no such message, only %d are here\r\n",
555 else if (POP3->msgs[which_one-1].deleted) {
556 cprintf("-ERR Sorry, you deleted that message.\r\n");
560 cprintf("+OK %d %ld\r\n",
562 POP3->msgs[which_one-1].msgnum
568 /* "list all" (scan listing) mode */
570 cprintf("+OK Here's your mail:\r\n");
571 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
572 if (! POP3->msgs[i].deleted) {
573 cprintf("%d %ld\r\n",
575 POP3->msgs[i].msgnum);
584 * implements the STLS command (Citadel API version)
588 char ok_response[SIZ];
589 char nosup_response[SIZ];
590 char error_response[SIZ];
593 "+OK Begin TLS negotiation now\r\n");
594 sprintf(nosup_response,
595 "-ERR TLS not supported here\r\n");
596 sprintf(error_response,
597 "-ERR Internal error\r\n");
598 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
608 * Main command loop for POP3 sessions.
610 void pop3_command_loop(void)
612 struct CitContext *CCC = CC;
616 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
617 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
618 POP3M_syslog(LOG_INFO, "POP3 client disconnected: ending session.");
619 CCC->kill_me = KILLME_CLIENT_DISCONNECTED;
622 if (!strncasecmp(cmdbuf, "PASS", 4)) {
623 POP3M_syslog(LOG_DEBUG, "POP3: PASS...");
626 POP3_syslog(LOG_DEBUG, "POP3: %s", cmdbuf);
628 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
630 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
631 cprintf("+OK No operation.\r\n");
634 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
638 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
639 cprintf("+OK Goodbye...\r\n");
641 CCC->kill_me = KILLME_CLIENT_LOGGED_OUT;
645 else if (!strncasecmp(cmdbuf, "USER", 4)) {
646 pop3_user(&cmdbuf[5]);
649 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
650 pop3_pass(&cmdbuf[5]);
654 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
659 else if (!CCC->logged_in) {
660 cprintf("-ERR Not logged in.\r\n");
663 else if (CCC->nologin) {
664 cprintf("-ERR System busy, try later.\r\n");
665 CCC->kill_me = KILLME_NOLOGIN;
668 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
669 pop3_list(&cmdbuf[5]);
672 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
673 pop3_stat(&cmdbuf[5]);
676 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
677 pop3_retr(&cmdbuf[5]);
680 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
681 pop3_dele(&cmdbuf[5]);
684 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
685 pop3_rset(&cmdbuf[5]);
688 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
689 pop3_uidl(&cmdbuf[5]);
692 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
693 pop3_top(&cmdbuf[4]);
696 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
697 pop3_last(&cmdbuf[4]);
701 cprintf("-ERR I'm afraid I can't do that.\r\n");
706 const char *CitadelServicePop3="POP3";
707 const char *CitadelServicePop3S="POP3S";
709 void SetPOP3DebugEnabled(const int n)
711 POP3DebugEnabled = n;
715 CTDL_MODULE_INIT(pop3)
719 CtdlRegisterDebugFlagHook(HKEY("pop3srv"), SetPOP3DebugEnabled, &POP3DebugEnabled);
721 CtdlRegisterServiceHook(config.c_pop3_port,
728 CtdlRegisterServiceHook(config.c_pop3s_port,
733 CitadelServicePop3S);
735 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP, PRIO_STOP + 30);
738 /* return our module name for the log */