4 * POP3 service for the Citadel/UX system
5 * Copyright (C) 1998-2001 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 * -> All optional POP3 commands described in RFC1939 are also implemented.
14 * -> The deprecated "LAST" command is included in this implementation, because
15 * there exist mail clients which insist on using it (such as Bynari
16 * TradeMail, and certain versions of Eudora).
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
47 #include "sysdep_decls.h"
48 #include "citserver.h"
51 #include "serv_extensions.h"
58 #include "internet_addressing.h"
59 #include "serv_pop3.h"
64 * This cleanup function blows away the temporary memory and files used by
67 void pop3_cleanup_function(void) {
70 /* Don't do this stuff if this is not a POP3 session! */
71 if (CC->h_command_function != pop3_command_loop) return;
73 lprintf(9, "Performing POP3 cleanup hook\n");
75 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
76 if (POP3->msgs[i].temp != NULL) {
77 fclose(POP3->msgs[i].temp);
78 POP3->msgs[i].temp = NULL;
81 if (POP3->msgs != NULL) phree(POP3->msgs);
83 lprintf(9, "Finished POP3 cleanup hook\n");
89 * Here's where our POP3 session begins its happy day.
91 void pop3_greeting(void) {
92 strcpy(CC->cs_clientname, "POP3 session");
94 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
98 cprintf("+OK Citadel/UX POP3 server %s\r\n",
104 * Specify user name (implements POP3 "USER" command)
106 void pop3_user(char *argbuf) {
110 cprintf("-ERR You are already logged in.\r\n");
114 strcpy(username, argbuf);
117 lprintf(9, "Trying <%s>\n", username);
118 if (CtdlLoginExistingUser(username) == login_ok) {
119 cprintf("+OK Password required for %s\r\n", username);
122 cprintf("-ERR No such user.\r\n");
129 * Back end for pop3_grab_mailbox()
131 void pop3_add_message(long msgnum, void *userdata) {
133 lprintf(9, "in pop3_add_message()\n");
136 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
137 else POP3->msgs = reallok(POP3->msgs,
138 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
139 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
140 POP3->msgs[POP3->num_msgs-1].deleted = 0;
142 POP3->msgs[POP3->num_msgs-1].temp = fp;
144 CtdlRedirectOutput(fp, -1);
145 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1);
146 CtdlRedirectOutput(NULL, -1);
148 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
154 * Open the inbox and read its contents.
155 * (This should be called only once, by pop3_pass(), and returns the number
156 * of messages in the inbox, or -1 for error)
158 int pop3_grab_mailbox(void) {
162 if (getroom(&CC->room, MAILROOM) != 0) return(-1);
164 /* Load up the messages */
165 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
166 pop3_add_message, NULL);
168 /* Figure out which are old and which are new */
169 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
170 POP3->lastseen = (-1);
171 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
172 if (is_msg_in_mset(vbuf.v_seen,
173 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
178 return(POP3->num_msgs);
181 void pop3_login(void)
185 msgs = pop3_grab_mailbox();
187 cprintf("+OK %s is logged in (%d messages)\r\n",
188 CC->user.fullname, msgs);
189 lprintf(9, "POP3 password login successful\n");
192 cprintf("-ERR Can't open your mailbox\r\n");
197 void pop3_apop(char *argbuf)
200 char userdigest[MD5_HEXSTRING_SIZE];
201 char realdigest[MD5_HEXSTRING_SIZE];
206 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
210 if ((sptr = strchr(argbuf, ' ')) == NULL)
212 cprintf("-ERR Invalid APOP line.\r\n");
218 while ((*sptr) && isspace(*sptr))
221 strncpy(username, argbuf, sizeof(username)-1);
222 username[sizeof(username)-1] = '\0';
224 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
225 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
227 if (CtdlLoginExistingUser(username) != login_ok)
229 cprintf("-ERR No such user.\r\n");
233 if (getuser(&CC->user, CC->curr_user))
235 cprintf("-ERR No such user.\r\n");
239 make_apop_string(CC->user.password, CC->cs_nonce, realdigest, sizeof realdigest);
240 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
247 cprintf("-ERR That is NOT the password.\r\n");
253 * Authorize with password (implements POP3 "PASS" command)
255 void pop3_pass(char *argbuf) {
258 strcpy(password, argbuf);
261 lprintf(9, "Trying <%s>\n", password);
262 if (CtdlTryPassword(password) == pass_ok) {
266 cprintf("-ERR That is NOT the password.\r\n");
273 * list available msgs
275 void pop3_list(char *argbuf) {
279 which_one = atoi(argbuf);
281 /* "list one" mode */
283 if (which_one > POP3->num_msgs) {
284 cprintf("-ERR no such message, only %d are here\r\n",
288 else if (POP3->msgs[which_one-1].deleted) {
289 cprintf("-ERR Sorry, you deleted that message.\r\n");
293 cprintf("+OK %d %ld\r\n",
295 (long)POP3->msgs[which_one-1].rfc822_length
301 /* "list all" (scan listing) mode */
303 cprintf("+OK Here's your mail:\r\n");
304 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
305 if (! POP3->msgs[i].deleted) {
306 cprintf("%d %ld\r\n",
308 (long)POP3->msgs[i].rfc822_length);
317 * STAT (tally up the total message count and byte count) command
319 void pop3_stat(char *argbuf) {
321 size_t total_octets = 0;
324 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
325 if (! POP3->msgs[i].deleted) {
327 total_octets += POP3->msgs[i].rfc822_length;
331 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
337 * RETR command (fetch a message)
339 void pop3_retr(char *argbuf) {
342 size_t bytes_remaining;
344 which_one = atoi(argbuf);
345 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
346 cprintf("-ERR No such message.\r\n");
350 if (POP3->msgs[which_one - 1].deleted) {
351 cprintf("-ERR Sorry, you deleted that message.\r\n");
355 cprintf("+OK Message %d:\r\n", which_one);
356 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
357 rewind(POP3->msgs[which_one - 1].temp);
358 while (bytes_remaining-- > 0) {
359 ch = getc(POP3->msgs[which_one - 1].temp);
363 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
370 * TOP command (dumb way of fetching a partial message or headers-only)
372 void pop3_top(char *argbuf) {
374 int lines_requested = 0;
375 int lines_dumped = 0;
381 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
382 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
383 cprintf("-ERR No such message.\r\n");
387 if (POP3->msgs[which_one - 1].deleted) {
388 cprintf("-ERR Sorry, you deleted that message.\r\n");
392 cprintf("+OK Message %d:\r\n", which_one);
393 rewind(POP3->msgs[which_one - 1].temp);
394 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
395 ( (ptr!=NULL) && (done == 0))) {
397 if (lines_dumped >= lines_requested) done = 1;
398 if ((in_body == 0) || (done == 0))
399 client_write(buf, strlen(buf));
400 if (in_body) ++lines_dumped;
401 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
403 if (buf[strlen(buf)-1] != 10) cprintf("\n");
409 * DELE (delete message from mailbox)
411 void pop3_dele(char *argbuf) {
414 which_one = atoi(argbuf);
415 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
416 cprintf("-ERR No such message.\r\n");
420 if (POP3->msgs[which_one - 1].deleted) {
421 cprintf("-ERR You already deleted that message.\r\n");
425 /* Flag the message as deleted. Will expunge during QUIT command. */
426 POP3->msgs[which_one - 1].deleted = 1;
427 cprintf("+OK Message %d deleted.\r\n",
432 /* Perform "UPDATE state" stuff
434 void pop3_update(void) {
438 /* Remove messages marked for deletion */
439 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
440 if (POP3->msgs[i].deleted) {
441 CtdlDeleteMessages(MAILROOM,
442 POP3->msgs[i].msgnum, "");
446 /* Set last read pointer */
447 if (POP3->num_msgs > 0) {
448 lgetuser(&CC->user, CC->curr_user);
450 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
451 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
452 POP3->msgs[POP3->num_msgs-1].msgnum);
453 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
462 * RSET (reset, i.e. undelete any deleted messages) command
464 void pop3_rset(char *argbuf) {
467 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
468 if (POP3->msgs[i].deleted) {
469 POP3->msgs[i].deleted = 0;
472 cprintf("+OK Reset completed.\r\n");
478 * LAST (Determine which message is the last unread message)
480 void pop3_last(char *argbuf) {
481 cprintf("+OK %d\r\n", POP3->lastseen + 1);
487 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
488 * identifiers are simply the Citadel message numbers in the database.
490 void pop3_uidl(char *argbuf) {
494 which_one = atoi(argbuf);
496 /* "list one" mode */
498 if (which_one > POP3->num_msgs) {
499 cprintf("-ERR no such message, only %d are here\r\n",
503 else if (POP3->msgs[which_one-1].deleted) {
504 cprintf("-ERR Sorry, you deleted that message.\r\n");
508 cprintf("+OK %d %ld\r\n",
510 POP3->msgs[which_one-1].msgnum
516 /* "list all" (scan listing) mode */
518 cprintf("+OK Here's your mail:\r\n");
519 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
520 if (! POP3->msgs[i].deleted) {
521 cprintf("%d %ld\r\n",
523 POP3->msgs[i].msgnum);
534 * Main command loop for POP3 sessions.
536 void pop3_command_loop(void) {
540 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
541 if (client_gets(cmdbuf) < 1) {
542 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
546 lprintf(5, "POP3: %s\r\n", cmdbuf);
547 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
549 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
550 cprintf("+OK No operation.\r\n");
553 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
554 cprintf("+OK Goodbye...\r\n");
560 else if (!strncasecmp(cmdbuf, "USER", 4)) {
561 pop3_user(&cmdbuf[5]);
564 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
565 pop3_pass(&cmdbuf[5]);
568 else if (!strncasecmp(cmdbuf, "APOP", 4))
570 pop3_apop(&cmdbuf[5]);
573 else if (!CC->logged_in) {
574 cprintf("-ERR Not logged in.\r\n");
577 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
578 pop3_list(&cmdbuf[5]);
581 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
582 pop3_stat(&cmdbuf[5]);
585 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
586 pop3_retr(&cmdbuf[5]);
589 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
590 pop3_dele(&cmdbuf[5]);
593 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
594 pop3_rset(&cmdbuf[5]);
597 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
598 pop3_uidl(&cmdbuf[5]);
601 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
602 pop3_top(&cmdbuf[4]);
605 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
606 pop3_last(&cmdbuf[4]);
610 cprintf("-ERR I'm afraid I can't do that.\r\n");
617 char *serv_pop3_init(void)
619 CtdlRegisterServiceHook(config.c_pop3_port,
623 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);