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 * Copyright (c) 1998-2012 by the citadel.org team
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 3 of the License, or
25 * (at your option) any later version.
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.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46 #include <sys/types.h>
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
54 # include <sys/time.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <libcitadel.h>
69 #include "citserver.h"
76 #include "internet_addressing.h"
79 #include "clientsocket.h"
80 #include "locate_host.h"
81 #include "citadel_dirs.h"
83 #include "ctdl_module.h"
85 #include "smtp_util.h"
86 #include "event_client.h"
87 #include "smtpqueue.h"
88 #include "smtp_clienthandlers.h"
91 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do { \
92 Msg->MyQEntry->Status = WHICH_ERR; \
93 StrBufAppendBufPlain(Msg->MyQEntry->StatusMessage, \
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); \
106 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)
108 #define SMTP_DBG_SEND() \
109 EVS_syslog(LOG_DEBUG, "SMTP: > %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
111 #define SMTP_DBG_READ() \
112 EVS_syslog(LOG_DEBUG, "SMTP: < %s\n", ChrPtr(Msg->IO.IOBuf))
115 /*****************************************************************************/
116 /* SMTP CLIENT STATE CALLBACKS */
117 /*****************************************************************************/
118 eNextState SMTPC_read_greeting(SmtpOutMsg *Msg)
120 /* Process the SMTP greeting from the server */
121 AsyncIO *IO = &Msg->IO;
124 if (!SMTP_IS_STATE('2')) {
125 if (SMTP_IS_STATE('4'))
133 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
135 AsyncIO *IO = &Msg->IO;
136 /* At this point we know we are talking to a real SMTP server */
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);
146 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
148 AsyncIO *IO = &Msg->IO;
151 if (SMTP_IS_STATE('2')) {
154 if ((Msg->pCurrRelay == NULL) ||
155 (Msg->pCurrRelay->User == NULL))
156 Msg->State ++; /* Skip auth... */
158 /* else we fall back to 'helo' */
162 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
164 AsyncIO *IO = &Msg->IO;
165 StrBufPrintf(Msg->IO.SendBuf.Buf,
166 "HELO %s\r\n", config.c_fqdn);
172 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
174 AsyncIO *IO = &Msg->IO;
177 if (!SMTP_IS_STATE('2'))
179 if (SMTP_IS_STATE('4'))
184 if ((Msg->pCurrRelay == NULL) ||
185 (Msg->pCurrRelay->User == NULL))
186 Msg->State ++; /* Skip auth... */
191 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
193 AsyncIO *IO = &Msg->IO;
197 if ((Msg->pCurrRelay == NULL) ||
198 (Msg->pCurrRelay->User == NULL))
199 Msg->State ++; /* Skip auth, shouldn't even come here!... */
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);
207 CtdlEncodeBase64(encoded, buf,
208 strlen(Msg->pCurrRelay->User) * 2 +
209 strlen(Msg->pCurrRelay->Pass) + 2, 0);
211 StrBufPrintf(Msg->IO.SendBuf.Buf,
212 "AUTH PLAIN %s\r\n", encoded);
218 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
220 AsyncIO *IO = &Msg->IO;
221 /* Do an AUTH command if necessary */
225 if (!SMTP_IS_STATE('2')) {
226 if (SMTP_IS_STATE('4'))
234 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
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",
246 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
248 AsyncIO *IO = &Msg->IO;
251 if (!SMTP_IS_STATE('2')) {
252 if (SMTP_IS_STATE('4'))
261 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
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",
274 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
276 AsyncIO *IO = &Msg->IO;
279 if (!SMTP_IS_STATE('2')) {
280 if (SMTP_IS_STATE('4'))
288 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
290 AsyncIO *IO = &Msg->IO;
291 /* RCPT succeeded, now try the DATA command */
292 StrBufPlain(Msg->IO.SendBuf.Buf,
299 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
301 AsyncIO *IO = &Msg->IO;
304 if (!SMTP_IS_STATE('3')) {
305 if (SMTP_IS_STATE('4'))
313 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
316 /* If we reach this point, the server is expecting data.*/
318 Buf = Msg->IO.SendBuf.Buf;
319 Msg->IO.SendBuf.Buf = Msg->msgtext;
326 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
330 Buf = Msg->IO.SendBuf.Buf;
331 Msg->IO.SendBuf.Buf = Msg->msgtext;
334 StrBufPlain(Msg->IO.SendBuf.Buf,
341 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
343 AsyncIO *IO = &Msg->IO;
346 if (!SMTP_IS_STATE('2')) {
347 if (SMTP_IS_STATE('4'))
354 StrBufPlain(Msg->MyQEntry->StatusMessage,
355 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
356 StrLength(Msg->IO.RecvBuf.Buf) - 4);
357 Msg->MyQEntry->Status = 2;
361 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
363 AsyncIO *IO = &Msg->IO;
364 StrBufPlain(Msg->IO.SendBuf.Buf,
371 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
373 AsyncIO *IO = &Msg->IO;
377 "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
383 return eTerminateConnection;
386 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
391 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
396 /*****************************************************************************/
397 /* SMTP CLIENT DISPATCHER */
398 /*****************************************************************************/
399 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
401 SMTPC_read_EHLO_reply,
402 SMTPC_read_HELO_reply,
403 SMTPC_read_auth_reply,
404 SMTPC_read_FROM_reply,
405 SMTPC_read_RCPT_reply,
406 SMTPC_read_DATAcmd_reply,
408 SMTPC_read_data_body_reply,
409 SMTPC_read_QUIT_reply
411 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
412 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
419 SMTPC_send_data_body,
420 SMTPC_send_terminate_data_body,
424 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
426 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
427 300., /* Greeting... */
435 90., /* end of body... */
438 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
439 90., /* Greeting... */
447 900., /* end of body... */
451 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
452 {HKEY("Connection broken during SMTP conversation")},
453 {HKEY("Connection broken during SMTP EHLO")},
454 {HKEY("Connection broken during SMTP HELO")},
455 {HKEY("Connection broken during SMTP AUTH")},
456 {HKEY("Connection broken during SMTP MAIL FROM")},
457 {HKEY("Connection broken during SMTP RCPT")},
458 {HKEY("Connection broken during SMTP DATA")},
459 {HKEY("Connection broken during SMTP message transmit")},
460 {HKEY("")},/* quit reply, don't care. */
461 {HKEY("")},/* quit reply, don't care. */
462 {HKEY("")}/* quit reply, don't care. */
469 int smtp_resolve_recipients(SmtpOutMsg *Msg)
471 AsyncIO *IO = &Msg->IO;
478 EVNCS_syslog(LOG_DEBUG, "SMTP: %s\n", __FUNCTION__);
481 (Msg->MyQEntry == NULL) ||
482 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
486 /* Parse out the host portion of the recipient address */
487 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
492 EVNCS_syslog(LOG_DEBUG,
493 "SMTP client[%ld]: Attempting delivery to "
494 "<%s> @ <%s> (%s)\n",
500 /* If no envelope_from is supplied, extract one from the message */
501 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
502 if ( (Msg->envelope_from == NULL) ||
503 (IsEmptyStr(Msg->envelope_from)) ) {
504 Msg->mailfrom[0] = '\0';
506 ptr = ChrPtr(Msg->msgtext);
508 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
512 if (!strncasecmp(buf, "From:", 5))
514 safestrncpy(Msg->mailfrom,
516 sizeof Msg->mailfrom);
518 striplt(Msg->mailfrom);
519 for (i=0; Msg->mailfrom[i]; ++i) {
520 if (!isprint(Msg->mailfrom[i]))
522 strcpy(&Msg->mailfrom[i],
523 &Msg->mailfrom[i+1]);
528 /* Strip out parenthesized names */
532 !IsEmptyStr(Msg->mailfrom + i);
535 if (Msg->mailfrom[i] == '(') lp = i;
536 if (Msg->mailfrom[i] == ')') rp = i;
540 strcpy(&Msg->mailfrom[lp-1],
541 &Msg->mailfrom[rp+1]);
544 /* Prefer brokketized names */
548 !IsEmptyStr(Msg->mailfrom + i);
551 if (Msg->mailfrom[i] == '<') lp = i;
552 if (Msg->mailfrom[i] == '>') rp = i;
554 if ( (lp>=0) && (rp>lp) ) {
555 Msg->mailfrom[rp] = 0;
556 memmove(Msg->mailfrom,
557 &Msg->mailfrom[lp + 1],
563 } while (scan_done == 0);
564 if (IsEmptyStr(Msg->mailfrom))
565 strcpy(Msg->mailfrom, "someone@somewhere.org");
567 stripallbut(Msg->mailfrom, '<', '>');
568 Msg->envelope_from = Msg->mailfrom;