-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLdapServer.cs
190 lines (164 loc) · 7.11 KB
/
LdapServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using Flexinets.Ldap.Core;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Flexinets.Ldap
{
public class LdapServer
{
private readonly TcpListener _server;
private readonly ILogger<LdapServer> _logger;
/// <summary>
/// Create a new server on endpoint
/// </summary>
/// <param name="localEndpoint"></param>
public LdapServer(IPEndPoint localEndpoint, ILogger<LdapServer> logger)
{
_server = new TcpListener(localEndpoint);
_logger = logger;
}
/// <summary>
/// Start listening for requests
/// </summary>
public void Start()
{
_server.Start();
var receiveTask = StartAcceptingClientsAsync();
}
/// <summary>
/// Stop listening
/// </summary>
public void Stop()
{
_server?.Stop();
}
/// <summary>
/// Start the loop used for accepting clients
/// </summary>
/// <returns></returns>
private async Task StartAcceptingClientsAsync()
{
while (_server.Server.IsBound)
{
try
{
var client = await _server.AcceptTcpClientAsync();
var task = Task.Factory.StartNew(() => HandleClient(client), TaskCreationOptions.LongRunning);
}
catch (ObjectDisposedException) { } // Thrown when server is stopped while still receiving. This can be safely ignored
catch (Exception ex)
{
_logger.LogCritical("Something went wrong accepting client", ex);
}
}
}
/// <summary>
/// Handle clients
/// </summary>
/// <param name="ar"></param>
private void HandleClient(TcpClient client)
{
try
{
_logger.LogCritical($"Connection from {client.Client.RemoteEndPoint}");
var isBound = false;
var stream = client.GetStream();
while (LdapPacket.TryParsePacket(stream, out var requestPacket))
{
LogPacket(requestPacket);
if (requestPacket.ChildAttributes.Any(o => o.LdapOperation == LdapOperation.BindRequest))
{
isBound = HandleBindRequest(stream, requestPacket);
}
if (isBound) // Only handle other requests if the client is bound, dont allow any anonymous searches
{
if (requestPacket.ChildAttributes.Any(o => o.LdapOperation == LdapOperation.SearchRequest))
{
HandleSearchRequest(stream, requestPacket);
}
}
}
_logger.LogDebug($"Connection closed to {client.Client.RemoteEndPoint}");
}
catch (IOException ioex)
{
_logger.LogWarning("oops", ioex);
}
catch (Exception ex)
{
_logger.LogError("Something went wrong", ex);
}
}
/// <summary>
/// Handle search requests
/// </summary>
/// <param name="searchRequest"></param>
/// <returns></returns>
private void HandleSearchRequest(NetworkStream stream, LdapPacket requestPacket)
{
var searchRequest = requestPacket.ChildAttributes.SingleOrDefault(o => o.LdapOperation == LdapOperation.SearchRequest);
var filter = searchRequest.ChildAttributes[6];
if ((LdapFilterChoice)filter.ContextType == LdapFilterChoice.equalityMatch && filter.ChildAttributes[0].GetValue<String>() == "sAMAccountName" && filter.ChildAttributes[1].GetValue<String>() == "testuser") // equalityMatch
{
var responseEntryPacket = new LdapPacket(requestPacket.MessageId);
var searchResultEntry = new LdapAttribute(LdapOperation.SearchResultEntry);
searchResultEntry.ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, "cn=testuser,cn=Users,dc=dev,dc=company,dc=com"));
searchResultEntry.ChildAttributes.Add(new LdapAttribute(UniversalDataType.Sequence));
responseEntryPacket.ChildAttributes.Add(searchResultEntry);
var responsEntryBytes = responseEntryPacket.GetBytes();
stream.Write(responsEntryBytes, 0, responsEntryBytes.Length);
}
var responseDonePacket = new LdapPacket(requestPacket.MessageId);
responseDonePacket.ChildAttributes.Add(new LdapResultAttribute(LdapOperation.SearchResultDone, LdapResult.success));
var responseDoneBytes = responseDonePacket.GetBytes();
stream.Write(responseDoneBytes, 0, responseDoneBytes.Length);
}
/// <summary>
/// Handle bindrequests
/// </summary>
/// <param name="bindrequest"></param>
private Boolean HandleBindRequest(Stream stream, LdapPacket requestPacket)
{
var bindrequest = requestPacket.ChildAttributes.SingleOrDefault(o => o.LdapOperation == LdapOperation.BindRequest);
var username = bindrequest.ChildAttributes[1].GetValue<String>();
var password = bindrequest.ChildAttributes[2].GetValue<String>();
var response = LdapResult.invalidCredentials;
if (username == "cn=bindUser,cn=Users,dc=dev,dc=company,dc=com" && password == "bindUserPassword"
|| username == "cn=user,dc=example,dc=com" && password == "123")
{
response = LdapResult.success;
}
var responsePacket = new LdapPacket(requestPacket.MessageId);
responsePacket.ChildAttributes.Add(new LdapResultAttribute(LdapOperation.BindResponse, response));
var responseBytes = responsePacket.GetBytes();
stream.Write(responseBytes, 0, responseBytes.Length);
return response == LdapResult.success;
}
/// <summary>
/// Dump the packet to log
/// </summary>
/// <param name="attribute"></param>
private void LogPacket(LdapAttribute attribute)
{
var sb = new StringBuilder();
RecurseAttributes(sb, attribute);
_logger.LogDebug($"Packet dump\n{sb}");
}
private void RecurseAttributes(StringBuilder sb, LdapAttribute attribute, Int32 depth = 1)
{
if (attribute != null)
{
sb.AppendLine($"{Utils.Repeat(">", depth)} {attribute.Class}:{attribute.DataType}{attribute.LdapOperation}{attribute.ContextType} - Type: {attribute.GetValue().GetType()} - {attribute.GetValue()}");
if (attribute.IsConstructed)
{
attribute.ChildAttributes.ForEach(o => RecurseAttributes(sb, o, depth + 1));
}
}
}
}
}