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