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