174
177
static int SMTPGetFilterFile(const char *pszFiltID, char *pszFileName, int iMaxName);
175
178
static int SMTPFilterMacroSubstitutes(char **ppszCmdTokens, SMTPSession & SMTPS);
176
179
static char *SMTPGetFilterRejMessage(char const *pszMsgFilePath);
177
static int SMTPRunFilters(SMTPSession & SMTPS, char const *pszFilterPath, char *&pszError);
180
static int SMTPLogFilter(SMTPSession & SMTPS, char const * const *ppszExec, int iExecResult,
181
int iExitCode, char const *pszType, char const *pszInfo);
182
static int SMTPPreFilterExec(SMTPSession & SMTPS, FilterTokens *pToks, char **ppszPEError);
183
static int SMTPRunFilters(SMTPSession & SMTPS, char const *pszFilterPath, char const *pszType,
178
185
static int SMTPFilterMessage(SMTPSession & SMTPS, const char *pszFiltID, char *&pszError);
179
186
static int SMTPHandleCmd_DATA(const char *pszCommand, BSOCK_HANDLE hBSock, SMTPSession & SMTPS);
180
187
static int SMTPAddReceived(int iType, char const *pszAuth, char const *const *ppszMsgInfo,
309
316
static unsigned int SMTPClientThread(void *pThreadData)
312
SYS_SOCKET SockFD = (SYS_SOCKET) (unsigned int) pThreadData;
319
SYS_SOCKET SockFD = (SYS_SOCKET) (unsigned long) pThreadData;
314
321
///////////////////////////////////////////////////////////////////////////////
315
322
// Link socket to the bufferer
702
711
SvrReleaseConfigHandle(SMTPS.hSvrConfig);
703
712
return (ErrorPop());
715
///////////////////////////////////////////////////////////////////////////////
716
// Get maximum errors count allowed in an SMTP session
717
///////////////////////////////////////////////////////////////////////////////
718
SMTPS.iErrorsMax = SvrGetConfigInt("SMTP-MaxErrors", 0, SMTPS.hSvrConfig);
705
720
///////////////////////////////////////////////////////////////////////////////
706
721
// Setup SMTP domain
707
722
///////////////////////////////////////////////////////////////////////////////
957
973
SysFree(pszBuffer);
975
///////////////////////////////////////////////////////////////////////////////
976
// Increase the number of errors we encountered in this session. If the
977
// maximum number of allowed errors is not zero, and the current number of
978
// errors exceed the maximum number, we set the state to 'exit' so that the
979
// SMTP connection will be dropped
980
///////////////////////////////////////////////////////////////////////////////
981
SMTPS.iErrorsCount++;
982
if ((SMTPS.iErrorsMax > 0) && (SMTPS.iErrorsCount >= SMTPS.iErrorsMax)) {
985
if (SMTPLogEnabled(SMTPS.hShbSMTP, SMTPS.pSMTPCfg))
986
SMTPLogSession(SMTPS, SMTPS.pszFrom != NULL ? SMTPS.pszFrom: "",
987
"", "SMTP=EERRS", 0);
989
SysLogMessage(LOG_LEV_MESSAGE, "SMTP forced exit (too many errors: %d) [%s]\n",
990
SMTPS.iErrorsCount, SysInetNToA(SMTPS.PeerInfo, szIP));
992
SMTPS.iSMTPState = stateExit;
1166
1202
UsrFreeUserInfo(pUI);
1167
1203
return (ErrorPop());
1205
///////////////////////////////////////////////////////////////////////////////
1206
// If the user did not authenticate, set the logon user token
1207
///////////////////////////////////////////////////////////////////////////////
1208
if (IsEmptyString(SMTPS.szLogonUser))
1209
UsrGetAddress(pUI, SMTPS.szLogonUser);
1170
1211
UsrFreeUserInfo(pUI);
1893
static int SMTPRunFilters(SMTPSession & SMTPS, char const *pszFilterPath, char *&pszError)
1934
static int SMTPLogFilter(SMTPSession & SMTPS, char const * const *ppszExec, int iExecResult,
1935
int iExitCode, char const *pszType, char const *pszInfo)
1940
FLI.pszSender = SMTPS.pszFrom != NULL ? SMTPS.pszFrom: "";
1941
FLI.pszRecipient = SMTPS.pszRcpt != NULL ? (SMTPS.iRcptCount == 1 ? SMTPS.pszRcpt: "*"): "";
1942
FLI.LocalAddr = SMTPS.SockInfo;
1943
FLI.RemoteAddr = SMTPS.PeerInfo;
1944
FLI.ppszExec = ppszExec;
1945
FLI.iExecResult = iExecResult;
1946
FLI.iExitCode = iExitCode;
1947
FLI.pszType = pszType;
1948
FLI.pszInfo = pszInfo != NULL ? pszInfo: "";
1950
return (FilLogFilter(&FLI));
1954
static int SMTPPreFilterExec(SMTPSession & SMTPS, FilterTokens *pToks, char **ppszPEError)
1960
FCtx.pszAuthName = SMTPS.szLogonUser;
1962
return (FilExecPreParse(&FCtx, ppszPEError));
1966
static int SMTPRunFilters(SMTPSession & SMTPS, char const *pszFilterPath, char const *pszType,
1895
1969
///////////////////////////////////////////////////////////////////////////////
1896
1970
// Share lock the filter file
1932
SMTPFilterMacroSubstitutes(ppszCmdTokens, SMTPS);
1934
iExitCode = iExitFlags = 0;
1935
iExecResult = SysExec(ppszCmdTokens[0], &ppszCmdTokens[0],
2006
///////////////////////////////////////////////////////////////////////////////
2007
// Perform pre-exec filtering (like exec exclude if authenticated, ...)
2008
///////////////////////////////////////////////////////////////////////////////
2009
char *pszPEError = NULL;
2012
Toks.ppszCmdTokens = ppszCmdTokens;
2013
Toks.iTokenCount = iFieldsCount;
2015
if (SMTPPreFilterExec(SMTPS, &Toks, &pszPEError) < 0) {
2016
if (bFilterLogEnabled)
2017
SMTPLogFilter(SMTPS, Toks.ppszCmdTokens, -1,
2018
-1, pszType, pszPEError);
2019
if (pszPEError != NULL)
2020
SysFree(pszPEError);
2021
StrFreeStrings(ppszCmdTokens);
2025
///////////////////////////////////////////////////////////////////////////////
2026
// Do filter line macro substitution
2027
///////////////////////////////////////////////////////////////////////////////
2028
SMTPFilterMacroSubstitutes(Toks.ppszCmdTokens, SMTPS);
2032
iExecResult = SysExec(Toks.ppszCmdTokens[0], &Toks.ppszCmdTokens[0],
1936
2033
iFilterTimeout, SYS_PRIORITY_NORMAL, &iExitCode);
2035
///////////////////////////////////////////////////////////////////////////////
2036
// Log filter execution, if enabled
2037
///////////////////////////////////////////////////////////////////////////////
2038
if (bFilterLogEnabled)
2039
SMTPLogFilter(SMTPS, Toks.ppszCmdTokens, iExecResult,
2040
iExitCode, pszType, NULL);
1938
2042
if (iExecResult == 0) {
1939
2043
SysLogMessage(LOG_LEV_MESSAGE,
1940
2044
"SMTP filter run: Filter = \"%s\" Retcode = %d\n",
1941
ppszCmdTokens[0], iExitCode);
2045
Toks.ppszCmdTokens[0], iExitCode);
1943
2047
iExitFlags = iExitCode & SMTP_FILTER_FL_MASK;
1944
2048
iExitCode &= ~SMTP_FILTER_FL_MASK;
1961
2065
SysLogMessage(LOG_LEV_ERROR,
1962
2066
"SMTP filter error (%d): Filter = \"%s\"\n",
1963
iExecResult, ppszCmdTokens[0]);
2067
iExecResult, Toks.ppszCmdTokens[0]);
1966
2070
StrFreeStrings(ppszCmdTokens);
2001
if (SMTPRunFilters(SMTPS, szFilterFile, pszError) < 0)
2105
if (SMTPRunFilters(SMTPS, szFilterFile, pszFiltID, pszError) < 0)
2002
2106
return (ErrGetErrorCode());