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() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.SendBuf.Buf))
111 #define SMTP_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->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 */
122 if (!SMTP_IS_STATE('2')) {
123 if (SMTP_IS_STATE('4'))
131 eNextState SMTPC_send_EHLO(SmtpOutMsg *SendMsg)
133 /* At this point we know we are talking to a real SMTP server */
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);
143 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *SendMsg)
147 if (SMTP_IS_STATE('2')) {
150 if ((SendMsg->pCurrRelay == NULL) ||
151 (SendMsg->pCurrRelay->User == NULL))
152 SendMsg->State ++; /* Skip auth... */
154 /* else we fall back to 'helo' */
158 eNextState STMPC_send_HELO(SmtpOutMsg *SendMsg)
160 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
161 "HELO %s\r\n", config.c_fqdn);
167 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *SendMsg)
171 if (!SMTP_IS_STATE('2')) {
172 if (SMTP_IS_STATE('4'))
177 if ((SendMsg->pCurrRelay == NULL) ||
178 (SendMsg->pCurrRelay->User == NULL))
179 SendMsg->State ++; /* Skip auth... */
183 eNextState SMTPC_send_auth(SmtpOutMsg *SendMsg)
188 if ((SendMsg->pCurrRelay == NULL) ||
189 (SendMsg->pCurrRelay->User == NULL))
190 SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
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);
207 eNextState SMTPC_read_auth_reply(SmtpOutMsg *SendMsg)
209 /* Do an AUTH command if necessary */
213 if (!SMTP_IS_STATE('2')) {
214 if (SMTP_IS_STATE('4'))
222 eNextState SMTPC_send_FROM(SmtpOutMsg *SendMsg)
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);
233 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *SendMsg)
237 if (!SMTP_IS_STATE('2')) {
238 if (SMTP_IS_STATE('4'))
247 eNextState SMTPC_send_RCPT(SmtpOutMsg *SendMsg)
249 /* MAIL succeeded, now try the RCPT To: command */
250 StrBufPrintf(SendMsg->IO.SendBuf.Buf,
251 "RCPT TO:<%s@%s>\r\n",
259 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *SendMsg)
263 if (!SMTP_IS_STATE('2')) {
264 if (SMTP_IS_STATE('4'))
272 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *SendMsg)
274 /* RCPT succeeded, now try the DATA command */
275 StrBufPlain(SendMsg->IO.SendBuf.Buf,
282 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *SendMsg)
286 if (!SMTP_IS_STATE('3')) {
287 if (SMTP_IS_STATE('4'))
295 eNextState SMTPC_send_data_body(SmtpOutMsg *SendMsg)
298 /* If we reach this point, the server is expecting data.*/
300 Buf = SendMsg->IO.SendBuf.Buf;
301 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
302 SendMsg->msgtext = Buf;
308 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *SendMsg)
312 Buf = SendMsg->IO.SendBuf.Buf;
313 SendMsg->IO.SendBuf.Buf = SendMsg->msgtext;
314 SendMsg->msgtext = Buf;
316 StrBufPlain(SendMsg->IO.SendBuf.Buf,
323 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
327 if (!SMTP_IS_STATE('2')) {
328 if (SMTP_IS_STATE('4'))
335 StrBufPlain(SendMsg->MyQEntry->StatusMessage,
336 &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
337 StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
338 SendMsg->MyQEntry->Status = 2;
342 eNextState SMTPC_send_QUIT(SmtpOutMsg *SendMsg)
344 StrBufPlain(SendMsg->IO.SendBuf.Buf,
351 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
355 CtdlLogPrintf(CTDL_INFO, "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
356 SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
357 return eTerminateConnection;
360 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
365 eNextState SMTPC_send_dummy(SmtpOutMsg *SendMsg)
370 /*****************************************************************************/
371 /* SMTP CLIENT DISPATCHER */
372 /*****************************************************************************/
373 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
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,
382 SMTPC_read_data_body_reply,
383 SMTPC_read_QUIT_reply
385 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
386 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
393 SMTPC_send_data_body,
394 SMTPC_send_terminate_data_body,
398 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
400 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
401 300., /* Greeting... */
409 90., /* end of body... */
412 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
413 90., /* Greeting... */
421 900., /* end of body... */
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. */
441 int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
449 CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
451 if ((SendMsg==NULL) ||
452 (SendMsg->MyQEntry == NULL) ||
453 (StrLength(SendMsg->MyQEntry->Recipient) == 0)) {
457 /* Parse out the host portion of the recipient address */
458 process_rfc822_addr(ChrPtr(SendMsg->MyQEntry->Recipient),
463 CtdlLogPrintf(CTDL_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';
471 ptr = ChrPtr(SendMsg->msgtext);
473 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
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]);
486 /* Strip out parenthesized names */
489 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
490 if (SendMsg->mailfrom[i] == '(') lp = i;
491 if (SendMsg->mailfrom[i] == ')') rp = i;
493 if ((lp>0)&&(rp>lp)) {
494 strcpy(&SendMsg->mailfrom[lp-1], &SendMsg->mailfrom[rp+1]);
497 /* Prefer brokketized names */
500 for (i=0; !IsEmptyStr(SendMsg->mailfrom + i); ++i) {
501 if (SendMsg->mailfrom[i] == '<') lp = i;
502 if (SendMsg->mailfrom[i] == '>') rp = i;
504 if ( (lp>=0) && (rp>lp) ) {
505 SendMsg->mailfrom[rp] = 0;
506 memmove(SendMsg->mailfrom,
507 &SendMsg->mailfrom[lp + 1],
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;