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 const unsigned short DefaultMXPort = 25;
94 void DeleteSmtpOutMsg(void *v)
97 AsyncIO *IO = &Msg->IO;
98 EV_syslog(LOG_DEBUG, "SMTP: %s Aborting\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 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
122 eNextState mx_connect_ip(AsyncIO *IO);
123 eNextState get_one_mx_host_ip(AsyncIO *IO);
125 /******************************************************************************
126 * So, we're finished with sending (regardless of success or failure) *
127 * This Message might be referenced by several Queue-Items, if we're the last,*
128 * we need to free the memory and send bounce messages (on terminal failure) *
129 * else we just free our SMTP-Message struct. *
130 ******************************************************************************/
131 void FinalizeMessageSend(SmtpOutMsg *Msg)
133 int IDestructQueItem;
136 AsyncIO *IO = &Msg->IO;
138 IDestructQueItem = DecreaseQReference(Msg->MyQItem);
140 nRemain = CountActiveQueueEntries(Msg->MyQItem);
142 if ((nRemain > 0) || IDestructQueItem)
143 MsgData = SerializeQueueItem(Msg->MyQItem);
148 * Uncompleted delivery instructions remain, so delete the old
149 * instructions and replace with the updated ones.
151 EVS_syslog(LOG_DEBUG, "SMTPQD: %ld", Msg->MyQItem->QueMsgID);
152 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
154 if (IDestructQueItem)
155 smtpq_do_bounce(Msg->MyQItem, Msg->msgtext);
159 struct CtdlMessage *msg;
160 msg = malloc(sizeof(struct CtdlMessage));
161 memset(msg, 0, sizeof(struct CtdlMessage));
162 msg->cm_magic = CTDLMESSAGE_MAGIC;
163 msg->cm_anon_type = MES_NORMAL;
164 msg->cm_format_type = FMT_RFC822;
165 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
166 msg->cm_fields['U'] = strdup("QMSG");
167 Msg->MyQItem->QueMsgID =
168 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
169 EVS_syslog(LOG_DEBUG, "SMTPQ: %ld", Msg->MyQItem->QueMsgID);
170 CtdlFreeMessage(msg);
173 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
174 &Msg->MyQItem->MessageID,
177 FreeStrBuf(&MsgData);
179 RemoveContext(Msg->IO.CitContext);
180 if (IDestructQueItem)
181 RemoveQItem(Msg->MyQItem);
182 DeleteSmtpOutMsg(Msg);
185 eNextState FailOneAttempt(AsyncIO *IO)
187 SmtpOutMsg *Msg = IO->Data;
189 if (Msg->MyQEntry->Status == 2)
193 * possible ways here:
194 * - connection timeout
195 * - dns lookup failed
197 StopClientWatchers(IO);
199 if (Msg->pCurrRelay != NULL)
200 Msg->pCurrRelay = Msg->pCurrRelay->Next;
202 if (Msg->pCurrRelay == NULL) {
203 EVS_syslog(LOG_DEBUG, "SMTP: %s Aborting\n", __FUNCTION__);
206 if (Msg->pCurrRelay->IsIP) {
207 EVS_syslog(LOG_DEBUG, "SMTP: %s connecting IP\n", __FUNCTION__);
208 return mx_connect_ip(IO);
211 EVS_syslog(LOG_DEBUG,
212 "SMTP: %s resolving next MX Record\n",
214 return get_one_mx_host_ip(IO);
219 void SetConnectStatus(AsyncIO *IO)
221 SmtpOutMsg *Msg = IO->Data;
227 if (IO->ConnectMe->IPv6) {
228 src = &IO->ConnectMe->Addr.sin6_addr;
231 struct sockaddr_in *addr;
233 addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
234 src = &addr->sin_addr.s_addr;
237 inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
242 if (Msg->mx_host == NULL)
243 Msg->mx_host = "<no MX-Record>";
245 EVS_syslog(LOG_DEBUG,
246 "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
250 Msg->IO.ConnectMe->Port);
252 Msg->MyQEntry->Status = 5;
253 StrBufPrintf(Msg->MyQEntry->StatusMessage,
254 "Timeout while connecting %s [%s]:%d ",
257 Msg->IO.ConnectMe->Port);
258 Msg->IO.NextState = eConnect;
261 /*****************************************************************************
262 * So we connect our Relay IP here. *
263 *****************************************************************************/
264 eNextState mx_connect_ip(AsyncIO *IO)
266 SmtpOutMsg *Msg = IO->Data;
268 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
270 IO->ConnectMe = Msg->pCurrRelay;
271 Msg->State = eConnectMX;
273 SetConnectStatus(IO);
275 return EvConnectSock(IO,
277 SMTP_C_ReadTimeouts[0],
281 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
283 SmtpOutMsg *Msg = IO->Data;
284 struct hostent *hostent;
288 hostent = Msg->HostLookup.VParsedDNSReply;
289 if ((Msg->HostLookup.DNSStatus == ARES_SUCCESS) &&
290 (hostent != NULL) ) {
291 memset(&Msg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
292 if (Msg->pCurrRelay->IPv6) {
293 memcpy(&Msg->pCurrRelay->Addr.sin6_addr.s6_addr,
294 &hostent->h_addr_list[0],
295 sizeof(struct in6_addr));
297 Msg->pCurrRelay->Addr.sin6_family =
299 Msg->pCurrRelay->Addr.sin6_port =
300 htons(DefaultMXPort);
303 struct sockaddr_in *addr;
305 * Bypass the ns lookup result like this:
306 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
307 * addr->sin_addr.s_addr =
308 * htonl((uint32_t)&hostent->h_addr_list[0]);
311 addr = (struct sockaddr_in*) &Msg->pCurrRelay->Addr;
313 memcpy(&addr->sin_addr.s_addr,
314 hostent->h_addr_list[0],
317 addr->sin_family = hostent->h_addrtype;
318 addr->sin_port = htons(DefaultMXPort);
320 Msg->mx_host = Msg->pCurrRelay->Host;
321 return mx_connect_ip(IO);
324 return FailOneAttempt(IO);
327 eNextState get_one_mx_host_ip(AsyncIO *IO)
329 SmtpOutMsg * Msg = IO->Data;
331 * here we start with the lookup of one host. it might be...
332 * - the relay host *sigh*
333 * - the direct hostname if there was no mx record
339 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
341 EVS_syslog(LOG_DEBUG,
342 "SMTP client[%ld]: looking up %s-Record %s : %d ...\n",
344 (Msg->pCurrRelay->IPv6)? "aaaa": "a",
345 Msg->pCurrRelay->Host,
346 Msg->pCurrRelay->Port);
348 if (!QueueQuery((Msg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
349 Msg->pCurrRelay->Host,
352 get_one_mx_host_ip_done))
354 Msg->MyQEntry->Status = 5;
355 StrBufPrintf(Msg->MyQEntry->StatusMessage,
356 "No MX hosts found for <%s>", Msg->node);
357 Msg->IO.NextState = eTerminateConnection;
358 return IO->NextState;
360 IO->NextState = eReadDNSReply;
361 return IO->NextState;
365 /*****************************************************************************
366 * here we try to find out about the MX records for our recipients. *
367 *****************************************************************************/
368 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
370 SmtpOutMsg * Msg = IO->Data;
375 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
378 while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
381 if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
382 (IO->DNS.Query->VParsedDNSReply != NULL))
383 { /* ok, we found mx records. */
387 = IO->DNS.Query->VParsedDNSReply;
388 while (Msg->CurrMX) {
390 for (i = 0; i < 2; i++) {
393 p = (ParsedURL*) malloc(sizeof(ParsedURL));
394 memset(p, 0, sizeof(ParsedURL));
395 p->Priority = Msg->CurrMX->priority;
397 p->Port = DefaultMXPort;
399 p->Host = Msg->CurrMX->host;
403 ParsedURL *ppp = *pp;
405 while ((ppp->Next != NULL) &&
406 (ppp->Next->Priority <= p->Priority))
409 (ppp->Priority > p->Priority)) {
419 Msg->CurrMX = Msg->CurrMX->next;
421 Msg->CXFlags = Msg->CXFlags & F_HAVE_MX;
423 else { /* else fall back to the plain hostname */
425 for (i = 0; i < 2; i++) {
428 p = (ParsedURL*) malloc(sizeof(ParsedURL));
429 memset(p, 0, sizeof(ParsedURL));
431 p->Port = DefaultMXPort;
438 Msg->CXFlags = Msg->CXFlags & F_DIRECT;
440 if (Msg->MyQItem->FallBackHost != NULL)
442 Msg->MyQItem->FallBackHost->Next = *pp;
443 *pp = Msg->MyQItem->FallBackHost;
445 Msg->pCurrRelay = Msg->Relay;
446 return get_one_mx_host_ip(IO);
449 eNextState resolve_mx_records(AsyncIO *IO)
451 SmtpOutMsg * Msg = IO->Data;
453 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
454 /* start resolving MX records here. */
455 if (!QueueQuery(ns_t_mx,
459 smtp_resolve_mx_record_done))
461 Msg->MyQEntry->Status = 5;
462 StrBufPrintf(Msg->MyQEntry->StatusMessage,
463 "No MX hosts found for <%s>", Msg->node);
464 return IO->NextState;
466 Msg->IO.NextState = eReadDNSReply;
467 return IO->NextState;
472 /******************************************************************************
473 * so, we're going to start a SMTP delivery. lets get it on. *
474 ******************************************************************************/
476 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
477 MailQEntry *MyQEntry,
482 Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
483 memset(Msg, 0, sizeof(SmtpOutMsg));
486 Msg->MyQEntry = MyQEntry;
487 Msg->MyQItem = MyQItem;
488 Msg->pCurrRelay = MyQItem->URL;
490 InitIOStruct(&Msg->IO,
493 SMTP_C_ReadServerStatus,
495 SMTP_C_DispatchWriteDone,
496 SMTP_C_DispatchReadDone,
502 Msg->IO.ErrMsg = Msg->MyQEntry->StatusMessage;
507 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
508 MailQEntry *MyQEntry,
510 /*KeepMsgText allows us to use MsgText as ours.*/
516 syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
518 Msg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
519 if (KeepMsgText) Msg->msgtext = MsgText;
520 else Msg->msgtext = NewStrBufDup(MsgText);
522 if (smtp_resolve_recipients(Msg)) {
525 ((CitContext *)Msg->IO.CitContext)->cs_host,
527 sizeof(((CitContext *)
528 Msg->IO.CitContext)->cs_host));
530 syslog(LOG_DEBUG, "SMTP Starting: [%ld] <%s> CC <%d> \n",
531 Msg->MyQItem->MessageID,
532 ChrPtr(Msg->MyQEntry->Recipient),
533 ((CitContext*)Msg->IO.CitContext)->cs_pid);
534 if (Msg->pCurrRelay == NULL)
535 QueueEventContext(&Msg->IO,
537 else { /* oh... via relay host */
538 if (Msg->pCurrRelay->IsIP) {
539 QueueEventContext(&Msg->IO,
543 /* uneducated admin has chosen to
544 add DNS to the equation... */
545 QueueEventContext(&Msg->IO,
551 /* No recipients? well fail then. */
553 (Msg->MyQEntry == NULL)) {
554 Msg->MyQEntry->Status = 5;
555 StrBufPlain(Msg->MyQEntry->StatusMessage,
556 HKEY("Invalid Recipient!"));
558 FinalizeMessageSend(Msg);
567 /*****************************************************************************/
568 /* SMTP CLIENT DISPATCHER */
569 /*****************************************************************************/
571 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *Msg)
573 double Timeout = 0.0;
574 AsyncIO *IO = &Msg->IO;
576 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
578 switch (NextTCPState) {
582 Timeout = SMTP_C_SendTimeouts[Msg->State];
583 if (Msg->State == eDATABody) {
584 /* if we're sending a huge message,
587 Timeout += StrLength(Msg->msgtext) / 512;
591 Timeout = SMTP_C_ReadTimeouts[Msg->State];
592 if (Msg->State == eDATATerminateBody) {
594 * some mailservers take a nap before accepting
595 * the message content inspection and such.
597 Timeout += StrLength(Msg->msgtext) / 512;
607 case eTerminateConnection:
611 SetNextTimeout(&Msg->IO, Timeout);
613 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
615 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
616 SmtpOutMsg *Msg = IO->Data;
619 rc = ReadHandlers[Msg->State](Msg);
623 SMTPSetTimeout(rc, Msg);
627 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
629 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
630 SmtpOutMsg *Msg = IO->Data;
633 rc = SendHandlers[Msg->State](Msg);
634 SMTPSetTimeout(rc, Msg);
639 /*****************************************************************************/
640 /* SMTP CLIENT ERROR CATCHERS */
641 /*****************************************************************************/
642 eNextState SMTP_C_Terminate(AsyncIO *IO)
644 SmtpOutMsg *Msg = IO->Data;
646 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
647 FinalizeMessageSend(Msg);
650 eNextState SMTP_C_Timeout(AsyncIO *IO)
652 SmtpOutMsg *Msg = IO->Data;
654 Msg->MyQEntry->Status = 4;
655 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
656 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
657 if (Msg->State > eRCPT)
660 return FailOneAttempt(IO);
662 eNextState SMTP_C_ConnFail(AsyncIO *IO)
664 SmtpOutMsg *Msg = IO->Data;
666 Msg->MyQEntry->Status = 4;
667 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
668 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
669 return FailOneAttempt(IO);
671 eNextState SMTP_C_DNSFail(AsyncIO *IO)
673 SmtpOutMsg *Msg = IO->Data;
674 Msg->MyQEntry->Status = 4;
675 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
676 return FailOneAttempt(IO);
678 eNextState SMTP_C_Shutdown(AsyncIO *IO)
680 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
681 SmtpOutMsg *Msg = IO->Data;
683 Msg->MyQEntry->Status = 3;
684 StrBufPlain(Msg->MyQEntry->StatusMessage,
685 HKEY("server shutdown during message submit."));
686 FinalizeMessageSend(Msg);
692 * @brief lineread Handler;
693 * understands when to read more SMTP lines, and when this is a one-lined reply.
695 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
697 eReadState Finished = eBufferNotEmpty;
699 while (Finished == eBufferNotEmpty) {
700 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
703 case eMustReadMore: /// read new from socket...
706 case eBufferNotEmpty: /* shouldn't happen... */
707 case eReadSuccess: /// done for now...
708 if (StrLength(IO->IOBuf) < 4)
710 if (ChrPtr(IO->IOBuf)[3] == '-')
711 Finished = eBufferNotEmpty;
715 case eReadFail: /// WHUT?
723 CTDL_MODULE_INIT(smtp_eventclient)
725 return "smtpeventclient";