5d53110cbf0cd9e4b8ac8012c4b2056d58fe136b
[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-2009 by the citadel.org team
24  *
25  *  This program is free software; you can redistribute it and/or modify
26  *  it under the terms of the GNU General Public License as published by
27  *  the Free Software Foundation; either version 3 of the License, or
28  *  (at your option) any later version.
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  *  You should have received a copy of the GNU General Public License
36  *  along with this program; if not, write to the Free Software
37  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
92 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
93 /*****************************************************************************/
94 /*               SMTP CLIENT (OUTBOUND PROCESSING) STUFF                     */
95 /*****************************************************************************/
96
97 typedef enum _eSMTP_C_States {
98         eConnect, 
99         eEHLO,
100         eHELO,
101         eSMTPAuth,
102         eFROM,
103         eRCPT,
104         eDATA,
105         eDATABody,
106         eDATATerminateBody,
107         eQUIT,
108         eMaxSMTPC
109 } eSMTP_C_States;
110
111 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
112 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
113         300., /* Greeting... */
114         30., /* EHLO */
115         30., /* HELO */
116         30., /* Auth */
117         30., /* From */
118         90., /* RCPT */
119         30., /* DATA */
120         90., /* DATABody */
121         90., /* end of body... */
122         30.  /* QUIT */
123 };
124 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
125         90., /* Greeting... */
126         30., /* EHLO */
127         30., /* HELO */
128         30., /* Auth */
129         30., /* From */
130         30., /* RCPT */
131         30., /* DATA */
132         90., /* DATABody */
133         900., /* end of body... */
134         30.  /* QUIT */
135 };
136
137 static const ConstStr ReadErrors[eMaxSMTPC] = {
138         {HKEY("Connection broken during SMTP conversation")},
139         {HKEY("Connection broken during SMTP EHLO")},
140         {HKEY("Connection broken during SMTP HELO")},
141         {HKEY("Connection broken during SMTP AUTH")},
142         {HKEY("Connection broken during SMTP MAIL FROM")},
143         {HKEY("Connection broken during SMTP RCPT")},
144         {HKEY("Connection broken during SMTP DATA")},
145         {HKEY("Connection broken during SMTP message transmit")},
146         {HKEY("")}/* quit reply, don't care. */
147 };
148
149
150 typedef struct _stmp_out_msg {
151         MailQEntry *MyQEntry;
152         OneQueItem *MyQItem;
153         long n;
154         AsyncIO IO;
155
156         eSMTP_C_States State;
157
158         struct ares_mx_reply *AllMX;
159         struct ares_mx_reply *CurrMX;
160         const char *mx_port;
161         const char *mx_host;
162
163         DNSQueryParts MxLookup;
164         DNSQueryParts HostLookup;
165         struct hostent *OneMX;
166 ///     struct hostent *HEnt;
167
168         ParsedURL *Relay;
169         ParsedURL *pCurrRelay;
170         StrBuf *msgtext;
171         char *envelope_from;
172         char user[1024];
173         char node[1024];
174         char name[1024];
175         char mailfrom[1024];
176 } SmtpOutMsg;
177
178 void DeleteSmtpOutMsg(void *v)
179 {
180         SmtpOutMsg *Msg = v;
181
182         ares_free_data(Msg->AllMX);
183         if (Msg->HostLookup.VParsedDNSReply != NULL)
184                 Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
185         FreeStrBuf(&Msg->msgtext);
186         FreeAsyncIOContents(&Msg->IO);
187         free(Msg);
188 }
189
190 eNextState SMTP_C_Shutdown(AsyncIO *IO);
191 eNextState SMTP_C_Timeout(AsyncIO *IO);
192 eNextState SMTP_C_ConnFail(AsyncIO *IO);
193 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
194 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
195 eNextState SMTP_C_Terminate(AsyncIO *IO);
196 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
197
198 typedef eNextState (*SMTPReadHandler)(SmtpOutMsg *Msg);
199 typedef eNextState (*SMTPSendHandler)(SmtpOutMsg *Msg);
200
201
202 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {\
203                 SendMsg->MyQEntry->Status = WHICH_ERR; \
204                 StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); \
205                 return eAbort; } \
206         while (0)
207
208 #define SMTP_VERROR(WHICH_ERR) do {\
209                 SendMsg->MyQEntry->Status = WHICH_ERR; \
210                 StrBufPlain(SendMsg->MyQEntry->StatusMessage, \
211                             ChrPtr(SendMsg->IO.IOBuf) + 4, \
212                             StrLength(SendMsg->IO.IOBuf) - 4); \
213                 return eAbort; } \
214         while (0)
215
216 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
217
218 #define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.SendBuf.Buf))
219 #define SMTP_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
220
221
222 void FinalizeMessageSend(SmtpOutMsg *Msg)
223 {
224         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
225         
226         if (DecreaseQReference(Msg->MyQItem)) 
227         {
228                 int nRemain;
229                 StrBuf *MsgData;
230
231                 nRemain = CountActiveQueueEntries(Msg->MyQItem);
232
233                 MsgData = SerializeQueueItem(Msg->MyQItem);
234                 /*
235                  * Uncompleted delivery instructions remain, so delete the old
236                  * instructions and replace with the updated ones.
237                  */
238                 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
239                 smtpq_do_bounce(Msg->MyQItem,
240                                Msg->msgtext); 
241                 if (nRemain > 0) {
242                         struct CtdlMessage *msg;
243                         msg = malloc(sizeof(struct CtdlMessage));
244                         memset(msg, 0, sizeof(struct CtdlMessage));
245                         msg->cm_magic = CTDLMESSAGE_MAGIC;
246                         msg->cm_anon_type = MES_NORMAL;
247                         msg->cm_format_type = FMT_RFC822;
248                         msg->cm_fields['M'] = SmashStrBuf(&MsgData);
249                         CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
250                         CtdlFreeMessage(msg);
251                 }
252                 else {
253                         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->MessageID, 1, "");
254                         FreeStrBuf(&MsgData);
255                 }
256
257                 RemoveQItem(Msg->MyQItem);
258         }
259         DeleteSmtpOutMsg(Msg);
260 }
261
262
263 void SetConnectStatus(AsyncIO *IO)
264 {
265         
266         SmtpOutMsg *SendMsg = IO->Data;
267         char buf[256];
268         void *src;
269
270         buf[0] = '\0';
271
272         if (IO->IP6) {
273                 src = &IO->Addr.sin6_addr;
274         }
275         else {
276                 struct sockaddr_in *addr = (struct sockaddr_in *)&IO->Addr;
277
278                 src = &addr->sin_addr.s_addr;
279         }
280
281         inet_ntop((IO->IP6)?AF_INET6:AF_INET,
282                   src,
283                   buf, sizeof(buf));
284         if (SendMsg->mx_host == NULL)
285                 SendMsg->mx_host = "<no name>";
286
287         CtdlLogPrintf(CTDL_DEBUG, 
288                       "SMTP client[%ld]: connecting to %s [%s]:%d ...\n", 
289                       SendMsg->n, 
290                       SendMsg->mx_host, 
291                       buf,
292                       SendMsg->IO.dport);
293
294         SendMsg->MyQEntry->Status = 5; 
295         StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
296                      "Timeout while connecting %s [%s]:%d ", 
297                      SendMsg->mx_host,
298                      buf,
299                      SendMsg->IO.dport);
300 }
301
302 eNextState mx_connect_relay_ip(AsyncIO *IO)
303 {
304         
305         SmtpOutMsg *SendMsg = IO->Data;
306
307         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
308
309         IO->IP6 = SendMsg->pCurrRelay->af == AF_INET6;
310         
311         if (SendMsg->pCurrRelay->Port != 0)
312                 IO->dport = SendMsg->pCurrRelay->Port;
313
314         memset(&IO->Addr, 0, sizeof(struct sockaddr_in6));
315         if (IO->IP6) {
316                 memcpy(&IO->Addr.sin6_addr.s6_addr, 
317                        &SendMsg->pCurrRelay->Addr,
318                        sizeof(struct in6_addr));
319
320                 IO->Addr.sin6_family = AF_INET6;
321                 IO->Addr.sin6_port = htons(IO->dport);
322         }
323         else {
324                 struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
325                 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
326                 memcpy(&addr->sin_addr,///.s_addr, 
327                        &SendMsg->pCurrRelay->Addr,
328                        sizeof(struct in_addr));
329                 
330                 addr->sin_family = AF_INET;
331                 addr->sin_port = htons(IO->dport);
332         }
333
334         SetConnectStatus(IO);
335
336         return InitEventIO(IO, SendMsg, 
337                            SMTP_C_ConnTimeout, 
338                            SMTP_C_ReadTimeouts[0],
339                             1);
340 }
341
342 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
343 {
344         SmtpOutMsg *SendMsg = IO->Data;
345         struct hostent *hostent;
346
347         QueryCbDone(IO);
348
349         hostent = SendMsg->HostLookup.VParsedDNSReply;
350         if ((SendMsg->HostLookup.DNSStatus == ARES_SUCCESS) && 
351             (hostent != NULL) ) {
352                 
353                 IO->IP6  = hostent->h_addrtype == AF_INET6;
354                 ////IO->HEnt = hostent;
355
356                 memset(&IO->Addr, 0, sizeof(struct in6_addr));
357                 if (IO->IP6) {
358                         memcpy(&IO->Addr.sin6_addr.s6_addr, 
359                                &hostent->h_addr_list[0],
360                                sizeof(struct in6_addr));
361                         
362                         IO->Addr.sin6_family = hostent->h_addrtype;
363                         IO->Addr.sin6_port = htons(IO->dport);
364                 }
365                 else {
366                         struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
367                         /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
368 //                      addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
369                         memcpy(&addr->sin_addr.s_addr, hostent->h_addr_list[0], sizeof(uint32_t));
370                         
371                         addr->sin_family = hostent->h_addrtype;
372                         addr->sin_port = htons(IO->dport);
373                         
374                 }
375                 ////SendMsg->IO.HEnt = hostent;
376                 SetConnectStatus(IO);
377                 return InitEventIO(IO, SendMsg, 
378                                    SMTP_C_ConnTimeout, 
379                                    SMTP_C_ReadTimeouts[0],
380                                    1);
381         }
382         else
383                 return SMTP_C_Terminate(IO);
384 }
385
386 const unsigned short DefaultMXPort = 25;
387 eNextState get_one_mx_host_ip(AsyncIO *IO)
388 {
389         SmtpOutMsg * SendMsg = IO->Data;
390         const char *Hostname;
391         //char *endpart;
392         //char buf[SIZ];
393         InitC_ares_dns(IO);
394
395         if (SendMsg->CurrMX) {
396                 SendMsg->mx_host = SendMsg->CurrMX->host;
397                 SendMsg->CurrMX = SendMsg->CurrMX->next;
398         }
399
400         if (SendMsg->pCurrRelay != NULL) {
401                 SendMsg->mx_host = Hostname = SendMsg->pCurrRelay->Host;
402                 if (SendMsg->pCurrRelay->Port != 0)
403                         SendMsg->IO.dport = SendMsg->pCurrRelay->Port;
404         }
405         else if (SendMsg->mx_host != NULL) Hostname = SendMsg->mx_host;
406         else Hostname = SendMsg->node;
407
408         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
409
410         CtdlLogPrintf(CTDL_DEBUG, 
411                       "SMTP client[%ld]: looking up %s : %d ...\n", 
412                       SendMsg->n, 
413                       Hostname, 
414                       SendMsg->IO.dport);
415
416         if (!QueueQuery(ns_t_a, 
417                         Hostname, 
418                         &SendMsg->IO, 
419                         &SendMsg->HostLookup, 
420                         get_one_mx_host_ip_done))
421         {
422                 SendMsg->MyQEntry->Status = 5;
423                 StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
424                              "No MX hosts found for <%s>", SendMsg->node);
425                 return IO->NextState;
426         }
427         return IO->NextState;
428 }
429
430
431 eNextState smtp_resolve_mx_done(AsyncIO *IO)
432 {
433         SmtpOutMsg * SendMsg = IO->Data;
434
435         QueryCbDone(IO);
436
437         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
438
439         SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
440
441         SendMsg->CurrMX = SendMsg->AllMX = IO->DNSQuery->VParsedDNSReply;
442         //// TODO: should we remove the current ares context???
443         get_one_mx_host_ip(IO);
444         return IO->NextState;
445 }
446
447
448 eNextState resolve_mx_records(AsyncIO *IO)
449 {
450         SmtpOutMsg * SendMsg = IO->Data;
451
452         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
453
454         if (!QueueQuery(ns_t_mx, 
455                         SendMsg->node, 
456                         &SendMsg->IO, 
457                         &SendMsg->MxLookup, 
458                         smtp_resolve_mx_done))
459         {
460                 SendMsg->MyQEntry->Status = 5;
461                 StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
462                              "No MX hosts found for <%s>", SendMsg->node);
463                 return IO->NextState;
464         }
465         return eAbort;
466 }
467
468
469 int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
470 {
471         const char *ptr;
472         char buf[1024];
473         int scan_done;
474         int lp, rp;
475         int i;
476
477         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
478
479         if ((SendMsg==NULL) || 
480             (SendMsg->MyQEntry == NULL) || 
481             (StrLength(SendMsg->MyQEntry->Recipient) == 0)) {
482                 return 0;
483         }
484
485         /* Parse out the host portion of the recipient address */
486         process_rfc822_addr(ChrPtr(SendMsg->MyQEntry->Recipient), 
487                             SendMsg->user, 
488                             SendMsg->node, 
489                             SendMsg->name);
490
491         CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: Attempting delivery to <%s> @ <%s> (%s)\n",
492                       SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
493         /* If no envelope_from is supplied, extract one from the message */
494         if ( (SendMsg->envelope_from == NULL) || 
495              (IsEmptyStr(SendMsg->envelope_from)) ) {
496                 SendMsg->mailfrom[0] = '\0';
497                 scan_done = 0;
498                 ptr = ChrPtr(SendMsg->msgtext);
499                 do {
500                         if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
501                                 scan_done = 1;
502                         }
503                         if (!strncasecmp(buf, "From:", 5)) {
504                                 safestrncpy(SendMsg->mailfrom, &buf[5], sizeof SendMsg->mailfrom);
505                                 striplt(SendMsg->mailfrom);
506                                 for (i=0; SendMsg->mailfrom[i]; ++i) {
507                                         if (!isprint(SendMsg->mailfrom[i])) {
508                                                 strcpy(&SendMsg->mailfrom[i], &SendMsg->mailfrom[i+1]);
509                                                 i=0;
510                                         }
511                                 }
512         
513                                 /* Strip out parenthesized names */
514                                 lp = (-1);
515                                 rp = (-1);
516                                 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
517                                         if (SendMsg->mailfrom[i] == '(') lp = i;
518                                         if (SendMsg->mailfrom[i] == ')') rp = i;
519                                 }
520                                 if ((lp>0)&&(rp>lp)) {
521                                         strcpy(&SendMsg->mailfrom[lp-1], &SendMsg->mailfrom[rp+1]);
522                                 }
523         
524                                 /* Prefer brokketized names */
525                                 lp = (-1);
526                                 rp = (-1);
527                                 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
528                                         if (SendMsg->mailfrom[i] == '<') lp = i;
529                                         if (SendMsg->mailfrom[i] == '>') rp = i;
530                                 }
531                                 if ( (lp>=0) && (rp>lp) ) {
532                                         SendMsg->mailfrom[rp] = 0;
533                                         memmove(SendMsg->mailfrom, 
534                                                 &SendMsg->mailfrom[lp + 1], 
535                                                 rp - lp);
536                                 }
537         
538                                 scan_done = 1;
539                         }
540                 } while (scan_done == 0);
541                 if (IsEmptyStr(SendMsg->mailfrom)) strcpy(SendMsg->mailfrom, "someone@somewhere.org");
542                 stripallbut(SendMsg->mailfrom, '<', '>');
543                 SendMsg->envelope_from = SendMsg->mailfrom;
544         }
545
546         return 1;
547 }
548
549
550
551 void smtp_try(OneQueItem *MyQItem, 
552               MailQEntry *MyQEntry, 
553               StrBuf *MsgText, 
554               int KeepMsgText,  /* KeepMsgText allows us to use MsgText as ours. */
555               int MsgCount)
556 {
557         SmtpOutMsg * SendMsg;
558
559         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
560
561         SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
562         memset(SendMsg, 0, sizeof(SmtpOutMsg));
563         SendMsg->IO.sock      = (-1);
564         SendMsg->IO.NextState = eReadMessage;
565         SendMsg->n            = MsgCount++;
566         SendMsg->MyQEntry     = MyQEntry;
567         SendMsg->MyQItem      = MyQItem;
568         SendMsg->pCurrRelay   = MyQItem->URL;
569
570         SendMsg->IO.dport         = DefaultMXPort;
571         SendMsg->IO.Data          = SendMsg;
572         SendMsg->IO.SendDone      = SMTP_C_DispatchWriteDone;
573         SendMsg->IO.ReadDone      = SMTP_C_DispatchReadDone;
574         SendMsg->IO.Terminate     = SMTP_C_Terminate;
575         SendMsg->IO.LineReader    = SMTP_C_ReadServerStatus;
576         SendMsg->IO.ConnFail      = SMTP_C_ConnFail;
577         SendMsg->IO.Timeout       = SMTP_C_Timeout;
578         SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
579         SendMsg->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
580         SendMsg->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
581         SendMsg->IO.IOBuf         = NewStrBuf();
582                         
583
584         if (KeepMsgText) {
585                 SendMsg->msgtext    = MsgText;
586         }
587         else {
588                 SendMsg->msgtext = NewStrBufDup(MsgText);
589         }
590
591         if (smtp_resolve_recipients(SendMsg)) {
592                 if (SendMsg->pCurrRelay == NULL)
593                         QueueEventContext(&SendMsg->IO,
594                                           resolve_mx_records);
595                 else {
596                         if (SendMsg->pCurrRelay->IsIP) {
597                                 QueueEventContext(&SendMsg->IO,
598                                                   mx_connect_relay_ip);
599                         }
600                         else {
601                                 QueueEventContext(&SendMsg->IO,
602                                                   get_one_mx_host_ip);
603                         }
604                 }
605         }
606         else {
607                 if ((SendMsg==NULL) || 
608                     (SendMsg->MyQEntry == NULL)) {
609                         SendMsg->MyQEntry->Status = 5;
610                         StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
611                                     HKEY("Invalid Recipient!"));
612                 }
613                 FinalizeMessageSend(SendMsg);
614         }
615 }
616
617
618
619
620
621 /*****************************************************************************/
622 /*                     SMTP CLIENT STATE CALLBACKS                           */
623 /*****************************************************************************/
624 eNextState SMTPC_read_greeting(SmtpOutMsg *SendMsg)
625 {
626         /* Process the SMTP greeting from the server */
627         SMTP_DBG_READ();
628
629         if (!SMTP_IS_STATE('2')) {
630                 if (SMTP_IS_STATE('4')) 
631                         SMTP_VERROR(4);
632                 else 
633                         SMTP_VERROR(5);
634         }
635         return eSendReply;
636 }
637
638 eNextState SMTPC_send_EHLO(SmtpOutMsg *SendMsg)
639 {
640         /* At this point we know we are talking to a real SMTP server */
641
642         /* Do a EHLO command.  If it fails, try the HELO command. */
643         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
644                      "EHLO %s\r\n", config.c_fqdn);
645
646         SMTP_DBG_SEND();
647         return eReadMessage;
648 }
649
650 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *SendMsg)
651 {
652         SMTP_DBG_READ();
653
654         if (SMTP_IS_STATE('2')) {
655                 SendMsg->State ++;
656
657                 if ((SendMsg->pCurrRelay == NULL) || 
658                     (SendMsg->pCurrRelay->User == NULL))
659                         SendMsg->State ++; /* Skip auth... */
660         }
661         /* else we fall back to 'helo' */
662         return eSendReply;
663 }
664
665 eNextState STMPC_send_HELO(SmtpOutMsg *SendMsg)
666 {
667         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
668                      "HELO %s\r\n", config.c_fqdn);
669
670         SMTP_DBG_SEND();
671         return eReadMessage;
672 }
673
674 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *SendMsg)
675 {
676         SMTP_DBG_READ();
677
678         if (!SMTP_IS_STATE('2')) {
679                 if (SMTP_IS_STATE('4'))
680                         SMTP_VERROR(4);
681                 else 
682                         SMTP_VERROR(5);
683         }
684                 if ((SendMsg->pCurrRelay == NULL) || 
685                     (SendMsg->pCurrRelay->User == NULL))
686                 SendMsg->State ++; /* Skip auth... */
687         return eSendReply;
688 }
689
690 eNextState SMTPC_send_auth(SmtpOutMsg *SendMsg)
691 {
692         char buf[SIZ];
693         char encoded[1024];
694
695         if ((SendMsg->pCurrRelay == NULL) || 
696             (SendMsg->pCurrRelay->User == NULL))
697                 SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
698         else {
699         /* Do an AUTH command if necessary */
700         sprintf(buf, "%s%c%s%c%s", 
701                 SendMsg->pCurrRelay->User, '\0', 
702                 SendMsg->pCurrRelay->User, '\0', 
703                 SendMsg->pCurrRelay->Pass);
704         CtdlEncodeBase64(encoded, buf, 
705                          strlen(SendMsg->pCurrRelay->User) * 2 +
706                          strlen(SendMsg->pCurrRelay->Pass) + 2, 0);
707         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
708                      "AUTH PLAIN %s\r\n", encoded);
709         }
710         SMTP_DBG_SEND();
711         return eReadMessage;
712 }
713
714 eNextState SMTPC_read_auth_reply(SmtpOutMsg *SendMsg)
715 {
716         /* Do an AUTH command if necessary */
717         
718         SMTP_DBG_READ();
719         
720         if (!SMTP_IS_STATE('2')) {
721                 if (SMTP_IS_STATE('4'))
722                         SMTP_VERROR(4);
723                 else 
724                         SMTP_VERROR(5);
725         }
726         return eSendReply;
727 }
728
729 eNextState SMTPC_send_FROM(SmtpOutMsg *SendMsg)
730 {
731         /* previous command succeeded, now try the MAIL FROM: command */
732         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
733                      "MAIL FROM:<%s>\r\n", 
734                      SendMsg->envelope_from);
735
736         SMTP_DBG_SEND();
737         return eReadMessage;
738 }
739
740 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *SendMsg)
741 {
742         SMTP_DBG_READ();
743
744         if (!SMTP_IS_STATE('2')) {
745                 if (SMTP_IS_STATE('4'))
746                         SMTP_VERROR(4);
747                 else 
748                         SMTP_VERROR(5);
749         }
750         return eSendReply;
751 }
752
753
754 eNextState SMTPC_send_RCPT(SmtpOutMsg *SendMsg)
755 {
756         /* MAIL succeeded, now try the RCPT To: command */
757         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
758                      "RCPT TO:<%s@%s>\r\n", 
759                      SendMsg->user, 
760                      SendMsg->node);
761
762         SMTP_DBG_SEND();
763         return eReadMessage;
764 }
765
766 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *SendMsg)
767 {
768         SMTP_DBG_READ();
769
770         if (!SMTP_IS_STATE('2')) {
771                 if (SMTP_IS_STATE('4')) 
772                         SMTP_VERROR(4);
773                 else 
774                         SMTP_VERROR(5);
775         }
776         return eSendReply;
777 }
778
779 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *SendMsg)
780 {
781         /* RCPT succeeded, now try the DATA command */
782         StrBufPlain(SendMsg->IO.SendBuf.Buf,
783                     HKEY("DATA\r\n"));
784
785         SMTP_DBG_SEND();
786         return eReadMessage;
787 }
788
789 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *SendMsg)
790 {
791         SMTP_DBG_READ();
792
793         if (!SMTP_IS_STATE('3')) {
794                 if (SMTP_IS_STATE('4')) 
795                         SMTP_VERROR(3);
796                 else 
797                         SMTP_VERROR(5);
798         }
799         return eSendReply;
800 }
801
802 eNextState SMTPC_send_data_body(SmtpOutMsg *SendMsg)
803 {
804         StrBuf *Buf;
805         /* If we reach this point, the server is expecting data.*/
806
807         Buf = SendMsg->IO.SendBuf.Buf;
808         SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
809         SendMsg->msgtext = Buf;
810         SendMsg->State ++;
811
812         return eSendMore;
813 }
814
815 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *SendMsg)
816 {
817         StrBuf *Buf;
818
819         Buf = SendMsg->IO.SendBuf.Buf;
820         SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
821         SendMsg->msgtext = Buf;
822
823         StrBufPlain(SendMsg->IO.SendBuf.Buf,
824                     HKEY(".\r\n"));
825
826         return eReadMessage;
827
828 }
829
830 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
831 {
832         SMTP_DBG_READ();
833
834         if (!SMTP_IS_STATE('2')) {
835                 if (SMTP_IS_STATE('4'))
836                         SMTP_VERROR(4);
837                 else 
838                         SMTP_VERROR(5);
839         }
840
841         /* We did it! */
842         StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
843                     &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
844                     StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
845         SendMsg->MyQEntry->Status = 2;
846         return eSendReply;
847 }
848
849 eNextState SMTPC_send_QUIT(SmtpOutMsg *SendMsg)
850 {
851         StrBufPlain(SendMsg->IO.SendBuf.Buf,
852                     HKEY("QUIT\r\n"));
853
854         SMTP_DBG_SEND();
855         return eReadMessage;
856 }
857
858 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
859 {
860         SMTP_DBG_READ();
861
862         CtdlLogPrintf(CTDL_INFO, "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
863                       SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
864         return eTerminateConnection;
865 }
866
867 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
868 {
869         return eSendReply;
870 }
871
872 eNextState SMTPC_send_dummy(SmtpOutMsg *SendMsg)
873 {
874         return eReadMessage;
875 }
876
877
878 /*****************************************************************************/
879 /*                     SMTP CLIENT DISPATCHER                                */
880 /*****************************************************************************/
881 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
882         SMTPC_read_greeting,
883         SMTPC_read_EHLO_reply,
884         SMTPC_read_HELO_reply,
885         SMTPC_read_auth_reply,
886         SMTPC_read_FROM_reply,
887         SMTPC_read_RCPT_reply,
888         SMTPC_read_DATAcmd_reply,
889         SMTPC_read_dummy,
890         SMTPC_read_data_body_reply,
891         SMTPC_read_QUIT_reply
892 };
893 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
894         SMTPC_send_dummy, /* we don't send a greeting, the server does... */
895         SMTPC_send_EHLO,
896         STMPC_send_HELO,
897         SMTPC_send_auth,
898         SMTPC_send_FROM,
899         SMTPC_send_RCPT,
900         SMTPC_send_DATAcmd,
901         SMTPC_send_data_body,
902         SMTPC_send_terminate_data_body,
903         SMTPC_send_QUIT
904 };
905
906 void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
907 {
908         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
909         double Timeout;
910         switch (NextTCPState) {
911         case eSendReply:
912         case eSendMore:
913                 Timeout = SMTP_C_SendTimeouts[pMsg->State];
914                 if (pMsg->State == eDATABody) {
915                         /* if we're sending a huge message, we need more time. */
916                         Timeout += StrLength(pMsg->msgtext) / 1024;
917                 }
918                 break;
919         case eReadMessage:
920                 Timeout = SMTP_C_ReadTimeouts[pMsg->State];
921                 if (pMsg->State == eDATATerminateBody) {
922                         /* 
923                          * some mailservers take a nap before accepting the message
924                          * content inspection and such.
925                          */
926                         Timeout += StrLength(pMsg->msgtext) / 1024;
927                 }
928                 break;
929         case eTerminateConnection:
930         case eAbort:
931                 return;
932         }
933         SetNextTimeout(&pMsg->IO, Timeout);
934 }
935 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
936 {
937         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
938         SmtpOutMsg *pMsg = IO->Data;
939         eNextState rc;
940
941         rc = ReadHandlers[pMsg->State](pMsg);
942         pMsg->State++;
943         SMTPSetTimeout(rc, pMsg);
944         return rc;
945 }
946 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
947 {
948         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
949         SmtpOutMsg *pMsg = IO->Data;
950         eNextState rc;
951
952         rc = SendHandlers[pMsg->State](pMsg);
953         SMTPSetTimeout(rc, pMsg);
954         return rc;
955 }
956
957
958 /*****************************************************************************/
959 /*                     SMTP CLIENT ERROR CATCHERS                            */
960 /*****************************************************************************/
961 eNextState SMTP_C_Terminate(AsyncIO *IO)
962 {
963         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
964         SmtpOutMsg *pMsg = IO->Data;
965         FinalizeMessageSend(pMsg);
966         return eAbort;
967 }
968 eNextState SMTP_C_Timeout(AsyncIO *IO)
969 {
970         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
971         SmtpOutMsg *pMsg = IO->Data;
972         StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
973         FinalizeMessageSend(pMsg);
974         return eAbort;
975 }
976 eNextState SMTP_C_ConnFail(AsyncIO *IO)
977 {
978         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
979         SmtpOutMsg *pMsg = IO->Data;
980         FinalizeMessageSend(pMsg);
981         return eAbort;
982 }
983 eNextState SMTP_C_Shutdown(AsyncIO *IO)
984 {
985         CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
986         SmtpOutMsg *pMsg = IO->Data;
987
988         pMsg->MyQEntry->Status = 3;
989         StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
990         FinalizeMessageSend(pMsg);
991         return eAbort;
992 }
993
994
995 /**
996  * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
997  */
998 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
999 {
1000         eReadState Finished = eBufferNotEmpty; 
1001
1002         while (Finished == eBufferNotEmpty) {
1003                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
1004                 
1005                 switch (Finished) {
1006                 case eMustReadMore: /// read new from socket... 
1007                         return Finished;
1008                         break;
1009                 case eBufferNotEmpty: /* shouldn't happen... */
1010                 case eReadSuccess: /// done for now...
1011                         if (StrLength(IO->IOBuf) < 4)
1012                                 continue;
1013                         if (ChrPtr(IO->IOBuf)[3] == '-')
1014                                 Finished = eBufferNotEmpty;
1015                         else 
1016                                 return Finished;
1017                         break;
1018                 case eReadFail: /// WHUT?
1019                         ///todo: shut down! 
1020                         break;
1021                 }
1022         }
1023         return Finished;
1024 }
1025
1026 #endif
1027 CTDL_MODULE_INIT(smtp_eventclient)
1028 {
1029         return "smtpeventclient";
1030 }