2 * This module is an SMTP and ESMTP server for the Citadel system.
3 * It is compliant with all of the following:
5 * RFC 821 - Simple Mail Transfer Protocol
6 * RFC 876 - Survey of SMTP Implementations
7 * RFC 1047 - Duplicate messages and SMTP
8 * RFC 1652 - 8 bit MIME
9 * RFC 1869 - Extended Simple Mail Transfer Protocol
10 * RFC 1870 - SMTP Service Extension for Message Size Declaration
11 * RFC 2033 - Local Mail Transfer Protocol
12 * RFC 2197 - SMTP Service Extension for Command Pipelining
13 * RFC 2476 - Message Submission
14 * RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
15 * RFC 2554 - SMTP Service Extension for Authentication
16 * RFC 2821 - Simple Mail Transfer Protocol
17 * RFC 2822 - Internet Message Format
18 * RFC 2920 - SMTP Service Extension for Command Pipelining
20 * The VRFY and EXPN commands have been removed from this implementation
21 * because nobody uses these commands anymore, except for spammers.
23 * Copyright (c) 1998-2012 by the citadel.org team
25 * This program is open source software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License version 3.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
43 #include <sys/types.h>
46 #if TIME_WITH_SYS_TIME
47 # include <sys/time.h>
51 # include <sys/time.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <libcitadel.h>
67 #include "citserver.h"
74 #include "internet_addressing.h"
77 #include "clientsocket.h"
78 #include "locate_host.h"
79 #include "citadel_dirs.h"
80 #include "ctdl_module.h"
82 #include "smtp_util.h"
83 enum { /* Command states for login authentication */
96 typedef void (*smtp_handler)(long offest, long Flags);
98 typedef struct _smtp_handler_hook {
103 HashList *SMTPCmds = NULL;
104 #define MaxSMTPCmdLen 10
106 #define RegisterSmtpCMD(First, H, Flags) \
107 registerSmtpCMD(HKEY(First), H, Flags)
108 void registerSmtpCMD(const char *First, long FLen,
112 smtp_handler_hook *h;
114 if (FLen >= MaxSMTPCmdLen)
115 cit_panic_backtrace (0);
117 h = (smtp_handler_hook*) malloc(sizeof(smtp_handler_hook));
118 memset(h, 0, sizeof(smtp_handler_hook));
122 Put(SMTPCmds, First, FLen, h, NULL);
125 void smtp_cleanup(void)
127 DeleteHash(&SMTPCmds);
131 * Here's where our SMTP session begins its happy day.
133 void smtp_greeting(int is_msa)
136 char message_to_spammer[1024];
138 strcpy(CC->cs_clientname, "SMTP session");
139 CC->internal_pgm = 1;
140 CC->cs_flags |= CS_STEALTH;
141 CC->session_specific_data = malloc(sizeof(citsmtp));
142 memset(SMTP, 0, sizeof(citsmtp));
144 sSMTP->is_msa = is_msa;
145 sSMTP->Cmd = NewStrBufPlain(NULL, SIZ);
146 sSMTP->helo_node = NewStrBuf();
147 sSMTP->from = NewStrBufPlain(NULL, SIZ);
148 sSMTP->recipients = NewStrBufPlain(NULL, SIZ);
149 sSMTP->OneRcpt = NewStrBufPlain(NULL, SIZ);
151 /* If this config option is set, reject connections from problem
152 * addresses immediately instead of after they execute a RCPT
154 if ( (config.c_rbl_at_greeting) && (sSMTP->is_msa == 0) ) {
155 if (rbl_check(message_to_spammer)) {
156 if (server_shutting_down)
157 cprintf("421 %s\r\n", message_to_spammer);
159 cprintf("550 %s\r\n", message_to_spammer);
160 CC->kill_me = KILLME_SPAMMER;
161 /* no need to free_recipients(valid), it's not allocated yet */
166 /* Otherwise we're either clean or we check later. */
168 if (CC->nologin==1) {
169 cprintf("451 Too many connections are already open; please try again later.\r\n");
170 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
171 /* no need to free_recipients(valid), it's not allocated yet */
175 /* Note: the FQDN *must* appear as the first thing after the 220 code.
176 * Some clients (including citmail.c) depend on it being there.
178 cprintf("220 %s ESMTP Citadel server ready.\r\n", config.c_fqdn);
183 * SMTPS is just like SMTP, except it goes crypto right away.
185 void smtps_greeting(void) {
186 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
188 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
195 * SMTP MSA port requires authentication.
197 void smtp_msa_greeting(void) {
203 * LMTP is like SMTP but with some extra bonus footage added.
205 void lmtp_greeting(void) {
213 * Generic SMTP MTA greeting
215 void smtp_mta_greeting(void) {
221 * We also have an unfiltered LMTP socket that bypasses spam filters.
223 void lmtp_unfiltered_greeting(void) {
229 sSMTP->is_unfiltered = 1;
234 * Login greeting common to all auth methods
236 void smtp_auth_greeting(long offset, long Flags) {
237 cprintf("235 Hello, %s\r\n", CC->user.fullname);
238 syslog(LOG_NOTICE, "SMTP authenticated %s\n", CC->user.fullname);
239 CC->internal_pgm = 0;
240 CC->cs_flags &= ~CS_STEALTH;
245 * Implement HELO and EHLO commands.
247 * which_command: 0=HELO, 1=EHLO, 2=LHLO
249 void smtp_hello(long offset, long which_command)
251 citsmtp *sSMTP = SMTP;
253 StrBufAppendBuf (sSMTP->helo_node, sSMTP->Cmd, offset);
255 if ( (which_command != LHLO) && (sSMTP->is_lmtp) ) {
256 cprintf("500 Only LHLO is allowed when running LMTP\r\n");
260 if ( (which_command == LHLO) && (sSMTP->is_lmtp == 0) ) {
261 cprintf("500 LHLO is only allowed when running LMTP\r\n");
265 if (which_command == HELO) {
266 cprintf("250 Hello %s (%s [%s])\r\n",
267 ChrPtr(sSMTP->helo_node),
273 if (which_command == EHLO) {
274 cprintf("250-Hello %s (%s [%s])\r\n",
275 ChrPtr(sSMTP->helo_node),
281 cprintf("250-Greetings and joyous salutations.\r\n");
283 cprintf("250-HELP\r\n");
284 cprintf("250-SIZE %ld\r\n", config.c_maxmsglen);
288 * Offer TLS, but only if TLS is not already active.
289 * Furthermore, only offer TLS when running on
290 * the SMTP-MSA port, not on the SMTP-MTA port, due to
291 * questionable reliability of TLS in certain sending MTA's.
293 if ( (!CC->redirect_ssl) && (sSMTP->is_msa) ) {
294 cprintf("250-STARTTLS\r\n");
296 #endif /* HAVE_OPENSSL */
298 cprintf("250-AUTH LOGIN PLAIN\r\n"
299 "250-AUTH=LOGIN PLAIN\r\n"
307 * Backend function for smtp_webcit_preferences_hack().
308 * Look at a message and determine if it's the preferences file.
310 void smtp_webcit_preferences_hack_backend(long msgnum, void *userdata) {
311 struct CtdlMessage *msg;
312 char **webcit_conf = (char **) userdata;
315 return; // already got it
318 msg = CtdlFetchMessage(msgnum, 1);
323 if ( !CM_IsEmpty(msg, eMsgSubject) &&
324 (!strcasecmp(msg->cm_fields[eMsgSubject], "__ WebCit Preferences __")))
326 /* This is it! Change ownership of the message text so it doesn't get freed. */
327 *webcit_conf = (char *)msg->cm_fields[eMesageText];
328 msg->cm_fields[eMesageText] = NULL;
335 * The configuration item for the user's preferred display name for outgoing email is, unfortunately,
336 * stored in the account's WebCit configuration. We have to fetch it now.
338 void smtp_webcit_preferences_hack(void) {
339 char config_roomname[ROOMNAMELEN];
340 char *webcit_conf = NULL;
342 snprintf(config_roomname, sizeof config_roomname, "%010ld.%s", CC->user.usernum, USERCONFIGROOM);
343 if (CtdlGetRoom(&CC->room, config_roomname) != 0) {
348 * Find the WebCit configuration message
351 CtdlForEachMessage(MSGS_ALL, 1, NULL, NULL, NULL, smtp_webcit_preferences_hack_backend, (void *)&webcit_conf);
357 /* FIXME : now do something with this data */
366 * Implement HELP command.
368 void smtp_help(long offset, long Flags) {
369 cprintf("214 RTFM http://www.ietf.org/rfc/rfc2821.txt\r\n");
376 void smtp_get_user(long offset)
380 citsmtp *sSMTP = SMTP;
382 CtdlDecodeBase64(username, ChrPtr(sSMTP->Cmd) + offset, SIZ);
383 /* syslog(LOG_DEBUG, "Trying <%s>\n", username); */
384 if (CtdlLoginExistingUser(NULL, username) == login_ok) {
385 CtdlEncodeBase64(buf, "Password:", 9, 0);
386 cprintf("334 %s\r\n", buf);
387 sSMTP->command_state = smtp_password;
390 cprintf("500 No such user.\r\n");
391 sSMTP->command_state = smtp_command;
399 void smtp_get_pass(long offset, long Flags)
401 citsmtp *sSMTP = SMTP;
405 memset(password, 0, sizeof(password));
406 len = CtdlDecodeBase64(password, ChrPtr(sSMTP->Cmd), SIZ);
407 /* syslog(LOG_DEBUG, "Trying <%s>\n", password); */
408 if (CtdlTryPassword(password, len) == pass_ok) {
409 smtp_auth_greeting(offset, Flags);
412 cprintf("535 Authentication failed.\r\n");
414 sSMTP->command_state = smtp_command;
419 * Back end for PLAIN auth method (either inline or multistate)
421 void smtp_try_plain(long offset, long Flags)
423 citsmtp *sSMTP = SMTP;
424 char decoded_authstring[1024];
431 CtdlDecodeBase64(decoded_authstring, ChrPtr(sSMTP->Cmd), StrLength(sSMTP->Cmd));
432 safestrncpy(ident, decoded_authstring, sizeof ident);
433 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
434 len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
436 len = sizeof(pass) - 1;
438 sSMTP->command_state = smtp_command;
440 if (!IsEmptyStr(ident)) {
441 result = CtdlLoginExistingUser(user, ident);
444 result = CtdlLoginExistingUser(NULL, user);
447 if (result == login_ok) {
448 if (CtdlTryPassword(pass, len) == pass_ok) {
449 //// smtp_webcit_preferences_hack();
450 smtp_auth_greeting(offset, Flags);
454 cprintf("504 Authentication failed.\r\n");
459 * Attempt to perform authenticated SMTP
461 void smtp_auth(long offset, long Flags)
463 citsmtp *sSMTP = SMTP;
464 char username_prompt[64];
466 char encoded_authstring[1024];
469 cprintf("504 Already logged in.\r\n");
473 extract_token(method, ChrPtr(sSMTP->Cmd) + offset, 0, ' ', sizeof method);
475 if (!strncasecmp(method, "login", 5) ) {
476 if (StrLength(sSMTP->Cmd) - offset >= 7) {
480 CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
481 cprintf("334 %s\r\n", username_prompt);
482 sSMTP->command_state = smtp_user;
487 if (!strncasecmp(method, "plain", 5) ) {
489 if (num_tokens(ChrPtr(sSMTP->Cmd) + offset, ' ') < 2) {
491 SMTP->command_state = smtp_plain;
495 len = extract_token(encoded_authstring,
496 ChrPtr(sSMTP->Cmd) + offset,
498 sizeof encoded_authstring);
499 StrBufPlain(sSMTP->Cmd, encoded_authstring, len);
500 smtp_try_plain(0, Flags);
504 if (strncasecmp(method, "login", 5) ) {
505 cprintf("504 Unknown authentication method.\r\n");
513 * Implements the RSET (reset state) command.
514 * Currently this just zeroes out the state buffer. If pointers to data
515 * allocated with malloc() are ever placed in the state buffer, we have to
516 * be sure to free() them first!
518 * Set do_response to nonzero to output the SMTP RSET response code.
520 void smtp_rset(long offset, long do_response) {
521 citsmtp *sSMTP = SMTP;
524 * Our entire SMTP state is discarded when a RSET command is issued,
525 * but we need to preserve this one little piece of information, so
526 * we save it for later.
529 FlushStrBuf(sSMTP->Cmd);
530 FlushStrBuf(sSMTP->helo_node);
531 FlushStrBuf(sSMTP->from);
532 FlushStrBuf(sSMTP->recipients);
533 FlushStrBuf(sSMTP->OneRcpt);
535 sSMTP->command_state = 0;
536 sSMTP->number_of_recipients = 0;
537 sSMTP->delivery_mode = 0;
538 sSMTP->message_originated_locally = 0;
541 * we must remember is_lmtp & is_unfiltered.
545 * It is somewhat ambiguous whether we want to log out when a RSET
546 * command is issued. Here's the code to do it. It is commented out
547 * because some clients (such as Pine) issue RSET commands before
548 * each message, but still expect to be logged in.
550 * if (CC->logged_in) {
556 cprintf("250 Zap!\r\n");
561 * Clear out the portions of the state buffer that need to be cleared out
562 * after the DATA command finishes.
564 void smtp_data_clear(long offset, long flags)
566 citsmtp *sSMTP = SMTP;
568 FlushStrBuf(sSMTP->from);
569 FlushStrBuf(sSMTP->recipients);
570 FlushStrBuf(sSMTP->OneRcpt);
571 sSMTP->number_of_recipients = 0;
572 sSMTP->delivery_mode = 0;
573 sSMTP->message_originated_locally = 0;
577 * Implements the "MAIL FROM:" command
579 void smtp_mail(long offset, long flags) {
583 citsmtp *sSMTP = SMTP;
585 if (StrLength(sSMTP->from) > 0) {
586 cprintf("503 Only one sender permitted\r\n");
590 if (strncasecmp(ChrPtr(sSMTP->Cmd) + offset, "From:", 5)) {
591 cprintf("501 Syntax error\r\n");
595 StrBufAppendBuf(sSMTP->from, sSMTP->Cmd, offset);
596 StrBufTrim(sSMTP->from);
597 if (strchr(ChrPtr(sSMTP->from), '<') != NULL) {
598 StrBufStripAllBut(sSMTP->from, '<', '>');
601 /* We used to reject empty sender names, until it was brought to our
602 * attention that RFC1123 5.2.9 requires that this be allowed. So now
603 * we allow it, but replace the empty string with a fake
604 * address so we don't have to contend with the empty string causing
605 * other code to fail when it's expecting something there.
607 if (StrLength(sSMTP->from)) {
608 StrBufPlain(sSMTP->from, HKEY("someone@example.com"));
611 /* If this SMTP connection is from a logged-in user, force the 'from'
612 * to be the user's Internet e-mail address as Citadel knows it.
615 StrBufPlain(sSMTP->from, CC->cs_inet_email, -1);
616 cprintf("250 Sender ok <%s>\r\n", ChrPtr(sSMTP->from));
617 sSMTP->message_originated_locally = 1;
621 else if (sSMTP->is_lmtp) {
622 /* Bypass forgery checking for LMTP */
625 /* Otherwise, make sure outsiders aren't trying to forge mail from
626 * this system (unless, of course, c_allow_spoofing is enabled)
628 else if (config.c_allow_spoofing == 0) {
629 process_rfc822_addr(ChrPtr(sSMTP->from), user, node, name);
630 syslog(LOG_DEBUG, "Claimed envelope sender is '%s' == '%s' @ '%s' ('%s')",
631 ChrPtr(sSMTP->from), user, node, name
633 if (CtdlHostAlias(node) != hostalias_nomatch) {
634 cprintf("550 You must log in to send mail from %s\r\n", node);
635 FlushStrBuf(sSMTP->from);
636 syslog(LOG_DEBUG, "Rejecting unauthenticated mail from %s", node);
641 cprintf("250 Sender ok\r\n");
647 * Implements the "RCPT To:" command
649 void smtp_rcpt(long offset, long flags)
651 struct CitContext *CCC = CC;
652 char message_to_spammer[SIZ];
653 struct recptypes *valid = NULL;
654 citsmtp *sSMTP = SMTP;
656 if (StrLength(sSMTP->from) == 0) {
657 cprintf("503 Need MAIL before RCPT\r\n");
661 if (strncasecmp(ChrPtr(sSMTP->Cmd) + offset, "To:", 3)) {
662 cprintf("501 Syntax error\r\n");
666 if ( (sSMTP->is_msa) && (!CCC->logged_in) ) {
667 cprintf("550 You must log in to send mail on this port.\r\n");
668 FlushStrBuf(sSMTP->from);
671 FlushStrBuf(sSMTP->OneRcpt);
672 StrBufAppendBuf(sSMTP->OneRcpt, sSMTP->Cmd, offset + 3);
673 StrBufTrim(sSMTP->OneRcpt);
674 StrBufStripAllBut(sSMTP->OneRcpt, '<', '>');
676 if ( (StrLength(sSMTP->OneRcpt) + StrLength(sSMTP->recipients)) >= SIZ) {
677 cprintf("452 Too many recipients\r\n");
682 if ( (!CCC->logged_in) /* Don't RBL authenticated users */
683 && (!sSMTP->is_lmtp) ) { /* Don't RBL LMTP clients */
684 if (config.c_rbl_at_greeting == 0) { /* Don't RBL again if we already did it */
685 if (rbl_check(message_to_spammer)) {
686 if (server_shutting_down)
687 cprintf("421 %s\r\n", message_to_spammer);
689 cprintf("550 %s\r\n", message_to_spammer);
690 /* no need to free_recipients(valid), it's not allocated yet */
696 valid = validate_recipients(
697 ChrPtr(sSMTP->OneRcpt),
698 smtp_get_Recipients(),
699 (sSMTP->is_lmtp)? POST_LMTP: (CCC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
701 if (valid->num_error != 0) {
702 cprintf("550 %s\r\n", valid->errormsg);
703 free_recipients(valid);
707 if (valid->num_internet > 0) {
708 if (CCC->logged_in) {
709 if (CtdlCheckInternetMailPermission(&CCC->user)==0) {
710 cprintf("551 <%s> - you do not have permission to send Internet mail\r\n",
711 ChrPtr(sSMTP->OneRcpt));
712 free_recipients(valid);
718 if (valid->num_internet > 0) {
719 if ( (sSMTP->message_originated_locally == 0)
720 && (sSMTP->is_lmtp == 0) ) {
721 cprintf("551 <%s> - relaying denied\r\n", ChrPtr(sSMTP->OneRcpt));
722 free_recipients(valid);
727 cprintf("250 RCPT ok <%s>\r\n", ChrPtr(sSMTP->OneRcpt));
728 if (StrLength(sSMTP->recipients) > 0) {
729 StrBufAppendBufPlain(sSMTP->recipients, HKEY(","), 0);
731 StrBufAppendBuf(sSMTP->recipients, sSMTP->OneRcpt, 0);
732 sSMTP->number_of_recipients ++;
734 free_recipients(valid);
742 * Implements the DATA command
744 void smtp_data(long offset, long flags)
746 struct CitContext *CCC = CC;
749 struct CtdlMessage *msg = NULL;
752 struct recptypes *valid;
755 citsmtp *sSMTP = SMTP;
757 if (StrLength(sSMTP->from) == 0) {
758 cprintf("503 Need MAIL command first.\r\n");
762 if (sSMTP->number_of_recipients < 1) {
763 cprintf("503 Need RCPT command first.\r\n");
767 cprintf("354 Transmit message now - terminate with '.' by itself\r\n");
769 datestring(nowstamp, sizeof nowstamp, time(NULL), DATESTRING_RFC822);
770 defbody = NewStrBufPlain(NULL, SIZ);
772 if (defbody != NULL) {
773 if (sSMTP->is_lmtp && (CCC->cs_UDSclientUID != -1)) {
776 "Received: from %s (Citadel from userid %ld)\n"
778 ChrPtr(sSMTP->helo_node),
779 (long int) CCC->cs_UDSclientUID,
786 "Received: from %s (%s [%s])\n"
788 ChrPtr(sSMTP->helo_node),
795 body = CtdlReadMessageBodyBuf(HKEY("."), config.c_maxmsglen, defbody, 1, NULL);
796 FreeStrBuf(&defbody);
798 cprintf("550 Unable to save message: internal error.\r\n");
802 syslog(LOG_DEBUG, "Converting message...\n");
803 msg = convert_internet_message_buf(&body);
805 /* If the user is locally authenticated, FORCE the From: header to
806 * show up as the real sender. Yes, this violates the RFC standard,
807 * but IT MAKES SENSE. If you prefer strict RFC adherence over
808 * common sense, you can disable this in the configuration.
810 * We also set the "message room name" ('O' field) to MAILROOM
811 * (which is Mail> on most systems) to prevent it from getting set
812 * to something ugly like "0000058008.Sent Items>" when the message
813 * is read with a Citadel client.
815 if ( (CCC->logged_in) && (config.c_rfc822_strict_from != CFG_SMTP_FROM_NOFILTER) ) {
818 if (!CM_IsEmpty(msg, erFc822Addr) &&
819 ((config.c_rfc822_strict_from == CFG_SMTP_FROM_CORRECT) ||
820 (config.c_rfc822_strict_from == CFG_SMTP_FROM_REJECT) ) )
822 if (!IsEmptyStr(CCC->cs_inet_email))
823 validemail = strcmp(CCC->cs_inet_email, msg->cm_fields[erFc822Addr]) == 0;
825 (!IsEmptyStr(CCC->cs_inet_other_emails)))
827 int num_secondary_emails = 0;
829 num_secondary_emails = num_tokens(CCC->cs_inet_other_emails, '|');
830 for (i=0; i < num_secondary_emails && !validemail; ++i) {
832 extract_token(buf, CCC->cs_inet_other_emails,i,'|',sizeof CCC->cs_inet_other_emails);
833 validemail = strcmp(buf, msg->cm_fields[erFc822Addr]) == 0;
838 if (!validemail && (config.c_rfc822_strict_from == CFG_SMTP_FROM_REJECT)) {
839 syslog(LOG_ERR, "invalid sender '%s' - rejecting this message", msg->cm_fields[erFc822Addr]);
840 cprintf("550 Invalid sender '%s' - rejecting this message.\r\n", msg->cm_fields[erFc822Addr]);
844 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
845 CM_SetField(msg, eNodeName, config.c_nodename, strlen(config.c_nodename));
846 CM_SetField(msg, eHumanNode, config.c_humannode, strlen(config.c_humannode));
847 CM_SetField(msg, eOriginalRoom, HKEY(MAILROOM));
850 CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
854 /* Set the "envelope from" address */
855 CM_SetField(msg, eMessagePath, SKEY(sSMTP->from));
857 /* Set the "envelope to" address */
858 CM_SetField(msg, eenVelopeTo, SKEY(sSMTP->recipients));
860 /* Submit the message into the Citadel system. */
861 valid = validate_recipients(
862 ChrPtr(sSMTP->recipients),
863 smtp_get_Recipients(),
864 (sSMTP->is_lmtp)? POST_LMTP: (CCC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
867 /* If there are modules that want to scan this message before final
868 * submission (such as virus checkers or spam filters), call them now
869 * and give them an opportunity to reject the message.
871 if (sSMTP->is_unfiltered) {
875 scan_errors = PerformMessageHooks(msg, EVT_SMTPSCAN);
878 if (scan_errors > 0) { /* We don't want this message! */
880 if (CM_IsEmpty(msg, eErrorMsg)) {
881 CM_SetField(msg, eErrorMsg, HKEY("Message rejected by filter"));
884 StrBufPrintf(sSMTP->OneRcpt, "550 %s\r\n", msg->cm_fields[eErrorMsg]);
887 else { /* Ok, we'll accept this message. */
888 msgnum = CtdlSubmitMsg(msg, valid, "", 0);
890 StrBufPrintf(sSMTP->OneRcpt, "250 Message accepted.\r\n");
893 StrBufPrintf(sSMTP->OneRcpt, "550 Internal delivery error\r\n");
897 /* For SMTP and ESMTP, just print the result message. For LMTP, we
898 * have to print one result message for each recipient. Since there
899 * is nothing in Citadel which would cause different recipients to
900 * have different results, we can get away with just spitting out the
901 * same message once for each recipient.
903 if (sSMTP->is_lmtp) {
904 for (i=0; i<sSMTP->number_of_recipients; ++i) {
905 cputbuf(sSMTP->OneRcpt);
909 cputbuf(sSMTP->OneRcpt);
912 /* Write something to the syslog(which may or may not be where the
913 * rest of the Citadel logs are going; some sysadmins want LOG_MAIL).
915 syslog((LOG_MAIL | LOG_INFO),
916 "%ld: from=<%s>, nrcpts=%d, relay=%s [%s], stat=%s",
919 sSMTP->number_of_recipients,
922 ChrPtr(sSMTP->OneRcpt)
927 free_recipients(valid);
928 smtp_data_clear(0, 0); /* clear out the buffers now */
933 * implements the STARTTLS command
935 void smtp_starttls(long offset, long flags)
937 char ok_response[SIZ];
938 char nosup_response[SIZ];
939 char error_response[SIZ];
941 sprintf(ok_response, "220 Begin TLS negotiation now\r\n");
942 sprintf(nosup_response, "554 TLS not supported here\r\n");
943 sprintf(error_response, "554 Internal error\r\n");
944 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
950 * Main command loop for SMTP server sessions.
952 void smtp_command_loop(void)
954 struct CitContext *CCC = CC;
955 citsmtp *sSMTP = SMTP;
956 const char *pch, *pchs;
958 char CMD[MaxSMTPCmdLen + 1];
961 syslog(LOG_EMERG, "Session SMTP data is null. WTF? We will crash now.\n");
962 return cit_panic_backtrace (0);
966 if (CtdlClientGetLine(sSMTP->Cmd) < 1) {
967 syslog(LOG_CRIT, "SMTP: client disconnected: ending session.\n");
968 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
971 syslog(LOG_DEBUG, "SMTP server: %s\n", ChrPtr(sSMTP->Cmd));
973 if (sSMTP->command_state == smtp_user) {
977 else if (sSMTP->command_state == smtp_password) {
981 else if (sSMTP->command_state == smtp_plain) {
982 smtp_try_plain(0, 0);
985 pchs = pch = ChrPtr(sSMTP->Cmd);
987 while ((*pch != '\0') &&
989 (pch - pchs <= MaxSMTPCmdLen))
991 CMD[i] = toupper(*pch);
997 if ((*pch == '\0') ||
1002 if (GetHash(SMTPCmds, CMD, i, &v) &&
1005 smtp_handler_hook *h = (smtp_handler_hook*) v;
1007 if (isblank(pchs[i]))
1015 cprintf("502 I'm afraid I can't do that.\r\n");
1018 void smtp_noop(long offest, long Flags)
1020 cprintf("250 NOOP\r\n");
1023 void smtp_quit(long offest, long Flags)
1025 cprintf("221 Goodbye...\r\n");
1026 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
1029 /*****************************************************************************/
1030 /* MODULE INITIALIZATION STUFF */
1031 /*****************************************************************************/
1033 * This cleanup function blows away the temporary memory used by
1036 void smtp_cleanup_function(void)
1038 citsmtp *sSMTP = SMTP;
1040 /* Don't do this stuff if this is not an SMTP session! */
1041 if (CC->h_command_function != smtp_command_loop) return;
1043 syslog(LOG_DEBUG, "Performing SMTP cleanup hook\n");
1045 FreeStrBuf(&sSMTP->Cmd);
1046 FreeStrBuf(&sSMTP->helo_node);
1047 FreeStrBuf(&sSMTP->from);
1048 FreeStrBuf(&sSMTP->recipients);
1049 FreeStrBuf(&sSMTP->OneRcpt);
1054 const char *CitadelServiceSMTP_MTA="SMTP-MTA";
1055 const char *CitadelServiceSMTPS_MTA="SMTPs-MTA";
1056 const char *CitadelServiceSMTP_MSA="SMTP-MSA";
1057 const char *CitadelServiceSMTP_LMTP="LMTP";
1058 const char *CitadelServiceSMTP_LMTP_UNF="LMTP-UnF";
1060 CTDL_MODULE_INIT(smtp)
1064 SMTPCmds = NewHash(1, NULL);
1066 RegisterSmtpCMD("AUTH", smtp_auth, 0);
1067 RegisterSmtpCMD("DATA", smtp_data, 0);
1068 RegisterSmtpCMD("HELO", smtp_hello, HELO);
1069 RegisterSmtpCMD("EHLO", smtp_hello, EHLO);
1070 RegisterSmtpCMD("LHLO", smtp_hello, LHLO);
1071 RegisterSmtpCMD("HELP", smtp_help, 0);
1072 RegisterSmtpCMD("MAIL", smtp_mail, 0);
1073 RegisterSmtpCMD("NOOP", smtp_noop, 0);
1074 RegisterSmtpCMD("QUIT", smtp_quit, 0);
1075 RegisterSmtpCMD("RCPT", smtp_rcpt, 0);
1076 RegisterSmtpCMD("RSET", smtp_rset, 1);
1078 RegisterSmtpCMD("STARTTLS", smtp_starttls, 0);
1082 CtdlRegisterServiceHook(config.c_smtp_port, /* SMTP MTA */
1087 CitadelServiceSMTP_MTA);
1090 CtdlRegisterServiceHook(config.c_smtps_port,
1095 CitadelServiceSMTPS_MTA);
1098 CtdlRegisterServiceHook(config.c_msa_port, /* SMTP MSA */
1103 CitadelServiceSMTP_MSA);
1105 CtdlRegisterServiceHook(0, /* local LMTP */
1110 CitadelServiceSMTP_LMTP);
1112 CtdlRegisterServiceHook(0, /* local LMTP */
1113 file_lmtp_unfiltered_socket,
1114 lmtp_unfiltered_greeting,
1117 CitadelServiceSMTP_LMTP_UNF);
1119 CtdlRegisterCleanupHook(smtp_cleanup);
1120 CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1123 /* return our module name for the log */