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("-ERR 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))
240 cprintf("-ERR That is NOT the password.\r\n");
246 * Authorize with password (implements POP3 "PASS" command)
248 void pop3_pass(char *argbuf) {
251 strcpy(password, argbuf);
254 lprintf(9, "Trying <%s>\n", password);
255 if (CtdlTryPassword(password) == pass_ok) {
259 cprintf("-ERR That is NOT the password.\r\n");
266 * list available msgs
268 void pop3_list(char *argbuf) {
272 which_one = atoi(argbuf);
274 /* "list one" mode */
276 if (which_one > POP3->num_msgs) {
277 cprintf("-ERR no such message, only %d are here\r\n",
281 else if (POP3->msgs[which_one-1].deleted) {
282 cprintf("-ERR Sorry, you deleted that message.\r\n");
286 cprintf("+OK %d %d\r\n",
288 POP3->msgs[which_one-1].rfc822_length
294 /* "list all" (scan listing) mode */
296 cprintf("+OK Here's your mail:\r\n");
297 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
298 if (! POP3->msgs[i].deleted) {
301 POP3->msgs[i].rfc822_length);
310 * STAT (tally up the total message count and byte count) command
312 void pop3_stat(char *argbuf) {
314 size_t total_octets = 0;
317 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
318 if (! POP3->msgs[i].deleted) {
320 total_octets += POP3->msgs[i].rfc822_length;
324 cprintf("+OK %d %d\r\n", total_msgs, total_octets);
330 * RETR command (fetch a message)
332 void pop3_retr(char *argbuf) {
335 size_t bytes_remaining;
337 which_one = atoi(argbuf);
338 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
339 cprintf("-ERR No such message.\r\n");
343 if (POP3->msgs[which_one - 1].deleted) {
344 cprintf("-ERR Sorry, you deleted that message.\r\n");
348 cprintf("+OK Message %d:\r\n", which_one);
349 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
350 rewind(POP3->msgs[which_one - 1].temp);
351 while (bytes_remaining-- > 0) {
352 ch = getc(POP3->msgs[which_one - 1].temp);
356 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
363 * TOP command (dumb way of fetching a partial message or headers-only)
365 void pop3_top(char *argbuf) {
367 int lines_requested = 0;
368 int lines_dumped = 0;
374 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
375 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
376 cprintf("-ERR No such message.\r\n");
380 if (POP3->msgs[which_one - 1].deleted) {
381 cprintf("-ERR Sorry, you deleted that message.\r\n");
385 cprintf("+OK Message %d:\r\n", which_one);
386 rewind(POP3->msgs[which_one - 1].temp);
387 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
388 ( (ptr!=NULL) && (done == 0))) {
390 if (lines_dumped >= lines_requested) done = 1;
391 if ((in_body == 0) || (done == 0))
392 client_write(buf, strlen(buf));
393 if (in_body) ++lines_dumped;
394 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
396 if (buf[strlen(buf)-1] != 10) cprintf("\n");
402 * DELE (delete message from mailbox)
404 void pop3_dele(char *argbuf) {
407 which_one = atoi(argbuf);
408 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
409 cprintf("-ERR No such message.\r\n");
413 if (POP3->msgs[which_one - 1].deleted) {
414 cprintf("-ERR You already deleted that message.\r\n");
418 /* Flag the message as deleted. Will expunge during QUIT command. */
419 POP3->msgs[which_one - 1].deleted = 1;
420 cprintf("+OK Message %d deleted.\r\n",
425 /* Perform "UPDATE state" stuff
427 void pop3_update(void) {
431 /* Remove messages marked for deletion */
432 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
433 if (POP3->msgs[i].deleted) {
434 CtdlDeleteMessages(MAILROOM,
435 POP3->msgs[i].msgnum, "");
439 /* Set last read pointer */
440 if (POP3->num_msgs > 0) {
441 lgetuser(&CC->usersupp, CC->curr_user);
443 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
444 vbuf.v_lastseen = POP3->msgs[POP3->num_msgs-1].msgnum;
445 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
447 lputuser(&CC->usersupp);
454 * RSET (reset, i.e. undelete any deleted messages) command
456 void pop3_rset(char *argbuf) {
459 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
460 if (POP3->msgs[i].deleted) {
461 POP3->msgs[i].deleted = 0;
464 cprintf("+OK Reset completed.\r\n");
470 * LAST (Determine which message is the last unread message)
472 void pop3_last(char *argbuf) {
473 cprintf("+OK %d\r\n", POP3->lastseen + 1);
479 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
480 * identifiers are simply the Citadel message numbers in the database.
482 void pop3_uidl(char *argbuf) {
486 which_one = atoi(argbuf);
488 /* "list one" mode */
490 if (which_one > POP3->num_msgs) {
491 cprintf("-ERR no such message, only %d are here\r\n",
495 else if (POP3->msgs[which_one-1].deleted) {
496 cprintf("-ERR Sorry, you deleted that message.\r\n");
500 cprintf("+OK %d %ld\r\n",
502 POP3->msgs[which_one-1].msgnum
508 /* "list all" (scan listing) mode */
510 cprintf("+OK Here's your mail:\r\n");
511 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
512 if (! POP3->msgs[i].deleted) {
513 cprintf("%d %ld\r\n",
515 POP3->msgs[i].msgnum);
526 * Main command loop for POP3 sessions.
528 void pop3_command_loop(void) {
532 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
533 if (client_gets(cmdbuf) < 1) {
534 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
538 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
539 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
541 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
542 cprintf("+OK No operation.\r\n");
545 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
546 cprintf("+OK Goodbye...\r\n");
552 else if (!strncasecmp(cmdbuf, "USER", 4)) {
553 pop3_user(&cmdbuf[5]);
556 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
557 pop3_pass(&cmdbuf[5]);
560 else if (!strncasecmp(cmdbuf, "APOP", 4))
562 pop3_apop(&cmdbuf[5]);
565 else if (!CC->logged_in) {
566 cprintf("-ERR Not logged in.\r\n");
569 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
570 pop3_list(&cmdbuf[5]);
573 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
574 pop3_stat(&cmdbuf[5]);
577 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
578 pop3_retr(&cmdbuf[5]);
581 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
582 pop3_dele(&cmdbuf[5]);
585 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
586 pop3_rset(&cmdbuf[5]);
589 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
590 pop3_uidl(&cmdbuf[5]);
593 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
594 pop3_top(&cmdbuf[4]);
597 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
598 pop3_last(&cmdbuf[4]);
602 cprintf("-ERR I'm afraid I can't do that.\r\n");
609 char *Dynamic_Module_Init(void)
611 SYM_POP3 = CtdlGetDynamicSymbol();
612 CtdlRegisterServiceHook(config.c_pop3_port,
616 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);