2 * POP3 service for the Citadel system
4 * Copyright (c) 1998-2017 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.
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "citserver.h"
58 #include "internet_addressing.h"
59 #include "serv_pop3.h"
61 #include "ctdl_module.h"
65 * This cleanup function blows away the temporary memory and files used by
68 void pop3_cleanup_function(void)
70 struct CitContext *CCC = CC;
72 /* Don't do this stuff if this is not a POP3 session! */
73 if (CCC->h_command_function != pop3_command_loop) return;
75 struct citpop3 *pop3 = ((struct citpop3 *)CCC->session_specific_data);
76 syslog(LOG_DEBUG, "pop3: performing cleanup hook");
77 if (pop3->msgs != NULL) {
86 * Here's where our POP3 session begins its happy day.
88 void pop3_greeting(void)
90 struct CitContext *CCC = CC;
92 strcpy(CCC->cs_clientname, "POP3 session");
93 CCC->internal_pgm = 1;
94 CCC->session_specific_data = malloc(sizeof(struct citpop3));
95 memset(POP3, 0, sizeof(struct citpop3));
97 cprintf("+OK Citadel POP3 server ready.\r\n");
102 * POP3S is just like POP3, except it goes crypto right away.
104 void pop3s_greeting(void)
106 struct CitContext *CCC = CC;
107 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
109 /* kill session if no crypto */
111 if (!CCC->redirect_ssl) CCC->kill_me = KILLME_NO_CRYPTO;
113 CCC->kill_me = KILLME_NO_CRYPTO;
121 * Specify user name (implements POP3 "USER" command)
123 void pop3_user(char *argbuf)
125 struct CitContext *CCC = CC;
128 if (CCC->logged_in) {
129 cprintf("-ERR You are already logged in.\r\n");
133 strcpy(username, argbuf);
136 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
137 cprintf("+OK Password required for %s\r\n", username);
140 cprintf("-ERR No such user.\r\n");
146 * Back end for pop3_grab_mailbox()
148 void pop3_add_message(long msgnum, void *userdata)
150 struct CitContext *CCC = CC;
154 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
155 else POP3->msgs = realloc(POP3->msgs,
156 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
157 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
158 POP3->msgs[POP3->num_msgs-1].deleted = 0;
160 /* We need to know the length of this message when it is printed in
161 * RFC822 format. Perhaps we have cached this length in the message's
162 * metadata record. If so, great; if not, measure it and then cache
165 GetMetaData(&smi, msgnum);
166 if (smi.meta_rfc822_length <= 0L) {
167 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
168 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO, NULL, NULL, NULL);
169 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
170 FreeStrBuf(&CCC->redirect_buffer);
173 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
178 * Open the inbox and read its contents.
179 * (This should be called only once, by pop3_pass(), and returns the number
180 * of messages in the inbox, or -1 for error)
182 int pop3_grab_mailbox(void)
184 struct CitContext *CCC = CC;
188 if (CtdlGetRoom(&CCC->room, MAILROOM) != 0) return(-1);
190 /* Load up the messages */
191 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, pop3_add_message, NULL);
193 /* Figure out which are old and which are new */
194 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
195 POP3->lastseen = (-1);
196 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
197 if (is_msg_in_sequence_set(vbuf.v_seen, (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
202 return(POP3->num_msgs);
206 void pop3_login(void)
208 struct CitContext *CCC = CC;
211 msgs = pop3_grab_mailbox();
213 cprintf("+OK %s is logged in (%d messages)\r\n",
214 CCC->user.fullname, msgs);
215 syslog(LOG_DEBUG, "pop3: authenticated %s", CCC->user.fullname);
218 cprintf("-ERR Can't open your mailbox\r\n");
225 * Authorize with password (implements POP3 "PASS" command)
227 void pop3_pass(char *argbuf) {
230 safestrncpy(password, argbuf, sizeof password);
233 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
237 cprintf("-ERR That is NOT the password.\r\n");
243 * list available msgs
245 void pop3_list(char *argbuf) {
249 which_one = atoi(argbuf);
251 /* "list one" mode */
253 if (which_one > POP3->num_msgs) {
254 cprintf("-ERR no such message, only %d are here\r\n", POP3->num_msgs);
257 else if (POP3->msgs[which_one-1].deleted) {
258 cprintf("-ERR Sorry, you deleted that message.\r\n");
262 cprintf("+OK %d %ld\r\n", which_one, (long)POP3->msgs[which_one-1].rfc822_length);
267 /* "list all" (scan listing) mode */
269 cprintf("+OK Here's your mail:\r\n");
270 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
271 if (! POP3->msgs[i].deleted) {
272 cprintf("%d %ld\r\n", i+1, (long)POP3->msgs[i].rfc822_length);
281 * STAT (tally up the total message count and byte count) command
283 void pop3_stat(char *argbuf) {
285 size_t total_octets = 0;
288 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
289 if (! POP3->msgs[i].deleted) {
291 total_octets += POP3->msgs[i].rfc822_length;
295 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
300 * RETR command (fetch a message)
302 void pop3_retr(char *argbuf) {
305 which_one = atoi(argbuf);
306 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
307 cprintf("-ERR No such message.\r\n");
311 if (POP3->msgs[which_one - 1].deleted) {
312 cprintf("-ERR Sorry, you deleted that message.\r\n");
316 cprintf("+OK Message %d:\r\n", which_one);
317 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822,
318 HEADERS_ALL, 0, 1, NULL,
319 (ESC_DOT|SUPPRESS_ENV_TO), NULL, NULL, NULL
326 * TOP command (dumb way of fetching a partial message or headers-only)
328 void pop3_top(char *argbuf)
330 struct CitContext *CCC = CC;
332 int lines_requested = 0;
333 int lines_dumped = 0;
340 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
341 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
342 cprintf("-ERR No such message.\r\n");
346 if (POP3->msgs[which_one - 1].deleted) {
347 cprintf("-ERR Sorry, you deleted that message.\r\n");
351 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
353 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
360 msgtext = CCC->redirect_buffer;
361 CCC->redirect_buffer = NULL;
363 cprintf("+OK Message %d:\r\n", which_one);
365 ptr = ChrPtr(msgtext);
366 while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
367 ( (*ptr != 0) && (done == 0))) {
370 if (lines_dumped >= lines_requested) {
374 if ((in_body == 0) || (done == 0)) {
375 client_write(buf, strlen(buf));
380 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
383 if (buf[strlen(buf)-1] != 10) cprintf("\n");
384 FreeStrBuf(&msgtext);
391 * DELE (delete message from mailbox)
393 void pop3_dele(char *argbuf) {
396 which_one = atoi(argbuf);
397 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
398 cprintf("-ERR No such message.\r\n");
402 if (POP3->msgs[which_one - 1].deleted) {
403 cprintf("-ERR You already deleted that message.\r\n");
407 /* Flag the message as deleted. Will expunge during QUIT command. */
408 POP3->msgs[which_one - 1].deleted = 1;
409 cprintf("+OK Message %d deleted.\r\n",
414 /* Perform "UPDATE state" stuff
416 void pop3_update(void)
418 struct CitContext *CCC = CC;
422 long *deletemsgs = NULL;
423 int num_deletemsgs = 0;
425 /* Remove messages marked for deletion */
426 if (POP3->num_msgs > 0) {
427 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
428 for (i=0; i<POP3->num_msgs; ++i) {
429 if (POP3->msgs[i].deleted) {
430 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
433 if (num_deletemsgs > 0) {
434 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
439 /* Set last read pointer */
440 if (POP3->num_msgs > 0) {
441 CtdlLockGetCurrentUser();
442 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
443 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", POP3->msgs[POP3->num_msgs-1].msgnum);
444 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
445 CtdlPutCurrentUserLock();
452 * RSET (reset, i.e. undelete any deleted messages) command
454 void pop3_rset(char *argbuf) {
457 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
458 if (POP3->msgs[i].deleted) {
459 POP3->msgs[i].deleted = 0;
462 cprintf("+OK Reset completed.\r\n");
467 * LAST (Determine which message is the last unread message)
469 void pop3_last(char *argbuf) {
470 cprintf("+OK %d\r\n", POP3->lastseen + 1);
475 * CAPA is a command which tells the client which POP3 extensions are supported.
477 void pop3_capa(void) {
478 cprintf("+OK Capability list follows\r\n"
482 "IMPLEMENTATION %s\r\n"
491 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
492 * identifiers are simply the Citadel message numbers in the database.
494 void pop3_uidl(char *argbuf) {
498 which_one = atoi(argbuf);
500 /* "list one" mode */
502 if (which_one > POP3->num_msgs) {
503 cprintf("-ERR no such message, only %d are here\r\n", POP3->num_msgs);
506 else if (POP3->msgs[which_one-1].deleted) {
507 cprintf("-ERR Sorry, you deleted that message.\r\n");
511 cprintf("+OK %d %ld\r\n", which_one, POP3->msgs[which_one-1].msgnum);
516 /* "list all" (scan listing) mode */
518 cprintf("+OK Here's your mail:\r\n");
519 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
520 if (! POP3->msgs[i].deleted) {
521 cprintf("%d %ld\r\n", i+1, POP3->msgs[i].msgnum);
530 * implements the STLS command (Citadel API version)
534 char ok_response[SIZ];
535 char nosup_response[SIZ];
536 char error_response[SIZ];
538 sprintf(ok_response, "+OK Begin TLS negotiation now\r\n");
539 sprintf(nosup_response, "-ERR TLS not supported here\r\n");
540 sprintf(error_response, "-ERR Internal error\r\n");
541 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
546 * Main command loop for POP3 sessions.
548 void pop3_command_loop(void)
550 struct CitContext *CCC = CC;
554 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
555 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
556 syslog(LOG_INFO, "pop3: client disconnected; ending session.");
557 CCC->kill_me = KILLME_CLIENT_DISCONNECTED;
560 if (!strncasecmp(cmdbuf, "PASS", 4)) {
561 syslog(LOG_DEBUG, "pop3: PASS...");
564 syslog(LOG_DEBUG, "pop3: %s", cmdbuf);
566 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
568 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
569 cprintf("+OK No operation.\r\n");
572 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
576 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
577 cprintf("+OK Goodbye...\r\n");
579 CCC->kill_me = KILLME_CLIENT_LOGGED_OUT;
583 else if (!strncasecmp(cmdbuf, "USER", 4)) {
584 pop3_user(&cmdbuf[5]);
587 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
588 pop3_pass(&cmdbuf[5]);
592 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
597 else if (!CCC->logged_in) {
598 cprintf("-ERR Not logged in.\r\n");
601 else if (CCC->nologin) {
602 cprintf("-ERR System busy, try later.\r\n");
603 CCC->kill_me = KILLME_NOLOGIN;
606 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
607 pop3_list(&cmdbuf[5]);
610 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
611 pop3_stat(&cmdbuf[5]);
614 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
615 pop3_retr(&cmdbuf[5]);
618 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
619 pop3_dele(&cmdbuf[5]);
622 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
623 pop3_rset(&cmdbuf[5]);
626 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
627 pop3_uidl(&cmdbuf[5]);
630 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
631 pop3_top(&cmdbuf[4]);
634 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
635 pop3_last(&cmdbuf[4]);
639 cprintf("-ERR I'm afraid I can't do that.\r\n");
644 const char *CitadelServicePop3="POP3";
645 const char *CitadelServicePop3S="POP3S";
648 CTDL_MODULE_INIT(pop3)
652 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3_port"),
659 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3s_port"),
664 CitadelServicePop3S);
666 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP, PRIO_STOP + 30);
669 /* return our module name for the log */