Fix the mess with ldb includes.
[metze/samba/wip.git] / source4 / lib / ldb / common / ldb_utf8.c
index 577766d9f72cc802699b76f70fd2754246127335..0a8a89ac1d14e2d48a1248f613c8a7e1c4f2f20e 100644 (file)
@@ -10,7 +10,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
@@ -18,8 +18,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 <ctype.h>
+#include "ldb_private.h"
+#include "system/locale.h"
+
 
 /*
-  TODO:
-  a simple case folding function - will be replaced by a UTF8 aware function later
+  this allow the user to pass in a caseless comparison
+  function to handle utf8 caseless comparisons
+ */
+void ldb_set_utf8_fns(struct ldb_context *ldb,
+                     void *context,
+                     char *(*casefold)(void *, void *, const char *, size_t))
+{
+       if (context)
+               ldb->utf8_fns.context = context;
+       if (casefold)
+               ldb->utf8_fns.casefold = casefold;
+}
+
+/*
+  a simple case folding function
+  NOTE: does not handle UTF8
 */
-char *ldb_casefold(struct ldb_context *ldb, const char *s)
+char *ldb_casefold_default(void *context, void *mem_ctx, const char *s, size_t n)
 {
        int i;
-       char *ret = talloc_strdup(ldb, s);
+       char *ret = talloc_strndup(mem_ctx, s, n);
        if (!s) {
                errno = ENOMEM;
                return NULL;
        }
        for (i=0;ret[i];i++) {
-               ret[i] = toupper(ret[i]);
+               ret[i] = toupper((unsigned char)ret[i]);
        }
        return ret;
 }
 
+void ldb_set_utf8_default(struct ldb_context *ldb)
+{
+       ldb_set_utf8_fns(ldb, NULL, ldb_casefold_default);
+}
+
+char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s, size_t n)
+{
+       return ldb->utf8_fns.casefold(ldb->utf8_fns.context, mem_ctx, s, n);
+}
+
 /*
-  a caseless compare, optimised for 7 bit
-  TODO: doesn't yet handle UTF8
-*/
-static int ldb_caseless_cmp(const char *s1, const char *s2)
+  check the attribute name is valid according to rfc2251
+  returns 1 if the name is ok
+ */
+
+int ldb_valid_attr_name(const char *s)
 {
        int i;
-       for (i=0;s1[i] != 0;i++) {
-               int c1 = toupper(s1[i]), c2 = toupper(s2[i]);
-               if (c1 != c2) {
-                       return c1 - c2;
+
+       if (!s || !s[0])
+               return 0;
+
+       /* handle special ldb_tdb wildcard */
+       if (strcmp(s, "*") == 0) return 1;
+
+       for (i = 0; s[i]; i++) {
+               if (! isascii(s[i])) {
+                       return 0;
+               }
+               if (i == 0) { /* first char must be an alpha (or our special '@' identifier) */
+                       if (! (isalpha(s[i]) || (s[i] == '@'))) {
+                               return 0;
+                       }
+               } else {
+                       if (! (isalnum(s[i]) || (s[i] == '-'))) {
+                               return 0;
+                       }
                }
        }
-       return s2[i];
+       return 1;
 }
 
-/*
-  compare two basedn fields
-  return 0 for match
-*/
-int ldb_dn_cmp(const char *dn1, const char *dn2)
+char *ldb_attr_casefold(void *mem_ctx, const char *s)
 {
-       return ldb_caseless_cmp(dn1, dn2);
+       int i;
+       char *ret = talloc_strdup(mem_ctx, s);
+       if (!ret) {
+               errno = ENOMEM;
+               return NULL;
+       }
+       for (i = 0; ret[i]; i++) {
+               ret[i] = toupper((unsigned char)ret[i]);
+       }
+       return ret;
 }
 
 /*
-  compare two attributes
-  return 0 for match
+  we accept either 'dn' or 'distinguishedName' for a distinguishedName
 */
-int ldb_attr_cmp(const char *dn1, const char *dn2)
+int ldb_attr_dn(const char *attr)
 {
-       return ldb_caseless_cmp(dn1, dn2);
+       if (ldb_attr_cmp(attr, "dn") == 0 ||
+           ldb_attr_cmp(attr, "distinguishedName") == 0) {
+               return 0;
+       }
+       return -1;
 }