2 * POP3 service for the Citadel system
4 * Copyright (c) 1998-2009 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
76 * This cleanup function blows away the temporary memory and files used by
79 void pop3_cleanup_function(void) {
81 /* Don't do this stuff if this is not a POP3 session! */
82 if (CC->h_command_function != pop3_command_loop) return;
84 syslog(LOG_DEBUG, "Performing POP3 cleanup hook");
85 if (POP3->msgs != NULL) free(POP3->msgs);
93 * Here's where our POP3 session begins its happy day.
95 void pop3_greeting(void) {
96 strcpy(CC->cs_clientname, "POP3 session");
98 CC->session_specific_data = malloc(sizeof(struct citpop3));
99 memset(POP3, 0, sizeof(struct citpop3));
101 cprintf("+OK Citadel POP3 server ready.\r\n");
106 * POP3S is just like POP3, except it goes crypto right away.
108 void pop3s_greeting(void) {
109 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
111 /* kill session if no crypto */
113 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO;
115 CC->kill_me = KILLME_NO_CRYPTO;
124 * Specify user name (implements POP3 "USER" command)
126 void pop3_user(char *argbuf) {
130 cprintf("-ERR You are already logged in.\r\n");
134 strcpy(username, argbuf);
137 /* syslog(LOG_DEBUG, "Trying <%s>", username); */
138 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
139 cprintf("+OK Password required for %s\r\n", username);
142 cprintf("-ERR No such user.\r\n");
149 * Back end for pop3_grab_mailbox()
151 void pop3_add_message(long msgnum, void *userdata) {
155 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
156 else POP3->msgs = realloc(POP3->msgs,
157 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
158 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
159 POP3->msgs[POP3->num_msgs-1].deleted = 0;
161 /* We need to know the length of this message when it is printed in
162 * RFC822 format. Perhaps we have cached this length in the message's
163 * metadata record. If so, great; if not, measure it and then cache
166 GetMetaData(&smi, msgnum);
167 if (smi.meta_rfc822_length <= 0L) {
168 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
169 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
170 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
171 FreeStrBuf(&CC->redirect_buffer); /* TODO: WHEW, all this for just knowing the length???? */
174 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
180 * Open the inbox and read its contents.
181 * (This should be called only once, by pop3_pass(), and returns the number
182 * of messages in the inbox, or -1 for error)
184 int pop3_grab_mailbox(void) {
188 if (CtdlGetRoom(&CC->room, MAILROOM) != 0) return(-1);
190 /* Load up the messages */
191 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
192 pop3_add_message, NULL);
194 /* Figure out which are old and which are new */
195 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
196 POP3->lastseen = (-1);
197 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
198 if (is_msg_in_sequence_set(vbuf.v_seen,
199 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
204 return(POP3->num_msgs);
207 void pop3_login(void)
211 msgs = pop3_grab_mailbox();
213 cprintf("+OK %s is logged in (%d messages)\r\n",
214 CC->user.fullname, msgs);
215 syslog(LOG_NOTICE, "POP3 authenticated %s", CC->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 /* syslog(LOG_DEBUG, "Trying <%s>", password); */
234 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
238 cprintf("-ERR That is NOT the password.\r\n");
245 * list available msgs
247 void pop3_list(char *argbuf) {
251 which_one = atoi(argbuf);
253 /* "list one" mode */
255 if (which_one > POP3->num_msgs) {
256 cprintf("-ERR no such message, only %d are here\r\n",
260 else if (POP3->msgs[which_one-1].deleted) {
261 cprintf("-ERR Sorry, you deleted that message.\r\n");
265 cprintf("+OK %d %ld\r\n",
267 (long)POP3->msgs[which_one-1].rfc822_length
273 /* "list all" (scan listing) mode */
275 cprintf("+OK Here's your mail:\r\n");
276 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
277 if (! POP3->msgs[i].deleted) {
278 cprintf("%d %ld\r\n",
280 (long)POP3->msgs[i].rfc822_length);
289 * STAT (tally up the total message count and byte count) command
291 void pop3_stat(char *argbuf) {
293 size_t total_octets = 0;
296 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
297 if (! POP3->msgs[i].deleted) {
299 total_octets += POP3->msgs[i].rfc822_length;
303 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
309 * RETR command (fetch a message)
311 void pop3_retr(char *argbuf) {
314 which_one = atoi(argbuf);
315 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
316 cprintf("-ERR No such message.\r\n");
320 if (POP3->msgs[which_one - 1].deleted) {
321 cprintf("-ERR Sorry, you deleted that message.\r\n");
325 cprintf("+OK Message %d:\r\n", which_one);
326 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
327 MT_RFC822, HEADERS_ALL, 0, 1, NULL,
328 (ESC_DOT|SUPPRESS_ENV_TO)
335 * TOP command (dumb way of fetching a partial message or headers-only)
337 void pop3_top(char *argbuf) {
339 int lines_requested = 0;
340 int lines_dumped = 0;
347 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
348 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
349 cprintf("-ERR No such message.\r\n");
353 if (POP3->msgs[which_one - 1].deleted) {
354 cprintf("-ERR Sorry, you deleted that message.\r\n");
358 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
359 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
360 msgtext = CC->redirect_buffer;
361 CC->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) {
420 long *deletemsgs = NULL;
421 int num_deletemsgs = 0;
423 /* Remove messages marked for deletion */
424 if (POP3->num_msgs > 0) {
425 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
426 for (i=0; i<POP3->num_msgs; ++i) {
427 if (POP3->msgs[i].deleted) {
428 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
431 if (num_deletemsgs > 0) {
432 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
437 /* Set last read pointer */
438 if (POP3->num_msgs > 0) {
439 CtdlGetUserLock(&CC->user, CC->curr_user);
441 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
442 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
443 POP3->msgs[POP3->num_msgs-1].msgnum);
444 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
446 CtdlPutUserLock(&CC->user);
453 * RSET (reset, i.e. undelete any deleted messages) command
455 void pop3_rset(char *argbuf) {
458 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
459 if (POP3->msgs[i].deleted) {
460 POP3->msgs[i].deleted = 0;
463 cprintf("+OK Reset completed.\r\n");
469 * LAST (Determine which message is the last unread message)
471 void pop3_last(char *argbuf) {
472 cprintf("+OK %d\r\n", POP3->lastseen + 1);
477 * CAPA is a command which tells the client which POP3 extensions
480 void pop3_capa(void) {
481 cprintf("+OK Capability list follows\r\n"
485 "IMPLEMENTATION %s\r\n"
495 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
496 * identifiers are simply the Citadel message numbers in the database.
498 void pop3_uidl(char *argbuf) {
502 which_one = atoi(argbuf);
504 /* "list one" mode */
506 if (which_one > POP3->num_msgs) {
507 cprintf("-ERR no such message, only %d are here\r\n",
511 else if (POP3->msgs[which_one-1].deleted) {
512 cprintf("-ERR Sorry, you deleted that message.\r\n");
516 cprintf("+OK %d %ld\r\n",
518 POP3->msgs[which_one-1].msgnum
524 /* "list all" (scan listing) mode */
526 cprintf("+OK Here's your mail:\r\n");
527 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
528 if (! POP3->msgs[i].deleted) {
529 cprintf("%d %ld\r\n",
531 POP3->msgs[i].msgnum);
540 * implements the STLS command (Citadel API version)
544 char ok_response[SIZ];
545 char nosup_response[SIZ];
546 char error_response[SIZ];
549 "+OK Begin TLS negotiation now\r\n");
550 sprintf(nosup_response,
551 "-ERR TLS not supported here\r\n");
552 sprintf(error_response,
553 "-ERR Internal error\r\n");
554 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
564 * Main command loop for POP3 sessions.
566 void pop3_command_loop(void) {
570 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
571 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
572 syslog(LOG_ERR, "POP3 client disconnected: ending session.");
573 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
576 if (!strncasecmp(cmdbuf, "PASS", 4)) {
577 syslog(LOG_INFO, "POP3: PASS...");
580 syslog(LOG_INFO, "POP3: %s", cmdbuf);
582 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
584 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
585 cprintf("+OK No operation.\r\n");
588 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
592 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
593 cprintf("+OK Goodbye...\r\n");
595 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
599 else if (!strncasecmp(cmdbuf, "USER", 4)) {
600 pop3_user(&cmdbuf[5]);
603 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
604 pop3_pass(&cmdbuf[5]);
608 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
613 else if (!CC->logged_in) {
614 cprintf("-ERR Not logged in.\r\n");
617 else if (CC->nologin) {
618 cprintf("-ERR System busy, try later.\r\n");
619 CC->kill_me = KILLME_NOLOGIN;
622 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
623 pop3_list(&cmdbuf[5]);
626 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
627 pop3_stat(&cmdbuf[5]);
630 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
631 pop3_retr(&cmdbuf[5]);
634 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
635 pop3_dele(&cmdbuf[5]);
638 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
639 pop3_rset(&cmdbuf[5]);
642 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
643 pop3_uidl(&cmdbuf[5]);
646 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
647 pop3_top(&cmdbuf[4]);
650 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
651 pop3_last(&cmdbuf[4]);
655 cprintf("-ERR I'm afraid I can't do that.\r\n");
660 const char *CitadelServicePop3="POP3";
661 const char *CitadelServicePop3S="POP3S";
664 CTDL_MODULE_INIT(pop3)
668 CtdlRegisterServiceHook(config.c_pop3_port,
675 CtdlRegisterServiceHook(config.c_pop3s_port,
680 CitadelServicePop3S);
682 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
685 /* return our module name for the log */