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