CVE-2019-3824 ldb: wildcard_match check tree operation
[Samba.git] / lib / ldb / common / ldb_match.c
blob59f48b52b70449781f2073649e4bd616fa296cad
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004-2005
5 Copyright (C) Simo Sorce 2005
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
9 ** under the LGPL
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 * Name: ldb
28 * Component: ldb expression matching
30 * Description: ldb expression matching
32 * Author: Andrew Tridgell
35 #include "ldb_private.h"
36 #include "dlinklist.h"
39 check if the scope matches in a search result
41 static int ldb_match_scope(struct ldb_context *ldb,
42 struct ldb_dn *base,
43 struct ldb_dn *dn,
44 enum ldb_scope scope)
46 int ret = 0;
48 if (base == NULL || dn == NULL) {
49 return 1;
52 switch (scope) {
53 case LDB_SCOPE_BASE:
54 if (ldb_dn_compare(base, dn) == 0) {
55 ret = 1;
57 break;
59 case LDB_SCOPE_ONELEVEL:
60 if (ldb_dn_get_comp_num(dn) == (ldb_dn_get_comp_num(base) + 1)) {
61 if (ldb_dn_compare_base(base, dn) == 0) {
62 ret = 1;
65 break;
67 case LDB_SCOPE_SUBTREE:
68 default:
69 if (ldb_dn_compare_base(base, dn) == 0) {
70 ret = 1;
72 break;
75 return ret;
80 match if node is present
82 static int ldb_match_present(struct ldb_context *ldb,
83 const struct ldb_message *msg,
84 const struct ldb_parse_tree *tree,
85 enum ldb_scope scope, bool *matched)
87 const struct ldb_schema_attribute *a;
88 struct ldb_message_element *el;
90 if (ldb_attr_dn(tree->u.present.attr) == 0) {
91 *matched = true;
92 return LDB_SUCCESS;
95 el = ldb_msg_find_element(msg, tree->u.present.attr);
96 if (el == NULL) {
97 *matched = false;
98 return LDB_SUCCESS;
101 a = ldb_schema_attribute_by_name(ldb, el->name);
102 if (!a) {
103 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
106 if (a->syntax->operator_fn) {
107 unsigned int i;
108 for (i = 0; i < el->num_values; i++) {
109 int ret = a->syntax->operator_fn(ldb, LDB_OP_PRESENT, a, &el->values[i], NULL, matched);
110 if (ret != LDB_SUCCESS) return ret;
111 if (*matched) return LDB_SUCCESS;
113 *matched = false;
114 return LDB_SUCCESS;
117 *matched = true;
118 return LDB_SUCCESS;
121 static int ldb_match_comparison(struct ldb_context *ldb,
122 const struct ldb_message *msg,
123 const struct ldb_parse_tree *tree,
124 enum ldb_scope scope,
125 enum ldb_parse_op comp_op, bool *matched)
127 unsigned int i;
128 struct ldb_message_element *el;
129 const struct ldb_schema_attribute *a;
131 /* FIXME: APPROX comparison not handled yet */
132 if (comp_op == LDB_OP_APPROX) {
133 return LDB_ERR_INAPPROPRIATE_MATCHING;
136 el = ldb_msg_find_element(msg, tree->u.comparison.attr);
137 if (el == NULL) {
138 *matched = false;
139 return LDB_SUCCESS;
142 a = ldb_schema_attribute_by_name(ldb, el->name);
143 if (!a) {
144 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
147 for (i = 0; i < el->num_values; i++) {
148 if (a->syntax->operator_fn) {
149 int ret;
150 ret = a->syntax->operator_fn(ldb, comp_op, a, &el->values[i], &tree->u.comparison.value, matched);
151 if (ret != LDB_SUCCESS) return ret;
152 if (*matched) return LDB_SUCCESS;
153 } else {
154 int ret = a->syntax->comparison_fn(ldb, ldb, &el->values[i], &tree->u.comparison.value);
156 if (ret == 0) {
157 *matched = true;
158 return LDB_SUCCESS;
160 if (ret > 0 && comp_op == LDB_OP_GREATER) {
161 *matched = true;
162 return LDB_SUCCESS;
164 if (ret < 0 && comp_op == LDB_OP_LESS) {
165 *matched = true;
166 return LDB_SUCCESS;
171 *matched = false;
172 return LDB_SUCCESS;
176 match a simple leaf node
178 static int ldb_match_equality(struct ldb_context *ldb,
179 const struct ldb_message *msg,
180 const struct ldb_parse_tree *tree,
181 enum ldb_scope scope,
182 bool *matched)
184 unsigned int i;
185 struct ldb_message_element *el;
186 const struct ldb_schema_attribute *a;
187 struct ldb_dn *valuedn;
188 int ret;
190 if (ldb_attr_dn(tree->u.equality.attr) == 0) {
191 valuedn = ldb_dn_from_ldb_val(ldb, ldb, &tree->u.equality.value);
192 if (valuedn == NULL) {
193 return LDB_ERR_INVALID_DN_SYNTAX;
196 ret = ldb_dn_compare(msg->dn, valuedn);
198 talloc_free(valuedn);
200 *matched = (ret == 0);
201 return LDB_SUCCESS;
204 /* TODO: handle the "*" case derived from an extended search
205 operation without the attibute type defined */
206 el = ldb_msg_find_element(msg, tree->u.equality.attr);
207 if (el == NULL) {
208 *matched = false;
209 return LDB_SUCCESS;
212 a = ldb_schema_attribute_by_name(ldb, el->name);
213 if (a == NULL) {
214 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
217 for (i=0;i<el->num_values;i++) {
218 if (a->syntax->operator_fn) {
219 ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
220 &tree->u.equality.value, &el->values[i], matched);
221 if (ret != LDB_SUCCESS) return ret;
222 if (*matched) return LDB_SUCCESS;
223 } else {
224 if (a->syntax->comparison_fn(ldb, ldb, &tree->u.equality.value,
225 &el->values[i]) == 0) {
226 *matched = true;
227 return LDB_SUCCESS;
232 *matched = false;
233 return LDB_SUCCESS;
236 static int ldb_wildcard_compare(struct ldb_context *ldb,
237 const struct ldb_parse_tree *tree,
238 const struct ldb_val value, bool *matched)
240 const struct ldb_schema_attribute *a;
241 struct ldb_val val;
242 struct ldb_val cnk;
243 struct ldb_val *chunk;
244 uint8_t *save_p = NULL;
245 unsigned int c = 0;
247 if (tree->operation != LDB_OP_SUBSTRING) {
248 *matched = false;
249 return LDB_ERR_INAPPROPRIATE_MATCHING;
252 a = ldb_schema_attribute_by_name(ldb, tree->u.substring.attr);
253 if (!a) {
254 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
257 if (tree->u.substring.chunks == NULL) {
258 *matched = false;
259 return LDB_SUCCESS;
262 if (a->syntax->canonicalise_fn(ldb, ldb, &value, &val) != 0) {
263 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
266 save_p = val.data;
267 cnk.data = NULL;
269 if ( ! tree->u.substring.start_with_wildcard ) {
271 chunk = tree->u.substring.chunks[c];
272 if (a->syntax->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto mismatch;
274 /* This deals with wildcard prefix searches on binary attributes (eg objectGUID) */
275 if (cnk.length > val.length) {
276 goto mismatch;
279 * Empty strings are returned as length 0. Ensure
280 * we can cope with this.
282 if (cnk.length == 0) {
283 goto mismatch;
286 if (memcmp((char *)val.data, (char *)cnk.data, cnk.length) != 0) goto mismatch;
287 val.length -= cnk.length;
288 val.data += cnk.length;
289 c++;
290 talloc_free(cnk.data);
291 cnk.data = NULL;
294 while (tree->u.substring.chunks[c]) {
295 uint8_t *p;
297 chunk = tree->u.substring.chunks[c];
298 if(a->syntax->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto mismatch;
301 * Empty strings are returned as length 0. Ensure
302 * we can cope with this.
304 if (cnk.length == 0) {
305 goto mismatch;
308 * Values might be binary blobs. Don't use string
309 * search, but memory search instead.
311 p = memmem((const void *)val.data,val.length,
312 (const void *)cnk.data, cnk.length);
313 if (p == NULL) goto mismatch;
316 * At this point we know cnk.length <= val.length as
317 * otherwise there could be no match
320 if ( (! tree->u.substring.chunks[c + 1]) && (! tree->u.substring.end_with_wildcard) ) {
321 uint8_t *g;
322 uint8_t *end = val.data + val.length;
323 do { /* greedy */
326 * haystack is a valid pointer in val
327 * because the memmem() can only
328 * succeed if the needle (cnk.length)
329 * is <= haystacklen
331 * p will be a pointer at least
332 * cnk.length from the end of haystack
334 uint8_t *haystack
335 = p + cnk.length;
336 size_t haystacklen
337 = end - (haystack);
339 g = memmem(haystack,
340 haystacklen,
341 (const uint8_t *)cnk.data,
342 cnk.length);
343 if (g) {
344 p = g;
346 } while(g);
348 val.length = val.length - (p - (uint8_t *)(val.data)) - cnk.length;
349 val.data = (uint8_t *)(p + cnk.length);
350 c++;
351 talloc_free(cnk.data);
352 cnk.data = NULL;
355 /* last chunk may not have reached end of string */
356 if ( (! tree->u.substring.end_with_wildcard) && (*(val.data) != 0) ) goto mismatch;
357 talloc_free(save_p);
358 *matched = true;
359 return LDB_SUCCESS;
361 mismatch:
362 *matched = false;
363 talloc_free(save_p);
364 talloc_free(cnk.data);
365 return LDB_SUCCESS;
369 match a simple leaf node
371 static int ldb_match_substring(struct ldb_context *ldb,
372 const struct ldb_message *msg,
373 const struct ldb_parse_tree *tree,
374 enum ldb_scope scope, bool *matched)
376 unsigned int i;
377 struct ldb_message_element *el;
379 el = ldb_msg_find_element(msg, tree->u.substring.attr);
380 if (el == NULL) {
381 *matched = false;
382 return LDB_SUCCESS;
385 for (i = 0; i < el->num_values; i++) {
386 int ret;
387 ret = ldb_wildcard_compare(ldb, tree, el->values[i], matched);
388 if (ret != LDB_SUCCESS) return ret;
389 if (*matched) return LDB_SUCCESS;
392 *matched = false;
393 return LDB_SUCCESS;
398 bitwise and/or comparator depending on oid
400 static int ldb_comparator_bitmask(const char *oid, const struct ldb_val *v1, const struct ldb_val *v2,
401 bool *matched)
403 uint64_t i1, i2;
404 char ibuf[100];
405 char *endptr = NULL;
407 if (v1->length >= sizeof(ibuf)-1) {
408 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
410 memcpy(ibuf, (char *)v1->data, v1->length);
411 ibuf[v1->length] = 0;
412 i1 = strtoull(ibuf, &endptr, 0);
413 if (endptr != NULL) {
414 if (endptr == ibuf || *endptr != 0) {
415 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
419 if (v2->length >= sizeof(ibuf)-1) {
420 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
422 endptr = NULL;
423 memcpy(ibuf, (char *)v2->data, v2->length);
424 ibuf[v2->length] = 0;
425 i2 = strtoull(ibuf, &endptr, 0);
426 if (endptr != NULL) {
427 if (endptr == ibuf || *endptr != 0) {
428 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
431 if (strcmp(LDB_OID_COMPARATOR_AND, oid) == 0) {
432 *matched = ((i1 & i2) == i2);
433 } else if (strcmp(LDB_OID_COMPARATOR_OR, oid) == 0) {
434 *matched = ((i1 & i2) != 0);
435 } else {
436 return LDB_ERR_INAPPROPRIATE_MATCHING;
438 return LDB_SUCCESS;
441 static int ldb_match_bitmask(struct ldb_context *ldb,
442 const char *oid,
443 const struct ldb_message *msg,
444 const char *attribute_to_match,
445 const struct ldb_val *value_to_match,
446 bool *matched)
448 unsigned int i;
449 struct ldb_message_element *el;
451 /* find the message element */
452 el = ldb_msg_find_element(msg, attribute_to_match);
453 if (el == NULL) {
454 *matched = false;
455 return LDB_SUCCESS;
458 for (i=0;i<el->num_values;i++) {
459 int ret;
460 struct ldb_val *v = &el->values[i];
462 ret = ldb_comparator_bitmask(oid, v, value_to_match, matched);
463 if (ret != LDB_SUCCESS) {
464 return ret;
466 if (*matched) {
467 return LDB_SUCCESS;
471 *matched = false;
472 return LDB_SUCCESS;
476 always return false
478 static int ldb_comparator_false(struct ldb_context *ldb,
479 const char *oid,
480 const struct ldb_message *msg,
481 const char *attribute_to_match,
482 const struct ldb_val *value_to_match,
483 bool *matched)
485 *matched = false;
486 return LDB_SUCCESS;
490 static const struct ldb_extended_match_rule *ldb_find_extended_match_rule(struct ldb_context *ldb,
491 const char *oid)
493 struct ldb_extended_match_entry *extended_match_rule;
495 for (extended_match_rule = ldb->extended_match_rules;
496 extended_match_rule;
497 extended_match_rule = extended_match_rule->next) {
498 if (strcmp(extended_match_rule->rule->oid, oid) == 0) {
499 return extended_match_rule->rule;
503 return NULL;
508 extended match, handles things like bitops
510 static int ldb_match_extended(struct ldb_context *ldb,
511 const struct ldb_message *msg,
512 const struct ldb_parse_tree *tree,
513 enum ldb_scope scope, bool *matched)
515 const struct ldb_extended_match_rule *rule;
517 if (tree->u.extended.dnAttributes) {
518 /* FIXME: We really need to find out what this ":dn" part in
519 * an extended match means and how to handle it. For now print
520 * only a warning to have s3 winbind and other tools working
521 * against us. - Matthias */
522 ldb_debug(ldb, LDB_DEBUG_WARNING, "ldb: dnAttributes extended match not supported yet");
524 if (tree->u.extended.rule_id == NULL) {
525 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: no-rule extended matches not supported yet");
526 return LDB_ERR_INAPPROPRIATE_MATCHING;
528 if (tree->u.extended.attr == NULL) {
529 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: no-attribute extended matches not supported yet");
530 return LDB_ERR_INAPPROPRIATE_MATCHING;
533 rule = ldb_find_extended_match_rule(ldb, tree->u.extended.rule_id);
534 if (rule == NULL) {
535 *matched = false;
536 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: unknown extended rule_id %s",
537 tree->u.extended.rule_id);
538 return LDB_SUCCESS;
541 return rule->callback(ldb, rule->oid, msg,
542 tree->u.extended.attr,
543 &tree->u.extended.value, matched);
547 Check if a particular message will match the given filter
549 set *matched to true if it matches, false otherwise
551 returns LDB_SUCCESS or an error
553 this is a recursive function, and does short-circuit evaluation
555 int ldb_match_message(struct ldb_context *ldb,
556 const struct ldb_message *msg,
557 const struct ldb_parse_tree *tree,
558 enum ldb_scope scope, bool *matched)
560 unsigned int i;
561 int ret;
563 *matched = false;
565 if (scope != LDB_SCOPE_BASE && ldb_dn_is_special(msg->dn)) {
566 /* don't match special records except on base searches */
567 return LDB_SUCCESS;
570 switch (tree->operation) {
571 case LDB_OP_AND:
572 for (i=0;i<tree->u.list.num_elements;i++) {
573 ret = ldb_match_message(ldb, msg, tree->u.list.elements[i], scope, matched);
574 if (ret != LDB_SUCCESS) return ret;
575 if (!*matched) return LDB_SUCCESS;
577 *matched = true;
578 return LDB_SUCCESS;
580 case LDB_OP_OR:
581 for (i=0;i<tree->u.list.num_elements;i++) {
582 ret = ldb_match_message(ldb, msg, tree->u.list.elements[i], scope, matched);
583 if (ret != LDB_SUCCESS) return ret;
584 if (*matched) return LDB_SUCCESS;
586 *matched = false;
587 return LDB_SUCCESS;
589 case LDB_OP_NOT:
590 ret = ldb_match_message(ldb, msg, tree->u.isnot.child, scope, matched);
591 if (ret != LDB_SUCCESS) return ret;
592 *matched = ! *matched;
593 return LDB_SUCCESS;
595 case LDB_OP_EQUALITY:
596 return ldb_match_equality(ldb, msg, tree, scope, matched);
598 case LDB_OP_SUBSTRING:
599 return ldb_match_substring(ldb, msg, tree, scope, matched);
601 case LDB_OP_GREATER:
602 return ldb_match_comparison(ldb, msg, tree, scope, LDB_OP_GREATER, matched);
604 case LDB_OP_LESS:
605 return ldb_match_comparison(ldb, msg, tree, scope, LDB_OP_LESS, matched);
607 case LDB_OP_PRESENT:
608 return ldb_match_present(ldb, msg, tree, scope, matched);
610 case LDB_OP_APPROX:
611 return ldb_match_comparison(ldb, msg, tree, scope, LDB_OP_APPROX, matched);
613 case LDB_OP_EXTENDED:
614 return ldb_match_extended(ldb, msg, tree, scope, matched);
617 return LDB_ERR_INAPPROPRIATE_MATCHING;
621 return 0 if the given parse tree matches the given message. Assumes
622 the message is in sorted order
624 return 1 if it matches, and 0 if it doesn't match
627 int ldb_match_msg(struct ldb_context *ldb,
628 const struct ldb_message *msg,
629 const struct ldb_parse_tree *tree,
630 struct ldb_dn *base,
631 enum ldb_scope scope)
633 bool matched;
634 int ret;
636 if ( ! ldb_match_scope(ldb, base, msg->dn, scope) ) {
637 return 0;
640 ret = ldb_match_message(ldb, msg, tree, scope, &matched);
641 if (ret != LDB_SUCCESS) {
642 /* to match the old API, we need to consider this a
643 failure to match */
644 return 0;
646 return matched?1:0;
649 int ldb_match_msg_error(struct ldb_context *ldb,
650 const struct ldb_message *msg,
651 const struct ldb_parse_tree *tree,
652 struct ldb_dn *base,
653 enum ldb_scope scope,
654 bool *matched)
656 if ( ! ldb_match_scope(ldb, base, msg->dn, scope) ) {
657 *matched = false;
658 return LDB_SUCCESS;
661 return ldb_match_message(ldb, msg, tree, scope, matched);
664 int ldb_match_msg_objectclass(const struct ldb_message *msg,
665 const char *objectclass)
667 unsigned int i;
668 struct ldb_message_element *el = ldb_msg_find_element(msg, "objectClass");
669 if (!el) {
670 return 0;
672 for (i=0; i < el->num_values; i++) {
673 if (ldb_attr_cmp((const char *)el->values[i].data, objectclass) == 0) {
674 return 1;
677 return 0;
680 _PRIVATE_ int ldb_register_extended_match_rules(struct ldb_context *ldb)
682 struct ldb_extended_match_rule *bitmask_and;
683 struct ldb_extended_match_rule *bitmask_or;
684 struct ldb_extended_match_rule *always_false;
685 int ret;
687 /* Register bitmask-and match */
688 bitmask_and = talloc_zero(ldb, struct ldb_extended_match_rule);
689 if (bitmask_and == NULL) {
690 return LDB_ERR_OPERATIONS_ERROR;
693 bitmask_and->oid = LDB_OID_COMPARATOR_AND;
694 bitmask_and->callback = ldb_match_bitmask;
696 ret = ldb_register_extended_match_rule(ldb, bitmask_and);
697 if (ret != LDB_SUCCESS) {
698 return ret;
701 /* Register bitmask-or match */
702 bitmask_or = talloc_zero(ldb, struct ldb_extended_match_rule);
703 if (bitmask_or == NULL) {
704 return LDB_ERR_OPERATIONS_ERROR;
707 bitmask_or->oid = LDB_OID_COMPARATOR_OR;
708 bitmask_or->callback = ldb_match_bitmask;
710 ret = ldb_register_extended_match_rule(ldb, bitmask_or);
711 if (ret != LDB_SUCCESS) {
712 return ret;
715 /* Register always-false match */
716 always_false = talloc_zero(ldb, struct ldb_extended_match_rule);
717 if (always_false == NULL) {
718 return LDB_ERR_OPERATIONS_ERROR;
721 always_false->oid = SAMBA_LDAP_MATCH_ALWAYS_FALSE;
722 always_false->callback = ldb_comparator_false;
724 ret = ldb_register_extended_match_rule(ldb, always_false);
725 if (ret != LDB_SUCCESS) {
726 return ret;
729 return LDB_SUCCESS;
733 register a new ldb extended matching rule
735 int ldb_register_extended_match_rule(struct ldb_context *ldb,
736 const struct ldb_extended_match_rule *rule)
738 const struct ldb_extended_match_rule *lookup_rule;
739 struct ldb_extended_match_entry *entry;
741 lookup_rule = ldb_find_extended_match_rule(ldb, rule->oid);
742 if (lookup_rule) {
743 return LDB_ERR_ENTRY_ALREADY_EXISTS;
746 entry = talloc_zero(ldb, struct ldb_extended_match_entry);
747 if (!entry) {
748 return LDB_ERR_OPERATIONS_ERROR;
750 entry->rule = rule;
751 DLIST_ADD_END(ldb->extended_match_rules, entry);
753 return LDB_SUCCESS;