ab6aa0b7341a9871a3072e690095662753c75743
[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 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, see <http://www.gnu.org/licenses/>.
22 */
23 /*
24   register handlers for specific attributes and objectclass relationships
25
26   this allows a backend to store its schema information in any format
27   it likes (or to not have any schema information at all) while keeping the 
28   message matching logic generic
29 */
30
31 #include "ldb_includes.h"
32
33 /*
34   add a attribute to the ldb_schema
35
36   if flags contains LDB_ATTR_FLAG_ALLOCATED
37   the attribute name string will be copied using
38   talloc_strdup(), otherwise it needs to be a static const
39   string at least with a lifetime longer than the ldb struct!
40   
41   the ldb_schema_syntax structure should be a pointer
42   to a static const struct or at least it needs to be
43   a struct with a longer lifetime than the ldb context!
44
45 */
46 int ldb_schema_attribute_add_with_syntax(struct ldb_context *ldb, 
47                                          const char *attribute,
48                                          unsigned flags,
49                                          const struct ldb_schema_syntax *syntax)
50 {
51         int i, n;
52         struct ldb_schema_attribute *a;
53
54         n = ldb->schema.num_attributes + 1;
55
56         a = talloc_realloc(ldb, ldb->schema.attributes,
57                            struct ldb_schema_attribute, n);
58         if (a == NULL) {
59                 ldb_oom(ldb);
60                 return -1;
61         }
62         ldb->schema.attributes = a;
63
64         for (i = 0; i < ldb->schema.num_attributes; i++) {
65                 int cmp = ldb_attr_cmp(attribute, a[i].name);
66                 if (cmp == 0) {
67                         if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
68                                 talloc_free(discard_const_p(char, a[i].name));
69                         }
70                         /* To cancel out increment below */
71                         ldb->schema.num_attributes--;
72                         break;
73                 } else if (cmp < 0) {
74                         memmove(a+i+1, a+i, sizeof(*a) * (ldb->schema.num_attributes-i));
75                         break;
76                 }
77         }
78         ldb->schema.num_attributes++;
79
80         a[i].name       = attribute;
81         a[i].flags      = flags;
82         a[i].syntax     = syntax;
83
84         if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
85                 a[i].name = talloc_strdup(a, a[i].name);
86                 if (a[i].name == NULL) {
87                         ldb_oom(ldb);
88                         return -1;
89                 }
90         }
91
92         return 0;
93 }
94
95 static const struct ldb_schema_syntax ldb_syntax_default = {
96         .name            = LDB_SYNTAX_OCTET_STRING,
97         .ldif_read_fn    = ldb_handler_copy,
98         .ldif_write_fn   = ldb_handler_copy,
99         .canonicalise_fn = ldb_handler_copy,
100         .comparison_fn   = ldb_comparison_binary
101 };
102
103 static const struct ldb_schema_attribute ldb_attribute_default = {
104         .name   = NULL,
105         .flags  = 0,
106         .syntax = &ldb_syntax_default
107 };
108
109 /*
110   return the attribute handlers for a given attribute
111 */
112 const struct ldb_schema_attribute *ldb_schema_attribute_by_name(struct ldb_context *ldb,
113                                                                 const char *name)
114 {
115         int i, e, b = 0, r;
116         const struct ldb_schema_attribute *def = &ldb_attribute_default;
117
118         /* as handlers are sorted, '*' must be the first if present */
119         if (strcmp(ldb->schema.attributes[0].name, "*") == 0) {
120                 def = &ldb->schema.attributes[0];
121                 b = 1;
122         }
123
124         /* do a binary search on the array */
125         e = ldb->schema.num_attributes - 1;
126
127         while (b <= e) {
128
129                 i = (b + e) / 2;
130
131                 r = ldb_attr_cmp(name, ldb->schema.attributes[i].name);
132                 if (r == 0) {
133                         return &ldb->schema.attributes[i];
134                 }
135                 if (r < 0) {
136                         e = i - 1;
137                 } else {
138                         b = i + 1;
139                 }
140
141         }
142
143         return def;
144 }
145
146
147 /*
148   add to the list of ldif handlers for this ldb context
149 */
150 void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name)
151 {
152         const struct ldb_schema_attribute *a;
153         int i;
154
155         a = ldb_schema_attribute_by_name(ldb, name);
156         if (a == NULL || a->name == NULL) {
157                 return;
158         }
159
160         if (a->flags & LDB_ATTR_FLAG_ALLOCATED) {
161                 talloc_free(discard_const_p(char, a->name));
162         }
163
164         i = a - ldb->schema.attributes;
165         if (i < ldb->schema.num_attributes - 1) {
166                 memmove(&ldb->schema.attributes[i], 
167                         a+1, sizeof(*a) * (ldb->schema.num_attributes-(i+1)));
168         }
169
170         ldb->schema.num_attributes--;
171 }
172
173 /*
174   setup a attribute handler using a standard syntax
175 */
176 int ldb_schema_attribute_add(struct ldb_context *ldb,
177                              const char *attribute,
178                              unsigned flags,
179                              const char *syntax)
180 {
181         const struct ldb_schema_syntax *s = ldb_standard_syntax_by_name(ldb, syntax);
182         return ldb_schema_attribute_add_with_syntax(ldb, attribute, flags, s);
183 }
184
185 /*
186   setup the attribute handles for well known attributes
187 */
188 int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
189 {
190         const struct {
191                 const char *attr;
192                 const char *syntax;
193         } wellknown[] = {
194                 { "dn", LDB_SYNTAX_DN },
195                 { "distinguishedName", LDB_SYNTAX_DN },
196                 { "cn", LDB_SYNTAX_DIRECTORY_STRING },
197                 { "dc", LDB_SYNTAX_DIRECTORY_STRING },
198                 { "ou", LDB_SYNTAX_DIRECTORY_STRING },
199                 { "objectClass", LDB_SYNTAX_OBJECTCLASS }
200         };
201         int i;
202         int ret;
203
204         for (i=0;i<ARRAY_SIZE(wellknown);i++) {
205                 ret = ldb_schema_attribute_add(ldb, wellknown[i].attr, 0,
206                                                wellknown[i].syntax);
207                 if (ret != LDB_SUCCESS) {
208                         return ret;
209                 }
210         }
211
212         return LDB_SUCCESS;
213 }
214