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 open source software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 3.
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.
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, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
111 #define SMTP_DBG_READ() \
112 EVS_syslog(LOG_DEBUG, "< %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;
376 EVS_syslog(LOG_DEBUG,
377 "delivery to <%s> @ <%s> (%s) succeeded\n",
382 return eTerminateConnection;
385 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
390 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
395 /*****************************************************************************/
396 /* SMTP CLIENT DISPATCHER */
397 /*****************************************************************************/
398 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
400 SMTPC_read_EHLO_reply,
401 SMTPC_read_HELO_reply,
402 SMTPC_read_auth_reply,
403 SMTPC_read_FROM_reply,
404 SMTPC_read_RCPT_reply,
405 SMTPC_read_DATAcmd_reply,
407 SMTPC_read_data_body_reply,
408 SMTPC_read_QUIT_reply
410 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
411 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
418 SMTPC_send_data_body,
419 SMTPC_send_terminate_data_body,
423 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
425 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
426 300., /* Greeting... */
434 90., /* end of body... */
437 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
438 90., /* Greeting... */
446 900., /* end of body... */
450 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
451 {HKEY("Connection broken during SMTP conversation")},
452 {HKEY("Connection broken during SMTP EHLO")},
453 {HKEY("Connection broken during SMTP HELO")},
454 {HKEY("Connection broken during SMTP AUTH")},
455 {HKEY("Connection broken during SMTP MAIL FROM")},
456 {HKEY("Connection broken during SMTP RCPT")},
457 {HKEY("Connection broken during SMTP DATA")},
458 {HKEY("Connection broken during SMTP message transmit")},
459 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
460 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
461 {HKEY("")}/* quit reply, don't care. */
468 int smtp_resolve_recipients(SmtpOutMsg *Msg)
470 AsyncIO *IO = &Msg->IO;
477 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
480 (Msg->MyQEntry == NULL) ||
481 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
485 /* Parse out the host portion of the recipient address */
486 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
491 EVNCS_syslog(LOG_DEBUG,
492 "Attempting delivery to <%s> @ <%s> (%s)\n",
497 /* If no envelope_from is supplied, extract one from the message */
498 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
499 if ( (Msg->envelope_from == NULL) ||
500 (IsEmptyStr(Msg->envelope_from)) ) {
501 Msg->mailfrom[0] = '\0';
503 ptr = ChrPtr(Msg->msgtext);
505 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
509 if (!strncasecmp(buf, "From:", 5))
511 safestrncpy(Msg->mailfrom,
513 sizeof Msg->mailfrom);
515 striplt(Msg->mailfrom);
516 for (i=0; Msg->mailfrom[i]; ++i) {
517 if (!isprint(Msg->mailfrom[i]))
519 strcpy(&Msg->mailfrom[i],
520 &Msg->mailfrom[i+1]);
525 /* Strip out parenthesized names */
529 !IsEmptyStr(Msg->mailfrom + i);
532 if (Msg->mailfrom[i] == '(') lp = i;
533 if (Msg->mailfrom[i] == ')') rp = i;
537 strcpy(&Msg->mailfrom[lp-1],
538 &Msg->mailfrom[rp+1]);
541 /* Prefer brokketized names */
545 !IsEmptyStr(Msg->mailfrom + i);
548 if (Msg->mailfrom[i] == '<') lp = i;
549 if (Msg->mailfrom[i] == '>') rp = i;
551 if ( (lp>=0) && (rp>lp) ) {
552 Msg->mailfrom[rp] = 0;
553 memmove(Msg->mailfrom,
554 &Msg->mailfrom[lp + 1],
560 } while (scan_done == 0);
561 if (IsEmptyStr(Msg->mailfrom))
562 strcpy(Msg->mailfrom, "someone@somewhere.org");
564 stripallbut(Msg->mailfrom, '<', '>');
565 Msg->envelope_from = Msg->mailfrom;