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