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.
12 * -> All optional POP3 commands described in RFC1939 are also implemented.
14 * -> The deprecated "LAST" command is included in this implementation, because
15 * there exist mail clients which insist on using it (such as Bynari
16 * TradeMail, and certain versions of Eudora).
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
47 #include "sysdep_decls.h"
48 #include "citserver.h"
51 #include "serv_extensions.h"
58 #include "internet_addressing.h"
59 #include "serv_pop3.h"
63 #include "serv_crypto.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);
84 * Here's where our POP3 session begins its happy day.
86 void pop3_greeting(void) {
87 strcpy(CC->cs_clientname, "POP3 session");
89 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
93 cprintf("+OK Citadel POP3 server %s\r\n",
99 * POP3S is just like POP3, except it goes crypto right away.
102 void pop3s_greeting(void) {
103 CtdlStartTLS(NULL, NULL, NULL);
111 * Specify user name (implements POP3 "USER" command)
113 void pop3_user(char *argbuf) {
117 cprintf("-ERR You are already logged in.\r\n");
121 strcpy(username, argbuf);
124 lprintf(CTDL_DEBUG, "Trying <%s>\n", username);
125 if (CtdlLoginExistingUser(username) == login_ok) {
126 cprintf("+OK Password required for %s\r\n", username);
129 cprintf("-ERR No such user.\r\n");
136 * Back end for pop3_grab_mailbox()
138 void pop3_add_message(long msgnum, void *userdata) {
142 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
143 else POP3->msgs = realloc(POP3->msgs,
144 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
145 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
146 POP3->msgs[POP3->num_msgs-1].deleted = 0;
148 /* We need to know the length of this message when it is printed in
149 * RFC822 format. Perhaps we have cached this length in the message's
150 * metadata record. If so, great; if not, measure it and then cache
153 GetMetaData(&smi, POP3->num_msgs-1);
154 if (smi.meta_rfc822_length <= 0L) {
155 CC->redirect_buffer = malloc(SIZ);
156 CC->redirect_len = 0;
157 CC->redirect_alloc = SIZ;
158 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1);
159 smi.meta_rfc822_length = CC->redirect_len;
160 free(CC->redirect_buffer);
161 CC->redirect_buffer = NULL;
162 CC->redirect_len = 0;
163 CC->redirect_alloc = 0;
166 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
172 * Open the inbox and read its contents.
173 * (This should be called only once, by pop3_pass(), and returns the number
174 * of messages in the inbox, or -1 for error)
176 int pop3_grab_mailbox(void) {
180 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
182 /* Load up the messages */
183 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
184 pop3_add_message, NULL);
186 /* Figure out which are old and which are new */
187 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
188 POP3->lastseen = (-1);
189 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
190 if (is_msg_in_mset(vbuf.v_seen,
191 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
196 return(POP3->num_msgs);
199 void pop3_login(void)
203 msgs = pop3_grab_mailbox();
205 cprintf("+OK %s is logged in (%d messages)\r\n",
206 CC->user.fullname, msgs);
207 lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
210 cprintf("-ERR Can't open your mailbox\r\n");
215 void pop3_apop(char *argbuf)
218 char userdigest[MD5_HEXSTRING_SIZE];
219 char realdigest[MD5_HEXSTRING_SIZE];
224 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
228 if ((sptr = strchr(argbuf, ' ')) == NULL)
230 cprintf("-ERR Invalid APOP line.\r\n");
236 while ((*sptr) && isspace(*sptr))
239 strncpy(username, argbuf, sizeof(username)-1);
240 username[sizeof(username)-1] = '\0';
242 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
243 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
245 if (CtdlLoginExistingUser(username) != login_ok)
247 cprintf("-ERR No such user.\r\n");
251 if (getuser(&CC->user, CC->curr_user))
253 cprintf("-ERR No such user.\r\n");
257 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
258 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
265 cprintf("-ERR That is NOT the password.\r\n");
271 * Authorize with password (implements POP3 "PASS" command)
273 void pop3_pass(char *argbuf) {
276 strcpy(password, argbuf);
279 lprintf(CTDL_DEBUG, "Trying <%s>\n", password);
280 if (CtdlTryPassword(password) == pass_ok) {
284 cprintf("-ERR That is NOT the password.\r\n");
291 * list available msgs
293 void pop3_list(char *argbuf) {
297 which_one = atoi(argbuf);
299 /* "list one" mode */
301 if (which_one > POP3->num_msgs) {
302 cprintf("-ERR no such message, only %d are here\r\n",
306 else if (POP3->msgs[which_one-1].deleted) {
307 cprintf("-ERR Sorry, you deleted that message.\r\n");
311 cprintf("+OK %d %ld\r\n",
313 (long)POP3->msgs[which_one-1].rfc822_length
319 /* "list all" (scan listing) mode */
321 cprintf("+OK Here's your mail:\r\n");
322 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
323 if (! POP3->msgs[i].deleted) {
324 cprintf("%d %ld\r\n",
326 (long)POP3->msgs[i].rfc822_length);
335 * STAT (tally up the total message count and byte count) command
337 void pop3_stat(char *argbuf) {
339 size_t total_octets = 0;
342 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
343 if (! POP3->msgs[i].deleted) {
345 total_octets += POP3->msgs[i].rfc822_length;
349 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
355 * RETR command (fetch a message)
357 void pop3_retr(char *argbuf) {
360 which_one = atoi(argbuf);
361 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
362 cprintf("-ERR No such message.\r\n");
366 if (POP3->msgs[which_one - 1].deleted) {
367 cprintf("-ERR Sorry, you deleted that message.\r\n");
371 cprintf("+OK Message %d:\r\n", which_one);
372 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1);
378 * TOP command (dumb way of fetching a partial message or headers-only)
380 void pop3_top(char *argbuf) {
382 int lines_requested = 0;
383 int lines_dumped = 0;
390 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
391 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
392 cprintf("-ERR No such message.\r\n");
396 if (POP3->msgs[which_one - 1].deleted) {
397 cprintf("-ERR Sorry, you deleted that message.\r\n");
401 CC->redirect_buffer = malloc(SIZ);
402 CC->redirect_len = 0;
403 CC->redirect_alloc = SIZ;
404 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
405 MT_RFC822, HEADERS_ALL, 0, 1);
406 CC->redirect_buffer[CC->redirect_len] = 0;
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!=NULL) && (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 /* Remove messages marked for deletion */
471 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
472 if (POP3->msgs[i].deleted) {
473 CtdlDeleteMessages(MAILROOM,
474 POP3->msgs[i].msgnum, "");
478 /* Set last read pointer */
479 if (POP3->num_msgs > 0) {
480 lgetuser(&CC->user, CC->curr_user);
482 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
483 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
484 POP3->msgs[POP3->num_msgs-1].msgnum);
485 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
494 * RSET (reset, i.e. undelete any deleted messages) command
496 void pop3_rset(char *argbuf) {
499 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
500 if (POP3->msgs[i].deleted) {
501 POP3->msgs[i].deleted = 0;
504 cprintf("+OK Reset completed.\r\n");
510 * LAST (Determine which message is the last unread message)
512 void pop3_last(char *argbuf) {
513 cprintf("+OK %d\r\n", POP3->lastseen + 1);
519 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
520 * identifiers are simply the Citadel message numbers in the database.
522 void pop3_uidl(char *argbuf) {
526 which_one = atoi(argbuf);
528 /* "list one" mode */
530 if (which_one > POP3->num_msgs) {
531 cprintf("-ERR no such message, only %d are here\r\n",
535 else if (POP3->msgs[which_one-1].deleted) {
536 cprintf("-ERR Sorry, you deleted that message.\r\n");
540 cprintf("+OK %d %ld\r\n",
542 POP3->msgs[which_one-1].msgnum
548 /* "list all" (scan listing) mode */
550 cprintf("+OK Here's your mail:\r\n");
551 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
552 if (! POP3->msgs[i].deleted) {
553 cprintf("%d %ld\r\n",
555 POP3->msgs[i].msgnum);
564 * implements the STLS command (Citadel API version)
569 char ok_response[SIZ];
570 char nosup_response[SIZ];
571 char error_response[SIZ];
574 "+OK Begin TLS negotiation now\r\n");
575 sprintf(nosup_response,
576 "-ERR TLS not supported here\r\n");
577 sprintf(error_response,
578 "-ERR Internal error\r\n");
579 CtdlStartTLS(ok_response, nosup_response, error_response);
590 * Main command loop for POP3 sessions.
592 void pop3_command_loop(void) {
596 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
597 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
598 lprintf(CTDL_ERR, "POP3 socket is broken. Ending session.\r\n");
602 lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
603 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
605 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
606 cprintf("+OK No operation.\r\n");
609 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
610 cprintf("+OK Goodbye...\r\n");
616 else if (!strncasecmp(cmdbuf, "USER", 4)) {
617 pop3_user(&cmdbuf[5]);
620 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
621 pop3_pass(&cmdbuf[5]);
624 else if (!strncasecmp(cmdbuf, "APOP", 4))
626 pop3_apop(&cmdbuf[5]);
630 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
635 else if (!CC->logged_in) {
636 cprintf("-ERR Not logged in.\r\n");
639 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
640 pop3_list(&cmdbuf[5]);
643 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
644 pop3_stat(&cmdbuf[5]);
647 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
648 pop3_retr(&cmdbuf[5]);
651 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
652 pop3_dele(&cmdbuf[5]);
655 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
656 pop3_rset(&cmdbuf[5]);
659 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
660 pop3_uidl(&cmdbuf[5]);
663 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
664 pop3_top(&cmdbuf[4]);
667 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
668 pop3_last(&cmdbuf[4]);
672 cprintf("-ERR I'm afraid I can't do that.\r\n");
679 char *serv_pop3_init(void)
681 CtdlRegisterServiceHook(config.c_pop3_port,
687 CtdlRegisterServiceHook(config.c_pop3s_port,
693 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);