4 Copyright (C) Andrew Tridgell 2005
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 3 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, see <http://www.gnu.org/licenses/>.
24 register handlers for specific attributes and objectclass relationships
26 this allows a backend to store its schema information in any format
27 it likes (or to not have any schema information at all) while keeping the
28 message matching logic generic
31 #include "ldb_includes.h"
34 add a attribute to the ldb_schema
36 if flags contains LDB_ATTR_FLAG_ALLOCATED
37 the attribute name string will be copied using
38 talloc_strdup(), otherwise it needs to be a static const
39 string at least with a lifetime longer than the ldb struct!
41 the ldb_schema_syntax structure should be a pointer
42 to a static const struct or at least it needs to be
43 a struct with a longer lifetime than the ldb context!
46 int ldb_schema_attribute_add_with_syntax(struct ldb_context *ldb,
47 const char *attribute,
49 const struct ldb_schema_syntax *syntax)
52 struct ldb_schema_attribute *a;
54 n = ldb->schema.num_attributes + 1;
56 a = talloc_realloc(ldb, ldb->schema.attributes,
57 struct ldb_schema_attribute, n);
62 ldb->schema.attributes = a;
64 for (i = 0; i < ldb->schema.num_attributes; i++) {
65 if (ldb_attr_cmp(attribute, a[i].name) < 0) {
66 memmove(a+i+1, a+i, sizeof(*a) * (ldb->schema.num_attributes-i));
71 a[i].name = attribute;
75 if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
76 a[i].name = talloc_strdup(a, a[i].name);
77 if (a[i].name == NULL) {
83 ldb->schema.num_attributes++;
87 static const struct ldb_schema_syntax ldb_syntax_default = {
88 .name = LDB_SYNTAX_OCTET_STRING,
89 .ldif_read_fn = ldb_handler_copy,
90 .ldif_write_fn = ldb_handler_copy,
91 .canonicalise_fn = ldb_handler_copy,
92 .comparison_fn = ldb_comparison_binary
95 static const struct ldb_schema_attribute ldb_attribute_default = {
98 .syntax = &ldb_syntax_default
102 return the attribute handlers for a given attribute
104 const struct ldb_schema_attribute *ldb_schema_attribute_by_name(struct ldb_context *ldb,
108 const struct ldb_schema_attribute *def = &ldb_attribute_default;
110 /* as handlers are sorted, '*' must be the first if present */
111 if (strcmp(ldb->schema.attributes[0].name, "*") == 0) {
112 def = &ldb->schema.attributes[0];
116 /* do a binary search on the array */
117 e = ldb->schema.num_attributes - 1;
123 r = ldb_attr_cmp(name, ldb->schema.attributes[i].name);
125 return &ldb->schema.attributes[i];
140 add to the list of ldif handlers for this ldb context
142 void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name)
144 const struct ldb_schema_attribute *a;
147 a = ldb_schema_attribute_by_name(ldb, name);
152 if (a->flags & LDB_ATTR_FLAG_ALLOCATED) {
153 talloc_free(discard_const_p(char, a->name));
156 i = a - ldb->schema.attributes;
157 if (i < ldb->schema.num_attributes - 1) {
158 memmove(&ldb->schema.attributes[i],
159 a+1, sizeof(*a) * (ldb->schema.num_attributes-(i+1)));
162 ldb->schema.num_attributes--;
166 setup a attribute handler using a standard syntax
168 int ldb_schema_attribute_add(struct ldb_context *ldb,
169 const char *attribute,
173 const struct ldb_schema_syntax *s = ldb_standard_syntax_by_name(ldb, syntax);
174 return ldb_schema_attribute_add_with_syntax(ldb, attribute, flags, s);
178 setup the attribute handles for well known attributes
180 int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
186 { "dn", LDB_SYNTAX_DN },
187 { "distinguishedName", LDB_SYNTAX_DN },
188 { "cn", LDB_SYNTAX_DIRECTORY_STRING },
189 { "dc", LDB_SYNTAX_DIRECTORY_STRING },
190 { "ou", LDB_SYNTAX_DIRECTORY_STRING },
191 { "objectClass", LDB_SYNTAX_OBJECTCLASS }
196 for (i=0;i<ARRAY_SIZE(wellknown);i++) {
197 ret = ldb_schema_attribute_add(ldb, wellknown[i].attr, 0,
198 wellknown[i].syntax);
199 if (ret != LDB_SUCCESS) {
208 return the list of subclasses for a class
210 const char **ldb_subclass_list(struct ldb_context *ldb, const char *classname)
213 for (i=0;i<ldb->schema.num_classes;i++) {
214 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
215 return (const char **)ldb->schema.classes[i].subclasses;
225 static int ldb_subclass_new(struct ldb_context *ldb, const char *classname, const char *subclass)
227 struct ldb_subclass *s, *c;
228 s = talloc_realloc(ldb, ldb->schema.classes, struct ldb_subclass, ldb->schema.num_classes+1);
229 if (s == NULL) goto failed;
231 ldb->schema.classes = s;
232 c = &s[ldb->schema.num_classes];
233 c->name = talloc_strdup(s, classname);
234 if (c->name == NULL) goto failed;
236 c->subclasses = talloc_array(s, char *, 2);
237 if (c->subclasses == NULL) goto failed;
239 c->subclasses[0] = talloc_strdup(c->subclasses, subclass);
240 if (c->subclasses[0] == NULL) goto failed;
241 c->subclasses[1] = NULL;
243 ldb->schema.num_classes++;
254 int ldb_subclass_add(struct ldb_context *ldb, const char *classname, const char *subclass)
257 struct ldb_subclass *c;
260 for (i=0;i<ldb->schema.num_classes;i++) {
261 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
265 if (i == ldb->schema.num_classes) {
266 return ldb_subclass_new(ldb, classname, subclass);
268 c = &ldb->schema.classes[i];
270 for (n=0;c->subclasses[n];n++) /* noop */;
272 s = talloc_realloc(ldb->schema.classes, c->subclasses, char *, n+2);
279 s[n] = talloc_strdup(s, subclass);
290 remove a set of subclasses for a class
292 void ldb_subclass_remove(struct ldb_context *ldb, const char *classname)
295 struct ldb_subclass *c;
297 for (i=0;i<ldb->schema.num_classes;i++) {
298 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
302 if (i == ldb->schema.num_classes) {
306 c = &ldb->schema.classes[i];
307 talloc_free(c->name);
308 talloc_free(c->subclasses);
309 if (ldb->schema.num_classes-(i+1) > 0) {
310 memmove(c, c+1, sizeof(*c) * (ldb->schema.num_classes-(i+1)));
312 ldb->schema.num_classes--;
313 if (ldb->schema.num_classes == 0) {
314 talloc_free(ldb->schema.classes);
315 ldb->schema.classes = NULL;