2 * This module is an SMTP and ESMTP implementation for the Citadel system.
3 * It is compliant with all of the following:
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
20 * The VRFY and EXPN commands have been removed from this implementation
21 * because nobody uses these commands anymore, except for spammers.
23 * Copyright (c) 1998-2009 by the citadel.org team
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.
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.
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
49 #include <sys/types.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
79 #include "internet_addressing.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
86 #include "ctdl_module.h"
88 #include "smtp_util.h"
89 #include "event_client.h"
90 #include "smtpqueue.h"
91 #include "smtp_clienthandlers.h"
94 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {\
95 SendMsg->MyQEntry->Status = WHICH_ERR; \
96 StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); \
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); \
108 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
110 #define SMTP_DBG_SEND() EVS_syslog(LOG_DEBUG, "SMTP: > %s\n", ChrPtr(SendMsg->IO.SendBuf.Buf))
111 #define SMTP_DBG_READ() EVS_syslog(LOG_DEBUG, "SMTP: < %s\n", ChrPtr(SendMsg->IO.IOBuf))
114 /*****************************************************************************/
115 /* SMTP CLIENT STATE CALLBACKS */
116 /*****************************************************************************/
117 eNextState SMTPC_read_greeting(SmtpOutMsg *SendMsg)
119 /* Process the SMTP greeting from the server */
120 AsyncIO *IO = &SendMsg->IO;
123 if (!SMTP_IS_STATE('2')) {
124 if (SMTP_IS_STATE('4'))
132 eNextState SMTPC_send_EHLO(SmtpOutMsg *SendMsg)
134 AsyncIO *IO = &SendMsg->IO;
135 /* At this point we know we are talking to a real SMTP server */
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);
145 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *SendMsg)
147 AsyncIO *IO = &SendMsg->IO;
150 if (SMTP_IS_STATE('2')) {
153 if ((SendMsg->pCurrRelay == NULL) ||
154 (SendMsg->pCurrRelay->User == NULL))
155 SendMsg->State ++; /* Skip auth... */
157 /* else we fall back to 'helo' */
161 eNextState STMPC_send_HELO(SmtpOutMsg *SendMsg)
163 AsyncIO *IO = &SendMsg->IO;
164 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
165 "HELO %s\r\n", config.c_fqdn);
171 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *SendMsg)
173 AsyncIO *IO = &SendMsg->IO;
176 if (!SMTP_IS_STATE('2')) {
177 if (SMTP_IS_STATE('4'))
182 if ((SendMsg->pCurrRelay == NULL) ||
183 (SendMsg->pCurrRelay->User == NULL))
184 SendMsg->State ++; /* Skip auth... */
188 eNextState SMTPC_send_auth(SmtpOutMsg *SendMsg)
190 AsyncIO *IO = &SendMsg->IO;
194 if ((SendMsg->pCurrRelay == NULL) ||
195 (SendMsg->pCurrRelay->User == NULL))
196 SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
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);
213 eNextState SMTPC_read_auth_reply(SmtpOutMsg *SendMsg)
215 AsyncIO *IO = &SendMsg->IO;
216 /* Do an AUTH command if necessary */
220 if (!SMTP_IS_STATE('2')) {
221 if (SMTP_IS_STATE('4'))
229 eNextState SMTPC_send_FROM(SmtpOutMsg *SendMsg)
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);
241 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *SendMsg)
243 AsyncIO *IO = &SendMsg->IO;
246 if (!SMTP_IS_STATE('2')) {
247 if (SMTP_IS_STATE('4'))
256 eNextState SMTPC_send_RCPT(SmtpOutMsg *SendMsg)
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",
269 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *SendMsg)
271 AsyncIO *IO = &SendMsg->IO;
274 if (!SMTP_IS_STATE('2')) {
275 if (SMTP_IS_STATE('4'))
283 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *SendMsg)
285 AsyncIO *IO = &SendMsg->IO;
286 /* RCPT succeeded, now try the DATA command */
287 StrBufPlain(SendMsg->IO.SendBuf.Buf,
294 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *SendMsg)
296 AsyncIO *IO = &SendMsg->IO;
299 if (!SMTP_IS_STATE('3')) {
300 if (SMTP_IS_STATE('4'))
308 eNextState SMTPC_send_data_body(SmtpOutMsg *SendMsg)
311 /* If we reach this point, the server is expecting data.*/
313 Buf = SendMsg->IO.SendBuf.Buf;
314 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
315 SendMsg->msgtext = Buf;
321 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *SendMsg)
325 Buf = SendMsg->IO.SendBuf.Buf;
326 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
327 SendMsg->msgtext = Buf;
329 StrBufPlain(SendMsg->IO.SendBuf.Buf,
336 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
338 AsyncIO *IO = &SendMsg->IO;
341 if (!SMTP_IS_STATE('2')) {
342 if (SMTP_IS_STATE('4'))
349 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
350 &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
351 StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
352 SendMsg->MyQEntry->Status = 2;
356 eNextState SMTPC_send_QUIT(SmtpOutMsg *SendMsg)
358 AsyncIO *IO = &SendMsg->IO;
359 StrBufPlain(SendMsg->IO.SendBuf.Buf,
366 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
368 AsyncIO *IO = &SendMsg->IO;
371 EVS_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;
376 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
381 eNextState SMTPC_send_dummy(SmtpOutMsg *SendMsg)
386 /*****************************************************************************/
387 /* SMTP CLIENT DISPATCHER */
388 /*****************************************************************************/
389 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
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,
398 SMTPC_read_data_body_reply,
399 SMTPC_read_QUIT_reply
401 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
402 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
409 SMTPC_send_data_body,
410 SMTPC_send_terminate_data_body,
414 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
416 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
417 300., /* Greeting... */
425 90., /* end of body... */
428 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
429 90., /* Greeting... */
437 900., /* end of body... */
441 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
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 {HKEY("")},/* quit reply, don't care. */
452 {HKEY("")}/* quit reply, don't care. */
459 int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
461 AsyncIO *IO = &SendMsg->IO;
468 EVS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
470 if ((SendMsg==NULL) ||
471 (SendMsg->MyQEntry == NULL) ||
472 (StrLength(SendMsg->MyQEntry->Recipient) == 0)) {
476 /* Parse out the host portion of the recipient address */
477 process_rfc822_addr(ChrPtr(SendMsg->MyQEntry->Recipient),
482 EVS_syslog(LOG_DEBUG, "SMTP client[%ld]: Attempting delivery to <%s> @ <%s> (%s)\n",
483 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
484 /* If no envelope_from is supplied, extract one from the message */
485 SendMsg->envelope_from = ChrPtr(SendMsg->MyQItem->EnvelopeFrom);
486 if ( (SendMsg->envelope_from == NULL) ||
487 (IsEmptyStr(SendMsg->envelope_from)) ) {
488 SendMsg->mailfrom[0] = '\0';
490 ptr = ChrPtr(SendMsg->msgtext);
492 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
495 if (!strncasecmp(buf, "From:", 5)) {
496 safestrncpy(SendMsg->mailfrom, &buf[5], sizeof SendMsg->mailfrom);
497 striplt(SendMsg->mailfrom);
498 for (i=0; SendMsg->mailfrom[i]; ++i) {
499 if (!isprint(SendMsg->mailfrom[i])) {
500 strcpy(&SendMsg->mailfrom[i], &SendMsg->mailfrom[i+1]);
505 /* Strip out parenthesized names */
508 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
509 if (SendMsg->mailfrom[i] == '(') lp = i;
510 if (SendMsg->mailfrom[i] == ')') rp = i;
512 if ((lp>0)&&(rp>lp)) {
513 strcpy(&SendMsg->mailfrom[lp-1], &SendMsg->mailfrom[rp+1]);
516 /* Prefer brokketized names */
519 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
520 if (SendMsg->mailfrom[i] == '<') lp = i;
521 if (SendMsg->mailfrom[i] == '>') rp = i;
523 if ( (lp>=0) && (rp>lp) ) {
524 SendMsg->mailfrom[rp] = 0;
525 memmove(SendMsg->mailfrom,
526 &SendMsg->mailfrom[lp + 1],
532 } while (scan_done == 0);
533 if (IsEmptyStr(SendMsg->mailfrom)) strcpy(SendMsg->mailfrom, "someone@somewhere.org");
534 stripallbut(SendMsg->mailfrom, '<', '>');
535 SendMsg->envelope_from = SendMsg->mailfrom;