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);
        }
Example #2
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();
            }
        }
Example #3
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();
            }
        }
Example #4
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();
            }
        }
Example #5
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();
            }
        }
Example #6
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);
        }
Example #7
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);
        }
        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);
        }
Example #9
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);
        }
Example #10
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);
        }
Example #11
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);
            }
        }
Example #13
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();
        }
Example #14
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);
        }
Example #15
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;
 }
        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;
        }
Example #18
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();
        }