private BerValue ReadBerValue() { Int32 lCode = fCurrentConnection.ReadByte(); if (lCode < 0) { throw new ConnectionClosedException(); } Int32 lLength = fCurrentConnection.ReadByte(); if (lLength < 0) { throw new ConnectionClosedException(); } if (lLength >= 0x80) { Byte[] lBuffer = new Byte[lLength & ~0x80]; fCurrentConnection.Read(lBuffer, 0, lBuffer.Length); lLength = 0; for (Int32 i = 0; i < lBuffer.Length; i++) { lLength = lLength << 8 | lBuffer[i]; } } Byte[] lData = new Byte[lLength]; fCurrentConnection.Read(lData, 0, lData.Length); return(BerValue.Read(lData, lLength, (Byte)lCode)); }
protected override void IntRead(BinaryReader reader, Byte code, Int32 length) { this.TypeCodeTag = code; Byte[] lData = reader.Read(length); this.fItems.RemoveAll(); BinaryReader lReader = new BinaryReader(new MemoryStream(lData, false)); while (lReader.PeekChar() >= 0) { this.fItems.Add(BerValue.Read(lReader)); } }
internal static BerValue ParseFilter(String filter) { filter += "\0\0\0\0"; Int32 len = 0; Int32 pos = 0; String res; RFC1960Token tok; RFC1960Next(filter, ref pos, ref len, out tok, out res); BerValue val = ParseFilter(filter, ref pos, ref len, ref tok, ref res); if (tok != RFC1960Token.EOF) { throw new LdapException("EOF expected at " + pos); } return(val); }
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); } }
private Response ReadResponse() { BerValue lValue = ReadBerValue(); if (lValue != null && lValue.Type == BerType.Sequence && ((BerSequence)lValue).Items.Count >= 2 && ((BerSequence)lValue).Items[0].Type == BerType.Integer) { Response lResponse = new Response(); lResponse.SequenceId = ((BerInteger)((BerSequence)lValue).Items[0]).Value; BerSequence lSubValue = ((BerSequence)lValue).Items[1] as BerSequence; if (lSubValue == null) { return(null); } lResponse.TypeCode = lSubValue.TypeCodeTag; if (lSubValue.Items.Count == 0) { return(null); } if (lResponse.TypeCode == Asn1.LDAPSEARCHENTRY) { lResponse.RestData = ((List <BerValue>)lSubValue.Items).ToArray(); } else { if (!(lSubValue.Items[0] is BerInteger)) { return(null); // Int32 or sequence } lResponse.Code = ((BerInteger)lSubValue.Items[0]).Value; if (lSubValue.Items.Count > 1 && lSubValue.Items[1].Type == BerType.String) { lResponse.DN = ((BerString)lSubValue.Items[1]).Value; } if (lSubValue.Items.Count > 2 && lSubValue.Items[2].Type == BerType.String) { lResponse.Result = ((BerString)lSubValue.Items[2]).Value; } if (lSubValue.Items.Count > 3 && lResponse.Code == 10) // 10 = referers { lResponse.Referers = lSubValue.Items[3] as BerSequence; } else if (lSubValue.Items.Count > 3) { lResponse.RestData = new BerValue[lSubValue.Items.Count - 3]; for (Int32 i = 0; i < lResponse.RestData.Length; i++) { lResponse.RestData[i] = lSubValue.Items[i + 3]; } } } return(lResponse); } return(null); // crap on the line }