0a01805c6dae39dd9430b038d7dbd03977a638ea
[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                 return eAbort; }                                       \
96         while (0)
97
98 #define SMTP_VERROR(WHICH_ERR) do {                            \
99                 Msg->MyQEntry->Status = WHICH_ERR;             \
100                 StrBufPlain(Msg->MyQEntry->StatusMessage,      \
101                             ChrPtr(Msg->IO.IOBuf) + 4,         \
102                             StrLength(Msg->IO.IOBuf) - 4);     \
103                 return eAbort; }                               \
104         while (0)
105
106 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)
107
108 #define SMTP_DBG_SEND() \
109         EVS_syslog(LOG_DEBUG, "SMTP: > %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
110
111 #define SMTP_DBG_READ() \
112         EVS_syslog(LOG_DEBUG, "SMTP: < %s\n", ChrPtr(Msg->IO.IOBuf))
113
114
115 /*****************************************************************************/
116 /*                     SMTP CLIENT STATE CALLBACKS                           */
117 /*****************************************************************************/
118 eNextState SMTPC_read_greeting(SmtpOutMsg *Msg)
119 {
120         /* Process the SMTP greeting from the server */
121         AsyncIO *IO = &Msg->IO;
122         SMTP_DBG_READ();
123
124         if (!SMTP_IS_STATE('2')) {
125                 if (SMTP_IS_STATE('4'))
126                         SMTP_VERROR(4);
127                 else
128                         SMTP_VERROR(5);
129         }
130         return eSendReply;
131 }
132
133 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
134 {
135         AsyncIO *IO = &Msg->IO;
136         /* At this point we know we are talking to a real SMTP server */
137
138         /* Do a EHLO command.  If it fails, try the HELO command. */
139         StrBufPrintf(Msg->IO.SendBuf.Buf,
140                      "EHLO %s\r\n", config.c_fqdn);
141
142         SMTP_DBG_SEND();
143         return eReadMessage;
144 }
145
146 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
147 {
148         AsyncIO *IO = &Msg->IO;
149         SMTP_DBG_READ();
150
151         if (SMTP_IS_STATE('2')) {
152                 Msg->State ++;
153
154                 if ((Msg->pCurrRelay == NULL) ||
155                     (Msg->pCurrRelay->User == NULL))
156                         Msg->State ++; /* Skip auth... */
157         }
158         /* else we fall back to 'helo' */
159         return eSendReply;
160 }
161
162 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
163 {
164         AsyncIO *IO = &Msg->IO;
165         StrBufPrintf(Msg->IO.SendBuf.Buf,
166                      "HELO %s\r\n", config.c_fqdn);
167
168         SMTP_DBG_SEND();
169         return eReadMessage;
170 }
171
172 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
173 {
174         AsyncIO *IO = &Msg->IO;
175         SMTP_DBG_READ();
176
177         if (!SMTP_IS_STATE('2'))
178         {
179                 if (SMTP_IS_STATE('4'))
180                         SMTP_VERROR(4);
181                 else
182                         SMTP_VERROR(5);
183         }
184         if ((Msg->pCurrRelay == NULL) ||
185             (Msg->pCurrRelay->User == NULL))
186                 Msg->State ++; /* Skip auth... */
187
188         return eSendReply;
189 }
190
191 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
192 {
193         AsyncIO *IO = &Msg->IO;
194         char buf[SIZ];
195         char encoded[1024];
196
197         if ((Msg->pCurrRelay == NULL) ||
198             (Msg->pCurrRelay->User == NULL))
199                 Msg->State ++; /* Skip auth, shouldn't even come here!... */
200         else {
201                 /* Do an AUTH command if necessary */
202                 sprintf(buf, "%s%c%s%c%s",
203                         Msg->pCurrRelay->User, '\0',
204                         Msg->pCurrRelay->User, '\0',
205                         Msg->pCurrRelay->Pass);
206
207                 CtdlEncodeBase64(encoded, buf,
208                                  strlen(Msg->pCurrRelay->User) * 2 +
209                                  strlen(Msg->pCurrRelay->Pass) + 2, 0);
210
211                 StrBufPrintf(Msg->IO.SendBuf.Buf,
212                              "AUTH PLAIN %s\r\n", encoded);
213         }
214         SMTP_DBG_SEND();
215         return eReadMessage;
216 }
217
218 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
219 {
220         AsyncIO *IO = &Msg->IO;
221         /* Do an AUTH command if necessary */
222
223         SMTP_DBG_READ();
224
225         if (!SMTP_IS_STATE('2')) {
226                 if (SMTP_IS_STATE('4'))
227                         SMTP_VERROR(4);
228                 else
229                         SMTP_VERROR(5);
230         }
231         return eSendReply;
232 }
233
234 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
235 {
236         AsyncIO *IO = &Msg->IO;
237         /* previous command succeeded, now try the MAIL FROM: command */
238         StrBufPrintf(Msg->IO.SendBuf.Buf,
239                      "MAIL FROM:<%s>\r\n",
240                      Msg->envelope_from);
241
242         SMTP_DBG_SEND();
243         return eReadMessage;
244 }
245
246 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
247 {
248         AsyncIO *IO = &Msg->IO;
249         SMTP_DBG_READ();
250
251         if (!SMTP_IS_STATE('2')) {
252                 if (SMTP_IS_STATE('4'))
253                         SMTP_VERROR(4);
254                 else
255                         SMTP_VERROR(5);
256         }
257         return eSendReply;
258 }
259
260
261 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
262 {
263         AsyncIO *IO = &Msg->IO;
264         /* MAIL succeeded, now try the RCPT To: command */
265         StrBufPrintf(Msg->IO.SendBuf.Buf,
266                      "RCPT TO:<%s@%s>\r\n",
267                      Msg->user,
268                      Msg->node);
269
270         SMTP_DBG_SEND();
271         return eReadMessage;
272 }
273
274 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
275 {
276         AsyncIO *IO = &Msg->IO;
277         SMTP_DBG_READ();
278
279         if (!SMTP_IS_STATE('2')) {
280                 if (SMTP_IS_STATE('4'))
281                         SMTP_VERROR(4);
282                 else
283                         SMTP_VERROR(5);
284         }
285         return eSendReply;
286 }
287
288 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
289 {
290         AsyncIO *IO = &Msg->IO;
291         /* RCPT succeeded, now try the DATA command */
292         StrBufPlain(Msg->IO.SendBuf.Buf,
293                     HKEY("DATA\r\n"));
294
295         SMTP_DBG_SEND();
296         return eReadMessage;
297 }
298
299 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
300 {
301         AsyncIO *IO = &Msg->IO;
302         SMTP_DBG_READ();
303
304         if (!SMTP_IS_STATE('3')) {
305                 if (SMTP_IS_STATE('4'))
306                         SMTP_VERROR(3);
307                 else
308                         SMTP_VERROR(5);
309         }
310         return eSendReply;
311 }
312
313 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
314 {
315         StrBuf *Buf;
316         /* If we reach this point, the server is expecting data.*/
317
318         Buf = Msg->IO.SendBuf.Buf;
319         Msg->IO.SendBuf.Buf = Msg->msgtext;
320         Msg->msgtext = Buf;
321         Msg->State ++;
322
323         return eSendMore;
324 }
325
326 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
327 {
328         StrBuf *Buf;
329
330         Buf = Msg->IO.SendBuf.Buf;
331         Msg->IO.SendBuf.Buf = Msg->msgtext;
332         Msg->msgtext = Buf;
333
334         StrBufPlain(Msg->IO.SendBuf.Buf,
335                     HKEY(".\r\n"));
336
337         return eReadMessage;
338
339 }
340
341 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
342 {
343         AsyncIO *IO = &Msg->IO;
344         SMTP_DBG_READ();
345
346         if (!SMTP_IS_STATE('2')) {
347                 if (SMTP_IS_STATE('4'))
348                         SMTP_VERROR(4);
349                 else
350                         SMTP_VERROR(5);
351         }
352
353         /* We did it! */
354         StrBufPlain(Msg->MyQEntry->StatusMessage,
355                     &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
356                     StrLength(Msg->IO.RecvBuf.Buf) - 4);
357         Msg->MyQEntry->Status = 2;
358         return eSendReply;
359 }
360
361 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
362 {
363         AsyncIO *IO = &Msg->IO;
364         StrBufPlain(Msg->IO.SendBuf.Buf,
365                     HKEY("QUIT\r\n"));
366
367         SMTP_DBG_SEND();
368         return eReadMessage;
369 }
370
371 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
372 {
373         AsyncIO *IO = &Msg->IO;
374         SMTP_DBG_READ();
375
376         EVS_syslog(LOG_DEBUG,
377                    "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
378                    Msg->n,
379                    Msg->user,
380                    Msg->node,
381                    Msg->name);
382
383         return eTerminateConnection;
384 }
385
386 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
387 {
388         return eSendReply;
389 }
390
391 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
392 {
393         return eReadMessage;
394 }
395
396 /*****************************************************************************/
397 /*                     SMTP CLIENT DISPATCHER                                */
398 /*****************************************************************************/
399 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
400         SMTPC_read_greeting,
401         SMTPC_read_EHLO_reply,
402         SMTPC_read_HELO_reply,
403         SMTPC_read_auth_reply,
404         SMTPC_read_FROM_reply,
405         SMTPC_read_RCPT_reply,
406         SMTPC_read_DATAcmd_reply,
407         SMTPC_read_dummy,
408         SMTPC_read_data_body_reply,
409         SMTPC_read_QUIT_reply
410 };
411 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
412         SMTPC_send_dummy, /* we don't send a greeting, the server does... */
413         SMTPC_send_EHLO,
414         STMPC_send_HELO,
415         SMTPC_send_auth,
416         SMTPC_send_FROM,
417         SMTPC_send_RCPT,
418         SMTPC_send_DATAcmd,
419         SMTPC_send_data_body,
420         SMTPC_send_terminate_data_body,
421         SMTPC_send_QUIT
422 };
423
424 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
425
426 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
427         300., /* Greeting... */
428         30., /* EHLO */
429         30., /* HELO */
430         30., /* Auth */
431         30., /* From */
432         90., /* RCPT */
433         30., /* DATA */
434         90., /* DATABody */
435         90., /* end of body... */
436         30.  /* QUIT */
437 };
438 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
439         90., /* Greeting... */
440         30., /* EHLO */
441         30., /* HELO */
442         30., /* Auth */
443         30., /* From */
444         30., /* RCPT */
445         30., /* DATA */
446         90., /* DATABody */
447         900., /* end of body... */
448         30.  /* QUIT */
449 };
450
451 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
452         {HKEY("Connection broken during SMTP conversation")},
453         {HKEY("Connection broken during SMTP EHLO")},
454         {HKEY("Connection broken during SMTP HELO")},
455         {HKEY("Connection broken during SMTP AUTH")},
456         {HKEY("Connection broken during SMTP MAIL FROM")},
457         {HKEY("Connection broken during SMTP RCPT")},
458         {HKEY("Connection broken during SMTP DATA")},
459         {HKEY("Connection broken during SMTP message transmit")},
460         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
461         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
462         {HKEY("")}/* quit reply, don't care. */
463 };
464
465
466
467
468
469 int smtp_resolve_recipients(SmtpOutMsg *Msg)
470 {
471         AsyncIO *IO = &Msg->IO;
472         const char *ptr;
473         char buf[1024];
474         int scan_done;
475         int lp, rp;
476         int i;
477
478         EVNCS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
479
480         if ((Msg==NULL) ||
481             (Msg->MyQEntry == NULL) ||
482             (StrLength(Msg->MyQEntry->Recipient) == 0)) {
483                 return 0;
484         }
485
486         /* Parse out the host portion of the recipient address */
487         process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
488                             Msg->user,
489                             Msg->node,
490                             Msg->name);
491
492         EVNCS_syslog(LOG_DEBUG,
493                      "SMTP client[%ld]: Attempting delivery to "
494                      "<%s> @ <%s> (%s)\n",
495                      Msg->n,
496                      Msg->user,
497                      Msg->node,
498                      Msg->name);
499
500         /* If no envelope_from is supplied, extract one from the message */
501         Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
502         if ( (Msg->envelope_from == NULL) ||
503              (IsEmptyStr(Msg->envelope_from)) ) {
504                 Msg->mailfrom[0] = '\0';
505                 scan_done = 0;
506                 ptr = ChrPtr(Msg->msgtext);
507                 do {
508                         if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
509                         {
510                                 scan_done = 1;
511                         }
512                         if (!strncasecmp(buf, "From:", 5))
513                         {
514                                 safestrncpy(Msg->mailfrom,
515                                             &buf[5],
516                                             sizeof Msg->mailfrom);
517
518                                 striplt(Msg->mailfrom);
519                                 for (i=0; Msg->mailfrom[i]; ++i) {
520                                         if (!isprint(Msg->mailfrom[i]))
521                                         {
522                                                 strcpy(&Msg->mailfrom[i],
523                                                        &Msg->mailfrom[i+1]);
524                                                 i=0;
525                                         }
526                                 }
527
528                                 /* Strip out parenthesized names */
529                                 lp = (-1);
530                                 rp = (-1);
531                                 for (i=0;
532                                      !IsEmptyStr(Msg->mailfrom + i);
533                                      ++i)
534                                 {
535                                         if (Msg->mailfrom[i] == '(') lp = i;
536                                         if (Msg->mailfrom[i] == ')') rp = i;
537                                 }
538                                 if ((lp>0)&&(rp>lp))
539                                 {
540                                         strcpy(&Msg->mailfrom[lp-1],
541                                                &Msg->mailfrom[rp+1]);
542                                 }
543
544                                 /* Prefer brokketized names */
545                                 lp = (-1);
546                                 rp = (-1);
547                                 for (i=0;
548                                      !IsEmptyStr(Msg->mailfrom + i);
549                                      ++i)
550                                 {
551                                         if (Msg->mailfrom[i] == '<') lp = i;
552                                         if (Msg->mailfrom[i] == '>') rp = i;
553                                 }
554                                 if ( (lp>=0) && (rp>lp) ) {
555                                         Msg->mailfrom[rp] = 0;
556                                         memmove(Msg->mailfrom,
557                                                 &Msg->mailfrom[lp + 1],
558                                                 rp - lp);
559                                 }
560
561                                 scan_done = 1;
562                         }
563                 } while (scan_done == 0);
564                 if (IsEmptyStr(Msg->mailfrom))
565                         strcpy(Msg->mailfrom, "someone@somewhere.org");
566
567                 stripallbut(Msg->mailfrom, '<', '>');
568                 Msg->envelope_from = Msg->mailfrom;
569         }
570
571         return 1;
572 }