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
|
/*
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 search functions
*
* Description: functions to search ldb+tdb databases
*
* Author: Andrew Tridgell
*/
#include "includes.h"
#include "ldb/ldb_tdb/ldb_tdb.h"
#include "ldb/include/ldb_parse.h"
/*
free a message that has all parts separately allocated
*/
static void msg_free_all_parts(struct ldb_context *ldb, struct ldb_message *msg)
{
unsigned int i, j;
ldb_free(ldb, msg->dn);
for (i=0;i<msg->num_elements;i++) {
ldb_free(ldb, msg->elements[i].name);
for (j=0;j<msg->elements[i].num_values;j++) {
ldb_free(ldb, msg->elements[i].values[j].data);
}
ldb_free(ldb, msg->elements[i].values);
}
ldb_free(ldb, msg->elements);
ldb_free(ldb, msg);
}
/*
duplicate a ldb_val structure
*/
struct ldb_val ldb_val_dup(struct ldb_context *ldb,
const struct ldb_val *v)
{
struct ldb_val v2;
v2.length = v->length;
if (v->length == 0) {
v2.data = NULL;
return v2;
}
/* the +1 is to cope with buggy C library routines like strndup
that look one byte beyond */
v2.data = ldb_malloc(ldb, v->length+1);
if (!v2.data) {
v2.length = 0;
return v2;
}
memcpy(v2.data, v->data, v->length);
((char *)v2.data)[v->length] = 0;
return v2;
}
/*
add one element to a message
*/
static int msg_add_element(struct ldb_context *ldb,
struct ldb_message *ret, const struct ldb_message_element *el)
{
unsigned int i;
struct ldb_message_element *e2, *elnew;
e2 = ldb_realloc_p(ldb, ret->elements, struct ldb_message_element, ret->num_elements+1);
if (!e2) {
return -1;
}
ret->elements = e2;
elnew = &e2[ret->num_elements];
elnew->name = ldb_strdup(ldb, el->name);
if (!elnew->name) {
return -1;
}
if (el->num_values) {
elnew->values = ldb_malloc_array_p(ldb, struct ldb_val, el->num_values);
if (!elnew->values) {
return -1;
}
} else {
elnew->values = NULL;
}
for (i=0;i<el->num_values;i++) {
elnew->values[i] = ldb_val_dup(ldb, &el->values[i]);
if (elnew->values[i].length != el->values[i].length) {
return -1;
}
}
elnew->num_values = el->num_values;
ret->num_elements++;
return 0;
}
/*
add all elements from one message into another
*/
static int msg_add_all_elements(struct ldb_context *ldb, struct ldb_message *ret,
const struct ldb_message *msg)
{
unsigned int i;
for (i=0;i<msg->num_elements;i++) {
int flags = ltdb_attribute_flags(ldb, msg->elements[i].name);
if ((msg->dn[0] != '@') && (flags & LTDB_FLAG_HIDDEN)) {
continue;
}
if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
return -1;
}
}
return 0;
}
/*
pull the specified list of attributes from a message
*/
static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
const struct ldb_message *msg,
const char * const *attrs)
{
struct ldb_message *ret;
int i;
ret = ldb_malloc_p(ldb, struct ldb_message);
if (!ret) {
return NULL;
}
ret->dn = ldb_strdup(ldb, msg->dn);
if (!ret->dn) {
ldb_free(ldb, ret);
return NULL;
}
ret->num_elements = 0;
ret->elements = NULL;
ret->private_data = NULL;
if (!attrs) {
if (msg_add_all_elements(ldb, ret, msg) != 0) {
msg_free_all_parts(ldb, ret);
return NULL;
}
return ret;
}
for (i=0;attrs[i];i++) {
struct ldb_message_element *el;
if (strcmp(attrs[i], "*") == 0) {
if (msg_add_all_elements(ldb, ret, msg) != 0) {
msg_free_all_parts(ldb, ret);
return NULL;
}
continue;
}
if (ldb_attr_cmp(attrs[i], "dn") == 0) {
struct ldb_message_element el2;
struct ldb_val val;
el2.flags = 0;
el2.name = ldb_strdup(ldb, "dn");
if (!el2.name) {
msg_free_all_parts(ldb, ret);
ldb_free(ldb, el2.name);
return NULL;
}
el2.num_values = 1;
el2.values = &val;
val.data = ret->dn;
val.length = strlen(ret->dn);
if (msg_add_element(ldb, ret, &el2) != 0) {
msg_free_all_parts(ldb, ret);
ldb_free(ldb, el2.name);
return NULL;
}
ldb_free(ldb, el2.name);
continue;
}
el = ldb_msg_find_element(msg, attrs[i]);
if (!el) {
continue;
}
if (msg_add_element(ldb, ret, el) != 0) {
msg_free_all_parts(ldb, ret);
return NULL;
}
}
return ret;
}
/*
see if a ldb_val is a wildcard
return 1 if yes, 0 if no
*/
int ltdb_has_wildcard(struct ldb_context *ldb, const char *attr_name,
const struct ldb_val *val)
{
int flags;
/* all attribute types recognise the "*" wildcard */
if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
return 1;
}
if (strpbrk(val->data, "*?") == NULL) {
return 0;
}
flags = ltdb_attribute_flags(ldb, attr_name);
if (flags & LTDB_FLAG_WILDCARD) {
return 1;
}
return 0;
}
/*
free the results of a ltdb_search_dn1 search
*/
void ltdb_search_dn1_free(struct ldb_context *ldb, struct ldb_message *msg)
{
unsigned int i;
ldb_free(ldb, msg->dn);
ldb_free(ldb, msg->private_data);
for (i=0;i<msg->num_elements;i++) {
ldb_free(ldb, msg->elements[i].values);
}
ldb_free(ldb, msg->elements);
memset(msg, 0, sizeof(*msg));
}
/*
search the database for a single simple dn, returning all attributes
in a single message
return 1 on success, 0 on record-not-found and -1 on error
*/
int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message *msg)
{
struct ltdb_private *ltdb = ldb->private_data;
int ret;
TDB_DATA tdb_key, tdb_data, tdb_data2;
memset(msg, 0, sizeof(*msg));
/* form the key */
tdb_key = ltdb_key(ldb, dn);
if (!tdb_key.dptr) {
return -1;
}
tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
ldb_free(ldb, tdb_key.dptr);
if (!tdb_data.dptr) {
return 0;
}
tdb_data2.dptr = ldb_malloc(ldb, tdb_data.dsize);
if (!tdb_data2.dptr) {
free(tdb_data.dptr);
return -1;
}
memcpy(tdb_data2.dptr, tdb_data.dptr, tdb_data.dsize);
free(tdb_data.dptr);
tdb_data2.dsize = tdb_data.dsize;
msg->private_data = tdb_data2.dptr;
msg->num_elements = 0;
msg->elements = NULL;
ret = ltdb_unpack_data(ldb, &tdb_data2, msg);
if (ret == -1) {
ldb_free(ldb, tdb_data2.dptr);
return -1;
}
if (!msg->dn) {
msg->dn = ldb_strdup(ldb, dn);
}
if (!msg->dn) {
ldb_free(ldb, tdb_data2.dptr);
return -1;
}
return 1;
}
/*
search the database for a single simple dn
*/
int ltdb_search_dn(struct ldb_context *ldb, char *dn,
const char * const attrs[], struct ldb_message ***res)
{
int ret;
struct ldb_message msg, *msg2;
ret = ltdb_search_dn1(ldb, dn, &msg);
if (ret != 1) {
return ret;
}
msg2 = ltdb_pull_attrs(ldb, &msg, attrs);
ltdb_search_dn1_free(ldb, &msg);
if (!msg2) {
return -1;
}
*res = ldb_malloc_array_p(ldb, struct ldb_message *, 2);
if (! *res) {
msg_free_all_parts(ldb, msg2);
return -1;
}
(*res)[0] = msg2;
(*res)[1] = NULL;
return 1;
}
/*
add a set of attributes from a record to a set of results
return 0 on success, -1 on failure
*/
int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
const char * const attrs[],
unsigned int *count,
struct ldb_message ***res)
{
struct ldb_message *msg2;
struct ldb_message **res2;
/* pull the attributes that the user wants */
msg2 = ltdb_pull_attrs(ldb, msg, attrs);
if (!msg2) {
return -1;
}
/* add to the results list */
res2 = ldb_realloc_p(ldb, *res, struct ldb_message *, (*count)+2);
if (!res2) {
msg_free_all_parts(ldb, msg2);
return -1;
}
(*res) = res2;
(*res)[*count] = msg2;
(*res)[(*count)+1] = NULL;
(*count)++;
return 0;
}
/*
internal search state during a full db search
*/
struct ltdb_search_info {
struct ldb_context *ldb;
struct ldb_parse_tree *tree;
const char *base;
enum ldb_scope scope;
const char * const *attrs;
struct ldb_message **msgs;
int failures;
int count;
};
/*
search function for a non-indexed search
*/
static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
{
struct ltdb_search_info *sinfo = state;
struct ldb_message msg;
int ret;
if (key.dsize < 4 ||
strncmp(key.dptr, "DN=", 3) != 0) {
return 0;
}
/* unpack the record */
ret = ltdb_unpack_data(sinfo->ldb, &data, &msg);
if (ret == -1) {
sinfo->failures++;
return 0;
}
if (!msg.dn) {
msg.dn = key.dptr + 3;
}
/* see if it matches the given expression */
if (!ldb_message_match(sinfo->ldb, &msg, sinfo->tree,
sinfo->base, sinfo->scope)) {
ltdb_unpack_data_free(sinfo->ldb, &msg);
return 0;
}
ret = ltdb_add_attr_results(sinfo->ldb, &msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
if (ret == -1) {
sinfo->failures++;
}
ltdb_unpack_data_free(sinfo->ldb, &msg);
return ret;
}
/*
free a set of search results
*/
int ltdb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
{
struct ltdb_private *ltdb = ldb->private_data;
int i;
ltdb->last_err_string = NULL;
if (!msgs) return 0;
for (i=0;msgs[i];i++) {
msg_free_all_parts(ldb, msgs[i]);
}
ldb_free(ldb, msgs);
return 0;
}
/*
search the database with a LDAP-like expression.
this is the "full search" non-indexed variant
*/
static int ltdb_search_full(struct ldb_context *ldb,
const char *base,
enum ldb_scope scope,
struct ldb_parse_tree *tree,
const char * const attrs[], struct ldb_message ***res)
{
struct ltdb_private *ltdb = ldb->private_data;
int ret;
struct ltdb_search_info sinfo;
sinfo.tree = tree;
sinfo.ldb = ldb;
sinfo.scope = scope;
sinfo.base = base;
sinfo.attrs = attrs;
sinfo.msgs = NULL;
sinfo.count = 0;
sinfo.failures = 0;
ret = tdb_traverse(ltdb->tdb, search_func, &sinfo);
if (ret == -1) {
ltdb_search_free(ldb, sinfo.msgs);
return -1;
}
*res = sinfo.msgs;
return sinfo.count;
}
/*
search the database with a LDAP-like expression.
choses a search method
*/
int ltdb_search(struct ldb_context *ldb, const char *base,
enum ldb_scope scope, const char *expression,
const char * const attrs[], struct ldb_message ***res)
{
struct ltdb_private *ltdb = ldb->private_data;
struct ldb_parse_tree *tree;
int ret;
ltdb->last_err_string = NULL;
if (ltdb_cache_load(ldb) != 0) {
return -1;
}
*res = NULL;
/* form a parse tree for the expression */
tree = ldb_parse_tree(ldb, expression);
if (!tree) {
ltdb->last_err_string = "expression parse failed";
return -1;
}
if (tree->operation == LDB_OP_SIMPLE &&
ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 &&
!ltdb_has_wildcard(ldb, tree->u.simple.attr, &tree->u.simple.value)) {
/* yay! its a nice simple one */
ret = ltdb_search_dn(ldb, tree->u.simple.value.data, attrs, res);
} else {
ret = ltdb_search_indexed(ldb, base, scope, tree, attrs, res);
if (ret == -1) {
ret = ltdb_search_full(ldb, base, scope, tree, attrs, res);
}
}
ldb_parse_tree_free(ldb, tree);
return ret;
}
|