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