r13333: revert previous commit I will use ldb_caseless_cmp in attrib_handlers
authorSimo Sorce <idra@samba.org>
Sat, 4 Feb 2006 06:57:28 +0000 (06:57 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:51:45 +0000 (13:51 -0500)
to correctly support utf8 comparisons

add an ldb_attr_Casefold function for attribute names and use it
instead of casefold in the right places

source/lib/db_wrap.c
source/lib/ldb/common/ldb_dn.c
source/lib/ldb/common/ldb_utf8.c
source/lib/ldb/include/ldb.h
source/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
source/lib/ldb/ldb_tdb/ldb_index.c
source/lib/ldb/tools/cmdline.c

index 4223d19b3fa4ce5e8bcdbac88196e76b4b8515dc..f3358bff98bbdee279cc1966afcab727eb2fadfd 100644 (file)
@@ -55,7 +55,12 @@ static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
        free(s);
 }
 
-char *wrap_casefold(void *context, void *mem_ctx, const char *s)
+static int wrap_caseless_cmp(void *context, const char *s1, const char *s2)
+{
+       return strcasecmp_m(s1, s2);
+}
+
+static char *wrap_casefold(void *context, void *mem_ctx, const char *s)
 {
        return strupper_talloc(mem_ctx, s);
 }
@@ -128,7 +133,7 @@ struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
 
        ldb_set_debug(ldb, ldb_wrap_debug, NULL);
 
-       ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
+       ldb_set_utf8_fns(ldb, NULL, wrap_caseless_cmp, wrap_casefold);
 
        return ldb;
 }
index bcd5a067a6366ef600d4239b97f6c9adcc855802..9f3374bceb207ef2b9ca4af4738ba3d63c617f9c 100644 (file)
@@ -581,7 +581,7 @@ struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, const struct ldb_dn *edn
                struct ldb_dn_component dc;
                const struct ldb_attrib_handler *h;
 
-               dc.name = ldb_casefold(ldb, cedn, edn->components[i].name);
+               dc.name = ldb_attr_casefold(cedn, edn->components[i].name);
                LDB_DN_NULL_FAILED(dc.name);
 
                h = ldb_attrib_handler(ldb, dc.name);
index 1b7319915b60c07064fac0911dfa88af8b367fc5..743e976e84e000696db21ec2e8ef30f00c74b6e2 100644 (file)
  */
 void ldb_set_utf8_fns(struct ldb_context *ldb,
                        void *context,
+                       int (*cmp)(void *, const char *, const char *),
                        char *(*casefold)(void *, void *, const char *))
 {
        if (context)
                ldb->utf8_fns.context = context;
+       if (cmp)
+               ldb->utf8_fns.caseless_cmp = cmp;
        if (casefold)
                ldb->utf8_fns.casefold = casefold;
 }
@@ -68,9 +71,19 @@ char *ldb_casefold_default(void *context, void *mem_ctx, const char *s)
        return ret;
 }
 
+/*
+  a caseless compare, optimised for 7 bit
+  NOTE: doesn't handle UTF8
+*/
+
+int ldb_caseless_cmp_default(void *context, const char *s1, const char *s2)
+{
+       return strcasecmp(s1,s2);
+}
+
 void ldb_set_utf8_default(struct ldb_context *ldb)
 {
-       ldb_set_utf8_fns(ldb, NULL, ldb_casefold_default);
+       ldb_set_utf8_fns(ldb, NULL, ldb_caseless_cmp_default, ldb_casefold_default);
 }
 
 char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s)
@@ -78,6 +91,11 @@ char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s)
        return ldb->utf8_fns.casefold(ldb->utf8_fns.context, mem_ctx, s);
 }
 
+int ldb_caseless_cmp(struct ldb_context *ldb, const char *s1, const char *s2)
+{
+       return ldb->utf8_fns.caseless_cmp(ldb->utf8_fns.context, s1, s2);
+}
+
 /*
   check the attribute name is valid according to rfc2251
   returns 1 if the name is ok
@@ -112,7 +130,8 @@ int ldb_valid_attr_name(const char *s)
 
 /*
   compare two attribute names
-  attribute names are restricted by rfc2251 so using strcasecmp here is ok.
+  attribute names are restricted by rfc2251 so using
+  strcasecmp and toupper here is ok.
   return 0 for match
 */
 int ldb_attr_cmp(const char *attr1, const char *attr2)
@@ -120,6 +139,20 @@ int ldb_attr_cmp(const char *attr1, const char *attr2)
        return strcasecmp(attr1, attr2);
 }
 
+char *ldb_attr_casefold(void *mem_ctx, const char *s)
+{
+       int i;
+       char *ret = talloc_strdup(mem_ctx, s);
+       if (!s) {
+               errno = ENOMEM;
+               return NULL;
+       }
+       for (i = 0; ret[i]; i++) {
+               ret[i] = toupper((unsigned char)ret[i]);
+       }
+       return ret;
+}
+
 /*
   we accept either 'dn' or 'distinguishedName' for a distinguishedName
 */
index 0e192c2e31635cb989d959e6ed6a8ab0845fd8a4..7c39aeeeb9569bc97ed5afbdd67b5f0bbf8943b3 100644 (file)
@@ -214,6 +214,7 @@ struct ldb_debug_ops {
 */
 struct ldb_utf8_fns {
        void *context;
+       int (*caseless_cmp)(void *context, const char *s1, const char *s2);
        char *(*casefold)(void *context, void *mem_ctx, const char *s);
 };
 
@@ -747,6 +748,21 @@ void ldb_set_utf8_default(struct ldb_context *ldb);
 */
 char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s);
 
+/**
+   Compare two strings, without regard to case. 
+
+   \param ldb the ldb context
+   \param s1 the first string to compare
+   \param s2 the second string to compare
+
+   \return 0 if the strings are the same, non-zero if there are any
+   differences except for case.
+
+   \note The default function is not yet UTF8 aware. Provide your own
+         set of functions through ldb_set_utf8_fns()
+*/
+int ldb_caseless_cmp(struct ldb_context *ldb, const char *s1, const char *s2);
+
 /**
    Check the attribute name is valid according to rfc2251
    \param s tthe string to check
@@ -1101,6 +1117,7 @@ int ldb_set_debug(struct ldb_context *ldb,
 */
 void ldb_set_utf8_fns(struct ldb_context *ldb,
                        void *context,
+                       int (*cmp)(void *, const char *, const char *),
                        char *(*casefold)(void *, void *, const char *));
 
 /**
index fbbbf037b3d2e7e53575d92bb6d0acb8960ff1e5..3e24093118e1973ff75a1865c4c0021f881b842d 100644 (file)
@@ -289,7 +289,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                 * For simple searches, we want to retrieve the list of EIDs that
                 * match the criteria.
                */
-               attr = ldb_casefold(module->ldb, mem_ctx, t->u.equality.attr);
+               attr = ldb_attr_casefold(module->ldb, mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
                h = ldb_attrib_handler(module->ldb, attr);
 
@@ -353,7 +353,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                        wild_card_string[strlen(wild_card_string) - 1] = '\0';
                }
 
-               attr = ldb_casefold(module->ldb, mem_ctx, t->u.substring.attr);
+               attr = ldb_attr_casefold(module->ldb, mem_ctx, t->u.substring.attr);
                if (attr == NULL) return NULL;
                h = ldb_attrib_handler(module->ldb, attr);
 
@@ -374,7 +374,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                                        value.data);
 
        case LDB_OP_GREATER:
-               attr = ldb_casefold(module->ldb, mem_ctx, t->u.equality.attr);
+               attr = ldb_attr_casefold(module->ldb, mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
                h = ldb_attrib_handler(module->ldb, attr);
 
@@ -393,7 +393,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                                        attr);
 
        case LDB_OP_LESS:
-               attr = ldb_casefold(module->ldb, mem_ctx, t->u.equality.attr);
+               attr = ldb_attr_casefold(module->ldb, mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
                h = ldb_attrib_handler(module->ldb, attr);
 
@@ -416,7 +416,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                        return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
                }
 
-               attr = ldb_casefold(module->ldb, mem_ctx, t->u.present.attr);
+               attr = ldb_attr_casefold(module->ldb, mem_ctx, t->u.present.attr);
                if (attr == NULL) return NULL;
 
                return lsqlite3_tprintf(mem_ctx,
@@ -425,7 +425,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                                        attr);
 
        case LDB_OP_APPROX:
-               attr = ldb_casefold(module->ldb, mem_ctx, t->u.equality.attr);
+               attr = ldb_attr_casefold(module->ldb, mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
                h = ldb_attrib_handler(module->ldb, attr);
 
@@ -1058,7 +1058,7 @@ static int lsqlite3_add(struct ldb_module *module, const struct ldb_message *msg
                int j;
 
                /* Get a case-folded copy of the attribute name */
-               attr = ldb_casefold(module->ldb, local_ctx, el->name);
+               attr = ldb_attr_casefold(module->ldb, local_ctx, el->name);
                if (attr == NULL) {
                        ret = LDB_ERR_OTHER;
                        goto failed;
@@ -1157,7 +1157,7 @@ static int lsqlite3_modify(struct ldb_module *module, const struct ldb_message *
                int j;
 
                /* Get a case-folded copy of the attribute name */
-               attr = ldb_casefold(module->ldb, local_ctx, el->name);
+               attr = ldb_attr_casefold(module->ldb, local_ctx, el->name);
                if (attr == NULL) {
                        ret = LDB_ERR_OTHER;
                        goto failed;
index fb29a9ddbfc5a04ff3597d9d464ffb8ffbcaa7c4..ac3063ef28323824030baf2149e019322066fbac 100644 (file)
@@ -106,7 +106,7 @@ static struct ldb_dn *ldb_dn_key(struct ldb_context *ldb,
        const struct ldb_attrib_handler *h;
        char *attr_folded;
 
-       attr_folded = ldb_casefold(ldb, ldb, attr);
+       attr_folded = ldb_attr_casefold(ldb, ldb, attr);
        if (!attr_folded) {
                return NULL;
        }
index 232cfcbb16dcdfcabc3a76949f8f13369c70a98c..446923f282d1a7970d1dd3091a32b77758e9bd47 100644 (file)
@@ -204,7 +204,7 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const
                if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
                        goto failed;
                }
-               ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
+               ldb_set_utf8_fns(ldb, NULL, wrap_caseless_cmp, wrap_casefold);
 #endif
                if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
                        fprintf(stderr, "Failed to connect to %s - %s\n",