예제 #1
0
 public void EnvironmentVars_System()
 {
     EnvironmentVars.Load("");
     Assert.AreEqual(Environment.GetEnvironmentVariable("path"), EnvironmentVars.Get("path"));
     Assert.AreEqual(Environment.GetEnvironmentVariable("systemdrive"), EnvironmentVars.Get("systemdrive"));
     Assert.AreEqual(Environment.GetEnvironmentVariable("windir"), EnvironmentVars.Get("windir"));
     Assert.AreEqual(Environment.GetEnvironmentVariable("comspec"), EnvironmentVars.Get("comspec"));
 }
예제 #2
0
        private static string conString;   // The default test database connection string

        /// <summary>
        /// Static constructor.
        /// </summary>
        static SqlTestDatabase()
        {
            conString = EnvironmentVars.Get("LT_TEST_DB");
            if (conString == null)
            {
                conString = "server=.\\SQLEXPRESS;Integrated Security=SSPI";
            }
        }
예제 #3
0
        /// <summary>
        /// Starts the RADIUS server, intializing the user and client
        /// databases.
        /// </summary>
        /// <param name="users">The user database file contents.</param>
        /// <param name="clients">The client database file contents.</param>
        /// <remarks>
        /// See the <b>users.example</b> and <b>clients.example</b> files
        /// in the <b>raddb</b> folder for a description of how these
        /// files must be formatted.
        /// </remarks>
        /// <exception cref="NotAvailableException">Thrown if the RADIUS server application is not installed.</exception>
        public void Start(string users, string clients)
        {
            StreamWriter writer = null;
            string       testBinPath;
            string       exePath;

            testBinPath = EnvironmentVars.Get("LT_TESTBIN");
            if (testBinPath == null)
            {
                throw new ArgumentException("[LT_TESTBIN] environment variable does not exist.");
            }

            testBinPath = Helper.AddTrailingSlash(testBinPath) + @"RadiusServer\";
            exePath     = testBinPath + "Radl.exe";
            usersPath   = testBinPath + @"raddb\users";
            clientsPath = testBinPath + @"raddb\clients";

            if (!File.Exists(exePath))
            {
                throw new NotAvailableException("RADIUS Server not found at: " + exePath);
            }

            orgDir = Environment.CurrentDirectory;
            Environment.CurrentDirectory = testBinPath.Substring(0, testBinPath.Length - 1);

            try
            {
                writer = new StreamWriter(usersPath, false, Helper.AnsiEncoding);
                writer.Write(users);
                writer.Close();
                writer = null;

                writer = new StreamWriter(clientsPath, false, Helper.AnsiEncoding);
                writer.Write(clients);
                writer.Close();
                writer = null;

                serverProcess = Process.Start(exePath, string.Empty);
                serverProcess.EnableRaisingEvents = true;

                Thread.Sleep(5000);    // Give the server a chance to initialize
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
예제 #4
0
        public void RadiusClient_Interop_AD_IAS()
        {
            if (EnvironmentVars.Get("LT_TESTBIN") == null)
            {
                Assert.Inconclusive("[LT_TESTBIN] environment variable does not exist.");
            }

            if (EnvironmentVars.Get("LT_TEST_AD") == null)
            {
                Assert.Inconclusive("[LT_TEST_AD] environment variable does not exist.");
            }

            var ad = new ADTestSettings();

            if (ad.NasSecret == string.Empty)
            {
                Assert.Inconclusive("AD/IAS Testing is disabled");
                return;
            }

            // Verify that RADIUS client works against AD/IAS.  This requires that
            // the LT_TEST_AD environment variable be set properly as described
            // in the LillTek DevInstall.doc document.  The IAS server must also
            // be manually configured with the NAS shared secret for this client.

            RadiusClient         client         = new RadiusClient();
            NetworkBinding       serverEP       = new NetworkBinding(EnhancedDns.GetHostByName(ad.Servers[0]).AddressList.IPv4Only()[0], NetworkPort.RADIUS);
            RadiusClientSettings clientSettings = new RadiusClientSettings(serverEP, ad.NasSecret);

            clientSettings.RealmFormat = RealmFormat.Email;
            clientSettings.PortCount   = 1;

            try
            {
                client.Open(clientSettings);

                Assert.IsTrue(client.Authenticate(ad.Domain, ad.Account, ad.Password));

                Assert.IsFalse(client.Authenticate(ad.Domain + "x", ad.Account, ad.Password));
                Assert.IsFalse(client.Authenticate(ad.Domain, ad.Account + "x", ad.Password));
                Assert.IsFalse(client.Authenticate(ad.Domain, ad.Account, ad.Password + "x"));
            }
            finally
            {
                client.Close();
            }
        }
예제 #5
0
        public void EnvironmentVars_BuiltIn()
        {
            EnvironmentVars.Load("");

            Assert.AreEqual(Environment.GetEnvironmentVariable("temp"), EnvironmentVars.Get("temp"));
            Assert.AreEqual(Environment.GetEnvironmentVariable("tmp"), EnvironmentVars.Get("tmp"));
            Assert.AreEqual(Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVars.Get("SystemRoot"));
            Assert.AreEqual((Environment.GetEnvironmentVariable("SystemRoot") + @"\system32").ToLowerInvariant(), EnvironmentVars.Get("SystemDirectory").ToLowerInvariant());
            Assert.AreEqual(Helper.EntryAssemblyFolder, EnvironmentVars.Get("AppPath"));
            Assert.IsNotNull(EnvironmentVars.Get("OS"));
            Assert.IsNotNull(EnvironmentVars.Get("WINFULL"));
            Assert.IsNull(EnvironmentVars.Get("WINCE"));
            Assert.AreEqual(Helper.GetVersionString(Assembly.GetExecutingAssembly()), EnvironmentVars.Get("appversion"));
            Assert.AreEqual(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), EnvironmentVars.ProgramDataPath);
            Assert.IsTrue(!EnvironmentVars.ProgramDataPath.EndsWith("/"));
            Assert.IsTrue(!EnvironmentVars.ProgramDataPath.EndsWith("\\"));
            Assert.AreNotEqual(EnvironmentVars.Get("guid"), EnvironmentVars.Get("Guid"));
            Assert.AreEqual(Helper.MachineName, EnvironmentVars.Get("MachineName"));
            Assert.AreEqual(Dns.GetHostName(), EnvironmentVars.Get("HostName"));
            Assert.AreEqual(EnvironmentVars.ServerID, EnvironmentVars.Get("ServerID"));

            Assert.AreEqual(Environment.GetEnvironmentVariable("temp"), EnvironmentVars.Expand("$(temp)"));
            Assert.AreEqual(Environment.GetEnvironmentVariable("tmp"), EnvironmentVars.Expand("$(tmp)"));
            Assert.AreEqual(Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVars.Expand("$(SystemRoot)"));
            Assert.AreEqual((Environment.GetEnvironmentVariable("SystemRoot") + @"\system32").ToLowerInvariant(), EnvironmentVars.Expand("$(SystemDirectory)").ToLowerInvariant());
            Assert.AreEqual(Helper.EntryAssemblyFolder, EnvironmentVars.Expand("$(AppPath)"));
            Assert.IsNotNull(EnvironmentVars.Expand("$(OS)"));
            Assert.IsNotNull(EnvironmentVars.Expand("$(WINFULL)"));
            Assert.AreEqual("$(WINCE)", EnvironmentVars.Expand("$(WINCE)"));
            Assert.AreNotEqual(EnvironmentVars.Expand("$(guid)"), EnvironmentVars.Expand("$(Guid)"));
            Assert.AreEqual(Helper.MachineName, EnvironmentVars.Expand("$(MachineName)"));
            Assert.AreEqual(Environment.ProcessorCount.ToString(), EnvironmentVars.Expand("$(ProcessorCount)"));

            Assert.AreEqual(Const.DCCloudEP.ToString(), EnvironmentVars.Expand("$(LillTek.DC.CloudEP)"));
            Assert.AreEqual(Const.DCCloudGroup.ToString(), EnvironmentVars.Expand("$(LillTek.DC.CloudGroup)"));
            Assert.AreEqual(Const.DCCloudPort.ToString(), EnvironmentVars.Expand("$(LillTek.DC.CloudPort)"));
            Assert.AreEqual(Const.DCRootPort.ToString(), EnvironmentVars.Expand("$(LillTek.DC.RootPort)"));
            Assert.AreEqual(Const.DCDefHubName, EnvironmentVars.Expand("$(LillTek.DC.DefHubName)"));
#if DEBUG
            Assert.AreEqual("true", EnvironmentVars.Expand("$(IsDebug)"));
            Assert.AreEqual("false", EnvironmentVars.Expand("$(IsRelease)"));
#else
            Assert.AreEqual("false", EnvironmentVars.Expand("$(IsDebug)"));
            Assert.AreEqual("true", EnvironmentVars.Expand("$(IsRelease)"));
#endif
        }
예제 #6
0
        public void RadiusClient_Interop()
        {
            if (EnvironmentVars.Get("LT_TESTBIN") == null)
            {
                Assert.Inconclusive("[LT_TESTBIN] environment variable does not exist.");
            }

            // Verify that my RADIUS client code can work against a server from
            // another vendor.

            RadiusTestServer               server = new RadiusTestServer();
            Dictionary <string, string>    users;
            Dictionary <IPAddress, string> devices;
            RadiusClient         client         = new RadiusClient();
            RadiusClientSettings clientSettings = new RadiusClientSettings(Local_AAA, "secret");

            clientSettings.RealmFormat = RealmFormat.Email;
            clientSettings.PortCount   = 1;

            users = new Dictionary <string, string>();
            users.Add("jeff", "password1");
            users.Add("joe", "password2");

            devices = new Dictionary <IPAddress, string>();
            devices.Add(IPAddress.Loopback, "secret");
            devices.Add(NetHelper.GetActiveAdapter(), "secret");

            try
            {
                server.Start(users, devices);
                client.Open(clientSettings);

                Assert.IsTrue(client.Authenticate("", "jeff", "password1"));
                Assert.IsTrue(client.Authenticate("", "joe", "password2"));

                Assert.IsFalse(client.Authenticate("", "jeff", "passwordX"));
                Assert.IsFalse(client.Authenticate("", "billy", "x"));
            }
            finally
            {
                client.Close();
                server.Stop();
            }
        }
예제 #7
0
        public void EnvironmentVars_Basic()
        {
            string cfg1 =
                @"
var1=10
// This is a comment  
   var2  =  20   

VAR3=30

path=$(ProgramDataPath)\Foo
";

            EnvironmentVars.Load(cfg1);
            Assert.AreEqual("10", EnvironmentVars.Get("var1"));
            Assert.AreEqual("20", EnvironmentVars.Get("VAR2"));
            Assert.AreEqual("30", EnvironmentVars.Get("var3"));
            Assert.IsNull(EnvironmentVars.Get("var4"));
            Assert.AreEqual(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Foo"), EnvironmentVars.Expand(EnvironmentVars.Get("path")));
        }
예제 #8
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="appName">The application name.</param>
        /// <param name="database">The database name.</param>
        /// <remarks>
        /// <note>
        /// The application name must be no longer than 32 characters
        /// and can include only letters or digit characters.
        /// The application name is used to generate a unique database
        /// user account and password to be used by the application to gain
        /// gain access to the database.
        /// </note>
        /// </remarks>
        public DBInstallParams(string appName, string database)
        {
            SqlConnectionInfo conInfo;
            string            conString;
            string            path;
            int pos;

            if (string.IsNullOrWhiteSpace(appName))
            {
                throw new ArgumentException("Invalid application name.");
            }
            else if (appName.Length > 32)
            {
                throw new ArgumentException("Application name exceeds 32 characters.");
            }
            else
            {
                foreach (char ch in appName)
                {
                    if (Char.IsLetterOrDigit(ch))
                    {
                        continue;
                    }
                    else
                    {
                        throw new ArgumentException("Application name may include only letters or digits characters.");
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(database))
            {
                throw new ArgumentException("Invalid database path.");
            }

            conString = EnvironmentVars.Get("LT_TEST_DB");
            if (conString != null)
            {
                conInfo = new SqlConnectionInfo(conString);
            }
            else
            {
                conInfo = new SqlConnectionInfo("server=.\\SQLEXPRESS;Integrated Security=SSPI");
            }

            this.appName       = appName;
            this.server        = conInfo.Server;
            this.database      = database;
            this.adminSecurity = string.IsNullOrWhiteSpace(conInfo.Security) ? "" : "Integrated Security=" + conInfo.Security;
            this.appSecurity   = string.Format("uid={0}User;pwd={1}", appName, Crypto.GeneratePassword(8, true));
            this.dbPath        = null;
            this.logPath       = null;

            path = Environment.SystemDirectory;
            pos  = path.IndexOf(':');
            if (pos == -1)
            {
                throw new InvalidOperationException("Unexpected system directory path.");
            }

            this.defDBFolder = path.Substring(0, pos + 1) + @"\LillTek\Data";
        }
예제 #9
0
        /// <summary>
        /// Sends an access request packet to the specified IP endpoint with the
        /// </summary>
        /// <param name="targetEP">The RADIUS server endpoint.</param>
        /// <param name="secret">The NAS shared secret.</param>
        /// <param name="userName">The user account.</param>
        /// <param name="password">The password.</param>
        /// <returns><c>true</c> if authentition succeeded, <c>false</c> if it was explicitly rejected.</returns>
        /// <exception cref="TimeoutException">Thrown if the transaction timed out.</exception>
        /// <exception cref="NotAvailableException">Thrown if RADIUS command line client is not installed.</exception>
        /// <remarks>
        /// <note>
        /// The command line client tool is primitive and supports a maximum of 8 character
        /// user names.
        /// </note>
        /// </remarks>
        public static bool Authenticate(IPEndPoint targetEP, string secret, string userName, string password)
        {
            StreamWriter writer     = null;
            StringReader reader     = null;
            string       scriptPath = null;
            string       testBinPath;
            string       exePath;
            string       args;
            int          cAuths;
            int          cDenied;

            if (userName.Length > 8)
            {
                throw new ArgumentException("[userName] exceeds 8 characters.", "userName");
            }

            testBinPath = EnvironmentVars.Get("LT_TESTBIN");
            if (testBinPath == null)
            {
                throw new ArgumentException("[LT_TESTBIN] environment variable does not exist.");
            }

            if (secret.IndexOfAny(new char[] { ' ', '\t', '\r', '\n' }) != -1)
            {
                throw new NotImplementedException("NAS secret cannot have whitespace.");
            }

            testBinPath = Helper.AddTrailingSlash(testBinPath) + @"RadiusClient\";
            exePath     = testBinPath + "Radclient.exe";
            scriptPath  = Path.GetTempFileName();

            if (!File.Exists(exePath))
            {
                throw new NotAvailableException("RADIUS Client not found at: " + exePath);
            }

            try
            {
                // Write the script file specifying the packet attributes

                writer = new StreamWriter(scriptPath, false, Helper.AnsiEncoding);
                writer.WriteLine("User-Name={0}", userName);
                writer.WriteLine("Password={0}", password);
                writer.WriteLine("NAS-IP-Address={0}", NetHelper.GetActiveAdapter());
                writer.Close();
                writer = null;

                // Invoke the command line client

                args = string.Format("-c 1 -i {0} -n 0 -p 1 -r 2 -t 7 -f\"{1}\" -sx {2} auth {3}",
                                     nextID++, scriptPath, targetEP, secret);

                Environment.CurrentDirectory = testBinPath.Substring(0, testBinPath.Length - 1);
                var result = Helper.ExecuteCaptureStreams(exePath, args);

                if (result.ExitCode != 0 || result.StandardOutput.Length == 0)
                {
                    throw new TimeoutException("RADIUS client timeout or other error.");
                }

                // Parse the output.

                reader = new StringReader(result.StandardOutput);

                cAuths  = -1;
                cDenied = -1;
                for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                {
                    line = line.Trim();
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    if (line.StartsWith("Total approved auths:"))
                    {
                        cAuths = int.Parse(line.Substring(23));
                    }

                    else if (line.StartsWith("Total denied auths:"))
                    {
                        cDenied = int.Parse(line.Substring(21));
                    }
                }

                reader.Close();
                reader = null;

                if (cAuths == -1 || cDenied == -1)
                {
                    return(false);
                }
                else if (cAuths > 0)
                {
                    return(true);
                }
                else if (cDenied > 0)
                {
                    return(false);
                }
                else
                {
                    throw new TimeoutException("RADIUS client timeout.");
                }
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }

                Helper.DeleteFile(scriptPath);
            }
        }