Skip to content

Commit

Permalink
Merge pull request #62 from lukeIam/release/0.7.0
Browse files Browse the repository at this point in the history
Release/0.7.0
  • Loading branch information
lukeIam authored Mar 12, 2023
2 parents 29bfbd3 + ae6aae2 commit 1f5653b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 30 deletions.
82 changes: 60 additions & 22 deletions Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using KeePassLib.Keys;
using KeePassLib.Serialization;
using KeePassLib.Utility;
using System.Text.RegularExpressions;

namespace KeePassSubsetExport
{
Expand Down Expand Up @@ -90,7 +91,7 @@ private static PwGroup FindSettingsGroup(PwDatabase sourceDb, string settingsGro

private static PwGroup FindGroupRecursive(PwGroup startGroup, string groupName)
{
if(startGroup.Name == groupName)
if (startGroup.Name == groupName)
{
return startGroup;
}
Expand Down Expand Up @@ -183,13 +184,17 @@ private static void CopyToNewDb(PwDatabase sourceDb, Settings settings)
CompositeKey key = CreateCompositeKey(settings);

// Trigger an export for multiple target dbs (as we could also write to en existing db coping is not an option)
foreach (string targetFilePathLoopVar in settings.TargetFilePath.Split(',')) {
foreach (string targetFilePathLoopVar in settings.TargetFilePath.Split(','))
{
string targetFilePath = targetFilePathLoopVar;
// Create or open the target database
PwDatabase targetDatabase = CreateTargetDatabase(sourceDb, settings, key, ref targetFilePath);

// Copy database settings
CopyDatabaseSettings(sourceDb, targetDatabase);
if (settings.ExportDatebaseSettings)
{
// Copy database settings
CopyDatabaseSettings(sourceDb, targetDatabase);
}

// Copy key derivation function parameters
CopyKdfSettings(sourceDb, settings, targetDatabase);
Expand Down Expand Up @@ -402,39 +407,59 @@ private static void CopyEntriesAndGroups(PwDatabase sourceDb, Settings settings,
FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.UserNameField));
peNew.Strings.Set(PwDefs.PasswordField,
FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.PasswordField));

// Handle custom icon
HandleCustomIcon(targetDatabase, sourceDb, entry);
}
}

private static void SaveTargetDatabase(string targetFilePath, PwDatabase targetDatabase, bool overrideTargetDatabase)
{
if (!overrideTargetDatabase && File.Exists(targetFilePath))
{
// Save changes to existing target database
targetDatabase.Save(new NullStatusLogger());
}
else
Regex rg = new Regex(@".+://.+", RegexOptions.None, TimeSpan.FromMilliseconds(200));
if (!rg.IsMatch(targetFilePath))
{
// Create target folder (if not exist)
string targetFolder = Path.GetDirectoryName(targetFilePath);

if (targetFolder == null)
// local file path
if (!overrideTargetDatabase && File.Exists(targetFilePath))
{
throw new ArgumentException("Can't get target folder.");
// Save changes to existing target database
targetDatabase.Save(new NullStatusLogger());
}
else
{
// Create target folder (if not exist)
string targetFolder = Path.GetDirectoryName(targetFilePath);

Directory.CreateDirectory(targetFolder);
if (targetFolder == null)
{
throw new ArgumentException("Can't get target folder.");
}

// Save the new database under the target path
KdbxFile kdbx = new KdbxFile(targetDatabase);
Directory.CreateDirectory(targetFolder);

using (FileStream outputStream = new FileStream(targetFilePath, FileMode.Create))
{
kdbx.Save(outputStream, null, KdbxFormat.Default, new NullStatusLogger());
// Save the new database under the target path
KdbxFile kdbx = new KdbxFile(targetDatabase);

using (FileStream outputStream = new FileStream(targetFilePath, FileMode.Create))
{
kdbx.Save(outputStream, null, KdbxFormat.Default, new NullStatusLogger());
}
}
}
else
{
// Non local file (ftp, webdav, ...)
Uri targetUrl = new Uri(targetFilePath);
string[] userAndPw = targetUrl.UserInfo.Split(':');
IOConnectionInfo conInfo = new IOConnectionInfo
{
Path = Regex.Replace(targetUrl.AbsoluteUri, @"(?<=//)[^@]+@", "", RegexOptions.None, TimeSpan.FromMilliseconds(200)),
CredSaveMode = IOCredSaveMode.NoSave,
UserName = userAndPw[0],
Password = userAndPw[1]
};

targetDatabase.SaveAs(conInfo, false, new NullStatusLogger());
}
}

private static void CopyKdfSettings(PwDatabase sourceDb, Settings settings, PwDatabase targetDatabase)
Expand Down Expand Up @@ -497,6 +522,19 @@ private static void CopyDatabaseSettings(PwDatabase sourceDb, PwDatabase targetD

private static PwDatabase CreateTargetDatabase(PwDatabase sourceDb, Settings settings, CompositeKey key, ref string targetFilePath)
{
Regex rg = new Regex(@".+://.+", RegexOptions.None, TimeSpan.FromMilliseconds(200));
if (rg.IsMatch(targetFilePath))
{
// Non local file (ftp, webdav, ...)
// Create a new database
PwDatabase targetDatabaseForUri = new PwDatabase();

// Apply the created key to the new database
targetDatabaseForUri.New(new IOConnectionInfo(), key);

return targetDatabaseForUri;
}

// Default to same folder as sourceDb for target if no directory is specified
if (!Path.IsPathRooted(targetFilePath))
{
Expand Down
3 changes: 2 additions & 1 deletion KeePassSubsetExport.Tests/KeePassSubsetExport.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>KeePassSubsetExport.Tests</RootNamespace>
<AssemblyName>KeePassSubsetExport.Tests</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
Expand All @@ -19,6 +19,7 @@
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
2 changes: 1 addition & 1 deletion KeePassSubsetExport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>KeePassSubsetExport</RootNamespace>
<AssemblyName>KeePassSubsetExport</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

[assembly: Guid("3463A091-5683-487E-843F-47086294C5C1")]

[assembly: AssemblyVersion("0.6.0.0")]
[assembly: AssemblyFileVersion("0.6.0.0")]
[assembly: AssemblyVersion("0.7.0.0")]
[assembly: AssemblyFileVersion("0.7.0.0")]

[assembly: InternalsVisibleTo("KeePassSubsetExport.Tests")]
2 changes: 1 addition & 1 deletion Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public class Settings
/// If true, only Username and Password will be exported to the target database.
/// </summary>
public bool ExportUserAndPassOnly { get; private set; }
/// <summary>
/// If true, the settings of the source database will be exported to the target database.
/// </summary>
public bool ExportDatebaseSettings { get; private set; }

// Private constructor
private Settings()
Expand Down Expand Up @@ -112,7 +116,7 @@ private static uint GetUIntValue(string key, PwEntry settingsEntry)
/// <param name="settingsEntry">The entry to read the settings from.</param>
/// <param name="sourceDb">A database to resolve refs in the password field.</param>
/// <returns>A settings object containing all the settings for this job.</returns>
public static Settings Parse(PwEntry settingsEntry, PwDatabase sourceDb=null)
public static Settings Parse(PwEntry settingsEntry, PwDatabase sourceDb = null)
{
return new Settings()
{
Expand All @@ -131,7 +135,8 @@ public static Settings Parse(PwEntry settingsEntry, PwDatabase sourceDb=null)
Argon2ParamMemory = GetUlongValue("SubsetExport_Argon2ParamMemory", settingsEntry),
Argon2ParamParallelism = GetUIntValue("SubsetExport_Argon2ParamParallelism", settingsEntry),
Disabled = (settingsEntry.Expires && DateTime.Now > settingsEntry.ExpiryTime),
ExportUserAndPassOnly = settingsEntry.Strings.ReadSafe("SubsetExport_ExportUserAndPassOnly").ToLower().Trim() == "true"
ExportUserAndPassOnly = settingsEntry.Strings.ReadSafe("SubsetExport_ExportUserAndPassOnly").ToLower().Trim() == "true",
ExportDatebaseSettings = settingsEntry.Strings.ReadSafe("SubsetExport_ExportDatebaseSettings").ToLower().Trim() != "false",
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion keepass.version
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
:
KeePassSubsetExport:0.6.0
KeePassSubsetExport:0.7.0
:

0 comments on commit 1f5653b

Please sign in to comment.