b3ec5da0e5ff8bed1a35197d855e7fdc44b8a1ad
[citadel] / citadel / modules / smtp / serv_smtpeventclient.c
1 /*
2  * This module is an SMTP and ESMTP implementation for the Citadel system.
3  * It is compliant with all of the following:
4  *
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
19  *
20  * The VRFY and EXPN commands have been removed from this implementation
21  * because nobody uses these commands anymore, except for spammers.
22  *
23  * Copyright (c) 1998-2012 by the citadel.org team
24  *
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.
27  *  
28  *  
29  *
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.
34  *
35  *  
36  *  
37  *  
38  */
39
40 #include "sysdep.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <termios.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 #  include <sys/time.h>
58 # else
59 #  include <time.h>
60 # endif
61 #endif
62 #include <sys/wait.h>
63 #include <ctype.h>
64 #include <string.h>
65 #include <limits.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
70 #include "citadel.h"
71 #include "server.h"
72 #include "citserver.h"
73 #include "support.h"
74 #include "config.h"
75 #include "control.h"
76 #include "user_ops.h"
77 #include "database.h"
78 #include "msgbase.h"
79 #include "internet_addressing.h"
80 #include "genstamp.h"
81 #include "domain.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
85
86 #include "ctdl_module.h"
87
88 #include "smtp_util.h"
89 #include "event_client.h"
90 #include "smtpqueue.h"
91 #include "smtp_clienthandlers.h"
92
93 ConstStr SMTPStates[] = {
94         {HKEY("looking up mx - record")},
95         {HKEY("evaluating what to do next")},
96         {HKEY("looking up a - record")},
97         {HKEY("looking up aaaa - record")},
98         {HKEY("connecting remote")},
99         {HKEY("smtp conversation ongoing")},
100         {HKEY("smtp sending maildata")},
101         {HKEY("smtp sending done")},
102         {HKEY("smtp successfully finished")},
103         {HKEY("failed one attempt")},
104         {HKEY("failed temporarily")},
105         {HKEY("failed permanently")}
106 };
107
108 void SetSMTPState(AsyncIO *IO, smtpstate State)
109 {
110         CitContext* CCC = IO->CitContext;
111         memcpy(CCC->cs_clientname, SMTPStates[State].Key, SMTPStates[State].len + 1);
112 }
113
114 int SMTPClientDebugEnabled = 0;
115 void DeleteSmtpOutMsg(void *v)
116 {
117         SmtpOutMsg *Msg = v;
118         AsyncIO *IO = &Msg->IO;
119         EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
120
121         /* these are kept in our own space and free'd below */
122         Msg->IO.ConnectMe = NULL;
123
124         ares_free_data(Msg->AllMX);
125         if (Msg->HostLookup.VParsedDNSReply != NULL)
126                 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
127         FreeURL(&Msg->Relay);
128         FreeStrBuf(&Msg->msgtext);
129         FreeAsyncIOContents(&Msg->IO);
130         memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
131         free(Msg);
132 }
133
134 eNextState SMTP_C_Shutdown(AsyncIO *IO);
135 eNextState SMTP_C_Timeout(AsyncIO *IO);
136 eNextState SMTP_C_ConnFail(AsyncIO *IO);
137 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
138 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
139 eNextState SMTP_C_DNSFail(AsyncIO *IO);
140 eNextState SMTP_C_Terminate(AsyncIO *IO);
141 eNextState SMTP_C_TerminateDB(AsyncIO *IO);
142 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
143
144 eNextState mx_connect_ip(AsyncIO *IO);
145 eNextState get_one_mx_host_ip(AsyncIO *IO);
146
147 /******************************************************************************
148  * So, we're finished with sending (regardless of success or failure)         *
149  * This Message might be referenced by several Queue-Items, if we're the last,*
150  * we need to free the memory and send bounce messages (on terminal failure)  *
151  * else we just free our SMTP-Message struct.                                 *
152  ******************************************************************************/
153 eNextState FinalizeMessageSend_DB(AsyncIO *IO)
154 {
155         const char *Status;
156         SmtpOutMsg *Msg = IO->Data;
157         StrBuf *StatusMessage;
158
159         if (Msg->MyQEntry->AllStatusMessages != NULL)
160                 StatusMessage = Msg->MyQEntry->AllStatusMessages;
161         else
162                 StatusMessage = Msg->MyQEntry->StatusMessage;
163
164
165         if (Msg->MyQEntry->Status == 2) {
166                 SetSMTPState(IO, eSTMPfinished);
167                 Status = "Delivery successful.";
168         }
169         else if (Msg->MyQEntry->Status == 5) {
170                 SetSMTPState(IO, eSMTPFailTotal);
171                 Status = "Delivery failed permanently; giving up.";
172         }
173         else {
174                 SetSMTPState(IO, eSMTPFailTemporary);
175                 Status = "Delivery failed temporarily; will retry later.";
176         }
177                         
178         EVS_syslog(LOG_INFO,
179                    "%s Time[%fs] Recipient <%s> @ <%s> (%s) Status message: %s\n",
180                    Status,
181                    Msg->IO.Now - Msg->IO.StartIO,
182                    Msg->user,
183                    Msg->node,
184                    Msg->name,
185                    ChrPtr(StatusMessage));
186
187
188         Msg->IDestructQueItem = DecreaseQReference(Msg->MyQItem);
189
190         Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem, 0);
191
192         if (Msg->MyQEntry->Active && 
193             !Msg->MyQEntry->StillActive &&
194             CheckQEntryIsBounce(Msg->MyQEntry))
195         {
196                 /* are we casue for a bounce mail? */
197                 Msg->MyQItem->SendBounceMail |= (1<<Msg->MyQEntry->Status);
198         }
199
200         if ((Msg->nRemain > 0) || Msg->IDestructQueItem)
201                 Msg->QMsgData = SerializeQueueItem(Msg->MyQItem);
202         else
203                 Msg->QMsgData = NULL;
204
205         /*
206          * Uncompleted delivery instructions remain, so delete the old
207          * instructions and replace with the updated ones.
208          */
209         EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
210         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
211         Msg->MyQItem->QueMsgID = -1;
212
213         if (Msg->IDestructQueItem)
214                 smtpq_do_bounce(Msg->MyQItem, StatusMessage, Msg->msgtext, Msg->pCurrRelay);
215
216         if (Msg->nRemain > 0)
217         {
218                 struct CtdlMessage *msg;
219                 msg = malloc(sizeof(struct CtdlMessage));
220                 memset(msg, 0, sizeof(struct CtdlMessage));
221                 msg->cm_magic = CTDLMESSAGE_MAGIC;
222                 msg->cm_anon_type = MES_NORMAL;
223                 msg->cm_format_type = FMT_RFC822;
224                 msg->cm_fields['M'] = SmashStrBuf(&Msg->QMsgData);
225                 msg->cm_fields['U'] = strdup("QMSG");
226                 Msg->MyQItem->QueMsgID =
227                         CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
228                 EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
229                 CtdlFreeMessage(msg);
230         }
231         else {
232                 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
233                                    &Msg->MyQItem->MessageID,
234                                    1,
235                                    "");
236                 FreeStrBuf(&Msg->QMsgData);
237         }
238
239         RemoveContext(Msg->IO.CitContext);
240         return eAbort;
241 }
242
243 eNextState Terminate(AsyncIO *IO)
244 {
245         SmtpOutMsg *Msg = IO->Data;
246
247         if (Msg->IDestructQueItem)
248                 RemoveQItem(Msg->MyQItem);
249
250         DeleteSmtpOutMsg(Msg);
251         return eAbort;
252 }
253 eNextState FinalizeMessageSend(SmtpOutMsg *Msg)
254 {
255         /* hand over to DB Queue */
256         return QueueDBOperation(&Msg->IO, FinalizeMessageSend_DB);
257 }
258
259 eNextState FailOneAttempt(AsyncIO *IO)
260 {
261         SmtpOutMsg *Msg = IO->Data;
262
263         SetSMTPState(IO, eSTMPfailOne);
264         if (Msg->MyQEntry->Status == 2)
265                 return eAbort;
266
267         /*
268          * possible ways here:
269          * - connection timeout
270          * - dns lookup failed
271          */
272         StopClientWatchers(IO, 1);
273
274         Msg->MyQEntry->nAttempt ++;
275         if (Msg->MyQEntry->AllStatusMessages == NULL)
276                 Msg->MyQEntry->AllStatusMessages = NewStrBuf();
277
278         StrBufAppendPrintf(Msg->MyQEntry->AllStatusMessages, "%ld) ", Msg->MyQEntry->nAttempt);
279         StrBufAppendBuf(Msg->MyQEntry->AllStatusMessages, Msg->MyQEntry->StatusMessage, 0);
280         StrBufAppendBufPlain(Msg->MyQEntry->AllStatusMessages, HKEY("; "), 0);
281
282         if (Msg->pCurrRelay != NULL)
283                 Msg->pCurrRelay = Msg->pCurrRelay->Next;
284         if ((Msg->pCurrRelay != NULL) &&
285             !Msg->pCurrRelay->IsRelay &&
286             Msg->MyQItem->HaveRelay)
287         {
288                 EVS_syslog(LOG_DEBUG, "%s Aborting; last relay failed.\n", __FUNCTION__);
289                 return eAbort;
290         }
291
292         if (Msg->pCurrRelay == NULL) {
293                 EVS_syslog(LOG_DEBUG, "%s Aborting\n", __FUNCTION__);
294                 return eAbort;
295         }
296         if (Msg->pCurrRelay->IsIP) {
297                 EVS_syslog(LOG_DEBUG, "%s connecting IP\n", __FUNCTION__);
298                 return mx_connect_ip(IO);
299         }
300         else {
301                 EVS_syslog(LOG_DEBUG,
302                            "%s resolving next MX Record\n",
303                            __FUNCTION__);
304                 return get_one_mx_host_ip(IO);
305         }
306 }
307
308
309 void SetConnectStatus(AsyncIO *IO)
310 {
311         SmtpOutMsg *Msg = IO->Data;
312         char buf[256];
313         void *src;
314
315         buf[0] = '\0';
316
317         if (IO->ConnectMe->IPv6) {
318                 src = &IO->ConnectMe->Addr.sin6_addr;
319         }
320         else {
321                 struct sockaddr_in *addr;
322
323                 addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
324                 src = &addr->sin_addr.s_addr;
325         }
326
327         inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
328                   src,
329                   buf,
330                   sizeof(buf));
331
332         if (Msg->mx_host == NULL)
333                 Msg->mx_host = "<no MX-Record>";
334
335         EVS_syslog(LOG_INFO,
336                   "connecting to %s [%s]:%d ...\n",
337                   Msg->mx_host,
338                   buf,
339                   Msg->IO.ConnectMe->Port);
340
341         Msg->MyQEntry->Status = 4;
342         StrBufPrintf(Msg->MyQEntry->StatusMessage,
343                      "Timeout while connecting %s [%s]:%d ",
344                      Msg->mx_host,
345                      buf,
346                      Msg->IO.ConnectMe->Port);
347         Msg->IO.NextState = eConnect;
348 }
349
350 /*****************************************************************************
351  * So we connect our Relay IP here.                                          *
352  *****************************************************************************/
353 eNextState mx_connect_ip(AsyncIO *IO)
354 {
355         SmtpOutMsg *Msg = IO->Data;
356         SetSMTPState(IO, eSTMPconnecting);
357
358         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
359
360         IO->ConnectMe = Msg->pCurrRelay;
361         Msg->State = eConnectMX;
362
363         SetConnectStatus(IO);
364
365         return EvConnectSock(IO,
366                              SMTP_C_ConnTimeout,
367                              SMTP_C_ReadTimeouts[0],
368                              1);
369 }
370
371 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
372 {
373         SmtpOutMsg *Msg = IO->Data;
374         struct hostent *hostent;
375
376         IO->ConnectMe = Msg->pCurrRelay;
377
378         QueryCbDone(IO);
379         EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
380                    __FUNCTION__,
381                    IO->Now - IO->DNS.Start);
382
383         hostent = Msg->HostLookup.VParsedDNSReply;
384         if ((Msg->HostLookup.DNSStatus == ARES_SUCCESS) &&
385             (hostent != NULL) ) {
386                 memset(&Msg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
387                 if (Msg->pCurrRelay->IPv6) {
388                         memcpy(&Msg->pCurrRelay->Addr.sin6_addr.s6_addr,
389                                &hostent->h_addr_list[0],
390                                sizeof(struct in6_addr));
391
392                         Msg->pCurrRelay->Addr.sin6_family =
393                                 hostent->h_addrtype;
394                         Msg->pCurrRelay->Addr.sin6_port =
395                                 htons(Msg->IO.ConnectMe->Port);
396                 }
397                 else {
398                         struct sockaddr_in *addr;
399                         /*
400                          * Bypass the ns lookup result like this:
401                          * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
402                          * addr->sin_addr.s_addr =
403                          *   htonl((uint32_t)&hostent->h_addr_list[0]);
404                          */
405
406                         addr = (struct sockaddr_in*) &Msg->pCurrRelay->Addr;
407
408                         memcpy(&addr->sin_addr.s_addr,
409                                hostent->h_addr_list[0],
410                                sizeof(uint32_t));
411
412                         addr->sin_family = hostent->h_addrtype;
413                         addr->sin_port   = htons(Msg->IO.ConnectMe->Port);
414                 }
415                 Msg->mx_host = Msg->pCurrRelay->Host;
416                 if (Msg->HostLookup.VParsedDNSReply != NULL) {
417                         Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
418                         Msg->HostLookup.VParsedDNSReply = NULL;
419                 }
420                 return mx_connect_ip(IO);
421         }
422         else {
423                 SetSMTPState(IO, eSTMPfailOne);
424                 if (Msg->HostLookup.VParsedDNSReply != NULL) {
425                         Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
426                         Msg->HostLookup.VParsedDNSReply = NULL;
427                 }
428                 return FailOneAttempt(IO);
429         }
430 }
431
432 eNextState get_one_mx_host_ip(AsyncIO *IO)
433 {
434         SmtpOutMsg * Msg = IO->Data;
435         /*
436          * here we start with the lookup of one host. it might be...
437          * - the relay host *sigh*
438          * - the direct hostname if there was no mx record
439          * - one of the mx'es
440          */
441         SetSMTPState(IO, (Msg->pCurrRelay->IPv6)?eSTMPalookup:eSTMPaaaalookup);
442
443         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
444
445         EVS_syslog(LOG_DEBUG,
446                   "looking up %s-Record %s : %d ...\n",
447                   (Msg->pCurrRelay->IPv6)? "aaaa": "a",
448                   Msg->pCurrRelay->Host,
449                   Msg->pCurrRelay->Port);
450
451         if (!QueueQuery((Msg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
452                         Msg->pCurrRelay->Host,
453                         &Msg->IO,
454                         &Msg->HostLookup,
455                         get_one_mx_host_ip_done))
456         {
457                 Msg->MyQEntry->Status = 5;
458                 StrBufPrintf(Msg->MyQEntry->StatusMessage,
459                              "No MX hosts found for <%s>", Msg->node);
460                 Msg->IO.NextState = eTerminateConnection;
461                 return IO->NextState;
462         }
463         IO->NextState = eReadDNSReply;
464         return IO->NextState;
465 }
466
467
468 /*****************************************************************************
469  * here we try to find out about the MX records for our recipients.          *
470  *****************************************************************************/
471 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
472 {
473         SmtpOutMsg * Msg = IO->Data;
474         ParsedURL **pp;
475
476         QueryCbDone(IO);
477
478         EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
479                    __FUNCTION__,
480                    IO->Now - IO->DNS.Start);
481
482         pp = &Msg->Relay;
483         while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
484                 pp = &(*pp)->Next;
485
486         if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
487             (IO->DNS.Query->VParsedDNSReply != NULL))
488         { /* ok, we found mx records. */
489
490                 Msg->CurrMX
491                         = Msg->AllMX
492                         = IO->DNS.Query->VParsedDNSReply;
493                 while (Msg->CurrMX) {
494                         int i;
495                         for (i = 0; i < 2; i++) {
496                                 ParsedURL *p;
497
498                                 p = (ParsedURL*) malloc(sizeof(ParsedURL));
499                                 memset(p, 0, sizeof(ParsedURL));
500                                 p->Priority = Msg->CurrMX->priority;
501                                 p->IsIP = 0;
502                                 p->Port = DefaultMXPort;
503                                 p->IPv6 = i == 1;
504                                 p->Host = Msg->CurrMX->host;
505                                 if (*pp == NULL)
506                                         *pp = p;
507                                 else {
508                                         ParsedURL *ppp = *pp;
509
510                                         while ((ppp->Next != NULL) &&
511                                                (ppp->Next->Priority <= p->Priority))
512                                                ppp = ppp->Next;
513                                         if ((ppp == *pp) &&
514                                             (ppp->Priority > p->Priority)) {
515                                                 p->Next = *pp;
516                                                 *pp = p;
517                                         }
518                                         else {
519                                                 p->Next = ppp->Next;
520                                                 ppp->Next = p;
521                                         }
522                                 }
523                         }
524                         Msg->CurrMX    = Msg->CurrMX->next;
525                 }
526                 Msg->CXFlags   = Msg->CXFlags & F_HAVE_MX;
527         }
528         else { /* else fall back to the plain hostname */
529                 int i;
530                 for (i = 0; i < 2; i++) {
531                         ParsedURL *p;
532
533                         p = (ParsedURL*) malloc(sizeof(ParsedURL));
534                         memset(p, 0, sizeof(ParsedURL));
535                         p->IsIP = 0;
536                         p->Port = DefaultMXPort;
537                         p->IPv6 = i == 1;
538                         p->Host = Msg->node;
539
540                         *pp = p;
541                         pp = &p->Next;
542                 }
543                 Msg->CXFlags   = Msg->CXFlags & F_DIRECT;
544         }
545         if (Msg->MyQItem->FallBackHost != NULL)
546         {
547                 Msg->MyQItem->FallBackHost->Next = *pp;
548                 *pp = Msg->MyQItem->FallBackHost;
549         }
550         Msg->pCurrRelay = Msg->Relay;
551         return get_one_mx_host_ip(IO);
552 }
553
554 eNextState resolve_mx_records(AsyncIO *IO)
555 {
556         SmtpOutMsg * Msg = IO->Data;
557
558         SetSMTPState(IO, eSTMPmxlookup);
559
560         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
561         /* start resolving MX records here. */
562         if (!QueueQuery(ns_t_mx,
563                         Msg->node,
564                         &Msg->IO,
565                         &Msg->MxLookup,
566                         smtp_resolve_mx_record_done))
567         {
568                 Msg->MyQEntry->Status = 5;
569                 StrBufPrintf(Msg->MyQEntry->StatusMessage,
570                              "No MX hosts found for <%s>", Msg->node);
571                 return IO->NextState;
572         }
573         Msg->IO.NextState = eReadDNSReply;
574         return IO->NextState;
575 }
576
577
578
579 /******************************************************************************
580  *  so, we're going to start a SMTP delivery.  lets get it on.                *
581  ******************************************************************************/
582
583 SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
584                             MailQEntry *MyQEntry,
585                             int MsgCount)
586 {
587         SmtpOutMsg * Msg;
588
589         Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
590         if (Msg == NULL)
591                 return NULL;
592         memset(Msg, 0, sizeof(SmtpOutMsg));
593
594         Msg->n                = MsgCount;
595         Msg->MyQEntry         = MyQEntry;
596         Msg->MyQItem          = MyQItem;
597         Msg->pCurrRelay       = MyQItem->URL;
598
599         InitIOStruct(&Msg->IO,
600                      Msg,
601                      eReadMessage,
602                      SMTP_C_ReadServerStatus,
603                      SMTP_C_DNSFail,
604                      SMTP_C_DispatchWriteDone,
605                      SMTP_C_DispatchReadDone,
606                      SMTP_C_Terminate,
607                      SMTP_C_TerminateDB,
608                      SMTP_C_ConnFail,
609                      SMTP_C_Timeout,
610                      SMTP_C_Shutdown);
611
612         Msg->IO.ErrMsg = Msg->MyQEntry->StatusMessage;
613
614         return Msg;
615 }
616
617 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
618                               MailQEntry *MyQEntry,
619                               StrBuf *MsgText,
620                         /*KeepMsgText allows us to use MsgText as ours.*/
621                               int KeepMsgText,
622                               int MsgCount)
623 {
624         SmtpOutMsg *Msg;
625
626         SMTPC_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
627
628         Msg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
629         if (Msg == NULL) {
630                 SMTPC_syslog(LOG_DEBUG, "%s Failed to alocate message context.\n", __FUNCTION__);
631                 if (KeepMsgText) 
632                         FreeStrBuf (&MsgText);
633                 return;
634         }
635         if (KeepMsgText) Msg->msgtext = MsgText;
636         else             Msg->msgtext = NewStrBufDup(MsgText);
637
638         if (smtp_resolve_recipients(Msg) &&
639             (!MyQItem->HaveRelay ||
640              (MyQItem->URL != NULL)))
641         {
642                 safestrncpy(
643                         ((CitContext *)Msg->IO.CitContext)->cs_host,
644                         Msg->node,
645                         sizeof(((CitContext *)
646                                 Msg->IO.CitContext)->cs_host));
647
648                 SMTPC_syslog(LOG_DEBUG, "Starting: [%ld] <%s> CC <%d> \n",
649                              Msg->MyQItem->MessageID,
650                              ChrPtr(Msg->MyQEntry->Recipient),
651                              ((CitContext*)Msg->IO.CitContext)->cs_pid);
652                 if (Msg->pCurrRelay == NULL) {
653                         SetSMTPState(&Msg->IO, eSTMPmxlookup);
654                         QueueEventContext(&Msg->IO,
655                                           resolve_mx_records);
656                 }
657                 else { /* oh... via relay host */
658                         if (Msg->pCurrRelay->IsIP) {
659                                 SetSMTPState(&Msg->IO, eSTMPconnecting);
660                                 QueueEventContext(&Msg->IO,
661                                                   mx_connect_ip);
662                         }
663                         else {
664                                 SetSMTPState(&Msg->IO, eSTMPalookup);
665                                 /* uneducated admin has chosen to
666                                    add DNS to the equation... */
667                                 QueueEventContext(&Msg->IO,
668                                                   get_one_mx_host_ip);
669                         }
670                 }
671         }
672         else {
673                 SetSMTPState(&Msg->IO, eSMTPFailTotal);
674                 /* No recipients? well fail then. */
675                 if (Msg->MyQEntry != NULL) {
676                         Msg->MyQEntry->Status = 5;
677                         if (StrLength(Msg->MyQEntry->StatusMessage) == 0)
678                                 StrBufPlain(Msg->MyQEntry->StatusMessage,
679                                             HKEY("Invalid Recipient!"));
680                 }
681                 FinalizeMessageSend_DB(&Msg->IO);
682                 DeleteSmtpOutMsg(Msg);
683         }
684 }
685
686
687
688
689
690
691 /*****************************************************************************/
692 /*                     SMTP CLIENT DISPATCHER                                */
693 /*****************************************************************************/
694
695 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *Msg)
696 {
697         double Timeout = 0.0;
698         AsyncIO *IO = &Msg->IO;
699
700         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
701
702         switch (NextTCPState) {
703         case eSendFile:
704         case eSendReply:
705         case eSendMore:
706                 Timeout = SMTP_C_SendTimeouts[Msg->State];
707                 if (Msg->State == eDATABody) {
708                         /* if we're sending a huge message,
709                          * we need more time.
710                          */
711                         Timeout += StrLength(Msg->msgtext) / 512;
712                 }
713                 break;
714         case eReadMessage:
715                 Timeout = SMTP_C_ReadTimeouts[Msg->State];
716                 if (Msg->State == eDATATerminateBody) {
717                         /*
718                          * some mailservers take a nap before accepting
719                          * the message content inspection and such.
720                          */
721                         Timeout += StrLength(Msg->msgtext) / 512;
722                 }
723                 break;
724         case eSendDNSQuery:
725         case eReadDNSReply:
726         case eDBQuery:
727         case eReadFile:
728         case eReadMore:
729         case eReadPayload:
730         case eConnect:
731         case eTerminateConnection:
732         case eAbort:
733                 return;
734         }
735         SetNextTimeout(&Msg->IO, Timeout);
736 }
737 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
738 {
739         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
740         SmtpOutMsg *Msg = IO->Data;
741         eNextState rc;
742
743         rc = ReadHandlers[Msg->State](Msg);
744         if (rc != eAbort)
745         {
746                 Msg->State++;
747                 SMTPSetTimeout(rc, Msg);
748         }
749         return rc;
750 }
751 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
752 {
753         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
754         SmtpOutMsg *Msg = IO->Data;
755         eNextState rc;
756
757         rc = SendHandlers[Msg->State](Msg);
758         SMTPSetTimeout(rc, Msg);
759         return rc;
760 }
761
762
763 /*****************************************************************************/
764 /*                     SMTP CLIENT ERROR CATCHERS                            */
765 /*****************************************************************************/
766 eNextState SMTP_C_Terminate(AsyncIO *IO)
767 {
768         SmtpOutMsg *Msg = IO->Data;
769
770         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
771         return FinalizeMessageSend(Msg);
772 }
773 eNextState SMTP_C_TerminateDB(AsyncIO *IO)
774 {
775         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
776         return Terminate(IO);
777 }
778 eNextState SMTP_C_Timeout(AsyncIO *IO)
779 {
780         SmtpOutMsg *Msg = IO->Data;
781
782         Msg->MyQEntry->Status = 4;
783         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
784         StrBufPrintf(IO->ErrMsg, "Timeout: %s while talking to %s",
785                      ReadErrors[Msg->State].Key,
786                      Msg->mx_host);
787         if (Msg->State > eRCPT)
788                 return eAbort;
789         else
790                 return FailOneAttempt(IO);
791 }
792 eNextState SMTP_C_ConnFail(AsyncIO *IO)
793 {
794         SmtpOutMsg *Msg = IO->Data;
795
796         Msg->MyQEntry->Status = 4;
797         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
798         StrBufPrintf(IO->ErrMsg, "Connection failure: %s while talking to %s",
799                      ReadErrors[Msg->State].Key,
800                      Msg->mx_host);
801
802         return FailOneAttempt(IO);
803 }
804 eNextState SMTP_C_DNSFail(AsyncIO *IO)
805 {
806         SmtpOutMsg *Msg = IO->Data;
807         Msg->MyQEntry->Status = 4;
808         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
809         return FailOneAttempt(IO);
810 }
811 eNextState SMTP_C_Shutdown(AsyncIO *IO)
812 {
813         EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
814         SmtpOutMsg *Msg = IO->Data;
815
816         switch (IO->NextState) {
817         case eSendDNSQuery:
818         case eReadDNSReply:
819
820                 /* todo: abort c-ares */
821         case eConnect:
822         case eSendReply:
823         case eSendMore:
824         case eSendFile:
825         case eReadMessage:
826         case eReadMore:
827         case eReadPayload:
828         case eReadFile:
829                 StopClientWatchers(IO, 1);
830                 break;
831         case eDBQuery:
832
833                 break;
834         case eTerminateConnection:
835         case eAbort:
836                 break;
837         }
838         Msg->MyQEntry->Status = 3;
839         StrBufPlain(Msg->MyQEntry->StatusMessage,
840                     HKEY("server shutdown during message submit."));
841         return FinalizeMessageSend(Msg);
842 }
843
844
845 /**
846  * @brief lineread Handler;
847  * understands when to read more SMTP lines, and when this is a one-lined reply.
848  */
849 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
850 {
851         eReadState Finished = eBufferNotEmpty;
852
853         while (Finished == eBufferNotEmpty) {
854                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
855
856                 switch (Finished) {
857                 case eMustReadMore: /// read new from socket...
858                         return Finished;
859                         break;
860                 case eBufferNotEmpty: /* shouldn't happen... */
861                 case eReadSuccess: /// done for now...
862                         if (StrLength(IO->IOBuf) < 4)
863                                 continue;
864                         if (ChrPtr(IO->IOBuf)[3] == '-')
865                                 Finished = eBufferNotEmpty;
866                         else
867                                 return Finished;
868                         break;
869                 case eReadFail: /// WHUT?
870                         ///todo: shut down!
871                         break;
872                 }
873         }
874         return Finished;
875 }
876
877 void LogDebugEnableSMTPClient(const int n)
878 {
879         SMTPClientDebugEnabled = n;
880 }
881
882 CTDL_MODULE_INIT(smtp_eventclient)
883 {
884         if (!threading)
885                 CtdlRegisterDebugFlagHook(HKEY("smtpeventclient"), LogDebugEnableSMTPClient, &SMTPClientDebugEnabled);
886         return "smtpeventclient";
887 }