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;
125 SetSMTPState(IO, eSTMPsmtp);
127 if (!SMTP_IS_STATE('2')) {
128 if (SMTP_IS_STATE('4'))
136 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
138 AsyncIO *IO = &Msg->IO;
139 /* At this point we know we are talking to a real SMTP server */
141 /* Do a EHLO command. If it fails, try the HELO command. */
142 StrBufPrintf(Msg->IO.SendBuf.Buf,
143 "EHLO %s\r\n", config.c_fqdn);
149 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
151 AsyncIO *IO = &Msg->IO;
154 if (SMTP_IS_STATE('2')) {
157 if ((Msg->pCurrRelay == NULL) ||
158 (Msg->pCurrRelay->User == NULL))
159 Msg->State ++; /* Skip auth... */
161 /* else we fall back to 'helo' */
165 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
167 AsyncIO *IO = &Msg->IO;
168 StrBufPrintf(Msg->IO.SendBuf.Buf,
169 "HELO %s\r\n", config.c_fqdn);
175 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
177 AsyncIO *IO = &Msg->IO;
180 if (!SMTP_IS_STATE('2'))
182 if (SMTP_IS_STATE('4'))
187 if ((Msg->pCurrRelay == NULL) ||
188 (Msg->pCurrRelay->User == NULL))
189 Msg->State ++; /* Skip auth... */
194 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
196 AsyncIO *IO = &Msg->IO;
200 if ((Msg->pCurrRelay == NULL) ||
201 (Msg->pCurrRelay->User == NULL))
202 Msg->State ++; /* Skip auth, shouldn't even come here!... */
204 /* Do an AUTH command if necessary */
205 sprintf(buf, "%s%c%s%c%s",
206 Msg->pCurrRelay->User, '\0',
207 Msg->pCurrRelay->User, '\0',
208 Msg->pCurrRelay->Pass);
210 CtdlEncodeBase64(encoded, buf,
211 strlen(Msg->pCurrRelay->User) * 2 +
212 strlen(Msg->pCurrRelay->Pass) + 2, 0);
214 StrBufPrintf(Msg->IO.SendBuf.Buf,
215 "AUTH PLAIN %s\r\n", encoded);
221 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
223 AsyncIO *IO = &Msg->IO;
224 /* Do an AUTH command if necessary */
228 if (!SMTP_IS_STATE('2')) {
229 if (SMTP_IS_STATE('4'))
237 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
239 AsyncIO *IO = &Msg->IO;
240 /* previous command succeeded, now try the MAIL FROM: command */
241 StrBufPrintf(Msg->IO.SendBuf.Buf,
242 "MAIL FROM:<%s>\r\n",
249 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
251 AsyncIO *IO = &Msg->IO;
254 if (!SMTP_IS_STATE('2')) {
255 if (SMTP_IS_STATE('4'))
264 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
266 AsyncIO *IO = &Msg->IO;
267 /* MAIL succeeded, now try the RCPT To: command */
268 StrBufPrintf(Msg->IO.SendBuf.Buf,
269 "RCPT TO:<%s@%s>\r\n",
277 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
279 AsyncIO *IO = &Msg->IO;
282 if (!SMTP_IS_STATE('2')) {
283 if (SMTP_IS_STATE('4'))
291 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
293 AsyncIO *IO = &Msg->IO;
294 /* RCPT succeeded, now try the DATA command */
295 StrBufPlain(Msg->IO.SendBuf.Buf,
302 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
304 AsyncIO *IO = &Msg->IO;
307 if (!SMTP_IS_STATE('3')) {
308 SetSMTPState(IO, eSTMPfailOne);
309 if (SMTP_IS_STATE('4'))
314 SetSMTPState(IO, eSTMPsmtpdata);
318 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
321 /* If we reach this point, the server is expecting data.*/
323 Buf = Msg->IO.SendBuf.Buf;
324 Msg->IO.SendBuf.Buf = Msg->msgtext;
331 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
335 Buf = Msg->IO.SendBuf.Buf;
336 Msg->IO.SendBuf.Buf = Msg->msgtext;
339 StrBufPlain(Msg->IO.SendBuf.Buf,
346 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
348 AsyncIO *IO = &Msg->IO;
351 if (!SMTP_IS_STATE('2')) {
352 if (SMTP_IS_STATE('4'))
358 SetSMTPState(IO, eSTMPsmtpdone);
360 StrBufPlain(Msg->MyQEntry->StatusMessage,
361 &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
362 StrLength(Msg->IO.RecvBuf.Buf) - 4);
363 StrBufTrim(Msg->MyQEntry->StatusMessage);
364 Msg->MyQEntry->Status = 2;
368 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
370 AsyncIO *IO = &Msg->IO;
371 StrBufPlain(Msg->IO.SendBuf.Buf,
378 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
380 AsyncIO *IO = &Msg->IO;
383 EVS_syslog(LOG_DEBUG,
384 "delivery to <%s> @ <%s> (%s) succeeded\n",
389 return eTerminateConnection;
392 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
397 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
402 /*****************************************************************************/
403 /* SMTP CLIENT DISPATCHER */
404 /*****************************************************************************/
405 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
407 SMTPC_read_EHLO_reply,
408 SMTPC_read_HELO_reply,
409 SMTPC_read_auth_reply,
410 SMTPC_read_FROM_reply,
411 SMTPC_read_RCPT_reply,
412 SMTPC_read_DATAcmd_reply,
414 SMTPC_read_data_body_reply,
415 SMTPC_read_QUIT_reply
417 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
418 SMTPC_send_dummy, /* we don't send a greeting, the server does... */
425 SMTPC_send_data_body,
426 SMTPC_send_terminate_data_body,
430 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
432 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
433 300., /* Greeting... */
441 90., /* end of body... */
444 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
445 90., /* Greeting... */
453 900., /* end of body... */
457 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
458 {HKEY("Connection broken during SMTP conversation")},
459 {HKEY("Connection broken during SMTP EHLO")},
460 {HKEY("Connection broken during SMTP HELO")},
461 {HKEY("Connection broken during SMTP AUTH")},
462 {HKEY("Connection broken during SMTP MAIL FROM")},
463 {HKEY("Connection broken during SMTP RCPT")},
464 {HKEY("Connection broken during SMTP DATA")},
465 {HKEY("Connection broken during SMTP message transmit")},
466 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
467 {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
468 {HKEY("")}/* quit reply, don't care. */
475 int smtp_resolve_recipients(SmtpOutMsg *Msg)
477 AsyncIO *IO = &Msg->IO;
484 EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
487 (Msg->MyQEntry == NULL) ||
488 (StrLength(Msg->MyQEntry->Recipient) == 0)) {
492 /* Parse out the host portion of the recipient address */
493 process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
498 EVNCS_syslog(LOG_DEBUG,
499 "Attempting delivery to <%s> @ <%s> (%s)\n",
504 /* If no envelope_from is supplied, extract one from the message */
505 Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
506 if ( (Msg->envelope_from == NULL) ||
507 (IsEmptyStr(Msg->envelope_from)) ) {
508 Msg->mailfrom[0] = '\0';
510 ptr = ChrPtr(Msg->msgtext);
512 if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
516 if (!strncasecmp(buf, "From:", 5))
518 safestrncpy(Msg->mailfrom,
520 sizeof Msg->mailfrom);
522 striplt(Msg->mailfrom);
523 for (i=0; Msg->mailfrom[i]; ++i) {
524 if (!isprint(Msg->mailfrom[i]))
526 strcpy(&Msg->mailfrom[i],
527 &Msg->mailfrom[i+1]);
532 /* Strip out parenthesized names */
536 !IsEmptyStr(Msg->mailfrom + i);
539 if (Msg->mailfrom[i] == '(') lp = i;
540 if (Msg->mailfrom[i] == ')') rp = i;
544 strcpy(&Msg->mailfrom[lp-1],
545 &Msg->mailfrom[rp+1]);
548 /* Prefer brokketized names */
552 !IsEmptyStr(Msg->mailfrom + i);
555 if (Msg->mailfrom[i] == '<') lp = i;
556 if (Msg->mailfrom[i] == '>') rp = i;
558 if ( (lp>=0) && (rp>lp) ) {
559 Msg->mailfrom[rp] = 0;
560 memmove(Msg->mailfrom,
561 &Msg->mailfrom[lp + 1],
567 } while (scan_done == 0);
568 if (IsEmptyStr(Msg->mailfrom))
569 strcpy(Msg->mailfrom, "someone@somewhere.org");
571 stripallbut(Msg->mailfrom, '<', '>');
572 Msg->envelope_from = Msg->mailfrom;