String placeholders and field reference replacement engine.
Example #1
0
        private static string FillEntryStringsSpecial(string str, SprContext ctx,
                                                      uint uRecursionLevel)
        {
            if ((str.IndexOf(UrlSpecialRmvScm, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialScm, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialHost, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialPort, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialPath, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialQuery, SprEngine.ScMethod) >= 0))
            {
                SprContext ctxRaw = ctx.WithoutContentTransformations();
                string     strUrl = SprEngine.FillIfExists(@"{URL}", @"{URL}",
                                                           ctx.Entry.Strings.GetSafe(PwDefs.UrlField), ctxRaw, uRecursionLevel);

                str = SprEngine.FillPlaceholder(str, UrlSpecialRmvScm,
                                                UrlUtil.RemoveScheme(strUrl), ctx);

                try
                {
                    Uri uri = new Uri(strUrl);

                    str = SprEngine.FillPlaceholder(str, UrlSpecialScm,
                                                    uri.Scheme, ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialHost,
                                                    uri.Host, ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialPort,
                                                    uri.Port.ToString(NumberFormatInfo.InvariantInfo), ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialPath,
                                                    uri.AbsolutePath, ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialQuery,
                                                    uri.Query, ctx);
                }
                catch (Exception) { }                // Invalid URI
            }

            return(str);
        }
Example #2
0
        private static string FillEntryStringsSpecial(string str, SprContext ctx,
                                                      uint uRecursionLevel)
        {
            if ((str.IndexOf(UrlSpecialRmvScm, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialScm, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialHost, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialPort, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialPath, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialQuery, SprEngine.ScMethod) >= 0))
            {
                string strUrl = SprEngine.FillIfExists(@"{URL}", @"{URL}",
                                                       ctx.Entry.Strings.GetSafe(PwDefs.UrlField), ctx, uRecursionLevel);

                str = StrUtil.ReplaceCaseInsensitive(str, UrlSpecialRmvScm,
                                                     UrlUtil.RemoveScheme(strUrl));

                try
                {
                    Uri uri = new Uri(strUrl);

                    str = StrUtil.ReplaceCaseInsensitive(str, UrlSpecialScm,
                                                         uri.Scheme);
                    str = StrUtil.ReplaceCaseInsensitive(str, UrlSpecialHost,
                                                         uri.Host);
                    str = StrUtil.ReplaceCaseInsensitive(str, UrlSpecialPort,
                                                         uri.Port.ToString());
                    str = StrUtil.ReplaceCaseInsensitive(str, UrlSpecialPath,
                                                         uri.AbsolutePath);
                    str = StrUtil.ReplaceCaseInsensitive(str, UrlSpecialQuery,
                                                         uri.Query);
                }
                catch (Exception) { }                // Invalid URI
            }

            return(str);
        }
Example #3
0
        private static string ReplacePickPwPlaceholder(string str,
                                                       string strPlaceholder, uint uCharCount, SprContext ctx,
                                                       uint uRecursionLevel)
        {
            if (str.IndexOf(strPlaceholder, StrUtil.CaseIgnoreCmp) < 0)
            {
                return(str);
            }

            ProtectedString ps = ctx.Entry.Strings.Get(PwDefs.PasswordField);

            if (ps != null)
            {
                string strPassword = ps.ReadString();

                string strPick = SprEngine.CompileInternal(strPassword,
                                                           ctx.WithoutContentTransformations(), uRecursionLevel + 1);

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

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

            return(StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, string.Empty));
        }
Example #4
0
        private static string FillRefPlaceholders(string strSeq, PwDatabase pwDatabase,
                                                  SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache)
        {
            if (pwDatabase == null)
            {
                return(strSeq);
            }

            string str = strSeq;

            const string strStart = @"{REF:";
            const string strEnd   = @"}";

            int nOffset = 0;

            for (int iLoop = 0; iLoop < 20; ++iLoop)
            {
                str = SprEngine.FillRefsUsingCache(str, vRefsCache);

                int nStart = str.IndexOf(strStart, nOffset, SprEngine.ScMethod);
                if (nStart < 0)
                {
                    break;
                }
                int nEnd = str.IndexOf(strEnd, nStart, SprEngine.ScMethod);
                if (nEnd < 0)
                {
                    break;
                }

                string strFullRef = str.Substring(nStart, nEnd - nStart + 1);

                string strRef = str.Substring(nStart + strStart.Length, nEnd -
                                              nStart - strStart.Length);
                if (strRef.Length <= 4)
                {
                    nOffset = nStart + 1; continue;
                }
                if (strRef[1] != '@')
                {
                    nOffset = nStart + 1; continue;
                }
                if (strRef[3] != ':')
                {
                    nOffset = nStart + 1; continue;
                }

                char chScan   = char.ToUpper(strRef[2]);
                char chWanted = char.ToUpper(strRef[0]);

                SearchParameters sp = SearchParameters.None;
                sp.SearchString = strRef.Substring(4);
                if (chScan == 'T')
                {
                    sp.SearchInTitles = true;
                }
                else if (chScan == 'U')
                {
                    sp.SearchInUserNames = true;
                }
                else if (chScan == 'A')
                {
                    sp.SearchInUrls = true;
                }
                else if (chScan == 'P')
                {
                    sp.SearchInPasswords = true;
                }
                else if (chScan == 'N')
                {
                    sp.SearchInNotes = true;
                }
                else if (chScan == 'I')
                {
                    sp.SearchInUuids = true;
                }
                else if (chScan == 'O')
                {
                    sp.SearchInOther = true;
                }
                else
                {
                    nOffset = nStart + 1; continue;
                }

                PwObjectList <PwEntry> lFound = new PwObjectList <PwEntry>();
                pwDatabase.RootGroup.SearchEntries(sp, lFound, true);
                if (lFound.UCount > 0)
                {
                    PwEntry peFound = lFound.GetAt(0);

                    string strInsData;
                    if (chWanted == 'T')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.TitleField);
                    }
                    else if (chWanted == 'U')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.UserNameField);
                    }
                    else if (chWanted == 'A')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.UrlField);
                    }
                    else if (chWanted == 'P')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.PasswordField);
                    }
                    else if (chWanted == 'N')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.NotesField);
                    }
                    else if (chWanted == 'I')
                    {
                        strInsData = peFound.Uuid.ToHexString();
                    }
                    else
                    {
                        nOffset = nStart + 1; continue;
                    }

                    string strInnerContent = SprEngine.CompileInternal(strInsData,
                                                                       peFound, pwDatabase, null, uRecursionLevel + 1, vRefsCache);
                    strInnerContent = SprEngine.TransformContent(strInnerContent, cf);

                    // str = str.Substring(0, nStart) + strInnerContent + str.Substring(nEnd + 1);
                    SprEngine.AddRefToCache(strFullRef, strInnerContent, vRefsCache);
                    str = SprEngine.FillRefsUsingCache(str, vRefsCache);
                }
                else
                {
                    nOffset = nStart + 1; continue;
                }
            }

            return(str);
        }
Example #5
0
        private static string CompileInternal(string strText, PwEntry pwEntry,
                                              PwDatabase pwDatabase, SprContentFlags cf, uint uRecursionLevel,
                                              SprRefsCache vRefsCache)
        {
            if (strText == null)
            {
                Debug.Assert(false); return(string.Empty);
            }

            if (uRecursionLevel >= SprEngine.MaxRecursionDepth)
            {
                Debug.Assert(false);                 // Most likely a recursive reference
                return(string.Empty);                // Do not return strText
            }

            string str = strText;

            str = AppLocator.FillPlaceholders(str, cf);
            str = EntryUtil.FillPlaceholders(str, pwEntry, cf);

            if (pwEntry != null)
            {
                foreach (KeyValuePair <string, ProtectedString> kvp in pwEntry.Strings)
                {
                    string strKey = PwDefs.IsStandardField(kvp.Key) ?
                                    (@"{" + kvp.Key + @"}") :
                                    (@"{" + PwDefs.AutoTypeStringPrefix + kvp.Key + @"}");

                    str = SprEngine.FillIfExists(str, strKey, kvp.Value, pwEntry,
                                                 pwDatabase, cf, uRecursionLevel, vRefsCache);
                }

                if (pwEntry.ParentGroup != null)
                {
                    str = SprEngine.FillIfExists(str, @"{GROUP}", new ProtectedString(
                                                     false, pwEntry.ParentGroup.Name), pwEntry, pwDatabase,
                                                 cf, uRecursionLevel, vRefsCache);

                    str = SprEngine.FillIfExists(str, @"{GROUPPATH}", new ProtectedString(
                                                     false, pwEntry.ParentGroup.GetFullPath()), pwEntry, pwDatabase,
                                                 cf, uRecursionLevel, vRefsCache);
                }
            }

            if (m_strAppExePath != null)
            {
                str = SprEngine.FillIfExists(str, @"{APPDIR}", new ProtectedString(
                                                 false, UrlUtil.GetFileDirectory(m_strAppExePath, false)),
                                             pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache);
            }

            if (pwDatabase != null)
            {
                str = SprEngine.FillIfExists(str, @"{DOCDIR}", new ProtectedString(
                                                 false, UrlUtil.GetFileDirectory(pwDatabase.IOConnectionInfo.Path, false)),
                                             pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache);
            }

            str = SprEngine.FillRefPlaceholders(str, pwDatabase, cf, uRecursionLevel,
                                                vRefsCache);

            // Replace environment variables
            foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
            {
                string strKey   = de.Key as string;
                string strValue = de.Value as string;

                if ((strKey != null) && (strValue != null))
                {
                    str = SprEngine.FillIfExists(str, @"%" + strKey + @"%",
                                                 new ProtectedString(false, strValue), pwEntry, pwDatabase,
                                                 cf, uRecursionLevel, vRefsCache);
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            return(str);
        }