Esempio n. 1
0
        internal static LdapConfiguration GetConfiguration(string configFile)
        {
            if (!File.Exists(configFile))
            {
                Environment.FailFast("LDAP test configuration file not found: " + configFile);
            }

            // To use test servers, set an environment variable LDAP_TEST_SERVER_INDEX
            // to the 0-based index of the <Connection> element in LDAP.Configuration.xml
            if (!int.TryParse(Environment.GetEnvironmentVariable("LDAP_TEST_SERVER_INDEX"), out int serverIndex))
            {
                return(null);
            }

            LdapConfiguration ldapConfig = null;

            try
            {
                XElement configuration = XDocument.Load(configFile).Element("Configuration");

                XElement connection = configuration.Elements("Connection").Skip(serverIndex).First();

                Debug.WriteLine($"Using test LDAP server {connection.Attribute("Name").Value}");

                string serverName           = "";
                string searchDn             = "";
                string port                 = "";
                string user                 = "";
                string password             = "";
                AuthenticationTypes at      = AuthenticationTypes.None;
                bool useTls                 = false;
                bool supportsServerSideSort = false;

                XElement child = connection.Element("ServerName");
                if (child != null)
                {
                    serverName = child.Value;
                }

                child = connection.Element("SearchDN");
                if (child != null)
                {
                    searchDn = child.Value;
                }

                child = connection.Element("Port");
                if (child != null)
                {
                    port = child.Value;
                }

                child = connection.Element("User");
                if (child != null)
                {
                    user = child.Value;
                }

                child = connection.Element("Password");
                if (child != null)
                {
                    string val = child.Value;
                    if (val.StartsWith("%") && val.EndsWith("%"))
                    {
                        val = Environment.GetEnvironmentVariable(val.Substring(1, val.Length - 2));
                    }
                    password = val;
                }

                child = connection.Element("UseTls");
                if (child != null)
                {
                    useTls = bool.Parse(child.Value);
                }

                child = connection.Element("SupportsServerSideSort");
                if (child != null)
                {
                    supportsServerSideSort = bool.Parse(child.Value);
                }

                child = connection.Element("AuthenticationTypes");
                if (child != null)
                {
                    string[] parts = child.Value.Split(',');
                    foreach (string p in parts)
                    {
                        string s = p.Trim();
                        if (s.Equals("Anonymous", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.Anonymous;
                        }
                        if (s.Equals("Delegation", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.Delegation;
                        }
                        if (s.Equals("Encryption", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.FastBind;
                        }
                        if (s.Equals("FastBind", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.FastBind;
                        }
                        if (s.Equals("ReadonlyServer", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.ReadonlyServer;
                        }
                        if (s.Equals("Sealing", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.Sealing;
                        }
                        if (s.Equals("Secure", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.Secure;
                        }
                        if (s.Equals("SecureSocketsLayer", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.SecureSocketsLayer;
                        }
                        if (s.Equals("ServerBind", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.ServerBind;
                        }
                        if (s.Equals("Signing", StringComparison.OrdinalIgnoreCase))
                        {
                            at |= AuthenticationTypes.Signing;
                        }
                    }

                    ldapConfig = new LdapConfiguration(serverName, searchDn, user, password, port, at, useTls, supportsServerSideSort);
                }
            }
            catch (Exception ex)
            {
                // This runs within a test filter; if it throws, the test just skips. Instead we want to stop
                // so that it's quite clear that the server configuration is malformed.
                Environment.FailFast(ex.ToString());
            }
            return(ldapConfig);
        }
Esempio n. 2
0
        internal static LdapConfiguration GetConfiguration(string configFile)
        {
            if (!File.Exists(configFile))
            {
                return(null);
            }

            LdapConfiguration ldapConfig = null;

            try
            {
                string serverName      = "";
                string domain          = "";
                string port            = "";
                string user            = "";
                string password        = "";
                AuthenticationTypes at = AuthenticationTypes.None;

                XElement config = XDocument.Load(configFile).Element("Configuration");
                if (config != null)
                {
                    XElement child = config.Element("ServerName");
                    if (child != null)
                    {
                        serverName = child.Value;
                    }

                    child = config.Element("Domain");
                    if (child != null)
                    {
                        domain = child.Value;
                    }

                    child = config.Element("Port");
                    if (child != null)
                    {
                        port = child.Value;
                    }

                    child = config.Element("User");
                    if (child != null)
                    {
                        user = child.Value;
                    }

                    child = config.Element("Password");
                    if (child != null)
                    {
                        password = child.Value;
                    }

                    child = config.Element("AuthenticationTypes");
                    if (child != null)
                    {
                        string[] parts = child.Value.Split(',');
                        foreach (string p in parts)
                        {
                            string s = p.Trim();
                            if (s.Equals("Anonymous", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.Anonymous;
                            }
                            if (s.Equals("Delegation", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.Delegation;
                            }
                            if (s.Equals("Encryption", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.FastBind;
                            }
                            if (s.Equals("FastBind", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.FastBind;
                            }
                            if (s.Equals("ReadonlyServer", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.ReadonlyServer;
                            }
                            if (s.Equals("Sealing", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.Sealing;
                            }
                            if (s.Equals("Secure", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.Secure;
                            }
                            if (s.Equals("SecureSocketsLayer", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.SecureSocketsLayer;
                            }
                            if (s.Equals("ServerBind", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.ServerBind;
                            }
                            if (s.Equals("Signing", StringComparison.OrdinalIgnoreCase))
                            {
                                at |= AuthenticationTypes.Signing;
                            }
                        }
                    }

                    ldapConfig = new LdapConfiguration(serverName, domain, user, password, port, at);
                }
            }
            catch
            {
                // Couldn't read the configurations, usually we'll skip the tests which depend on that
            }
            return(ldapConfig);
        }