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 /* these are kept in our own space and free'd below */
99 Msg->IO.ConnectMe = NULL;
101 ares_free_data(Msg->AllMX);
102 if (Msg->HostLookup.VParsedDNSReply != NULL)
103 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
104 FreeURL(&Msg->Relay);
105 FreeStrBuf(&Msg->msgtext);
106 FreeAsyncIOContents(&Msg->IO);
107 memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
111 eNextState SMTP_C_Shutdown(AsyncIO *IO);
112 eNextState SMTP_C_Timeout(AsyncIO *IO);
113 eNextState SMTP_C_ConnFail(AsyncIO *IO);
114 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
115 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
116 eNextState SMTP_C_DNSFail(AsyncIO *IO);
117 eNextState SMTP_C_Terminate(AsyncIO *IO);
118 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
120 eNextState mx_connect_ip(AsyncIO *IO);
121 eNextState get_one_mx_host_ip(AsyncIO *IO);
123 /******************************************************************************
124 * So, we're finished with sending (regardless of success or failure) *
125 * This Message might be referenced by several Queue-Items, if we're the last,*
126 * we need to free the memory and send bounce messages (on terminal failure) *
127 * else we just free our SMTP-Message struct. *
128 ******************************************************************************/
129 void FinalizeMessageSend(SmtpOutMsg *Msg)
131 int IDestructQueItem;
134 AsyncIO *IO = &Msg->IO;
136 IDestructQueItem = DecreaseQReference(Msg->MyQItem);
138 nRemain = CountActiveQueueEntries(Msg->MyQItem);
140 if ((nRemain > 0) || IDestructQueItem)
141 MsgData = SerializeQueueItem(Msg->MyQItem);
146 * Uncompleted delivery instructions remain, so delete the old
147 * instructions and replace with the updated ones.
149 EVS_syslog(LOG_DEBUG, "SMTPQD: %ld", Msg->MyQItem->QueMsgID);
150 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
152 if (IDestructQueItem)
153 smtpq_do_bounce(Msg->MyQItem,Msg->msgtext);
157 struct CtdlMessage *msg;
158 msg = malloc(sizeof(struct CtdlMessage));
159 memset(msg, 0, sizeof(struct CtdlMessage));
160 msg->cm_magic = CTDLMESSAGE_MAGIC;
161 msg->cm_anon_type = MES_NORMAL;
162 msg->cm_format_type = FMT_RFC822;
163 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
164 Msg->MyQItem->QueMsgID =
165 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
166 EVS_syslog(LOG_DEBUG, "SMTPQ: %ld", Msg->MyQItem->QueMsgID);
167 CtdlFreeMessage(msg);
170 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
171 &Msg->MyQItem->MessageID,
174 FreeStrBuf(&MsgData);
176 RemoveContext(Msg->IO.CitContext);
177 if (IDestructQueItem)
178 RemoveQItem(Msg->MyQItem);
179 DeleteSmtpOutMsg(Msg);
182 eNextState FailOneAttempt(AsyncIO *IO)
184 SmtpOutMsg *SendMsg = IO->Data;
186 if (SendMsg->MyQEntry->Status == 2)
190 * possible ways here:
191 * - connection timeout
192 * - dns lookup failed
194 StopClientWatchers(IO);
196 if (SendMsg->pCurrRelay != NULL)
197 SendMsg->pCurrRelay = SendMsg->pCurrRelay->Next;
199 if (SendMsg->pCurrRelay == NULL) {
200 EVS_syslog(LOG_DEBUG, "SMTP: %s Aborting\n", __FUNCTION__);
203 if (SendMsg->pCurrRelay->IsIP) {
204 EVS_syslog(LOG_DEBUG, "SMTP: %s connecting IP\n", __FUNCTION__);
205 return mx_connect_ip(IO);
208 EVS_syslog(LOG_DEBUG,
209 "SMTP: %s resolving next MX Record\n",
211 return get_one_mx_host_ip(IO);
216 void SetConnectStatus(AsyncIO *IO)
218 SmtpOutMsg *SendMsg = IO->Data;
224 if (IO->ConnectMe->IPv6) {
225 src = &IO->ConnectMe->Addr.sin6_addr;
228 struct sockaddr_in *addr;
230 addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
231 src = &addr->sin_addr.s_addr;
234 inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
239 if (SendMsg->mx_host == NULL)
240 SendMsg->mx_host = "<no MX-Record>";
242 EVS_syslog(LOG_DEBUG,
243 "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
247 SendMsg->IO.ConnectMe->Port);
249 SendMsg->MyQEntry->Status = 5;
250 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
251 "Timeout while connecting %s [%s]:%d ",
254 SendMsg->IO.ConnectMe->Port);
255 SendMsg->IO.NextState = eConnect;
258 /*****************************************************************************
259 * So we connect our Relay IP here. *
260 *****************************************************************************/
261 eNextState mx_connect_ip(AsyncIO *IO)
263 SmtpOutMsg *SendMsg = IO->Data;
265 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
267 IO->ConnectMe = SendMsg->pCurrRelay;
269 SetConnectStatus(IO);
271 return EvConnectSock(IO,
273 SMTP_C_ReadTimeouts[0],
277 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
279 SmtpOutMsg *SendMsg = IO->Data;
280 struct hostent *hostent;
284 hostent = SendMsg->HostLookup.VParsedDNSReply;
285 if ((SendMsg->HostLookup.DNSStatus == ARES_SUCCESS) &&
286 (hostent != NULL) ) {
287 memset(&SendMsg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
288 if (SendMsg->pCurrRelay->IPv6) {
289 memcpy(&SendMsg->pCurrRelay->Addr.sin6_addr.s6_addr,
290 &hostent->h_addr_list[0],
291 sizeof(struct in6_addr));
293 SendMsg->pCurrRelay->Addr.sin6_family =
295 SendMsg->pCurrRelay->Addr.sin6_port =
296 htons(DefaultMXPort);
299 struct sockaddr_in *addr;
301 * Bypass the ns lookup result like this:
302 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
303 * addr->sin_addr.s_addr =
304 * htonl((uint32_t)&hostent->h_addr_list[0]);
307 addr = (struct sockaddr_in*) &SendMsg->pCurrRelay->Addr;
309 memcpy(&addr->sin_addr.s_addr,
310 hostent->h_addr_list[0],
313 addr->sin_family = hostent->h_addrtype;
314 addr->sin_port = htons(DefaultMXPort);
316 SendMsg->mx_host = SendMsg->pCurrRelay->Host;
317 return mx_connect_ip(IO);
320 return FailOneAttempt(IO);
323 eNextState get_one_mx_host_ip(AsyncIO *IO)
325 SmtpOutMsg * SendMsg = IO->Data;
327 * here we start with the lookup of one host. it might be...
328 * - the relay host *sigh*
329 * - the direct hostname if there was no mx record
335 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
337 EVS_syslog(LOG_DEBUG,
338 "SMTP client[%ld]: looking up %s-Record %s : %d ...\n",
340 (SendMsg->pCurrRelay->IPv6)? "aaaa": "a",
341 SendMsg->pCurrRelay->Host,
342 SendMsg->pCurrRelay->Port);
344 if (!QueueQuery((SendMsg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
345 SendMsg->pCurrRelay->Host,
347 &SendMsg->HostLookup,
348 get_one_mx_host_ip_done))
350 SendMsg->MyQEntry->Status = 5;
351 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
352 "No MX hosts found for <%s>", SendMsg->node);
353 SendMsg->IO.NextState = eTerminateConnection;
354 return IO->NextState;
356 IO->NextState = eReadDNSReply;
357 return IO->NextState;
361 /*****************************************************************************
362 * here we try to find out about the MX records for our recipients. *
363 *****************************************************************************/
364 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
366 SmtpOutMsg * SendMsg = IO->Data;
371 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
372 pp = &SendMsg->Relay;
373 while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
376 if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
377 (IO->DNS.Query->VParsedDNSReply != NULL))
378 { /* ok, we found mx records. */
379 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
383 = IO->DNS.Query->VParsedDNSReply;
384 while (SendMsg->CurrMX) {
386 for (i = 0; i < 2; i++) {
389 p = (ParsedURL*) malloc(sizeof(ParsedURL));
390 memset(p, 0, sizeof(ParsedURL));
392 p->Port = DefaultMXPort;
394 p->Host = SendMsg->CurrMX->host;
399 SendMsg->CurrMX = SendMsg->CurrMX->next;
401 SendMsg->CXFlags = SendMsg->CXFlags & F_HAVE_MX;
403 else { /* else fall back to the plain hostname */
405 for (i = 0; i < 2; i++) {
408 p = (ParsedURL*) malloc(sizeof(ParsedURL));
409 memset(p, 0, sizeof(ParsedURL));
411 p->Port = DefaultMXPort;
413 p->Host = SendMsg->node;
418 SendMsg->CXFlags = SendMsg->CXFlags & F_DIRECT;
420 *pp = SendMsg->MyQItem->FallBackHost;
421 SendMsg->pCurrRelay = SendMsg->Relay;
422 return get_one_mx_host_ip(IO);
425 eNextState resolve_mx_records(AsyncIO *IO)
427 SmtpOutMsg * SendMsg = IO->Data;
429 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
430 /* start resolving MX records here. */
431 if (!QueueQuery(ns_t_mx,
435 smtp_resolve_mx_record_done))
437 SendMsg->MyQEntry->Status = 5;
438 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
439 "No MX hosts found for <%s>", SendMsg->node);
440 return IO->NextState;
442 SendMsg->IO.NextState = eReadDNSReply;
443 return IO->NextState;
448 /******************************************************************************
449 * so, we're going to start a SMTP delivery. lets get it on. *
450 ******************************************************************************/
452 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
453 MailQEntry *MyQEntry,
456 SmtpOutMsg * SendMsg;
458 SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
459 memset(SendMsg, 0, sizeof(SmtpOutMsg));
461 SendMsg->n = MsgCount;
462 SendMsg->MyQEntry = MyQEntry;
463 SendMsg->MyQItem = MyQItem;
464 SendMsg->pCurrRelay = MyQItem->URL;
466 InitIOStruct(&SendMsg->IO,
469 SMTP_C_ReadServerStatus,
471 SMTP_C_DispatchWriteDone,
472 SMTP_C_DispatchReadDone,
481 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
482 MailQEntry *MyQEntry,
484 /*KeepMsgText allows us to use MsgText as ours.*/
490 syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
492 SendMsg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
493 if (KeepMsgText) SendMsg->msgtext = MsgText;
494 else SendMsg->msgtext = NewStrBufDup(MsgText);
496 if (smtp_resolve_recipients(SendMsg)) {
499 ((CitContext *)SendMsg->IO.CitContext)->cs_host,
501 sizeof(((CitContext *)
502 SendMsg->IO.CitContext)->cs_host));
504 syslog(LOG_DEBUG, "SMTP Starting: [%ld] <%s> CC <%d> \n",
505 SendMsg->MyQItem->MessageID,
506 ChrPtr(SendMsg->MyQEntry->Recipient),
507 ((CitContext*)SendMsg->IO.CitContext)->cs_pid);
508 if (SendMsg->pCurrRelay == NULL)
509 QueueEventContext(&SendMsg->IO,
511 else { /* oh... via relay host */
512 if (SendMsg->pCurrRelay->IsIP) {
513 QueueEventContext(&SendMsg->IO,
517 /* uneducated admin has chosen to
518 add DNS to the equation... */
519 QueueEventContext(&SendMsg->IO,
525 /* No recipients? well fail then. */
526 if ((SendMsg==NULL) ||
527 (SendMsg->MyQEntry == NULL)) {
528 SendMsg->MyQEntry->Status = 5;
529 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
530 HKEY("Invalid Recipient!"));
532 FinalizeMessageSend(SendMsg);
541 /*****************************************************************************/
542 /* SMTP CLIENT DISPATCHER */
543 /*****************************************************************************/
545 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
547 double Timeout = 0.0;
548 AsyncIO *IO = &pMsg->IO;
550 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
552 switch (NextTCPState) {
556 Timeout = SMTP_C_SendTimeouts[pMsg->State];
557 if (pMsg->State == eDATABody) {
558 /* if we're sending a huge message,
561 Timeout += StrLength(pMsg->msgtext) / 1024;
565 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
566 if (pMsg->State == eDATATerminateBody) {
568 * some mailservers take a nap before accepting
569 * the message content inspection and such.
571 Timeout += StrLength(pMsg->msgtext) / 1024;
581 case eTerminateConnection:
585 SetNextTimeout(&pMsg->IO, Timeout);
587 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
589 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
590 SmtpOutMsg *pMsg = IO->Data;
593 rc = ReadHandlers[pMsg->State](pMsg);
597 SMTPSetTimeout(rc, pMsg);
601 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
603 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
604 SmtpOutMsg *pMsg = IO->Data;
607 rc = SendHandlers[pMsg->State](pMsg);
608 SMTPSetTimeout(rc, pMsg);
613 /*****************************************************************************/
614 /* SMTP CLIENT ERROR CATCHERS */
615 /*****************************************************************************/
616 eNextState SMTP_C_Terminate(AsyncIO *IO)
618 SmtpOutMsg *pMsg = IO->Data;
620 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
621 FinalizeMessageSend(pMsg);
624 eNextState SMTP_C_Timeout(AsyncIO *IO)
626 SmtpOutMsg *pMsg = IO->Data;
628 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
629 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
630 return FailOneAttempt(IO);
632 eNextState SMTP_C_ConnFail(AsyncIO *IO)
634 SmtpOutMsg *pMsg = IO->Data;
636 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
637 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
638 return FailOneAttempt(IO);
640 eNextState SMTP_C_DNSFail(AsyncIO *IO)
642 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
643 return FailOneAttempt(IO);
645 eNextState SMTP_C_Shutdown(AsyncIO *IO)
647 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
648 SmtpOutMsg *pMsg = IO->Data;
650 pMsg->MyQEntry->Status = 3;
651 StrBufPlain(pMsg->MyQEntry->StatusMessage,
652 HKEY("server shutdown during message submit."));
653 FinalizeMessageSend(pMsg);
659 * @brief lineread Handler;
660 * understands when to read more SMTP lines, and when this is a one-lined reply.
662 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
664 eReadState Finished = eBufferNotEmpty;
666 while (Finished == eBufferNotEmpty) {
667 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
670 case eMustReadMore: /// read new from socket...
673 case eBufferNotEmpty: /* shouldn't happen... */
674 case eReadSuccess: /// done for now...
675 if (StrLength(IO->IOBuf) < 4)
677 if (ChrPtr(IO->IOBuf)[3] == '-')
678 Finished = eBufferNotEmpty;
682 case eReadFail: /// WHUT?
690 CTDL_MODULE_INIT(smtp_eventclient)
692 return "smtpeventclient";