From f9bb47c987998adae8a2a8c06c0c22e00204cb0c Mon Sep 17 00:00:00 2001 From: aalbng Date: Thu, 22 Dec 2016 13:17:21 +0100 Subject: [PATCH] Added optional portable/admin configuration --- AdvancedConnectPlugin/AdvancedConnectExt.cs | 30 ++-- .../AdvancedConnectPlugin.csproj | 1 + AdvancedConnectPlugin/CHANGELOG.txt | 3 + AdvancedConnectPlugin/Data/Settings.cs | 23 ++- .../Tools/ExecutableDirectory.cs | 32 ++++ Libs/KeePass.config.xml | 141 ++++++++++++++++++ README.md | 2 + 7 files changed, 217 insertions(+), 15 deletions(-) create mode 100644 AdvancedConnectPlugin/Tools/ExecutableDirectory.cs create mode 100644 Libs/KeePass.config.xml diff --git a/AdvancedConnectPlugin/AdvancedConnectExt.cs b/AdvancedConnectPlugin/AdvancedConnectExt.cs index 57ab966..32dec9d 100644 --- a/AdvancedConnectPlugin/AdvancedConnectExt.cs +++ b/AdvancedConnectPlugin/AdvancedConnectExt.cs @@ -10,12 +10,12 @@ with the License. You may obtain a copy of the License at distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ +using AdvancedConnectPlugin.Tools; using KeePass.Plugins; using System; using System.Drawing; using System.IO; - - +using System.Reflection; namespace AdvancedConnectPlugin { @@ -61,18 +61,28 @@ public override void Terminate() this.toolsMenuExtension.removeToolsMenuExtensions(); } - //Build configuration folder structure + //Build configuration public void buildConfigPath() { - //Set and create directory - String configDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "KeePass/"); - System.IO.Directory.CreateDirectory(configDirectory); - - //Set configuration file path + //Set configuration file name String configFileName = "AdvancedConnect.xml"; - this.pathToPluginConfigFile = Path.Combine(configDirectory, configFileName); - } + String configDirectory = String.Empty; + //Check if portable / admin configuration is available + if (File.Exists(Path.Combine(ExecutableDirectory.GetExecutableDirectory(), configFileName))) + { + //Set directory in installation path (portable configuration) + configDirectory = ExecutableDirectory.GetExecutableDirectory(); + this.pathToPluginConfigFile = Path.Combine(configDirectory, configFileName); + } + else + { + //Set and create directory within appdata roaming (user configuration) + configDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "KeePass/"); + System.IO.Directory.CreateDirectory(configDirectory); + this.pathToPluginConfigFile = Path.Combine(configDirectory, configFileName); + } + } } } diff --git a/AdvancedConnectPlugin/AdvancedConnectPlugin.csproj b/AdvancedConnectPlugin/AdvancedConnectPlugin.csproj index c668c73..8a840bc 100644 --- a/AdvancedConnectPlugin/AdvancedConnectPlugin.csproj +++ b/AdvancedConnectPlugin/AdvancedConnectPlugin.csproj @@ -72,6 +72,7 @@ + diff --git a/AdvancedConnectPlugin/CHANGELOG.txt b/AdvancedConnectPlugin/CHANGELOG.txt index e39577c..fe86d76 100644 --- a/AdvancedConnectPlugin/CHANGELOG.txt +++ b/AdvancedConnectPlugin/CHANGELOG.txt @@ -1,3 +1,6 @@ +1.0.4 +Added optional portable/admin configuration + 1.0.3 Added Unix/Linux compatibility diff --git a/AdvancedConnectPlugin/Data/Settings.cs b/AdvancedConnectPlugin/Data/Settings.cs index b649b6c..184097a 100644 --- a/AdvancedConnectPlugin/Data/Settings.cs +++ b/AdvancedConnectPlugin/Data/Settings.cs @@ -63,16 +63,29 @@ public Settings load() { XmlSerializer serializerObj = new XmlSerializer(typeof(Settings)); FileStream readFileStream = new FileStream(this.plugin.pathToPluginConfigFile, FileMode.Open, FileAccess.Read, FileShare.Read); - loadedSettings = (Settings)serializerObj.Deserialize(readFileStream); - readFileStream.Close(); + + //Try to parse settings from configuration file. Create new configuration on parsing error + try + { + //Load configuration + loadedSettings = (Settings)serializerObj.Deserialize(readFileStream); + } + catch (InvalidOperationException) + { + //Create new configuration on error + loadedSettings = new Settings(); + } + finally + { + readFileStream.Close(); + } } else { + //Create new configuration file if there is no configuration file present loadedSettings = new Settings(); } - - - + loadedSettings.plugin = this.plugin; return loadedSettings; } diff --git a/AdvancedConnectPlugin/Tools/ExecutableDirectory.cs b/AdvancedConnectPlugin/Tools/ExecutableDirectory.cs new file mode 100644 index 0000000..4e07c67 --- /dev/null +++ b/AdvancedConnectPlugin/Tools/ExecutableDirectory.cs @@ -0,0 +1,32 @@ +/* +Copyright 2016 TGW Software Services GmbH + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is +distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +*/ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Text; + +namespace AdvancedConnectPlugin.Tools +{ + public static class ExecutableDirectory + { + //Returns the assembly directory + public static string GetExecutableDirectory() + { + string codeBase = Assembly.GetEntryAssembly().CodeBase; + UriBuilder uri = new UriBuilder(codeBase); + string path = Uri.UnescapeDataString(uri.Path); + return Path.GetDirectoryName(path); + } + } +} diff --git a/Libs/KeePass.config.xml b/Libs/KeePass.config.xml new file mode 100644 index 0000000..8b4b073 --- /dev/null +++ b/Libs/KeePass.config.xml @@ -0,0 +1,141 @@ + + + + false + true + + + + + Obf + NoSave + + + 12 + + + + + false + true + + + + + + + + + + 625 + 317 + 670 + 525 + 0.8313253 + 0.250764519 + Default + + + + + + Title + 90 + + + UserName + 90 + + + Password + 90 + true + + + Url + 90 + + + Notes + 90 + + + 0 1 2 3 4 + + Ascending + + + + + + + Microsoft Sans Serif + 8.25 + Point + + false + + + Courier New + 8.25 + Point + + false + + WinVistaBlack + + Microsoft Sans Serif + 8.25 + Point + + false + + 0 + 0 + 0 + + + + 0 + 0 + + + + 0 + 0 + + + + + + CharSet + 20 + ULD_______ + + + CharSet + 20 + ULD_______ + + + + + 0 + + InvariantCultureIgnoreCase + + + + + 393281 + 0 + 393291 + 0 + + 1 + + + System + + + \ No newline at end of file diff --git a/README.md b/README.md index 9b96537..434211a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ AdvancedConnect is a plugin for [KeePass](http://keepass.info) password manager On windows operation system the native remote desktop client have no option to provide the username and password via command-line. The built-in rdp support is a little workaround wich provides this functionality. You have to configure the keepass connection field (containing ip or hostname), a connection method (e.g. rdp) and you can set additional parameters (e.g. /w:1440 /h:900).
- Use the **Applications** tab in the **Options** dialog to configure your connection applications.
The **Path** and **Commandline Options** column is also supporting keepass placeholders and OS environment variables. +- To use a **portable configuration** you have to create a emtpy file named **AdvancedConnect.xml** next to **KeePass.exe**.
+(If a portable\admin configuration file is available, the default configuration *%appdata%\Keepass\AdvancedConnect.xml* will be ignored) ## Example