Fix the mess with ldb includes.
[metze/samba/wip.git] / source4 / lib / ldb / common / ldb_attributes.c
index 13e721a266eab6c0bf1932bc565b20bbc6db462f..4d688a4d7e56bf8bb33b940fbd4b3783e24ff0a1 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/>.
 */
 /*
   register handlers for specific attributes and objectclass relationships
   message matching logic generic
 */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_private.h"
 
 /*
-  add to the list of ldif handlers for this ldb context
+  add a attribute to the ldb_schema
+
+  if flags contains LDB_ATTR_FLAG_ALLOCATED
+  the attribute name string will be copied using
+  talloc_strdup(), otherwise it needs to be a static const
+  string at least with a lifetime longer than the ldb struct!
+  
+  the ldb_schema_syntax structure should be a pointer
+  to a static const struct or at least it needs to be
+  a struct with a longer lifetime than the ldb context!
+
 */
-int ldb_set_attrib_handlers(struct ldb_context *ldb, 
-                           const struct ldb_attrib_handler *handlers, 
-                           unsigned num_handlers)
+int ldb_schema_attribute_add_with_syntax(struct ldb_context *ldb, 
+                                        const char *attribute,
+                                        unsigned flags,
+                                        const struct ldb_schema_syntax *syntax)
 {
-       struct ldb_attrib_handler *h;
-       h = talloc_realloc(ldb, ldb->schema.attrib_handlers,
-                          struct ldb_attrib_handler,
-                          ldb->schema.num_attrib_handlers + num_handlers);
-       if (h == NULL) {
+       int i, n;
+       struct ldb_schema_attribute *a;
+
+       if (!syntax) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       n = ldb->schema.num_attributes + 1;
+
+       a = talloc_realloc(ldb, ldb->schema.attributes,
+                          struct ldb_schema_attribute, n);
+       if (a == NULL) {
                ldb_oom(ldb);
                return -1;
        }
-       ldb->schema.attrib_handlers = h;
-       memcpy(h + ldb->schema.num_attrib_handlers, 
-              handlers, sizeof(*h) * num_handlers);
-       ldb->schema.num_attrib_handlers += num_handlers;
-       return 0;
-}
-                         
+       ldb->schema.attributes = a;
+
+       for (i = 0; i < ldb->schema.num_attributes; i++) {
+               int cmp = ldb_attr_cmp(attribute, a[i].name);
+               if (cmp == 0) {
+                       /* silently ignore attempts to overwrite fixed attributes */
+                       if (a[i].flags & LDB_ATTR_FLAG_FIXED) {
+                               return 0;
+                       }
+                       if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
+                               talloc_free(discard_const_p(char, a[i].name));
+                       }
+                       /* To cancel out increment below */
+                       ldb->schema.num_attributes--;
+                       break;
+               } else if (cmp < 0) {
+                       memmove(a+i+1, a+i, sizeof(*a) * (ldb->schema.num_attributes-i));
+                       break;
+               }
+       }
+       ldb->schema.num_attributes++;
 
-/*
-  default function for read/write/canonicalise
-*/
-static int ldb_default_copy(struct ldb_context *ldb, 
-                           void *mem_ctx,
-                           const struct ldb_val *in, 
-                           struct ldb_val *out)
-{
-       *out = ldb_val_dup(mem_ctx, in);
+       a[i].name       = attribute;
+       a[i].flags      = flags;
+       a[i].syntax     = syntax;
 
-       if (out->data == NULL && in->data != NULL) {
-               return -1;
+       if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
+               a[i].name = talloc_strdup(a, a[i].name);
+               if (a[i].name == NULL) {
+                       ldb_oom(ldb);
+                       return -1;
+               }
        }
 
        return 0;
 }
 
-/*
-  default function for comparison
-*/
-static int ldb_default_cmp(struct ldb_context *ldb, 
-                           void *mem_ctx,
-                          const struct ldb_val *v1, 
-                          const struct ldb_val *v2)
-{
-       if (v1->length != v2->length) {
-               return v1->length - v2->length;
-       }
-       return memcmp(v1->data, v2->data, v1->length);
-}
+static const struct ldb_schema_syntax ldb_syntax_default = {
+       .name            = LDB_SYNTAX_OCTET_STRING,
+       .ldif_read_fn    = ldb_handler_copy,
+       .ldif_write_fn   = ldb_handler_copy,
+       .canonicalise_fn = ldb_handler_copy,
+       .comparison_fn   = ldb_comparison_binary
+};
 
-/*
-  default handler function pointers
-*/
-static const struct ldb_attrib_handler ldb_default_attrib_handler = {
-       .attr = NULL,
-       .ldif_read_fn    = ldb_default_copy,
-       .ldif_write_fn   = ldb_default_copy,
-       .canonicalise_fn = ldb_default_copy,
-       .comparison_fn   = ldb_default_cmp,
+static const struct ldb_schema_attribute ldb_attribute_default = {
+       .name   = NULL,
+       .flags  = 0,
+       .syntax = &ldb_syntax_default
 };
 
 /*
   return the attribute handlers for a given attribute
 */
-const struct ldb_attrib_handler *ldb_attrib_handler(struct ldb_context *ldb,
-                                                   const char *attrib)
+const struct ldb_schema_attribute *ldb_schema_attribute_by_name(struct ldb_context *ldb,
+                                                               const char *name)
 {
-       int i;
-       const struct ldb_attrib_handler *def = &ldb_default_attrib_handler;
-       /* TODO: should be replaced with a binary search, with a sort on add */
-       for (i=0;i<ldb->schema.num_attrib_handlers;i++) {
-               if (strcmp(ldb->schema.attrib_handlers[i].attr, "*") == 0) {
-                       def = &ldb->schema.attrib_handlers[i];
+       int i, e, b = 0, r;
+       const struct ldb_schema_attribute *def = &ldb_attribute_default;
+
+       /* as handlers are sorted, '*' must be the first if present */
+       if (strcmp(ldb->schema.attributes[0].name, "*") == 0) {
+               def = &ldb->schema.attributes[0];
+               b = 1;
+       }
+
+       /* do a binary search on the array */
+       e = ldb->schema.num_attributes - 1;
+
+       while (b <= e) {
+
+               i = (b + e) / 2;
+
+               r = ldb_attr_cmp(name, ldb->schema.attributes[i].name);
+               if (r == 0) {
+                       return &ldb->schema.attributes[i];
                }
-               if (ldb_attr_cmp(attrib, ldb->schema.attrib_handlers[i].attr) == 0) {
-                       return &ldb->schema.attrib_handlers[i];
+               if (r < 0) {
+                       e = i - 1;
+               } else {
+                       b = i + 1;
                }
+
        }
+
        return def;
 }
 
@@ -121,37 +155,44 @@ const struct ldb_attrib_handler *ldb_attrib_handler(struct ldb_context *ldb,
 /*
   add to the list of ldif handlers for this ldb context
 */
-void ldb_remove_attrib_handler(struct ldb_context *ldb, const char *attrib)
+void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name)
 {
-       const struct ldb_attrib_handler *h;
+       const struct ldb_schema_attribute *a;
        int i;
-       h = ldb_attrib_handler(ldb, attrib);
-       if (h == &ldb_default_attrib_handler) {
+
+       a = ldb_schema_attribute_by_name(ldb, name);
+       if (a == NULL || a->name == NULL) {
                return;
        }
-       i = h - ldb->schema.attrib_handlers;
-       if (i < ldb->schema.num_attrib_handlers - 1) {
-               memmove(&ldb->schema.attrib_handlers[i], 
-                       h+1, sizeof(*h) * (ldb->schema.num_attrib_handlers-(i+1)));
+
+       /* FIXED attributes are never removed */
+       if (a->flags & LDB_ATTR_FLAG_FIXED) {
+               return;
+       }
+
+       if (a->flags & LDB_ATTR_FLAG_ALLOCATED) {
+               talloc_free(discard_const_p(char, a->name));
+       }
+
+       i = a - ldb->schema.attributes;
+       if (i < ldb->schema.num_attributes - 1) {
+               memmove(&ldb->schema.attributes[i], 
+                       a+1, sizeof(*a) * (ldb->schema.num_attributes-(i+1)));
        }
-       ldb->schema.num_attrib_handlers--;
+
+       ldb->schema.num_attributes--;
 }
 
 /*
   setup a attribute handler using a standard syntax
 */
-int ldb_set_attrib_handler_syntax(struct ldb_context *ldb, 
-                                 const char *attr, const char *syntax)
+int ldb_schema_attribute_add(struct ldb_context *ldb,
+                            const char *attribute,
+                            unsigned flags,
+                            const char *syntax)
 {
-       const struct ldb_attrib_handler *h = ldb_attrib_handler_syntax(ldb, syntax);
-       struct ldb_attrib_handler h2;
-       if (h == NULL) {
-               ldb_debug(ldb, LDB_DEBUG_ERROR, "Unknown syntax '%s'\n", syntax);
-               return -1;
-       }
-       h2 = *h;
-       h2.attr = attr;
-       return ldb_set_attrib_handlers(ldb, &h2, 1);
+       const struct ldb_schema_syntax *s = ldb_standard_syntax_by_name(ldb, syntax);
+       return ldb_schema_attribute_add_with_syntax(ldb, attribute, flags, s);
 }
 
 /*
@@ -164,7 +205,6 @@ int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
                const char *syntax;
        } wellknown[] = {
                { "dn", LDB_SYNTAX_DN },
-               { "ncName", LDB_SYNTAX_DN },
                { "distinguishedName", LDB_SYNTAX_DN },
                { "cn", LDB_SYNTAX_DIRECTORY_STRING },
                { "dc", LDB_SYNTAX_DIRECTORY_STRING },
@@ -172,124 +212,63 @@ int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
                { "objectClass", LDB_SYNTAX_OBJECTCLASS }
        };
        int i;
+       int ret;
+
        for (i=0;i<ARRAY_SIZE(wellknown);i++) {
-               if (ldb_set_attrib_handler_syntax(ldb, wellknown[i].attr, 
-                                                 wellknown[i].syntax) != 0) {
-                       return -1;
+               ret = ldb_schema_attribute_add(ldb, wellknown[i].attr, 0,
+                                              wellknown[i].syntax);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
                }
        }
-       return 0;
-}
-
 
-/*
-  return the list of subclasses for a class
-*/
-const char **ldb_subclass_list(struct ldb_context *ldb, const char *class)
-{
-       int i;
-       for (i=0;i<ldb->schema.num_classes;i++) {
-               if (ldb_attr_cmp(class, ldb->schema.classes[i].name) == 0) {
-                       return (const char **)ldb->schema.classes[i].subclasses;
-               }
-       }
-       return NULL;
+       return LDB_SUCCESS;
 }
 
 
 /*
-  add a new subclass
+  add a extended dn syntax to the ldb_schema
 */
-static int ldb_subclass_new(struct ldb_context *ldb, const char *class, const char *subclass)
+int ldb_dn_extended_add_syntax(struct ldb_context *ldb, 
+                              unsigned flags,
+                              const struct ldb_dn_extended_syntax *syntax)
 {
-       struct ldb_subclass *s, *c;
-       s = talloc_realloc(ldb, ldb->schema.classes, struct ldb_subclass, ldb->schema.num_classes+1);
-       if (s == NULL) goto failed;
-
-       ldb->schema.classes = s;
-       c = &s[ldb->schema.num_classes];
-       c->name = talloc_strdup(s, class);
-       if (c->name == NULL) goto failed;
-
-       c->subclasses = talloc_array(s, char *, 2);
-       if (c->subclasses == NULL) goto failed;
+       int n;
+       struct ldb_dn_extended_syntax *a;
 
-       c->subclasses[0] = talloc_strdup(c->subclasses, subclass);
-       if (c->subclasses[0] == NULL) goto failed;
-       c->subclasses[1] = NULL;
+       if (!syntax) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       ldb->schema.num_classes++;
+       n = ldb->schema.num_dn_extended_syntax + 1;
 
-       return 0;
-failed:
-       ldb_oom(ldb);
-       return -1;
-}
-
-/*
-  add a subclass
-*/
-int ldb_subclass_add(struct ldb_context *ldb, const char *class, const char *subclass)
-{
-       int i, n;
-       struct ldb_subclass *c;
-       char **s;
+       a = talloc_realloc(ldb, ldb->schema.dn_extended_syntax,
+                          struct ldb_dn_extended_syntax, n);
 
-       for (i=0;i<ldb->schema.num_classes;i++) {
-               if (ldb_attr_cmp(class, ldb->schema.classes[i].name) == 0) {
-                       break;
-               }
-       }
-       if (i == ldb->schema.num_classes) {
-               return ldb_subclass_new(ldb, class, subclass);
+       if (!a) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       c = &ldb->schema.classes[i];
-       
-       for (n=0;c->subclasses[n];n++) /* noop */;
 
-       s = talloc_realloc(ldb->schema.classes, c->subclasses, char *, n+2);
-       if (s == NULL) {
-               ldb_oom(ldb);
-               return -1;
-       }
+       a[ldb->schema.num_dn_extended_syntax] = *syntax;
+       ldb->schema.dn_extended_syntax = a;
 
-       c->subclasses = s;
-       s[n] = talloc_strdup(s, subclass);
-       if (s[n] == NULL) {
-               ldb_oom(ldb);
-               return -1;
-       }
-       s[n+1] = NULL;
+       ldb->schema.num_dn_extended_syntax = n;
 
-       return 0;
+       return LDB_SUCCESS;
 }
 
 /*
-  remove a set of subclasses for a class
+  return the extended dn syntax for a given name
 */
-void ldb_subclass_remove(struct ldb_context *ldb, const char *class)
+const struct ldb_dn_extended_syntax *ldb_dn_extended_syntax_by_name(struct ldb_context *ldb,
+                                                                   const char *name)
 {
        int i;
-       struct ldb_subclass *c;
-
-       for (i=0;i<ldb->schema.num_classes;i++) {
-               if (ldb_attr_cmp(class, ldb->schema.classes[i].name) == 0) {
-                       break;
+       for (i=0; i < ldb->schema.num_dn_extended_syntax; i++) {
+               if (ldb_attr_cmp(ldb->schema.dn_extended_syntax[i].name, name) == 0) {
+                       return &ldb->schema.dn_extended_syntax[i];
                }
        }
-       if (i == ldb->schema.num_classes) {
-               return;
-       }
-
-       c = &ldb->schema.classes[i];
-       talloc_free(c->name);
-       talloc_free(c->subclasses);
-       if (ldb->schema.num_classes-(i+1) > 0) {
-               memmove(c, c+1, sizeof(*c) * (ldb->schema.num_classes-(i+1)));
-       }
-       ldb->schema.num_classes--;
-       if (ldb->schema.num_classes == 0) {
-               talloc_free(ldb->schema.classes);
-               ldb->schema.classes = NULL;
-       }
+       return NULL;
 }
+