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, \
95 StrBufTrim(Msg->MyQEntry->StatusMessage); \
99 #define SMTP_VERROR(WHICH_ERR) do { \
100 Msg->MyQEntry->Status = WHICH_ERR; \
101 StrBufPlain(Msg->MyQEntry->StatusMessage, \
102 ChrPtr(Msg->IO.IOBuf) + 4, \
103 StrLength(Msg->IO.IOBuf) - 4); \
104 StrBufTrim(Msg->MyQEntry->StatusMessage); \
108 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)
110 #define SMTP_DBG_SEND() \
111 EVS_syslog(LOG_DEBUG, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
113 #define SMTP_DBG_READ() \
114 EVS_syslog(LOG_DEBUG, "< %s\n", ChrPtr(Msg->IO.IOBuf))
117 /*****************************************************************************/
118 /* SMTP CLIENT STATE CALLBACKS */
119 /*****************************************************************************/
120 eNextState SMTPC_read_greeting(SmtpOutMsg *Msg)
122 /* Process the SMTP greeting from the server */
123 AsyncIO *IO = &Msg->IO;
126 if (!SMTP_IS_STATE('2')) {
127 if (SMTP_IS_STATE('4'))
135 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
137 AsyncIO *IO = &Msg->IO;
138 /* At this point we know we are talking to a real SMTP server */
140 /* Do a EHLO command. If it fails, try the HELO command. */
141 StrBufPrintf(Msg->IO.SendBuf.Buf,
142 "EHLO %s\r\n", config.c_fqdn);
148 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
150 AsyncIO *IO = &Msg->IO;
153 if (SMTP_IS_STATE('2')) {
156 if ((Msg->pCurrRelay == NULL) ||
157 (Msg->pCurrRelay->User == NULL))
158 Msg->State ++; /* Skip auth... */
160 /* else we fall back to 'helo' */
164 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
166 AsyncIO *IO = &Msg->IO;
167 StrBufPrintf(Msg->IO.SendBuf.Buf,
168 "HELO %s\r\n", config.c_fqdn);
174 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
176 AsyncIO *IO = &Msg->IO;
179 if (!SMTP_IS_STATE('2'))
181 if (SMTP_IS_STATE('4'))
186 if ((Msg->pCurrRelay == NULL) ||
187 (Msg->pCurrRelay->User == NULL))
188 Msg->State ++; /* Skip auth... */
193 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
195 AsyncIO *IO = &Msg->IO;
199 if ((Msg->pCurrRelay == NULL) ||
200 (Msg->pCurrRelay->User == NULL))
201 Msg->State ++; /* Skip auth, shouldn't even come here!... */
203 /* Do an AUTH command if necessary */
204 sprintf(buf, "%s%c%s%c%s",
205 Msg->pCurrRelay->User, '\0',
206 Msg->pCurrRelay->User, '\0',
207 Msg->pCurrRelay->Pass);
209 CtdlEncodeBase64(encoded, buf,
210 strlen(Msg->pCurrRelay->User) * 2 +
211 strlen(Msg->pCurrRelay->Pass) + 2, 0);
213 StrBufPrintf(Msg->IO.SendBuf.Buf,
214 "AUTH PLAIN %s\r\n", encoded);
220 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
222 AsyncIO *IO = &Msg->IO;
223 /* Do an AUTH command if necessary */
227 if (!SMTP_IS_STATE('2')) {
228 if (SMTP_IS_STATE('4'))
236 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
238 AsyncIO *IO = &Msg->IO;
239 /* previous command succeeded, now try the MAIL FROM: command */
240 StrBufPrintf(Msg->IO.SendBuf.Buf,
241 "MAIL FROM:<%s>\r\n",
248 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
250 AsyncIO *IO = &Msg->IO;
253 if (!SMTP_IS_STATE('2')) {
254 if (SMTP_IS_STATE('4'))
263 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
265 AsyncIO *IO = &Msg->IO;
266 /* MAIL succeeded, now try the RCPT To: command */
267 StrBufPrintf(Msg->IO.SendBuf.Buf,
268 "RCPT TO:<%s@%s>\r\n",
276 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
278 AsyncIO *IO = &Msg->IO;
281 if (!SMTP_IS_STATE('2')) {
282 if (SMTP_IS_STATE('4'))
290 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
292 AsyncIO *IO = &Msg->IO;
293 /* RCPT succeeded, now try the DATA command */
294 StrBufPlain(Msg->IO.SendBuf.Buf,
301 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
303 AsyncIO *IO = &Msg->IO;
306 if (!SMTP_IS_STATE('3')) {
307 if (SMTP_IS_STATE('4'))
315 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
318 /* If we reach this point, the server is expecting data.*/
320 Buf = Msg->IO.SendBuf.Buf;
321 Msg->IO.SendBuf.Buf = Msg->msgtext;
328 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
332 Buf = Msg->IO.SendBuf.Buf;
333 Msg->IO.SendBuf.Buf = Msg->msgtext;
336 StrBufPlain(Msg->IO.SendBuf.Buf,
343 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
345 AsyncIO *IO = &Msg->IO;
348 if (!SMTP_IS_STATE('2')) {
349 if (SMTP_IS_STATE('4'))
356 StrBufPlain(Msg->MyQEntry->StatusMessage,
357 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
358 StrLength(Msg->IO.RecvBuf.Buf) - 4);
359 Msg->MyQEntry->Status = 2;
363 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
365 AsyncIO *IO = &Msg->IO;
366 StrBufPlain(Msg->IO.SendBuf.Buf,
373 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
375 AsyncIO *IO = &Msg->IO;
378 EVS_syslog(LOG_DEBUG,
379 "delivery to <%s> @ <%s> (%s) succeeded\n",
384 return eTerminateConnection;
387 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
392 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
397 /*****************************************************************************/
398 /* SMTP CLIENT DISPATCHER */
399 /*****************************************************************************/
400 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
402 SMTPC_read_EHLO_reply,
403 SMTPC_read_HELO_reply,
404 SMTPC_read_auth_reply,
405 SMTPC_read_FROM_reply,
406 SMTPC_read_RCPT_reply,
407 SMTPC_read_DATAcmd_reply,
409 SMTPC_read_data_body_reply,
410 SMTPC_read_QUIT_reply
412 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
413 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
420 SMTPC_send_data_body,
421 SMTPC_send_terminate_data_body,
425 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
427 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
428 300., /* Greeting... */
436 90., /* end of body... */
439 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
440 90., /* Greeting... */
448 900., /* end of body... */
452 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
453 {HKEY("Connection broken during SMTP conversation")},
454 {HKEY("Connection broken during SMTP EHLO")},
455 {HKEY("Connection broken during SMTP HELO")},
456 {HKEY("Connection broken during SMTP AUTH")},
457 {HKEY("Connection broken during SMTP MAIL FROM")},
458 {HKEY("Connection broken during SMTP RCPT")},
459 {HKEY("Connection broken during SMTP DATA")},
460 {HKEY("Connection broken during SMTP message transmit")},
461 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
462 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
463 {HKEY("")}/* quit reply, don't care. */
470 int smtp_resolve_recipients(SmtpOutMsg *Msg)
472 AsyncIO *IO = &Msg->IO;
479 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
482 (Msg->MyQEntry == NULL) ||
483 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
487 /* Parse out the host portion of the recipient address */
488 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
493 EVNCS_syslog(LOG_DEBUG,
494 "Attempting delivery to <%s> @ <%s> (%s)\n",
499 /* If no envelope_from is supplied, extract one from the message */
500 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
501 if ( (Msg->envelope_from == NULL) ||
502 (IsEmptyStr(Msg->envelope_from)) ) {
503 Msg->mailfrom[0] = '\0';
505 ptr = ChrPtr(Msg->msgtext);
507 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
511 if (!strncasecmp(buf, "From:", 5))
513 safestrncpy(Msg->mailfrom,
515 sizeof Msg->mailfrom);
517 striplt(Msg->mailfrom);
518 for (i=0; Msg->mailfrom[i]; ++i) {
519 if (!isprint(Msg->mailfrom[i]))
521 strcpy(&Msg->mailfrom[i],
522 &Msg->mailfrom[i+1]);
527 /* Strip out parenthesized names */
531 !IsEmptyStr(Msg->mailfrom + i);
534 if (Msg->mailfrom[i] == '(') lp = i;
535 if (Msg->mailfrom[i] == ')') rp = i;
539 strcpy(&Msg->mailfrom[lp-1],
540 &Msg->mailfrom[rp+1]);
543 /* Prefer brokketized names */
547 !IsEmptyStr(Msg->mailfrom + i);
550 if (Msg->mailfrom[i] == '<') lp = i;
551 if (Msg->mailfrom[i] == '>') rp = i;
553 if ( (lp>=0) && (rp>lp) ) {
554 Msg->mailfrom[rp] = 0;
555 memmove(Msg->mailfrom,
556 &Msg->mailfrom[lp + 1],
562 } while (scan_done == 0);
563 if (IsEmptyStr(Msg->mailfrom))
564 strcpy(Msg->mailfrom, "someone@somewhere.org");
566 stripallbut(Msg->mailfrom, '<', '>');
567 Msg->envelope_from = Msg->mailfrom;