From 12b5644789db8ebf73d0c1c2ed0b2be637e2440b Mon Sep 17 00:00:00 2001 From: verhoek Date: Thu, 21 Jun 2018 17:41:30 +0200 Subject: [PATCH 1/6] Public unnecessary. --- Duplicati/Library/SQLiteHelper/DatabaseUpgrader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Duplicati/Library/SQLiteHelper/DatabaseUpgrader.cs b/Duplicati/Library/SQLiteHelper/DatabaseUpgrader.cs index cda18b622..510f53bd6 100644 --- a/Duplicati/Library/SQLiteHelper/DatabaseUpgrader.cs +++ b/Duplicati/Library/SQLiteHelper/DatabaseUpgrader.cs @@ -182,7 +182,7 @@ namespace Duplicati.Library.SQLiteHelper /// /// The database connection to use /// The file the database is placed in - public static void UpgradeDatabase(IDbConnection connection, string sourcefile, string schema, IList versions) + private static void UpgradeDatabase(IDbConnection connection, string sourcefile, string schema, IList versions) { if (connection.State != ConnectionState.Open) { From 772eb1f1870bf789f7689cc69f133dbc5f0a108f Mon Sep 17 00:00:00 2001 From: verhoek Date: Thu, 21 Jun 2018 18:08:52 +0200 Subject: [PATCH 2/6] Moved method to sqlite loader. --- .../Library/SQLiteHelper/SQLiteLoader.cs | 70 ++++++++++++++++++ Duplicati/Server/Program.cs | 74 +------------------ 2 files changed, 72 insertions(+), 72 deletions(-) diff --git a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs index 437cf7409..f00f087ac 100644 --- a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs +++ b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs @@ -35,6 +35,76 @@ namespace Duplicati.Library.SQLiteHelper /// private static Type m_type = null; + /// + /// Helper method with logic to handle opening a database in possibly encrypted format + /// + /// The SQLite connection object + /// Specify if database is encrypted + /// Encryption password + public static void OpenDatabase(System.Data.IDbConnection con, bool useDatabaseEncryption, string password) + { + System.Reflection.MethodInfo setPwdMethod = con.GetType().GetMethod("SetPassword", new Type[] { typeof(string) }); + string attemptedPassword; + + if (!useDatabaseEncryption || string.IsNullOrEmpty(password)) + attemptedPassword = null; //No encryption specified, attempt to open without + else + attemptedPassword = password; //Encryption specified, attempt to open with + + if (setPwdMethod != null) + setPwdMethod.Invoke(con, new object[] { attemptedPassword }); + + try + { + //Attempt to open in preferred state + con.Open(); + + // Do a dummy query to make sure we have a working db + using (var cmd = con.CreateCommand()) + { + cmd.CommandText = "SELECT COUNT(*) FROM SQLITE_MASTER"; + cmd.ExecuteScalar(); + } + } + catch + { + try + { + //We can't try anything else without a password + if (string.IsNullOrEmpty(password)) + throw; + + //Open failed, now try the reverse + attemptedPassword = attemptedPassword == null ? password : null; + + con.Close(); + if (setPwdMethod != null) + setPwdMethod.Invoke(con, new object[] { attemptedPassword }); + con.Open(); + + // Do a dummy query to make sure we have a working db + using (var cmd = con.CreateCommand()) + { + cmd.CommandText = "SELECT COUNT(*) FROM SQLITE_MASTER"; + cmd.ExecuteScalar(); + } + } + catch + { + try { con.Close(); } + catch { } + } + + //If the db is not open now, it won't open + if (con.State != System.Data.ConnectionState.Open) + throw; //Report original error + + //The open method succeeded with the non-default method, now change the password + System.Reflection.MethodInfo changePwdMethod = con.GetType().GetMethod("ChangePassword", new Type[] { typeof(string) }); + changePwdMethod.Invoke(con, new object[] { useDatabaseEncryption ? password : null }); + } + } + /// /// Loads an SQLite connection instance, optionally setting the tempfolder and opening the the database /// diff --git a/Duplicati/Server/Program.cs b/Duplicati/Server/Program.cs index 84ec98f97..5b87a0e6e 100644 --- a/Duplicati/Server/Program.cs +++ b/Duplicati/Server/Program.cs @@ -599,7 +599,7 @@ namespace Duplicati.Server con.ConnectionString = "Data Source=" + DatabasePath; //Attempt to open the database, handling any encryption present - OpenDatabase(con, useDatabaseEncryption, dbPassword); + Duplicati.Library.SQLiteHelper.SQLiteLoader.OpenDatabase(con, useDatabaseEncryption, dbPassword); Duplicati.Library.SQLiteHelper.DatabaseUpgrader.UpgradeDatabase(con, DatabasePath, typeof(Database.Connection)); } @@ -692,77 +692,7 @@ namespace Duplicati.Server StatusEventNotifyer.SignalNewEvent(); } - - /// - /// Helper method with logic to handle opening a database in possibly encrypted format - /// - /// The SQLite connection object - /// Specify if database is encrypted - /// Encryption password - public static void OpenDatabase(System.Data.IDbConnection con, bool useDatabaseEncryption, string password) - { - System.Reflection.MethodInfo setPwdMethod = con.GetType().GetMethod("SetPassword", new Type[] { typeof(string) }); - string attemptedPassword; - - if (!useDatabaseEncryption || string.IsNullOrEmpty(password)) - attemptedPassword = null; //No encryption specified, attempt to open without - else - attemptedPassword = password; //Encryption specified, attempt to open with - - if (setPwdMethod != null) - setPwdMethod.Invoke(con, new object[] { attemptedPassword }); - - try - { - //Attempt to open in preferred state - con.Open(); - - // Do a dummy query to make sure we have a working db - using (var cmd = con.CreateCommand()) - { - cmd.CommandText = "SELECT COUNT(*) FROM SQLITE_MASTER"; - cmd.ExecuteScalar(); - } - } - catch - { - try - { - //We can't try anything else without a password - if (string.IsNullOrEmpty(password)) - throw; - - //Open failed, now try the reverse - attemptedPassword = attemptedPassword == null ? password : null; - - con.Close(); - if (setPwdMethod != null) - setPwdMethod.Invoke(con, new object[] { attemptedPassword }); - con.Open(); - - // Do a dummy query to make sure we have a working db - using (var cmd = con.CreateCommand()) - { - cmd.CommandText = "SELECT COUNT(*) FROM SQLITE_MASTER"; - cmd.ExecuteScalar(); - } - } - catch - { - try { con.Close(); } - catch { } - } - - //If the db is not open now, it won't open - if (con.State != System.Data.ConnectionState.Open) - throw; //Report original error - - //The open method succeeded with the non-default method, now change the password - System.Reflection.MethodInfo changePwdMethod = con.GetType().GetMethod("ChangePassword", new Type[] { typeof(string) }); - changePwdMethod.Invoke(con, new object[] { useDatabaseEncryption ? password : null }); - } - } - + /// /// Simple method for tracking if the server has crashed /// From 05b2483b4ccbc9b3f884b778053001286b55268d Mon Sep 17 00:00:00 2001 From: verhoek Date: Thu, 21 Jun 2018 18:58:55 +0200 Subject: [PATCH 3/6] Added chmoding of sqlite files after opening by sqlite. Removal of some duplication. --- .../Duplicati.Library.SQLiteHelper.csproj | 1 + .../Library/SQLiteHelper/SQLiteLoader.cs | 49 ++++++++++++------- Duplicati/Server/Program.cs | 3 +- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj b/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj index 6541ab122..12a3b4f48 100644 --- a/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj +++ b/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj @@ -35,6 +35,7 @@ + diff --git a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs index f00f087ac..6df72d1aa 100644 --- a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs +++ b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs @@ -39,9 +39,10 @@ namespace Duplicati.Library.SQLiteHelper /// Helper method with logic to handle opening a database in possibly encrypted format /// /// The SQLite connection object + /// The location of Duplicati's database. /// Specify if database is encrypted /// Encryption password - public static void OpenDatabase(System.Data.IDbConnection con, bool useDatabaseEncryption, string password) + public static void OpenDatabase(System.Data.IDbConnection con, string DatabasePath, bool useDatabaseEncryption, string password) { System.Reflection.MethodInfo setPwdMethod = con.GetType().GetMethod("SetPassword", new Type[] { typeof(string) }); string attemptedPassword; @@ -57,14 +58,9 @@ namespace Duplicati.Library.SQLiteHelper try { //Attempt to open in preferred state - con.Open(); + OpenSQLiteFile(con, DatabasePath); - // Do a dummy query to make sure we have a working db - using (var cmd = con.CreateCommand()) - { - cmd.CommandText = "SELECT COUNT(*) FROM SQLITE_MASTER"; - cmd.ExecuteScalar(); - } + TestSQLiteFile(con); } catch { @@ -80,14 +76,9 @@ namespace Duplicati.Library.SQLiteHelper con.Close(); if (setPwdMethod != null) setPwdMethod.Invoke(con, new object[] { attemptedPassword }); - con.Open(); + OpenSQLiteFile(con, DatabasePath); - // Do a dummy query to make sure we have a working db - using (var cmd = con.CreateCommand()) - { - cmd.CommandText = "SELECT COUNT(*) FROM SQLITE_MASTER"; - cmd.ExecuteScalar(); - } + TestSQLiteFile(con); } catch { @@ -126,8 +117,7 @@ namespace Duplicati.Library.SQLiteHelper con = (System.Data.IDbConnection)Activator.CreateInstance(Duplicati.Library.SQLiteHelper.SQLiteLoader.SQLiteConnectionType); if (!string.IsNullOrWhiteSpace(targetpath)) { - con.ConnectionString = "Data Source=" + targetpath; - con.Open(); + OpenSQLiteFile(con, targetpath); // Try to set the temp_dir even tough it is deprecated if (!string.IsNullOrWhiteSpace(tempdir)) @@ -240,6 +230,31 @@ namespace Duplicati.Library.SQLiteHelper return m_type; } } + + private static void OpenSQLiteFile(System.Data.IDbConnection con, string path) + { + con.ConnectionString = "Data Source=" + path; + con.Open(); + SecureDatabasePermissions(path); + } + + private static void SecureDatabasePermissions(string path) + { + if (!Library.Utility.Utility.IsClientWindows) + { + Mono.Unix.Native.Syscall.chmod(path, Mono.Unix.Native.FilePermissions.S_IRUSR | Mono.Unix.Native.FilePermissions.S_IWUSR); + } + } + + private static void TestSQLiteFile(System.Data.IDbConnection con) + { + // Do a dummy query to make sure we have a working db + using (var cmd = con.CreateCommand()) + { + cmd.CommandText = "SELECT COUNT(*) FROM SQLITE_MASTER"; + cmd.ExecuteScalar(); + } + } } /// diff --git a/Duplicati/Server/Program.cs b/Duplicati/Server/Program.cs index 5b87a0e6e..393d5e01b 100644 --- a/Duplicati/Server/Program.cs +++ b/Duplicati/Server/Program.cs @@ -596,10 +596,9 @@ namespace Duplicati.Server #else var useDatabaseEncryption = !commandlineOptions.ContainsKey("unencrypted-database") || !Library.Utility.Utility.ParseBool(commandlineOptions["unencrypted-database"], true); #endif - con.ConnectionString = "Data Source=" + DatabasePath; //Attempt to open the database, handling any encryption present - Duplicati.Library.SQLiteHelper.SQLiteLoader.OpenDatabase(con, useDatabaseEncryption, dbPassword); + Duplicati.Library.SQLiteHelper.SQLiteLoader.OpenDatabase(con, DatabasePath, useDatabaseEncryption, dbPassword); Duplicati.Library.SQLiteHelper.DatabaseUpgrader.UpgradeDatabase(con, DatabasePath, typeof(Database.Connection)); } From edc866c17ee4bb953a09ec33d87d2caa5c18200a Mon Sep 17 00:00:00 2001 From: verhoek Date: Fri, 22 Jun 2018 09:47:46 +0200 Subject: [PATCH 4/6] Make Codacy happier. --- Duplicati/Library/SQLiteHelper/SQLiteLoader.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs index 6df72d1aa..8ffdcf1b0 100644 --- a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs +++ b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs @@ -44,7 +44,7 @@ namespace Duplicati.Library.SQLiteHelper /// Encryption password public static void OpenDatabase(System.Data.IDbConnection con, string DatabasePath, bool useDatabaseEncryption, string password) { - System.Reflection.MethodInfo setPwdMethod = con.GetType().GetMethod("SetPassword", new Type[] { typeof(string) }); + System.Reflection.MethodInfo setPwdMethod = con.GetType().GetMethod("SetPassword", new [] { typeof(string) }); string attemptedPassword; if (!useDatabaseEncryption || string.IsNullOrEmpty(password)) @@ -91,7 +91,7 @@ namespace Duplicati.Library.SQLiteHelper throw; //Report original error //The open method succeeded with the non-default method, now change the password - System.Reflection.MethodInfo changePwdMethod = con.GetType().GetMethod("ChangePassword", new Type[] { typeof(string) }); + System.Reflection.MethodInfo changePwdMethod = con.GetType().GetMethod("ChangePassword", new [] { typeof(string) }); changePwdMethod.Invoke(con, new object[] { useDatabaseEncryption ? password : null }); } } From eabb840a4cb984b16b31c9fd2d89f703bd05ba5c Mon Sep 17 00:00:00 2001 From: verhoek Date: Fri, 22 Jun 2018 15:02:18 +0200 Subject: [PATCH 5/6] Only add mono.posix on non-windows. --- .../Duplicati.Library.SQLiteHelper.csproj | 7 ++- .../Library/SQLiteHelper/SQLiteLoader.cs | 58 ++++++++++--------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj b/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj index 12a3b4f48..56d20d0e5 100644 --- a/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj +++ b/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj @@ -32,9 +32,14 @@ false false + + _NOTWINDOWS + + + @@ -87,4 +92,4 @@ Duplicati.Library.Logging - \ No newline at end of file + diff --git a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs index 8ffdcf1b0..10f1f5df7 100644 --- a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs +++ b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs @@ -24,11 +24,11 @@ using System.Text; namespace Duplicati.Library.SQLiteHelper { public static class SQLiteLoader - { - /// + { + /// /// The tag used for logging /// - private static readonly string LOGTAG = Logging.Log.LogTagFromType(typeof(SQLiteLoader)); + private static readonly string LOGTAG = Logging.Log.LogTagFromType(typeof(SQLiteLoader)); /// /// A cached copy of the type @@ -44,7 +44,7 @@ namespace Duplicati.Library.SQLiteHelper /// Encryption password public static void OpenDatabase(System.Data.IDbConnection con, string DatabasePath, bool useDatabaseEncryption, string password) { - System.Reflection.MethodInfo setPwdMethod = con.GetType().GetMethod("SetPassword", new [] { typeof(string) }); + System.Reflection.MethodInfo setPwdMethod = con.GetType().GetMethod("SetPassword", new[] { typeof(string) }); string attemptedPassword; if (!useDatabaseEncryption || string.IsNullOrEmpty(password)) @@ -91,7 +91,7 @@ namespace Duplicati.Library.SQLiteHelper throw; //Report original error //The open method succeeded with the non-default method, now change the password - System.Reflection.MethodInfo changePwdMethod = con.GetType().GetMethod("ChangePassword", new [] { typeof(string) }); + System.Reflection.MethodInfo changePwdMethod = con.GetType().GetMethod("ChangePassword", new[] { typeof(string) }); changePwdMethod.Invoke(con, new object[] { useDatabaseEncryption ? password : null }); } } @@ -148,9 +148,9 @@ namespace Duplicati.Library.SQLiteHelper finally { System.Environment.SetEnvironmentVariable("SQLITE_TMPDIR", prev); - } - - + } + + return con; } @@ -194,16 +194,18 @@ namespace Duplicati.Library.SQLiteHelper catch { } } - } else { - //On Mono, we try to find the Mono version of SQLite - - //This secret environment variable can be used to support older installations + } + else + { + //On Mono, we try to find the Mono version of SQLite + + //This secret environment variable can be used to support older installations var envvalue = System.Environment.GetEnvironmentVariable("DISABLE_MONO_DATA_SQLITE"); if (!Utility.Utility.ParseBool(envvalue, envvalue != null)) { - foreach(var asmversion in new string[] {"4.0.0.0", "2.0.0.0"}) + foreach (var asmversion in new string[] { "4.0.0.0", "2.0.0.0" }) { - try + try { Type t = System.Reflection.Assembly.Load(string.Format("Mono.Data.Sqlite, Version={0}, Culture=neutral, PublicKeyToken=0738eb9f132ed756", asmversion)).GetType("Mono.Data.Sqlite.SqliteConnection"); if (t != null && t.GetInterface("System.Data.IDbConnection", false) != null) @@ -214,13 +216,15 @@ namespace Duplicati.Library.SQLiteHelper m_type = t; return m_type; } - } - - } catch { + } + + } + catch + { } - } - - Logging.Log.WriteVerboseMessage(LOGTAG, "FailedToLoadSQLite", "Failed to load Mono.Data.Sqlite.SqliteConnection, reverting to built-in."); + } + + Logging.Log.WriteVerboseMessage(LOGTAG, "FailedToLoadSQLite", "Failed to load Mono.Data.Sqlite.SqliteConnection, reverting to built-in."); } } @@ -234,16 +238,14 @@ namespace Duplicati.Library.SQLiteHelper private static void OpenSQLiteFile(System.Data.IDbConnection con, string path) { con.ConnectionString = "Data Source=" + path; - con.Open(); - SecureDatabasePermissions(path); - } - - private static void SecureDatabasePermissions(string path) - { - if (!Library.Utility.Utility.IsClientWindows) - { + con.Open(); + + #if _NOTWINDOWS + if (!Library.Utility.Utility.IsClientWindows) + { Mono.Unix.Native.Syscall.chmod(path, Mono.Unix.Native.FilePermissions.S_IRUSR | Mono.Unix.Native.FilePermissions.S_IWUSR); } + #endif } private static void TestSQLiteFile(System.Data.IDbConnection con) From fc5f5828002fc673b13cbd88c9726de1a3c8b823 Mon Sep 17 00:00:00 2001 From: verhoek Date: Fri, 22 Jun 2018 20:01:23 +0200 Subject: [PATCH 6/6] moved dll filename var to const. Only change permissions on new file creation. --- .../Duplicati.Library.SQLiteHelper.csproj | 2 +- .../Library/SQLiteHelper/SQLiteLoader.cs | 25 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj b/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj index 56d20d0e5..7dc1ad452 100644 --- a/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj +++ b/Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj @@ -33,7 +33,7 @@ false - _NOTWINDOWS + _NOT_WINDOWS diff --git a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs index 10f1f5df7..1264f4e5a 100644 --- a/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs +++ b/Duplicati/Library/SQLiteHelper/SQLiteLoader.cs @@ -19,6 +19,7 @@ #endregion using System; using System.Collections.Generic; +using System.IO; using System.Text; namespace Duplicati.Library.SQLiteHelper @@ -35,6 +36,9 @@ namespace Duplicati.Library.SQLiteHelper /// private static Type m_type = null; + + private const string SQLiteAssembly = "System.Data.SQLite.dll"; + /// /// Helper method with logic to handle opening a database in possibly encrypted format /// @@ -163,7 +167,6 @@ namespace Duplicati.Library.SQLiteHelper { if (m_type == null) { - var filename = "System.Data.SQLite.dll"; var basePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "SQLite"); // Set this to make SQLite preload automatically @@ -177,12 +180,12 @@ namespace Duplicati.Library.SQLiteHelper //If we run with MS.Net we can use the mixed mode assemblies if (Environment.Is64BitProcess) { - if (System.IO.File.Exists(System.IO.Path.Combine(System.IO.Path.Combine(basePath, "win64"), filename))) + if (System.IO.File.Exists(System.IO.Path.Combine(System.IO.Path.Combine(basePath, "win64"), SQLiteAssembly))) assemblyPath = System.IO.Path.Combine(basePath, "win64"); } else { - if (System.IO.File.Exists(System.IO.Path.Combine(System.IO.Path.Combine(basePath, "win32"), filename))) + if (System.IO.File.Exists(System.IO.Path.Combine(System.IO.Path.Combine(basePath, "win32"), SQLiteAssembly))) assemblyPath = System.IO.Path.Combine(basePath, "win32"); } @@ -228,7 +231,7 @@ namespace Duplicati.Library.SQLiteHelper } } - m_type = System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(assemblyPath, filename)).GetType("System.Data.SQLite.SQLiteConnection"); + m_type = System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(assemblyPath, SQLiteAssembly)).GetType("System.Data.SQLite.SQLiteConnection"); } return m_type; @@ -237,11 +240,21 @@ namespace Duplicati.Library.SQLiteHelper private static void OpenSQLiteFile(System.Data.IDbConnection con, string path) { + // Check if SQLite database exists before opening a connection to it. + // This information is used to 'fix' permissions on a newly created file. + #if _NOT_WINDOWS + bool fileExists = false; + if (!Library.Utility.Utility.IsClientWindows) + { + fileExists = File.Exists(path); + } + #endif + con.ConnectionString = "Data Source=" + path; con.Open(); - #if _NOTWINDOWS - if (!Library.Utility.Utility.IsClientWindows) + #if _NOT_WINDOWS + if (!Library.Utility.Utility.IsClientWindows && !fileExists) { Mono.Unix.Native.Syscall.chmod(path, Mono.Unix.Native.FilePermissions.S_IRUSR | Mono.Unix.Native.FilePermissions.S_IWUSR); }