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) {
88 strcpy(CC->cs_clientname, "POP3 session");
89 gettimeofday(&tv, NULL);
90 memset(CC->cs_nonce, NONCE_SIZE, 0);
91 snprintf(CC->cs_nonce, NONCE_SIZE, "<%d%ld@%s>", rand(), tv.tv_usec, config.c_fqdn);
93 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
97 cprintf("+OK Welcome to the Citadel/UX POP3 server %s\r\n",
98 CC->cs_nonce, config.c_fqdn);
103 * Specify user name (implements POP3 "USER" command)
105 void pop3_user(char *argbuf) {
109 cprintf("-ERR You are already logged in.\r\n");
113 strcpy(username, argbuf);
116 lprintf(9, "Trying <%s>\n", username);
117 if (CtdlLoginExistingUser(username) == login_ok) {
118 cprintf("+OK Password required for %s\r\n", username);
121 cprintf("-ERR No such user.\r\n");
128 * Back end for pop3_grab_mailbox()
130 void pop3_add_message(long msgnum, void *userdata) {
132 lprintf(9, "in pop3_add_message()\n");
135 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
136 else POP3->msgs = reallok(POP3->msgs,
137 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
138 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
139 POP3->msgs[POP3->num_msgs-1].deleted = 0;
141 POP3->msgs[POP3->num_msgs-1].temp = fp;
143 CtdlRedirectOutput(fp, -1);
144 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
145 CtdlRedirectOutput(NULL, -1);
147 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
153 * Open the inbox and read its contents.
154 * (This should be called only once, by pop3_pass(), and returns the number
155 * of messages in the inbox, or -1 for error)
157 int pop3_grab_mailbox(void) {
161 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
163 /* Load up the messages */
164 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
165 pop3_add_message, NULL);
167 /* Figure out which are old and which are new */
168 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
169 POP3->lastseen = (-1);
170 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
171 if ((POP3->msgs[POP3->num_msgs-1].msgnum) <= vbuf.v_lastseen) {
176 return(POP3->num_msgs);
179 void pop3_login(void)
183 msgs = pop3_grab_mailbox();
185 cprintf("+OK %s is logged in (%d messages)\r\n",
186 CC->usersupp.fullname, msgs);
187 lprintf(9, "POP3 password login successful\n");
190 cprintf("-ERR Can't open your mailbox\r\n");
195 void pop3_apop(char *argbuf)
198 char userdigest[MD5_HEXSTRING_SIZE];
199 char realdigest[MD5_HEXSTRING_SIZE];
204 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
208 if ((sptr = strchr(argbuf, ' ')) == NULL)
210 cprintf("Invalid APOP line.\r\n");
216 while ((*sptr) && isspace(*sptr))
219 strncpy(username, argbuf, sizeof(username)-1);
220 username[sizeof(username)-1] = '\0';
222 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
223 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
225 if (CtdlLoginExistingUser(username) != login_ok)
227 cprintf("-ERR No such user.\r\n");
231 if (getuser(&CC->usersupp, CC->curr_user))
233 cprintf("-ERR No such user.\r\n");
237 make_apop_string(CC->usersupp.password, CC->cs_nonce, realdigest);
238 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
244 cprintf("-ERR That is NOT the password! Go away!\r\n");
250 * Authorize with password (implements POP3 "PASS" command)
252 void pop3_pass(char *argbuf) {
255 strcpy(password, argbuf);
258 lprintf(9, "Trying <%s>\n", password);
259 if (CtdlTryPassword(password) == pass_ok) {
263 cprintf("-ERR That is NOT the password! Go away!\r\n");
270 * list available msgs
272 void pop3_list(char *argbuf) {
276 which_one = atoi(argbuf);
278 /* "list one" mode */
280 if (which_one > POP3->num_msgs) {
281 cprintf("-ERR no such message, only %d are here\r\n",
285 else if (POP3->msgs[which_one-1].deleted) {
286 cprintf("-ERR Sorry, you deleted that message.\r\n");
290 cprintf("+OK %d %d\n",
292 POP3->msgs[which_one-1].rfc822_length
298 /* "list all" (scan listing) mode */
300 cprintf("+OK Here's your mail:\r\n");
301 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
302 if (! POP3->msgs[i].deleted) {
305 POP3->msgs[i].rfc822_length);
314 * STAT (tally up the total message count and byte count) command
316 void pop3_stat(char *argbuf) {
318 size_t total_octets = 0;
321 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
322 if (! POP3->msgs[i].deleted) {
324 total_octets += POP3->msgs[i].rfc822_length;
328 cprintf("+OK %d %d\n", total_msgs, total_octets);
334 * RETR command (fetch a message)
336 void pop3_retr(char *argbuf) {
339 size_t bytes_remaining;
341 which_one = atoi(argbuf);
342 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
343 cprintf("-ERR No such message.\r\n");
347 if (POP3->msgs[which_one - 1].deleted) {
348 cprintf("-ERR Sorry, you deleted that message.\r\n");
352 cprintf("+OK Whoop, there it is:\r\n");
353 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
354 rewind(POP3->msgs[which_one - 1].temp);
355 while (bytes_remaining-- > 0) {
356 ch = getc(POP3->msgs[which_one - 1].temp);
360 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
367 * TOP command (dumb way of fetching a partial message or headers-only)
369 void pop3_top(char *argbuf) {
371 int lines_requested = 0;
372 int lines_dumped = 0;
378 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
379 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
380 cprintf("-ERR No such message.\r\n");
384 if (POP3->msgs[which_one - 1].deleted) {
385 cprintf("-ERR Sorry, you deleted that message.\r\n");
389 cprintf("+OK Whoop, there it is:\r\n");
390 rewind(POP3->msgs[which_one - 1].temp);
391 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
392 ( (ptr!=NULL) && (done == 0))) {
394 if (lines_dumped >= lines_requested) done = 1;
395 if ((in_body == 0) || (done == 0))
396 client_write(buf, strlen(buf));
397 if (in_body) ++lines_dumped;
398 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
400 if (buf[strlen(buf)-1] != 10) cprintf("\n");
406 * DELE (delete message from mailbox)
408 void pop3_dele(char *argbuf) {
411 which_one = atoi(argbuf);
412 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
413 cprintf("-ERR No such message.\r\n");
417 if (POP3->msgs[which_one - 1].deleted) {
418 cprintf("-ERR You already deleted that message.\r\n");
422 /* Flag the message as deleted. Will expunge during QUIT command. */
423 POP3->msgs[which_one - 1].deleted = 1;
424 cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
429 /* Perform "UPDATE state" stuff
431 void pop3_update(void) {
435 /* Remove messages marked for deletion */
436 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
437 if (POP3->msgs[i].deleted) {
438 CtdlDeleteMessages(MAILROOM,
439 POP3->msgs[i].msgnum, "");
443 /* Set last read pointer */
444 if (POP3->num_msgs > 0) {
445 lgetuser(&CC->usersupp, CC->curr_user);
447 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
448 vbuf.v_lastseen = POP3->msgs[POP3->num_msgs-1].msgnum;
449 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
451 lputuser(&CC->usersupp);
458 * RSET (reset, i.e. undelete any deleted messages) command
460 void pop3_rset(char *argbuf) {
463 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
464 if (POP3->msgs[i].deleted) {
465 POP3->msgs[i].deleted = 0;
468 cprintf("+OK all that has come to pass, has now gone away.\r\n");
474 * LAST (Determine which message is the last unread message)
476 void pop3_last(char *argbuf) {
477 cprintf("+OK %d\r\n", POP3->lastseen + 1);
483 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
484 * identifiers are simply the Citadel message numbers in the database.
486 void pop3_uidl(char *argbuf) {
490 which_one = atoi(argbuf);
492 /* "list one" mode */
494 if (which_one > POP3->num_msgs) {
495 cprintf("-ERR no such message, only %d are here\r\n",
499 else if (POP3->msgs[which_one-1].deleted) {
500 cprintf("-ERR Sorry, you deleted that message.\r\n");
504 cprintf("+OK %d %ld\n",
506 POP3->msgs[which_one-1].msgnum
512 /* "list all" (scan listing) mode */
514 cprintf("+OK Here's your mail:\r\n");
515 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
516 if (! POP3->msgs[i].deleted) {
517 cprintf("%d %ld\r\n",
519 POP3->msgs[i].msgnum);
530 * Main command loop for POP3 sessions.
532 void pop3_command_loop(void) {
536 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
537 if (client_gets(cmdbuf) < 1) {
538 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
542 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
543 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
545 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
546 cprintf("+OK This command successfully did nothing.\r\n");
549 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
550 cprintf("+OK Goodbye...\r\n");
556 else if (!strncasecmp(cmdbuf, "USER", 4)) {
557 pop3_user(&cmdbuf[5]);
560 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
561 pop3_pass(&cmdbuf[5]);
564 else if (!strncasecmp(cmdbuf, "APOP", 4))
566 pop3_apop(&cmdbuf[5]);
569 else if (!CC->logged_in) {
570 cprintf("-ERR Not logged in.\r\n");
573 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
574 pop3_list(&cmdbuf[5]);
577 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
578 pop3_stat(&cmdbuf[5]);
581 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
582 pop3_retr(&cmdbuf[5]);
585 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
586 pop3_dele(&cmdbuf[5]);
589 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
590 pop3_rset(&cmdbuf[5]);
593 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
594 pop3_uidl(&cmdbuf[5]);
597 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
598 pop3_top(&cmdbuf[4]);
601 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
602 pop3_last(&cmdbuf[4]);
606 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
613 char *Dynamic_Module_Init(void)
615 SYM_POP3 = CtdlGetDynamicSymbol();
616 printf("Registering POP3 port %d\n", config.c_pop3_port);
617 CtdlRegisterServiceHook(config.c_pop3_port,
621 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);