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