2 * This module is an SMTP and ESMTP implementation 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 * Copyright (c) 1998-2012 by the citadel.org team
22 * This program is open source software; you can redistribute it and/or modify
23 * 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.
46 #include <sys/types.h>
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
54 # include <sys/time.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <libcitadel.h>
69 #include "citserver.h"
76 #include "internet_addressing.h"
79 #include "clientsocket.h"
80 #include "locate_host.h"
81 #include "citadel_dirs.h"
83 #include "ctdl_module.h"
85 #include "smtp_util.h"
86 #include "event_client.h"
87 #include "smtpqueue.h"
88 #include "smtp_clienthandlers.h"
91 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do { \
92 Msg->MyQEntry->Status = WHICH_ERR; \
93 StrBufAppendBufPlain(Msg->MyQEntry->StatusMessage, \
95 StrBufTrim(Msg->MyQEntry->StatusMessage); \
99 #define SMTP_VERROR(WHICH_ERR) do { \
100 Msg->MyQEntry->Status = WHICH_ERR; \
101 StrBufPlain(Msg->MyQEntry->StatusMessage, \
102 ChrPtr(Msg->IO.IOBuf) + 4, \
103 StrLength(Msg->IO.IOBuf) - 4); \
104 StrBufTrim(Msg->MyQEntry->StatusMessage); \
108 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)
110 #define SMTP_DBG_SEND() \
111 EVS_syslog(LOG_DEBUG, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
113 #define SMTP_DBG_READ() \
114 EVS_syslog(LOG_DEBUG, "< %s\n", ChrPtr(Msg->IO.IOBuf))
117 * if a Read handler wants to skip to a specific part use this macro.
118 * the -1 is here since the auto-forward following has to be taken into account.
120 #define READ_NEXT_STATE(state) Msg->State = state - 1
122 /*****************************************************************************/
123 /* SMTP CLIENT STATE CALLBACKS */
124 /*****************************************************************************/
125 eNextState SMTPC_read_greeting(SmtpOutMsg *Msg)
127 /* Process the SMTP greeting from the server */
128 AsyncIO *IO = &Msg->IO;
130 SetSMTPState(IO, eSTMPsmtp);
132 if (!SMTP_IS_STATE('2')) {
133 if (SMTP_IS_STATE('4'))
141 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
143 AsyncIO *IO = &Msg->IO;
144 /* At this point we know we are talking to a real SMTP server */
146 /* Do a EHLO command. If it fails, try the HELO command. */
147 StrBufPrintf(Msg->IO.SendBuf.Buf,
148 "EHLO %s\r\n", config.c_fqdn);
154 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
156 AsyncIO *IO = &Msg->IO;
159 if (SMTP_IS_STATE('2')) {
160 READ_NEXT_STATE(eSMTPAuth);
162 if ((Msg->pCurrRelay == NULL) ||
163 (Msg->pCurrRelay->User == NULL))
164 READ_NEXT_STATE(eFROM); /* Skip auth... */
165 if (Msg->pCurrRelay != NULL)
167 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
169 else if ((Msg->MultiLineBuf != NULL) &&
170 strstr(ChrPtr(Msg->MultiLineBuf), "LOGIN") != NULL)
176 /* else we fall back to 'helo' */
180 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
182 AsyncIO *IO = &Msg->IO;
183 StrBufPrintf(Msg->IO.SendBuf.Buf,
184 "HELO %s\r\n", config.c_fqdn);
190 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
192 AsyncIO *IO = &Msg->IO;
195 if (!SMTP_IS_STATE('2'))
197 if (SMTP_IS_STATE('4'))
202 if (Msg->pCurrRelay != NULL)
204 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
207 if ((Msg->pCurrRelay == NULL) ||
208 (Msg->pCurrRelay->User == NULL))
209 READ_NEXT_STATE(eFROM); /* Skip auth... */
214 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
216 AsyncIO *IO = &Msg->IO;
220 if ((Msg->pCurrRelay == NULL) ||
221 (Msg->pCurrRelay->User == NULL))
222 READ_NEXT_STATE(eFROM); /* Skip auth, shouldn't even come here!... */
224 /* Do an AUTH command if necessary */
227 StrBufPlain(Msg->IO.SendBuf.Buf,
228 HKEY("AUTH LOGIN\r\n"));
232 sprintf(buf, "%s%c%s%c%s",
233 Msg->pCurrRelay->User, '\0',
234 Msg->pCurrRelay->User, '\0',
235 Msg->pCurrRelay->Pass);
237 size_t len = CtdlEncodeBase64(encoded, buf,
238 strlen(Msg->pCurrRelay->User) * 2 +
239 strlen(Msg->pCurrRelay->Pass) + 2, 0);
241 if (buf[len - 1] == '\n') {
245 StrBufPrintf(Msg->IO.SendBuf.Buf,
255 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
257 AsyncIO *IO = &Msg->IO;
258 /* Do an AUTH command if necessary */
264 if (!SMTP_IS_STATE('3'))
269 if (!SMTP_IS_STATE('2')) {
270 if (SMTP_IS_STATE('4'))
275 READ_NEXT_STATE(eFROM);
281 eNextState SMTPC_send_authplain_1(SmtpOutMsg *Msg)
283 AsyncIO *IO = &Msg->IO;
289 Msg->pCurrRelay->User);
291 encodedlen = CtdlEncodeBase64(
293 Msg->pCurrRelay->User,
294 strlen(Msg->pCurrRelay->User),
296 if (encoded[encodedlen - 1] == '\n') {
298 encoded[encodedlen] = '\0';
301 StrBufPlain(Msg->IO.SendBuf.Buf,
305 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
312 eNextState SMTPC_read_auth_plain_reply_1(SmtpOutMsg *Msg)
314 AsyncIO *IO = &Msg->IO;
315 /* Do an AUTH command if necessary */
319 if (!SMTP_IS_STATE('3'))
325 eNextState SMTPC_send_authplain_2(SmtpOutMsg *Msg)
327 AsyncIO *IO = &Msg->IO;
333 Msg->pCurrRelay->Pass);
335 encodedlen = CtdlEncodeBase64(
337 Msg->pCurrRelay->Pass,
338 strlen(Msg->pCurrRelay->Pass),
341 if (encoded[encodedlen - 1] == '\n') {
343 encoded[encodedlen] = '\0';
346 StrBufPlain(Msg->IO.SendBuf.Buf,
350 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
357 eNextState SMTPC_read_auth_plain_reply_2(SmtpOutMsg *Msg)
359 AsyncIO *IO = &Msg->IO;
360 /* Do an AUTH command if necessary */
364 if (!SMTP_IS_STATE('2')) {
365 if (SMTP_IS_STATE('4'))
373 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
375 AsyncIO *IO = &Msg->IO;
376 /* previous command succeeded, now try the MAIL FROM: command */
377 StrBufPrintf(Msg->IO.SendBuf.Buf,
378 "MAIL FROM:<%s>\r\n",
385 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
387 AsyncIO *IO = &Msg->IO;
390 if (!SMTP_IS_STATE('2')) {
391 if (SMTP_IS_STATE('4'))
400 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
402 AsyncIO *IO = &Msg->IO;
403 /* MAIL succeeded, now try the RCPT To: command */
404 StrBufPrintf(Msg->IO.SendBuf.Buf,
405 "RCPT TO:<%s@%s>\r\n",
413 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
415 AsyncIO *IO = &Msg->IO;
418 if (!SMTP_IS_STATE('2')) {
419 if (SMTP_IS_STATE('4'))
427 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
429 AsyncIO *IO = &Msg->IO;
430 /* RCPT succeeded, now try the DATA command */
431 StrBufPlain(Msg->IO.SendBuf.Buf,
438 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
440 AsyncIO *IO = &Msg->IO;
443 if (!SMTP_IS_STATE('3')) {
444 SetSMTPState(IO, eSTMPfailOne);
445 if (SMTP_IS_STATE('4'))
450 SetSMTPState(IO, eSTMPsmtpdata);
454 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
457 /* If we reach this point, the server is expecting data.*/
459 Buf = Msg->IO.SendBuf.Buf;
460 Msg->IO.SendBuf.Buf = Msg->msgtext;
463 * sending the message itself doesn't use this state machine.
464 * so we have to operate it here by ourselves.
471 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
475 Buf = Msg->IO.SendBuf.Buf;
476 Msg->IO.SendBuf.Buf = Msg->msgtext;
479 StrBufPlain(Msg->IO.SendBuf.Buf,
486 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
488 AsyncIO *IO = &Msg->IO;
491 if (!SMTP_IS_STATE('2')) {
492 if (SMTP_IS_STATE('4'))
498 SetSMTPState(IO, eSTMPsmtpdone);
500 StrBufPlain(Msg->MyQEntry->StatusMessage,
501 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
502 StrLength(Msg->IO.RecvBuf.Buf) - 4);
503 StrBufTrim(Msg->MyQEntry->StatusMessage);
504 Msg->MyQEntry->Status = 2;
508 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
510 AsyncIO *IO = &Msg->IO;
511 StrBufPlain(Msg->IO.SendBuf.Buf,
518 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
520 AsyncIO *IO = &Msg->IO;
523 EVS_syslog(LOG_DEBUG,
524 "delivery to <%s> @ <%s> (%s) succeeded\n",
529 return eTerminateConnection;
532 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
537 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
542 /*****************************************************************************/
543 /* SMTP CLIENT DISPATCHER */
544 /*****************************************************************************/
545 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
547 SMTPC_read_EHLO_reply,
548 SMTPC_read_HELO_reply,
549 SMTPC_read_auth_reply,
550 SMTPC_read_auth_plain_reply_1,
551 SMTPC_read_auth_plain_reply_2,
552 SMTPC_read_FROM_reply,
553 SMTPC_read_RCPT_reply,
554 SMTPC_read_DATAcmd_reply,
556 SMTPC_read_data_body_reply,
557 SMTPC_read_QUIT_reply
559 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
560 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
564 SMTPC_send_authplain_1,
565 SMTPC_send_authplain_2,
569 SMTPC_send_data_body,
570 SMTPC_send_terminate_data_body,
574 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
576 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
577 300., /* Greeting... */
587 90., /* end of body... */
590 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
591 90., /* Greeting... */
601 900., /* end of body... */
605 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
606 {HKEY("Connection broken during SMTP conversation")},
607 {HKEY("Connection broken during SMTP EHLO")},
608 {HKEY("Connection broken during SMTP HELO")},
609 {HKEY("Connection broken during SMTP AUTH")},
610 {HKEY("Connection broken during SMTP AUTH PLAIN I")},
611 {HKEY("Connection broken during SMTP AUTH PLAIN II")},
612 {HKEY("Connection broken during SMTP MAIL FROM")},
613 {HKEY("Connection broken during SMTP RCPT")},
614 {HKEY("Connection broken during SMTP DATA")},
615 {HKEY("Connection broken during SMTP message transmit")},
616 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
617 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
618 {HKEY("")}/* quit reply, don't care. */
625 int smtp_resolve_recipients(SmtpOutMsg *Msg)
627 AsyncIO *IO = &Msg->IO;
634 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
637 (Msg->MyQEntry == NULL) ||
638 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
642 /* Parse out the host portion of the recipient address */
643 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
648 EVNCS_syslog(LOG_DEBUG,
649 "Attempting delivery to <%s> @ <%s> (%s)\n",
654 /* If no envelope_from is supplied, extract one from the message */
655 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
656 if ( (Msg->envelope_from == NULL) ||
657 (IsEmptyStr(Msg->envelope_from)) ) {
658 Msg->mailfrom[0] = '\0';
660 ptr = ChrPtr(Msg->msgtext);
662 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
666 if (!strncasecmp(buf, "From:", 5))
668 safestrncpy(Msg->mailfrom,
670 sizeof Msg->mailfrom);
672 striplt(Msg->mailfrom);
673 for (i=0; Msg->mailfrom[i]; ++i) {
674 if (!isprint(Msg->mailfrom[i]))
676 strcpy(&Msg->mailfrom[i],
677 &Msg->mailfrom[i+1]);
682 /* Strip out parenthesized names */
686 !IsEmptyStr(Msg->mailfrom + i);
689 if (Msg->mailfrom[i] == '(') lp = i;
690 if (Msg->mailfrom[i] == ')') rp = i;
694 strcpy(&Msg->mailfrom[lp-1],
695 &Msg->mailfrom[rp+1]);
698 /* Prefer brokketized names */
702 !IsEmptyStr(Msg->mailfrom + i);
705 if (Msg->mailfrom[i] == '<') lp = i;
706 if (Msg->mailfrom[i] == '>') rp = i;
708 if ( (lp>=0) && (rp>lp) ) {
709 Msg->mailfrom[rp] = 0;
710 memmove(Msg->mailfrom,
711 &Msg->mailfrom[lp + 1],
717 } while (scan_done == 0);
718 if (IsEmptyStr(Msg->mailfrom))
719 strcpy(Msg->mailfrom, "someone@somewhere.org");
721 stripallbut(Msg->mailfrom, '<', '>');
722 Msg->envelope_from = Msg->mailfrom;