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