2 * POP3 service for the Citadel system
4 * Copyright (c) 1998-2015 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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * Current status of standards conformance:
16 * -> All required POP3 commands described in RFC1939 are implemented.
17 * -> All optional POP3 commands described in RFC1939 are also implemented.
18 * -> The deprecated "LAST" command is included in this implementation, because
19 * there exist mail clients which insist on using it (such as Bynari
20 * TradeMail, and certain versions of Eudora).
21 * -> Capability detection via the method described in RFC2449 is implemented.
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "serv_pop3.h"
65 #include "ctdl_module.h"
67 int POP3DebugEnabled = 0;
69 #define POP3DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (POP3DebugEnabled != 0))
70 #define CCCID CCC->cs_pid
71 #define POP3_syslog(LEVEL, FORMAT, ...) \
72 POP3DBGLOG(LEVEL) syslog(LEVEL, \
73 "POP3CC[%d] " FORMAT, \
76 #define POP3M_syslog(LEVEL, FORMAT) \
77 POP3DBGLOG(LEVEL) syslog(LEVEL, \
78 "POP3CC[%d] " FORMAT, \
82 * This cleanup function blows away the temporary memory and files used by
85 void pop3_cleanup_function(void)
87 struct CitContext *CCC = CC;
89 /* Don't do this stuff if this is not a POP3 session! */
90 if (CCC->h_command_function != pop3_command_loop) return;
92 struct citpop3 *pop3 = ((struct citpop3 *)CCC->session_specific_data);
93 POP3M_syslog(LOG_DEBUG, "Performing POP3 cleanup hook");
94 if (pop3->msgs != NULL) {
104 * Here's where our POP3 session begins its happy day.
106 void pop3_greeting(void)
108 struct CitContext *CCC = CC;
110 strcpy(CCC->cs_clientname, "POP3 session");
111 CCC->internal_pgm = 1;
112 CCC->session_specific_data = malloc(sizeof(struct citpop3));
113 memset(POP3, 0, sizeof(struct citpop3));
115 cprintf("+OK Citadel POP3 server ready.\r\n");
120 * POP3S is just like POP3, except it goes crypto right away.
122 void pop3s_greeting(void)
124 struct CitContext *CCC = CC;
125 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
127 /* kill session if no crypto */
129 if (!CCC->redirect_ssl) CCC->kill_me = KILLME_NO_CRYPTO;
131 CCC->kill_me = KILLME_NO_CRYPTO;
140 * Specify user name (implements POP3 "USER" command)
142 void pop3_user(char *argbuf)
144 struct CitContext *CCC = CC;
147 if (CCC->logged_in) {
148 cprintf("-ERR You are already logged in.\r\n");
152 strcpy(username, argbuf);
155 /* POP3_syslog(LOG_DEBUG, "Trying <%s>", username); */
156 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
157 cprintf("+OK Password required for %s\r\n", username);
160 cprintf("-ERR No such user.\r\n");
167 * Back end for pop3_grab_mailbox()
169 void pop3_add_message(long msgnum, void *userdata)
171 struct CitContext *CCC = CC;
175 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
176 else POP3->msgs = realloc(POP3->msgs,
177 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
178 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
179 POP3->msgs[POP3->num_msgs-1].deleted = 0;
181 /* We need to know the length of this message when it is printed in
182 * RFC822 format. Perhaps we have cached this length in the message's
183 * metadata record. If so, great; if not, measure it and then cache
186 GetMetaData(&smi, msgnum);
187 if (smi.meta_rfc822_length <= 0L) {
188 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
190 CtdlOutputMsg(msgnum,
197 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
198 FreeStrBuf(&CCC->redirect_buffer); /* TODO: WHEW, all this for just knowing the length???? */
201 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
207 * Open the inbox and read its contents.
208 * (This should be called only once, by pop3_pass(), and returns the number
209 * of messages in the inbox, or -1 for error)
211 int pop3_grab_mailbox(void)
213 struct CitContext *CCC = CC;
217 if (CtdlGetRoom(&CCC->room, MAILROOM) != 0) return(-1);
219 /* Load up the messages */
220 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
221 pop3_add_message, NULL);
223 /* Figure out which are old and which are new */
224 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
225 POP3->lastseen = (-1);
226 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
227 if (is_msg_in_sequence_set(vbuf.v_seen,
228 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
233 return(POP3->num_msgs);
236 void pop3_login(void)
238 struct CitContext *CCC = CC;
241 msgs = pop3_grab_mailbox();
243 cprintf("+OK %s is logged in (%d messages)\r\n",
244 CCC->user.fullname, msgs);
245 POP3_syslog(LOG_DEBUG, "POP3 authenticated %s", CCC->user.fullname);
248 cprintf("-ERR Can't open your mailbox\r\n");
255 * Authorize with password (implements POP3 "PASS" command)
257 void pop3_pass(char *argbuf) {
260 safestrncpy(password, argbuf, sizeof password);
263 /* POP3_syslog(LOG_DEBUG, "Trying <%s>", password); */
264 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
268 cprintf("-ERR That is NOT the password.\r\n");
275 * list available msgs
277 void pop3_list(char *argbuf) {
281 which_one = atoi(argbuf);
283 /* "list one" mode */
285 if (which_one > POP3->num_msgs) {
286 cprintf("-ERR no such message, only %d are here\r\n",
290 else if (POP3->msgs[which_one-1].deleted) {
291 cprintf("-ERR Sorry, you deleted that message.\r\n");
295 cprintf("+OK %d %ld\r\n",
297 (long)POP3->msgs[which_one-1].rfc822_length
303 /* "list all" (scan listing) mode */
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",
310 (long)POP3->msgs[i].rfc822_length);
319 * STAT (tally up the total message count and byte count) command
321 void pop3_stat(char *argbuf) {
323 size_t total_octets = 0;
326 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
327 if (! POP3->msgs[i].deleted) {
329 total_octets += POP3->msgs[i].rfc822_length;
333 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
339 * RETR command (fetch a message)
341 void pop3_retr(char *argbuf) {
344 which_one = atoi(argbuf);
345 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
346 cprintf("-ERR No such message.\r\n");
350 if (POP3->msgs[which_one - 1].deleted) {
351 cprintf("-ERR Sorry, you deleted that message.\r\n");
355 cprintf("+OK Message %d:\r\n", which_one);
356 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
357 MT_RFC822, HEADERS_ALL, 0, 1, NULL,
358 (ESC_DOT|SUPPRESS_ENV_TO),
365 * TOP command (dumb way of fetching a partial message or headers-only)
367 void pop3_top(char *argbuf)
369 struct CitContext *CCC = CC;
371 int lines_requested = 0;
372 int lines_dumped = 0;
379 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
380 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
381 cprintf("-ERR No such message.\r\n");
385 if (POP3->msgs[which_one - 1].deleted) {
386 cprintf("-ERR Sorry, you deleted that message.\r\n");
390 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
392 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
399 msgtext = CCC->redirect_buffer;
400 CCC->redirect_buffer = NULL;
402 cprintf("+OK Message %d:\r\n", which_one);
404 ptr = ChrPtr(msgtext);
405 while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
406 ( (*ptr != 0) && (done == 0))) {
409 if (lines_dumped >= lines_requested) {
413 if ((in_body == 0) || (done == 0)) {
414 client_write(buf, strlen(buf));
419 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
422 if (buf[strlen(buf)-1] != 10) cprintf("\n");
423 FreeStrBuf(&msgtext);
430 * DELE (delete message from mailbox)
432 void pop3_dele(char *argbuf) {
435 which_one = atoi(argbuf);
436 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
437 cprintf("-ERR No such message.\r\n");
441 if (POP3->msgs[which_one - 1].deleted) {
442 cprintf("-ERR You already deleted that message.\r\n");
446 /* Flag the message as deleted. Will expunge during QUIT command. */
447 POP3->msgs[which_one - 1].deleted = 1;
448 cprintf("+OK Message %d deleted.\r\n",
453 /* Perform "UPDATE state" stuff
455 void pop3_update(void)
457 struct CitContext *CCC = CC;
461 long *deletemsgs = NULL;
462 int num_deletemsgs = 0;
464 /* Remove messages marked for deletion */
465 if (POP3->num_msgs > 0) {
466 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
467 for (i=0; i<POP3->num_msgs; ++i) {
468 if (POP3->msgs[i].deleted) {
469 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
472 if (num_deletemsgs > 0) {
473 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
478 /* Set last read pointer */
479 if (POP3->num_msgs > 0) {
480 CtdlLockGetCurrentUser();
482 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
483 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
484 POP3->msgs[POP3->num_msgs-1].msgnum);
485 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
487 CtdlPutCurrentUserLock();
494 * RSET (reset, i.e. undelete any deleted messages) command
496 void pop3_rset(char *argbuf) {
499 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
500 if (POP3->msgs[i].deleted) {
501 POP3->msgs[i].deleted = 0;
504 cprintf("+OK Reset completed.\r\n");
510 * LAST (Determine which message is the last unread message)
512 void pop3_last(char *argbuf) {
513 cprintf("+OK %d\r\n", POP3->lastseen + 1);
518 * CAPA is a command which tells the client which POP3 extensions
521 void pop3_capa(void) {
522 cprintf("+OK Capability list follows\r\n"
526 "IMPLEMENTATION %s\r\n"
536 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
537 * identifiers are simply the Citadel message numbers in the database.
539 void pop3_uidl(char *argbuf) {
543 which_one = atoi(argbuf);
545 /* "list one" mode */
547 if (which_one > POP3->num_msgs) {
548 cprintf("-ERR no such message, only %d are here\r\n",
552 else if (POP3->msgs[which_one-1].deleted) {
553 cprintf("-ERR Sorry, you deleted that message.\r\n");
557 cprintf("+OK %d %ld\r\n",
559 POP3->msgs[which_one-1].msgnum
565 /* "list all" (scan listing) mode */
567 cprintf("+OK Here's your mail:\r\n");
568 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
569 if (! POP3->msgs[i].deleted) {
570 cprintf("%d %ld\r\n",
572 POP3->msgs[i].msgnum);
581 * implements the STLS command (Citadel API version)
585 char ok_response[SIZ];
586 char nosup_response[SIZ];
587 char error_response[SIZ];
590 "+OK Begin TLS negotiation now\r\n");
591 sprintf(nosup_response,
592 "-ERR TLS not supported here\r\n");
593 sprintf(error_response,
594 "-ERR Internal error\r\n");
595 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
605 * Main command loop for POP3 sessions.
607 void pop3_command_loop(void)
609 struct CitContext *CCC = CC;
613 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
614 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
615 POP3M_syslog(LOG_INFO, "POP3 client disconnected: ending session.");
616 CCC->kill_me = KILLME_CLIENT_DISCONNECTED;
619 if (!strncasecmp(cmdbuf, "PASS", 4)) {
620 POP3M_syslog(LOG_DEBUG, "POP3: PASS...");
623 POP3_syslog(LOG_DEBUG, "POP3: %s", cmdbuf);
625 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
627 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
628 cprintf("+OK No operation.\r\n");
631 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
635 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
636 cprintf("+OK Goodbye...\r\n");
638 CCC->kill_me = KILLME_CLIENT_LOGGED_OUT;
642 else if (!strncasecmp(cmdbuf, "USER", 4)) {
643 pop3_user(&cmdbuf[5]);
646 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
647 pop3_pass(&cmdbuf[5]);
651 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
656 else if (!CCC->logged_in) {
657 cprintf("-ERR Not logged in.\r\n");
660 else if (CCC->nologin) {
661 cprintf("-ERR System busy, try later.\r\n");
662 CCC->kill_me = KILLME_NOLOGIN;
665 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
666 pop3_list(&cmdbuf[5]);
669 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
670 pop3_stat(&cmdbuf[5]);
673 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
674 pop3_retr(&cmdbuf[5]);
677 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
678 pop3_dele(&cmdbuf[5]);
681 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
682 pop3_rset(&cmdbuf[5]);
685 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
686 pop3_uidl(&cmdbuf[5]);
689 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
690 pop3_top(&cmdbuf[4]);
693 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
694 pop3_last(&cmdbuf[4]);
698 cprintf("-ERR I'm afraid I can't do that.\r\n");
703 const char *CitadelServicePop3="POP3";
704 const char *CitadelServicePop3S="POP3S";
706 void SetPOP3DebugEnabled(const int n)
708 POP3DebugEnabled = n;
712 CTDL_MODULE_INIT(pop3)
716 CtdlRegisterDebugFlagHook(HKEY("pop3srv"), SetPOP3DebugEnabled, &POP3DebugEnabled);
718 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3_port"),
725 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3s_port"),
730 CitadelServicePop3S);
732 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP, PRIO_STOP + 30);
735 /* return our module name for the log */