-
Notifications
You must be signed in to change notification settings - Fork 0
/
LdapWrapper.c
executable file
·670 lines (442 loc) · 13.4 KB
/
LdapWrapper.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
============================================================================
Name : LdapWrapper.c
Author : Diego Marafetti
Version : 0.1
Copyright : Your copyright notice
Description : OpenLDAP Wrapper declaration file
============================================================================
*/
#include "LdapWrapper.h"
#define __LDAP_ALLOC(POINTER_TO, STR_LENGTH, SIZE) \
(POINTER_TO) malloc( sizeof(STR_LENGTH) * SIZE)
/*************************************************************************
* Global LDAP utility functions
*************************************************************************/
/**
*
*/
PCHAR getErrorDescription(INT errorCode) {
return ldap_err2string(errorCode);
}
VOID buildLDAPMod(PLDAP_ENTRY object) {
object->ldapMod = __LDAP_ALLOC(LDAPMod **, LDAPMod *, object->actualSize + 1);
PLDAP_ATTRIBUTE currentAttrib = object->firstAttribute;
INT index = 0;
while(currentAttrib != NULL) {
object->ldapMod[index] = currentAttrib->modObj;
currentAttrib = currentAttrib->next;
index++;
}
object->ldapMod[index] = NULL;
}
/*************************************************************************
* Definition of LDAP Context operations
*************************************************************************/
/**
*
*/
VOID setOption(PLDAP_CONTEXT context, INT option, PVOID value) {
INT returnValue = ldap_set_option(context->ldap, option, value);
if( returnValue != LDAP_OPT_SUCCESS) {
context->errorCode = returnValue;
}
}
/**
*
*/
PVOID getOption(PLDAP_CONTEXT context, INT option) {
PVOID value = NULL;
INT returnValue = ldap_get_option(context->ldap, option, value);
if( returnValue != LDAP_OPT_SUCCESS) {
context->errorCode = returnValue;
return NULL;
}
return value;
}
/**
*
*/
VOID initialize(PLDAP_CONTEXT context, PCHAR host) {
LDAP *ldap = NULL;
INT returnValue;
INT version = LDAP_VERSION3;
returnValue = ldap_initialize(&ldap, host);
if(returnValue != LDAP_SUCCESS) {
printf("Init Error: %s\n", getErrorDescription(returnValue));
context->errorCode = returnValue;
return;
}
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
context->ldap = ldap;
context->host = host;
}
/**
*
*/
PLDAP_SESSION newSession(PLDAP_CONTEXT context, PCHAR dn, PCHAR password) {
PLDAP_SESSION session = newLDAPSession();
if(context == NULL) {
return NULL;
}
session->context = context;
session->dn = dn;
session->password = password;
return session;
}
/*************************************************************************
* Definition of LDAP Session operations
*************************************************************************/
/**
*
*/
VOID startSession(PLDAP_SESSION session) {
PLDAP_CONTEXT context = session->context;
ldap_simple_bind_s(context->ldap, session->dn, session->password);
session->started = 1;
}
/**
*
*/
VOID endSession(PLDAP_SESSION session) {
PLDAP_CONTEXT context = session->context;
ldap_unbind_s(context->ldap);
session->started = 0;
}
/**
*
*/
INT isStarted(PLDAP_SESSION session) {
return session->started;
}
/**
*
*/
VOID addEntry(PLDAP_SESSION session, PLDAP_ENTRY object) {
buildLDAPMod(object);
INT returnValue = ldap_add_ext_s(session->context->ldap, object->dn, object->ldapMod, NULL, NULL);
if( returnValue != LDAP_OPT_SUCCESS) {
session->errorCode = returnValue;
printf("add Error: %s\n", getErrorDescription(returnValue));
}
}
/**
*
*/
INT doDeleteEntry(LDAP *ldap, PCHAR dn) {
return ldap_delete_ext_s(ldap, dn, NULL, NULL);
}
/**
*
*/
VOID deleteEntryObj(PLDAP_SESSION session, PLDAP_ENTRY entry) {
INT returnValue = doDeleteEntry(session->context->ldap, entry->dn);
if( returnValue != LDAP_OPT_SUCCESS) {
session->errorCode = returnValue;
printf("add Error: %s\n", getErrorDescription(returnValue));
}
}
/**
*
*/
VOID deleteEntryDn(PLDAP_SESSION session, PCHAR dn) {
INT returnValue = doDeleteEntry(session->context->ldap, dn);
if( returnValue != LDAP_OPT_SUCCESS) {
session->errorCode = returnValue;
printf("add Error: %s\n", getErrorDescription(returnValue));
}
}
VOID editEntry(PLDAP_SESSION session, PLDAP_ENTRY entry) {
buildLDAPMod(entry);
INT returnValue = ldap_modify_ext_s(session->context->ldap, entry->dn, entry->ldapMod, NULL, NULL);
if( returnValue != LDAP_OPT_SUCCESS) {
session->errorCode = returnValue;
printf("add Error: %s\n", getErrorDescription(returnValue));
}
}
/**
*
*/
PLDAP_RESULT_SET searchEntry(PLDAP_SESSION session, PCHAR baseDn, PCHAR filter) {
PLDAP_RESULT_SET resultSet = __LDAP_ALLOC(PLDAP_RESULT_SET, LDAP_RESULT_SET, 1);
PLDAP_ITERATOR iterator = newLDAPIterator();
INT returnValue = ldap_search_ext_s(
session->context->ldap,
baseDn,
LDAP_SCOPE_SUBTREE,
filter,
NULL,
0,
NULL,
NULL,
LDAP_NO_LIMIT,
LDAP_NO_LIMIT,
&resultSet->results
);
if( returnValue != LDAP_OPT_SUCCESS) {
session->errorCode = returnValue;
printf("add Error: %s\n", getErrorDescription(returnValue));
return NULL;
}
/* guardo la cantidad de resultados obtenidos */
resultSet->count = ldap_count_messages(session->context->ldap, resultSet->results);
resultSet->iterator = iterator;
resultSet->session = session;
/* guardo el primer mensaje de los
resultador para el iterador */
resultSet->next = ldap_first_entry(session->context->ldap, resultSet->results);
return resultSet;
}
VOID searchEntryAsync(PLDAP_SESSION session, PCHAR baseDn, PCHAR filter) {
INT msgid;
INT returnValue = ldap_search_ext(
session->context->ldap,
baseDn,
LDAP_SCOPE_SUBTREE,
filter,
NULL,
0,
NULL,
NULL,
LDAP_NO_LIMIT,
LDAP_NO_LIMIT,
&msgid
);
if( returnValue != LDAP_OPT_SUCCESS) {
session->errorCode = returnValue;
printf("add Error: %s\n", getErrorDescription(returnValue));
return;
}
}
/*************************************************************************
* Entries LDAP functions
*************************************************************************/
/**
*Init
*/
PLDAP_ATTRIBUTE createAttribute(PCHAR attributeName, INT valueSize, ...) {
PLDAP_ATTRIBUTE attribute = NULL;
LDAPMod *modObj = NULL;
PPCHAR attributes = NULL;
UINT index;
va_list argPtr;
attribute = __LDAP_ALLOC(PLDAP_ATTRIBUTE, LDAP_ATTRIBUTE, 1);
modObj = __LDAP_ALLOC(LDAPMod *, LDAPMod, 1);
attributes = __LDAP_ALLOC(PPCHAR, PCHAR, valueSize + 1);
va_start(argPtr, valueSize);
/* extraigo los argumentos de la lista
y genero el array de attributes */
for(index = 0; index < valueSize; index++) {
attributes[index] = va_arg(argPtr, PCHAR);
}
/* se finaliza indicando con null
que no hay mas attributos */
attributes[index] = NULL;
va_end(argPtr);
modObj->mod_type = attributeName;
modObj->mod_values = attributes;
attribute->modObj = modObj;
attribute->next = NULL;
return attribute;
}
/**
*
*/
PLDAP_ENTRY createEntry() {
PLDAP_ENTRY entry = __LDAP_ALLOC(PLDAP_ENTRY, LDAP_ENTRY, 1);
entry->actualSize = 0;
entry->firstAttribute = NULL;
entry->lastAttribute = NULL;
entry->ldapMod = NULL;
return entry;
}
VOID appendAttribute(PLDAP_ENTRY entry, PLDAP_ATTRIBUTE attribute, INT operationType) {
/* establesco el tipo de operacion */
attribute->modObj->mod_op = operationType;
/* si es el primer attribute */
if(entry->firstAttribute == NULL) {
entry->firstAttribute = attribute;
entry->lastAttribute = attribute;
entry->actualSize = 1;
return;
}
/* lo agrego al final */
entry->lastAttribute->next = attribute;
entry->lastAttribute = attribute;
entry->actualSize++;
}
VOID addAttribute(PLDAP_ENTRY entry, PLDAP_ATTRIBUTE attribute) {
appendAttribute(entry, attribute, LDAP_MOD_ADD);
}
VOID deleteAttribute(PLDAP_ENTRY entry, PLDAP_ATTRIBUTE attribute) {
appendAttribute(entry, attribute, LDAP_MOD_DELETE);
}
VOID editAttribute(PLDAP_ENTRY entry, PLDAP_ATTRIBUTE attribute) {
appendAttribute(entry, attribute, LDAP_MOD_REPLACE);
}
/*************************************************************************
* Iterator LDAP functions
*************************************************************************/
BOOLEAN hasNext(PLDAP_RESULT_SET resultSet) {
return (resultSet->next == NULL) ? FALSE : TRUE;
}
PLDAP_RECORD next(PLDAP_RESULT_SET resultSet) {
PLDAP_RECORD record = __LDAP_ALLOC(PLDAP_RECORD, LDAP_RECORD, 1);
if(resultSet->next == NULL) {
return NULL;
}
record->ldap = resultSet->session->context->ldap;
record->message = resultSet->next;
record->dn = ldap_get_dn(record->ldap, resultSet->next);
record->next = ldap_first_attribute(record->ldap, record->message, &record->ber);
resultSet->next = ldap_next_entry(record->ldap, record->message);
return record;
}
/*************************************************************************
* Record LDAP functions
*************************************************************************/
BOOLEAN hasNextField(PLDAP_RECORD record) {
return (record->next == NULL) ? FALSE : TRUE;
}
PLDAP_FIELD nextField(PLDAP_RECORD record) {
PLDAP_FIELD field = __LDAP_ALLOC(PLDAP_FIELD, LDAP_FIELD, 1);
if(record->next == NULL) {
return NULL;
}
field->name = record->next;
field->values = (char **)ldap_get_values( record->ldap, record->message, record->next);
field->valuesSize = ldap_count_values(field->values);
record->next = ldap_next_attribute(record->ldap, record->message, record->ber);
return field;
}
/*************************************************************************
* Initialization LDAP functions
*************************************************************************/
PLDAP_CONTEXT newLDAPContext(){
PLDAP_CONTEXT context = __LDAP_ALLOC(PLDAP_CONTEXT, LDAP_CONTEXT, 1);
context->errorCode = 0;
context->host = NULL;
context->ldap = NULL;
return context;
}
PLDAP_SESSION newLDAPSession(){
PLDAP_SESSION session = __LDAP_ALLOC(PLDAP_SESSION, LDAP_SESSION, 1);
session->context = NULL;
session->dn = NULL;
session->errorCode = 0;
session->password = NULL;
session->started = 0;
return session;
}
/**
*
*/
PLDAP_CONTEXT_OP newLDAPContextOperations() {
PLDAP_CONTEXT_OP operations = __LDAP_ALLOC(PLDAP_CONTEXT_OP, LDAP_CONTEXT_OP, 1);
operations->newSession = newSession;
operations->getOption = getOption;
operations->setOption = setOption;
operations->initialize = initialize;
return operations;
}
/**
*
*/
PLDAP_SESSION_OP newLDAPSessionOperations() {
PLDAP_SESSION_OP operations = __LDAP_ALLOC(PLDAP_SESSION_OP, LDAP_SESSION_OP, 1);
operations->addEntry = addEntry;
operations->deleteEntryDn = deleteEntryDn;
operations->deleteEntryObj = deleteEntryObj;
operations->editEntry = editEntry;
operations->searchEntry = searchEntry;
operations->searchEntryAsync = searchEntryAsync;
operations->startSession = startSession;
operations->endSession = endSession;
operations->isStarted = isStarted;
return operations;
}
PLDAP_ENTRY_OP newLDAPEntryOperations() {
PLDAP_ENTRY_OP operations = __LDAP_ALLOC(PLDAP_ENTRY_OP, LDAP_ENTRY_OP, 1);
operations->addAttribute = addAttribute;
operations->deleteAttribute = deleteAttribute;
operations->editAttribute = editAttribute;
operations->createEntry = createEntry;
return operations;
}
PLDAP_ATTRIBUTE_OP newLDAPAttributeOperations() {
PLDAP_ATTRIBUTE_OP operations = __LDAP_ALLOC(PLDAP_ATTRIBUTE_OP, LDAP_ATTRIBUTE_OP, 1);
operations->createAttribute = createAttribute;
return operations;
}
PLDAP_ITERATOR newLDAPIterator() {
PLDAP_ITERATOR iterator = __LDAP_ALLOC(PLDAP_ITERATOR, LDAP_ITERATOR, 1);
iterator->hasNext = hasNext;
iterator->next = next;
return iterator;
}
PLDAP_RECORD_OP newLDAPRecordOperations() {
PLDAP_RECORD_OP operations = __LDAP_ALLOC(PLDAP_RECORD_OP, LDAP_RECORD_OP, 1);
operations->nextField = nextField;
operations->hasNextField = hasNextField;
return operations;
}
/**
*
*/
void freeLDAPContextOperations(PLDAP_CONTEXT_OP operations) {
if(NULL == operations) return;
free(operations);
}
/**
*
*/
void freeLDAPSessionOperations(PLDAP_SESSION_OP operations) {
if(NULL == operations) return;
free(operations);
}
/**
*
*/
void freeLDAPSession(PLDAP_SESSION session) {
if(NULL == session) return;
free(session);
}
/**
*
*/
void freeLDAPContext(PLDAP_CONTEXT context) {
if(NULL == context) return;
free(context);
}
/**
*
*/
VOID freeLDAPIterator(PLDAP_ITERATOR iterator) {
if(NULL == iterator) return;
free(iterator);
}
/**
*
*/
VOID freeLDAPRecordOperations(PLDAP_RECORD_OP operations) {
if(NULL == operations) return;
free(operations);
}
/**
*
*/
VOID freeLDAPRecord(PLDAP_RECORD record) {
if(NULL == record) return;
ber_free(record->ber, 0);
ldap_memfree(record->dn);
ldap_memfree(record->message);
free(record);
}
/**
*
*/
VOID freeLDAPField(PLDAP_FIELD field) {
if(NULL == field) return;
ldap_memfree(field->values);
free(field);
}