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 * The VRFY and EXPN commands have been removed from this implementation
21 * because nobody uses these commands anymore, except for spammers.
23 * Copyright (c) 1998-2012 by the citadel.org team
25 * This program is open source software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License version 3.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
49 #include <sys/types.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
79 #include "internet_addressing.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
86 #include "ctdl_module.h"
88 #include "smtp_util.h"
89 #include "event_client.h"
90 #include "smtpqueue.h"
91 #include "smtp_clienthandlers.h"
93 int SMTPClientDebugEnabled = 0;
94 const unsigned short DefaultMXPort = 25;
95 void DeleteSmtpOutMsg(void *v)
98 AsyncIO *IO = &Msg->IO;
99 EVS_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
101 /* these are kept in our own space and free'd below */
102 Msg->IO.ConnectMe = NULL;
104 ares_free_data(Msg->AllMX);
105 if (Msg->HostLookup.VParsedDNSReply != NULL)
106 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
107 FreeURL(&Msg->Relay);
108 FreeStrBuf(&Msg->msgtext);
109 FreeAsyncIOContents(&Msg->IO);
110 memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
114 eNextState SMTP_C_Shutdown(AsyncIO *IO);
115 eNextState SMTP_C_Timeout(AsyncIO *IO);
116 eNextState SMTP_C_ConnFail(AsyncIO *IO);
117 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
118 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
119 eNextState SMTP_C_DNSFail(AsyncIO *IO);
120 eNextState SMTP_C_Terminate(AsyncIO *IO);
121 eNextState SMTP_C_TerminateDB(AsyncIO *IO);
122 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
124 eNextState mx_connect_ip(AsyncIO *IO);
125 eNextState get_one_mx_host_ip(AsyncIO *IO);
127 /******************************************************************************
128 * So, we're finished with sending (regardless of success or failure) *
129 * This Message might be referenced by several Queue-Items, if we're the last,*
130 * we need to free the memory and send bounce messages (on terminal failure) *
131 * else we just free our SMTP-Message struct. *
132 ******************************************************************************/
133 eNextState FinalizeMessageSend_DB(AsyncIO *IO)
136 SmtpOutMsg *Msg = IO->Data;
138 if (Msg->MyQEntry->Status == 2)
139 Status = "Delivery successful.";
140 else if (Msg->MyQEntry->Status == 5)
141 Status = "Delivery failed permanently; giving up.";
143 Status = "Delivery failed temporarily; will retry later.";
146 "%s Time[%fs] Recipient <%s> @ <%s> (%s) Status message: %s\n",
148 Msg->IO.Now - Msg->IO.StartIO,
152 ChrPtr(Msg->MyQEntry->StatusMessage));
155 Msg->IDestructQueItem = DecreaseQReference(Msg->MyQItem);
157 Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem);
159 if (Msg->MyQEntry->Active &&
160 CheckQEntryIsBounce(Msg->MyQEntry))
162 /* are we casue for a bounce mail? */
163 Msg->MyQItem->SendBounceMail |= (1<<Msg->MyQEntry->Status);
166 if ((Msg->nRemain > 0) || Msg->IDestructQueItem)
167 Msg->QMsgData = SerializeQueueItem(Msg->MyQItem);
169 Msg->QMsgData = NULL;
172 * Uncompleted delivery instructions remain, so delete the old
173 * instructions and replace with the updated ones.
175 EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
176 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
177 Msg->MyQItem->QueMsgID = -1;
179 if (Msg->IDestructQueItem)
180 smtpq_do_bounce(Msg->MyQItem, Msg->msgtext);
182 if (Msg->nRemain > 0)
184 struct CtdlMessage *msg;
185 msg = malloc(sizeof(struct CtdlMessage));
186 memset(msg, 0, sizeof(struct CtdlMessage));
187 msg->cm_magic = CTDLMESSAGE_MAGIC;
188 msg->cm_anon_type = MES_NORMAL;
189 msg->cm_format_type = FMT_RFC822;
190 msg->cm_fields['M'] = SmashStrBuf(&Msg->QMsgData);
191 msg->cm_fields['U'] = strdup("QMSG");
192 Msg->MyQItem->QueMsgID =
193 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
194 EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
195 CtdlFreeMessage(msg);
198 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
199 &Msg->MyQItem->MessageID,
202 FreeStrBuf(&Msg->QMsgData);
205 RemoveContext(Msg->IO.CitContext);
206 if (Msg->IDestructQueItem)
207 RemoveQItem(Msg->MyQItem);
211 eNextState Terminate(AsyncIO *IO)
213 SmtpOutMsg *Msg = IO->Data;
214 DeleteSmtpOutMsg(Msg);
217 eNextState FinalizeMessageSend(SmtpOutMsg *Msg)
219 /* hand over to DB Queue */
220 return QueueDBOperation(&Msg->IO, FinalizeMessageSend_DB);
223 eNextState FailOneAttempt(AsyncIO *IO)
225 SmtpOutMsg *Msg = IO->Data;
227 if (Msg->MyQEntry->Status == 2)
231 * possible ways here:
232 * - connection timeout
233 * - dns lookup failed
235 StopClientWatchers(IO);
237 if (Msg->pCurrRelay != NULL)
238 Msg->pCurrRelay = Msg->pCurrRelay->Next;
240 if (Msg->pCurrRelay == NULL) {
241 EVS_syslog(LOG_DEBUG, "%s Aborting\n", __FUNCTION__);
244 if (Msg->pCurrRelay->IsIP) {
245 EVS_syslog(LOG_DEBUG, "%s connecting IP\n", __FUNCTION__);
246 return mx_connect_ip(IO);
249 EVS_syslog(LOG_DEBUG,
250 "%s resolving next MX Record\n",
252 return get_one_mx_host_ip(IO);
257 void SetConnectStatus(AsyncIO *IO)
259 SmtpOutMsg *Msg = IO->Data;
265 if (IO->ConnectMe->IPv6) {
266 src = &IO->ConnectMe->Addr.sin6_addr;
269 struct sockaddr_in *addr;
271 addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
272 src = &addr->sin_addr.s_addr;
275 inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
280 if (Msg->mx_host == NULL)
281 Msg->mx_host = "<no MX-Record>";
284 "connecting to %s [%s]:%d ...\n",
287 Msg->IO.ConnectMe->Port);
289 Msg->MyQEntry->Status = 4;
290 StrBufPrintf(Msg->MyQEntry->StatusMessage,
291 "Timeout while connecting %s [%s]:%d ",
294 Msg->IO.ConnectMe->Port);
295 Msg->IO.NextState = eConnect;
298 /*****************************************************************************
299 * So we connect our Relay IP here. *
300 *****************************************************************************/
301 eNextState mx_connect_ip(AsyncIO *IO)
303 SmtpOutMsg *Msg = IO->Data;
305 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
307 IO->ConnectMe = Msg->pCurrRelay;
308 Msg->State = eConnectMX;
310 SetConnectStatus(IO);
312 return EvConnectSock(IO,
314 SMTP_C_ReadTimeouts[0],
318 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
320 SmtpOutMsg *Msg = IO->Data;
321 struct hostent *hostent;
324 EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
326 IO->Now - IO->DNS.Start);
328 hostent = Msg->HostLookup.VParsedDNSReply;
329 if ((Msg->HostLookup.DNSStatus == ARES_SUCCESS) &&
330 (hostent != NULL) ) {
331 memset(&Msg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
332 if (Msg->pCurrRelay->IPv6) {
333 memcpy(&Msg->pCurrRelay->Addr.sin6_addr.s6_addr,
334 &hostent->h_addr_list[0],
335 sizeof(struct in6_addr));
337 Msg->pCurrRelay->Addr.sin6_family =
339 Msg->pCurrRelay->Addr.sin6_port =
340 htons(DefaultMXPort);
343 struct sockaddr_in *addr;
345 * Bypass the ns lookup result like this:
346 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
347 * addr->sin_addr.s_addr =
348 * htonl((uint32_t)&hostent->h_addr_list[0]);
351 addr = (struct sockaddr_in*) &Msg->pCurrRelay->Addr;
353 memcpy(&addr->sin_addr.s_addr,
354 hostent->h_addr_list[0],
357 addr->sin_family = hostent->h_addrtype;
358 addr->sin_port = htons(DefaultMXPort);
360 Msg->mx_host = Msg->pCurrRelay->Host;
361 if (Msg->HostLookup.VParsedDNSReply != NULL) {
362 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
363 Msg->HostLookup.VParsedDNSReply = NULL;
365 return mx_connect_ip(IO);
368 if (Msg->HostLookup.VParsedDNSReply != NULL) {
369 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
370 Msg->HostLookup.VParsedDNSReply = NULL;
372 return FailOneAttempt(IO);
376 eNextState get_one_mx_host_ip(AsyncIO *IO)
378 SmtpOutMsg * Msg = IO->Data;
380 * here we start with the lookup of one host. it might be...
381 * - the relay host *sigh*
382 * - the direct hostname if there was no mx record
386 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
388 EVS_syslog(LOG_DEBUG,
389 "looking up %s-Record %s : %d ...\n",
390 (Msg->pCurrRelay->IPv6)? "aaaa": "a",
391 Msg->pCurrRelay->Host,
392 Msg->pCurrRelay->Port);
394 if (!QueueQuery((Msg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
395 Msg->pCurrRelay->Host,
398 get_one_mx_host_ip_done))
400 Msg->MyQEntry->Status = 5;
401 StrBufPrintf(Msg->MyQEntry->StatusMessage,
402 "No MX hosts found for <%s>", Msg->node);
403 Msg->IO.NextState = eTerminateConnection;
404 return IO->NextState;
406 IO->NextState = eReadDNSReply;
407 return IO->NextState;
411 /*****************************************************************************
412 * here we try to find out about the MX records for our recipients. *
413 *****************************************************************************/
414 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
416 SmtpOutMsg * Msg = IO->Data;
421 EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
423 IO->Now - IO->DNS.Start);
426 while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
429 if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
430 (IO->DNS.Query->VParsedDNSReply != NULL))
431 { /* ok, we found mx records. */
435 = IO->DNS.Query->VParsedDNSReply;
436 while (Msg->CurrMX) {
438 for (i = 0; i < 2; i++) {
441 p = (ParsedURL*) malloc(sizeof(ParsedURL));
442 memset(p, 0, sizeof(ParsedURL));
443 p->Priority = Msg->CurrMX->priority;
445 p->Port = DefaultMXPort;
447 p->Host = Msg->CurrMX->host;
451 ParsedURL *ppp = *pp;
453 while ((ppp->Next != NULL) &&
454 (ppp->Next->Priority <= p->Priority))
457 (ppp->Priority > p->Priority)) {
467 Msg->CurrMX = Msg->CurrMX->next;
469 Msg->CXFlags = Msg->CXFlags & F_HAVE_MX;
471 else { /* else fall back to the plain hostname */
473 for (i = 0; i < 2; i++) {
476 p = (ParsedURL*) malloc(sizeof(ParsedURL));
477 memset(p, 0, sizeof(ParsedURL));
479 p->Port = DefaultMXPort;
486 Msg->CXFlags = Msg->CXFlags & F_DIRECT;
488 if (Msg->MyQItem->FallBackHost != NULL)
490 Msg->MyQItem->FallBackHost->Next = *pp;
491 *pp = Msg->MyQItem->FallBackHost;
493 Msg->pCurrRelay = Msg->Relay;
494 return get_one_mx_host_ip(IO);
497 eNextState resolve_mx_records(AsyncIO *IO)
499 SmtpOutMsg * Msg = IO->Data;
501 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
502 /* start resolving MX records here. */
503 if (!QueueQuery(ns_t_mx,
507 smtp_resolve_mx_record_done))
509 Msg->MyQEntry->Status = 5;
510 StrBufPrintf(Msg->MyQEntry->StatusMessage,
511 "No MX hosts found for <%s>", Msg->node);
512 return IO->NextState;
514 Msg->IO.NextState = eReadDNSReply;
515 return IO->NextState;
520 /******************************************************************************
521 * so, we're going to start a SMTP delivery. lets get it on. *
522 ******************************************************************************/
524 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
525 MailQEntry *MyQEntry,
530 Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
531 memset(Msg, 0, sizeof(SmtpOutMsg));
534 Msg->MyQEntry = MyQEntry;
535 Msg->MyQItem = MyQItem;
536 Msg->pCurrRelay = MyQItem->URL;
538 InitIOStruct(&Msg->IO,
541 SMTP_C_ReadServerStatus,
543 SMTP_C_DispatchWriteDone,
544 SMTP_C_DispatchReadDone,
551 Msg->IO.ErrMsg = Msg->MyQEntry->StatusMessage;
556 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
557 MailQEntry *MyQEntry,
559 /*KeepMsgText allows us to use MsgText as ours.*/
565 SMTPC_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
567 Msg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
568 if (KeepMsgText) Msg->msgtext = MsgText;
569 else Msg->msgtext = NewStrBufDup(MsgText);
571 if (smtp_resolve_recipients(Msg)) {
574 ((CitContext *)Msg->IO.CitContext)->cs_host,
576 sizeof(((CitContext *)
577 Msg->IO.CitContext)->cs_host));
579 SMTPC_syslog(LOG_DEBUG, "Starting: [%ld] <%s> CC <%d> \n",
580 Msg->MyQItem->MessageID,
581 ChrPtr(Msg->MyQEntry->Recipient),
582 ((CitContext*)Msg->IO.CitContext)->cs_pid);
583 if (Msg->pCurrRelay == NULL)
584 QueueEventContext(&Msg->IO,
586 else { /* oh... via relay host */
587 if (Msg->pCurrRelay->IsIP) {
588 QueueEventContext(&Msg->IO,
592 /* uneducated admin has chosen to
593 add DNS to the equation... */
594 QueueEventContext(&Msg->IO,
600 /* No recipients? well fail then. */
602 (Msg->MyQEntry == NULL)) {
603 Msg->MyQEntry->Status = 5;
604 StrBufPlain(Msg->MyQEntry->StatusMessage,
605 HKEY("Invalid Recipient!"));
607 FinalizeMessageSend_DB(&Msg->IO);
608 DeleteSmtpOutMsg(&Msg->IO);
617 /*****************************************************************************/
618 /* SMTP CLIENT DISPATCHER */
619 /*****************************************************************************/
621 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *Msg)
623 double Timeout = 0.0;
624 AsyncIO *IO = &Msg->IO;
626 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
628 switch (NextTCPState) {
632 Timeout = SMTP_C_SendTimeouts[Msg->State];
633 if (Msg->State == eDATABody) {
634 /* if we're sending a huge message,
637 Timeout += StrLength(Msg->msgtext) / 512;
641 Timeout = SMTP_C_ReadTimeouts[Msg->State];
642 if (Msg->State == eDATATerminateBody) {
644 * some mailservers take a nap before accepting
645 * the message content inspection and such.
647 Timeout += StrLength(Msg->msgtext) / 512;
657 case eTerminateConnection:
661 SetNextTimeout(&Msg->IO, Timeout);
663 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
665 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
666 SmtpOutMsg *Msg = IO->Data;
669 rc = ReadHandlers[Msg->State](Msg);
673 SMTPSetTimeout(rc, Msg);
677 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
679 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
680 SmtpOutMsg *Msg = IO->Data;
683 rc = SendHandlers[Msg->State](Msg);
684 SMTPSetTimeout(rc, Msg);
689 /*****************************************************************************/
690 /* SMTP CLIENT ERROR CATCHERS */
691 /*****************************************************************************/
692 eNextState SMTP_C_Terminate(AsyncIO *IO)
694 SmtpOutMsg *Msg = IO->Data;
696 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
697 return FinalizeMessageSend(Msg);
699 eNextState SMTP_C_TerminateDB(AsyncIO *IO)
701 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
702 return Terminate(IO);
704 eNextState SMTP_C_Timeout(AsyncIO *IO)
706 SmtpOutMsg *Msg = IO->Data;
708 Msg->MyQEntry->Status = 4;
709 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
710 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
711 if (Msg->State > eRCPT)
714 return FailOneAttempt(IO);
716 eNextState SMTP_C_ConnFail(AsyncIO *IO)
718 SmtpOutMsg *Msg = IO->Data;
720 Msg->MyQEntry->Status = 4;
721 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
722 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
723 return FailOneAttempt(IO);
725 eNextState SMTP_C_DNSFail(AsyncIO *IO)
727 SmtpOutMsg *Msg = IO->Data;
728 Msg->MyQEntry->Status = 4;
729 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
730 return FailOneAttempt(IO);
732 eNextState SMTP_C_Shutdown(AsyncIO *IO)
734 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
735 SmtpOutMsg *Msg = IO->Data;
737 Msg->MyQEntry->Status = 3;
738 StrBufPlain(Msg->MyQEntry->StatusMessage,
739 HKEY("server shutdown during message submit."));
740 return FinalizeMessageSend(Msg);
745 * @brief lineread Handler;
746 * understands when to read more SMTP lines, and when this is a one-lined reply.
748 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
750 eReadState Finished = eBufferNotEmpty;
752 while (Finished == eBufferNotEmpty) {
753 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
756 case eMustReadMore: /// read new from socket...
759 case eBufferNotEmpty: /* shouldn't happen... */
760 case eReadSuccess: /// done for now...
761 if (StrLength(IO->IOBuf) < 4)
763 if (ChrPtr(IO->IOBuf)[3] == '-')
764 Finished = eBufferNotEmpty;
768 case eReadFail: /// WHUT?
776 void LogDebugEnableSMTPClient(const int n)
778 SMTPClientDebugEnabled = n;
781 CTDL_MODULE_INIT(smtp_eventclient)
784 CtdlRegisterDebugFlagHook(HKEY("smtpeventclient"), LogDebugEnableSMTPClient, &SMTPClientDebugEnabled);
785 return "smtpeventclient";