-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap.c
410 lines (364 loc) · 11.8 KB
/
ldap.c
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
Copyright (C) 2012 Daniel Hazelbaker
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "ldap.h"
#include "common.h"
#include "conf.h"
#include "keys.h"
#include "utils.h"
#include <lber.h>
#include <ldap.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BIND_RETRIES 5
//
// Create a new connection to the LDAP server, optionally bind to the
// server using the configured credentials.
//
LDAP *ldap_connect(int bind) {
struct berval cred;
const char *uri, *binddn = NULL, *bindpw = NULL;
LDAP *ldap = NULL;
int i, version = 3, result;
//
// Get the config options we need to connect to the LDAP server.
//
uri = conf_find("ldap_uri");
if (uri == NULL)
return NULL;
//
// Get the config options we need to bind to the LDAP server.
//
if (bind) {
binddn = conf_find("ldap_binddn");
bindpw = conf_find("ldap_bindpw");
if (binddn == NULL || bindpw == NULL)
return NULL;
}
//
// Connect to the LDAP server.
//
if (ldap_initialize(&ldap, uri) != LDAP_SUCCESS)
return NULL;
//
// Set protocol version 3.
//
if (ldap_set_option(ldap, LDAP_OPT_PROTOCOL_VERSION, &version) !=
LDAP_SUCCESS) {
ldap_unbind_ext_s(ldap, NULL, NULL);
return NULL;
}
//
// Try to bind if requested.
//
if (bind) {
cred.bv_val = malloc(strlen(bindpw) + 1);
strcpy(cred.bv_val, bindpw);
cred.bv_len = strlen(bindpw);
for (i = 0; i < BIND_RETRIES; i++) {
result = ldap_sasl_bind_s(ldap, binddn, LDAP_SASL_SIMPLE, &cred,
NULL, NULL, NULL);
if (result == LDAP_SUCCESS)
break;
printf("Failed to bind to LDAP server, error = %d\r\n", result);
sleep(2);
}
free(cred.bv_val);
//
// Check for failure to bind.
//
if (i == BIND_RETRIES) {
ldap_unbind_ext_s(ldap, NULL, NULL);
return NULL;
}
}
return ldap;
}
//
// Disconnect from the LDAP server.
//
void ldap_disconnect(LDAP *ldap) {
if (ldap != NULL)
ldap_unbind_ext_s(ldap, NULL, NULL);
}
//
// Retrieve a list of replica servers from the LDAP directory. The returned
// string is in XML format and should be free'd by the caller.
//
char *ldap_replicalist() {
struct berval **attrvalues;
LDAPMessage *ldapresults = NULL, *ldapresult = NULL;
const char *basedn;
char *xml = NULL, *attrlist[2];
LDAP *ldap = NULL;
int result;
//
// Get the config options we need to search the LDAP server.
//
basedn = conf_find("ldap_basedn");
if (basedn == NULL)
return NULL;
//
// Get an LDAP connection.
//
if ((ldap = ldap_connect(0)) == NULL)
return NULL;
//
// We want only a single attribute.
//
attrlist[0] = "apple-password-server-list";
attrlist[1] = NULL;
//
// Search the LDAP database for the cn=passwordserver record.
//
result =
ldap_search_ext_s(ldap, basedn, LDAP_SCOPE_SUBTREE, "cn=passwordserver",
attrlist, 0, NULL, NULL, NULL, 1, &ldapresults);
if (result != LDAP_SUCCESS) {
ldap_disconnect(ldap);
return NULL;
}
//
// Get the first entry. If no results were found this will return NULL.
//
if ((ldapresult = ldap_first_entry(ldap, ldapresults)) == NULL) {
ldap_msgfree(ldapresults);
ldap_disconnect(ldap);
return NULL;
}
//
// Try to retrieve the results from the search.
//
attrvalues =
ldap_get_values_len(ldap, ldapresult, "apple-password-server-list");
if (attrvalues == NULL || attrvalues[0] == NULL) {
ldap_msgfree(ldapresults);
ldap_disconnect(ldap);
return NULL;
}
//
// Allocate enough space for the result string and store it.
//
xml = (char *)malloc(attrvalues[0]->bv_len + 1);
memcpy(xml, attrvalues[0]->bv_val, attrvalues[0]->bv_len);
xml[attrvalues[0]->bv_len] = '\0';
//
// Close the LDAP connection.
//
ldap_msgfree(ldapresults);
ldap_disconnect(ldap);
return xml;
}
//
// Search the LDAP directory for any records that need an authAuthority
// attribute. If the force parameter is set then all records that have
// an authAuthority attribute will be deleted and updated.
//
// Returns 0 on success or a negative number to indicate a fatal error
// that prevented us from doing anything. A positive number is returned
// to indicate how many records had errors of some kind.
//
int ldap_updateAuthority(int force) {
struct berval **attrvalues;
LDAPMessage *ldapresults = NULL, *ldapresult = NULL;
const char *basedn, *query;
LDAPMod modOp, *modUser[2] = {&modOp, NULL};
char *attrlist[4], *dn, *authAuthority, *uid, *userPassword, *modValues[2];
LDAP *ldap = NULL;
int result, errors = 0, len;
//
// Get the config options we need to search the LDAP server.
//
basedn = conf_find("ldap_basedn");
if (basedn == NULL) {
#ifdef DEBUG
printf("No search base configured.\r\n");
#endif
return -1;
}
//
// Get an LDAP connection.
//
if ((ldap = ldap_connect(1)) == NULL) {
#ifdef DEBUG
printf("Could not connect to LDAP server.\r\n");
#endif
return -1;
}
//
// We want only a single attribute.
//
attrlist[0] = "uid";
attrlist[1] = "userPassword";
attrlist[2] = NULL;
attrlist[3] = NULL;
//
// If we are forcing an update then just search for any person record,
// otherwise search for any person record that does not have a
// authAuthority.
//
if (force)
query = "(&(objectClass=person)(uid=*))";
else
query = "(&(objectClass=person)(uid=*)(!(authAuthority=*)))";
//
// Search the LDAP database for the wanted records.
// Only take the first 1,000 records. If you have more, uhh, too bad?
//
result =
ldap_search_ext_s(ldap, basedn, LDAP_SCOPE_SUBTREE, query, attrlist, 0,
NULL, NULL, NULL, 1000, &ldapresults);
if (result != LDAP_SUCCESS) {
#ifdef DEBUG
printf("LDAP search request failed: %s.\r\n", ldap_err2string(result));
#endif
ldap_disconnect(ldap);
return -1;
}
//
// Go through each returned person record and process it accordingly.
//
for (ldapresult = ldap_first_entry(ldap, ldapresults); ldapresult != NULL;
ldapresult = ldap_next_entry(ldap, ldapresult)) {
//
// Mark the fields as unknown to begin with.
//
uid = NULL;
userPassword = NULL;
dn = ldap_get_dn(ldap, ldapresult);
#ifdef DEBUG
printf("Processing record %s\r\n", (dn ? dn : ""));
#endif
//
// Retrieve the first uid attribute.
//
attrvalues = ldap_get_values_len(ldap, ldapresult, "uid");
if (attrvalues != NULL && attrvalues[0] != NULL) {
uid = malloc(attrvalues[0]->bv_len + 1);
memcpy(uid, attrvalues[0]->bv_val, attrvalues[0]->bv_len);
uid[attrvalues[0]->bv_len] = '\0';
}
//
// Retrieve the first userPassword attribute.
//
attrvalues = ldap_get_values_len(ldap, ldapresult, "userPassword");
if (attrvalues != NULL && attrvalues[0] != NULL) {
userPassword = malloc(attrvalues[0]->bv_len + 1);
memcpy(userPassword, attrvalues[0]->bv_val, attrvalues[0]->bv_len);
userPassword[attrvalues[0]->bv_len] = '\0';
}
//
// Check for missing values.
//
if (uid == NULL || userPassword == NULL) {
#ifdef DEBUG
printf(
"Record %s has either no uid or no userPassword attribute.\r\n",
dn);
#endif
ldap_memfree(dn);
free(uid);
free(userPassword);
errors += 1;
continue;
}
//
// Generate a proper authAuthority and set it.
//
len = strlen(publicKeyThumbprint) + strlen(uid) + 64;
authAuthority = malloc(len);
snprintf(authAuthority, len, ";ApplePasswordServer;%s,%s:%s", uid,
publicKeyThumbprint, myAddress);
//
// Run an LDAP modification request to remove any existing
// authAuthority attribute if we are in force mode.
//
result = LDAP_SUCCESS;
if (force) {
modOp.mod_op = LDAP_MOD_DELETE;
modOp.mod_type = "authAuthority";
modOp.mod_values = NULL;
result = ldap_modify_ext_s(ldap, dn, modUser, NULL, NULL);
if (result != LDAP_SUCCESS && result != LDAP_NO_SUCH_ATTRIBUTE) {
#ifdef DEBUG
printf("Could not remove authAuthority attribute for record "
"%s: %s.\r\n",
dn, ldap_err2string(result));
#endif
errors += 1;
}
}
//
// Run an LDAP modifiction request to add the new attribute.
//
if (result == LDAP_SUCCESS || result == LDAP_NO_SUCH_ATTRIBUTE) {
modOp.mod_op = LDAP_MOD_ADD;
modOp.mod_type = "authAuthority";
modOp.mod_values = modValues;
modValues[0] = authAuthority;
modValues[1] = NULL;
result = ldap_modify_ext_s(ldap, dn, modUser, NULL, NULL);
if (result != LDAP_SUCCESS) {
#ifdef DEBUG
printf("Failed to add authAuthority attribute for record %s: "
"%s.\r\n",
dn, ldap_err2string(result));
#endif
errors += 1;
}
}
//
// Run an LDAP request to replace the userPassword.
//
if (result == LDAP_SUCCESS || result == LDAP_NO_SUCH_ATTRIBUTE) {
free(authAuthority);
len = strlen(uid) + 6 + 1;
authAuthority = malloc(len);
snprintf(authAuthority, len, "{LPWS}%s", uid);
modOp.mod_op = LDAP_MOD_REPLACE;
modOp.mod_type = "userPassword";
modOp.mod_values = modValues;
modValues[0] = authAuthority;
modValues[1] = NULL;
result = ldap_modify_ext_s(ldap, dn, modUser, NULL, NULL);
if (result != LDAP_SUCCESS) {
#ifdef DEBUG
printf("Failed to replace userPassword attribute for record "
"%s: %s.\r\n",
dn, ldap_err2string(result));
#endif
errors += 1;
}
}
//
// Free up memory used by this iteration.
//
ldap_memfree(dn);
free(authAuthority);
free(uid);
free(userPassword);
}
//
// Close the LDAP connection.
//
ldap_msgfree(ldapresults);
ldap_disconnect(ldap);
return errors;
//";ApplePasswordServer;<uid>,<public thumbprint>:<myipaddress>"
}