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);
101 if (!CC->redirect_ssl) CC->kill_me = 1; /* kill session if no crypto */
108 * Specify user name (implements POP3 "USER" command)
110 void pop3_user(char *argbuf) {
114 cprintf("-ERR You are already logged in.\r\n");
118 strcpy(username, argbuf);
121 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", username); */
122 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
123 cprintf("+OK Password required for %s\r\n", username);
126 cprintf("-ERR No such user.\r\n");
133 * Back end for pop3_grab_mailbox()
135 void pop3_add_message(long msgnum, void *userdata) {
139 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
140 else POP3->msgs = realloc(POP3->msgs,
141 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
142 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
143 POP3->msgs[POP3->num_msgs-1].deleted = 0;
145 /* We need to know the length of this message when it is printed in
146 * RFC822 format. Perhaps we have cached this length in the message's
147 * metadata record. If so, great; if not, measure it and then cache
150 GetMetaData(&smi, msgnum);
151 if (smi.meta_rfc822_length <= 0L) {
152 CC->redirect_buffer = malloc(SIZ);
153 CC->redirect_len = 0;
154 CC->redirect_alloc = SIZ;
155 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
156 smi.meta_rfc822_length = CC->redirect_len;
157 free(CC->redirect_buffer);
158 CC->redirect_buffer = NULL;
159 CC->redirect_len = 0;
160 CC->redirect_alloc = 0;
163 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
169 * Open the inbox and read its contents.
170 * (This should be called only once, by pop3_pass(), and returns the number
171 * of messages in the inbox, or -1 for error)
173 int pop3_grab_mailbox(void) {
177 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
179 /* Load up the messages */
180 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
181 pop3_add_message, NULL);
183 /* Figure out which are old and which are new */
184 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
185 POP3->lastseen = (-1);
186 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
187 if (is_msg_in_sequence_set(vbuf.v_seen,
188 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
193 return(POP3->num_msgs);
196 void pop3_login(void)
200 msgs = pop3_grab_mailbox();
202 cprintf("+OK %s is logged in (%d messages)\r\n",
203 CC->user.fullname, msgs);
204 lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
207 cprintf("-ERR Can't open your mailbox\r\n");
212 void pop3_apop(char *argbuf)
215 char userdigest[MD5_HEXSTRING_SIZE];
216 char realdigest[MD5_HEXSTRING_SIZE];
221 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
225 if ((sptr = strchr(argbuf, ' ')) == NULL)
227 cprintf("-ERR Invalid APOP line.\r\n");
233 while ((*sptr) && isspace(*sptr))
236 strncpy(username, argbuf, sizeof(username)-1);
237 username[sizeof(username)-1] = '\0';
239 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
240 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
242 if (CtdlLoginExistingUser(NULL, username) != login_ok)
244 cprintf("-ERR No such user.\r\n");
248 if (getuser(&CC->user, CC->curr_user))
250 cprintf("-ERR No such user.\r\n");
254 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
255 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
262 cprintf("-ERR That is NOT the password.\r\n");
268 * Authorize with password (implements POP3 "PASS" command)
270 void pop3_pass(char *argbuf) {
273 strcpy(password, argbuf);
276 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", password); */
277 if (CtdlTryPassword(password) == pass_ok) {
281 cprintf("-ERR That is NOT the password.\r\n");
288 * list available msgs
290 void pop3_list(char *argbuf) {
294 which_one = atoi(argbuf);
296 /* "list one" mode */
298 if (which_one > POP3->num_msgs) {
299 cprintf("-ERR no such message, only %d are here\r\n",
303 else if (POP3->msgs[which_one-1].deleted) {
304 cprintf("-ERR Sorry, you deleted that message.\r\n");
308 cprintf("+OK %d %ld\r\n",
310 (long)POP3->msgs[which_one-1].rfc822_length
316 /* "list all" (scan listing) mode */
318 cprintf("+OK Here's your mail:\r\n");
319 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
320 if (! POP3->msgs[i].deleted) {
321 cprintf("%d %ld\r\n",
323 (long)POP3->msgs[i].rfc822_length);
332 * STAT (tally up the total message count and byte count) command
334 void pop3_stat(char *argbuf) {
336 size_t total_octets = 0;
339 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
340 if (! POP3->msgs[i].deleted) {
342 total_octets += POP3->msgs[i].rfc822_length;
346 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
352 * RETR command (fetch a message)
354 void pop3_retr(char *argbuf) {
357 which_one = atoi(argbuf);
358 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
359 cprintf("-ERR No such message.\r\n");
363 if (POP3->msgs[which_one - 1].deleted) {
364 cprintf("-ERR Sorry, you deleted that message.\r\n");
368 cprintf("+OK Message %d:\r\n", which_one);
369 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
375 * TOP command (dumb way of fetching a partial message or headers-only)
377 void pop3_top(char *argbuf) {
379 int lines_requested = 0;
380 int lines_dumped = 0;
387 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
388 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
389 cprintf("-ERR No such message.\r\n");
393 if (POP3->msgs[which_one - 1].deleted) {
394 cprintf("-ERR Sorry, you deleted that message.\r\n");
398 CC->redirect_buffer = malloc(SIZ);
399 CC->redirect_len = 0;
400 CC->redirect_alloc = SIZ;
401 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
402 MT_RFC822, HEADERS_ALL, 0, 1, NULL);
403 msgtext = CC->redirect_buffer;
404 CC->redirect_buffer = NULL;
405 CC->redirect_len = 0;
406 CC->redirect_alloc = 0;
408 cprintf("+OK Message %d:\r\n", which_one);
412 while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
413 ( (*ptr != 0) && (done == 0))) {
416 if (lines_dumped >= lines_requested) {
420 if ((in_body == 0) || (done == 0)) {
421 client_write(buf, strlen(buf));
426 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
429 if (buf[strlen(buf)-1] != 10) cprintf("\n");
437 * DELE (delete message from mailbox)
439 void pop3_dele(char *argbuf) {
442 which_one = atoi(argbuf);
443 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
444 cprintf("-ERR No such message.\r\n");
448 if (POP3->msgs[which_one - 1].deleted) {
449 cprintf("-ERR You already deleted that message.\r\n");
453 /* Flag the message as deleted. Will expunge during QUIT command. */
454 POP3->msgs[which_one - 1].deleted = 1;
455 cprintf("+OK Message %d deleted.\r\n",
460 /* Perform "UPDATE state" stuff
462 void pop3_update(void) {
466 long *deletemsgs = NULL;
467 int num_deletemsgs = 0;
469 /* Remove messages marked for deletion */
470 if (POP3->num_msgs > 0) {
471 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
472 for (i=0; i<POP3->num_msgs; ++i) {
473 if (POP3->msgs[i].deleted) {
474 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
477 if (num_deletemsgs > 0) {
478 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
483 /* Set last read pointer */
484 if (POP3->num_msgs > 0) {
485 lgetuser(&CC->user, CC->curr_user);
487 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
488 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
489 POP3->msgs[POP3->num_msgs-1].msgnum);
490 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
499 * RSET (reset, i.e. undelete any deleted messages) command
501 void pop3_rset(char *argbuf) {
504 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
505 if (POP3->msgs[i].deleted) {
506 POP3->msgs[i].deleted = 0;
509 cprintf("+OK Reset completed.\r\n");
515 * LAST (Determine which message is the last unread message)
517 void pop3_last(char *argbuf) {
518 cprintf("+OK %d\r\n", POP3->lastseen + 1);
523 * CAPA is a command which tells the client which POP3 extensions
526 void pop3_capa(void) {
527 cprintf("+OK Capability list follows\r\n"
531 "IMPLEMENTATION %s\r\n"
541 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
542 * identifiers are simply the Citadel message numbers in the database.
544 void pop3_uidl(char *argbuf) {
548 which_one = atoi(argbuf);
550 /* "list one" mode */
552 if (which_one > POP3->num_msgs) {
553 cprintf("-ERR no such message, only %d are here\r\n",
557 else if (POP3->msgs[which_one-1].deleted) {
558 cprintf("-ERR Sorry, you deleted that message.\r\n");
562 cprintf("+OK %d %ld\r\n",
564 POP3->msgs[which_one-1].msgnum
570 /* "list all" (scan listing) mode */
572 cprintf("+OK Here's your mail:\r\n");
573 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
574 if (! POP3->msgs[i].deleted) {
575 cprintf("%d %ld\r\n",
577 POP3->msgs[i].msgnum);
586 * implements the STLS command (Citadel API version)
590 char ok_response[SIZ];
591 char nosup_response[SIZ];
592 char error_response[SIZ];
595 "+OK Begin TLS negotiation now\r\n");
596 sprintf(nosup_response,
597 "-ERR TLS not supported here\r\n");
598 sprintf(error_response,
599 "-ERR Internal error\r\n");
600 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
610 * Main command loop for POP3 sessions.
612 void pop3_command_loop(void) {
616 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
617 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
618 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
622 if (!strncasecmp(cmdbuf, "PASS", 4)) {
623 lprintf(CTDL_INFO, "POP3: PASS...\r\n");
626 lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
628 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
630 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
631 cprintf("+OK No operation.\r\n");
634 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
638 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
639 cprintf("+OK Goodbye...\r\n");
645 else if (!strncasecmp(cmdbuf, "USER", 4)) {
646 pop3_user(&cmdbuf[5]);
649 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
650 pop3_pass(&cmdbuf[5]);
653 else if (!strncasecmp(cmdbuf, "APOP", 4))
655 pop3_apop(&cmdbuf[5]);
659 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
664 else if (!CC->logged_in) {
665 cprintf("-ERR Not logged in.\r\n");
668 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
669 pop3_list(&cmdbuf[5]);
672 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
673 pop3_stat(&cmdbuf[5]);
676 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
677 pop3_retr(&cmdbuf[5]);
680 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
681 pop3_dele(&cmdbuf[5]);
684 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
685 pop3_rset(&cmdbuf[5]);
688 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
689 pop3_uidl(&cmdbuf[5]);
692 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
693 pop3_top(&cmdbuf[4]);
696 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
697 pop3_last(&cmdbuf[4]);
701 cprintf("-ERR I'm afraid I can't do that.\r\n");
706 const char *CitadelServicePop3="POP3";
707 const char *CitadelServicePop3S="POP3S";
710 CTDL_MODULE_INIT(pop3)
712 CtdlRegisterServiceHook(config.c_pop3_port,
719 CtdlRegisterServiceHook(config.c_pop3s_port,
724 CitadelServicePop3S);
726 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
728 /* return our Subversion id for the Log */