r7602: fix some compiler warnings
[metze/samba/wip.git] / source4 / lib / ldb / ldb_tdb / ldb_search.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb search functions
29  *
30  *  Description: functions to search ldb+tdb databases
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "ldb/ldb_tdb/ldb_tdb.h"
39
40 /*
41   add one element to a message
42 */
43 static int msg_add_element(struct ldb_context *ldb, 
44                            struct ldb_message *ret, const struct ldb_message_element *el)
45 {
46         unsigned int i;
47         struct ldb_message_element *e2, *elnew;
48
49         e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
50         if (!e2) {
51                 return -1;
52         }
53         ret->elements = e2;
54         
55         elnew = &e2[ret->num_elements];
56
57         elnew->name = talloc_strdup(ret->elements, el->name);
58         if (!elnew->name) {
59                 return -1;
60         }
61
62         if (el->num_values) {
63                 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
64                 if (!elnew->values) {
65                         return -1;
66                 }
67         } else {
68                 elnew->values = NULL;
69         }
70
71         for (i=0;i<el->num_values;i++) {
72                 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
73                 if (elnew->values[i].length != el->values[i].length) {
74                         return -1;
75                 }
76         }
77
78         elnew->num_values = el->num_values;
79
80         ret->num_elements++;
81
82         return 0;
83 }
84
85 /*
86   add all elements from one message into another
87  */
88 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
89                                 const struct ldb_message *msg)
90 {
91         struct ldb_context *ldb = module->ldb;
92         unsigned int i;
93
94         for (i=0;i<msg->num_elements;i++) {
95                 int flags = ltdb_attribute_flags(module, msg->elements[i].name);
96                 if ((msg->dn[0] != '@') && (flags & LTDB_FLAG_HIDDEN)) {
97                         continue;
98                 }
99                 if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
100                         return -1;
101                 }
102         }
103
104         return 0;
105 }
106
107
108 /*
109   pull the specified list of attributes from a message
110  */
111 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module, 
112                                            const struct ldb_message *msg, 
113                                            const char * const *attrs)
114 {
115         struct ldb_context *ldb = module->ldb;
116         struct ldb_message *ret;
117         int i;
118
119         ret = talloc(ldb, struct ldb_message);
120         if (!ret) {
121                 return NULL;
122         }
123
124         ret->dn = talloc_strdup(ret, msg->dn);
125         if (!ret->dn) {
126                 talloc_free(ret);
127                 return NULL;
128         }
129
130         ret->num_elements = 0;
131         ret->elements = NULL;
132
133         if (!attrs) {
134                 if (msg_add_all_elements(module, ret, msg) != 0) {
135                         talloc_free(ret);
136                         return NULL;
137                 }
138                 return ret;
139         }
140
141         for (i=0;attrs[i];i++) {
142                 struct ldb_message_element *el;
143
144                 if (strcmp(attrs[i], "*") == 0) {
145                         if (msg_add_all_elements(module, ret, msg) != 0) {
146                                 talloc_free(ret);
147                                 return NULL;
148                         }
149                         continue;
150                 }
151
152                 if (ldb_attr_cmp(attrs[i], "dn") == 0 ||
153                     ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
154                         struct ldb_message_element el2;
155                         struct ldb_val val;
156
157                         el2.flags = 0;
158                         el2.name = talloc_strdup(ret, attrs[i]);
159                         if (!el2.name) {
160                                 talloc_free(ret);
161                                 return NULL;                            
162                         }
163                         el2.num_values = 1;
164                         el2.values = &val;
165                         val.data = ret->dn;
166                         val.length = strlen(ret->dn);
167
168                         if (msg_add_element(ldb, ret, &el2) != 0) {
169                                 talloc_free(ret);
170                                 return NULL;                            
171                         }
172                         talloc_free(discard_const_p(char, el2.name));
173                         continue;
174                 }
175
176                 el = ldb_msg_find_element(msg, attrs[i]);
177                 if (!el) {
178                         continue;
179                 }
180                 if (msg_add_element(ldb, ret, el) != 0) {
181                         talloc_free(ret);
182                         return NULL;                            
183                 }
184         }
185
186         return ret;
187 }
188
189
190
191 /*
192   see if a ldb_val is a wildcard
193   return 1 if yes, 0 if no
194 */
195 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
196                       const struct ldb_val *val)
197 {
198         int flags;
199
200         /* all attribute types recognise the "*" wildcard */
201         if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
202                 return 1;
203         }
204
205         if (strpbrk(val->data, "*?") == NULL) {
206                 return 0;
207         }
208
209         flags = ltdb_attribute_flags(module, attr_name);
210         if (flags & LTDB_FLAG_WILDCARD) {
211                 return 1;
212         }
213
214         return 0;
215 }
216
217
218 /*
219   search the database for a single simple dn, returning all attributes
220   in a single message
221
222   return 1 on success, 0 on record-not-found and -1 on error
223 */
224 int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg)
225 {
226         struct ltdb_private *ltdb = module->private_data;
227         int ret;
228         TDB_DATA tdb_key, tdb_data, tdb_data2;
229
230         memset(msg, 0, sizeof(*msg));
231
232         /* form the key */
233         tdb_key = ltdb_key(module, dn);
234         if (!tdb_key.dptr) {
235                 return -1;
236         }
237
238         tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
239         talloc_free(tdb_key.dptr);
240         if (!tdb_data.dptr) {
241                 return 0;
242         }
243
244         tdb_data2.dptr = talloc_memdup(msg, tdb_data.dptr, tdb_data.dsize);
245         free(tdb_data.dptr);
246         if (!tdb_data2.dptr) {
247                 return -1;
248         }
249         tdb_data2.dsize = tdb_data.dsize;
250
251         msg->num_elements = 0;
252         msg->elements = NULL;
253
254         ret = ltdb_unpack_data(module, &tdb_data2, msg);
255         if (ret == -1) {
256                 talloc_free(tdb_data2.dptr);
257                 return -1;              
258         }
259
260         if (!msg->dn) {
261                 msg->dn = talloc_strdup(tdb_data2.dptr, dn);
262         }
263         if (!msg->dn) {
264                 talloc_free(tdb_data2.dptr);
265                 return -1;
266         }
267
268         return 1;
269 }
270
271
272 /*
273   search the database for a single simple dn
274 */
275 int ltdb_search_dn(struct ldb_module *module, const char *dn,
276                    const char * const attrs[], struct ldb_message ***res)
277 {
278         struct ldb_context *ldb = module->ldb;
279         int ret;
280         struct ldb_message *msg, *msg2;
281
282         *res = talloc_array(ldb, struct ldb_message *, 2);
283         if (! *res) {
284                 return -1;              
285         }
286
287         msg = talloc(*res, struct ldb_message);
288         if (msg == NULL) {
289                 talloc_free(*res);
290                 *res = NULL;
291                 return -1;
292         }
293
294         ret = ltdb_search_dn1(module, dn, msg);
295         if (ret != 1) {
296                 talloc_free(*res);
297                 *res = NULL;
298                 return ret;
299         }
300
301         msg2 = ltdb_pull_attrs(module, msg, attrs);
302
303         talloc_free(msg);
304
305         if (!msg2) {
306                 return -1;              
307         }
308
309         (*res)[0] = talloc_steal(*res, msg2);
310         (*res)[1] = NULL;
311
312         return 1;
313 }
314
315
316 /*
317   add a set of attributes from a record to a set of results
318   return 0 on success, -1 on failure
319 */
320 int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
321                           const char * const attrs[], 
322                           int *count, 
323                           struct ldb_message ***res)
324 {
325         struct ldb_context *ldb = module->ldb;
326         struct ldb_message *msg2;
327         struct ldb_message **res2;
328
329         /* pull the attributes that the user wants */
330         msg2 = ltdb_pull_attrs(module, msg, attrs);
331         if (!msg2) {
332                 return -1;
333         }
334
335         /* add to the results list */
336         res2 = talloc_realloc(ldb, *res, struct ldb_message *, (*count)+2);
337         if (!res2) {
338                 talloc_free(msg2);
339                 return -1;
340         }
341
342         (*res) = res2;
343
344         (*res)[*count] = talloc_steal(*res, msg2);
345         (*res)[(*count)+1] = NULL;
346         (*count)++;
347
348         return 0;
349 }
350
351
352 /*
353   internal search state during a full db search
354 */
355 struct ltdb_search_info {
356         struct ldb_module *module;
357         struct ldb_parse_tree *tree;
358         const char *base;
359         enum ldb_scope scope;
360         const char * const *attrs;
361         struct ldb_message **msgs;
362         int failures;
363         int count;
364 };
365
366
367 /*
368   search function for a non-indexed search
369  */
370 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
371 {
372         struct ltdb_search_info *sinfo = state;
373         struct ldb_message *msg;
374         int ret;
375
376         if (key.dsize < 4 || 
377             strncmp(key.dptr, "DN=", 3) != 0) {
378                 return 0;
379         }
380
381         msg = talloc(sinfo, struct ldb_message);
382         if (msg == NULL) {
383                 return -1;
384         }
385
386         /* unpack the record */
387         ret = ltdb_unpack_data(sinfo->module, &data, msg);
388         if (ret == -1) {
389                 sinfo->failures++;
390                 talloc_free(msg);
391                 return 0;
392         }
393
394         if (!msg->dn) {
395                 msg->dn = key.dptr + 3;
396         }
397
398         /* see if it matches the given expression */
399         if (!ltdb_message_match(sinfo->module, msg, sinfo->tree, 
400                                 sinfo->base, sinfo->scope)) {
401                 talloc_free(msg);
402                 return 0;
403         }
404
405         ret = ltdb_add_attr_results(sinfo->module, msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
406
407         if (ret == -1) {
408                 sinfo->failures++;
409         }
410
411         talloc_free(msg);
412
413         return ret;
414 }
415
416
417 /*
418   search the database with a LDAP-like expression.
419   this is the "full search" non-indexed variant
420 */
421 static int ltdb_search_full(struct ldb_module *module, 
422                             const char *base,
423                             enum ldb_scope scope,
424                             struct ldb_parse_tree *tree,
425                             const char * const attrs[], struct ldb_message ***res)
426 {
427         struct ltdb_private *ltdb = module->private_data;
428         int ret, count;
429         struct ltdb_search_info *sinfo;
430
431         sinfo = talloc(ltdb, struct ltdb_search_info);
432         if (sinfo == NULL) {
433                 return -1;
434         }
435
436         sinfo->tree = tree;
437         sinfo->module = module;
438         sinfo->scope = scope;
439         sinfo->base = base;
440         sinfo->attrs = attrs;
441         sinfo->msgs = NULL;
442         sinfo->count = 0;
443         sinfo->failures = 0;
444
445         ret = tdb_traverse(ltdb->tdb, search_func, sinfo);
446
447         if (ret == -1) {
448                 talloc_free(sinfo);
449                 return -1;
450         }
451
452         *res = talloc_steal(ltdb, sinfo->msgs);
453         count = sinfo->count;
454
455         talloc_free(sinfo);
456
457         return count;
458 }
459
460
461 /*
462   search the database with a LDAP-like expression.
463   choses a search method
464 */
465 int ltdb_search_bytree(struct ldb_module *module, const char *base,
466                        enum ldb_scope scope, struct ldb_parse_tree *tree,
467                        const char * const attrs[], struct ldb_message ***res)
468 {
469         struct ltdb_private *ltdb = module->private_data;
470         int ret;
471
472         if (ltdb_lock_read(module) != 0) {
473                 return -1;
474         }
475
476         ltdb->last_err_string = NULL;
477
478         if (ltdb_cache_load(module) != 0) {
479                 ltdb_unlock_read(module);
480                 return -1;
481         }
482
483         *res = NULL;
484
485         ret = ltdb_search_indexed(module, base, scope, tree, attrs, res);
486         if (ret == -1) {
487                 ret = ltdb_search_full(module, base, scope, tree, attrs, res);
488         }
489
490         ltdb_unlock_read(module);
491
492         return ret;
493 }
494
495
496 /*
497   search the database with a LDAP-like expression.
498   choses a search method
499 */
500 int ltdb_search(struct ldb_module *module, const char *base,
501                 enum ldb_scope scope, const char *expression,
502                 const char * const attrs[], struct ldb_message ***res)
503 {
504         struct ltdb_private *ltdb = module->private_data;
505         struct ldb_parse_tree *tree;
506         int ret;
507
508         /* check if we are looking for a simple dn */
509         if (scope == LDB_SCOPE_BASE && (expression == NULL || expression[0] == '\0')) {
510                 ret = ltdb_search_dn(module, base, attrs, res);
511                 ltdb_unlock_read(module);
512                 return ret;
513         }
514
515         tree = ldb_parse_tree(ltdb, expression);
516         if (tree == NULL) {
517                 ltdb->last_err_string = "expression parse failed";
518                 return -1;
519         }
520
521         ret = ltdb_search_bytree(module, base, scope, tree, attrs, res);
522         talloc_free(tree);
523         return ret;
524 }
525