Beispiel #1
0
        protected override XmlElement ToXmlNode(XmlDocument doc)
        {
            string       str;
            XmlElement   xmlElement   = base.CreateRequestElement(doc, "searchRequest", true, this.dn);
            XmlAttribute xmlAttribute = doc.CreateAttribute("scope", null);
            SearchScope  searchScope  = this.directoryScope;

            switch (searchScope)
            {
            case SearchScope.Base:
            {
                xmlAttribute.InnerText = "baseObject";
                break;
            }

            case SearchScope.OneLevel:
            {
                xmlAttribute.InnerText = "singleLevel";
                break;
            }

            case SearchScope.Subtree:
            {
                xmlAttribute.InnerText = "wholeSubtree";
                break;
            }
            }
            xmlElement.Attributes.Append(xmlAttribute);
            XmlAttribute     xmlAttribute1   = doc.CreateAttribute("derefAliases", null);
            DereferenceAlias dereferenceAlia = this.directoryRefAlias;

            switch (dereferenceAlia)
            {
            case DereferenceAlias.Never:
            {
                xmlAttribute1.InnerText = "neverDerefAliases";
                break;
            }

            case DereferenceAlias.InSearching:
            {
                xmlAttribute1.InnerText = "derefInSearching";
                break;
            }

            case DereferenceAlias.FindingBaseObject:
            {
                xmlAttribute1.InnerText = "derefFindingBaseObj";
                break;
            }

            case DereferenceAlias.Always:
            {
                xmlAttribute1.InnerText = "derefAlways";
                break;
            }
            }
            xmlElement.Attributes.Append(xmlAttribute1);
            XmlAttribute str1 = doc.CreateAttribute("sizeLimit", null);

            str1.InnerText = this.directorySizeLimit.ToString(CultureInfo.InvariantCulture);
            xmlElement.Attributes.Append(str1);
            XmlAttribute str2  = doc.CreateAttribute("timeLimit", null);
            long         ticks = this.directoryTimeLimit.Ticks / (long)0x989680;

            str2.InnerText = ticks.ToString(CultureInfo.InvariantCulture);
            xmlElement.Attributes.Append(str2);
            XmlAttribute xmlAttribute2 = doc.CreateAttribute("typesOnly", null);
            XmlAttribute xmlAttribute3 = xmlAttribute2;

            if (this.directoryTypesOnly)
            {
                str = "true";
            }
            else
            {
                str = "false";
            }
            xmlAttribute3.InnerText = str;
            xmlElement.Attributes.Append(xmlAttribute2);
            XmlElement outerXml = doc.CreateElement("filter", "urn:oasis:names:tc:DSML:2:0:core");

            if (this.Filter == null)
            {
                outerXml.InnerXml = "<present name='objectClass' xmlns=\"urn:oasis:names:tc:DSML:2:0:core\"/>";
            }
            else
            {
                StringWriter  stringWriter  = new StringWriter(CultureInfo.InvariantCulture);
                XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
                try
                {
                    if (this.Filter as XmlDocument == null)
                    {
                        if (this.Filter as string != null)
                        {
                            string filter = (string)this.Filter;
                            if (!filter.StartsWith("(", StringComparison.Ordinal) && !filter.EndsWith(")", StringComparison.Ordinal))
                            {
                                filter = filter.Insert(0, "(");
                                filter = string.Concat(filter, ")");
                            }
                            ADFilter aDFilter = FilterParser.ParseFilterString(filter);
                            if (aDFilter != null)
                            {
                                DSMLFilterWriter dSMLFilterWriter = new DSMLFilterWriter();
                                dSMLFilterWriter.WriteFilter(aDFilter, false, xmlTextWriter, "urn:oasis:names:tc:DSML:2:0:core");
                                outerXml.InnerXml = stringWriter.ToString();
                            }
                            else
                            {
                                throw new ArgumentException(Res.GetString("BadSearchLDAPFilter"));
                            }
                        }
                    }
                    else
                    {
                        if (((XmlDocument)this.Filter).NamespaceURI.Length != 0)
                        {
                            outerXml.InnerXml = ((XmlDocument)this.Filter).OuterXml;
                        }
                        else
                        {
                            this.CopyFilter((XmlDocument)this.Filter, xmlTextWriter);
                            outerXml.InnerXml = stringWriter.ToString();
                        }
                    }
                }
                finally
                {
                    xmlTextWriter.Close();
                }
            }
            xmlElement.AppendChild(outerXml);
            if (this.directoryAttributes != null && this.directoryAttributes.Count != 0)
            {
                XmlElement xmlElement1 = doc.CreateElement("attributes", "urn:oasis:names:tc:DSML:2:0:core");
                xmlElement.AppendChild(xmlElement1);
                foreach (string directoryAttribute in this.directoryAttributes)
                {
                    DirectoryAttribute directoryAttribute1 = new DirectoryAttribute();
                    directoryAttribute1.Name = directoryAttribute;
                    XmlElement xmlNode = directoryAttribute1.ToXmlNode(doc, "attribute");
                    xmlElement1.AppendChild(xmlNode);
                }
            }
            return(xmlElement);
        }
Beispiel #2
0
        protected override XmlElement ToXmlNode(XmlDocument doc)
        {
            XmlElement elem = CreateRequestElement(doc, "searchRequest", true, _dn);

            // attach the "scope" attribute (required)
            XmlAttribute attrScope = doc.CreateAttribute("scope", null);

            switch (_directoryScope)
            {
            case SearchScope.Subtree:
                attrScope.InnerText = "wholeSubtree";
                break;

            case SearchScope.OneLevel:
                attrScope.InnerText = "singleLevel";
                break;

            case SearchScope.Base:
                attrScope.InnerText = "baseObject";
                break;

            default:
                Debug.Assert(false, "Unknown DsmlSearchScope type");
                break;
            }

            elem.Attributes.Append(attrScope);

            // attach the "derefAliases" attribute (required)
            XmlAttribute attrDerefAliases = doc.CreateAttribute("derefAliases", null);

            switch (_directoryRefAlias)
            {
            case DereferenceAlias.Never:
                attrDerefAliases.InnerText = "neverDerefAliases";
                break;

            case DereferenceAlias.InSearching:
                attrDerefAliases.InnerText = "derefInSearching";
                break;

            case DereferenceAlias.FindingBaseObject:
                attrDerefAliases.InnerText = "derefFindingBaseObj";
                break;

            case DereferenceAlias.Always:
                attrDerefAliases.InnerText = "derefAlways";
                break;

            default:
                Debug.Assert(false, "Unknown DsmlDereferenceAlias type");
                break;
            }

            elem.Attributes.Append(attrDerefAliases);

            // attach the "sizeLimit" attribute (optional)
            XmlAttribute attrSizeLimit = doc.CreateAttribute("sizeLimit", null);

            attrSizeLimit.InnerText = _directorySizeLimit.ToString(CultureInfo.InvariantCulture);
            elem.Attributes.Append(attrSizeLimit);

            // attach the "timeLimit" attribute (optional)
            XmlAttribute attrTimeLimit = doc.CreateAttribute("timeLimit", null);

            attrTimeLimit.InnerText = (_directoryTimeLimit.Ticks / TimeSpan.TicksPerSecond).ToString(CultureInfo.InvariantCulture);
            elem.Attributes.Append(attrTimeLimit);

            // attach the "typesOnly" attribute (optional, defaults to false)
            XmlAttribute attrTypesOnly = doc.CreateAttribute("typesOnly", null);

            attrTypesOnly.InnerText = _directoryTypesOnly ? "true" : "false";
            elem.Attributes.Append(attrTypesOnly);

            // add in the <filter> element (required)
            XmlElement elemFilter = doc.CreateElement("filter", DsmlConstants.DsmlUri);

            if (Filter != null)
            {
                StringWriter  stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                XmlTextWriter xmlWriter    = new XmlTextWriter(stringWriter);

                try
                {
                    if (Filter is XmlDocument)
                    {
                        if (((XmlDocument)Filter).NamespaceURI.Length == 0)
                        {
                            // namespaceURI is not explicitly specified
                            CopyFilter((XmlDocument)Filter, xmlWriter);
                            elemFilter.InnerXml = stringWriter.ToString();
                        }
                        else
                        {
                            elemFilter.InnerXml = ((XmlDocument)Filter).OuterXml;
                        }
                    }
                    else if (Filter is string)
                    {
                        //
                        // Search Filter
                        // We make use of the code from DSDE, which requires an intermediary
                        // trip through the ADFilter representation.  Although ADFilter is unnecessary
                        // for our purposes, this enables us to use the same exact code as
                        // DSDE, without having to maintain a second copy of it.
                        //

                        // AD filter currently does not support filter without paranthesis, so adding it explicitly
                        string tempFilter = (string)Filter;
                        if (!tempFilter.StartsWith("(", StringComparison.Ordinal) && !tempFilter.EndsWith(")", StringComparison.Ordinal))
                        {
                            tempFilter = tempFilter.Insert(0, "(");
                            tempFilter = String.Concat(tempFilter, ")");
                        }

                        // Convert LDAP filter string to ADFilter representation
                        ADFilter adfilter = FilterParser.ParseFilterString(tempFilter);

                        if (adfilter == null)
                        {
                            // The LDAP filter string didn't parse correctly
                            throw new ArgumentException(Res.GetString(Res.BadSearchLDAPFilter));
                        }

                        // Convert ADFilter representation to a DSML filter string
                        //   Ideally, we'd skip the intemediary string, but the DSDE conversion
                        //   routines expect a XmlWriter, and the only XmlWriter available
                        //   is the XmlTextWriter, which produces text.
                        DSMLFilterWriter filterwriter = new DSMLFilterWriter();

                        filterwriter.WriteFilter(adfilter, false, xmlWriter, DsmlConstants.DsmlUri);

                        elemFilter.InnerXml = stringWriter.ToString();
                    }
                    else
                    {
                        Debug.Assert(false, "Unknown filter type");
                    }
                }
                finally
                {
                    // close this stream and the underlying stream.
                    xmlWriter.Close();
                }
            }
            else
            {
                // default filter: (objectclass=*)
                elemFilter.InnerXml = DsmlConstants.DefaultSearchFilter;
            }

            elem.AppendChild(elemFilter);

            // add in the <attributes> element (optional)
            if (_directoryAttributes != null && _directoryAttributes.Count != 0)
            {
                // create and attach the <attributes> element
                XmlElement elemAttributes = doc.CreateElement("attributes", DsmlConstants.DsmlUri);
                elem.AppendChild(elemAttributes);

                // create and attach the <attribute> elements under the <attributes> element
                foreach (string attrName in _directoryAttributes)
                {
                    // DsmlAttribute objects know how to persist themself in the right
                    // XML format, so we'll make use of that here rather than
                    // duplicating the code
                    DirectoryAttribute attr = new DirectoryAttribute();
                    attr.Name = attrName;
                    XmlElement elemAttr = attr.ToXmlNode(doc, "attribute");
                    elemAttributes.AppendChild(elemAttr);
                }
            }

            return(elem);
        }
        protected override XmlElement ToXmlNode(XmlDocument doc)
        {
            XmlElement   element = base.CreateRequestElement(doc, "searchRequest", true, this.dn);
            XmlAttribute node    = doc.CreateAttribute("scope", null);

            switch (this.directoryScope)
            {
            case SearchScope.Base:
                node.InnerText = "baseObject";
                break;

            case SearchScope.OneLevel:
                node.InnerText = "singleLevel";
                break;

            case SearchScope.Subtree:
                node.InnerText = "wholeSubtree";
                break;
            }
            element.Attributes.Append(node);
            XmlAttribute attribute2 = doc.CreateAttribute("derefAliases", null);

            switch (this.directoryRefAlias)
            {
            case DereferenceAlias.Never:
                attribute2.InnerText = "neverDerefAliases";
                break;

            case DereferenceAlias.InSearching:
                attribute2.InnerText = "derefInSearching";
                break;

            case DereferenceAlias.FindingBaseObject:
                attribute2.InnerText = "derefFindingBaseObj";
                break;

            case DereferenceAlias.Always:
                attribute2.InnerText = "derefAlways";
                break;
            }
            element.Attributes.Append(attribute2);
            XmlAttribute attribute3 = doc.CreateAttribute("sizeLimit", null);

            attribute3.InnerText = this.directorySizeLimit.ToString(CultureInfo.InvariantCulture);
            element.Attributes.Append(attribute3);
            XmlAttribute attribute4 = doc.CreateAttribute("timeLimit", null);

            attribute4.InnerText = (this.directoryTimeLimit.Ticks / 0x989680L).ToString(CultureInfo.InvariantCulture);
            element.Attributes.Append(attribute4);
            XmlAttribute attribute5 = doc.CreateAttribute("typesOnly", null);

            attribute5.InnerText = this.directoryTypesOnly ? "true" : "false";
            element.Attributes.Append(attribute5);
            XmlElement newChild = doc.CreateElement("filter", "urn:oasis:names:tc:DSML:2:0:core");

            if (this.Filter != null)
            {
                StringWriter  w      = new StringWriter(CultureInfo.InvariantCulture);
                XmlTextWriter writer = new XmlTextWriter(w);
                try
                {
                    if (this.Filter is XmlDocument)
                    {
                        if (((XmlDocument)this.Filter).NamespaceURI.Length == 0)
                        {
                            this.CopyFilter((XmlDocument)this.Filter, writer);
                            newChild.InnerXml = w.ToString();
                        }
                        else
                        {
                            newChild.InnerXml = ((XmlDocument)this.Filter).OuterXml;
                        }
                    }
                    else if (this.Filter is string)
                    {
                        string str = (string)this.Filter;
                        if (!str.StartsWith("(", StringComparison.Ordinal) && !str.EndsWith(")", StringComparison.Ordinal))
                        {
                            str = str.Insert(0, "(") + ")";
                        }
                        ADFilter filter = FilterParser.ParseFilterString(str);
                        if (filter == null)
                        {
                            throw new ArgumentException(System.DirectoryServices.Protocols.Res.GetString("BadSearchLDAPFilter"));
                        }
                        new DSMLFilterWriter().WriteFilter(filter, false, writer, "urn:oasis:names:tc:DSML:2:0:core");
                        newChild.InnerXml = w.ToString();
                    }
                }
                finally
                {
                    writer.Close();
                }
            }
            else
            {
                newChild.InnerXml = "<present name='objectClass' xmlns=\"urn:oasis:names:tc:DSML:2:0:core\"/>";
            }
            element.AppendChild(newChild);
            if ((this.directoryAttributes != null) && (this.directoryAttributes.Count != 0))
            {
                XmlElement element3 = doc.CreateElement("attributes", "urn:oasis:names:tc:DSML:2:0:core");
                element.AppendChild(element3);
                foreach (string str2 in this.directoryAttributes)
                {
                    XmlElement element4 = new DirectoryAttribute {
                        Name = str2
                    }.ToXmlNode(doc, "attribute");
                    element3.AppendChild(element4);
                }
            }
            return(element);
        }
Beispiel #4
0
        public static ADFilter ParseFilterString(string filter)
        {
            ADFilter aDFilter  = null;
            ADFilter aDFilter1 = null;

            try
            {
                Match match = FilterParser.mFilter.Match(filter);
                if (match.Success)
                {
                    ADFilter arrayLists = new ADFilter();
                    if (match.Groups["item"].ToString().Length == 0)
                    {
                        ArrayList arrayLists1 = new ArrayList();
                        string    str         = match.Groups["filterlist"].ToString().Trim();
                        while (str.Length > 0)
                        {
                            if (str[0] == '(')
                            {
                                int  num  = 1;
                                int  num1 = 1;
                                bool flag = false;
                                while (num < str.Length && !flag)
                                {
                                    if (str[num] == '(')
                                    {
                                        num1++;
                                    }
                                    if (str[num] == ')')
                                    {
                                        if (num1 >= 1)
                                        {
                                            if (num1 != 1)
                                            {
                                                num1--;
                                            }
                                            else
                                            {
                                                flag = true;
                                            }
                                        }
                                        else
                                        {
                                            aDFilter1 = null;
                                            return(aDFilter1);
                                        }
                                    }
                                    num++;
                                }
                                if (flag)
                                {
                                    arrayLists1.Add(str.Substring(0, num));
                                    str = str.Substring(num).TrimStart(new char[0]);
                                }
                                else
                                {
                                    aDFilter1 = null;
                                    return(aDFilter1);
                                }
                            }
                            else
                            {
                                aDFilter1 = null;
                                return(aDFilter1);
                            }
                        }
                        string str1 = match.Groups["filtercomp"].ToString();
                        string str2 = str1;
                        if (str1 != null)
                        {
                            if (str2 == "|")
                            {
                                arrayLists.Type      = ADFilter.FilterType.Or;
                                arrayLists.Filter.Or = new ArrayList();
                                foreach (string arrayList in arrayLists1)
                                {
                                    aDFilter = FilterParser.ParseFilterString(arrayList);
                                    if (aDFilter != null)
                                    {
                                        arrayLists.Filter.Or.Add(aDFilter);
                                    }
                                    else
                                    {
                                        aDFilter1 = null;
                                        return(aDFilter1);
                                    }
                                }
                                if (arrayLists.Filter.Or.Count < 1)
                                {
                                    aDFilter1 = null;
                                    return(aDFilter1);
                                }
                                else
                                {
                                    goto Label1;
                                }
                            }
                            else
                            {
                                if (str2 == "&")
                                {
                                    arrayLists.Type       = ADFilter.FilterType.And;
                                    arrayLists.Filter.And = new ArrayList();
                                    foreach (string arrayList1 in arrayLists1)
                                    {
                                        aDFilter = FilterParser.ParseFilterString(arrayList1);
                                        if (aDFilter != null)
                                        {
                                            arrayLists.Filter.And.Add(aDFilter);
                                        }
                                        else
                                        {
                                            aDFilter1 = null;
                                            return(aDFilter1);
                                        }
                                    }
                                    if (arrayLists.Filter.And.Count < 1)
                                    {
                                        aDFilter1 = null;
                                        return(aDFilter1);
                                    }
                                    else
                                    {
                                        goto Label1;
                                    }
                                }
                                else
                                {
                                    if (str2 != "!")
                                    {
                                        aDFilter1 = null;
                                        return(aDFilter1);
                                    }
                                    arrayLists.Type = ADFilter.FilterType.Not;
                                    aDFilter        = FilterParser.ParseFilterString((string)arrayLists1[0]);
                                    if (arrayLists1.Count > 1 || aDFilter == null)
                                    {
                                        aDFilter1 = null;
                                        return(aDFilter1);
                                    }
                                    else
                                    {
                                        arrayLists.Filter.Not = aDFilter;
                                        goto Label1;
                                    }
                                }
                            }
                        }
                        aDFilter1 = null;
                        return(aDFilter1);
                    }
                    else
                    {
                        if (match.Groups["present"].ToString().Length == 0)
                        {
                            if (match.Groups["simple"].ToString().Length == 0)
                            {
                                if (match.Groups["substr"].ToString().Length == 0)
                                {
                                    if (match.Groups["extensible"].ToString().Length == 0)
                                    {
                                        aDFilter1 = null;
                                        return(aDFilter1);
                                    }
                                    else
                                    {
                                        arrayLists.Type = ADFilter.FilterType.ExtensibleMatch;
                                        ADExtenMatchFilter aDExtenMatchFilter = new ADExtenMatchFilter();
                                        aDExtenMatchFilter.Value          = FilterParser.StringFilterValueToADValue(match.Groups["extenvalue"].ToString());
                                        aDExtenMatchFilter.DNAttributes   = match.Groups["dnattr"].ToString().Length != 0;
                                        aDExtenMatchFilter.Name           = match.Groups["extenattr"].ToString();
                                        aDExtenMatchFilter.MatchingRule   = match.Groups["matchrule"].ToString();
                                        arrayLists.Filter.ExtensibleMatch = aDExtenMatchFilter;
                                    }
                                }
                                else
                                {
                                    arrayLists.Type = ADFilter.FilterType.Substrings;
                                    ADSubstringFilter aDSubstringFilter = new ADSubstringFilter();
                                    aDSubstringFilter.Initial = FilterParser.StringFilterValueToADValue(match.Groups["initialvalue"].ToString());
                                    aDSubstringFilter.Final   = FilterParser.StringFilterValueToADValue(match.Groups["finalvalue"].ToString());
                                    if (match.Groups["anyvalue"].ToString().Length != 0)
                                    {
                                        foreach (Capture capture in match.Groups["anyvalue"].Captures)
                                        {
                                            aDSubstringFilter.Any.Add(FilterParser.StringFilterValueToADValue(capture.ToString()));
                                        }
                                    }
                                    aDSubstringFilter.Name       = match.Groups["substrattr"].ToString();
                                    arrayLists.Filter.Substrings = aDSubstringFilter;
                                }
                            }
                            else
                            {
                                ADAttribute aDAttribute = new ADAttribute();
                                if (match.Groups["simplevalue"].ToString().Length != 0)
                                {
                                    ADValue aDValue = FilterParser.StringFilterValueToADValue(match.Groups["simplevalue"].ToString());
                                    aDAttribute.Values.Add(aDValue);
                                }
                                aDAttribute.Name = match.Groups["simpleattr"].ToString();
                                string str3 = match.Groups["filtertype"].ToString();
                                string str4 = str3;
                                if (str3 != null)
                                {
                                    if (str4 == "=")
                                    {
                                        arrayLists.Type = ADFilter.FilterType.EqualityMatch;
                                        arrayLists.Filter.EqualityMatch = aDAttribute;
                                        goto Label1;
                                    }
                                    else
                                    {
                                        if (str4 == "~=")
                                        {
                                            arrayLists.Type = ADFilter.FilterType.ApproxMatch;
                                            arrayLists.Filter.ApproxMatch = aDAttribute;
                                            goto Label1;
                                        }
                                        else
                                        {
                                            if (str4 == "<=")
                                            {
                                                arrayLists.Type = ADFilter.FilterType.LessOrEqual;
                                                arrayLists.Filter.LessOrEqual = aDAttribute;
                                                goto Label1;
                                            }
                                            else
                                            {
                                                if (str4 != ">=")
                                                {
                                                    aDFilter1 = null;
                                                    return(aDFilter1);
                                                }
                                                arrayLists.Type = ADFilter.FilterType.GreaterOrEqual;
                                                arrayLists.Filter.GreaterOrEqual = aDAttribute;
                                                goto Label1;
                                            }
                                        }
                                    }
                                }
                                aDFilter1 = null;
                                return(aDFilter1);
                            }
                        }
                        else
                        {
                            arrayLists.Type           = ADFilter.FilterType.Present;
                            arrayLists.Filter.Present = match.Groups["presentattr"].ToString();
                        }
                    }
Label1:
                    aDFilter1 = arrayLists;
                }
                else
                {
                    aDFilter1 = null;
                }
            }
            catch (Exception regexMatchTimeoutException)
            {
                aDFilter1 = null;
            }
            return(aDFilter1);
        }