r8373: New wildcard matching code.
[abartlet/samba.git/.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                 const struct ldb_attrib_handler *h;
96                 h = ldb_attrib_handler(ldb, msg->elements[i].name);
97                 if ((msg->dn[0] != '@') && (h->flags & LDB_ATTR_FLAG_HIDDEN)) {
98                         continue;
99                 }
100                 if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
101                         return -1;
102                 }
103         }
104
105         return 0;
106 }
107
108
109 /*
110   pull the specified list of attributes from a message
111  */
112 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module, 
113                                            const struct ldb_message *msg, 
114                                            const char * const *attrs)
115 {
116         struct ldb_context *ldb = module->ldb;
117         struct ldb_message *ret;
118         int i;
119
120         ret = talloc(ldb, struct ldb_message);
121         if (!ret) {
122                 return NULL;
123         }
124
125         ret->dn = talloc_strdup(ret, msg->dn);
126         if (!ret->dn) {
127                 talloc_free(ret);
128                 return NULL;
129         }
130
131         ret->num_elements = 0;
132         ret->elements = NULL;
133
134         if (!attrs) {
135                 if (msg_add_all_elements(module, ret, msg) != 0) {
136                         talloc_free(ret);
137                         return NULL;
138                 }
139                 return ret;
140         }
141
142         for (i=0;attrs[i];i++) {
143                 struct ldb_message_element *el;
144
145                 if (strcmp(attrs[i], "*") == 0) {
146                         if (msg_add_all_elements(module, ret, msg) != 0) {
147                                 talloc_free(ret);
148                                 return NULL;
149                         }
150                         continue;
151                 }
152
153                 if (ldb_attr_cmp(attrs[i], "dn") == 0 ||
154                     ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
155                         struct ldb_message_element el2;
156                         struct ldb_val val;
157
158                         el2.flags = 0;
159                         el2.name = talloc_strdup(ret, attrs[i]);
160                         if (!el2.name) {
161                                 talloc_free(ret);
162                                 return NULL;                            
163                         }
164                         el2.num_values = 1;
165                         el2.values = &val;
166                         val.data = ret->dn;
167                         val.length = strlen(ret->dn);
168
169                         if (msg_add_element(ldb, ret, &el2) != 0) {
170                                 talloc_free(ret);
171                                 return NULL;                            
172                         }
173                         talloc_free(discard_const_p(char, el2.name));
174                         continue;
175                 }
176
177                 el = ldb_msg_find_element(msg, attrs[i]);
178                 if (!el) {
179                         continue;
180                 }
181                 if (msg_add_element(ldb, ret, el) != 0) {
182                         talloc_free(ret);
183                         return NULL;                            
184                 }
185         }
186
187         return ret;
188 }
189
190
191 /*
192   search the database for a single simple dn, returning all attributes
193   in a single message
194
195   return 1 on success, 0 on record-not-found and -1 on error
196 */
197 int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg)
198 {
199         struct ltdb_private *ltdb = module->private_data;
200         int ret;
201         TDB_DATA tdb_key, tdb_data, tdb_data2;
202
203         memset(msg, 0, sizeof(*msg));
204
205         /* form the key */
206         tdb_key = ltdb_key(module, dn);
207         if (!tdb_key.dptr) {
208                 return -1;
209         }
210
211         tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
212         talloc_free(tdb_key.dptr);
213         if (!tdb_data.dptr) {
214                 return 0;
215         }
216
217         tdb_data2.dptr = talloc_memdup(msg, tdb_data.dptr, tdb_data.dsize);
218         free(tdb_data.dptr);
219         if (!tdb_data2.dptr) {
220                 return -1;
221         }
222         tdb_data2.dsize = tdb_data.dsize;
223
224         msg->num_elements = 0;
225         msg->elements = NULL;
226
227         ret = ltdb_unpack_data(module, &tdb_data2, msg);
228         if (ret == -1) {
229                 talloc_free(tdb_data2.dptr);
230                 return -1;              
231         }
232
233         if (!msg->dn) {
234                 msg->dn = talloc_strdup(tdb_data2.dptr, dn);
235         }
236         if (!msg->dn) {
237                 talloc_free(tdb_data2.dptr);
238                 return -1;
239         }
240
241         return 1;
242 }
243
244
245 /*
246   search the database for a single simple dn
247 */
248 static int ltdb_search_dn(struct ldb_module *module, const char *dn,
249                           const char * const attrs[], struct ldb_message ***res)
250 {
251         struct ldb_context *ldb = module->ldb;
252         struct ltdb_private *ltdb = module->private_data;
253         int ret;
254         struct ldb_message *msg, *msg2;
255
256         *res = NULL;
257
258         if (ltdb_lock_read(module) != 0) {
259                 return -1;
260         }
261
262         ltdb->last_err_string = NULL;
263
264         if (ltdb_cache_load(module) != 0) {
265                 ltdb_unlock_read(module);
266                 return -1;
267         }
268
269         *res = talloc_array(ldb, struct ldb_message *, 2);
270         if (! *res) {
271                 goto failed;
272         }
273
274         msg = talloc(*res, struct ldb_message);
275         if (msg == NULL) {
276                 goto failed;
277         }
278
279         ret = ltdb_search_dn1(module, dn, msg);
280         if (ret != 1) {
281                 talloc_free(*res);
282                 *res = NULL;
283                 ltdb_unlock_read(module);
284                 return 0;
285         }
286
287         msg2 = ltdb_pull_attrs(module, msg, attrs);
288
289         talloc_free(msg);
290         if (!msg2) {
291                 goto failed;
292         }
293
294         (*res)[0] = talloc_steal(*res, msg2);
295         (*res)[1] = NULL;
296
297         ltdb_unlock_read(module);
298
299         return 1;
300
301 failed:
302         talloc_free(*res);
303         ltdb_unlock_read(module);
304         return -1;
305 }
306
307
308 /*
309   add a set of attributes from a record to a set of results
310   return 0 on success, -1 on failure
311 */
312 int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
313                           const char * const attrs[], 
314                           int *count, 
315                           struct ldb_message ***res)
316 {
317         struct ldb_context *ldb = module->ldb;
318         struct ldb_message *msg2;
319         struct ldb_message **res2;
320
321         /* pull the attributes that the user wants */
322         msg2 = ltdb_pull_attrs(module, msg, attrs);
323         if (!msg2) {
324                 return -1;
325         }
326
327         /* add to the results list */
328         res2 = talloc_realloc(ldb, *res, struct ldb_message *, (*count)+2);
329         if (!res2) {
330                 talloc_free(msg2);
331                 return -1;
332         }
333
334         (*res) = res2;
335
336         (*res)[*count] = talloc_steal(*res, msg2);
337         (*res)[(*count)+1] = NULL;
338         (*count)++;
339
340         return 0;
341 }
342
343
344 /*
345   internal search state during a full db search
346 */
347 struct ltdb_search_info {
348         struct ldb_module *module;
349         struct ldb_parse_tree *tree;
350         const char *base;
351         enum ldb_scope scope;
352         const char * const *attrs;
353         struct ldb_message **msgs;
354         int failures;
355         int count;
356 };
357
358
359 /*
360   search function for a non-indexed search
361  */
362 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
363 {
364         struct ltdb_search_info *sinfo = state;
365         struct ldb_message *msg;
366         int ret;
367
368         if (key.dsize < 4 || 
369             strncmp(key.dptr, "DN=", 3) != 0) {
370                 return 0;
371         }
372
373         msg = talloc(sinfo, struct ldb_message);
374         if (msg == NULL) {
375                 return -1;
376         }
377
378         /* unpack the record */
379         ret = ltdb_unpack_data(sinfo->module, &data, msg);
380         if (ret == -1) {
381                 sinfo->failures++;
382                 talloc_free(msg);
383                 return 0;
384         }
385
386         if (!msg->dn) {
387                 msg->dn = key.dptr + 3;
388         }
389
390         /* see if it matches the given expression */
391         if (!ldb_match_msg(sinfo->module->ldb, msg, sinfo->tree, 
392                                sinfo->base, sinfo->scope)) {
393                 talloc_free(msg);
394                 return 0;
395         }
396
397         ret = ltdb_add_attr_results(sinfo->module, msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
398
399         if (ret == -1) {
400                 sinfo->failures++;
401         }
402
403         talloc_free(msg);
404
405         return ret;
406 }
407
408
409 /*
410   search the database with a LDAP-like expression.
411   this is the "full search" non-indexed variant
412 */
413 static int ltdb_search_full(struct ldb_module *module, 
414                             const char *base,
415                             enum ldb_scope scope,
416                             struct ldb_parse_tree *tree,
417                             const char * const attrs[], struct ldb_message ***res)
418 {
419         struct ltdb_private *ltdb = module->private_data;
420         int ret, count;
421         struct ltdb_search_info *sinfo;
422
423         sinfo = talloc(ltdb, struct ltdb_search_info);
424         if (sinfo == NULL) {
425                 return -1;
426         }
427
428         sinfo->tree = tree;
429         sinfo->module = module;
430         sinfo->scope = scope;
431         sinfo->base = base;
432         sinfo->attrs = attrs;
433         sinfo->msgs = NULL;
434         sinfo->count = 0;
435         sinfo->failures = 0;
436
437         ret = tdb_traverse(ltdb->tdb, search_func, sinfo);
438
439         if (ret == -1) {
440                 talloc_free(sinfo);
441                 return -1;
442         }
443
444         *res = talloc_steal(ltdb, sinfo->msgs);
445         count = sinfo->count;
446
447         talloc_free(sinfo);
448
449         return count;
450 }
451
452
453 /*
454   search the database with a LDAP-like expression.
455   choses a search method
456 */
457 int ltdb_search_bytree(struct ldb_module *module, const char *base,
458                        enum ldb_scope scope, struct ldb_parse_tree *tree,
459                        const char * const attrs[], struct ldb_message ***res)
460 {
461         struct ltdb_private *ltdb = module->private_data;
462         int ret;
463
464         if (ltdb_lock_read(module) != 0) {
465                 return -1;
466         }
467
468         ltdb->last_err_string = NULL;
469
470         if (ltdb_cache_load(module) != 0) {
471                 ltdb_unlock_read(module);
472                 return -1;
473         }
474
475         *res = NULL;
476
477         ret = ltdb_search_indexed(module, base, scope, tree, attrs, res);
478         if (ret == -1) {
479                 ret = ltdb_search_full(module, base, scope, tree, attrs, res);
480         }
481
482         ltdb_unlock_read(module);
483
484         return ret;
485 }
486
487
488 /*
489   search the database with a LDAP-like expression.
490   choses a search method
491 */
492 int ltdb_search(struct ldb_module *module, const char *base,
493                 enum ldb_scope scope, const char *expression,
494                 const char * const attrs[], struct ldb_message ***res)
495 {
496         struct ltdb_private *ltdb = module->private_data;
497         struct ldb_parse_tree *tree;
498         int ret;
499
500         /* check if we are looking for a simple dn */
501         if (scope == LDB_SCOPE_BASE && (expression == NULL || expression[0] == '\0')) {
502                 ret = ltdb_search_dn(module, base, attrs, res);
503                 return ret;
504         }
505
506         tree = ldb_parse_tree(ltdb, expression);
507         if (tree == NULL) {
508                 ltdb->last_err_string = "expression parse failed";
509                 return -1;
510         }
511
512         ret = ltdb_search_bytree(module, base, scope, tree, attrs, res);
513         talloc_free(tree);
514         return ret;
515 }
516