private static void TouchSaveEntry(PwDatabase m_pwDatabase, PwEntry m_pwEntry, bool is_new, bool update_parents)
        {
            //Save procedure taken from

            PwObjectList <PwEntry> m_vHistory = m_pwEntry.History.CloneDeep();
            PwEntry peTarget = m_pwEntry;

            peTarget.History = m_vHistory;             // Must be called before CreateBackup()
            if (!is_new)
            {
                peTarget.CreateBackup(null);
            }
            peTarget.Touch(true, update_parents);             // Touch *after* backup
            StrUtil.NormalizeNewLines(peTarget.Strings, true);
            peTarget.MaintainBackups(m_pwDatabase);
        }
Ejemplo n.º 2
0
        private static void ImportEntry(XmlNode xmlNode, PwDatabase pwStorage,
                                        string strLineBreak)
        {
            Debug.Assert(xmlNode != null); if (xmlNode == null)
            {
                return;
            }

            PwEntry pe           = new PwEntry(true, true);
            string  strGroupName = string.Empty;

            List <DatePasswordPair> listHistory = null;

            foreach (XmlNode xmlChild in xmlNode.ChildNodes)
            {
                if (xmlChild.Name == ElemGroup)
                {
                    strGroupName = XmlUtil.SafeInnerText(xmlChild);
                }
                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 == ElemPassword)
                {
                    pe.Strings.Set(PwDefs.PasswordField,
                                   new ProtectedString(pwStorage.MemoryProtection.ProtectPassword,
                                                       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 == ElemNotes)
                {
                    pe.Strings.Set(PwDefs.NotesField,
                                   new ProtectedString(pwStorage.MemoryProtection.ProtectNotes,
                                                       XmlUtil.SafeInnerText(xmlChild, strLineBreak)));
                }
                else if (xmlChild.Name == ElemEMail)
                {
                    pe.Strings.Set("E-Mail", new ProtectedString(false,
                                                                 XmlUtil.SafeInnerText(xmlChild)));
                }
                else if (xmlChild.Name == ElemCreationTime)
                {
                    pe.CreationTime = ReadDateTime(xmlChild);
                }
                else if (xmlChild.Name == ElemLastAccessTime)
                {
                    pe.LastAccessTime = ReadDateTime(xmlChild);
                }
                else if (xmlChild.Name == ElemExpireTime)
                {
                    pe.ExpiryTime = ReadDateTime(xmlChild);
                    pe.Expires    = true;
                }
                else if (xmlChild.Name == ElemLastModTime)                // = last mod
                {
                    pe.LastModificationTime = ReadDateTime(xmlChild);
                }
                else if (xmlChild.Name == ElemRecordModTime)                // = last mod
                {
                    pe.LastModificationTime = ReadDateTime(xmlChild);
                }
                else if (xmlChild.Name == ElemCreationTimeX)
                {
                    pe.CreationTime = ReadDateTimeX(xmlChild);
                }
                else if (xmlChild.Name == ElemLastAccessTimeX)
                {
                    pe.LastAccessTime = ReadDateTimeX(xmlChild);
                }
                else if (xmlChild.Name == ElemExpireTimeX)
                {
                    pe.ExpiryTime = ReadDateTimeX(xmlChild);
                    pe.Expires    = true;
                }
                else if (xmlChild.Name == ElemLastModTimeX)                // = last mod
                {
                    pe.LastModificationTime = ReadDateTimeX(xmlChild);
                }
                else if (xmlChild.Name == ElemRecordModTimeX)                // = last mod
                {
                    pe.LastModificationTime = ReadDateTimeX(xmlChild);
                }
                else if (xmlChild.Name == ElemAutoType)
                {
                    pe.AutoType.DefaultSequence = XmlUtil.SafeInnerText(xmlChild);
                }
                else if (xmlChild.Name == ElemRunCommand)
                {
                    pe.OverrideUrl = XmlUtil.SafeInnerText(xmlChild);
                }
                else if (xmlChild.Name == ElemEntryHistory)
                {
                    listHistory = ReadEntryHistory(xmlChild);
                }
            }

            if (listHistory != null)
            {
                string   strPassword = pe.Strings.ReadSafe(PwDefs.PasswordField);
                DateTime dtLastMod   = pe.LastModificationTime;

                foreach (DatePasswordPair dpp in listHistory)
                {
                    pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
                                       pwStorage.MemoryProtection.ProtectPassword,
                                       dpp.Password));
                    pe.LastModificationTime = dpp.Time;

                    pe.CreateBackup(null);
                }
                // Maintain backups manually now (backups from the imported file
                // might have been out of order)
                pe.MaintainBackups(pwStorage);

                pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
                                   pwStorage.MemoryProtection.ProtectPassword,
                                   strPassword));
                pe.LastModificationTime = dtLastMod;
            }

            PwGroup pgContainer = pwStorage.RootGroup;

            if (strGroupName.Length != 0)
            {
                pgContainer = pwStorage.RootGroup.FindCreateSubTree(strGroupName,
                                                                    new string[1] {
                    "."
                }, true);
            }
            pgContainer.AddEntry(pe, true);
            pgContainer.IsExpanded = true;
        }
Ejemplo n.º 3
0
		private static void ImportEntry(XmlNode xmlNode, PwDatabase pwStorage,
			string strLineBreak)
		{
			Debug.Assert(xmlNode != null); if(xmlNode == null) return;

			PwEntry pe = new PwEntry(true, true);
			string strGroupName = string.Empty;

			List<DatePasswordPair> listHistory = null;

			foreach(XmlNode xmlChild in xmlNode.ChildNodes)
			{
				if(xmlChild.Name == ElemGroup)
					strGroupName = XmlUtil.SafeInnerText(xmlChild);
				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 == ElemPassword)
					pe.Strings.Set(PwDefs.PasswordField,
						new ProtectedString(pwStorage.MemoryProtection.ProtectPassword,
						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 == ElemNotes)
					pe.Strings.Set(PwDefs.NotesField,
						new ProtectedString(pwStorage.MemoryProtection.ProtectNotes,
						XmlUtil.SafeInnerText(xmlChild, strLineBreak)));
				else if(xmlChild.Name == ElemEMail)
					pe.Strings.Set("E-Mail", new ProtectedString(false,
						XmlUtil.SafeInnerText(xmlChild)));
				else if(xmlChild.Name == ElemCreationTime)
					pe.CreationTime = ReadDateTime(xmlChild);
				else if(xmlChild.Name == ElemLastAccessTime)
					pe.LastAccessTime = ReadDateTime(xmlChild);
				else if(xmlChild.Name == ElemExpireTime)
				{
					pe.ExpiryTime = ReadDateTime(xmlChild);
					pe.Expires = true;
				}
				else if(xmlChild.Name == ElemLastModTime) // = last mod
					pe.LastModificationTime = ReadDateTime(xmlChild);
				else if(xmlChild.Name == ElemRecordModTime) // = last mod
					pe.LastModificationTime = ReadDateTime(xmlChild);
				else if(xmlChild.Name == ElemCreationTimeX)
					pe.CreationTime = ReadDateTimeX(xmlChild);
				else if(xmlChild.Name == ElemLastAccessTimeX)
					pe.LastAccessTime = ReadDateTimeX(xmlChild);
				else if(xmlChild.Name == ElemExpireTimeX)
				{
					pe.ExpiryTime = ReadDateTimeX(xmlChild);
					pe.Expires = true;
				}
				else if(xmlChild.Name == ElemLastModTimeX) // = last mod
					pe.LastModificationTime = ReadDateTimeX(xmlChild);
				else if(xmlChild.Name == ElemRecordModTimeX) // = last mod
					pe.LastModificationTime = ReadDateTimeX(xmlChild);
				else if(xmlChild.Name == ElemAutoType)
					pe.AutoType.DefaultSequence = XmlUtil.SafeInnerText(xmlChild);
				else if(xmlChild.Name == ElemRunCommand)
					pe.OverrideUrl = XmlUtil.SafeInnerText(xmlChild);
				else if(xmlChild.Name == ElemEntryHistory)
					listHistory = ReadEntryHistory(xmlChild);
			}

			if(listHistory != null)
			{
				string strPassword = pe.Strings.ReadSafe(PwDefs.PasswordField);
				DateTime dtLastMod = pe.LastModificationTime;

				foreach(DatePasswordPair dpp in listHistory)
				{
					pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
						pwStorage.MemoryProtection.ProtectPassword,
						dpp.Password));
					pe.LastModificationTime = dpp.Time;

					pe.CreateBackup(null);
				}
				// Maintain backups manually now (backups from the imported file
				// might have been out of order)
				pe.MaintainBackups(pwStorage);

				pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
					pwStorage.MemoryProtection.ProtectPassword,
					strPassword));
				pe.LastModificationTime = dtLastMod;
			}

			PwGroup pgContainer = pwStorage.RootGroup;
			if(strGroupName.Length != 0)
				pgContainer = pwStorage.RootGroup.FindCreateSubTree(strGroupName,
					new string[1]{ "." }, true);
			pgContainer.AddEntry(pe, true);
			pgContainer.IsExpanded = true;
		}
Ejemplo n.º 4
0
        void SaveEntry()
        {
            Database          db  = App.Kp2a.GetDb();
            EntryEditActivity act = this;

            if (!ValidateBeforeSaving())
            {
                return;
            }

            PwEntry initialEntry = State.EntryInDatabase.CloneDeep();

            PwEntry newEntry = State.EntryInDatabase;

            //Clone history and re-assign:
            newEntry.History = newEntry.History.CloneDeep();

            //Based on KeePass Desktop
            bool bCreateBackup = (!State.IsNew);

            if (bCreateBackup)
            {
                newEntry.CreateBackup(null);
            }

            if (State.SelectedIcon)
            {
                newEntry.IconId         = State.SelectedIconId;
                newEntry.CustomIconUuid = State.SelectedCustomIconId;
            }             //else the State.EntryInDatabase.Icon

            /* KPDesktop
             *      if(m_cbCustomForegroundColor.Checked)
             *              newEntry.ForegroundColor = m_clrForeground;
             *      else newEntry.ForegroundColor = Color.Empty;
             *      if(m_cbCustomBackgroundColor.Checked)
             *              newEntry.BackgroundColor = m_clrBackground;
             *      else newEntry.BackgroundColor = Color.Empty;
             *
             */

            UpdateEntryFromUi(newEntry);
            newEntry.Binaries = State.Entry.Binaries;
            newEntry.Expires  = State.Entry.Expires;
            if (newEntry.Expires)
            {
                newEntry.ExpiryTime = State.Entry.ExpiryTime;
            }


            newEntry.Touch(true, false);             // Touch *after* backup

            StrUtil.NormalizeNewLines(newEntry.Strings, true);

            bool             bUndoBackup = false;
            PwCompareOptions cmpOpt      = (PwCompareOptions.NullEmptyEquivStd |
                                            PwCompareOptions.IgnoreTimes);

            if (bCreateBackup)
            {
                cmpOpt |= PwCompareOptions.IgnoreLastBackup;
            }
            if (newEntry.EqualsEntry(initialEntry, cmpOpt, MemProtCmpMode.CustomOnly))
            {
                // No modifications at all => restore last mod time and undo backup
                newEntry.LastModificationTime = initialEntry.LastModificationTime;
                bUndoBackup = bCreateBackup;
            }
            else if (bCreateBackup)
            {
                // If only history items have been modified (deleted) => undo
                // backup, but without restoring the last mod time
                PwCompareOptions cmpOptNh = (cmpOpt | PwCompareOptions.IgnoreHistory);
                if (newEntry.EqualsEntry(initialEntry, cmpOptNh, MemProtCmpMode.CustomOnly))
                {
                    bUndoBackup = true;
                }
            }
            if (bUndoBackup)
            {
                newEntry.History.RemoveAt(newEntry.History.UCount - 1);
            }

            newEntry.MaintainBackups(db.KpDatabase);

            //if ( newEntry.Strings.ReadSafe (PwDefs.TitleField).Equals(State.Entry.Strings.ReadSafe (PwDefs.TitleField)) ) {
            //	SetResult(KeePass.EXIT_REFRESH);
            //} else {
            //it's safer to always update the title as we might add further information in the title like expiry etc.
            SetResult(KeePass.ExitRefreshTitle);
            //}

            RunnableOnFinish runnable;

            ActionOnFinish closeOrShowError = new ActionOnFinish((success, message) => {
                if (success)
                {
                    Finish();
                }
                else
                {
                    OnFinish.DisplayMessage(this, message);
                }
            });

            ActionOnFinish afterAddEntry = new ActionOnFinish((success, message) =>
            {
                if (success)
                {
                    _appTask.AfterAddNewEntry(this, newEntry);
                }
            }, closeOrShowError);

            if (State.IsNew)
            {
                runnable = AddEntry.GetInstance(this, App.Kp2a, newEntry, State.ParentGroup, afterAddEntry);
            }
            else
            {
                runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError);
            }
            ProgressTask pt = new ProgressTask(App.Kp2a, act, runnable);

            pt.Run();
        }
Ejemplo n.º 5
0
        private bool SaveEntry(PwEntry peTarget, bool bValidate)
        {
            if(m_pwEditMode == PwEditMode.ViewReadOnlyEntry) return true;

            if(bValidate && !m_icgPassword.ValidateData(true)) return false;

            if(this.EntrySaving != null)
            {
                CancellableOperationEventArgs eaCancel = new CancellableOperationEventArgs();
                this.EntrySaving(this, eaCancel);
                if(eaCancel.Cancel) return false;
            }

            peTarget.History = m_vHistory; // Must be called before CreateBackup()
            bool bCreateBackup = (m_pwEditMode != PwEditMode.AddNewEntry);
            if(bCreateBackup) peTarget.CreateBackup(null);

            peTarget.IconId = m_pwEntryIcon;
            peTarget.CustomIconUuid = m_pwCustomIconID;

            if(m_cbCustomForegroundColor.Checked)
                peTarget.ForegroundColor = m_clrForeground;
            else peTarget.ForegroundColor = Color.Empty;
            if(m_cbCustomBackgroundColor.Checked)
                peTarget.BackgroundColor = m_clrBackground;
            else peTarget.BackgroundColor = Color.Empty;

            peTarget.OverrideUrl = m_cmbOverrideUrl.Text;

            List<string> vNewTags = StrUtil.StringToTags(m_tbTags.Text);
            peTarget.Tags.Clear();
            foreach(string strTag in vNewTags) peTarget.AddTag(strTag);

            peTarget.Expires = m_cgExpiry.Checked;
            if(peTarget.Expires) peTarget.ExpiryTime = m_cgExpiry.Value;

            UpdateEntryStrings(true, false, false);

            peTarget.Strings = m_vStrings;
            peTarget.Binaries = m_vBinaries;

            m_atConfig.Enabled = m_cbAutoTypeEnabled.Checked;
            m_atConfig.ObfuscationOptions = (m_cbAutoTypeObfuscation.Checked ?
                AutoTypeObfuscationOptions.UseClipboard :
                AutoTypeObfuscationOptions.None);

            SaveDefaultSeq();

            peTarget.AutoType = m_atConfig;

            peTarget.Touch(true, false); // Touch *after* backup
            if(object.ReferenceEquals(peTarget, m_pwEntry)) m_bTouchedOnce = true;

            StrUtil.NormalizeNewLines(peTarget.Strings, true);

            bool bUndoBackup = false;
            PwCompareOptions cmpOpt = m_cmpOpt;
            if(bCreateBackup) cmpOpt |= PwCompareOptions.IgnoreLastBackup;
            if(peTarget.EqualsEntry(m_pwInitialEntry, cmpOpt, MemProtCmpMode.CustomOnly))
            {
                // No modifications at all => restore last mod time and undo backup
                peTarget.LastModificationTime = m_pwInitialEntry.LastModificationTime;
                bUndoBackup = bCreateBackup;
            }
            else if(bCreateBackup)
            {
                // If only history items have been modified (deleted) => undo
                // backup, but without restoring the last mod time
                PwCompareOptions cmpOptNH = (m_cmpOpt | PwCompareOptions.IgnoreHistory);
                if(peTarget.EqualsEntry(m_pwInitialEntry, cmpOptNH, MemProtCmpMode.CustomOnly))
                    bUndoBackup = true;
            }
            if(bUndoBackup) peTarget.History.RemoveAt(peTarget.History.UCount - 1);

            peTarget.MaintainBackups(m_pwDatabase);

            if(this.EntrySaved != null) this.EntrySaved(this, EventArgs.Empty);

            return true;
        }
Ejemplo n.º 6
0
        public bool SaveEntry(EntryModel edited, PwEntry m_pwInitialEntry /*, bool bValidate*/)
        {
            const PwCompareOptions m_cmpOpt = (PwCompareOptions.NullEmptyEquivStd | PwCompareOptions.IgnoreTimes);

            PwEntry originalEntry = edited.Entry;
            //peTarget.History = m_vHistory; // Must be called before CreateBackup()

            bool bCreateBackup = !edited.IsNew;

            if (bCreateBackup)
            {
                originalEntry.CreateBackup(null);
            }

//         peTarget.IconId = m_pwEntryIcon;
//         peTarget.CustomIconUuid = m_pwCustomIconID;
//
//         if(m_cbCustomForegroundColor.Checked)
//            peTarget.ForegroundColor = m_clrForeground;
//         else peTarget.ForegroundColor = Color.Empty;
//         if(m_cbCustomBackgroundColor.Checked)
//            peTarget.BackgroundColor = m_clrBackground;
//         else peTarget.BackgroundColor = Color.Empty;

            //peTarget.OverrideUrl = m_tbOverrideUrl.Text;

//         List<string> vNewTags = StrUtil.StringToTags(m_tbTags.Text);
//         peTarget.Tags.Clear();
//         foreach(string strTag in vNewTags) peTarget.AddTag(strTag);

            originalEntry.Expires = edited.Expires;

            if (originalEntry.Expires)
            {
                originalEntry.ExpiryTime = edited.ExpireDate;
            }

            UpdateEntryStrings(edited, originalEntry.Strings);

//         peTarget.Strings = m_vStrings;
//         peTarget.Binaries = m_vBinaries;

//         m_atConfig.Enabled = m_cbAutoTypeEnabled.Checked;
//         m_atConfig.ObfuscationOptions = (m_cbAutoTypeObfuscation.Checked ?
//            AutoTypeObfuscationOptions.UseClipboard :
//            AutoTypeObfuscationOptions.None);
//
//         SaveDefaultSeq();

//         peTarget.AutoType = m_atConfig;

            originalEntry.Touch(true, false); // Touch *after* backup

//         if(object.ReferenceEquals(peTarget, m_pwEntry))
//            m_bTouchedOnce = true;

            StrUtil.NormalizeNewLines(originalEntry.Strings, true);

            bool             bUndoBackup = false;
            PwCompareOptions cmpOpt      = m_cmpOpt;

            if (bCreateBackup)
            {
                cmpOpt |= PwCompareOptions.IgnoreLastBackup;
            }

            if (originalEntry.EqualsEntry(m_pwInitialEntry, cmpOpt, MemProtCmpMode.CustomOnly))
            {
                // No modifications at all => restore last mod time and undo backup
                originalEntry.LastModificationTime = m_pwInitialEntry.LastModificationTime;
                bUndoBackup = bCreateBackup;
            }
            else if (bCreateBackup)
            {
                // If only history items have been modified (deleted) => undo
                // backup, but without restoring the last mod time
                PwCompareOptions cmpOptNH = (m_cmpOpt | PwCompareOptions.IgnoreHistory);
                if (originalEntry.EqualsEntry(m_pwInitialEntry, cmpOptNH, MemProtCmpMode.CustomOnly))
                {
                    bUndoBackup = true;
                }
            }

            if (bUndoBackup)
            {
                originalEntry.History.RemoveAt(originalEntry.History.UCount - 1);
            }

            originalEntry.MaintainBackups(db);

            return(true);
        }