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_private.h"
32 #include "ldb_handlers.h"
35 add a attribute to the ldb_schema
37 if flags contains LDB_ATTR_FLAG_ALLOCATED
38 the attribute name string will be copied using
39 talloc_strdup(), otherwise it needs to be a static const
40 string at least with a lifetime longer than the ldb struct!
42 the ldb_schema_syntax structure should be a pointer
43 to a static const struct or at least it needs to be
44 a struct with a longer lifetime than the ldb context!
47 int ldb_schema_attribute_add_with_syntax(struct ldb_context
*ldb
,
48 const char *attribute
,
50 const struct ldb_schema_syntax
*syntax
)
53 struct ldb_schema_attribute
*a
;
56 return LDB_ERR_OPERATIONS_ERROR
;
59 n
= ldb
->schema
.num_attributes
+ 1;
61 a
= talloc_realloc(ldb
, ldb
->schema
.attributes
,
62 struct ldb_schema_attribute
, n
);
67 ldb
->schema
.attributes
= a
;
69 for (i
= 0; i
< ldb
->schema
.num_attributes
; i
++) {
70 int cmp
= ldb_attr_cmp(attribute
, a
[i
].name
);
72 /* silently ignore attempts to overwrite fixed attributes */
73 if (a
[i
].flags
& LDB_ATTR_FLAG_FIXED
) {
76 if (a
[i
].flags
& LDB_ATTR_FLAG_ALLOCATED
) {
77 talloc_free(discard_const_p(char, a
[i
].name
));
79 /* To cancel out increment below */
80 ldb
->schema
.num_attributes
--;
83 memmove(a
+i
+1, a
+i
, sizeof(*a
) * (ldb
->schema
.num_attributes
-i
));
87 ldb
->schema
.num_attributes
++;
89 a
[i
].name
= attribute
;
93 if (a
[i
].flags
& LDB_ATTR_FLAG_ALLOCATED
) {
94 a
[i
].name
= talloc_strdup(a
, a
[i
].name
);
95 if (a
[i
].name
== NULL
) {
104 static const struct ldb_schema_syntax ldb_syntax_default
= {
105 .name
= LDB_SYNTAX_OCTET_STRING
,
106 .ldif_read_fn
= ldb_handler_copy
,
107 .ldif_write_fn
= ldb_handler_copy
,
108 .canonicalise_fn
= ldb_handler_copy
,
109 .comparison_fn
= ldb_comparison_binary
112 static const struct ldb_schema_attribute ldb_attribute_default
= {
115 .syntax
= &ldb_syntax_default
119 return the attribute handlers for a given attribute
121 static const struct ldb_schema_attribute
*ldb_schema_attribute_by_name_internal(
122 struct ldb_context
*ldb
,
125 /* for binary search we need signed variables */
126 unsigned int i
, e
, b
= 0;
128 const struct ldb_schema_attribute
*def
= &ldb_attribute_default
;
130 /* as handlers are sorted, '*' must be the first if present */
131 if (strcmp(ldb
->schema
.attributes
[0].name
, "*") == 0) {
132 def
= &ldb
->schema
.attributes
[0];
136 /* do a binary search on the array */
137 e
= ldb
->schema
.num_attributes
- 1;
139 while ((b
<= e
) && (e
!= (unsigned int) -1)) {
142 r
= ldb_attr_cmp(name
, ldb
->schema
.attributes
[i
].name
);
144 return &ldb
->schema
.attributes
[i
];
157 return the attribute handlers for a given attribute
159 const struct ldb_schema_attribute
*ldb_schema_attribute_by_name(struct ldb_context
*ldb
,
162 if (ldb
->schema
.attribute_handler_override
) {
163 const struct ldb_schema_attribute
*ret
=
164 ldb
->schema
.attribute_handler_override(ldb
,
165 ldb
->schema
.attribute_handler_override_private
,
172 return ldb_schema_attribute_by_name_internal(ldb
, name
);
177 add to the list of ldif handlers for this ldb context
179 void ldb_schema_attribute_remove(struct ldb_context
*ldb
, const char *name
)
181 const struct ldb_schema_attribute
*a
;
184 a
= ldb_schema_attribute_by_name_internal(ldb
, name
);
185 if (a
== NULL
|| a
->name
== NULL
) {
189 /* FIXED attributes are never removed */
190 if (a
->flags
& LDB_ATTR_FLAG_FIXED
) {
194 if (a
->flags
& LDB_ATTR_FLAG_ALLOCATED
) {
195 talloc_free(discard_const_p(char, a
->name
));
198 i
= a
- ldb
->schema
.attributes
;
199 if (i
< ldb
->schema
.num_attributes
- 1) {
200 memmove(&ldb
->schema
.attributes
[i
],
201 a
+1, sizeof(*a
) * (ldb
->schema
.num_attributes
-(i
+1)));
204 ldb
->schema
.num_attributes
--;
208 setup a attribute handler using a standard syntax
210 int ldb_schema_attribute_add(struct ldb_context
*ldb
,
211 const char *attribute
,
215 const struct ldb_schema_syntax
*s
= ldb_standard_syntax_by_name(ldb
, syntax
);
216 return ldb_schema_attribute_add_with_syntax(ldb
, attribute
, flags
, s
);
220 setup the attribute handles for well known attributes
222 int ldb_setup_wellknown_attributes(struct ldb_context
*ldb
)
228 { "dn", LDB_SYNTAX_DN
},
229 { "distinguishedName", LDB_SYNTAX_DN
},
230 { "cn", LDB_SYNTAX_DIRECTORY_STRING
},
231 { "dc", LDB_SYNTAX_DIRECTORY_STRING
},
232 { "ou", LDB_SYNTAX_DIRECTORY_STRING
},
233 { "objectClass", LDB_SYNTAX_OBJECTCLASS
}
238 for (i
=0;i
<ARRAY_SIZE(wellknown
);i
++) {
239 ret
= ldb_schema_attribute_add(ldb
, wellknown
[i
].attr
, 0,
240 wellknown
[i
].syntax
);
241 if (ret
!= LDB_SUCCESS
) {
251 add a extended dn syntax to the ldb_schema
253 int ldb_dn_extended_add_syntax(struct ldb_context
*ldb
,
255 const struct ldb_dn_extended_syntax
*syntax
)
258 struct ldb_dn_extended_syntax
*a
;
261 return LDB_ERR_OPERATIONS_ERROR
;
264 n
= ldb
->schema
.num_dn_extended_syntax
+ 1;
266 a
= talloc_realloc(ldb
, ldb
->schema
.dn_extended_syntax
,
267 struct ldb_dn_extended_syntax
, n
);
270 return LDB_ERR_OPERATIONS_ERROR
;
273 a
[ldb
->schema
.num_dn_extended_syntax
] = *syntax
;
274 ldb
->schema
.dn_extended_syntax
= a
;
276 ldb
->schema
.num_dn_extended_syntax
= n
;
282 return the extended dn syntax for a given name
284 const struct ldb_dn_extended_syntax
*ldb_dn_extended_syntax_by_name(struct ldb_context
*ldb
,
288 for (i
=0; i
< ldb
->schema
.num_dn_extended_syntax
; i
++) {
289 if (ldb_attr_cmp(ldb
->schema
.dn_extended_syntax
[i
].name
, name
) == 0) {
290 return &ldb
->schema
.dn_extended_syntax
[i
];
297 set an attribute handler override function - used to delegate schema handling
300 void ldb_schema_attribute_set_override_handler(struct ldb_context
*ldb
,
301 ldb_attribute_handler_override_fn_t override
,
304 ldb
->schema
.attribute_handler_override_private
= private_data
;
305 ldb
->schema
.attribute_handler_override
= override
;