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;
131 if (!SMTP_IS_STATE('2')) {
132 if (SMTP_IS_STATE('4'))
140 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
142 AsyncIO *IO = &Msg->IO;
143 /* At this point we know we are talking to a real SMTP server */
145 /* Do a EHLO command. If it fails, try the HELO command. */
146 StrBufPrintf(Msg->IO.SendBuf.Buf,
147 "EHLO %s\r\n", config.c_fqdn);
153 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
155 AsyncIO *IO = &Msg->IO;
158 if (SMTP_IS_STATE('2')) {
159 READ_NEXT_STATE(eSMTPAuth);
161 if ((Msg->pCurrRelay == NULL) ||
162 (Msg->pCurrRelay->User == NULL))
163 READ_NEXT_STATE(eFROM); /* Skip auth... */
164 if (Msg->pCurrRelay != NULL)
166 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
170 /* else we fall back to 'helo' */
174 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
176 AsyncIO *IO = &Msg->IO;
177 StrBufPrintf(Msg->IO.SendBuf.Buf,
178 "HELO %s\r\n", config.c_fqdn);
184 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
186 AsyncIO *IO = &Msg->IO;
189 if (!SMTP_IS_STATE('2'))
191 if (SMTP_IS_STATE('4'))
196 if ((Msg->pCurrRelay == NULL) ||
197 (Msg->pCurrRelay->User == NULL))
198 READ_NEXT_STATE(eFROM); /* Skip auth... */
203 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
205 AsyncIO *IO = &Msg->IO;
209 if ((Msg->pCurrRelay == NULL) ||
210 (Msg->pCurrRelay->User == NULL))
211 READ_NEXT_STATE(eFROM); /* Skip auth, shouldn't even come here!... */
213 /* Do an AUTH command if necessary */
216 StrBufPlain(Msg->IO.SendBuf.Buf,
217 HKEY("AUTH LOGIN\r\n"));
221 sprintf(buf, "%s%c%s%c%s",
222 Msg->pCurrRelay->User, '\0',
223 Msg->pCurrRelay->User, '\0',
224 Msg->pCurrRelay->Pass);
226 CtdlEncodeBase64(encoded, buf,
227 strlen(Msg->pCurrRelay->User) * 2 +
228 strlen(Msg->pCurrRelay->Pass) + 2, 0);
230 StrBufPrintf(Msg->IO.SendBuf.Buf,
240 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
242 AsyncIO *IO = &Msg->IO;
243 /* Do an AUTH command if necessary */
249 if (!SMTP_IS_STATE('3'))
254 if (!SMTP_IS_STATE('2')) {
255 if (SMTP_IS_STATE('4'))
260 READ_NEXT_STATE(eFROM);
266 eNextState SMTPC_send_authplain_1(SmtpOutMsg *Msg)
268 AsyncIO *IO = &Msg->IO;
274 Msg->pCurrRelay->User);
276 encodedlen = CtdlEncodeBase64(
278 Msg->pCurrRelay->User,
279 strlen(Msg->pCurrRelay->User),
282 StrBufPlain(Msg->IO.SendBuf.Buf,
286 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
293 eNextState SMTPC_read_auth_plain_reply_1(SmtpOutMsg *Msg)
295 AsyncIO *IO = &Msg->IO;
296 /* Do an AUTH command if necessary */
300 if (!SMTP_IS_STATE('3'))
306 eNextState SMTPC_send_authplain_2(SmtpOutMsg *Msg)
308 AsyncIO *IO = &Msg->IO;
314 Msg->pCurrRelay->Pass);
316 encodedlen = CtdlEncodeBase64(
318 Msg->pCurrRelay->User,
319 strlen(Msg->pCurrRelay->User),
322 StrBufPlain(Msg->IO.SendBuf.Buf,
326 StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
333 eNextState SMTPC_read_auth_plain_reply_2(SmtpOutMsg *Msg)
335 AsyncIO *IO = &Msg->IO;
336 /* Do an AUTH command if necessary */
340 if (!SMTP_IS_STATE('2')) {
341 if (SMTP_IS_STATE('4'))
349 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
351 AsyncIO *IO = &Msg->IO;
352 /* previous command succeeded, now try the MAIL FROM: command */
353 StrBufPrintf(Msg->IO.SendBuf.Buf,
354 "MAIL FROM:<%s>\r\n",
361 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
363 AsyncIO *IO = &Msg->IO;
366 if (!SMTP_IS_STATE('2')) {
367 if (SMTP_IS_STATE('4'))
376 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
378 AsyncIO *IO = &Msg->IO;
379 /* MAIL succeeded, now try the RCPT To: command */
380 StrBufPrintf(Msg->IO.SendBuf.Buf,
381 "RCPT TO:<%s@%s>\r\n",
389 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
391 AsyncIO *IO = &Msg->IO;
394 if (!SMTP_IS_STATE('2')) {
395 if (SMTP_IS_STATE('4'))
403 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
405 AsyncIO *IO = &Msg->IO;
406 /* RCPT succeeded, now try the DATA command */
407 StrBufPlain(Msg->IO.SendBuf.Buf,
414 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
416 AsyncIO *IO = &Msg->IO;
419 if (!SMTP_IS_STATE('3')) {
420 if (SMTP_IS_STATE('4'))
428 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
431 /* If we reach this point, the server is expecting data.*/
433 Buf = Msg->IO.SendBuf.Buf;
434 Msg->IO.SendBuf.Buf = Msg->msgtext;
437 * sending the message itself doesn't use this state machine.
438 * so we have to operate it here by ourselves.
445 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
449 Buf = Msg->IO.SendBuf.Buf;
450 Msg->IO.SendBuf.Buf = Msg->msgtext;
453 StrBufPlain(Msg->IO.SendBuf.Buf,
460 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
462 AsyncIO *IO = &Msg->IO;
465 if (!SMTP_IS_STATE('2')) {
466 if (SMTP_IS_STATE('4'))
473 StrBufPlain(Msg->MyQEntry->StatusMessage,
474 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
475 StrLength(Msg->IO.RecvBuf.Buf) - 4);
476 StrBufTrim(Msg->MyQEntry->StatusMessage);
477 Msg->MyQEntry->Status = 2;
481 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
483 AsyncIO *IO = &Msg->IO;
484 StrBufPlain(Msg->IO.SendBuf.Buf,
491 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
493 AsyncIO *IO = &Msg->IO;
496 EVS_syslog(LOG_DEBUG,
497 "delivery to <%s> @ <%s> (%s) succeeded\n",
502 return eTerminateConnection;
505 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
510 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
515 /*****************************************************************************/
516 /* SMTP CLIENT DISPATCHER */
517 /*****************************************************************************/
518 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
520 SMTPC_read_EHLO_reply,
521 SMTPC_read_HELO_reply,
522 SMTPC_read_auth_reply,
523 SMTPC_read_auth_plain_reply_1,
524 SMTPC_read_auth_plain_reply_2,
525 SMTPC_read_FROM_reply,
526 SMTPC_read_RCPT_reply,
527 SMTPC_read_DATAcmd_reply,
529 SMTPC_read_data_body_reply,
530 SMTPC_read_QUIT_reply
532 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
533 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
537 SMTPC_send_authplain_1,
538 SMTPC_send_authplain_2,
542 SMTPC_send_data_body,
543 SMTPC_send_terminate_data_body,
547 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
549 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
550 300., /* Greeting... */
560 90., /* end of body... */
563 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
564 90., /* Greeting... */
574 900., /* end of body... */
578 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
579 {HKEY("Connection broken during SMTP conversation")},
580 {HKEY("Connection broken during SMTP EHLO")},
581 {HKEY("Connection broken during SMTP HELO")},
582 {HKEY("Connection broken during SMTP AUTH")},
583 {HKEY("Connection broken during SMTP AUTH PLAIN I")},
584 {HKEY("Connection broken during SMTP AUTH PLAIN II")},
585 {HKEY("Connection broken during SMTP MAIL FROM")},
586 {HKEY("Connection broken during SMTP RCPT")},
587 {HKEY("Connection broken during SMTP DATA")},
588 {HKEY("Connection broken during SMTP message transmit")},
589 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
590 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
591 {HKEY("")}/* quit reply, don't care. */
598 int smtp_resolve_recipients(SmtpOutMsg *Msg)
600 AsyncIO *IO = &Msg->IO;
607 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
610 (Msg->MyQEntry == NULL) ||
611 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
615 /* Parse out the host portion of the recipient address */
616 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
621 EVNCS_syslog(LOG_DEBUG,
622 "Attempting delivery to <%s> @ <%s> (%s)\n",
627 /* If no envelope_from is supplied, extract one from the message */
628 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
629 if ( (Msg->envelope_from == NULL) ||
630 (IsEmptyStr(Msg->envelope_from)) ) {
631 Msg->mailfrom[0] = '\0';
633 ptr = ChrPtr(Msg->msgtext);
635 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
639 if (!strncasecmp(buf, "From:", 5))
641 safestrncpy(Msg->mailfrom,
643 sizeof Msg->mailfrom);
645 striplt(Msg->mailfrom);
646 for (i=0; Msg->mailfrom[i]; ++i) {
647 if (!isprint(Msg->mailfrom[i]))
649 strcpy(&Msg->mailfrom[i],
650 &Msg->mailfrom[i+1]);
655 /* Strip out parenthesized names */
659 !IsEmptyStr(Msg->mailfrom + i);
662 if (Msg->mailfrom[i] == '(') lp = i;
663 if (Msg->mailfrom[i] == ')') rp = i;
667 strcpy(&Msg->mailfrom[lp-1],
668 &Msg->mailfrom[rp+1]);
671 /* Prefer brokketized names */
675 !IsEmptyStr(Msg->mailfrom + i);
678 if (Msg->mailfrom[i] == '<') lp = i;
679 if (Msg->mailfrom[i] == '>') rp = i;
681 if ( (lp>=0) && (rp>lp) ) {
682 Msg->mailfrom[rp] = 0;
683 memmove(Msg->mailfrom,
684 &Msg->mailfrom[lp + 1],
690 } while (scan_done == 0);
691 if (IsEmptyStr(Msg->mailfrom))
692 strcpy(Msg->mailfrom, "someone@somewhere.org");
694 stripallbut(Msg->mailfrom, '<', '>');
695 Msg->envelope_from = Msg->mailfrom;