-
Notifications
You must be signed in to change notification settings - Fork 17
/
connection.cc
356 lines (312 loc) · 9.58 KB
/
connection.cc
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string>
#include <vector>
#include "ldap++.h"
#include "ldap_compat.h"
#include <ldap.h>
namespace ldap_client
{
/**
* Sets the system-wide LDAP debug level.
*
* @param newlevel New debug level (see the LDAP manual).
*/
void LDAPSetDebuglevel(int newlevel)
{
int rc = ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &newlevel);
if (rc)
LDAPErrCode2Exception(NULL, rc);
rc = ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &newlevel);
if (rc)
LDAPErrCode2Exception(NULL, rc);
}
/**
* Set the LDAP CA certificate file.
*
* @param path Path to the CA certificate file the LDAP server certificate
* has been signed with.
*/
void LDAPSetCACert(std::string path)
{
int rc;
rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, path.c_str());
if (rc)
LDAPErrCode2Exception(NULL, rc);
}
/**
* Establish a new LDAP connection to the given URI.
*
* @param uri URI of the LDAP server.
* @param version LDAP version to use (LDAP_VERSION2 or LDAP_VERSION3).
*/
LDAPConnection::LDAPConnection(std::string uri, int version)
{
int rc = ldap_initialize(&_ldap, uri.c_str());
if (rc)
LDAPErrCode2Exception(_ldap, rc);
SetVersion(version);
_size_limit = -1;
}
/**
* Disconnect from the LDAP server, also unbind.
*/
LDAPConnection::~LDAPConnection()
{
ldap_unbind_ext_s(_ldap, 0, 0);
}
/**
* Retrieve the most recently occurred error message.
*
* @return String containing the most recent error message.
*/
std::string LDAPConnection::GetLastError()
{
char *str;
int rc;
rc = ldap_get_option(_ldap, LDAP_OPT_ERROR_STRING, &str);
if (rc)
LDAPErrCode2Exception(_ldap, rc);
return std::string(str);
}
/**
* Set the LDAP version used in the connection.
*
* @param newversion New LDAP version (LDAP_VERSION2 or LDAP_VERSION3).
* @throws LDAPException Invalid LDAP version or unable to set.
*/
void LDAPConnection::SetVersion(int newversion)
{
int rc;
if (newversion != LDAP_VERSION2 && newversion != LDAP_VERSION3)
throw new LDAPErrParamError("Unsupported LDAP version");
rc = ldap_set_option(_ldap, LDAP_OPT_PROTOCOL_VERSION, &newversion);
if (rc)
LDAPErrCode2Exception(_ldap, rc);
}
/**
* Perform LDAP authentication using a simple BIND.
* You should use SASLBind instead.
*
* @param user LDAP user name (typically a DN).
* @param password LDAP password as a string.
* @throws LDAPException Unable to perform bind.
*/
void LDAPConnection::SimpleBind(std::string user, std::string password)
{
struct berval passwd;
int rc;
passwd.bv_val = strdup(user.c_str());
passwd.bv_len = user.length();
rc = ldap_sasl_bind_s(_ldap, user.c_str(), LDAP_SASL_SIMPLE, &passwd,
0, 0, 0);
free(passwd.bv_val);
if (rc)
LDAPErrCode2Exception(_ldap, rc);
}
/**
* Perform SASL bind. Currently, essentially the same as simple bind.
*
* @param user LDAP user name (typically a DN).
* @param password LDAP password as a string.
* @throws LDAPException Unable to perform bind.
*/
void LDAPConnection::SASLBind(std::string user, std::string password)
{
LDAPControl **sctrlsp = NULL, **cctrlsp = NULL;
struct berval passwd;
int rc;
passwd.bv_val = ber_strdup(password.c_str());
passwd.bv_len = strlen(passwd.bv_val);
rc = ldap_sasl_bind_s(_ldap, user.c_str(), LDAP_SASL_SIMPLE,
&passwd, sctrlsp, cctrlsp, NULL);
if (passwd.bv_val)
ber_memfree(passwd.bv_val);
if (rc)
LDAPErrCode2Exception(_ldap, rc);
}
/**
* Set the number of records to be returned in an LDAP query.
*
* @param newlimit Number of records to return in subsequent calls to Search.
*/
void LDAPConnection::SetResultSizeLimit(int limit)
{
_size_limit = limit;
}
/**
* Search for LDAP records matching a given filter.
*
* @param base Search base to start looking from.
* @param scope LDAP search scope (e.g. ONE, SUB, etc.)
* @param filter Filter string (e.g. attribute=value).
* @param attrs Vector of attributes to fetch in the record.
* @param timeout Number of milliseconds to wait for an answer.
* @return LDAPResult object containing all LDAP results returned by
* the query.
* @throws LDAPException An error occurred processing the search query.
*/
LDAPResult* LDAPConnection::Search(const std::string base, int scope,
const std::string filter, const std::vector<std::string> attrs,
long timeout)
{
std::vector<char*> attrlist;
std::vector<LDAPMessage*> msgs;
std::vector<LDAPControl*> ctrls;
LDAPControl* pageCtrl, *ctrl;
LDAPControl** returnedCtrl;
berval* cookie = new berval;
timeval tv;
LDAPMessage *msg;
int rc, i = 0, errCode = 0, numResults = 0, count = 0;
cookie->bv_len = 0;
cookie->bv_val = 0;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
for (i = 0; i < attrs.size(); i++)
attrlist.push_back(const_cast<char*>(attrs[i].c_str()));
attrlist.push_back(0);
ctrl = new LDAPControl;
ctrl->ldctl_oid = (char*) LDAP_CONTROL_PAGEDRESULTS;
ctrl->ldctl_iscritical = 0;
ctrls.push_back(ctrl);
ctrls.push_back(0);
do {
rc = ldap_create_page_control_value(_ldap, _size_limit,
cookie, &ctrl->ldctl_value);
if (rc)
LDAPErrCode2Exception(_ldap, rc);
if (cookie && cookie->bv_val != NULL)
{
ber_memfree(cookie->bv_val);
cookie->bv_val = 0;
cookie->bv_len = 0;
}
rc = ldap_search_ext_s(_ldap, base.c_str(), scope,
filter.c_str(), &attrlist[0], 0, &ctrls[0], 0, &tv, 0, &msg);
if (rc && rc != LDAP_PARTIAL_RESULTS &&
rc != LDAP_ADMINLIMIT_EXCEEDED &&
rc != LDAP_SIZELIMIT_EXCEEDED)
LDAPErrCode2Exception(_ldap, rc);
msgs.push_back(msg);
rc = ldap_parse_result(_ldap, msg, &errCode, 0, 0, 0,
&returnedCtrl, 0);
if (rc)
LDAPErrCode2Exception(_ldap, rc);
pageCtrl = ldap_control_find(LDAP_CONTROL_PAGEDRESULTS,
returnedCtrl, 0);
if (!pageCtrl)
{
ldap_controls_free(returnedCtrl);
break;
}
if (cookie)
{
if (cookie->bv_val)
ber_memfree(cookie->bv_val);
cookie->bv_val = 0;
cookie->bv_len = 0;
}
rc = ldap_parse_pageresponse_control(_ldap, pageCtrl,
&i, cookie);
if (rc)
LDAPErrCode2Exception(_ldap, rc);
ldap_controls_free(returnedCtrl);
count = ldap_count_entries(_ldap, msg);
numResults += count;
} while (cookie->bv_len > 0 && strlen(cookie->bv_val) > 0);
if (cookie)
{
if (cookie->bv_val)
ber_memfree(cookie->bv_val);
cookie->bv_val = 0;
cookie->bv_len = 0;
delete cookie;
}
return new LDAPResult(this, msgs);
}
/**
* Search for LDAP records matching a given filter. A default timeout of
* 30 seconds is applied.
*
* @param base Search base to start looking from.
* @param scope LDAP search scope (e.g. ONE, SUB, etc.)
* @param filter Filter string (e.g. attribute=value).
* @param attrs Vector of attributes to fetch in the record.
* @return LDAPResult object containing all LDAP results returned by
* the query.
* @throws LDAPException An error occurred processing the search query.
*/
LDAPResult* LDAPConnection::Search(const std::string base, int scope,
const std::string filter, const std::vector<std::string> attrs)
{
return Search(base, scope, filter, attrs, 30000);
}
/**
* Search for LDAP records matching a given filter. All attributes of any
* matching record are fetched.
*
* @param base Search base to start looking from.
* @param scope LDAP search scope (e.g. ONE, SUB, etc.)
* @param filter Filter string (e.g. attribute=value).
* @param timeout Number of milliseconds to wait for a result.
* @return LDAPResult object containing all LDAP results returned by
* the query.
* @throws LDAPException An error occurred processing the search query.
*/
LDAPResult* LDAPConnection::Search(const std::string base, int scope,
const std::string filter, long timeout)
{
return Search(base, scope, filter, kLdapFilterAll, timeout);
}
/**
* Search for LDAP records matching a given filter. A default timeout of
* 30 seconds is applied. All attributes of any matching record are fetched.
*
* @param base Search base to start looking from.
* @param scope LDAP search scope (e.g. ONE, SUB, etc.)
* @param filter Filter string (e.g. attribute=value).
* @return LDAPResult object containing all LDAP results returned by
* the query.
* @throws LDAPException An error occurred processing the search query.
*/
LDAPResult* LDAPConnection::Search(const std::string base, int scope,
const std::string filter)
{
return Search(base, scope, filter, kLdapFilterAll, 30000);
}
/**
* Search for LDAP records matching a given filter. All attributes of any
* matching record are fetched. A subtree search is performed.
*
* @param base Search base to start looking from.
* @param filter Filter string (e.g. attribute=value).
* @param timeout Number of milliseconds to wait for a result.
* @return LDAPResult object containing all LDAP results returned by
* the query.
* @throws LDAPException An error occurred processing the search query.
*/
LDAPResult* LDAPConnection::Search(const std::string base,
const std::string filter, long timeout)
{
return Search(base, LDAP_SCOPE_SUBTREE, filter, kLdapFilterAll,
timeout);
}
/**
* Search for LDAP records matching a given filter. A default timeout of
* 30 seconds is applied. All attributes of any matching record are
* fetched. A subtree search is performed.
*
* @param base Search base to start looking from.
* @param filter Filter string (e.g. attribute=value).
* @return LDAPResult object containing all LDAP results returned by
* the query.
* @throws LDAPException An error occurred processing the search query.
*/
LDAPResult* LDAPConnection::Search(const std::string base,
const std::string filter)
{
return Search(base, LDAP_SCOPE_SUBTREE, filter, kLdapFilterAll, 30000);
}
}