4 * POP3 service for the Citadel system
5 * Copyright (C) 1998-2001 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * Current status of standards conformance:
10 * -> All required POP3 commands described in RFC1939 are implemented.
11 * -> All optional POP3 commands described in RFC1939 are also implemented.
12 * -> The deprecated "LAST" command is included in this implementation, because
13 * there exist mail clients which insist on using it (such as Bynari
14 * TradeMail, and certain versions of Eudora).
15 * -> Capability detection via the method described in RFC2449 is implemented.
27 #include <sys/types.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
46 #include "citserver.h"
55 #include "internet_addressing.h"
56 #include "serv_pop3.h"
60 #include "serv_crypto.h"
64 #include "ctdl_module.h"
69 * This cleanup function blows away the temporary memory and files used by
72 void pop3_cleanup_function(void) {
74 /* Don't do this stuff if this is not a POP3 session! */
75 if (CC->h_command_function != pop3_command_loop) return;
77 lprintf(CTDL_DEBUG, "Performing POP3 cleanup hook\n");
78 if (POP3->msgs != NULL) free(POP3->msgs);
86 * Here's where our POP3 session begins its happy day.
88 void pop3_greeting(void) {
89 strcpy(CC->cs_clientname, "POP3 session");
91 POP3 = malloc(sizeof(struct citpop3));
92 memset(POP3, 0, sizeof(struct citpop3));
94 cprintf("+OK Citadel POP3 server %s\r\n",
100 * POP3S is just like POP3, except it goes crypto right away.
103 void pop3s_greeting(void) {
104 CtdlStartTLS(NULL, NULL, NULL);
112 * Specify user name (implements POP3 "USER" command)
114 void pop3_user(char *argbuf) {
118 cprintf("-ERR You are already logged in.\r\n");
122 strcpy(username, argbuf);
125 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", username); */
126 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
127 cprintf("+OK Password required for %s\r\n", username);
130 cprintf("-ERR No such user.\r\n");
137 * Back end for pop3_grab_mailbox()
139 void pop3_add_message(long msgnum, void *userdata) {
143 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
144 else POP3->msgs = realloc(POP3->msgs,
145 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
146 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
147 POP3->msgs[POP3->num_msgs-1].deleted = 0;
149 /* We need to know the length of this message when it is printed in
150 * RFC822 format. Perhaps we have cached this length in the message's
151 * metadata record. If so, great; if not, measure it and then cache
154 GetMetaData(&smi, msgnum);
155 if (smi.meta_rfc822_length <= 0L) {
156 CC->redirect_buffer = malloc(SIZ);
157 CC->redirect_len = 0;
158 CC->redirect_alloc = SIZ;
159 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
160 smi.meta_rfc822_length = CC->redirect_len;
161 free(CC->redirect_buffer);
162 CC->redirect_buffer = NULL;
163 CC->redirect_len = 0;
164 CC->redirect_alloc = 0;
167 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
173 * Open the inbox and read its contents.
174 * (This should be called only once, by pop3_pass(), and returns the number
175 * of messages in the inbox, or -1 for error)
177 int pop3_grab_mailbox(void) {
181 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
183 /* Load up the messages */
184 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
185 pop3_add_message, NULL);
187 /* Figure out which are old and which are new */
188 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
189 POP3->lastseen = (-1);
190 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
191 if (is_msg_in_sequence_set(vbuf.v_seen,
192 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
197 return(POP3->num_msgs);
200 void pop3_login(void)
204 msgs = pop3_grab_mailbox();
206 cprintf("+OK %s is logged in (%d messages)\r\n",
207 CC->user.fullname, msgs);
208 lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
211 cprintf("-ERR Can't open your mailbox\r\n");
216 void pop3_apop(char *argbuf)
219 char userdigest[MD5_HEXSTRING_SIZE];
220 char realdigest[MD5_HEXSTRING_SIZE];
225 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
229 if ((sptr = strchr(argbuf, ' ')) == NULL)
231 cprintf("-ERR Invalid APOP line.\r\n");
237 while ((*sptr) && isspace(*sptr))
240 strncpy(username, argbuf, sizeof(username)-1);
241 username[sizeof(username)-1] = '\0';
243 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
244 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
246 if (CtdlLoginExistingUser(NULL, username) != login_ok)
248 cprintf("-ERR No such user.\r\n");
252 if (getuser(&CC->user, CC->curr_user))
254 cprintf("-ERR No such user.\r\n");
258 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
259 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
266 cprintf("-ERR That is NOT the password.\r\n");
272 * Authorize with password (implements POP3 "PASS" command)
274 void pop3_pass(char *argbuf) {
277 strcpy(password, argbuf);
280 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", password); */
281 if (CtdlTryPassword(password) == pass_ok) {
285 cprintf("-ERR That is NOT the password.\r\n");
292 * list available msgs
294 void pop3_list(char *argbuf) {
298 which_one = atoi(argbuf);
300 /* "list one" mode */
302 if (which_one > POP3->num_msgs) {
303 cprintf("-ERR no such message, only %d are here\r\n",
307 else if (POP3->msgs[which_one-1].deleted) {
308 cprintf("-ERR Sorry, you deleted that message.\r\n");
312 cprintf("+OK %d %ld\r\n",
314 (long)POP3->msgs[which_one-1].rfc822_length
320 /* "list all" (scan listing) mode */
322 cprintf("+OK Here's your mail:\r\n");
323 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
324 if (! POP3->msgs[i].deleted) {
325 cprintf("%d %ld\r\n",
327 (long)POP3->msgs[i].rfc822_length);
336 * STAT (tally up the total message count and byte count) command
338 void pop3_stat(char *argbuf) {
340 size_t total_octets = 0;
343 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
344 if (! POP3->msgs[i].deleted) {
346 total_octets += POP3->msgs[i].rfc822_length;
350 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
356 * RETR command (fetch a message)
358 void pop3_retr(char *argbuf) {
361 which_one = atoi(argbuf);
362 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
363 cprintf("-ERR No such message.\r\n");
367 if (POP3->msgs[which_one - 1].deleted) {
368 cprintf("-ERR Sorry, you deleted that message.\r\n");
372 cprintf("+OK Message %d:\r\n", which_one);
373 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
379 * TOP command (dumb way of fetching a partial message or headers-only)
381 void pop3_top(char *argbuf) {
383 int lines_requested = 0;
384 int lines_dumped = 0;
391 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
392 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
393 cprintf("-ERR No such message.\r\n");
397 if (POP3->msgs[which_one - 1].deleted) {
398 cprintf("-ERR Sorry, you deleted that message.\r\n");
402 CC->redirect_buffer = malloc(SIZ);
403 CC->redirect_len = 0;
404 CC->redirect_alloc = SIZ;
405 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
406 MT_RFC822, HEADERS_ALL, 0, 1, NULL);
407 msgtext = CC->redirect_buffer;
408 CC->redirect_buffer = NULL;
409 CC->redirect_len = 0;
410 CC->redirect_alloc = 0;
412 cprintf("+OK Message %d:\r\n", which_one);
416 while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
417 ( (*ptr != 0) && (done == 0))) {
420 if (lines_dumped >= lines_requested) {
424 if ((in_body == 0) || (done == 0)) {
425 client_write(buf, strlen(buf));
430 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
433 if (buf[strlen(buf)-1] != 10) cprintf("\n");
441 * DELE (delete message from mailbox)
443 void pop3_dele(char *argbuf) {
446 which_one = atoi(argbuf);
447 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
448 cprintf("-ERR No such message.\r\n");
452 if (POP3->msgs[which_one - 1].deleted) {
453 cprintf("-ERR You already deleted that message.\r\n");
457 /* Flag the message as deleted. Will expunge during QUIT command. */
458 POP3->msgs[which_one - 1].deleted = 1;
459 cprintf("+OK Message %d deleted.\r\n",
464 /* Perform "UPDATE state" stuff
466 void pop3_update(void) {
470 long *deletemsgs = NULL;
471 int num_deletemsgs = 0;
473 /* Remove messages marked for deletion */
474 if (POP3->num_msgs > 0) {
475 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
476 for (i=0; i<POP3->num_msgs; ++i) {
477 if (POP3->msgs[i].deleted) {
478 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
481 if (num_deletemsgs > 0) {
482 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
487 /* Set last read pointer */
488 if (POP3->num_msgs > 0) {
489 lgetuser(&CC->user, CC->curr_user);
491 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
492 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
493 POP3->msgs[POP3->num_msgs-1].msgnum);
494 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
503 * RSET (reset, i.e. undelete any deleted messages) command
505 void pop3_rset(char *argbuf) {
508 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
509 if (POP3->msgs[i].deleted) {
510 POP3->msgs[i].deleted = 0;
513 cprintf("+OK Reset completed.\r\n");
519 * LAST (Determine which message is the last unread message)
521 void pop3_last(char *argbuf) {
522 cprintf("+OK %d\r\n", POP3->lastseen + 1);
527 * CAPA is a command which tells the client which POP3 extensions
530 void pop3_capa(void) {
531 cprintf("+OK Capability list follows\r\n"
535 "IMPLEMENTATION %s\r\n"
545 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
546 * identifiers are simply the Citadel message numbers in the database.
548 void pop3_uidl(char *argbuf) {
552 which_one = atoi(argbuf);
554 /* "list one" mode */
556 if (which_one > POP3->num_msgs) {
557 cprintf("-ERR no such message, only %d are here\r\n",
561 else if (POP3->msgs[which_one-1].deleted) {
562 cprintf("-ERR Sorry, you deleted that message.\r\n");
566 cprintf("+OK %d %ld\r\n",
568 POP3->msgs[which_one-1].msgnum
574 /* "list all" (scan listing) mode */
576 cprintf("+OK Here's your mail:\r\n");
577 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
578 if (! POP3->msgs[i].deleted) {
579 cprintf("%d %ld\r\n",
581 POP3->msgs[i].msgnum);
590 * implements the STLS command (Citadel API version)
595 char ok_response[SIZ];
596 char nosup_response[SIZ];
597 char error_response[SIZ];
600 "+OK Begin TLS negotiation now\r\n");
601 sprintf(nosup_response,
602 "-ERR TLS not supported here\r\n");
603 sprintf(error_response,
604 "-ERR Internal error\r\n");
605 CtdlStartTLS(ok_response, nosup_response, error_response);
616 * Main command loop for POP3 sessions.
618 void pop3_command_loop(void) {
622 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
623 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
624 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
628 if (!strncasecmp(cmdbuf, "PASS", 4)) {
629 lprintf(CTDL_INFO, "POP3: PASS...\r\n");
632 lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
634 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
636 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
637 cprintf("+OK No operation.\r\n");
640 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
644 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
645 cprintf("+OK Goodbye...\r\n");
651 else if (!strncasecmp(cmdbuf, "USER", 4)) {
652 pop3_user(&cmdbuf[5]);
655 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
656 pop3_pass(&cmdbuf[5]);
659 else if (!strncasecmp(cmdbuf, "APOP", 4))
661 pop3_apop(&cmdbuf[5]);
665 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
670 else if (!CC->logged_in) {
671 cprintf("-ERR Not logged in.\r\n");
674 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
675 pop3_list(&cmdbuf[5]);
678 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
679 pop3_stat(&cmdbuf[5]);
682 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
683 pop3_retr(&cmdbuf[5]);
686 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
687 pop3_dele(&cmdbuf[5]);
690 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
691 pop3_rset(&cmdbuf[5]);
694 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
695 pop3_uidl(&cmdbuf[5]);
698 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
699 pop3_top(&cmdbuf[4]);
702 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
703 pop3_last(&cmdbuf[4]);
707 cprintf("-ERR I'm afraid I can't do that.\r\n");
714 CTDL_MODULE_INIT(pop3)
716 CtdlRegisterServiceHook(config.c_pop3_port,
722 CtdlRegisterServiceHook(config.c_pop3s_port,
728 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
730 /* return our Subversion id for the Log */