public static BerValue Read(BinaryReader reader, Int32 length, Byte code) { BerValue lResult; switch (code) { case Asn1.BITSTRING: lResult = new BerBinary(); break; case Asn1.ENUMERATED: lResult = new BerEnumerated(); break; case Asn1.BOOLEAN: lResult = new BerBoolean(); break; case Asn1.INTEGER: lResult = new BerInteger(); break; case Asn1.COUNTER: case Asn1.GAUGE: case Asn1.TIMETICKS: lResult = new BerUInteger(); break; case Asn1.LDAPREFERER: case Asn1.SET: case Asn1.SEQUENCE: lResult = new BerSequence(); break; case Asn1.IPADDRESS: lResult = new BerIpAddress(); break; case Asn1.OPAQUE: case Asn1.OCTETSTRING: lResult = new BerString(); break; default: if (code >= Asn1.LDAPSTARTRANGE && code <= Asn1.LDAPENDRANGE) { lResult = new BerSequence(); } else { lResult = new BerOther(); } break; } lResult.IntRead(reader, code, length); return(lResult); }
public LdapSearchResults Search(String baseObject, SearchScope scope, AliasDereferencing aliases, Int32 size, Int32 time, Boolean typesOnly, String filter, String[] attributes) { if (attributes == null || attributes.Length == 0) { attributes = new String[] { AllAttributes } } ; BerValue[] attributevalues = new BerValue[attributes.Length]; for (Int32 i = 0; i < attributes.Length; i++) { attributevalues[i] = new BerString(Asn1.OCTETSTRING, attributes[i]); } Int32 lSequenceId = SendLdapRequest(Asn1.LDAPSEARCHREQ, new BerString(Asn1.OCTETSTRING, baseObject), new BerEnumerated((Int32)scope), new BerEnumerated((Int32)aliases), new BerInteger(size), new BerInteger(time), new BerBoolean(typesOnly), Asn1.ParseFilter(String.IsNullOrEmpty(filter) ? "(objectclass=*)" : filter), new BerSequence(Asn1.SEQUENCE, attributevalues)); LdapSearchResults lResults = new LdapSearchResults(); while (true) { Response lResponse = ReadResponse(); if (lResponse.SequenceId != lSequenceId) { throw new LdapException("Invalid sequence id in bind response"); } if (lResponse.Code != 0) { throw new LdapException(lResponse.Code); } if (lResponse.TypeCode == Asn1.LDAPSEARCHENTRY) { if (lResponse.RestData != null && lResponse.RestData.Length > 0 && lResponse.RestData[0].Type == BerType.String) { LdapObject obj = new LdapObject(((BerString)lResponse.RestData[0]).Value); lResults.Add(obj); if (lResponse.RestData.Length > 1 && lResponse.RestData[1].Type == BerType.Sequence) { foreach (BerValue attribute in ((BerSequence)lResponse.RestData[1]).Items) { if (attribute.Type == BerType.Sequence && ((BerSequence)attribute).Items.Count > 0 && ((BerSequence)attribute).Items[0].Type == BerType.String) { LdapAttribute att = new LdapAttribute(((BerString)((BerSequence)attribute).Items[0]).Value); obj.Attributes.Add(att); if (((BerSequence)attribute).Items.Count <= 1 || ((BerSequence)attribute).Items[1].Type != BerType.Sequence) { continue; } foreach (BerValue value in ((BerSequence)((BerSequence)attribute).Items[1]).Items) { switch (value.Type) { case BerType.BitString: att.Binary = true; att.Add(((BerBinary)value).Value); break; case BerType.Boolean: att.Binary = false; att.Add(((BerBoolean)value).Value.ToString()); break; case BerType.Enumerated: case BerType.Integer: att.Binary = false; att.Add(((BerInteger)value).Value.ToString()); break; case BerType.IpAddress: att.Binary = false; att.Add(((BerIpAddress)value).Value.ToString()); break; case BerType.Other: att.Binary = true; att.Add(((BerOther)value).Value); break; case BerType.String: att.Binary = false; att.Add(((BerString)value).Value); break; case BerType.UInteger: att.Binary = false; att.Add(((BerUInteger)value).Value.ToString()); break; } } } } } } } else if (lResponse.TypeCode == Asn1.LDAPSEARCHREF) { if (lResponse.Referers != null) { foreach (BerValue val in lResponse.Referers.Items) { if (val is BerString) { lResults.Referals.Add(((BerString)val).Value); } } } else if (lResponse.RestData != null) { foreach (BerValue val in lResponse.RestData) { if (val is BerString) { lResults.Referals.Add(((BerString)val).Value); } if (val is BerSequence) { foreach (BerValue val2 in ((BerSequence)val).Items) { if (val2 is BerString) { lResults.Referals.Add(((BerString)val2).Value); } } } } } } else if (lResponse.TypeCode == Asn1.LDAPSEARCHDONE) { break; } else { throw new LdapException("Unknown response from server"); } } return(lResults); } }
public static BerValue Read(BinaryReader reader, Int32 length, Byte code) { BerValue lResult; switch (code) { case Asn1.BITSTRING: lResult = new BerBinary(); break; case Asn1.ENUMERATED: lResult = new BerEnumerated(); break; case Asn1.BOOLEAN: lResult = new BerBoolean(); break; case Asn1.INTEGER: lResult = new BerInteger(); break; case Asn1.COUNTER: case Asn1.GAUGE: case Asn1.TIMETICKS: lResult = new BerUInteger(); break; case Asn1.LDAPREFERER: case Asn1.SET: case Asn1.SEQUENCE: lResult = new BerSequence(); break; case Asn1.IPADDRESS: lResult = new BerIpAddress(); break; case Asn1.OPAQUE: case Asn1.OCTETSTRING: lResult = new BerString(); break; default: if (code >= Asn1.LDAPSTARTRANGE && code <= Asn1.LDAPENDRANGE) lResult = new BerSequence(); else lResult = new BerOther(); break; } lResult.IntRead(reader, code, length); return lResult; }
private static BerValue ParseSimple(String filter, ref Int32 pos, ref Int32 len, ref RFC1960Token tok, ref String res) { if (tok != RFC1960Token.Value) { throw new LdapException("Attribute name expected at " + pos); } String attributename = res; BerValue result; RFC1960Next(filter, ref pos, ref len, out tok, out res); switch (tok) { // <simple> ::= <attr> <filtertype> <value> // <filtertype> ::= <equal> | <approx> | <ge> | <le> // <equal> ::= '=' // <approx> ::= '~=' // <ge> ::= '>=' // <le> ::= '<=' case RFC1960Token.Equal: { RFC1960Next(filter, ref pos, ref len, out tok, out res); String currval = ParseValue(filter, ref pos, ref len, ref tok, ref res); if (tok != RFC1960Token.Any) { result = new BerSequence(Asn1.LDAPFILTEREQUALITYMATCH, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, currval)); break; } BerSequence items = new BerSequence(); items.Items.Add(new BerString(LDAPFILTERSUBSTRING_INIT, currval)); currval = null; while (tok == RFC1960Token.Any) { if (currval != null) { items.Items.Add(new BerString(LDAPFILTERSUBSTRING_ANY, currval)); } RFC1960Next(filter, ref pos, ref len, out tok, out res); currval = ParseValue(filter, ref pos, ref len, ref tok, ref res); } items.Items.Add(new BerString(LDAPFILTERSUBSTRING_FINAL, currval)); result = new BerSequence(Asn1.LDAPFILTERSUBSTRING, new BerString(OCTETSTRING, attributename), items); break; } // supports * too case RFC1960Token.AproxEqual: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerSequence(Asn1.LDAPFILTERAPPROXMATCH, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, ParseValue(filter, ref pos, ref len, ref tok, ref res))); break; case RFC1960Token.GreaterOrEqual: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerSequence(Asn1.LDAPFILTERGREATEROREQUAL, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, ParseValue(filter, ref pos, ref len, ref tok, ref res))); break; case RFC1960Token.LessOrEqual: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerSequence(Asn1.LDAPFILTERLESSOREQUAL, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, ParseValue(filter, ref pos, ref len, ref tok, ref res))); break; case RFC1960Token.Present: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerString(Asn1.LDAPFILTERPRESENT, attributename); break; default: throw new LdapException("expression expected at " + pos); } return(result); }
private static BerValue ParseSimple(String filter, ref Int32 pos, ref Int32 len, ref RFC1960Token tok, ref String res) { if (tok != RFC1960Token.Value) throw new LdapException("Attribute name expected at " + pos); String attributename = res; BerValue result; RFC1960Next(filter, ref pos, ref len, out tok, out res); switch (tok) { // <simple> ::= <attr> <filtertype> <value> // <filtertype> ::= <equal> | <approx> | <ge> | <le> // <equal> ::= '=' // <approx> ::= '~=' // <ge> ::= '>=' // <le> ::= '<=' case RFC1960Token.Equal: { RFC1960Next(filter, ref pos, ref len, out tok, out res); String currval = ParseValue(filter, ref pos, ref len, ref tok, ref res); if (tok != RFC1960Token.Any) { result = new BerSequence(Asn1.LDAPFILTEREQUALITYMATCH, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, currval)); break; } BerSequence items = new BerSequence(); items.Items.Add(new BerString(LDAPFILTERSUBSTRING_INIT, currval)); currval = null; while (tok == RFC1960Token.Any) { if (currval != null) { items.Items.Add(new BerString(LDAPFILTERSUBSTRING_ANY, currval)); } RFC1960Next(filter, ref pos, ref len, out tok, out res); currval = ParseValue(filter, ref pos, ref len, ref tok, ref res); } items.Items.Add(new BerString(LDAPFILTERSUBSTRING_FINAL, currval)); result = new BerSequence(Asn1.LDAPFILTERSUBSTRING, new BerString(OCTETSTRING, attributename), items); break; } // supports * too case RFC1960Token.AproxEqual: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerSequence(Asn1.LDAPFILTERAPPROXMATCH, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, ParseValue(filter, ref pos, ref len, ref tok, ref res))); break; case RFC1960Token.GreaterOrEqual: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerSequence(Asn1.LDAPFILTERGREATEROREQUAL, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, ParseValue(filter, ref pos, ref len, ref tok, ref res))); break; case RFC1960Token.LessOrEqual: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerSequence(Asn1.LDAPFILTERLESSOREQUAL, new BerString(OCTETSTRING, attributename), new BerString(OCTETSTRING, ParseValue(filter, ref pos, ref len, ref tok, ref res))); break; case RFC1960Token.Present: RFC1960Next(filter, ref pos, ref len, out tok, out res); result = new BerString(Asn1.LDAPFILTERPRESENT, attributename); break; default: throw new LdapException("expression expected at " + pos); } return result; }