ldb_match - Ignore ":dn" part of extended matches for now
[ira/wip.git] / source4 / lib / ldb / common / ldb_match.c
index 1269d99a0f803b0cbbca6ae1aaa8632be21fe5c4..4bd121a438a8ef02d91c878696db37a0800dc36d 100644 (file)
@@ -11,7 +11,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,8 +19,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
  *  Author: Andrew Tridgell
  */
 
-#include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-
+#include "ldb_private.h"
 
 /*
   check if the scope matches in a search result
 */
 static int ldb_match_scope(struct ldb_context *ldb,
-                          const struct ldb_dn *base,
-                          const struct ldb_dn *dn,
+                          struct ldb_dn *base,
+                          struct ldb_dn *dn,
                           enum ldb_scope scope)
 {
        int ret = 0;
@@ -54,14 +50,14 @@ static int ldb_match_scope(struct ldb_context *ldb,
 
        switch (scope) {
        case LDB_SCOPE_BASE:
-               if (ldb_dn_compare(ldb, base, dn) == 0) {
+               if (ldb_dn_compare(base, dn) == 0) {
                        ret = 1;
                }
                break;
 
        case LDB_SCOPE_ONELEVEL:
-               if (dn->comp_num == (base->comp_num + 1)) {
-                       if (ldb_dn_compare_base(ldb, base, dn) == 0) {
+               if (ldb_dn_get_comp_num(dn) == (ldb_dn_get_comp_num(base) + 1)) {
+                       if (ldb_dn_compare_base(base, dn) == 0) {
                                ret = 1;
                        }
                }
@@ -69,7 +65,7 @@ static int ldb_match_scope(struct ldb_context *ldb,
                
        case LDB_SCOPE_SUBTREE:
        default:
-               if (ldb_dn_compare_base(ldb, base, dn) == 0) {
+               if (ldb_dn_compare_base(base, dn) == 0) {
                        ret = 1;
                }
                break;
@@ -83,12 +79,11 @@ static int ldb_match_scope(struct ldb_context *ldb,
   match if node is present
 */
 static int ldb_match_present(struct ldb_context *ldb, 
-                           struct ldb_message *msg,
-                           struct ldb_parse_tree *tree,
-                           enum ldb_scope scope)
+                            const struct ldb_message *msg,
+                            const struct ldb_parse_tree *tree,
+                            enum ldb_scope scope)
 {
-
-       if (ldb_attr_cmp(tree->u.present.attr, "dn") == 0) {
+       if (ldb_attr_dn(tree->u.present.attr) == 0) {
                return 1;
        }
 
@@ -100,14 +95,14 @@ static int ldb_match_present(struct ldb_context *ldb,
 }
 
 static int ldb_match_comparison(struct ldb_context *ldb, 
-                               struct ldb_message *msg,
-                               struct ldb_parse_tree *tree,
+                               const struct ldb_message *msg,
+                               const struct ldb_parse_tree *tree,
                                enum ldb_scope scope,
                                enum ldb_parse_op comp_op)
 {
        unsigned int i;
        struct ldb_message_element *el;
-       const struct ldb_attrib_handler *h;
+       const struct ldb_schema_attribute *a;
        int ret;
 
        /* FIXME: APPROX comparison not handled yet */
@@ -118,10 +113,10 @@ static int ldb_match_comparison(struct ldb_context *ldb,
                return 0;
        }
 
-       h = ldb_attrib_handler(ldb, el->name);
+       a = ldb_schema_attribute_by_name(ldb, el->name);
 
        for (i = 0; i < el->num_values; i++) {
-               ret = h->comparison_fn(ldb, ldb, &el->values[i], &tree->u.comparison.value);
+               ret = a->syntax->comparison_fn(ldb, ldb, &el->values[i], &tree->u.comparison.value);
 
                if (ret == 0) {
                        return 1;
@@ -141,24 +136,23 @@ static int ldb_match_comparison(struct ldb_context *ldb,
   match a simple leaf node
 */
 static int ldb_match_equality(struct ldb_context *ldb, 
-                             struct ldb_message *msg,
-                             struct ldb_parse_tree *tree,
+                             const struct ldb_message *msg,
+                             const struct ldb_parse_tree *tree,
                              enum ldb_scope scope)
 {
        unsigned int i;
        struct ldb_message_element *el;
-       const struct ldb_attrib_handler *h;
+       const struct ldb_schema_attribute *a;
        struct ldb_dn *valuedn;
        int ret;
 
-       if (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0) {
-
-               valuedn = ldb_dn_explode_casefold(ldb, tree->u.equality.value.data);
+       if (ldb_attr_dn(tree->u.equality.attr) == 0) {
+               valuedn = ldb_dn_from_ldb_val(ldb, ldb, &tree->u.equality.value);
                if (valuedn == NULL) {
                        return 0;
                }
 
-               ret = ldb_dn_compare(ldb, msg->dn, valuedn);
+               ret = ldb_dn_compare(msg->dn, valuedn);
 
                talloc_free(valuedn);
 
@@ -173,11 +167,11 @@ static int ldb_match_equality(struct ldb_context *ldb,
                return 0;
        }
 
-       h = ldb_attrib_handler(ldb, el->name);
+       a = ldb_schema_attribute_by_name(ldb, el->name);
 
        for (i=0;i<el->num_values;i++) {
-               if (h->comparison_fn(ldb, ldb, &tree->u.equality.value, 
-                                    &el->values[i]) == 0) {
+               if (a->syntax->comparison_fn(ldb, ldb, &tree->u.equality.value, 
+                                            &el->values[i]) == 0) {
                        return 1;
                }
        }
@@ -186,20 +180,20 @@ static int ldb_match_equality(struct ldb_context *ldb,
 }
 
 static int ldb_wildcard_compare(struct ldb_context *ldb,
-                               struct ldb_parse_tree *tree,
+                               const struct ldb_parse_tree *tree,
                                const struct ldb_val value)
 {
-       const struct ldb_attrib_handler *h;
+       const struct ldb_schema_attribute *a;
        struct ldb_val val;
        struct ldb_val cnk;
        struct ldb_val *chunk;
        char *p, *g;
-       char *save_p = NULL;
+       uint8_t *save_p = NULL;
        int c = 0;
 
-       h = ldb_attrib_handler(ldb, tree->u.substring.attr);
+       a = ldb_schema_attribute_by_name(ldb, tree->u.substring.attr);
 
-       if(h->canonicalise_fn(ldb, ldb, &value, &val) != 0)
+       if(a->syntax->canonicalise_fn(ldb, ldb, &value, &val) != 0)
                return -1;
 
        save_p = val.data;
@@ -208,10 +202,13 @@ static int ldb_wildcard_compare(struct ldb_context *ldb,
        if ( ! tree->u.substring.start_with_wildcard ) {
 
                chunk = tree->u.substring.chunks[c];
-               if(h->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
+               if(a->syntax->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
 
-               /* FIXME: case of embedded nulls */
-               if (strncmp(val.data, cnk.data, cnk.length) != 0) goto failed;
+               /* This deals with wildcard prefix searches on binary attributes (eg objectGUID) */
+               if (cnk.length > val.length) {
+                       goto failed;
+               }
+               if (memcmp((char *)val.data, (char *)cnk.data, cnk.length) != 0) goto failed;
                val.length -= cnk.length;
                val.data += cnk.length;
                c++;
@@ -222,19 +219,19 @@ static int ldb_wildcard_compare(struct ldb_context *ldb,
        while (tree->u.substring.chunks[c]) {
 
                chunk = tree->u.substring.chunks[c];
-               if(h->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
+               if(a->syntax->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
 
                /* FIXME: case of embedded nulls */
-               p = strstr(val.data, cnk.data);
+               p = strstr((char *)val.data, (char *)cnk.data);
                if (p == NULL) goto failed;
                if ( (! tree->u.substring.chunks[c + 1]) && (! tree->u.substring.end_with_wildcard) ) {
                        do { /* greedy */
-                               g = strstr(p + cnk.length, cnk.data);
+                               g = strstr((char *)p + cnk.length, (char *)cnk.data);
                                if (g) p = g;
                        } while(g);
                }
                val.length = val.length - (p - (char *)(val.data)) - cnk.length;
-               val.data = p + cnk.length;
+               val.data = (uint8_t *)(p + cnk.length);
                c++;
                talloc_free(cnk.data);
                cnk.data = NULL;
@@ -254,8 +251,8 @@ failed:
   match a simple leaf node
 */
 static int ldb_match_substring(struct ldb_context *ldb, 
-                              struct ldb_message *msg,
-                              struct ldb_parse_tree *tree,
+                              const struct ldb_message *msg,
+                              const struct ldb_parse_tree *tree,
                               enum ldb_scope scope)
 {
        unsigned int i;
@@ -279,22 +276,22 @@ static int ldb_match_substring(struct ldb_context *ldb,
 /*
   bitwise-and comparator
 */
-static int ldb_comparator_and(struct ldb_val *v1, struct ldb_val *v2)
+static int ldb_comparator_and(const struct ldb_val *v1, const struct ldb_val *v2)
 {
        uint64_t i1, i2;
-       i1 = strtoull(v1->data, NULL, 0);
-       i2 = strtoull(v2->data, NULL, 0);
+       i1 = strtoull((char *)v1->data, NULL, 0);
+       i2 = strtoull((char *)v2->data, NULL, 0);
        return ((i1 & i2) == i2);
 }
 
 /*
   bitwise-or comparator
 */
-static int ldb_comparator_or(struct ldb_val *v1, struct ldb_val *v2)
+static int ldb_comparator_or(const struct ldb_val *v1, const struct ldb_val *v2)
 {
        uint64_t i1, i2;
-       i1 = strtoull(v1->data, NULL, 0);
-       i2 = strtoull(v2->data, NULL, 0);
+       i1 = strtoull((char *)v1->data, NULL, 0);
+       i2 = strtoull((char *)v2->data, NULL, 0);
        return ((i1 & i2) != 0);
 }
 
@@ -303,24 +300,27 @@ static int ldb_comparator_or(struct ldb_val *v1, struct ldb_val *v2)
   extended match, handles things like bitops
 */
 static int ldb_match_extended(struct ldb_context *ldb, 
-                             struct ldb_message *msg,
-                             struct ldb_parse_tree *tree,
+                             const struct ldb_message *msg,
+                             const struct ldb_parse_tree *tree,
                              enum ldb_scope scope)
 {
        int i;
        const struct {
                const char *oid;
-               int (*comparator)(struct ldb_val *, struct ldb_val *);
+               int (*comparator)(const struct ldb_val *, const struct ldb_val *);
        } rules[] = {
                { LDB_OID_COMPARATOR_AND, ldb_comparator_and},
                { LDB_OID_COMPARATOR_OR, ldb_comparator_or}
        };
-       int (*comp)(struct ldb_val *, struct ldb_val *) = NULL;
+       int (*comp)(const struct ldb_val *, const struct ldb_val *) = NULL;
        struct ldb_message_element *el;
 
        if (tree->u.extended.dnAttributes) {
-               ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: dnAttributes extended match not supported yet");
-               return -1;
+               /* FIXME: We really need to find out what this ":dn" part in
+                * an extended match means and how to handle it. For now print
+                * only a warning to have s3 winbind and other tools working
+                * against us. - Matthias */
+               ldb_debug(ldb, LDB_DEBUG_WARNING, "ldb: dnAttributes extended match not supported yet");
        }
        if (tree->u.extended.rule_id == NULL) {
                ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: no-rule extended matches not supported yet");
@@ -338,7 +338,7 @@ static int ldb_match_extended(struct ldb_context *ldb,
                }
        }
        if (comp == NULL) {
-               ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: unknown extended rule_id %s\n",
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: unknown extended rule_id %s",
                          tree->u.extended.rule_id);
                return -1;
        }
@@ -366,8 +366,8 @@ static int ldb_match_extended(struct ldb_context *ldb,
   this is a recursive function, and does short-circuit evaluation
  */
 static int ldb_match_message(struct ldb_context *ldb, 
-                            struct ldb_message *msg,
-                            struct ldb_parse_tree *tree,
+                            const struct ldb_message *msg,
+                            const struct ldb_parse_tree *tree,
                             enum ldb_scope scope)
 {
        unsigned int i;
@@ -418,9 +418,9 @@ static int ldb_match_message(struct ldb_context *ldb,
 }
 
 int ldb_match_msg(struct ldb_context *ldb,
-                 struct ldb_message *msg,
-                 struct ldb_parse_tree *tree,
-                 const struct ldb_dn *base,
+                 const struct ldb_message *msg,
+                 const struct ldb_parse_tree *tree,
+                 struct ldb_dn *base,
                  enum ldb_scope scope)
 {
        if ( ! ldb_match_scope(ldb, base, msg->dn, scope) ) {
@@ -429,3 +429,22 @@ int ldb_match_msg(struct ldb_context *ldb,
 
        return ldb_match_message(ldb, msg, tree, scope);
 }
+
+int ldb_match_msg_objectclass(const struct ldb_message *msg,
+                             const char *objectclass)
+{
+       int i;
+       struct ldb_message_element *el = ldb_msg_find_element(msg, "objectClass");
+       if (!el) {
+               return 0;
+       }
+       for (i=0; i < el->num_values; i++) {
+               if (ldb_attr_cmp((const char *)el->values[i].data, objectclass) == 0) {
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+
+