Logging enhancement & SMTP client fixes
[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  * The VRFY and EXPN commands have been removed from this implementation
21  * because nobody uses these commands anymore, except for spammers.
22  *
23  * Copyright (c) 1998-2009 by the citadel.org team
24  *
25  *  This program is free software; you can redistribute it and/or modify
26  *  it under the terms of the GNU General Public License as published by
27  *  the Free Software Foundation; either version 3 of the License, or
28  *  (at your option) any later version.
29  *
30  *  This program is distributed in the hope that it will be useful,
31  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
32  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  *  GNU General Public License for more details.
34  *
35  *  You should have received a copy of the GNU General Public License
36  *  along with this program; if not, write to the Free Software
37  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38  */
39
40 #include "sysdep.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <termios.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 #  include <sys/time.h>
58 # else
59 #  include <time.h>
60 # endif
61 #endif
62 #include <sys/wait.h>
63 #include <ctype.h>
64 #include <string.h>
65 #include <limits.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
70 #include "citadel.h"
71 #include "server.h"
72 #include "citserver.h"
73 #include "support.h"
74 #include "config.h"
75 #include "control.h"
76 #include "user_ops.h"
77 #include "database.h"
78 #include "msgbase.h"
79 #include "internet_addressing.h"
80 #include "genstamp.h"
81 #include "domain.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
85
86 #include "ctdl_module.h"
87
88 #include "smtp_util.h"
89 #include "event_client.h"
90 #include "smtpqueue.h"
91 #include "smtp_clienthandlers.h"
92
93
94 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {\
95                 SendMsg->MyQEntry->Status = WHICH_ERR; \
96                 StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); \
97                 return eAbort; } \
98         while (0)
99
100 #define SMTP_VERROR(WHICH_ERR) do {\
101                 SendMsg->MyQEntry->Status = WHICH_ERR; \
102                 StrBufPlain(SendMsg->MyQEntry->StatusMessage, \
103                             ChrPtr(SendMsg->IO.IOBuf) + 4, \
104                             StrLength(SendMsg->IO.IOBuf) - 4); \
105                 return eAbort; } \
106         while (0)
107
108 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
109
110 #define SMTP_DBG_SEND() EV_syslog(LOG_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.SendBuf.Buf))
111 #define SMTP_DBG_READ() EV_syslog(LOG_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
112
113
114 /*****************************************************************************/
115 /*                     SMTP CLIENT STATE CALLBACKS                           */
116 /*****************************************************************************/
117 eNextState SMTPC_read_greeting(SmtpOutMsg *SendMsg)
118 {
119         /* Process the SMTP greeting from the server */
120         AsyncIO *IO = &SendMsg->IO;
121         SMTP_DBG_READ();
122
123         if (!SMTP_IS_STATE('2')) {
124                 if (SMTP_IS_STATE('4')) 
125                         SMTP_VERROR(4);
126                 else 
127                         SMTP_VERROR(5);
128         }
129         return eSendReply;
130 }
131
132 eNextState SMTPC_send_EHLO(SmtpOutMsg *SendMsg)
133 {
134         AsyncIO *IO = &SendMsg->IO;
135         /* At this point we know we are talking to a real SMTP server */
136
137         /* Do a EHLO command.  If it fails, try the HELO command. */
138         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
139                      "EHLO %s\r\n", config.c_fqdn);
140
141         SMTP_DBG_SEND();
142         return eReadMessage;
143 }
144
145 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *SendMsg)
146 {
147         AsyncIO *IO = &SendMsg->IO;
148         SMTP_DBG_READ();
149
150         if (SMTP_IS_STATE('2')) {
151                 SendMsg->State ++;
152
153                 if ((SendMsg->pCurrRelay == NULL) || 
154                     (SendMsg->pCurrRelay->User == NULL))
155                         SendMsg->State ++; /* Skip auth... */
156         }
157         /* else we fall back to 'helo' */
158         return eSendReply;
159 }
160
161 eNextState STMPC_send_HELO(SmtpOutMsg *SendMsg)
162 {
163         AsyncIO *IO = &SendMsg->IO;
164         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
165                      "HELO %s\r\n", config.c_fqdn);
166
167         SMTP_DBG_SEND();
168         return eReadMessage;
169 }
170
171 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *SendMsg)
172 {
173         AsyncIO *IO = &SendMsg->IO;
174         SMTP_DBG_READ();
175
176         if (!SMTP_IS_STATE('2')) {
177                 if (SMTP_IS_STATE('4'))
178                         SMTP_VERROR(4);
179                 else 
180                         SMTP_VERROR(5);
181         }
182                 if ((SendMsg->pCurrRelay == NULL) || 
183                     (SendMsg->pCurrRelay->User == NULL))
184                 SendMsg->State ++; /* Skip auth... */
185         return eSendReply;
186 }
187
188 eNextState SMTPC_send_auth(SmtpOutMsg *SendMsg)
189 {
190         AsyncIO *IO = &SendMsg->IO;
191         char buf[SIZ];
192         char encoded[1024];
193
194         if ((SendMsg->pCurrRelay == NULL) || 
195             (SendMsg->pCurrRelay->User == NULL))
196                 SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
197         else {
198         /* Do an AUTH command if necessary */
199         sprintf(buf, "%s%c%s%c%s", 
200                 SendMsg->pCurrRelay->User, '\0', 
201                 SendMsg->pCurrRelay->User, '\0', 
202                 SendMsg->pCurrRelay->Pass);
203         CtdlEncodeBase64(encoded, buf, 
204                          strlen(SendMsg->pCurrRelay->User) * 2 +
205                          strlen(SendMsg->pCurrRelay->Pass) + 2, 0);
206         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
207                      "AUTH PLAIN %s\r\n", encoded);
208         }
209         SMTP_DBG_SEND();
210         return eReadMessage;
211 }
212
213 eNextState SMTPC_read_auth_reply(SmtpOutMsg *SendMsg)
214 {
215         AsyncIO *IO = &SendMsg->IO;
216         /* Do an AUTH command if necessary */
217         
218         SMTP_DBG_READ();
219         
220         if (!SMTP_IS_STATE('2')) {
221                 if (SMTP_IS_STATE('4'))
222                         SMTP_VERROR(4);
223                 else 
224                         SMTP_VERROR(5);
225         }
226         return eSendReply;
227 }
228
229 eNextState SMTPC_send_FROM(SmtpOutMsg *SendMsg)
230 {
231         AsyncIO *IO = &SendMsg->IO;
232         /* previous command succeeded, now try the MAIL FROM: command */
233         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
234                      "MAIL FROM:<%s>\r\n", 
235                      SendMsg->envelope_from);
236
237         SMTP_DBG_SEND();
238         return eReadMessage;
239 }
240
241 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *SendMsg)
242 {
243         AsyncIO *IO = &SendMsg->IO;
244         SMTP_DBG_READ();
245
246         if (!SMTP_IS_STATE('2')) {
247                 if (SMTP_IS_STATE('4'))
248                         SMTP_VERROR(4);
249                 else 
250                         SMTP_VERROR(5);
251         }
252         return eSendReply;
253 }
254
255
256 eNextState SMTPC_send_RCPT(SmtpOutMsg *SendMsg)
257 {
258         AsyncIO *IO = &SendMsg->IO;
259         /* MAIL succeeded, now try the RCPT To: command */
260         StrBufPrintf(SendMsg->IO.SendBuf.Buf,
261                      "RCPT TO:<%s@%s>\r\n", 
262                      SendMsg->user, 
263                      SendMsg->node);
264
265         SMTP_DBG_SEND();
266         return eReadMessage;
267 }
268
269 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *SendMsg)
270 {
271         AsyncIO *IO = &SendMsg->IO;
272         SMTP_DBG_READ();
273
274         if (!SMTP_IS_STATE('2')) {
275                 if (SMTP_IS_STATE('4')) 
276                         SMTP_VERROR(4);
277                 else 
278                         SMTP_VERROR(5);
279         }
280         return eSendReply;
281 }
282
283 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *SendMsg)
284 {
285         AsyncIO *IO = &SendMsg->IO;
286         /* RCPT succeeded, now try the DATA command */
287         StrBufPlain(SendMsg->IO.SendBuf.Buf,
288                     HKEY("DATA\r\n"));
289
290         SMTP_DBG_SEND();
291         return eReadMessage;
292 }
293
294 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *SendMsg)
295 {
296         AsyncIO *IO = &SendMsg->IO;
297         SMTP_DBG_READ();
298
299         if (!SMTP_IS_STATE('3')) {
300                 if (SMTP_IS_STATE('4')) 
301                         SMTP_VERROR(3);
302                 else 
303                         SMTP_VERROR(5);
304         }
305         return eSendReply;
306 }
307
308 eNextState SMTPC_send_data_body(SmtpOutMsg *SendMsg)
309 {
310         StrBuf *Buf;
311         /* If we reach this point, the server is expecting data.*/
312
313         Buf = SendMsg->IO.SendBuf.Buf;
314         SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
315         SendMsg->msgtext = Buf;
316         SendMsg->State ++;
317
318         return eSendMore;
319 }
320
321 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *SendMsg)
322 {
323         StrBuf *Buf;
324
325         Buf = SendMsg->IO.SendBuf.Buf;
326         SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
327         SendMsg->msgtext = Buf;
328
329         StrBufPlain(SendMsg->IO.SendBuf.Buf,
330                     HKEY(".\r\n"));
331
332         return eReadMessage;
333
334 }
335
336 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
337 {
338         AsyncIO *IO = &SendMsg->IO;
339         SMTP_DBG_READ();
340
341         if (!SMTP_IS_STATE('2')) {
342                 if (SMTP_IS_STATE('4'))
343                         SMTP_VERROR(4);
344                 else 
345                         SMTP_VERROR(5);
346         }
347
348         /* We did it! */
349         StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
350                     &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
351                     StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
352         SendMsg->MyQEntry->Status = 2;
353         return eSendReply;
354 }
355
356 eNextState SMTPC_send_QUIT(SmtpOutMsg *SendMsg)
357 {
358         AsyncIO *IO = &SendMsg->IO;
359         StrBufPlain(SendMsg->IO.SendBuf.Buf,
360                     HKEY("QUIT\r\n"));
361
362         SMTP_DBG_SEND();
363         return eReadMessage;
364 }
365
366 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
367 {
368         AsyncIO *IO = &SendMsg->IO;
369         SMTP_DBG_READ();
370
371         EV_syslog(LOG_INFO, "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
372                   SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
373         return eTerminateConnection;
374 }
375
376 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
377 {
378         return eSendReply;
379 }
380
381 eNextState SMTPC_send_dummy(SmtpOutMsg *SendMsg)
382 {
383         return eReadMessage;
384 }
385
386 /*****************************************************************************/
387 /*                     SMTP CLIENT DISPATCHER                                */
388 /*****************************************************************************/
389 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
390         SMTPC_read_greeting,
391         SMTPC_read_EHLO_reply,
392         SMTPC_read_HELO_reply,
393         SMTPC_read_auth_reply,
394         SMTPC_read_FROM_reply,
395         SMTPC_read_RCPT_reply,
396         SMTPC_read_DATAcmd_reply,
397         SMTPC_read_dummy,
398         SMTPC_read_data_body_reply,
399         SMTPC_read_QUIT_reply
400 };
401 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
402         SMTPC_send_dummy, /* we don't send a greeting, the server does... */
403         SMTPC_send_EHLO,
404         STMPC_send_HELO,
405         SMTPC_send_auth,
406         SMTPC_send_FROM,
407         SMTPC_send_RCPT,
408         SMTPC_send_DATAcmd,
409         SMTPC_send_data_body,
410         SMTPC_send_terminate_data_body,
411         SMTPC_send_QUIT
412 };
413
414 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
415
416 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
417         300., /* Greeting... */
418         30., /* EHLO */
419         30., /* HELO */
420         30., /* Auth */
421         30., /* From */
422         90., /* RCPT */
423         30., /* DATA */
424         90., /* DATABody */
425         90., /* end of body... */
426         30.  /* QUIT */
427 };
428 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
429         90., /* Greeting... */
430         30., /* EHLO */
431         30., /* HELO */
432         30., /* Auth */
433         30., /* From */
434         30., /* RCPT */
435         30., /* DATA */
436         90., /* DATABody */
437         900., /* end of body... */
438         30.  /* QUIT */
439 };
440
441 const ConstStr ReadErrors[eMaxSMTPC] = {
442         {HKEY("Connection broken during SMTP conversation")},
443         {HKEY("Connection broken during SMTP EHLO")},
444         {HKEY("Connection broken during SMTP HELO")},
445         {HKEY("Connection broken during SMTP AUTH")},
446         {HKEY("Connection broken during SMTP MAIL FROM")},
447         {HKEY("Connection broken during SMTP RCPT")},
448         {HKEY("Connection broken during SMTP DATA")},
449         {HKEY("Connection broken during SMTP message transmit")},
450         {HKEY("")}/* quit reply, don't care. */
451 };
452
453
454
455
456
457 int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
458 {
459         AsyncIO *IO = &SendMsg->IO;
460         const char *ptr;
461         char buf[1024];
462         int scan_done;
463         int lp, rp;
464         int i;
465
466         EV_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
467
468         if ((SendMsg==NULL) || 
469             (SendMsg->MyQEntry == NULL) || 
470             (StrLength(SendMsg->MyQEntry->Recipient) == 0)) {
471                 return 0;
472         }
473
474         /* Parse out the host portion of the recipient address */
475         process_rfc822_addr(ChrPtr(SendMsg->MyQEntry->Recipient), 
476                             SendMsg->user, 
477                             SendMsg->node, 
478                             SendMsg->name);
479
480         EV_syslog(LOG_DEBUG, "SMTP client[%ld]: Attempting delivery to <%s> @ <%s> (%s)\n",
481                   SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
482         /* If no envelope_from is supplied, extract one from the message */
483         SendMsg->envelope_from = ChrPtr(SendMsg->MyQItem->EnvelopeFrom);
484         if ( (SendMsg->envelope_from == NULL) || 
485              (IsEmptyStr(SendMsg->envelope_from)) ) {
486                 SendMsg->mailfrom[0] = '\0';
487                 scan_done = 0;
488                 ptr = ChrPtr(SendMsg->msgtext);
489                 do {
490                         if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
491                                 scan_done = 1;
492                         }
493                         if (!strncasecmp(buf, "From:", 5)) {
494                                 safestrncpy(SendMsg->mailfrom, &buf[5], sizeof SendMsg->mailfrom);
495                                 striplt(SendMsg->mailfrom);
496                                 for (i=0; SendMsg->mailfrom[i]; ++i) {
497                                         if (!isprint(SendMsg->mailfrom[i])) {
498                                                 strcpy(&SendMsg->mailfrom[i], &SendMsg->mailfrom[i+1]);
499                                                 i=0;
500                                         }
501                                 }
502         
503                                 /* Strip out parenthesized names */
504                                 lp = (-1);
505                                 rp = (-1);
506                                 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
507                                         if (SendMsg->mailfrom[i] == '(') lp = i;
508                                         if (SendMsg->mailfrom[i] == ')') rp = i;
509                                 }
510                                 if ((lp>0)&&(rp>lp)) {
511                                         strcpy(&SendMsg->mailfrom[lp-1], &SendMsg->mailfrom[rp+1]);
512                                 }
513         
514                                 /* Prefer brokketized names */
515                                 lp = (-1);
516                                 rp = (-1);
517                                 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
518                                         if (SendMsg->mailfrom[i] == '<') lp = i;
519                                         if (SendMsg->mailfrom[i] == '>') rp = i;
520                                 }
521                                 if ( (lp>=0) && (rp>lp) ) {
522                                         SendMsg->mailfrom[rp] = 0;
523                                         memmove(SendMsg->mailfrom, 
524                                                 &SendMsg->mailfrom[lp + 1], 
525                                                 rp - lp);
526                                 }
527         
528                                 scan_done = 1;
529                         }
530                 } while (scan_done == 0);
531                 if (IsEmptyStr(SendMsg->mailfrom)) strcpy(SendMsg->mailfrom, "someone@somewhere.org");
532                 stripallbut(SendMsg->mailfrom, '<', '>');
533                 SendMsg->envelope_from = SendMsg->mailfrom;
534         }
535
536         return 1;
537 }