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 void DeleteSmtpOutMsg(void *v)
97 AsyncIO *IO = &Msg->IO;
98 EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
100 /* these are kept in our own space and free'd below */
101 Msg->IO.ConnectMe = NULL;
103 ares_free_data(Msg->AllMX);
104 if (Msg->HostLookup.VParsedDNSReply != NULL)
105 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
106 FreeURL(&Msg->Relay);
107 FreeStrBuf(&Msg->msgtext);
108 FreeAsyncIOContents(&Msg->IO);
109 memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
113 eNextState SMTP_C_Shutdown(AsyncIO *IO);
114 eNextState SMTP_C_Timeout(AsyncIO *IO);
115 eNextState SMTP_C_ConnFail(AsyncIO *IO);
116 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
117 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
118 eNextState SMTP_C_DNSFail(AsyncIO *IO);
119 eNextState SMTP_C_Terminate(AsyncIO *IO);
120 eNextState SMTP_C_TerminateDB(AsyncIO *IO);
121 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
123 eNextState mx_connect_ip(AsyncIO *IO);
124 eNextState get_one_mx_host_ip(AsyncIO *IO);
126 /******************************************************************************
127 * So, we're finished with sending (regardless of success or failure) *
128 * This Message might be referenced by several Queue-Items, if we're the last,*
129 * we need to free the memory and send bounce messages (on terminal failure) *
130 * else we just free our SMTP-Message struct. *
131 ******************************************************************************/
132 eNextState FinalizeMessageSend_DB(AsyncIO *IO)
135 SmtpOutMsg *Msg = IO->Data;
137 if (Msg->MyQEntry->Status == 2)
138 Status = "Delivery successful.";
139 else if (Msg->MyQEntry->Status == 5)
140 Status = "Delivery failed permanently; giving up.";
142 Status = "Delivery failed temporarily; will retry later.";
145 "%s Time[%fs] Recipient <%s> @ <%s> (%s) Status message: %s\n",
147 Msg->IO.Now - Msg->IO.StartIO,
151 ChrPtr(Msg->MyQEntry->StatusMessage));
154 Msg->IDestructQueItem = DecreaseQReference(Msg->MyQItem);
156 Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem);
158 if (Msg->MyQEntry->Active &&
159 CheckQEntryIsBounce(Msg->MyQEntry))
161 /* are we casue for a bounce mail? */
162 Msg->MyQItem->SendBounceMail |= (1<<Msg->MyQEntry->Status);
165 if ((Msg->nRemain > 0) || Msg->IDestructQueItem)
166 Msg->QMsgData = SerializeQueueItem(Msg->MyQItem);
168 Msg->QMsgData = NULL;
171 * Uncompleted delivery instructions remain, so delete the old
172 * instructions and replace with the updated ones.
174 EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
175 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
176 Msg->MyQItem->QueMsgID = -1;
178 if (Msg->IDestructQueItem)
179 smtpq_do_bounce(Msg->MyQItem, Msg->msgtext);
181 if (Msg->nRemain > 0)
183 struct CtdlMessage *msg;
184 msg = malloc(sizeof(struct CtdlMessage));
185 memset(msg, 0, sizeof(struct CtdlMessage));
186 msg->cm_magic = CTDLMESSAGE_MAGIC;
187 msg->cm_anon_type = MES_NORMAL;
188 msg->cm_format_type = FMT_RFC822;
189 msg->cm_fields['M'] = SmashStrBuf(&Msg->QMsgData);
190 msg->cm_fields['U'] = strdup("QMSG");
191 Msg->MyQItem->QueMsgID =
192 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
193 EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
194 CtdlFreeMessage(msg);
197 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
198 &Msg->MyQItem->MessageID,
201 FreeStrBuf(&Msg->QMsgData);
204 RemoveContext(Msg->IO.CitContext);
208 eNextState Terminate(AsyncIO *IO)
210 SmtpOutMsg *Msg = IO->Data;
212 if (Msg->IDestructQueItem)
213 RemoveQItem(Msg->MyQItem);
215 DeleteSmtpOutMsg(Msg);
218 eNextState FinalizeMessageSend(SmtpOutMsg *Msg)
220 /* hand over to DB Queue */
221 return QueueDBOperation(&Msg->IO, FinalizeMessageSend_DB);
224 eNextState FailOneAttempt(AsyncIO *IO)
226 SmtpOutMsg *Msg = IO->Data;
228 if (Msg->MyQEntry->Status == 2)
232 * possible ways here:
233 * - connection timeout
234 * - dns lookup failed
236 StopClientWatchers(IO, 1);
238 if (Msg->pCurrRelay != NULL)
239 Msg->pCurrRelay = Msg->pCurrRelay->Next;
240 if ((Msg->pCurrRelay != NULL) &&
241 !Msg->pCurrRelay->IsRelay &&
242 Msg->MyQItem->HaveRelay)
244 EVS_syslog(LOG_DEBUG, "%s Aborting; last relay failed.\n", __FUNCTION__);
248 if (Msg->pCurrRelay == NULL) {
249 EVS_syslog(LOG_DEBUG, "%s Aborting\n", __FUNCTION__);
252 if (Msg->pCurrRelay->IsIP) {
253 EVS_syslog(LOG_DEBUG, "%s connecting IP\n", __FUNCTION__);
254 return mx_connect_ip(IO);
257 EVS_syslog(LOG_DEBUG,
258 "%s resolving next MX Record\n",
260 return get_one_mx_host_ip(IO);
265 void SetConnectStatus(AsyncIO *IO)
267 SmtpOutMsg *Msg = IO->Data;
273 if (IO->ConnectMe->IPv6) {
274 src = &IO->ConnectMe->Addr.sin6_addr;
277 struct sockaddr_in *addr;
279 addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
280 src = &addr->sin_addr.s_addr;
283 inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
288 if (Msg->mx_host == NULL)
289 Msg->mx_host = "<no MX-Record>";
292 "connecting to %s [%s]:%d ...\n",
295 Msg->IO.ConnectMe->Port);
297 Msg->MyQEntry->Status = 4;
298 StrBufPrintf(Msg->MyQEntry->StatusMessage,
299 "Timeout while connecting %s [%s]:%d ",
302 Msg->IO.ConnectMe->Port);
303 Msg->IO.NextState = eConnect;
306 /*****************************************************************************
307 * So we connect our Relay IP here. *
308 *****************************************************************************/
309 eNextState mx_connect_ip(AsyncIO *IO)
311 SmtpOutMsg *Msg = IO->Data;
313 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
315 IO->ConnectMe = Msg->pCurrRelay;
316 Msg->State = eConnectMX;
318 SetConnectStatus(IO);
320 return EvConnectSock(IO,
322 SMTP_C_ReadTimeouts[0],
326 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
328 SmtpOutMsg *Msg = IO->Data;
329 struct hostent *hostent;
331 IO->ConnectMe = Msg->pCurrRelay;
334 EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
336 IO->Now - IO->DNS.Start);
338 hostent = Msg->HostLookup.VParsedDNSReply;
339 if ((Msg->HostLookup.DNSStatus == ARES_SUCCESS) &&
340 (hostent != NULL) ) {
341 memset(&Msg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
342 if (Msg->pCurrRelay->IPv6) {
343 memcpy(&Msg->pCurrRelay->Addr.sin6_addr.s6_addr,
344 &hostent->h_addr_list[0],
345 sizeof(struct in6_addr));
347 Msg->pCurrRelay->Addr.sin6_family =
349 Msg->pCurrRelay->Addr.sin6_port =
350 htons(Msg->IO.ConnectMe->Port);
353 struct sockaddr_in *addr;
355 * Bypass the ns lookup result like this:
356 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
357 * addr->sin_addr.s_addr =
358 * htonl((uint32_t)&hostent->h_addr_list[0]);
361 addr = (struct sockaddr_in*) &Msg->pCurrRelay->Addr;
363 memcpy(&addr->sin_addr.s_addr,
364 hostent->h_addr_list[0],
367 addr->sin_family = hostent->h_addrtype;
368 addr->sin_port = htons(Msg->IO.ConnectMe->Port);
370 Msg->mx_host = Msg->pCurrRelay->Host;
371 if (Msg->HostLookup.VParsedDNSReply != NULL) {
372 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
373 Msg->HostLookup.VParsedDNSReply = NULL;
375 return mx_connect_ip(IO);
378 if (Msg->HostLookup.VParsedDNSReply != NULL) {
379 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
380 Msg->HostLookup.VParsedDNSReply = NULL;
382 return FailOneAttempt(IO);
386 eNextState get_one_mx_host_ip(AsyncIO *IO)
388 SmtpOutMsg * Msg = IO->Data;
390 * here we start with the lookup of one host. it might be...
391 * - the relay host *sigh*
392 * - the direct hostname if there was no mx record
396 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
398 EVS_syslog(LOG_DEBUG,
399 "looking up %s-Record %s : %d ...\n",
400 (Msg->pCurrRelay->IPv6)? "aaaa": "a",
401 Msg->pCurrRelay->Host,
402 Msg->pCurrRelay->Port);
404 if (!QueueQuery((Msg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
405 Msg->pCurrRelay->Host,
408 get_one_mx_host_ip_done))
410 Msg->MyQEntry->Status = 5;
411 StrBufPrintf(Msg->MyQEntry->StatusMessage,
412 "No MX hosts found for <%s>", Msg->node);
413 Msg->IO.NextState = eTerminateConnection;
414 return IO->NextState;
416 IO->NextState = eReadDNSReply;
417 return IO->NextState;
421 /*****************************************************************************
422 * here we try to find out about the MX records for our recipients. *
423 *****************************************************************************/
424 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
426 SmtpOutMsg * Msg = IO->Data;
431 EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
433 IO->Now - IO->DNS.Start);
436 while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
439 if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
440 (IO->DNS.Query->VParsedDNSReply != NULL))
441 { /* ok, we found mx records. */
445 = IO->DNS.Query->VParsedDNSReply;
446 while (Msg->CurrMX) {
448 for (i = 0; i < 2; i++) {
451 p = (ParsedURL*) malloc(sizeof(ParsedURL));
452 memset(p, 0, sizeof(ParsedURL));
453 p->Priority = Msg->CurrMX->priority;
455 p->Port = DefaultMXPort;
457 p->Host = Msg->CurrMX->host;
461 ParsedURL *ppp = *pp;
463 while ((ppp->Next != NULL) &&
464 (ppp->Next->Priority <= p->Priority))
467 (ppp->Priority > p->Priority)) {
477 Msg->CurrMX = Msg->CurrMX->next;
479 Msg->CXFlags = Msg->CXFlags & F_HAVE_MX;
481 else { /* else fall back to the plain hostname */
483 for (i = 0; i < 2; i++) {
486 p = (ParsedURL*) malloc(sizeof(ParsedURL));
487 memset(p, 0, sizeof(ParsedURL));
489 p->Port = DefaultMXPort;
496 Msg->CXFlags = Msg->CXFlags & F_DIRECT;
498 if (Msg->MyQItem->FallBackHost != NULL)
500 Msg->MyQItem->FallBackHost->Next = *pp;
501 *pp = Msg->MyQItem->FallBackHost;
503 Msg->pCurrRelay = Msg->Relay;
504 return get_one_mx_host_ip(IO);
507 eNextState resolve_mx_records(AsyncIO *IO)
509 SmtpOutMsg * Msg = IO->Data;
511 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
512 /* start resolving MX records here. */
513 if (!QueueQuery(ns_t_mx,
517 smtp_resolve_mx_record_done))
519 Msg->MyQEntry->Status = 5;
520 StrBufPrintf(Msg->MyQEntry->StatusMessage,
521 "No MX hosts found for <%s>", Msg->node);
522 return IO->NextState;
524 Msg->IO.NextState = eReadDNSReply;
525 return IO->NextState;
530 /******************************************************************************
531 * so, we're going to start a SMTP delivery. lets get it on. *
532 ******************************************************************************/
534 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
535 MailQEntry *MyQEntry,
540 Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
543 memset(Msg, 0, sizeof(SmtpOutMsg));
546 Msg->MyQEntry = MyQEntry;
547 Msg->MyQItem = MyQItem;
548 Msg->pCurrRelay = MyQItem->URL;
550 InitIOStruct(&Msg->IO,
553 SMTP_C_ReadServerStatus,
555 SMTP_C_DispatchWriteDone,
556 SMTP_C_DispatchReadDone,
563 Msg->IO.ErrMsg = Msg->MyQEntry->StatusMessage;
568 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
569 MailQEntry *MyQEntry,
571 /*KeepMsgText allows us to use MsgText as ours.*/
577 SMTPC_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
579 Msg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
581 SMTPC_syslog(LOG_DEBUG, "%s Failed to alocate message context.\n", __FUNCTION__);
583 FreeStrBuf (&MsgText);
586 if (KeepMsgText) Msg->msgtext = MsgText;
587 else Msg->msgtext = NewStrBufDup(MsgText);
589 if (smtp_resolve_recipients(Msg)) {
592 ((CitContext *)Msg->IO.CitContext)->cs_host,
594 sizeof(((CitContext *)
595 Msg->IO.CitContext)->cs_host));
597 SMTPC_syslog(LOG_DEBUG, "Starting: [%ld] <%s> CC <%d> \n",
598 Msg->MyQItem->MessageID,
599 ChrPtr(Msg->MyQEntry->Recipient),
600 ((CitContext*)Msg->IO.CitContext)->cs_pid);
601 if (Msg->pCurrRelay == NULL)
602 QueueEventContext(&Msg->IO,
604 else { /* oh... via relay host */
605 if (Msg->pCurrRelay->IsIP) {
606 QueueEventContext(&Msg->IO,
610 /* uneducated admin has chosen to
611 add DNS to the equation... */
612 QueueEventContext(&Msg->IO,
618 /* No recipients? well fail then. */
619 if (Msg->MyQEntry != NULL) {
620 Msg->MyQEntry->Status = 5;
621 StrBufPlain(Msg->MyQEntry->StatusMessage,
622 HKEY("Invalid Recipient!"));
624 FinalizeMessageSend_DB(&Msg->IO);
625 DeleteSmtpOutMsg(&Msg->IO);
634 /*****************************************************************************/
635 /* SMTP CLIENT DISPATCHER */
636 /*****************************************************************************/
638 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *Msg)
640 double Timeout = 0.0;
641 AsyncIO *IO = &Msg->IO;
643 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
645 switch (NextTCPState) {
649 Timeout = SMTP_C_SendTimeouts[Msg->State];
650 if (Msg->State == eDATABody) {
651 /* if we're sending a huge message,
654 Timeout += StrLength(Msg->msgtext) / 512;
658 Timeout = SMTP_C_ReadTimeouts[Msg->State];
659 if (Msg->State == eDATATerminateBody) {
661 * some mailservers take a nap before accepting
662 * the message content inspection and such.
664 Timeout += StrLength(Msg->msgtext) / 512;
674 case eTerminateConnection:
678 SetNextTimeout(&Msg->IO, Timeout);
680 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
682 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
683 SmtpOutMsg *Msg = IO->Data;
686 rc = ReadHandlers[Msg->State](Msg);
690 SMTPSetTimeout(rc, Msg);
694 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
696 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
697 SmtpOutMsg *Msg = IO->Data;
700 rc = SendHandlers[Msg->State](Msg);
701 SMTPSetTimeout(rc, Msg);
706 /*****************************************************************************/
707 /* SMTP CLIENT ERROR CATCHERS */
708 /*****************************************************************************/
709 eNextState SMTP_C_Terminate(AsyncIO *IO)
711 SmtpOutMsg *Msg = IO->Data;
713 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
714 return FinalizeMessageSend(Msg);
716 eNextState SMTP_C_TerminateDB(AsyncIO *IO)
718 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
719 return Terminate(IO);
721 eNextState SMTP_C_Timeout(AsyncIO *IO)
723 SmtpOutMsg *Msg = IO->Data;
725 Msg->MyQEntry->Status = 4;
726 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
727 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
728 if (Msg->State > eRCPT)
731 return FailOneAttempt(IO);
733 eNextState SMTP_C_ConnFail(AsyncIO *IO)
735 SmtpOutMsg *Msg = IO->Data;
737 Msg->MyQEntry->Status = 4;
738 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
739 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
740 return FailOneAttempt(IO);
742 eNextState SMTP_C_DNSFail(AsyncIO *IO)
744 SmtpOutMsg *Msg = IO->Data;
745 Msg->MyQEntry->Status = 4;
746 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
747 return FailOneAttempt(IO);
749 eNextState SMTP_C_Shutdown(AsyncIO *IO)
751 EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
752 SmtpOutMsg *Msg = IO->Data;
754 switch (IO->NextState) {
758 /* todo: abort c-ares */
767 StopClientWatchers(IO, 1);
772 case eTerminateConnection:
776 Msg->MyQEntry->Status = 3;
777 StrBufPlain(Msg->MyQEntry->StatusMessage,
778 HKEY("server shutdown during message submit."));
779 return FinalizeMessageSend(Msg);
784 * @brief lineread Handler;
785 * understands when to read more SMTP lines, and when this is a one-lined reply.
787 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
789 eReadState Finished = eBufferNotEmpty;
791 while (Finished == eBufferNotEmpty) {
792 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
795 case eMustReadMore: /// read new from socket...
798 case eBufferNotEmpty: /* shouldn't happen... */
799 case eReadSuccess: /// done for now...
800 if (StrLength(IO->IOBuf) < 4)
802 if (ChrPtr(IO->IOBuf)[3] == '-')
803 Finished = eBufferNotEmpty;
807 case eReadFail: /// WHUT?
815 void LogDebugEnableSMTPClient(const int n)
817 SMTPClientDebugEnabled = n;
820 CTDL_MODULE_INIT(smtp_eventclient)
823 CtdlRegisterDebugFlagHook(HKEY("smtpeventclient"), LogDebugEnableSMTPClient, &SMTPClientDebugEnabled);
824 return "smtpeventclient";