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 * -> The deprecated "LAST" command is included in this implementation, because
18 * there exist mail clients which insist on using it (such as Bynari
19 * TradeMail, and certain versions of Eudora).
31 #include <sys/types.h>
39 #include "sysdep_decls.h"
40 #include "citserver.h"
43 #include "dynloader.h"
50 #include "internet_addressing.h"
51 #include "serv_pop3.h"
58 * This cleanup function blows away the temporary memory and files used by
61 void pop3_cleanup_function(void) {
64 /* Don't do this stuff if this is not a POP3 session! */
65 if (CC->h_command_function != pop3_command_loop) return;
67 lprintf(9, "Performing POP3 cleanup hook\n");
69 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
70 fclose(POP3->msgs[i].temp);
72 if (POP3->msgs != NULL) phree(POP3->msgs);
74 lprintf(9, "Finished POP3 cleanup hook\n");
80 * Here's where our POP3 session begins its happy day.
82 void pop3_greeting(void) {
84 strcpy(CC->cs_clientname, "POP3 session");
86 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
90 cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
96 * Specify user name (implements POP3 "USER" command)
98 void pop3_user(char *argbuf) {
102 cprintf("-ERR You are already logged in.\r\n");
106 strcpy(username, argbuf);
109 lprintf(9, "Trying <%s>\n", username);
110 if (CtdlLoginExistingUser(username) == login_ok) {
111 cprintf("+OK Password required for %s\r\n", username);
114 cprintf("-ERR No such user.\r\n");
121 * Back end for pop3_grab_mailbox()
123 void pop3_add_message(long msgnum) {
125 lprintf(9, "in pop3_add_message()\n");
128 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
129 else POP3->msgs = reallok(POP3->msgs,
130 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
131 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
132 POP3->msgs[POP3->num_msgs-1].deleted = 0;
134 POP3->msgs[POP3->num_msgs-1].temp = fp;
136 CtdlRedirectOutput(fp, -1);
137 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
138 CtdlRedirectOutput(NULL, -1);
140 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
146 * Open the inbox and read its contents.
147 * (This should be called only once, by pop3_pass(), and returns the number
148 * of messages in the inbox, or -1 for error)
150 int pop3_grab_mailbox(void) {
154 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
156 /* Load up the messages */
157 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL, pop3_add_message);
159 /* Figure out which are old and which are new */
160 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
161 POP3->lastseen = (-1);
162 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
163 if ((POP3->msgs[POP3->num_msgs-1].msgnum) <= vbuf.v_lastseen) {
168 return(POP3->num_msgs);
172 * Authorize with password (implements POP3 "PASS" command)
174 void pop3_pass(char *argbuf) {
178 strcpy(password, argbuf);
181 lprintf(9, "Trying <%s>\n", password);
182 if (CtdlTryPassword(password) == pass_ok) {
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");
194 cprintf("-ERR That is NOT the password! Go away!\r\n");
201 * list available msgs
203 void pop3_list(char *argbuf) {
207 which_one = atoi(argbuf);
209 /* "list one" mode */
211 if (which_one > POP3->num_msgs) {
212 cprintf("-ERR no such message, only %d are here\r\n",
216 else if (POP3->msgs[which_one-1].deleted) {
217 cprintf("-ERR Sorry, you deleted that message.\r\n");
221 cprintf("+OK %d %d\n",
223 POP3->msgs[which_one-1].rfc822_length
229 /* "list all" (scan listing) mode */
231 cprintf("+OK Here's your mail:\r\n");
232 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
233 if (! POP3->msgs[i].deleted) {
236 POP3->msgs[i].rfc822_length);
245 * STAT (tally up the total message count and byte count) command
247 void pop3_stat(char *argbuf) {
249 size_t total_octets = 0;
252 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
253 if (! POP3->msgs[i].deleted) {
255 total_octets += POP3->msgs[i].rfc822_length;
259 cprintf("+OK %d %d\n", total_msgs, total_octets);
265 * RETR command (fetch a message)
267 void pop3_retr(char *argbuf) {
270 size_t bytes_remaining;
272 which_one = atoi(argbuf);
273 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
274 cprintf("-ERR No such message.\r\n");
278 if (POP3->msgs[which_one - 1].deleted) {
279 cprintf("-ERR Sorry, you deleted that message.\r\n");
283 cprintf("+OK Whoop, there it is:\r\n");
284 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
285 rewind(POP3->msgs[which_one - 1].temp);
286 while (bytes_remaining-- > 0) {
287 ch = getc(POP3->msgs[which_one - 1].temp);
291 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
298 * TOP command (dumb way of fetching a partial message or headers-only)
300 void pop3_top(char *argbuf) {
302 int lines_requested = 0;
303 int lines_dumped = 0;
309 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
310 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
311 cprintf("-ERR No such message.\r\n");
315 if (POP3->msgs[which_one - 1].deleted) {
316 cprintf("-ERR Sorry, you deleted that message.\r\n");
320 cprintf("+OK Whoop, there it is:\r\n");
321 rewind(POP3->msgs[which_one - 1].temp);
322 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
323 ( (ptr!=NULL) && (done == 0))) {
325 if (lines_dumped >= lines_requested) done = 1;
326 if ((in_body == 0) || (done == 0))
327 client_write(buf, strlen(buf));
328 if (in_body) ++lines_dumped;
329 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
331 if (buf[strlen(buf)-1] != 10) cprintf("\n");
337 * DELE (delete message from mailbox)
339 void pop3_dele(char *argbuf) {
342 which_one = atoi(argbuf);
343 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
344 cprintf("-ERR No such message.\r\n");
348 if (POP3->msgs[which_one - 1].deleted) {
349 cprintf("-ERR You already deleted that message.\r\n");
353 /* Flag the message as deleted. Will expunge during QUIT command. */
354 POP3->msgs[which_one - 1].deleted = 1;
355 cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
360 /* Perform "UPDATE state" stuff
362 void pop3_update(void) {
366 /* Remove messages marked for deletion */
367 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
368 if (POP3->msgs[i].deleted) {
369 CtdlDeleteMessages(MAILROOM,
370 POP3->msgs[i].msgnum, "");
374 /* Set last read pointer */
375 if (POP3->num_msgs > 0) {
376 lgetuser(&CC->usersupp, CC->curr_user);
378 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
379 vbuf.v_lastseen = POP3->msgs[POP3->num_msgs-1].msgnum;
380 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
382 lputuser(&CC->usersupp);
389 * RSET (reset, i.e. undelete any deleted messages) command
391 void pop3_rset(char *argbuf) {
394 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
395 if (POP3->msgs[i].deleted) {
396 POP3->msgs[i].deleted = 0;
399 cprintf("+OK all that has come to pass, has now gone away.\r\n");
405 * LAST (Determine which message is the last unread message)
407 void pop3_last(char *argbuf) {
408 cprintf("+OK %d\r\n", POP3->lastseen + 1);
414 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
415 * identifiers are simply the Citadel message numbers in the database.
417 void pop3_uidl(char *argbuf) {
421 which_one = atoi(argbuf);
423 /* "list one" mode */
425 if (which_one > POP3->num_msgs) {
426 cprintf("-ERR no such message, only %d are here\r\n",
430 else if (POP3->msgs[which_one-1].deleted) {
431 cprintf("-ERR Sorry, you deleted that message.\r\n");
435 cprintf("+OK %d %ld\n",
437 POP3->msgs[which_one-1].msgnum
443 /* "list all" (scan listing) mode */
445 cprintf("+OK Here's your mail:\r\n");
446 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
447 if (! POP3->msgs[i].deleted) {
448 cprintf("%d %ld\r\n",
450 POP3->msgs[i].msgnum);
461 * Main command loop for POP3 sessions.
463 void pop3_command_loop(void) {
467 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
468 if (client_gets(cmdbuf) < 1) {
469 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
473 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
474 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
476 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
477 cprintf("+OK This command successfully did nothing.\r\n");
480 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
481 cprintf("+OK Goodbye...\r\n");
487 else if (!strncasecmp(cmdbuf, "USER", 4)) {
488 pop3_user(&cmdbuf[5]);
491 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
492 pop3_pass(&cmdbuf[5]);
495 else if (!CC->logged_in) {
496 cprintf("-ERR Not logged in.\r\n");
499 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
500 pop3_list(&cmdbuf[5]);
503 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
504 pop3_stat(&cmdbuf[5]);
507 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
508 pop3_retr(&cmdbuf[5]);
511 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
512 pop3_dele(&cmdbuf[5]);
515 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
516 pop3_rset(&cmdbuf[5]);
519 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
520 pop3_uidl(&cmdbuf[5]);
523 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
524 pop3_top(&cmdbuf[4]);
527 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
528 pop3_last(&cmdbuf[4]);
532 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
539 char *Dynamic_Module_Init(void)
541 SYM_POP3 = CtdlGetDynamicSymbol();
542 CtdlRegisterServiceHook(config.c_pop3_port,
546 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);