major upgrade to the ldb attribute handling
[metze/samba/wip.git] / source4 / lib / ldb / common / ldb_attributes.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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/>.
22 */
23 /*
24   register handlers for specific attributes and objectclass relationships
25
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
29 */
30
31 #include "ldb_private.h"
32 #include "ldb_handlers.h"
33
34 /*
35   add a attribute to the ldb_schema
36
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!
41   
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!
45
46 */
47 int ldb_schema_attribute_add_with_syntax(struct ldb_context *ldb, 
48                                          const char *attribute,
49                                          unsigned flags,
50                                          const struct ldb_schema_syntax *syntax)
51 {
52         int i, n;
53         struct ldb_schema_attribute *a;
54
55         if (!syntax) {
56                 return LDB_ERR_OPERATIONS_ERROR;
57         }
58
59         n = ldb->schema.num_attributes + 1;
60
61         a = talloc_realloc(ldb, ldb->schema.attributes,
62                            struct ldb_schema_attribute, n);
63         if (a == NULL) {
64                 ldb_oom(ldb);
65                 return -1;
66         }
67         ldb->schema.attributes = a;
68
69         for (i = 0; i < ldb->schema.num_attributes; i++) {
70                 int cmp = ldb_attr_cmp(attribute, a[i].name);
71                 if (cmp == 0) {
72                         /* silently ignore attempts to overwrite fixed attributes */
73                         if (a[i].flags & LDB_ATTR_FLAG_FIXED) {
74                                 return 0;
75                         }
76                         if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
77                                 talloc_free(discard_const_p(char, a[i].name));
78                         }
79                         /* To cancel out increment below */
80                         ldb->schema.num_attributes--;
81                         break;
82                 } else if (cmp < 0) {
83                         memmove(a+i+1, a+i, sizeof(*a) * (ldb->schema.num_attributes-i));
84                         break;
85                 }
86         }
87         ldb->schema.num_attributes++;
88
89         a[i].name       = attribute;
90         a[i].flags      = flags;
91         a[i].syntax     = syntax;
92
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) {
96                         ldb_oom(ldb);
97                         return -1;
98                 }
99         }
100
101         return 0;
102 }
103
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
110 };
111
112 static const struct ldb_schema_attribute ldb_attribute_default = {
113         .name   = NULL,
114         .flags  = 0,
115         .syntax = &ldb_syntax_default
116 };
117
118 /*
119   return the attribute handlers for a given attribute
120 */
121 const struct ldb_schema_attribute *ldb_schema_attribute_by_name(struct ldb_context *ldb,
122                                                                 const char *name)
123 {
124         int i, e, b = 0, r;
125         const struct ldb_schema_attribute *def = &ldb_attribute_default;
126
127         if (ldb->schema.attribute_handler_override) {
128                 const struct ldb_schema_attribute *ret = 
129                         ldb->schema.attribute_handler_override(ldb, 
130                                                                ldb->schema.attribute_handler_override_private,
131                                                                name);
132                 if (ret) {
133                         return ret;
134                 }
135         }
136
137         /* as handlers are sorted, '*' must be the first if present */
138         if (strcmp(ldb->schema.attributes[0].name, "*") == 0) {
139                 def = &ldb->schema.attributes[0];
140                 b = 1;
141         }
142
143         /* do a binary search on the array */
144         e = ldb->schema.num_attributes - 1;
145
146         while (b <= e) {
147
148                 i = (b + e) / 2;
149
150                 r = ldb_attr_cmp(name, ldb->schema.attributes[i].name);
151                 if (r == 0) {
152                         return &ldb->schema.attributes[i];
153                 }
154                 if (r < 0) {
155                         e = i - 1;
156                 } else {
157                         b = i + 1;
158                 }
159
160         }
161
162         return def;
163 }
164
165
166 /*
167   add to the list of ldif handlers for this ldb context
168 */
169 void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name)
170 {
171         const struct ldb_schema_attribute *a;
172         int i;
173
174         a = ldb_schema_attribute_by_name(ldb, name);
175         if (a == NULL || a->name == NULL) {
176                 return;
177         }
178
179         /* FIXED attributes are never removed */
180         if (a->flags & LDB_ATTR_FLAG_FIXED) {
181                 return;
182         }
183
184         if (a->flags & LDB_ATTR_FLAG_ALLOCATED) {
185                 talloc_free(discard_const_p(char, a->name));
186         }
187
188         i = a - ldb->schema.attributes;
189         if (i < ldb->schema.num_attributes - 1) {
190                 memmove(&ldb->schema.attributes[i], 
191                         a+1, sizeof(*a) * (ldb->schema.num_attributes-(i+1)));
192         }
193
194         ldb->schema.num_attributes--;
195 }
196
197 /*
198   setup a attribute handler using a standard syntax
199 */
200 int ldb_schema_attribute_add(struct ldb_context *ldb,
201                              const char *attribute,
202                              unsigned flags,
203                              const char *syntax)
204 {
205         const struct ldb_schema_syntax *s = ldb_standard_syntax_by_name(ldb, syntax);
206         return ldb_schema_attribute_add_with_syntax(ldb, attribute, flags, s);
207 }
208
209 /*
210   setup the attribute handles for well known attributes
211 */
212 int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
213 {
214         const struct {
215                 const char *attr;
216                 const char *syntax;
217         } wellknown[] = {
218                 { "dn", LDB_SYNTAX_DN },
219                 { "distinguishedName", LDB_SYNTAX_DN },
220                 { "cn", LDB_SYNTAX_DIRECTORY_STRING },
221                 { "dc", LDB_SYNTAX_DIRECTORY_STRING },
222                 { "ou", LDB_SYNTAX_DIRECTORY_STRING },
223                 { "objectClass", LDB_SYNTAX_OBJECTCLASS }
224         };
225         int i;
226         int ret;
227
228         for (i=0;i<ARRAY_SIZE(wellknown);i++) {
229                 ret = ldb_schema_attribute_add(ldb, wellknown[i].attr, 0,
230                                                wellknown[i].syntax);
231                 if (ret != LDB_SUCCESS) {
232                         return ret;
233                 }
234         }
235
236         return LDB_SUCCESS;
237 }
238
239
240 /*
241   add a extended dn syntax to the ldb_schema
242 */
243 int ldb_dn_extended_add_syntax(struct ldb_context *ldb, 
244                                unsigned flags,
245                                const struct ldb_dn_extended_syntax *syntax)
246 {
247         int n;
248         struct ldb_dn_extended_syntax *a;
249
250         if (!syntax) {
251                 return LDB_ERR_OPERATIONS_ERROR;
252         }
253
254         n = ldb->schema.num_dn_extended_syntax + 1;
255
256         a = talloc_realloc(ldb, ldb->schema.dn_extended_syntax,
257                            struct ldb_dn_extended_syntax, n);
258
259         if (!a) {
260                 return LDB_ERR_OPERATIONS_ERROR;
261         }
262
263         a[ldb->schema.num_dn_extended_syntax] = *syntax;
264         ldb->schema.dn_extended_syntax = a;
265
266         ldb->schema.num_dn_extended_syntax = n;
267
268         return LDB_SUCCESS;
269 }
270
271 /*
272   return the extended dn syntax for a given name
273 */
274 const struct ldb_dn_extended_syntax *ldb_dn_extended_syntax_by_name(struct ldb_context *ldb,
275                                                                     const char *name)
276 {
277         int i;
278         for (i=0; i < ldb->schema.num_dn_extended_syntax; i++) {
279                 if (ldb_attr_cmp(ldb->schema.dn_extended_syntax[i].name, name) == 0) {
280                         return &ldb->schema.dn_extended_syntax[i];
281                 }
282         }
283         return NULL;
284 }
285
286 /*
287   set an attribute handler override function - used to delegate schema handling
288   to external code
289  */
290 void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
291                                                ldb_attribute_handler_override_fn_t override,
292                                                void *private_data)
293 {
294         ldb->schema.attribute_handler_override_private = private_data;
295         ldb->schema.attribute_handler_override = override;
296 }