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