r22681: Fix standalone ldb build when parent directory name != ldb.
[kamenim/samba.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 2 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, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24 /*
25   register handlers for specific attributes and objectclass relationships
26
27   this allows a backend to store its schema information in any format
28   it likes (or to not have any schema information at all) while keeping the 
29   message matching logic generic
30 */
31
32 #include "ldb_includes.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         n = ldb->schema.num_attributes + 1;
56
57         a = talloc_realloc(ldb, ldb->schema.attributes,
58                            struct ldb_schema_attribute, n);
59         if (a == NULL) {
60                 ldb_oom(ldb);
61                 return -1;
62         }
63         ldb->schema.attributes = a;
64
65         for (i = 0; i < ldb->schema.num_attributes; i++) {
66                 if (ldb_attr_cmp(attribute, a[i].name) < 0) {
67                         memmove(a+i+1, a+i, sizeof(*a) * (ldb->schema.num_attributes-i));
68                         break;
69                 }
70         }
71
72         a[i].name       = attribute;
73         a[i].flags      = flags;
74         a[i].syntax     = syntax;
75
76         if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
77                 a[i].name = talloc_strdup(a, a[i].name);
78                 if (a[i].name == NULL) {
79                         ldb_oom(ldb);
80                         return -1;
81                 }
82         }
83
84         ldb->schema.num_attributes++;
85         return 0;
86 }
87
88 static const struct ldb_schema_syntax ldb_syntax_default = {
89         .name            = LDB_SYNTAX_OCTET_STRING,
90         .ldif_read_fn    = ldb_handler_copy,
91         .ldif_write_fn   = ldb_handler_copy,
92         .canonicalise_fn = ldb_handler_copy,
93         .comparison_fn   = ldb_comparison_binary
94 };
95
96 static const struct ldb_schema_attribute ldb_attribute_default = {
97         .name   = NULL,
98         .flags  = 0,
99         .syntax = &ldb_syntax_default
100 };
101
102 /*
103   return the attribute handlers for a given attribute
104 */
105 const struct ldb_schema_attribute *ldb_schema_attribute_by_name(struct ldb_context *ldb,
106                                                                 const char *name)
107 {
108         int i, e, b = 0, r;
109         const struct ldb_schema_attribute *def = &ldb_attribute_default;
110
111         /* as handlers are sorted, '*' must be the first if present */
112         if (strcmp(ldb->schema.attributes[0].name, "*") == 0) {
113                 def = &ldb->schema.attributes[0];
114                 b = 1;
115         }
116
117         /* do a binary search on the array */
118         e = ldb->schema.num_attributes - 1;
119
120         while (b <= e) {
121
122                 i = (b + e) / 2;
123
124                 r = ldb_attr_cmp(name, ldb->schema.attributes[i].name);
125                 if (r == 0) {
126                         return &ldb->schema.attributes[i];
127                 }
128                 if (r < 0) {
129                         e = i - 1;
130                 } else {
131                         b = i + 1;
132                 }
133
134         }
135
136         return def;
137 }
138
139
140 /*
141   add to the list of ldif handlers for this ldb context
142 */
143 void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name)
144 {
145         const struct ldb_schema_attribute *a;
146         int i;
147
148         a = ldb_schema_attribute_by_name(ldb, name);
149         if (a == NULL) {
150                 return;
151         }
152
153         if (a->flags & LDB_ATTR_FLAG_ALLOCATED) {
154                 talloc_free(discard_const_p(char, a->name));
155         }
156
157         i = a - ldb->schema.attributes;
158         if (i < ldb->schema.num_attributes - 1) {
159                 memmove(&ldb->schema.attributes[i], 
160                         a+1, sizeof(*a) * (ldb->schema.num_attributes-(i+1)));
161         }
162
163         ldb->schema.num_attributes--;
164 }
165
166 /*
167   setup a attribute handler using a standard syntax
168 */
169 int ldb_schema_attribute_add(struct ldb_context *ldb,
170                              const char *attribute,
171                              unsigned flags,
172                              const char *syntax)
173 {
174         const struct ldb_schema_syntax *s = ldb_standard_syntax_by_name(ldb, syntax);
175         return ldb_schema_attribute_add_with_syntax(ldb, attribute, flags, s);
176 }
177
178 /*
179   setup the attribute handles for well known attributes
180 */
181 int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
182 {
183         const struct {
184                 const char *attr;
185                 const char *syntax;
186         } wellknown[] = {
187                 { "dn", LDB_SYNTAX_DN },
188                 { "distinguishedName", LDB_SYNTAX_DN },
189                 { "cn", LDB_SYNTAX_DIRECTORY_STRING },
190                 { "dc", LDB_SYNTAX_DIRECTORY_STRING },
191                 { "ou", LDB_SYNTAX_DIRECTORY_STRING },
192                 { "objectClass", LDB_SYNTAX_OBJECTCLASS }
193         };
194         int i;
195         int ret;
196
197         for (i=0;i<ARRAY_SIZE(wellknown);i++) {
198                 ret = ldb_schema_attribute_add(ldb, wellknown[i].attr, 0,
199                                                wellknown[i].syntax);
200                 if (ret != LDB_SUCCESS) {
201                         return ret;
202                 }
203         }
204
205         return LDB_SUCCESS;
206 }
207
208 /*
209   return the list of subclasses for a class
210 */
211 const char **ldb_subclass_list(struct ldb_context *ldb, const char *classname)
212 {
213         int i;
214         for (i=0;i<ldb->schema.num_classes;i++) {
215                 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
216                         return (const char **)ldb->schema.classes[i].subclasses;
217                 }
218         }
219         return NULL;
220 }
221
222
223 /*
224   add a new subclass
225 */
226 static int ldb_subclass_new(struct ldb_context *ldb, const char *classname, const char *subclass)
227 {
228         struct ldb_subclass *s, *c;
229         s = talloc_realloc(ldb, ldb->schema.classes, struct ldb_subclass, ldb->schema.num_classes+1);
230         if (s == NULL) goto failed;
231
232         ldb->schema.classes = s;
233         c = &s[ldb->schema.num_classes];
234         c->name = talloc_strdup(s, classname);
235         if (c->name == NULL) goto failed;
236
237         c->subclasses = talloc_array(s, char *, 2);
238         if (c->subclasses == NULL) goto failed;
239
240         c->subclasses[0] = talloc_strdup(c->subclasses, subclass);
241         if (c->subclasses[0] == NULL) goto failed;
242         c->subclasses[1] = NULL;
243
244         ldb->schema.num_classes++;
245
246         return 0;
247 failed:
248         ldb_oom(ldb);
249         return -1;
250 }
251
252 /*
253   add a subclass
254 */
255 int ldb_subclass_add(struct ldb_context *ldb, const char *classname, const char *subclass)
256 {
257         int i, n;
258         struct ldb_subclass *c;
259         char **s;
260
261         for (i=0;i<ldb->schema.num_classes;i++) {
262                 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
263                         break;
264                 }
265         }
266         if (i == ldb->schema.num_classes) {
267                 return ldb_subclass_new(ldb, classname, subclass);
268         }
269         c = &ldb->schema.classes[i];
270         
271         for (n=0;c->subclasses[n];n++) /* noop */;
272
273         s = talloc_realloc(ldb->schema.classes, c->subclasses, char *, n+2);
274         if (s == NULL) {
275                 ldb_oom(ldb);
276                 return -1;
277         }
278
279         c->subclasses = s;
280         s[n] = talloc_strdup(s, subclass);
281         if (s[n] == NULL) {
282                 ldb_oom(ldb);
283                 return -1;
284         }
285         s[n+1] = NULL;
286
287         return 0;
288 }
289
290 /*
291   remove a set of subclasses for a class
292 */
293 void ldb_subclass_remove(struct ldb_context *ldb, const char *classname)
294 {
295         int i;
296         struct ldb_subclass *c;
297
298         for (i=0;i<ldb->schema.num_classes;i++) {
299                 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
300                         break;
301                 }
302         }
303         if (i == ldb->schema.num_classes) {
304                 return;
305         }
306
307         c = &ldb->schema.classes[i];
308         talloc_free(c->name);
309         talloc_free(c->subclasses);
310         if (ldb->schema.num_classes-(i+1) > 0) {
311                 memmove(c, c+1, sizeof(*c) * (ldb->schema.num_classes-(i+1)));
312         }
313         ldb->schema.num_classes--;
314         if (ldb->schema.num_classes == 0) {
315                 talloc_free(ldb->schema.classes);
316                 ldb->schema.classes = NULL;
317         }
318 }