- Version number changed to 0.2.2-dev
authorVictor Kirhenshtein <victor@netxms.org>
Sat, 18 Jun 2005 23:26:32 +0000 (23:26 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Sat, 18 Jun 2005 23:26:32 +0000 (23:26 +0000)
- Added RSA key pair generation on server

include/netxms-version.h
include/nms_common.h
include/nxcscpapi.h
src/server/core/main.cpp
src/server/core/nxcore.dsp
src/server/include/nms_core.h
src/server/libnxsrv/libnxsrv.dsp
src/server/libnxsrv/messages.mc

index 55ad2e6..ae25ee9 100644 (file)
@@ -30,8 +30,8 @@
 
 #define NETXMS_VERSION_MAJOR        0
 #define NETXMS_VERSION_MINOR        2
-#define NETXMS_VERSION_BUILD        1
-#define NETXMS_VERSION_STRING       _T("0.2.1")
+#define NETXMS_VERSION_BUILD        2
+#define NETXMS_VERSION_STRING       _T("0.2.2-dev")
 
 
 //
index d2fd234..3ea1c08 100644 (file)
@@ -35,6 +35,8 @@
 #else
 #include <config.h>
 #endif
+#else    /* _WIN32 */
+#define _WITH_ENCRYPTION   1
 #endif
 
 #include <unicode.h>
@@ -54,6 +56,7 @@
 #define INVALID_POINTER_VALUE    ((void *)0xFFFFFFFF)
 #define MAX_DB_STRING            256
 #define MAX_PARAM_NAME           256
+#define NETXMS_RSA_KEYLEN        2048
 
 #ifndef LLONG_MAX
 #define LLONG_MAX    9223372036854775807
@@ -265,6 +268,19 @@ typedef int SOCKET;
 #endif   /* _WIN32 */
 
 
+//
+// OpenSSL
+//
+
+#ifdef _WITH_ENCRYPTION
+
+#include <openssl/crypto.h>
+#include <openssl/rsa.h>
+#include <openssl/pem.h>
+
+#endif
+
+
 //
 // open() flags compatibility
 //
index 79028f6..56a4fd4 100644 (file)
@@ -181,6 +181,8 @@ CSCP_MESSAGE LIBNXCSCP_EXPORTABLE *CreateRawCSCPMessage(WORD wCode, DWORD dwId,
 TCHAR LIBNXCSCP_EXPORTABLE *CSCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
 BOOL LIBNXCSCP_EXPORTABLE SendFileOverCSCP(SOCKET hSocket, DWORD dwId, TCHAR *pszFile);
    
+BOOL LIBNXCSCP_EXPORTABLE InitCryptoLib(void);
+
 #ifdef __cplusplus
 }
 #endif
index 0e3bdf1..a627dcf 100644 (file)
@@ -70,7 +70,9 @@ DWORD g_dwConfigurationPollingInterval;
 char g_szDataDir[MAX_PATH];
 DWORD g_dwDBSyntax = DB_SYNTAX_GENERIC;
 QWORD g_qwServerId;
-
+#ifdef _WITH_ENCRYPTION
+RSA *g_pServerKey = NULL;
+#endif
 
 //
 // Static data
@@ -184,6 +186,89 @@ static void LoadGlobalConfig()
 }
 
 
+//
+// Initialize cryptografic functions
+//
+
+static BOOL InitCryptografy(void)
+{
+#ifdef _WITH_ENCRYPTION
+   char szKeyFile[MAX_PATH];
+   BOOL bResult = FALSE;
+   int fd, iLen;
+   BYTE *pBufPos, *pKeyBuffer, hash[SHA1_DIGEST_SIZE];
+
+   if (!InitCryptoLib())
+      return FALSE;
+
+   strcpy(szKeyFile, g_szDataDir);
+   strcat(szKeyFile, DFILE_KEYS);
+   fd = open(szKeyFile, O_RDONLY | O_BINARY);
+   if (fd != -1)
+   {
+      if (read(fd, &iLen, sizeof(int)) == sizeof(int))
+      {
+         pKeyBuffer = (BYTE *)malloc(iLen);
+         pBufPos = pKeyBuffer;
+         if (read(fd, pKeyBuffer, iLen) == iLen)
+         {
+            BYTE hash2[SHA1_DIGEST_SIZE];
+
+            read(fd, hash, SHA1_DIGEST_SIZE);
+            CalculateSHA1Hash(pKeyBuffer, iLen, hash2);
+            if (!memcmp(hash, hash2, SHA1_DIGEST_SIZE))
+            {
+               g_pServerKey = d2i_RSAPublicKey(NULL, (const BYTE **)&pBufPos, iLen);
+               if (g_pServerKey != NULL)
+               {
+                  if (d2i_RSAPrivateKey(&g_pServerKey, (const BYTE **)&pBufPos,
+                                        iLen - (pBufPos - pKeyBuffer)) != NULL)
+                  {
+                     bResult = TRUE;
+                  }
+               }
+            }
+         }
+         free(pKeyBuffer);
+      }
+      close(fd);
+   }
+   else
+   {
+      DbgPrintf(AF_DEBUG_MISC, "Generating RSA key pair...");
+      g_pServerKey = RSA_generate_key(NETXMS_RSA_KEYLEN, 17, NULL, 0);
+      if (g_pServerKey != NULL)
+      {
+         fd = open(szKeyFile, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, 0600);
+         if (fd != -1)
+         {
+            iLen = i2d_RSAPublicKey(g_pServerKey, NULL);
+            iLen += i2d_RSAPrivateKey(g_pServerKey, NULL);
+            pKeyBuffer = (BYTE *)malloc(iLen);
+
+            pBufPos = pKeyBuffer;
+            i2d_RSAPublicKey(g_pServerKey, &pBufPos);
+            i2d_RSAPrivateKey(g_pServerKey, &pBufPos);
+            write(fd, &iLen, sizeof(int));
+            write(fd, pKeyBuffer, iLen);
+
+            CalculateSHA1Hash(pKeyBuffer, iLen, hash);
+            write(fd, hash, SHA1_DIGEST_SIZE);
+            
+            close(fd);
+            free(pKeyBuffer);
+            bResult = TRUE;
+         }
+      }
+   }
+
+   return bResult;
+#else
+   return TRUE;
+#endif
+}
+
+
 //
 // Server initialization
 //
@@ -280,13 +365,20 @@ BOOL NXCORE_EXPORTABLE Initialize(void)
    LoadGlobalConfig();
    DbgPrintf(AF_DEBUG_MISC, "Global configuration loaded");
 
-   // Initialize SNMP stuff
-   SnmpInit();
-
    // Check data directory
    if (!CheckDataDir())
       return FALSE;
 
+   // Initialize cryptografy
+   if (!InitCryptografy())
+   {
+      WriteLog(MSG_INIT_CRYPTO_FAILED, EVENTLOG_ERROR_TYPE, NULL);
+      return FALSE;
+   }
+
+   // Initialize SNMP stuff
+   SnmpInit();
+
    // Update hashes for image files
    UpdateImageHashes();
 
index b8ee5f3..8380ace 100644 (file)
@@ -53,7 +53,7 @@ BSC32=bscmake.exe
 # ADD BSC32 /nologo
 LINK32=link.exe
 # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
-# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib ws2_32.lib libnetxms.lib libnxcscp.lib libnxsrv.lib libnxsnmp.lib iphlpapi.lib /nologo /dll /machine:I386 /libpath:"..\..\libnetxms\Release" /libpath:"..\..\libnxcscp\Release" /libpath:"..\..\libnxsnmp\Release" /libpath:"..\libnxsrv\Release"
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib ws2_32.lib libnetxms.lib libnxcscp.lib libnxsrv.lib libnxsnmp.lib iphlpapi.lib libeay32.lib /nologo /dll /machine:I386 /libpath:"..\..\libnetxms\Release" /libpath:"..\..\libnxcscp\Release" /libpath:"..\..\libnxsnmp\Release" /libpath:"..\libnxsrv\Release"
 # Begin Special Build Tool
 SOURCE="$(InputPath)"
 PostBuild_Desc=Copy files
@@ -74,7 +74,7 @@ PostBuild_Cmds=copy Release\nxcore.dll C:\NetXMS\bin
 # PROP Ignore_Export_Lib 0
 # PROP Target_Dir ""
 # ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "NXCORE_EXPORTS" /YX /FD /GZ /c
-# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\include" /I "..\..\..\include" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "NXCORE_EXPORTS" /YX /FD /GZ /c
+# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\include" /I "..\..\..\include" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "NXCORE_EXPORTS" /FR /YX /FD /GZ /c
 # ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
 # ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
 # ADD BASE RSC /l 0x409 /d "_DEBUG"
@@ -84,7 +84,7 @@ BSC32=bscmake.exe
 # ADD BSC32 /nologo
 LINK32=link.exe
 # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
-# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib ws2_32.lib libnetxms.lib libnxcscp.lib libnxsrv.lib libnxsnmp.lib iphlpapi.lib /nologo /dll /debug /machine:I386 /pdbtype:sept /libpath:"..\..\libnetxms\Debug" /libpath:"..\..\libnxcscp\Debug" /libpath:"..\..\libnxsnmp\Debug" /libpath:"..\libnxsrv\Debug"
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib ws2_32.lib libnetxms.lib libnxcscp.lib libnxsrv.lib libnxsnmp.lib iphlpapi.lib libeay32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept /libpath:"..\..\libnetxms\Debug" /libpath:"..\..\libnxcscp\Debug" /libpath:"..\..\libnxsnmp\Debug" /libpath:"..\libnxsrv\Debug"
 # Begin Special Build Tool
 SOURCE="$(InputPath)"
 PostBuild_Desc=Copy files
index e0bd77d..19559a7 100644 (file)
@@ -118,6 +118,7 @@ typedef __console_ctx * CONSOLE_CTX;
 # define DDIR_MIBS             "\\mibs"
 # define DDIR_IMAGES           "\\images"
 # define DDIR_PACKAGES         "\\packages"
+# define DFILE_KEYS            "\\server_key"
 
 #else    /* _WIN32 */
 
@@ -133,6 +134,7 @@ typedef __console_ctx * CONSOLE_CTX;
 # define DDIR_MIBS             "/mibs"
 # define DDIR_IMAGES           "/images"
 # define DDIR_PACKAGES         "/packages"
+# define DFILE_KEYS            "/.server_key"
 
 #endif   /* _WIN32 */
 
index bfef00a..265ebad 100644 (file)
@@ -130,6 +130,10 @@ SOURCE=.\libnxsrv.h
 # End Source File
 # Begin Source File
 
+SOURCE="..\..\..\include\netxms-version.h"
+# End Source File
+# Begin Source File
+
 SOURCE=..\..\..\include\nms_agent.h
 # End Source File
 # Begin Source File
index f84c70e..d47813b 100644 (file)
@@ -482,4 +482,10 @@ Language=English
 GSM modem on %1 initialized successfully. Hardware ID: "%2".
 .
 
+MessageId=
+SymbolicName=MSG_INIT_CRYPTO_FAILED
+Language=English
+Failed to initialize cryptografy module
+.
+
 ;#endif