示例#1
0
        public void RefreshSchema()
        {
            const string baseDN  = "cn=aggregate,cn=schemacontext";
            var          attribs = new string[] { ATTRIBUTETYPES, OBJECTCLASSES, DITCONTENTRULES };
            ILdapMessage ldMsg   = null;

            try {
                List <ILdapEntry> response = _conn.SearchAndGetEntries(baseDN, LdapScope.SCOPE_SUBTREE, "(objectClass=*)", attribs, 0, ref ldMsg);
                if (response.Count == 0)
                {
                    throw new Exception("Failed to get schema");
                }

                LdapEntry baseEntry = (LdapEntry)response [0];
                var       dict      = CollectData(baseEntry);
                ParseAttributes(dict [ATTRIBUTETYPES]);
                ParseObjectClasses(dict [OBJECTCLASSES]);
                ParseContentRules(dict [DITCONTENTRULES]);
            } catch (Exception) {
                throw;
            }
            finally
            {
                if (ldMsg != null)
                {
                    (ldMsg as LdapMessage).FreeMessage();
                }
            }
        }
示例#2
0
 public void PopulateChildren(string itemDN)
 {
     if (!isChildrenLoaded)
     {
         ILdapMessage ldMsg = null;
         try {
             string[] ob = ServerDTO.Connection.SearchAndGetDN(itemDN, LdapScope.SCOPE_ONE_LEVEL, "(objectClass=*)", null, 0, ref ldMsg);
             int      numChildren, i;
             if (ob != null)
             {
                 numChildren = ob.Length;
                 for (i = 0; i < numChildren; i++)
                 {
                     this.Children.Add(new DirectoryNode(ob [i], ServerDTO, this));
                 }
             }
             else
             {
                 this.Children = null;
             }
             isChildrenLoaded = true;
             //NSNotificationCenter.DefaultCenter.PostNotificationName ("ReloadOutlineView", this);
         } catch (Exception e) {
             throw e;
         }
     }
 }
示例#3
0
        public void LdapSearch_SimpleBind_Failure()
        {
            ILdapConnection ldapConnection = LdapConnection.LdapInit(hostName, portNumber);

            Assert.IsNotNull(ldapConnection);

            ldapConnection.LdapSimpleBindS(myDN, password);

            ILdapMessage msg = ldapConnection.LdapSearchExtS("", 2, filter_F, null, 1, IntPtr.Zero, 0);

            Assert.IsNotNull(msg);

            List <ILdapEntry> entries = msg.GetEntries();

            if (entries.Count != 0)
            {
                Assert.Fail();
            }

            try
            {
                ldapConnection.LdapUnbindS();
            }
            catch
            {
                Assert.Fail();
            }
        }
        public string[] SearchAndGetDN(String searchDN, LdapScope scope, string filter, string[] attributes, int attrsOnly, ref ILdapMessage ldMsg)
        {
            string[]     dn;
            ILdapMessage searchRequest = null;

            MaintainSession(delegate() {
                searchRequest = ldConn.LdapSearchExtS(searchDN, (int)scope, filter, attributes, attrsOnly, IntPtr.Zero, 0);
            });
            ldMsg = searchRequest;
            if (ldMsg == null)
            {
                throw new Exception("Failed to do LDAP Search possibly due to lost connection. Close connection and try again");
            }
            List <ILdapEntry> entries = ldMsg.GetEntries();

            if (entries.Count <= 0)
            {
                return(null);
            }
            dn = new string[entries.Count];
            int i = 0;

            foreach (LdapEntry entry in entries)
            {
                dn [i++] = entry.getDN();
            }
            (ldMsg as LdapMessage).FreeMessage();
            return(dn);
        }
示例#5
0
        public static void LdapSearch_SASLBind_Failure()
        {
            ILdapMessage msg = null;

            ILdapConnection ldapConnection = LdapConnection.LdapInit(hostName, portNumber);

            Assert.IsNotNull(ldapConnection);

            try
            {
                ldapConnection.VmDirSafeLDAPBind(hostName, upn, password);
                msg = ldapConnection.LdapSearchExtS("", 2, filter_F, null, 1, IntPtr.Zero, 0);
            }
            catch
            {
                Assert.Fail();
            }

            List <ILdapEntry> entries = msg.GetEntries();

            if (entries.Count != 0)
            {
                Assert.Fail();
            }

            try
            {
                ldapConnection.LdapUnbindS();
            }
            catch
            {
                Assert.Fail();
            }
        }
 private string GetDomainFunctionalLevel(ILdapMessage searchRequest, List <ILdapEntry> values)
 {
     if (values != null)
     {
         var value = values[0].getAttributeValues("vmwDomainFunctionalLevel");
         if (value != null && value.Count > 0)
         {
             return(value[0].StringValue);
         }
     }
     return(string.Empty);
 }
示例#7
0
        public static void LdapSearch_SASLBind_Success()
        {
            ILdapMessage msg = null;

            ILdapConnection ldapConnection = LdapConnection.LdapInit(hostName, portNumber);

            Assert.IsNotNull(ldapConnection);

            try
            {
                ldapConnection.VmDirSafeLDAPBind(hostName, upn, password);
                msg = ldapConnection.LdapSearchExtS("", 2, filter, null, 1, IntPtr.Zero, 0);
            }
            catch
            {
                Assert.Fail();
            }

            List <ILdapEntry> entries = msg.GetEntries();

            foreach (LdapEntry entry in entries)
            {
                string dn = entry.getDN();
                Assert.IsNotNull(dn);

                List <string> attributeNames = entry.getAttributeNames();
                Assert.IsNotNull(attributeNames);

                foreach (string attributeName in attributeNames)
                {
                    List <LdapValue> attributeValues = entry.getAttributeValues(attributeName);
                    Assert.IsNotNull(attributeValues);

                    foreach (LdapValue attributeValue in attributeValues)
                    {
                        if (attributeName == attribute)
                        {
                            Assert.IsTrue(attributeValue.StringValue == value);
                        }
                    }
                }
            }

            try
            {
                ldapConnection.LdapUnbindS();
            }
            catch
            {
                Assert.Fail();
            }
        }
示例#8
0
        public void LdapSearch_SimpleBind_Success()
        {
            ILdapConnection ldapConnection = LdapConnection.LdapInit(hostName, portNumber);

            Assert.IsNotNull(ldapConnection);

            ldapConnection.LdapSimpleBindS(myDN, password);

            ILdapMessage msg = ldapConnection.LdapSearchExtS("", 2, filter, null, 1, IntPtr.Zero, 0);

            Assert.IsNotNull(msg);

            List <ILdapEntry> entries = msg.GetEntries();

            Assert.IsNotNull(entries);

            foreach (LdapEntry entry in entries)
            {
                string dn = entry.getDN();
                Assert.IsNotNull(dn);

                List <string> attributeNames = entry.getAttributeNames();
                Assert.IsNotNull(attributeNames);

                foreach (string attributeName in attributeNames)
                {
                    List <LdapValue> attributeValues = entry.getAttributeValues(attributeName);
                    Assert.IsNotNull(attributeValues);

                    foreach (LdapValue attributeValue in attributeValues)
                    {
                        if (attributeName == attribute)
                        {
                            Assert.Equals(attributeValue.StringValue, value);
                        }
                    }
                }
            }
            ldapConnection.CleanSearch();
            //msg.FreeMessage();

            try
            {
                ldapConnection.LdapUnbindS();
            }
            catch
            {
                Assert.Fail();
            }
        }
示例#9
0
        public ServerEntry GetServerEntry(String dn)
        {
            int    scope  = SearchScope.BASE;
            String filter = FilterStr.VMW_DIR_SERVER;

            String[] attrs = new string[] {
                AttributeConstants.CN, null
            };

            ILdapMessage msg   = SimpleSearch(dn, scope, filter, attrs);
            ServerEntry  entry = new ServerEntry(msg.GetEntries()[0]);

            return(entry);
        }
示例#10
0
        public SubSchemaSubEntry GetSubSchemaSubEntry()
        {
            String dn     = DnConstants.SUB_SCHEMA_SUB_ENTRY;
            int    scope  = SearchScope.BASE;
            String filter = FilterStr.SUB_SCHEMA;

            String[] attrs = new string[] {
                AttributeConstants.ATTRIBUTE_TYPES, AttributeConstants.OBJECT_CLASSES, AttributeConstants.DIT_CONTENT_RULES, null
            };

            ILdapMessage      msg   = SimpleSearch(dn, scope, filter, attrs);
            SubSchemaSubEntry entry = new SubSchemaSubEntry(msg.GetEntries()[0]);

            return(entry);
        }
示例#11
0
        public void LdapParseResult(ILdapMessage msg, ref IntPtr serverControls, bool fFreeIt)
        {
            int errorCode = 0;

            var returnError = LdapClientLibrary.ldap_parse_result(
                _connection,
                _result,
                out errorCode,
                null,
                null,
                null,
                ref serverControls,
                fFreeIt ? 1 : 0);

            ErrorCheckerHelper.Validate((int)returnError);
        }
        public List <ILdapEntry> SearchAndGetEntries(String searchDN, LdapScope scope, string filter, string[] attribsToReturn, int attrsOnly, ref ILdapMessage ldMsg)
        {
            List <ILdapEntry> entries;
            ILdapMessage      searchRequest = null;

            //Cannot free ldMsg in this function  as we lose the references to entries. Free the ldMsg in the calling function.
            MaintainSession(delegate() {
                searchRequest = ldConn.LdapSearchExtS(searchDN, (int)scope, filter, attribsToReturn, attrsOnly, IntPtr.Zero, 0);
            });
            ldMsg = searchRequest;
            if (ldMsg == null)
            {
                throw new Exception("Failed to do LDAP Search possibly due to lost connection. Close connection and try again");
            }
            entries = ldMsg.GetEntries();
            return(entries);
        }
示例#13
0
        public static string[]  SearchItemCN(string itemName, string searchObjectClass, string searchName, string[] attribs, VMDirServerDTO serverDTO)
        {
            ILdapMessage ldMsg = null;

            try {
                string            filter    = "(&(objectClass=" + searchObjectClass + ")(cn=" + searchName + "))";
                List <string>     dn        = new List <String> ();
                List <ILdapEntry> dnEntries = serverDTO.Connection.SearchAndGetEntries(itemName, LdapScope.SCOPE_SUBTREE, filter, attribs, 0, ref ldMsg);
                foreach (LdapEntry entry in dnEntries)
                {
                    dn.Add(entry.getDN());
                }
                return(dn.ToArray());
            } catch (Exception e) {
                throw e;
            }
        }
示例#14
0
        public DseRootEntry GetDseRootEntry()
        {
            String dn     = null;
            int    scope  = SearchScope.BASE;
            String filter = FilterStr.ASTERISK;

            String[] attrs = new string[] {
                AttributeConstants.NAMING_CONTEXT,
                AttributeConstants.SERVER_NAME,
                null
            };

            ILdapMessage msg   = SimpleSearch(dn, scope, filter, attrs);
            DseRootEntry entry = new DseRootEntry(msg.GetEntries()[0]);

            return(entry);
        }
示例#15
0
        public List <ILdapEntry> SearchAndGetEntries(String searchDN, LdapScope scope, string filter, string[] attribsToReturn, int attrsOnly, ref ILdapMessage ldMsg)
        {
            List <ILdapEntry> entries;
            ILdapMessage      searchRequest = null;

            //Cannot free ldMsg in this function  as we lose the references to entries. Free the ldMsg in the calling function.
            MaintainSession(delegate()
            {
                searchRequest = ldConn.LdapSearchExtS(searchDN, (int)scope, filter, attribsToReturn, attrsOnly, IntPtr.Zero, 0);
            });
            ldMsg = searchRequest;
            if (ldMsg == null)
            {
                throw new Exception(VMDirConstants.ERR_SERVER_CONNECTION_LOST);
            }
            entries = ldMsg.GetEntries();
            return(entries);
        }
示例#16
0
        public void PagedSearch(QueryDTO qdto, int pageSize, IntPtr cookie, bool morePages, Action <ILdapMessage, IntPtr, bool, List <ILdapEntry> > fn)
        {
            ILdapMessage ldMsg = null;

            IntPtr[] serverControls   = { IntPtr.Zero };
            IntPtr   returnedControls = IntPtr.Zero;

            MaintainSession(delegate()
            {
                serverControls[0] = ldConn.LdapCreatePageControl(pageSize, cookie, true);

                ldMsg = ldConn.LdapSearchExtExS(qdto.SearchBase, (int)qdto.SearchScope, qdto.GetFilterString(),
                                                qdto.AttrToReturn, qdto.AttrOnly, qdto.TimeOut, qdto.SizeLimit, serverControls);

                ldConn.LdapParseResult(ldMsg, ref returnedControls, false);
                if (cookie != IntPtr.Zero)
                {
                    LdapClientLibrary.ber_bvfree(cookie);
                    cookie = IntPtr.Zero;
                }
                ldConn.LdapParsePageControl(returnedControls, ref cookie);

                morePages = ldConn.HasMorePages(cookie);

                if (returnedControls != IntPtr.Zero)
                {
                    LdapClientLibrary.ldap_controls_free(returnedControls);
                    returnedControls = IntPtr.Zero;
                }
                LdapClientLibrary.ldap_control_free(serverControls[0]);
                serverControls[0] = IntPtr.Zero;
            });

            List <ILdapEntry> entries = ldMsg.GetEntries();

            if (fn != null)
            {
                fn(ldMsg, cookie, morePages, entries);
            }

            (ldMsg as LdapMessage).FreeMessage();
        }
        public void Search(String searchDN, LdapScope scope, string filter, string[] attribsToReturn, int attrsOnly, Action <ILdapMessage, List <ILdapEntry> > fn)
        {
            List <ILdapEntry> entries;
            ILdapMessage      searchRequest = null;

            MaintainSession(delegate()
            {
                searchRequest = ldConn.LdapSearchExtS(searchDN, (int)scope, filter, attribsToReturn, attrsOnly, IntPtr.Zero, 0);
            });
            if (searchRequest == null)
            {
                throw new Exception("Failed to do LDAP Search possibly due to lost connection. Close connection and try again");
            }

            entries = searchRequest.GetEntries();
            if (fn != null)
            {
                fn(searchRequest, entries);
            }
        }
示例#18
0
        //call checked exec on the caller of this method
        public static void  GetItemProperties(string itemName, VMDirServerDTO serverDTO, Dictionary <string, VMDirBagItem> _properties)
        {
            ILdapMessage ldMsg = null;

            try {
                List <ILdapEntry> dnEntries = serverDTO.Connection.SearchAndGetEntries(itemName, LdapScope.SCOPE_BASE, "(objectClass=*)", null, 0, ref ldMsg);
                foreach (LdapEntry entry in dnEntries)
                {
                    string[] names = entry.getAttributeNames().ToArray();
                    foreach (string name in names)
                    {
                        LdapValue[] vals       = entry.getAttributeValues(name).ToArray();
                        var         val        = vals;
                        var         key        = name;
                        var         attribType = serverDTO.Connection.SchemaManager.GetAttributeType(key);
                        bool        readOnly   = true;
                        string      desc       = "";
                        if (attribType != null)
                        {
                            readOnly = attribType.ReadOnly;
                            desc     = attribType.Description;
                            if (attribType != null)
                            {
                                _properties [key] = new VMDirBagItem {
                                    Value       = val,
                                    IsReadOnly  = readOnly,
                                    Description = desc,
                                    IsRequired  = true
                                };
                            }
                        }
                    }
                }
                if (ldMsg != null)
                {
                    (ldMsg as LdapMessage).FreeMessage();
                }
            } catch (Exception e) {
                throw e;
            }
        }
示例#19
0
        private void PopulateChildren(string itemDN)
        {
            this.Children.Clear();
            ILdapMessage ldMsg = null;

            try
            {
                string[] entries = ServerDTO.Connection.SearchAndGetDN(itemDN, LdapScope.SCOPE_ONE_LEVEL, "(objectClass=*)", new string[] { "entryDN" }, 0, ref ldMsg);
                if (entries != null)
                {
                    for (int i = 0; i < entries.Length; i++)
                    {
                        this.Children.Add(new DirectoryNode(entries[i], ServerDTO));
                    }
                }
            }
            catch (Exception exp)
            {
                MMCDlgHelper.ShowException(exp);
            }
        }
示例#20
0
        public void Search(QueryDTO dto, Action <ILdapMessage, List <ILdapEntry> > fn)
        {
            List <ILdapEntry> entries;
            ILdapMessage      ldMsg = null;

            MaintainSession(delegate()
            {
                ldMsg = ldConn.LdapSearchExtS(dto.SearchBase, (int)dto.SearchScope, dto.GetFilterString(), dto.AttrToReturn, dto.AttrOnly, IntPtr.Zero, 0);
            });
            if (ldMsg == null)
            {
                throw new Exception(VMDirConstants.ERR_NULL_CONN);
            }

            entries = ldMsg.GetEntries();
            if (fn != null)
            {
                fn(ldMsg, entries);
            }
            (ldMsg as LdapMessage).FreeMessage();
        }
示例#21
0
        private IList <SchemaEntry> GetSchemaEntries(String filter)
        {
            IList <SchemaEntry> entries = new List <SchemaEntry>();

            String dn    = DnConstants.SCHEMA_CONTEXT;
            int    scope = SearchScope.ONELEVEL;

            String[] attrs = new string[] {
                AttributeConstants.CN,
                AttributeConstants.ATTRIBUTE_METADATA,
                null
            };

            ILdapMessage msg = SimpleSearch(dn, scope, filter, attrs);

            foreach (ILdapEntry e in msg.GetEntries())
            {
                entries.Add(new SchemaEntry(e));
            }
            return(entries);
        }
示例#22
0
        public IList <ServerEntry> GetServerEntries()
        {
            IList <ServerEntry> entries = new List <ServerEntry>();

            String dn = string.Format(
                "{0},{1},{2}",
                CnConstants.SITES, CnConstants.CONFIGURATION,
                GetDseRootEntry().domain);

            int    scope  = SearchScope.SUBTREE;
            String filter = FilterStr.VMW_DIR_SERVER;

            String[] attrs = new string[] {
                AttributeConstants.DN, null
            };

            ILdapMessage msg = SimpleSearch(dn, scope, filter, attrs);

            foreach (ILdapEntry e in msg.GetEntries())
            {
                entries.Add(GetServerEntry(e.getDN()));
            }
            return(entries);
        }
 public string[] SearchAndGetDN (String searchDN, LdapScope scope, string filter, string[] attributes, int attrsOnly, ref ILdapMessage ldMsg)
 {
     string[] dn;
     ILdapMessage searchRequest = null;
     MaintainSession (delegate() {
         searchRequest = ldConn.LdapSearchExtS (searchDN, (int)scope, filter, attributes, attrsOnly, IntPtr.Zero, 0);
     });
     ldMsg = searchRequest;
     if (ldMsg == null)
         throw new Exception ("Failed to do LDAP Search possibly due to lost connection. Close connection and try again");
     List<ILdapEntry> entries = ldMsg.GetEntries ();
     if (entries.Count <= 0)
         return null;
     dn = new string[entries.Count];
     int i = 0;
     foreach (LdapEntry entry in entries) {
         dn [i++] = entry.getDN ();
     }
     (ldMsg as LdapMessage).FreeMessage ();
     return dn;
 }
示例#24
0
        public static void LdapPagedSearch()
        {
            ILdapConnection ldapConnection = LdapConnection.LdapInit(hostName, portNumber);

            Assert.IsNotNull(ldapConnection);

            ldapConnection.LdapSimpleBindS(myDN, password);

            int    totalCount       = 0;
            int    pageNumber       = 1;
            bool   morePages        = false;
            int    pageSize         = 2;
            IntPtr cookie           = IntPtr.Zero;
            IntPtr returnedControls = IntPtr.Zero;

            IntPtr[]     serverControls = { IntPtr.Zero };
            ILdapMessage msg            = null;
            string       searchBase     = "cn=users,dc=vsphere,dc=local";
            string       filter         = "(objectClass=user)";

            System.Console.WriteLine("The entries returned were:");

            do
            {
                serverControls[0] = ldapConnection.LdapCreatePageControl(pageSize, cookie, true);

                msg = ldapConnection.LdapSearchExtExS(searchBase, (int)LdapScope.SCOPE_SUBTREE, filter, null, 0, IntPtr.Zero, 0, serverControls);

                ldapConnection.LdapParseResult(msg, ref returnedControls, false);
                if (cookie != IntPtr.Zero)
                {
                    LdapClientLibrary.ber_bvfree(cookie);
                    cookie = IntPtr.Zero;
                }

                ldapConnection.LdapParsePageControl(returnedControls, ref cookie);

                morePages = ldapConnection.HasMorePages(cookie);

                if (returnedControls != IntPtr.Zero)
                {
                    LdapClientLibrary.ldap_controls_free(returnedControls);
                    returnedControls = IntPtr.Zero;
                }
                LdapClientLibrary.ldap_control_free(serverControls[0]);
                serverControls[0] = IntPtr.Zero;

                if (morePages)
                {
                    System.Console.WriteLine("===== Page : {0} =====", pageNumber);
                }

                totalCount += msg.GetEntriesCount();

                foreach (var entry in msg.GetEntries())
                {
                    System.Console.WriteLine("dn: {0}", entry.getDN());
                }

                ldapConnection.CleanSearch();
                pageNumber++;
            } while (morePages);

            System.Console.WriteLine("{0} entries found during the search", totalCount);
            LdapClientLibrary.ber_bvfree(cookie);
            cookie = IntPtr.Zero;

            ldapConnection.LdapUnbindS();
        }
示例#25
0
        public void RefreshSchema()

        {
            try
            {
                const string baseDN = "cn=schemacontext";
                ILdapMessage ldMsg  = null;
                try
                {
                    List <ILdapEntry> attributesResponse = _conn.SearchAndGetEntries(baseDN, LdapScope.SCOPE_SUBTREE, "(objectclass=attributeschema)", null, 0, ref ldMsg);
                    List <ILdapEntry> schemaResponse     = _conn.SearchAndGetEntries(baseDN, LdapScope.SCOPE_SUBTREE, "(objectclass=classschema)", null, 0, ref ldMsg);

                    if (attributesResponse.Count == 0 || schemaResponse.Count == 0)
                    {
                        throw new Exception("Failed to get schema");
                    }

                    var schemaDict = new Dictionary <string, Dictionary <string, object> >();
                    var attrDict   = new Dictionary <string, Dictionary <string, object> >();

                    CollectData(schemaResponse, OBJECTCLASSES, schemaDict);
                    CollectData(attributesResponse, ATTRIBUTETYPES, attrDict);
                    ParseAttributes(attrDict);
                    ParseObjectClasses(schemaDict);
                }
                finally
                {
                    if (ldMsg != null)
                    {
                        (ldMsg as LdapMessage).FreeMessage();
                    }
                }
            }
            catch (Exception e)
            {
                if (string.Equals(e.Message, "Failed to get schema"))
                {
                    const string baseDN  = "cn=aggregate,cn=schemacontext";
                    var          attribs = new string[] { ATTRIBUTETYPES, OBJECTCLASSES };
                    ILdapMessage ldMsg   = null;
                    try
                    {
                        List <ILdapEntry> response = _conn.SearchAndGetEntries(baseDN, LdapScope.SCOPE_SUBTREE, "(objectClass=*)", attribs, 0, ref ldMsg);
                        if (response.Count == 0)
                        {
                            throw new Exception("Failed to get schema, possibly server version is incompatible.");
                        }

                        LdapEntry baseEntry = (LdapEntry)response[0];
                        var       dict      = CollectData(baseEntry);
                        ParseAttributes(dict[ATTRIBUTETYPES]);
                        ParseObjectClasses(dict[OBJECTCLASSES]);
                    }
                    finally
                    {
                        if (ldMsg != null)
                        {
                            (ldMsg as LdapMessage).FreeMessage();
                        }
                    }
                }
                else
                {
                    throw e;
                }
            }
        }
        public List<ILdapEntry> SearchAndGetEntries (String searchDN, LdapScope scope, string filter, string[] attribsToReturn, int attrsOnly, ref ILdapMessage ldMsg)
        {
            List<ILdapEntry> entries;
            ILdapMessage searchRequest = null;

            //Cannot free ldMsg in this function  as we lose the references to entries. Free the ldMsg in the calling function.
            MaintainSession (delegate() {
                searchRequest = ldConn.LdapSearchExtS (searchDN, (int)scope, filter, attribsToReturn, attrsOnly, IntPtr.Zero, 0);
            });
            ldMsg = searchRequest;
            if (ldMsg == null)
                throw new Exception ("Failed to do LDAP Search possibly due to lost connection. Close connection and try again");
            entries = ldMsg.GetEntries ();
            return entries;
        }