c974e822bf7d63b2fc8d019d2964de4012fcfef6
[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, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
110
111 #define SMTP_DBG_READ() \
112         EVS_syslog(LOG_DEBUG, "< %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                    "delivery to <%s> @ <%s> (%s) succeeded\n",
378                    Msg->user,
379                    Msg->node,
380                    Msg->name);
381
382         return eTerminateConnection;
383 }
384
385 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
386 {
387         return eSendReply;
388 }
389
390 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
391 {
392         return eReadMessage;
393 }
394
395 /*****************************************************************************/
396 /*                     SMTP CLIENT DISPATCHER                                */
397 /*****************************************************************************/
398 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
399         SMTPC_read_greeting,
400         SMTPC_read_EHLO_reply,
401         SMTPC_read_HELO_reply,
402         SMTPC_read_auth_reply,
403         SMTPC_read_FROM_reply,
404         SMTPC_read_RCPT_reply,
405         SMTPC_read_DATAcmd_reply,
406         SMTPC_read_dummy,
407         SMTPC_read_data_body_reply,
408         SMTPC_read_QUIT_reply
409 };
410 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
411         SMTPC_send_dummy, /* we don't send a greeting, the server does... */
412         SMTPC_send_EHLO,
413         STMPC_send_HELO,
414         SMTPC_send_auth,
415         SMTPC_send_FROM,
416         SMTPC_send_RCPT,
417         SMTPC_send_DATAcmd,
418         SMTPC_send_data_body,
419         SMTPC_send_terminate_data_body,
420         SMTPC_send_QUIT
421 };
422
423 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
424
425 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
426         300., /* Greeting... */
427         30., /* EHLO */
428         30., /* HELO */
429         30., /* Auth */
430         30., /* From */
431         90., /* RCPT */
432         30., /* DATA */
433         90., /* DATABody */
434         90., /* end of body... */
435         30.  /* QUIT */
436 };
437 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
438         90., /* Greeting... */
439         30., /* EHLO */
440         30., /* HELO */
441         30., /* Auth */
442         30., /* From */
443         30., /* RCPT */
444         30., /* DATA */
445         90., /* DATABody */
446         900., /* end of body... */
447         30.  /* QUIT */
448 };
449
450 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
451         {HKEY("Connection broken during SMTP conversation")},
452         {HKEY("Connection broken during SMTP EHLO")},
453         {HKEY("Connection broken during SMTP HELO")},
454         {HKEY("Connection broken during SMTP AUTH")},
455         {HKEY("Connection broken during SMTP MAIL FROM")},
456         {HKEY("Connection broken during SMTP RCPT")},
457         {HKEY("Connection broken during SMTP DATA")},
458         {HKEY("Connection broken during SMTP message transmit")},
459         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
460         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
461         {HKEY("")}/* quit reply, don't care. */
462 };
463
464
465
466
467
468 int smtp_resolve_recipients(SmtpOutMsg *Msg)
469 {
470         AsyncIO *IO = &Msg->IO;
471         const char *ptr;
472         char buf[1024];
473         int scan_done;
474         int lp, rp;
475         int i;
476
477         EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
478
479         if ((Msg==NULL) ||
480             (Msg->MyQEntry == NULL) ||
481             (StrLength(Msg->MyQEntry->Recipient) == 0)) {
482                 return 0;
483         }
484
485         /* Parse out the host portion of the recipient address */
486         process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
487                             Msg->user,
488                             Msg->node,
489                             Msg->name);
490
491         EVNCS_syslog(LOG_DEBUG,
492                      "Attempting delivery to <%s> @ <%s> (%s)\n",
493                      Msg->user,
494                      Msg->node,
495                      Msg->name);
496
497         /* If no envelope_from is supplied, extract one from the message */
498         Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
499         if ( (Msg->envelope_from == NULL) ||
500              (IsEmptyStr(Msg->envelope_from)) ) {
501                 Msg->mailfrom[0] = '\0';
502                 scan_done = 0;
503                 ptr = ChrPtr(Msg->msgtext);
504                 do {
505                         if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
506                         {
507                                 scan_done = 1;
508                         }
509                         if (!strncasecmp(buf, "From:", 5))
510                         {
511                                 safestrncpy(Msg->mailfrom,
512                                             &buf[5],
513                                             sizeof Msg->mailfrom);
514
515                                 striplt(Msg->mailfrom);
516                                 for (i=0; Msg->mailfrom[i]; ++i) {
517                                         if (!isprint(Msg->mailfrom[i]))
518                                         {
519                                                 strcpy(&Msg->mailfrom[i],
520                                                        &Msg->mailfrom[i+1]);
521                                                 i=0;
522                                         }
523                                 }
524
525                                 /* Strip out parenthesized names */
526                                 lp = (-1);
527                                 rp = (-1);
528                                 for (i=0;
529                                      !IsEmptyStr(Msg->mailfrom + i);
530                                      ++i)
531                                 {
532                                         if (Msg->mailfrom[i] == '(') lp = i;
533                                         if (Msg->mailfrom[i] == ')') rp = i;
534                                 }
535                                 if ((lp>0)&&(rp>lp))
536                                 {
537                                         strcpy(&Msg->mailfrom[lp-1],
538                                                &Msg->mailfrom[rp+1]);
539                                 }
540
541                                 /* Prefer brokketized names */
542                                 lp = (-1);
543                                 rp = (-1);
544                                 for (i=0;
545                                      !IsEmptyStr(Msg->mailfrom + i);
546                                      ++i)
547                                 {
548                                         if (Msg->mailfrom[i] == '<') lp = i;
549                                         if (Msg->mailfrom[i] == '>') rp = i;
550                                 }
551                                 if ( (lp>=0) && (rp>lp) ) {
552                                         Msg->mailfrom[rp] = 0;
553                                         memmove(Msg->mailfrom,
554                                                 &Msg->mailfrom[lp + 1],
555                                                 rp - lp);
556                                 }
557
558                                 scan_done = 1;
559                         }
560                 } while (scan_done == 0);
561                 if (IsEmptyStr(Msg->mailfrom))
562                         strcpy(Msg->mailfrom, "someone@somewhere.org");
563
564                 stripallbut(Msg->mailfrom, '<', '>');
565                 Msg->envelope_from = Msg->mailfrom;
566         }
567
568         return 1;
569 }