]> code.citadel.org Git - citadel.git/blob - citadel/serv_pop3.c
* citserver.c: cleanup hook functions are now run under the proper context,
[citadel.git] / citadel / serv_pop3.c
1 /* $Id$ */
2
3 #include "sysdep.h"
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <pwd.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <sys/wait.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "citadel.h"
17 #include "server.h"
18 #include <time.h>
19 #include "sysdep_decls.h"
20 #include "citserver.h"
21 #include "support.h"
22 #include "config.h"
23 #include "dynloader.h"
24 #include "room_ops.h"
25 #include "user_ops.h"
26 #include "policy.h"
27 #include "database.h"
28 #include "msgbase.h"
29 #include "tools.h"
30 #include "internet_addressing.h"
31 #include "serv_pop3.h"
32
33
34 long SYM_POP3;
35
36
37 void pop3_cleanup_function(void) {
38         int i;
39
40         /* Don't do this stuff if this is not a POP3 session! */
41         if (CC->h_command_function != pop3_command_loop) return;
42
43         lprintf(9, "Performing POP3 cleanup hook\n");
44
45         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
46                 fclose(POP3->msgs[i].temp);
47         }
48         if (POP3->msgs != NULL) phree(POP3->msgs);
49
50         lprintf(9, "Finished POP3 cleanup hook\n");
51 }
52
53
54
55 /*
56  * Here's where our POP3 session begins its happy day.
57  */
58 void pop3_greeting(void) {
59
60         strcpy(CC->cs_clientname, "POP3 session");
61         CC->internal_pgm = 1;
62         CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
63         POP3->msgs = NULL;
64         POP3->num_msgs = 0;
65
66         cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
67                 config.c_fqdn);
68 }
69
70
71 /*
72  * Specify user name (implements POP3 "USER" command)
73  */
74 void pop3_user(char *argbuf) {
75         char username[256];
76
77         if (CC->logged_in) {
78                 cprintf("-ERR You are already logged in.\r\n");
79                 return;
80         }
81
82         strcpy(username, argbuf);
83         striplt(username);
84
85         lprintf(9, "Trying <%s>\n", username);
86         if (CtdlLoginExistingUser(username) == login_ok) {
87                 cprintf("+OK Password required for %s\r\n", username);
88         }
89         else {
90                 cprintf("-ERR No such user.\r\n");
91         }
92 }
93
94
95 /*
96  * Authorize with password (implements POP3 "PASS" command)
97  */
98 void pop3_pass(char *argbuf) {
99         char password[256];
100
101         strcpy(password, argbuf);
102         striplt(password);
103
104         lprintf(9, "Trying <%s>\n", password);
105         if (CtdlTryPassword(password) == pass_ok) {
106                 if (getroom(&CC->quickroom, MAILROOM) == 0) {
107                         cprintf("+OK %s is logged in!\r\n",
108                                 CC->usersupp.fullname);
109                         lprintf(9, "POP3 password login successful\n");
110                 }
111                 else {
112                         cprintf("-ERR can't find your mailbox\r\n");
113                 }
114         }
115         else {
116                 cprintf("-ERR That is NOT the password!  Go away!\r\n");
117         }
118 }
119
120
121
122 /*
123  * list available msgs
124  */
125 void pop3_list(char *argbuf) {
126         cprintf("-ERR oops, not finished\r\n");
127 }
128
129
130
131
132 /* 
133  * Main command loop for POP3 sessions.
134  */
135 void pop3_command_loop(void) {
136         char cmdbuf[256];
137
138         time(&CC->lastcmd);
139         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
140         if (client_gets(cmdbuf) < 1) {
141                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
142                 CC->kill_me = 1;
143                 return;
144         }
145         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
146         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
147
148         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
149                 cprintf("+OK This command successfully did nothing.\r\n");
150         }
151
152         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
153                 cprintf("+OK Goodbye...\r\n");
154                 CC->kill_me = 1;
155                 return;
156         }
157
158         else if (!strncasecmp(cmdbuf, "USER", 4)) {
159                 pop3_user(&cmdbuf[5]);
160         }
161
162         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
163                 pop3_pass(&cmdbuf[5]);
164         }
165
166         else if (!CC->logged_in) {
167                 cprintf("-ERR Not logged in.\r\n");
168         }
169
170         else if (!strncasecmp(cmdbuf, "LIST", 4)) {
171                 pop3_list(&cmdbuf[5]);
172         }
173
174         else {
175                 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
176         }
177
178 }
179
180
181
182 char *Dynamic_Module_Init(void)
183 {
184         SYM_POP3 = CtdlGetDynamicSymbol();
185         CtdlRegisterServiceHook(POP3_PORT,
186                                 pop3_greeting,
187                                 pop3_command_loop);
188         CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
189         return "$Id$";
190 }