r7514: make the ldb_parse code not depend on a ldb_context, so we can now potentially use
[ira/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 #include "ldb/include/ldb_parse.h"
40
41 /*
42   add one element to a message
43 */
44 static int msg_add_element(struct ldb_context *ldb, 
45                            struct ldb_message *ret, const struct ldb_message_element *el)
46 {
47         unsigned int i;
48         struct ldb_message_element *e2, *elnew;
49
50         e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
51         if (!e2) {
52                 return -1;
53         }
54         ret->elements = e2;
55         
56         elnew = &e2[ret->num_elements];
57
58         elnew->name = talloc_strdup(ret->elements, el->name);
59         if (!elnew->name) {
60                 return -1;
61         }
62
63         if (el->num_values) {
64                 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
65                 if (!elnew->values) {
66                         return -1;
67                 }
68         } else {
69                 elnew->values = NULL;
70         }
71
72         for (i=0;i<el->num_values;i++) {
73                 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
74                 if (elnew->values[i].length != el->values[i].length) {
75                         return -1;
76                 }
77         }
78
79         elnew->num_values = el->num_values;
80
81         ret->num_elements++;
82
83         return 0;
84 }
85
86 /*
87   add all elements from one message into another
88  */
89 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
90                                 const struct ldb_message *msg)
91 {
92         struct ldb_context *ldb = module->ldb;
93         unsigned int i;
94
95         for (i=0;i<msg->num_elements;i++) {
96                 int flags = ltdb_attribute_flags(module, msg->elements[i].name);
97                 if ((msg->dn[0] != '@') && (flags & LTDB_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(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 /*
193   see if a ldb_val is a wildcard
194   return 1 if yes, 0 if no
195 */
196 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
197                       const struct ldb_val *val)
198 {
199         int flags;
200
201         /* all attribute types recognise the "*" wildcard */
202         if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
203                 return 1;
204         }
205
206         if (strpbrk(val->data, "*?") == NULL) {
207                 return 0;
208         }
209
210         flags = ltdb_attribute_flags(module, attr_name);
211         if (flags & LTDB_FLAG_WILDCARD) {
212                 return 1;
213         }
214
215         return 0;
216 }
217
218
219 /*
220   search the database for a single simple dn, returning all attributes
221   in a single message
222
223   return 1 on success, 0 on record-not-found and -1 on error
224 */
225 int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg)
226 {
227         struct ltdb_private *ltdb = module->private_data;
228         int ret;
229         TDB_DATA tdb_key, tdb_data, tdb_data2;
230
231         memset(msg, 0, sizeof(*msg));
232
233         /* form the key */
234         tdb_key = ltdb_key(module, dn);
235         if (!tdb_key.dptr) {
236                 return -1;
237         }
238
239         tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
240         talloc_free(tdb_key.dptr);
241         if (!tdb_data.dptr) {
242                 return 0;
243         }
244
245         tdb_data2.dptr = talloc_memdup(msg, tdb_data.dptr, tdb_data.dsize);
246         free(tdb_data.dptr);
247         if (!tdb_data2.dptr) {
248                 return -1;
249         }
250         tdb_data2.dsize = tdb_data.dsize;
251
252         msg->num_elements = 0;
253         msg->elements = NULL;
254
255         ret = ltdb_unpack_data(module, &tdb_data2, msg);
256         if (ret == -1) {
257                 talloc_free(tdb_data2.dptr);
258                 return -1;              
259         }
260
261         if (!msg->dn) {
262                 msg->dn = talloc_strdup(tdb_data2.dptr, dn);
263         }
264         if (!msg->dn) {
265                 talloc_free(tdb_data2.dptr);
266                 return -1;
267         }
268
269         return 1;
270 }
271
272
273 /*
274   search the database for a single simple dn
275 */
276 int ltdb_search_dn(struct ldb_module *module, char *dn,
277                    const char * const attrs[], struct ldb_message ***res)
278 {
279         struct ldb_context *ldb = module->ldb;
280         int ret;
281         struct ldb_message *msg, *msg2;
282
283         *res = talloc_array(ldb, struct ldb_message *, 2);
284         if (! *res) {
285                 return -1;              
286         }
287
288         msg = talloc(*res, struct ldb_message);
289         if (msg == NULL) {
290                 talloc_free(*res);
291                 *res = NULL;
292                 return -1;
293         }
294
295         ret = ltdb_search_dn1(module, dn, msg);
296         if (ret != 1) {
297                 talloc_free(*res);
298                 *res = NULL;
299                 return ret;
300         }
301
302         msg2 = ltdb_pull_attrs(module, msg, attrs);
303
304         talloc_free(msg);
305
306         if (!msg2) {
307                 return -1;              
308         }
309
310         (*res)[0] = talloc_steal(*res, msg2);
311         (*res)[1] = NULL;
312
313         return 1;
314 }
315
316
317 /*
318   add a set of attributes from a record to a set of results
319   return 0 on success, -1 on failure
320 */
321 int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
322                           const char * const attrs[], 
323                           int *count, 
324                           struct ldb_message ***res)
325 {
326         struct ldb_context *ldb = module->ldb;
327         struct ldb_message *msg2;
328         struct ldb_message **res2;
329
330         /* pull the attributes that the user wants */
331         msg2 = ltdb_pull_attrs(module, msg, attrs);
332         if (!msg2) {
333                 return -1;
334         }
335
336         /* add to the results list */
337         res2 = talloc_realloc(ldb, *res, struct ldb_message *, (*count)+2);
338         if (!res2) {
339                 talloc_free(msg2);
340                 return -1;
341         }
342
343         (*res) = res2;
344
345         (*res)[*count] = talloc_steal(*res, msg2);
346         (*res)[(*count)+1] = NULL;
347         (*count)++;
348
349         return 0;
350 }
351
352
353 /*
354   internal search state during a full db search
355 */
356 struct ltdb_search_info {
357         struct ldb_module *module;
358         struct ldb_parse_tree *tree;
359         const char *base;
360         enum ldb_scope scope;
361         const char * const *attrs;
362         struct ldb_message **msgs;
363         int failures;
364         int count;
365 };
366
367
368 /*
369   search function for a non-indexed search
370  */
371 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
372 {
373         struct ltdb_search_info *sinfo = state;
374         struct ldb_message *msg;
375         int ret;
376
377         if (key.dsize < 4 || 
378             strncmp(key.dptr, "DN=", 3) != 0) {
379                 return 0;
380         }
381
382         msg = talloc(sinfo, struct ldb_message);
383         if (msg == NULL) {
384                 return -1;
385         }
386
387         /* unpack the record */
388         ret = ltdb_unpack_data(sinfo->module, &data, msg);
389         if (ret == -1) {
390                 sinfo->failures++;
391                 talloc_free(msg);
392                 return 0;
393         }
394
395         if (!msg->dn) {
396                 msg->dn = key.dptr + 3;
397         }
398
399         /* see if it matches the given expression */
400         if (!ltdb_message_match(sinfo->module, msg, sinfo->tree, 
401                                 sinfo->base, sinfo->scope)) {
402                 talloc_free(msg);
403                 return 0;
404         }
405
406         ret = ltdb_add_attr_results(sinfo->module, msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
407
408         if (ret == -1) {
409                 sinfo->failures++;
410         }
411
412         talloc_free(msg);
413
414         return ret;
415 }
416
417
418 /*
419   search the database with a LDAP-like expression.
420   this is the "full search" non-indexed variant
421 */
422 static int ltdb_search_full(struct ldb_module *module, 
423                             const char *base,
424                             enum ldb_scope scope,
425                             struct ldb_parse_tree *tree,
426                             const char * const attrs[], struct ldb_message ***res)
427 {
428         struct ltdb_private *ltdb = module->private_data;
429         int ret, count;
430         struct ltdb_search_info *sinfo;
431
432         sinfo = talloc(ltdb, struct ltdb_search_info);
433         if (sinfo == NULL) {
434                 return -1;
435         }
436
437         sinfo->tree = tree;
438         sinfo->module = module;
439         sinfo->scope = scope;
440         sinfo->base = base;
441         sinfo->attrs = attrs;
442         sinfo->msgs = NULL;
443         sinfo->count = 0;
444         sinfo->failures = 0;
445
446         ret = tdb_traverse(ltdb->tdb, search_func, sinfo);
447
448         if (ret == -1) {
449                 talloc_free(sinfo);
450                 return -1;
451         }
452
453         *res = talloc_steal(ltdb, sinfo->msgs);
454         count = sinfo->count;
455
456         talloc_free(sinfo);
457
458         return count;
459 }
460
461
462 /*
463   search the database with a LDAP-like expression.
464   choses a search method
465 */
466 int ltdb_search(struct ldb_module *module, const char *base,
467                 enum ldb_scope scope, const char *expression,
468                 const char * const attrs[], struct ldb_message ***res)
469 {
470         struct ldb_context *ldb = module->ldb;
471         struct ltdb_private *ltdb = module->private_data;
472         struct ldb_parse_tree *tree;
473         int ret;
474
475         if (ltdb_lock_read(module) != 0) {
476                 return -1;
477         }
478
479         ltdb->last_err_string = NULL;
480
481         if (ltdb_cache_load(module) != 0) {
482                 ltdb_unlock_read(module);
483                 return -1;
484         }
485
486         *res = NULL;
487
488         /* form a parse tree for the expression */
489         tree = ldb_parse_tree(ldb, expression);
490         if (!tree) {
491                 ltdb->last_err_string = "expression parse failed";
492                 ltdb_unlock_read(module);
493                 return -1;
494         }
495
496         if (tree->operation == LDB_OP_SIMPLE && 
497             (ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 ||
498              ldb_attr_cmp(tree->u.simple.attr, "distinguishedName") == 0) &&
499             !ltdb_has_wildcard(module, tree->u.simple.attr, &tree->u.simple.value)) {
500                 /* yay! its a nice simple one */
501                 ret = ltdb_search_dn(module, tree->u.simple.value.data, attrs, res);
502         } else {
503                 ret = ltdb_search_indexed(module, base, scope, tree, attrs, res);
504                 if (ret == -1) {
505                         ret = ltdb_search_full(module, base, scope, tree, attrs, res);
506                 }
507         }
508
509         talloc_free(tree);
510         ltdb_unlock_read(module);
511
512         return ret;
513 }
514