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