2 * POP3 service for the Citadel system
4 * Copyright (c) 1998-2019 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>
38 #include <libcitadel.h>
41 #include "citserver.h"
47 #include "internet_addressing.h"
48 #include "serv_pop3.h"
49 #include "ctdl_module.h"
53 * This cleanup function blows away the temporary memory and files used by
56 void pop3_cleanup_function(void)
58 struct CitContext *CCC = CC;
60 /* Don't do this stuff if this is not a POP3 session! */
61 if (CCC->h_command_function != pop3_command_loop) return;
63 struct citpop3 *pop3 = ((struct citpop3 *)CCC->session_specific_data);
64 syslog(LOG_DEBUG, "pop3: performing cleanup hook");
65 if (pop3->msgs != NULL) {
74 * Here's where our POP3 session begins its happy day.
76 void pop3_greeting(void)
78 struct CitContext *CCC = CC;
80 strcpy(CCC->cs_clientname, "POP3 session");
81 CCC->internal_pgm = 1;
82 CCC->session_specific_data = malloc(sizeof(struct citpop3));
83 memset(POP3, 0, sizeof(struct citpop3));
85 cprintf("+OK Citadel POP3 server ready.\r\n");
90 * POP3S is just like POP3, except it goes crypto right away.
92 void pop3s_greeting(void)
94 struct CitContext *CCC = CC;
95 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
97 /* kill session if no crypto */
99 if (!CCC->redirect_ssl) CCC->kill_me = KILLME_NO_CRYPTO;
101 CCC->kill_me = KILLME_NO_CRYPTO;
109 * Specify user name (implements POP3 "USER" command)
111 void pop3_user(char *argbuf)
113 struct CitContext *CCC = CC;
116 if (CCC->logged_in) {
117 cprintf("-ERR You are already logged in.\r\n");
121 strcpy(username, argbuf);
124 if (CtdlLoginExistingUser(username) == login_ok) {
125 cprintf("+OK Password required for %s\r\n", username);
128 cprintf("-ERR No such user.\r\n");
134 * Back end for pop3_grab_mailbox()
136 void pop3_add_message(long msgnum, void *userdata)
138 struct CitContext *CCC = CC;
142 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
143 else POP3->msgs = realloc(POP3->msgs,
144 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
145 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
146 POP3->msgs[POP3->num_msgs-1].deleted = 0;
148 /* We need to know the length of this message when it is printed in
149 * RFC822 format. Perhaps we have cached this length in the message's
150 * metadata record. If so, great; if not, measure it and then cache
153 GetMetaData(&smi, msgnum);
154 if (smi.meta_rfc822_length <= 0L) {
155 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
156 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO, NULL, NULL, NULL);
157 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
158 FreeStrBuf(&CCC->redirect_buffer);
161 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
166 * Open the inbox and read its contents.
167 * (This should be called only once, by pop3_pass(), and returns the number
168 * of messages in the inbox, or -1 for error)
170 int pop3_grab_mailbox(void)
172 struct CitContext *CCC = CC;
176 if (CtdlGetRoom(&CCC->room, MAILROOM) != 0) return(-1);
178 /* Load up the messages */
179 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, pop3_add_message, NULL);
181 /* Figure out which are old and which are new */
182 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
183 POP3->lastseen = (-1);
184 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
185 if (is_msg_in_sequence_set(vbuf.v_seen, (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
190 return(POP3->num_msgs);
194 void pop3_login(void)
196 struct CitContext *CCC = CC;
199 msgs = pop3_grab_mailbox();
201 cprintf("+OK %s is logged in (%d messages)\r\n",
202 CCC->user.fullname, msgs);
203 syslog(LOG_DEBUG, "pop3: authenticated %s", CCC->user.fullname);
206 cprintf("-ERR Can't open your mailbox\r\n");
213 * Authorize with password (implements POP3 "PASS" command)
215 void pop3_pass(char *argbuf) {
218 safestrncpy(password, argbuf, sizeof password);
221 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
225 cprintf("-ERR That is NOT the password.\r\n");
231 * list available msgs
233 void pop3_list(char *argbuf) {
237 which_one = atoi(argbuf);
239 /* "list one" mode */
241 if (which_one > POP3->num_msgs) {
242 cprintf("-ERR no such message, only %d are here\r\n", POP3->num_msgs);
245 else if (POP3->msgs[which_one-1].deleted) {
246 cprintf("-ERR Sorry, you deleted that message.\r\n");
250 cprintf("+OK %d %ld\r\n", which_one, (long)POP3->msgs[which_one-1].rfc822_length);
255 /* "list all" (scan listing) mode */
257 cprintf("+OK Here's your mail:\r\n");
258 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
259 if (! POP3->msgs[i].deleted) {
260 cprintf("%d %ld\r\n", i+1, (long)POP3->msgs[i].rfc822_length);
269 * STAT (tally up the total message count and byte count) command
271 void pop3_stat(char *argbuf) {
273 size_t total_octets = 0;
276 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
277 if (! POP3->msgs[i].deleted) {
279 total_octets += POP3->msgs[i].rfc822_length;
283 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
288 * RETR command (fetch a message)
290 void pop3_retr(char *argbuf) {
293 which_one = atoi(argbuf);
294 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
295 cprintf("-ERR No such message.\r\n");
299 if (POP3->msgs[which_one - 1].deleted) {
300 cprintf("-ERR Sorry, you deleted that message.\r\n");
304 cprintf("+OK Message %d:\r\n", which_one);
305 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822,
306 HEADERS_ALL, 0, 1, NULL,
307 (ESC_DOT|SUPPRESS_ENV_TO), NULL, NULL, NULL
314 * TOP command (dumb way of fetching a partial message or headers-only)
316 void pop3_top(char *argbuf)
318 struct CitContext *CCC = CC;
320 int lines_requested = 0;
321 int lines_dumped = 0;
328 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
329 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
330 cprintf("-ERR No such message.\r\n");
334 if (POP3->msgs[which_one - 1].deleted) {
335 cprintf("-ERR Sorry, you deleted that message.\r\n");
339 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
341 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
348 msgtext = CCC->redirect_buffer;
349 CCC->redirect_buffer = NULL;
351 cprintf("+OK Message %d:\r\n", which_one);
353 ptr = ChrPtr(msgtext);
354 while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
355 ( (*ptr != 0) && (done == 0))) {
358 if (lines_dumped >= lines_requested) {
362 if ((in_body == 0) || (done == 0)) {
363 client_write(buf, strlen(buf));
368 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
371 if (buf[strlen(buf)-1] != 10) cprintf("\n");
372 FreeStrBuf(&msgtext);
379 * DELE (delete message from mailbox)
381 void pop3_dele(char *argbuf) {
384 which_one = atoi(argbuf);
385 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
386 cprintf("-ERR No such message.\r\n");
390 if (POP3->msgs[which_one - 1].deleted) {
391 cprintf("-ERR You already deleted that message.\r\n");
395 /* Flag the message as deleted. Will expunge during QUIT command. */
396 POP3->msgs[which_one - 1].deleted = 1;
397 cprintf("+OK Message %d deleted.\r\n",
402 /* Perform "UPDATE state" stuff
404 void pop3_update(void)
406 struct CitContext *CCC = CC;
410 long *deletemsgs = NULL;
411 int num_deletemsgs = 0;
413 /* Remove messages marked for deletion */
414 if (POP3->num_msgs > 0) {
415 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
416 for (i=0; i<POP3->num_msgs; ++i) {
417 if (POP3->msgs[i].deleted) {
418 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
421 if (num_deletemsgs > 0) {
422 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
427 /* Set last read pointer */
428 if (POP3->num_msgs > 0) {
429 CtdlLockGetCurrentUser();
430 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
431 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", POP3->msgs[POP3->num_msgs-1].msgnum);
432 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
433 CtdlPutCurrentUserLock();
440 * RSET (reset, i.e. undelete any deleted messages) command
442 void pop3_rset(char *argbuf) {
445 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
446 if (POP3->msgs[i].deleted) {
447 POP3->msgs[i].deleted = 0;
450 cprintf("+OK Reset completed.\r\n");
455 * LAST (Determine which message is the last unread message)
457 void pop3_last(char *argbuf) {
458 cprintf("+OK %d\r\n", POP3->lastseen + 1);
463 * CAPA is a command which tells the client which POP3 extensions are supported.
465 void pop3_capa(void) {
466 cprintf("+OK Capability list follows\r\n"
470 "IMPLEMENTATION %s\r\n"
479 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
480 * identifiers are simply the Citadel message numbers in the database.
482 void pop3_uidl(char *argbuf) {
486 which_one = atoi(argbuf);
488 /* "list one" mode */
490 if (which_one > POP3->num_msgs) {
491 cprintf("-ERR no such message, only %d are here\r\n", POP3->num_msgs);
494 else if (POP3->msgs[which_one-1].deleted) {
495 cprintf("-ERR Sorry, you deleted that message.\r\n");
499 cprintf("+OK %d %ld\r\n", which_one, POP3->msgs[which_one-1].msgnum);
504 /* "list all" (scan listing) mode */
506 cprintf("+OK Here's your mail:\r\n");
507 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
508 if (! POP3->msgs[i].deleted) {
509 cprintf("%d %ld\r\n", i+1, POP3->msgs[i].msgnum);
518 * implements the STLS command (Citadel API version)
522 char ok_response[SIZ];
523 char nosup_response[SIZ];
524 char error_response[SIZ];
526 sprintf(ok_response, "+OK Begin TLS negotiation now\r\n");
527 sprintf(nosup_response, "-ERR TLS not supported here\r\n");
528 sprintf(error_response, "-ERR Internal error\r\n");
529 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
534 * Main command loop for POP3 sessions.
536 void pop3_command_loop(void)
538 struct CitContext *CCC = CC;
542 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
543 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
544 syslog(LOG_INFO, "pop3: client disconnected; ending session.");
545 CCC->kill_me = KILLME_CLIENT_DISCONNECTED;
548 if (!strncasecmp(cmdbuf, "PASS", 4)) {
549 syslog(LOG_DEBUG, "pop3: PASS...");
552 syslog(LOG_DEBUG, "pop3: %s", cmdbuf);
554 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
556 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
557 cprintf("+OK No operation.\r\n");
560 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
564 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
565 cprintf("+OK Goodbye...\r\n");
567 CCC->kill_me = KILLME_CLIENT_LOGGED_OUT;
571 else if (!strncasecmp(cmdbuf, "USER", 4)) {
572 pop3_user(&cmdbuf[5]);
575 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
576 pop3_pass(&cmdbuf[5]);
580 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
585 else if (!CCC->logged_in) {
586 cprintf("-ERR Not logged in.\r\n");
589 else if (CCC->nologin) {
590 cprintf("-ERR System busy, try later.\r\n");
591 CCC->kill_me = KILLME_NOLOGIN;
594 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
595 pop3_list(&cmdbuf[5]);
598 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
599 pop3_stat(&cmdbuf[5]);
602 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
603 pop3_retr(&cmdbuf[5]);
606 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
607 pop3_dele(&cmdbuf[5]);
610 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
611 pop3_rset(&cmdbuf[5]);
614 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
615 pop3_uidl(&cmdbuf[5]);
618 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
619 pop3_top(&cmdbuf[4]);
622 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
623 pop3_last(&cmdbuf[4]);
627 cprintf("-ERR I'm afraid I can't do that.\r\n");
632 const char *CitadelServicePop3="POP3";
633 const char *CitadelServicePop3S="POP3S";
636 CTDL_MODULE_INIT(pop3)
640 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3_port"),
647 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3s_port"),
652 CitadelServicePop3S);
654 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP, PRIO_STOP + 30);
657 /* return our module name for the log */