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