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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
|
/*
ldb database library
Copyright (C) Andrew Tridgell 2004
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Name: ldb
*
* Component: ldb tdb backend - indexing
*
* Description: indexing routines for ldb tdb backend
*
* Author: Andrew Tridgell
*/
#include "includes.h"
#include "ldb/ldb_tdb/ldb_tdb.h"
struct dn_list {
unsigned int count;
char **dn;
};
/*
free a struct dn_list
*/
static void dn_list_free(struct dn_list *list)
{
int i;
for (i=0;i<list->count;i++) {
free(list->dn[i]);
}
if (list->dn) free(list->dn);
}
/*
return the dn key to be used for an index
caller frees
*/
static char *ldb_dn_key(const char *attr, const struct ldb_val *value)
{
char *ret = NULL;
if (ldb_should_b64_encode(value)) {
char *vstr = ldb_base64_encode(value->data, value->length);
if (!vstr) return NULL;
asprintf(&ret, "@INDEX:%s::%s", attr, vstr);
free(vstr);
return ret;
}
asprintf(&ret, "@INDEX:%s:%s", attr, (char *)value->data);
return ret;
}
/*
see if a attribute value is in the list of indexed attributes
*/
static int ldb_msg_find_idx(const struct ldb_message *msg, const char *attr,
int *v_idx)
{
int i, j;
for (i=0;i<msg->num_elements;i++) {
if (strcmp(msg->elements[i].name, "@IDXATTR") == 0) {
const struct ldb_message_element *el =
&msg->elements[i];
for (j=0;j<el->num_values;j++) {
if (strcmp((char *)el->values[j].data, attr) == 0) {
if (v_idx) {
*v_idx = j;
}
return i;
}
}
}
}
return -1;
}
/*
return a list of dn's that might match a simple indexed search or
*/
static int ltdb_index_dn_simple(struct ldb_context *ldb,
struct ldb_parse_tree *tree,
const struct ldb_message *index_list,
struct dn_list *list)
{
char *dn = NULL;
int ret, i, j;
struct ldb_message msg;
list->count = 0;
list->dn = NULL;
/*
if the value is a wildcard then we can't do a match via indexing
*/
if (ltdb_has_wildcard(&tree->u.simple.value)) {
return -1;
}
/* if the attribute isn't in the list of indexed attributes then
this node needs a full search */
if (ldb_msg_find_idx(index_list, tree->u.simple.attr, NULL) == -1) {
return -1;
}
/* the attribute is indexed. Pull the list of DNs that match the
search criterion */
dn = ldb_dn_key(tree->u.simple.attr, &tree->u.simple.value);
if (!dn) return -1;
ret = ltdb_search_dn1(ldb, dn, &msg);
free(dn);
if (ret == 0 || ret == -1) {
return ret;
}
for (i=0;i<msg.num_elements;i++) {
struct ldb_message_element *el;
if (strcmp(msg.elements[i].name, "@IDX") != 0) {
continue;
}
el = &msg.elements[i];
list->dn = malloc_array_p(char *, el->num_values);
if (!list->dn) {
break;
}
for (j=0;j<el->num_values;j++) {
list->dn[list->count] =
strdup((char *)el->values[j].data);
if (!list->dn[list->count]) {
dn_list_free(list);
ltdb_search_dn1_free(ldb, &msg);
return -1;
}
list->count++;
}
}
ltdb_search_dn1_free(ldb, &msg);
qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t) strcmp);
return 1;
}
/*
list intersection
list = list & list2
relies on the lists being sorted
*/
static int list_intersect(struct dn_list *list, const struct dn_list *list2)
{
struct dn_list list3;
int i;
if (list->count == 0 || list2->count == 0) {
/* 0 & X == 0 */
dn_list_free(list);
return 0;
}
list3.dn = malloc_array_p(char *, list->count);
if (!list3.dn) {
dn_list_free(list);
return -1;
}
list3.count = 0;
for (i=0;i<list->count;i++) {
if (list_find(list->dn[i], list2->dn, list2->count,
sizeof(char *), (comparison_fn_t)strcmp) != -1) {
list3.dn[list3.count] = list->dn[i];
list3.count++;
} else {
free(list->dn[i]);
}
}
free(list->dn);
list->dn = list3.dn;
list->count = list3.count;
return 0;
}
/*
list union
list = list | list2
relies on the lists being sorted
*/
static int list_union(struct dn_list *list, const struct dn_list *list2)
{
int i;
char **d;
unsigned int count = list->count;
if (list->count == 0 && list2->count == 0) {
/* 0 | 0 == 0 */
dn_list_free(list);
return 0;
}
d = realloc_p(list->dn, char *, list->count + list2->count);
if (!d) {
dn_list_free(list);
return -1;
}
list->dn = d;
for (i=0;i<list2->count;i++) {
if (list_find(list2->dn[i], list->dn, count,
sizeof(char *), (comparison_fn_t)strcmp) == -1) {
list->dn[list->count] = strdup(list2->dn[i]);
if (!list->dn[list->count]) {
dn_list_free(list);
return -1;
}
list->count++;
}
}
if (list->count != count) {
qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t)strcmp);
}
return 0;
}
static int ltdb_index_dn(struct ldb_context *ldb,
struct ldb_parse_tree *tree,
const struct ldb_message *index_list,
struct dn_list *list);
/*
OR two index results
*/
static int ltdb_index_dn_or(struct ldb_context *ldb,
struct ldb_parse_tree *tree,
const struct ldb_message *index_list,
struct dn_list *list)
{
int ret, i;
ret = -1;
list->dn = NULL;
list->count = 0;
for (i=0;i<tree->u.list.num_elements;i++) {
struct dn_list list2;
int v;
v = ltdb_index_dn(ldb, tree->u.list.elements[i], index_list, &list2);
if (v == 0) {
/* 0 || X == X */
if (ret == -1) {
ret = 0;
}
continue;
}
if (v == -1) {
/* 1 || X == 1 */
dn_list_free(list);
return -1;
}
if (ret == -1) {
ret = 1;
*list = list2;
} else {
if (list_union(list, &list2) == -1) {
dn_list_free(&list2);
return -1;
}
dn_list_free(&list2);
}
}
if (list->count == 0) {
dn_list_free(list);
return 0;
}
return ret;
}
/*
NOT an index results
*/
static int ltdb_index_dn_not(struct ldb_context *ldb,
struct ldb_parse_tree *tree,
const struct ldb_message *index_list,
struct dn_list *list)
{
/* the only way to do an indexed not would be if we could
negate the not via another not or if we knew the total
number of database elements so we could know that the
existing expression covered the whole database.
instead, we just give up, and rely on a full index scan
(unless an outer & manages to reduce the list)
*/
return -1;
}
/*
AND two index results
*/
static int ltdb_index_dn_and(struct ldb_context *ldb,
struct ldb_parse_tree *tree,
const struct ldb_message *index_list,
struct dn_list *list)
{
int ret, i;
ret = -1;
list->dn = NULL;
list->count = 0;
for (i=0;i<tree->u.list.num_elements;i++) {
struct dn_list list2;
int v;
v = ltdb_index_dn(ldb, tree->u.list.elements[i], index_list, &list2);
if (v == 0) {
/* 0 && X == 0 */
dn_list_free(list);
return 0;
}
if (v == -1) {
continue;
}
if (ret == -1) {
ret = 1;
*list = list2;
} else {
if (list_intersect(list, &list2) == -1) {
dn_list_free(&list2);
return -1;
}
dn_list_free(&list2);
}
if (list->count == 0) {
if (list->dn) free(list->dn);
return 0;
}
}
return ret;
}
/*
return a list of dn's that might match a indexed search or
-1 if an error. return 0 for no matches, or 1 for matches
*/
static int ltdb_index_dn(struct ldb_context *ldb,
struct ldb_parse_tree *tree,
const struct ldb_message *index_list,
struct dn_list *list)
{
int ret;
switch (tree->operation) {
case LDB_OP_SIMPLE:
ret = ltdb_index_dn_simple(ldb, tree, index_list, list);
break;
case LDB_OP_AND:
ret = ltdb_index_dn_and(ldb, tree, index_list, list);
break;
case LDB_OP_OR:
ret = ltdb_index_dn_or(ldb, tree, index_list, list);
break;
case LDB_OP_NOT:
ret = ltdb_index_dn_not(ldb, tree, index_list, list);
break;
}
return ret;
}
/*
filter a candidate dn_list from an indexed search into a set of results
extracting just the given attributes
*/
static int ldb_index_filter(struct ldb_context *ldb, struct ldb_parse_tree *tree,
const char *base,
enum ldb_scope scope,
const struct dn_list *dn_list,
const char *attrs[], struct ldb_message ***res)
{
int i;
unsigned int count = 0;
for (i=0;i<dn_list->count;i++) {
struct ldb_message msg;
int ret;
ret = ltdb_search_dn1(ldb, dn_list->dn[i], &msg);
if (ret == 0) {
/* the record has disappeared? yes, this can happen */
continue;
}
if (ret == -1) {
/* an internal error */
return -1;
}
if (ldb_message_match(ldb, &msg, tree, base, scope) == 1) {
ret = ltdb_add_attr_results(ldb, &msg, attrs, &count, res);
}
ltdb_search_dn1_free(ldb, &msg);
if (ret != 0) {
return -1;
}
}
return count;
}
/*
search the database with a LDAP-like expression using indexes
returns -1 if an indexed search is not possible, in which
case the caller should call ltdb_search_full()
*/
int ltdb_search_indexed(struct ldb_context *ldb,
const char *base,
enum ldb_scope scope,
struct ldb_parse_tree *tree,
const char *attrs[], struct ldb_message ***res)
{
struct ldb_message index_list;
struct dn_list dn_list;
int ret;
/* find the list of indexed fields */
ret = ltdb_search_dn1(ldb, "@INDEXLIST", &index_list);
if (ret != 1) {
/* no index list? must do full search */
return -1;
}
ret = ltdb_index_dn(ldb, tree, &index_list, &dn_list);
ltdb_search_dn1_free(ldb, &index_list);
if (ret == 1) {
/* we've got a candidate list - now filter by the full tree
and extract the needed attributes */
ret = ldb_index_filter(ldb, tree, base, scope, &dn_list,
attrs, res);
dn_list_free(&dn_list);
}
return ret;
}
/*
add a index element where this is the first indexed DN for this value
*/
static int ltdb_index_add1_new(struct ldb_context *ldb,
struct ldb_message *msg,
struct ldb_message_element *el,
const char *dn)
{
struct ldb_message_element *el2;
/* add another entry */
el2 = realloc_p(msg->elements, struct ldb_message_element, msg->num_elements+1);
if (!el2) {
return -1;
}
msg->elements = el2;
msg->elements[msg->num_elements].name = "@IDX";
msg->elements[msg->num_elements].num_values = 0;
msg->elements[msg->num_elements].values = malloc_p(struct ldb_val);
if (!msg->elements[msg->num_elements].values) {
return -1;
}
msg->elements[msg->num_elements].values[0].length = strlen(dn);
msg->elements[msg->num_elements].values[0].data = dn;
msg->elements[msg->num_elements].num_values = 1;
msg->num_elements++;
return 0;
}
/*
add a index element where this is not the first indexed DN for this
value
*/
static int ltdb_index_add1_add(struct ldb_context *ldb,
struct ldb_message *msg,
struct ldb_message_element *el,
int idx,
const char *dn)
{
struct ldb_val *v2;
v2 = realloc_p(msg->elements[idx].values,
struct ldb_val,
msg->elements[idx].num_values+1);
if (!v2) {
return -1;
}
msg->elements[idx].values = v2;
msg->elements[idx].values[msg->elements[idx].num_values].length = strlen(dn);
msg->elements[idx].values[msg->elements[idx].num_values].data = dn;
msg->elements[idx].num_values++;
return 0;
}
/*
add an index entry for one message element
*/
static int ltdb_index_add1(struct ldb_context *ldb, const char *dn,
struct ldb_message_element *el, int v_idx)
{
struct ldb_message msg;
char *dn_key;
int ret, i;
dn_key = ldb_dn_key(el->name, &el->values[v_idx]);
if (!dn_key) {
return -1;
}
ret = ltdb_search_dn1(ldb, dn_key, &msg);
if (ret == -1) {
free(dn_key);
return -1;
}
if (ret == 0) {
msg.dn = strdup(dn_key);
if (!msg.dn) {
free(dn_key);
errno = ENOMEM;
return -1;
}
msg.num_elements = 0;
msg.elements = NULL;
msg.private_data = NULL;
}
free(dn_key);
for (i=0;i<msg.num_elements;i++) {
if (strcmp("@IDX", msg.elements[i].name) == 0) {
break;
}
}
if (i == msg.num_elements) {
ret = ltdb_index_add1_new(ldb, &msg, el, dn);
} else {
ret = ltdb_index_add1_add(ldb, &msg, el, i, dn);
}
if (ret == 0) {
ret = ltdb_store(ldb, &msg, TDB_REPLACE);
}
ltdb_search_dn1_free(ldb, &msg);
return ret;
}
/*
add the index entries for a new record
return -1 on failure
*/
int ltdb_index_add(struct ldb_context *ldb, const struct ldb_message *msg)
{
int ret, i, j;
struct ldb_message index_list;
/* find the list of indexed fields */
ret = ltdb_search_dn1(ldb, "@INDEXLIST", &index_list);
if (ret != 1) {
/* no indexed fields or an error */
return ret;
}
for (i=0;i<msg->num_elements;i++) {
ret = ldb_msg_find_idx(&index_list, msg->elements[i].name, NULL);
if (ret == -1) {
continue;
}
for (j=0;j<msg->elements[i].num_values;j++) {
ret = ltdb_index_add1(ldb, msg->dn, &msg->elements[i], j);
if (ret == -1) {
ltdb_search_dn1_free(ldb, &index_list);
return -1;
}
}
}
ltdb_search_dn1_free(ldb, &index_list);
return 0;
}
/*
delete an index entry for one message element
*/
static int ltdb_index_del1(struct ldb_context *ldb, const char *dn,
struct ldb_message_element *el, int v_idx)
{
struct ldb_message msg;
char *dn_key;
int ret, i, j;
dn_key = ldb_dn_key(el->name, &el->values[v_idx]);
if (!dn_key) {
return -1;
}
ret = ltdb_search_dn1(ldb, dn_key, &msg);
if (ret == -1) {
free(dn_key);
return -1;
}
if (ret == 0) {
/* it wasn't indexed. Did we have an earlier error? If we did then
its gone now */
ltdb_search_dn1_free(ldb, &msg);
return 0;
}
i = ldb_msg_find_idx(&msg, dn, &j);
if (i == -1) {
/* it ain't there. hmmm */
ltdb_search_dn1_free(ldb, &msg);
return 0;
}
if (j != msg.elements[i].num_values - 1) {
memmove(&msg.elements[i].values[j],
&msg.elements[i].values[j+1],
(msg.elements[i].num_values-1) *
sizeof(msg.elements[i].values[0]));
}
msg.elements[i].num_values--;
if (msg.elements[i].num_values == 0) {
ret = ltdb_delete_noindex(ldb, dn_key);
} else {
ret = ltdb_store(ldb, &msg, TDB_REPLACE);
}
ltdb_search_dn1_free(ldb, &msg);
return ret;
}
/*
delete the index entries for a record
return -1 on failure
*/
int ltdb_index_del(struct ldb_context *ldb, const struct ldb_message *msg)
{
int ret, i, j;
struct ldb_message index_list;
/* find the list of indexed fields */
ret = ltdb_search_dn1(ldb, "@INDEXLIST", &index_list);
if (ret != 1) {
/* no indexed fields or an error */
return ret;
}
for (i=0;i<msg->num_elements;i++) {
ret = ldb_msg_find_idx(&index_list, msg->elements[i].name, NULL);
if (ret == -1) {
continue;
}
for (j=0;j<msg->elements[i].num_values;j++) {
ret = ltdb_index_del1(ldb, msg->dn, &msg->elements[i], j);
if (ret == -1) {
ltdb_search_dn1_free(ldb, &index_list);
return -1;
}
}
}
return 0;
}
/*
traversal function that deletes all @INDEX records
*/
static int delete_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
{
if (strncmp(key.dptr, "DN=@INDEX:", 10) == 0) {
return tdb_delete(tdb, key);
}
return 0;
}
/*
traversal function that adds @INDEX records during a re index
*/
static int re_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
{
struct ldb_context *ldb = state;
struct ldb_message msg;
int ret;
if (strncmp(key.dptr, "DN=@", 4) == 0 ||
strncmp(key.dptr, "DN=", 3) != 0) {
return 0;
}
ret = ltdb_unpack_data(ldb, &data, &msg);
if (ret != 0) {
return -1;
}
msg.dn = key.dptr+3;
ret = ltdb_index_add(ldb, &msg);
ltdb_unpack_data_free(&msg);
return ret;
}
/*
force a complete reindex of the database
*/
int ltdb_reindex(struct ldb_context *ldb)
{
struct ltdb_private *ltdb = ldb->private_data;
int ret;
/* first traverse the database deleting any @INDEX records */
ret = tdb_traverse(ltdb->tdb, delete_index, NULL);
if (ret == -1) {
errno = EIO;
return -1;
}
/* now traverse adding any indexes for normal LDB records */
ret = tdb_traverse(ltdb->tdb, re_index, ldb);
if (ret == -1) {
errno = EIO;
return -1;
}
return 0;
}
|