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);
107 * Specify user name (implements POP3 "USER" command)
109 void pop3_user(char *argbuf) {
113 cprintf("-ERR You are already logged in.\r\n");
117 strcpy(username, argbuf);
120 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", username); */
121 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
122 cprintf("+OK Password required for %s\r\n", username);
125 cprintf("-ERR No such user.\r\n");
132 * Back end for pop3_grab_mailbox()
134 void pop3_add_message(long msgnum, void *userdata) {
138 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
139 else POP3->msgs = realloc(POP3->msgs,
140 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
141 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
142 POP3->msgs[POP3->num_msgs-1].deleted = 0;
144 /* We need to know the length of this message when it is printed in
145 * RFC822 format. Perhaps we have cached this length in the message's
146 * metadata record. If so, great; if not, measure it and then cache
149 GetMetaData(&smi, msgnum);
150 if (smi.meta_rfc822_length <= 0L) {
151 CC->redirect_buffer = malloc(SIZ);
152 CC->redirect_len = 0;
153 CC->redirect_alloc = SIZ;
154 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
155 smi.meta_rfc822_length = CC->redirect_len;
156 free(CC->redirect_buffer);
157 CC->redirect_buffer = NULL;
158 CC->redirect_len = 0;
159 CC->redirect_alloc = 0;
162 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
168 * Open the inbox and read its contents.
169 * (This should be called only once, by pop3_pass(), and returns the number
170 * of messages in the inbox, or -1 for error)
172 int pop3_grab_mailbox(void) {
176 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
178 /* Load up the messages */
179 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
180 pop3_add_message, NULL);
182 /* Figure out which are old and which are new */
183 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
184 POP3->lastseen = (-1);
185 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
186 if (is_msg_in_sequence_set(vbuf.v_seen,
187 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
192 return(POP3->num_msgs);
195 void pop3_login(void)
199 msgs = pop3_grab_mailbox();
201 cprintf("+OK %s is logged in (%d messages)\r\n",
202 CC->user.fullname, msgs);
203 lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
206 cprintf("-ERR Can't open your mailbox\r\n");
211 void pop3_apop(char *argbuf)
214 char userdigest[MD5_HEXSTRING_SIZE];
215 char realdigest[MD5_HEXSTRING_SIZE];
220 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
224 if ((sptr = strchr(argbuf, ' ')) == NULL)
226 cprintf("-ERR Invalid APOP line.\r\n");
232 while ((*sptr) && isspace(*sptr))
235 strncpy(username, argbuf, sizeof(username)-1);
236 username[sizeof(username)-1] = '\0';
238 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
239 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
241 if (CtdlLoginExistingUser(NULL, username) != login_ok)
243 cprintf("-ERR No such user.\r\n");
247 if (getuser(&CC->user, CC->curr_user))
249 cprintf("-ERR No such user.\r\n");
253 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
254 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
261 cprintf("-ERR That is NOT the password.\r\n");
267 * Authorize with password (implements POP3 "PASS" command)
269 void pop3_pass(char *argbuf) {
272 strcpy(password, argbuf);
275 /* lprintf(CTDL_DEBUG, "Trying <%s>\n", password); */
276 if (CtdlTryPassword(password) == pass_ok) {
280 cprintf("-ERR That is NOT the password.\r\n");
287 * list available msgs
289 void pop3_list(char *argbuf) {
293 which_one = atoi(argbuf);
295 /* "list one" mode */
297 if (which_one > POP3->num_msgs) {
298 cprintf("-ERR no such message, only %d are here\r\n",
302 else if (POP3->msgs[which_one-1].deleted) {
303 cprintf("-ERR Sorry, you deleted that message.\r\n");
307 cprintf("+OK %d %ld\r\n",
309 (long)POP3->msgs[which_one-1].rfc822_length
315 /* "list all" (scan listing) mode */
317 cprintf("+OK Here's your mail:\r\n");
318 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
319 if (! POP3->msgs[i].deleted) {
320 cprintf("%d %ld\r\n",
322 (long)POP3->msgs[i].rfc822_length);
331 * STAT (tally up the total message count and byte count) command
333 void pop3_stat(char *argbuf) {
335 size_t total_octets = 0;
338 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
339 if (! POP3->msgs[i].deleted) {
341 total_octets += POP3->msgs[i].rfc822_length;
345 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
351 * RETR command (fetch a message)
353 void pop3_retr(char *argbuf) {
356 which_one = atoi(argbuf);
357 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
358 cprintf("-ERR No such message.\r\n");
362 if (POP3->msgs[which_one - 1].deleted) {
363 cprintf("-ERR Sorry, you deleted that message.\r\n");
367 cprintf("+OK Message %d:\r\n", which_one);
368 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
374 * TOP command (dumb way of fetching a partial message or headers-only)
376 void pop3_top(char *argbuf) {
378 int lines_requested = 0;
379 int lines_dumped = 0;
386 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
387 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
388 cprintf("-ERR No such message.\r\n");
392 if (POP3->msgs[which_one - 1].deleted) {
393 cprintf("-ERR Sorry, you deleted that message.\r\n");
397 CC->redirect_buffer = malloc(SIZ);
398 CC->redirect_len = 0;
399 CC->redirect_alloc = SIZ;
400 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
401 MT_RFC822, HEADERS_ALL, 0, 1, NULL);
402 msgtext = CC->redirect_buffer;
403 CC->redirect_buffer = NULL;
404 CC->redirect_len = 0;
405 CC->redirect_alloc = 0;
407 cprintf("+OK Message %d:\r\n", which_one);
411 while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
412 ( (*ptr != 0) && (done == 0))) {
415 if (lines_dumped >= lines_requested) {
419 if ((in_body == 0) || (done == 0)) {
420 client_write(buf, strlen(buf));
425 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
428 if (buf[strlen(buf)-1] != 10) cprintf("\n");
436 * DELE (delete message from mailbox)
438 void pop3_dele(char *argbuf) {
441 which_one = atoi(argbuf);
442 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
443 cprintf("-ERR No such message.\r\n");
447 if (POP3->msgs[which_one - 1].deleted) {
448 cprintf("-ERR You already deleted that message.\r\n");
452 /* Flag the message as deleted. Will expunge during QUIT command. */
453 POP3->msgs[which_one - 1].deleted = 1;
454 cprintf("+OK Message %d deleted.\r\n",
459 /* Perform "UPDATE state" stuff
461 void pop3_update(void) {
465 long *deletemsgs = NULL;
466 int num_deletemsgs = 0;
468 /* Remove messages marked for deletion */
469 if (POP3->num_msgs > 0) {
470 deletemsgs = malloc(POP3->num_msgs * sizeof(long));
471 for (i=0; i<POP3->num_msgs; ++i) {
472 if (POP3->msgs[i].deleted) {
473 deletemsgs[num_deletemsgs++] = POP3->msgs[i].msgnum;
476 if (num_deletemsgs > 0) {
477 CtdlDeleteMessages(MAILROOM, deletemsgs, num_deletemsgs, "");
482 /* Set last read pointer */
483 if (POP3->num_msgs > 0) {
484 lgetuser(&CC->user, CC->curr_user);
486 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
487 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
488 POP3->msgs[POP3->num_msgs-1].msgnum);
489 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
498 * RSET (reset, i.e. undelete any deleted messages) command
500 void pop3_rset(char *argbuf) {
503 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
504 if (POP3->msgs[i].deleted) {
505 POP3->msgs[i].deleted = 0;
508 cprintf("+OK Reset completed.\r\n");
514 * LAST (Determine which message is the last unread message)
516 void pop3_last(char *argbuf) {
517 cprintf("+OK %d\r\n", POP3->lastseen + 1);
522 * CAPA is a command which tells the client which POP3 extensions
525 void pop3_capa(void) {
526 cprintf("+OK Capability list follows\r\n"
530 "IMPLEMENTATION %s\r\n"
540 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
541 * identifiers are simply the Citadel message numbers in the database.
543 void pop3_uidl(char *argbuf) {
547 which_one = atoi(argbuf);
549 /* "list one" mode */
551 if (which_one > POP3->num_msgs) {
552 cprintf("-ERR no such message, only %d are here\r\n",
556 else if (POP3->msgs[which_one-1].deleted) {
557 cprintf("-ERR Sorry, you deleted that message.\r\n");
561 cprintf("+OK %d %ld\r\n",
563 POP3->msgs[which_one-1].msgnum
569 /* "list all" (scan listing) mode */
571 cprintf("+OK Here's your mail:\r\n");
572 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
573 if (! POP3->msgs[i].deleted) {
574 cprintf("%d %ld\r\n",
576 POP3->msgs[i].msgnum);
585 * implements the STLS command (Citadel API version)
589 char ok_response[SIZ];
590 char nosup_response[SIZ];
591 char error_response[SIZ];
594 "+OK Begin TLS negotiation now\r\n");
595 sprintf(nosup_response,
596 "-ERR TLS not supported here\r\n");
597 sprintf(error_response,
598 "-ERR Internal error\r\n");
599 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
609 * Main command loop for POP3 sessions.
611 void pop3_command_loop(void) {
615 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
616 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
617 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
621 if (!strncasecmp(cmdbuf, "PASS", 4)) {
622 lprintf(CTDL_INFO, "POP3: PASS...\r\n");
625 lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
627 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
629 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
630 cprintf("+OK No operation.\r\n");
633 else if (!strncasecmp(cmdbuf, "CAPA", 4)) {
637 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
638 cprintf("+OK Goodbye...\r\n");
644 else if (!strncasecmp(cmdbuf, "USER", 4)) {
645 pop3_user(&cmdbuf[5]);
648 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
649 pop3_pass(&cmdbuf[5]);
652 else if (!strncasecmp(cmdbuf, "APOP", 4))
654 pop3_apop(&cmdbuf[5]);
658 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
663 else if (!CC->logged_in) {
664 cprintf("-ERR Not logged in.\r\n");
667 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
668 pop3_list(&cmdbuf[5]);
671 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
672 pop3_stat(&cmdbuf[5]);
675 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
676 pop3_retr(&cmdbuf[5]);
679 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
680 pop3_dele(&cmdbuf[5]);
683 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
684 pop3_rset(&cmdbuf[5]);
687 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
688 pop3_uidl(&cmdbuf[5]);
691 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
692 pop3_top(&cmdbuf[4]);
695 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
696 pop3_last(&cmdbuf[4]);
700 cprintf("-ERR I'm afraid I can't do that.\r\n");
705 const char *CitadelServicePop3="POP3";
706 const char *CitadelServicePop3S="POP3S";
709 CTDL_MODULE_INIT(pop3)
711 CtdlRegisterServiceHook(config.c_pop3_port,
718 CtdlRegisterServiceHook(config.c_pop3s_port,
723 CitadelServicePop3S);
725 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
727 /* return our Subversion id for the Log */