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-2009 by the citadel.org team
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 3 of the License, or
28 * (at your option) any later version.
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.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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)
98 ares_free_data(Msg->AllMX);
99 if (Msg->HostLookup.VParsedDNSReply != NULL)
100 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
101 FreeURL(&Msg->Relay);
102 FreeStrBuf(&Msg->msgtext);
103 FreeAsyncIOContents(&Msg->IO);
104 memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
108 eNextState SMTP_C_Shutdown(AsyncIO *IO);
109 eNextState SMTP_C_Timeout(AsyncIO *IO);
110 eNextState SMTP_C_ConnFail(AsyncIO *IO);
111 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
112 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
113 eNextState SMTP_C_DNSFail(AsyncIO *IO);
114 eNextState SMTP_C_Terminate(AsyncIO *IO);
115 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
117 eNextState mx_connect_ip(AsyncIO *IO);
118 eNextState get_one_mx_host_ip(AsyncIO *IO);
120 /******************************************************************************
121 * So, we're finished with sending (regardless of success or failure) *
122 * This Message might be referenced by several Queue-Items, if we're the last,*
123 * we need to free the memory and send bounce messages (on terminal failure) *
124 * else we just free our SMTP-Message struct. *
125 ******************************************************************************/
126 void FinalizeMessageSend(SmtpOutMsg *Msg)
128 int IDestructQueItem;
131 AsyncIO *IO = &Msg->IO;
133 IDestructQueItem = DecreaseQReference(Msg->MyQItem);
135 nRemain = CountActiveQueueEntries(Msg->MyQItem);
137 if ((nRemain > 0) || IDestructQueItem)
138 MsgData = SerializeQueueItem(Msg->MyQItem);
143 * Uncompleted delivery instructions remain, so delete the old
144 * instructions and replace with the updated ones.
146 EVS_syslog(LOG_DEBUG, "SMTPQD: %ld", Msg->MyQItem->QueMsgID);
147 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
149 if (IDestructQueItem)
150 smtpq_do_bounce(Msg->MyQItem,Msg->msgtext);
154 struct CtdlMessage *msg;
155 msg = malloc(sizeof(struct CtdlMessage));
156 memset(msg, 0, sizeof(struct CtdlMessage));
157 msg->cm_magic = CTDLMESSAGE_MAGIC;
158 msg->cm_anon_type = MES_NORMAL;
159 msg->cm_format_type = FMT_RFC822;
160 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
161 Msg->MyQItem->QueMsgID =
162 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
163 EVS_syslog(LOG_DEBUG, "SMTPQ: %ld", Msg->MyQItem->QueMsgID);
164 CtdlFreeMessage(msg);
167 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
168 &Msg->MyQItem->MessageID,
171 FreeStrBuf(&MsgData);
173 RemoveContext(Msg->IO.CitContext);
174 if (IDestructQueItem)
175 RemoveQItem(Msg->MyQItem);
176 DeleteSmtpOutMsg(Msg);
179 eNextState FailOneAttempt(AsyncIO *IO)
181 SmtpOutMsg *SendMsg = IO->Data;
183 if (SendMsg->MyQEntry->Status == 2)
187 * possible ways here:
188 * - connection timeout
189 * - dns lookup failed
191 StopClientWatchers(IO);
193 if (SendMsg->pCurrRelay != NULL)
194 SendMsg->pCurrRelay = SendMsg->pCurrRelay->Next;
196 if (SendMsg->pCurrRelay == NULL) {
197 EVS_syslog(LOG_DEBUG, "SMTP: %s Aborting\n", __FUNCTION__);
200 if (SendMsg->pCurrRelay->IsIP) {
201 EVS_syslog(LOG_DEBUG, "SMTP: %s connecting IP\n", __FUNCTION__);
202 return mx_connect_ip(IO);
205 EVS_syslog(LOG_DEBUG, "SMTP: %s resolving next MX Record\n", __FUNCTION__);
206 return get_one_mx_host_ip(IO);
211 void SetConnectStatus(AsyncIO *IO)
214 SmtpOutMsg *SendMsg = IO->Data;
220 if (IO->ConnectMe->IPv6) {
221 src = &IO->ConnectMe->Addr.sin6_addr;
224 struct sockaddr_in *addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
226 src = &addr->sin_addr.s_addr;
229 inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
234 if (SendMsg->mx_host == NULL)
235 SendMsg->mx_host = "<no MX-Record>";
237 EVS_syslog(LOG_DEBUG,
238 "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
242 SendMsg->IO.ConnectMe->Port);
244 SendMsg->MyQEntry->Status = 5;
245 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
246 "Timeout while connecting %s [%s]:%d ",
249 SendMsg->IO.ConnectMe->Port);
250 SendMsg->IO.NextState = eConnect;
253 /*****************************************************************************
254 * So we connect our Relay IP here. *
255 *****************************************************************************/
256 eNextState mx_connect_ip(AsyncIO *IO)
258 SmtpOutMsg *SendMsg = IO->Data;
260 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
262 IO->ConnectMe = SendMsg->pCurrRelay;
263 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
265 SetConnectStatus(IO);
267 return EvConnectSock(IO, SendMsg,
269 SMTP_C_ReadTimeouts[0],
273 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
275 SmtpOutMsg *SendMsg = IO->Data;
276 struct hostent *hostent;
280 hostent = SendMsg->HostLookup.VParsedDNSReply;
281 if ((SendMsg->HostLookup.DNSStatus == ARES_SUCCESS) &&
282 (hostent != NULL) ) {
283 memset(&SendMsg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
284 if (SendMsg->pCurrRelay->IPv6) {
285 memcpy(&SendMsg->pCurrRelay->Addr.sin6_addr.s6_addr,
286 &hostent->h_addr_list[0],
287 sizeof(struct in6_addr));
289 SendMsg->pCurrRelay->Addr.sin6_family = hostent->h_addrtype;
290 SendMsg->pCurrRelay->Addr.sin6_port = htons(DefaultMXPort);
293 struct sockaddr_in *addr = (struct sockaddr_in*) &SendMsg->pCurrRelay->Addr;
294 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
295 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
296 memcpy(&addr->sin_addr.s_addr,
297 hostent->h_addr_list[0],
300 addr->sin_family = hostent->h_addrtype;
301 addr->sin_port = htons(DefaultMXPort);
304 SendMsg->mx_host = SendMsg->pCurrRelay->Host;
305 return mx_connect_ip(IO);
307 else // TODO: here we need to find out whether there are more mx'es, backup relay, and so on
308 return FailOneAttempt(IO);
311 eNextState get_one_mx_host_ip(AsyncIO *IO)
313 SmtpOutMsg * SendMsg = IO->Data;
315 * here we start with the lookup of one host. it might be...
316 * - the relay host *sigh*
317 * - the direct hostname if there was no mx record
323 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
325 EVS_syslog(LOG_DEBUG,
326 "SMTP client[%ld]: looking up %s-Record %s : %d ...\n",
328 (SendMsg->pCurrRelay->IPv6)? "aaaa": "a",
329 SendMsg->pCurrRelay->Host,
330 SendMsg->pCurrRelay->Port);
332 if (!QueueQuery((SendMsg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
333 SendMsg->pCurrRelay->Host,
335 &SendMsg->HostLookup,
336 get_one_mx_host_ip_done))
338 SendMsg->MyQEntry->Status = 5;
339 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
340 "No MX hosts found for <%s>", SendMsg->node);
341 SendMsg->IO.NextState = eTerminateConnection;
342 return IO->NextState;
344 IO->NextState = eReadDNSReply;
345 return IO->NextState;
349 /*****************************************************************************
350 * here we try to find out about the MX records for our recipients. *
351 *****************************************************************************/
352 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
354 SmtpOutMsg * SendMsg = IO->Data;
359 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
360 pp = &SendMsg->Relay;
361 while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
364 if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
365 (IO->DNS.Query->VParsedDNSReply != NULL))
366 { /* ok, we found mx records. */
367 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
369 SendMsg->CurrMX = SendMsg->AllMX
370 = IO->DNS.Query->VParsedDNSReply;
371 while (SendMsg->CurrMX) {
373 for (i = 0; i < 2; i++) {
376 p = (ParsedURL*) malloc(sizeof(ParsedURL));
377 memset(p, 0, sizeof(ParsedURL));
379 p->Port = DefaultMXPort;
381 p->Host = SendMsg->CurrMX->host;
386 SendMsg->CurrMX = SendMsg->CurrMX->next;
388 SendMsg->CXFlags = SendMsg->CXFlags & F_HAVE_MX;
390 else { /* else fall back to the plain hostname */
392 for (i = 0; i < 2; i++) {
395 p = (ParsedURL*) malloc(sizeof(ParsedURL));
396 memset(p, 0, sizeof(ParsedURL));
398 p->Port = DefaultMXPort;
400 p->Host = SendMsg->node;
405 SendMsg->CXFlags = SendMsg->CXFlags & F_DIRECT;
407 *pp = SendMsg->MyQItem->FallBackHost;
408 SendMsg->pCurrRelay = SendMsg->Relay;
409 return get_one_mx_host_ip(IO);
412 eNextState resolve_mx_records(AsyncIO *IO)
414 SmtpOutMsg * SendMsg = IO->Data;
416 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
417 /* start resolving MX records here. */
418 if (!QueueQuery(ns_t_mx,
422 smtp_resolve_mx_record_done))
424 SendMsg->MyQEntry->Status = 5;
425 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
426 "No MX hosts found for <%s>", SendMsg->node);
427 return IO->NextState;
429 SendMsg->IO.NextState = eReadDNSReply;
430 return IO->NextState;
435 /******************************************************************************
436 * so, we're going to start a SMTP delivery. lets get it on. *
437 ******************************************************************************/
439 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
440 MailQEntry *MyQEntry,
443 SmtpOutMsg * SendMsg;
445 SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
446 memset(SendMsg, 0, sizeof(SmtpOutMsg));
448 SendMsg->n = MsgCount;
449 SendMsg->MyQEntry = MyQEntry;
450 SendMsg->MyQItem = MyQItem;
451 SendMsg->pCurrRelay = MyQItem->URL;
453 SendMsg->IO.Data = SendMsg;
455 SendMsg->IO.SendDone = SMTP_C_DispatchWriteDone;
456 SendMsg->IO.ReadDone = SMTP_C_DispatchReadDone;
457 SendMsg->IO.Terminate = SMTP_C_Terminate;
458 SendMsg->IO.LineReader = SMTP_C_ReadServerStatus;
459 SendMsg->IO.ConnFail = SMTP_C_ConnFail;
460 SendMsg->IO.DNS.Fail = SMTP_C_DNSFail;
461 SendMsg->IO.Timeout = SMTP_C_Timeout;
462 SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
464 SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
465 SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
466 SendMsg->IO.IOBuf = NewStrBuf();
468 SendMsg->IO.NextState = eReadMessage;
473 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
474 MailQEntry *MyQEntry,
476 int KeepMsgText, /* KeepMsgText allows us to use MsgText as ours. */
482 syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
484 SendMsg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
486 if (KeepMsgText) SendMsg->msgtext = MsgText;
487 else SendMsg->msgtext = NewStrBufDup(MsgText);
489 if (smtp_resolve_recipients(SendMsg)) {
491 SubC = CloneContext (CC);
492 SubC->session_specific_data = (char*) SendMsg;
493 SendMsg->IO.CitContext = SubC;
495 safestrncpy(SubC->cs_host, SendMsg->node, sizeof(SubC->cs_host));
496 syslog(LOG_DEBUG, "SMTP Starting: [%ld] <%s> CC <%d> \n",
497 SendMsg->MyQItem->MessageID,
498 ChrPtr(SendMsg->MyQEntry->Recipient),
499 ((CitContext*)SendMsg->IO.CitContext)->cs_pid);
500 if (SendMsg->pCurrRelay == NULL)
501 QueueEventContext(&SendMsg->IO,
503 else { /* oh... via relay host */
504 if (SendMsg->pCurrRelay->IsIP) {
505 QueueEventContext(&SendMsg->IO,
508 else { /* uneducated admin has chosen to add DNS to the equation... */
509 QueueEventContext(&SendMsg->IO,
515 /* No recipients? well fail then. */
516 if ((SendMsg==NULL) ||
517 (SendMsg->MyQEntry == NULL)) {
518 SendMsg->MyQEntry->Status = 5;
519 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
520 HKEY("Invalid Recipient!"));
522 FinalizeMessageSend(SendMsg);
531 /*****************************************************************************/
532 /* SMTP CLIENT DISPATCHER */
533 /*****************************************************************************/
535 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
537 double Timeout = 0.0;
538 AsyncIO *IO = &pMsg->IO;
540 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
542 switch (NextTCPState) {
546 Timeout = SMTP_C_SendTimeouts[pMsg->State];
547 if (pMsg->State == eDATABody) {
548 /* if we're sending a huge message, we need more time. */
549 Timeout += StrLength(pMsg->msgtext) / 1024;
553 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
554 if (pMsg->State == eDATATerminateBody) {
556 * some mailservers take a nap before accepting the message
557 * content inspection and such.
559 Timeout += StrLength(pMsg->msgtext) / 1024;
569 case eTerminateConnection:
573 SetNextTimeout(&pMsg->IO, Timeout);
575 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
577 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
578 SmtpOutMsg *pMsg = IO->Data;
581 rc = ReadHandlers[pMsg->State](pMsg);
585 SMTPSetTimeout(rc, pMsg);
589 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
591 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
592 SmtpOutMsg *pMsg = IO->Data;
595 rc = SendHandlers[pMsg->State](pMsg);
596 SMTPSetTimeout(rc, pMsg);
601 /*****************************************************************************/
602 /* SMTP CLIENT ERROR CATCHERS */
603 /*****************************************************************************/
604 eNextState SMTP_C_Terminate(AsyncIO *IO)
606 SmtpOutMsg *pMsg = IO->Data;
608 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
609 FinalizeMessageSend(pMsg);
612 eNextState SMTP_C_Timeout(AsyncIO *IO)
614 SmtpOutMsg *pMsg = IO->Data;
616 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
617 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
618 return FailOneAttempt(IO);
620 eNextState SMTP_C_ConnFail(AsyncIO *IO)
622 SmtpOutMsg *pMsg = IO->Data;
624 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
625 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
626 return FailOneAttempt(IO);
628 eNextState SMTP_C_DNSFail(AsyncIO *IO)
630 SmtpOutMsg *pMsg = IO->Data;
632 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
633 return FailOneAttempt(IO);
635 eNextState SMTP_C_Shutdown(AsyncIO *IO)
637 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
638 SmtpOutMsg *pMsg = IO->Data;
640 pMsg->MyQEntry->Status = 3;
641 StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
642 FinalizeMessageSend(pMsg);
648 * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
650 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
652 eReadState Finished = eBufferNotEmpty;
654 while (Finished == eBufferNotEmpty) {
655 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
658 case eMustReadMore: /// read new from socket...
661 case eBufferNotEmpty: /* shouldn't happen... */
662 case eReadSuccess: /// done for now...
663 if (StrLength(IO->IOBuf) < 4)
665 if (ChrPtr(IO->IOBuf)[3] == '-')
666 Finished = eBufferNotEmpty;
670 case eReadFail: /// WHUT?
678 CTDL_MODULE_INIT(smtp_eventclient)
680 return "smtpeventclient";