private static string FillIfExists(string strData, string strPlaceholder, ProtectedString psParsable, PwEntry pwEntry, PwDatabase pwDatabase, SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache) { if (strData == null) { Debug.Assert(false); return(string.Empty); } if (strPlaceholder == null) { Debug.Assert(false); return(strData); } if (strPlaceholder.Length == 0) { Debug.Assert(false); return(strData); } if (psParsable == null) { Debug.Assert(false); return(strData); } if (strData.IndexOf(strPlaceholder, SprEngine.ScMethod) >= 0) { return(SprEngine.FillPlaceholder(strData, strPlaceholder, SprEngine.CompileInternal(psParsable.ReadString(), pwEntry, pwDatabase, null, uRecursionLevel + 1, vRefsCache), cf)); } return(strData); }
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; }
public static string Compile(string strText, bool bIsAutoTypeSequence, PwEntry pwEntry, PwDatabase pwDatabase, bool bEscapeForAutoType, bool bEscapeQuotesForCommandLine) { if (strText == null) { Debug.Assert(false); return(string.Empty); } if (strText.Length == 0) { return(string.Empty); } SprEngine.InitializeStatic(); SprContentFlags cf = new SprContentFlags(bEscapeForAutoType && bIsAutoTypeSequence, bEscapeQuotesForCommandLine); SprRefsCache vRefsCache = new SprRefsCache(); string str = SprEngine.CompileInternal(strText, pwEntry, pwDatabase, cf, 0, vRefsCache); if (bEscapeForAutoType && !bIsAutoTypeSequence) { str = SprEncoding.MakeAutoTypeSequence(str); } return(str); }
private static string FillIfExists(string strData, string strPlaceholder, ProtectedString psParsable, PwEntry pwEntry, PwDatabase pwDatabase, SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache) { // The UrlRemoveSchemeOnce property of cf must be cleared // before this method returns and before any recursive call bool bRemoveScheme = false; if (cf != null) { bRemoveScheme = cf.UrlRemoveSchemeOnce; cf.UrlRemoveSchemeOnce = false; } if (strData == null) { Debug.Assert(false); return(string.Empty); } if (strPlaceholder == null) { Debug.Assert(false); return(strData); } if (strPlaceholder.Length == 0) { Debug.Assert(false); return(strData); } if (psParsable == null) { Debug.Assert(false); return(strData); } if (strData.IndexOf(strPlaceholder, SprEngine.ScMethod) >= 0) { string strReplacement = SprEngine.CompileInternal( psParsable.ReadString(), pwEntry, pwDatabase, null, uRecursionLevel + 1, vRefsCache); if (bRemoveScheme) { strReplacement = UrlUtil.RemoveScheme(strReplacement); } return(SprEngine.FillPlaceholder(strData, strPlaceholder, strReplacement, cf)); } return(strData); }
public static string TransformContent(string strContent, SprContentFlags cf) { if(strContent == null) { Debug.Assert(false); return string.Empty; } string str = strContent; if(cf != null) { if(cf.EncodeQuotesForCommandLine) str = SprEncoding.MakeCommandQuotes(str); if(cf.EncodeAsAutoTypeSequence) str = SprEncoding.MakeAutoTypeSequence(str); } return str; }
public static string Compile(string strText, bool bIsAutoTypeSequence, PwEntry pwEntry, PwDatabase pwDatabase, bool bEscapeForAutoType, bool bEscapeQuotesForCommandLine) { if(strText == null) { Debug.Assert(false); return string.Empty; } if(strText.Length == 0) return string.Empty; SprEngine.InitializeStatic(); SprContentFlags cf = new SprContentFlags(bEscapeForAutoType && bIsAutoTypeSequence, bEscapeQuotesForCommandLine); SprRefsCache vRefsCache = new SprRefsCache(); string str = SprEngine.CompileInternal(strText, pwEntry, pwDatabase, cf, 0, vRefsCache); if(bEscapeForAutoType && !bIsAutoTypeSequence) str = SprEncoding.MakeAutoTypeSequence(str); return str; }
private static string FillPlaceholder(string strData, string strPlaceholder, string strReplaceWith, SprContentFlags cf) { if (strData == null) { Debug.Assert(false); return(string.Empty); } if (strPlaceholder == null) { Debug.Assert(false); return(strData); } if (strPlaceholder.Length == 0) { Debug.Assert(false); return(strData); } if (strReplaceWith == null) { Debug.Assert(false); return(strData); } return(StrUtil.ReplaceCaseInsensitive(strData, strPlaceholder, SprEngine.TransformContent(strReplaceWith, cf))); }
public static string TransformContent(string strContent, SprContentFlags cf) { if (strContent == null) { Debug.Assert(false); return(string.Empty); } string str = strContent; if (cf != null) { if (cf.EncodeQuotesForCommandLine) { str = SprEncoding.MakeCommandQuotes(str); } if (cf.EncodeAsAutoTypeSequence) { str = SprEncoding.MakeAutoTypeSequence(str); } } return(str); }
public static string FillPlaceholdersFinal(string strText, PwEntry pe, PwDatabase pd, SprContentFlags cf) { if(pe == null) return strText; string str = strText; str = ReplaceNewPasswordPlaceholder(str, pe, pd, cf); str = ReplaceHmacOtpPlaceholder(str, pe, pd, cf); return str; }
public static string FillPlaceholders(string strText, PwEntry pe, PwDatabase pd, SprContentFlags cf) { if(pe == null) return strText; string str = strText; // Legacy, for backward compatibility only; see PickChars str = ReplacePickPw(str, pe, pd, cf); return str; }
public static string FillPlaceholders(string strText, SprContentFlags cf) { string str = strText; str = AppLocator.ReplacePath(str, @"{INTERNETEXPLORER}", AppLocator.InternetExplorerPath, cf); str = AppLocator.ReplacePath(str, @"{FIREFOX}", AppLocator.FirefoxPath, cf); str = AppLocator.ReplacePath(str, @"{OPERA}", AppLocator.OperaPath, cf); return str; }
private static string FillIfExists(string strData, string strPlaceholder, ProtectedString psParsable, PwEntry pwEntry, PwDatabase pwDatabase, SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache) { if(strData == null) { Debug.Assert(false); return string.Empty; } if(strPlaceholder == null) { Debug.Assert(false); return strData; } if(strPlaceholder.Length == 0) { Debug.Assert(false); return strData; } if(psParsable == null) { Debug.Assert(false); return strData; } if(strData.IndexOf(strPlaceholder, SprEngine.ScMethod) >= 0) return SprEngine.FillPlaceholder(strData, strPlaceholder, SprEngine.CompileInternal(psParsable.ReadString(), pwEntry, pwDatabase, null, uRecursionLevel + 1, vRefsCache), cf); return strData; }
private static string ReplacePickChars(string strText, PwEntry pe, PwDatabase pd, SprContentFlags cf) { string str = strText; Dictionary<string, string> dPicked = new Dictionary<string, string>(); while(true) { const string strStart = @"{PICKCHARS"; int iStart = str.IndexOf(strStart, StrUtil.CaseIgnoreCmp); if(iStart < 0) break; int iEnd = str.IndexOf('}', iStart); if(iEnd < 0) break; string strPlaceholder = str.Substring(iStart, iEnd - iStart + 1); string strParam = str.Substring(iStart + strStart.Length, iEnd - (iStart + strStart.Length)); string strRep = string.Empty; bool bEncode = true; if(strParam.Length == 0) strRep = ShowCharPickDlg(pe.Strings.GetSafe(PwDefs.PasswordField), 0, null); else if(strParam.StartsWith(":")) { string strParams = strParam.Substring(1); string[] vParams = strParams.Split(new char[] { ':' }, StringSplitOptions.None); string strField = string.Empty; if(vParams.Length >= 1) strField = (vParams[0] ?? string.Empty).Trim(); if(strField.Length == 0) strField = PwDefs.PasswordField; string strOptions = string.Empty; if(vParams.Length >= 2) strOptions = (vParams[1] ?? string.Empty); Dictionary<string, string> dOptions = new Dictionary<string, string>(); string[] vOptions = strOptions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach(string strOption in vOptions) { string[] vKvp = strOption.Split(new char[] { '=' }, StringSplitOptions.None); if(vKvp.Length != 2) continue; dOptions[vKvp[0].Trim().ToLower()] = vKvp[1].Trim(); } string strID = string.Empty; if(dOptions.ContainsKey("id")) strID = dOptions["id"].ToLower(); uint uCharCount = 0; if(dOptions.ContainsKey("c")) uint.TryParse(dOptions["c"], out uCharCount); if(dOptions.ContainsKey("count")) uint.TryParse(dOptions["count"], out uCharCount); bool? bInitHide = null; if(dOptions.ContainsKey("hide")) bInitHide = StrUtil.StringToBool(dOptions["hide"]); ProtectedString psContent = pe.Strings.GetSafe(strField); if(psContent.Length == 0) { } // Leave strRep empty else if((strID.Length > 0) && dPicked.ContainsKey(strID)) strRep = dPicked[strID]; else strRep = ShowCharPickDlg(psContent, uCharCount, bInitHide); if(strID.Length > 0) dPicked[strID] = strRep; if(dOptions.ContainsKey("conv")) { int iOffset = 0; if(dOptions.ContainsKey("conv-offset")) int.TryParse(dOptions["conv-offset"], out iOffset); string strConvFmt = string.Empty; if(dOptions.ContainsKey("conv-fmt")) strConvFmt = dOptions["conv-fmt"]; string strConv = dOptions["conv"]; if(strConv.Equals("d", StrUtil.CaseIgnoreCmp)) { strRep = ConvertToDownArrows(strRep, iOffset, strConvFmt); bEncode = false; } } } str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, bEncode ? SprEngine.TransformContent(strRep, cf) : strRep); } return str; }
private static string FillPlaceholder(string strData, string strPlaceholder, string strReplaceWith, SprContentFlags cf) { if(strData == null) { Debug.Assert(false); return string.Empty; } if(strPlaceholder == null) { Debug.Assert(false); return strData; } if(strPlaceholder.Length == 0) { Debug.Assert(false); return strData; } if(strReplaceWith == null) { Debug.Assert(false); return strData; } return StrUtil.ReplaceCaseInsensitive(strData, strPlaceholder, SprEngine.TransformContent(strReplaceWith, cf)); }
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, pwDatabase, cf); if(pwEntry != null) { List<string> vKeys = pwEntry.Strings.GetKeys(); // Ensure that all standard field names are in the list // (this is required in order to replace the standard placeholders // even if the corresponding standard field isn't present in // the entry) List<string> vStdNames = PwDefs.GetStandardFields(); foreach(string strStdField in vStdNames) { if(!vKeys.Contains(strStdField)) vKeys.Add(strStdField); } // Do not directly enumerate the strings in pwEntry.Strings, // because strings might change during the Spr compilation foreach(string strField in vKeys) { string strKey = (PwDefs.IsStandardField(strField) ? (@"{" + strField + @"}") : (@"{" + PwDefs.AutoTypeStringPrefix + strField + @"}")); // Use GetSafe because the field doesn't necessarily exist // (might be a standard field that has been added above) str = SprEngine.FillIfExists(str, strKey, pwEntry.Strings.GetSafe( strField), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); } if(cf != null) cf.UrlRemoveSchemeOnce = true; str = SprEngine.FillIfExists(str, @"{URL:RMVSCM}", pwEntry.Strings.GetSafe(PwDefs.UrlField), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); if(cf != null) { Debug.Assert(!cf.UrlRemoveSchemeOnce); } if(str.IndexOf(@"{PASSWORD_ENC}", SprEngine.ScMethod) >= 0) str = SprEngine.FillIfExists(str, @"{PASSWORD_ENC}", new ProtectedString(false, StrUtil.EncryptString(pwEntry.Strings.ReadSafe(PwDefs.PasswordField))), 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, false)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); if(pwDatabase != null) { // For backward compatibility only str = SprEngine.FillIfExists(str, @"{DOCDIR}", new ProtectedString( false, UrlUtil.GetFileDirectory(pwDatabase.IOConnectionInfo.Path, false, false)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_PATH}", new ProtectedString( false, pwDatabase.IOConnectionInfo.Path), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_DIR}", new ProtectedString( false, UrlUtil.GetFileDirectory(pwDatabase.IOConnectionInfo.Path, false, false)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_NAME}", new ProtectedString( false, UrlUtil.GetFileName(pwDatabase.IOConnectionInfo.Path)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_BASENAME}", new ProtectedString( false, UrlUtil.StripExtension(UrlUtil.GetFileName( pwDatabase.IOConnectionInfo.Path))), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_EXT}", new ProtectedString( false, UrlUtil.GetExtension(pwDatabase.IOConnectionInfo.Path)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); } str = SprEngine.FillIfExists(str, @"{ENV_DIRSEP}", new ProtectedString( false, Path.DirectorySeparatorChar.ToString()), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); DateTime dtNow = DateTime.Now; // Local time str = SprEngine.FillIfExists(str, @"{DT_YEAR}", new ProtectedString( false, dtNow.Year.ToString("D4")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_MONTH}", new ProtectedString( false, dtNow.Month.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_DAY}", new ProtectedString( false, dtNow.Day.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_HOUR}", new ProtectedString( false, dtNow.Hour.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_MINUTE}", new ProtectedString( false, dtNow.Minute.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_SECOND}", new ProtectedString( false, dtNow.Second.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_SIMPLE}", new ProtectedString( false, dtNow.ToString("yyyyMMddHHmmss")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); dtNow = dtNow.ToUniversalTime(); str = SprEngine.FillIfExists(str, @"{DT_UTC_YEAR}", new ProtectedString( false, dtNow.Year.ToString("D4")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_MONTH}", new ProtectedString( false, dtNow.Month.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_DAY}", new ProtectedString( false, dtNow.Day.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_HOUR}", new ProtectedString( false, dtNow.Hour.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_MINUTE}", new ProtectedString( false, dtNow.Minute.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_SECOND}", new ProtectedString( false, dtNow.Second.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_SIMPLE}", new ProtectedString( false, dtNow.ToString("yyyyMMddHHmmss")), 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); } } str = EntryUtil.FillPlaceholdersFinal(str, pwEntry, pwDatabase, cf); return str; }
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); }
public static string FillPlaceholders(string strText, PwEntry pe, PwDatabase pd, SprContentFlags cf) { if(pe == null) return strText; string str = strText; str = ReplacePickPw(str, pe, pd, cf); return str; }
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); }
/* private static string ReplacePickPw(string strText, PwEntry pe, SprContentFlags cf) { string str = strText; for(int iID = 1; iID < (int.MaxValue - 1); ++iID) { string strPlaceholder = @"{PICKPASSWORDCHARS"; if(iID > 1) strPlaceholder += iID.ToString(); strPlaceholder += @"}"; if(str.IndexOf(strPlaceholder, StrUtil.CaseIgnoreCmp) >= 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, 0); if(cpf.ShowDialog() == DialogResult.OK) str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, SprEngine.TransformContent(cpf.SelectedCharacters.ReadString(), cf)); } } str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, string.Empty); } else break; } return str; } */ private static string ReplacePickPw(string strText, PwEntry pe, PwDatabase pd, SprContentFlags cf) { string str = strText; while(true) { const string strStart = @"{PICKPASSWORDCHARS"; int iStart = str.IndexOf(strStart, StrUtil.CaseIgnoreCmp); if(iStart < 0) break; int iEnd = str.IndexOf('}', iStart); if(iEnd < 0) break; string strPlaceholder = str.Substring(iStart, iEnd - iStart + 1); string strParam = str.Substring(iStart + strStart.Length, iEnd - (iStart + strStart.Length)); string[] vParams = strParam.Split(new char[] { ':' }); uint uCharCount = 0; if(vParams.Length >= 2) uint.TryParse(vParams[1], out uCharCount); str = ReplacePickPlaceholder(str, strPlaceholder, pe, pd, cf, uCharCount); } return str; }
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, pwDatabase, cf); if (pwEntry != null) { List <string> vKeys = pwEntry.Strings.GetKeys(); // Ensure that all standard field names are in the list // (this is required in order to replace the standard placeholders // even if the corresponding standard field isn't present in // the entry) List <string> vStdNames = PwDefs.GetStandardFields(); foreach (string strStdField in vStdNames) { if (!vKeys.Contains(strStdField)) { vKeys.Add(strStdField); } } // Do not directly enumerate the strings in pwEntry.Strings, // because strings might change during the Spr compilation foreach (string strField in vKeys) { string strKey = (PwDefs.IsStandardField(strField) ? (@"{" + strField + @"}") : (@"{" + PwDefs.AutoTypeStringPrefix + strField + @"}")); // Use GetSafe because the field doesn't necessarily exist // (might be a standard field that has been added above) str = SprEngine.FillIfExists(str, strKey, pwEntry.Strings.GetSafe( strField), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); } if (cf != null) { cf.UrlRemoveSchemeOnce = true; } str = SprEngine.FillIfExists(str, @"{URL:RMVSCM}", pwEntry.Strings.GetSafe(PwDefs.UrlField), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); if (cf != null) { Debug.Assert(!cf.UrlRemoveSchemeOnce); } if (str.IndexOf(@"{PASSWORD_ENC}", SprEngine.ScMethod) >= 0) { str = SprEngine.FillIfExists(str, @"{PASSWORD_ENC}", new ProtectedString(false, StrUtil.EncryptString(pwEntry.Strings.ReadSafe(PwDefs.PasswordField))), 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, false)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); } if (pwDatabase != null) { // For backward compatibility only str = SprEngine.FillIfExists(str, @"{DOCDIR}", new ProtectedString( false, UrlUtil.GetFileDirectory(pwDatabase.IOConnectionInfo.Path, false, false)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_PATH}", new ProtectedString( false, pwDatabase.IOConnectionInfo.Path), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_DIR}", new ProtectedString( false, UrlUtil.GetFileDirectory(pwDatabase.IOConnectionInfo.Path, false, false)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_NAME}", new ProtectedString( false, UrlUtil.GetFileName(pwDatabase.IOConnectionInfo.Path)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_BASENAME}", new ProtectedString( false, UrlUtil.StripExtension(UrlUtil.GetFileName( pwDatabase.IOConnectionInfo.Path))), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DB_EXT}", new ProtectedString( false, UrlUtil.GetExtension(pwDatabase.IOConnectionInfo.Path)), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); } str = SprEngine.FillIfExists(str, @"{ENV_DIRSEP}", new ProtectedString( false, Path.DirectorySeparatorChar.ToString()), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); DateTime dtNow = DateTime.Now; // Local time str = SprEngine.FillIfExists(str, @"{DT_YEAR}", new ProtectedString( false, dtNow.Year.ToString("D4")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_MONTH}", new ProtectedString( false, dtNow.Month.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_DAY}", new ProtectedString( false, dtNow.Day.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_HOUR}", new ProtectedString( false, dtNow.Hour.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_MINUTE}", new ProtectedString( false, dtNow.Minute.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_SECOND}", new ProtectedString( false, dtNow.Second.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_SIMPLE}", new ProtectedString( false, dtNow.ToString("yyyyMMddHHmmss")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); dtNow = dtNow.ToUniversalTime(); str = SprEngine.FillIfExists(str, @"{DT_UTC_YEAR}", new ProtectedString( false, dtNow.Year.ToString("D4")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_MONTH}", new ProtectedString( false, dtNow.Month.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_DAY}", new ProtectedString( false, dtNow.Day.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_HOUR}", new ProtectedString( false, dtNow.Hour.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_MINUTE}", new ProtectedString( false, dtNow.Minute.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_SECOND}", new ProtectedString( false, dtNow.Second.ToString("D2")), pwEntry, pwDatabase, cf, uRecursionLevel, vRefsCache); str = SprEngine.FillIfExists(str, @"{DT_UTC_SIMPLE}", new ProtectedString( false, dtNow.ToString("yyyyMMddHHmmss")), 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); } } str = EntryUtil.FillPlaceholdersFinal(str, pwEntry, pwDatabase, cf); return(str); }
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); }
private static string FillIfExists(string strData, string strPlaceholder, ProtectedString psParsable, PwEntry pwEntry, PwDatabase pwDatabase, SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache) { // The UrlRemoveSchemeOnce property of cf must be cleared // before this method returns and before any recursive call bool bRemoveScheme = false; if(cf != null) { bRemoveScheme = cf.UrlRemoveSchemeOnce; cf.UrlRemoveSchemeOnce = false; } if(strData == null) { Debug.Assert(false); return string.Empty; } if(strPlaceholder == null) { Debug.Assert(false); return strData; } if(strPlaceholder.Length == 0) { Debug.Assert(false); return strData; } if(psParsable == null) { Debug.Assert(false); return strData; } if(strData.IndexOf(strPlaceholder, SprEngine.ScMethod) >= 0) { string strReplacement = SprEngine.CompileInternal( psParsable.ReadString(), pwEntry, pwDatabase, null, uRecursionLevel + 1, vRefsCache); if(bRemoveScheme) strReplacement = UrlUtil.RemoveScheme(strReplacement); return SprEngine.FillPlaceholder(strData, strPlaceholder, strReplacement, cf); } return strData; }
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; }
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; }
private static string ReplaceHmacOtpPlaceholder(string strText, PwEntry pe, PwDatabase pd, SprContentFlags cf) { if((pe == null) || (pd == null)) return strText; string str = strText; const string strHmacOtpPlh = @"{HMACOTP}"; if(str.IndexOf(strHmacOtpPlh, StrUtil.CaseIgnoreCmp) >= 0) { const string strKeyField = "HmacOtp-Secret"; const string strCounterField = "HmacOtp-Counter"; byte[] pbSecret = Encoding.UTF8.GetBytes(pe.Strings.ReadSafe( strKeyField)); string strCounter = pe.Strings.ReadSafe(strCounterField); ulong uCounter; ulong.TryParse(strCounter, out uCounter); string strValue = HmacOtp.Generate(pbSecret, uCounter, 6, false, -1); pe.Strings.Set(strCounterField, new ProtectedString(false, (uCounter + 1).ToString())); pd.Modified = true; str = StrUtil.ReplaceCaseInsensitive(str, strHmacOtpPlh, strValue); } return str; }
private static string ReplacePath(string str, string strPlaceholder, string strFill, SprContentFlags cf) { 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((cf != null) && cf.EncodeQuotesForCommandLine) strRep = "\"" + SprEngine.TransformContent(strFill, cf) + "\""; else strRep = SprEngine.TransformContent("\"" + strFill + "\"", cf); return StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, strRep); }
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; }