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 "dynloader.h"
58 #include "internet_addressing.h"
59 #include "serv_pop3.h"
66 * This cleanup function blows away the temporary memory and files used by
69 void pop3_cleanup_function(void) {
72 /* Don't do this stuff if this is not a POP3 session! */
73 if (CC->h_command_function != pop3_command_loop) return;
75 lprintf(9, "Performing POP3 cleanup hook\n");
77 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
78 if (POP3->msgs[i].temp != NULL) {
79 fclose(POP3->msgs[i].temp);
80 POP3->msgs[i].temp = NULL;
83 if (POP3->msgs != NULL) phree(POP3->msgs);
85 lprintf(9, "Finished POP3 cleanup hook\n");
91 * Here's where our POP3 session begins its happy day.
93 void pop3_greeting(void) {
94 strcpy(CC->cs_clientname, "POP3 session");
96 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
100 cprintf("+OK Citadel/UX POP3 server %s\r\n",
106 * Specify user name (implements POP3 "USER" command)
108 void pop3_user(char *argbuf) {
112 cprintf("-ERR You are already logged in.\r\n");
116 strcpy(username, argbuf);
119 lprintf(9, "Trying <%s>\n", username);
120 if (CtdlLoginExistingUser(username) == login_ok) {
121 cprintf("+OK Password required for %s\r\n", username);
124 cprintf("-ERR No such user.\r\n");
131 * Back end for pop3_grab_mailbox()
133 void pop3_add_message(long msgnum, void *userdata) {
135 lprintf(9, "in pop3_add_message()\n");
138 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
139 else POP3->msgs = reallok(POP3->msgs,
140 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
141 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
142 POP3->msgs[POP3->num_msgs-1].deleted = 0;
144 POP3->msgs[POP3->num_msgs-1].temp = fp;
146 CtdlRedirectOutput(fp, -1);
147 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
148 CtdlRedirectOutput(NULL, -1);
150 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
156 * Open the inbox and read its contents.
157 * (This should be called only once, by pop3_pass(), and returns the number
158 * of messages in the inbox, or -1 for error)
160 int pop3_grab_mailbox(void) {
164 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
166 /* Load up the messages */
167 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
168 pop3_add_message, NULL);
170 /* Figure out which are old and which are new */
171 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
172 POP3->lastseen = (-1);
173 if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
174 if (is_msg_in_mset(vbuf.v_seen,
175 (POP3->msgs[POP3->num_msgs-1].msgnum) )) {
180 return(POP3->num_msgs);
183 void pop3_login(void)
187 msgs = pop3_grab_mailbox();
189 cprintf("+OK %s is logged in (%d messages)\r\n",
190 CC->usersupp.fullname, msgs);
191 lprintf(9, "POP3 password login successful\n");
194 cprintf("-ERR Can't open your mailbox\r\n");
199 void pop3_apop(char *argbuf)
202 char userdigest[MD5_HEXSTRING_SIZE];
203 char realdigest[MD5_HEXSTRING_SIZE];
208 cprintf("-ERR You are already logged in; not in the AUTHORIZATION phase.\r\n");
212 if ((sptr = strchr(argbuf, ' ')) == NULL)
214 cprintf("-ERR Invalid APOP line.\r\n");
220 while ((*sptr) && isspace(*sptr))
223 strncpy(username, argbuf, sizeof(username)-1);
224 username[sizeof(username)-1] = '\0';
226 memset(userdigest, MD5_HEXSTRING_SIZE, 0);
227 strncpy(userdigest, sptr, MD5_HEXSTRING_SIZE-1);
229 if (CtdlLoginExistingUser(username) != login_ok)
231 cprintf("-ERR No such user.\r\n");
235 if (getuser(&CC->usersupp, CC->curr_user))
237 cprintf("-ERR No such user.\r\n");
241 make_apop_string(CC->usersupp.password, CC->cs_nonce, realdigest, sizeof realdigest);
242 if (!strncasecmp(realdigest, userdigest, MD5_HEXSTRING_SIZE-1))
249 cprintf("-ERR That is NOT the password.\r\n");
255 * Authorize with password (implements POP3 "PASS" command)
257 void pop3_pass(char *argbuf) {
260 strcpy(password, argbuf);
263 lprintf(9, "Trying <%s>\n", password);
264 if (CtdlTryPassword(password) == pass_ok) {
268 cprintf("-ERR That is NOT the password.\r\n");
275 * list available msgs
277 void pop3_list(char *argbuf) {
281 which_one = atoi(argbuf);
283 /* "list one" mode */
285 if (which_one > POP3->num_msgs) {
286 cprintf("-ERR no such message, only %d are here\r\n",
290 else if (POP3->msgs[which_one-1].deleted) {
291 cprintf("-ERR Sorry, you deleted that message.\r\n");
295 cprintf("+OK %d %ld\r\n",
297 (long)POP3->msgs[which_one-1].rfc822_length
303 /* "list all" (scan listing) mode */
305 cprintf("+OK Here's your mail:\r\n");
306 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
307 if (! POP3->msgs[i].deleted) {
308 cprintf("%d %ld\r\n",
310 (long)POP3->msgs[i].rfc822_length);
319 * STAT (tally up the total message count and byte count) command
321 void pop3_stat(char *argbuf) {
323 size_t total_octets = 0;
326 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
327 if (! POP3->msgs[i].deleted) {
329 total_octets += POP3->msgs[i].rfc822_length;
333 cprintf("+OK %d %ld\r\n", total_msgs, (long)total_octets);
339 * RETR command (fetch a message)
341 void pop3_retr(char *argbuf) {
344 size_t bytes_remaining;
346 which_one = atoi(argbuf);
347 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
348 cprintf("-ERR No such message.\r\n");
352 if (POP3->msgs[which_one - 1].deleted) {
353 cprintf("-ERR Sorry, you deleted that message.\r\n");
357 cprintf("+OK Message %d:\r\n", which_one);
358 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
359 rewind(POP3->msgs[which_one - 1].temp);
360 while (bytes_remaining-- > 0) {
361 ch = getc(POP3->msgs[which_one - 1].temp);
365 lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
372 * TOP command (dumb way of fetching a partial message or headers-only)
374 void pop3_top(char *argbuf) {
376 int lines_requested = 0;
377 int lines_dumped = 0;
383 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
384 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
385 cprintf("-ERR No such message.\r\n");
389 if (POP3->msgs[which_one - 1].deleted) {
390 cprintf("-ERR Sorry, you deleted that message.\r\n");
394 cprintf("+OK Message %d:\r\n", which_one);
395 rewind(POP3->msgs[which_one - 1].temp);
396 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
397 ( (ptr!=NULL) && (done == 0))) {
399 if (lines_dumped >= lines_requested) done = 1;
400 if ((in_body == 0) || (done == 0))
401 client_write(buf, strlen(buf));
402 if (in_body) ++lines_dumped;
403 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
405 if (buf[strlen(buf)-1] != 10) cprintf("\n");
411 * DELE (delete message from mailbox)
413 void pop3_dele(char *argbuf) {
416 which_one = atoi(argbuf);
417 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
418 cprintf("-ERR No such message.\r\n");
422 if (POP3->msgs[which_one - 1].deleted) {
423 cprintf("-ERR You already deleted that message.\r\n");
427 /* Flag the message as deleted. Will expunge during QUIT command. */
428 POP3->msgs[which_one - 1].deleted = 1;
429 cprintf("+OK Message %d deleted.\r\n",
434 /* Perform "UPDATE state" stuff
436 void pop3_update(void) {
440 /* Remove messages marked for deletion */
441 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
442 if (POP3->msgs[i].deleted) {
443 CtdlDeleteMessages(MAILROOM,
444 POP3->msgs[i].msgnum, "");
448 /* Set last read pointer */
449 if (POP3->num_msgs > 0) {
450 lgetuser(&CC->usersupp, CC->curr_user);
452 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
453 sprintf(vbuf.v_seen, "*:%ld",
454 POP3->msgs[POP3->num_msgs-1].msgnum);
455 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
457 lputuser(&CC->usersupp);
464 * RSET (reset, i.e. undelete any deleted messages) command
466 void pop3_rset(char *argbuf) {
469 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
470 if (POP3->msgs[i].deleted) {
471 POP3->msgs[i].deleted = 0;
474 cprintf("+OK Reset completed.\r\n");
480 * LAST (Determine which message is the last unread message)
482 void pop3_last(char *argbuf) {
483 cprintf("+OK %d\r\n", POP3->lastseen + 1);
489 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
490 * identifiers are simply the Citadel message numbers in the database.
492 void pop3_uidl(char *argbuf) {
496 which_one = atoi(argbuf);
498 /* "list one" mode */
500 if (which_one > POP3->num_msgs) {
501 cprintf("-ERR no such message, only %d are here\r\n",
505 else if (POP3->msgs[which_one-1].deleted) {
506 cprintf("-ERR Sorry, you deleted that message.\r\n");
510 cprintf("+OK %d %ld\r\n",
512 POP3->msgs[which_one-1].msgnum
518 /* "list all" (scan listing) mode */
520 cprintf("+OK Here's your mail:\r\n");
521 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
522 if (! POP3->msgs[i].deleted) {
523 cprintf("%d %ld\r\n",
525 POP3->msgs[i].msgnum);
536 * Main command loop for POP3 sessions.
538 void pop3_command_loop(void) {
542 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
543 if (client_gets(cmdbuf) < 1) {
544 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
548 lprintf(5, "POP3: %s\r\n", cmdbuf);
549 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
551 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
552 cprintf("+OK No operation.\r\n");
555 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
556 cprintf("+OK Goodbye...\r\n");
562 else if (!strncasecmp(cmdbuf, "USER", 4)) {
563 pop3_user(&cmdbuf[5]);
566 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
567 pop3_pass(&cmdbuf[5]);
570 else if (!strncasecmp(cmdbuf, "APOP", 4))
572 pop3_apop(&cmdbuf[5]);
575 else if (!CC->logged_in) {
576 cprintf("-ERR Not logged in.\r\n");
579 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
580 pop3_list(&cmdbuf[5]);
583 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
584 pop3_stat(&cmdbuf[5]);
587 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
588 pop3_retr(&cmdbuf[5]);
591 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
592 pop3_dele(&cmdbuf[5]);
595 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
596 pop3_rset(&cmdbuf[5]);
599 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
600 pop3_uidl(&cmdbuf[5]);
603 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
604 pop3_top(&cmdbuf[4]);
607 else if (!strncasecmp(cmdbuf, "LAST", 4)) {
608 pop3_last(&cmdbuf[4]);
612 cprintf("-ERR I'm afraid I can't do that.\r\n");
619 char *Dynamic_Module_Init(void)
621 SYM_POP3 = CtdlGetDynamicSymbol();
622 CtdlRegisterServiceHook(config.c_pop3_port,
626 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);