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