- Conditions under UNIX changed to have the same behaviour as under Windows
authorVictor Kirhenshtein <victor@netxms.org>
Sun, 26 Jun 2005 21:03:04 +0000 (21:03 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Sun, 26 Jun 2005 21:03:04 +0000 (21:03 +0000)
- Message wait queue changed to use broadcast conditions when waiting for message
- Implemented encryption between client and server

23 files changed:
ChangeLog
include/nms_cscp.h
include/nms_threads.h
include/nxclapi.h
include/nxcscpapi.h
src/agent/core/nxagentd.cpp
src/console/win32/LoginDialog.cpp
src/console/win32/LoginDialog.h
src/console/win32/comm.cpp
src/console/win32/globals.cpp
src/console/win32/globals.h
src/console/win32/nxcon.clw
src/console/win32/nxcon.cpp
src/console/win32/nxcon.rc
src/console/win32/resource.h
src/libnxcl/comm.cpp
src/libnxcl/libnxcl.h
src/libnxcl/session.cpp
src/server/core/client.cpp
src/server/core/debug.cpp
src/server/core/main.cpp
src/server/core/session.cpp
src/server/include/nms_core.h

index a7dd7ff..bd58d6c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+*
+* 0.2.2
+*
+
+- Implemented communication session encryption
+
+
 *
 * 0.2.1
 *
index 7a72ec2..ba1c71a 100644 (file)
@@ -353,6 +353,7 @@ typedef struct
 #define CMD_REQUEST_SESSION_KEY     0x0082
 #define CMD_ENCRYPTED_MESSAGE       0x0083
 #define CMD_SESSION_KEY             0x0084
+#define CMD_REQUEST_ENCRYPTION      0x0085
 
 
 //
index 48a4208..b637f54 100644 (file)
@@ -147,6 +147,16 @@ inline void ConditionDestroy(CONDITION hCond)
 }
 
 inline void ConditionSet(CONDITION hCond)
+{
+   SetEvent(hCond);
+}
+
+inline void ConditionReset(CONDITION hCond)
+{
+   ResetEvent(hCond);
+}
+
+inline void ConditionPulse(CONDITION hCond)
 {
    PulseEvent(hCond);
 }
@@ -182,6 +192,7 @@ struct condition_t
        pthread_cond_t cond;
        pthread_mutex_t mutex;
        BOOL broadcast;
+   BOOL isSet;
 };
 typedef struct condition_t * CONDITION;
 
@@ -326,6 +337,7 @@ inline CONDITION ConditionCreate(BOOL bBroadcast)
       pthread_cond_init(&cond->cond, NULL);
       pthread_mutex_init(&cond->mutex, NULL);
                cond->broadcast = bBroadcast;
+      cond->isSet = FALSE;
        }
 
    return cond;
@@ -333,7 +345,7 @@ inline CONDITION ConditionCreate(BOOL bBroadcast)
 
 inline void ConditionDestroy(CONDITION cond)
 {
-       if (cond != NULL)
+       if (cond != INVALID_CONDITION_HANDLE)
        {
                pthread_cond_destroy(&cond->cond);
                pthread_mutex_destroy(&cond->mutex);
@@ -343,9 +355,10 @@ inline void ConditionDestroy(CONDITION cond)
 
 inline void ConditionSet(CONDITION cond)
 {
-       if (cond != NULL)
+       if (cond != INVALID_CONDITION_HANDLE)
        {
                pthread_mutex_lock(&cond->mutex);
+      cond->isSet = TRUE;
                if (cond->broadcast)
                {
                        pthread_cond_broadcast(&cond->cond);
@@ -358,6 +371,34 @@ inline void ConditionSet(CONDITION cond)
        }
 }
 
+inline void ConditionReset(CONDITION cond)
+{
+       if (cond != INVALID_CONDITION_HANDLE)
+       {
+               pthread_mutex_lock(&cond->mutex);
+      cond->isSet = FALSE;
+               pthread_mutex_unlock(&cond->mutex);
+       }
+}
+
+inline void ConditionPulse(CONDITION cond)
+{
+       if (cond != INVALID_CONDITION_HANDLE)
+       {
+               pthread_mutex_lock(&cond->mutex);
+               if (cond->broadcast)
+               {
+                       pthread_cond_broadcast(&cond->cond);
+               }
+               else
+               {
+                       pthread_cond_signal(&cond->cond);
+               }
+      cond->isSet = FALSE;
+               pthread_mutex_unlock(&cond->mutex);
+       }
+}
+
 inline BOOL ConditionWait(CONDITION cond, DWORD dwTimeOut)
 {
        BOOL ret = FALSE;
@@ -367,46 +408,54 @@ inline BOOL ConditionWait(CONDITION cond, DWORD dwTimeOut)
                int retcode;
 
                pthread_mutex_lock(&cond->mutex);
-
-               if (dwTimeOut != INFINITE)
-               {
+      if (cond->isSet)
+      {
+         ret = TRUE;
+         if (!cond->broadcast)
+            cond->isSet = FALSE;
+      }
+      else
+      {
+                  if (dwTimeOut != INFINITE)
+                  {
 #if HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
-                       struct timespec timeout;
+                          struct timespec timeout;
 
-                       timeout.tv_sec = dwTimeOut / 1000;
-                       timeout.tv_nsec = (dwTimeOut % 1000) * 1000000;
-                       retcode = pthread_cond_reltimedwait_np(&cond->cond, &cond->mutex, &timeout);
+                          timeout.tv_sec = dwTimeOut / 1000;
+                          timeout.tv_nsec = (dwTimeOut % 1000) * 1000000;
+                          retcode = pthread_cond_reltimedwait_np(&cond->cond, &cond->mutex, &timeout);
 #else
-                       struct timeval now;
-                       struct timespec timeout;
+                          struct timeval now;
+                          struct timespec timeout;
 
-                       // note.
-                       // mili - 10^-3
-                       // micro - 10^-6
-                       // nano - 10^-9
+                          // note.
+                          // mili - 10^-3
+                          // micro - 10^-6
+                          // nano - 10^-9
 
-                       // FIXME there should be more accurate way
-                       gettimeofday(&now, NULL);
-                       timeout.tv_sec = now.tv_sec + (dwTimeOut / 1000);
+                          // FIXME there should be more accurate way
+                          gettimeofday(&now, NULL);
+                          timeout.tv_sec = now.tv_sec + (dwTimeOut / 1000);
 
-                       now.tv_usec += (dwTimeOut % 1000) * 1000;
-                       timeout.tv_sec += now.tv_usec / 1000000;
-                       timeout.tv_nsec = (now.tv_usec % 1000000) * 1000;
+                          now.tv_usec += (dwTimeOut % 1000) * 1000;
+                          timeout.tv_sec += now.tv_usec / 1000000;
+                          timeout.tv_nsec = (now.tv_usec % 1000000) * 1000;
 
-                       retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
+                          retcode = pthread_cond_timedwait(&cond->cond, &cond->mutex, &timeout);
 #endif
-               }
-               else
-               {
-                       retcode = pthread_cond_wait(&cond->cond, &cond->mutex);
-               }
+                  }
+                  else
+                  {
+                          retcode = pthread_cond_wait(&cond->cond, &cond->mutex);
+                  }
+
+                  if (retcode == 0)
+                  {
+                          ret = TRUE;
+                  }
+      }
 
                pthread_mutex_unlock(&cond->mutex);
-
-               if (retcode == 0)
-               {
-                       ret = TRUE;
-               }
        }
 
        return ret;
index 747dbd6..edde534 100644 (file)
@@ -988,7 +988,7 @@ void LIBNXCL_EXPORTABLE NXCSetDebugCallback(NXC_DEBUG_CALLBACK pFunc);
 
 DWORD LIBNXCL_EXPORTABLE NXCConnect(TCHAR *szServer, TCHAR *szLogin,
                                     TCHAR *szPassword, NXC_SESSION *phSession,
-                                    BOOL bExactVersionMatch);
+                                    BOOL bExactVersionMatch, BOOL bEncrypt);
 void LIBNXCL_EXPORTABLE NXCDisconnect(NXC_SESSION hSession);
 void LIBNXCL_EXPORTABLE NXCSetEventHandler(NXC_SESSION hSession, NXC_EVENT_HANDLER pHandler);
 void LIBNXCL_EXPORTABLE NXCSetCommandTimeout(NXC_SESSION hSession, DWORD dwTimeout);
index 4ea4ea1..d3d190d 100644 (file)
@@ -133,16 +133,16 @@ typedef struct
 class LIBNXCSCP_EXPORTABLE MsgWaitQueue
 {
 private:
-   MUTEX m_hMutexDataAccess;
-   CONDITION m_hStopCondition;
+   MUTEX m_mutexDataAccess;
+   CONDITION m_condStop;
+   CONDITION m_condNewMsg;
    DWORD m_dwMsgHoldTime;
    DWORD m_dwNumElements;
    WAIT_QUEUE_ELEMENT *m_pElements;
-   BOOL m_bIsRunning;
    THREAD m_hHkThread;
 
-   void Lock(void) { MutexLock(m_hMutexDataAccess, INFINITE); }
-   void Unlock(void) { MutexUnlock(m_hMutexDataAccess); }
+   void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
+   void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
    void HousekeeperThread(void);
    void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
    
index 047d35a..166a96d 100644 (file)
@@ -464,7 +464,7 @@ BOOL Initialize(void)
    // Start network listener
    ThreadCreate(ListenerThread, 0, NULL);
 
-   m_hCondShutdown = ConditionCreate(FALSE);
+   m_hCondShutdown = ConditionCreate(TRUE);
    ThreadSleep(1);
 
    return TRUE;
index 8351a49..1131748 100644 (file)
@@ -24,10 +24,10 @@ CLoginDialog::CLoginDialog(CWnd* pParent /*=NULL*/)
        m_szLogin = _T("");
        m_szPassword = _T("");
        m_szServer = _T("");
-       m_iEncryption = -1;
        m_bClearCache = FALSE;
        m_bMatchVersion = FALSE;
        m_bNoCache = FALSE;
+       m_bEncrypt = FALSE;
        //}}AFX_DATA_INIT
 
    lb.lbColor = 0;
@@ -52,10 +52,10 @@ void CLoginDialog::DoDataExchange(CDataExchange* pDX)
        DDV_MaxChars(pDX, m_szPassword, 64);
        DDX_Text(pDX, IDC_EDIT_SERVER, m_szServer);
        DDV_MaxChars(pDX, m_szServer, 64);
-       DDX_Radio(pDX, IDC_RADIO_CLEAR, m_iEncryption);
        DDX_Check(pDX, IDC_CHECK_CACHE, m_bClearCache);
        DDX_Check(pDX, IDC_CHECK_VERSION_MATCH, m_bMatchVersion);
        DDX_Check(pDX, IDC_CHECK_NOCACHE, m_bNoCache);
+       DDX_Check(pDX, IDC_CHECK_ENCRYPT, m_bEncrypt);
        //}}AFX_DATA_MAP
 }
 
index 815fde7..47f540f 100644 (file)
@@ -23,10 +23,10 @@ public:
        CString m_szLogin;
        CString m_szPassword;
        CString m_szServer;
-       int             m_iEncryption;
        BOOL    m_bClearCache;
        BOOL    m_bMatchVersion;
        BOOL    m_bNoCache;
+       BOOL    m_bEncrypt;
        //}}AFX_DATA
 
 
index d2563a1..2171f2e 100644 (file)
@@ -102,7 +102,8 @@ static DWORD WINAPI LoginThread(void *pArg)
    DWORD dwResult;
 
    dwResult = NXCConnect(g_szServer, g_szLogin, g_szPassword, &g_hSession,
-                         (g_dwOptions & OPT_MATCH_SERVER_VERSION) ? TRUE : FALSE);
+                         (g_dwOptions & OPT_MATCH_SERVER_VERSION) ? TRUE : FALSE,
+                         (g_dwOptions & OPT_ENCRYPT_CONNECTION) ? TRUE : FALSE);
 
    // If successful, load container objects' categories
    if (dwResult == RCC_SUCCESS)
index 734a8dd..14eb1d5 100644 (file)
@@ -47,7 +47,6 @@ NXC_SESSION g_hSession = NULL;
 TCHAR g_szServer[MAX_SERVER_NAME_LEN] = _T("localhost");
 TCHAR g_szLogin[MAX_LOGIN_NAME_LEN] = _T("");
 TCHAR g_szPassword[MAX_PASSWORD_LEN] = _T("");
-DWORD g_dwEncryptionMethod = CSCP_ENCRYPTION_NONE;
 
 
 //
index daecf01..bec6e1b 100644 (file)
@@ -74,6 +74,7 @@ extern CConsoleApp theApp;
 
 #define OPT_MATCH_SERVER_VERSION 0x00010000
 #define OPT_DONT_CACHE_OBJECTS   0x00020000
+#define OPT_ENCRYPT_CONNECTION   0x00040000
 
 
 //
@@ -290,7 +291,6 @@ extern DWORD g_dwOptions;
 extern TCHAR g_szServer[];
 extern TCHAR g_szLogin[];
 extern TCHAR g_szPassword[];
-extern DWORD g_dwEncryptionMethod;
 extern TCHAR g_szWorkDir[];
 extern TCHAR *g_szStatusText[];
 extern TCHAR *g_szStatusTextSmall[];
index a47afc0..24fbc6f 100644 (file)
@@ -2,7 +2,7 @@
 
 [General Info]
 Version=1
-LastClass=CEditActionDlg
+LastClass=CLoginDialog
 LastTemplate=CDialog
 NewFileInclude1=#include "stdafx.h"
 NewFileInclude2=#include "nxcon.h"
@@ -16,9 +16,9 @@ Class7=CEventBrowser
 Class9=CMapView
 
 ResourceCount=120
-Resource1=IDD_EDIT_TRAP_ARG (English (U.S.))
+Resource1=IDA_TRAP_EDITOR (English (U.S.))
 Resource2=IDD_OBJECT_NODE_GENERAL
-Resource3=IDD_CREATE_NETSRV (English (U.S.))
+Resource3=IDD_DCI_COLLECTION (English (U.S.))
 Resource4=IDD_SELECT_OBJECT
 Resource5=IDD_SELECT_ACTION
 Class2=CChildView
@@ -32,9 +32,9 @@ Resource7=IDD_NEW_USER
 Class12=CObjectBrowser
 Resource8=IDD_CREATE_CONTAINER
 Class13=CObjectPropDlg
-Resource9=IDD_CHANGE_IP
-Resource10=IDA_OBJECT_BROWSER (English (U.S.))
-Resource11=IDD_GROUP_PROPERTIES (English (U.S.))
+Resource9=IDD_SELECT_USER (English (U.S.))
+Resource10=IDD_GROUP_PROPERTIES (English (U.S.))
+Resource11=IDD_DATA_QUERY (English (U.S.))
 Resource12=IDR_CTRLPANEL (English (U.S.))
 Resource13=IDR_EVENTS (English (U.S.))
 Resource14=IDR_MAPFRAME (English (U.S.))
@@ -42,17 +42,17 @@ Resource15=IDR_OBJECTS (English (U.S.))
 Resource16=IDD_DUMMY (English (U.S.))
 Class14=CEventEditor
 Class15=CEditEventDlg
-Resource17=IDD_CREATE_CONTAINER (English (U.S.))
+Resource17=IDA_MDI_DEFAULT (English (U.S.))
 Class16=CDebugFrame
-Resource18=IDD_SELECT_INTERNAL_ITEM (English (U.S.))
+Resource18=IDD_USER_PROPERTIES (English (U.S.))
 Resource19=IDD_OBJECT_PROPERTIES (English (U.S.))
 Resource20=IDD_SELECT_EVENT
 Class17=CObjectPreview
-Resource21=IDD_EDIT_EVENT (English (U.S.))
+Resource21=IDD_CP_GENERAL (English (U.S.))
 Class18=CToolBox
 Class19=CObjectInfoBox
 Class20=CObjectSearchBox
-Resource22=IDD_DCI_THRESHOLDS (English (U.S.))
+Resource22=IDD_SELECT_INTERNAL_ITEM (English (U.S.))
 Class21=CEditBox
 Class22=COPGeneral
 Class23=CNodePropsGeneral
@@ -61,14 +61,14 @@ Class24=CObjectPropCaps
 Class25=CObjectPropSheet
 Resource24=IDA_MDI_DEFAULT
 Class26=CRequestProcessingDlg
-Resource25=IDD_LOGIN (English (U.S.))
+Resource25=IDD_NEW_USER (English (U.S.))
 Resource26=IDD_PROGRESS (English (U.S.))
 Resource27=IDD_EDIT_RULE_ALARM
-Resource28=IDA_EVENT_EDITOR (English (U.S.))
+Resource28=IDD_THRESHOLD (English (U.S.))
 Class27=CObjectPropsGeneral
-Resource29=IDD_GRAPH_PROP_SETTINGS (English (U.S.))
+Resource29=IDD_REQUEST_PROCESSING (English (U.S.))
 Class28=CObjectPropsSecurity
-Resource30=IDD_OBJECT_CAPS (English (U.S.))
+Resource30=IDD_ACTION_PROPERTIES (English (U.S.))
 Resource31=IDD_SELECT_USER
 Resource32=IDD_OBJECT_SECURITY
 Class29=CUserSelectDlg
@@ -79,36 +79,36 @@ Class31=CNewUserDlg
 Resource35=IDD_THRESHOLD
 Resource36=IDA_EPP
 Class32=CUserPropDlg
-Resource37=IDD_REMOVE_TEMPLATE
+Resource37=IDA_EVENT_EDITOR (English (U.S.))
 Resource38=IDD_ABOUTBOX
 Class33=CGroupPropDlg
 Resource39=IDD_CP_GENERAL
 Resource40=IDA_TRAP_EDITOR
 Resource41=IDM_VIEW_SPECIFIC
-Resource42=IDD_DATA_QUERY (English (U.S.))
+Resource42=IDD_OBJECT_NODE_POLL (English (U.S.))
 Class34=CPasswordChangeDlg
 Class35=CNodeSummary
 Class36=CNetSummaryFrame
 Class37=CDataCollectionEditor
-Resource43=IDD_SELECT_EVENT (English (U.S.))
+Resource43=IDD_OBJECT_SECURITY (English (U.S.))
 Class38=CDCIPropPage
-Resource44=IDD_CP_GENERAL (English (U.S.))
+Resource44=IDD_GRAPH_PROP_DATA (English (U.S.))
 Class39=CDCIDataView
-Resource45=IDA_NETMAP (English (U.S.))
+Resource45=IDD_CREATE_CONTAINER (English (U.S.))
 Class40=CGraph
 Class41=CGraphFrame
 Class42=CDCIThresholdsPage
-Resource46=IDD_OBJECT_RELATIONS (English (U.S.))
-Resource47=IDD_CREATE_TG (English (U.S.))
+Resource46=IDD_OBJECT_NODE_GENERAL (English (U.S.))
+Resource47=IDA_ALARM_BROWSER (English (U.S.))
 Resource48=IDM_CONTEXT
 Class43=CThresholdDlg
-Resource49=IDD_REQUEST_PROCESSING (English (U.S.))
+Resource49=IDD_SELECT_ACTION (English (U.S.))
 Resource50=IDD_EDIT_TRAP_ARG
 Class44=CMIBBrowserDlg
 Class45=CEventPolicyEditor
 Class46=CRuleList
 Class47=CRuleHeader
-Resource51=IDA_ALARM_BROWSER (English (U.S.))
+Resource51=IDD_ABOUTBOX (English (U.S.))
 Resource52=IDD_CREATE_NODE
 Class48=CObjectSelDlg
 Resource53=IDD_OBJECT_CAPS
@@ -117,22 +117,22 @@ Class49=CRuleCommentDlg
 Resource55=IDD_DCI_COLLECTION
 Class50=CEventSelDlg
 Resource56=IDD_DATA_QUERY
-Resource57=IDD_OBJECT_SECURITY (English (U.S.))
+Resource57=IDD_CREATE_NODE (English (U.S.))
 Resource58=IDD_CREATE_TG
-Resource59=IDD_EDIT_TRAP (English (U.S.))
-Resource60=IDD_OBJECT_NODE_POLL (English (U.S.))
+Resource59=IDA_ACTION_EDITOR (English (U.S.))
+Resource60=IDD_EDIT_EVENT (English (U.S.))
 Class51=CObjectPropsPresentation
-Resource61=IDD_SELECT_ACTION (English (U.S.))
+Resource61=IDD_EDIT_TRAP_ARG (English (U.S.))
 Resource62=IDD_EDIT_RULE_COMMENT
 Class52=CRuleSeverityDlg
 Resource63=IDD_ACTION_PROPERTIES
 Class53=CRuleAlarmDlg
 Class54=CAlarmBrowser
-Resource64=IDD_CREATE_NODE (English (U.S.))
+Resource64=IDA_OBJECT_BROWSER (English (U.S.))
 Resource65=IDD_DCI_TRANSFORM
-Resource66=IDD_OBJECT_PRESENTATION (English (U.S.))
-Resource67=IDD_OBJECT_GENERAL (English (U.S.))
-Resource68=IDD_CREATE_TEMPLATE (English (U.S.))
+Resource66=IDD_OBJECT_RELATIONS (English (U.S.))
+Resource67=IDD_DCI_THRESHOLDS (English (U.S.))
+Resource68=IDA_NETMAP (English (U.S.))
 Class55=CConsolePropsGeneral
 Class56=CActionEditor
 Resource69=IDA_ACTION_EDITOR
@@ -143,18 +143,18 @@ Class58=CEditActionDlg
 Resource72=IDA_NETMAP
 Class59=CActionSelDlg
 Resource73=IDD_MIB_BROWSER
-Resource74=IDD_NEW_USER (English (U.S.))
-Resource75=IDA_MDI_DEFAULT (English (U.S.))
+Resource74=IDD_SELECT_OBJECT (English (U.S.))
+Resource75=IDA_GRAPH (English (U.S.))
 Resource76=IDD_USER_PROPERTIES
 Class60=CCreateObjectDlg
 Class61=CCreateContainerDlg
 Resource77=IDR_MAINFRAME
 Class62=CCreateNodeDlg
-Resource78=IDD_OBJECT_NODE_GENERAL (English (U.S.))
-Resource79=IDD_OBJECT_NETSRV_GENERAL (English (U.S.))
-Resource80=IDD_DCI_COLLECTION (English (U.S.))
+Resource78=IDD_NEW_ACTION (English (U.S.))
+Resource79=IDD_LOGIN (English (U.S.))
+Resource80=IDR_MAINFRAME (English (U.S.))
 Class63=CDCITransformPage
-Resource81=IDD_SELECT_AGENT_PARAM (English (U.S.))
+Resource81=IDA_PACKAGE_MGR (English (U.S.))
 Class64=CPollNodeDlg
 Resource82=IDD_POLL_NODE (English (U.S.))
 Class65=CNodePoller
@@ -179,46 +179,46 @@ Class72=CGraphPropDlg
 Class73=CColorSelector
 Class74=CPackageMgr
 Resource94=IDD_GROUP_PROPERTIES
-Resource95=IDD_NEW_ACTION (English (U.S.))
-Resource96=IDD_EDIT_RULE_ALARM (English (U.S.))
-Resource97=IDA_TRAP_EDITOR (English (U.S.))
-Resource98=IDA_EPP (English (U.S.))
-Resource99=IDD_ABOUTBOX (English (U.S.))
-Resource100=IDA_LAST_VALUES (English (U.S.))
-Resource101=IDA_ACTION_EDITOR (English (U.S.))
-Resource102=IDD_DCI_TRANSFORM (English (U.S.))
-Resource103=IDD_EDIT_RULE_COMMENT (English (U.S.))
-Resource104=IDD_GRAPH_PROP_DATA (English (U.S.))
-Resource105=IDD_SET_PASSWORD (English (U.S.))
-Resource106=IDA_PACKAGE_MGR (English (U.S.))
-Resource107=IDD_EDIT_RULE_SEVERITY (English (U.S.))
+Resource95=IDD_REMOVE_TEMPLATE
+Resource96=IDD_GRAPH_PROP_SETTINGS (English (U.S.))
+Resource97=IDD_OBJECT_CAPS (English (U.S.))
+Resource98=IDD_SET_PASSWORD (English (U.S.))
+Resource99=IDD_EDIT_RULE_SEVERITY (English (U.S.))
+Resource100=IDD_OBJECT_PRESENTATION (English (U.S.))
+Resource101=IDD_MIB_BROWSER (English (U.S.))
+Resource102=IDA_EPP (English (U.S.))
+Resource103=IDD_OBJECT_NETSRV_GENERAL (English (U.S.))
+Resource104=IDD_OBJECT_GENERAL (English (U.S.))
+Resource105=IDD_EDIT_TRAP (English (U.S.))
+Resource106=IDD_SELECT_EVENT (English (U.S.))
+Resource107=IDD_DESKTOP_SAVE_AS (English (U.S.))
 Class75=CInternalItemSelDlg
-Resource108=IDD_MIB_BROWSER (English (U.S.))
+Resource108=IDD_INPUT_BOX (English (U.S.))
 Class76=CAgentParamSelDlg
-Resource109=IDD_USER_PROPERTIES (English (U.S.))
+Resource109=IDD_CREATE_TEMPLATE (English (U.S.))
 Class77=CInputBox
-Resource110=IDD_DESKTOP_SAVE_AS (English (U.S.))
+Resource110=IDD_CREATE_TG (English (U.S.))
 Class78=CCreateNetSrvDlg
-Resource111=IDA_GRAPH (English (U.S.))
+Resource111=IDD_EDIT_RULE_COMMENT (English (U.S.))
 Class79=CNetSrvPropsGeneral
-Resource112=IDR_MAINFRAME (English (U.S.))
+Resource112=IDD_EDIT_RULE_ALARM (English (U.S.))
 Class80=CNodePropsPolling
 Class81=CDeploymentView
 Class82=CLastValuesView
 Class83=CValueList
-Resource113=IDD_SELECT_OBJECT (English (U.S.))
+Resource113=IDD_CREATE_NETSRV (English (U.S.))
 Class84=CObjectPropsRelations
-Resource114=IDD_THRESHOLD (English (U.S.))
+Resource114=IDD_SELECT_AGENT_PARAM (English (U.S.))
 Class85=CSaveDesktopDlg
-Resource115=IDM_CONTEXT (English (U.S.))
+Resource115=IDM_VIEW_SPECIFIC (English (U.S.))
 Class86=CGraphSettingsPage
-Resource116=IDD_INPUT_BOX (English (U.S.))
+Resource116=IDA_LAST_VALUES (English (U.S.))
 Class87=CGraphDataPage
-Resource117=IDD_ACTION_PROPERTIES (English (U.S.))
+Resource117=IDD_CHANGE_IP
 Class88=CRemoveTemplateDlg
-Resource118=IDD_SELECT_USER (English (U.S.))
+Resource118=IDD_DCI_TRANSFORM (English (U.S.))
 Class89=CAddrChangeDlg
-Resource119=IDM_VIEW_SPECIFIC (English (U.S.))
+Resource119=IDM_CONTEXT (English (U.S.))
 Class90=CLastValuesPropDlg
 Resource120=IDD_LASTVAL_PROP (English (U.S.))
 
@@ -569,28 +569,25 @@ Control4=IDOK,button,1342373889
 [DLG:IDD_LOGIN (English (U.S.))]
 Type=1
 Class=CLoginDialog
-ControlCount=21
+ControlCount=18
 Control1=IDC_EDIT_SERVER,edit,1350631552
 Control2=IDC_EDIT_LOGIN,edit,1350631552
 Control3=IDC_EDIT_PASSWORD,edit,1350631584
-Control4=IDC_RADIO_CLEAR,button,1342373897
-Control5=IDC_RADIO_BLOWFISH,button,1342177289
-Control6=IDC_CHECK_CACHE,button,1342242819
-Control7=IDC_CHECK_NOCACHE,button,1342242819
-Control8=IDC_CHECK_VERSION_MATCH,button,1342242819
-Control9=IDOK,button,1342242817
-Control10=IDCANCEL,button,1342242816
-Control11=IDC_STATIC,static,1342179342
-Control12=IDC_STATIC,static,1342308352
-Control13=IDC_STATIC,static,1342177296
+Control4=IDC_CHECK_ENCRYPT,button,1342242819
+Control5=IDC_CHECK_CACHE,button,1342242819
+Control6=IDC_CHECK_NOCACHE,button,1342242819
+Control7=IDC_CHECK_VERSION_MATCH,button,1342242819
+Control8=IDOK,button,1342242817
+Control9=IDCANCEL,button,1342242816
+Control10=IDC_STATIC,static,1342179342
+Control11=IDC_STATIC,static,1342308352
+Control12=IDC_STATIC,static,1342177296
+Control13=IDC_STATIC,static,1342308352
 Control14=IDC_STATIC,static,1342308352
 Control15=IDC_STATIC,static,1342308352
-Control16=IDC_STATIC,static,1342308352
-Control17=IDC_STATIC,static,1342308352
-Control18=IDC_STATIC,static,1342177296
-Control19=IDC_STATIC_VERSION,static,1342308482
-Control20=IDC_STATIC,static,1342177296
-Control21=IDC_STATIC,static,1342308352
+Control16=IDC_STATIC_VERSION,static,1342308482
+Control17=IDC_STATIC,static,1342177296
+Control18=IDC_STATIC,static,1342308352
 
 [DLG:IDD_PROGRESS (English (U.S.))]
 Type=1
index 744b4ff..eadcf5f 100644 (file)
@@ -211,7 +211,6 @@ BOOL CConsoleApp::InitInstance()
    g_dwOptions = GetProfileInt(_T("General"), _T("Options"), 0);
    strcpy(g_szServer, (LPCTSTR)GetProfileString(_T("Connection"), _T("Server"), _T("localhost")));
    strcpy(g_szLogin, (LPCTSTR)GetProfileString(_T("Connection"), _T("Login"), NULL));
-   g_dwEncryptionMethod = GetProfileInt(_T("Connection"), _T("Encryption"), CSCP_ENCRYPTION_NONE);
 
    // Create mutex for action list access
    g_mutexActionListAccess = CreateMutex(NULL, FALSE, NULL);
@@ -664,7 +663,7 @@ void CConsoleApp::OnConnectToServer()
 
    dlgLogin.m_szServer = g_szServer;
    dlgLogin.m_szLogin = g_szLogin;
-   dlgLogin.m_iEncryption = g_dwEncryptionMethod;
+   dlgLogin.m_bEncrypt = (g_dwOptions & OPT_ENCRYPT_CONNECTION) ? TRUE : FALSE;
    dlgLogin.m_bNoCache = FALSE;
    dlgLogin.m_bClearCache = FALSE;
    dlgLogin.m_bMatchVersion = (g_dwOptions & OPT_MATCH_SERVER_VERSION) ? TRUE : FALSE;
@@ -678,7 +677,10 @@ void CConsoleApp::OnConnectToServer()
       strcpy(g_szServer, (LPCTSTR)dlgLogin.m_szServer);
       strcpy(g_szLogin, (LPCTSTR)dlgLogin.m_szLogin);
       strcpy(g_szPassword, (LPCTSTR)dlgLogin.m_szPassword);
-      g_dwEncryptionMethod = dlgLogin.m_iEncryption;
+      if (dlgLogin.m_bEncrypt)
+         g_dwOptions |= OPT_ENCRYPT_CONNECTION;
+      else
+         g_dwOptions &= ~OPT_ENCRYPT_CONNECTION;
       if (dlgLogin.m_bMatchVersion)
          g_dwOptions |= OPT_MATCH_SERVER_VERSION;
       else
@@ -691,7 +693,6 @@ void CConsoleApp::OnConnectToServer()
       // Save last connection parameters
       WriteProfileString(_T("Connection"), _T("Server"), g_szServer);
       WriteProfileString(_T("Connection"), _T("Login"), g_szLogin);
-      WriteProfileInt(_T("Connection"), _T("Encryption"), g_dwEncryptionMethod);
 
       // Initiate connection
       dwResult = DoLogin(dlgLogin.m_bClearCache);
index 1a49021..0aa118d 100644 (file)
@@ -808,7 +808,7 @@ BEGIN
     DEFPUSHBUTTON   "OK",IDOK,178,7,50,14,WS_GROUP
 END
 
-IDD_LOGIN DIALOGEX 0, 0, 215, 255
+IDD_LOGIN DIALOGEX 0, 0, 215, 217
 STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
 CAPTION "Connect to server"
 FONT 8, "MS Sans Serif"
@@ -817,20 +817,17 @@ BEGIN
     EDITTEXT        IDC_EDIT_LOGIN,52,92,156,14,ES_AUTOHSCROLL
     EDITTEXT        IDC_EDIT_PASSWORD,52,113,156,14,ES_PASSWORD | 
                     ES_AUTOHSCROLL
-    CONTROL         "&No encryption",IDC_RADIO_CLEAR,"Button",
-                    BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,7,148,60,10
-    CONTROL         "Encrypt connection using &Blowfish + SHA1",
-                    IDC_RADIO_BLOWFISH,"Button",BS_AUTORADIOBUTTON,7,160,151,
-                    10
+    CONTROL         "&Encrypt connection",IDC_CHECK_ENCRYPT,"Button",
+                    BS_AUTOCHECKBOX | WS_TABSTOP,7,145,77,10
     CONTROL         "&Clear object cache on connect",IDC_CHECK_CACHE,"Button",
-                    BS_AUTOCHECKBOX | WS_TABSTOP,7,190,113,10
+                    BS_AUTOCHECKBOX | WS_TABSTOP,7,156,113,10
     CONTROL         "&Do not cache this session",IDC_CHECK_NOCACHE,"Button",
-                    BS_AUTOCHECKBOX | WS_TABSTOP,7,201,97,10
+                    BS_AUTOCHECKBOX | WS_TABSTOP,7,167,97,10
     CONTROL         "&Server version should match client version",
                     IDC_CHECK_VERSION_MATCH,"Button",BS_AUTOCHECKBOX | 
-                    WS_TABSTOP,7,212,149,10
-    DEFPUSHBUTTON   "OK",IDOK,35,234,50,14
-    PUSHBUTTON      "&Quit",IDCANCEL,129,234,50,14
+                    WS_TABSTOP,7,178,149,10
+    DEFPUSHBUTTON   "OK",IDOK,35,196,50,14
+    PUSHBUTTON      "&Quit",IDCANCEL,129,196,50,14
     CONTROL         142,IDC_STATIC,"Static",SS_BITMAP | SS_REALSIZEIMAGE,7,7,
                     201,43
     LTEXT           "Connect to server",IDC_STATIC,7,58,59,8
@@ -838,12 +835,10 @@ BEGIN
     LTEXT           "User name:",IDC_STATIC,7,95,37,8
     LTEXT           "Server:",IDC_STATIC,7,73,24,8
     LTEXT           "Password:",IDC_STATIC,7,115,34,8
-    LTEXT           "Encryption",IDC_STATIC,7,135,37,8
-    CONTROL         "",IDC_STATIC,"Static",SS_ETCHEDHORZ,44,139,163,1
     RTEXT           "Version",IDC_STATIC_VERSION,87,41,120,8,SS_NOPREFIX,
                     WS_EX_TRANSPARENT
-    CONTROL         "",IDC_STATIC,"Static",SS_ETCHEDHORZ,35,182,172,1
-    LTEXT           "Options",IDC_STATIC,7,178,25,8
+    CONTROL         "",IDC_STATIC,"Static",SS_ETCHEDHORZ,35,138,172,1
+    LTEXT           "Options",IDC_STATIC,7,134,25,8
 END
 
 IDD_EDIT_EVENT DIALOG DISCARDABLE  0, 0, 270, 190
@@ -1885,7 +1880,7 @@ BEGIN
         LEFTMARGIN, 7
         RIGHTMARGIN, 208
         TOPMARGIN, 7
-        BOTTOMMARGIN, 248
+        BOTTOMMARGIN, 210
     END
 
     IDD_EDIT_EVENT, DIALOG
index 33f85b7..307a32f 100644 (file)
 #define IDC_STATIC_RCPT                 1261
 #define IDC_STATIC_SUBJ                 1262
 #define IDC_STATIC_DATA                 1263
+#define IDC_CHECK_ENCRYPT               1264
 #define IDR_PACKAGE_MGR                 8156
 #define IDR_DEPLOYMENT_VIEW             8157
 #define IDR_LAST_VALUES_VIEW            8158
 #define _APS_3D_CONTROLS                     1
 #define _APS_NEXT_RESOURCE_VALUE        274
 #define _APS_NEXT_COMMAND_VALUE         32934
-#define _APS_NEXT_CONTROL_VALUE         1264
+#define _APS_NEXT_CONTROL_VALUE         1265
 #define _APS_NEXT_SYMED_VALUE           121
 #endif
 #endif
index 259b42a..305b002 100644 (file)
@@ -1,7 +1,7 @@
 /* 
 ** NetXMS - Network Management System
 ** Client Library
-** Copyright (C) 2004 Victor Kirhenshtein
+** Copyright (C) 2004, 2005 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
@@ -23,9 +23,6 @@
 
 #include "libnxcl.h"
 
-// for TCP_NODELAY
-//#include "netinet/tcp.h"
-
 
 //
 // Network receiver thread
@@ -36,6 +33,7 @@ THREAD_RESULT THREAD_CALL NetReceiver(NXCL_Session *pSession)
    CSCPMessage *pMsg;
    CSCP_MESSAGE *pRawMsg;
    CSCP_BUFFER *pMsgBuffer;
+   BYTE *pDecryptionBuffer;
    int iErr;
    BOOL bMsgNotNeeded;
    TCHAR szBuffer[128];
@@ -46,13 +44,15 @@ THREAD_RESULT THREAD_CALL NetReceiver(NXCL_Session *pSession)
 
    // Allocate space for raw message
    pRawMsg = (CSCP_MESSAGE *)malloc(pSession->m_dwReceiverBufferSize);
+   pDecryptionBuffer = (BYTE *)malloc(pSession->m_dwReceiverBufferSize);
 
    // Message receiving loop
    while(1)
    {
       // Receive raw message
       if ((iErr = RecvCSCPMessage(pSession->m_hSocket, pRawMsg, 
-                                  pMsgBuffer, pSession->m_dwReceiverBufferSize)) <= 0)
+                                  pMsgBuffer, pSession->m_dwReceiverBufferSize,
+                                  &pSession->m_pCtx, pDecryptionBuffer)) <= 0)
          break;
 
       // Check if we get too large message
@@ -64,6 +64,13 @@ THREAD_RESULT THREAD_CALL NetReceiver(NXCL_Session *pSession)
          continue;
       }
 
+      // Check for decryption errors
+      if (iErr == 2)
+      {
+         DebugPrintf(_T("Message decryption error")); 
+         continue;
+      }
+
       // Check that actual received packet size is equal to encoded in packet
       if ((int)ntohl(pRawMsg->dwSize) != iErr)
       {
@@ -106,6 +113,15 @@ THREAD_RESULT THREAD_CALL NetReceiver(NXCL_Session *pSession)
             case CMD_KEEPALIVE:     // Keepalive message, ignore it
                pSession->SetTimeStamp(pMsg->GetVariableLong(VID_TIMESTAMP));
                break;
+            case CMD_REQUEST_SESSION_KEY:
+               if (pSession->m_pCtx == NULL)
+               {
+                  CSCPMessage *pResponce;
+
+                  SetupEncryptionContext(pMsg, &pSession->m_pCtx, &pResponce, NULL);
+                  pSession->SendMsg(pResponce);
+                  delete pResponce;
+               }
             case CMD_OBJECT:        // Object information
             case CMD_OBJECT_UPDATE:
             case CMD_OBJECT_LIST_END:
@@ -173,7 +189,7 @@ THREAD_RESULT THREAD_CALL NetReceiver(NXCL_Session *pSession)
 
 DWORD LIBNXCL_EXPORTABLE NXCConnect(TCHAR *pszServer, TCHAR *pszLogin, 
                                     TCHAR *pszPassword, NXC_SESSION *phSession,
-                                    BOOL bExactVersionMatch)
+                                    BOOL bExactVersionMatch, BOOL bEncrypt)
 {
    struct sockaddr_in servAddr;
    CSCPMessage msg, *pResp;
@@ -256,6 +272,22 @@ DWORD LIBNXCL_EXPORTABLE NXCConnect(TCHAR *pszServer, TCHAR *pszLogin,
                   }
                   delete pResp;
 
+                  // Request encryption if needed
+                  if ((dwRetCode == RCC_SUCCESS) && bEncrypt)
+                  {
+                     msg.DeleteAllVariables();
+                     msg.SetId(pSession->CreateRqId());
+                     msg.SetCode(CMD_REQUEST_ENCRYPTION);
+                     if (pSession->SendMsg(&msg))
+                     {
+                        dwRetCode = pSession->WaitForRCC(msg.GetId());
+                     }
+                     else
+                     {
+                        dwRetCode = RCC_COMM_FAILURE;
+                     }
+                  }
+
                   if (dwRetCode == RCC_SUCCESS)
                   {
                      // Do login if we are requested to do so
@@ -293,6 +325,10 @@ DWORD LIBNXCL_EXPORTABLE NXCConnect(TCHAR *pszServer, TCHAR *pszLogin,
                               dwRetCode = RCC_TIMEOUT;
                            }
                         }
+                        else
+                        {
+                           dwRetCode = RCC_COMM_FAILURE;
+                        }
                      }
                   }
                }
index 499f5cc..bbb3a54 100644 (file)
@@ -101,6 +101,7 @@ private:
    INDEX *m_pIndexById;
    MUTEX m_mutexIndexAccess;
    SOCKET m_hSocket;
+   CSCP_ENCRYPTION_CONTEXT *m_pCtx;
    MsgWaitQueue m_msgWaitQueue;
    DWORD m_dwReceiverBufferSize;
    NXC_DCI_LIST *m_pItemList;
index bfe3bde..1cdbf34 100644 (file)
@@ -51,6 +51,7 @@ NXCL_Session::NXCL_Session()
    m_pUserList = NULL;
 
    m_hRecvThread = INVALID_THREAD_HANDLE;
+   m_pCtx = NULL;
 
 #ifdef _WIN32
    m_condSyncOp = CreateEvent(NULL, FALSE, FALSE, NULL);
@@ -85,6 +86,8 @@ NXCL_Session::~NXCL_Session()
    pthread_mutex_destroy(&m_mutexSyncOp);
    pthread_cond_destroy(&m_condSyncOp);
 #endif
+
+   DestroyEncryptionContext(m_pCtx);
 }
 
 
@@ -106,6 +109,9 @@ void NXCL_Session::Disconnect(void)
    DestroyAllObjects();
    DestroyEventDB();
    DestroyUserDB();
+
+   DestroyEncryptionContext(m_pCtx);
+   m_pCtx = NULL;
 }
 
 
@@ -180,12 +186,29 @@ DWORD NXCL_Session::WaitForRCC(DWORD dwRqId, DWORD dwTimeOut)
 BOOL NXCL_Session::SendMsg(CSCPMessage *pMsg)
 {
    CSCP_MESSAGE *pRawMsg;
+   CSCP_ENCRYPTED_MESSAGE *pEnMsg;
    BOOL bResult;
    TCHAR szBuffer[128];
 
    DebugPrintf(_T("SendMsg(\"%s\"), id:%ld)"), CSCPMessageCodeName(pMsg->GetCode(), szBuffer), pMsg->GetId());
    pRawMsg = pMsg->CreateMessage();
-   bResult = (SendEx(m_hSocket, (char *)pRawMsg, ntohl(pRawMsg->dwSize), 0) == (int)ntohl(pRawMsg->dwSize));
+   if (m_pCtx != NULL)
+   {
+      pEnMsg = CSCPEncryptMessage(m_pCtx, pRawMsg);
+      if (pEnMsg != NULL)
+      {
+         bResult = (SendEx(m_hSocket, (char *)pEnMsg, ntohl(pEnMsg->dwSize), 0) == (int)ntohl(pEnMsg->dwSize));
+         free(pEnMsg);
+      }
+      else
+      {
+         bResult = FALSE;
+      }
+   }
+   else
+   {
+      bResult = (SendEx(m_hSocket, (char *)pRawMsg, ntohl(pRawMsg->dwSize), 0) == (int)ntohl(pRawMsg->dwSize));
+   }
    free(pRawMsg);
    return bResult;
 }
@@ -758,5 +781,5 @@ DWORD NXCL_Session::LoadUserDB(void)
 
 DWORD NXCL_Session::SendFile(DWORD dwRqId, TCHAR *pszFileName)
 {
-   return SendFileOverCSCP(m_hSocket, dwRqId, pszFileName) ? RCC_SUCCESS : RCC_IO_ERROR;
+   return SendFileOverCSCP(m_hSocket, dwRqId, pszFileName, m_pCtx) ? RCC_SUCCESS : RCC_IO_ERROR;
 }
index 774f76c..48fdfda 100644 (file)
@@ -210,17 +210,19 @@ void DumpSessions(CONSOLE_CTX pCtx)
 {
    int i, iCount;
    TCHAR szBuffer[256];
-   static TCHAR *pszStateName[] = { "init", "idle", "processing" };
+   static TCHAR *pszStateName[] = { _T("init"), _T("idle"), _T("processing") };
+   static TCHAR *pszCipherName[] = { _T("NONE"), _T("AES-256"), _T("BLOWFISH"), _T("IDEA"), _T("3DES") };
 
-   ConsolePrintf(pCtx, "ID  STATE                    USER\n");
+   ConsolePrintf(pCtx, "ID  STATE                    CIPHER   USER\n");
    RWLockReadLock(m_rwlockSessionListAccess, INFINITE);
    for(i = 0, iCount = 0; i < MAX_CLIENT_SESSIONS; i++)
       if (m_pSessionList[i] != NULL)
       {
-         ConsolePrintf(pCtx, "%-3d %-24s %s\n", i, 
+         ConsolePrintf(pCtx, "%-3d %-24s %-8s %s\n", i, 
                        (m_pSessionList[i]->GetState() != SESSION_STATE_PROCESSING) ?
                          pszStateName[m_pSessionList[i]->GetState()] :
                          CSCPMessageCodeName(m_pSessionList[i]->GetCurrentCmd(), szBuffer),
+                                          pszCipherName[m_pSessionList[i]->GetCipher() + 1],
                        m_pSessionList[i]->GetUserName());
          iCount++;
       }
index 4ee69e2..d6f86a4 100644 (file)
@@ -1,6 +1,6 @@
 /* 
-** Project X - Network Management System
-** Copyright (C) 2003 Victor Kirhenshtein
+** NetXMS - Network Management System
+** Copyright (C) 2003, 2004, 2005 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
index 344c41f..7e1bdd5 100644 (file)
@@ -76,7 +76,7 @@ RSA *g_pServerKey = NULL;
 // Static data
 //
 
-static CONDITION m_hEventShutdown;
+static CONDITION m_hEventShutdown = INVALID_CONDITION_HANDLE;
 static THREAD m_thNodePollMgr = INVALID_THREAD_HANDLE;
 static THREAD m_thHouseKeeper = INVALID_THREAD_HANDLE;
 static THREAD m_thSyncer = INVALID_THREAD_HANDLE;
index 1af1fb0..08ed287 100644 (file)
@@ -171,6 +171,7 @@ ClientSession::ClientSession(SOCKET hSocket, DWORD dwHostAddr)
    m_dwIndex = INVALID_INDEX;
    m_iState = SESSION_STATE_INIT;
    m_pMsgBuffer = (CSCP_BUFFER *)malloc(sizeof(CSCP_BUFFER));
+   m_pCtx = NULL;
    m_hWriteThread = INVALID_THREAD_HANDLE;
    m_hProcessingThread = INVALID_THREAD_HANDLE;
    m_hUpdateThread = INVALID_THREAD_HANDLE;
@@ -189,6 +190,8 @@ ClientSession::ClientSession(SOCKET hSocket, DWORD dwHostAddr)
    m_hCurrFile = -1;
    m_dwFileRqId = 0;
    m_dwRefCount = 0;
+   m_dwEncryptionRqId = 0;
+   m_condEncryptionSetup = INVALID_CONDITION_HANDLE;
 }
 
 
@@ -219,6 +222,7 @@ ClientSession::~ClientSession()
             delete m_ppEPPRuleList[i];
       free(m_ppEPPRuleList);
    }
+   DestroyEncryptionContext(m_pCtx);
 }
 
 
@@ -261,6 +265,7 @@ void ClientSession::ReadThread(void)
 {
    CSCP_MESSAGE *pRawMsg;
    CSCPMessage *pMsg;
+   BYTE *pDecryptionBuffer;
    TCHAR szBuffer[256];
    int iErr;
    DWORD i;
@@ -271,9 +276,12 @@ void ClientSession::ReadThread(void)
    RecvCSCPMessage(0, NULL, m_pMsgBuffer, 0, NULL, NULL);
 
    pRawMsg = (CSCP_MESSAGE *)malloc(RAW_MSG_SIZE);
+   pDecryptionBuffer = (BYTE *)malloc(RAW_MSG_SIZE);
    while(1)
    {
-      if ((iErr = RecvCSCPMessage(m_hSocket, pRawMsg, m_pMsgBuffer, RAW_MSG_SIZE, NULL, NULL)) <= 0)
+      if ((iErr = RecvCSCPMessage(m_hSocket, pRawMsg, 
+                                  m_pMsgBuffer, RAW_MSG_SIZE, 
+                                  &m_pCtx, pDecryptionBuffer)) <= 0)
          break;
 
       // Check if message is too large
@@ -285,6 +293,13 @@ void ClientSession::ReadThread(void)
          continue;
       }
 
+      // Check for decryption error
+      if (iErr == 2)
+      {
+         DebugPrintf("Unable to decrypt received message\n");
+         continue;
+      }
+
       // Check that actual received packet size is equal to encoded in packet
       if ((int)ntohl(pRawMsg->dwSize) != iErr)
       {
@@ -361,12 +376,23 @@ void ClientSession::ReadThread(void)
       {
          // Create message object from raw message
          pMsg = new CSCPMessage(pRawMsg);
-         m_pMessageQueue->Put(pMsg);
+         if ((pMsg->GetCode() == CMD_SESSION_KEY) && (pMsg->GetId() == m_dwEncryptionRqId))
+         {
+            m_dwEncryptionResult = SetupEncryptionContext(pMsg, &m_pCtx, NULL, g_pServerKey);
+            ConditionSet(m_condEncryptionSetup);
+            m_dwEncryptionRqId = 0;
+            delete pMsg;
+         }
+         else
+         {
+            m_pMessageQueue->Put(pMsg);
+         }
       }
    }
    if (iErr < 0)
       WriteLog(MSG_SESSION_CLOSED, EVENTLOG_WARNING_TYPE, "e", WSAGetLastError());
    free(pRawMsg);
+   free(pDecryptionBuffer);
 
    // Notify other threads to exit
    m_pSendQueue->Clear();
@@ -420,23 +446,42 @@ void ClientSession::ReadThread(void)
 
 void ClientSession::WriteThread(void)
 {
-   CSCP_MESSAGE *pMsg;
+   CSCP_MESSAGE *pRawMsg;
+   CSCP_ENCRYPTED_MESSAGE *pEnMsg;
    char szBuffer[128];
+   BOOL bResult;
 
    while(1)
    {
-      pMsg = (CSCP_MESSAGE *)m_pSendQueue->GetOrBlock();
-      if (pMsg == INVALID_POINTER_VALUE)    // Session termination indicator
+      pRawMsg = (CSCP_MESSAGE *)m_pSendQueue->GetOrBlock();
+      if (pRawMsg == INVALID_POINTER_VALUE)    // Session termination indicator
          break;
 
-      DebugPrintf("Sending message %s\n", CSCPMessageCodeName(ntohs(pMsg->wCode), szBuffer));
-      if (SendEx(m_hSocket, (const char *)pMsg, ntohl(pMsg->dwSize), 0) <= 0)
+      DebugPrintf("Sending message %s\n", CSCPMessageCodeName(ntohs(pRawMsg->wCode), szBuffer));
+      if (m_pCtx != NULL)
+      {
+         pEnMsg = CSCPEncryptMessage(m_pCtx, pRawMsg);
+         if (pEnMsg != NULL)
+         {
+            bResult = (SendEx(m_hSocket, (char *)pEnMsg, ntohl(pEnMsg->dwSize), 0) == (int)ntohl(pEnMsg->dwSize));
+            free(pEnMsg);
+         }
+         else
+         {
+            bResult = FALSE;
+         }
+      }
+      else
+      {
+         bResult = (SendEx(m_hSocket, (const char *)pRawMsg, ntohl(pRawMsg->dwSize), 0) == (int)ntohl(pRawMsg->dwSize));
+      }
+      free(pRawMsg);
+
+      if (!bResult)
       {
-         safe_free(pMsg);
          closesocket(m_hSocket);
          break;
       }
-      safe_free(pMsg);
    }
 }
 
@@ -525,7 +570,9 @@ void ClientSession::ProcessingThread(void)
 
       DebugPrintf("Received message %s\n", CSCPMessageCodeName(pMsg->GetCode(), szBuffer));
       if (!(m_dwFlags & CSF_AUTHENTICATED) && 
-          (pMsg->GetCode() != CMD_LOGIN) && (pMsg->GetCode() != CMD_GET_SERVER_INFO))
+          (pMsg->GetCode() != CMD_LOGIN) && 
+          (pMsg->GetCode() != CMD_GET_SERVER_INFO) &&
+          (pMsg->GetCode() != CMD_REQUEST_ENCRYPTION))
       {
          delete pMsg;
          continue;
@@ -759,6 +806,9 @@ void ClientSession::ProcessingThread(void)
          case CMD_CHANGE_IP_ADDR:
             ChangeObjectIP(pMsg);
             break;
+         case CMD_REQUEST_ENCRYPTION:
+            SetupEncryption(pMsg->GetId());
+            break;
          default:
             // Pass message to loaded modules
             for(i = 0; i < g_dwNumModules; i++)
@@ -4500,3 +4550,40 @@ void ClientSession::ChangeObjectIP(CSCPMessage *pRequest)
    // Send responce
    SendMessage(&msg);
 }
+
+
+//
+// Setup encryption with client
+//
+
+void ClientSession::SetupEncryption(DWORD dwRqId)
+{
+   CSCPMessage msg;
+
+#ifdef _WITH_ENCRYPTION
+   m_dwEncryptionRqId = dwRqId;
+   m_dwEncryptionResult = RCC_TIMEOUT;
+   if (m_condEncryptionSetup == INVALID_CONDITION_HANDLE)
+      m_condEncryptionSetup = ConditionCreate(FALSE);
+
+   // Send request for session key
+   PrepareKeyRequestMsg(&msg, g_pServerKey);
+   msg.SetId(dwRqId);
+   SendMessage(&msg);
+   msg.DeleteAllVariables();
+
+   // Wait for encryption setup
+   ConditionWait(m_condEncryptionSetup, 3000);
+
+   // Send responce
+   msg.SetCode(CMD_REQUEST_COMPLETED);
+   msg.SetId(dwRqId);
+   msg.SetVariable(VID_RCC, m_dwEncryptionResult);
+#else    /* _WITH_ENCRYPTION not defined */
+   msg.SetCode(CMD_REQUEST_COMPLETED);
+   msg.SetId(dwRqId);
+   msg.SetVariable(VID_RCC, RCC_NOT_IMPLEMENTED);
+#endif
+
+   SendMessage(&msg);
+}
index faa40f4..f6f31b3 100644 (file)
@@ -251,6 +251,7 @@ private:
    DWORD m_dwSystemAccess;    // User's system access rights
    DWORD m_dwFlags;           // Session flags
    CSCP_BUFFER *m_pMsgBuffer;
+   CSCP_ENCRYPTION_CONTEXT *m_pCtx;
    THREAD m_hWriteThread;
    THREAD m_hProcessingThread;
    THREAD m_hUpdateThread;
@@ -272,6 +273,9 @@ private:
    DWORD m_dwUploadData;
    TCHAR m_szCurrFileName[MAX_PATH];
    DWORD m_dwRefCount;
+   DWORD m_dwEncryptionRqId;
+   DWORD m_dwEncryptionResult;
+   CONDITION m_condEncryptionSetup;
 
    static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
    static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
@@ -291,6 +295,7 @@ private:
          ((dwRequiredAccess & m_dwSystemAccess) ? TRUE : FALSE);
    }
 
+   void SetupEncryption(DWORD dwRqId);
    void OnFileUpload(BOOL bSuccess);
    void DebugPrintf(char *szFormat, ...);
    void SendServerInfo(DWORD dwRqId);
@@ -377,6 +382,7 @@ public:
    DWORD GetUserId(void) { return m_dwUserId; }
    BOOL IsAuthenticated(void) { return (m_dwFlags & CSF_AUTHENTICATED) ? TRUE : FALSE; }
    WORD GetCurrentCmd(void) { return m_wCurrentCmd; }
+   int GetCipher(void) { return (m_pCtx == NULL) ? -1 : m_pCtx->nCipher; }
 
    void Kill(void);
    void Notify(DWORD dwCode, DWORD dwData = 0);