public static void CloseKeePassDatabase(PwDatabase database) { database.Save(null); database.Close(); }
private void SyncDatabase(string databaseFilename) { var db = new PwDatabase(); db.Open(IOConnectionInfo.FromPath(databaseFilename), this.host.Database.MasterKey, new NullStatusLogger()); this.host.Database.MergeIn(db, PwMergeMethod.Synchronize); this.host.MainWindow.RefreshEntriesList(); db.Close(); }
/// <summary> /// The function tries to merge possible updates from the deltaDB into /// the actual database. If there was made changes to the actualDB, then /// the function fires a event, which cause the KeePass UI to update and /// show the "save"-button. /// </summary> public void Import(object sender, SyncSource source) { Debug.Assert(source.DestinationDB != null && source.DestinationDB.RootGroup != null); //merge all updates in PwDatabase deltaDB = new PwDatabase(); try { deltaDB.Open(IOConnectionInfo.FromPath(source.Location), source.Key, null); } catch (InvalidCompositeKeyException e) { Debug.WriteLine("Wrong key! exception was: " + e.Message); //brand this entry as a false one => red bg-color and "X" as group icon ShowErrorHighlight(source.DestinationDB, source.Uuid); if (Imported != null) Imported.Invoke(this, source.DestinationDB.RootGroup); return; } catch (Exception e) { Debug.WriteLine("Standard exception was thrown during deltaDB.Open(): " + e.Message); //maybe the process has not finished writing to our file, but the filewtcher fires our event //sourceEntryUuid we have to ignore it and wait for the next one. return; } HideErrorHighlight(source.DestinationDB, source.Uuid); MergeIn(source.DestinationDB, deltaDB); deltaDB.Close(); }
private async void AddKeeButton_Click(object sender, RoutedEventArgs e) { List <KeepassClass> PassList = new List <KeepassClass>(); var dbpath = ""; StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile file = await folder.GetFileAsync((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["KeepassLocalFilePath"]); if (file != null) { dbpath = file.Path; } var masterpw = MasterPassword.Password; var ioConnInfo = new IOConnectionInfo { Path = file.Path }; var compKey = new CompositeKey(); compKey.AddUserKey(new KcpPassword(MasterPassword.Password)); var db = new KeePassLib.PwDatabase(); /* var kdbx = new KdbxFile(db); * using (var fs = await file.OpenReadAsync()) * { * await Task.Run(() => * { * kdbx.Load(file.Path, KdbxFormat.Default, null); * }); * * // return new KdbxDatabase(dbFile, db, dbFile.IdFromPath()); * }*/ db.Open(ioConnInfo, compKey, null); var pwEntry = new PwEntry(true, true); if (!string.IsNullOrEmpty(Title.Text)) { pwEntry.Strings.Set(PwDefs.TitleField, new ProtectedString(true, Title.Text)); } if (!string.IsNullOrEmpty(User.Text)) { pwEntry.Strings.Set(PwDefs.UserNameField, new ProtectedString(true, User.Text)); } if (!string.IsNullOrEmpty(Password.Text)) { pwEntry.Strings.Set(PwDefs.PasswordField, new ProtectedString(true, Password.Text)); } if (!string.IsNullOrEmpty(Notes.Text)) { pwEntry.Strings.Set(PwDefs.NotesField, new ProtectedString(true, Notes.Text)); } if (!string.IsNullOrEmpty(Url.Text)) { pwEntry.Strings.Set(PwDefs.UrlField, new ProtectedString(true, Url.Text)); } // db.RootGroup.AddEntry(pwEntry, true); db.RootGroup.Entries.Add(pwEntry); db.Modified = true; // db.Save(); var stream = await file.OpenStreamForWriteAsync(); var kdbx = new KdbxFile(db); kdbx.Save(stream, null, KdbxFormat.Default, null); db.Close(); int duration = 3000; try { TabViewPage.InAppNotificationMain.Show("Saved, please refresh list", duration); } catch { IncognitoTabView.InAppNotificationMain.Show("Saved, please refresh list", duration); } }
private async void LoadKeeButton_Click(object sender, RoutedEventArgs e) { try { List <KeepassClass> PassList = new List <KeepassClass>(); var dbpath = ""; var picker = new Windows.Storage.Pickers.FileOpenPicker(); picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail; picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".kdb"); picker.FileTypeFilter.Add(".kdbx"); Windows.Storage.StorageFile File = await picker.PickSingleFileAsync(); StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile file = await File.CopyAsync(folder); if (file != null) { dbpath = file.Path; } var masterpw = MasterPassword.Password; var ioConnInfo = new IOConnectionInfo { Path = file.Path }; var compKey = new CompositeKey(); compKey.AddUserKey(new KcpPassword(MasterPassword.Password)); var db = new KeePassLib.PwDatabase(); Windows.Storage.ApplicationData.Current.LocalSettings.Values["KeepassLocalFilePathBool"] = true; Windows.Storage.ApplicationData.Current.LocalSettings.Values["KeepassLocalFilePath"] = file.Name; /* var kdbx = new KdbxFile(db); * using (var fs = await file.OpenReadAsync()) * { * await Task.Run(() => * { * kdbx.Load(file.Path, KdbxFormat.Default, null); * }); * * // return new KdbxDatabase(dbFile, db, dbFile.IdFromPath()); * }*/ db.Open(ioConnInfo, compKey, null); var kpdata = from entry in db.RootGroup.GetEntries(true) select new { Group = entry.ParentGroup.Name, Title = entry.Strings.ReadSafe("Title"), Username = entry.Strings.ReadSafe("UserName"), Password = entry.Strings.ReadSafe("Password"), URL = entry.Strings.ReadSafe("URL"), Notes = entry.Strings.ReadSafe("Notes") }; foreach (var Item in kpdata) { PassList.Add(new KeepassClass() { Title = Item.Title, Website = Item.URL, Password = Item.Password, User = Item.Username }); } PassListView.ItemsSource = PassList; LoadKeeButton.Visibility = Visibility.Collapsed; AddKeeButton.Visibility = Visibility.Visible; EnterKeeButton.Visibility = Visibility.Visible; db.Close(); } catch { int duration = 3000; try { TabViewPage.InAppNotificationMain.Show("Wrong password!", duration); } catch { IncognitoTabView.InAppNotificationMain.Show("Wrong password!", duration); } Windows.Storage.ApplicationData.Current.LocalSettings.Values["KeepassLocalFilePathBool"] = false; Windows.Storage.ApplicationData.Current.LocalSettings.Values["KeepassLocalFilePath"] = null; } }
private static void CloseDatabase() { db.Close(); IsDatabaseOpen = db.IsOpen; }
public async void LoadKeePass(object sender, RoutedEventArgs e) { try { List <KeepassClass> PassList = new List <KeepassClass>(); var dbpath = ""; StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file = await folder.GetFileAsync((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["KeepassLocalFilePath"]); if (file != null) { dbpath = file.Path; } var masterpw = MasterPassword.Password; var ioConnInfo = new IOConnectionInfo { Path = file.Path }; var compKey = new CompositeKey(); compKey.AddUserKey(new KcpPassword(MasterPassword.Password)); var db = new KeePassLib.PwDatabase(); /* var kdbx = new KdbxFile(db); * using (var fs = await file.OpenReadAsync()) * { * await Task.Run(() => * { * kdbx.Load(file.Path, KdbxFormat.Default, null); * }); * * // return new KdbxDatabase(dbFile, db, dbFile.IdFromPath()); * }*/ db.Open(ioConnInfo, compKey, null); var kpdata = from entry in db.RootGroup.GetEntries(true) select new { Group = entry.ParentGroup.Name, Title = entry.Strings.ReadSafe("Title"), Username = entry.Strings.ReadSafe("UserName"), Password = entry.Strings.ReadSafe("Password"), URL = entry.Strings.ReadSafe("URL"), Notes = entry.Strings.ReadSafe("Notes") }; foreach (var Item in kpdata) { PassList.Add(new KeepassClass() { Title = Item.Title, Website = Item.URL, Password = Item.Password, User = Item.Username }); } PassListView.ItemsSource = PassList; db.Close(); } catch { int duration = 3000; try { TabViewPage.InAppNotificationMain.Show("An error occured", duration); } catch { IncognitoTabView.InAppNotificationMain.Show("An error occured", duration); } } }
public void ShouldNotImportDatabasesWithDifferentUsers() { var exportPath = GetTestPath(); m_syncManager.AddExportPath(exportPath); var userMrX = TestHelper.GetUserRootNodeByNameFor(m_database, "mrX"); var userMrY = TestHelper.GetUserRootNodeByNameFor(m_database, "mrY"); m_database.GetExportGroup().Groups.GetAt(0).AddEntry(PwNode.CreateProxyNode(userMrY), true); var existingEntry = new PwEntry(true, true); existingEntry.SetTitle("Entry Version 1"); m_database.RootGroup.AddEntry(existingEntry, true); m_database.RootGroup.AddEntry(PwNode.CreateProxyNode(userMrY), true); m_treeManager.CorrectStructure(); string exportFile = exportPath + SyncSource.FileNameFor(userMrY) + SyncExporter.FileExtension; Assert.IsFalse(File.Exists(exportFile)); m_syncManager.Export(); existingEntry.SetTitle("Entry Version 2"); var deltaDBInitial = new PwDatabase(); deltaDBInitial.Open(IOConnectionInfo.FromPath(exportFile), m_standardKey, null); Assert.AreEqual(0, deltaDBInitial.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "Entry Version 2")); Assert.AreEqual(1, deltaDBInitial.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "Entry Version 1")); deltaDBInitial.Close(); m_syncManager.AddImportPath(exportFile); m_database.GetImportGroup().Groups.GetAt(0).Entries.GetAt(0).SetPassword("InvalidPassword"); m_syncManager.RefeshSourcesList(); Assert.AreEqual(1, m_database.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "Entry Version 2")); Assert.AreEqual(0, m_database.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "Entry Version 1")); }
public void ShouldOnlyExportToCurrentSharedUsers() { m_treeManager.Initialize(m_database); var exportPath = GetTestPath(); m_syncManager.AddExportPath(exportPath); var exportGroup = m_database.GetExportGroup().Groups.GetAt( 0 ) ; m_treeManager.CreateNewUser("Adam"); m_treeManager.CreateNewUser("Eva"); var userAdam = TestHelper.GetUserRootNodeByNameFor(m_database, "Adam"); var userEva = TestHelper.GetUserRootNodeByNameFor(m_database, "Eva"); userAdam.SetPassword(STANDARD_PASSWORD); userEva.SetPassword(STANDARD_PASSWORD); m_database.RootGroup.AddEntry(PwNode.CreateProxyNode(userAdam), true); m_database.RootGroup.AddEntry(PwNode.CreateProxyNode(userEva), true); exportGroup.AddEntry(PwNode.CreateProxyNode(userAdam), true); exportGroup.AddEntry(PwNode.CreateProxyNode(userEva), true); m_treeManager.CorrectStructure(); string exportFileAdam = exportPath + SyncSource.FileNameFor(userAdam) + SyncExporter.FileExtension; string exportFileEva = exportPath + SyncSource.FileNameFor(userEva) + SyncExporter.FileExtension; Assert.IsFalse(File.Exists(exportFileAdam)); Assert.IsFalse(File.Exists(exportFileEva)); m_syncManager.Export(); // TODO CK: At this point it may be possible that the files are not created - we need to wait for the filesystem to respond - maybe using a delay? Assert.IsTrue(File.Exists(exportFileAdam)); Assert.IsTrue(File.Exists(exportFileEva)); var homeAdam = TestHelper.GetUserHomeNodeByNameFor(m_database, "Adam"); homeAdam.ParentGroup.Groups.Remove(homeAdam); var trash = m_database.RootGroup.FindGroup(m_database.RecycleBinUuid, true); trash.AddGroup(homeAdam, true); trash.DeleteAllObjects(m_database); //update should delete all references to the non existing user m_treeManager.CorrectStructure(); var changedEntry = m_database.RootGroup.Entries.GetAt(0); changedEntry.SetTitle("Changed"); TestHelper.SimulateTouch(changedEntry); m_syncManager.Export(); TestHelper.DelayAction(); var deltaDBAdamReexport = new PwDatabase(); deltaDBAdamReexport.Open(IOConnectionInfo.FromPath(exportFileAdam), m_standardKey, null); Assert.AreEqual(Uuid1, deltaDBAdamReexport.RootGroup.Entries.GetAt(0).Uuid); Assert.AreNotEqual("Changed", deltaDBAdamReexport.RootGroup.Entries.GetAt(0).GetTitle()); deltaDBAdamReexport.Close(); var deltaDBEvaReexport = new PwDatabase(); deltaDBEvaReexport.Open(IOConnectionInfo.FromPath(exportFileEva), m_standardKey, null); Assert.AreEqual(Uuid1, deltaDBEvaReexport.RootGroup.Entries.GetAt(0).Uuid); Assert.AreEqual("Changed", deltaDBEvaReexport.RootGroup.Entries.GetAt(0).GetTitle()); deltaDBEvaReexport.Close(); }
public void ShouldHandleCyclesOfNodesInImportAndExport() { var exportPath = GetTestPath(); m_syncManager.AddExportPath(exportPath); var userMrX = TestHelper.GetUserRootNodeByNameFor(m_database, "mrX"); m_database.GetExportGroup().Groups.GetAt(0).AddEntry(PwNode.CreateProxyNode(userMrX), true); var existingEntry = new PwEntry(true, true); existingEntry.SetTitle("Entry Version 1"); m_database.RootGroup.AddEntry(existingEntry, true); m_database.RootGroup.AddEntry(PwNode.CreateProxyNode(userMrX), true); m_treeManager.CorrectStructure(); string exportFile = exportPath + SyncSource.FileNameFor(userMrX) + SyncExporter.FileExtension; Assert.IsFalse(File.Exists(exportFile)); m_syncManager.Export(); var deltaDBInitial = new PwDatabase(); deltaDBInitial.Open(IOConnectionInfo.FromPath(exportFile), m_standardKey, null); Assert.AreEqual(1, deltaDBInitial.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "Entry Version 1")); foreach(var entry in deltaDBInitial.RootGroup.GetEntries(true)) { entry.SetTitle("Changed"); } deltaDBInitial.Save(null); deltaDBInitial.Close(); m_syncManager.AddImportPath(exportFile); m_database.GetImportGroup().Groups.GetAt(0).Entries.GetAt(0).SetPassword(userMrX.Strings.ReadSafe(KeeShare.KeeShare.PasswordField)); m_syncManager.RefeshSourcesList(); // Node normalEntry6 is within the user home and is relocated on export which changes the parent node - during import, the parents of // not "officially" relocated nodes is checked and an assertion is thrown Assert.AreEqual(0, m_database.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "Changed")); }
public void ShouldExportToTargets() { //we change the password which is used to encrypt the delta container so we can later access the delta container //more easily. PwEntry mrX = TestHelper.GetUserRootNodeFor(m_database, 0); //the first autoExport only checks if there is a delta container allready and if not it will export one //in our case there should be no existing container so a new one will be created. var exportFolder = m_database.GetExportGroup(); Assert.IsTrue(0 == exportFolder.Groups.UCount); string exportPath = GetTestPath(); m_syncManager.AddExportPath(exportPath); var exportGroup = exportFolder.Groups.GetAt(0); exportGroup.AddEntry(PwNode.CreateProxyNode(mrX), true); string exportFile = exportPath + SyncSource.FileNameFor(mrX) + SyncExporter.FileExtension; Assert.IsFalse(File.Exists(exportFile)); m_syncManager.RefeshSourcesList(); m_syncManager.Export(); mrX = TestHelper.GetUserRootNodeFor(m_database, 0); Assert.AreEqual("mrX", mrX.Strings.ReadSafe(KeeShare.KeeShare.TitleField)); Assert.IsTrue(File.Exists(exportFile)); //now we open the creted delta container and verify the contend PwDatabase deltaDB = new PwDatabase(); Assert.DoesNotThrow(delegate { deltaDB.Open(IOConnectionInfo.FromPath(exportFile), m_standardKey, null); }); Assert.AreEqual(5, deltaDB.RootGroup.GetEntries(true).UCount); Assert.AreEqual(3, deltaDB.RootGroup.Entries.UCount); Assert.AreEqual("grp1", deltaDB.RootGroup.Groups.GetAt(0).Name); Assert.AreEqual(2, deltaDB.RootGroup.Groups.GetAt(0).Entries.UCount); //now we will test in detail if there are only the expected entries in the created delta container Assert.AreEqual(Uuid1, deltaDB.RootGroup.Entries.GetAt(0).Uuid); Assert.AreEqual(Uuid3, deltaDB.RootGroup.Entries.GetAt(2).Uuid); Assert.AreEqual(Uuid6, deltaDB.RootGroup.Entries.GetAt(1).Uuid); Assert.AreEqual(Uuid4, deltaDB.RootGroup.Groups.GetAt(0).Entries.GetAt(0).Uuid); Assert.AreEqual(Uuid5, deltaDB.RootGroup.Groups.GetAt(0).Entries.GetAt(1).Uuid); Assert.AreEqual("normalEntry1", deltaDB.RootGroup.Entries.GetAt(0).GetTitle()); deltaDB.Close(); }
public void ShouldExportImportedNodes() { var importDatabase = TestHelper.CreateDatabase(); var importedEntry = new PwEntry(true, true); importedEntry.SetTitle("ImportedEntry"); var importedGroup = new PwGroup(true, true); importedGroup.Name = "ImportedGroup"; importedGroup.AddEntry(importedEntry, true); importDatabase.RootGroup.AddGroup(importedGroup, true); var exportPath = GetTestPath(); m_syncManager.AddExportPath(exportPath); var userMrX = TestHelper.GetUserRootNodeByNameFor(m_database, "mrX"); m_database.GetExportGroup().Groups.GetAt(0).AddEntry(PwNode.CreateProxyNode(userMrX), true); m_database.RootGroup.AddEntry(PwNode.CreateProxyNode(userMrX), true); var existingEntry = new PwEntry(true, true); existingEntry.SetTitle("ExistingEntry"); m_database.RootGroup.AddEntry(existingEntry, true); m_treeManager.CorrectStructure(); string exportFile = exportPath + SyncSource.FileNameFor(userMrX) + SyncExporter.FileExtension; Assert.IsFalse(File.Exists(exportFile)); Assert.AreEqual(0, m_database.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "ImportedEntry")); Assert.AreEqual(0, m_database.RootGroup.GetGroups(true).Count(g => g.Name == "ImportedGroup")); m_syncManager.Export(); Assert.IsTrue(File.Exists(exportFile)); var importer = new SyncImporterAccessor(); importer.MergeInAccessor(m_database, importDatabase); Assert.AreEqual(1, m_database.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "ImportedEntry")); Assert.AreEqual(1, m_database.RootGroup.GetGroups(true).Count(g => g.Name == "ImportedGroup")); m_syncManager.Export(); Assert.IsTrue(File.Exists(exportFile)); var deltaDBUpdated = new PwDatabase(); deltaDBUpdated.Open(IOConnectionInfo.FromPath(exportFile), m_standardKey, null); Assert.AreEqual(1, deltaDBUpdated.RootGroup.GetEntries(true).Count(e => e.GetTitle() == "ImportedEntry")); Assert.AreEqual(1, deltaDBUpdated.RootGroup.GetGroups(true).Count(g => g.Name == "ImportedGroup")); deltaDBUpdated.Close(); }
public void ShouldExportAfterChangedContent() { PwEntry mrX = TestHelper.GetUserRootNodeFor(m_database, 0); //the first autoExport only checks if there is a delta container allready and if not it will export one //in our case there should be no existing container so a new one will be created. string exportPath = GetTestPath(); PwGroup exportGroup = new PwGroup(true, true, exportPath, PwIcon.Apple); m_database.GetExportGroup().AddGroup(exportGroup, true); exportGroup.AddEntry(PwNode.CreateProxyNode(mrX), true); string exportFile = exportPath + SyncSource.FileNameFor(mrX) + SyncExporter.FileExtension; Assert.IsFalse(File.Exists(exportFile)); m_syncManager.RefeshSourcesList(); m_syncManager.Export(); mrX = TestHelper.GetUserRootNodeFor(m_database, 0); Assert.AreEqual("mrX", mrX.GetTitle()); Assert.IsTrue(File.Exists(exportFile)); //now we will change a password that is shared to mrX and then trigger the AutoExport method //like it will happen on any OnChangeEvent. After that again we validate the data in the export container. PwEntry entry1 = m_database.RootGroup.FindEntry(Uuid1, true); entry1.SetTitle("new title"); //due to the fact that the UnitTest is way faster than userIneraction we have to manipulate the lastModTimestamp //because if we don't do that the um.Update() method will maybe use another value to update all references and //then we will have the old title in the stringField TestHelper.SimulateTouch(entry1); //now we run the update methods that will be triggered on every UiChangeEvent m_treeManager.CorrectStructure(); m_syncManager.RefeshSourcesList(); //the autoexport method was triggered by in import or OnSaveEvent only so we have to trigger it manually here m_syncManager.Export(); PwDatabase deltaDB = new PwDatabase(); Assert.DoesNotThrow(delegate { deltaDB.Open(IOConnectionInfo.FromPath(exportFile), m_standardKey, null); }); //as before we want to have the same content except that entry1 should now have a new title! Assert.AreEqual(5, deltaDB.RootGroup.GetEntries(true).UCount); Assert.AreEqual(3, deltaDB.RootGroup.Entries.UCount); Assert.AreEqual("grp1", deltaDB.RootGroup.Groups.GetAt(0).Name); Assert.AreEqual(2, deltaDB.RootGroup.Groups.GetAt(0).Entries.UCount); //now we will test in detail if there are only the expected entries in the created delta container Assert.AreEqual(Uuid1, deltaDB.RootGroup.Entries.GetAt(0).Uuid); Assert.AreEqual(Uuid3, deltaDB.RootGroup.Entries.GetAt(2).Uuid); Assert.AreEqual(Uuid6, deltaDB.RootGroup.Entries.GetAt(1).Uuid); Assert.AreEqual(Uuid4, deltaDB.RootGroup.Groups.GetAt(0).Entries.GetAt(0).Uuid); Assert.AreEqual(Uuid5, deltaDB.RootGroup.Groups.GetAt(0).Entries.GetAt(1).Uuid); Assert.AreEqual("new title", deltaDB.RootGroup.Entries.GetAt(0).GetTitle()); deltaDB.Close(); }
private void loadMenu() { if (setDriveLetter()) { dbPath = path + "passwords.kdbx"; // drive letter calculated masterPath = path + "master"; // init local hashes localPwdHash = new Dictionary<string, string>(); localURLHash = new Dictionary<string, string>(); string masterpw = File.ReadAllText(masterPath); var ioConnInfo = new IOConnectionInfo { Path = dbPath }; var compKey = new CompositeKey(); compKey.AddUserKey(new KcpPassword(masterpw)); var db = new KeePassLib.PwDatabase(); db.Open(ioConnInfo, compKey, null); foreach (var i in db.RootGroup.GetGroups(false)) { var currMenu = new MenuItem(i.Name); trayMenu.MenuItems.Add(currMenu); loadSubMenuItems(i, currMenu); } db.Close(); if (watcher == null) { watchForDbChanges(); } } else { const string DbFileNotFound = "Database file not found!"; trayMenu.MenuItems.Add(DbFileNotFound); } //trayMenu.MenuItems.Add(MenuSeparator); //const string AddItem = "Add New Entry"; //trayMenu.MenuItems.Add(AddItem, onAddNewItem); trayMenu.MenuItems.Add(MenuSeparator); const string Exit = "Exit"; trayMenu.MenuItems.Add(Exit, OnExit); }