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