4 * POP3 server for the Citadel/UX system
5 * Copyright (C) 1998-2000 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 * -> Nearly all of the optional commands in RFC1939 are also implemented.
13 * The only one missing is APOP, because it implements a "shared secret"
14 * method of authentication which would require some major changes to the
15 * Citadel server core.
17 * This is no longer true- APOP is implemented.
19 * -> The deprecated "LAST" command is included in this implementation, because
20 * there exist mail clients which insist on using it (such as Bynari
21 * TradeMail, and certain versions of Eudora).
33 #include <sys/types.h>
42 #include "sysdep_decls.h"
43 #include "citserver.h"
46 #include "dynloader.h"
53 #include "internet_addressing.h"
54 #include "serv_pop3.h"
61 * This cleanup function blows away the temporary memory and files used by
64 void pop3_cleanup_function(void) {
67 /* Don't do this stuff if this is not a POP3 session! */
68 if (CC->h_command_function != pop3_command_loop) return;
70 lprintf(9, "Performing POP3 cleanup hook\n");
72 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
73 fclose(POP3->msgs[i].temp);
75 if (POP3->msgs != NULL) phree(POP3->msgs);
77 lprintf(9, "Finished POP3 cleanup hook\n");
83 * Here's where our POP3 session begins its happy day.
85 void pop3_greeting(void) {
86 strcpy(CC->cs_clientname, "POP3 session");
88 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
92 cprintf("+OK Citadel/UX POP3 server %s\r\n",
93 CC->cs_nonce, config.c_fqdn);
98 * Specify user name (implements POP3 "USER" command)
100 void pop3_user(char *argbuf) {
104 cprintf("-ERR You are already logged in.\r\n");
108 strcpy(username, argbuf);
111 lprintf(9, "Trying <%s>\n", username);
112 if (CtdlLoginExistingUser(username) == login_ok) {
113 cprintf("+OK Password required for %s\r\n", username);
116 cprintf("-ERR No such user.\r\n");
123 * Back end for pop3_grab_mailbox()
125 void pop3_add_message(long msgnum, void *userdata) {
127 lprintf(9, "in pop3_add_message()\n");
130 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
131 else POP3->msgs = reallok(POP3->msgs,
132 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
133 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
134 POP3->msgs[POP3->num_msgs-1].deleted = 0;
136 POP3->msgs[POP3->num_msgs-1].temp = fp;
138 CtdlRedirectOutput(fp, -1);
139 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
140 CtdlRedirectOutput(NULL, -1);
142 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
148 * Open the inbox and read its contents.
149 * (This should be called only once, by pop3_pass(), and returns the number
150 * of messages in the inbox, or -1 for error)
152 int pop3_grab_mailbox(void) {
156 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
158 /* Load up the messages */
159 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
160 pop3_add_message, NULL);
162 /* Figure out which are old and which are new */
163 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
164 POP3->lastseen = (-1);
165 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
166 if ((POP3->msgs[POP3->num_msgs-1].msgnum) <= vbuf.v_lastseen) {
171 return(POP3->num_msgs);
174 void pop3_login(void)
178 msgs = pop3_grab_mailbox();
180 cprintf("+OK %s is logged in (%d messages)\r\n",
181 CC->usersupp.fullname, msgs);
182 lprintf(9, "POP3 password login successful\n");
185 cprintf("-ERR Can't open your mailbox\r\n");
190 void pop3_apop(char *argbuf)
193 char userdigest[MD5_HEXSTRING_SIZE];
194 char realdigest[MD5_HEXSTRING_SIZE];
199 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
203 if ((sptr = strchr(argbuf, ' ')) == NULL)
205 cprintf("Invalid APOP line.\r\n");
211 while ((*sptr) && isspace(*sptr))
214 strncpy(username, argbuf, sizeof(username)-1);
215 username[sizeof(username)-1] = '\0';
217 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
218 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
220 if (CtdlLoginExistingUser(username) != login_ok)
222 cprintf("-ERR No such user.\r\n");
226 if (getuser(&CC->usersupp, CC->curr_user))
228 cprintf("-ERR No such user.\r\n");
232 make_apop_string(CC->usersupp.password, CC->cs_nonce, realdigest);
233 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
239 cprintf("-ERR That is NOT the password.\r\n");
245 * Authorize with password (implements POP3 "PASS" command)
247 void pop3_pass(char *argbuf) {
250 strcpy(password, argbuf);
253 lprintf(9, "Trying <%s>\n", password);
254 if (CtdlTryPassword(password) == pass_ok) {
258 cprintf("-ERR That is NOT the password.\r\n");
265 * list available msgs
267 void pop3_list(char *argbuf) {
271 which_one = atoi(argbuf);
273 /* "list one" mode */
275 if (which_one > POP3->num_msgs) {
276 cprintf("-ERR no such message, only %d are here\r\n",
280 else if (POP3->msgs[which_one-1].deleted) {
281 cprintf("-ERR Sorry, you deleted that message.\r\n");
285 cprintf("+OK %d %d\r\n",
287 POP3->msgs[which_one-1].rfc822_length
293 /* "list all" (scan listing) mode */
295 cprintf("+OK Here's your mail:\r\n");
296 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
297 if (! POP3->msgs[i].deleted) {
300 POP3->msgs[i].rfc822_length);
309 * STAT (tally up the total message count and byte count) command
311 void pop3_stat(char *argbuf) {
313 size_t total_octets = 0;
316 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
317 if (! POP3->msgs[i].deleted) {
319 total_octets += POP3->msgs[i].rfc822_length;
323 cprintf("+OK %d %d\r\n", total_msgs, total_octets);
329 * RETR command (fetch a message)
331 void pop3_retr(char *argbuf) {
334 size_t bytes_remaining;
336 which_one = atoi(argbuf);
337 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
338 cprintf("-ERR No such message.\r\n");
342 if (POP3->msgs[which_one - 1].deleted) {
343 cprintf("-ERR Sorry, you deleted that message.\r\n");
347 cprintf("+OK Message %d:\r\n", which_one);
348 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
349 rewind(POP3->msgs[which_one - 1].temp);
350 while (bytes_remaining-- > 0) {
351 ch = getc(POP3->msgs[which_one - 1].temp);
355 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
362 * TOP command (dumb way of fetching a partial message or headers-only)
364 void pop3_top(char *argbuf) {
366 int lines_requested = 0;
367 int lines_dumped = 0;
373 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
374 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
375 cprintf("-ERR No such message.\r\n");
379 if (POP3->msgs[which_one - 1].deleted) {
380 cprintf("-ERR Sorry, you deleted that message.\r\n");
384 cprintf("+OK Message %d:\r\n", which_one);
385 rewind(POP3->msgs[which_one - 1].temp);
386 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
387 ( (ptr!=NULL) && (done == 0))) {
389 if (lines_dumped >= lines_requested) done = 1;
390 if ((in_body == 0) || (done == 0))
391 client_write(buf, strlen(buf));
392 if (in_body) ++lines_dumped;
393 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
395 if (buf[strlen(buf)-1] != 10) cprintf("\n");
401 * DELE (delete message from mailbox)
403 void pop3_dele(char *argbuf) {
406 which_one = atoi(argbuf);
407 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
408 cprintf("-ERR No such message.\r\n");
412 if (POP3->msgs[which_one - 1].deleted) {
413 cprintf("-ERR You already deleted that message.\r\n");
417 /* Flag the message as deleted. Will expunge during QUIT command. */
418 POP3->msgs[which_one - 1].deleted = 1;
419 cprintf("+OK Message %d deleted.\r\n",
424 /* Perform "UPDATE state" stuff
426 void pop3_update(void) {
430 /* Remove messages marked for deletion */
431 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
432 if (POP3->msgs[i].deleted) {
433 CtdlDeleteMessages(MAILROOM,
434 POP3->msgs[i].msgnum, "");
438 /* Set last read pointer */
439 if (POP3->num_msgs > 0) {
440 lgetuser(&CC->usersupp, CC->curr_user);
442 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
443 vbuf.v_lastseen = POP3->msgs[POP3->num_msgs-1].msgnum;
444 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
446 lputuser(&CC->usersupp);
453 * RSET (reset, i.e. undelete any deleted messages) command
455 void pop3_rset(char *argbuf) {
458 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
459 if (POP3->msgs[i].deleted) {
460 POP3->msgs[i].deleted = 0;
463 cprintf("+OK Reset completed.\r\n");
469 * LAST (Determine which message is the last unread message)
471 void pop3_last(char *argbuf) {
472 cprintf("+OK %d\r\n", POP3->lastseen + 1);
478 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
479 * identifiers are simply the Citadel message numbers in the database.
481 void pop3_uidl(char *argbuf) {
485 which_one = atoi(argbuf);
487 /* "list one" mode */
489 if (which_one > POP3->num_msgs) {
490 cprintf("-ERR no such message, only %d are here\r\n",
494 else if (POP3->msgs[which_one-1].deleted) {
495 cprintf("-ERR Sorry, you deleted that message.\r\n");
499 cprintf("+OK %d %ld\r\n",
501 POP3->msgs[which_one-1].msgnum
507 /* "list all" (scan listing) mode */
509 cprintf("+OK Here's your mail:\r\n");
510 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
511 if (! POP3->msgs[i].deleted) {
512 cprintf("%d %ld\r\n",
514 POP3->msgs[i].msgnum);
525 * Main command loop for POP3 sessions.
527 void pop3_command_loop(void) {
531 if (client_gets(&icmdbuf) < 1) {
532 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
536 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, icmdbuf);
537 while (strlen(icmdbuf) < 5) strcat(icmdbuf, " ");
539 if (!strncasecmp(icmdbuf, "NOOP", 4)) {
540 cprintf("+OK No operation.\r\n");
543 else if (!strncasecmp(icmdbuf, "QUIT", 4)) {
544 cprintf("+OK Goodbye...\r\n");
550 else if (!strncasecmp(icmdbuf, "USER", 4)) {
551 pop3_user(&icmdbuf[5]);
554 else if (!strncasecmp(icmdbuf, "PASS", 4)) {
555 pop3_pass(&icmdbuf[5]);
558 else if (!strncasecmp(icmdbuf, "APOP", 4))
560 pop3_apop(&icmdbuf[5]);
563 else if (!CC->logged_in) {
564 cprintf("-ERR Not logged in.\r\n");
567 else if (!strncasecmp(icmdbuf, "LIST", 4)) {
568 pop3_list(&icmdbuf[5]);
571 else if (!strncasecmp(icmdbuf, "STAT", 4)) {
572 pop3_stat(&icmdbuf[5]);
575 else if (!strncasecmp(icmdbuf, "RETR", 4)) {
576 pop3_retr(&icmdbuf[5]);
579 else if (!strncasecmp(icmdbuf, "DELE", 4)) {
580 pop3_dele(&icmdbuf[5]);
583 else if (!strncasecmp(icmdbuf, "RSET", 4)) {
584 pop3_rset(&icmdbuf[5]);
587 else if (!strncasecmp(icmdbuf, "UIDL", 4)) {
588 pop3_uidl(&icmdbuf[5]);
591 else if (!strncasecmp(icmdbuf, "TOP", 3)) {
592 pop3_top(&icmdbuf[4]);
595 else if (!strncasecmp(icmdbuf, "LAST", 4)) {
596 pop3_last(&icmdbuf[4]);
600 cprintf("500 I'm afraid I can't do that.\r\n");
607 char *Dynamic_Module_Init(void)
609 SYM_POP3 = CtdlGetDynamicSymbol();
610 CtdlRegisterServiceHook(config.c_pop3_port,
614 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);