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