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 /*****************************************************************************/
118 /* SMTP CLIENT STATE CALLBACKS */
119 /*****************************************************************************/
120 eNextState SMTPC_read_greeting(SmtpOutMsg *Msg)
122 /* Process the SMTP greeting from the server */
123 AsyncIO *IO = &Msg->IO;
125 SetSMTPState(IO, eSTMPsmtp);
127 if (!SMTP_IS_STATE('2')) {
128 if (SMTP_IS_STATE('4'))
136 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
138 AsyncIO *IO = &Msg->IO;
139 /* At this point we know we are talking to a real SMTP server */
141 /* Do a EHLO command. If it fails, try the HELO command. */
142 StrBufPrintf(Msg->IO.SendBuf.Buf,
143 "EHLO %s\r\n", config.c_fqdn);
149 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
151 AsyncIO *IO = &Msg->IO;
154 if (SMTP_IS_STATE('2')) {
157 if ((Msg->pCurrRelay == NULL) ||
158 (Msg->pCurrRelay->User == NULL))
159 Msg->State ++; /* Skip auth... */
160 if (Msg->pCurrRelay != NULL)
162 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
166 /* else we fall back to 'helo' */
170 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
172 AsyncIO *IO = &Msg->IO;
173 StrBufPrintf(Msg->IO.SendBuf.Buf,
174 "HELO %s\r\n", config.c_fqdn);
180 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
182 AsyncIO *IO = &Msg->IO;
185 if (!SMTP_IS_STATE('2'))
187 if (SMTP_IS_STATE('4'))
192 if (Msg->pCurrRelay != NULL)
194 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
197 if ((Msg->pCurrRelay == NULL) ||
198 (Msg->pCurrRelay->User == NULL))
199 Msg->State ++; /* Skip auth... */
204 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
206 AsyncIO *IO = &Msg->IO;
210 if ((Msg->pCurrRelay == NULL) ||
211 (Msg->pCurrRelay->User == NULL))
212 Msg->State ++; /* Skip auth, shouldn't even come here!... */
214 /* Do an AUTH command if necessary */
218 Msg->pCurrRelay->User);
220 CtdlEncodeBase64(encoded, buf,
221 strlen(Msg->pCurrRelay->User) * 2 +
222 strlen(Msg->pCurrRelay->Pass) + 2, 0);
224 StrBufPrintf(Msg->IO.SendBuf.Buf,
228 Msg->pCurrRelay->Pass);
230 CtdlEncodeBase64(encoded, buf,
231 strlen(Msg->pCurrRelay->User) * 2 +
232 strlen(Msg->pCurrRelay->Pass) + 2, 0);
234 StrBufAppendPrintf(Msg->IO.SendBuf.Buf,
240 sprintf(buf, "%s%c%s%c%s",
241 Msg->pCurrRelay->User, '\0',
242 Msg->pCurrRelay->User, '\0',
243 Msg->pCurrRelay->Pass);
245 CtdlEncodeBase64(encoded, buf,
246 strlen(Msg->pCurrRelay->User) * 2 +
247 strlen(Msg->pCurrRelay->Pass) + 2, 0);
249 StrBufPrintf(Msg->IO.SendBuf.Buf,
258 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
260 AsyncIO *IO = &Msg->IO;
261 /* Do an AUTH command if necessary */
265 if (!SMTP_IS_STATE('2')) {
266 if (SMTP_IS_STATE('4'))
274 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
276 AsyncIO *IO = &Msg->IO;
277 /* previous command succeeded, now try the MAIL FROM: command */
278 StrBufPrintf(Msg->IO.SendBuf.Buf,
279 "MAIL FROM:<%s>\r\n",
286 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
288 AsyncIO *IO = &Msg->IO;
291 if (!SMTP_IS_STATE('2')) {
292 if (SMTP_IS_STATE('4'))
301 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
303 AsyncIO *IO = &Msg->IO;
304 /* MAIL succeeded, now try the RCPT To: command */
305 StrBufPrintf(Msg->IO.SendBuf.Buf,
306 "RCPT TO:<%s@%s>\r\n",
314 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
316 AsyncIO *IO = &Msg->IO;
319 if (!SMTP_IS_STATE('2')) {
320 if (SMTP_IS_STATE('4'))
328 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
330 AsyncIO *IO = &Msg->IO;
331 /* RCPT succeeded, now try the DATA command */
332 StrBufPlain(Msg->IO.SendBuf.Buf,
339 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
341 AsyncIO *IO = &Msg->IO;
344 if (!SMTP_IS_STATE('3')) {
345 SetSMTPState(IO, eSTMPfailOne);
346 if (SMTP_IS_STATE('4'))
351 SetSMTPState(IO, eSTMPsmtpdata);
355 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
358 /* If we reach this point, the server is expecting data.*/
360 Buf = Msg->IO.SendBuf.Buf;
361 Msg->IO.SendBuf.Buf = Msg->msgtext;
368 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
372 Buf = Msg->IO.SendBuf.Buf;
373 Msg->IO.SendBuf.Buf = Msg->msgtext;
376 StrBufPlain(Msg->IO.SendBuf.Buf,
383 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
385 AsyncIO *IO = &Msg->IO;
388 if (!SMTP_IS_STATE('2')) {
389 if (SMTP_IS_STATE('4'))
395 SetSMTPState(IO, eSTMPsmtpdone);
397 StrBufPlain(Msg->MyQEntry->StatusMessage,
398 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
399 StrLength(Msg->IO.RecvBuf.Buf) - 4);
400 StrBufTrim(Msg->MyQEntry->StatusMessage);
401 Msg->MyQEntry->Status = 2;
405 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
407 AsyncIO *IO = &Msg->IO;
408 StrBufPlain(Msg->IO.SendBuf.Buf,
415 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
417 AsyncIO *IO = &Msg->IO;
420 EVS_syslog(LOG_DEBUG,
421 "delivery to <%s> @ <%s> (%s) succeeded\n",
426 return eTerminateConnection;
429 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
434 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
439 /*****************************************************************************/
440 /* SMTP CLIENT DISPATCHER */
441 /*****************************************************************************/
442 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
444 SMTPC_read_EHLO_reply,
445 SMTPC_read_HELO_reply,
446 SMTPC_read_auth_reply,
447 SMTPC_read_FROM_reply,
448 SMTPC_read_RCPT_reply,
449 SMTPC_read_DATAcmd_reply,
451 SMTPC_read_data_body_reply,
452 SMTPC_read_QUIT_reply
454 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
455 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
462 SMTPC_send_data_body,
463 SMTPC_send_terminate_data_body,
467 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
469 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
470 300., /* Greeting... */
478 90., /* end of body... */
481 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
482 90., /* Greeting... */
490 900., /* end of body... */
494 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
495 {HKEY("Connection broken during SMTP conversation")},
496 {HKEY("Connection broken during SMTP EHLO")},
497 {HKEY("Connection broken during SMTP HELO")},
498 {HKEY("Connection broken during SMTP AUTH")},
499 {HKEY("Connection broken during SMTP MAIL FROM")},
500 {HKEY("Connection broken during SMTP RCPT")},
501 {HKEY("Connection broken during SMTP DATA")},
502 {HKEY("Connection broken during SMTP message transmit")},
503 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
504 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
505 {HKEY("")}/* quit reply, don't care. */
512 int smtp_resolve_recipients(SmtpOutMsg *Msg)
514 AsyncIO *IO = &Msg->IO;
521 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
524 (Msg->MyQEntry == NULL) ||
525 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
529 /* Parse out the host portion of the recipient address */
530 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
535 EVNCS_syslog(LOG_DEBUG,
536 "Attempting delivery to <%s> @ <%s> (%s)\n",
541 /* If no envelope_from is supplied, extract one from the message */
542 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
543 if ( (Msg->envelope_from == NULL) ||
544 (IsEmptyStr(Msg->envelope_from)) ) {
545 Msg->mailfrom[0] = '\0';
547 ptr = ChrPtr(Msg->msgtext);
549 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
553 if (!strncasecmp(buf, "From:", 5))
555 safestrncpy(Msg->mailfrom,
557 sizeof Msg->mailfrom);
559 striplt(Msg->mailfrom);
560 for (i=0; Msg->mailfrom[i]; ++i) {
561 if (!isprint(Msg->mailfrom[i]))
563 strcpy(&Msg->mailfrom[i],
564 &Msg->mailfrom[i+1]);
569 /* Strip out parenthesized names */
573 !IsEmptyStr(Msg->mailfrom + i);
576 if (Msg->mailfrom[i] == '(') lp = i;
577 if (Msg->mailfrom[i] == ')') rp = i;
581 strcpy(&Msg->mailfrom[lp-1],
582 &Msg->mailfrom[rp+1]);
585 /* Prefer brokketized names */
589 !IsEmptyStr(Msg->mailfrom + i);
592 if (Msg->mailfrom[i] == '<') lp = i;
593 if (Msg->mailfrom[i] == '>') rp = i;
595 if ( (lp>=0) && (rp>lp) ) {
596 Msg->mailfrom[rp] = 0;
597 memmove(Msg->mailfrom,
598 &Msg->mailfrom[lp + 1],
604 } while (scan_done == 0);
605 if (IsEmptyStr(Msg->mailfrom))
606 strcpy(Msg->mailfrom, "someone@somewhere.org");
608 stripallbut(Msg->mailfrom, '<', '>');
609 Msg->envelope_from = Msg->mailfrom;