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\n");
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 %s\r\n",
107 * POP3S is just like POP3, except it goes crypto right away.
109 void pop3s_greeting(void) {
110 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
112 /* kill session if no crypto */
114 if (!CC->redirect_ssl) CC->kill_me = 1;
125 * Specify user name (implements POP3 "USER" command)
127 void pop3_user(char *argbuf) {
131 cprintf("-ERR You are already logged in.\r\n");
135 strcpy(username, argbuf);
138 /* syslog(LOG_DEBUG, "Trying <%s>\n", username); */
139 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
140 cprintf("+OK Password required for %s\r\n", username);
143 cprintf("-ERR No such user.\r\n");
150 * Back end for pop3_grab_mailbox()
152 void pop3_add_message(long msgnum, void *userdata) {
156 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
157 else POP3->msgs = realloc(POP3->msgs,
158 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
159 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
160 POP3->msgs[POP3->num_msgs-1].deleted = 0;
162 /* We need to know the length of this message when it is printed in
163 * RFC822 format. Perhaps we have cached this length in the message's
164 * metadata record. If so, great; if not, measure it and then cache
167 GetMetaData(&smi, msgnum);
168 if (smi.meta_rfc822_length <= 0L) {
169 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
170 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
171 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
172 FreeStrBuf(&CC->redirect_buffer); /* TODO: WHEW, all this for just knowing the length???? */
175 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
181 * Open the inbox and read its contents.
182 * (This should be called only once, by pop3_pass(), and returns the number
183 * of messages in the inbox, or -1 for error)
185 int pop3_grab_mailbox(void) {
189 if (CtdlGetRoom(&CC->room, MAILROOM) != 0) return(-1);
191 /* Load up the messages */
192 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
193 pop3_add_message, NULL);
195 /* Figure out which are old and which are new */
196 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
197 POP3->lastseen = (-1);
198 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
199 if (is_msg_in_sequence_set(vbuf.v_seen,
200 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
205 return(POP3->num_msgs);
208 void pop3_login(void)
212 msgs = pop3_grab_mailbox();
214 cprintf("+OK %s is logged in (%d messages)\r\n",
215 CC->user.fullname, msgs);
216 syslog(LOG_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
219 cprintf("-ERR Can't open your mailbox\r\n");
224 void pop3_apop(char *argbuf)
227 char userdigest[MD5_HEXSTRING_SIZE];
228 char realdigest[MD5_HEXSTRING_SIZE];
233 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
237 if ((sptr = strchr(argbuf, ' ')) == NULL)
239 cprintf("-ERR Invalid APOP line.\r\n");
245 while ((*sptr) && isspace(*sptr))
248 strncpy(username, argbuf, sizeof(username)-1);
249 username[sizeof(username)-1] = '\0';
251 memset(userdigest, 0, MD5_HEXSTRING_SIZE);
252 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
254 if (CtdlLoginExistingUser(NULL, username) != login_ok)
256 cprintf("-ERR No such user.\r\n");
260 if (CtdlGetUser(&CC->user, CC->curr_user))
262 cprintf("-ERR No such user.\r\n");
266 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
267 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
274 cprintf("-ERR That is NOT the password.\r\n");
280 * Authorize with password (implements POP3 "PASS" command)
282 void pop3_pass(char *argbuf) {
285 safestrncpy(password, argbuf, sizeof password);
288 /* syslog(LOG_DEBUG, "Trying <%s>\n", password); */
289 if (CtdlTryPassword(password, strlen(password)) == pass_ok) {
293 cprintf("-ERR That is NOT the password.\r\n");
300 * list available msgs
302 void pop3_list(char *argbuf) {
306 which_one = atoi(argbuf);
308 /* "list one" mode */
310 if (which_one > POP3->num_msgs) {
311 cprintf("-ERR no such message, only %d are here\r\n",
315 else if (POP3->msgs[which_one-1].deleted) {
316 cprintf("-ERR Sorry, you deleted that message.\r\n");
320 cprintf("+OK %d %ld\r\n",
322 (long)POP3->msgs[which_one-1].rfc822_length
328 /* "list all" (scan listing) mode */
330 cprintf("+OK Here's your mail:\r\n");
331 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
332 if (! POP3->msgs[i].deleted) {
333 cprintf("%d %ld\r\n",
335 (long)POP3->msgs[i].rfc822_length);
344 * STAT (tally up the total message count and byte count) command
346 void pop3_stat(char *argbuf) {
348 size_t total_octets = 0;
351 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
352 if (! POP3->msgs[i].deleted) {
354 total_octets += POP3->msgs[i].rfc822_length;
358 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
364 * RETR command (fetch a message)
366 void pop3_retr(char *argbuf) {
369 which_one = atoi(argbuf);
370 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
371 cprintf("-ERR No such message.\r\n");
375 if (POP3->msgs[which_one - 1].deleted) {
376 cprintf("-ERR Sorry, you deleted that message.\r\n");
380 cprintf("+OK Message %d:\r\n", which_one);
381 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
382 MT_RFC822, HEADERS_ALL, 0, 1, NULL,
383 (ESC_DOT|SUPPRESS_ENV_TO)
390 * TOP command (dumb way of fetching a partial message or headers-only)
392 void pop3_top(char *argbuf) {
394 int lines_requested = 0;
395 int lines_dumped = 0;
402 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
403 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
404 cprintf("-ERR No such message.\r\n");
408 if (POP3->msgs[which_one - 1].deleted) {
409 cprintf("-ERR Sorry, you deleted that message.\r\n");
413 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
414 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
415 msgtext = CC->redirect_buffer;
416 CC->redirect_buffer = NULL;
418 cprintf("+OK Message %d:\r\n", which_one);
420 ptr = ChrPtr(msgtext);
421 while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
422 ( (*ptr != 0) && (done == 0))) {
425 if (lines_dumped >= lines_requested) {
429 if ((in_body == 0) || (done == 0)) {
430 client_write(buf, strlen(buf));
435 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
438 if (buf[strlen(buf)-1] != 10) cprintf("\n");
439 FreeStrBuf(&msgtext);
446 * DELE (delete message from mailbox)
448 void pop3_dele(char *argbuf) {
451 which_one = atoi(argbuf);
452 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
453 cprintf("-ERR No such message.\r\n");
457 if (POP3->msgs[which_one - 1].deleted) {
458 cprintf("-ERR You already deleted that message.\r\n");
462 /* Flag the message as deleted. Will expunge during QUIT command. */
463 POP3->msgs[which_one - 1].deleted = 1;
464 cprintf("+OK Message %d deleted.\r\n",
469 /* Perform "UPDATE state" stuff
471 void pop3_update(void) {
475 long *deletemsgs = NULL;
476 int num_deletemsgs = 0;
478 /* Remove messages marked for deletion */
479 if (POP3->num_msgs > 0) {
480 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
481 for (i=0; i<POP3->num_msgs; ++i) {
482 if (POP3->msgs[i].deleted) {
483 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
486 if (num_deletemsgs > 0) {
487 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
492 /* Set last read pointer */
493 if (POP3->num_msgs > 0) {
494 CtdlGetUserLock(&CC->user, CC->curr_user);
496 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
497 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
498 POP3->msgs[POP3->num_msgs-1].msgnum);
499 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
501 CtdlPutUserLock(&CC->user);
508 * RSET (reset, i.e. undelete any deleted messages) command
510 void pop3_rset(char *argbuf) {
513 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
514 if (POP3->msgs[i].deleted) {
515 POP3->msgs[i].deleted = 0;
518 cprintf("+OK Reset completed.\r\n");
524 * LAST (Determine which message is the last unread message)
526 void pop3_last(char *argbuf) {
527 cprintf("+OK %d\r\n", POP3->lastseen + 1);
532 * CAPA is a command which tells the client which POP3 extensions
535 void pop3_capa(void) {
536 cprintf("+OK Capability list follows\r\n"
540 "IMPLEMENTATION %s\r\n"
550 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
551 * identifiers are simply the Citadel message numbers in the database.
553 void pop3_uidl(char *argbuf) {
557 which_one = atoi(argbuf);
559 /* "list one" mode */
561 if (which_one > POP3->num_msgs) {
562 cprintf("-ERR no such message, only %d are here\r\n",
566 else if (POP3->msgs[which_one-1].deleted) {
567 cprintf("-ERR Sorry, you deleted that message.\r\n");
571 cprintf("+OK %d %ld\r\n",
573 POP3->msgs[which_one-1].msgnum
579 /* "list all" (scan listing) mode */
581 cprintf("+OK Here's your mail:\r\n");
582 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
583 if (! POP3->msgs[i].deleted) {
584 cprintf("%d %ld\r\n",
586 POP3->msgs[i].msgnum);
595 * implements the STLS command (Citadel API version)
599 char ok_response[SIZ];
600 char nosup_response[SIZ];
601 char error_response[SIZ];
604 "+OK Begin TLS negotiation now\r\n");
605 sprintf(nosup_response,
606 "-ERR TLS not supported here\r\n");
607 sprintf(error_response,
608 "-ERR Internal error\r\n");
609 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
619 * Main command loop for POP3 sessions.
621 void pop3_command_loop(void) {
625 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
626 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
627 syslog(LOG_ERR, "Client disconnected: ending session.\r\n");
631 if (!strncasecmp(cmdbuf, "PASS", 4)) {
632 syslog(LOG_INFO, "POP3: PASS...\r\n");
635 syslog(LOG_INFO, "POP3: %s\r\n", cmdbuf);
637 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
639 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
640 cprintf("+OK No operation.\r\n");
643 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
647 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
648 cprintf("+OK Goodbye...\r\n");
654 else if (!strncasecmp(cmdbuf, "USER", 4)) {
655 pop3_user(&cmdbuf[5]);
658 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
659 pop3_pass(&cmdbuf[5]);
662 else if (!strncasecmp(cmdbuf, "APOP", 4))
664 pop3_apop(&cmdbuf[5]);
668 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
673 else if (!CC->logged_in) {
674 cprintf("-ERR Not logged in.\r\n");
677 else if (CC->nologin) {
678 cprintf("-ERR System busy, try later.\r\n");
682 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
683 pop3_list(&cmdbuf[5]);
686 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
687 pop3_stat(&cmdbuf[5]);
690 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
691 pop3_retr(&cmdbuf[5]);
694 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
695 pop3_dele(&cmdbuf[5]);
698 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
699 pop3_rset(&cmdbuf[5]);
702 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
703 pop3_uidl(&cmdbuf[5]);
706 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
707 pop3_top(&cmdbuf[4]);
710 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
711 pop3_last(&cmdbuf[4]);
715 cprintf("-ERR I'm afraid I can't do that.\r\n");
720 const char *CitadelServicePop3="POP3";
721 const char *CitadelServicePop3S="POP3S";
724 CTDL_MODULE_INIT(pop3)
728 CtdlRegisterServiceHook(config.c_pop3_port,
735 CtdlRegisterServiceHook(config.c_pop3s_port,
740 CitadelServicePop3S);
742 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
745 /* return our Subversion id for the Log */