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