~ubuntu-branches/debian/squeeze/xmail/squeeze

« back to all changes in this revision

Viewing changes to SMTPSvr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Radu Spineanu
  • Date: 2007-02-16 12:16:16 UTC
  • mfrom: (3.1.2 feisty)
  • Revision ID: james.westby@ubuntu.com-20070216121616-c1eczdj65j5pp2mh
Tags: 1.22-5
Updated Czech debconf translation. (closes: #408660)
Thanks to Miroslav Kure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#include "SMTPUtils.h"
42
42
#include "MailDomains.h"
43
43
#include "POP3Utils.h"
 
44
#include "Filter.h"
44
45
#include "MailConfig.h"
45
46
#include "AppDefines.h"
46
47
#include "MailSvr.h"
120
121
        char *pszRcpt;
121
122
        char *pszSendRcpt;
122
123
        int iRcptCount;
 
124
        int iErrorsCount;
 
125
        int iErrorsMax;
123
126
        SYS_UINT64 ullMessageID;
124
127
        char szMessageID[128];
125
128
        char szTimeStamp[256];
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,
 
184
                          char *&pszError);
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)
310
317
{
311
318
 
312
 
        SYS_SOCKET SockFD = (SYS_SOCKET) (unsigned int) pThreadData;
 
319
        SYS_SOCKET SockFD = (SYS_SOCKET) (unsigned long) pThreadData;
313
320
 
314
321
///////////////////////////////////////////////////////////////////////////////
315
322
//  Link socket to the bufferer
668
675
        SMTPS.pszRcpt = NULL;
669
676
        SMTPS.pszSendRcpt = NULL;
670
677
        SMTPS.iRcptCount = 0;
 
678
        SMTPS.iErrorsCount = 0;
 
679
        SMTPS.iErrorsMax = 0;
671
680
        SMTPS.iCmdDelay = 0;
672
681
        SMTPS.ulMaxMsgSize = 0;
673
682
        SetEmptyString(SMTPS.szMessageID);
702
711
                SvrReleaseConfigHandle(SMTPS.hSvrConfig);
703
712
                return (ErrorPop());
704
713
        }
 
714
 
 
715
///////////////////////////////////////////////////////////////////////////////
 
716
//  Get maximum errors count allowed in an SMTP session
 
717
///////////////////////////////////////////////////////////////////////////////
 
718
        SMTPS.iErrorsMax = SvrGetConfigInt("SMTP-MaxErrors", 0, SMTPS.hSvrConfig);
 
719
 
705
720
///////////////////////////////////////////////////////////////////////////////
706
721
//  Setup SMTP domain
707
722
///////////////////////////////////////////////////////////////////////////////
710
725
 
711
726
        if (pszSvrDomain != NULL) {
712
727
                StrSNCpy(SMTPS.szSvrDomain, pszSvrDomain);
 
728
                StrSNCpy(SMTPS.szSvrFQDN, pszSvrDomain);
713
729
 
714
730
                SysFree(pszSvrDomain);
715
731
        } else {
956
972
 
957
973
        SysFree(pszBuffer);
958
974
 
 
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)) {
 
983
                char szIP[128] = "";
 
984
 
 
985
                if (SMTPLogEnabled(SMTPS.hShbSMTP, SMTPS.pSMTPCfg))
 
986
                        SMTPLogSession(SMTPS, SMTPS.pszFrom != NULL ? SMTPS.pszFrom: "",
 
987
                                       "", "SMTP=EERRS", 0);
 
988
 
 
989
                SysLogMessage(LOG_LEV_MESSAGE, "SMTP forced exit (too many errors: %d) [%s]\n",
 
990
                              SMTPS.iErrorsCount, SysInetNToA(SMTPS.PeerInfo, szIP));
 
991
 
 
992
                SMTPS.iSMTPState = stateExit;
 
993
        }
 
994
 
959
995
        return (0);
960
996
 
961
997
}
1166
1202
                UsrFreeUserInfo(pUI);
1167
1203
                return (ErrorPop());
1168
1204
        }
 
1205
///////////////////////////////////////////////////////////////////////////////
 
1206
//  If the user did not authenticate, set the logon user token
 
1207
///////////////////////////////////////////////////////////////////////////////
 
1208
        if (IsEmptyString(SMTPS.szLogonUser))
 
1209
                UsrGetAddress(pUI, SMTPS.szLogonUser);
1169
1210
 
1170
1211
        UsrFreeUserInfo(pUI);
1171
1212
 
1334
1375
                SMTPResetSession(SMTPS);
1335
1376
 
1336
1377
                SMTPSendError(hBSock, SMTPS,
1337
 
                              "451 Requested action aborted: (%d) local error in processing",
 
1378
                              "501 Syntax error in parameters or arguments: (%d)",
1338
1379
                              ErrorFetch());
1339
1380
                return (ErrorPop());
1340
1381
        }
1762
1803
                SMTPResetSession(SMTPS);
1763
1804
 
1764
1805
                SMTPSendError(hBSock, SMTPS,
1765
 
                              "451 Requested action aborted: (%d) local error in processing",
 
1806
                              "501 Syntax error in parameters or arguments: (%d)",
1766
1807
                              ErrorFetch());
1767
1808
                return (ErrorPop());
1768
1809
        }
1890
1931
 
1891
1932
}
1892
1933
 
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)
 
1936
{
 
1937
 
 
1938
        FilterLogInfo FLI;
 
1939
 
 
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: "";
 
1949
 
 
1950
        return (FilLogFilter(&FLI));
 
1951
 
 
1952
}
 
1953
 
 
1954
static int SMTPPreFilterExec(SMTPSession & SMTPS, FilterTokens *pToks, char **ppszPEError)
 
1955
{
 
1956
 
 
1957
        FilterExecCtx FCtx;
 
1958
 
 
1959
        FCtx.pToks = pToks;
 
1960
        FCtx.pszAuthName = SMTPS.szLogonUser;
 
1961
 
 
1962
        return (FilExecPreParse(&FCtx, ppszPEError));
 
1963
 
 
1964
}
 
1965
 
 
1966
static int SMTPRunFilters(SMTPSession & SMTPS, char const *pszFilterPath, char const *pszType,
 
1967
                          char *&pszError)
1894
1968
{
1895
1969
///////////////////////////////////////////////////////////////////////////////
1896
1970
//  Share lock the filter file
1929
2003
                        continue;
1930
2004
                }
1931
2005
 
1932
 
                SMTPFilterMacroSubstitutes(ppszCmdTokens, SMTPS);
1933
 
 
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;
 
2010
                FilterTokens Toks;
 
2011
 
 
2012
                Toks.ppszCmdTokens = ppszCmdTokens;
 
2013
                Toks.iTokenCount = iFieldsCount;
 
2014
 
 
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);
 
2022
                        continue;
 
2023
                }
 
2024
 
 
2025
///////////////////////////////////////////////////////////////////////////////
 
2026
//  Do filter line macro substitution
 
2027
///////////////////////////////////////////////////////////////////////////////
 
2028
                SMTPFilterMacroSubstitutes(Toks.ppszCmdTokens, SMTPS);
 
2029
 
 
2030
                iExitCode = -1;
 
2031
                iExitFlags = 0;
 
2032
                iExecResult = SysExec(Toks.ppszCmdTokens[0], &Toks.ppszCmdTokens[0],
1936
2033
                                      iFilterTimeout, SYS_PRIORITY_NORMAL, &iExitCode);
1937
2034
 
 
2035
///////////////////////////////////////////////////////////////////////////////
 
2036
//  Log filter execution, if enabled
 
2037
///////////////////////////////////////////////////////////////////////////////
 
2038
                if (bFilterLogEnabled)
 
2039
                        SMTPLogFilter(SMTPS, Toks.ppszCmdTokens, iExecResult,
 
2040
                                      iExitCode, pszType, NULL);
 
2041
 
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);
1942
2046
 
1943
2047
                        iExitFlags = iExitCode & SMTP_FILTER_FL_MASK;
1944
2048
                        iExitCode &= ~SMTP_FILTER_FL_MASK;
1960
2064
                } else {
1961
2065
                        SysLogMessage(LOG_LEV_ERROR,
1962
2066
                                      "SMTP filter error (%d): Filter = \"%s\"\n",
1963
 
                                      iExecResult, ppszCmdTokens[0]);
 
2067
                                      iExecResult, Toks.ppszCmdTokens[0]);
1964
2068
                }
1965
2069
 
1966
2070
                StrFreeStrings(ppszCmdTokens);
1998
2102
                iReOpen++;
1999
2103
        }
2000
2104
 
2001
 
        if (SMTPRunFilters(SMTPS, szFilterFile, pszError) < 0)
 
2105
        if (SMTPRunFilters(SMTPS, szFilterFile, pszFiltID, pszError) < 0)
2002
2106
                return (ErrGetErrorCode());
2003
2107
 
2004
2108
        if (iReOpen) {