]> code.citadel.org Git - citadel.git/blob - citadel/serv_pop3.c
* Implemented LIST and STAT commands in the pop3 server
[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);
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  * Main command loop for POP3 sessions.
233  */
234 void pop3_command_loop(void) {
235         char cmdbuf[256];
236
237         time(&CC->lastcmd);
238         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
239         if (client_gets(cmdbuf) < 1) {
240                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
241                 CC->kill_me = 1;
242                 return;
243         }
244         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
245         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
246
247         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
248                 cprintf("+OK This command successfully did nothing.\r\n");
249         }
250
251         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
252                 cprintf("+OK Goodbye...\r\n");
253                 CC->kill_me = 1;
254                 return;
255         }
256
257         else if (!strncasecmp(cmdbuf, "USER", 4)) {
258                 pop3_user(&cmdbuf[5]);
259         }
260
261         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
262                 pop3_pass(&cmdbuf[5]);
263         }
264
265         else if (!CC->logged_in) {
266                 cprintf("-ERR Not logged in.\r\n");
267         }
268
269         else if (!strncasecmp(cmdbuf, "LIST", 4)) {
270                 pop3_list(&cmdbuf[5]);
271         }
272
273         else if (!strncasecmp(cmdbuf, "STAT", 4)) {
274                 pop3_stat(&cmdbuf[5]);
275         }
276
277         else {
278                 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
279         }
280
281 }
282
283
284
285 char *Dynamic_Module_Init(void)
286 {
287         SYM_POP3 = CtdlGetDynamicSymbol();
288         CtdlRegisterServiceHook(POP3_PORT,
289                                 pop3_greeting,
290                                 pop3_command_loop);
291         CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
292         return "$Id$";
293 }