diff --git a/mRemoteV1/Config/Connections/SqlConnectionsLoader.cs b/mRemoteV1/Config/Connections/SqlConnectionsLoader.cs index ed566424e..4ad6f3e18 100644 --- a/mRemoteV1/Config/Connections/SqlConnectionsLoader.cs +++ b/mRemoteV1/Config/Connections/SqlConnectionsLoader.cs @@ -1,23 +1,23 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Security; using mRemoteNG.Config.DatabaseConnectors; using mRemoteNG.Config.DataProviders; using mRemoteNG.Config.Serializers; using mRemoteNG.Config.Serializers.MsSql; using mRemoteNG.Config.Serializers.Versioning; using mRemoteNG.Container; -using mRemoteNG.Tools; -using mRemoteNG.Tree; -using mRemoteNG.Tree.Root; -using System.Collections.Generic; -using System.Linq; -using System.Security; using mRemoteNG.Security; using mRemoteNG.Security.Authentication; using mRemoteNG.Security.SymmetricEncryption; +using mRemoteNG.Tools; +using mRemoteNG.Tree; +using mRemoteNG.Tree.Root; namespace mRemoteNG.Config.Connections { - public class SqlConnectionsLoader : IConnectionsLoader + public class SqlConnectionsLoader : IConnectionsLoader { private readonly IDeserializer> _localConnectionPropertiesDeserializer; private readonly IDataProvider _dataProvider; @@ -41,7 +41,8 @@ namespace mRemoteNG.Config.Connections var databaseVersionVerifier = new SqlDatabaseVersionVerifier(connector); var cryptoProvider = new LegacyRijndaelCryptographyProvider(); - var metaData = metaDataRetriever.GetDatabaseMetaData(connector); + var metaData = metaDataRetriever.GetDatabaseMetaData(connector) ?? + HandleFirstRun(metaDataRetriever, connector); var decryptionKey = GetDecryptionKey(metaData); if (!decryptionKey.Any()) @@ -85,5 +86,11 @@ namespace mRemoteNG.Config.Connections container.IsExpanded = x.LocalProperties.Expanded; }); } + + private SqlConnectionListMetaData HandleFirstRun(SqlDatabaseMetaDataRetriever metaDataRetriever, SqlDatabaseConnector connector) + { + metaDataRetriever.WriteDatabaseMetaData(new RootNodeInfo(RootNodeType.Connection), connector); + return metaDataRetriever.GetDatabaseMetaData(connector); + } } } \ No newline at end of file diff --git a/mRemoteV1/Config/Connections/SqlConnectionsSaver.cs b/mRemoteV1/Config/Connections/SqlConnectionsSaver.cs index 922bee10f..2f5e80b98 100644 --- a/mRemoteV1/Config/Connections/SqlConnectionsSaver.cs +++ b/mRemoteV1/Config/Connections/SqlConnectionsSaver.cs @@ -1,10 +1,16 @@ -using mRemoteNG.App; +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Globalization; +using System.Linq; +using mRemoteNG.App; using mRemoteNG.App.Info; using mRemoteNG.Config.DatabaseConnectors; using mRemoteNG.Config.DataProviders; using mRemoteNG.Config.Serializers; using mRemoteNG.Config.Serializers.MsSql; using mRemoteNG.Config.Serializers.Versioning; +using mRemoteNG.Connection; using mRemoteNG.Container; using mRemoteNG.Messages; using mRemoteNG.Security; @@ -12,19 +18,11 @@ using mRemoteNG.Security.SymmetricEncryption; using mRemoteNG.Tools; using mRemoteNG.Tree; using mRemoteNG.Tree.Root; -using System; -using System.Collections.Generic; -using System.Data.SqlClient; -using System.Globalization; -using System.Linq; -using System.Security; -using mRemoteNG.Connection; namespace mRemoteNG.Config.Connections { - public class SqlConnectionsSaver : ISaver + public class SqlConnectionsSaver : ISaver { - private SecureString _password = Runtime.EncryptionKey; private readonly SaveFilter _saveFilter; private readonly ISerializer, string> _localPropertiesSerializer; private readonly IDataProvider _dataProvider; @@ -73,7 +71,7 @@ namespace mRemoteNG.Config.Connections return; } - UpdateRootNodeTable(rootTreeNode, sqlConnector); + metaDataRetriever.WriteDatabaseMetaData(rootTreeNode, sqlConnector); UpdateConnectionsTable(rootTreeNode, sqlConnector); UpdateUpdatesTable(sqlConnector); } @@ -117,17 +115,17 @@ namespace mRemoteNG.Config.Connections { if (rootTreeNode.Password) { - _password = rootTreeNode.PasswordString.ConvertToSecureString(); - strProtected = cryptographyProvider.Encrypt("ThisIsProtected", _password); + var password = rootTreeNode.PasswordString.ConvertToSecureString(); + strProtected = cryptographyProvider.Encrypt("ThisIsProtected", password); } else { - strProtected = cryptographyProvider.Encrypt("ThisIsNotProtected", _password); + strProtected = cryptographyProvider.Encrypt("ThisIsNotProtected", Runtime.EncryptionKey); } } else { - strProtected = cryptographyProvider.Encrypt("ThisIsNotProtected", _password); + strProtected = cryptographyProvider.Encrypt("ThisIsNotProtected", Runtime.EncryptionKey); } var sqlQuery = new SqlCommand("DELETE FROM tblRoot", sqlDatabaseConnector.SqlConnection); diff --git a/mRemoteV1/Config/Serializers/ConnectionSerializers/MsSql/SqlDatabaseMetaDataRetriever.cs b/mRemoteV1/Config/Serializers/ConnectionSerializers/MsSql/SqlDatabaseMetaDataRetriever.cs index 8c97292d1..e48b2c85b 100644 --- a/mRemoteV1/Config/Serializers/ConnectionSerializers/MsSql/SqlDatabaseMetaDataRetriever.cs +++ b/mRemoteV1/Config/Serializers/ConnectionSerializers/MsSql/SqlDatabaseMetaDataRetriever.cs @@ -2,12 +2,17 @@ using System.Data.SqlClient; using System.Globalization; using mRemoteNG.App; +using mRemoteNG.App.Info; using mRemoteNG.Config.DatabaseConnectors; using mRemoteNG.Messages; +using mRemoteNG.Security; +using mRemoteNG.Security.SymmetricEncryption; +using mRemoteNG.Tools; +using mRemoteNG.Tree.Root; namespace mRemoteNG.Config.Serializers.MsSql { - public class SqlDatabaseMetaDataRetriever + public class SqlDatabaseMetaDataRetriever { public SqlConnectionListMetaData GetDatabaseMetaData(SqlDatabaseConnector sqlDatabaseConnector) { @@ -44,5 +49,45 @@ namespace mRemoteNG.Config.Serializers.MsSql } return metaData; } + + public void WriteDatabaseMetaData(RootNodeInfo rootTreeNode, SqlDatabaseConnector sqlDatabaseConnector) + { + var cryptographyProvider = new LegacyRijndaelCryptographyProvider(); + string strProtected; + if (rootTreeNode != null) + { + if (rootTreeNode.Password) + { + var password = rootTreeNode.PasswordString.ConvertToSecureString(); + strProtected = cryptographyProvider.Encrypt("ThisIsProtected", password); + } + else + { + strProtected = cryptographyProvider.Encrypt("ThisIsNotProtected", Runtime.EncryptionKey); + } + } + else + { + strProtected = cryptographyProvider.Encrypt("ThisIsNotProtected", Runtime.EncryptionKey); + } + + var sqlQuery = new SqlCommand("DELETE FROM tblRoot", sqlDatabaseConnector.SqlConnection); + sqlQuery.ExecuteNonQuery(); + + if (rootTreeNode != null) + { + sqlQuery = + new SqlCommand( + "INSERT INTO tblRoot (Name, Export, Protected, ConfVersion) VALUES(\'" + + MiscTools.PrepareValueForDB(rootTreeNode.Name) + "\', 0, \'" + strProtected + "\'," + + ConnectionsFileInfo.ConnectionFileVersion.ToString(CultureInfo.InvariantCulture) + ")", + sqlDatabaseConnector.SqlConnection); + sqlQuery.ExecuteNonQuery(); + } + else + { + Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, $"UpdateRootNodeTable: rootTreeNode was null. Could not insert!"); + } + } } } \ No newline at end of file