public void When_Serialize_BindResponse_With_Error() { var operation = new BindResponse(); operation.Result = new LDAPResult { MatchedDN = new DEROctetString("administrator"), DiagnosticMessage = new DEROctetString("error"), ResultCode = new DEREnumerated <LDAPResultCodes> { Value = LDAPResultCodes.AuthMethodNotSupported } }; var ldapPacket = new LdapPacket { MessageId = new DERInteger(1), ProtocolOperation = new DERProtocolOperation { Operation = operation } }; var payload = ldapPacket.Serialize(); var deserializedLdapPacket = LdapPacket.Extract(payload); var ldapResult = operation.Result as LDAPResult; Assert.NotNull(deserializedLdapPacket); Assert.Equal("administrator", ldapResult.MatchedDN.Value); Assert.Equal("error", ldapResult.DiagnosticMessage.Value); Assert.Equal(LDAPResultCodes.AuthMethodNotSupported, ldapResult.ResultCode.Value); }
public async Task WhenExecuteSearchRequest(string dn, int messageId) { var payload = LdapPacketBuilder.NewSearchRequest(messageId, dn, SearchRequestScopes.WholeSubtree, SearchRequestDeferAliases.NeverDerefAliases, 10, 10, false, new List <string> { }, (opt) => { opt.SetPresentFilter("objectClass"); }).Serialize().ToArray(); await _stream.WriteAsync(payload); bool isSearchResultDone = false; int i = 0; while (!isSearchResultDone) { var data = new byte[5000]; await _stream.ReadAsync(data, 0, data.Length); var ldapPacket = LdapPacket.Extract(data.ToList()); var ldapPacketJSON = JObject.FromObject(ldapPacket); if (ldapPacket.ProtocolOperation.Operation is SearchResultDone) { isSearchResultDone = true; _scenarioContext.Set(ldapPacketJSON, "searchResultDone"); } else { _scenarioContext.Set(ldapPacketJSON, $"searchResultEntry-{i}"); } i++; } }
public void When_Serialize_SearchRequest() { var payload = LdapPacketBuilder.NewSearchRequest(1, "cn=system", SearchRequestScopes.BaseObject, SearchRequestDeferAliases.NeverDerefAliases, 10, 10, false, new List <string> { }, (opt) => { opt.SetEqualFilter("name", "value"); }).Serialize().ToList(); var ldapPacket = LdapPacket.Extract(payload); var searchRequest = ldapPacket.ProtocolOperation.Operation as SearchRequest; Assert.NotNull(searchRequest); }
private async Task Send(byte[] payload) { await _stream.WriteAsync(payload); var data = new byte[5000]; await _stream.ReadAsync(data, 0, data.Length); var ldapPacket = LdapPacket.Extract(data.ToList()); var ldapPacketJSON = JObject.FromObject(ldapPacket); _scenarioContext.Set(ldapPacketJSON, "ldapPacket"); }
public void When_Serialize_BindRequest() { var payload = LdapPacketBuilder.NewBindRequest(1, 3, "administrator", (opt) => { opt.SetSimpleAuthentication("password"); }).Serialize().ToList(); var ldapPacket = LdapPacket.Extract(payload); var bindRequest = ldapPacket.ProtocolOperation.Operation as BindRequest; var simpleAuthChoice = bindRequest.Authentication as SimpleAuthChoice; Assert.NotNull(ldapPacket); Assert.Equal(1, ldapPacket.MessageId.Value); Assert.Equal(3, bindRequest.Version.Value); Assert.Equal("administrator", bindRequest.Name.Value); Assert.Equal("password", simpleAuthChoice.Value.Value); }
public void When_Serialize_AddRequest() { var payload = LdapPacketBuilder.NewAddRequest(1, "administrator", (opt) => { opt.AddAttribute("objectClass", new List <string> { "inetOrgPerson" }); }).Serialize().ToList(); var ldapPacket = LdapPacket.Extract(payload); var addRequest = ldapPacket.ProtocolOperation.Operation as AddRequest; Assert.NotNull(ldapPacket); Assert.Equal(1, ldapPacket.MessageId.Value); Assert.True(addRequest.Attributes.Values.Count() == 1); Assert.Equal("objectClass", addRequest.Attributes.Values.First().Type.Value); }
private async Task HandleTcpClient(TcpClient client) { if (ClientConnected != null) { ClientConnected(this, EventArgs.Empty); } var stream = client.GetStream(); var ldapSession = new LdapSession(stream); _ldapSessionStoreLst.Add(ldapSession); bool isContinue = true; while (!_tokenSource.IsCancellationRequested && isContinue) { LdapPacket ldapRequest = null; try { byte[] buffer = new byte[1024]; stream.Read(buffer, 0, buffer.Length); ldapRequest = LdapPacket.Extract(buffer.ToList()); // Sometimes we received empty request. if (ldapRequest.Length == 0) { continue; } if (ldapSession.State == LdapSessionStates.Created && !(ldapRequest.ProtocolOperation.Operation is BindRequest)) { throw new LdapException(Global.NoLdapSession, LDAPResultCodes.OperationsError, string.Empty); } ldapSession.State = LdapSessionStates.Authenticated; var packetLst = await _ldapService.Handle(ldapRequest); foreach (var packet in packetLst) { var payload = packet.Serialize(); await stream.WriteAsync(payload.ToArray(), 0, payload.Count()); } } catch (LdapException ex) { _logger.LogError(ex, ex.Message); var res = BuildError(ldapRequest, ex).Serialize(); await stream.WriteAsync(res.ToArray(), 0, res.Count()); } catch (ObjectDisposedException) { isContinue = false; } catch (Exception ex) { _logger.LogError(ex, ex.ToString()); isContinue = false; client.Client.Disconnect(false); } } _ldapSessionStoreLst.Remove(ldapSession); if (ClientDisconnected != null) { ClientDisconnected(this, EventArgs.Empty); } }