Ejemplo n.º 1
0
        public LdapValue[] GetAttributeValues(IntPtr ldapEntry, string attrName, DirectoryContext dirContext)
        {
            if (dirContext == null ||
                String.IsNullOrEmpty(attrName))
            {
                return(null);
            }

            if (dirContext.SchemaCache == null)
            {
                return(GetNonBervals(ldapEntry, attrName));
            }

            LDAPSchemaCache schema    = dirContext.SchemaCache;
            SchemaType      foundType = schema.GetSchemaTypeByDisplayName(attrName);

            // if cannot find type in schemaCache, get byte[] first
            //(sometimes schemaCache doesn't contain objectGUID,...type information)
            if (foundType == null)
            {
                if (String.Equals(attrName, "objectGUID", StringComparison.InvariantCultureIgnoreCase) ||
                    String.Equals(attrName, "objectSid", StringComparison.InvariantCultureIgnoreCase) ||
                    String.Equals(attrName, "nTsecurityDescriptor", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(ReformatBervalsInHex(ldapEntry, attrName));
                }
                else
                {
                    return(GetNonBervals(ldapEntry, attrName));
                }
            }
            ADSType adstype = foundType.DataType;

            if (adstype == ADSType.ADSTYPE_OCTET_STRING ||
                adstype == ADSType.ADSTYPE_NT_SECURITY_DESCRIPTOR)
            {
                return(ReformatBervalsInHex(ldapEntry, attrName));
            }
            else if (adstype == ADSType.ADSTYPE_INTEGER ||
                     adstype == ADSType.ADSTYPE_LARGE_INTEGER)
            {
                LdapValue[] values = GetBervals(ldapEntry, attrName);

                if (values == null || values.Length == 0)
                {
                    return(values);
                }
                foreach (LdapValue value in values)
                {
                    string replace      = value.stringData.Replace("-", "");
                    char[] intValuechrs = new char[replace.Length / 2];

                    for (int i = 0; i < replace.Length / 2; i++)
                    {
                        intValuechrs[i] = replace[i * 2 + 1];
                    }

                    for (int i = 0; i < intValuechrs.Length; i++)
                    {
                        if (intValuechrs[i] < '0' || intValuechrs[i] > '9')
                        {
                            intValuechrs[i] = '-';
                        }
                    }
                    value.stringData = new string(intValuechrs);
                    value.stringData.Replace("-", "");

                    if (adstype == ADSType.ADSTYPE_INTEGER)
                    {
                        value.intData = Convert.ToInt32(value.stringData);
                        value.AdsType = ADSType.ADSTYPE_INTEGER;
                    }
                    else if (adstype == ADSType.ADSTYPE_LARGE_INTEGER)
                    {
                        value.longData = Convert.ToInt64(value.stringData);
                        value.AdsType  = ADSType.ADSTYPE_LARGE_INTEGER;
                    }
                }
            }
            return(GetNonBervals(ldapEntry, attrName));
        }
Ejemplo n.º 2
0
        public static LDAPSchemaCache Build(DirectoryContext ldapContext)
        {
            DateTime BuildTimer = DateTime.Now;

            LDAPSchemaCache result = null;

            List <LdapEntry> ldapEntries = new List <LdapEntry>();
            Dictionary <string, LdapEntry> ldapEntriesDic = new Dictionary <string, LdapEntry>();

            string[] search_attrs = { null };

            DateTime timer = Logger.StartTimer();

            Logger.Log(
                "Started building schema cache",
                Logger.ldapLogLevel);

            int ret = ldapContext.ListChildEntriesSynchronous(
                string.Concat("cn=Schema,", ldapContext.ConfigurationNamingContext),
                LdapAPI.LDAPSCOPE.SUB_TREE,
                "(objectClass=*)",
                search_attrs,
                false,
                out ldapEntries);

            if (ldapEntries != null && ldapEntries.Count > 0)
            {
                Logger.TimerMsg(ref timer,
                                String.Format(
                                    "schema cache: {0} entries read from LDAP",
                                    ldapEntries.Count));
                result        = new LDAPSchemaCache();
                result.rootDN = ldapContext.RootDN;
                int entryIndex = 0;

                foreach (LdapEntry entry in ldapEntries)
                {
                    LdapValue[] attrName = entry.GetAttributeValues("lDAPDisplayName", ldapContext);
                    if (attrName != null)
                    {
                        ldapEntriesDic.Add(attrName[0].stringData, entry);
                    }
                }

                foreach (LdapEntry ldapNextEntry in ldapEntries)
                {
                    AttributeMap attrmap = new AttributeMap();

                    string dn = ldapNextEntry.GetDN();

                    Logger.Log(String.Format(
                                   "LDAPSchemaCache.Build: ldapEntry[{0}]: DN = " + dn,
                                   entryIndex++),
                               Logger.ldapLogLevel);

                    //Getting 'objectClasss' value instead 'subClassOf' attribute
                    //becuase 'subClassOf' attribute is not giving the all mandatory attribute for the selected object.
                    LdapValue[] objectClasses = ldapNextEntry.GetAttributeValues("objectClass", ldapContext);
                    foreach (LdapValue Oclass in objectClasses)
                    {
                        if (ldapEntriesDic.ContainsKey(Oclass.stringData))
                        {
                            attrmap.ObjectClasses.Add(Oclass.stringData, ldapEntriesDic[Oclass.stringData]);
                        }
                    }
                    attrmap.ldapContext = ldapContext;
                    attrmap.Tag         = ldapNextEntry;

                    string[] attrs = ldapNextEntry.GetAttributeNames();

                    foreach (string attr in attrs)
                    {
                        LdapValue[] values = ldapNextEntry.GetAttributeValues(attr, ldapContext);

                        attrmap.Add(attr, values);
                    }

                    result.AddSchemaType(attrmap);
                }

                Logger.TimerMsg(ref timer,
                                String.Format(
                                    "LDAPSchemaCache.Build({0})-- finished building the schemaCache",
                                    ldapContext.RootDN));

                result.reportSchemaCache(Logger.ldapLogLevel);
            }


            return(result);
        }