]> code.citadel.org Git - citadel.git/blob - citadel/serv_smtp.c
* Fixed some SMTP bugs
[citadel.git] / citadel / serv_smtp.c
1 /* $Id$ */
2
3 #define SMTP_PORT       2525
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <pwd.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/wait.h>
16 #include <string.h>
17 #include <limits.h>
18 #include "citadel.h"
19 #include "server.h"
20 #include <time.h>
21 #include "sysdep_decls.h"
22 #include "citserver.h"
23 #include "support.h"
24 #include "config.h"
25 #include "dynloader.h"
26 #include "room_ops.h"
27 #include "user_ops.h"
28 #include "policy.h"
29 #include "database.h"
30 #include "msgbase.h"
31 #include "tools.h"
32
33 struct citsmtp {
34         int command_state;
35         struct usersupp vrfy_buffer;
36         int vrfy_count;
37         char vrfy_match[256];
38 };
39
40 enum {
41         smtp_command,
42         smtp_user,
43         smtp_password
44 };
45
46 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
47
48 long SYM_SMTP;
49
50
51 /*
52  * Here's where our SMTP session begins its happy day.
53  */
54 void smtp_greeting(void) {
55
56         strcpy(CC->cs_clientname, "Citadel SMTP");
57         CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
58
59         cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
60                 config.c_fqdn);
61 }
62
63
64 /*
65  * Implement HELO and EHLO commands.
66  */
67 void smtp_hello(char *argbuf, int is_esmtp) {
68
69         if (!is_esmtp) {
70                 cprintf("250 Greetings and joyous salutations.\n");
71         }
72         else {
73                 cprintf("250-Greetings and joyous salutations.\n");
74                 cprintf("250-HELP\n");
75                 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
76                 cprintf("250 AUTH=LOGIN\n");
77         }
78 }
79
80
81 /*
82  * Implement HELP command.
83  */
84 void smtp_help(void) {
85         cprintf("214-Here's the frequency, Kenneth:\n");
86         cprintf("214-    EHLO\n");
87         cprintf("214-    EXPN\n");
88         cprintf("214-    HELO\n");
89         cprintf("214-    HELP\n");
90         cprintf("214-    NOOP\n");
91         cprintf("214-    QUIT\n");
92         cprintf("214-    VRFY\n");
93         cprintf("214 I could tell you more, but then I'd have to kill you.\n");
94 }
95
96
97 /*
98  *
99  */
100 void smtp_get_user(char *argbuf) {
101         char buf[256];
102         char username[256];
103
104         decode_base64(username, argbuf);
105         lprintf(9, "Trying <%s>\n", username);
106         if (CtdlLoginExistingUser(username) == login_ok) {
107                 encode_base64(buf, "Password:");
108                 cprintf("334 %s\n", buf);
109                 SMTP->command_state = smtp_password;
110         }
111         else {
112                 cprintf("500 No such user.\n");
113                 SMTP->command_state = smtp_command;
114         }
115 }
116
117
118 /*
119  *
120  */
121 void smtp_get_pass(char *argbuf) {
122         char password[256];
123
124         decode_base64(password, argbuf);
125         lprintf(9, "Trying <%s>\n", password);
126         if (CtdlTryPassword(password) == pass_ok) {
127                 cprintf("235 Authentication successful.\n");
128                 lprintf(9, "SMTP auth login successful\n");
129         }
130         else {
131                 cprintf("500 Authentication failed.\n");
132         }
133         SMTP->command_state = smtp_command;
134 }
135
136
137 /*
138  *
139  */
140 void smtp_auth(char *argbuf) {
141         char buf[256];
142
143         if (strncasecmp(argbuf, "login", 5) ) {
144                 cprintf("550 We only support LOGIN authentication.\n");
145                 return;
146         }
147
148         if (strlen(argbuf) >= 7) {
149                 smtp_get_user(&argbuf[6]);
150         }
151
152         else {
153                 encode_base64(buf, "Username:");
154                 cprintf("334 %s\n", buf);
155                 SMTP->command_state = smtp_user;
156         }
157 }
158
159
160 /*
161  * Return 0 if a given string fuzzy-matches a Citadel user account
162  *
163  * FIX ... this needs to be updated to match any and all ways of addressing
164  *         a user.  It may even be appropriate to move this out of SMTP and
165  *         into the server core.
166  */
167 int fuzzy_match(struct usersupp *us, char *matchstring) {
168         int a;
169
170         for (a=0; a<strlen(us->fullname); ++a) {
171                 if (!strncasecmp(&us->fullname[a],
172                    matchstring, strlen(matchstring))) {
173                         return 0;
174                 }
175         }
176         return -1;
177 }
178
179
180 /*
181  * Back end for smtp_vrfy() command
182  */
183 void smtp_vrfy_backend(struct usersupp *us) {
184
185         if (!fuzzy_match(us, SMTP->vrfy_match)) {
186                 ++SMTP->vrfy_count;
187                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
188         }
189 }
190
191
192 /* 
193  * Implements the VRFY (verify user name) command.
194  * Performs fuzzy match on full user names.
195  */
196 void smtp_vrfy(char *argbuf) {
197         SMTP->vrfy_count = 0;
198         strcpy(SMTP->vrfy_match, argbuf);
199         ForEachUser(smtp_vrfy_backend);
200
201         if (SMTP->vrfy_count < 1) {
202                 cprintf("550 String does not match anything.\n");
203         }
204         else if (SMTP->vrfy_count == 1) {
205                 cprintf("250 %s <cit%ld@%s>\n",
206                         SMTP->vrfy_buffer.fullname,
207                         SMTP->vrfy_buffer.usernum,
208                         config.c_fqdn);
209         }
210         else if (SMTP->vrfy_count > 1) {
211                 cprintf("553 Request ambiguous: %d users matched.\n",
212                         SMTP->vrfy_count);
213         }
214
215 }
216
217
218
219 /*
220  * Back end for smtp_expn() command
221  */
222 void smtp_expn_backend(struct usersupp *us) {
223
224         if (!fuzzy_match(us, SMTP->vrfy_match)) {
225
226                 if (SMTP->vrfy_count >= 1) {
227                         cprintf("250-%s <cit%ld@%s>\n",
228                                 SMTP->vrfy_buffer.fullname,
229                                 SMTP->vrfy_buffer.usernum,
230                                 config.c_fqdn);
231                 }
232
233                 ++SMTP->vrfy_count;
234                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
235         }
236 }
237
238
239 /* 
240  * Implements the EXPN (expand user name) command.
241  * Performs fuzzy match on full user names.
242  */
243 void smtp_expn(char *argbuf) {
244         SMTP->vrfy_count = 0;
245         strcpy(SMTP->vrfy_match, argbuf);
246         ForEachUser(smtp_expn_backend);
247
248         if (SMTP->vrfy_count < 1) {
249                 cprintf("550 String does not match anything.\n");
250         }
251         else if (SMTP->vrfy_count >= 1) {
252                 cprintf("250 %s <cit%ld@%s>\n",
253                         SMTP->vrfy_buffer.fullname,
254                         SMTP->vrfy_buffer.usernum,
255                         config.c_fqdn);
256         }
257 }
258
259
260
261 /* 
262  * Main command loop for SMTP sessions.
263  */
264 void smtp_command_loop(void) {
265         char cmdbuf[256];
266
267         time(&CC->lastcmd);
268         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
269         if (client_gets(cmdbuf) < 1) {
270                 lprintf(3, "SMTP socket is broken.  Ending session.\n");
271                 CC->kill_me = 1;
272                 return;
273         }
274         lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
275         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
276
277         if (SMTP->command_state == smtp_user) {
278                 smtp_get_user(cmdbuf);
279         }
280
281         else if (SMTP->command_state == smtp_password) {
282                 smtp_get_pass(cmdbuf);
283         }
284
285         else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
286                 smtp_auth(&cmdbuf[5]);
287         }
288
289         else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
290                 smtp_hello(&cmdbuf[5], 1);
291         }
292
293         else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
294                 smtp_expn(&cmdbuf[5]);
295         }
296
297         else if (!strncasecmp(cmdbuf, "HELO", 4)) {
298                 smtp_hello(&cmdbuf[5], 0);
299         }
300
301         else if (!strncasecmp(cmdbuf, "HELP", 4)) {
302                 smtp_help();
303         }
304
305         else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
306                 cprintf("250 This command successfully did nothing.\n");
307         }
308
309         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
310                 cprintf("221 Goodbye...\n");
311                 CC->kill_me = 1;
312                 return;
313                 }
314
315         else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
316                 smtp_vrfy(&cmdbuf[5]);
317         }
318
319         else {
320                 cprintf("500 I'm afraid I can't do that, Dave.\n");
321         }
322
323 }
324
325
326
327 char *Dynamic_Module_Init(void)
328 {
329         SYM_SMTP = CtdlGetDynamicSymbol();
330         CtdlRegisterServiceHook(SMTP_PORT,
331                                 smtp_greeting,
332                                 smtp_command_loop);
333         return "$Id$";
334 }