3 * POP3 server for the Citadel/UX system
4 * Copyright (C) 1998-2000 by Art Cancro and others.
5 * This code is released under the terms of the GNU General Public License.
7 * Current status of standards conformance:
9 * -> All required POP3 commands described in RFC1939 are implemented.
11 * -> Nearly all of the optional commands in RFC1939 are also implemented.
12 * The only one missing is APOP, because it implements a "shared secret"
13 * method of authentication which would require some major changes to the
14 * Citadel server core.
16 * -> The deprecated "LAST" command is included in this implementation, because
17 * there exist mail clients which insist on using it (such as Bynari
18 * TradeMail, and certain versions of Eudora).
30 #include <sys/types.h>
38 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "dynloader.h"
49 #include "internet_addressing.h"
50 #include "serv_pop3.h"
57 * This cleanup function blows away the temporary memory and files used by
60 void pop3_cleanup_function(void) {
63 /* Don't do this stuff if this is not a POP3 session! */
64 if (CC->h_command_function != pop3_command_loop) return;
66 lprintf(9, "Performing POP3 cleanup hook\n");
68 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
69 fclose(POP3->msgs[i].temp);
71 if (POP3->msgs != NULL) phree(POP3->msgs);
73 lprintf(9, "Finished POP3 cleanup hook\n");
79 * Here's where our POP3 session begins its happy day.
81 void pop3_greeting(void) {
83 strcpy(CC->cs_clientname, "POP3 session");
85 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
89 cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
95 * Specify user name (implements POP3 "USER" command)
97 void pop3_user(char *argbuf) {
101 cprintf("-ERR You are already logged in.\r\n");
105 strcpy(username, argbuf);
108 lprintf(9, "Trying <%s>\n", username);
109 if (CtdlLoginExistingUser(username) == login_ok) {
110 cprintf("+OK Password required for %s\r\n", username);
113 cprintf("-ERR No such user.\r\n");
120 * Back end for pop3_grab_mailbox()
122 void pop3_add_message(long msgnum) {
124 lprintf(9, "in pop3_add_message()\n");
127 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
128 else POP3->msgs = reallok(POP3->msgs,
129 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
130 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
131 POP3->msgs[POP3->num_msgs-1].deleted = 0;
133 POP3->msgs[POP3->num_msgs-1].temp = fp;
135 CtdlRedirectOutput(fp, -1);
136 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
137 CtdlRedirectOutput(NULL, -1);
139 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
145 * Open the inbox and read its contents.
146 * (This should be called only once, by pop3_pass(), and returns the number
147 * of messages in the inbox, or -1 for error)
149 int pop3_grab_mailbox(void) {
153 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
155 /* Load up the messages */
156 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, pop3_add_message);
158 /* Figure out which are old and which are new */
159 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
160 POP3->lastseen = (-1);
161 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
162 if ((POP3->msgs[POP3->num_msgs-1].msgnum) <= vbuf.v_lastseen) {
167 return(POP3->num_msgs);
171 * Authorize with password (implements POP3 "PASS" command)
173 void pop3_pass(char *argbuf) {
177 strcpy(password, argbuf);
180 lprintf(9, "Trying <%s>\n", password);
181 if (CtdlTryPassword(password) == pass_ok) {
182 msgs = pop3_grab_mailbox();
184 cprintf("+OK %s is logged in (%d messages)\r\n",
185 CC->usersupp.fullname, msgs);
186 lprintf(9, "POP3 password login successful\n");
189 cprintf("-ERR Can't open your mailbox\r\n");
193 cprintf("-ERR That is NOT the password! Go away!\r\n");
200 * list available msgs
202 void pop3_list(char *argbuf) {
206 which_one = atoi(argbuf);
208 /* "list one" mode */
210 if (which_one > POP3->num_msgs) {
211 cprintf("-ERR no such message, only %d are here\r\n",
215 else if (POP3->msgs[which_one-1].deleted) {
216 cprintf("-ERR Sorry, you deleted that message.\r\n");
220 cprintf("+OK %d %d\n",
222 POP3->msgs[which_one-1].rfc822_length
228 /* "list all" (scan listing) mode */
230 cprintf("+OK Here's your mail:\r\n");
231 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
232 if (! POP3->msgs[i].deleted) {
235 POP3->msgs[i].rfc822_length);
244 * STAT (tally up the total message count and byte count) command
246 void pop3_stat(char *argbuf) {
248 size_t total_octets = 0;
251 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
252 if (! POP3->msgs[i].deleted) {
254 total_octets += POP3->msgs[i].rfc822_length;
258 cprintf("+OK %d %d\n", total_msgs, total_octets);
264 * RETR command (fetch a message)
266 void pop3_retr(char *argbuf) {
269 size_t bytes_remaining;
271 which_one = atoi(argbuf);
272 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
273 cprintf("-ERR No such message.\r\n");
277 if (POP3->msgs[which_one - 1].deleted) {
278 cprintf("-ERR Sorry, you deleted that message.\r\n");
282 cprintf("+OK Whoop, there it is:\r\n");
283 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
284 rewind(POP3->msgs[which_one - 1].temp);
285 while (bytes_remaining-- > 0) {
286 ch = getc(POP3->msgs[which_one - 1].temp);
294 * TOP command (dumb way of fetching a partial message or headers-only)
296 void pop3_top(char *argbuf) {
298 int lines_requested = 0;
299 int lines_dumped = 0;
305 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
306 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
307 cprintf("-ERR No such message.\r\n");
311 if (POP3->msgs[which_one - 1].deleted) {
312 cprintf("-ERR Sorry, you deleted that message.\r\n");
316 cprintf("+OK Whoop, there it is:\r\n");
317 rewind(POP3->msgs[which_one - 1].temp);
318 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
319 ( (ptr!=NULL) && (done == 0))) {
321 if (lines_dumped >= lines_requested) done = 1;
322 if ((in_body == 0) || (done == 0))
323 client_write(buf, strlen(buf));
324 if (in_body) ++lines_dumped;
325 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
327 if (buf[strlen(buf)-1] != 10) cprintf("\n");
333 * DELE (delete message from mailbox)
335 void pop3_dele(char *argbuf) {
338 which_one = atoi(argbuf);
339 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
340 cprintf("-ERR No such message.\r\n");
344 if (POP3->msgs[which_one - 1].deleted) {
345 cprintf("-ERR You already deleted that message.\r\n");
349 /* Flag the message as deleted. Will expunge during QUIT command. */
350 POP3->msgs[which_one - 1].deleted = 1;
351 cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
356 /* Perform "UPDATE state" stuff
358 void pop3_update(void) {
362 /* Remove messages marked for deletion */
363 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
364 if (POP3->msgs[i].deleted) {
365 CtdlDeleteMessages(MAILROOM,
366 POP3->msgs[i].msgnum, NULL);
370 /* Set last read pointer */
371 if (POP3->num_msgs > 0) {
372 lgetuser(&CC->usersupp, CC->curr_user);
374 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
375 vbuf.v_lastseen = POP3->msgs[POP3->num_msgs-1].msgnum;
376 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
378 lputuser(&CC->usersupp);
385 * RSET (reset, i.e. undelete any deleted messages) command
387 void pop3_rset(char *argbuf) {
390 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
391 if (POP3->msgs[i].deleted) {
392 POP3->msgs[i].deleted = 0;
395 cprintf("+OK all that has come to pass, has now gone away.\r\n");
401 * LAST (Determine which message is the last unread message)
403 void pop3_last(char *argbuf) {
404 cprintf("+OK %d\r\n", POP3->lastseen + 1);
410 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
411 * identifiers are simply the Citadel message numbers in the database.
413 void pop3_uidl(char *argbuf) {
417 which_one = atoi(argbuf);
419 /* "list one" mode */
421 if (which_one > POP3->num_msgs) {
422 cprintf("-ERR no such message, only %d are here\r\n",
426 else if (POP3->msgs[which_one-1].deleted) {
427 cprintf("-ERR Sorry, you deleted that message.\r\n");
431 cprintf("+OK %d %ld\n",
433 POP3->msgs[which_one-1].msgnum
439 /* "list all" (scan listing) mode */
441 cprintf("+OK Here's your mail:\r\n");
442 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
443 if (! POP3->msgs[i].deleted) {
444 cprintf("%d %ld\r\n",
446 POP3->msgs[i].msgnum);
457 * Main command loop for POP3 sessions.
459 void pop3_command_loop(void) {
463 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
464 if (client_gets(cmdbuf) < 1) {
465 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
469 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
470 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
472 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
473 cprintf("+OK This command successfully did nothing.\r\n");
476 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
477 cprintf("+OK Goodbye...\r\n");
483 else if (!strncasecmp(cmdbuf, "USER", 4)) {
484 pop3_user(&cmdbuf[5]);
487 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
488 pop3_pass(&cmdbuf[5]);
491 else if (!CC->logged_in) {
492 cprintf("-ERR Not logged in.\r\n");
495 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
496 pop3_list(&cmdbuf[5]);
499 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
500 pop3_stat(&cmdbuf[5]);
503 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
504 pop3_retr(&cmdbuf[5]);
507 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
508 pop3_dele(&cmdbuf[5]);
511 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
512 pop3_rset(&cmdbuf[5]);
515 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
516 pop3_uidl(&cmdbuf[5]);
519 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
520 pop3_top(&cmdbuf[4]);
523 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
524 pop3_last(&cmdbuf[4]);
528 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
535 char *Dynamic_Module_Init(void)
537 SYM_POP3 = CtdlGetDynamicSymbol();
538 CtdlRegisterServiceHook(POP3_PORT,
541 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);