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 CtdlEncodeBase64(encoded, buf,
238 strlen(Msg->pCurrRelay->User) * 2 +
239 strlen(Msg->pCurrRelay->Pass) + 2, 0);
241 StrBufPrintf(Msg->IO.SendBuf.Buf,
251 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
253 AsyncIO *IO = &Msg->IO;
254 /* Do an AUTH command if necessary */
260 if (!SMTP_IS_STATE('3'))
265 if (!SMTP_IS_STATE('2')) {
266 if (SMTP_IS_STATE('4'))
271 READ_NEXT_STATE(eFROM);
277 eNextState SMTPC_send_authplain_1(SmtpOutMsg *Msg)
279 AsyncIO *IO = &Msg->IO;
285 Msg->pCurrRelay->User);
287 encodedlen = CtdlEncodeBase64(
289 Msg->pCurrRelay->User,
290 strlen(Msg->pCurrRelay->User),
293 StrBufPlain(Msg->IO.SendBuf.Buf,
297 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
304 eNextState SMTPC_read_auth_plain_reply_1(SmtpOutMsg *Msg)
306 AsyncIO *IO = &Msg->IO;
307 /* Do an AUTH command if necessary */
311 if (!SMTP_IS_STATE('3'))
317 eNextState SMTPC_send_authplain_2(SmtpOutMsg *Msg)
319 AsyncIO *IO = &Msg->IO;
325 Msg->pCurrRelay->Pass);
327 encodedlen = CtdlEncodeBase64(
329 Msg->pCurrRelay->User,
330 strlen(Msg->pCurrRelay->User),
333 StrBufPlain(Msg->IO.SendBuf.Buf,
337 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
344 eNextState SMTPC_read_auth_plain_reply_2(SmtpOutMsg *Msg)
346 AsyncIO *IO = &Msg->IO;
347 /* Do an AUTH command if necessary */
351 if (!SMTP_IS_STATE('2')) {
352 if (SMTP_IS_STATE('4'))
360 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
362 AsyncIO *IO = &Msg->IO;
363 /* previous command succeeded, now try the MAIL FROM: command */
364 StrBufPrintf(Msg->IO.SendBuf.Buf,
365 "MAIL FROM:<%s>\r\n",
372 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
374 AsyncIO *IO = &Msg->IO;
377 if (!SMTP_IS_STATE('2')) {
378 if (SMTP_IS_STATE('4'))
387 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
389 AsyncIO *IO = &Msg->IO;
390 /* MAIL succeeded, now try the RCPT To: command */
391 StrBufPrintf(Msg->IO.SendBuf.Buf,
392 "RCPT TO:<%s@%s>\r\n",
400 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
402 AsyncIO *IO = &Msg->IO;
405 if (!SMTP_IS_STATE('2')) {
406 if (SMTP_IS_STATE('4'))
414 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
416 AsyncIO *IO = &Msg->IO;
417 /* RCPT succeeded, now try the DATA command */
418 StrBufPlain(Msg->IO.SendBuf.Buf,
425 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
427 AsyncIO *IO = &Msg->IO;
430 if (!SMTP_IS_STATE('3')) {
431 SetSMTPState(IO, eSTMPfailOne);
432 if (SMTP_IS_STATE('4'))
437 SetSMTPState(IO, eSTMPsmtpdata);
441 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
444 /* If we reach this point, the server is expecting data.*/
446 Buf = Msg->IO.SendBuf.Buf;
447 Msg->IO.SendBuf.Buf = Msg->msgtext;
450 * sending the message itself doesn't use this state machine.
451 * so we have to operate it here by ourselves.
458 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
462 Buf = Msg->IO.SendBuf.Buf;
463 Msg->IO.SendBuf.Buf = Msg->msgtext;
466 StrBufPlain(Msg->IO.SendBuf.Buf,
473 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
475 AsyncIO *IO = &Msg->IO;
478 if (!SMTP_IS_STATE('2')) {
479 if (SMTP_IS_STATE('4'))
485 SetSMTPState(IO, eSTMPsmtpdone);
487 StrBufPlain(Msg->MyQEntry->StatusMessage,
488 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
489 StrLength(Msg->IO.RecvBuf.Buf) - 4);
490 StrBufTrim(Msg->MyQEntry->StatusMessage);
491 Msg->MyQEntry->Status = 2;
495 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
497 AsyncIO *IO = &Msg->IO;
498 StrBufPlain(Msg->IO.SendBuf.Buf,
505 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
507 AsyncIO *IO = &Msg->IO;
510 EVS_syslog(LOG_DEBUG,
511 "delivery to <%s> @ <%s> (%s) succeeded\n",
516 return eTerminateConnection;
519 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
524 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
529 /*****************************************************************************/
530 /* SMTP CLIENT DISPATCHER */
531 /*****************************************************************************/
532 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
534 SMTPC_read_EHLO_reply,
535 SMTPC_read_HELO_reply,
536 SMTPC_read_auth_reply,
537 SMTPC_read_auth_plain_reply_1,
538 SMTPC_read_auth_plain_reply_2,
539 SMTPC_read_FROM_reply,
540 SMTPC_read_RCPT_reply,
541 SMTPC_read_DATAcmd_reply,
543 SMTPC_read_data_body_reply,
544 SMTPC_read_QUIT_reply
546 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
547 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
551 SMTPC_send_authplain_1,
552 SMTPC_send_authplain_2,
556 SMTPC_send_data_body,
557 SMTPC_send_terminate_data_body,
561 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
563 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
564 300., /* Greeting... */
574 90., /* end of body... */
577 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
578 90., /* Greeting... */
588 900., /* end of body... */
592 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
593 {HKEY("Connection broken during SMTP conversation")},
594 {HKEY("Connection broken during SMTP EHLO")},
595 {HKEY("Connection broken during SMTP HELO")},
596 {HKEY("Connection broken during SMTP AUTH")},
597 {HKEY("Connection broken during SMTP AUTH PLAIN I")},
598 {HKEY("Connection broken during SMTP AUTH PLAIN II")},
599 {HKEY("Connection broken during SMTP MAIL FROM")},
600 {HKEY("Connection broken during SMTP RCPT")},
601 {HKEY("Connection broken during SMTP DATA")},
602 {HKEY("Connection broken during SMTP message transmit")},
603 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
604 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
605 {HKEY("")}/* quit reply, don't care. */
612 int smtp_resolve_recipients(SmtpOutMsg *Msg)
614 AsyncIO *IO = &Msg->IO;
621 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
624 (Msg->MyQEntry == NULL) ||
625 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
629 /* Parse out the host portion of the recipient address */
630 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
635 EVNCS_syslog(LOG_DEBUG,
636 "Attempting delivery to <%s> @ <%s> (%s)\n",
641 /* If no envelope_from is supplied, extract one from the message */
642 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
643 if ( (Msg->envelope_from == NULL) ||
644 (IsEmptyStr(Msg->envelope_from)) ) {
645 Msg->mailfrom[0] = '\0';
647 ptr = ChrPtr(Msg->msgtext);
649 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
653 if (!strncasecmp(buf, "From:", 5))
655 safestrncpy(Msg->mailfrom,
657 sizeof Msg->mailfrom);
659 striplt(Msg->mailfrom);
660 for (i=0; Msg->mailfrom[i]; ++i) {
661 if (!isprint(Msg->mailfrom[i]))
663 strcpy(&Msg->mailfrom[i],
664 &Msg->mailfrom[i+1]);
669 /* Strip out parenthesized names */
673 !IsEmptyStr(Msg->mailfrom + i);
676 if (Msg->mailfrom[i] == '(') lp = i;
677 if (Msg->mailfrom[i] == ')') rp = i;
681 strcpy(&Msg->mailfrom[lp-1],
682 &Msg->mailfrom[rp+1]);
685 /* Prefer brokketized names */
689 !IsEmptyStr(Msg->mailfrom + i);
692 if (Msg->mailfrom[i] == '<') lp = i;
693 if (Msg->mailfrom[i] == '>') rp = i;
695 if ( (lp>=0) && (rp>lp) ) {
696 Msg->mailfrom[rp] = 0;
697 memmove(Msg->mailfrom,
698 &Msg->mailfrom[lp + 1],
704 } while (scan_done == 0);
705 if (IsEmptyStr(Msg->mailfrom))
706 strcpy(Msg->mailfrom, "someone@somewhere.org");
708 stripallbut(Msg->mailfrom, '<', '>');
709 Msg->envelope_from = Msg->mailfrom;