Exemple #1
0
        public void MacroProcessor_Basic2()
        {
            var macros = new MacroProcessor();

            macros.Add("var1", "HELLO");
            macros.Add("var2", "WORLD");

            Assert.AreEqual("HELLO", macros["var1"]);
            Assert.AreEqual("HELLO", macros["VAR1"]);
            Assert.IsNull(macros["VAR3"]);

            Assert.AreEqual("HELLO", macros.Expand("$(var1)"));
            Assert.AreEqual("WORLD", macros.Expand("$(var2)"));
            Assert.AreEqual("HELLO WORLD", macros.Expand("$(var1) $(var2)"));
            Assert.AreEqual("prefix HELLO suffix", macros.Expand("prefix $(var1) suffix"));
            Assert.AreEqual("HELLO WORLD", macros.Expand("$(VAR1) $(VAR2)"));

            macros.Clear();
            Assert.AreEqual("$(var1)", macros.Expand("$(var1)"));

            macros["VAR3"] = "TEST";
            Assert.AreEqual("TEST", macros["var3"]);
            Assert.AreEqual("TEST", macros["VAR3"]);
            Assert.AreEqual("TEST", macros.Expand("$(var3)"));
            Assert.AreEqual("TEST", macros.Expand("$(VAR3)"));
        }
Exemple #2
0
        public void MacroProcessor_Recursive()
        {
            var macros = new MacroProcessor();

            macros.Add("var1", "$(var2)");
            macros.Add("var2", "$(var3) $(var4)");
            macros.Add("var3", "HELLO");
            macros.Add("VAR4", "WORLD");

            Assert.AreEqual("HELLO WORLD", macros.Expand("$(var1)"));
        }
Exemple #3
0
        public void MacroProcessor_InfiniteRecursion()
        {
            var macros = new MacroProcessor();

            macros.Add("var1", "$(var2)");
            macros.Add("var2", "$(var1)");

            try
            {
                macros.Expand("$(var1)");
                Assert.Fail();  // Expected a StackOverflowException
            }
            catch (StackOverflowException)
            {
            }
        }
Exemple #4
0
        /// <summary>
        /// HAdds the a file to the package, handling any special processing necessary when
        /// adding the PACKAGE.INI file.
        /// </summary>
        /// <param name="package">The application package.</param>
        /// <param name="path">Path to he file.</param>
        /// <param name="basePath">Base path.</param>
        /// <returns><c>true</c> if the file processed was the PACKAGE.INI file.</returns>
        private static bool AddFile(AppPackage package, string path, string basePath)
        {
            string file;

            if (path.ToLowerInvariant().StartsWith(basePath.ToLowerInvariant() + Helper.PathSepString))
            {
                file = path.Substring(basePath.Length + 1);
            }
            else
            {
                file = path;
            }

            if (String.Compare(file, "package.ini", true) != 0)
            {
                package.AddFile(path, basePath);
                return(false);
            }
            else
            {
                // Handle special processing of the PACKAGE.INI file.

                StreamReader   reader = new StreamReader(path, Encoding.UTF8);
                string         settings;
                MacroProcessor processor;

                try
                {
                    processor = new MacroProcessor();
                    processor.Add("appref", package.AppRef.ToString());

                    settings = reader.ReadToEnd();
                    settings = processor.Expand(settings);

                    package.AddFile("Package.ini", Helper.ToUTF8(settings));
                }
                finally
                {
                    reader.Close();
                }

                return(true);
            }
        }
Exemple #5
0
        public void MacroProcessor_Basic1()
        {
            var macros = new MacroProcessor();

            macros.Add("var1", "INSERT");

            Assert.AreEqual("", macros.Expand(""));
            Assert.AreEqual("INSERT", macros.Expand("INSERT"));
            Assert.AreEqual("INSERT", macros.Expand("$(var1)"));
            Assert.AreEqual("prefix INSERT suffix", macros.Expand("prefix $(VAR1) suffix"));
            Assert.AreEqual("$(none)", macros.Expand("$(none)"));
            Assert.AreEqual("prefix $(none) suffix", macros.Expand("prefix $(none) suffix"));
            Assert.AreEqual("$", macros.Expand("$"));
            Assert.AreEqual("$(", macros.Expand("$("));
            Assert.AreEqual("$hello", macros.Expand("$hello"));
            Assert.AreEqual("$(hello", macros.Expand("$(hello"));
            Assert.AreEqual("$(hello)", macros.Expand("$(hello)"));
            Assert.AreEqual("hello)", macros.Expand("hello)"));
            Assert.AreEqual("hello)world", macros.Expand("hello)world"));
        }
Exemple #6
0
        /// <summary>
        /// Appends a SQL command with named parameters to the batch.
        /// </summary>
        /// <param name="command">The command string with embedded parameter references.</param>
        /// <param name="args">The command parameters.</param>
        /// <remarks>
        /// <para>
        /// This method uses the <see cref="MacroProcessor" /> class to replaced named
        /// parameter values in the command string with the literal parameter value.  Use
        /// the <b>$(param-name)</b> or archaic <b>%param-name%</b> syntax in the command
        /// string to reference specific arguments in the <paramref name="args"/> array.
        /// </para>
        /// </remarks>
        public void AppendCommand(string command, params SqlParam[] args)
        {
            if (sb.Length > 0)
            {
                sb.Append(';');
            }

            if (args == null || args.Length == 0)
            {
                sb.Append(command);
                return;
            }

            var processor = new MacroProcessor();

            foreach (var arg in args)
            {
                processor.Add(arg.Name, arg.Literal);
            }

            sb.Append(processor.Expand(command));
            this.Count++;
        }
Exemple #7
0
        public void Initialize()
        {
            Helper.InitializeApp(Assembly.GetExecutingAssembly());

            this.ADSettings   = new ADTestSettings();
            this.DB           = SqlTestDatabase.Create();
            this.AuthFilePath = Path.GetTempFileName();

            //-------------------------------------------------------------
            // Initialize file authentication

            Helper.WriteToFile(this.AuthFilePath, @"

file.com;file1;file-password1
file.com;file2;file-password2
");
            this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.File, "file.com", "file1", "file-password1"));
            this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.File, "file.com", "file2", "file-password2"));

            //-------------------------------------------------------------
            // Initialize RADIUS authentication

            RadiusServerSettings radiusSettings = new RadiusServerSettings();

            radiusSettings.NetworkBinding = NetworkBinding.Parse("ANY:52111");
            radiusSettings.Devices.Add(new RadiusNasInfo(IPAddress.Loopback, this.RadiusSecret));
            radiusSettings.Devices.Add(new RadiusNasInfo(NetHelper.GetActiveAdapter(), this.RadiusSecret));

            this.RadiusServer = new RadiusServer();
            this.RadiusServer.Start(radiusSettings);
            this.RadiusServer.LoadAccountsFromString(@"

radius.com;radius1;radius-password1
radius.com;radius2;radius-password2
");
            this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.Radius, "radius.com", "radius1", "radius-password1"));
            this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.Radius, "radius.com", "radius2", "radius-password2"));

            //-------------------------------------------------------------
            // Initialize config authentication

            Config.SetConfig(@"

Accounts[0] = config.com;config1;config-password1
Accounts[1] = config.com;config2;config-password2
");
            this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.Config, "config.com", "config1", "config-password1"));
            this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.Config, "config.com", "config2", "config-password2"));

#if TEST_AD
            //-------------------------------------------------------------
            // Initialize active directory authentication

#if !TEST_AD_LDAP
            if (ADSettings.NasSecret != string.Empty)   // Disable the test if the NAS secret is blank
#endif
            this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.Ldap, ADSettings.Domain, ADSettings.Account, ADSettings.Password));
#endif

            //-------------------------------------------------------------
            // Initalize ODBC authentication

            SqlConnection   sqlCon = null;
            SqlScriptRunner scriptRunner;
            MacroProcessor  processor;
            string          initScript =
                @"
create table Accounts (

Realm           varchar(64),
Account         varchar(64),
Password        varchar(64),
MD5             varbinary(128),
SHA1            varbinary(128),
SHA256          varbinary(128),
SHA512          varbinary(128)
)
go

insert into Accounts(Realm,Account,Password,MD5,SHA1,SHA256,SHA512)
values ('odbc.com','odbc1','odbc-password1',$(md5-1),$(sha1-1),$(sha256-1),$(sha512-1))

insert into Accounts(Realm,Account,Password,MD5,SHA1,SHA256,SHA512)
values ('odbc.com','odbc2','odbc-password2',$(md5-2),$(sha1-2),$(sha256-2),$(sha512-2))

go
";
            try
            {
                processor = new MacroProcessor();
                processor.Add("md5-1", SqlHelper.Literal(MD5Hasher.Compute("odbc-password1")));
                processor.Add("sha1-1", SqlHelper.Literal(SHA1Hasher.Compute("odbc-password1")));
                processor.Add("sha256-1", SqlHelper.Literal(SHA256Hasher.Compute("odbc-password1")));
                processor.Add("sha512-1", SqlHelper.Literal(SHA512Hasher.Compute("odbc-password1")));

                processor.Add("md5-2", SqlHelper.Literal(MD5Hasher.Compute("odbc-password2")));
                processor.Add("sha1-2", SqlHelper.Literal(SHA1Hasher.Compute("odbc-password2")));
                processor.Add("sha256-2", SqlHelper.Literal(SHA256Hasher.Compute("odbc-password2")));
                processor.Add("sha512-2", SqlHelper.Literal(SHA512Hasher.Compute("odbc-password2")));

                initScript = processor.Expand(initScript);

                sqlCon       = DB.OpenConnection();
                scriptRunner = new SqlScriptRunner(initScript);
                scriptRunner.Run(sqlCon);

                this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.Odbc, "odbc.com", "odbc1", "odbc-password1"));
                this.Accounts.Add(new AuthTestAccount(AuthTestExtensionType.Odbc, "odbc.com", "odbc2", "odbc-password2"));
            }
            finally
            {
                if (sqlCon != null)
                {
                    sqlCon.Close();
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Authenticates the account credentials against the authentication extension.
        /// </summary>
        /// <param name="realm">The authentication realm.</param>
        /// <param name="account">The account ID.</param>
        /// <param name="password">The password.</param>
        /// <returns>A <see cref="AuthenticationResult" /> instance with the result of the operation.</returns>
        /// <remarks>
        /// <para>
        /// The <see cref="AuthenticationResult.Status" /> property indicates the disposition
        /// of the authentication operation.  Extensions will return <see cref="AuthenticationStatus.Authenticated" />
        /// if the operation was successful.  Authentication failures due to the
        /// sumbission of invalid credentials will be indicated by returning one of
        /// the error codes.  Extensions may return specific error codes such as
        /// <see cref="AuthenticationStatus.BadPassword" /> and <see cref="AuthenticationStatus.BadAccount" />
        /// or the generic error code <see cref="AuthenticationStatus.AccessDenied" />.
        /// </para>
        /// <para>
        /// The <see cref="AuthenticationResult.MaxCacheTime" /> returns as the maximum time the
        /// results of the authentication operation should be cached.
        /// </para>
        /// </remarks>
        /// <exception cref="AuthenticationException">Thrown for authentication related exception.</exception>
        public AuthenticationResult Authenticate(string realm, string account, string password)
        {
            OdbcConnection dbCon;
            OdbcCommand    cmd;
            OdbcDataReader reader = null;
            MacroProcessor processor;
            string         _conString;
            string         query;
            int            authCode;

            using (TimedLock.Lock(this))
            {
                if (!IsOpen)
                {
                    throw new AuthenticationException("Authentication extension is closed.");
                }

                cAuthentications++;
                _conString = conString;

                // Substitute the credentials into the query template.

                processor = new MacroProcessor();
                processor.Add("realm", SqlHelper.Literal(realm));
                processor.Add("account", SqlHelper.Literal(account));
                processor.Add("password", SqlHelper.Literal(password));
                processor.Add("md5-password", SqlHelper.Literal(MD5Hasher.Compute(password)));
                processor.Add("sha1-password", SqlHelper.Literal(SHA1Hasher.Compute(password)));
                processor.Add("sha256-password", SqlHelper.Literal(SHA256Hasher.Compute(password)));
                processor.Add("sha512-password", SqlHelper.Literal(SHA512Hasher.Compute(password)));
                query = processor.Expand(queryTemplate);
            }

            // Perform the query.

            dbCon = new OdbcConnection(_conString);
            dbCon.Open();

            try
            {
                cmd             = dbCon.CreateCommand();
                cmd.CommandText = query;
                cmd.CommandType = CommandType.Text;

                perf.Queries.Increment();
                reader = cmd.ExecuteReader();
                if (!reader.Read())
                {
                    authCode = (int)AuthenticationStatus.AccessDenied; // Empty result set
                }
                else
                {
                    object o    = reader[0];
                    Type   type = o.GetType();

                    if (type == typeof(byte))
                    {
                        authCode = (int)(byte)o;
                    }
                    else if (type == typeof(int))
                    {
                        authCode = (int)o;
                    }
                    else if (type == typeof(long))
                    {
                        authCode = (int)(long)o;
                    }
                    else
                    {
                        throw new AuthenticationException("ODBC authenticate query returned a [{0}] instead of the expected [integer].", type.Name);
                    }

                    if (authCode < 0 || authCode > 5)
                    {
                        throw new AuthenticationException("ODBC authenticate query returned the invalid return code [{0}]. Valid codes range from 0..5", authCode);
                    }
                }
            }
            catch (Exception e)
            {
                perf.Exceptions.Increment();
                throw new AuthenticationException(e);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                dbCon.Close();
            }

            return(new AuthenticationResult((AuthenticationStatus)authCode, maxCacheTime));
        }