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)
171 /* else we fall back to 'helo' */
175 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
177 AsyncIO *IO = &Msg->IO;
178 StrBufPrintf(Msg->IO.SendBuf.Buf,
179 "HELO %s\r\n", config.c_fqdn);
185 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
187 AsyncIO *IO = &Msg->IO;
190 if (!SMTP_IS_STATE('2'))
192 if (SMTP_IS_STATE('4'))
197 if (Msg->pCurrRelay != NULL)
199 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
202 if ((Msg->pCurrRelay == NULL) ||
203 (Msg->pCurrRelay->User == NULL))
204 READ_NEXT_STATE(eFROM); /* Skip auth... */
209 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
211 AsyncIO *IO = &Msg->IO;
215 if ((Msg->pCurrRelay == NULL) ||
216 (Msg->pCurrRelay->User == NULL))
217 READ_NEXT_STATE(eFROM); /* Skip auth, shouldn't even come here!... */
219 /* Do an AUTH command if necessary */
222 StrBufPlain(Msg->IO.SendBuf.Buf,
223 HKEY("AUTH LOGIN\r\n"));
227 sprintf(buf, "%s%c%s%c%s",
228 Msg->pCurrRelay->User, '\0',
229 Msg->pCurrRelay->User, '\0',
230 Msg->pCurrRelay->Pass);
232 CtdlEncodeBase64(encoded, buf,
233 strlen(Msg->pCurrRelay->User) * 2 +
234 strlen(Msg->pCurrRelay->Pass) + 2, 0);
236 StrBufPrintf(Msg->IO.SendBuf.Buf,
246 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
248 AsyncIO *IO = &Msg->IO;
249 /* Do an AUTH command if necessary */
255 if (!SMTP_IS_STATE('3'))
260 if (!SMTP_IS_STATE('2')) {
261 if (SMTP_IS_STATE('4'))
266 READ_NEXT_STATE(eFROM);
272 eNextState SMTPC_send_authplain_1(SmtpOutMsg *Msg)
274 AsyncIO *IO = &Msg->IO;
280 Msg->pCurrRelay->User);
282 encodedlen = CtdlEncodeBase64(
284 Msg->pCurrRelay->User,
285 strlen(Msg->pCurrRelay->User),
288 StrBufPlain(Msg->IO.SendBuf.Buf,
292 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
299 eNextState SMTPC_read_auth_plain_reply_1(SmtpOutMsg *Msg)
301 AsyncIO *IO = &Msg->IO;
302 /* Do an AUTH command if necessary */
306 if (!SMTP_IS_STATE('3'))
312 eNextState SMTPC_send_authplain_2(SmtpOutMsg *Msg)
314 AsyncIO *IO = &Msg->IO;
320 Msg->pCurrRelay->Pass);
322 encodedlen = CtdlEncodeBase64(
324 Msg->pCurrRelay->User,
325 strlen(Msg->pCurrRelay->User),
328 StrBufPlain(Msg->IO.SendBuf.Buf,
332 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
339 eNextState SMTPC_read_auth_plain_reply_2(SmtpOutMsg *Msg)
341 AsyncIO *IO = &Msg->IO;
342 /* Do an AUTH command if necessary */
346 if (!SMTP_IS_STATE('2')) {
347 if (SMTP_IS_STATE('4'))
355 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
357 AsyncIO *IO = &Msg->IO;
358 /* previous command succeeded, now try the MAIL FROM: command */
359 StrBufPrintf(Msg->IO.SendBuf.Buf,
360 "MAIL FROM:<%s>\r\n",
367 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
369 AsyncIO *IO = &Msg->IO;
372 if (!SMTP_IS_STATE('2')) {
373 if (SMTP_IS_STATE('4'))
382 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
384 AsyncIO *IO = &Msg->IO;
385 /* MAIL succeeded, now try the RCPT To: command */
386 StrBufPrintf(Msg->IO.SendBuf.Buf,
387 "RCPT TO:<%s@%s>\r\n",
395 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
397 AsyncIO *IO = &Msg->IO;
400 if (!SMTP_IS_STATE('2')) {
401 if (SMTP_IS_STATE('4'))
409 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
411 AsyncIO *IO = &Msg->IO;
412 /* RCPT succeeded, now try the DATA command */
413 StrBufPlain(Msg->IO.SendBuf.Buf,
420 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
422 AsyncIO *IO = &Msg->IO;
425 if (!SMTP_IS_STATE('3')) {
426 SetSMTPState(IO, eSTMPfailOne);
427 if (SMTP_IS_STATE('4'))
432 SetSMTPState(IO, eSTMPsmtpdata);
436 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
439 /* If we reach this point, the server is expecting data.*/
441 Buf = Msg->IO.SendBuf.Buf;
442 Msg->IO.SendBuf.Buf = Msg->msgtext;
445 * sending the message itself doesn't use this state machine.
446 * so we have to operate it here by ourselves.
453 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
457 Buf = Msg->IO.SendBuf.Buf;
458 Msg->IO.SendBuf.Buf = Msg->msgtext;
461 StrBufPlain(Msg->IO.SendBuf.Buf,
468 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
470 AsyncIO *IO = &Msg->IO;
473 if (!SMTP_IS_STATE('2')) {
474 if (SMTP_IS_STATE('4'))
480 SetSMTPState(IO, eSTMPsmtpdone);
482 StrBufPlain(Msg->MyQEntry->StatusMessage,
483 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
484 StrLength(Msg->IO.RecvBuf.Buf) - 4);
485 StrBufTrim(Msg->MyQEntry->StatusMessage);
486 Msg->MyQEntry->Status = 2;
490 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
492 AsyncIO *IO = &Msg->IO;
493 StrBufPlain(Msg->IO.SendBuf.Buf,
500 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
502 AsyncIO *IO = &Msg->IO;
505 EVS_syslog(LOG_DEBUG,
506 "delivery to <%s> @ <%s> (%s) succeeded\n",
511 return eTerminateConnection;
514 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
519 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
524 /*****************************************************************************/
525 /* SMTP CLIENT DISPATCHER */
526 /*****************************************************************************/
527 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
529 SMTPC_read_EHLO_reply,
530 SMTPC_read_HELO_reply,
531 SMTPC_read_auth_reply,
532 SMTPC_read_auth_plain_reply_1,
533 SMTPC_read_auth_plain_reply_2,
534 SMTPC_read_FROM_reply,
535 SMTPC_read_RCPT_reply,
536 SMTPC_read_DATAcmd_reply,
538 SMTPC_read_data_body_reply,
539 SMTPC_read_QUIT_reply
541 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
542 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
546 SMTPC_send_authplain_1,
547 SMTPC_send_authplain_2,
551 SMTPC_send_data_body,
552 SMTPC_send_terminate_data_body,
556 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
558 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
559 300., /* Greeting... */
569 90., /* end of body... */
572 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
573 90., /* Greeting... */
583 900., /* end of body... */
587 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
588 {HKEY("Connection broken during SMTP conversation")},
589 {HKEY("Connection broken during SMTP EHLO")},
590 {HKEY("Connection broken during SMTP HELO")},
591 {HKEY("Connection broken during SMTP AUTH")},
592 {HKEY("Connection broken during SMTP AUTH PLAIN I")},
593 {HKEY("Connection broken during SMTP AUTH PLAIN II")},
594 {HKEY("Connection broken during SMTP MAIL FROM")},
595 {HKEY("Connection broken during SMTP RCPT")},
596 {HKEY("Connection broken during SMTP DATA")},
597 {HKEY("Connection broken during SMTP message transmit")},
598 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
599 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
600 {HKEY("")}/* quit reply, don't care. */
607 int smtp_resolve_recipients(SmtpOutMsg *Msg)
609 AsyncIO *IO = &Msg->IO;
616 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
619 (Msg->MyQEntry == NULL) ||
620 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
624 /* Parse out the host portion of the recipient address */
625 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
630 EVNCS_syslog(LOG_DEBUG,
631 "Attempting delivery to <%s> @ <%s> (%s)\n",
636 /* If no envelope_from is supplied, extract one from the message */
637 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
638 if ( (Msg->envelope_from == NULL) ||
639 (IsEmptyStr(Msg->envelope_from)) ) {
640 Msg->mailfrom[0] = '\0';
642 ptr = ChrPtr(Msg->msgtext);
644 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
648 if (!strncasecmp(buf, "From:", 5))
650 safestrncpy(Msg->mailfrom,
652 sizeof Msg->mailfrom);
654 striplt(Msg->mailfrom);
655 for (i=0; Msg->mailfrom[i]; ++i) {
656 if (!isprint(Msg->mailfrom[i]))
658 strcpy(&Msg->mailfrom[i],
659 &Msg->mailfrom[i+1]);
664 /* Strip out parenthesized names */
668 !IsEmptyStr(Msg->mailfrom + i);
671 if (Msg->mailfrom[i] == '(') lp = i;
672 if (Msg->mailfrom[i] == ')') rp = i;
676 strcpy(&Msg->mailfrom[lp-1],
677 &Msg->mailfrom[rp+1]);
680 /* Prefer brokketized names */
684 !IsEmptyStr(Msg->mailfrom + i);
687 if (Msg->mailfrom[i] == '<') lp = i;
688 if (Msg->mailfrom[i] == '>') rp = i;
690 if ( (lp>=0) && (rp>lp) ) {
691 Msg->mailfrom[rp] = 0;
692 memmove(Msg->mailfrom,
693 &Msg->mailfrom[lp + 1],
699 } while (scan_done == 0);
700 if (IsEmptyStr(Msg->mailfrom))
701 strcpy(Msg->mailfrom, "someone@somewhere.org");
703 stripallbut(Msg->mailfrom, '<', '>');
704 Msg->envelope_from = Msg->mailfrom;