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>
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"
60 #include "ctdl_module.h"
64 * This cleanup function blows away the temporary memory and files used by
67 void pop3_cleanup_function(void)
69 struct CitContext *CCC = CC;
71 /* Don't do this stuff if this is not a POP3 session! */
72 if (CCC->h_command_function != pop3_command_loop) return;
74 struct citpop3 *pop3 = ((struct citpop3 *)CCC->session_specific_data);
75 syslog(LOG_DEBUG, "pop3: performing cleanup hook");
76 if (pop3->msgs != NULL) {
85 * Here's where our POP3 session begins its happy day.
87 void pop3_greeting(void)
89 struct CitContext *CCC = CC;
91 strcpy(CCC->cs_clientname, "POP3 session");
92 CCC->internal_pgm = 1;
93 CCC->session_specific_data = malloc(sizeof(struct citpop3));
94 memset(POP3, 0, sizeof(struct citpop3));
96 cprintf("+OK Citadel POP3 server ready.\r\n");
101 * POP3S is just like POP3, except it goes crypto right away.
103 void pop3s_greeting(void)
105 struct CitContext *CCC = CC;
106 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
108 /* kill session if no crypto */
110 if (!CCC->redirect_ssl) CCC->kill_me = KILLME_NO_CRYPTO;
112 CCC->kill_me = KILLME_NO_CRYPTO;
120 * Specify user name (implements POP3 "USER" command)
122 void pop3_user(char *argbuf)
124 struct CitContext *CCC = CC;
127 if (CCC->logged_in) {
128 cprintf("-ERR You are already logged in.\r\n");
132 strcpy(username, argbuf);
135 if (CtdlLoginExistingUser(username) == login_ok) {
136 cprintf("+OK Password required for %s\r\n", username);
139 cprintf("-ERR No such user.\r\n");
145 * Back end for pop3_grab_mailbox()
147 void pop3_add_message(long msgnum, void *userdata)
149 struct CitContext *CCC = CC;
153 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
154 else POP3->msgs = realloc(POP3->msgs,
155 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
156 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
157 POP3->msgs[POP3->num_msgs-1].deleted = 0;
159 /* We need to know the length of this message when it is printed in
160 * RFC822 format. Perhaps we have cached this length in the message's
161 * metadata record. If so, great; if not, measure it and then cache
164 GetMetaData(&smi, msgnum);
165 if (smi.meta_rfc822_length <= 0L) {
166 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
167 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO, NULL, NULL, NULL);
168 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
169 FreeStrBuf(&CCC->redirect_buffer);
172 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
177 * Open the inbox and read its contents.
178 * (This should be called only once, by pop3_pass(), and returns the number
179 * of messages in the inbox, or -1 for error)
181 int pop3_grab_mailbox(void)
183 struct CitContext *CCC = CC;
187 if (CtdlGetRoom(&CCC->room, MAILROOM) != 0) return(-1);
189 /* Load up the messages */
190 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, pop3_add_message, NULL);
192 /* Figure out which are old and which are new */
193 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
194 POP3->lastseen = (-1);
195 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
196 if (is_msg_in_sequence_set(vbuf.v_seen, (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
201 return(POP3->num_msgs);
205 void pop3_login(void)
207 struct CitContext *CCC = CC;
210 msgs = pop3_grab_mailbox();
212 cprintf("+OK %s is logged in (%d messages)\r\n",
213 CCC->user.fullname, msgs);
214 syslog(LOG_DEBUG, "pop3: authenticated %s", CCC->user.fullname);
217 cprintf("-ERR Can't open your mailbox\r\n");
224 * Authorize with password (implements POP3 "PASS" command)
226 void pop3_pass(char *argbuf) {
229 safestrncpy(password, argbuf, sizeof password);
232 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
236 cprintf("-ERR That is NOT the password.\r\n");
242 * list available msgs
244 void pop3_list(char *argbuf) {
248 which_one = atoi(argbuf);
250 /* "list one" mode */
252 if (which_one > POP3->num_msgs) {
253 cprintf("-ERR no such message, only %d are here\r\n", POP3->num_msgs);
256 else if (POP3->msgs[which_one-1].deleted) {
257 cprintf("-ERR Sorry, you deleted that message.\r\n");
261 cprintf("+OK %d %ld\r\n", which_one, (long)POP3->msgs[which_one-1].rfc822_length);
266 /* "list all" (scan listing) mode */
268 cprintf("+OK Here's your mail:\r\n");
269 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
270 if (! POP3->msgs[i].deleted) {
271 cprintf("%d %ld\r\n", i+1, (long)POP3->msgs[i].rfc822_length);
280 * STAT (tally up the total message count and byte count) command
282 void pop3_stat(char *argbuf) {
284 size_t total_octets = 0;
287 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
288 if (! POP3->msgs[i].deleted) {
290 total_octets += POP3->msgs[i].rfc822_length;
294 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
299 * RETR command (fetch a message)
301 void pop3_retr(char *argbuf) {
304 which_one = atoi(argbuf);
305 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
306 cprintf("-ERR No such message.\r\n");
310 if (POP3->msgs[which_one - 1].deleted) {
311 cprintf("-ERR Sorry, you deleted that message.\r\n");
315 cprintf("+OK Message %d:\r\n", which_one);
316 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822,
317 HEADERS_ALL, 0, 1, NULL,
318 (ESC_DOT|SUPPRESS_ENV_TO), NULL, NULL, NULL
325 * TOP command (dumb way of fetching a partial message or headers-only)
327 void pop3_top(char *argbuf)
329 struct CitContext *CCC = CC;
331 int lines_requested = 0;
332 int lines_dumped = 0;
339 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
340 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
341 cprintf("-ERR No such message.\r\n");
345 if (POP3->msgs[which_one - 1].deleted) {
346 cprintf("-ERR Sorry, you deleted that message.\r\n");
350 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
352 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
359 msgtext = CCC->redirect_buffer;
360 CCC->redirect_buffer = NULL;
362 cprintf("+OK Message %d:\r\n", which_one);
364 ptr = ChrPtr(msgtext);
365 while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
366 ( (*ptr != 0) && (done == 0))) {
369 if (lines_dumped >= lines_requested) {
373 if ((in_body == 0) || (done == 0)) {
374 client_write(buf, strlen(buf));
379 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
382 if (buf[strlen(buf)-1] != 10) cprintf("\n");
383 FreeStrBuf(&msgtext);
390 * DELE (delete message from mailbox)
392 void pop3_dele(char *argbuf) {
395 which_one = atoi(argbuf);
396 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
397 cprintf("-ERR No such message.\r\n");
401 if (POP3->msgs[which_one - 1].deleted) {
402 cprintf("-ERR You already deleted that message.\r\n");
406 /* Flag the message as deleted. Will expunge during QUIT command. */
407 POP3->msgs[which_one - 1].deleted = 1;
408 cprintf("+OK Message %d deleted.\r\n",
413 /* Perform "UPDATE state" stuff
415 void pop3_update(void)
417 struct CitContext *CCC = CC;
421 long *deletemsgs = NULL;
422 int num_deletemsgs = 0;
424 /* Remove messages marked for deletion */
425 if (POP3->num_msgs > 0) {
426 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
427 for (i=0; i<POP3->num_msgs; ++i) {
428 if (POP3->msgs[i].deleted) {
429 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
432 if (num_deletemsgs > 0) {
433 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
438 /* Set last read pointer */
439 if (POP3->num_msgs > 0) {
440 CtdlLockGetCurrentUser();
441 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
442 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", POP3->msgs[POP3->num_msgs-1].msgnum);
443 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
444 CtdlPutCurrentUserLock();
451 * RSET (reset, i.e. undelete any deleted messages) command
453 void pop3_rset(char *argbuf) {
456 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
457 if (POP3->msgs[i].deleted) {
458 POP3->msgs[i].deleted = 0;
461 cprintf("+OK Reset completed.\r\n");
466 * LAST (Determine which message is the last unread message)
468 void pop3_last(char *argbuf) {
469 cprintf("+OK %d\r\n", POP3->lastseen + 1);
474 * CAPA is a command which tells the client which POP3 extensions are supported.
476 void pop3_capa(void) {
477 cprintf("+OK Capability list follows\r\n"
481 "IMPLEMENTATION %s\r\n"
490 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
491 * identifiers are simply the Citadel message numbers in the database.
493 void pop3_uidl(char *argbuf) {
497 which_one = atoi(argbuf);
499 /* "list one" mode */
501 if (which_one > POP3->num_msgs) {
502 cprintf("-ERR no such message, only %d are here\r\n", POP3->num_msgs);
505 else if (POP3->msgs[which_one-1].deleted) {
506 cprintf("-ERR Sorry, you deleted that message.\r\n");
510 cprintf("+OK %d %ld\r\n", which_one, POP3->msgs[which_one-1].msgnum);
515 /* "list all" (scan listing) mode */
517 cprintf("+OK Here's your mail:\r\n");
518 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
519 if (! POP3->msgs[i].deleted) {
520 cprintf("%d %ld\r\n", i+1, POP3->msgs[i].msgnum);
529 * implements the STLS command (Citadel API version)
533 char ok_response[SIZ];
534 char nosup_response[SIZ];
535 char error_response[SIZ];
537 sprintf(ok_response, "+OK Begin TLS negotiation now\r\n");
538 sprintf(nosup_response, "-ERR TLS not supported here\r\n");
539 sprintf(error_response, "-ERR Internal error\r\n");
540 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
545 * Main command loop for POP3 sessions.
547 void pop3_command_loop(void)
549 struct CitContext *CCC = CC;
553 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
554 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
555 syslog(LOG_INFO, "pop3: client disconnected; ending session.");
556 CCC->kill_me = KILLME_CLIENT_DISCONNECTED;
559 if (!strncasecmp(cmdbuf, "PASS", 4)) {
560 syslog(LOG_DEBUG, "pop3: PASS...");
563 syslog(LOG_DEBUG, "pop3: %s", cmdbuf);
565 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
567 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
568 cprintf("+OK No operation.\r\n");
571 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
575 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
576 cprintf("+OK Goodbye...\r\n");
578 CCC->kill_me = KILLME_CLIENT_LOGGED_OUT;
582 else if (!strncasecmp(cmdbuf, "USER", 4)) {
583 pop3_user(&cmdbuf[5]);
586 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
587 pop3_pass(&cmdbuf[5]);
591 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
596 else if (!CCC->logged_in) {
597 cprintf("-ERR Not logged in.\r\n");
600 else if (CCC->nologin) {
601 cprintf("-ERR System busy, try later.\r\n");
602 CCC->kill_me = KILLME_NOLOGIN;
605 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
606 pop3_list(&cmdbuf[5]);
609 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
610 pop3_stat(&cmdbuf[5]);
613 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
614 pop3_retr(&cmdbuf[5]);
617 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
618 pop3_dele(&cmdbuf[5]);
621 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
622 pop3_rset(&cmdbuf[5]);
625 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
626 pop3_uidl(&cmdbuf[5]);
629 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
630 pop3_top(&cmdbuf[4]);
633 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
634 pop3_last(&cmdbuf[4]);
638 cprintf("-ERR I'm afraid I can't do that.\r\n");
643 const char *CitadelServicePop3="POP3";
644 const char *CitadelServicePop3S="POP3S";
647 CTDL_MODULE_INIT(pop3)
651 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3_port"),
658 CtdlRegisterServiceHook(CtdlGetConfigInt("c_pop3s_port"),
663 CitadelServicePop3S);
665 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP, PRIO_STOP + 30);
668 /* return our module name for the log */