Пример #1
0
        private static string ReplacePath(string str, string strPlaceholder,
                                          string strFill, SprContext ctx)
        {
            if (str == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (strPlaceholder == null)
            {
                Debug.Assert(false); return(str);
            }
            if (strPlaceholder.Length == 0)
            {
                Debug.Assert(false); return(str);
            }
            if (strFill == null)
            {
                return(str);                            // No assert
            }
            string strRep;

            if ((ctx != null) && ctx.EncodeForCommandLine)
            {
                strRep = "\"" + SprEngine.TransformContent(strFill, ctx) + "\"";
            }
            else
            {
                strRep = SprEngine.TransformContent("\"" + strFill + "\"", ctx);
            }

            return(StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, strRep));
        }
Пример #2
0
        private static string ReplaceNewPasswordPlaceholder(string strText,
                                                            PwEntry pe, PwDatabase pd, SprContentFlags cf)
        {
            if ((pe == null) || (pd == null))
            {
                return(strText);
            }

            string str = strText;

            const string strNewPwPlh = @"{NEWPASSWORD}";

            if (str.IndexOf(strNewPwPlh, StrUtil.CaseIgnoreCmp) >= 0)
            {
                ProtectedString psAutoGen = new ProtectedString(
                    pd.MemoryProtection.ProtectPassword);
                PwgError e = PwGenerator.Generate(psAutoGen,
                                                  Program.Config.PasswordGenerator.AutoGeneratedPasswordsProfile,
                                                  null, Program.PwGeneratorPool);

                if (e == PwgError.Success)
                {
                    pe.CreateBackup();
                    pe.Strings.Set(PwDefs.PasswordField, psAutoGen);
                    pd.Modified = true;

                    string strIns = SprEngine.TransformContent(psAutoGen.ReadString(), cf);
                    str = StrUtil.ReplaceCaseInsensitive(str, strNewPwPlh, strIns);
                }
            }

            return(str);
        }
Пример #3
0
        private static string ReplacePickPlaceholder(string str,
                                                     string strPlaceholder, PwEntry pe, PwDatabase pd, SprContentFlags cf,
                                                     uint uCharCount)
        {
            if (str.IndexOf(strPlaceholder, StrUtil.CaseIgnoreCmp) < 0)
            {
                return(str);
            }

            ProtectedString ps = pe.Strings.Get(PwDefs.PasswordField);

            if (ps != null)
            {
                string strPassword = ps.ReadString();
                string strPick     = SprEngine.Compile(strPassword, false, pe, pd,
                                                       false, false); // Do not transform content yet

                if (!string.IsNullOrEmpty(strPick))
                {
                    ProtectedString psPick = new ProtectedString(false, strPick);
                    CharPickerForm  dlg    = new CharPickerForm();
                    dlg.InitEx(psPick, true, true, uCharCount);

                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder,
                                                             SprEngine.TransformContent(dlg.SelectedCharacters.ReadString(), cf));
                    }
                }
            }

            return(StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, string.Empty));
        }
Пример #4
0
        private static string GeneratePassword(string strProfile, SprContext ctx)
        {
            PwProfile prf = Program.Config.PasswordGenerator.AutoGeneratedPasswordsProfile;

            if (!string.IsNullOrEmpty(strProfile))
            {
                if (strProfile == @"~")
                {
                    prf = PwProfile.DeriveFromPassword(ctx.Entry.Strings.GetSafe(
                                                           PwDefs.PasswordField));
                }
                else
                {
                    List <PwProfile> lPrf = PwGeneratorUtil.GetAllProfiles(false);
                    foreach (PwProfile p in lPrf)
                    {
                        if (strProfile.Equals(p.Name, StrUtil.CaseIgnoreCmp))
                        {
                            prf = p;
                            break;
                        }
                    }
                }
            }

            ProtectedString ps;
            PwgError        e = PwGenerator.Generate(out ps, prf, null,
                                                     Program.PwGeneratorPool);

            if ((e != PwgError.Success) || (ps == null))
            {
                return(string.Empty);
            }

            string strGen = ps.ReadString();

            strGen = SprEngine.TransformContent(strGen, ctx);
            return(strGen);
        }
Пример #5
0
        public static string FillPlaceholders(string strText, PwEntry pe,
                                              SprContentFlags cf)
        {
            if (pe == null)
            {
                return(strText);
            }

            string str = strText;

            if (str.ToUpper().IndexOf(@"{PICKPASSWORDCHARS}") >= 0)
            {
                ProtectedString ps = pe.Strings.Get(PwDefs.PasswordField);
                if (ps != null)
                {
                    byte[] pb        = ps.ReadUtf8();
                    bool   bNotEmpty = (pb.Length > 0);
                    Array.Clear(pb, 0, pb.Length);

                    if (bNotEmpty)
                    {
                        CharPickerForm cpf = new CharPickerForm();
                        cpf.InitEx(ps, true, true);

                        if (cpf.ShowDialog() == DialogResult.OK)
                        {
                            str = StrUtil.ReplaceCaseInsensitive(str, @"{PICKPASSWORDCHARS}",
                                                                 SprEngine.TransformContent(cpf.SelectedCharacters.ReadString(), cf));
                        }
                    }
                }

                str = StrUtil.ReplaceCaseInsensitive(str, @"{PICKPASSWORDCHARS}", string.Empty);
            }

            return(str);
        }
Пример #6
0
        private static string ReplaceNewPasswordPlaceholder(string strText,
                                                            SprContext ctx, uint uRecursionLevel)
        {
            PwEntry    pe = ctx.Entry;
            PwDatabase pd = ctx.Database;

            if ((pe == null) || (pd == null))
            {
                return(strText);
            }

            string str = strText;

            const string strNewPwStart = @"{NEWPASSWORD";

            if (str.IndexOf(strNewPwStart, StrUtil.CaseIgnoreCmp) < 0)
            {
                return(str);
            }

            string strGen = null;

            int           iStart;
            List <string> lParams;

            while (SprEngine.ParseAndRemovePlhWithParams(ref str, ctx, uRecursionLevel,
                                                         strNewPwStart + ":", out iStart, out lParams, true))
            {
                if (strGen == null)
                {
                    strGen = GeneratePassword((((lParams != null) &&
                                                (lParams.Count > 0)) ? lParams[0] : string.Empty), ctx);
                }

                string strIns = SprEngine.TransformContent(strGen, ctx);
                str = str.Insert(iStart, strIns);
            }

            const string strNewPwPlh = strNewPwStart + @"}";

            if (str.IndexOf(strNewPwPlh, StrUtil.CaseIgnoreCmp) >= 0)
            {
                if (strGen == null)
                {
                    strGen = GeneratePassword(null, ctx);
                }

                string strIns = SprEngine.TransformContent(strGen, ctx);
                str = StrUtil.ReplaceCaseInsensitive(str, strNewPwPlh, strIns);
            }

            if (strGen != null)
            {
                pe.CreateBackup(pd);

                ProtectedString psGen = new ProtectedString(
                    pd.MemoryProtection.ProtectPassword, strGen);
                pe.Strings.Set(PwDefs.PasswordField, psGen);

                pe.Touch(true, false);
                pd.Modified = true;
            }
            else
            {
                Debug.Assert(false);
            }

            return(str);
        }
Пример #7
0
        private static string ReplacePickField(string strText, SprContext ctx)
        {
            string     str = strText;
            PwEntry    pe  = ((ctx != null) ? ctx.Entry : null);
            PwDatabase pd  = ((ctx != null) ? ctx.Database : null);

            while (true)
            {
                const string strPlh = @"{PICKFIELD}";
                int          p      = str.IndexOf(strPlh, StrUtil.CaseIgnoreCmp);
                if (p < 0)
                {
                    break;
                }

                string strRep = string.Empty;

                List <FpField> l = new List <FpField>();
                string         strGroup;

                if (pe != null)
                {
                    strGroup = KPRes.Entry + " - " + KPRes.StandardFields;

                    Debug.Assert(PwDefs.GetStandardFields().Count == 5);
                    l.Add(new FpField(KPRes.Title, pe.Strings.GetSafe(PwDefs.TitleField),
                                      strGroup));
                    l.Add(new FpField(KPRes.UserName, pe.Strings.GetSafe(PwDefs.UserNameField),
                                      strGroup));
                    l.Add(new FpField(KPRes.Password, pe.Strings.GetSafe(PwDefs.PasswordField),
                                      strGroup));
                    l.Add(new FpField(KPRes.Url, pe.Strings.GetSafe(PwDefs.UrlField),
                                      strGroup));
                    l.Add(new FpField(KPRes.Notes, pe.Strings.GetSafe(PwDefs.NotesField),
                                      strGroup));

                    strGroup = KPRes.Entry + " - " + KPRes.CustomFields;
                    foreach (KeyValuePair <string, ProtectedString> kvp in pe.Strings)
                    {
                        if (PwDefs.IsStandardField(kvp.Key))
                        {
                            continue;
                        }

                        l.Add(new FpField(kvp.Key, kvp.Value, strGroup));
                    }

                    PwGroup pg = pe.ParentGroup;
                    if (pg != null)
                    {
                        strGroup = KPRes.Group;

                        l.Add(new FpField(KPRes.Name, new ProtectedString(
                                              false, pg.Name), strGroup));

                        if (pg.Notes.Length > 0)
                        {
                            l.Add(new FpField(KPRes.Notes, new ProtectedString(
                                                  false, pg.Notes), strGroup));
                        }
                    }
                }

                if (pd != null)
                {
                    strGroup = KPRes.Database;

                    if (pd.Name.Length > 0)
                    {
                        l.Add(new FpField(KPRes.Name, new ProtectedString(
                                              false, pd.Name), strGroup));
                    }
                    l.Add(new FpField(KPRes.FileOrUrl, new ProtectedString(
                                          false, pd.IOConnectionInfo.Path), strGroup));
                }

                FpField fpf = FieldPickerForm.ShowAndRestore(KPRes.PickField,
                                                             KPRes.PickFieldDesc, l);
                if (fpf != null)
                {
                    strRep = fpf.Value.ReadString();
                }

                strRep = SprEngine.Compile(strRep, ctx.WithoutContentTransformations());
                strRep = SprEngine.TransformContent(strRep, ctx);

                str = str.Remove(p, strPlh.Length);
                str = str.Insert(p, strRep);
            }

            return(str);
        }