Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[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                         /* silently ignore attempts to overwrite fixed attributes */
68                         if (a[i].flags & LDB_ATTR_FLAG_FIXED) {
69                                 return 0;
70                         }
71                         if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
72                                 talloc_free(discard_const_p(char, a[i].name));
73                         }
74                         /* To cancel out increment below */
75                         ldb->schema.num_attributes--;
76                         break;
77                 } else if (cmp < 0) {
78                         memmove(a+i+1, a+i, sizeof(*a) * (ldb->schema.num_attributes-i));
79                         break;
80                 }
81         }
82         ldb->schema.num_attributes++;
83
84         a[i].name       = attribute;
85         a[i].flags      = flags;
86         a[i].syntax     = syntax;
87
88         if (a[i].flags & LDB_ATTR_FLAG_ALLOCATED) {
89                 a[i].name = talloc_strdup(a, a[i].name);
90                 if (a[i].name == NULL) {
91                         ldb_oom(ldb);
92                         return -1;
93                 }
94         }
95
96         return 0;
97 }
98
99 static const struct ldb_schema_syntax ldb_syntax_default = {
100         .name            = LDB_SYNTAX_OCTET_STRING,
101         .ldif_read_fn    = ldb_handler_copy,
102         .ldif_write_fn   = ldb_handler_copy,
103         .canonicalise_fn = ldb_handler_copy,
104         .comparison_fn   = ldb_comparison_binary
105 };
106
107 static const struct ldb_schema_attribute ldb_attribute_default = {
108         .name   = NULL,
109         .flags  = 0,
110         .syntax = &ldb_syntax_default
111 };
112
113 /*
114   return the attribute handlers for a given attribute
115 */
116 const struct ldb_schema_attribute *ldb_schema_attribute_by_name(struct ldb_context *ldb,
117                                                                 const char *name)
118 {
119         int i, e, b = 0, r;
120         const struct ldb_schema_attribute *def = &ldb_attribute_default;
121
122         /* as handlers are sorted, '*' must be the first if present */
123         if (strcmp(ldb->schema.attributes[0].name, "*") == 0) {
124                 def = &ldb->schema.attributes[0];
125                 b = 1;
126         }
127
128         /* do a binary search on the array */
129         e = ldb->schema.num_attributes - 1;
130
131         while (b <= e) {
132
133                 i = (b + e) / 2;
134
135                 r = ldb_attr_cmp(name, ldb->schema.attributes[i].name);
136                 if (r == 0) {
137                         return &ldb->schema.attributes[i];
138                 }
139                 if (r < 0) {
140                         e = i - 1;
141                 } else {
142                         b = i + 1;
143                 }
144
145         }
146
147         return def;
148 }
149
150
151 /*
152   add to the list of ldif handlers for this ldb context
153 */
154 void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name)
155 {
156         const struct ldb_schema_attribute *a;
157         int i;
158
159         a = ldb_schema_attribute_by_name(ldb, name);
160         if (a == NULL || a->name == NULL) {
161                 return;
162         }
163
164         /* FIXED attributes are never removed */
165         if (a->flags & LDB_ATTR_FLAG_FIXED) {
166                 return;
167         }
168
169         if (a->flags & LDB_ATTR_FLAG_ALLOCATED) {
170                 talloc_free(discard_const_p(char, a->name));
171         }
172
173         i = a - ldb->schema.attributes;
174         if (i < ldb->schema.num_attributes - 1) {
175                 memmove(&ldb->schema.attributes[i], 
176                         a+1, sizeof(*a) * (ldb->schema.num_attributes-(i+1)));
177         }
178
179         ldb->schema.num_attributes--;
180 }
181
182 /*
183   setup a attribute handler using a standard syntax
184 */
185 int ldb_schema_attribute_add(struct ldb_context *ldb,
186                              const char *attribute,
187                              unsigned flags,
188                              const char *syntax)
189 {
190         const struct ldb_schema_syntax *s = ldb_standard_syntax_by_name(ldb, syntax);
191         return ldb_schema_attribute_add_with_syntax(ldb, attribute, flags, s);
192 }
193
194 /*
195   setup the attribute handles for well known attributes
196 */
197 int ldb_setup_wellknown_attributes(struct ldb_context *ldb)
198 {
199         const struct {
200                 const char *attr;
201                 const char *syntax;
202         } wellknown[] = {
203                 { "dn", LDB_SYNTAX_DN },
204                 { "distinguishedName", LDB_SYNTAX_DN },
205                 { "cn", LDB_SYNTAX_DIRECTORY_STRING },
206                 { "dc", LDB_SYNTAX_DIRECTORY_STRING },
207                 { "ou", LDB_SYNTAX_DIRECTORY_STRING },
208                 { "objectClass", LDB_SYNTAX_OBJECTCLASS }
209         };
210         int i;
211         int ret;
212
213         for (i=0;i<ARRAY_SIZE(wellknown);i++) {
214                 ret = ldb_schema_attribute_add(ldb, wellknown[i].attr, 0,
215                                                wellknown[i].syntax);
216                 if (ret != LDB_SUCCESS) {
217                         return ret;
218                 }
219         }
220
221         return LDB_SUCCESS;
222 }
223