1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | DSDB schema header
|
---|
4 |
|
---|
5 | Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
|
---|
6 | Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 3 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 |
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 | #include "lib/util/dlinklist.h"
|
---|
25 | #include "dsdb/samdb/samdb.h"
|
---|
26 | #include <ldb_module.h>
|
---|
27 | #include "param/param.h"
|
---|
28 | #include "librpc/ndr/libndr.h"
|
---|
29 | #include "librpc/gen_ndr/ndr_misc.h"
|
---|
30 | #include "lib/util/tsort.h"
|
---|
31 |
|
---|
32 | /*
|
---|
33 | override the name to attribute handler function
|
---|
34 | */
|
---|
35 | const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb,
|
---|
36 | void *private_data,
|
---|
37 | const char *name)
|
---|
38 | {
|
---|
39 | struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
|
---|
40 | const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
|
---|
41 | if (a == NULL) {
|
---|
42 | /* this will fall back to ldb internal handling */
|
---|
43 | return NULL;
|
---|
44 | }
|
---|
45 | return a->ldb_schema_attribute;
|
---|
46 | }
|
---|
47 |
|
---|
48 | static int dsdb_schema_set_attributes(struct ldb_context *ldb, struct dsdb_schema *schema, bool write_attributes)
|
---|
49 | {
|
---|
50 | int ret = LDB_SUCCESS;
|
---|
51 | struct ldb_result *res;
|
---|
52 | struct ldb_result *res_idx;
|
---|
53 | struct dsdb_attribute *attr;
|
---|
54 | struct ldb_message *mod_msg;
|
---|
55 | TALLOC_CTX *mem_ctx;
|
---|
56 | struct ldb_message *msg;
|
---|
57 | struct ldb_message *msg_idx;
|
---|
58 |
|
---|
59 | /* setup our own attribute name to schema handler */
|
---|
60 | ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
|
---|
61 |
|
---|
62 | if (!write_attributes) {
|
---|
63 | return ret;
|
---|
64 | }
|
---|
65 |
|
---|
66 | mem_ctx = talloc_new(ldb);
|
---|
67 | if (!mem_ctx) {
|
---|
68 | return ldb_oom(ldb);
|
---|
69 | }
|
---|
70 |
|
---|
71 | msg = ldb_msg_new(mem_ctx);
|
---|
72 | if (!msg) {
|
---|
73 | ldb_oom(ldb);
|
---|
74 | goto op_error;
|
---|
75 | }
|
---|
76 | msg_idx = ldb_msg_new(mem_ctx);
|
---|
77 | if (!msg_idx) {
|
---|
78 | ldb_oom(ldb);
|
---|
79 | goto op_error;
|
---|
80 | }
|
---|
81 | msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
|
---|
82 | if (!msg->dn) {
|
---|
83 | ldb_oom(ldb);
|
---|
84 | goto op_error;
|
---|
85 | }
|
---|
86 | msg_idx->dn = ldb_dn_new(msg_idx, ldb, "@INDEXLIST");
|
---|
87 | if (!msg_idx->dn) {
|
---|
88 | ldb_oom(ldb);
|
---|
89 | goto op_error;
|
---|
90 | }
|
---|
91 |
|
---|
92 | ret = ldb_msg_add_string(msg_idx, "@IDXONE", "1");
|
---|
93 | if (ret != LDB_SUCCESS) {
|
---|
94 | goto op_error;
|
---|
95 | }
|
---|
96 |
|
---|
97 | for (attr = schema->attributes; attr; attr = attr->next) {
|
---|
98 | const char *syntax = attr->syntax->ldb_syntax;
|
---|
99 |
|
---|
100 | if (!syntax) {
|
---|
101 | syntax = attr->syntax->ldap_oid;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Write out a rough approximation of the schema
|
---|
106 | * as an @ATTRIBUTES value, for bootstrapping
|
---|
107 | */
|
---|
108 | if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
|
---|
109 | ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
|
---|
110 | } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
|
---|
111 | ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "CASE_INSENSITIVE");
|
---|
112 | }
|
---|
113 | if (ret != LDB_SUCCESS) {
|
---|
114 | break;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
|
---|
118 | ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
|
---|
119 | if (ret != LDB_SUCCESS) {
|
---|
120 | break;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (ret != LDB_SUCCESS) {
|
---|
126 | talloc_free(mem_ctx);
|
---|
127 | return ret;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Try to avoid churning the attributes too much,
|
---|
132 | * we only want to do this if they have changed
|
---|
133 | */
|
---|
134 | ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL,
|
---|
135 | NULL);
|
---|
136 | if (ret == LDB_ERR_NO_SUCH_OBJECT) {
|
---|
137 | ret = ldb_add(ldb, msg);
|
---|
138 | } else if (ret != LDB_SUCCESS) {
|
---|
139 | } else if (res->count != 1) {
|
---|
140 | ret = ldb_add(ldb, msg);
|
---|
141 | } else {
|
---|
142 | ret = LDB_SUCCESS;
|
---|
143 | /* Annoyingly added to our search results */
|
---|
144 | ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
|
---|
145 |
|
---|
146 | ret = ldb_msg_difference(ldb, mem_ctx,
|
---|
147 | res->msgs[0], msg, &mod_msg);
|
---|
148 | if (ret != LDB_SUCCESS) {
|
---|
149 | goto op_error;
|
---|
150 | }
|
---|
151 | if (mod_msg->num_elements > 0) {
|
---|
152 | ret = dsdb_replace(ldb, mod_msg, 0);
|
---|
153 | }
|
---|
154 | talloc_free(mod_msg);
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
|
---|
158 | /* We might be on a read-only DB or LDAP */
|
---|
159 | ret = LDB_SUCCESS;
|
---|
160 | }
|
---|
161 | if (ret != LDB_SUCCESS) {
|
---|
162 | talloc_free(mem_ctx);
|
---|
163 | return ret;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /* Now write out the indexes, as found in the schema (if they have changed) */
|
---|
167 |
|
---|
168 | ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE,
|
---|
169 | NULL, NULL);
|
---|
170 | if (ret == LDB_ERR_NO_SUCH_OBJECT) {
|
---|
171 | ret = ldb_add(ldb, msg_idx);
|
---|
172 | } else if (ret != LDB_SUCCESS) {
|
---|
173 | } else if (res_idx->count != 1) {
|
---|
174 | ret = ldb_add(ldb, msg_idx);
|
---|
175 | } else {
|
---|
176 | ret = LDB_SUCCESS;
|
---|
177 | /* Annoyingly added to our search results */
|
---|
178 | ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
|
---|
179 |
|
---|
180 | ret = ldb_msg_difference(ldb, mem_ctx,
|
---|
181 | res_idx->msgs[0], msg_idx, &mod_msg);
|
---|
182 | if (ret != LDB_SUCCESS) {
|
---|
183 | goto op_error;
|
---|
184 | }
|
---|
185 | if (mod_msg->num_elements > 0) {
|
---|
186 | ret = dsdb_replace(ldb, mod_msg, 0);
|
---|
187 | }
|
---|
188 | talloc_free(mod_msg);
|
---|
189 | }
|
---|
190 | if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
|
---|
191 | /* We might be on a read-only DB */
|
---|
192 | ret = LDB_SUCCESS;
|
---|
193 | }
|
---|
194 | talloc_free(mem_ctx);
|
---|
195 | return ret;
|
---|
196 |
|
---|
197 | op_error:
|
---|
198 | talloc_free(mem_ctx);
|
---|
199 | return ldb_operr(ldb);
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int uint32_cmp(uint32_t c1, uint32_t c2)
|
---|
203 | {
|
---|
204 | if (c1 == c2) return 0;
|
---|
205 | return c1 > c2 ? 1 : -1;
|
---|
206 | }
|
---|
207 |
|
---|
208 | static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
|
---|
209 | {
|
---|
210 | return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
|
---|
211 | }
|
---|
212 | static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
|
---|
213 | {
|
---|
214 | return uint32_cmp((*c1)->governsID_id, (*c2)->governsID_id);
|
---|
215 | }
|
---|
216 | static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
|
---|
217 | {
|
---|
218 | return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
|
---|
219 | }
|
---|
220 | static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
|
---|
221 | {
|
---|
222 | return strcasecmp((*c1)->cn, (*c2)->cn);
|
---|
223 | }
|
---|
224 |
|
---|
225 | static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
|
---|
226 | {
|
---|
227 | return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
|
---|
228 | }
|
---|
229 | static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
|
---|
230 | {
|
---|
231 | return uint32_cmp((*a1)->attributeID_id, (*a2)->attributeID_id);
|
---|
232 | }
|
---|
233 | static int dsdb_compare_attribute_by_msDS_IntId(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
|
---|
234 | {
|
---|
235 | return uint32_cmp((*a1)->msDS_IntId, (*a2)->msDS_IntId);
|
---|
236 | }
|
---|
237 | static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
|
---|
238 | {
|
---|
239 | return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
|
---|
240 | }
|
---|
241 | static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
|
---|
242 | {
|
---|
243 | return uint32_cmp((*a1)->linkID, (*a2)->linkID);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Clean up Classes and Attributes accessor arrays
|
---|
248 | */
|
---|
249 | static void dsdb_sorted_accessors_free(struct dsdb_schema *schema)
|
---|
250 | {
|
---|
251 | /* free classes accessors */
|
---|
252 | TALLOC_FREE(schema->classes_by_lDAPDisplayName);
|
---|
253 | TALLOC_FREE(schema->classes_by_governsID_id);
|
---|
254 | TALLOC_FREE(schema->classes_by_governsID_oid);
|
---|
255 | TALLOC_FREE(schema->classes_by_cn);
|
---|
256 | /* free attribute accessors */
|
---|
257 | TALLOC_FREE(schema->attributes_by_lDAPDisplayName);
|
---|
258 | TALLOC_FREE(schema->attributes_by_attributeID_id);
|
---|
259 | TALLOC_FREE(schema->attributes_by_msDS_IntId);
|
---|
260 | TALLOC_FREE(schema->attributes_by_attributeID_oid);
|
---|
261 | TALLOC_FREE(schema->attributes_by_linkID);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /*
|
---|
265 | create the sorted accessor arrays for the schema
|
---|
266 | */
|
---|
267 | int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
|
---|
268 | struct dsdb_schema *schema)
|
---|
269 | {
|
---|
270 | struct dsdb_class *cur;
|
---|
271 | struct dsdb_attribute *a;
|
---|
272 | unsigned int i;
|
---|
273 | unsigned int num_int_id;
|
---|
274 |
|
---|
275 | /* free all caches */
|
---|
276 | dsdb_sorted_accessors_free(schema);
|
---|
277 |
|
---|
278 | /* count the classes */
|
---|
279 | for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
|
---|
280 | schema->num_classes = i;
|
---|
281 |
|
---|
282 | /* setup classes_by_* */
|
---|
283 | schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
|
---|
284 | schema->classes_by_governsID_id = talloc_array(schema, struct dsdb_class *, i);
|
---|
285 | schema->classes_by_governsID_oid = talloc_array(schema, struct dsdb_class *, i);
|
---|
286 | schema->classes_by_cn = talloc_array(schema, struct dsdb_class *, i);
|
---|
287 | if (schema->classes_by_lDAPDisplayName == NULL ||
|
---|
288 | schema->classes_by_governsID_id == NULL ||
|
---|
289 | schema->classes_by_governsID_oid == NULL ||
|
---|
290 | schema->classes_by_cn == NULL) {
|
---|
291 | goto failed;
|
---|
292 | }
|
---|
293 |
|
---|
294 | for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
|
---|
295 | schema->classes_by_lDAPDisplayName[i] = cur;
|
---|
296 | schema->classes_by_governsID_id[i] = cur;
|
---|
297 | schema->classes_by_governsID_oid[i] = cur;
|
---|
298 | schema->classes_by_cn[i] = cur;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /* sort the arrays */
|
---|
302 | TYPESAFE_QSORT(schema->classes_by_lDAPDisplayName, schema->num_classes, dsdb_compare_class_by_lDAPDisplayName);
|
---|
303 | TYPESAFE_QSORT(schema->classes_by_governsID_id, schema->num_classes, dsdb_compare_class_by_governsID_id);
|
---|
304 | TYPESAFE_QSORT(schema->classes_by_governsID_oid, schema->num_classes, dsdb_compare_class_by_governsID_oid);
|
---|
305 | TYPESAFE_QSORT(schema->classes_by_cn, schema->num_classes, dsdb_compare_class_by_cn);
|
---|
306 |
|
---|
307 | /* now build the attribute accessor arrays */
|
---|
308 |
|
---|
309 | /* count the attributes
|
---|
310 | * and attributes with msDS-IntId set */
|
---|
311 | num_int_id = 0;
|
---|
312 | for (i=0, a=schema->attributes; a; i++, a=a->next) {
|
---|
313 | if (a->msDS_IntId != 0) {
|
---|
314 | num_int_id++;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | schema->num_attributes = i;
|
---|
318 | schema->num_int_id_attr = num_int_id;
|
---|
319 |
|
---|
320 | /* setup attributes_by_* */
|
---|
321 | schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
|
---|
322 | schema->attributes_by_attributeID_id = talloc_array(schema, struct dsdb_attribute *, i);
|
---|
323 | schema->attributes_by_msDS_IntId = talloc_array(schema,
|
---|
324 | struct dsdb_attribute *, num_int_id);
|
---|
325 | schema->attributes_by_attributeID_oid = talloc_array(schema, struct dsdb_attribute *, i);
|
---|
326 | schema->attributes_by_linkID = talloc_array(schema, struct dsdb_attribute *, i);
|
---|
327 | if (schema->attributes_by_lDAPDisplayName == NULL ||
|
---|
328 | schema->attributes_by_attributeID_id == NULL ||
|
---|
329 | schema->attributes_by_msDS_IntId == NULL ||
|
---|
330 | schema->attributes_by_attributeID_oid == NULL ||
|
---|
331 | schema->attributes_by_linkID == NULL) {
|
---|
332 | goto failed;
|
---|
333 | }
|
---|
334 |
|
---|
335 | num_int_id = 0;
|
---|
336 | for (i=0, a=schema->attributes; a; i++, a=a->next) {
|
---|
337 | schema->attributes_by_lDAPDisplayName[i] = a;
|
---|
338 | schema->attributes_by_attributeID_id[i] = a;
|
---|
339 | schema->attributes_by_attributeID_oid[i] = a;
|
---|
340 | schema->attributes_by_linkID[i] = a;
|
---|
341 | /* append attr-by-msDS-IntId values */
|
---|
342 | if (a->msDS_IntId != 0) {
|
---|
343 | schema->attributes_by_msDS_IntId[num_int_id] = a;
|
---|
344 | num_int_id++;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | SMB_ASSERT(num_int_id == schema->num_int_id_attr);
|
---|
348 |
|
---|
349 | /* sort the arrays */
|
---|
350 | TYPESAFE_QSORT(schema->attributes_by_lDAPDisplayName, schema->num_attributes, dsdb_compare_attribute_by_lDAPDisplayName);
|
---|
351 | TYPESAFE_QSORT(schema->attributes_by_attributeID_id, schema->num_attributes, dsdb_compare_attribute_by_attributeID_id);
|
---|
352 | TYPESAFE_QSORT(schema->attributes_by_msDS_IntId, schema->num_int_id_attr, dsdb_compare_attribute_by_msDS_IntId);
|
---|
353 | TYPESAFE_QSORT(schema->attributes_by_attributeID_oid, schema->num_attributes, dsdb_compare_attribute_by_attributeID_oid);
|
---|
354 | TYPESAFE_QSORT(schema->attributes_by_linkID, schema->num_attributes, dsdb_compare_attribute_by_linkID);
|
---|
355 |
|
---|
356 | return LDB_SUCCESS;
|
---|
357 |
|
---|
358 | failed:
|
---|
359 | dsdb_sorted_accessors_free(schema);
|
---|
360 | return ldb_oom(ldb);
|
---|
361 | }
|
---|
362 |
|
---|
363 | int dsdb_setup_schema_inversion(struct ldb_context *ldb, struct dsdb_schema *schema)
|
---|
364 | {
|
---|
365 | /* Walk the list of schema classes */
|
---|
366 |
|
---|
367 | /* For each subClassOf, add us to subclasses of the parent */
|
---|
368 |
|
---|
369 | /* collect these subclasses into a recursive list of total subclasses, preserving order */
|
---|
370 |
|
---|
371 | /* For each subclass under 'top', write the index from it's
|
---|
372 | * order as an integer in the dsdb_class (for sorting
|
---|
373 | * objectClass lists efficiently) */
|
---|
374 |
|
---|
375 | /* Walk the list of schema classes */
|
---|
376 |
|
---|
377 | /* Create a 'total possible superiors' on each class */
|
---|
378 | return LDB_SUCCESS;
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Attach the schema to an opaque pointer on the ldb,
|
---|
383 | * so ldb modules can find it
|
---|
384 | */
|
---|
385 | int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
|
---|
386 | {
|
---|
387 | struct dsdb_schema *old_schema;
|
---|
388 | int ret;
|
---|
389 |
|
---|
390 | ret = dsdb_setup_sorted_accessors(ldb, schema);
|
---|
391 | if (ret != LDB_SUCCESS) {
|
---|
392 | return ret;
|
---|
393 | }
|
---|
394 |
|
---|
395 | ret = schema_fill_constructed(schema);
|
---|
396 | if (ret != LDB_SUCCESS) {
|
---|
397 | return ret;
|
---|
398 | }
|
---|
399 |
|
---|
400 | old_schema = ldb_get_opaque(ldb, "dsdb_schema");
|
---|
401 |
|
---|
402 | ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
|
---|
403 | if (ret != LDB_SUCCESS) {
|
---|
404 | return ret;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /* Remove the reference to the schema we just overwrote - if there was
|
---|
408 | * none, NULL is harmless here */
|
---|
409 | if (old_schema != schema) {
|
---|
410 | talloc_unlink(ldb, old_schema);
|
---|
411 | talloc_steal(ldb, schema);
|
---|
412 | }
|
---|
413 |
|
---|
414 | ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
|
---|
415 | if (ret != LDB_SUCCESS) {
|
---|
416 | return ret;
|
---|
417 | }
|
---|
418 |
|
---|
419 | /* Set the new attributes based on the new schema */
|
---|
420 | ret = dsdb_schema_set_attributes(ldb, schema, true);
|
---|
421 | if (ret != LDB_SUCCESS) {
|
---|
422 | return ret;
|
---|
423 | }
|
---|
424 |
|
---|
425 | return LDB_SUCCESS;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Global variable to hold one copy of the schema, used to avoid memory bloat
|
---|
430 | */
|
---|
431 | static struct dsdb_schema *global_schema;
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Make this ldb use a specified schema, already fully calculated and belonging to another ldb
|
---|
435 | */
|
---|
436 | int dsdb_reference_schema(struct ldb_context *ldb, struct dsdb_schema *schema,
|
---|
437 | bool write_attributes)
|
---|
438 | {
|
---|
439 | int ret;
|
---|
440 | struct dsdb_schema *old_schema;
|
---|
441 | old_schema = ldb_get_opaque(ldb, "dsdb_schema");
|
---|
442 | ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
|
---|
443 | if (ret != LDB_SUCCESS) {
|
---|
444 | return ret;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /* Remove the reference to the schema we just overwrote - if there was
|
---|
448 | * none, NULL is harmless here */
|
---|
449 | talloc_unlink(ldb, old_schema);
|
---|
450 |
|
---|
451 | if (talloc_reference(ldb, schema) == NULL) {
|
---|
452 | return ldb_oom(ldb);
|
---|
453 | }
|
---|
454 |
|
---|
455 | /* Make this ldb use local schema preferably */
|
---|
456 | ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
|
---|
457 | if (ret != LDB_SUCCESS) {
|
---|
458 | return ret;
|
---|
459 | }
|
---|
460 |
|
---|
461 | ret = dsdb_schema_set_attributes(ldb, schema, write_attributes);
|
---|
462 | if (ret != LDB_SUCCESS) {
|
---|
463 | return ret;
|
---|
464 | }
|
---|
465 |
|
---|
466 | return LDB_SUCCESS;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
|
---|
471 | */
|
---|
472 | int dsdb_set_global_schema(struct ldb_context *ldb)
|
---|
473 | {
|
---|
474 | int ret;
|
---|
475 | void *use_global_schema = (void *)1;
|
---|
476 | if (!global_schema) {
|
---|
477 | return LDB_SUCCESS;
|
---|
478 | }
|
---|
479 | ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", use_global_schema);
|
---|
480 | if (ret != LDB_SUCCESS) {
|
---|
481 | return ret;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /* Set the new attributes based on the new schema */
|
---|
485 | ret = dsdb_schema_set_attributes(ldb, global_schema, false /* Don't write attributes, it's expensive */);
|
---|
486 | if (ret == LDB_SUCCESS) {
|
---|
487 | /* Keep a reference to this schema, just in case the original copy is replaced */
|
---|
488 | if (talloc_reference(ldb, global_schema) == NULL) {
|
---|
489 | return ldb_oom(ldb);
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | return ret;
|
---|
494 | }
|
---|
495 |
|
---|
496 | bool dsdb_uses_global_schema(struct ldb_context *ldb)
|
---|
497 | {
|
---|
498 | return (ldb_get_opaque(ldb, "dsdb_use_global_schema") != NULL);
|
---|
499 | }
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Find the schema object for this ldb
|
---|
503 | *
|
---|
504 | * If reference_ctx is not NULL, then talloc_reference onto that context
|
---|
505 | */
|
---|
506 |
|
---|
507 | struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb, TALLOC_CTX *reference_ctx)
|
---|
508 | {
|
---|
509 | const void *p;
|
---|
510 | struct dsdb_schema *schema_out;
|
---|
511 | struct dsdb_schema *schema_in;
|
---|
512 | bool use_global_schema;
|
---|
513 | TALLOC_CTX *tmp_ctx = talloc_new(reference_ctx);
|
---|
514 | if (!tmp_ctx) {
|
---|
515 | return NULL;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /* see if we have a cached copy */
|
---|
519 | use_global_schema = dsdb_uses_global_schema(ldb);
|
---|
520 | if (use_global_schema) {
|
---|
521 | schema_in = global_schema;
|
---|
522 | } else {
|
---|
523 | p = ldb_get_opaque(ldb, "dsdb_schema");
|
---|
524 |
|
---|
525 | schema_in = talloc_get_type(p, struct dsdb_schema);
|
---|
526 | if (!schema_in) {
|
---|
527 | talloc_free(tmp_ctx);
|
---|
528 | return NULL;
|
---|
529 | }
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (schema_in->refresh_fn && !schema_in->refresh_in_progress) {
|
---|
533 | if (!talloc_reference(tmp_ctx, schema_in)) {
|
---|
534 | /*
|
---|
535 | * ensure that the schema_in->refresh_in_progress
|
---|
536 | * remains valid for the right amount of time
|
---|
537 | */
|
---|
538 | talloc_free(tmp_ctx);
|
---|
539 | return NULL;
|
---|
540 | }
|
---|
541 | schema_in->refresh_in_progress = true;
|
---|
542 | /* This may change schema, if it needs to reload it from disk */
|
---|
543 | schema_out = schema_in->refresh_fn(schema_in->loaded_from_module,
|
---|
544 | schema_in,
|
---|
545 | use_global_schema);
|
---|
546 | schema_in->refresh_in_progress = false;
|
---|
547 | } else {
|
---|
548 | schema_out = schema_in;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* This removes the extra reference above */
|
---|
552 | talloc_free(tmp_ctx);
|
---|
553 | if (!reference_ctx) {
|
---|
554 | return schema_out;
|
---|
555 | } else {
|
---|
556 | return talloc_reference(reference_ctx, schema_out);
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Make the schema found on this ldb the 'global' schema
|
---|
562 | */
|
---|
563 |
|
---|
564 | void dsdb_make_schema_global(struct ldb_context *ldb, struct dsdb_schema *schema)
|
---|
565 | {
|
---|
566 | if (!schema) {
|
---|
567 | return;
|
---|
568 | }
|
---|
569 |
|
---|
570 | if (global_schema) {
|
---|
571 | talloc_unlink(talloc_autofree_context(), global_schema);
|
---|
572 | }
|
---|
573 |
|
---|
574 | /* we want the schema to be around permanently */
|
---|
575 | talloc_reparent(ldb, talloc_autofree_context(), schema);
|
---|
576 | global_schema = schema;
|
---|
577 |
|
---|
578 | /* This calls the talloc_reference() of the global schema back onto the ldb */
|
---|
579 | dsdb_set_global_schema(ldb);
|
---|
580 | }
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * When loading the schema from LDIF files, we don't get the extended DNs.
|
---|
584 | *
|
---|
585 | * We need to set these up, so that from the moment we start the provision,
|
---|
586 | * the defaultObjectCategory links are set up correctly.
|
---|
587 | */
|
---|
588 | int dsdb_schema_fill_extended_dn(struct ldb_context *ldb, struct dsdb_schema *schema)
|
---|
589 | {
|
---|
590 | struct dsdb_class *cur;
|
---|
591 | const struct dsdb_class *target_class;
|
---|
592 | for (cur = schema->classes; cur; cur = cur->next) {
|
---|
593 | const struct ldb_val *rdn;
|
---|
594 | struct ldb_val guid;
|
---|
595 | NTSTATUS status;
|
---|
596 | struct ldb_dn *dn = ldb_dn_new(NULL, ldb, cur->defaultObjectCategory);
|
---|
597 |
|
---|
598 | if (!dn) {
|
---|
599 | return LDB_ERR_INVALID_DN_SYNTAX;
|
---|
600 | }
|
---|
601 | rdn = ldb_dn_get_component_val(dn, 0);
|
---|
602 | if (!rdn) {
|
---|
603 | talloc_free(dn);
|
---|
604 | return LDB_ERR_INVALID_DN_SYNTAX;
|
---|
605 | }
|
---|
606 | target_class = dsdb_class_by_cn_ldb_val(schema, rdn);
|
---|
607 | if (!target_class) {
|
---|
608 | talloc_free(dn);
|
---|
609 | return LDB_ERR_CONSTRAINT_VIOLATION;
|
---|
610 | }
|
---|
611 |
|
---|
612 | status = GUID_to_ndr_blob(&target_class->objectGUID, dn, &guid);
|
---|
613 | if (!NT_STATUS_IS_OK(status)) {
|
---|
614 | talloc_free(dn);
|
---|
615 | return ldb_operr(ldb);
|
---|
616 | }
|
---|
617 | ldb_dn_set_extended_component(dn, "GUID", &guid);
|
---|
618 |
|
---|
619 | cur->defaultObjectCategory = ldb_dn_get_extended_linearized(cur, dn, 1);
|
---|
620 | talloc_free(dn);
|
---|
621 | }
|
---|
622 | return LDB_SUCCESS;
|
---|
623 | }
|
---|
624 |
|
---|
625 | /**
|
---|
626 | * Add an element to the schema (attribute or class) from an LDB message
|
---|
627 | */
|
---|
628 | WERROR dsdb_schema_set_el_from_ldb_msg(struct ldb_context *ldb, struct dsdb_schema *schema,
|
---|
629 | struct ldb_message *msg)
|
---|
630 | {
|
---|
631 | if (samdb_find_attribute(ldb, msg,
|
---|
632 | "objectclass", "attributeSchema") != NULL) {
|
---|
633 | return dsdb_attribute_from_ldb(ldb, schema, msg);
|
---|
634 | } else if (samdb_find_attribute(ldb, msg,
|
---|
635 | "objectclass", "classSchema") != NULL) {
|
---|
636 | return dsdb_class_from_ldb(schema, msg);
|
---|
637 | }
|
---|
638 |
|
---|
639 | /* Don't fail on things not classes or attributes */
|
---|
640 | return WERR_OK;
|
---|
641 | }
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * Rather than read a schema from the LDB itself, read it from an ldif
|
---|
645 | * file. This allows schema to be loaded and used while adding the
|
---|
646 | * schema itself to the directory.
|
---|
647 | */
|
---|
648 |
|
---|
649 | WERROR dsdb_set_schema_from_ldif(struct ldb_context *ldb, const char *pf, const char *df)
|
---|
650 | {
|
---|
651 | struct ldb_ldif *ldif;
|
---|
652 | struct ldb_message *msg;
|
---|
653 | TALLOC_CTX *mem_ctx;
|
---|
654 | WERROR status;
|
---|
655 | int ret;
|
---|
656 | struct dsdb_schema *schema;
|
---|
657 | const struct ldb_val *prefix_val;
|
---|
658 | const struct ldb_val *info_val;
|
---|
659 | struct ldb_val info_val_default;
|
---|
660 |
|
---|
661 |
|
---|
662 | mem_ctx = talloc_new(ldb);
|
---|
663 | if (!mem_ctx) {
|
---|
664 | goto nomem;
|
---|
665 | }
|
---|
666 |
|
---|
667 | schema = dsdb_new_schema(mem_ctx);
|
---|
668 |
|
---|
669 | schema->fsmo.we_are_master = true;
|
---|
670 | schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
|
---|
671 | if (!schema->fsmo.master_dn) {
|
---|
672 | goto nomem;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /*
|
---|
676 | * load the prefixMap attribute from pf
|
---|
677 | */
|
---|
678 | ldif = ldb_ldif_read_string(ldb, &pf);
|
---|
679 | if (!ldif) {
|
---|
680 | status = WERR_INVALID_PARAM;
|
---|
681 | goto failed;
|
---|
682 | }
|
---|
683 | talloc_steal(mem_ctx, ldif);
|
---|
684 |
|
---|
685 | ret = ldb_msg_normalize(ldb, mem_ctx, ldif->msg, &msg);
|
---|
686 | if (ret != LDB_SUCCESS) {
|
---|
687 | goto nomem;
|
---|
688 | }
|
---|
689 | talloc_free(ldif);
|
---|
690 |
|
---|
691 | prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
|
---|
692 | if (!prefix_val) {
|
---|
693 | status = WERR_INVALID_PARAM;
|
---|
694 | goto failed;
|
---|
695 | }
|
---|
696 |
|
---|
697 | info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
|
---|
698 | if (!info_val) {
|
---|
699 | status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
|
---|
700 | W_ERROR_NOT_OK_GOTO(status, failed);
|
---|
701 | info_val = &info_val_default;
|
---|
702 | }
|
---|
703 |
|
---|
704 | status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
|
---|
705 | if (!W_ERROR_IS_OK(status)) {
|
---|
706 | DEBUG(0,("ERROR: dsdb_load_oid_mappings_ldb() failed with %s\n", win_errstr(status)));
|
---|
707 | goto failed;
|
---|
708 | }
|
---|
709 |
|
---|
710 | /* load the attribute and class definitions out of df */
|
---|
711 | while ((ldif = ldb_ldif_read_string(ldb, &df))) {
|
---|
712 | talloc_steal(mem_ctx, ldif);
|
---|
713 |
|
---|
714 | ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &msg);
|
---|
715 | if (ret != LDB_SUCCESS) {
|
---|
716 | goto nomem;
|
---|
717 | }
|
---|
718 |
|
---|
719 | status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, msg);
|
---|
720 | talloc_free(ldif);
|
---|
721 | if (!W_ERROR_IS_OK(status)) {
|
---|
722 | goto failed;
|
---|
723 | }
|
---|
724 | }
|
---|
725 |
|
---|
726 | ret = dsdb_set_schema(ldb, schema);
|
---|
727 | if (ret != LDB_SUCCESS) {
|
---|
728 | status = WERR_FOOBAR;
|
---|
729 | goto failed;
|
---|
730 | }
|
---|
731 |
|
---|
732 | ret = dsdb_schema_fill_extended_dn(ldb, schema);
|
---|
733 | if (ret != LDB_SUCCESS) {
|
---|
734 | status = WERR_FOOBAR;
|
---|
735 | goto failed;
|
---|
736 | }
|
---|
737 |
|
---|
738 | goto done;
|
---|
739 |
|
---|
740 | nomem:
|
---|
741 | status = WERR_NOMEM;
|
---|
742 | failed:
|
---|
743 | done:
|
---|
744 | talloc_free(mem_ctx);
|
---|
745 | return status;
|
---|
746 | }
|
---|