4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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
28 * Component: ldb expression matching
30 * Description: ldb expression matching for tdb backend
32 * Author: Andrew Tridgell
39 see if two ldb_val structures contain the same data
40 return 1 for a match, 0 for a mis-match
42 int ldb_val_equal(const struct ldb_val *v1, const struct ldb_val *v2)
44 if (v1->length != v2->length) return 0;
46 if (v1->length == 0) return 1;
48 if (memcmp(v1->data, v2->data, v1->length) == 0) {
56 check if the scope matches in a search result
58 static int scope_match(const char *dn, const char *base, enum ldb_scope scope)
60 size_t dn_len, base_len;
66 base_len = strlen(base);
69 if (strcmp(dn, base) == 0) {
73 if (base_len+1 >= dn_len) {
81 case LDB_SCOPE_ONELEVEL:
82 if (strcmp(dn + (dn_len - base_len), base) == 0 &&
83 dn[dn_len - base_len - 1] == ',' &&
84 strchr(dn, ',') == &dn[dn_len - base_len - 1]) {
89 case LDB_SCOPE_SUBTREE:
91 if (strcmp(dn + (dn_len - base_len), base) == 0 &&
92 dn[dn_len - base_len - 1] == ',') {
105 static int match_leaf(struct ldb_context *ldb,
106 struct ldb_message *msg,
107 struct ldb_parse_tree *tree,
109 enum ldb_scope scope)
113 if (!scope_match(msg->dn, base, scope)) {
117 if (strcmp(tree->u.simple.attr, "dn") == 0) {
118 if (strcmp(tree->u.simple.value.data, "*") == 0) {
121 return strcmp(msg->dn, tree->u.simple.value.data) == 0;
124 for (i=0;i<msg->num_elements;i++) {
125 if (strcmp(msg->elements[i].name, tree->u.simple.attr) == 0) {
126 if (strcmp(tree->u.simple.value.data, "*") == 0) {
129 for (j=0;j<msg->elements[i].num_values;j++) {
130 if (ldb_val_equal(&msg->elements[i].values[j],
131 &tree->u.simple.value)) {
142 return 0 if the given parse tree matches the given message. Assumes
143 the message is in sorted order
145 return 1 if it matches, and 0 if it doesn't match
147 this is a recursive function, and does short-circuit evaluation
149 int ldb_message_match(struct ldb_context *ldb,
150 struct ldb_message *msg,
151 struct ldb_parse_tree *tree,
153 enum ldb_scope scope)
157 switch (tree->operation) {
162 return ! ldb_message_match(ldb, msg, tree->u.not.child, base, scope);
165 for (i=0;i<tree->u.list.num_elements;i++) {
166 v = ldb_message_match(ldb, msg, tree->u.list.elements[i],
173 for (i=0;i<tree->u.list.num_elements;i++) {
174 v = ldb_message_match(ldb, msg, tree->u.list.elements[i],
181 return match_leaf(ldb, msg, tree, base, scope);