Example #1
0
        public static PEDCalcValue GetPEDCValue(PwEntry pe, bool recursion)
        {
            if (!m_dPEDValues.ContainsKey(pe))
            {
                string         days_string = pe.ReadPEDCString();
                PEDCValueEntry pve         = new PEDCValueEntry();
                pve.value = PEDCalcValue.ConvertFromString(days_string);

                if (pve.value.Inherit && (pe.ParentGroup != null))
                {
                    pve.valueinherit = pe.ParentGroup.GetPEDValue(true);
                }
                else
                {
                    pve.valueinherit = new PEDCalcValue(PEDC.Off);
                }
                m_dPEDValues[pe] = pve;
                PluginDebug.AddInfo("Add PEDCValues to buffer", 0,
                                    "Entry: " + pe.Uuid.ToHexString() + " / " + pe.Strings.ReadSafe(PwDefs.TitleField),
                                    "Value: " + pve.value.ToString(),
                                    "Value inherited: " + pve.valueinherit.ToString());
            }
            if (recursion && m_dPEDValues[pe].value.Inherit)
            {
                return(m_dPEDValues[pe].valueinherit);
            }
            return(m_dPEDValues[pe].value);
        }
Example #2
0
        public override bool Initialize(IPluginHost host)
        {
            Terminate();

            if (host == null)
            {
                return(false);
            }
            m_host = host;
            PluginTranslate.Init(this, KeePass.Program.Translation.Properties.Iso6391Code);
            PluginTranslate.TranslationChanged += (o, e) => { PEDCalcValue.SetTranslatedUnits(); };
            Tools.DefaultCaption = PluginTranslate.PluginName;
            Tools.PluginURL      = "https://github.com/rookiestyle/pedcalc/";

            m_iconActive   = GfxUtil.ScaleImage(Resources.pedcalc, DpiUtil.ScaleIntX(16), DpiUtil.ScaleIntY(16));
            m_iconInactive = ToolStripRenderer.CreateDisabledImage(m_iconActive);

            PwEntry.EntryTouched            += OnEntryTouched;
            GlobalWindowManager.WindowAdded += OnWindowAdded;

            Tools.OptionsFormShown += Tools_OptionsFormShown;

            PEDCValueDAO.StartLogging();

            m_host.ColumnProviderPool.Add(m_cp);

            AddMenu();

            return(true);
        }
Example #3
0
        private void OnButtonClick(object sender, EventArgs e)
        {
            if (ItemOrButtonClick != null)
            {
                PEDCalcValue pcv;
                PEDC         unit = PEDC.Days;
                PEDCListItem i    = m_cbUnit.Items[m_cbUnit.SelectedIndex] as PEDCListItem;
                unit = i.Unit;
                int dummy = -1;
                if (!int.TryParse(m_tbValue.Text, out dummy) || (dummy == -1))
                {
                    pcv = new PEDCalcValue(PEDC.Inherit);
                    PluginDebug.AddInfo("Converted expiry value", 0, "Given: " + m_tbValue.Text, "Converted: " + pcv.unit.ToString());
                }
                else if (dummy <= 0)
                {
                    pcv = new PEDCalcValue(PEDC.Off);
                    PluginDebug.AddInfo("Converted expiry value", 0, "Given: " + m_tbValue.Text, "Converted: " + pcv.unit.ToString());
                }
                else
                {
                    pcv = new PEDCalcValue(unit, dummy);
                }

                QuickActionEventArgs qe = new QuickActionEventArgs(pcv);
                ItemOrButtonClick(this, qe);
            }
        }
Example #4
0
        internal static void RecalcExpiry(this PwEntry pe, bool forceRecalculation)
        {
            if (!pe.Expires)
            {
                return;                 //password does not expire: nothing to do
            }
            if (!forceRecalculation && !RecalcRequired(pe))
            {
                return;
            }

            PEDCalcValue days = GetPEDValue(pe, true);

            PluginDebug.AddInfo("Recalc expiry date", pe.Uuid.ToString(), "Force: " + forceRecalculation.ToString(), "Old: " + pe.ExpiryTime.ToString("YYYYMMddTHHmmssZ"),
                                "New: " + days.NewExpiryDateUtc.ToString("yyyyMMddTHHmmssZ"),
                                "Recalc required: " + (days.Specific && (pe.ExpiryTime != days.NewExpiryDateUtc)).ToString());
            if (!days.Specific)
            {
                return;
            }

            if (days.NewExpiryDateUtc == pe.ExpiryTime)
            {
                return;
            }
            pe.ExpiryTime = days.NewExpiryDateUtc;
            pe.Touch(true, false);
            Tools.RefreshEntriesList(true);
        }
Example #5
0
 private void OnItemClick(object sender, EventArgs e)
 {
     if (ItemOrButtonClick != null)
     {
         PEDCalcValue         value = new PEDCalcValue(((sender as ToolStripMenuItem).Tag as PEDCalcValue).unit);
         QuickActionEventArgs qe    = new QuickActionEventArgs(value);
         ItemOrButtonClick(this, qe);
     }
 }
Example #6
0
        private ToolStripMenuItem CreateMenuItem(string sText, PEDCalcValue pcv)
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem(sText);

            tsmi.TextAlign = ContentAlignment.MiddleLeft;
            tsmi.Dock      = DockStyle.Fill;
            tsmi.Click    += OnItemClick;
            tsmi.Tag       = pcv;
            return(tsmi);
        }
Example #7
0
 internal static void SavePEDCString(this PwGroup pg, PEDCalcValue days)
 {
     pg.CustomData.Remove(Configuration.DaysField);
     if (days.Inherit)
     {
         pg.CustomData.Remove(Configuration.Interval);
     }
     else
     {
         pg.CustomData.Set(Configuration.Interval, days.ToString());
     }
     PEDCValueDAO.Invalidate(pg);
 }
Example #8
0
 internal static void SavePEDCString(this PwEntry pe, PEDCalcValue days)
 {
     pe.Strings.Remove(Configuration.DaysField);
     if (days.Inherit)
     {
         pe.Strings.Remove(Configuration.Interval);
     }
     else
     {
         pe.Strings.Set(Configuration.Interval, new ProtectedString(false, days.ToString()));
     }
     PEDCValueDAO.Invalidate(pe);
 }
Example #9
0
        internal static PEDCalcValue ReadPEDCString(this ProtectedStringDictionary psd)
        {
            string sPEDC = psd.ReadSafe(Configuration.DaysField);

            if (string.IsNullOrEmpty(sPEDC))
            {
                sPEDC = psd.ReadSafe(Configuration.Interval);
            }
            if (!string.IsNullOrEmpty(sPEDC))
            {
                return(PEDCalcValue.ConvertFromString(sPEDC));
            }
            return(null);
        }
Example #10
0
        internal void SetValue(PEDCalcValue pcv)
        {
            FontStyle fs      = DropDown.Items[0].Font.Style & ~FontStyle.Bold;
            Font      f       = new Font(DropDown.Items[0].Font, fs);
            Font      fActive = new Font(DropDown.Items[0].Font, fs | FontStyle.Bold);

            DropDown.Items[1].Font = pcv.Off ? fActive : f;
            DropDown.Items[3].Font = pcv.Inherit ? fActive : f;

            if (KeePassLib.Native.NativeLib.IsUnix())             //Mono does not support changing the fontstyle
            {
                DropDown.Items[0].Text = DropDown.Items[1].Text.Replace("-> ", "");
                DropDown.Items[1].Text = DropDown.Items[3].Text.Replace("-> ", "");
                if (pcv.Off)
                {
                    DropDown.Items[1].Text = "-> " + DropDown.Items[1].Text;
                }
                if (pcv.Inherit)
                {
                    DropDown.Items[3].Text = "-> " + DropDown.Items[3].Text;
                }
            }
            DropDown.Items[0].Image = pcv.Off ? PEDCalcExt.m_iconActive : PEDCalcExt.m_iconInactive;
            DropDown.Items[2].Image = pcv.Inherit ? PEDCalcExt.m_iconActive : PEDCalcExt.m_iconInactive;

            if (pcv.Specific)
            {
                m_tbValue.Text = pcv.value.ToString();
                for (int i = 0; i < m_cbUnit.Items.Count; i++)
                {
                    PEDCListItem pli = m_cbUnit.Items[i] as PEDCListItem;
                    if (pli != null && pli.Unit == pcv.unit)
                    {
                        m_cbUnit.SelectedIndex = i;
                        break;
                    }
                }
            }
            else
            {
                if (KeePass.Program.Config.Defaults.NewEntryExpiresInDays > 0)
                {
                    m_tbValue.Text = KeePass.Program.Config.Defaults.NewEntryExpiresInDays.ToString();
                }
                m_cbUnit.SelectedIndex = 0;
            }

            DropDown.Items[DropDown.Items.Count - 4].Image = !pcv.Off && !pcv.Inherit ? PEDCalcExt.m_iconActive : PEDCalcExt.m_iconInactive;
            m_b.CheckState = !pcv.Off && !pcv.Inherit ? CheckState.Checked : CheckState.Unchecked;
        }
Example #11
0
        private void CheckShowNewExpireDate()
        {
            if (m_pweForm == null)
            {
                return;
            }

            Label lNewExpireDate = (Label)Tools.GetControl("PEDCalc_NewExpireDate", m_pweForm);

            if (lNewExpireDate == null)
            {
                return;
            }
            lNewExpireDate.Visible = false;

            CheckBox     cbExpires = (CheckBox)Tools.GetControl("m_cbExpires", m_pweForm);
            PEDCalcValue ped       = m_pweForm.EntryRef.GetPEDValue(true);

            //Automatic recalculation required?
            if (ped.Off && (m_iSelectedEntries == 1))
            {
                return;
            }

            //Entry does not expire => Don't show calculated expiry date
            if ((cbExpires == null) || !cbExpires.Checked)
            {
                return;
            }

            //Check for manual change of expiry date
            DateTimePicker dtExpireDate = (DateTimePicker)Tools.GetControl("m_dtExpireDateTime", m_pweForm);

            if ((dtExpireDate.Value != m_pweForm.EntryRef.ExpiryTime.ToLocalTime()) && m_pweForm.EntryRef.Expires)
            {
                return;
            }

            //Check for change of password
            SecureTextBoxEx password = (SecureTextBoxEx)Tools.GetControl("m_tbPassword", m_pweForm);
            ProtectedString psOldPw  = m_pweForm.EntryRef.Strings.GetSafe(PwDefs.PasswordField);

            if ((password == null) || password.TextEx.Equals(psOldPw, false))
            {
                return;
            }

            lNewExpireDate.Visible = true;
        }
Example #12
0
        internal static PEDCalcValue GetPEDValue(this PwGroup pg, bool recursion)
        {
            string       days_string = pg.ReadPEDCString();
            PEDCalcValue PEDValue    = PEDCalcValue.ConvertFromString(days_string);

            if (PEDValue.Inherit && recursion && pg.ParentGroup != null)
            {
                return(GetPEDValue(pg.ParentGroup, true));
            }
            if (PEDValue.Inherit && pg.ParentGroup == null)
            {
                PEDValue.unit = PEDC.Off;
            }
            return(PEDValue);
        }
Example #13
0
        private void OnPEDMenuOpening(object sender, EventArgs e)
        {
            ToolStripMenuItem tsmi = sender as ToolStripMenuItem;

            if (tsmi == null)
            {
                return;
            }
            if (tsmi.DropDown == null)
            {
                return;
            }
            PEDValue_QuickAction pqaActions = tsmi.DropDown.Tag as PEDValue_QuickAction;

            if (pqaActions == null)
            {
                return;
            }
            if ((tsmi == m_ContextMenuEntry) || (tsmi == m_MainMenuEntry))
            {
                PwEntry[]    pe         = m_host.MainWindow.GetSelectedEntries();
                PEDCalcValue pcvInherit = pe[0].GetPEDValueInherit();
                pqaActions.SetInheritValue(pcvInherit);
                pqaActions.SetValue(pe[0].GetPEDValue(false));
            }
            else if ((tsmi == m_ContextMenuGroup) || (tsmi == m_MainMenuGroup))
            {
                PwGroup      pg         = m_host.MainWindow.GetSelectedGroup();
                PEDCalcValue pcvInherit = pg.GetPEDValueInherit();
                pqaActions.SetInheritValue(pcvInherit);
                pqaActions.SetValue(pg.GetPEDValue(false));
            }
            else if (m_pweForm != null)
            {
                PwEntry pe = m_pweForm.EntryRef;
                m_pweForm.UpdateEntryStrings(true, true);
                PEDCalcValue currentValue = m_pweForm.EntryStrings.ReadPEDCString();
                if (currentValue == null)
                {
                    currentValue = pe.GetPEDValue(false);
                }
                pqaActions.SetInheritValue(pe.GetPEDValueInherit());
                pqaActions.SetValue(currentValue);
            }
        }
Example #14
0
 public static string GetPEDCValueString(PwEntry pe)
 {
     if (!m_dPEDValuesString.ContainsKey(pe.Uuid))
     {
         PEDCalcValue pcv_entry = pe.GetPEDValue(false);
         bool         bInherit  = pcv_entry.Inherit;
         if (bInherit)
         {
             pcv_entry = pe.GetPEDValue(true);
         }
         string sUnit = pcv_entry.ToString(true);
         m_dPEDValuesString[pe.Uuid] = sUnit + (bInherit ? "*" : string.Empty);
         PluginDebug.AddInfo("Add PEDCValue-string to buffer", 0,
                             "Entry: " + pe.Uuid.ToHexString() + " / " + pe.Strings.ReadSafe(PwDefs.TitleField),
                             "Value: " + m_dPEDValuesString[pe.Uuid]);
     }
     return(m_dPEDValuesString[pe.Uuid]);
 }
Example #15
0
        private void OnPEDCalcEntryForm(object sender, QuickActionEventArgs e)
        {
            ToolStripDropDown tsdd = (sender as PEDValue_QuickAction).DropDown;

            if (tsdd == null)
            {
                return;
            }

            m_pweForm.EntryStrings.Remove(Configuration.DaysField);
            if (e.Value.Inherit)
            {
                m_pweForm.EntryStrings.Remove(Configuration.Interval);
            }
            else if (e.Value.unit != PEDC.SetExpired)
            {
                m_pweForm.EntryStrings.Set(Configuration.Interval, new ProtectedString(false, e.Value.ToString()));
            }
            m_pweForm.UpdateEntryStrings(false, true);
            if (e.Value.Off)
            {
                return;
            }
            ExpiryControlGroup ecg = (ExpiryControlGroup)Tools.GetField("m_cgExpiry", m_pweForm);
            PEDCalcValue       pcv = e.Value;

            if (!e.Value.Specific)
            {
                pcv = m_pweForm.EntryRef.GetPEDValueInherit();
            }
            if (!pcv.Off)
            {
                ecg.Checked = true;
                if (e.Value.unit == PEDC.SetExpired)
                {
                    ecg.Value = PEDCalcValue.UnixStart;
                }
                else
                {
                    ecg.Value = pcv.NewExpiryDateUtc;
                }
            }
        }
Example #16
0
        public static PEDCalcValue ConvertFromString(string stringValue)
        {
            PEDCalcValue result = new PEDCalcValue(PEDC.Inherit);

            if (string.IsNullOrEmpty(stringValue))
            {
                return(result);
            }
            string[] s   = stringValue.Split(m_Sep);
            int      val = 0;

            if (s.Count() < 1)
            {
                return(result);
            }
            if (!int.TryParse(s[0], out val))
            {
                //Ok, it's nothing like '5 days'
                //So it better is one of the defined values for enum PEDC
                try
                {
                    PEDC unit = (PEDC)Enum.Parse(typeof(PEDC), s[0], true);
                    result.unit = unit;
                }
                catch { }
                return(result);
            }
            result.value = val;
            try
            {
                PEDC unit = (PEDC)Enum.Parse(typeof(PEDC), s[1], true);
                result.unit = unit;
            }
            catch { }
            return(result);
        }
Example #17
0
 internal void SetInheritValue(PEDCalcValue pcvInherit)
 {
     DropDown.Items[3].Text = string.Format(PluginTranslate.OptionsInherit, pcvInherit.ToString(true));
 }
Example #18
0
        private void OnEntrySaving(object sender, KeePass.Util.CancellableOperationEventArgs e)
        {
            if (!Configuration.Active)
            {
                return;
            }
            //try reading fields from password entry form
            //checking and update the expiry date this way will save
            //us from creating one more backup just for
            //changing the expiry date in the "Touched" event
            if (m_pweForm == null)
            {
                return;
            }

            PEDCalcValue days = m_pweForm.EntryRef.GetPEDValue(true);

            if (!days.Specific)
            {
                return;                             //Nothing to do
            }
            CheckBox expires = (CheckBox)Tools.GetControl("m_cbExpires", m_pweForm);

            if (expires == null)
            {
                return;                              //read failed
            }
            if (!expires.Checked)
            {
                return;                               //entry does not expire (any longer)
            }
            DateTimePicker expiryDate = (DateTimePicker)Tools.GetControl("m_dtExpireDateTime", m_pweForm);

            if (expiryDate == null)
            {
                return;                                //read failed
            }
            if ((TimeUtil.ToUtc(expiryDate.Value, false) != m_pweForm.EntryRef.ExpiryTime) && m_pweForm.EntryRef.Expires)
            {
                return;                                                                                                                       //expiry date was already changed by the user
            }
            SecureTextBoxEx password = (SecureTextBoxEx)Tools.GetControl("m_tbPassword", m_pweForm);

            if (password == null)
            {
                return;                               //read failed;
            }
            byte[] pw_new = password.TextEx.ReadUtf8();
            byte[] pw_old = m_pweForm.EntryRef.Strings.GetSafe(PwDefs.PasswordField).ReadUtf8();
            if (MemUtil.ArraysEqual(pw_new, pw_old))
            {
                return;                                                  //password was not changed
            }
            //calculate new expiry date and write back to form field
            if (expiryDate.Value.Kind == DateTimeKind.Local)
            {
                expiryDate.Value = days.NewExpiryDateUtc.ToLocalTime();
            }
            else
            {
                expiryDate.Value = days.NewExpiryDateUtc;
            }
        }
Example #19
0
 internal QuickActionEventArgs(PEDCalcValue value)
 {
     Value = value;
 }
Example #20
0
 public override string ToString()
 {
     return(PEDCalcValue.GetTranslatedUnit(Unit));
 }
Example #21
0
        private void OnFormShown(object sender, EventArgs e)
        {
            PwEditMode   m         = PwEditMode.Invalid;
            PropertyInfo pEditMode = typeof(PwEntryForm).GetProperty("EditModeEx");

            if (pEditMode != null)             //will work starting with KeePass 2.41, preferred way as it's a public attribute
            {
                m = (PwEditMode)pEditMode.GetValue(m_pweForm, null);
            }
            else             // try reading private field
            {
                m = (PwEditMode)Tools.GetField("m_pwEditMode", m_pweForm);
            }
            PluginDebug.AddSuccess("Entryform shown, editmode: " + m.ToString(), 0);
            if ((m != PwEditMode.AddNewEntry) && (m != PwEditMode.EditExistingEntry))
            {
                return;
            }
            CustomContextMenuStripEx ctx = (CustomContextMenuStripEx)Tools.GetField("m_ctxDefaultTimes", m_pweForm);

            if (ctx != null)
            {
                ctx.Items.Add(new ToolStripSeparator());
                ToolStripMenuItem tsmiPED = CreatePEDMenu(false, true);
                ctx.Items.Add(tsmiPED);
                PluginDebug.AddSuccess("Found m_ctxDefaultTimes", 0);
            }
            else
            {
                PluginDebug.AddError("Could not find m_ctxDefaultTimes", 0);
            }

            PEDCalcValue   ped          = m_pweForm.EntryRef.GetPEDValue(true);
            CheckBox       cbExpires    = (CheckBox)Tools.GetControl("m_cbExpires", m_pweForm);
            DateTimePicker dtExpireDate = (DateTimePicker)Tools.GetControl("m_dtExpireDateTime", m_pweForm);
            DateTime       expiry       = ped.NewExpiryDateUtc.ToLocalTime();

            if (m == PwEditMode.EditExistingEntry)
            {
                cbExpires.CheckedChanged  += (o, e1) => CheckShowNewExpireDate();
                dtExpireDate.ValueChanged += (o, e1) => CheckShowNewExpireDate();
                SecureTextBoxEx password = (SecureTextBoxEx)Tools.GetControl("m_tbPassword", m_pweForm);
                password.TextChanged += (o, e1) => CheckShowNewExpireDate();
                Label lNewExpireDate = new Label();
                lNewExpireDate.Name = "PEDCalc_NewExpireDate";
                string sDate = string.Empty;
                m_iSelectedEntries = (int)m_host.MainWindow.GetSelectedEntriesCount();
                if ((Tools.KeePassVersion >= Configuration.KeePassMultipleEntries) && (m_iSelectedEntries > 1))
                {
                    PropertyInfo piMultiple = typeof(KeePass.Resources.KPRes).GetProperty("MultipleValues");
                    if (piMultiple != null)
                    {
                        sDate = piMultiple.GetValue(null, null) as string;
                    }
                    else
                    {
                        sDate = "?";
                    }
                }
                else if (dtExpireDate.Format == DateTimePickerFormat.Long)
                {
                    sDate = expiry.ToLongDateString();
                }
                else if (dtExpireDate.Format == DateTimePickerFormat.Short)
                {
                    sDate = expiry.ToShortDateString();
                }
                else if (dtExpireDate.Format == DateTimePickerFormat.Time)
                {
                    sDate = expiry.ToLongTimeString();
                }
                else
                {
                    sDate = expiry.ToString(dtExpireDate.CustomFormat);
                }
                lNewExpireDate.Text  = PluginTranslate.PluginName + ": " + sDate;
                lNewExpireDate.Left  = dtExpireDate.Left;
                lNewExpireDate.Top   = dtExpireDate.Top + dtExpireDate.Height + 2;
                lNewExpireDate.Width = dtExpireDate.Width;
                ToolTip tt = new ToolTip();
                tt.ToolTipTitle = PluginTranslate.PluginName;
                tt.ToolTipIcon  = ToolTipIcon.Info;
                tt.SetToolTip(lNewExpireDate, PluginTranslate.NewExpiryDateTooltip);
                dtExpireDate.Parent.Controls.Add(lNewExpireDate);
                int h = dtExpireDate.Parent.ClientSize.Height;
                if (h < lNewExpireDate.Top + lNewExpireDate.Height + 2)
                {
                    h = lNewExpireDate.Top + lNewExpireDate.Height + 2 - h;
                }
                else
                {
                    h = 0;
                }
                try
                {
                    dtExpireDate.Parent.Parent.Height += h;
                }
                catch { }
                CheckShowNewExpireDate();
            }

            if (m == PwEditMode.AddNewEntry)
            {
                if (ped.Off)
                {
                    return;
                }
                if ((cbExpires == null) || (dtExpireDate == null))
                {
                    Tools.ShowError(string.Format(PluginTranslate.ErrorInitExpiryDate, expiry.ToString()));
                    return;
                }
                m_pweForm.EntryRef.ExpiryTime = dtExpireDate.Value = expiry;
                m_pweForm.EntryRef.Expires    = cbExpires.Checked = true;
                PwEntry peInitialEntry = (PwEntry)Tools.GetField("m_pwInitialEntry", m_pweForm);
                if (peInitialEntry != null)
                {
                    peInitialEntry.Expires    = true;
                    peInitialEntry.ExpiryTime = expiry.ToUniversalTime();
                }
            }
        }