]> code.citadel.org Git - citadel.git/blob - citadel/serv_pop3.c
* CR to CRLF hacks (lose, lose, lose)
[citadel.git] / citadel / serv_pop3.c
1 /* $Id$ 
2  *
3  * An implementation of Post Office Protocol version 3 (RFC 1939).
4  *
5  * Copyright (C) 1998-2000 by Art Cancro and others.
6  * This code is released under the terms of the GNU General Public License.
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <string.h>
21 #include <limits.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <time.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
27 #include "support.h"
28 #include "config.h"
29 #include "dynloader.h"
30 #include "room_ops.h"
31 #include "user_ops.h"
32 #include "policy.h"
33 #include "database.h"
34 #include "msgbase.h"
35 #include "tools.h"
36 #include "internet_addressing.h"
37 #include "serv_pop3.h"
38
39
40 long SYM_POP3;
41
42
43 /*
44  * This cleanup function blows away the temporary memory and files used by
45  * the POP3 server.
46  */
47 void pop3_cleanup_function(void) {
48         int i;
49
50         /* Don't do this stuff if this is not a POP3 session! */
51         if (CC->h_command_function != pop3_command_loop) return;
52
53         lprintf(9, "Performing POP3 cleanup hook\n");
54
55         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
56                 fclose(POP3->msgs[i].temp);
57         }
58         if (POP3->msgs != NULL) phree(POP3->msgs);
59
60         lprintf(9, "Finished POP3 cleanup hook\n");
61 }
62
63
64
65 /*
66  * Here's where our POP3 session begins its happy day.
67  */
68 void pop3_greeting(void) {
69
70         strcpy(CC->cs_clientname, "POP3 session");
71         CC->internal_pgm = 1;
72         CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
73         POP3->msgs = NULL;
74         POP3->num_msgs = 0;
75
76         cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
77                 config.c_fqdn);
78 }
79
80
81 /*
82  * Specify user name (implements POP3 "USER" command)
83  */
84 void pop3_user(char *argbuf) {
85         char username[256];
86
87         if (CC->logged_in) {
88                 cprintf("-ERR You are already logged in.\r\n");
89                 return;
90         }
91
92         strcpy(username, argbuf);
93         striplt(username);
94
95         lprintf(9, "Trying <%s>\n", username);
96         if (CtdlLoginExistingUser(username) == login_ok) {
97                 cprintf("+OK Password required for %s\r\n", username);
98         }
99         else {
100                 cprintf("-ERR No such user.\r\n");
101         }
102 }
103
104
105
106 /*
107  * Back end for pop3_grab_mailbox()
108  */
109 void pop3_add_message(long msgnum) {
110         FILE *fp;
111         lprintf(9, "in pop3_add_message()\n");
112
113         ++POP3->num_msgs;
114         if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
115         else POP3->msgs = reallok(POP3->msgs, 
116                 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
117         POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
118         POP3->msgs[POP3->num_msgs-1].deleted = 0;
119         fp = tmpfile();
120         POP3->msgs[POP3->num_msgs-1].temp = fp;
121         CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, fp, 0, 1);
122         POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
123 }
124
125
126
127 /*
128  * Open the inbox and read its contents.
129  * (This should be called only once, by pop3_pass(), and returns the number
130  * of messages in the inbox, or -1 for error)
131  */
132 int pop3_grab_mailbox(void) {
133         if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
134         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, pop3_add_message);
135         return(POP3->num_msgs);
136 }
137
138 /*
139  * Authorize with password (implements POP3 "PASS" command)
140  */
141 void pop3_pass(char *argbuf) {
142         char password[256];
143         int msgs;
144
145         strcpy(password, argbuf);
146         striplt(password);
147
148         lprintf(9, "Trying <%s>\n", password);
149         if (CtdlTryPassword(password) == pass_ok) {
150                 msgs = pop3_grab_mailbox();
151                 if (msgs >= 0) {
152                         cprintf("+OK %s is logged in (%d messages)\r\n",
153                                 CC->usersupp.fullname, msgs);
154                         lprintf(9, "POP3 password login successful\n");
155                 }
156                 else {
157                         cprintf("-ERR Can't open your mailbox\r\n");
158                 }
159         }
160         else {
161                 cprintf("-ERR That is NOT the password!  Go away!\r\n");
162         }
163 }
164
165
166
167 /*
168  * list available msgs
169  */
170 void pop3_list(char *argbuf) {
171         int i;
172         int which_one;
173
174         which_one = atoi(argbuf);
175
176         /* "list one" mode */
177         if (which_one > 0) {
178                 if (which_one > POP3->num_msgs) {
179                         cprintf("-ERR no such message, only %d are here\r\n",
180                                 POP3->num_msgs);
181                         return;
182                 }
183                 else if (POP3->msgs[which_one-1].deleted) {
184                         cprintf("-ERR Sorry, you deleted that message.\r\n");
185                         return;
186                 }
187                 else {
188                         cprintf("+OK %d %d\n",
189                                 which_one,
190                                 POP3->msgs[which_one-1].rfc822_length
191                                 );
192                         return;
193                 }
194         }
195
196         /* "list all" (scan listing) mode */
197         else {
198                 cprintf("+OK Here's your mail:\r\n");
199                 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
200                         if (! POP3->msgs[i].deleted) {
201                                 cprintf("%d %d\r\n",
202                                         i+1,
203                                         POP3->msgs[i].rfc822_length);
204                         }
205                 }
206                 cprintf(".\r\n");
207         }
208 }
209
210
211 /*
212  * STAT (tally up the total message count and byte count) command
213  */
214 void pop3_stat(char *argbuf) {
215         int total_msgs = 0;
216         size_t total_octets = 0;
217         int i;
218         
219         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
220                 if (! POP3->msgs[i].deleted) {
221                         ++total_msgs;
222                         total_octets += POP3->msgs[i].rfc822_length;
223                 }
224         }
225
226         cprintf("+OK %d %d\n", total_msgs, total_octets);
227 }
228
229
230
231 /*
232  * RETR command (fetch a message)
233  */
234 void pop3_retr(char *argbuf) {
235         int which_one;
236         int ch;
237         size_t bytes_remaining;
238
239         which_one = atoi(argbuf);
240         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
241                 cprintf("-ERR No such message.\r\n");
242                 return;
243         }
244
245         if (POP3->msgs[which_one - 1].deleted) {
246                 cprintf("-ERR Sorry, you deleted that message.\r\n");
247                 return;
248         }
249
250         cprintf("+OK Whoop, there it is:\r\n");
251         bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
252         rewind(POP3->msgs[which_one - 1].temp);
253         while (bytes_remaining-- > 0) {
254                 ch = getc(POP3->msgs[which_one - 1].temp);
255                 cprintf("%c", ch);
256         }
257         cprintf(".\r\n");
258 }
259
260
261 /*
262  * TOP command (dumb way of fetching a partial message or headers-only)
263  */
264 void pop3_top(char *argbuf) {
265         int which_one;
266         int lines_requested = 0;
267         int lines_dumped = 0;
268         char buf[1024];
269         char *ptr;
270         int in_body = 0;
271         int done = 0;
272
273         sscanf(argbuf, "%d %d", &which_one, &lines_requested);
274         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
275                 cprintf("-ERR No such message.\r\n");
276                 return;
277         }
278
279         if (POP3->msgs[which_one - 1].deleted) {
280                 cprintf("-ERR Sorry, you deleted that message.\r\n");
281                 return;
282         }
283
284         cprintf("+OK Whoop, there it is:\r\n");
285         rewind(POP3->msgs[which_one - 1].temp);
286         while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
287               ( (ptr!=NULL) && (done == 0))) {
288                 if (in_body == 1)
289                         if (lines_dumped >= lines_requested) done = 1;
290                 if ((in_body == 0) || (done == 0))
291                         client_write(buf, strlen(buf));
292                 if (in_body) ++lines_dumped;
293                 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
294         }
295         if (buf[strlen(buf)-1] != 10) cprintf("\n");
296         cprintf(".\r\n");
297 }
298
299
300 /*
301  * DELE (delete message from mailbox)
302  */
303 void pop3_dele(char *argbuf) {
304         int which_one;
305
306         which_one = atoi(argbuf);
307         if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
308                 cprintf("-ERR No such message.\r\n");
309                 return;
310         }
311
312         if (POP3->msgs[which_one - 1].deleted) {
313                 cprintf("-ERR You already deleted that message.\r\n");
314                 return;
315         }
316
317         /* Flag the message as deleted.  Will expunge during QUIT command. */
318         POP3->msgs[which_one - 1].deleted = 1;
319         cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
320                 which_one);
321 }
322
323
324 /* Perform "UPDATE state" stuff (remove messages marked for deletion)
325  */
326 void pop3_update(void) {
327         int i;
328
329         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
330                 if (POP3->msgs[i].deleted) {
331                         CtdlDeleteMessages(MAILROOM,
332                                 POP3->msgs[i].msgnum, NULL);
333                 }
334         }
335 }
336
337
338 /* 
339  * RSET (reset, i.e. undelete any deleted messages) command
340  */
341 void pop3_rset(char *argbuf) {
342         int i;
343
344         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
345                 if (POP3->msgs[i].deleted) {
346                         POP3->msgs[i].deleted = 0;
347                 }
348         }
349         cprintf("+OK all that has come to pass, has now gone away.\r\n");
350 }
351
352
353
354
355 /*
356  * UIDL (Universal IDentifier Listing) is easy.  Our 'unique' message
357  * identifiers are simply the Citadel message numbers in the database.
358  */
359 void pop3_uidl(char *argbuf) {
360         int i;
361         int which_one;
362
363         which_one = atoi(argbuf);
364
365         /* "list one" mode */
366         if (which_one > 0) {
367                 if (which_one > POP3->num_msgs) {
368                         cprintf("-ERR no such message, only %d are here\r\n",
369                                 POP3->num_msgs);
370                         return;
371                 }
372                 else if (POP3->msgs[which_one-1].deleted) {
373                         cprintf("-ERR Sorry, you deleted that message.\r\n");
374                         return;
375                 }
376                 else {
377                         cprintf("+OK %d %ld\n",
378                                 which_one,
379                                 POP3->msgs[which_one-1].msgnum
380                                 );
381                         return;
382                 }
383         }
384
385         /* "list all" (scan listing) mode */
386         else {
387                 cprintf("+OK Here's your mail:\r\n");
388                 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
389                         if (! POP3->msgs[i].deleted) {
390                                 cprintf("%d %ld\r\n",
391                                         i+1,
392                                         POP3->msgs[i].msgnum);
393                         }
394                 }
395                 cprintf(".\r\n");
396         }
397 }
398
399
400
401
402 /* 
403  * Main command loop for POP3 sessions.
404  */
405 void pop3_command_loop(void) {
406         char cmdbuf[256];
407
408         time(&CC->lastcmd);
409         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
410         if (client_gets(cmdbuf) < 1) {
411                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
412                 CC->kill_me = 1;
413                 return;
414         }
415         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
416         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
417
418         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
419                 cprintf("+OK This command successfully did nothing.\r\n");
420         }
421
422         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
423                 cprintf("+OK Goodbye...\r\n");
424                 pop3_update();
425                 CC->kill_me = 1;
426                 return;
427         }
428
429         else if (!strncasecmp(cmdbuf, "USER", 4)) {
430                 pop3_user(&cmdbuf[5]);
431         }
432
433         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
434                 pop3_pass(&cmdbuf[5]);
435         }
436
437         else if (!CC->logged_in) {
438                 cprintf("-ERR Not logged in.\r\n");
439         }
440
441         else if (!strncasecmp(cmdbuf, "LIST", 4)) {
442                 pop3_list(&cmdbuf[5]);
443         }
444
445         else if (!strncasecmp(cmdbuf, "STAT", 4)) {
446                 pop3_stat(&cmdbuf[5]);
447         }
448
449         else if (!strncasecmp(cmdbuf, "RETR", 4)) {
450                 pop3_retr(&cmdbuf[5]);
451         }
452
453         else if (!strncasecmp(cmdbuf, "DELE", 4)) {
454                 pop3_dele(&cmdbuf[5]);
455         }
456
457         else if (!strncasecmp(cmdbuf, "RSET", 4)) {
458                 pop3_rset(&cmdbuf[5]);
459         }
460
461         else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
462                 pop3_uidl(&cmdbuf[5]);
463         }
464
465         else if (!strncasecmp(cmdbuf, "TOP", 3)) {
466                 pop3_top(&cmdbuf[4]);
467         }
468
469         else {
470                 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
471         }
472
473 }
474
475
476
477 char *Dynamic_Module_Init(void)
478 {
479         SYM_POP3 = CtdlGetDynamicSymbol();
480         CtdlRegisterServiceHook(POP3_PORT,
481                                 pop3_greeting,
482                                 pop3_command_loop);
483         CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
484         return "$Id$";
485 }