//find the DN of given the sid
        public static string SearchBySid(string sid, Likewise.LMC.LDAP.DirectoryContext dirContext)
        {
            string searchFilter = string.Concat("(objectSid=", sid, ")");

            LdapMessage ldapMessage = dirContext.SearchSynchronous(
                       dirContext.RootDN,
                       LdapAPI.LDAPSCOPE.SUB_TREE,
                       searchFilter,
                       null,
                       false);

            if (ldapMessage == null)
            {
               // Logger.Log("ldapMessage = null");
                return null;
            }
            else
            {
                List<LdapEntry> ldapEntries = ldapMessage.Ldap_Get_Entries();
                if (ldapEntries == null || ldapEntries.Count == 0)
                {
                   // Logger.Log("ldapEntries.Count == 0");
                    return null;
                }

                LdapEntry ldapNextEntry = ldapEntries[0];

                if (ldapNextEntry != null)
                {
                    string[] attrsList = ldapNextEntry.GetAttributeNames();

                    if (attrsList != null)
                    {
                        foreach (string attr in attrsList)
                        {
                            if (attr.Equals("distinguishedName", StringComparison.InvariantCultureIgnoreCase))
                            {
                                LdapValue[] attrValues = ldapNextEntry.GetAttributeValues(attr, dirContext);

                                if (attrValues != null && attrValues.Length > 0)
                                {
                                    return attrValues[0].stringData;
                                }
                            }
                        }
                    }
                }
                return null;
            }
        }
        //this function will return a ldapMessage that contains all the attributes that are available for an object
        //use this to populate DirectoryEntry's properties
        public static List<string> InitLdapMessageFilterForProperties(Likewise.LMC.LDAP.DirectoryContext dirContext, string nodeDN)
        {
            LdapMessage ldapMessagetemp = null;

            string[] attrs = { "name", "allowedAttributes", null };

            if (ldapMessagetemp == null)
            {
                ldapMessagetemp = dirContext.SearchSynchronous(
                    nodeDN,
                    LdapAPI.LDAPSCOPE.BASE,
                    "(objectClass=*)",
                    attrs,
                    false);
            }

            if (ldapMessagetemp == null)
                return null;

            List<LdapEntry> ldapEntries = ldapMessagetemp.Ldap_Get_Entries();

            if (ldapEntries == null || ldapEntries.Count == 0)
            {
                return null;
            }

            LdapEntry ldapNextEntry = ldapEntries[0];

            List<string> allowedAttributes = new List<string>();

            LdapValue[] attrValues = ldapNextEntry.GetAttributeValues("allowedAttributes", dirContext);
            if (attrValues != null && attrValues.Length > 0)
                foreach (LdapValue attrValue in attrValues)
                    allowedAttributes.Add(attrValue.stringData);

            return allowedAttributes;


        }
        public static int DeleteObj(Likewise.LMC.LDAP.DirectoryContext dirContext, string nodeDN)
        {
            if (dirContext != null)
            {
                return dirContext.DeleteSynchronous(nodeDN);
            }

            return -1;
        }
        public static int ModifyProperty(Likewise.LMC.LDAP.DirectoryContext dirContext, string nodeDN, string propertyName, PropertyValueCollection propertyValue)
        {
            List<object> valueObjects = propertyValue.ValueCollection;

            string[] values;

            if (valueObjects == null || valueObjects.Count == 0)
                values = new string[] { null };
            else if (valueObjects.Count == 1)
            {
                values = new string[] { ParsingValueObj(valueObjects[0]), null };
                //Console.WriteLine("In SDSUtils::modifyPropertyvalue is " + ParsingValueObj(valueObjects[0]));
            }
            else
            {
                values = new string[valueObjects.Count + 1];
                int i;
                for (i = 0; i < valueObjects.Count; i++)
                {
                    values[i] = ParsingValueObj(valueObjects[i]);
                    //Console.WriteLine("In SDSUtils::modifyPropertyvalue " + i + "is " + values[i]);
                }
                values[i] = null;
            }

            LDAPMod[] attrinfo = new LDAPMod[] { new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, propertyName, values) };

            return dirContext.ModifySynchronous(nodeDN, attrinfo);
        }
        public static int AddNewObj(Likewise.LMC.LDAP.DirectoryContext dirContext, string choosenclass, string nodeDN)
        {
            if (dirContext != null)
            {
                LDAPMod[] info = new LDAPMod[1];

                string[] objectClass_values = new string[] { choosenclass, null };
                info[0] = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_ADD, "ObjectClass", objectClass_values);

                return dirContext.AddSynchronous(nodeDN, info);
            }

            return -1;
        }