-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldap.cpp
More file actions
253 lines (212 loc) · 6.08 KB
/
ldap.cpp
File metadata and controls
253 lines (212 loc) · 6.08 KB
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
#include <assert.h>
#include <fstream>
#include <iostream>
#include <ldap.h>
#include<sstream>
#include<vector>
#include "host.h"
#include "ldap.h"
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
using namespace std;
// Standard constructor.
Ldap::Ldap()
{
properties = readProperties();
host = new Host(properties);
initialize();
set_options();
bind();
}
// Host application copy constructor.
Ldap::Ldap(const Ldap & l)
{
host = l.host;
}
// Destructor.
Ldap::~Ldap()
{
unbind();
delete host;
host = 0;
}
void Ldap::initialize()
{
ldap_initialize(&ld, host->getUrl().c_str());
}
void Ldap::set_options()
{
const int option = LDAP_VERSION3;
ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &option);
}
void Ldap::bind()
{
BerValue credentials;
credentials.bv_val = (char*) malloc(host->getPassword().size() * sizeof(char));
host->getPassword().copy(credentials.bv_val, string::npos);
credentials.bv_len = host->getPassword().size();
int result = ldap_sasl_bind_s(ld,
host->getBindDn().c_str(),
LDAP_SASL_SIMPLE,
&credentials,
nullptr, nullptr, nullptr);
if (result != LDAP_SUCCESS) {
cerr << "bind error: " << ldap_err2string(result) << endl;
exit(EXIT_FAILURE);
}
}
void Ldap::unbind()
{
ldap_unbind(ld);
}
void Ldap::search(const string value)
{
LDAPMessage * answer = searchp(value);
count_entries(answer);
print(answer);
ldap_msgfree(answer);
}
vector<char*> Ldap::attributes()
{
vector<char*> vc;
string attrs = trim(properties["ldapuh.attributes"]);
if (attrs.size() > 0) {
vector<string> vs;
stringstream s_stream(attrs);
while (s_stream.good()) {
string substr;
getline(s_stream, substr, ',');
vs.push_back(substr);
}
transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);
}
vc.push_back(NULL);
return vc;
}
LDAPMessage * Ldap::searchp(const string value)
{
const string filter = "(|(uid=" + value + ")"
+ "(mail=" + value + ")"
+ "(uhuuid=" + value + "))";
vector<char*> ats = attributes();
char **attry = ats.data();
const int attrsonly = 0;
LDAPMessage *res;
int result = ldap_search_s(ld,
host->getBase().c_str(),
LDAP_SCOPE_SUBTREE,
filter.c_str(),
attry,
attrsonly,
&res);
for (size_t i = 0; i < ats.size(); i++) {
if (ats[i] != NULL) {
delete [] ats[i];
}
}
if (result != LDAP_SUCCESS) {
cerr << "ldap_search_s: " << ldap_err2string(result) << endl;
exit(EXIT_FAILURE);
}
return res;
}
void Ldap::count_entries(LDAPMessage *answer)
{
int entries_found = ldap_count_entries(ld, answer);
if (entries_found == 0) {
exit(EXIT_FAILURE);
}
}
void Ldap::print(LDAPMessage *answer)
{
BerElement *ber;
char *attribute = "";
char **values;
// Cycle through all objects returned with our search.
for (LDAPMessage *entry = ldap_first_entry(ld, answer);
entry != NULL;
entry = ldap_next_entry(ld, entry)) {
// Cycle through all returned attributes.
for (attribute = ldap_first_attribute(ld, entry, &ber);
attribute != NULL;
attribute = ldap_next_attribute(ld, entry, ber)) {
if ((values = ldap_get_values(ld, entry, attribute)) != NULL) {
// Cycle through all values returned for this attribute.
for (int i = 0; values[i] != NULL; i++) {
cout << attribute << ": " << values[i] << endl;
}
ldap_value_free(values);
}
}
}
cout << ">------------------------------------------------------------<";
cout << endl;
}
string Ldap::propertiesPath()
{
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
const char *username = pw->pw_name;
string propertiesPath = string(homedir);
propertiesPath.append("/.");
propertiesPath.append(username);
propertiesPath.append("-conf");
propertiesPath.append("/ldapuh.properties");
return propertiesPath;
}
map<string, string> Ldap::readProperties()
{
map<string, string> propertyMap;
string filename = propertiesPath();
vector<string> lines = readFile(filename);
for (unsigned int i = 0; i < lines.size(); i++) {
string str(lines[i]);
string::size_type lastPos = str.size();
// Find the first '=' character, and then
// separate the key and value from each other.
string::size_type pos = str.find_first_of('=', 0);
if (string::npos != pos || string::npos != lastPos) {
const string key = trim(str.substr(0, pos));
string value = trim(str.substr(pos + 1, lastPos));
propertyMap[key.data()] = value;
}
}
return propertyMap;
}
vector<string> Ldap::readFile(string filename)
{
ifstream inFile(filename.data(), ios::in);
if (!inFile) {
cerr << "File '" << filename << "' could not be opened.\n";
exit(1);
}
vector<string> lines;
string line;
while (inFile && !inFile.eof()) {
getline(inFile, line);
line = trim(line);
int len = line.size();
if (len > 1) {
lines.push_back(line.substr(0, line.size()));
}
}
inFile.close();
return lines;
}
const string Ldap::trim(const string& s)
{
if (s.length() == 0)
return s;
int f = s.find_first_not_of(" \t"); // Find first char.
int e = s.find_last_not_of(" \t"); // Find ending char.
if (e == -1)
return ""; // Didn't find any chars.
return string(s, f, e - f + 1); // Trim off spaces.
}
char * Ldap::convert(const string & s)
{
char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
}