Exemple #1
0
		private static string CompileInternal(string strText, SprContext ctx,
			uint uRecursionLevel)
		{
			if(strText == null) { Debug.Assert(false); return string.Empty; }
			if(ctx == null) { Debug.Assert(false); ctx = new SprContext(); }

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

			string str = strText;

			bool bExt = ((ctx.Flags & (SprCompileFlags.ExtActive |
				SprCompileFlags.ExtNonActive)) != SprCompileFlags.None);
			if(bExt && (SprEngine.FilterCompilePre != null))
			{
				SprEventArgs args = new SprEventArgs(str, ctx.Clone());
				SprEngine.FilterCompilePre(null, args);
				str = args.Text;
			}

			if((ctx.Flags & SprCompileFlags.Comments) != SprCompileFlags.None)
				str = RemoveComments(str);

			if((ctx.Flags & SprCompileFlags.TextTransforms) != SprCompileFlags.None)
				str = PerformTextTransforms(str, ctx, uRecursionLevel);

			if((ctx.Flags & SprCompileFlags.AppPaths) != SprCompileFlags.None)
				str = AppLocator.FillPlaceholders(str, ctx);

			if(ctx.Entry != null)
			{
				if((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
					str = ReplacePickPw(str, ctx, uRecursionLevel);

				if((ctx.Flags & SprCompileFlags.EntryStrings) != SprCompileFlags.None)
					str = FillEntryStrings(str, ctx, uRecursionLevel);

				if((ctx.Flags & SprCompileFlags.EntryStringsSpecial) != SprCompileFlags.None)
				{
					// ctx.UrlRemoveSchemeOnce = true;
					// str = SprEngine.FillIfExists(str, @"{URL:RMVSCM}",
					//	ctx.Entry.Strings.GetSafe(PwDefs.UrlField), ctx, uRecursionLevel);
					// Debug.Assert(!ctx.UrlRemoveSchemeOnce);

					str = FillEntryStringsSpecial(str, ctx, uRecursionLevel);
				}

				if(((ctx.Flags & SprCompileFlags.PasswordEnc) != SprCompileFlags.None) &&
					(str.IndexOf(@"{PASSWORD_ENC}", SprEngine.ScMethod) >= 0))
				{
					string strPwCmp = SprEngine.FillIfExists(@"{PASSWORD}",
						@"{PASSWORD}", ctx.Entry.Strings.GetSafe(PwDefs.PasswordField),
						ctx.WithoutContentTransformations(), uRecursionLevel);

					str = SprEngine.FillPlaceholder(str, @"{PASSWORD_ENC}",
						StrUtil.EncryptString(strPwCmp), ctx);
				}

				if(((ctx.Flags & SprCompileFlags.Group) != SprCompileFlags.None) &&
					(ctx.Entry.ParentGroup != null))
				{
					str = SprEngine.FillIfExists(str, @"{GROUP}", new ProtectedString(
						false, ctx.Entry.ParentGroup.Name), ctx, uRecursionLevel);

					str = SprEngine.FillIfExists(str, @"{GROUPPATH}", new ProtectedString(
						false, ctx.Entry.ParentGroup.GetFullPath()), ctx, uRecursionLevel);
				}
			}

			if((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
				str = SprEngine.FillIfExists(str, @"{APPDIR}", new ProtectedString(
					false, UrlUtil.GetFileDirectory(m_strAppExePath, false, false)),
					ctx, uRecursionLevel);

			if(ctx.Database != null)
			{
				if((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
				{
					// For backward compatibility only
					str = SprEngine.FillIfExists(str, @"{DOCDIR}", new ProtectedString(
						false, UrlUtil.GetFileDirectory(ctx.Database.IOConnectionInfo.Path,
						false, false)), ctx, uRecursionLevel);

					str = SprEngine.FillIfExists(str, @"{DB_PATH}", new ProtectedString(
						false, ctx.Database.IOConnectionInfo.Path), ctx, uRecursionLevel);
					str = SprEngine.FillIfExists(str, @"{DB_DIR}", new ProtectedString(
						false, UrlUtil.GetFileDirectory(ctx.Database.IOConnectionInfo.Path,
						false, false)), ctx, uRecursionLevel);
					str = SprEngine.FillIfExists(str, @"{DB_NAME}", new ProtectedString(
						false, UrlUtil.GetFileName(ctx.Database.IOConnectionInfo.Path)),
						ctx, uRecursionLevel);
					str = SprEngine.FillIfExists(str, @"{DB_BASENAME}", new ProtectedString(
						false, UrlUtil.StripExtension(UrlUtil.GetFileName(
						ctx.Database.IOConnectionInfo.Path))), ctx, uRecursionLevel);
					str = SprEngine.FillIfExists(str, @"{DB_EXT}", new ProtectedString(
						false, UrlUtil.GetExtension(ctx.Database.IOConnectionInfo.Path)),
						ctx, uRecursionLevel);
				}
			}

			if((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
			{
				str = SprEngine.FillIfExists(str, @"{ENV_DIRSEP}", new ProtectedString(
					false, Path.DirectorySeparatorChar.ToString()), ctx, uRecursionLevel);

				string strPF86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
				if(string.IsNullOrEmpty(strPF86))
					strPF86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
				if(strPF86 != null)
					str = SprEngine.FillIfExists(str, @"{ENV_PROGRAMFILES_X86}",
						new ProtectedString(false, strPF86), ctx, uRecursionLevel);
				else { Debug.Assert(false); }
			}

			if((ctx.Flags & SprCompileFlags.AutoType) != SprCompileFlags.None)
			{
				// Use Bksp instead of Del (in order to avoid Ctrl+Alt+Del);
				// https://sourceforge.net/p/keepass/discussion/329220/thread/4f1aa6b8/
				str = StrUtil.ReplaceCaseInsensitive(str, @"{CLEARFIELD}",
					@"{HOME}+({END}){BKSP}{DELAY 50}");
			}

			if((ctx.Flags & SprCompileFlags.DateTime) != SprCompileFlags.None)
			{
				DateTime dtNow = DateTime.Now; // Local time
				str = SprEngine.FillIfExists(str, @"{DT_YEAR}", new ProtectedString(
					false, dtNow.Year.ToString("D4")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_MONTH}", new ProtectedString(
					false, dtNow.Month.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_DAY}", new ProtectedString(
					false, dtNow.Day.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_HOUR}", new ProtectedString(
					false, dtNow.Hour.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_MINUTE}", new ProtectedString(
					false, dtNow.Minute.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_SECOND}", new ProtectedString(
					false, dtNow.Second.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_SIMPLE}", new ProtectedString(
					false, dtNow.ToString("yyyyMMddHHmmss")), ctx, uRecursionLevel);

				dtNow = dtNow.ToUniversalTime();
				str = SprEngine.FillIfExists(str, @"{DT_UTC_YEAR}", new ProtectedString(
					false, dtNow.Year.ToString("D4")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_UTC_MONTH}", new ProtectedString(
					false, dtNow.Month.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_UTC_DAY}", new ProtectedString(
					false, dtNow.Day.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_UTC_HOUR}", new ProtectedString(
					false, dtNow.Hour.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_UTC_MINUTE}", new ProtectedString(
					false, dtNow.Minute.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_UTC_SECOND}", new ProtectedString(
					false, dtNow.Second.ToString("D2")), ctx, uRecursionLevel);
				str = SprEngine.FillIfExists(str, @"{DT_UTC_SIMPLE}", new ProtectedString(
					false, dtNow.ToString("yyyyMMddHHmmss")), ctx, uRecursionLevel);
			}

			if((ctx.Flags & SprCompileFlags.References) != SprCompileFlags.None)
				str = SprEngine.FillRefPlaceholders(str, ctx, uRecursionLevel);

			if(((ctx.Flags & SprCompileFlags.EnvVars) != SprCompileFlags.None) &&
				(str.IndexOf('%') >= 0))
			{
				// 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), ctx, uRecursionLevel);
					else { Debug.Assert(false); }
				}
			}

			if((ctx.Flags & SprCompileFlags.Env) != SprCompileFlags.None)
				str = FillUriSpecial(str, ctx, @"{BASE", (ctx.Base ?? string.Empty),
					ctx.BaseIsEncoded, uRecursionLevel);

			str = EntryUtil.FillPlaceholders(str, ctx, uRecursionLevel);

			if((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
				str = ReplacePickChars(str, ctx, uRecursionLevel);

			if(bExt && (SprEngine.FilterCompile != null))
			{
				SprEventArgs args = new SprEventArgs(str, ctx.Clone());
				SprEngine.FilterCompile(null, args);
				str = args.Text;
			}

			if(ctx.EncodeAsAutoTypeSequence)
			{
				str = StrUtil.NormalizeNewLines(str, false);
				str = str.Replace("\n", @"{ENTER}");
			}

			return str;
		}
Exemple #2
0
        private static string CompileInternal(string strText, SprContext ctx,
                                              uint uRecursionLevel)
        {
            if (strText == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (ctx == null)
            {
                Debug.Assert(false); ctx = new SprContext();
            }

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

            string   str = strText;
            MainForm mf  = Program.MainForm;

            bool bExt = ((ctx.Flags & (SprCompileFlags.ExtActive |
                                       SprCompileFlags.ExtNonActive)) != SprCompileFlags.None);

            if (bExt && (SprEngine.FilterCompilePre != null))
            {
                SprEventArgs args = new SprEventArgs(str, ctx.Clone());
                SprEngine.FilterCompilePre(null, args);
                str = args.Text;
            }

            if ((ctx.Flags & SprCompileFlags.Comments) != SprCompileFlags.None)
            {
                str = RemoveComments(str);
            }

            // The following realizes {T-CONV:/Text/Raw/}, which should be
            // one of the first transformations (except comments)
            if ((ctx.Flags & SprCompileFlags.TextTransforms) != SprCompileFlags.None)
            {
                str = PerformTextTransforms(str, ctx, uRecursionLevel);
            }

            if ((ctx.Flags & SprCompileFlags.Run) != SprCompileFlags.None)
            {
                str = RunCommands(str, ctx, uRecursionLevel);
            }

            if ((ctx.Flags & SprCompileFlags.DataActive) != SprCompileFlags.None)
            {
                str = PerformClipboardCopy(str, ctx, uRecursionLevel);
            }

            if (((ctx.Flags & SprCompileFlags.DataNonActive) != SprCompileFlags.None) &&
                (str.IndexOf(@"{CLIPBOARD}", SprEngine.ScMethod) >= 0))
            {
                string strCb = null;
                try { strCb = ClipboardUtil.GetText(); }
                catch (Exception) { Debug.Assert(false); }
                str = Fill(str, @"{CLIPBOARD}", strCb ?? string.Empty, ctx, null);
            }

            if ((ctx.Flags & SprCompileFlags.AppPaths) != SprCompileFlags.None)
            {
                str = AppLocator.FillPlaceholders(str, ctx);
            }

            if (ctx.Entry != null)
            {
                if ((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
                {
                    str = ReplacePickPw(str, ctx, uRecursionLevel);
                }

                if ((ctx.Flags & SprCompileFlags.EntryStrings) != SprCompileFlags.None)
                {
                    str = FillEntryStrings(str, ctx, uRecursionLevel);
                }

                if ((ctx.Flags & SprCompileFlags.EntryStringsSpecial) != SprCompileFlags.None)
                {
                    str = FillEntryStringsSpecial(str, ctx, uRecursionLevel);
                }

                if (((ctx.Flags & SprCompileFlags.EntryProperties) != SprCompileFlags.None) &&
                    (str.IndexOf(@"{UUID}", SprEngine.ScMethod) >= 0))
                {
                    str = Fill(str, @"{UUID}", ctx.Entry.Uuid.ToHexString(), ctx, null);
                }

                if (((ctx.Flags & SprCompileFlags.PasswordEnc) != SprCompileFlags.None) &&
                    (str.IndexOf(@"{PASSWORD_ENC}", SprEngine.ScMethod) >= 0))
                {
                    string strPwCmp = SprEngine.CompileInternal(@"{PASSWORD}",
                                                                ctx.WithoutContentTransformations(), uRecursionLevel + 1);
                    str = Fill(str, @"{PASSWORD_ENC}", StrUtil.EncryptString(
                                   strPwCmp), ctx, null);
                }

                PwGroup pg = ctx.Entry.ParentGroup;
                if (((ctx.Flags & SprCompileFlags.Group) != SprCompileFlags.None) &&
                    (pg != null))
                {
                    str = FillGroupPlh(str, @"{GROUP", pg, ctx, uRecursionLevel);
                }
            }

            if ((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
            {
                if (mf != null)
                {
                    PwGroup pgSel = mf.GetSelectedGroup();
                    if (pgSel != null)
                    {
                        str = FillGroupPlh(str, @"{GROUP_SEL", pgSel, ctx, uRecursionLevel);
                    }
                }

                str = Fill(str, @"{APPDIR}", UrlUtil.GetFileDirectory(
                               WinUtil.GetExecutable(), false, false), ctx, uRecursionLevel);

                str = Fill(str, @"{ENV_DIRSEP}", Path.DirectorySeparatorChar.ToString(),
                           ctx, null);

                string strPF86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
                if (string.IsNullOrEmpty(strPF86))
                {
                    strPF86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                }
                if (strPF86 != null)
                {
                    str = Fill(str, @"{ENV_PROGRAMFILES_X86}", strPF86, ctx, uRecursionLevel);
                }
                else
                {
                    Debug.Assert(false);
                }

                if (ctx.Database != null)
                {
                    string strPath = ctx.Database.IOConnectionInfo.Path;
                    string strDir  = UrlUtil.GetFileDirectory(strPath, false, false);
                    string strName = UrlUtil.GetFileName(strPath);

                    // For backward compatibility only
                    str = Fill(str, @"{DOCDIR}", strDir, ctx, uRecursionLevel);

                    str = Fill(str, @"{DB_PATH}", strPath, ctx, uRecursionLevel);
                    str = Fill(str, @"{DB_DIR}", strDir, ctx, uRecursionLevel);
                    str = Fill(str, @"{DB_NAME}", strName, ctx, uRecursionLevel);
                    str = Fill(str, @"{DB_BASENAME}", UrlUtil.StripExtension(
                                   strName), ctx, uRecursionLevel);
                    str = Fill(str, @"{DB_EXT}", UrlUtil.GetExtension(
                                   strPath), ctx, uRecursionLevel);
                }
            }

            if ((ctx.Flags & SprCompileFlags.AutoType) != SprCompileFlags.None)
            {
                // Use Bksp instead of Del (in order to avoid Ctrl+Alt+Del);
                // https://sourceforge.net/p/keepass/discussion/329220/thread/4f1aa6b8/
                str = StrUtil.ReplaceCaseInsensitive(str, @"{CLEARFIELD}",
                                                     @"{HOME}+({END}){BKSP}{DELAY 50}");
            }

            if (((ctx.Flags & SprCompileFlags.DateTime) != SprCompileFlags.None) &&
                (str.IndexOf(@"{DT_", SprEngine.ScMethod) >= 0))
            {
                DateTime dtNow = DateTime.UtcNow;
                str = Fill(str, @"{DT_UTC_YEAR}", dtNow.Year.ToString("D4"),
                           ctx, null);
                str = Fill(str, @"{DT_UTC_MONTH}", dtNow.Month.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_UTC_DAY}", dtNow.Day.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_UTC_HOUR}", dtNow.Hour.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_UTC_MINUTE}", dtNow.Minute.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_UTC_SECOND}", dtNow.Second.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_UTC_SIMPLE}", dtNow.ToString("yyyyMMddHHmmss"),
                           ctx, null);

                dtNow = dtNow.ToLocalTime();
                str   = Fill(str, @"{DT_YEAR}", dtNow.Year.ToString("D4"),
                             ctx, null);
                str = Fill(str, @"{DT_MONTH}", dtNow.Month.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_DAY}", dtNow.Day.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_HOUR}", dtNow.Hour.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_MINUTE}", dtNow.Minute.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_SECOND}", dtNow.Second.ToString("D2"),
                           ctx, null);
                str = Fill(str, @"{DT_SIMPLE}", dtNow.ToString("yyyyMMddHHmmss"),
                           ctx, null);
            }

            if ((ctx.Flags & SprCompileFlags.References) != SprCompileFlags.None)
            {
                str = SprEngine.FillRefPlaceholders(str, ctx, uRecursionLevel);
            }

            if (((ctx.Flags & SprCompileFlags.EnvVars) != SprCompileFlags.None) &&
                (str.IndexOf('%') >= 0))
            {
                foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
                {
                    string strKey = (de.Key as string);
                    if (string.IsNullOrEmpty(strKey))
                    {
                        Debug.Assert(false); continue;
                    }

                    string strValue = (de.Value as string);
                    if (strValue == null)
                    {
                        Debug.Assert(false); strValue = string.Empty;
                    }

                    str = Fill(str, @"%" + strKey + @"%", strValue, ctx, uRecursionLevel);
                }
            }

            if ((ctx.Flags & SprCompileFlags.Env) != SprCompileFlags.None)
            {
                str = FillUriSpecial(str, ctx, @"{BASE", (ctx.Base ?? string.Empty),
                                     ctx.BaseIsEncoded, uRecursionLevel);
            }

            str = EntryUtil.FillPlaceholders(str, ctx, uRecursionLevel);

            if ((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
            {
                str = ReplacePickChars(str, ctx, uRecursionLevel);
            }

            if (bExt && (SprEngine.FilterCompile != null))
            {
                SprEventArgs args = new SprEventArgs(str, ctx.Clone());
                SprEngine.FilterCompile(null, args);
                str = args.Text;
            }

            if (ctx.EncodeAsAutoTypeSequence)
            {
                str = StrUtil.NormalizeNewLines(str, false);
                str = str.Replace("\n", @"{ENTER}");
            }

            return(str);
        }
Exemple #3
0
        private static string CompileInternal(string strText, SprContext ctx,
                                              uint uRecursionLevel)
        {
            if (strText == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (ctx == null)
            {
                Debug.Assert(false); ctx = new SprContext();
            }

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

            string str = strText;

            bool bExt = ((ctx.Flags & (SprCompileFlags.ExtActive |
                                       SprCompileFlags.ExtNonActive)) != SprCompileFlags.None);

            if (bExt && (SprEngine.FilterCompilePre != null))
            {
                SprEventArgs args = new SprEventArgs(str, ctx.Clone());
                SprEngine.FilterCompilePre(null, args);
                str = args.Text;
            }

            if ((ctx.Flags & SprCompileFlags.Comments) != SprCompileFlags.None)
            {
                str = RemoveComments(str);
            }

            if ((ctx.Flags & SprCompileFlags.TextTransforms) != SprCompileFlags.None)
            {
                str = PerformTextTransforms(str, ctx, uRecursionLevel);
            }

            if ((ctx.Flags & SprCompileFlags.AppPaths) != SprCompileFlags.None)
            {
                str = AppLocator.FillPlaceholders(str, ctx);
            }

            if (ctx.Entry != null)
            {
                if ((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
                {
                    str = ReplacePickPw(str, ctx, uRecursionLevel);
                }

                if ((ctx.Flags & SprCompileFlags.EntryStrings) != SprCompileFlags.None)
                {
                    str = FillEntryStrings(str, ctx, uRecursionLevel);
                }

                if ((ctx.Flags & SprCompileFlags.EntryStringsSpecial) != SprCompileFlags.None)
                {
                    // ctx.UrlRemoveSchemeOnce = true;
                    // str = SprEngine.FillIfExists(str, @"{URL:RMVSCM}",
                    //	ctx.Entry.Strings.GetSafe(PwDefs.UrlField), ctx, uRecursionLevel);
                    // Debug.Assert(!ctx.UrlRemoveSchemeOnce);

                    str = FillEntryStringsSpecial(str, ctx, uRecursionLevel);
                }

                if (((ctx.Flags & SprCompileFlags.PasswordEnc) != SprCompileFlags.None) &&
                    (str.IndexOf(@"{PASSWORD_ENC}", SprEngine.ScMethod) >= 0))
                {
                    string strPwCmp = SprEngine.FillIfExists(@"{PASSWORD}",
                                                             @"{PASSWORD}", ctx.Entry.Strings.GetSafe(PwDefs.PasswordField),
                                                             ctx.WithoutContentTransformations(), uRecursionLevel);

                    str = SprEngine.FillPlaceholder(str, @"{PASSWORD_ENC}",
                                                    StrUtil.EncryptString(strPwCmp), ctx);
                }

                if (((ctx.Flags & SprCompileFlags.Group) != SprCompileFlags.None) &&
                    (ctx.Entry.ParentGroup != null))
                {
                    str = SprEngine.FillIfExists(str, @"{GROUP}", new ProtectedString(
                                                     false, ctx.Entry.ParentGroup.Name), ctx, uRecursionLevel);

                    str = SprEngine.FillIfExists(str, @"{GROUPPATH}", new ProtectedString(
                                                     false, ctx.Entry.ParentGroup.GetFullPath()), ctx, uRecursionLevel);
                }
            }

            if ((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
            {
                str = SprEngine.FillIfExists(str, @"{APPDIR}", new ProtectedString(
                                                 false, UrlUtil.GetFileDirectory(m_strAppExePath, false, false)),
                                             ctx, uRecursionLevel);
            }

            if (ctx.Database != null)
            {
                if ((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
                {
                    // For backward compatibility only
                    str = SprEngine.FillIfExists(str, @"{DOCDIR}", new ProtectedString(
                                                     false, UrlUtil.GetFileDirectory(ctx.Database.IOConnectionInfo.Path,
                                                                                     false, false)), ctx, uRecursionLevel);

                    str = SprEngine.FillIfExists(str, @"{DB_PATH}", new ProtectedString(
                                                     false, ctx.Database.IOConnectionInfo.Path), ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_DIR}", new ProtectedString(
                                                     false, UrlUtil.GetFileDirectory(ctx.Database.IOConnectionInfo.Path,
                                                                                     false, false)), ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_NAME}", new ProtectedString(
                                                     false, UrlUtil.GetFileName(ctx.Database.IOConnectionInfo.Path)),
                                                 ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_BASENAME}", new ProtectedString(
                                                     false, UrlUtil.StripExtension(UrlUtil.GetFileName(
                                                                                       ctx.Database.IOConnectionInfo.Path))), ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_EXT}", new ProtectedString(
                                                     false, UrlUtil.GetExtension(ctx.Database.IOConnectionInfo.Path)),
                                                 ctx, uRecursionLevel);
                }
            }

            if ((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
            {
                str = SprEngine.FillIfExists(str, @"{ENV_DIRSEP}", new ProtectedString(
                                                 false, Path.DirectorySeparatorChar.ToString()), ctx, uRecursionLevel);

                string strPF86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
                if (string.IsNullOrEmpty(strPF86))
                {
                    strPF86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                }
                if (strPF86 != null)
                {
                    str = SprEngine.FillIfExists(str, @"{ENV_PROGRAMFILES_X86}",
                                                 new ProtectedString(false, strPF86), ctx, uRecursionLevel);
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            if ((ctx.Flags & SprCompileFlags.AutoType) != SprCompileFlags.None)
            {
                // Use Bksp instead of Del (in order to avoid Ctrl+Alt+Del);
                // https://sourceforge.net/p/keepass/discussion/329220/thread/4f1aa6b8/
                str = StrUtil.ReplaceCaseInsensitive(str, @"{CLEARFIELD}",
                                                     @"{HOME}+({END}){BKSP}{DELAY 50}");
            }

            if ((ctx.Flags & SprCompileFlags.DateTime) != SprCompileFlags.None)
            {
                DateTime dtNow = DateTime.Now;                 // Local time
                str = SprEngine.FillIfExists(str, @"{DT_YEAR}", new ProtectedString(
                                                 false, dtNow.Year.ToString("D4")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_MONTH}", new ProtectedString(
                                                 false, dtNow.Month.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_DAY}", new ProtectedString(
                                                 false, dtNow.Day.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_HOUR}", new ProtectedString(
                                                 false, dtNow.Hour.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_MINUTE}", new ProtectedString(
                                                 false, dtNow.Minute.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_SECOND}", new ProtectedString(
                                                 false, dtNow.Second.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_SIMPLE}", new ProtectedString(
                                                 false, dtNow.ToString("yyyyMMddHHmmss")), ctx, uRecursionLevel);

                dtNow = dtNow.ToUniversalTime();
                str   = SprEngine.FillIfExists(str, @"{DT_UTC_YEAR}", new ProtectedString(
                                                   false, dtNow.Year.ToString("D4")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_MONTH}", new ProtectedString(
                                                 false, dtNow.Month.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_DAY}", new ProtectedString(
                                                 false, dtNow.Day.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_HOUR}", new ProtectedString(
                                                 false, dtNow.Hour.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_MINUTE}", new ProtectedString(
                                                 false, dtNow.Minute.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_SECOND}", new ProtectedString(
                                                 false, dtNow.Second.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_SIMPLE}", new ProtectedString(
                                                 false, dtNow.ToString("yyyyMMddHHmmss")), ctx, uRecursionLevel);
            }

            if ((ctx.Flags & SprCompileFlags.References) != SprCompileFlags.None)
            {
                str = SprEngine.FillRefPlaceholders(str, ctx, uRecursionLevel);
            }

            if (((ctx.Flags & SprCompileFlags.EnvVars) != SprCompileFlags.None) &&
                (str.IndexOf('%') >= 0))
            {
                // 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), ctx, uRecursionLevel);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
            }

            if ((ctx.Flags & SprCompileFlags.Env) != SprCompileFlags.None)
            {
                str = FillUriSpecial(str, ctx, @"{BASE", (ctx.Base ?? string.Empty),
                                     ctx.BaseIsEncoded, uRecursionLevel);
            }

            str = EntryUtil.FillPlaceholders(str, ctx, uRecursionLevel);

            if ((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
            {
                str = ReplacePickChars(str, ctx, uRecursionLevel);
            }

            if (bExt && (SprEngine.FilterCompile != null))
            {
                SprEventArgs args = new SprEventArgs(str, ctx.Clone());
                SprEngine.FilterCompile(null, args);
                str = args.Text;
            }

            if (ctx.EncodeAsAutoTypeSequence)
            {
                str = StrUtil.NormalizeNewLines(str, false);
                str = str.Replace("\n", @"{ENTER}");
            }

            return(str);
        }
 /// <summary>
 /// Auto-Type Function.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void SprEngine_FilterCompile(object sender, SprEventArgs e)
 {
     if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive)
     {
         if (e.Text.IndexOf(m_host.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), StringComparison.InvariantCultureIgnoreCase) >= 0)
         {
             if (SettingsCheck(e.Context.Entry) && SeedCheck(e.Context.Entry))
             {
                 bool ValidInterval = false; bool ValidLength = false; bool ValidUrl = false;
                 if (SettingsValidate(e.Context.Entry, out ValidInterval, out ValidLength, out ValidUrl))
                 {
                     bool NoTimeCorrection = false;
                     string[] Settings = SettingsGet(e.Context.Entry);
                     var TotpGenerator = new Totp_Provider(Convert.ToInt16(Settings[0]), Convert.ToInt16(Settings[1]));
                     if (ValidUrl)
                     {
                         var CurrentTimeCorrection = TimeCorrections[Settings[2]];
                         if (CurrentTimeCorrection != null)
                         {
                             TotpGenerator.TimeCorrection = CurrentTimeCorrection.TimeCorrection;
                         }
                         else
                         {
                             TotpGenerator.TimeCorrection = TimeSpan.Zero;
                             NoTimeCorrection = true;
                         }
                     }
                     string InvalidCharacters;
                     if (SeedValidate(e.Context.Entry, out InvalidCharacters))
                     {
                         e.Context.Entry.Touch(false);
                         e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, m_host.CustomConfig.GetString(setname_string_AutoType_FieldName,setdef_string_AutoType_FieldName).ExtWithBrackets(),TotpGenerator.Generate(Base32.Decode(SeedGet(e.Context.Entry).ReadString().ExtWithoutSpaces())));
                     }
                     else
                     {
                         e.Text = string.Empty;
                         MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadSeed + InvalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore());
                     }
                     if (NoTimeCorrection) MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadUrl);
                 }
                 else
                 {
                     e.Text = string.Empty;
                     MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadSet);
                 }
             }
             else
             {
                 e.Text = string.Empty;
                 MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningNotSet);
             }
         }
     }
 }
Exemple #5
0
 private void SprEngine_FilterCompile(object sender, SprEventArgs e)
 {
     if (!e.Context.Flags.HasFlag(SprCompileFlags.ExtNonActive))
     return;
       var path = string.Empty;
       try {
     using (var key = e.Context.Entry.GetSshKey()) {
       var fingerprint = key.GetMD5Fingerprint().ToHexString();
       if (keyFileMap.ContainsKey(fingerprint))
     path = keyFileMap[fingerprint].Path;
     }
       } catch (Exception) { }
       e.Text = StrUtil.ReplaceCaseInsensitive(e.Text,
     keyFilePathSprPlaceholder, path);
       e.Text = StrUtil.ReplaceCaseInsensitive(e.Text,
     identFileOptSprPlaceholder, string.Format("-i \"{0}\"", path));
 }
        /// <summary>
        /// Auto-Type Function.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SprEngine_FilterCompile(object sender, SprEventArgs e)
        {
            if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive)
            {
                if (e.Text.IndexOf(m_host.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    if (SettingsCheck(e.Context.Entry) && SeedCheck(e.Context.Entry))
                    {
                        bool ValidInterval = false; bool ValidLength = false; bool ValidUrl = false;
                        if (SettingsValidate(e.Context.Entry, out ValidInterval, out ValidLength, out ValidUrl))
                        {
                            string[] Settings = SettingsGet(e.Context.Entry);

                            TOTPProvider TOTPGenerator = new TOTPProvider(Settings, ref this.TimeCorrections);

                            string InvalidCharacters;

                            if (SeedValidate(e.Context.Entry, out InvalidCharacters))
                            {
                                e.Context.Entry.Touch(false);
                                string totp = TOTPGenerator.GenerateByByte(Base32.Decode(SeedGet(e.Context.Entry).ReadString().ExtWithoutSpaces()));
                                e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, m_host.CustomConfig.GetString(setname_string_AutoType_FieldName,setdef_string_AutoType_FieldName).ExtWithBrackets(), totp);
                            }
                            else
                            {
                                e.Text = string.Empty;
                                MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadSeed + InvalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore());
                            }
                            if (TOTPGenerator.TimeCorrectionError) MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadUrl);
                        }
                        else
                        {
                            e.Text = string.Empty;
                            MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadSet);
                        }
                    }
                    else
                    {
                        e.Text = string.Empty;
                        MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningNotSet);
                    }
                }
            }
        }