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 struct citpop3 *pop3 = ((struct citpop3 *)CCC->session_specific_data);
99 POP3M_syslog(LOG_DEBUG, "Performing POP3 cleanup hook");
100 if (pop3->msgs != NULL) {
110 * Here's where our POP3 session begins its happy day.
112 void pop3_greeting(void)
114 struct CitContext *CCC = CC;
116 strcpy(CCC->cs_clientname, "POP3 session");
117 CCC->internal_pgm = 1;
118 CCC->session_specific_data = malloc(sizeof(struct citpop3));
119 memset(POP3, 0, sizeof(struct citpop3));
121 cprintf("+OK Citadel POP3 server ready.\r\n");
126 * POP3S is just like POP3, except it goes crypto right away.
128 void pop3s_greeting(void)
130 struct CitContext *CCC = CC;
131 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
133 /* kill session if no crypto */
135 if (!CCC->redirect_ssl) CCC->kill_me = KILLME_NO_CRYPTO;
137 CCC->kill_me = KILLME_NO_CRYPTO;
146 * Specify user name (implements POP3 "USER" command)
148 void pop3_user(char *argbuf)
150 struct CitContext *CCC = CC;
153 if (CCC->logged_in) {
154 cprintf("-ERR You are already logged in.\r\n");
158 strcpy(username, argbuf);
161 /* POP3_syslog(LOG_DEBUG, "Trying <%s>", username); */
162 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
163 cprintf("+OK Password required for %s\r\n", username);
166 cprintf("-ERR No such user.\r\n");
173 * Back end for pop3_grab_mailbox()
175 void pop3_add_message(long msgnum, void *userdata)
177 struct CitContext *CCC = CC;
181 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
182 else POP3->msgs = realloc(POP3->msgs,
183 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
184 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
185 POP3->msgs[POP3->num_msgs-1].deleted = 0;
187 /* We need to know the length of this message when it is printed in
188 * RFC822 format. Perhaps we have cached this length in the message's
189 * metadata record. If so, great; if not, measure it and then cache
192 GetMetaData(&smi, msgnum);
193 if (smi.meta_rfc822_length <= 0L) {
194 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
196 CtdlOutputMsg(msgnum,
203 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
204 FreeStrBuf(&CCC->redirect_buffer); /* TODO: WHEW, all this for just knowing the length???? */
207 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
213 * Open the inbox and read its contents.
214 * (This should be called only once, by pop3_pass(), and returns the number
215 * of messages in the inbox, or -1 for error)
217 int pop3_grab_mailbox(void)
219 struct CitContext *CCC = CC;
223 if (CtdlGetRoom(&CCC->room, MAILROOM) != 0) return(-1);
225 /* Load up the messages */
226 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
227 pop3_add_message, NULL);
229 /* Figure out which are old and which are new */
230 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
231 POP3->lastseen = (-1);
232 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
233 if (is_msg_in_sequence_set(vbuf.v_seen,
234 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
239 return(POP3->num_msgs);
242 void pop3_login(void)
244 struct CitContext *CCC = CC;
247 msgs = pop3_grab_mailbox();
249 cprintf("+OK %s is logged in (%d messages)\r\n",
250 CCC->user.fullname, msgs);
251 POP3_syslog(LOG_DEBUG, "POP3 authenticated %s", CCC->user.fullname);
254 cprintf("-ERR Can't open your mailbox\r\n");
261 * Authorize with password (implements POP3 "PASS" command)
263 void pop3_pass(char *argbuf) {
266 safestrncpy(password, argbuf, sizeof password);
269 /* POP3_syslog(LOG_DEBUG, "Trying <%s>", password); */
270 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
274 cprintf("-ERR That is NOT the password.\r\n");
281 * list available msgs
283 void pop3_list(char *argbuf) {
287 which_one = atoi(argbuf);
289 /* "list one" mode */
291 if (which_one > POP3->num_msgs) {
292 cprintf("-ERR no such message, only %d are here\r\n",
296 else if (POP3->msgs[which_one-1].deleted) {
297 cprintf("-ERR Sorry, you deleted that message.\r\n");
301 cprintf("+OK %d %ld\r\n",
303 (long)POP3->msgs[which_one-1].rfc822_length
309 /* "list all" (scan listing) mode */
311 cprintf("+OK Here's your mail:\r\n");
312 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
313 if (! POP3->msgs[i].deleted) {
314 cprintf("%d %ld\r\n",
316 (long)POP3->msgs[i].rfc822_length);
325 * STAT (tally up the total message count and byte count) command
327 void pop3_stat(char *argbuf) {
329 size_t total_octets = 0;
332 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
333 if (! POP3->msgs[i].deleted) {
335 total_octets += POP3->msgs[i].rfc822_length;
339 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
345 * RETR command (fetch a message)
347 void pop3_retr(char *argbuf) {
350 which_one = atoi(argbuf);
351 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
352 cprintf("-ERR No such message.\r\n");
356 if (POP3->msgs[which_one - 1].deleted) {
357 cprintf("-ERR Sorry, you deleted that message.\r\n");
361 cprintf("+OK Message %d:\r\n", which_one);
362 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
363 MT_RFC822, HEADERS_ALL, 0, 1, NULL,
364 (ESC_DOT|SUPPRESS_ENV_TO),
371 * TOP command (dumb way of fetching a partial message or headers-only)
373 void pop3_top(char *argbuf)
375 struct CitContext *CCC = CC;
377 int lines_requested = 0;
378 int lines_dumped = 0;
385 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
386 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
387 cprintf("-ERR No such message.\r\n");
391 if (POP3->msgs[which_one - 1].deleted) {
392 cprintf("-ERR Sorry, you deleted that message.\r\n");
396 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
398 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
405 msgtext = CCC->redirect_buffer;
406 CCC->redirect_buffer = NULL;
408 cprintf("+OK Message %d:\r\n", which_one);
410 ptr = ChrPtr(msgtext);
411 while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
412 ( (*ptr != 0) && (done == 0))) {
415 if (lines_dumped >= lines_requested) {
419 if ((in_body == 0) || (done == 0)) {
420 client_write(buf, strlen(buf));
425 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
428 if (buf[strlen(buf)-1] != 10) cprintf("\n");
429 FreeStrBuf(&msgtext);
436 * DELE (delete message from mailbox)
438 void pop3_dele(char *argbuf) {
441 which_one = atoi(argbuf);
442 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
443 cprintf("-ERR No such message.\r\n");
447 if (POP3->msgs[which_one - 1].deleted) {
448 cprintf("-ERR You already deleted that message.\r\n");
452 /* Flag the message as deleted. Will expunge during QUIT command. */
453 POP3->msgs[which_one - 1].deleted = 1;
454 cprintf("+OK Message %d deleted.\r\n",
459 /* Perform "UPDATE state" stuff
461 void pop3_update(void)
463 struct CitContext *CCC = CC;
467 long *deletemsgs = NULL;
468 int num_deletemsgs = 0;
470 /* Remove messages marked for deletion */
471 if (POP3->num_msgs > 0) {
472 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
473 for (i=0; i<POP3->num_msgs; ++i) {
474 if (POP3->msgs[i].deleted) {
475 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
478 if (num_deletemsgs > 0) {
479 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
484 /* Set last read pointer */
485 if (POP3->num_msgs > 0) {
486 CtdlLockGetCurrentUser();
488 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
489 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
490 POP3->msgs[POP3->num_msgs-1].msgnum);
491 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
493 CtdlPutCurrentUserLock();
500 * RSET (reset, i.e. undelete any deleted messages) command
502 void pop3_rset(char *argbuf) {
505 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
506 if (POP3->msgs[i].deleted) {
507 POP3->msgs[i].deleted = 0;
510 cprintf("+OK Reset completed.\r\n");
516 * LAST (Determine which message is the last unread message)
518 void pop3_last(char *argbuf) {
519 cprintf("+OK %d\r\n", POP3->lastseen + 1);
524 * CAPA is a command which tells the client which POP3 extensions
527 void pop3_capa(void) {
528 cprintf("+OK Capability list follows\r\n"
532 "IMPLEMENTATION %s\r\n"
542 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
543 * identifiers are simply the Citadel message numbers in the database.
545 void pop3_uidl(char *argbuf) {
549 which_one = atoi(argbuf);
551 /* "list one" mode */
553 if (which_one > POP3->num_msgs) {
554 cprintf("-ERR no such message, only %d are here\r\n",
558 else if (POP3->msgs[which_one-1].deleted) {
559 cprintf("-ERR Sorry, you deleted that message.\r\n");
563 cprintf("+OK %d %ld\r\n",
565 POP3->msgs[which_one-1].msgnum
571 /* "list all" (scan listing) mode */
573 cprintf("+OK Here's your mail:\r\n");
574 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
575 if (! POP3->msgs[i].deleted) {
576 cprintf("%d %ld\r\n",
578 POP3->msgs[i].msgnum);
587 * implements the STLS command (Citadel API version)
591 char ok_response[SIZ];
592 char nosup_response[SIZ];
593 char error_response[SIZ];
596 "+OK Begin TLS negotiation now\r\n");
597 sprintf(nosup_response,
598 "-ERR TLS not supported here\r\n");
599 sprintf(error_response,
600 "-ERR Internal error\r\n");
601 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
611 * Main command loop for POP3 sessions.
613 void pop3_command_loop(void)
615 struct CitContext *CCC = CC;
619 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
620 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
621 POP3M_syslog(LOG_INFO, "POP3 client disconnected: ending session.");
622 CCC->kill_me = KILLME_CLIENT_DISCONNECTED;
625 if (!strncasecmp(cmdbuf, "PASS", 4)) {
626 POP3M_syslog(LOG_DEBUG, "POP3: PASS...");
629 POP3_syslog(LOG_DEBUG, "POP3: %s", cmdbuf);
631 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
633 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
634 cprintf("+OK No operation.\r\n");
637 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
641 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
642 cprintf("+OK Goodbye...\r\n");
644 CCC->kill_me = KILLME_CLIENT_LOGGED_OUT;
648 else if (!strncasecmp(cmdbuf, "USER", 4)) {
649 pop3_user(&cmdbuf[5]);
652 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
653 pop3_pass(&cmdbuf[5]);
657 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
662 else if (!CCC->logged_in) {
663 cprintf("-ERR Not logged in.\r\n");
666 else if (CCC->nologin) {
667 cprintf("-ERR System busy, try later.\r\n");
668 CCC->kill_me = KILLME_NOLOGIN;
671 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
672 pop3_list(&cmdbuf[5]);
675 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
676 pop3_stat(&cmdbuf[5]);
679 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
680 pop3_retr(&cmdbuf[5]);
683 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
684 pop3_dele(&cmdbuf[5]);
687 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
688 pop3_rset(&cmdbuf[5]);
691 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
692 pop3_uidl(&cmdbuf[5]);
695 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
696 pop3_top(&cmdbuf[4]);
699 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
700 pop3_last(&cmdbuf[4]);
704 cprintf("-ERR I'm afraid I can't do that.\r\n");
709 const char *CitadelServicePop3="POP3";
710 const char *CitadelServicePop3S="POP3S";
712 void SetPOP3DebugEnabled(const int n)
714 POP3DebugEnabled = n;
718 CTDL_MODULE_INIT(pop3)
722 CtdlRegisterDebugFlagHook(HKEY("pop3srv"), SetPOP3DebugEnabled, &POP3DebugEnabled);
724 CtdlRegisterServiceHook(config.c_pop3_port,
731 CtdlRegisterServiceHook(config.c_pop3s_port,
736 CitadelServicePop3S);
738 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP, PRIO_STOP + 30);
741 /* return our module name for the log */