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>
44 #include <libcitadel.h>
47 #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 CC->session_specific_data = 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 /* kill session if no crypto */
104 if (!CC->redirect_ssl) CC->kill_me = 1;
115 * Specify user name (implements POP3 "USER" command)
117 void pop3_user(char *argbuf) {
121 cprintf("-ERR You are already logged in.\r\n");
125 strcpy(username, argbuf);
128 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", username); */
129 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
130 cprintf("+OK Password required for %s\r\n", username);
133 cprintf("-ERR No such user.\r\n");
140 * Back end for pop3_grab_mailbox()
142 void pop3_add_message(long msgnum, void *userdata) {
146 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
147 else POP3->msgs = realloc(POP3->msgs,
148 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
149 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
150 POP3->msgs[POP3->num_msgs-1].deleted = 0;
152 /* We need to know the length of this message when it is printed in
153 * RFC822 format. Perhaps we have cached this length in the message's
154 * metadata record. If so, great; if not, measure it and then cache
157 GetMetaData(&smi, msgnum);
158 if (smi.meta_rfc822_length <= 0L) {
159 CC->redirect_buffer = malloc(SIZ);
160 CC->redirect_len = 0;
161 CC->redirect_alloc = SIZ;
162 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
163 smi.meta_rfc822_length = CC->redirect_len;
164 free(CC->redirect_buffer);
165 CC->redirect_buffer = NULL;
166 CC->redirect_len = 0;
167 CC->redirect_alloc = 0;
170 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
176 * Open the inbox and read its contents.
177 * (This should be called only once, by pop3_pass(), and returns the number
178 * of messages in the inbox, or -1 for error)
180 int pop3_grab_mailbox(void) {
184 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
186 /* Load up the messages */
187 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
188 pop3_add_message, NULL);
190 /* Figure out which are old and which are new */
191 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
192 POP3->lastseen = (-1);
193 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
194 if (is_msg_in_sequence_set(vbuf.v_seen,
195 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
200 return(POP3->num_msgs);
203 void pop3_login(void)
207 msgs = pop3_grab_mailbox();
209 cprintf("+OK %s is logged in (%d messages)\r\n",
210 CC->user.fullname, msgs);
211 lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
214 cprintf("-ERR Can't open your mailbox\r\n");
219 void pop3_apop(char *argbuf)
222 char userdigest[MD5_HEXSTRING_SIZE];
223 char realdigest[MD5_HEXSTRING_SIZE];
228 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
232 if ((sptr = strchr(argbuf, ' ')) == NULL)
234 cprintf("-ERR Invalid APOP line.\r\n");
240 while ((*sptr) && isspace(*sptr))
243 strncpy(username, argbuf, sizeof(username)-1);
244 username[sizeof(username)-1] = '\0';
246 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
247 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
249 if (CtdlLoginExistingUser(NULL, username) != login_ok)
251 cprintf("-ERR No such user.\r\n");
255 if (getuser(&CC->user, CC->curr_user))
257 cprintf("-ERR No such user.\r\n");
261 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
262 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
269 cprintf("-ERR That is NOT the password.\r\n");
275 * Authorize with password (implements POP3 "PASS" command)
277 void pop3_pass(char *argbuf) {
280 strcpy(password, argbuf);
283 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", password); */
284 if (CtdlTryPassword(password) == pass_ok) {
288 cprintf("-ERR That is NOT the password.\r\n");
295 * list available msgs
297 void pop3_list(char *argbuf) {
301 which_one = atoi(argbuf);
303 /* "list one" mode */
305 if (which_one > POP3->num_msgs) {
306 cprintf("-ERR no such message, only %d are here\r\n",
310 else if (POP3->msgs[which_one-1].deleted) {
311 cprintf("-ERR Sorry, you deleted that message.\r\n");
315 cprintf("+OK %d %ld\r\n",
317 (long)POP3->msgs[which_one-1].rfc822_length
323 /* "list all" (scan listing) mode */
325 cprintf("+OK Here's your mail:\r\n");
326 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
327 if (! POP3->msgs[i].deleted) {
328 cprintf("%d %ld\r\n",
330 (long)POP3->msgs[i].rfc822_length);
339 * STAT (tally up the total message count and byte count) command
341 void pop3_stat(char *argbuf) {
343 size_t total_octets = 0;
346 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
347 if (! POP3->msgs[i].deleted) {
349 total_octets += POP3->msgs[i].rfc822_length;
353 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
359 * RETR command (fetch a message)
361 void pop3_retr(char *argbuf) {
364 which_one = atoi(argbuf);
365 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
366 cprintf("-ERR No such message.\r\n");
370 if (POP3->msgs[which_one - 1].deleted) {
371 cprintf("-ERR Sorry, you deleted that message.\r\n");
375 cprintf("+OK Message %d:\r\n", which_one);
376 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
382 * TOP command (dumb way of fetching a partial message or headers-only)
384 void pop3_top(char *argbuf) {
386 int lines_requested = 0;
387 int lines_dumped = 0;
394 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
395 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
396 cprintf("-ERR No such message.\r\n");
400 if (POP3->msgs[which_one - 1].deleted) {
401 cprintf("-ERR Sorry, you deleted that message.\r\n");
405 CC->redirect_buffer = malloc(SIZ);
406 CC->redirect_len = 0;
407 CC->redirect_alloc = SIZ;
408 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
409 MT_RFC822, HEADERS_ALL, 0, 1, NULL);
410 msgtext = CC->redirect_buffer;
411 CC->redirect_buffer = NULL;
412 CC->redirect_len = 0;
413 CC->redirect_alloc = 0;
415 cprintf("+OK Message %d:\r\n", which_one);
419 while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
420 ( (*ptr != 0) && (done == 0))) {
423 if (lines_dumped >= lines_requested) {
427 if ((in_body == 0) || (done == 0)) {
428 client_write(buf, strlen(buf));
433 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
436 if (buf[strlen(buf)-1] != 10) cprintf("\n");
444 * DELE (delete message from mailbox)
446 void pop3_dele(char *argbuf) {
449 which_one = atoi(argbuf);
450 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
451 cprintf("-ERR No such message.\r\n");
455 if (POP3->msgs[which_one - 1].deleted) {
456 cprintf("-ERR You already deleted that message.\r\n");
460 /* Flag the message as deleted. Will expunge during QUIT command. */
461 POP3->msgs[which_one - 1].deleted = 1;
462 cprintf("+OK Message %d deleted.\r\n",
467 /* Perform "UPDATE state" stuff
469 void pop3_update(void) {
473 long *deletemsgs = NULL;
474 int num_deletemsgs = 0;
476 /* Remove messages marked for deletion */
477 if (POP3->num_msgs > 0) {
478 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
479 for (i=0; i<POP3->num_msgs; ++i) {
480 if (POP3->msgs[i].deleted) {
481 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
484 if (num_deletemsgs > 0) {
485 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
490 /* Set last read pointer */
491 if (POP3->num_msgs > 0) {
492 lgetuser(&CC->user, CC->curr_user);
494 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
495 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
496 POP3->msgs[POP3->num_msgs-1].msgnum);
497 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
506 * RSET (reset, i.e. undelete any deleted messages) command
508 void pop3_rset(char *argbuf) {
511 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
512 if (POP3->msgs[i].deleted) {
513 POP3->msgs[i].deleted = 0;
516 cprintf("+OK Reset completed.\r\n");
522 * LAST (Determine which message is the last unread message)
524 void pop3_last(char *argbuf) {
525 cprintf("+OK %d\r\n", POP3->lastseen + 1);
530 * CAPA is a command which tells the client which POP3 extensions
533 void pop3_capa(void) {
534 cprintf("+OK Capability list follows\r\n"
538 "IMPLEMENTATION %s\r\n"
548 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
549 * identifiers are simply the Citadel message numbers in the database.
551 void pop3_uidl(char *argbuf) {
555 which_one = atoi(argbuf);
557 /* "list one" mode */
559 if (which_one > POP3->num_msgs) {
560 cprintf("-ERR no such message, only %d are here\r\n",
564 else if (POP3->msgs[which_one-1].deleted) {
565 cprintf("-ERR Sorry, you deleted that message.\r\n");
569 cprintf("+OK %d %ld\r\n",
571 POP3->msgs[which_one-1].msgnum
577 /* "list all" (scan listing) mode */
579 cprintf("+OK Here's your mail:\r\n");
580 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
581 if (! POP3->msgs[i].deleted) {
582 cprintf("%d %ld\r\n",
584 POP3->msgs[i].msgnum);
593 * implements the STLS command (Citadel API version)
597 char ok_response[SIZ];
598 char nosup_response[SIZ];
599 char error_response[SIZ];
602 "+OK Begin TLS negotiation now\r\n");
603 sprintf(nosup_response,
604 "-ERR TLS not supported here\r\n");
605 sprintf(error_response,
606 "-ERR Internal error\r\n");
607 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
617 * Main command loop for POP3 sessions.
619 void pop3_command_loop(void) {
623 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
624 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
625 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
629 if (!strncasecmp(cmdbuf, "PASS", 4)) {
630 lprintf(CTDL_INFO, "POP3: PASS...\r\n");
633 lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
635 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
637 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
638 cprintf("+OK No operation.\r\n");
641 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
645 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
646 cprintf("+OK Goodbye...\r\n");
652 else if (!strncasecmp(cmdbuf, "USER", 4)) {
653 pop3_user(&cmdbuf[5]);
656 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
657 pop3_pass(&cmdbuf[5]);
660 else if (!strncasecmp(cmdbuf, "APOP", 4))
662 pop3_apop(&cmdbuf[5]);
666 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
671 else if (!CC->logged_in) {
672 cprintf("-ERR Not logged in.\r\n");
675 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
676 pop3_list(&cmdbuf[5]);
679 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
680 pop3_stat(&cmdbuf[5]);
683 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
684 pop3_retr(&cmdbuf[5]);
687 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
688 pop3_dele(&cmdbuf[5]);
691 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
692 pop3_rset(&cmdbuf[5]);
695 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
696 pop3_uidl(&cmdbuf[5]);
699 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
700 pop3_top(&cmdbuf[4]);
703 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
704 pop3_last(&cmdbuf[4]);
708 cprintf("-ERR I'm afraid I can't do that.\r\n");
713 const char *CitadelServicePop3="POP3";
714 const char *CitadelServicePop3S="POP3S";
717 CTDL_MODULE_INIT(pop3)
721 CtdlRegisterServiceHook(config.c_pop3_port,
728 CtdlRegisterServiceHook(config.c_pop3s_port,
733 CitadelServicePop3S);
735 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
738 /* return our Subversion id for the Log */