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"
61 #include "ctdl_module.h"
66 * This cleanup function blows away the temporary memory and files used by
69 void pop3_cleanup_function(void) {
71 /* Don't do this stuff if this is not a POP3 session! */
72 if (CC->h_command_function != pop3_command_loop) return;
74 lprintf(CTDL_DEBUG, "Performing POP3 cleanup hook\n");
75 if (POP3->msgs != NULL) free(POP3->msgs);
83 * Here's where our POP3 session begins its happy day.
85 void pop3_greeting(void) {
86 strcpy(CC->cs_clientname, "POP3 session");
88 POP3 = malloc(sizeof(struct citpop3));
89 memset(POP3, 0, sizeof(struct citpop3));
91 cprintf("+OK Citadel POP3 server %s\r\n",
97 * POP3S is just like POP3, except it goes crypto right away.
99 void pop3s_greeting(void) {
100 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
102 if (!CC->redirect_ssl) CC->kill_me = 1; /* kill session if no crypto */
110 * Specify user name (implements POP3 "USER" command)
112 void pop3_user(char *argbuf) {
116 cprintf("-ERR You are already logged in.\r\n");
120 strcpy(username, argbuf);
123 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", username); */
124 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
125 cprintf("+OK Password required for %s\r\n", username);
128 cprintf("-ERR No such user.\r\n");
135 * Back end for pop3_grab_mailbox()
137 void pop3_add_message(long msgnum, void *userdata) {
141 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
142 else POP3->msgs = realloc(POP3->msgs,
143 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
144 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
145 POP3->msgs[POP3->num_msgs-1].deleted = 0;
147 /* We need to know the length of this message when it is printed in
148 * RFC822 format. Perhaps we have cached this length in the message's
149 * metadata record. If so, great; if not, measure it and then cache
152 GetMetaData(&smi, msgnum);
153 if (smi.meta_rfc822_length <= 0L) {
154 CC->redirect_buffer = malloc(SIZ);
155 CC->redirect_len = 0;
156 CC->redirect_alloc = SIZ;
157 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
158 smi.meta_rfc822_length = CC->redirect_len;
159 free(CC->redirect_buffer);
160 CC->redirect_buffer = NULL;
161 CC->redirect_len = 0;
162 CC->redirect_alloc = 0;
165 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
171 * Open the inbox and read its contents.
172 * (This should be called only once, by pop3_pass(), and returns the number
173 * of messages in the inbox, or -1 for error)
175 int pop3_grab_mailbox(void) {
179 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
181 /* Load up the messages */
182 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
183 pop3_add_message, NULL);
185 /* Figure out which are old and which are new */
186 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
187 POP3->lastseen = (-1);
188 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
189 if (is_msg_in_sequence_set(vbuf.v_seen,
190 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
195 return(POP3->num_msgs);
198 void pop3_login(void)
202 msgs = pop3_grab_mailbox();
204 cprintf("+OK %s is logged in (%d messages)\r\n",
205 CC->user.fullname, msgs);
206 lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
209 cprintf("-ERR Can't open your mailbox\r\n");
214 void pop3_apop(char *argbuf)
217 char userdigest[MD5_HEXSTRING_SIZE];
218 char realdigest[MD5_HEXSTRING_SIZE];
223 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
227 if ((sptr = strchr(argbuf, ' ')) == NULL)
229 cprintf("-ERR Invalid APOP line.\r\n");
235 while ((*sptr) && isspace(*sptr))
238 strncpy(username, argbuf, sizeof(username)-1);
239 username[sizeof(username)-1] = '\0';
241 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
242 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
244 if (CtdlLoginExistingUser(NULL, username) != login_ok)
246 cprintf("-ERR No such user.\r\n");
250 if (getuser(&CC->user, CC->curr_user))
252 cprintf("-ERR No such user.\r\n");
256 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
257 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
264 cprintf("-ERR That is NOT the password.\r\n");
270 * Authorize with password (implements POP3 "PASS" command)
272 void pop3_pass(char *argbuf) {
275 strcpy(password, argbuf);
278 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", password); */
279 if (CtdlTryPassword(password) == pass_ok) {
283 cprintf("-ERR That is NOT the password.\r\n");
290 * list available msgs
292 void pop3_list(char *argbuf) {
296 which_one = atoi(argbuf);
298 /* "list one" mode */
300 if (which_one > POP3->num_msgs) {
301 cprintf("-ERR no such message, only %d are here\r\n",
305 else if (POP3->msgs[which_one-1].deleted) {
306 cprintf("-ERR Sorry, you deleted that message.\r\n");
310 cprintf("+OK %d %ld\r\n",
312 (long)POP3->msgs[which_one-1].rfc822_length
318 /* "list all" (scan listing) mode */
320 cprintf("+OK Here's your mail:\r\n");
321 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
322 if (! POP3->msgs[i].deleted) {
323 cprintf("%d %ld\r\n",
325 (long)POP3->msgs[i].rfc822_length);
334 * STAT (tally up the total message count and byte count) command
336 void pop3_stat(char *argbuf) {
338 size_t total_octets = 0;
341 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
342 if (! POP3->msgs[i].deleted) {
344 total_octets += POP3->msgs[i].rfc822_length;
348 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
354 * RETR command (fetch a message)
356 void pop3_retr(char *argbuf) {
359 which_one = atoi(argbuf);
360 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
361 cprintf("-ERR No such message.\r\n");
365 if (POP3->msgs[which_one - 1].deleted) {
366 cprintf("-ERR Sorry, you deleted that message.\r\n");
370 cprintf("+OK Message %d:\r\n", which_one);
371 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
377 * TOP command (dumb way of fetching a partial message or headers-only)
379 void pop3_top(char *argbuf) {
381 int lines_requested = 0;
382 int lines_dumped = 0;
389 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
390 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
391 cprintf("-ERR No such message.\r\n");
395 if (POP3->msgs[which_one - 1].deleted) {
396 cprintf("-ERR Sorry, you deleted that message.\r\n");
400 CC->redirect_buffer = malloc(SIZ);
401 CC->redirect_len = 0;
402 CC->redirect_alloc = SIZ;
403 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
404 MT_RFC822, HEADERS_ALL, 0, 1, NULL);
405 msgtext = CC->redirect_buffer;
406 CC->redirect_buffer = NULL;
407 CC->redirect_len = 0;
408 CC->redirect_alloc = 0;
410 cprintf("+OK Message %d:\r\n", which_one);
414 while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
415 ( (*ptr != 0) && (done == 0))) {
418 if (lines_dumped >= lines_requested) {
422 if ((in_body == 0) || (done == 0)) {
423 client_write(buf, strlen(buf));
428 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
431 if (buf[strlen(buf)-1] != 10) cprintf("\n");
439 * DELE (delete message from mailbox)
441 void pop3_dele(char *argbuf) {
444 which_one = atoi(argbuf);
445 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
446 cprintf("-ERR No such message.\r\n");
450 if (POP3->msgs[which_one - 1].deleted) {
451 cprintf("-ERR You already deleted that message.\r\n");
455 /* Flag the message as deleted. Will expunge during QUIT command. */
456 POP3->msgs[which_one - 1].deleted = 1;
457 cprintf("+OK Message %d deleted.\r\n",
462 /* Perform "UPDATE state" stuff
464 void pop3_update(void) {
468 long *deletemsgs = NULL;
469 int num_deletemsgs = 0;
471 /* Remove messages marked for deletion */
472 if (POP3->num_msgs > 0) {
473 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
474 for (i=0; i<POP3->num_msgs; ++i) {
475 if (POP3->msgs[i].deleted) {
476 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
479 if (num_deletemsgs > 0) {
480 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
485 /* Set last read pointer */
486 if (POP3->num_msgs > 0) {
487 lgetuser(&CC->user, CC->curr_user);
489 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
490 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
491 POP3->msgs[POP3->num_msgs-1].msgnum);
492 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
501 * RSET (reset, i.e. undelete any deleted messages) command
503 void pop3_rset(char *argbuf) {
506 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
507 if (POP3->msgs[i].deleted) {
508 POP3->msgs[i].deleted = 0;
511 cprintf("+OK Reset completed.\r\n");
517 * LAST (Determine which message is the last unread message)
519 void pop3_last(char *argbuf) {
520 cprintf("+OK %d\r\n", POP3->lastseen + 1);
525 * CAPA is a command which tells the client which POP3 extensions
528 void pop3_capa(void) {
529 cprintf("+OK Capability list follows\r\n"
533 "IMPLEMENTATION %s\r\n"
543 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
544 * identifiers are simply the Citadel message numbers in the database.
546 void pop3_uidl(char *argbuf) {
550 which_one = atoi(argbuf);
552 /* "list one" mode */
554 if (which_one > POP3->num_msgs) {
555 cprintf("-ERR no such message, only %d are here\r\n",
559 else if (POP3->msgs[which_one-1].deleted) {
560 cprintf("-ERR Sorry, you deleted that message.\r\n");
564 cprintf("+OK %d %ld\r\n",
566 POP3->msgs[which_one-1].msgnum
572 /* "list all" (scan listing) mode */
574 cprintf("+OK Here's your mail:\r\n");
575 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
576 if (! POP3->msgs[i].deleted) {
577 cprintf("%d %ld\r\n",
579 POP3->msgs[i].msgnum);
588 * implements the STLS command (Citadel API version)
592 char ok_response[SIZ];
593 char nosup_response[SIZ];
594 char error_response[SIZ];
597 "+OK Begin TLS negotiation now\r\n");
598 sprintf(nosup_response,
599 "-ERR TLS not supported here\r\n");
600 sprintf(error_response,
601 "-ERR Internal error\r\n");
602 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
612 * Main command loop for POP3 sessions.
614 void pop3_command_loop(void) {
618 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
619 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
620 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
624 if (!strncasecmp(cmdbuf, "PASS", 4)) {
625 lprintf(CTDL_INFO, "POP3: PASS...\r\n");
628 lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
630 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
632 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
633 cprintf("+OK No operation.\r\n");
636 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
640 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
641 cprintf("+OK Goodbye...\r\n");
647 else if (!strncasecmp(cmdbuf, "USER", 4)) {
648 pop3_user(&cmdbuf[5]);
651 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
652 pop3_pass(&cmdbuf[5]);
655 else if (!strncasecmp(cmdbuf, "APOP", 4))
657 pop3_apop(&cmdbuf[5]);
661 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
666 else if (!CC->logged_in) {
667 cprintf("-ERR Not logged in.\r\n");
670 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
671 pop3_list(&cmdbuf[5]);
674 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
675 pop3_stat(&cmdbuf[5]);
678 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
679 pop3_retr(&cmdbuf[5]);
682 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
683 pop3_dele(&cmdbuf[5]);
686 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
687 pop3_rset(&cmdbuf[5]);
690 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
691 pop3_uidl(&cmdbuf[5]);
694 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
695 pop3_top(&cmdbuf[4]);
698 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
699 pop3_last(&cmdbuf[4]);
703 cprintf("-ERR I'm afraid I can't do that.\r\n");
708 const char *CitadelServicePop3="POP3";
709 const char *CitadelServicePop3S="POP3S";
712 CTDL_MODULE_INIT(pop3)
714 CtdlRegisterServiceHook(config.c_pop3_port,
721 CtdlRegisterServiceHook(config.c_pop3s_port,
726 CitadelServicePop3S);
728 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
730 /* return our Subversion id for the Log */