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 * This module implements all required POP3 server commands as described
8 * in RFC1939. It also implements nearly all of the optional commands. The
9 * only one missing is APOP, because it implements a "shared secret" method
10 * of authentication which currently does not fit well with Citadel. Perhaps
22 #include <sys/types.h>
30 #include "sysdep_decls.h"
31 #include "citserver.h"
34 #include "dynloader.h"
41 #include "internet_addressing.h"
42 #include "serv_pop3.h"
49 * This cleanup function blows away the temporary memory and files used by
52 void pop3_cleanup_function(void) {
55 /* Don't do this stuff if this is not a POP3 session! */
56 if (CC->h_command_function != pop3_command_loop) return;
58 lprintf(9, "Performing POP3 cleanup hook\n");
60 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
61 fclose(POP3->msgs[i].temp);
63 if (POP3->msgs != NULL) phree(POP3->msgs);
65 lprintf(9, "Finished POP3 cleanup hook\n");
71 * Here's where our POP3 session begins its happy day.
73 void pop3_greeting(void) {
75 strcpy(CC->cs_clientname, "POP3 session");
77 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
81 cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
87 * Specify user name (implements POP3 "USER" command)
89 void pop3_user(char *argbuf) {
93 cprintf("-ERR You are already logged in.\r\n");
97 strcpy(username, argbuf);
100 lprintf(9, "Trying <%s>\n", username);
101 if (CtdlLoginExistingUser(username) == login_ok) {
102 cprintf("+OK Password required for %s\r\n", username);
105 cprintf("-ERR No such user.\r\n");
112 * Back end for pop3_grab_mailbox()
114 void pop3_add_message(long msgnum) {
116 lprintf(9, "in pop3_add_message()\n");
119 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
120 else POP3->msgs = reallok(POP3->msgs,
121 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
122 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
123 POP3->msgs[POP3->num_msgs-1].deleted = 0;
125 POP3->msgs[POP3->num_msgs-1].temp = fp;
126 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, fp, 0, 1);
127 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
133 * Open the inbox and read its contents.
134 * (This should be called only once, by pop3_pass(), and returns the number
135 * of messages in the inbox, or -1 for error)
137 int pop3_grab_mailbox(void) {
138 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
139 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, pop3_add_message);
140 return(POP3->num_msgs);
144 * Authorize with password (implements POP3 "PASS" command)
146 void pop3_pass(char *argbuf) {
150 strcpy(password, argbuf);
153 lprintf(9, "Trying <%s>\n", password);
154 if (CtdlTryPassword(password) == pass_ok) {
155 msgs = pop3_grab_mailbox();
157 cprintf("+OK %s is logged in (%d messages)\r\n",
158 CC->usersupp.fullname, msgs);
159 lprintf(9, "POP3 password login successful\n");
162 cprintf("-ERR Can't open your mailbox\r\n");
166 cprintf("-ERR That is NOT the password! Go away!\r\n");
173 * list available msgs
175 void pop3_list(char *argbuf) {
179 which_one = atoi(argbuf);
181 /* "list one" mode */
183 if (which_one > POP3->num_msgs) {
184 cprintf("-ERR no such message, only %d are here\r\n",
188 else if (POP3->msgs[which_one-1].deleted) {
189 cprintf("-ERR Sorry, you deleted that message.\r\n");
193 cprintf("+OK %d %d\n",
195 POP3->msgs[which_one-1].rfc822_length
201 /* "list all" (scan listing) mode */
203 cprintf("+OK Here's your mail:\r\n");
204 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
205 if (! POP3->msgs[i].deleted) {
208 POP3->msgs[i].rfc822_length);
217 * STAT (tally up the total message count and byte count) command
219 void pop3_stat(char *argbuf) {
221 size_t total_octets = 0;
224 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
225 if (! POP3->msgs[i].deleted) {
227 total_octets += POP3->msgs[i].rfc822_length;
231 cprintf("+OK %d %d\n", total_msgs, total_octets);
237 * RETR command (fetch a message)
239 void pop3_retr(char *argbuf) {
242 size_t bytes_remaining;
244 which_one = atoi(argbuf);
245 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
246 cprintf("-ERR No such message.\r\n");
250 if (POP3->msgs[which_one - 1].deleted) {
251 cprintf("-ERR Sorry, you deleted that message.\r\n");
255 cprintf("+OK Whoop, there it is:\r\n");
256 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
257 rewind(POP3->msgs[which_one - 1].temp);
258 while (bytes_remaining-- > 0) {
259 ch = getc(POP3->msgs[which_one - 1].temp);
267 * TOP command (dumb way of fetching a partial message or headers-only)
269 void pop3_top(char *argbuf) {
271 int lines_requested = 0;
272 int lines_dumped = 0;
278 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
279 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
280 cprintf("-ERR No such message.\r\n");
284 if (POP3->msgs[which_one - 1].deleted) {
285 cprintf("-ERR Sorry, you deleted that message.\r\n");
289 cprintf("+OK Whoop, there it is:\r\n");
290 rewind(POP3->msgs[which_one - 1].temp);
291 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
292 ( (ptr!=NULL) && (done == 0))) {
294 if (lines_dumped >= lines_requested) done = 1;
295 if ((in_body == 0) || (done == 0))
296 client_write(buf, strlen(buf));
297 if (in_body) ++lines_dumped;
298 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
300 if (buf[strlen(buf)-1] != 10) cprintf("\n");
306 * DELE (delete message from mailbox)
308 void pop3_dele(char *argbuf) {
311 which_one = atoi(argbuf);
312 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
313 cprintf("-ERR No such message.\r\n");
317 if (POP3->msgs[which_one - 1].deleted) {
318 cprintf("-ERR You already deleted that message.\r\n");
322 /* Flag the message as deleted. Will expunge during QUIT command. */
323 POP3->msgs[which_one - 1].deleted = 1;
324 cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
329 /* Perform "UPDATE state" stuff (remove messages marked for deletion)
331 void pop3_update(void) {
334 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
335 if (POP3->msgs[i].deleted) {
336 CtdlDeleteMessages(MAILROOM,
337 POP3->msgs[i].msgnum, NULL);
344 * RSET (reset, i.e. undelete any deleted messages) command
346 void pop3_rset(char *argbuf) {
349 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
350 if (POP3->msgs[i].deleted) {
351 POP3->msgs[i].deleted = 0;
354 cprintf("+OK all that has come to pass, has now gone away.\r\n");
361 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
362 * identifiers are simply the Citadel message numbers in the database.
364 void pop3_uidl(char *argbuf) {
368 which_one = atoi(argbuf);
370 /* "list one" mode */
372 if (which_one > POP3->num_msgs) {
373 cprintf("-ERR no such message, only %d are here\r\n",
377 else if (POP3->msgs[which_one-1].deleted) {
378 cprintf("-ERR Sorry, you deleted that message.\r\n");
382 cprintf("+OK %d %ld\n",
384 POP3->msgs[which_one-1].msgnum
390 /* "list all" (scan listing) mode */
392 cprintf("+OK Here's your mail:\r\n");
393 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
394 if (! POP3->msgs[i].deleted) {
395 cprintf("%d %ld\r\n",
397 POP3->msgs[i].msgnum);
408 * Main command loop for POP3 sessions.
410 void pop3_command_loop(void) {
414 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
415 if (client_gets(cmdbuf) < 1) {
416 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
420 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
421 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
423 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
424 cprintf("+OK This command successfully did nothing.\r\n");
427 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
428 cprintf("+OK Goodbye...\r\n");
434 else if (!strncasecmp(cmdbuf, "USER", 4)) {
435 pop3_user(&cmdbuf[5]);
438 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
439 pop3_pass(&cmdbuf[5]);
442 else if (!CC->logged_in) {
443 cprintf("-ERR Not logged in.\r\n");
446 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
447 pop3_list(&cmdbuf[5]);
450 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
451 pop3_stat(&cmdbuf[5]);
454 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
455 pop3_retr(&cmdbuf[5]);
458 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
459 pop3_dele(&cmdbuf[5]);
462 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
463 pop3_rset(&cmdbuf[5]);
466 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
467 pop3_uidl(&cmdbuf[5]);
470 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
471 pop3_top(&cmdbuf[4]);
475 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
482 char *Dynamic_Module_Init(void)
484 SYM_POP3 = CtdlGetDynamicSymbol();
485 CtdlRegisterServiceHook(POP3_PORT,
488 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);