14131564cccee875802f28a45f77ff18757bb746
[ira/wip.git] / source3 / 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, 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 to the list of ldif handlers for this ldb context
37 */
38 int ldb_set_attrib_handlers(struct ldb_context *ldb, 
39                             const struct ldb_attrib_handler *handlers, 
40                             unsigned num_handlers)
41 {
42         int i;
43         struct ldb_attrib_handler *h;
44         h = talloc_realloc(ldb, ldb->schema.attrib_handlers,
45                            struct ldb_attrib_handler,
46                            ldb->schema.num_attrib_handlers + num_handlers);
47         if (h == NULL) {
48                 ldb_oom(ldb);
49                 return -1;
50         }
51         ldb->schema.attrib_handlers = h;
52         memcpy(h + ldb->schema.num_attrib_handlers, 
53                handlers, sizeof(*h) * num_handlers);
54         for (i=0;i<num_handlers;i++) {
55                 if (h[ldb->schema.num_attrib_handlers+i].flags & LDB_ATTR_FLAG_ALLOCATED) {
56                         h[ldb->schema.num_attrib_handlers+i].attr = talloc_strdup(ldb->schema.attrib_handlers,
57                                                                                   h[ldb->schema.num_attrib_handlers+i].attr);
58                         if (h[ldb->schema.num_attrib_handlers+i].attr == NULL) {
59                                 ldb_oom(ldb);
60                                 return -1;
61                         }
62                 }
63         }
64         ldb->schema.num_attrib_handlers += num_handlers;
65         return 0;
66 }
67                           
68
69 /*
70   default function for read/write/canonicalise
71 */
72 static int ldb_default_copy(struct ldb_context *ldb, 
73                             void *mem_ctx,
74                             const struct ldb_val *in, 
75                             struct ldb_val *out)
76 {
77         *out = ldb_val_dup(mem_ctx, in);
78
79         if (out->data == NULL && in->data != NULL) {
80                 return -1;
81         }
82
83         return 0;
84 }
85
86 /*
87   default function for comparison
88 */
89 static int ldb_default_cmp(struct ldb_context *ldb, 
90                             void *mem_ctx,
91                            const struct ldb_val *v1, 
92                            const struct ldb_val *v2)
93 {
94         if (v1->length != v2->length) {
95                 return v1->length - v2->length;
96         }
97         return memcmp(v1->data, v2->data, v1->length);
98 }
99
100 /*
101   default handler function pointers
102 */
103 static const struct ldb_attrib_handler ldb_default_attrib_handler = {
104         .attr = NULL,
105         .ldif_read_fn    = ldb_default_copy,
106         .ldif_write_fn   = ldb_default_copy,
107         .canonicalise_fn = ldb_default_copy,
108         .comparison_fn   = ldb_default_cmp,
109 };
110
111 /*
112   return the attribute handlers for a given attribute
113 */
114 const struct ldb_attrib_handler *ldb_attrib_handler(struct ldb_context *ldb,
115                                                     const char *attrib)
116 {
117         int i;
118         const struct ldb_attrib_handler *def = &ldb_default_attrib_handler;
119         /* TODO: should be replaced with a binary search, with a sort on add */
120         for (i=0;i<ldb->schema.num_attrib_handlers;i++) {
121                 if (strcmp(ldb->schema.attrib_handlers[i].attr, "*") == 0) {
122                         def = &ldb->schema.attrib_handlers[i];
123                 }
124                 if (ldb_attr_cmp(attrib, ldb->schema.attrib_handlers[i].attr) == 0) {
125                         return &ldb->schema.attrib_handlers[i];
126                 }
127         }
128         return def;
129 }
130
131
132 /*
133   add to the list of ldif handlers for this ldb context
134 */
135 void ldb_remove_attrib_handler(struct ldb_context *ldb, const char *attrib)
136 {
137         const struct ldb_attrib_handler *h;
138         int i;
139         h = ldb_attrib_handler(ldb, attrib);
140         if (h == &ldb_default_attrib_handler) {
141                 return;
142         }
143         if (h->flags & LDB_ATTR_FLAG_ALLOCATED) {
144                 talloc_free(discard_const_p(char, h->attr));
145         }
146         i = h - ldb->schema.attrib_handlers;
147         if (i < ldb->schema.num_attrib_handlers - 1) {
148                 memmove(&ldb->schema.attrib_handlers[i], 
149                         h+1, sizeof(*h) * (ldb->schema.num_attrib_handlers-(i+1)));
150         }
151         ldb->schema.num_attrib_handlers--;
152 }
153
154 /*
155   setup a attribute handler using a standard syntax
156 */
157 int ldb_set_attrib_handler_syntax(struct ldb_context *ldb, 
158                                   const char *attr, const char *syntax)
159 {
160         const struct ldb_attrib_handler *h = ldb_attrib_handler_syntax(ldb, syntax);
161         struct ldb_attrib_handler h2;
162         if (h == NULL) {
163                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Unknown syntax '%s'\n", syntax);
164                 return -1;
165         }
166         h2 = *h;
167         h2.attr = attr;
168         return ldb_set_attrib_handlers(ldb, &h2, 1);
169 }
170
171 /*
172   setup the attribute handles for well known attributes
173 */
174 int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
175 {
176         const struct {
177                 const char *attr;
178                 const char *syntax;
179         } wellknown[] = {
180                 { "dn", LDB_SYNTAX_DN },
181                 { "ncName", LDB_SYNTAX_DN },
182                 { "distinguishedName", LDB_SYNTAX_DN },
183                 { "cn", LDB_SYNTAX_DIRECTORY_STRING },
184                 { "dc", LDB_SYNTAX_DIRECTORY_STRING },
185                 { "ou", LDB_SYNTAX_DIRECTORY_STRING },
186                 { "objectClass", LDB_SYNTAX_OBJECTCLASS }
187         };
188         int i;
189         for (i=0;i<ARRAY_SIZE(wellknown);i++) {
190                 if (ldb_set_attrib_handler_syntax(ldb, wellknown[i].attr, 
191                                                   wellknown[i].syntax) != 0) {
192                         return -1;
193                 }
194         }
195         return 0;
196 }
197
198
199 /*
200   return the list of subclasses for a class
201 */
202 const char **ldb_subclass_list(struct ldb_context *ldb, const char *classname)
203 {
204         int i;
205         for (i=0;i<ldb->schema.num_classes;i++) {
206                 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
207                         return (const char **)ldb->schema.classes[i].subclasses;
208                 }
209         }
210         return NULL;
211 }
212
213
214 /*
215   add a new subclass
216 */
217 static int ldb_subclass_new(struct ldb_context *ldb, const char *classname, const char *subclass)
218 {
219         struct ldb_subclass *s, *c;
220         s = talloc_realloc(ldb, ldb->schema.classes, struct ldb_subclass, ldb->schema.num_classes+1);
221         if (s == NULL) goto failed;
222
223         ldb->schema.classes = s;
224         c = &s[ldb->schema.num_classes];
225         c->name = talloc_strdup(s, classname);
226         if (c->name == NULL) goto failed;
227
228         c->subclasses = talloc_array(s, char *, 2);
229         if (c->subclasses == NULL) goto failed;
230
231         c->subclasses[0] = talloc_strdup(c->subclasses, subclass);
232         if (c->subclasses[0] == NULL) goto failed;
233         c->subclasses[1] = NULL;
234
235         ldb->schema.num_classes++;
236
237         return 0;
238 failed:
239         ldb_oom(ldb);
240         return -1;
241 }
242
243 /*
244   add a subclass
245 */
246 int ldb_subclass_add(struct ldb_context *ldb, const char *classname, const char *subclass)
247 {
248         int i, n;
249         struct ldb_subclass *c;
250         char **s;
251
252         for (i=0;i<ldb->schema.num_classes;i++) {
253                 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
254                         break;
255                 }
256         }
257         if (i == ldb->schema.num_classes) {
258                 return ldb_subclass_new(ldb, classname, subclass);
259         }
260         c = &ldb->schema.classes[i];
261         
262         for (n=0;c->subclasses[n];n++) /* noop */;
263
264         s = talloc_realloc(ldb->schema.classes, c->subclasses, char *, n+2);
265         if (s == NULL) {
266                 ldb_oom(ldb);
267                 return -1;
268         }
269
270         c->subclasses = s;
271         s[n] = talloc_strdup(s, subclass);
272         if (s[n] == NULL) {
273                 ldb_oom(ldb);
274                 return -1;
275         }
276         s[n+1] = NULL;
277
278         return 0;
279 }
280
281 /*
282   remove a set of subclasses for a class
283 */
284 void ldb_subclass_remove(struct ldb_context *ldb, const char *classname)
285 {
286         int i;
287         struct ldb_subclass *c;
288
289         for (i=0;i<ldb->schema.num_classes;i++) {
290                 if (ldb_attr_cmp(classname, ldb->schema.classes[i].name) == 0) {
291                         break;
292                 }
293         }
294         if (i == ldb->schema.num_classes) {
295                 return;
296         }
297
298         c = &ldb->schema.classes[i];
299         talloc_free(c->name);
300         talloc_free(c->subclasses);
301         if (ldb->schema.num_classes-(i+1) > 0) {
302                 memmove(c, c+1, sizeof(*c) * (ldb->schema.num_classes-(i+1)));
303         }
304         ldb->schema.num_classes--;
305         if (ldb->schema.num_classes == 0) {
306                 talloc_free(ldb->schema.classes);
307                 ldb->schema.classes = NULL;
308         }
309 }