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