예제 #1
0
        public DirectoryEntry(string sLDAPPath)
        {
            this.sLDAPPath     = sLDAPPath;
            propertyCollection = null;
            nativeObject       = null;
            sName           = null;
            children        = null;
            objectSecurity  = null;
            guid            = Guid.Empty;
            parent          = null;
            objectClassType = null;

            SDSUtils.CrackPath(sLDAPPath, out sProtocol, out sServer, out sCNs, out sDCs);

            /*if (sProtocol != null) Console.WriteLine("sProtocol is " + sProtocol);
             * if (sServer != null) Console.WriteLine("sServer is " + sServer);
             * if (sCNs != null) Console.WriteLine("sCNs is " + sCNs);
             * if (sDCs != null) Console.WriteLine("sDCs is " + sDCs); */

            string[] rootDNcom;

            if (sServer != null)
            {
                rootDNcom = sServer.Split('.');

                rootDN = "";

                foreach (string str in rootDNcom)
                {
                    string temp = string.Concat("dc=", str, ",");
                    rootDN = string.Concat(rootDN, temp);
                }

                rootDN = rootDN.Substring(0, rootDN.Length - 1);
            }
            //beacuse rootDN is nothing but collection of all DC's from DN
            if (sDCs != null)
            {
                rootDN = sDCs;
            }

            baseDn = "";

            //sCNs = RootDSE, Configuration, Schema, Domain
            if (sCNs != null && sDCs == null)
            {
                if (sCNs.Equals("RootDSE", StringComparison.InvariantCultureIgnoreCase))
                {
                    baseDn = "";
                }
                else if (sCNs.Equals("Configuration", StringComparison.InvariantCultureIgnoreCase))
                {
                    baseDn = string.Concat("CN=Configuration,", rootDN);
                }
                else if (sCNs.Equals("Schema", StringComparison.InvariantCultureIgnoreCase))
                {
                    baseDn = string.Concat("CN=Schema,", rootDN);
                }
                else if (sCNs.Equals("Domain", StringComparison.InvariantCultureIgnoreCase) ||
                         sCNs.Equals("", StringComparison.InvariantCultureIgnoreCase) ||
                         sCNs.StartsWith("<"))
                {
                    if (rootDN != null)
                    {
                        baseDn = rootDN;
                    }
                }
                else
                {
                    baseDn = string.Concat(sCNs, ",", rootDN);
                }
            }

            if (sCNs != null && sDCs != null)
            {
                baseDn = string.Concat(sCNs, ",", sDCs);
            }

            if (sCNs == null && sDCs != null)
            {
                baseDn = sDCs;
            }

            if (sCNs == null && sDCs == null)
            {
                baseDn = rootDN;
            }

            //assign sName value using the dN of this node
            if (baseDn.Equals("", StringComparison.InvariantCultureIgnoreCase))
            {
                sName = "RootDSE";
            }
            else
            {
                sName = baseDn;
            }
        }
예제 #2
0
        private void Get_baseDn_Guid_Or_sid()
        {
            if (sCNs != null && sCNs.StartsWith("<")) //for instance, LDAP://corpqa.centeris.com/<GUID=***> <GUID...> part will be used as filter to search the whole domain
            {
                //GUID=\XX\XX\XX...
                if (sCNs.Substring(1, 4).Equals("GUID", StringComparison.InvariantCultureIgnoreCase))
                {
                    string guidstr = sCNs.Substring(6);
                    guidstr = guidstr.Substring(0, guidstr.Length - 1);

                    Guid myguid = new Guid(guidstr);

                    byte[] guidbytes         = myguid.ToByteArray();
                    System.Text.Encoding enc = System.Text.Encoding.ASCII;
                    string guidbytestr       = BitConverter.ToString(guidbytes);
                    guidbytestr = guidbytestr.Replace("-", "\\");
                    guidbytestr = string.Concat("\\", guidbytestr);

                    string dN = SDSUtils.SearchByGuid(guidbytestr, dirContext);

                    if (dN != null)
                    {
                        baseDn = dN;
                    }

                    get_baseDnFor_guidOrsid_called = true;
                }
                //SID=S-1-... Or SID=\XX\XX\XX....
                if (sCNs.Substring(1, 3).Equals("SID", StringComparison.InvariantCultureIgnoreCase))
                {
                    string sid = sCNs.Substring(5);
                    sid = sid.Substring(0, sid.Length - 1);

                    if (!sid.StartsWith("S-"))
                    {
                        char[] ldapsid = new char[sid.Length / 2 * 3];

                        int j = 0;

                        for (int i = 0; i < ldapsid.Length; i++)
                        {
                            if (i % 3 == 0)
                            {
                                ldapsid[i] = '\\';
                            }
                            else
                            {
                                ldapsid[i] = sid[j];
                                j++;
                            }
                        }

                        sid = new string(ldapsid);
                    }

                    string dN = SDSUtils.SearchBySid(sid, dirContext);

                    baseDn = dN;

                    get_baseDnFor_guidOrsid_called = true;
                }
            }

            //assign sName value using the dN of this node
            if (baseDn.Equals("", StringComparison.InvariantCultureIgnoreCase))
            {
                sName = "RootDSE";
            }
            else
            {
                sName = baseDn;
            }
        }
예제 #3
0
        public void CommitChanges()
        {
            Assign_dirContext();

            if (dirContext == null)
            {
                return;
            }

            if (!get_baseDnFor_guidOrsid_called)
            {
                Get_baseDn_Guid_Or_sid();
            }

            string[]    search_attrs = { null };
            LdapMessage ldapMessage  = dirContext.SearchSynchronous(
                baseDn,
                LdapAPI.LDAPSCOPE.BASE,
                "(objectClass=*)",
                search_attrs,
                false);
            List <LdapEntry> ldapEntries = (ldapMessage != null ? ldapMessage.Ldap_Get_Entries() : null);

            //if this object does not exist in AD, we need create it first
            if (ldapEntries == null || ldapEntries.Count == 0)
            {
                int ret = SDSUtils.AddNewObj(dirContext, objectClassType, baseDn);
                if (ret != 0)
                {
                    //Console.WriteLine("Create new object failed!");
                    return;
                }
            }

            //go through the properties to check whether there is PropertyValueCollection has been modified
            //PropertyCollection: Dictionary<string, PropertyValueCollection>
            if (propertyCollection != null && propertyCollection.Count > 0)
            {
                foreach (KeyValuePair <string, PropertyValueCollection> kvp in propertyCollection)
                {
                    if (kvp.Value.Modified)
                    {
                        //Console.WriteLine("BaseDN is " + baseDn + " Modified key value pair: " + kvp.Key );
                        int ret = SDSUtils.ModifyProperty(dirContext, baseDn, kvp.Key, kvp.Value);
                        //if (ret != 0) ; Console.WriteLine("Modify a property failed");
                    }
                }
            }

            //go through its children to see whether this is any children marked needed be deleted
            if (children != null && children.Count > 0)
            {
                DirectoryEntries modifiedChildren = new DirectoryEntries();

                foreach (DirectoryEntry child in children)
                {
                    if (child.ToBeDeleted) //delete this DE
                    {
                        int ret = SDSUtils.DeleteObj(dirContext, child.Name);
                    }
                }

                //reflect the changes to children collection
                foreach (DirectoryEntry child in children)
                {
                    if (!child.ToBeDeleted)
                    {
                        modifiedChildren.Add(child);
                    }
                }

                children = modifiedChildren;
            }
        }