/// <summary> /// creates a new ProxyNode for the specified user. This Proxy can be used to mark a /// PwGroup as "shared" to this user. /// </summary> /// <param name="name">oldUserName you want to have a proxyNode of</param> /// <returns>PwEntry ProxyNode of the specified user</returns> public static PwEntry CreateProxyNode(this PwEntry entry) { Debug.Assert(entry != null, "CreateProxy got rootNode==null!"); string uuid = entry.Strings.Exists(KeeShare.UuidLinkField) ? entry.Strings.ReadSafe(KeeShare.UuidLinkField) : entry.Uuid.ToHexString(); PwEntry proxy = entry.CloneDeep(); proxy.SetUuid(new PwUuid(true), false); Debug.Assert(KeeShare.UuidLinkField != "", "CreateProxy has an empty linkIdentifier!"); proxy.Strings.Set(KeeShare.UuidLinkField, new ProtectedString(true, uuid)); Debug.Assert(proxy != null, "CreateProxy would return null!"); return(proxy); }
/// <summary> /// The function compares two PwEntries in every scope except the Uuid /// </summary> /// <param name="entry">the first entry</param> /// <param name="otherEntry">the second entry</param> /// <param name="bIgnoreKeeShareFields">Should the KeeShare-specific fields be ignored?</param> /// <returns>True if both entries are equal in all field, accordingly to the parametersettings</returns> public static bool IsSimilarTo(this PwEntry entry, PwEntry otherEntry, bool bIgnoreKeeShareFields) { //if both are null they are equal if (entry == null && otherEntry == null) { return(true); } //if only one of them is null we could not clone it => they are not equal if (entry == null || otherEntry == null) { return(false); } // only clone both entries if we need to PwEntry myCopy = entry.CloneDeep(); PwEntry otherCopy = otherEntry; if (bIgnoreKeeShareFields) { myCopy.Strings.Remove(KeeShare.UuidLinkField); otherCopy = otherEntry.CloneDeep(); otherCopy.Strings.Remove(KeeShare.UuidLinkField); } //we have to make the Uuids and creation times equal, because PwEntry.EqualsEntry compares these too //and returns false if they are not equal!! myCopy.SetUuid(otherCopy.Uuid, false); myCopy.CreationTime = otherCopy.CreationTime; PwCompareOptions opts = PwCompareOptions.IgnoreHistory | PwCompareOptions.IgnoreLastAccess | PwCompareOptions.IgnoreLastBackup | PwCompareOptions.IgnoreLastMod | PwCompareOptions.IgnoreParentGroup | PwCompareOptions.IgnoreTimes; return(myCopy.EqualsEntry(otherCopy, opts, MemProtCmpMode.Full)); }
private static void ReadEntry(XmlNode xmlNode, PwDatabase pwStorage) { PwEntry pe = new PwEntry(true, true); PwGroup pg = pwStorage.RootGroup; string strAttachDesc = null, strAttachment = null; foreach(XmlNode xmlChild in xmlNode) { if(xmlChild.Name == ElemGroup) { string strPreTree = null; try { XmlNode xmlTree = xmlChild.Attributes.GetNamedItem(AttribGroupTree); strPreTree = xmlTree.Value; } catch(Exception) { } string strLast = XmlUtil.SafeInnerText(xmlChild); string strGroup = ((!string.IsNullOrEmpty(strPreTree)) ? strPreTree + "\\" + strLast : strLast); pg = pwStorage.RootGroup.FindCreateSubTree(strGroup, new string[1]{ "\\" }, true); } else if(xmlChild.Name == ElemTitle) pe.Strings.Set(PwDefs.TitleField, new ProtectedString( pwStorage.MemoryProtection.ProtectTitle, XmlUtil.SafeInnerText(xmlChild))); else if(xmlChild.Name == ElemUserName) pe.Strings.Set(PwDefs.UserNameField, new ProtectedString( pwStorage.MemoryProtection.ProtectUserName, XmlUtil.SafeInnerText(xmlChild))); else if(xmlChild.Name == ElemUrl) pe.Strings.Set(PwDefs.UrlField, new ProtectedString( pwStorage.MemoryProtection.ProtectUrl, XmlUtil.SafeInnerText(xmlChild))); else if(xmlChild.Name == ElemPassword) pe.Strings.Set(PwDefs.PasswordField, new ProtectedString( pwStorage.MemoryProtection.ProtectPassword, XmlUtil.SafeInnerText(xmlChild))); else if(xmlChild.Name == ElemNotes) pe.Strings.Set(PwDefs.NotesField, new ProtectedString( pwStorage.MemoryProtection.ProtectNotes, XmlUtil.SafeInnerText(xmlChild))); else if(xmlChild.Name == ElemUuid) pe.SetUuid(new PwUuid(MemUtil.HexStringToByteArray( XmlUtil.SafeInnerText(xmlChild))), false); else if(xmlChild.Name == ElemImage) { int nImage; if(int.TryParse(XmlUtil.SafeInnerText(xmlChild), out nImage)) { if((nImage >= 0) && (nImage < (int)PwIcon.Count)) pe.IconId = (PwIcon)nImage; else { Debug.Assert(false); } } else { Debug.Assert(false); } } else if(xmlChild.Name == ElemCreationTime) pe.CreationTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); else if(xmlChild.Name == ElemLastModTime) pe.LastModificationTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); else if(xmlChild.Name == ElemLastAccessTime) pe.LastAccessTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); else if(xmlChild.Name == ElemExpiryTime) { try { XmlNode xmlExpires = xmlChild.Attributes.GetNamedItem(AttribExpires); if(StrUtil.StringToBool(xmlExpires.Value)) { pe.Expires = true; pe.ExpiryTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); } else { Debug.Assert(ParseTime(XmlUtil.SafeInnerText(xmlChild)).Year == 2999); } } catch(Exception) { Debug.Assert(false); } } else if(xmlChild.Name == ElemAttachDesc) strAttachDesc = XmlUtil.SafeInnerText(xmlChild); else if(xmlChild.Name == ElemAttachment) strAttachment = XmlUtil.SafeInnerText(xmlChild); else { Debug.Assert(false); } } if(!string.IsNullOrEmpty(strAttachDesc) && (strAttachment != null)) { byte[] pbData = Convert.FromBase64String(strAttachment); ProtectedBinary pb = new ProtectedBinary(false, pbData); pe.Binaries.Set(strAttachDesc, pb); } pg.AddEntry(pe, true); }
private void ReadEntries(KdbManager mgr, Dictionary<UInt32, PwGroup> dictGroups) { DateTime dtNeverExpire = KdbManager.GetNeverExpireTime(); uint uEntryCount = mgr.EntryCount; for(uint uEntry = 0; uEntry < uEntryCount; ++uEntry) { KdbEntry e = mgr.GetEntry(uEntry); PwGroup pgContainer; if(!dictGroups.TryGetValue(e.GroupId, out pgContainer)) { Debug.Assert(false); continue; } PwEntry pe = new PwEntry(false, false); pe.SetUuid(new PwUuid(e.Uuid.ToByteArray()), false); pgContainer.AddEntry(pe, true, true); pe.IconId = (e.ImageId < (uint)PwIcon.Count) ? (PwIcon)e.ImageId : PwIcon.Key; pe.Strings.Set(PwDefs.TitleField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectTitle, e.Title)); pe.Strings.Set(PwDefs.UserNameField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectUserName, e.UserName)); pe.Strings.Set(PwDefs.PasswordField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectPassword, e.Password)); pe.Strings.Set(PwDefs.UrlField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectUrl, e.Url)); string strNotes = e.Additional; ImportAutoType(ref strNotes, pe); ImportUrlOverride(ref strNotes, pe); pe.Strings.Set(PwDefs.NotesField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectNotes, strNotes)); pe.CreationTime = e.CreationTime.ToDateTime(); pe.LastModificationTime = e.LastModificationTime.ToDateTime(); pe.LastAccessTime = e.LastAccessTime.ToDateTime(); pe.ExpiryTime = e.ExpirationTime.ToDateTime(); pe.Expires = (pe.ExpiryTime != dtNeverExpire); if((e.BinaryDataLength > 0) && (e.BinaryData != IntPtr.Zero)) { byte[] pbData = KdbManager.ReadBinary(e.BinaryData, e.BinaryDataLength); Debug.Assert(pbData.Length == e.BinaryDataLength); string strDesc = e.BinaryDescription; if(string.IsNullOrEmpty(strDesc)) strDesc = "Attachment"; pe.Binaries.Set(strDesc, new ProtectedBinary(false, pbData)); } if(m_slLogger != null) { if(!m_slLogger.SetProgress((100 * uEntry) / uEntryCount)) throw new Exception(KPRes.Cancel); } } }
private static void ReadEntry(XmlNode xmlNode, PwDatabase pwStorage) { PwEntry pe = new PwEntry(true, true); PwGroup pg = pwStorage.RootGroup; string strAttachDesc = null, strAttachment = null; foreach (XmlNode xmlChild in xmlNode) { if (xmlChild.Name == ElemGroup) { string strPreTree = null; try { XmlNode xmlTree = xmlChild.Attributes.GetNamedItem(AttribGroupTree); strPreTree = xmlTree.Value; } catch (Exception) { } string strLast = XmlUtil.SafeInnerText(xmlChild); string strGroup = ((!string.IsNullOrEmpty(strPreTree)) ? strPreTree + "\\" + strLast : strLast); pg = pwStorage.RootGroup.FindCreateSubTree(strGroup, new string[1] { "\\" }, true); } else if (xmlChild.Name == ElemTitle) { pe.Strings.Set(PwDefs.TitleField, new ProtectedString( pwStorage.MemoryProtection.ProtectTitle, XmlUtil.SafeInnerText(xmlChild))); } else if (xmlChild.Name == ElemUserName) { pe.Strings.Set(PwDefs.UserNameField, new ProtectedString( pwStorage.MemoryProtection.ProtectUserName, XmlUtil.SafeInnerText(xmlChild))); } else if (xmlChild.Name == ElemUrl) { pe.Strings.Set(PwDefs.UrlField, new ProtectedString( pwStorage.MemoryProtection.ProtectUrl, XmlUtil.SafeInnerText(xmlChild))); } else if (xmlChild.Name == ElemPassword) { pe.Strings.Set(PwDefs.PasswordField, new ProtectedString( pwStorage.MemoryProtection.ProtectPassword, XmlUtil.SafeInnerText(xmlChild))); } else if (xmlChild.Name == ElemNotes) { pe.Strings.Set(PwDefs.NotesField, new ProtectedString( pwStorage.MemoryProtection.ProtectNotes, XmlUtil.SafeInnerText(xmlChild))); } else if (xmlChild.Name == ElemUuid) { pe.SetUuid(new PwUuid(MemUtil.HexStringToByteArray( XmlUtil.SafeInnerText(xmlChild))), false); } else if (xmlChild.Name == ElemImage) { int nImage; if (int.TryParse(XmlUtil.SafeInnerText(xmlChild), out nImage)) { if ((nImage >= 0) && (nImage < (int)PwIcon.Count)) { pe.IconId = (PwIcon)nImage; } else { Debug.Assert(false); } } else { Debug.Assert(false); } } else if (xmlChild.Name == ElemCreationTime) { pe.CreationTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); } else if (xmlChild.Name == ElemLastModTime) { pe.LastModificationTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); } else if (xmlChild.Name == ElemLastAccessTime) { pe.LastAccessTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); } else if (xmlChild.Name == ElemExpiryTime) { try { XmlNode xmlExpires = xmlChild.Attributes.GetNamedItem(AttribExpires); if (StrUtil.StringToBool(xmlExpires.Value)) { pe.Expires = true; pe.ExpiryTime = ParseTime(XmlUtil.SafeInnerText(xmlChild)); } else { Debug.Assert(ParseTime(XmlUtil.SafeInnerText(xmlChild)).Year == 2999); } } catch (Exception) { Debug.Assert(false); } } else if (xmlChild.Name == ElemAttachDesc) { strAttachDesc = XmlUtil.SafeInnerText(xmlChild); } else if (xmlChild.Name == ElemAttachment) { strAttachment = XmlUtil.SafeInnerText(xmlChild); } else { Debug.Assert(false); } } if (!string.IsNullOrEmpty(strAttachDesc) && (strAttachment != null)) { byte[] pbData = Convert.FromBase64String(strAttachment); ProtectedBinary pb = new ProtectedBinary(false, pbData); pe.Binaries.Set(strAttachDesc, pb); } pg.AddEntry(pe, true); }
private void ReadEntries(KdbManager mgr, Dictionary <UInt32, PwGroup> dictGroups) { DateTime dtNeverExpire = KdbManager.GetNeverExpireTime(); uint uEntryCount = mgr.EntryCount; for (uint uEntry = 0; uEntry < uEntryCount; ++uEntry) { KdbEntry e = mgr.GetEntry(uEntry); PwGroup pgContainer; if (!dictGroups.TryGetValue(e.GroupId, out pgContainer)) { Debug.Assert(false); continue; } PwEntry pe = new PwEntry(false, false); pe.SetUuid(new PwUuid(e.Uuid.ToByteArray()), false); pgContainer.AddEntry(pe, true, true); pe.IconId = (e.ImageId < (uint)PwIcon.Count) ? (PwIcon)e.ImageId : PwIcon.Key; pe.Strings.Set(PwDefs.TitleField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectTitle, e.Title)); pe.Strings.Set(PwDefs.UserNameField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectUserName, e.UserName)); pe.Strings.Set(PwDefs.PasswordField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectPassword, e.Password)); pe.Strings.Set(PwDefs.UrlField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectUrl, e.Url)); string strNotes = e.Additional; ImportAutoType(ref strNotes, pe); ImportUrlOverride(ref strNotes, pe); pe.Strings.Set(PwDefs.NotesField, new ProtectedString( m_pwDatabase.MemoryProtection.ProtectNotes, strNotes)); pe.CreationTime = e.CreationTime.ToDateTime(); pe.LastModificationTime = e.LastModificationTime.ToDateTime(); pe.LastAccessTime = e.LastAccessTime.ToDateTime(); pe.ExpiryTime = e.ExpirationTime.ToDateTime(); pe.Expires = (pe.ExpiryTime != dtNeverExpire); if ((e.BinaryDataLength > 0) && (e.BinaryData != IntPtr.Zero)) { byte[] pbData = KdbManager.ReadBinary(e.BinaryData, e.BinaryDataLength); Debug.Assert(pbData.Length == e.BinaryDataLength); string strDesc = e.BinaryDescription; if (string.IsNullOrEmpty(strDesc)) { strDesc = "Attachment"; } pe.Binaries.Set(strDesc, new ProtectedBinary(false, pbData)); } if (m_slLogger != null) { if (!m_slLogger.SetProgress((100 * uEntry) / uEntryCount)) { throw new Exception(KPRes.Cancel); } } } }
private static void CreateEntry(PwEntry peTemplate) { if (peTemplate == null) { Debug.Assert(false); return; } PwDatabase pd = Program.MainForm.ActiveDatabase; if (pd == null) { Debug.Assert(false); return; } if (pd.IsOpen == false) { Debug.Assert(false); return; } PwGroup pgContainer = Program.MainForm.GetSelectedGroup(); if (pgContainer == null) { pgContainer = pd.RootGroup; } PwEntry pe = peTemplate.CloneDeep(); pe.SetUuid(new PwUuid(true), true); pe.CreationTime = pe.LastModificationTime = pe.LastAccessTime = DateTime.Now; if (EntryTemplates.EntryCreating != null) { EntryTemplates.EntryCreating(null, new TemplateEntryEventArgs( peTemplate.CloneDeep(), pe)); } PwEntryForm pef = new PwEntryForm(); pef.InitEx(pe, PwEditMode.AddNewEntry, pd, Program.MainForm.ClientIcons, false, true); if (UIUtil.ShowDialogAndDestroy(pef) == DialogResult.OK) { pgContainer.AddEntry(pe, true, true); if (EntryTemplates.EntryCreated != null) { EntryTemplates.EntryCreated(null, new TemplateEntryEventArgs( peTemplate.CloneDeep(), pe)); } // Program.MainForm.UpdateEntryList(null, true); // Program.MainForm.UpdateUIState(true); Program.MainForm.UpdateUI(false, null, pd.UINeedsIconUpdate, null, true, null, true); } else { Program.MainForm.UpdateUI(false, null, pd.UINeedsIconUpdate, null, pd.UINeedsIconUpdate, null, false); } }