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) {
143 if (POP3->num_msgs < 2) POP3->msgs = malloc(sizeof(struct pop3msg));
144 else POP3->msgs = realloc(POP3->msgs,
145 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
146 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
147 POP3->msgs[POP3->num_msgs-1].deleted = 0;
149 /* We need to know the length of this message when it is printed in
150 * RFC822 format. Perhaps we have cached this length in the message's
151 * metadata record. If so, great; if not, measure it and then cache
154 GetMetaData(&smi, POP3->num_msgs-1);
155 if (smi.meta_rfc822_length <= 0L) {
157 CtdlRedirectOutput(fp, -1);
158 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1);
159 CtdlRedirectOutput(NULL, -1);
160 smi.meta_rfc822_length = ftell(fp);
164 POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
170 * Open the inbox and read its contents.
171 * (This should be called only once, by pop3_pass(), and returns the number
172 * of messages in the inbox, or -1 for error)
174 int pop3_grab_mailbox(void) {
178 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
180 /* Load up the messages */
181 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
182 pop3_add_message, NULL);
184 /* Figure out which are old and which are new */
185 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
186 POP3->lastseen = (-1);
187 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
188 if (is_msg_in_mset(vbuf.v_seen,
189 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
194 return(POP3->num_msgs);
197 void pop3_login(void)
201 msgs = pop3_grab_mailbox();
203 cprintf("+OK %s is logged in (%d messages)\r\n",
204 CC->user.fullname, msgs);
205 lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
208 cprintf("-ERR Can't open your mailbox\r\n");
213 void pop3_apop(char *argbuf)
216 char userdigest[MD5_HEXSTRING_SIZE];
217 char realdigest[MD5_HEXSTRING_SIZE];
222 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
226 if ((sptr = strchr(argbuf, ' ')) == NULL)
228 cprintf("-ERR Invalid APOP line.\r\n");
234 while ((*sptr) && isspace(*sptr))
237 strncpy(username, argbuf, sizeof(username)-1);
238 username[sizeof(username)-1] = '\0';
240 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
241 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
243 if (CtdlLoginExistingUser(username) != login_ok)
245 cprintf("-ERR No such user.\r\n");
249 if (getuser(&CC->user, CC->curr_user))
251 cprintf("-ERR No such user.\r\n");
255 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
256 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
263 cprintf("-ERR That is NOT the password.\r\n");
269 * Authorize with password (implements POP3 "PASS" command)
271 void pop3_pass(char *argbuf) {
274 strcpy(password, argbuf);
277 lprintf(CTDL_DEBUG, "Trying <%s>\n", password);
278 if (CtdlTryPassword(password) == pass_ok) {
282 cprintf("-ERR That is NOT the password.\r\n");
289 * list available msgs
291 void pop3_list(char *argbuf) {
295 which_one = atoi(argbuf);
297 /* "list one" mode */
299 if (which_one > POP3->num_msgs) {
300 cprintf("-ERR no such message, only %d are here\r\n",
304 else if (POP3->msgs[which_one-1].deleted) {
305 cprintf("-ERR Sorry, you deleted that message.\r\n");
309 cprintf("+OK %d %ld\r\n",
311 (long)POP3->msgs[which_one-1].rfc822_length
317 /* "list all" (scan listing) mode */
319 cprintf("+OK Here's your mail:\r\n");
320 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
321 if (! POP3->msgs[i].deleted) {
322 cprintf("%d %ld\r\n",
324 (long)POP3->msgs[i].rfc822_length);
333 * STAT (tally up the total message count and byte count) command
335 void pop3_stat(char *argbuf) {
337 size_t total_octets = 0;
340 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
341 if (! POP3->msgs[i].deleted) {
343 total_octets += POP3->msgs[i].rfc822_length;
347 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
353 * RETR command (fetch a message)
355 void pop3_retr(char *argbuf) {
358 which_one = atoi(argbuf);
359 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
360 cprintf("-ERR No such message.\r\n");
364 if (POP3->msgs[which_one - 1].deleted) {
365 cprintf("-ERR Sorry, you deleted that message.\r\n");
369 cprintf("+OK Message %d:\r\n", which_one);
370 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1);
376 * TOP command (dumb way of fetching a partial message or headers-only)
378 void pop3_top(char *argbuf) {
380 int lines_requested = 0;
381 int lines_dumped = 0;
388 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
389 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
390 cprintf("-ERR No such message.\r\n");
394 if (POP3->msgs[which_one - 1].deleted) {
395 cprintf("-ERR Sorry, you deleted that message.\r\n");
401 cprintf("-ERR Internal error: could not create temp file\r\n");
404 CtdlRedirectOutput(fp, -1);
405 CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1);
406 CtdlRedirectOutput(NULL, -1);
408 cprintf("+OK Message %d:\r\n", which_one);
410 while (ptr = fgets(buf, sizeof buf, fp),
411 ( (ptr!=NULL) && (done == 0))) {
413 if (lines_dumped >= lines_requested) done = 1;
414 if ((in_body == 0) || (done == 0))
415 client_write(buf, strlen(buf));
416 if (in_body) ++lines_dumped;
417 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
419 if (buf[strlen(buf)-1] != 10) cprintf("\n");
426 * DELE (delete message from mailbox)
428 void pop3_dele(char *argbuf) {
431 which_one = atoi(argbuf);
432 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
433 cprintf("-ERR No such message.\r\n");
437 if (POP3->msgs[which_one - 1].deleted) {
438 cprintf("-ERR You already deleted that message.\r\n");
442 /* Flag the message as deleted. Will expunge during QUIT command. */
443 POP3->msgs[which_one - 1].deleted = 1;
444 cprintf("+OK Message %d deleted.\r\n",
449 /* Perform "UPDATE state" stuff
451 void pop3_update(void) {
455 /* Remove messages marked for deletion */
456 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
457 if (POP3->msgs[i].deleted) {
458 CtdlDeleteMessages(MAILROOM,
459 POP3->msgs[i].msgnum, "");
463 /* Set last read pointer */
464 if (POP3->num_msgs > 0) {
465 lgetuser(&CC->user, CC->curr_user);
467 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
468 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
469 POP3->msgs[POP3->num_msgs-1].msgnum);
470 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
479 * RSET (reset, i.e. undelete any deleted messages) command
481 void pop3_rset(char *argbuf) {
484 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
485 if (POP3->msgs[i].deleted) {
486 POP3->msgs[i].deleted = 0;
489 cprintf("+OK Reset completed.\r\n");
495 * LAST (Determine which message is the last unread message)
497 void pop3_last(char *argbuf) {
498 cprintf("+OK %d\r\n", POP3->lastseen + 1);
504 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
505 * identifiers are simply the Citadel message numbers in the database.
507 void pop3_uidl(char *argbuf) {
511 which_one = atoi(argbuf);
513 /* "list one" mode */
515 if (which_one > POP3->num_msgs) {
516 cprintf("-ERR no such message, only %d are here\r\n",
520 else if (POP3->msgs[which_one-1].deleted) {
521 cprintf("-ERR Sorry, you deleted that message.\r\n");
525 cprintf("+OK %d %ld\r\n",
527 POP3->msgs[which_one-1].msgnum
533 /* "list all" (scan listing) mode */
535 cprintf("+OK Here's your mail:\r\n");
536 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
537 if (! POP3->msgs[i].deleted) {
538 cprintf("%d %ld\r\n",
540 POP3->msgs[i].msgnum);
549 * implements the STLS command (Citadel API version)
554 char ok_response[SIZ];
555 char nosup_response[SIZ];
556 char error_response[SIZ];
559 "+OK Begin TLS negotiation now\r\n");
560 sprintf(nosup_response,
561 "-ERR TLS not supported here\r\n");
562 sprintf(error_response,
563 "-ERR Internal error\r\n");
564 CtdlStartTLS(ok_response, nosup_response, error_response);
575 * Main command loop for POP3 sessions.
577 void pop3_command_loop(void) {
581 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
582 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
583 lprintf(CTDL_ERR, "POP3 socket is broken. Ending session.\r\n");
587 lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
588 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
590 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
591 cprintf("+OK No operation.\r\n");
594 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
595 cprintf("+OK Goodbye...\r\n");
601 else if (!strncasecmp(cmdbuf, "USER", 4)) {
602 pop3_user(&cmdbuf[5]);
605 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
606 pop3_pass(&cmdbuf[5]);
609 else if (!strncasecmp(cmdbuf, "APOP", 4))
611 pop3_apop(&cmdbuf[5]);
615 else if (!strncasecmp(cmdbuf, "STLS", 4)) {
620 else if (!CC->logged_in) {
621 cprintf("-ERR Not logged in.\r\n");
624 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
625 pop3_list(&cmdbuf[5]);
628 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
629 pop3_stat(&cmdbuf[5]);
632 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
633 pop3_retr(&cmdbuf[5]);
636 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
637 pop3_dele(&cmdbuf[5]);
640 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
641 pop3_rset(&cmdbuf[5]);
644 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
645 pop3_uidl(&cmdbuf[5]);
648 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
649 pop3_top(&cmdbuf[4]);
652 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
653 pop3_last(&cmdbuf[4]);
657 cprintf("-ERR I'm afraid I can't do that.\r\n");
664 char *serv_pop3_init(void)
666 CtdlRegisterServiceHook(config.c_pop3_port,
672 CtdlRegisterServiceHook(config.c_pop3s_port,
678 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);