SMTP-Client: Implement AUTH LOGIN
[citadel.git] / citadel / modules / smtp / smtp_clienthandlers.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  * Copyright (c) 1998-2012 by the citadel.org team
21  *
22  *  This program is open source software; you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License version 3.
24  *  
25  *  
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  
33  *  
34  *  
35  */
36
37 #include "sysdep.h"
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <termios.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <pwd.h>
45 #include <errno.h>
46 #include <sys/types.h>
47 #include <syslog.h>
48
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
51 # include <time.h>
52 #else
53 # if HAVE_SYS_TIME_H
54 #  include <sys/time.h>
55 # else
56 #  include <time.h>
57 # endif
58 #endif
59 #include <sys/wait.h>
60 #include <ctype.h>
61 #include <string.h>
62 #include <limits.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <libcitadel.h>
67 #include "citadel.h"
68 #include "server.h"
69 #include "citserver.h"
70 #include "support.h"
71 #include "config.h"
72 #include "control.h"
73 #include "user_ops.h"
74 #include "database.h"
75 #include "msgbase.h"
76 #include "internet_addressing.h"
77 #include "genstamp.h"
78 #include "domain.h"
79 #include "clientsocket.h"
80 #include "locate_host.h"
81 #include "citadel_dirs.h"
82
83 #include "ctdl_module.h"
84
85 #include "smtp_util.h"
86 #include "event_client.h"
87 #include "smtpqueue.h"
88 #include "smtp_clienthandlers.h"
89
90
91 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {                             \
92                 Msg->MyQEntry->Status = WHICH_ERR;                     \
93                 StrBufAppendBufPlain(Msg->MyQEntry->StatusMessage,     \
94                                      HKEY(ERRSTR), 0);                 \
95                 StrBufTrim(Msg->MyQEntry->StatusMessage);              \
96                 return eAbort; }                                       \
97         while (0)
98
99 #define SMTP_VERROR(WHICH_ERR) do {                            \
100                 Msg->MyQEntry->Status = WHICH_ERR;             \
101                 StrBufPlain(Msg->MyQEntry->StatusMessage,      \
102                             ChrPtr(Msg->IO.IOBuf) + 4,         \
103                             StrLength(Msg->IO.IOBuf) - 4);     \
104                 StrBufTrim(Msg->MyQEntry->StatusMessage);      \
105                 return eAbort; }                               \
106         while (0)
107
108 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)
109
110 #define SMTP_DBG_SEND() \
111         EVS_syslog(LOG_DEBUG, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
112
113 #define SMTP_DBG_READ() \
114         EVS_syslog(LOG_DEBUG, "< %s\n", ChrPtr(Msg->IO.IOBuf))
115
116 /*
117  * if a Read handler wants to skip to a specific part use this macro.
118  * the -1 is here since the auto-forward following has to be taken into account.
119  */
120 #define READ_NEXT_STATE(state) Msg->State = state - 1
121
122 /*****************************************************************************/
123 /*                     SMTP CLIENT STATE CALLBACKS                           */
124 /*****************************************************************************/
125 eNextState SMTPC_read_greeting(SmtpOutMsg *Msg)
126 {
127         /* Process the SMTP greeting from the server */
128         AsyncIO *IO = &Msg->IO;
129         SMTP_DBG_READ();
130
131         if (!SMTP_IS_STATE('2')) {
132                 if (SMTP_IS_STATE('4'))
133                         SMTP_VERROR(4);
134                 else
135                         SMTP_VERROR(5);
136         }
137         return eSendReply;
138 }
139
140 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
141 {
142         AsyncIO *IO = &Msg->IO;
143         /* At this point we know we are talking to a real SMTP server */
144
145         /* Do a EHLO command.  If it fails, try the HELO command. */
146         StrBufPrintf(Msg->IO.SendBuf.Buf,
147                      "EHLO %s\r\n", config.c_fqdn);
148
149         SMTP_DBG_SEND();
150         return eReadMessage;
151 }
152
153 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
154 {
155         AsyncIO *IO = &Msg->IO;
156         SMTP_DBG_READ();
157
158         if (SMTP_IS_STATE('2')) {
159                 READ_NEXT_STATE(eSMTPAuth);
160
161                 if ((Msg->pCurrRelay == NULL) ||
162                     (Msg->pCurrRelay->User == NULL))
163                         READ_NEXT_STATE(eFROM); /* Skip auth... */
164                 if (Msg->pCurrRelay != NULL)
165                 {
166                         if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
167                                 Msg->SendLogin = 1;
168                 }
169         }
170         /* else we fall back to 'helo' */
171         return eSendReply;
172 }
173
174 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
175 {
176         AsyncIO *IO = &Msg->IO;
177         StrBufPrintf(Msg->IO.SendBuf.Buf,
178                      "HELO %s\r\n", config.c_fqdn);
179
180         SMTP_DBG_SEND();
181         return eReadMessage;
182 }
183
184 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
185 {
186         AsyncIO *IO = &Msg->IO;
187         SMTP_DBG_READ();
188
189         if (!SMTP_IS_STATE('2'))
190         {
191                 if (SMTP_IS_STATE('4'))
192                         SMTP_VERROR(4);
193                 else
194                         SMTP_VERROR(5);
195         }
196         if ((Msg->pCurrRelay == NULL) ||
197             (Msg->pCurrRelay->User == NULL))
198                 READ_NEXT_STATE(eFROM); /* Skip auth... */
199
200         return eSendReply;
201 }
202
203 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
204 {
205         AsyncIO *IO = &Msg->IO;
206         char buf[SIZ];
207         char encoded[1024];
208
209         if ((Msg->pCurrRelay == NULL) ||
210             (Msg->pCurrRelay->User == NULL))
211                 READ_NEXT_STATE(eFROM); /* Skip auth, shouldn't even come here!... */
212         else {
213                 /* Do an AUTH command if necessary */
214                 if (Msg->SendLogin)
215                 {
216                         StrBufPlain(Msg->IO.SendBuf.Buf,
217                                     HKEY("AUTH LOGIN\r\n"));
218                 }
219                 else
220                 {
221                         sprintf(buf, "%s%c%s%c%s",
222                                 Msg->pCurrRelay->User, '\0',
223                                 Msg->pCurrRelay->User, '\0',
224                                 Msg->pCurrRelay->Pass);
225                         
226                         CtdlEncodeBase64(encoded, buf,
227                                          strlen(Msg->pCurrRelay->User) * 2 +
228                                          strlen(Msg->pCurrRelay->Pass) + 2, 0);
229                         
230                         StrBufPrintf(Msg->IO.SendBuf.Buf,
231                                      "AUTH PLAIN %s\r\n",
232                                      encoded);
233                 }
234         }
235         SMTP_DBG_SEND();
236         return eReadMessage;
237 }
238
239
240 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
241 {
242         AsyncIO *IO = &Msg->IO;
243         /* Do an AUTH command if necessary */
244
245         SMTP_DBG_READ();
246
247         if (Msg->SendLogin)
248         {
249                 if (!SMTP_IS_STATE('3'))
250                         SMTP_VERROR(5);
251         }
252         else
253         {
254                 if (!SMTP_IS_STATE('2')) {
255                         if (SMTP_IS_STATE('4'))
256                                 SMTP_VERROR(4);
257                         else
258                                 SMTP_VERROR(5);
259                 }
260                 READ_NEXT_STATE(eFROM);
261         }
262         return eSendReply;
263 }
264
265
266 eNextState SMTPC_send_authplain_1(SmtpOutMsg *Msg)
267 {
268         AsyncIO *IO = &Msg->IO;
269         char buf[SIZ];
270         char encoded[1024];
271         long encodedlen;
272
273         sprintf(buf, "%s",
274                 Msg->pCurrRelay->User);
275         
276         encodedlen = CtdlEncodeBase64(
277                 encoded,
278                 Msg->pCurrRelay->User,
279                 strlen(Msg->pCurrRelay->User),
280                 0);
281
282         StrBufPlain(Msg->IO.SendBuf.Buf,
283                     encoded,
284                     encodedlen);
285
286         StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
287                              HKEY("\r\n"), 0);
288
289         SMTP_DBG_SEND();
290
291         return eReadMessage;
292 }
293 eNextState SMTPC_read_auth_plain_reply_1(SmtpOutMsg *Msg)
294 {
295         AsyncIO *IO = &Msg->IO;
296         /* Do an AUTH command if necessary */
297
298         SMTP_DBG_READ();
299
300         if (!SMTP_IS_STATE('3'))
301                 SMTP_VERROR(5);
302         return eSendReply;
303 }
304
305
306 eNextState SMTPC_send_authplain_2(SmtpOutMsg *Msg)
307 {
308         AsyncIO *IO = &Msg->IO;
309         char buf[SIZ];
310         char encoded[1024];
311         long encodedlen;
312
313         sprintf(buf, "%s",
314                 Msg->pCurrRelay->Pass);
315         
316         encodedlen = CtdlEncodeBase64(
317                 encoded,
318                 Msg->pCurrRelay->User,
319                 strlen(Msg->pCurrRelay->User),
320                 0);
321
322         StrBufPlain(Msg->IO.SendBuf.Buf,
323                     encoded,
324                     encodedlen);
325
326         StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
327                              HKEY("\r\n"), 0);
328
329         SMTP_DBG_SEND();
330
331         return eReadMessage;
332 }
333 eNextState SMTPC_read_auth_plain_reply_2(SmtpOutMsg *Msg)
334 {
335         AsyncIO *IO = &Msg->IO;
336         /* Do an AUTH command if necessary */
337
338         SMTP_DBG_READ();
339
340         if (!SMTP_IS_STATE('2')) {
341                 if (SMTP_IS_STATE('4'))
342                         SMTP_VERROR(4);
343                 else
344                         SMTP_VERROR(5);
345         }
346         return eSendReply;
347 }
348
349 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
350 {
351         AsyncIO *IO = &Msg->IO;
352         /* previous command succeeded, now try the MAIL FROM: command */
353         StrBufPrintf(Msg->IO.SendBuf.Buf,
354                      "MAIL FROM:<%s>\r\n",
355                      Msg->envelope_from);
356
357         SMTP_DBG_SEND();
358         return eReadMessage;
359 }
360
361 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
362 {
363         AsyncIO *IO = &Msg->IO;
364         SMTP_DBG_READ();
365
366         if (!SMTP_IS_STATE('2')) {
367                 if (SMTP_IS_STATE('4'))
368                         SMTP_VERROR(4);
369                 else
370                         SMTP_VERROR(5);
371         }
372         return eSendReply;
373 }
374
375
376 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
377 {
378         AsyncIO *IO = &Msg->IO;
379         /* MAIL succeeded, now try the RCPT To: command */
380         StrBufPrintf(Msg->IO.SendBuf.Buf,
381                      "RCPT TO:<%s@%s>\r\n",
382                      Msg->user,
383                      Msg->node);
384
385         SMTP_DBG_SEND();
386         return eReadMessage;
387 }
388
389 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
390 {
391         AsyncIO *IO = &Msg->IO;
392         SMTP_DBG_READ();
393
394         if (!SMTP_IS_STATE('2')) {
395                 if (SMTP_IS_STATE('4'))
396                         SMTP_VERROR(4);
397                 else
398                         SMTP_VERROR(5);
399         }
400         return eSendReply;
401 }
402
403 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
404 {
405         AsyncIO *IO = &Msg->IO;
406         /* RCPT succeeded, now try the DATA command */
407         StrBufPlain(Msg->IO.SendBuf.Buf,
408                     HKEY("DATA\r\n"));
409
410         SMTP_DBG_SEND();
411         return eReadMessage;
412 }
413
414 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
415 {
416         AsyncIO *IO = &Msg->IO;
417         SMTP_DBG_READ();
418
419         if (!SMTP_IS_STATE('3')) {
420                 if (SMTP_IS_STATE('4'))
421                         SMTP_VERROR(3);
422                 else
423                         SMTP_VERROR(5);
424         }
425         return eSendReply;
426 }
427
428 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
429 {
430         StrBuf *Buf;
431         /* If we reach this point, the server is expecting data.*/
432
433         Buf = Msg->IO.SendBuf.Buf;
434         Msg->IO.SendBuf.Buf = Msg->msgtext;
435         Msg->msgtext = Buf;
436         /* 
437          * sending the message itself doesn't use this state machine.
438          * so we have to operate it here by ourselves.
439          */
440         Msg->State ++;
441
442         return eSendMore;
443 }
444
445 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
446 {
447         StrBuf *Buf;
448
449         Buf = Msg->IO.SendBuf.Buf;
450         Msg->IO.SendBuf.Buf = Msg->msgtext;
451         Msg->msgtext = Buf;
452
453         StrBufPlain(Msg->IO.SendBuf.Buf,
454                     HKEY(".\r\n"));
455
456         return eReadMessage;
457
458 }
459
460 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
461 {
462         AsyncIO *IO = &Msg->IO;
463         SMTP_DBG_READ();
464
465         if (!SMTP_IS_STATE('2')) {
466                 if (SMTP_IS_STATE('4'))
467                         SMTP_VERROR(4);
468                 else
469                         SMTP_VERROR(5);
470         }
471
472         /* We did it! */
473         StrBufPlain(Msg->MyQEntry->StatusMessage,
474                     &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
475                     StrLength(Msg->IO.RecvBuf.Buf) - 4);
476         StrBufTrim(Msg->MyQEntry->StatusMessage);
477         Msg->MyQEntry->Status = 2;
478         return eSendReply;
479 }
480
481 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
482 {
483         AsyncIO *IO = &Msg->IO;
484         StrBufPlain(Msg->IO.SendBuf.Buf,
485                     HKEY("QUIT\r\n"));
486
487         SMTP_DBG_SEND();
488         return eReadMessage;
489 }
490
491 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
492 {
493         AsyncIO *IO = &Msg->IO;
494         SMTP_DBG_READ();
495
496         EVS_syslog(LOG_DEBUG,
497                    "delivery to <%s> @ <%s> (%s) succeeded\n",
498                    Msg->user,
499                    Msg->node,
500                    Msg->name);
501
502         return eTerminateConnection;
503 }
504
505 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
506 {
507         return eSendReply;
508 }
509
510 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
511 {
512         return eReadMessage;
513 }
514
515 /*****************************************************************************/
516 /*                     SMTP CLIENT DISPATCHER                                */
517 /*****************************************************************************/
518 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
519         SMTPC_read_greeting,
520         SMTPC_read_EHLO_reply,
521         SMTPC_read_HELO_reply,
522         SMTPC_read_auth_reply,
523         SMTPC_read_auth_plain_reply_1,
524         SMTPC_read_auth_plain_reply_2,
525         SMTPC_read_FROM_reply,
526         SMTPC_read_RCPT_reply,
527         SMTPC_read_DATAcmd_reply,
528         SMTPC_read_dummy,
529         SMTPC_read_data_body_reply,
530         SMTPC_read_QUIT_reply
531 };
532 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
533         SMTPC_send_dummy, /* we don't send a greeting, the server does... */
534         SMTPC_send_EHLO,
535         STMPC_send_HELO,
536         SMTPC_send_auth,
537         SMTPC_send_authplain_1,
538         SMTPC_send_authplain_2,
539         SMTPC_send_FROM,
540         SMTPC_send_RCPT,
541         SMTPC_send_DATAcmd,
542         SMTPC_send_data_body,
543         SMTPC_send_terminate_data_body,
544         SMTPC_send_QUIT
545 };
546
547 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
548
549 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
550         300., /* Greeting... */
551         30., /* EHLO */
552         30., /* HELO */
553         30., /* Auth */
554         30., /* Auth */
555         30., /* Auth */
556         30., /* From */
557         90., /* RCPT */
558         30., /* DATA */
559         90., /* DATABody */
560         90., /* end of body... */
561         30.  /* QUIT */
562 };
563 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
564         90., /* Greeting... */
565         30., /* EHLO */
566         30., /* HELO */
567         30., /* Auth */
568         30., /* Auth */
569         30., /* Auth */
570         30., /* From */
571         30., /* RCPT */
572         30., /* DATA */
573         90., /* DATABody */
574         900., /* end of body... */
575         30.  /* QUIT */
576 };
577
578 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
579         {HKEY("Connection broken during SMTP conversation")},
580         {HKEY("Connection broken during SMTP EHLO")},
581         {HKEY("Connection broken during SMTP HELO")},
582         {HKEY("Connection broken during SMTP AUTH")},
583         {HKEY("Connection broken during SMTP AUTH PLAIN I")},
584         {HKEY("Connection broken during SMTP AUTH PLAIN II")},
585         {HKEY("Connection broken during SMTP MAIL FROM")},
586         {HKEY("Connection broken during SMTP RCPT")},
587         {HKEY("Connection broken during SMTP DATA")},
588         {HKEY("Connection broken during SMTP message transmit")},
589         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
590         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
591         {HKEY("")}/* quit reply, don't care. */
592 };
593
594
595
596
597
598 int smtp_resolve_recipients(SmtpOutMsg *Msg)
599 {
600         AsyncIO *IO = &Msg->IO;
601         const char *ptr;
602         char buf[1024];
603         int scan_done;
604         int lp, rp;
605         int i;
606
607         EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
608
609         if ((Msg==NULL) ||
610             (Msg->MyQEntry == NULL) ||
611             (StrLength(Msg->MyQEntry->Recipient) == 0)) {
612                 return 0;
613         }
614
615         /* Parse out the host portion of the recipient address */
616         process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
617                             Msg->user,
618                             Msg->node,
619                             Msg->name);
620
621         EVNCS_syslog(LOG_DEBUG,
622                      "Attempting delivery to <%s> @ <%s> (%s)\n",
623                      Msg->user,
624                      Msg->node,
625                      Msg->name);
626
627         /* If no envelope_from is supplied, extract one from the message */
628         Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
629         if ( (Msg->envelope_from == NULL) ||
630              (IsEmptyStr(Msg->envelope_from)) ) {
631                 Msg->mailfrom[0] = '\0';
632                 scan_done = 0;
633                 ptr = ChrPtr(Msg->msgtext);
634                 do {
635                         if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
636                         {
637                                 scan_done = 1;
638                         }
639                         if (!strncasecmp(buf, "From:", 5))
640                         {
641                                 safestrncpy(Msg->mailfrom,
642                                             &buf[5],
643                                             sizeof Msg->mailfrom);
644
645                                 striplt(Msg->mailfrom);
646                                 for (i=0; Msg->mailfrom[i]; ++i) {
647                                         if (!isprint(Msg->mailfrom[i]))
648                                         {
649                                                 strcpy(&Msg->mailfrom[i],
650                                                        &Msg->mailfrom[i+1]);
651                                                 i=0;
652                                         }
653                                 }
654
655                                 /* Strip out parenthesized names */
656                                 lp = (-1);
657                                 rp = (-1);
658                                 for (i=0;
659                                      !IsEmptyStr(Msg->mailfrom + i);
660                                      ++i)
661                                 {
662                                         if (Msg->mailfrom[i] == '(') lp = i;
663                                         if (Msg->mailfrom[i] == ')') rp = i;
664                                 }
665                                 if ((lp>0)&&(rp>lp))
666                                 {
667                                         strcpy(&Msg->mailfrom[lp-1],
668                                                &Msg->mailfrom[rp+1]);
669                                 }
670
671                                 /* Prefer brokketized names */
672                                 lp = (-1);
673                                 rp = (-1);
674                                 for (i=0;
675                                      !IsEmptyStr(Msg->mailfrom + i);
676                                      ++i)
677                                 {
678                                         if (Msg->mailfrom[i] == '<') lp = i;
679                                         if (Msg->mailfrom[i] == '>') rp = i;
680                                 }
681                                 if ( (lp>=0) && (rp>lp) ) {
682                                         Msg->mailfrom[rp] = 0;
683                                         memmove(Msg->mailfrom,
684                                                 &Msg->mailfrom[lp + 1],
685                                                 rp - lp);
686                                 }
687
688                                 scan_done = 1;
689                         }
690                 } while (scan_done == 0);
691                 if (IsEmptyStr(Msg->mailfrom))
692                         strcpy(Msg->mailfrom, "someone@somewhere.org");
693
694                 stripallbut(Msg->mailfrom, '<', '>');
695                 Msg->envelope_from = Msg->mailfrom;
696         }
697
698         return 1;
699 }