5d2136b559c19ab31a84d230091c01f047e59c7d
[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 #include "internet_addressing.h"
33
34 struct citsmtp {
35         int command_state;
36         struct usersupp vrfy_buffer;
37         int vrfy_count;
38         char vrfy_match[256];
39         char from[256];
40 };
41
42 enum {
43         smtp_command,
44         smtp_user,
45         smtp_password
46 };
47
48 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
49
50 long SYM_SMTP;
51
52
53 /*
54  * Here's where our SMTP session begins its happy day.
55  */
56 void smtp_greeting(void) {
57
58         strcpy(CC->cs_clientname, "Citadel SMTP");
59         CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
60
61         cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
62                 config.c_fqdn);
63 }
64
65
66 /*
67  * Implement HELO and EHLO commands.
68  */
69 void smtp_hello(char *argbuf, int is_esmtp) {
70
71         if (!is_esmtp) {
72                 cprintf("250 Greetings and joyous salutations.\n");
73         }
74         else {
75                 cprintf("250-Greetings and joyous salutations.\n");
76                 cprintf("250-HELP\n");
77                 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
78                 cprintf("250 AUTH=LOGIN\n");
79         }
80 }
81
82
83 /*
84  * Implement HELP command.
85  */
86 void smtp_help(void) {
87         cprintf("214-Here's the frequency, Kenneth:\n");
88         cprintf("214-    EHLO\n");
89         cprintf("214-    EXPN\n");
90         cprintf("214-    HELO\n");
91         cprintf("214-    HELP\n");
92         cprintf("214-    MAIL\n");
93         cprintf("214-    NOOP\n");
94         cprintf("214-    QUIT\n");
95         cprintf("214-    RCPT\n");
96         cprintf("214-    RSET\n");
97         cprintf("214-    VRFY\n");
98         cprintf("214 I could tell you more, but then I'd have to kill you.\n");
99 }
100
101
102 /*
103  *
104  */
105 void smtp_get_user(char *argbuf) {
106         char buf[256];
107         char username[256];
108
109         decode_base64(username, argbuf);
110         lprintf(9, "Trying <%s>\n", username);
111         if (CtdlLoginExistingUser(username) == login_ok) {
112                 encode_base64(buf, "Password:");
113                 cprintf("334 %s\n", buf);
114                 SMTP->command_state = smtp_password;
115         }
116         else {
117                 cprintf("500 No such user.\n");
118                 SMTP->command_state = smtp_command;
119         }
120 }
121
122
123 /*
124  *
125  */
126 void smtp_get_pass(char *argbuf) {
127         char password[256];
128
129         decode_base64(password, argbuf);
130         lprintf(9, "Trying <%s>\n", password);
131         if (CtdlTryPassword(password) == pass_ok) {
132                 cprintf("235 Authentication successful.\n");
133                 lprintf(9, "SMTP auth login successful\n");
134         }
135         else {
136                 cprintf("500 Authentication failed.\n");
137         }
138         SMTP->command_state = smtp_command;
139 }
140
141
142 /*
143  *
144  */
145 void smtp_auth(char *argbuf) {
146         char buf[256];
147
148         if (strncasecmp(argbuf, "login", 5) ) {
149                 cprintf("550 We only support LOGIN authentication.\n");
150                 return;
151         }
152
153         if (strlen(argbuf) >= 7) {
154                 smtp_get_user(&argbuf[6]);
155         }
156
157         else {
158                 encode_base64(buf, "Username:");
159                 cprintf("334 %s\n", buf);
160                 SMTP->command_state = smtp_user;
161         }
162 }
163
164
165 /*
166  * Back end for smtp_vrfy() command
167  */
168 void smtp_vrfy_backend(struct usersupp *us) {
169
170         if (!fuzzy_match(us, SMTP->vrfy_match)) {
171                 ++SMTP->vrfy_count;
172                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
173         }
174 }
175
176
177 /* 
178  * Implements the VRFY (verify user name) command.
179  * Performs fuzzy match on full user names.
180  */
181 void smtp_vrfy(char *argbuf) {
182         SMTP->vrfy_count = 0;
183         strcpy(SMTP->vrfy_match, argbuf);
184         ForEachUser(smtp_vrfy_backend);
185
186         if (SMTP->vrfy_count < 1) {
187                 cprintf("550 String does not match anything.\n");
188         }
189         else if (SMTP->vrfy_count == 1) {
190                 cprintf("250 %s <cit%ld@%s>\n",
191                         SMTP->vrfy_buffer.fullname,
192                         SMTP->vrfy_buffer.usernum,
193                         config.c_fqdn);
194         }
195         else if (SMTP->vrfy_count > 1) {
196                 cprintf("553 Request ambiguous: %d users matched.\n",
197                         SMTP->vrfy_count);
198         }
199
200 }
201
202
203
204 /*
205  * Back end for smtp_expn() command
206  */
207 void smtp_expn_backend(struct usersupp *us) {
208
209         if (!fuzzy_match(us, SMTP->vrfy_match)) {
210
211                 if (SMTP->vrfy_count >= 1) {
212                         cprintf("250-%s <cit%ld@%s>\n",
213                                 SMTP->vrfy_buffer.fullname,
214                                 SMTP->vrfy_buffer.usernum,
215                                 config.c_fqdn);
216                 }
217
218                 ++SMTP->vrfy_count;
219                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
220         }
221 }
222
223
224 /* 
225  * Implements the EXPN (expand user name) command.
226  * Performs fuzzy match on full user names.
227  */
228 void smtp_expn(char *argbuf) {
229         SMTP->vrfy_count = 0;
230         strcpy(SMTP->vrfy_match, argbuf);
231         ForEachUser(smtp_expn_backend);
232
233         if (SMTP->vrfy_count < 1) {
234                 cprintf("550 String does not match anything.\n");
235         }
236         else if (SMTP->vrfy_count >= 1) {
237                 cprintf("250 %s <cit%ld@%s>\n",
238                         SMTP->vrfy_buffer.fullname,
239                         SMTP->vrfy_buffer.usernum,
240                         config.c_fqdn);
241         }
242 }
243
244
245 /*
246  * Implements the RSET (reset state) command.
247  * Currently this just zeroes out the state buffer.  If pointers to data
248  * allocated with mallok() are ever placed in the state buffer, we have to
249  * be sure to phree() them first!
250  */
251 void smtp_rset(void) {
252         memset(SMTP, 0, sizeof(struct citsmtp));
253         if (CC->logged_in) logout(CC);
254         cprintf("250 Zap!\n");
255 }
256
257
258
259 /*
260  * Implements the "MAIL From:" command
261  */
262 void smtp_mail(char *argbuf) {
263         char user[256];
264         char node[256];
265         int cvt;
266
267         if (strlen(SMTP->from) != 0) {
268                 cprintf("503 Only one sender permitted\n");
269                 return;
270         }
271
272         if (strncasecmp(argbuf, "From:", 5)) {
273                 cprintf("501 Syntax error\n");
274                 return;
275         }
276
277         strcpy(SMTP->from, &argbuf[5]);
278         striplt(SMTP->from);
279
280         if (strlen(SMTP->from) == 0) {
281                 cprintf("501 Empty sender name is not permitted\n");
282                 return;
283         }
284
285
286         /* If this SMTP connection is from a logged-in user, make sure that
287          * the user only sends email from his/her own address.
288          */
289         if (CC->logged_in) {
290                 cvt = convert_internet_address(user, node, SMTP->from);
291                 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
292                 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
293                         cprintf("550 <%s> is not your address.\n", SMTP->from);
294                         strcpy(SMTP->from, "");
295                         return;
296                 }
297         }
298
299         /* Otherwise, make sure outsiders aren't trying to forge mail from
300          * this system.
301          */
302         else {
303                 cvt = convert_internet_address(user, node, SMTP->from);
304                 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
305                 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
306                         cprintf("550 You must log in to send mail from %s\n",
307                                 config.c_fqdn);
308                         return;
309                 }
310         }
311
312         cprintf("250 Sender ok.  Groovy.\n");
313 }
314
315
316
317 /*
318  * Implements the "RCPT To:" command
319  */
320 void smtp_rcpt(char *argbuf) {
321
322         if (strlen(SMTP->from) == 0) {
323                 cprintf("503 MAIL first, then RCPT.  Duh.\n");
324                 return;
325         }
326
327         if (strncasecmp(argbuf, "To:", 3)) {
328                 cprintf("501 Syntax error\n");
329                 return;
330         }
331
332         cprintf("599 this is unfinished\n");
333 }
334
335
336
337
338 /* 
339  * Main command loop for SMTP sessions.
340  */
341 void smtp_command_loop(void) {
342         char cmdbuf[256];
343
344         time(&CC->lastcmd);
345         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
346         if (client_gets(cmdbuf) < 1) {
347                 lprintf(3, "SMTP socket is broken.  Ending session.\n");
348                 CC->kill_me = 1;
349                 return;
350         }
351         lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
352         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
353
354         if (SMTP->command_state == smtp_user) {
355                 smtp_get_user(cmdbuf);
356         }
357
358         else if (SMTP->command_state == smtp_password) {
359                 smtp_get_pass(cmdbuf);
360         }
361
362         else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
363                 smtp_auth(&cmdbuf[5]);
364         }
365
366         else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
367                 smtp_hello(&cmdbuf[5], 1);
368         }
369
370         else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
371                 smtp_expn(&cmdbuf[5]);
372         }
373
374         else if (!strncasecmp(cmdbuf, "HELO", 4)) {
375                 smtp_hello(&cmdbuf[5], 0);
376         }
377
378         else if (!strncasecmp(cmdbuf, "HELP", 4)) {
379                 smtp_help();
380         }
381
382         else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
383                 smtp_mail(&cmdbuf[5]);
384         }
385
386         else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
387                 cprintf("250 This command successfully did nothing.\n");
388         }
389
390         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
391                 cprintf("221 Goodbye...\n");
392                 CC->kill_me = 1;
393                 return;
394                 }
395
396         else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
397                 smtp_rcpt(&cmdbuf[5]);
398         }
399
400         else if (!strncasecmp(cmdbuf, "RSET", 4)) {
401                 smtp_rset();
402         }
403
404         else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
405                 smtp_vrfy(&cmdbuf[5]);
406         }
407
408         else {
409                 cprintf("500 I'm afraid I can't do that, Dave.\n");
410         }
411
412 }
413
414
415
416 char *Dynamic_Module_Init(void)
417 {
418         SYM_SMTP = CtdlGetDynamicSymbol();
419         CtdlRegisterServiceHook(SMTP_PORT,
420                                 smtp_greeting,
421                                 smtp_command_loop);
422         return "$Id$";
423 }