ldb_tdb: raise level of full index scan message so that it starts to be really visible
[mat/samba.git] / 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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb search functions
28  *
29  *  Description: functions to search ldb+tdb databases
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb_tdb.h"
35 #include "ldb_private.h"
36 #include <tdb.h>
37
38 /*
39   add one element to a message
40 */
41 static int msg_add_element(struct ldb_message *ret, 
42                            const struct ldb_message_element *el,
43                            int check_duplicates)
44 {
45         unsigned int i;
46         struct ldb_message_element *e2, *elnew;
47
48         if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
49                 /* its already there */
50                 return 0;
51         }
52
53         e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
54         if (!e2) {
55                 return -1;
56         }
57         ret->elements = e2;
58         
59         elnew = &e2[ret->num_elements];
60
61         elnew->name = talloc_strdup(ret->elements, el->name);
62         if (!elnew->name) {
63                 return -1;
64         }
65
66         if (el->num_values) {
67                 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
68                 if (!elnew->values) {
69                         return -1;
70                 }
71         } else {
72                 elnew->values = NULL;
73         }
74
75         for (i=0;i<el->num_values;i++) {
76                 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
77                 if (elnew->values[i].length != el->values[i].length) {
78                         return -1;
79                 }
80         }
81
82         elnew->num_values = el->num_values;
83         elnew->flags = el->flags;
84
85         ret->num_elements++;
86
87         return 0;
88 }
89
90 /*
91   add the special distinguishedName element
92 */
93 static int msg_add_distinguished_name(struct ldb_message *msg)
94 {
95         struct ldb_message_element el;
96         struct ldb_val val;
97         int ret;
98
99         el.flags = 0;
100         el.name = "distinguishedName";
101         el.num_values = 1;
102         el.values = &val;
103         el.flags = 0;
104         val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
105         val.length = strlen((char *)val.data);
106         
107         ret = msg_add_element(msg, &el, 1);
108         return ret;
109 }
110
111 /*
112   add all elements from one message into another
113  */
114 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
115                                 const struct ldb_message *msg)
116 {
117         struct ldb_context *ldb;
118         unsigned int i;
119         int check_duplicates = (ret->num_elements != 0);
120
121         ldb = ldb_module_get_ctx(module);
122
123         if (msg_add_distinguished_name(ret) != 0) {
124                 return -1;
125         }
126
127         for (i=0;i<msg->num_elements;i++) {
128                 const struct ldb_schema_attribute *a;
129                 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
130                 if (a->flags & LDB_ATTR_FLAG_HIDDEN) {
131                         continue;
132                 }
133                 if (msg_add_element(ret, &msg->elements[i],
134                                     check_duplicates) != 0) {
135                         return -1;
136                 }
137         }
138
139         return 0;
140 }
141
142
143 /*
144   pull the specified list of attributes from a message
145  */
146 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module, 
147                                            TALLOC_CTX *mem_ctx, 
148                                            const struct ldb_message *msg, 
149                                            const char * const *attrs)
150 {
151         struct ldb_message *ret;
152         unsigned int i;
153
154         ret = talloc(mem_ctx, struct ldb_message);
155         if (!ret) {
156                 return NULL;
157         }
158
159         ret->dn = ldb_dn_copy(ret, msg->dn);
160         if (!ret->dn) {
161                 talloc_free(ret);
162                 return NULL;
163         }
164
165         ret->num_elements = 0;
166         ret->elements = NULL;
167
168         if (!attrs) {
169                 if (msg_add_all_elements(module, ret, msg) != 0) {
170                         talloc_free(ret);
171                         return NULL;
172                 }
173                 return ret;
174         }
175
176         for (i=0;attrs[i];i++) {
177                 struct ldb_message_element *el;
178
179                 if (strcmp(attrs[i], "*") == 0) {
180                         if (msg_add_all_elements(module, ret, msg) != 0) {
181                                 talloc_free(ret);
182                                 return NULL;
183                         }
184                         continue;
185                 }
186
187                 if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
188                         if (msg_add_distinguished_name(ret) != 0) {
189                                 return NULL;
190                         }
191                         continue;
192                 }
193
194                 el = ldb_msg_find_element(msg, attrs[i]);
195                 if (!el) {
196                         continue;
197                 }
198                 if (msg_add_element(ret, el, 1) != 0) {
199                         talloc_free(ret);
200                         return NULL;                            
201                 }
202         }
203
204         return ret;
205 }
206
207 /*
208   search the database for a single simple dn.
209   return LDB_ERR_NO_SUCH_OBJECT on record-not-found
210   and LDB_SUCCESS on success
211 */
212 static int ltdb_search_base(struct ldb_module *module, struct ldb_dn *dn)
213 {
214         void *data = ldb_module_get_private(module);
215         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
216         TDB_DATA tdb_key;
217         int exists;
218
219         if (ldb_dn_is_null(dn)) {
220                 return LDB_ERR_NO_SUCH_OBJECT;
221         }
222
223         /* form the key */
224         tdb_key = ltdb_key(module, dn);
225         if (!tdb_key.dptr) {
226                 return LDB_ERR_OPERATIONS_ERROR;
227         }
228
229         exists = tdb_exists(ltdb->tdb, tdb_key);
230         talloc_free(tdb_key.dptr);
231                 
232         if (exists) {
233                 return LDB_SUCCESS;
234         }
235         return LDB_ERR_NO_SUCH_OBJECT;
236 }
237
238 struct ltdb_parse_data_unpack_ctx {
239         struct ldb_message *msg;
240         struct ldb_module *module;
241 };
242
243 static int ltdb_parse_data_unpack(TDB_DATA key, TDB_DATA data,
244                                   void *private_data)
245 {
246         struct ltdb_parse_data_unpack_ctx *ctx = private_data;
247
248         struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
249         int ret = ldb_unpack_data(ldb, (struct ldb_val *)&data, ctx->msg);
250         if (ret == -1) {
251                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %*.*s\n",
252                           (int)key.dsize, (int)key.dsize, key.dptr);
253                 return LDB_ERR_OPERATIONS_ERROR;                
254         }
255         return ret;
256 }
257
258 /*
259   search the database for a single simple dn, returning all attributes
260   in a single message
261
262   return LDB_ERR_NO_SUCH_OBJECT on record-not-found
263   and LDB_SUCCESS on success
264 */
265 int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg)
266 {
267         void *data = ldb_module_get_private(module);
268         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
269         int ret;
270         TDB_DATA tdb_key;
271         struct ltdb_parse_data_unpack_ctx ctx = {
272                 .msg = msg,
273                 .module = module
274         };
275
276         /* form the key */
277         tdb_key = ltdb_key(module, dn);
278         if (!tdb_key.dptr) {
279                 return LDB_ERR_OPERATIONS_ERROR;
280         }
281
282         memset(msg, 0, sizeof(*msg));
283
284         msg->num_elements = 0;
285         msg->elements = NULL;
286
287         ret = tdb_parse_record(ltdb->tdb, tdb_key, 
288                                ltdb_parse_data_unpack, &ctx); 
289         talloc_free(tdb_key.dptr);
290         
291         if (ret == -1) {
292                 if (tdb_error(ltdb->tdb) == TDB_ERR_NOEXIST) {
293                         return LDB_ERR_NO_SUCH_OBJECT;
294                 }
295                 return LDB_ERR_OPERATIONS_ERROR;
296         } else if (ret != LDB_SUCCESS) {
297                 return ret;
298         }
299         
300         if (!msg->dn) {
301                 msg->dn = ldb_dn_copy(msg, dn);
302         }
303         if (!msg->dn) {
304                 return LDB_ERR_OPERATIONS_ERROR;
305         }
306
307         return LDB_SUCCESS;
308 }
309
310 /*
311   add a set of attributes from a record to a set of results
312   return 0 on success, -1 on failure
313 */
314 int ltdb_add_attr_results(struct ldb_module *module, 
315                           TALLOC_CTX *mem_ctx, 
316                           struct ldb_message *msg,
317                           const char * const attrs[], 
318                           unsigned int *count, 
319                           struct ldb_message ***res)
320 {
321         struct ldb_message *msg2;
322         struct ldb_message **res2;
323
324         /* pull the attributes that the user wants */
325         msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
326         if (!msg2) {
327                 return -1;
328         }
329
330         /* add to the results list */
331         res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
332         if (!res2) {
333                 talloc_free(msg2);
334                 return -1;
335         }
336
337         (*res) = res2;
338
339         (*res)[*count] = talloc_move(*res, &msg2);
340         (*res)[(*count)+1] = NULL;
341         (*count)++;
342
343         return 0;
344 }
345
346
347
348 /*
349   filter the specified list of attributes from a message
350   removing not requested attrs.
351  */
352 int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
353 {
354         unsigned int i;
355         int keep_all = 0;
356         struct ldb_message_element *el2;
357         uint32_t num_elements;
358
359         if (attrs) {
360                 /* check for special attrs */
361                 for (i = 0; attrs[i]; i++) {
362                         if (strcmp(attrs[i], "*") == 0) {
363                                 keep_all = 1;
364                                 break;
365                         }
366
367                         if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
368                                 if (msg_add_distinguished_name(msg) != 0) {
369                                         return -1;
370                                 }
371                         }
372                 }
373         } else {
374                 keep_all = 1;
375         }
376         
377         if (keep_all) {
378                 if (msg_add_distinguished_name(msg) != 0) {
379                         return -1;
380                 }
381                 return 0;
382         }
383
384         el2 = talloc_array(msg, struct ldb_message_element, msg->num_elements);
385         if (el2 == NULL) {
386                 return -1;
387         }
388         num_elements = 0;
389
390         for (i = 0; i < msg->num_elements; i++) {
391                 unsigned int j;
392                 int found = 0;
393                 
394                 for (j = 0; attrs[j]; j++) {
395                         if (ldb_attr_cmp(msg->elements[i].name, attrs[j]) == 0) {
396                                 found = 1;
397                                 break;
398                         }
399                 }
400
401                 if (found) {
402                         el2[num_elements] = msg->elements[i];
403                         talloc_steal(el2, el2[num_elements].name);
404                         talloc_steal(el2, el2[num_elements].values);
405                         num_elements++;
406                 }
407         }
408
409         talloc_free(msg->elements);
410         msg->elements = talloc_realloc(msg, el2, struct ldb_message_element, msg->num_elements);
411         if (msg->elements == NULL) {
412                 return -1;
413         }
414         msg->num_elements = num_elements;
415
416         return 0;
417 }
418
419 /*
420   search function for a non-indexed search
421  */
422 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
423 {
424         struct ldb_context *ldb;
425         struct ltdb_context *ac;
426         struct ldb_message *msg;
427         int ret;
428         bool matched;
429
430         ac = talloc_get_type(state, struct ltdb_context);
431         ldb = ldb_module_get_ctx(ac->module);
432
433         if (key.dsize < 4 || 
434             strncmp((char *)key.dptr, "DN=", 3) != 0) {
435                 return 0;
436         }
437
438         msg = ldb_msg_new(ac);
439         if (!msg) {
440                 return -1;
441         }
442
443         /* unpack the record */
444         ret = ldb_unpack_data(ldb, (struct ldb_val *)&data, msg);
445         if (ret == -1) {
446                 talloc_free(msg);
447                 return -1;
448         }
449
450         if (!msg->dn) {
451                 msg->dn = ldb_dn_new(msg, ldb,
452                                      (char *)key.dptr + 3);
453                 if (msg->dn == NULL) {
454                         talloc_free(msg);
455                         return -1;
456                 }
457         }
458
459         /* see if it matches the given expression */
460         ret = ldb_match_msg_error(ldb, msg,
461                                   ac->tree, ac->base, ac->scope, &matched);
462         if (ret != LDB_SUCCESS) {
463                 talloc_free(msg);
464                 return -1;
465         }
466         if (!matched) {
467                 talloc_free(msg);
468                 return 0;
469         }
470
471         /* filter the attributes that the user wants */
472         ret = ltdb_filter_attrs(msg, ac->attrs);
473
474         if (ret == -1) {
475                 talloc_free(msg);
476                 return -1;
477         }
478
479         ret = ldb_module_send_entry(ac->req, msg, NULL);
480         if (ret != LDB_SUCCESS) {
481                 ac->request_terminated = true;
482                 /* the callback failed, abort the operation */
483                 return -1;
484         }
485
486         return 0;
487 }
488
489
490 /*
491   search the database with a LDAP-like expression.
492   this is the "full search" non-indexed variant
493 */
494 static int ltdb_search_full(struct ltdb_context *ctx)
495 {
496         void *data = ldb_module_get_private(ctx->module);
497         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
498         int ret;
499
500         if (ltdb->in_transaction != 0) {
501                 ret = tdb_traverse(ltdb->tdb, search_func, ctx);
502         } else {
503                 ret = tdb_traverse_read(ltdb->tdb, search_func, ctx);
504         }
505
506         if (ret < 0) {
507                 return LDB_ERR_OPERATIONS_ERROR;
508         }
509
510         return LDB_SUCCESS;
511 }
512
513 /*
514   search the database with a LDAP-like expression.
515   choses a search method
516 */
517 int ltdb_search(struct ltdb_context *ctx)
518 {
519         struct ldb_context *ldb;
520         struct ldb_module *module = ctx->module;
521         struct ldb_request *req = ctx->req;
522         void *data = ldb_module_get_private(module);
523         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
524         int ret;
525
526         ldb = ldb_module_get_ctx(module);
527
528         ldb_request_set_state(req, LDB_ASYNC_PENDING);
529
530         if (ltdb_lock_read(module) != 0) {
531                 return LDB_ERR_OPERATIONS_ERROR;
532         }
533
534         if (ltdb_cache_load(module) != 0) {
535                 ltdb_unlock_read(module);
536                 return LDB_ERR_OPERATIONS_ERROR;
537         }
538
539         if (req->op.search.tree == NULL) {
540                 ltdb_unlock_read(module);
541                 return LDB_ERR_OPERATIONS_ERROR;
542         }
543
544         if ((req->op.search.base == NULL) || (ldb_dn_is_null(req->op.search.base) == true)) {
545
546                 /* Check what we should do with a NULL dn */
547                 switch (req->op.search.scope) {
548                 case LDB_SCOPE_BASE:
549                         ldb_asprintf_errstring(ldb, 
550                                                "NULL Base DN invalid for a base search");
551                         ret = LDB_ERR_INVALID_DN_SYNTAX;
552                         break;
553                 case LDB_SCOPE_ONELEVEL:
554                         ldb_asprintf_errstring(ldb, 
555                                                "NULL Base DN invalid for a one-level search");
556                         ret = LDB_ERR_INVALID_DN_SYNTAX;        
557                         break;
558                 case LDB_SCOPE_SUBTREE:
559                 default:
560                         /* We accept subtree searches from a NULL base DN, ie over the whole DB */
561                         ret = LDB_SUCCESS;
562                 }
563         } else if (ldb_dn_is_valid(req->op.search.base) == false) {
564
565                 /* We don't want invalid base DNs here */
566                 ldb_asprintf_errstring(ldb, 
567                                        "Invalid Base DN: %s", 
568                                        ldb_dn_get_linearized(req->op.search.base));
569                 ret = LDB_ERR_INVALID_DN_SYNTAX;
570
571         } else if (ltdb->check_base) {
572                 /* This database has been marked as 'checkBaseOnSearch', so do a spot check of the base dn */
573                 ret = ltdb_search_base(module, req->op.search.base);
574                 
575                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
576                         ldb_asprintf_errstring(ldb, 
577                                                "No such Base DN: %s", 
578                                                ldb_dn_get_linearized(req->op.search.base));
579                 }
580                         
581         } else {
582                 /* If we are not checking the base DN life is easy */
583                 ret = LDB_SUCCESS;
584         }
585
586         ctx->tree = req->op.search.tree;
587         ctx->scope = req->op.search.scope;
588         ctx->base = req->op.search.base;
589         ctx->attrs = req->op.search.attrs;
590
591         if (ret == LDB_SUCCESS) {
592                 uint32_t match_count = 0;
593
594                 ret = ltdb_search_indexed(ctx, &match_count);
595                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
596                         /* Not in the index, therefore OK! */
597                         ret = LDB_SUCCESS;
598                         
599                 }
600                 /* Check if we got just a normal error.
601                  * In that case proceed to a full search unless we got a
602                  * callback error */
603                 if ( ! ctx->request_terminated && ret != LDB_SUCCESS) {
604                         /* Not indexed, so we need to do a full scan */
605                         if (ltdb->warn_unindexed) {
606                                 /* useful for debugging when slow performance
607                                  * is caused by unindexed searches */
608                                 char *expression = ldb_filter_from_tree(ctx, ctx->tree);
609                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb FULL SEARCH: %s SCOPE: %s DN: %s",
610                                                         expression,
611                                                         req->op.search.scope==LDB_SCOPE_BASE?"base":
612                                                         req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
613                                                         req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN",
614                                                         ldb_dn_get_linearized(req->op.search.base));
615
616                                 talloc_free(expression);
617                         }
618                         if (match_count != 0) {
619                                 /* the indexing code gave an error
620                                  * after having returned at least one
621                                  * entry. This means the indexes are
622                                  * corrupt or a database record is
623                                  * corrupt. We cannot continue with a
624                                  * full search or we may return
625                                  * duplicate entries
626                                  */
627                                 ltdb_unlock_read(module);
628                                 return LDB_ERR_OPERATIONS_ERROR;
629                         }
630                         ret = ltdb_search_full(ctx);
631                         if (ret != LDB_SUCCESS) {
632                                 ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
633                         }
634                 }
635         }
636
637         ltdb_unlock_read(module);
638
639         return ret;
640 }
641