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