1 // This module is an SMTP and ESMTP server for the Citadel system.
2 // It is compliant with all of the following:
4 // RFC 821 - Simple Mail Transfer Protocol
5 // RFC 876 - Survey of SMTP Implementations
6 // RFC 1047 - Duplicate messages and SMTP
7 // RFC 1652 - 8 bit MIME
8 // RFC 1869 - Extended Simple Mail Transfer Protocol
9 // RFC 1870 - SMTP Service Extension for Message Size Declaration
10 // RFC 2033 - Local Mail Transfer Protocol
11 // RFC 2197 - SMTP Service Extension for Command Pipelining
12 // RFC 2476 - Message Submission
13 // RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
14 // RFC 2554 - SMTP Service Extension for Authentication
15 // RFC 2821 - Simple Mail Transfer Protocol
16 // RFC 2822 - Internet Message Format
17 // RFC 2920 - SMTP Service Extension for Command Pipelining
19 // The VRFY and EXPN commands have been removed from this implementation
20 // because nobody uses these commands anymore, except for spammers.
22 // Copyright (c) 1998-2022 by the citadel.org team
24 // This program is open source software; you can redistribute it and/or modify
25 // it under the terms of the GNU General Public License version 3.
27 // This program is distributed in the hope that it will be useful,
28 // but WITHOUT ANY WARRANTY; without even the implied warranty of
29 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 // GNU General Public License for more details.
32 #include "../../sysdep.h"
41 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <libcitadel.h>
52 #include "../../citadel.h"
53 #include "../../server.h"
54 #include "../../citserver.h"
55 #include "../../support.h"
56 #include "../../config.h"
57 #include "../../control.h"
58 #include "../../user_ops.h"
59 #include "../../room_ops.h"
60 #include "../../database.h"
61 #include "../../msgbase.h"
62 #include "../../internet_addressing.h"
63 #include "../../genstamp.h"
64 #include "../../domain.h"
65 #include "../../clientsocket.h"
66 #include "../../locate_host.h"
67 #include "../../citadel_dirs.h"
68 #include "../../ctdl_module.h"
70 #include "smtp_util.h"
72 enum { // Command states for login authentication
86 // Here's where our SMTP session begins its happy day.
87 void smtp_greeting(int is_msa) {
88 char message_to_spammer[1024];
90 strcpy(CC->cs_clientname, "SMTP session");
92 CC->cs_flags |= CS_STEALTH;
93 CC->session_specific_data = malloc(sizeof(struct citsmtp));
94 memset(SMTP, 0, sizeof(struct citsmtp));
95 SMTP->is_msa = is_msa;
96 SMTP->Cmd = NewStrBufPlain(NULL, SIZ);
97 SMTP->helo_node = NewStrBuf();
98 SMTP->from = NewStrBufPlain(NULL, SIZ);
99 SMTP->recipients = NewStrBufPlain(NULL, SIZ);
100 SMTP->OneRcpt = NewStrBufPlain(NULL, SIZ);
101 SMTP->preferred_sender_email = NULL;
102 SMTP->preferred_sender_name = NULL;
104 // If this config option is set, reject connections from problem
105 // addresses immediately instead of after they execute a RCPT
106 if ( (CtdlGetConfigInt("c_rbl_at_greeting")) && (SMTP->is_msa == 0) ) {
107 if (rbl_check(CC->cs_addr, message_to_spammer)) {
108 if (server_shutting_down)
109 cprintf("421 %s\r\n", message_to_spammer);
111 cprintf("550 %s\r\n", message_to_spammer);
112 CC->kill_me = KILLME_SPAMMER;
113 /* no need to free_recipients(valid), it's not allocated yet */
118 // Otherwise we're either clean or we check later.
120 if (CC->nologin==1) {
121 cprintf("451 Too many connections are already open; please try again later.\r\n");
122 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
123 // no need to free_recipients(valid), it's not allocated yet
127 // Note: the FQDN *must* appear as the first thing after the 220 code.
128 // Some clients (including citmail.c) depend on it being there.
129 cprintf("220 %s ESMTP Citadel server ready.\r\n", CtdlGetConfigStr("c_fqdn"));
133 // SMTPS is just like SMTP, except it goes crypto right away.
134 void smtps_greeting(void) {
135 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
137 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; // kill session if no crypto
143 // SMTP MSA port requires authentication.
144 void smtp_msa_greeting(void) {
149 // LMTP is like SMTP but with some extra bonus footage added.
150 void lmtp_greeting(void) {
157 // Generic SMTP MTA greeting
158 void smtp_mta_greeting(void) {
163 // We also have an unfiltered LMTP socket that bypasses spam filters.
164 void lmtp_unfiltered_greeting(void) {
167 SMTP->is_unfiltered = 1;
171 // Login greeting common to all auth methods
172 void smtp_auth_greeting(void) {
173 cprintf("235 Hello, %s\r\n", CC->user.fullname);
174 syslog(LOG_INFO, "serv_smtp: SMTP authenticated %s", CC->user.fullname);
175 CC->internal_pgm = 0;
176 CC->cs_flags &= ~CS_STEALTH;
180 // Implement HELO and EHLO commands.
181 // which_command: 0=HELO, 1=EHLO, 2=LHLO
182 void smtp_hello(int which_command) {
184 if (StrLength(SMTP->Cmd) >= 6) {
185 FlushStrBuf(SMTP->helo_node);
186 StrBufAppendBuf(SMTP->helo_node, SMTP->Cmd, 5);
189 if ( (which_command != LHLO) && (SMTP->is_lmtp) ) {
190 cprintf("500 Only LHLO is allowed when running LMTP\r\n");
194 if ( (which_command == LHLO) && (SMTP->is_lmtp == 0) ) {
195 cprintf("500 LHLO is only allowed when running LMTP\r\n");
199 if (which_command == HELO) {
200 cprintf("250 Hello %s (%s [%s])\r\n",
201 ChrPtr(SMTP->helo_node),
207 if (which_command == EHLO) {
208 cprintf("250-Hello %s (%s [%s])\r\n",
209 ChrPtr(SMTP->helo_node),
215 cprintf("250-Greetings and joyous salutations.\r\n");
217 cprintf("250-HELP\r\n");
218 cprintf("250-SIZE %ld\r\n", CtdlGetConfigLong("c_maxmsglen"));
221 // Offer TLS, but only if TLS is not already active.
222 // Furthermore, only offer TLS when running on
223 // the SMTP-MSA port, not on the SMTP-MTA port, due to
224 // questionable reliability of TLS in certain sending MTA's.
225 if ( (!CC->redirect_ssl) && (SMTP->is_msa) ) {
226 cprintf("250-STARTTLS\r\n");
230 cprintf("250-AUTH LOGIN PLAIN\r\n"
231 "250-AUTH=LOGIN PLAIN\r\n"
238 // Backend function for smtp_webcit_preferences_hack().
239 // Look at a message and determine if it's the preferences file.
240 void smtp_webcit_preferences_hack_backend(long msgnum, void *userdata) {
241 struct CtdlMessage *msg;
242 char **webcit_conf = (char **) userdata;
245 return; // already got it
248 msg = CtdlFetchMessage(msgnum, 1);
253 if ( !CM_IsEmpty(msg, eMsgSubject) && (!strcasecmp(msg->cm_fields[eMsgSubject], "__ WebCit Preferences __"))) {
254 // This is it! Change ownership of the message text so it doesn't get freed.
255 *webcit_conf = (char *)msg->cm_fields[eMesageText];
256 msg->cm_fields[eMesageText] = NULL;
262 // The configuration item for the user's preferred display name for outgoing email is, unfortunately,
263 // stored in the account's WebCit configuration. We have to fetch it now.
264 void smtp_webcit_preferences_hack(void) {
265 char config_roomname[ROOMNAMELEN];
266 char *webcit_conf = NULL;
268 snprintf(config_roomname, sizeof config_roomname, "%010ld.%s", CC->user.usernum, USERCONFIGROOM);
269 if (CtdlGetRoom(&CC->room, config_roomname) != 0) {
273 // Find the WebCit configuration message
274 CtdlForEachMessage(MSGS_ALL, 1, NULL, NULL, NULL, smtp_webcit_preferences_hack_backend, (void *)&webcit_conf);
280 // Parse the webcit configuration and attempt to do something useful with it
281 char *str = webcit_conf;
283 char *this_line = NULL;
284 while (this_line = strtok_r(str, "\n", &saveptr), this_line != NULL) {
286 if (!strncasecmp(this_line, "defaultfrom|", 12)) {
287 SMTP->preferred_sender_email = NewStrBufPlain(&this_line[12], -1);
289 if (!strncasecmp(this_line, "defaultname|", 12)) {
290 SMTP->preferred_sender_name = NewStrBufPlain(&this_line[12], -1);
292 if ((!strncasecmp(this_line, "defaultname|", 12)) && (SMTP->preferred_sender_name == NULL)) {
293 SMTP->preferred_sender_name = NewStrBufPlain(&this_line[12], -1);
301 // Implement HELP command.
302 void smtp_help(void) {
303 cprintf("214 RTFM http://www.ietf.org/rfc/rfc2821.txt\r\n");
307 void smtp_get_user(int offset) {
310 StrBuf *UserName = NewStrBufDup(SMTP->Cmd);
311 StrBufCutLeft(UserName, offset);
312 StrBufDecodeBase64(UserName);
314 if (CtdlLoginExistingUser(ChrPtr(UserName)) == login_ok) {
315 size_t len = CtdlEncodeBase64(buf, "Password:", 9, 0);
317 if (buf[len - 1] == '\n') {
320 cprintf("334 %s\r\n", buf);
321 SMTP->command_state = smtp_password;
324 cprintf("500 No such user.\r\n");
325 SMTP->command_state = smtp_command;
327 FreeStrBuf(&UserName);
331 void smtp_get_pass(void) {
334 memset(password, 0, sizeof(password));
335 StrBufDecodeBase64(SMTP->Cmd);
336 syslog(LOG_DEBUG, "serv_smtp: trying <%s>", password);
337 if (CtdlTryPassword(SKEY(SMTP->Cmd)) == pass_ok) {
338 smtp_auth_greeting();
341 cprintf("535 Authentication failed.\r\n");
343 SMTP->command_state = smtp_command;
347 // Back end for PLAIN auth method (either inline or multistate)
348 void smtp_try_plain(void) {
349 const char*decoded_authstring;
350 char ident[256] = "";
359 memset(pass, 0, sizeof(pass));
360 decoded_len = StrBufDecodeBase64(SMTP->Cmd);
362 if (decoded_len > 0) {
363 decoded_authstring = ChrPtr(SMTP->Cmd);
365 len = safestrncpy(ident, decoded_authstring, sizeof ident);
367 decoded_len -= len - 1;
368 decoded_authstring += len + 1;
370 if (decoded_len > 0) {
371 len = safestrncpy(user, decoded_authstring, sizeof user);
373 decoded_authstring += len + 1;
374 decoded_len -= len - 1;
377 if (decoded_len > 0) {
378 plen = safestrncpy(pass, decoded_authstring, sizeof pass);
381 plen = sizeof(pass) - 1;
385 SMTP->command_state = smtp_command;
387 if (!IsEmptyStr(ident)) {
388 result = CtdlLoginExistingUser(ident);
391 result = CtdlLoginExistingUser(user);
394 if (result == login_ok) {
395 if (CtdlTryPassword(pass, plen) == pass_ok) {
396 smtp_webcit_preferences_hack();
397 smtp_auth_greeting();
401 cprintf("504 Authentication failed.\r\n");
405 // Attempt to perform authenticated SMTP
406 void smtp_auth(void) {
407 char username_prompt[64];
409 char encoded_authstring[1024];
412 cprintf("504 Already logged in.\r\n");
416 if (StrLength(SMTP->Cmd) < 6) {
417 cprintf("501 Syntax error\r\n");
421 extract_token(method, ChrPtr(SMTP->Cmd) + 5, 0, ' ', sizeof method);
423 if (!strncasecmp(method, "login", 5) ) {
424 if (StrLength(SMTP->Cmd) >= 12) {
425 syslog(LOG_DEBUG, "serv_smtp: username <%s> supplied inline", ChrPtr(SMTP->Cmd)+11);
429 size_t len = CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
430 if (username_prompt[len - 1] == '\n') {
431 username_prompt[len - 1] = '\0';
433 cprintf("334 %s\r\n", username_prompt);
434 SMTP->command_state = smtp_user;
439 if (!strncasecmp(method, "plain", 5) ) {
441 if (num_tokens(ChrPtr(SMTP->Cmd) + 5, ' ') < 2) {
443 SMTP->command_state = smtp_plain;
447 len = extract_token(encoded_authstring,
448 ChrPtr(SMTP->Cmd) + 5,
450 sizeof encoded_authstring);
451 StrBufPlain(SMTP->Cmd, encoded_authstring, len);
456 cprintf("504 Unknown authentication method.\r\n");
461 // Implements the RSET (reset state) command.
462 // Currently this just zeroes out the state buffer. If pointers to data
463 // allocated with malloc() are ever placed in the state buffer, we have to
464 // be sure to free() them first!
466 // Set do_response to nonzero to output the SMTP RSET response code.
467 void smtp_rset(int do_response) {
468 FlushStrBuf(SMTP->Cmd);
469 FlushStrBuf(SMTP->helo_node);
470 FlushStrBuf(SMTP->from);
471 FlushStrBuf(SMTP->recipients);
472 FlushStrBuf(SMTP->OneRcpt);
474 SMTP->command_state = 0;
475 SMTP->number_of_recipients = 0;
476 SMTP->delivery_mode = 0;
477 SMTP->message_originated_locally = 0;
479 // is_lmtp and is_unfiltered should not be cleared.
482 cprintf("250 Zap!\r\n");
487 // Clear out the portions of the state buffer that need to be cleared out
488 // after the DATA command finishes.
489 void smtp_data_clear(void) {
490 FlushStrBuf(SMTP->from);
491 FlushStrBuf(SMTP->recipients);
492 FlushStrBuf(SMTP->OneRcpt);
493 SMTP->number_of_recipients = 0;
494 SMTP->delivery_mode = 0;
495 SMTP->message_originated_locally = 0;
499 // Implements the "MAIL FROM:" command
500 void smtp_mail(void) {
505 if (StrLength(SMTP->from) > 0) {
506 cprintf("503 Only one sender permitted\r\n");
510 if (StrLength(SMTP->Cmd) < 6) {
511 cprintf("501 Syntax error\r\n");
515 if (strncasecmp(ChrPtr(SMTP->Cmd) + 5, "From:", 5)) {
516 cprintf("501 Syntax error\r\n");
520 StrBufAppendBuf(SMTP->from, SMTP->Cmd, 5);
521 StrBufTrim(SMTP->from);
522 if (strchr(ChrPtr(SMTP->from), '<') != NULL) {
523 StrBufStripAllBut(SMTP->from, '<', '>');
526 // We used to reject empty sender names, until it was brought to our
527 // attention that RFC1123 5.2.9 requires that this be allowed. So now
528 // we allow it, but replace the empty string with a fake
529 // address so we don't have to contend with the empty string causing
530 // other code to fail when it's expecting something there.
531 if (StrLength(SMTP->from) == 0) {
532 StrBufPlain(SMTP->from, HKEY("someone@example.com"));
535 // If this SMTP connection is from a logged-in user, force the 'from'
536 // to be the user's Internet e-mail address as Citadel knows it.
538 StrBufPlain(SMTP->from, CC->cs_inet_email, -1);
539 cprintf("250 Sender ok <%s>\r\n", ChrPtr(SMTP->from));
540 SMTP->message_originated_locally = 1;
544 else if (SMTP->is_lmtp) {
545 // Bypass forgery checking for LMTP
548 // Otherwise, make sure outsiders aren't trying to forge mail from
549 // this system (unless, of course, c_allow_spoofing is enabled)
550 else if (CtdlGetConfigInt("c_allow_spoofing") == 0) {
551 process_rfc822_addr(ChrPtr(SMTP->from), user, node, name);
552 syslog(LOG_DEBUG, "serv_smtp: claimed envelope sender is '%s' == '%s' @ '%s' ('%s')",
553 ChrPtr(SMTP->from), user, node, name
555 if (CtdlHostAlias(node) != hostalias_nomatch) {
556 cprintf("550 You must log in to send mail from %s\r\n", node);
557 FlushStrBuf(SMTP->from);
558 syslog(LOG_DEBUG, "serv_smtp: rejecting unauthenticated mail from %s", node);
563 cprintf("250 Sender ok\r\n");
567 // Implements the "RCPT To:" command
568 void smtp_rcpt(void) {
569 char message_to_spammer[SIZ];
570 struct recptypes *valid = NULL;
572 if (StrLength(SMTP->from) == 0) {
573 cprintf("503 Need MAIL before RCPT\r\n");
577 if (StrLength(SMTP->Cmd) < 6) {
578 cprintf("501 Syntax error\r\n");
582 if (strncasecmp(ChrPtr(SMTP->Cmd) + 5, "To:", 3)) {
583 cprintf("501 Syntax error\r\n");
587 if ( (SMTP->is_msa) && (!CC->logged_in) ) {
588 cprintf("550 You must log in to send mail on this port.\r\n");
589 FlushStrBuf(SMTP->from);
593 FlushStrBuf(SMTP->OneRcpt);
594 StrBufAppendBuf(SMTP->OneRcpt, SMTP->Cmd, 8);
595 StrBufTrim(SMTP->OneRcpt);
596 StrBufStripAllBut(SMTP->OneRcpt, '<', '>');
598 if ( (StrLength(SMTP->OneRcpt) + StrLength(SMTP->recipients)) >= SIZ) {
599 cprintf("452 Too many recipients\r\n");
605 (!CC->logged_in) // Don't RBL authenticated users
606 && (!SMTP->is_lmtp) // Don't RBL LMTP clients
607 && (CtdlGetConfigInt("c_rbl_at_greeting") == 0) // Don't RBL if we did it at connection time
608 && (rbl_check(CC->cs_addr, message_to_spammer))
610 cprintf("550 %s\r\n", message_to_spammer);
611 return; // no need to free_recipients(valid)
612 } // because it hasn't been allocated yet
614 // This is a *preliminary* call to validate_recipients() to evaluate one recipient.
615 valid = validate_recipients(
616 (char *)ChrPtr(SMTP->OneRcpt),
617 smtp_get_Recipients(),
618 (SMTP->is_lmtp)? POST_LMTP: (CC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
621 // Any type of error thrown by validate_recipients() will make the SMTP transaction fail at this point.
622 if (valid->num_error != 0) {
623 cprintf("550 %s\r\n", valid->errormsg);
624 free_recipients(valid);
629 (valid->num_internet > 0) // If it's outbound Internet mail...
630 && (CC->logged_in) // ...and we're a logged-in user...
631 && (CtdlCheckInternetMailPermission(&CC->user)==0) // ...who does not have Internet mail rights...
633 cprintf("551 <%s> - you do not have permission to send Internet mail\r\n", ChrPtr(SMTP->OneRcpt));
634 free_recipients(valid);
639 (valid->num_internet > 0) // If it's outbound Internet mail...
640 && (SMTP->message_originated_locally == 0) // ...and also inbound Internet mail...
641 && (SMTP->is_lmtp == 0) /// ...and didn't arrive via LMTP...
643 cprintf("551 <%s> - relaying denied\r\n", ChrPtr(SMTP->OneRcpt));
644 free_recipients(valid);
649 (valid->num_room > 0) // If it's mail to a room (mailing list)...
650 && (SMTP->message_originated_locally == 0) // ...and also inbound Internet mail...
651 && (is_email_subscribed_to_list((char *)ChrPtr(SMTP->from), valid->recp_room) == 0) // ...and not a subscriber
653 cprintf("551 <%s> - The message is not from a list member\r\n", ChrPtr(SMTP->OneRcpt));
654 free_recipients(valid);
658 cprintf("250 RCPT ok <%s>\r\n", ChrPtr(SMTP->OneRcpt));
659 if (StrLength(SMTP->recipients) > 0) {
660 StrBufAppendBufPlain(SMTP->recipients, HKEY(","), 0);
662 StrBufAppendBuf(SMTP->recipients, SMTP->OneRcpt, 0);
663 SMTP->number_of_recipients ++;
665 free_recipients(valid);
670 // Implements the DATA command
671 void smtp_data(void) {
674 struct CtdlMessage *msg = NULL;
677 struct recptypes *valid;
681 if (StrLength(SMTP->from) == 0) {
682 cprintf("503 Need MAIL command first.\r\n");
686 if (SMTP->number_of_recipients < 1) {
687 cprintf("503 Need RCPT command first.\r\n");
691 cprintf("354 Transmit message now - terminate with '.' by itself\r\n");
693 datestring(nowstamp, sizeof nowstamp, time(NULL), DATESTRING_RFC822);
694 defbody = NewStrBufPlain(NULL, SIZ);
696 if (defbody != NULL) {
697 if (SMTP->is_lmtp && (CC->cs_UDSclientUID != -1)) {
700 "Received: from %s (Citadel from userid %ld)\n"
702 ChrPtr(SMTP->helo_node),
703 (long int) CC->cs_UDSclientUID,
704 CtdlGetConfigStr("c_fqdn"),
710 "Received: from %s (%s [%s])\n"
712 ChrPtr(SMTP->helo_node),
715 CtdlGetConfigStr("c_fqdn"),
719 body = CtdlReadMessageBodyBuf(HKEY("."), CtdlGetConfigLong("c_maxmsglen"), defbody, 1);
720 FreeStrBuf(&defbody);
722 cprintf("550 Unable to save message: internal error.\r\n");
726 syslog(LOG_DEBUG, "serv_smtp: converting message...");
727 msg = convert_internet_message_buf(&body);
729 // If the user is locally authenticated, FORCE the From: header to
730 // show up as the real sender. Yes, this violates the RFC standard,
731 // but IT MAKES SENSE. If you prefer strict RFC adherence over
732 // common sense, you can disable this in the configuration.
734 // We also set the "message room name" ('O' field) to MAILROOM
735 // (which is Mail> on most systems) to prevent it from getting set
736 // to something ugly like "0000058008.Sent Items>" when the message
737 // is read with a Citadel client.
739 if ( (CC->logged_in) && (CtdlGetConfigInt("c_rfc822_strict_from") != CFG_SMTP_FROM_NOFILTER) ) {
742 if (!CM_IsEmpty(msg, erFc822Addr) &&
743 ((CtdlGetConfigInt("c_rfc822_strict_from") == CFG_SMTP_FROM_CORRECT) ||
744 (CtdlGetConfigInt("c_rfc822_strict_from") == CFG_SMTP_FROM_REJECT) ) )
746 if (!IsEmptyStr(CC->cs_inet_email))
747 validemail = strcmp(CC->cs_inet_email, msg->cm_fields[erFc822Addr]) == 0;
749 (!IsEmptyStr(CC->cs_inet_other_emails)))
751 int num_secondary_emails = 0;
753 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
754 for (i=0; i < num_secondary_emails && !validemail; ++i) {
756 extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
757 validemail = strcmp(buf, msg->cm_fields[erFc822Addr]) == 0;
762 if (!validemail && (CtdlGetConfigInt("c_rfc822_strict_from") == CFG_SMTP_FROM_REJECT)) {
763 syslog(LOG_ERR, "serv_smtp: invalid sender '%s' - rejecting this message", msg->cm_fields[erFc822Addr]);
764 cprintf("550 Invalid sender '%s' - rejecting this message.\r\n", msg->cm_fields[erFc822Addr]);
768 CM_SetField(msg, eOriginalRoom, HKEY(MAILROOM));
769 if (SMTP->preferred_sender_name != NULL)
770 CM_SetField(msg, eAuthor, SKEY(SMTP->preferred_sender_name));
772 CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
775 if (SMTP->preferred_sender_email != NULL) {
776 CM_SetField(msg, erFc822Addr, SKEY(SMTP->preferred_sender_email));
779 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, strlen(CC->cs_inet_email));
784 // Set the "envelope from" address
785 CM_SetField(msg, eMessagePath, SKEY(SMTP->from));
787 // Set the "envelope to" address
788 CM_SetField(msg, eenVelopeTo, SKEY(SMTP->recipients));
790 // Submit the message into the Citadel system.
791 valid = validate_recipients(
792 (char *)ChrPtr(SMTP->recipients),
793 smtp_get_Recipients(),
794 (SMTP->is_lmtp)? POST_LMTP: (CC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
797 // If there are modules that want to scan this message before final
798 // submission (such as virus checkers or spam filters), call them now
799 // and give them an opportunity to reject the message.
800 if (SMTP->is_unfiltered) {
804 scan_errors = PerformMessageHooks(msg, valid, EVT_SMTPSCAN);
807 if (scan_errors > 0) { // We don't want this message!
809 if (CM_IsEmpty(msg, eErrorMsg)) {
810 CM_SetField(msg, eErrorMsg, HKEY("Message rejected by filter"));
813 StrBufPrintf(SMTP->OneRcpt, "550 %s\r\n", msg->cm_fields[eErrorMsg]);
816 else { // Ok, we'll accept this message.
817 msgnum = CtdlSubmitMsg(msg, valid, "");
819 StrBufPrintf(SMTP->OneRcpt, "250 Message accepted.\r\n");
822 StrBufPrintf(SMTP->OneRcpt, "550 Internal delivery error\r\n");
826 // For SMTP and ESMTP, just print the result message. For LMTP, we
827 // have to print one result message for each recipient. Since there
828 // is nothing in Citadel which would cause different recipients to
829 // have different results, we can get away with just spitting out the
830 // same message once for each recipient.
832 for (i=0; i<SMTP->number_of_recipients; ++i) {
833 cputbuf(SMTP->OneRcpt);
837 cputbuf(SMTP->OneRcpt);
840 // Write something to the syslog(which may or may not be where the
841 // rest of the Citadel logs are going; some sysadmins want LOG_MAIL).
842 syslog((LOG_MAIL | LOG_INFO),
843 "%ld: from=<%s>, nrcpts=%d, relay=%s [%s], stat=%s",
846 SMTP->number_of_recipients,
849 ChrPtr(SMTP->OneRcpt)
854 free_recipients(valid);
855 smtp_data_clear(); // clear out the buffers now
859 // Implements the STARTTLS command
860 void smtp_starttls(void) {
861 char ok_response[SIZ];
862 char nosup_response[SIZ];
863 char error_response[SIZ];
865 sprintf(ok_response, "220 Begin TLS negotiation now\r\n");
866 sprintf(nosup_response, "554 TLS not supported here\r\n");
867 sprintf(error_response, "554 Internal error\r\n");
868 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
873 // Implements the NOOP (NO OPeration) command
874 void smtp_noop(void) {
875 cprintf("250 NOOP\r\n");
879 // Implements the QUIT command
880 void smtp_quit(void) {
881 cprintf("221 Goodbye...\r\n");
882 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
886 // Main command loop for SMTP server sessions.
887 void smtp_command_loop(void) {
888 static const ConstStr AuthPlainStr = {HKEY("AUTH PLAIN")};
891 syslog(LOG_ERR, "serv_smtp: Session SMTP data is null. WTF? We will crash now.");
896 if (CtdlClientGetLine(SMTP->Cmd) < 1) {
897 syslog(LOG_INFO, "SMTP: client disconnected: ending session.");
898 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
902 if (SMTP->command_state == smtp_user) {
903 if (!strncmp(ChrPtr(SMTP->Cmd), AuthPlainStr.Key, AuthPlainStr.len)) {
912 else if (SMTP->command_state == smtp_password) {
917 else if (SMTP->command_state == smtp_plain) {
922 syslog(LOG_DEBUG, "serv_smtp: client sent command <%s>", ChrPtr(SMTP->Cmd));
924 if (!strncasecmp(ChrPtr(SMTP->Cmd), "NOOP", 4)) {
929 if (!strncasecmp(ChrPtr(SMTP->Cmd), "QUIT", 4)) {
934 if (!strncasecmp(ChrPtr(SMTP->Cmd), "HELO", 4)) {
939 if (!strncasecmp(ChrPtr(SMTP->Cmd), "EHLO", 4)) {
944 if (!strncasecmp(ChrPtr(SMTP->Cmd), "LHLO", 4)) {
949 if (!strncasecmp(ChrPtr(SMTP->Cmd), "RSET", 4)) {
954 if (!strncasecmp(ChrPtr(SMTP->Cmd), "AUTH", 4)) {
959 if (!strncasecmp(ChrPtr(SMTP->Cmd), "DATA", 4)) {
964 if (!strncasecmp(ChrPtr(SMTP->Cmd), "HELP", 4)) {
969 if (!strncasecmp(ChrPtr(SMTP->Cmd), "MAIL", 4)) {
974 if (!strncasecmp(ChrPtr(SMTP->Cmd), "RCPT", 4)) {
979 if (!strncasecmp(ChrPtr(SMTP->Cmd), "STARTTLS", 8)) {
985 cprintf("502 I'm afraid I can't do that.\r\n");
989 /*****************************************************************************/
990 /* MODULE INITIALIZATION STUFF */
991 /*****************************************************************************/
993 * This cleanup function blows away the temporary memory used by
996 void smtp_cleanup_function(void)
998 /* Don't do this stuff if this is not an SMTP session! */
999 if (CC->h_command_function != smtp_command_loop) return;
1001 syslog(LOG_DEBUG, "Performing SMTP cleanup hook");
1003 FreeStrBuf(&SMTP->Cmd);
1004 FreeStrBuf(&SMTP->helo_node);
1005 FreeStrBuf(&SMTP->from);
1006 FreeStrBuf(&SMTP->recipients);
1007 FreeStrBuf(&SMTP->OneRcpt);
1008 FreeStrBuf(&SMTP->preferred_sender_email);
1009 FreeStrBuf(&SMTP->preferred_sender_name);
1014 const char *CitadelServiceSMTP_MTA="SMTP-MTA";
1015 const char *CitadelServiceSMTPS_MTA="SMTPs-MTA";
1016 const char *CitadelServiceSMTP_MSA="SMTP-MSA";
1017 const char *CitadelServiceSMTP_LMTP="LMTP";
1018 const char *CitadelServiceSMTP_LMTP_UNF="LMTP-UnF";
1021 // Initialization function, called from modules_init.c
1022 char *ctdl_module_init_smtp(void) {
1024 CtdlRegisterServiceHook(CtdlGetConfigInt("c_smtp_port"), /* SMTP MTA */
1029 CitadelServiceSMTP_MTA);
1032 CtdlRegisterServiceHook(CtdlGetConfigInt("c_smtps_port"), /* SMTPS MTA */
1037 CitadelServiceSMTPS_MTA);
1040 CtdlRegisterServiceHook(CtdlGetConfigInt("c_msa_port"), /* SMTP MSA */
1045 CitadelServiceSMTP_MSA);
1047 CtdlRegisterServiceHook(0, /* local LMTP */
1052 CitadelServiceSMTP_LMTP);
1054 CtdlRegisterServiceHook(0, /* local LMTP */
1055 file_lmtp_unfiltered_socket,
1056 lmtp_unfiltered_greeting,
1059 CitadelServiceSMTP_LMTP_UNF);
1061 CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1064 /* return our module name for the log */