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>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
56 #include "dynloader.h"
63 #include "internet_addressing.h"
64 #include "serv_pop3.h"
71 * This cleanup function blows away the temporary memory and files used by
74 void pop3_cleanup_function(void) {
77 /* Don't do this stuff if this is not a POP3 session! */
78 if (CC->h_command_function != pop3_command_loop) return;
80 lprintf(9, "Performing POP3 cleanup hook\n");
82 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
83 fclose(POP3->msgs[i].temp);
85 if (POP3->msgs != NULL) phree(POP3->msgs);
87 lprintf(9, "Finished POP3 cleanup hook\n");
93 * Here's where our POP3 session begins its happy day.
95 void pop3_greeting(void) {
96 strcpy(CC->cs_clientname, "POP3 session");
98 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
102 cprintf("+OK Citadel/UX POP3 server %s\r\n",
103 CC->cs_nonce, config.c_fqdn);
108 * Specify user name (implements POP3 "USER" command)
110 void pop3_user(char *argbuf) {
114 cprintf("-ERR You are already logged in.\r\n");
118 strcpy(username, argbuf);
121 lprintf(9, "Trying <%s>\n", username);
122 if (CtdlLoginExistingUser(username) == login_ok) {
123 cprintf("+OK Password required for %s\r\n", username);
126 cprintf("-ERR No such user.\r\n");
133 * Back end for pop3_grab_mailbox()
135 void pop3_add_message(long msgnum, void *userdata) {
137 lprintf(9, "in pop3_add_message()\n");
140 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
141 else POP3->msgs = reallok(POP3->msgs,
142 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
143 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
144 POP3->msgs[POP3->num_msgs-1].deleted = 0;
146 POP3->msgs[POP3->num_msgs-1].temp = fp;
148 CtdlRedirectOutput(fp, -1);
149 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
150 CtdlRedirectOutput(NULL, -1);
152 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
158 * Open the inbox and read its contents.
159 * (This should be called only once, by pop3_pass(), and returns the number
160 * of messages in the inbox, or -1 for error)
162 int pop3_grab_mailbox(void) {
166 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
168 /* Load up the messages */
169 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
170 pop3_add_message, NULL);
172 /* Figure out which are old and which are new */
173 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
174 POP3->lastseen = (-1);
175 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
176 if (is_msg_in_mset(vbuf.v_seen,
177 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
182 return(POP3->num_msgs);
185 void pop3_login(void)
189 msgs = pop3_grab_mailbox();
191 cprintf("+OK %s is logged in (%d messages)\r\n",
192 CC->usersupp.fullname, msgs);
193 lprintf(9, "POP3 password login successful\n");
196 cprintf("-ERR Can't open your mailbox\r\n");
201 void pop3_apop(char *argbuf)
204 char userdigest[MD5_HEXSTRING_SIZE];
205 char realdigest[MD5_HEXSTRING_SIZE];
210 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
214 if ((sptr = strchr(argbuf, ' ')) == NULL)
216 cprintf("-ERR Invalid APOP line.\r\n");
222 while ((*sptr) && isspace(*sptr))
225 strncpy(username, argbuf, sizeof(username)-1);
226 username[sizeof(username)-1] = '\0';
228 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
229 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
231 if (CtdlLoginExistingUser(username) != login_ok)
233 cprintf("-ERR No such user.\r\n");
237 if (getuser(&CC->usersupp, CC->curr_user))
239 cprintf("-ERR No such user.\r\n");
243 make_apop_string(CC->usersupp.password, CC->cs_nonce, realdigest);
244 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
251 cprintf("-ERR That is NOT the password.\r\n");
257 * Authorize with password (implements POP3 "PASS" command)
259 void pop3_pass(char *argbuf) {
262 strcpy(password, argbuf);
265 lprintf(9, "Trying <%s>\n", password);
266 if (CtdlTryPassword(password) == pass_ok) {
270 cprintf("-ERR That is NOT the password.\r\n");
277 * list available msgs
279 void pop3_list(char *argbuf) {
283 which_one = atoi(argbuf);
285 /* "list one" mode */
287 if (which_one > POP3->num_msgs) {
288 cprintf("-ERR no such message, only %d are here\r\n",
292 else if (POP3->msgs[which_one-1].deleted) {
293 cprintf("-ERR Sorry, you deleted that message.\r\n");
297 cprintf("+OK %d %d\r\n",
299 POP3->msgs[which_one-1].rfc822_length
305 /* "list all" (scan listing) mode */
307 cprintf("+OK Here's your mail:\r\n");
308 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
309 if (! POP3->msgs[i].deleted) {
312 POP3->msgs[i].rfc822_length);
321 * STAT (tally up the total message count and byte count) command
323 void pop3_stat(char *argbuf) {
325 size_t total_octets = 0;
328 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
329 if (! POP3->msgs[i].deleted) {
331 total_octets += POP3->msgs[i].rfc822_length;
335 cprintf("+OK %d %d\r\n", total_msgs, total_octets);
341 * RETR command (fetch a message)
343 void pop3_retr(char *argbuf) {
346 size_t bytes_remaining;
348 which_one = atoi(argbuf);
349 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
350 cprintf("-ERR No such message.\r\n");
354 if (POP3->msgs[which_one - 1].deleted) {
355 cprintf("-ERR Sorry, you deleted that message.\r\n");
359 cprintf("+OK Message %d:\r\n", which_one);
360 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
361 rewind(POP3->msgs[which_one - 1].temp);
362 while (bytes_remaining-- > 0) {
363 ch = getc(POP3->msgs[which_one - 1].temp);
367 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
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;
385 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
386 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
387 cprintf("-ERR No such message.\r\n");
391 if (POP3->msgs[which_one - 1].deleted) {
392 cprintf("-ERR Sorry, you deleted that message.\r\n");
396 cprintf("+OK Message %d:\r\n", which_one);
397 rewind(POP3->msgs[which_one - 1].temp);
398 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
399 ( (ptr!=NULL) && (done == 0))) {
401 if (lines_dumped >= lines_requested) done = 1;
402 if ((in_body == 0) || (done == 0))
403 client_write(buf, strlen(buf));
404 if (in_body) ++lines_dumped;
405 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
407 if (buf[strlen(buf)-1] != 10) cprintf("\n");
413 * DELE (delete message from mailbox)
415 void pop3_dele(char *argbuf) {
418 which_one = atoi(argbuf);
419 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
420 cprintf("-ERR No such message.\r\n");
424 if (POP3->msgs[which_one - 1].deleted) {
425 cprintf("-ERR You already deleted that message.\r\n");
429 /* Flag the message as deleted. Will expunge during QUIT command. */
430 POP3->msgs[which_one - 1].deleted = 1;
431 cprintf("+OK Message %d deleted.\r\n",
436 /* Perform "UPDATE state" stuff
438 void pop3_update(void) {
442 /* Remove messages marked for deletion */
443 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
444 if (POP3->msgs[i].deleted) {
445 CtdlDeleteMessages(MAILROOM,
446 POP3->msgs[i].msgnum, "");
450 /* Set last read pointer */
451 if (POP3->num_msgs > 0) {
452 lgetuser(&CC->usersupp, CC->curr_user);
454 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
455 sprintf(vbuf.v_seen, "*:%ld",
456 POP3->msgs[POP3->num_msgs-1].msgnum);
457 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
459 lputuser(&CC->usersupp);
466 * RSET (reset, i.e. undelete any deleted messages) command
468 void pop3_rset(char *argbuf) {
471 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
472 if (POP3->msgs[i].deleted) {
473 POP3->msgs[i].deleted = 0;
476 cprintf("+OK Reset completed.\r\n");
482 * LAST (Determine which message is the last unread message)
484 void pop3_last(char *argbuf) {
485 cprintf("+OK %d\r\n", POP3->lastseen + 1);
491 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
492 * identifiers are simply the Citadel message numbers in the database.
494 void pop3_uidl(char *argbuf) {
498 which_one = atoi(argbuf);
500 /* "list one" mode */
502 if (which_one > POP3->num_msgs) {
503 cprintf("-ERR no such message, only %d are here\r\n",
507 else if (POP3->msgs[which_one-1].deleted) {
508 cprintf("-ERR Sorry, you deleted that message.\r\n");
512 cprintf("+OK %d %ld\r\n",
514 POP3->msgs[which_one-1].msgnum
520 /* "list all" (scan listing) mode */
522 cprintf("+OK Here's your mail:\r\n");
523 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
524 if (! POP3->msgs[i].deleted) {
525 cprintf("%d %ld\r\n",
527 POP3->msgs[i].msgnum);
538 * Main command loop for POP3 sessions.
540 void pop3_command_loop(void) {
544 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
545 if (client_gets(cmdbuf) < 1) {
546 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
550 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
551 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
553 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
554 cprintf("+OK No operation.\r\n");
557 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
558 cprintf("+OK Goodbye...\r\n");
564 else if (!strncasecmp(cmdbuf, "USER", 4)) {
565 pop3_user(&cmdbuf[5]);
568 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
569 pop3_pass(&cmdbuf[5]);
572 else if (!strncasecmp(cmdbuf, "APOP", 4))
574 pop3_apop(&cmdbuf[5]);
577 else if (!CC->logged_in) {
578 cprintf("-ERR Not logged in.\r\n");
581 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
582 pop3_list(&cmdbuf[5]);
585 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
586 pop3_stat(&cmdbuf[5]);
589 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
590 pop3_retr(&cmdbuf[5]);
593 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
594 pop3_dele(&cmdbuf[5]);
597 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
598 pop3_rset(&cmdbuf[5]);
601 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
602 pop3_uidl(&cmdbuf[5]);
605 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
606 pop3_top(&cmdbuf[4]);
609 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
610 pop3_last(&cmdbuf[4]);
614 cprintf("-ERR I'm afraid I can't do that.\r\n");
621 char *Dynamic_Module_Init(void)
623 SYM_POP3 = CtdlGetDynamicSymbol();
624 CtdlRegisterServiceHook(config.c_pop3_port,
628 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);