Back to index

citadel  8.12
Defines | Functions | Variables
smtp_clienthandlers.c File Reference
#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <errno.h>
#include <sys/types.h>
#include <syslog.h>
#include <time.h>
#include <sys/wait.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <libcitadel.h>
#include "citadel.h"
#include "server.h"
#include "citserver.h"
#include "support.h"
#include "config.h"
#include "control.h"
#include "user_ops.h"
#include "database.h"
#include "msgbase.h"
#include "internet_addressing.h"
#include "genstamp.h"
#include "domain.h"
#include "clientsocket.h"
#include "locate_host.h"
#include "citadel_dirs.h"
#include "ctdl_module.h"
#include "smtp_util.h"
#include "event_client.h"
#include "smtpqueue.h"
#include "smtp_clienthandlers.h"

Go to the source code of this file.

Defines

#define SMTP_ERROR(WHICH_ERR, ERRSTR)
#define SMTP_VERROR(WHICH_ERR)
#define SMTP_IS_STATE(WHICH_STATE)   (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)
#define SMTP_DBG_SEND()   EVS_syslog(LOG_DEBUG, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
#define SMTP_DBG_READ()   EVS_syslog(LOG_DEBUG, "< %s\n", ChrPtr(Msg->IO.IOBuf))

Functions

eNextState SMTPC_read_greeting (SmtpOutMsg *Msg)
eNextState SMTPC_send_EHLO (SmtpOutMsg *Msg)
eNextState SMTPC_read_EHLO_reply (SmtpOutMsg *Msg)
eNextState STMPC_send_HELO (SmtpOutMsg *Msg)
eNextState SMTPC_read_HELO_reply (SmtpOutMsg *Msg)
eNextState SMTPC_send_auth (SmtpOutMsg *Msg)
eNextState SMTPC_read_auth_reply (SmtpOutMsg *Msg)
eNextState SMTPC_send_FROM (SmtpOutMsg *Msg)
eNextState SMTPC_read_FROM_reply (SmtpOutMsg *Msg)
eNextState SMTPC_send_RCPT (SmtpOutMsg *Msg)
eNextState SMTPC_read_RCPT_reply (SmtpOutMsg *Msg)
eNextState SMTPC_send_DATAcmd (SmtpOutMsg *Msg)
eNextState SMTPC_read_DATAcmd_reply (SmtpOutMsg *Msg)
eNextState SMTPC_send_data_body (SmtpOutMsg *Msg)
eNextState SMTPC_send_terminate_data_body (SmtpOutMsg *Msg)
eNextState SMTPC_read_data_body_reply (SmtpOutMsg *Msg)
eNextState SMTPC_send_QUIT (SmtpOutMsg *Msg)
eNextState SMTPC_read_QUIT_reply (SmtpOutMsg *Msg)
eNextState SMTPC_read_dummy (SmtpOutMsg *Msg)
eNextState SMTPC_send_dummy (SmtpOutMsg *Msg)
int smtp_resolve_recipients (SmtpOutMsg *Msg)

Variables

SMTPReadHandler ReadHandlers [eMaxSMTPC]
SMTPSendHandler SendHandlers [eMaxSMTPC]
const double SMTP_C_ConnTimeout = 300.
const double SMTP_C_ReadTimeouts [eMaxSMTPC]
const double SMTP_C_SendTimeouts [eMaxSMTPC]
const ConstStr ReadErrors [eMaxSMTPC+1]

Define Documentation

#define SMTP_DBG_READ ( )    EVS_syslog(LOG_DEBUG, "< %s\n", ChrPtr(Msg->IO.IOBuf))

Definition at line 113 of file smtp_clienthandlers.c.

#define SMTP_DBG_SEND ( )    EVS_syslog(LOG_DEBUG, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))

Definition at line 110 of file smtp_clienthandlers.c.

#define SMTP_ERROR (   WHICH_ERR,
  ERRSTR 
)
Value:
do {                        \
              Msg->MyQEntry->Status = WHICH_ERR;               \
              StrBufAppendBufPlain(Msg->MyQEntry->StatusMessage,     \
                                 HKEY(ERRSTR), 0);                    \
              StrBufTrim(Msg->MyQEntry->StatusMessage);        \
              return eAbort; }                                 \
       while (0)

Definition at line 91 of file smtp_clienthandlers.c.

#define SMTP_IS_STATE (   WHICH_STATE)    (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)

Definition at line 108 of file smtp_clienthandlers.c.

#define SMTP_VERROR (   WHICH_ERR)
Value:
do {                        \
              Msg->MyQEntry->Status = WHICH_ERR;        \
              StrBufPlain(Msg->MyQEntry->StatusMessage,      \
                         ChrPtr(Msg->IO.IOBuf) + 4,            \
                         StrLength(Msg->IO.IOBuf) - 4);     \
              StrBufTrim(Msg->MyQEntry->StatusMessage);      \
              return eAbort; }                          \
       while (0)

Definition at line 99 of file smtp_clienthandlers.c.


Function Documentation

Definition at line 471 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       const char *ptr;
       char buf[1024];
       int scan_done;
       int lp, rp;
       int i;

       EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);

       if ((Msg==NULL) ||
           (Msg->MyQEntry == NULL) ||
           (StrLength(Msg->MyQEntry->Recipient) == 0)) {
              return 0;
       }

       /* Parse out the host portion of the recipient address */
       process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
                         Msg->user,
                         Msg->node,
                         Msg->name);

       EVNCS_syslog(LOG_DEBUG,
                   "Attempting delivery to <%s> @ <%s> (%s)\n",
                   Msg->user,
                   Msg->node,
                   Msg->name);

       /* If no envelope_from is supplied, extract one from the message */
       Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
       if ( (Msg->envelope_from == NULL) ||
            (IsEmptyStr(Msg->envelope_from)) ) {
              Msg->mailfrom[0] = '\0';
              scan_done = 0;
              ptr = ChrPtr(Msg->msgtext);
              do {
                     if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
                     {
                            scan_done = 1;
                     }
                     if (!strncasecmp(buf, "From:", 5))
                     {
                            safestrncpy(Msg->mailfrom,
                                       &buf[5],
                                       sizeof Msg->mailfrom);

                            striplt(Msg->mailfrom);
                            for (i=0; Msg->mailfrom[i]; ++i) {
                                   if (!isprint(Msg->mailfrom[i]))
                                   {
                                          strcpy(&Msg->mailfrom[i],
                                                 &Msg->mailfrom[i+1]);
                                          i=0;
                                   }
                            }

                            /* Strip out parenthesized names */
                            lp = (-1);
                            rp = (-1);
                            for (i=0;
                                 !IsEmptyStr(Msg->mailfrom + i);
                                 ++i)
                            {
                                   if (Msg->mailfrom[i] == '(') lp = i;
                                   if (Msg->mailfrom[i] == ')') rp = i;
                            }
                            if ((lp>0)&&(rp>lp))
                            {
                                   strcpy(&Msg->mailfrom[lp-1],
                                          &Msg->mailfrom[rp+1]);
                            }

                            /* Prefer brokketized names */
                            lp = (-1);
                            rp = (-1);
                            for (i=0;
                                 !IsEmptyStr(Msg->mailfrom + i);
                                 ++i)
                            {
                                   if (Msg->mailfrom[i] == '<') lp = i;
                                   if (Msg->mailfrom[i] == '>') rp = i;
                            }
                            if ( (lp>=0) && (rp>lp) ) {
                                   Msg->mailfrom[rp] = 0;
                                   memmove(Msg->mailfrom,
                                          &Msg->mailfrom[lp + 1],
                                          rp - lp);
                            }

                            scan_done = 1;
                     }
              } while (scan_done == 0);
              if (IsEmptyStr(Msg->mailfrom))
                     strcpy(Msg->mailfrom, "someone@somewhere.org");

              stripallbut(Msg->mailfrom, '<', '>');
              Msg->envelope_from = Msg->mailfrom;
       }

       return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 220 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       /* Do an AUTH command if necessary */

       SMTP_DBG_READ();

       if (!SMTP_IS_STATE('2')) {
              if (SMTP_IS_STATE('4'))
                     SMTP_VERROR(4);
              else
                     SMTP_VERROR(5);
       }
       return eSendReply;
}

Definition at line 343 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       if (!SMTP_IS_STATE('2')) {
              if (SMTP_IS_STATE('4'))
                     SMTP_VERROR(4);
              else
                     SMTP_VERROR(5);
       }

       /* We did it! */
       StrBufPlain(Msg->MyQEntry->StatusMessage,
                  &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
                  StrLength(Msg->IO.RecvBuf.Buf) - 4);
       StrBufTrim(Msg->MyQEntry->StatusMessage);
       Msg->MyQEntry->Status = 2;
       return eSendReply;
}

Definition at line 301 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       if (!SMTP_IS_STATE('3')) {
              if (SMTP_IS_STATE('4'))
                     SMTP_VERROR(3);
              else
                     SMTP_VERROR(5);
       }
       return eSendReply;
}

Definition at line 388 of file smtp_clienthandlers.c.

{
       return eSendReply;
}

Definition at line 148 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       if (SMTP_IS_STATE('2')) {
              Msg->State ++;

              if ((Msg->pCurrRelay == NULL) ||
                  (Msg->pCurrRelay->User == NULL))
                     Msg->State ++; /* Skip auth... */
       }
       /* else we fall back to 'helo' */
       return eSendReply;
}

Definition at line 248 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       if (!SMTP_IS_STATE('2')) {
              if (SMTP_IS_STATE('4'))
                     SMTP_VERROR(4);
              else
                     SMTP_VERROR(5);
       }
       return eSendReply;
}

Definition at line 120 of file smtp_clienthandlers.c.

{
       /* Process the SMTP greeting from the server */
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       if (!SMTP_IS_STATE('2')) {
              if (SMTP_IS_STATE('4'))
                     SMTP_VERROR(4);
              else
                     SMTP_VERROR(5);
       }
       return eSendReply;
}

Definition at line 174 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       if (!SMTP_IS_STATE('2'))
       {
              if (SMTP_IS_STATE('4'))
                     SMTP_VERROR(4);
              else
                     SMTP_VERROR(5);
       }
       if ((Msg->pCurrRelay == NULL) ||
           (Msg->pCurrRelay->User == NULL))
              Msg->State ++; /* Skip auth... */

       return eSendReply;
}

Definition at line 374 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       EVS_syslog(LOG_DEBUG,
                 "delivery to <%s> @ <%s> (%s) succeeded\n",
                 Msg->user,
                 Msg->node,
                 Msg->name);

       return eTerminateConnection;
}

Definition at line 276 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       SMTP_DBG_READ();

       if (!SMTP_IS_STATE('2')) {
              if (SMTP_IS_STATE('4'))
                     SMTP_VERROR(4);
              else
                     SMTP_VERROR(5);
       }
       return eSendReply;
}

Definition at line 193 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       char buf[SIZ];
       char encoded[1024];

       if ((Msg->pCurrRelay == NULL) ||
           (Msg->pCurrRelay->User == NULL))
              Msg->State ++; /* Skip auth, shouldn't even come here!... */
       else {
              /* Do an AUTH command if necessary */
              sprintf(buf, "%s%c%s%c%s",
                     Msg->pCurrRelay->User, '\0',
                     Msg->pCurrRelay->User, '\0',
                     Msg->pCurrRelay->Pass);

              CtdlEncodeBase64(encoded, buf,
                             strlen(Msg->pCurrRelay->User) * 2 +
                             strlen(Msg->pCurrRelay->Pass) + 2, 0);

              StrBufPrintf(Msg->IO.SendBuf.Buf,
                          "AUTH PLAIN %s\r\n", encoded);
       }
       SMTP_DBG_SEND();
       return eReadMessage;
}

Definition at line 315 of file smtp_clienthandlers.c.

{
       StrBuf *Buf;
       /* If we reach this point, the server is expecting data.*/

       Buf = Msg->IO.SendBuf.Buf;
       Msg->IO.SendBuf.Buf = Msg->msgtext;
       Msg->msgtext = Buf;
       Msg->State ++;

       return eSendMore;
}

Definition at line 290 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       /* RCPT succeeded, now try the DATA command */
       StrBufPlain(Msg->IO.SendBuf.Buf,
                  HKEY("DATA\r\n"));

       SMTP_DBG_SEND();
       return eReadMessage;
}

Definition at line 393 of file smtp_clienthandlers.c.

{
       return eReadMessage;
}

Definition at line 135 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       /* At this point we know we are talking to a real SMTP server */

       /* Do a EHLO command.  If it fails, try the HELO command. */
       StrBufPrintf(Msg->IO.SendBuf.Buf,
                   "EHLO %s\r\n", config.c_fqdn);

       SMTP_DBG_SEND();
       return eReadMessage;
}

Definition at line 236 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       /* previous command succeeded, now try the MAIL FROM: command */
       StrBufPrintf(Msg->IO.SendBuf.Buf,
                   "MAIL FROM:<%s>\r\n",
                   Msg->envelope_from);

       SMTP_DBG_SEND();
       return eReadMessage;
}

Definition at line 364 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       StrBufPlain(Msg->IO.SendBuf.Buf,
                  HKEY("QUIT\r\n"));

       SMTP_DBG_SEND();
       return eReadMessage;
}

Definition at line 263 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       /* MAIL succeeded, now try the RCPT To: command */
       StrBufPrintf(Msg->IO.SendBuf.Buf,
                   "RCPT TO:<%s@%s>\r\n",
                   Msg->user,
                   Msg->node);

       SMTP_DBG_SEND();
       return eReadMessage;
}

Definition at line 328 of file smtp_clienthandlers.c.

{
       StrBuf *Buf;

       Buf = Msg->IO.SendBuf.Buf;
       Msg->IO.SendBuf.Buf = Msg->msgtext;
       Msg->msgtext = Buf;

       StrBufPlain(Msg->IO.SendBuf.Buf,
                  HKEY(".\r\n"));

       return eReadMessage;

}

Definition at line 164 of file smtp_clienthandlers.c.

{
       AsyncIO *IO = &Msg->IO;
       StrBufPrintf(Msg->IO.SendBuf.Buf,
                   "HELO %s\r\n", config.c_fqdn);

       SMTP_DBG_SEND();
       return eReadMessage;
}

Variable Documentation

const ConstStr ReadErrors[eMaxSMTPC+1]
Initial value:
 {
       {HKEY("Connection broken during SMTP conversation")},
       {HKEY("Connection broken during SMTP EHLO")},
       {HKEY("Connection broken during SMTP HELO")},
       {HKEY("Connection broken during SMTP AUTH")},
       {HKEY("Connection broken during SMTP MAIL FROM")},
       {HKEY("Connection broken during SMTP RCPT")},
       {HKEY("Connection broken during SMTP DATA")},
       {HKEY("Connection broken during SMTP message transmit")},
       {HKEY("Connection broken during SMTP message transmit")},
       {HKEY("Connection broken during SMTP message transmit")},
       {HKEY("")}
}

Definition at line 453 of file smtp_clienthandlers.c.

const double SMTP_C_ConnTimeout = 300.

Definition at line 426 of file smtp_clienthandlers.c.

Initial value:
 {
       300., 
       30., 
       30., 
       30., 
       30., 
       90., 
       30., 
       90., 
       90., 
       30.  
}

Definition at line 428 of file smtp_clienthandlers.c.

Initial value:
 {
       90., 
       30., 
       30., 
       30., 
       30., 
       30., 
       30., 
       90., 
       900., 
       30.  
}

Definition at line 440 of file smtp_clienthandlers.c.