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, void *userdata) {
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,
158 pop3_add_message, NULL);
160 /* Figure out which are old and which are new */
161 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
162 POP3->lastseen = (-1);
163 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
164 if ((POP3->msgs[POP3->num_msgs-1].msgnum) <= vbuf.v_lastseen) {
169 return(POP3->num_msgs);
173 * Authorize with password (implements POP3 "PASS" command)
175 void pop3_pass(char *argbuf) {
179 strcpy(password, argbuf);
182 lprintf(9, "Trying <%s>\n", password);
183 if (CtdlTryPassword(password) == pass_ok) {
184 msgs = pop3_grab_mailbox();
186 cprintf("+OK %s is logged in (%d messages)\r\n",
187 CC->usersupp.fullname, msgs);
188 lprintf(9, "POP3 password login successful\n");
191 cprintf("-ERR Can't open your mailbox\r\n");
195 cprintf("-ERR That is NOT the password! Go away!\r\n");
202 * list available msgs
204 void pop3_list(char *argbuf) {
208 which_one = atoi(argbuf);
210 /* "list one" mode */
212 if (which_one > POP3->num_msgs) {
213 cprintf("-ERR no such message, only %d are here\r\n",
217 else if (POP3->msgs[which_one-1].deleted) {
218 cprintf("-ERR Sorry, you deleted that message.\r\n");
222 cprintf("+OK %d %d\n",
224 POP3->msgs[which_one-1].rfc822_length
230 /* "list all" (scan listing) mode */
232 cprintf("+OK Here's your mail:\r\n");
233 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
234 if (! POP3->msgs[i].deleted) {
237 POP3->msgs[i].rfc822_length);
246 * STAT (tally up the total message count and byte count) command
248 void pop3_stat(char *argbuf) {
250 size_t total_octets = 0;
253 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
254 if (! POP3->msgs[i].deleted) {
256 total_octets += POP3->msgs[i].rfc822_length;
260 cprintf("+OK %d %d\n", total_msgs, total_octets);
266 * RETR command (fetch a message)
268 void pop3_retr(char *argbuf) {
271 size_t bytes_remaining;
273 which_one = atoi(argbuf);
274 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
275 cprintf("-ERR No such message.\r\n");
279 if (POP3->msgs[which_one - 1].deleted) {
280 cprintf("-ERR Sorry, you deleted that message.\r\n");
284 cprintf("+OK Whoop, there it is:\r\n");
285 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
286 rewind(POP3->msgs[which_one - 1].temp);
287 while (bytes_remaining-- > 0) {
288 ch = getc(POP3->msgs[which_one - 1].temp);
292 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
299 * TOP command (dumb way of fetching a partial message or headers-only)
301 void pop3_top(char *argbuf) {
303 int lines_requested = 0;
304 int lines_dumped = 0;
310 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
311 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
312 cprintf("-ERR No such message.\r\n");
316 if (POP3->msgs[which_one - 1].deleted) {
317 cprintf("-ERR Sorry, you deleted that message.\r\n");
321 cprintf("+OK Whoop, there it is:\r\n");
322 rewind(POP3->msgs[which_one - 1].temp);
323 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
324 ( (ptr!=NULL) && (done == 0))) {
326 if (lines_dumped >= lines_requested) done = 1;
327 if ((in_body == 0) || (done == 0))
328 client_write(buf, strlen(buf));
329 if (in_body) ++lines_dumped;
330 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
332 if (buf[strlen(buf)-1] != 10) cprintf("\n");
338 * DELE (delete message from mailbox)
340 void pop3_dele(char *argbuf) {
343 which_one = atoi(argbuf);
344 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
345 cprintf("-ERR No such message.\r\n");
349 if (POP3->msgs[which_one - 1].deleted) {
350 cprintf("-ERR You already deleted that message.\r\n");
354 /* Flag the message as deleted. Will expunge during QUIT command. */
355 POP3->msgs[which_one - 1].deleted = 1;
356 cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
361 /* Perform "UPDATE state" stuff
363 void pop3_update(void) {
367 /* Remove messages marked for deletion */
368 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
369 if (POP3->msgs[i].deleted) {
370 CtdlDeleteMessages(MAILROOM,
371 POP3->msgs[i].msgnum, "");
375 /* Set last read pointer */
376 if (POP3->num_msgs > 0) {
377 lgetuser(&CC->usersupp, CC->curr_user);
379 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
380 vbuf.v_lastseen = POP3->msgs[POP3->num_msgs-1].msgnum;
381 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
383 lputuser(&CC->usersupp);
390 * RSET (reset, i.e. undelete any deleted messages) command
392 void pop3_rset(char *argbuf) {
395 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
396 if (POP3->msgs[i].deleted) {
397 POP3->msgs[i].deleted = 0;
400 cprintf("+OK all that has come to pass, has now gone away.\r\n");
406 * LAST (Determine which message is the last unread message)
408 void pop3_last(char *argbuf) {
409 cprintf("+OK %d\r\n", POP3->lastseen + 1);
415 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
416 * identifiers are simply the Citadel message numbers in the database.
418 void pop3_uidl(char *argbuf) {
422 which_one = atoi(argbuf);
424 /* "list one" mode */
426 if (which_one > POP3->num_msgs) {
427 cprintf("-ERR no such message, only %d are here\r\n",
431 else if (POP3->msgs[which_one-1].deleted) {
432 cprintf("-ERR Sorry, you deleted that message.\r\n");
436 cprintf("+OK %d %ld\n",
438 POP3->msgs[which_one-1].msgnum
444 /* "list all" (scan listing) mode */
446 cprintf("+OK Here's your mail:\r\n");
447 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
448 if (! POP3->msgs[i].deleted) {
449 cprintf("%d %ld\r\n",
451 POP3->msgs[i].msgnum);
462 * Main command loop for POP3 sessions.
464 void pop3_command_loop(void) {
468 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
469 if (client_gets(cmdbuf) < 1) {
470 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
474 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
475 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
477 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
478 cprintf("+OK This command successfully did nothing.\r\n");
481 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
482 cprintf("+OK Goodbye...\r\n");
488 else if (!strncasecmp(cmdbuf, "USER", 4)) {
489 pop3_user(&cmdbuf[5]);
492 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
493 pop3_pass(&cmdbuf[5]);
496 else if (!CC->logged_in) {
497 cprintf("-ERR Not logged in.\r\n");
500 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
501 pop3_list(&cmdbuf[5]);
504 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
505 pop3_stat(&cmdbuf[5]);
508 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
509 pop3_retr(&cmdbuf[5]);
512 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
513 pop3_dele(&cmdbuf[5]);
516 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
517 pop3_rset(&cmdbuf[5]);
520 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
521 pop3_uidl(&cmdbuf[5]);
524 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
525 pop3_top(&cmdbuf[4]);
528 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
529 pop3_last(&cmdbuf[4]);
533 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
540 char *Dynamic_Module_Init(void)
542 SYM_POP3 = CtdlGetDynamicSymbol();
543 CtdlRegisterServiceHook(config.c_pop3_port,
547 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);