-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
277 lines (254 loc) · 10.3 KB
/
main.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <ctime>
#include <iostream>
#include <string>
#include <thread>
#include <asio.hpp>
#include <cctype>
#include <utility>
#include <set>
#include <yaml-cpp/yaml.h>
#include <pthread.h>
#include "loguru.hpp"
#include "exceptions.h"
#include "ldapproto.h"
#include "storage.h"
#include "passwords.h"
using asio::ip::tcp;
YAML::Node config;
void sendResponse(tcp::socket& sock, uint64_t messageId, Ber::Packet response) {
Ber::Packet envelope(
Ber::Type::Constructed, Ber::Class::Universal, Ber::Tag::Sequence);
envelope.appendChild(Ber::Packet(Ber::Tag::Integer, messageId));
envelope.appendChild(response);
std::vector<uint8_t> bytes;
bytes.reserve(envelope.length());
envelope.copyBytes(bytes);
sock.send(asio::buffer(bytes));
}
void session_thread(tcp::socket sock) {
std::stringstream threadName;
threadName << sock.remote_endpoint();
loguru::set_thread_name(threadName.str().c_str());
auto db = Storage::Mongo::MongoBackend{
"mongodb://localhost",
"directory",
"rootdn",
"dc=mongodb,dc=com"
};
bool noAuthentication = false;
// Put this into its own scope so the YAML nodes get cleaned up.
{
auto check = config["noAuthentication"];
if (check && check.as<bool>() == true)
noAuthentication = true;
}
bool userBound = false;
std::string userBoundDN;
for (;;)
{
std::vector<uint8_t> header(2);;
size_t length;
asio::error_code error;
length = sock.read_some(asio::buffer(header), error);
if (error) {
if (error == asio::error::eof)
break;
else
throw asio::system_error(error);
}
if (length != header.size()) {
LOG_F(ERROR, "Client sent malformed BER header");
break;
}
std::vector<uint8_t> reqBuffer;
reqBuffer.reserve(1024);
if ((header[1] & 128) != 0) {
header[1] -= 128;
reqBuffer.resize(header[1]);
length = sock.read_some(asio::buffer(reqBuffer), error);
if (length != reqBuffer.size()) {
LOG_F(ERROR, "Client sent mal-formed BER size header");
break;
}
auto decodedReqSize = Ber::decodeInteger(reqBuffer.cbegin(), reqBuffer.cend());
reqBuffer.resize(decodedReqSize, 0);
} else {
reqBuffer.resize(header[1], 0);
}
length = sock.read_some(asio::buffer(reqBuffer), error);
if (length != reqBuffer.size()) {
LOG_F(ERROR, "Client sent fewer bytes than expected");
break;
}
auto ber = Ber::Packet::decode(header[0], reqBuffer);
auto messageId = static_cast<uint64_t>(ber.children[0]);
auto messageType = Ldap::MessageTag { static_cast<Ldap::MessageTag>(ber.children[1].tag) };
Ldap::MessageTag errorResponseType;
switch (messageType) {
case Ldap::MessageTag::SearchRequest:
errorResponseType = Ldap::MessageTag::SearchResDone;
break;
default:
errorResponseType = static_cast<Ldap::MessageTag>(static_cast<uint8_t>(messageType) + 1);
break;
}
try {
if (messageType == Ldap::MessageTag::BindRequest) {
Ldap::Bind::Request bindReq(ber.children[1]);
if (bindReq.type == Ldap::Bind::Request::Type::Sasl) {
// TODO SUPPORT SASL BINDS!
throw Ldap::Exception(Ldap::ErrorCode::authMethodNotSupported);
}
bool passOkay = false;
if (noAuthentication) {
LOG_S(INFO)
<< "Authentication is disabled, sending bogus bind for "
<< bindReq.dn;
passOkay = true;
} else {
LOG_S(INFO) << "Authenticating " << bindReq.dn;
try {
auto entry = db.findEntry(bindReq.dn);
for (const auto& pass: entry->attributes.at("userPassword")) {
passOkay = Password::checkPassword(bindReq.simple, pass);
if (passOkay)
break;
}
} catch (Ldap::Exception e) {
LOG_S(ERROR) << "Error during authentication " << e.what();
if (e == Ldap::ErrorCode::noSuchObject) {
throw Ldap::Exception(Ldap::ErrorCode::invalidCredentials);
}
throw;
}
}
Ldap::ErrorCode respCode;
if (passOkay) {
respCode = Ldap::ErrorCode::success;
userBound = true;
userBoundDN = bindReq.dn;
} else {
respCode = Ldap::ErrorCode::invalidCredentials;
userBound = false;
userBoundDN = "";
}
Ldap::Bind::Response bindResp(Ldap::buildLdapResult(respCode, bindReq.dn, "",
Ldap::MessageTag::BindResponse));
sendResponse(sock, messageId, bindResp.response);
}
else if (messageType == Ldap::MessageTag::SearchRequest) {
Ldap::Search::Request searchReq(ber.children[1]);
auto cursor = db.findEntries(searchReq);
for (const auto& entry: *cursor) {
sendResponse(sock, messageId, Ldap::Search::generateResult(entry));
}
sendResponse(sock, messageId,
Ldap::buildLdapResult(Ldap::ErrorCode::success,
"", "", Ldap::MessageTag::SearchResDone));
}
else if (messageType == Ldap::MessageTag::AddRequest) {
Ldap::Entry entry = Ldap::Add::parseRequest(ber.children[1]);
db.saveEntry(entry, true);
sendResponse(sock, messageId,
Ldap::buildLdapResult(Ldap::ErrorCode::success,
"", "", Ldap::MessageTag::AddResponse));
}
else if (messageType == Ldap::MessageTag::ModifyRequest) {
Ldap::Modify::Request req(ber.children[1]);
auto entry = db.findEntry(req.dn);
if (entry == nullptr) {
throw Ldap::Exception(Ldap::ErrorCode::noSuchObject);
}
for (const auto& mod: req.mods) {
using ModType = Ldap::Modify::Modification::Type;
switch(mod.type) {
case ModType::Add:
for (const auto& v: mod.values) {
entry->appendValue(mod.name, v);
}
break;
case ModType::Delete:
if (mod.values.size() == 0) {
if (entry->attributes.erase(mod.name) == 0) {
throw Ldap::Exception(Ldap::ErrorCode::noSuchAttribute);
}
} else {
try {
auto curVals = entry->attributes.at(mod.name);
std::set<std::string> finalVals(curVals.begin(), curVals.end());
for (const auto& v: mod.values) {
if (finalVals.erase(v) == 0) {
throw Ldap::Exception(Ldap::ErrorCode::noSuchAttribute);
}
}
curVals.clear();
std::copy(finalVals.begin(), finalVals.end(),
std::back_inserter(curVals));
} catch(std::out_of_range) {
throw Ldap::Exception(Ldap::ErrorCode::noSuchAttribute);
}
}
break;
case ModType::Replace:
if (mod.values.size() == 0) {
entry->attributes.erase(mod.name);
} else {
entry->attributes[mod.name] = mod.values;
}
break;
}
}
db.saveEntry(*entry, false);
sendResponse(sock, messageId,
Ldap::buildLdapResult(Ldap::ErrorCode::success,
"", "", Ldap::MessageTag::ModifyResponse));
}
else if (messageType == Ldap::MessageTag::DelRequest) {
std::string dn = Ldap::Delete::parseRequest(ber.children[1]);
db.deleteEntry(dn);
sendResponse(sock, messageId,
Ldap::buildLdapResult(Ldap::ErrorCode::success,
"", "", Ldap::MessageTag::DelResponse));
}
} catch (const Ldap::Exception& e) {
auto resPacket = Ldap::buildLdapResult(e, "", e.what(), errorResponseType);
sendResponse(sock, messageId, resPacket);
break;
} catch (const std::exception& e) {
auto resPacket = Ldap::buildLdapResult(Ldap::ErrorCode::other,
"", e.what(), errorResponseType);
sendResponse(sock, messageId, resPacket);
break;
} catch (...) {
auto resPacket = Ldap::buildLdapResult(Ldap::ErrorCode::other,
"", "Unknown error occurred", errorResponseType);
sendResponse(sock, messageId, resPacket);
break;
}
}
}
int main(int argc, char** argv)
{
loguru::init(argc, argv);
try
{
config = YAML::LoadFile(argv[1]);
asio::io_service io_service;
int port = 3890;
if (config["port"]) {
port = config["port"].as<int>();
}
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);
std::thread(session_thread, std::move(socket)).detach();
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}