private static void CopyEntriesAndGroups(PwDatabase sourceDb, Settings settings, PwObjectList <PwEntry> entries, PwDatabase targetDatabase) { foreach (PwEntry entry in entries) { // Get (or create in case its the first sync) the target group in the target database (including hierarchy) PwGroup targetGroup = TargetGroupInDatebase(entry, targetDatabase, sourceDb); PwEntry peNew = targetGroup.FindEntry(entry.Uuid, bSearchRecursive: false); // Check if the target entry is newer than the source entry && peNew.LastModificationTime > entry.LastModificationTime if (peNew != null && peNew.LastModificationTime.CompareTo(entry.LastModificationTime) > 0) { CloneEntry(targetDatabase, sourceDb, peNew, entry, targetGroup, settings); continue; } // Handle Duplicates entries' Uuids PwEntry duplicatedEntry = targetDatabase.RootGroup.FindEntry(entry.Uuid, true); if (duplicatedEntry != null && duplicatedEntry.ParentGroup.Uuid.ToHexString() != targetGroup.Uuid.ToHexString()) { DeleteEntry(duplicatedEntry, targetDatabase); } CloneEntry(sourceDb, targetDatabase, entry, peNew, targetGroup, settings); } }
private static void CopyEntriesAndGroups(PwDatabase sourceDb, Settings settings, PwObjectList <PwEntry> entries, PwDatabase targetDatabase) { foreach (PwEntry entry in entries) { // Get or create the target group in the target database (including hierarchy) PwGroup targetGroup = settings.FlatExport ? targetDatabase.RootGroup : CreateTargetGroupInDatebase(entry, targetDatabase, sourceDb); PwEntry peNew = null; if (!settings.OverrideTargetDatabase) { peNew = targetGroup.FindEntry(entry.Uuid, bSearchRecursive: false); // Check if the target entry is newer than the source entry if (settings.OverrideEntryOnlyNewer && peNew != null && peNew.LastModificationTime > entry.LastModificationTime) { // Yes -> skip this entry continue; } } // Was no existing entry in the target database found? if (peNew == null) { // Create a new entry peNew = new PwEntry(false, false); peNew.Uuid = entry.Uuid; // Add entry to the target group in the new database targetGroup.AddEntry(peNew, true); } // Clone entry properties peNew.AssignProperties(entry, false, true, true); // Handle custom icon HandleCustomIcon(targetDatabase, sourceDb, entry); } }
//[InlineData(false)] /// <summary> /// Delete the first entry in the list. /// </summary> public void DeleteEntryTests(bool permanent) { PwGroup rootGroup = passxyz.PxDb.RootGroup; var entry1 = rootGroup.Entries.GetAt(0); var uuid = entry1.Uuid; Debug.WriteLine($"Entry {entry1.Strings.ReadSafe("Title")} is deleted."); passxyz.PxDb.DeleteEntry(entry1, permanent); var entry2 = rootGroup.FindEntry(uuid, true); if (permanent) { Assert.Null(entry2); } else { Assert.NotNull(entry2); } }
public void PrepareForm() { if (KeePassPasswordChangerExt.PrepareDatabase()) { DialogResult allEntriesResult = MessageBox.Show("Notice: Please read the about section of the plugin. This is an alpha release. Backup your Database before you use this plugin.\r\n\r\nDo you want to proceed?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (allEntriesResult == DialogResult.No) { KeePassPasswordChangerExt.ExtentEntryNote("User aborted..."); this.Close(); } else { foreach (var pwEntry in KeePassPasswordChangerExt._mHost.MainWindow.ActiveDatabase.RootGroup.GetEntries(true)) { try { if (KeePassPasswordChangerExt.GroupPlugin.FindEntry(pwEntry.Uuid, true) != null) { continue; } if (pwEntry.Strings.Get(PwDefs.UrlField) == null) { continue; } string uri = pwEntry.Strings.Get(PwDefs.UrlField).ReadString(); int count = 0; Template template = null; TemplateManagement.LoadTemplates(); foreach (var availableTemplate in TemplateManagement.AvailableTemplates) { if (availableTemplate.Value.BoundUrl.IsRegex.Value) { if (Regex.IsMatch(uri, availableTemplate.Value.BoundUrl.Value.Value)) { template = availableTemplate.Value; count++; } } else { if (uri.Contains(availableTemplate.Value.BoundUrl.Value.Value) || availableTemplate.Value.BoundUrl.Value.Value.Contains(uri)) { template = availableTemplate.Value; count++; } } } switch (count) { case 0: KeePassPasswordChangerExt.ExtentEntryNote("Ignoring entry " + pwEntry.Strings.Get(PwDefs.TitleField).ReadString() + "(UID: " + pwEntry.Uuid.ToString() + ") it has no template"); continue; case 1: { PwGroup dustbin = KeePassPasswordChangerExt._mHost.MainWindow.ActiveDatabase.RootGroup.FindGroup( KeePassPasswordChangerExt._mHost.MainWindow.ActiveDatabase.RecycleBinUuid, true); if (dustbin != null) { if (dustbin.FindEntry(pwEntry.Uuid, true) != null) { KeePassPasswordChangerExt.ExtentEntryNote("Ignoring entry " + pwEntry.Strings.Get( PwDefs.TitleField) .ReadString() + "(UID: " + pwEntry.Uuid.ToString() + ") because it is dustbinned"); continue; } if (DateTimeNotLaterThan != DateTime.MinValue && pwEntry.LastModificationTime >= DateTimeNotLaterThan) { KeePassPasswordChangerExt.ExtentEntryNote("Ignoring entry " + pwEntry.Strings.Get( PwDefs.TitleField) .ReadString() + "(UID: " + pwEntry.Uuid.ToString() + ") because was modified @ " + pwEntry .LastModificationTime .ToString() + "(UTC) and Limit is: " + DateTimeNotLaterThan .ToString() + "(UTC)"); continue; } } KeePassPasswordChangerExt.ExtentEntryNote("Entry " + pwEntry.Strings.Get(PwDefs.TitleField).ReadString() + "(UID: " + pwEntry.Uuid.ToString() + ") matched on the template " + template.Name); Template templateSibling = (Template)template.Clone(); List <KeyValuePairEx <string, ProtectedString> > parameters = new List <KeyValuePairEx <string, ProtectedString> >(); List <string> requiredParameters = template.GetRequiredKeepassVariables(); foreach (var requiredKeepassVariable in requiredParameters) { try { if ( BaseObject.ExtractSinglePlaceholderToString(requiredKeepassVariable) == "") { ProtectedString newPassword = KeePassPasswordChangerExt .GeneratePassword( template.PasswordCreationPolicy); parameters.Add( new KeyValuePairEx <string, ProtectedString>( BaseObject.ConvertStringToPlaceholderString(""), newPassword)); continue; } bool foundPwDef = false; foreach (var pwdef in PwDefs.GetStandardFields()) { if (pwdef == BaseObject.ExtractSinglePlaceholderToString( requiredKeepassVariable)) { parameters.Add( new KeyValuePairEx <string, ProtectedString>( requiredKeepassVariable, pwEntry.Strings.Get( BaseObject.ExtractSinglePlaceholderToString( requiredKeepassVariable)))); foundPwDef = true; break; } } if (!foundPwDef) { parameters.Add( new KeyValuePairEx <string, ProtectedString>( requiredKeepassVariable, new ProtectedString(true, requiredKeepassVariable))); } } catch (Exception ex) { ExceptionHandling.Handling.GetException("Unexpected", ex); } } if (requiredParameters.Count != parameters.Count) { KeePassPasswordChangerExt.ExtentEntryNote( "Sorry, i could not gather all required parameters from the password entry " + pwEntry.Strings.Get(PwDefs.TitleField).ReadString()); } else { while (true) { try { templateSibling.GenerateNewUtid(KeePassPasswordChangerExt.Counter++.ToString()); TemplateManagement.LockTemplates.AcquireWriterLock(Options.LockTimeOut); templateSibling.InitializeTemplate(parameters, KeePassPasswordChangerExt.CefControl, pwEntry.Strings.Get(PwDefs.TitleField).ReadString(), pwEntry.Uuid); TemplateManagement.TemplatesReady.Add(templateSibling.UTID, templateSibling); break; } catch (ApplicationException ex) { ExceptionHandling.Handling.GetException("ReaderWriterLock", ex); } finally { if (TemplateManagement.LockTemplates.IsWriterLockHeld) { TemplateManagement.LockTemplates.ReleaseWriterLock(); } } } } } break; default: //Write detailed list! KeePassPasswordChangerExt.ExtentEntryNote("Entry \"" + pwEntry.Strings.Get(PwDefs.TitleField).ReadString() + "\" matches on too many templates, ignoring this one, sorry"); continue; } } catch (Exception ex) { ExceptionHandling.Handling.GetException("Unexpected", ex); } } } } else { return; } KeePassPasswordChangerExt.UnlockDatabase(); CheckValues(); }
private static void PrepareModDbForMerge(PwDatabase pd, PwDatabase pdOrg) { PwGroup pgRootOrg = pdOrg.RootGroup; PwGroup pgRootNew = pd.RootGroup; if (pgRootNew == null) { Debug.Assert(false); return; } PwCompareOptions pwCmp = (PwCompareOptions.IgnoreParentGroup | PwCompareOptions.NullEmptyEquivStd); DateTime dtNow = DateTime.UtcNow; GroupHandler ghOrg = delegate(PwGroup pg) { PwGroup pgNew = pgRootNew.FindGroup(pg.Uuid, true); if (pgNew == null) { AddDeletedObject(pd, pg.Uuid); return(true); } if (!pgNew.EqualsGroup(pg, (pwCmp | PwCompareOptions.PropertiesOnly), MemProtCmpMode.Full)) { pgNew.Touch(true, false); } PwGroup pgParentA = pg.ParentGroup; PwGroup pgParentB = pgNew.ParentGroup; if ((pgParentA != null) && (pgParentB != null)) { if (!pgParentA.Uuid.Equals(pgParentB.Uuid)) { pgNew.LocationChanged = dtNow; } } else if ((pgParentA == null) && (pgParentB == null)) { } else { pgNew.LocationChanged = dtNow; } return(true); }; EntryHandler ehOrg = delegate(PwEntry pe) { PwEntry peNew = pgRootNew.FindEntry(pe.Uuid, true); if (peNew == null) { AddDeletedObject(pd, pe.Uuid); return(true); } if (!peNew.EqualsEntry(pe, pwCmp, MemProtCmpMode.Full)) { peNew.Touch(true, false); bool bRestoreHistory = false; if (peNew.History.UCount != pe.History.UCount) { bRestoreHistory = true; } else { for (uint u = 0; u < pe.History.UCount; ++u) { if (!peNew.History.GetAt(u).EqualsEntry( pe.History.GetAt(u), pwCmp, MemProtCmpMode.CustomOnly)) { bRestoreHistory = true; break; } } } if (bRestoreHistory) { peNew.History = pe.History.CloneDeep(); foreach (PwEntry peHistNew in peNew.History) { peHistNew.ParentGroup = peNew.ParentGroup; } } } PwGroup pgParentA = pe.ParentGroup; PwGroup pgParentB = peNew.ParentGroup; if ((pgParentA != null) && (pgParentB != null)) { if (!pgParentA.Uuid.Equals(pgParentB.Uuid)) { peNew.LocationChanged = dtNow; } } else if ((pgParentA == null) && (pgParentB == null)) { } else { peNew.LocationChanged = dtNow; } return(true); }; pgRootOrg.TraverseTree(TraversalMethod.PreOrder, ghOrg, ehOrg); }
private static void PrepareModDbForMerge(PwDatabase pd, PwDatabase pdOrg) { PwGroup pgRootOrg = pdOrg.RootGroup; PwGroup pgRootNew = pd.RootGroup; if (pgRootNew == null) { Debug.Assert(false); return; } const PwCompareOptions cmpOpt = (PwCompareOptions.IgnoreParentGroup | PwCompareOptions.IgnoreHistory | PwCompareOptions.NullEmptyEquivStd); const MemProtCmpMode cmpMem = MemProtCmpMode.CustomOnly; DateTime dtNow = DateTime.UtcNow; GroupHandler ghOrg = delegate(PwGroup pg) { PwGroup pgNew = pgRootNew.FindGroup(pg.Uuid, true); if (pgNew == null) { AddDeletedObject(pd, pg.Uuid); return(true); } if (!pgNew.EqualsGroup(pg, (cmpOpt | PwCompareOptions.PropertiesOnly), cmpMem)) { pgNew.Touch(true, false); } PwGroup pgParentA = pg.ParentGroup; PwGroup pgParentB = pgNew.ParentGroup; if ((pgParentA != null) && (pgParentB != null)) { if (!pgParentA.Uuid.Equals(pgParentB.Uuid)) { pgNew.LocationChanged = dtNow; } } else if ((pgParentA == null) && (pgParentB == null)) { } else { pgNew.LocationChanged = dtNow; } return(true); }; EntryHandler ehOrg = delegate(PwEntry pe) { PwEntry peNew = pgRootNew.FindEntry(pe.Uuid, true); if (peNew == null) { AddDeletedObject(pd, pe.Uuid); return(true); } // Restore history entries peNew.History = pe.History.CloneDeep(); foreach (PwEntry peHistNew in peNew.History) { peHistNew.ParentGroup = peNew.ParentGroup; } if (!peNew.EqualsEntry(pe, cmpOpt, cmpMem)) { peNew.Touch(true, false); } PwGroup pgParentA = pe.ParentGroup; PwGroup pgParentB = peNew.ParentGroup; if ((pgParentA != null) && (pgParentB != null)) { if (!pgParentA.Uuid.Equals(pgParentB.Uuid)) { peNew.LocationChanged = dtNow; } } else if ((pgParentA == null) && (pgParentB == null)) { } else { peNew.LocationChanged = dtNow; } return(true); }; pgRootOrg.TraverseTree(TraversalMethod.PreOrder, ghOrg, ehOrg); }
private static void CopyEntriesAndGroups(PwDatabase sourceDb, Settings settings, PwObjectList <PwEntry> entries, PwDatabase targetDatabase) { //If OverrideEntireGroup is set to true if (!settings.OverrideTargetDatabase && !settings.FlatExport && settings.OverrideEntireGroup && !string.IsNullOrEmpty(settings.Group)) { //Delete every entry in target database' groups to override them IEnumerable <PwGroup> groupsToDelete = entries.Select(x => x.ParentGroup).Distinct(); DeleteTargetGroupsInDatabase(groupsToDelete, targetDatabase); } foreach (PwEntry entry in entries) { // Get or create the target group in the target database (including hierarchy) PwGroup targetGroup = settings.FlatExport ? targetDatabase.RootGroup : CreateTargetGroupInDatebase(entry, targetDatabase, sourceDb); PwEntry peNew = null; if (!settings.OverrideTargetDatabase) { peNew = targetGroup.FindEntry(entry.Uuid, bSearchRecursive: false); // Check if the target entry is newer than the source entry if (settings.OverrideEntryOnlyNewer && peNew != null && peNew.LastModificationTime > entry.LastModificationTime) { // Yes -> skip this entry continue; } } // Was no existing entry in the target database found? if (peNew == null) { // Create a new entry peNew = new PwEntry(false, false); peNew.Uuid = entry.Uuid; // Add entry to the target group in the new database targetGroup.AddEntry(peNew, true); } // Clone entry properties if ExportUserAndPassOnly is false if (!settings.ExportUserAndPassOnly) { peNew.AssignProperties(entry, false, true, true); peNew.Strings.Set(PwDefs.UrlField, FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.UrlField)); peNew.Strings.Set(PwDefs.NotesField, FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.NotesField)); } // Copy/override some supported fields with ref resolving values peNew.Strings.Set(PwDefs.TitleField, FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.TitleField)); peNew.Strings.Set(PwDefs.UserNameField, FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.UserNameField)); peNew.Strings.Set(PwDefs.PasswordField, FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.PasswordField)); // Handle custom icon HandleCustomIcon(targetDatabase, sourceDb, entry); } }