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-2021 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.
41 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <libcitadel.h>
54 #include "citserver.h"
62 #include "internet_addressing.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 */
87 * Here's where our SMTP session begins its happy day.
89 void smtp_greeting(int is_msa) {
90 char message_to_spammer[1024];
92 strcpy(CC->cs_clientname, "SMTP session");
94 CC->cs_flags |= CS_STEALTH;
95 CC->session_specific_data = malloc(sizeof(struct citsmtp));
96 memset(SMTP, 0, sizeof(struct citsmtp));
97 SMTP->is_msa = is_msa;
98 SMTP->Cmd = NewStrBufPlain(NULL, SIZ);
99 SMTP->helo_node = NewStrBuf();
100 SMTP->from = NewStrBufPlain(NULL, SIZ);
101 SMTP->recipients = NewStrBufPlain(NULL, SIZ);
102 SMTP->OneRcpt = NewStrBufPlain(NULL, SIZ);
103 SMTP->preferred_sender_email = NULL;
104 SMTP->preferred_sender_name = NULL;
106 /* If this config option is set, reject connections from problem
107 * addresses immediately instead of after they execute a RCPT
109 if ( (CtdlGetConfigInt("c_rbl_at_greeting")) && (SMTP->is_msa == 0) ) {
110 if (rbl_check(CC->cs_addr, message_to_spammer)) {
111 if (server_shutting_down)
112 cprintf("421 %s\r\n", message_to_spammer);
114 cprintf("550 %s\r\n", message_to_spammer);
115 CC->kill_me = KILLME_SPAMMER;
116 /* no need to free_recipients(valid), it's not allocated yet */
121 /* Otherwise we're either clean or we check later. */
123 if (CC->nologin==1) {
124 cprintf("451 Too many connections are already open; please try again later.\r\n");
125 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
126 /* no need to free_recipients(valid), it's not allocated yet */
130 /* Note: the FQDN *must* appear as the first thing after the 220 code.
131 * Some clients (including citmail.c) depend on it being there.
133 cprintf("220 %s ESMTP Citadel server ready.\r\n", CtdlGetConfigStr("c_fqdn"));
138 * SMTPS is just like SMTP, except it goes crypto right away.
140 void smtps_greeting(void) {
141 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
143 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
150 * SMTP MSA port requires authentication.
152 void smtp_msa_greeting(void) {
158 * LMTP is like SMTP but with some extra bonus footage added.
160 void lmtp_greeting(void) {
168 * Generic SMTP MTA greeting
170 void smtp_mta_greeting(void) {
176 * We also have an unfiltered LMTP socket that bypasses spam filters.
178 void lmtp_unfiltered_greeting(void) {
181 SMTP->is_unfiltered = 1;
186 * Login greeting common to all auth methods
188 void smtp_auth_greeting(void) {
189 cprintf("235 Hello, %s\r\n", CC->user.fullname);
190 syslog(LOG_INFO, "serv_smtp: SMTP authenticated %s", CC->user.fullname);
191 CC->internal_pgm = 0;
192 CC->cs_flags &= ~CS_STEALTH;
197 * Implement HELO and EHLO commands.
199 * which_command: 0=HELO, 1=EHLO, 2=LHLO
201 void smtp_hello(int which_command) {
203 if (StrLength(SMTP->Cmd) >= 6) {
204 StrBufAppendBuf(SMTP->helo_node, SMTP->Cmd, 5);
207 if ( (which_command != LHLO) && (SMTP->is_lmtp) ) {
208 cprintf("500 Only LHLO is allowed when running LMTP\r\n");
212 if ( (which_command == LHLO) && (SMTP->is_lmtp == 0) ) {
213 cprintf("500 LHLO is only allowed when running LMTP\r\n");
217 if (which_command == HELO) {
218 cprintf("250 Hello %s (%s [%s])\r\n",
219 ChrPtr(SMTP->helo_node),
225 if (which_command == EHLO) {
226 cprintf("250-Hello %s (%s [%s])\r\n",
227 ChrPtr(SMTP->helo_node),
233 cprintf("250-Greetings and joyous salutations.\r\n");
235 cprintf("250-HELP\r\n");
236 cprintf("250-SIZE %ld\r\n", CtdlGetConfigLong("c_maxmsglen"));
240 * Offer TLS, but only if TLS is not already active.
241 * Furthermore, only offer TLS when running on
242 * the SMTP-MSA port, not on the SMTP-MTA port, due to
243 * questionable reliability of TLS in certain sending MTA's.
245 if ( (!CC->redirect_ssl) && (SMTP->is_msa) ) {
246 cprintf("250-STARTTLS\r\n");
248 #endif /* HAVE_OPENSSL */
250 cprintf("250-AUTH LOGIN PLAIN\r\n"
251 "250-AUTH=LOGIN PLAIN\r\n"
259 * Backend function for smtp_webcit_preferences_hack().
260 * Look at a message and determine if it's the preferences file.
262 void smtp_webcit_preferences_hack_backend(long msgnum, void *userdata) {
263 struct CtdlMessage *msg;
264 char **webcit_conf = (char **) userdata;
267 return; // already got it
270 msg = CtdlFetchMessage(msgnum, 1);
275 if ( !CM_IsEmpty(msg, eMsgSubject) && (!strcasecmp(msg->cm_fields[eMsgSubject], "__ WebCit Preferences __"))) {
276 /* This is it! Change ownership of the message text so it doesn't get freed. */
277 *webcit_conf = (char *)msg->cm_fields[eMesageText];
278 msg->cm_fields[eMesageText] = NULL;
285 * The configuration item for the user's preferred display name for outgoing email is, unfortunately,
286 * stored in the account's WebCit configuration. We have to fetch it now.
288 void smtp_webcit_preferences_hack(void) {
289 char config_roomname[ROOMNAMELEN];
290 char *webcit_conf = NULL;
292 snprintf(config_roomname, sizeof config_roomname, "%010ld.%s", CC->user.usernum, USERCONFIGROOM);
293 if (CtdlGetRoom(&CC->room, config_roomname) != 0) {
298 * Find the WebCit configuration message
301 CtdlForEachMessage(MSGS_ALL, 1, NULL, NULL, NULL, smtp_webcit_preferences_hack_backend, (void *)&webcit_conf);
307 /* Parse the webcit configuration and attempt to do something useful with it */
308 char *str = webcit_conf;
310 char *this_line = NULL;
311 while (this_line = strtok_r(str, "\n", &saveptr), this_line != NULL) {
313 if (!strncasecmp(this_line, "defaultfrom|", 12)) {
314 SMTP->preferred_sender_email = NewStrBufPlain(&this_line[12], -1);
316 if (!strncasecmp(this_line, "defaultname|", 12)) {
317 SMTP->preferred_sender_name = NewStrBufPlain(&this_line[12], -1);
319 if ((!strncasecmp(this_line, "defaultname|", 12)) && (SMTP->preferred_sender_name == NULL)) {
320 SMTP->preferred_sender_name = NewStrBufPlain(&this_line[12], -1);
329 * Implement HELP command.
331 void smtp_help(void) {
332 cprintf("214 RTFM http://www.ietf.org/rfc/rfc2821.txt\r\n");
339 void smtp_get_user(int offset) {
342 StrBuf *UserName = NewStrBufDup(SMTP->Cmd);
343 StrBufCutLeft(UserName, offset);
344 StrBufDecodeBase64(UserName);
346 if (CtdlLoginExistingUser(ChrPtr(UserName)) == login_ok) {
347 size_t len = CtdlEncodeBase64(buf, "Password:", 9, 0);
349 if (buf[len - 1] == '\n') {
352 cprintf("334 %s\r\n", buf);
353 SMTP->command_state = smtp_password;
356 cprintf("500 No such user.\r\n");
357 SMTP->command_state = smtp_command;
359 FreeStrBuf(&UserName);
366 void smtp_get_pass(void)
370 memset(password, 0, sizeof(password));
371 StrBufDecodeBase64(SMTP->Cmd);
372 syslog(LOG_DEBUG, "serv_smtp: trying <%s>", password);
373 if (CtdlTryPassword(SKEY(SMTP->Cmd)) == pass_ok) {
374 smtp_auth_greeting();
377 cprintf("535 Authentication failed.\r\n");
379 SMTP->command_state = smtp_command;
384 * Back end for PLAIN auth method (either inline or multistate)
386 void smtp_try_plain(void) {
387 const char*decoded_authstring;
388 char ident[256] = "";
397 memset(pass, 0, sizeof(pass));
398 decoded_len = StrBufDecodeBase64(SMTP->Cmd);
400 if (decoded_len > 0) {
401 decoded_authstring = ChrPtr(SMTP->Cmd);
403 len = safestrncpy(ident, decoded_authstring, sizeof ident);
405 decoded_len -= len - 1;
406 decoded_authstring += len + 1;
408 if (decoded_len > 0) {
409 len = safestrncpy(user, decoded_authstring, sizeof user);
411 decoded_authstring += len + 1;
412 decoded_len -= len - 1;
415 if (decoded_len > 0) {
416 plen = safestrncpy(pass, decoded_authstring, sizeof pass);
419 plen = sizeof(pass) - 1;
423 SMTP->command_state = smtp_command;
425 if (!IsEmptyStr(ident)) {
426 result = CtdlLoginExistingUser(ident);
429 result = CtdlLoginExistingUser(user);
432 if (result == login_ok) {
433 if (CtdlTryPassword(pass, plen) == pass_ok) {
434 smtp_webcit_preferences_hack();
435 smtp_auth_greeting();
439 cprintf("504 Authentication failed.\r\n");
444 * Attempt to perform authenticated SMTP
446 void smtp_auth(void) {
447 char username_prompt[64];
449 char encoded_authstring[1024];
452 cprintf("504 Already logged in.\r\n");
456 if (StrLength(SMTP->Cmd) < 6) {
457 cprintf("501 Syntax error\r\n");
461 extract_token(method, ChrPtr(SMTP->Cmd) + 5, 0, ' ', sizeof method);
463 if (!strncasecmp(method, "login", 5) ) {
464 if (StrLength(SMTP->Cmd) >= 12) {
465 syslog(LOG_DEBUG, "serv_smtp: username <%s> supplied inline", ChrPtr(SMTP->Cmd)+11);
469 size_t len = CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
470 if (username_prompt[len - 1] == '\n') {
471 username_prompt[len - 1] = '\0';
473 cprintf("334 %s\r\n", username_prompt);
474 SMTP->command_state = smtp_user;
479 if (!strncasecmp(method, "plain", 5) ) {
481 if (num_tokens(ChrPtr(SMTP->Cmd) + 5, ' ') < 2) {
483 SMTP->command_state = smtp_plain;
487 len = extract_token(encoded_authstring,
488 ChrPtr(SMTP->Cmd) + 5,
490 sizeof encoded_authstring);
491 StrBufPlain(SMTP->Cmd, encoded_authstring, len);
496 cprintf("504 Unknown authentication method.\r\n");
502 * Implements the RSET (reset state) command.
503 * Currently this just zeroes out the state buffer. If pointers to data
504 * allocated with malloc() are ever placed in the state buffer, we have to
505 * be sure to free() them first!
507 * Set do_response to nonzero to output the SMTP RSET response code.
509 void smtp_rset(int do_response) {
511 * Our entire SMTP state is discarded when a RSET command is issued,
512 * but we need to preserve this one little piece of information, so
513 * we save it for later.
516 FlushStrBuf(SMTP->Cmd);
517 FlushStrBuf(SMTP->helo_node);
518 FlushStrBuf(SMTP->from);
519 FlushStrBuf(SMTP->recipients);
520 FlushStrBuf(SMTP->OneRcpt);
522 SMTP->command_state = 0;
523 SMTP->number_of_recipients = 0;
524 SMTP->delivery_mode = 0;
525 SMTP->message_originated_locally = 0;
528 * we must remember is_lmtp & is_unfiltered.
532 * It is somewhat ambiguous whether we want to log out when a RSET
533 * command is issued. Here's the code to do it. It is commented out
534 * because some clients (such as Pine) issue RSET commands before
535 * each message, but still expect to be logged in.
537 * if (CC->logged_in) {
543 cprintf("250 Zap!\r\n");
549 * Clear out the portions of the state buffer that need to be cleared out
550 * after the DATA command finishes.
552 void smtp_data_clear(void) {
553 FlushStrBuf(SMTP->from);
554 FlushStrBuf(SMTP->recipients);
555 FlushStrBuf(SMTP->OneRcpt);
556 SMTP->number_of_recipients = 0;
557 SMTP->delivery_mode = 0;
558 SMTP->message_originated_locally = 0;
563 * Implements the "MAIL FROM:" command
565 void smtp_mail(void) {
570 if (StrLength(SMTP->from) > 0) {
571 cprintf("503 Only one sender permitted\r\n");
575 if (StrLength(SMTP->Cmd) < 6) {
576 cprintf("501 Syntax error\r\n");
580 if (strncasecmp(ChrPtr(SMTP->Cmd) + 5, "From:", 5)) {
581 cprintf("501 Syntax error\r\n");
585 StrBufAppendBuf(SMTP->from, SMTP->Cmd, 5);
586 StrBufTrim(SMTP->from);
587 if (strchr(ChrPtr(SMTP->from), '<') != NULL) {
588 StrBufStripAllBut(SMTP->from, '<', '>');
591 /* We used to reject empty sender names, until it was brought to our
592 * attention that RFC1123 5.2.9 requires that this be allowed. So now
593 * we allow it, but replace the empty string with a fake
594 * address so we don't have to contend with the empty string causing
595 * other code to fail when it's expecting something there.
597 if (StrLength(SMTP->from) == 0) {
598 StrBufPlain(SMTP->from, HKEY("someone@example.com"));
601 /* If this SMTP connection is from a logged-in user, force the 'from'
602 * to be the user's Internet e-mail address as Citadel knows it.
605 StrBufPlain(SMTP->from, CC->cs_inet_email, -1);
606 cprintf("250 Sender ok <%s>\r\n", ChrPtr(SMTP->from));
607 SMTP->message_originated_locally = 1;
611 else if (SMTP->is_lmtp) {
612 /* Bypass forgery checking for LMTP */
615 /* Otherwise, make sure outsiders aren't trying to forge mail from
616 * this system (unless, of course, c_allow_spoofing is enabled)
618 else if (CtdlGetConfigInt("c_allow_spoofing") == 0) {
619 process_rfc822_addr(ChrPtr(SMTP->from), user, node, name);
620 syslog(LOG_DEBUG, "serv_smtp: claimed envelope sender is '%s' == '%s' @ '%s' ('%s')",
621 ChrPtr(SMTP->from), user, node, name
623 if (CtdlHostAlias(node) != hostalias_nomatch) {
624 cprintf("550 You must log in to send mail from %s\r\n", node);
625 FlushStrBuf(SMTP->from);
626 syslog(LOG_DEBUG, "serv_smtp: rejecting unauthenticated mail from %s", node);
631 cprintf("250 Sender ok\r\n");
636 * Implements the "RCPT To:" command
638 void smtp_rcpt(void) {
639 char message_to_spammer[SIZ];
640 struct recptypes *valid = NULL;
642 if (StrLength(SMTP->from) == 0) {
643 cprintf("503 Need MAIL before RCPT\r\n");
647 if (StrLength(SMTP->Cmd) < 6) {
648 cprintf("501 Syntax error\r\n");
652 if (strncasecmp(ChrPtr(SMTP->Cmd) + 5, "To:", 3)) {
653 cprintf("501 Syntax error\r\n");
657 if ( (SMTP->is_msa) && (!CC->logged_in) ) {
658 cprintf("550 You must log in to send mail on this port.\r\n");
659 FlushStrBuf(SMTP->from);
663 FlushStrBuf(SMTP->OneRcpt);
664 StrBufAppendBuf(SMTP->OneRcpt, SMTP->Cmd, 8);
665 StrBufTrim(SMTP->OneRcpt);
666 StrBufStripAllBut(SMTP->OneRcpt, '<', '>');
668 if ( (StrLength(SMTP->OneRcpt) + StrLength(SMTP->recipients)) >= SIZ) {
669 cprintf("452 Too many recipients\r\n");
675 (!CC->logged_in) // Don't RBL authenticated users
676 && (!SMTP->is_lmtp) // Don't RBL LMTP clients
677 && (CtdlGetConfigInt("c_rbl_at_greeting") == 0) // Don't RBL if we did it at connection time
678 && (rbl_check(CC->cs_addr, message_to_spammer))
680 cprintf("550 %s\r\n", message_to_spammer);
681 return; // no need to free_recipients(valid)
682 } // because it hasn't been allocated yet
684 // This is a *preliminary* call to validate_recipients() to evaluate one recipient.
685 valid = validate_recipients(
686 (char *)ChrPtr(SMTP->OneRcpt),
687 smtp_get_Recipients(),
688 (SMTP->is_lmtp)? POST_LMTP: (CC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
691 // Any type of error thrown by validate_recipients() will make the SMTP transaction fail at this point.
692 if (valid->num_error != 0) {
693 cprintf("550 %s\r\n", valid->errormsg);
694 free_recipients(valid);
699 (valid->num_internet > 0) // If it's outbound Internet mail...
700 && (CC->logged_in) // ...and we're a logged-in user...
701 && (CtdlCheckInternetMailPermission(&CC->user)==0) // ...who does not have Internet mail rights...
703 cprintf("551 <%s> - you do not have permission to send Internet mail\r\n", ChrPtr(SMTP->OneRcpt));
704 free_recipients(valid);
708 if (valid->num_internet > 0) {
709 if ( (SMTP->message_originated_locally == 0)
710 && (SMTP->is_lmtp == 0) ) {
711 cprintf("551 <%s> - relaying denied\r\n", ChrPtr(SMTP->OneRcpt));
712 free_recipients(valid);
717 cprintf("250 RCPT ok <%s>\r\n", ChrPtr(SMTP->OneRcpt));
718 if (StrLength(SMTP->recipients) > 0) {
719 StrBufAppendBufPlain(SMTP->recipients, HKEY(","), 0);
721 StrBufAppendBuf(SMTP->recipients, SMTP->OneRcpt, 0);
722 SMTP->number_of_recipients ++;
724 free_recipients(valid);
730 * Implements the DATA command
732 void smtp_data(void) {
735 struct CtdlMessage *msg = NULL;
738 struct recptypes *valid;
742 if (StrLength(SMTP->from) == 0) {
743 cprintf("503 Need MAIL command first.\r\n");
747 if (SMTP->number_of_recipients < 1) {
748 cprintf("503 Need RCPT command first.\r\n");
752 cprintf("354 Transmit message now - terminate with '.' by itself\r\n");
754 datestring(nowstamp, sizeof nowstamp, time(NULL), DATESTRING_RFC822);
755 defbody = NewStrBufPlain(NULL, SIZ);
757 if (defbody != NULL) {
758 if (SMTP->is_lmtp && (CC->cs_UDSclientUID != -1)) {
761 "Received: from %s (Citadel from userid %ld)\n"
763 ChrPtr(SMTP->helo_node),
764 (long int) CC->cs_UDSclientUID,
765 CtdlGetConfigStr("c_fqdn"),
771 "Received: from %s (%s [%s])\n"
773 ChrPtr(SMTP->helo_node),
776 CtdlGetConfigStr("c_fqdn"),
780 body = CtdlReadMessageBodyBuf(HKEY("."), CtdlGetConfigLong("c_maxmsglen"), defbody, 1);
781 FreeStrBuf(&defbody);
783 cprintf("550 Unable to save message: internal error.\r\n");
787 syslog(LOG_DEBUG, "serv_smtp: converting message...");
788 msg = convert_internet_message_buf(&body);
790 /* If the user is locally authenticated, FORCE the From: header to
791 * show up as the real sender. Yes, this violates the RFC standard,
792 * but IT MAKES SENSE. If you prefer strict RFC adherence over
793 * common sense, you can disable this in the configuration.
795 * We also set the "message room name" ('O' field) to MAILROOM
796 * (which is Mail> on most systems) to prevent it from getting set
797 * to something ugly like "0000058008.Sent Items>" when the message
798 * is read with a Citadel client.
800 if ( (CC->logged_in) && (CtdlGetConfigInt("c_rfc822_strict_from") != CFG_SMTP_FROM_NOFILTER) ) {
803 if (!CM_IsEmpty(msg, erFc822Addr) &&
804 ((CtdlGetConfigInt("c_rfc822_strict_from") == CFG_SMTP_FROM_CORRECT) ||
805 (CtdlGetConfigInt("c_rfc822_strict_from") == CFG_SMTP_FROM_REJECT) ) )
807 if (!IsEmptyStr(CC->cs_inet_email))
808 validemail = strcmp(CC->cs_inet_email, msg->cm_fields[erFc822Addr]) == 0;
810 (!IsEmptyStr(CC->cs_inet_other_emails)))
812 int num_secondary_emails = 0;
814 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
815 for (i=0; i < num_secondary_emails && !validemail; ++i) {
817 extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
818 validemail = strcmp(buf, msg->cm_fields[erFc822Addr]) == 0;
823 if (!validemail && (CtdlGetConfigInt("c_rfc822_strict_from") == CFG_SMTP_FROM_REJECT)) {
824 syslog(LOG_ERR, "serv_smtp: invalid sender '%s' - rejecting this message", msg->cm_fields[erFc822Addr]);
825 cprintf("550 Invalid sender '%s' - rejecting this message.\r\n", msg->cm_fields[erFc822Addr]);
829 CM_SetField(msg, eOriginalRoom, HKEY(MAILROOM));
830 if (SMTP->preferred_sender_name != NULL)
831 CM_SetField(msg, eAuthor, SKEY(SMTP->preferred_sender_name));
833 CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
836 if (SMTP->preferred_sender_email != NULL) {
837 CM_SetField(msg, erFc822Addr, SKEY(SMTP->preferred_sender_email));
840 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, strlen(CC->cs_inet_email));
845 /* Set the "envelope from" address */
846 CM_SetField(msg, eMessagePath, SKEY(SMTP->from));
848 /* Set the "envelope to" address */
849 CM_SetField(msg, eenVelopeTo, SKEY(SMTP->recipients));
851 /* Submit the message into the Citadel system. */
852 valid = validate_recipients(
853 (char *)ChrPtr(SMTP->recipients),
854 smtp_get_Recipients(),
855 (SMTP->is_lmtp)? POST_LMTP: (CC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
858 /* If there are modules that want to scan this message before final
859 * submission (such as virus checkers or spam filters), call them now
860 * and give them an opportunity to reject the message.
862 if (SMTP->is_unfiltered) {
866 scan_errors = PerformMessageHooks(msg, valid, EVT_SMTPSCAN);
869 if (scan_errors > 0) { /* We don't want this message! */
871 if (CM_IsEmpty(msg, eErrorMsg)) {
872 CM_SetField(msg, eErrorMsg, HKEY("Message rejected by filter"));
875 StrBufPrintf(SMTP->OneRcpt, "550 %s\r\n", msg->cm_fields[eErrorMsg]);
878 else { /* Ok, we'll accept this message. */
879 msgnum = CtdlSubmitMsg(msg, valid, "");
881 StrBufPrintf(SMTP->OneRcpt, "250 Message accepted.\r\n");
884 StrBufPrintf(SMTP->OneRcpt, "550 Internal delivery error\r\n");
888 /* For SMTP and ESMTP, just print the result message. For LMTP, we
889 * have to print one result message for each recipient. Since there
890 * is nothing in Citadel which would cause different recipients to
891 * have different results, we can get away with just spitting out the
892 * same message once for each recipient.
895 for (i=0; i<SMTP->number_of_recipients; ++i) {
896 cputbuf(SMTP->OneRcpt);
900 cputbuf(SMTP->OneRcpt);
903 /* Write something to the syslog(which may or may not be where the
904 * rest of the Citadel logs are going; some sysadmins want LOG_MAIL).
906 syslog((LOG_MAIL | LOG_INFO),
907 "%ld: from=<%s>, nrcpts=%d, relay=%s [%s], stat=%s",
910 SMTP->number_of_recipients,
913 ChrPtr(SMTP->OneRcpt)
918 free_recipients(valid);
919 smtp_data_clear(); /* clear out the buffers now */
924 * Implements the STARTTLS command
926 void smtp_starttls(void) {
927 char ok_response[SIZ];
928 char nosup_response[SIZ];
929 char error_response[SIZ];
931 sprintf(ok_response, "220 Begin TLS negotiation now\r\n");
932 sprintf(nosup_response, "554 TLS not supported here\r\n");
933 sprintf(error_response, "554 Internal error\r\n");
934 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
940 * Implements the NOOP (NO OPeration) command
942 void smtp_noop(void) {
943 cprintf("250 NOOP\r\n");
948 * Implements the QUIT command
950 void smtp_quit(void) {
951 cprintf("221 Goodbye...\r\n");
952 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
957 * Main command loop for SMTP server sessions.
959 void smtp_command_loop(void) {
960 static const ConstStr AuthPlainStr = {HKEY("AUTH PLAIN")};
963 syslog(LOG_ERR, "serv_smtp: Session SMTP data is null. WTF? We will crash now.");
968 if (CtdlClientGetLine(SMTP->Cmd) < 1) {
969 syslog(LOG_INFO, "SMTP: client disconnected: ending session.");
970 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
974 if (SMTP->command_state == smtp_user) {
975 if (!strncmp(ChrPtr(SMTP->Cmd), AuthPlainStr.Key, AuthPlainStr.len)) {
984 else if (SMTP->command_state == smtp_password) {
989 else if (SMTP->command_state == smtp_plain) {
994 syslog(LOG_DEBUG, "serv_smtp: client sent command <%s>", ChrPtr(SMTP->Cmd));
996 if (!strncasecmp(ChrPtr(SMTP->Cmd), "NOOP", 4)) {
1001 if (!strncasecmp(ChrPtr(SMTP->Cmd), "QUIT", 4)) {
1006 if (!strncasecmp(ChrPtr(SMTP->Cmd), "HELO", 4)) {
1011 if (!strncasecmp(ChrPtr(SMTP->Cmd), "EHLO", 4)) {
1016 if (!strncasecmp(ChrPtr(SMTP->Cmd), "LHLO", 4)) {
1021 if (!strncasecmp(ChrPtr(SMTP->Cmd), "RSET", 4)) {
1026 if (!strncasecmp(ChrPtr(SMTP->Cmd), "AUTH", 4)) {
1031 if (!strncasecmp(ChrPtr(SMTP->Cmd), "DATA", 4)) {
1036 if (!strncasecmp(ChrPtr(SMTP->Cmd), "HELP", 4)) {
1041 if (!strncasecmp(ChrPtr(SMTP->Cmd), "MAIL", 4)) {
1046 if (!strncasecmp(ChrPtr(SMTP->Cmd), "RCPT", 4)) {
1051 if (!strncasecmp(ChrPtr(SMTP->Cmd), "STARTTLS", 8)) {
1057 cprintf("502 I'm afraid I can't do that.\r\n");
1061 /*****************************************************************************/
1062 /* MODULE INITIALIZATION STUFF */
1063 /*****************************************************************************/
1065 * This cleanup function blows away the temporary memory used by
1068 void smtp_cleanup_function(void)
1070 /* Don't do this stuff if this is not an SMTP session! */
1071 if (CC->h_command_function != smtp_command_loop) return;
1073 syslog(LOG_DEBUG, "Performing SMTP cleanup hook");
1075 FreeStrBuf(&SMTP->Cmd);
1076 FreeStrBuf(&SMTP->helo_node);
1077 FreeStrBuf(&SMTP->from);
1078 FreeStrBuf(&SMTP->recipients);
1079 FreeStrBuf(&SMTP->OneRcpt);
1080 FreeStrBuf(&SMTP->preferred_sender_email);
1081 FreeStrBuf(&SMTP->preferred_sender_name);
1086 const char *CitadelServiceSMTP_MTA="SMTP-MTA";
1087 const char *CitadelServiceSMTPS_MTA="SMTPs-MTA";
1088 const char *CitadelServiceSMTP_MSA="SMTP-MSA";
1089 const char *CitadelServiceSMTP_LMTP="LMTP";
1090 const char *CitadelServiceSMTP_LMTP_UNF="LMTP-UnF";
1093 CTDL_MODULE_INIT(smtp)
1096 CtdlRegisterServiceHook(CtdlGetConfigInt("c_smtp_port"), /* SMTP MTA */
1101 CitadelServiceSMTP_MTA);
1104 CtdlRegisterServiceHook(CtdlGetConfigInt("c_smtps_port"), /* SMTPS MTA */
1109 CitadelServiceSMTPS_MTA);
1112 CtdlRegisterServiceHook(CtdlGetConfigInt("c_msa_port"), /* SMTP MSA */
1117 CitadelServiceSMTP_MSA);
1119 CtdlRegisterServiceHook(0, /* local LMTP */
1124 CitadelServiceSMTP_LMTP);
1126 CtdlRegisterServiceHook(0, /* local LMTP */
1127 file_lmtp_unfiltered_socket,
1128 lmtp_unfiltered_greeting,
1131 CitadelServiceSMTP_LMTP_UNF);
1133 CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1136 /* return our module name for the log */