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 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
94 const unsigned short DefaultMXPort = 25;
95 void DeleteSmtpOutMsg(void *v)
99 ares_free_data(Msg->AllMX);
100 if (Msg->HostLookup.VParsedDNSReply != NULL)
101 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
102 FreeStrBuf(&Msg->msgtext);
103 FreeAsyncIOContents(&Msg->IO);
107 eNextState SMTP_C_Shutdown(AsyncIO *IO);
108 eNextState SMTP_C_Timeout(AsyncIO *IO);
109 eNextState SMTP_C_ConnFail(AsyncIO *IO);
110 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
111 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
112 eNextState SMTP_C_Terminate(AsyncIO *IO);
113 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
115 eNextState mx_connect_ip(AsyncIO *IO);
116 eNextState get_one_mx_host_ip(AsyncIO *IO);
118 /******************************************************************************
119 * So, we're finished with sending (regardless of success or failure) *
120 * This Message might be referenced by several Queue-Items, if we're the last,*
121 * we need to free the memory and send bounce messages (on terminal failure) *
122 * else we just free our SMTP-Message struct. *
123 ******************************************************************************/
124 void FinalizeMessageSend(SmtpOutMsg *Msg)
126 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
128 if (DecreaseQReference(Msg->MyQItem))
133 nRemain = CountActiveQueueEntries(Msg->MyQItem);
135 MsgData = SerializeQueueItem(Msg->MyQItem);
137 * Uncompleted delivery instructions remain, so delete the old
138 * instructions and replace with the updated ones.
140 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
141 smtpq_do_bounce(Msg->MyQItem,
144 struct CtdlMessage *msg;
145 msg = malloc(sizeof(struct CtdlMessage));
146 memset(msg, 0, sizeof(struct CtdlMessage));
147 msg->cm_magic = CTDLMESSAGE_MAGIC;
148 msg->cm_anon_type = MES_NORMAL;
149 msg->cm_format_type = FMT_RFC822;
150 msg->cm_fields['M'] = SmashStrBuf(&MsgData);
151 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
152 CtdlFreeMessage(msg);
155 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->MessageID, 1, "");
156 FreeStrBuf(&MsgData);
159 RemoveQItem(Msg->MyQItem);
161 DeleteSmtpOutMsg(Msg);
164 eNextState FailOneAttempt(AsyncIO *IO)
166 SmtpOutMsg *SendMsg = IO->Data;
168 * possible ways here:
169 * - connection timeout
172 SendMsg->pCurrRelay = SendMsg->pCurrRelay->Next;
174 if (SendMsg->pCurrRelay == NULL)
176 if (SendMsg->pCurrRelay->IsIP)
177 return mx_connect_ip(IO);
179 return get_one_mx_host_ip(IO);
183 void SetConnectStatus(AsyncIO *IO)
186 SmtpOutMsg *SendMsg = IO->Data;
193 src = &IO->Addr->sin6_addr;
196 struct sockaddr_in *addr = (struct sockaddr_in *)IO->Addr;
198 src = &addr->sin_addr.s_addr;
201 inet_ntop((IO->IP6)?AF_INET6:AF_INET,
206 if (SendMsg->mx_host == NULL)
207 SendMsg->mx_host = "<no name>";
209 CtdlLogPrintf(CTDL_DEBUG,
210 "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
216 SendMsg->MyQEntry->Status = 5;
217 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
218 "Timeout while connecting %s [%s]:%d ",
224 /*****************************************************************************
225 * So we connect our Relay IP here. *
226 *****************************************************************************/
227 eNextState mx_connect_ip(AsyncIO *IO)
229 SmtpOutMsg *SendMsg = IO->Data;
231 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
233 IO->IP6 = SendMsg->pCurrRelay->af == AF_INET6;
235 if (SendMsg->pCurrRelay->Port != 0)
236 IO->dport = SendMsg->pCurrRelay->Port;
237 IO->Addr = &SendMsg->pCurrRelay->Addr;
238 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
240 SetConnectStatus(IO);
242 return InitEventIO(IO, SendMsg,
244 SMTP_C_ReadTimeouts[0],
248 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
250 SmtpOutMsg *SendMsg = IO->Data;
251 struct hostent *hostent;
255 hostent = SendMsg->HostLookup.VParsedDNSReply;
256 if ((SendMsg->HostLookup.DNSStatus == ARES_SUCCESS) &&
257 (hostent != NULL) ) {
258 memset(&SendMsg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
260 memcpy(&SendMsg->pCurrRelay->Addr.sin6_addr.s6_addr,
261 &hostent->h_addr_list[0],
262 sizeof(struct in6_addr));
264 SendMsg->pCurrRelay->Addr.sin6_family = hostent->h_addrtype;
265 SendMsg->pCurrRelay->Addr.sin6_port = htons(IO->dport);
268 struct sockaddr_in *addr = (struct sockaddr_in*) &SendMsg->pCurrRelay->Addr;
269 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
270 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
271 memcpy(&addr->sin_addr.s_addr,
272 hostent->h_addr_list[0],
275 addr->sin_family = hostent->h_addrtype;
276 addr->sin_port = htons(IO->dport);
279 return mx_connect_ip(IO);
281 else // TODO: here we need to find out whether there are more mx'es, backup relay, and so on
282 return FailOneAttempt(IO);
285 eNextState get_one_mx_host_ip(AsyncIO *IO)
287 SmtpOutMsg * SendMsg = IO->Data;
289 * here we start with the lookup of one host. it might be...
290 * - the relay host *sigh*
291 * - the direct hostname if there was no mx record
297 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
299 CtdlLogPrintf(CTDL_DEBUG,
300 "SMTP client[%ld]: looking up %s : %d ...\n",
302 SendMsg->pCurrRelay->Host,
305 if (!QueueQuery((SendMsg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
306 SendMsg->pCurrRelay->Host,
308 &SendMsg->HostLookup,
309 get_one_mx_host_ip_done))
311 SendMsg->MyQEntry->Status = 5;
312 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
313 "No MX hosts found for <%s>", SendMsg->node);
314 return IO->NextState;
316 return IO->NextState;
320 /*****************************************************************************
321 * here we try to find out about the MX records for our recipients. *
322 *****************************************************************************/
323 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
325 SmtpOutMsg * SendMsg = IO->Data;
330 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
331 pp = &SendMsg->Relay;
332 while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
335 if ((IO->DNSQuery->DNSStatus == ARES_SUCCESS) &&
336 (IO->DNSQuery->VParsedDNSReply != NULL))
337 { /* ok, we found mx records. */
338 SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
340 SendMsg->CurrMX = SendMsg->AllMX
341 = IO->DNSQuery->VParsedDNSReply;
342 while (SendMsg->CurrMX) {
344 for (i = 0; i < 2; i++) {
347 p = (ParsedURL*) malloc(sizeof(ParsedURL));
348 memset(p, 0, sizeof(ParsedURL));
350 p->Port = 25; //// TODO define.
352 p->Host = SendMsg->CurrMX->host;
357 SendMsg->CurrMX = SendMsg->CurrMX->next;
359 SendMsg->CXFlags = SendMsg->CXFlags & F_HAVE_MX;
361 else { /* else fall back to the plain hostname */
363 for (i = 0; i < 2; i++) {
366 p = (ParsedURL*) malloc(sizeof(ParsedURL));
367 memset(p, 0, sizeof(ParsedURL));
369 p->Port = 25; //// TODO define.
371 p->Host = SendMsg->node;
376 SendMsg->CXFlags = SendMsg->CXFlags & F_DIRECT;
378 *pp = SendMsg->MyQItem->FallBackHost;
379 SendMsg->pCurrRelay = SendMsg->Relay;
380 return get_one_mx_host_ip(IO);
383 eNextState resolve_mx_records(AsyncIO *IO)
385 SmtpOutMsg * SendMsg = IO->Data;
387 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
388 /* start resolving MX records here. */
389 if (!QueueQuery(ns_t_mx,
393 smtp_resolve_mx_record_done))
395 SendMsg->MyQEntry->Status = 5;
396 StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
397 "No MX hosts found for <%s>", SendMsg->node);
398 return IO->NextState;
405 /******************************************************************************
406 * so, we're going to start a SMTP delivery. lets get it on. *
407 ******************************************************************************/
409 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
410 MailQEntry *MyQEntry,
413 SmtpOutMsg * SendMsg;
415 SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
416 memset(SendMsg, 0, sizeof(SmtpOutMsg));
418 SendMsg->n = MsgCount;
419 SendMsg->MyQEntry = MyQEntry;
420 SendMsg->MyQItem = MyQItem;
421 SendMsg->pCurrRelay = MyQItem->URL;
423 SendMsg->IO.Data = SendMsg;
425 SendMsg->IO.SendDone = SMTP_C_DispatchWriteDone;
426 SendMsg->IO.ReadDone = SMTP_C_DispatchReadDone;
427 SendMsg->IO.Terminate = SMTP_C_Terminate;
428 SendMsg->IO.LineReader = SMTP_C_ReadServerStatus;
429 SendMsg->IO.ConnFail = SMTP_C_ConnFail;
430 SendMsg->IO.Timeout = SMTP_C_Timeout;
431 SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
433 SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
434 SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
435 SendMsg->IO.IOBuf = NewStrBuf();
437 SendMsg->IO.sock = (-1);
438 SendMsg->IO.NextState = eReadMessage;
439 SendMsg->IO.dport = DefaultMXPort;
444 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
445 MailQEntry *MyQEntry,
447 int KeepMsgText, /* KeepMsgText allows us to use MsgText as ours. */
452 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
454 SendMsg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
455 if (KeepMsgText) SendMsg->msgtext = MsgText;
456 else SendMsg->msgtext = NewStrBufDup(MsgText);
458 if (smtp_resolve_recipients(SendMsg)) {
459 if (SendMsg->pCurrRelay == NULL)
460 QueueEventContext(&SendMsg->IO,
462 else { /* oh... via relay host */
463 if (SendMsg->pCurrRelay->IsIP) {
464 QueueEventContext(&SendMsg->IO,
467 else { /* uneducated admin has chosen to add DNS to the equation... */
468 QueueEventContext(&SendMsg->IO,
474 /* No recipients? well fail then. */
475 if ((SendMsg==NULL) ||
476 (SendMsg->MyQEntry == NULL)) {
477 SendMsg->MyQEntry->Status = 5;
478 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
479 HKEY("Invalid Recipient!"));
481 FinalizeMessageSend(SendMsg);
490 /*****************************************************************************/
491 /* SMTP CLIENT DISPATCHER */
492 /*****************************************************************************/
494 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
496 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
498 switch (NextTCPState) {
501 Timeout = SMTP_C_SendTimeouts[pMsg->State];
502 if (pMsg->State == eDATABody) {
503 /* if we're sending a huge message, we need more time. */
504 Timeout += StrLength(pMsg->msgtext) / 1024;
508 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
509 if (pMsg->State == eDATATerminateBody) {
511 * some mailservers take a nap before accepting the message
512 * content inspection and such.
514 Timeout += StrLength(pMsg->msgtext) / 1024;
517 case eTerminateConnection:
521 SetNextTimeout(&pMsg->IO, Timeout);
523 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
525 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
526 SmtpOutMsg *pMsg = IO->Data;
529 rc = ReadHandlers[pMsg->State](pMsg);
531 SMTPSetTimeout(rc, pMsg);
534 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
536 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
537 SmtpOutMsg *pMsg = IO->Data;
540 rc = SendHandlers[pMsg->State](pMsg);
541 SMTPSetTimeout(rc, pMsg);
546 /*****************************************************************************/
547 /* SMTP CLIENT ERROR CATCHERS */
548 /*****************************************************************************/
549 eNextState SMTP_C_Terminate(AsyncIO *IO)
551 SmtpOutMsg *pMsg = IO->Data;
553 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
554 FinalizeMessageSend(pMsg);
557 eNextState SMTP_C_Timeout(AsyncIO *IO)
559 SmtpOutMsg *pMsg = IO->Data;
561 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
562 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
563 return FailOneAttempt(IO);
565 eNextState SMTP_C_ConnFail(AsyncIO *IO)
567 SmtpOutMsg *pMsg = IO->Data;
569 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
570 StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
571 return FailOneAttempt(IO);
573 eNextState SMTP_C_Shutdown(AsyncIO *IO)
575 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
576 SmtpOutMsg *pMsg = IO->Data;
578 pMsg->MyQEntry->Status = 3;
579 StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
580 FinalizeMessageSend(pMsg);
586 * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
588 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
590 eReadState Finished = eBufferNotEmpty;
592 while (Finished == eBufferNotEmpty) {
593 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
596 case eMustReadMore: /// read new from socket...
599 case eBufferNotEmpty: /* shouldn't happen... */
600 case eReadSuccess: /// done for now...
601 if (StrLength(IO->IOBuf) < 4)
603 if (ChrPtr(IO->IOBuf)[3] == '-')
604 Finished = eBufferNotEmpty;
608 case eReadFail: /// WHUT?
617 CTDL_MODULE_INIT(smtp_eventclient)
619 return "smtpeventclient";