Line data Source code
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 : Copyright (C) Matthieu Patou <mat@matws.net> 2011
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 :
22 : */
23 :
24 : #include "includes.h"
25 : #include "lib/util/dlinklist.h"
26 : #include "dsdb/samdb/samdb.h"
27 : #include <ldb_module.h>
28 : #include "param/param.h"
29 : #include "librpc/ndr/libndr.h"
30 : #include "librpc/gen_ndr/ndr_misc.h"
31 : #include "lib/util/tsort.h"
32 :
33 : #undef strcasecmp
34 :
35 : /* change this when we change something in our schema code that
36 : * requires a re-index of the database
37 : */
38 : #define SAMDB_INDEXING_VERSION "3"
39 :
40 : /*
41 : override the name to attribute handler function
42 : */
43 924494212 : const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb,
44 : void *private_data,
45 : const char *name)
46 : {
47 924494212 : struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
48 924494212 : const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
49 924494212 : if (a == NULL) {
50 : /* this will fall back to ldb internal handling */
51 3218671 : return NULL;
52 : }
53 921067380 : return a->ldb_schema_attribute;
54 : }
55 :
56 : /*
57 : * Set the attribute handlers onto the LDB, and potentially write the
58 : * @INDEXLIST, @IDXONE and @ATTRIBUTES records. The @ATTRIBUTES records
59 : * are required so we can operate on a schema-less database (say the
60 : * backend during emergency fixes) and during the schema load.
61 : */
62 239862 : int dsdb_schema_set_indices_and_attributes(struct ldb_context *ldb,
63 : struct dsdb_schema *schema,
64 : enum schema_set_enum mode)
65 : {
66 239862 : int ret = LDB_SUCCESS;
67 6548 : struct ldb_result *res;
68 6548 : struct ldb_result *res_idx;
69 6548 : struct dsdb_attribute *attr;
70 6548 : struct ldb_message *mod_msg;
71 6548 : TALLOC_CTX *mem_ctx;
72 6548 : struct ldb_message *msg;
73 6548 : struct ldb_message *msg_idx;
74 :
75 6548 : struct loadparm_context *lp_ctx =
76 239862 : talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
77 : struct loadparm_context);
78 239862 : bool guid_indexing = true;
79 239862 : bool declare_ordered_integer_in_attributes = true;
80 6548 : uint32_t pack_format_override;
81 239862 : if (lp_ctx != NULL) {
82 : /*
83 : * GUID indexing is wanted by Samba by default. This allows
84 : * an override in a specific case for downgrades.
85 : */
86 239474 : guid_indexing = lpcfg_parm_bool(lp_ctx,
87 : NULL,
88 : "dsdb",
89 : "guid index",
90 : true);
91 : /*
92 : * If the pack format has been overridden to a previous
93 : * version, then act like ORDERED_INTEGER doesn't exist,
94 : * because it's a new type and we don't want to deal with
95 : * possible issues with databases containing version 1 pack
96 : * format and ordered types.
97 : *
98 : * This approach means that the @ATTRIBUTES will be
99 : * incorrect for integers. Many other @ATTRIBUTES
100 : * values are gross simplifications, but the presence
101 : * of the ORDERED_INTEGER keyword prevents the old
102 : * Samba from starting and then forcing a reindex.
103 : *
104 : * It is too difficult to override the actual index
105 : * formatter, but this doesn't matter in practice.
106 : */
107 245997 : pack_format_override =
108 239474 : (intptr_t)ldb_get_opaque(ldb, "pack_format_override");
109 239474 : if (pack_format_override == LDB_PACKING_FORMAT ||
110 : pack_format_override == LDB_PACKING_FORMAT_NODN) {
111 5 : declare_ordered_integer_in_attributes = false;
112 : }
113 : }
114 : /* setup our own attribute name to schema handler */
115 239862 : ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
116 239862 : ldb_schema_set_override_indexlist(ldb, true);
117 239862 : if (guid_indexing) {
118 239857 : ldb_schema_set_override_GUID_index(ldb, "objectGUID", "GUID");
119 : }
120 :
121 239862 : if (mode == SCHEMA_MEMORY_ONLY) {
122 224124 : return ret;
123 : }
124 :
125 9467 : mem_ctx = talloc_new(ldb);
126 9467 : if (!mem_ctx) {
127 0 : return ldb_oom(ldb);
128 : }
129 :
130 9467 : msg = ldb_msg_new(mem_ctx);
131 9467 : if (!msg) {
132 0 : ldb_oom(ldb);
133 0 : goto op_error;
134 : }
135 9467 : msg_idx = ldb_msg_new(mem_ctx);
136 9467 : if (!msg_idx) {
137 0 : ldb_oom(ldb);
138 0 : goto op_error;
139 : }
140 9467 : msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
141 9467 : if (!msg->dn) {
142 0 : ldb_oom(ldb);
143 0 : goto op_error;
144 : }
145 9467 : msg_idx->dn = ldb_dn_new(msg_idx, ldb, "@INDEXLIST");
146 9467 : if (!msg_idx->dn) {
147 0 : ldb_oom(ldb);
148 0 : goto op_error;
149 : }
150 :
151 9467 : ret = ldb_msg_add_string(msg_idx, "@IDXONE", "1");
152 9467 : if (ret != LDB_SUCCESS) {
153 0 : goto op_error;
154 : }
155 :
156 9467 : if (guid_indexing) {
157 9465 : ret = ldb_msg_add_string(msg_idx, "@IDXGUID", "objectGUID");
158 9465 : if (ret != LDB_SUCCESS) {
159 0 : goto op_error;
160 : }
161 :
162 9465 : ret = ldb_msg_add_string(msg_idx, "@IDX_DN_GUID", "GUID");
163 9465 : if (ret != LDB_SUCCESS) {
164 0 : goto op_error;
165 : }
166 : }
167 :
168 9467 : ret = ldb_msg_add_string(msg_idx, "@SAMDB_INDEXING_VERSION", SAMDB_INDEXING_VERSION);
169 9467 : if (ret != LDB_SUCCESS) {
170 0 : goto op_error;
171 : }
172 :
173 9467 : ret = ldb_msg_add_string(msg_idx, SAMBA_FEATURES_SUPPORTED_FLAG, "1");
174 9467 : if (ret != LDB_SUCCESS) {
175 0 : goto op_error;
176 : }
177 :
178 13782544 : for (attr = schema->attributes; attr; attr = attr->next) {
179 13773077 : const char *syntax = attr->syntax->ldb_syntax;
180 :
181 13773077 : if (!syntax) {
182 7725633 : syntax = attr->syntax->ldap_oid;
183 : }
184 :
185 : /*
186 : * Write out a rough approximation of the schema
187 : * as an @ATTRIBUTES value, for bootstrapping.
188 : * Only write ORDERED_INTEGER if we're using GUID indexes,
189 : */
190 13773077 : if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
191 0 : ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
192 13773077 : } else if (strcmp(syntax, LDB_SYNTAX_ORDERED_INTEGER) == 0) {
193 892876 : if (declare_ordered_integer_in_attributes &&
194 : guid_indexing) {
195 : /*
196 : * The normal production case
197 : */
198 892488 : ret = ldb_msg_add_string(msg,
199 : attr->lDAPDisplayName,
200 : "ORDERED_INTEGER");
201 : } else {
202 : /*
203 : * For this mode, we are going back to
204 : * before GUID indexing so we write it out
205 : * as INTEGER
206 : *
207 : * Down in LDB, the special handler
208 : * (index_format_fn) that made
209 : * ORDERED_INTEGER and INTEGER
210 : * different has been disabled.
211 : */
212 388 : ret = ldb_msg_add_string(msg,
213 : attr->lDAPDisplayName,
214 : "INTEGER");
215 : }
216 12880201 : } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
217 5041441 : ret = ldb_msg_add_string(msg, attr->lDAPDisplayName,
218 : "CASE_INSENSITIVE");
219 : }
220 13773077 : if (ret != LDB_SUCCESS) {
221 0 : break;
222 : }
223 :
224 : /*
225 : * Is the attribute indexed? By treating confidential attributes
226 : * as unindexed, we force searches to go through the unindexed
227 : * search path, avoiding observable timing differences.
228 : */
229 13773077 : if (attr->searchFlags & SEARCH_FLAG_ATTINDEX &&
230 1356811 : !(attr->searchFlags & SEARCH_FLAG_CONFIDENTIAL))
231 : {
232 : /*
233 : * When preparing to downgrade Samba, we need to write
234 : * out an LDB without the new key word ORDERED_INTEGER.
235 : */
236 1396783 : if (strcmp(syntax, LDB_SYNTAX_ORDERED_INTEGER) == 0
237 82973 : && !declare_ordered_integer_in_attributes) {
238 : /*
239 : * Ugly, but do nothing, the best
240 : * thing is to omit the reference
241 : * entirely, the next transaction will
242 : * spot this and rewrite everything.
243 : *
244 : * This way nothing will look at the
245 : * index for this attribute until
246 : * Samba starts and this is all
247 : * rewritten.
248 : */
249 : } else {
250 1396763 : ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
251 1396763 : if (ret != LDB_SUCCESS) {
252 0 : break;
253 : }
254 : }
255 : }
256 : }
257 :
258 9467 : if (ret != LDB_SUCCESS) {
259 0 : talloc_free(mem_ctx);
260 0 : return ret;
261 : }
262 :
263 : /*
264 : * Try to avoid churning the attributes too much,
265 : * we only want to do this if they have changed
266 : */
267 9467 : ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL,
268 : NULL);
269 9467 : if (ret == LDB_ERR_NO_SUCH_OBJECT) {
270 205 : if (mode == SCHEMA_COMPARE) {
271 : /* We are probably not in a transaction */
272 0 : goto wrong_mode;
273 : }
274 205 : ret = ldb_add(ldb, msg);
275 9262 : } else if (ret != LDB_SUCCESS) {
276 8775 : } else if (res->count != 1) {
277 6 : if (mode == SCHEMA_COMPARE) {
278 : /* We are probably not in a transaction */
279 0 : goto wrong_mode;
280 : }
281 6 : ret = ldb_add(ldb, msg);
282 : } else {
283 : /* Annoyingly added to our search results */
284 8769 : ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
285 :
286 8998 : ret = ldb_msg_difference(ldb, mem_ctx,
287 8769 : res->msgs[0], msg, &mod_msg);
288 8769 : if (ret != LDB_SUCCESS) {
289 0 : goto op_error;
290 : }
291 8769 : if (mod_msg->num_elements > 0) {
292 : /*
293 : * Do the replace with the difference, as we
294 : * are under the read lock and we wish to do a
295 : * delete of any removed/renamed attributes
296 : */
297 208 : if (mode == SCHEMA_COMPARE) {
298 : /* We are probably not in a transaction */
299 35 : goto wrong_mode;
300 : }
301 173 : ret = dsdb_modify(ldb, mod_msg, 0);
302 : }
303 8734 : talloc_free(mod_msg);
304 : }
305 :
306 9432 : if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
307 : /* We might be on a read-only DB or LDAP */
308 462 : ret = LDB_SUCCESS;
309 : }
310 9407 : if (ret != LDB_SUCCESS) {
311 0 : DBG_ERR("Failed to set schema into @ATTRIBUTES: %s\n",
312 : ldb_errstring(ldb));
313 0 : talloc_free(mem_ctx);
314 0 : return ret;
315 : }
316 :
317 : /* Now write out the indexes, as found in the schema (if they have changed) */
318 :
319 9432 : ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE,
320 : NULL, NULL);
321 9432 : if (ret == LDB_ERR_NO_SUCH_OBJECT) {
322 205 : if (mode == SCHEMA_COMPARE) {
323 : /* We are probably not in a transaction */
324 0 : goto wrong_mode;
325 : }
326 205 : ret = ldb_add(ldb, msg_idx);
327 9227 : } else if (ret != LDB_SUCCESS) {
328 8740 : } else if (res_idx->count != 1) {
329 6 : if (mode == SCHEMA_COMPARE) {
330 : /* We are probably not in a transaction */
331 0 : goto wrong_mode;
332 : }
333 6 : ret = ldb_add(ldb, msg_idx);
334 : } else {
335 : /* Annoyingly added to our search results */
336 8734 : ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
337 :
338 8952 : ret = ldb_msg_difference(ldb, mem_ctx,
339 8734 : res_idx->msgs[0], msg_idx, &mod_msg);
340 8734 : if (ret != LDB_SUCCESS) {
341 0 : goto op_error;
342 : }
343 :
344 : /*
345 : * We don't want to re-index just because we didn't
346 : * see this flag
347 : *
348 : * DO NOT backport this logic earlier than 4.7, it
349 : * isn't needed and would be dangerous before 4.6,
350 : * where we add logic to samba_dsdb to manage
351 : * @SAMBA_FEATURES_SUPPORTED and need to know if the
352 : * DB has been re-opened by an earlier version.
353 : *
354 : */
355 :
356 8734 : if (mod_msg->num_elements == 1
357 86 : && ldb_attr_cmp(mod_msg->elements[0].name,
358 : SAMBA_FEATURES_SUPPORTED_FLAG) == 0) {
359 : /*
360 : * Ignore only adding
361 : * @SAMBA_FEATURES_SUPPORTED
362 : */
363 8734 : } else if (mod_msg->num_elements > 0) {
364 :
365 : /*
366 : * Do the replace with the difference, as we
367 : * are under the read lock and we wish to do a
368 : * delete of any removed/renamed attributes
369 : */
370 106 : if (mode == SCHEMA_COMPARE) {
371 : /* We are probably not in a transaction */
372 2 : goto wrong_mode;
373 : }
374 104 : ret = dsdb_modify(ldb, mod_msg, 0);
375 : }
376 8732 : talloc_free(mod_msg);
377 : }
378 9430 : if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
379 : /* We might be on a read-only DB */
380 462 : ret = LDB_SUCCESS;
381 : }
382 :
383 9405 : if (ret != LDB_SUCCESS) {
384 0 : DBG_ERR("Failed to set schema into @INDEXLIST: %s\n",
385 : ldb_errstring(ldb));
386 : }
387 :
388 9430 : talloc_free(mem_ctx);
389 9430 : return ret;
390 :
391 0 : op_error:
392 0 : talloc_free(mem_ctx);
393 0 : return ldb_operr(ldb);
394 :
395 37 : wrong_mode:
396 37 : talloc_free(mem_ctx);
397 37 : return LDB_ERR_BUSY;
398 : }
399 :
400 :
401 : /*
402 : create extra attribute shortcuts
403 : */
404 28488 : static void dsdb_setup_attribute_shortcuts(struct ldb_context *ldb, struct dsdb_schema *schema)
405 : {
406 341 : struct dsdb_attribute *attribute;
407 28488 : const struct dsdb_class *top_class = NULL;
408 28488 : TALLOC_CTX *frame = talloc_stackframe();
409 28488 : const char **top_allowed_attrs = NULL;
410 :
411 28488 : top_class = dsdb_class_by_lDAPDisplayName(schema, "top");
412 28488 : if (top_class != NULL) {
413 28488 : top_allowed_attrs = dsdb_attribute_list(frame,
414 : top_class,
415 : DSDB_SCHEMA_ALL);
416 : }
417 :
418 : /* setup fast access to one_way_link and DN format */
419 39841113 : for (attribute=schema->attributes; attribute; attribute=attribute->next) {
420 39812625 : attribute->dn_format = dsdb_dn_oid_to_format(attribute->syntax->ldap_oid);
421 :
422 39812625 : attribute->bl_maybe_invisible = false;
423 :
424 39812625 : if (attribute->dn_format == DSDB_INVALID_DN) {
425 34428561 : attribute->one_way_link = false;
426 34428561 : continue;
427 : }
428 :
429 : /* these are not considered to be one way links for
430 : the purpose of DN link fixups */
431 5384064 : if (ldb_attr_cmp("distinguishedName", attribute->lDAPDisplayName) == 0 ||
432 5355576 : ldb_attr_cmp("objectCategory", attribute->lDAPDisplayName) == 0) {
433 56976 : attribute->one_way_link = false;
434 56976 : continue;
435 : }
436 :
437 5327088 : if (attribute->linkID == 0) {
438 2135460 : attribute->one_way_link = true;
439 2135460 : continue;
440 : }
441 :
442 3191628 : if (attribute->linkID & 1) {
443 1344795 : const struct dsdb_attribute *fw_attr = NULL;
444 1344795 : bool in_top = false;
445 :
446 1344795 : if (top_allowed_attrs != NULL) {
447 1344795 : in_top = str_list_check(top_allowed_attrs,
448 : attribute->lDAPDisplayName);
449 : }
450 :
451 1344795 : if (in_top) {
452 1150094 : continue;
453 : }
454 :
455 194701 : attribute->bl_maybe_invisible = true;
456 :
457 197599 : fw_attr = dsdb_attribute_by_linkID(schema,
458 194701 : attribute->linkID - 1);
459 194701 : if (fw_attr != NULL) {
460 194701 : struct dsdb_attribute *_fw_attr =
461 : discard_const_p(struct dsdb_attribute,
462 : fw_attr);
463 194701 : _fw_attr->bl_maybe_invisible = true;
464 : }
465 :
466 194701 : continue;
467 : }
468 :
469 : /* handle attributes with a linkID but no backlink */
470 3670458 : if ((attribute->linkID & 1) == 0 &&
471 1846833 : dsdb_attribute_by_linkID(schema, attribute->linkID + 1) == NULL) {
472 502038 : attribute->one_way_link = true;
473 502038 : continue;
474 : }
475 1344795 : attribute->one_way_link = false;
476 : }
477 :
478 28488 : TALLOC_FREE(frame);
479 28488 : }
480 :
481 750846960 : static int uint32_cmp(uint32_t c1, uint32_t c2)
482 : {
483 750846960 : if (c1 == c2) return 0;
484 567377918 : return c1 > c2 ? 1 : -1;
485 : }
486 :
487 50875519 : static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
488 : {
489 50875519 : return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
490 : }
491 50873013 : static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
492 : {
493 50873013 : return uint32_cmp((*c1)->governsID_id, (*c2)->governsID_id);
494 : }
495 51015748 : static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
496 : {
497 51015748 : return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
498 : }
499 50831556 : static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
500 : {
501 50831556 : return strcasecmp((*c1)->cn, (*c2)->cn);
502 : }
503 :
504 365031406 : static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
505 : {
506 365031406 : return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
507 : }
508 366420124 : static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
509 : {
510 366420124 : return uint32_cmp((*a1)->attributeID_id, (*a2)->attributeID_id);
511 : }
512 724148 : static int dsdb_compare_attribute_by_msDS_IntId(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
513 : {
514 724148 : return uint32_cmp((*a1)->msDS_IntId, (*a2)->msDS_IntId);
515 : }
516 366424999 : static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
517 : {
518 366424999 : return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
519 : }
520 332829675 : static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
521 : {
522 332829675 : return uint32_cmp((*a1)->linkID, (*a2)->linkID);
523 : }
524 363104435 : static int dsdb_compare_attribute_by_cn(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
525 : {
526 363104435 : return strcasecmp((*a1)->cn, (*a2)->cn);
527 : }
528 :
529 : /**
530 : * Clean up Classes and Attributes accessor arrays
531 : */
532 28488 : static void dsdb_sorted_accessors_free(struct dsdb_schema *schema)
533 : {
534 : /* free classes accessors */
535 28488 : TALLOC_FREE(schema->classes_by_lDAPDisplayName);
536 28488 : TALLOC_FREE(schema->classes_by_governsID_id);
537 28488 : TALLOC_FREE(schema->classes_by_governsID_oid);
538 28488 : TALLOC_FREE(schema->classes_by_cn);
539 : /* free attribute accessors */
540 28488 : TALLOC_FREE(schema->attributes_by_lDAPDisplayName);
541 28488 : TALLOC_FREE(schema->attributes_by_attributeID_id);
542 28488 : TALLOC_FREE(schema->attributes_by_msDS_IntId);
543 28488 : TALLOC_FREE(schema->attributes_by_attributeID_oid);
544 28488 : TALLOC_FREE(schema->attributes_by_linkID);
545 28488 : TALLOC_FREE(schema->attributes_by_cn);
546 28488 : }
547 :
548 : /*
549 : create the sorted accessor arrays for the schema
550 : */
551 28488 : int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
552 : struct dsdb_schema *schema)
553 : {
554 341 : struct dsdb_class *cur;
555 341 : struct dsdb_attribute *a;
556 341 : unsigned int i;
557 341 : unsigned int num_int_id;
558 341 : int ret;
559 :
560 28488 : for (i=0; i < schema->classes_to_remove_size; i++) {
561 0 : DLIST_REMOVE(schema->classes, schema->classes_to_remove[i]);
562 0 : TALLOC_FREE(schema->classes_to_remove[i]);
563 : }
564 28488 : for (i=0; i < schema->attributes_to_remove_size; i++) {
565 0 : DLIST_REMOVE(schema->attributes, schema->attributes_to_remove[i]);
566 0 : TALLOC_FREE(schema->attributes_to_remove[i]);
567 : }
568 :
569 28488 : TALLOC_FREE(schema->classes_to_remove);
570 28488 : schema->classes_to_remove_size = 0;
571 28488 : TALLOC_FREE(schema->attributes_to_remove);
572 28488 : schema->attributes_to_remove_size = 0;
573 :
574 : /* free all caches */
575 28488 : dsdb_sorted_accessors_free(schema);
576 :
577 : /* count the classes */
578 7538439 : for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
579 28488 : schema->num_classes = i;
580 :
581 : /* setup classes_by_* */
582 28488 : schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
583 28488 : schema->classes_by_governsID_id = talloc_array(schema, struct dsdb_class *, i);
584 28488 : schema->classes_by_governsID_oid = talloc_array(schema, struct dsdb_class *, i);
585 28488 : schema->classes_by_cn = talloc_array(schema, struct dsdb_class *, i);
586 28488 : if (schema->classes_by_lDAPDisplayName == NULL ||
587 28488 : schema->classes_by_governsID_id == NULL ||
588 28488 : schema->classes_by_governsID_oid == NULL ||
589 28147 : schema->classes_by_cn == NULL) {
590 0 : goto failed;
591 : }
592 :
593 7538439 : for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
594 7509951 : schema->classes_by_lDAPDisplayName[i] = cur;
595 7509951 : schema->classes_by_governsID_id[i] = cur;
596 7509951 : schema->classes_by_governsID_oid[i] = cur;
597 7509951 : schema->classes_by_cn[i] = cur;
598 : }
599 :
600 : /* sort the arrays */
601 28488 : TYPESAFE_QSORT(schema->classes_by_lDAPDisplayName, schema->num_classes, dsdb_compare_class_by_lDAPDisplayName);
602 28488 : TYPESAFE_QSORT(schema->classes_by_governsID_id, schema->num_classes, dsdb_compare_class_by_governsID_id);
603 28488 : TYPESAFE_QSORT(schema->classes_by_governsID_oid, schema->num_classes, dsdb_compare_class_by_governsID_oid);
604 28488 : TYPESAFE_QSORT(schema->classes_by_cn, schema->num_classes, dsdb_compare_class_by_cn);
605 :
606 : /* now build the attribute accessor arrays */
607 :
608 : /* count the attributes
609 : * and attributes with msDS-IntId set */
610 28488 : num_int_id = 0;
611 39841113 : for (i=0, a=schema->attributes; a; i++, a=a->next) {
612 39812625 : if (a->msDS_IntId != 0) {
613 230063 : num_int_id++;
614 : }
615 : }
616 28488 : schema->num_attributes = i;
617 28488 : schema->num_int_id_attr = num_int_id;
618 :
619 : /* setup attributes_by_* */
620 28488 : schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
621 28488 : schema->attributes_by_attributeID_id = talloc_array(schema, struct dsdb_attribute *, i);
622 28488 : schema->attributes_by_msDS_IntId = talloc_array(schema,
623 : struct dsdb_attribute *, num_int_id);
624 28488 : schema->attributes_by_attributeID_oid = talloc_array(schema, struct dsdb_attribute *, i);
625 28488 : schema->attributes_by_linkID = talloc_array(schema, struct dsdb_attribute *, i);
626 28488 : schema->attributes_by_cn = talloc_array(schema, struct dsdb_attribute *, i);
627 28488 : if (schema->attributes_by_lDAPDisplayName == NULL ||
628 28488 : schema->attributes_by_attributeID_id == NULL ||
629 28488 : schema->attributes_by_msDS_IntId == NULL ||
630 28488 : schema->attributes_by_attributeID_oid == NULL ||
631 28488 : schema->attributes_by_linkID == NULL ||
632 28147 : schema->attributes_by_cn == NULL) {
633 0 : goto failed;
634 : }
635 :
636 28488 : num_int_id = 0;
637 39841113 : for (i=0, a=schema->attributes; a; i++, a=a->next) {
638 39812625 : schema->attributes_by_lDAPDisplayName[i] = a;
639 39812625 : schema->attributes_by_attributeID_id[i] = a;
640 39812625 : schema->attributes_by_attributeID_oid[i] = a;
641 39812625 : schema->attributes_by_linkID[i] = a;
642 39812625 : schema->attributes_by_cn[i] = a;
643 : /* append attr-by-msDS-IntId values */
644 39812625 : if (a->msDS_IntId != 0) {
645 230063 : schema->attributes_by_msDS_IntId[num_int_id] = a;
646 230063 : num_int_id++;
647 : }
648 : }
649 28488 : SMB_ASSERT(num_int_id == schema->num_int_id_attr);
650 :
651 : /* sort the arrays */
652 28488 : TYPESAFE_QSORT(schema->attributes_by_lDAPDisplayName, schema->num_attributes, dsdb_compare_attribute_by_lDAPDisplayName);
653 28488 : TYPESAFE_QSORT(schema->attributes_by_attributeID_id, schema->num_attributes, dsdb_compare_attribute_by_attributeID_id);
654 28488 : TYPESAFE_QSORT(schema->attributes_by_msDS_IntId, schema->num_int_id_attr, dsdb_compare_attribute_by_msDS_IntId);
655 28488 : TYPESAFE_QSORT(schema->attributes_by_attributeID_oid, schema->num_attributes, dsdb_compare_attribute_by_attributeID_oid);
656 28488 : TYPESAFE_QSORT(schema->attributes_by_linkID, schema->num_attributes, dsdb_compare_attribute_by_linkID);
657 28488 : TYPESAFE_QSORT(schema->attributes_by_cn, schema->num_attributes, dsdb_compare_attribute_by_cn);
658 :
659 28488 : dsdb_setup_attribute_shortcuts(ldb, schema);
660 :
661 28488 : ret = schema_fill_constructed(schema);
662 28488 : if (ret != LDB_SUCCESS) {
663 0 : dsdb_sorted_accessors_free(schema);
664 0 : return ret;
665 : }
666 :
667 28147 : return LDB_SUCCESS;
668 :
669 0 : failed:
670 0 : dsdb_sorted_accessors_free(schema);
671 0 : return ldb_oom(ldb);
672 : }
673 :
674 : /**
675 : * Attach the schema to an opaque pointer on the ldb,
676 : * so ldb modules can find it
677 : */
678 181799 : int dsdb_set_schema_refresh_function(struct ldb_context *ldb,
679 : dsdb_schema_refresh_fn refresh_fn,
680 : struct ldb_module *module)
681 : {
682 181799 : int ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", refresh_fn);
683 181799 : if (ret != LDB_SUCCESS) {
684 0 : return ret;
685 : }
686 :
687 181799 : ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn_private_data", module);
688 181799 : if (ret != LDB_SUCCESS) {
689 0 : return ret;
690 : }
691 175806 : return LDB_SUCCESS;
692 : }
693 :
694 : /**
695 : * Attach the schema to an opaque pointer on the ldb,
696 : * so ldb modules can find it
697 : */
698 28131 : int dsdb_set_schema(struct ldb_context *ldb,
699 : struct dsdb_schema *schema,
700 : enum schema_set_enum write_indices_and_attributes)
701 : {
702 341 : struct dsdb_schema *old_schema;
703 341 : int ret;
704 :
705 28131 : ret = dsdb_setup_sorted_accessors(ldb, schema);
706 28131 : if (ret != LDB_SUCCESS) {
707 0 : return ret;
708 : }
709 :
710 28131 : old_schema = ldb_get_opaque(ldb, "dsdb_schema");
711 :
712 28131 : ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
713 28131 : if (ret != LDB_SUCCESS) {
714 0 : return ret;
715 : }
716 :
717 28131 : ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
718 28131 : if (ret != LDB_SUCCESS) {
719 0 : return ret;
720 : }
721 :
722 28131 : talloc_steal(ldb, schema);
723 :
724 : /* Set the new attributes based on the new schema */
725 28131 : ret = dsdb_schema_set_indices_and_attributes(ldb, schema,
726 : write_indices_and_attributes);
727 28131 : if (ret != LDB_SUCCESS) {
728 0 : return ret;
729 : }
730 :
731 : /*
732 : * Remove the reference to the schema we just overwrote - if there was
733 : * none, NULL is harmless here.
734 : */
735 28131 : if (old_schema != schema) {
736 28130 : talloc_unlink(ldb, old_schema);
737 : }
738 :
739 27790 : return ret;
740 : }
741 :
742 : /**
743 : * Global variable to hold one copy of the schema, used to avoid memory bloat
744 : */
745 : static struct dsdb_schema *global_schema;
746 :
747 : /**
748 : * Make this ldb use a specified schema, already fully calculated and belonging to another ldb
749 : *
750 : * The write_indices_and_attributes controls writing of the @ records
751 : * because we cannot write to a database that does not yet exist on
752 : * disk.
753 : */
754 646 : int dsdb_reference_schema(struct ldb_context *ldb, struct dsdb_schema *schema,
755 : enum schema_set_enum write_indices_and_attributes)
756 : {
757 46 : int ret;
758 46 : void *ptr;
759 646 : void *schema_parent = NULL;
760 46 : bool is_already_parent;
761 46 : struct dsdb_schema *old_schema;
762 646 : old_schema = ldb_get_opaque(ldb, "dsdb_schema");
763 646 : ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
764 646 : if (ret != LDB_SUCCESS) {
765 0 : return ret;
766 : }
767 :
768 : /* Remove the reference to the schema we just overwrote - if there was
769 : * none, NULL is harmless here */
770 646 : talloc_unlink(ldb, old_schema);
771 :
772 : /* Reference schema on ldb if it wasn't done already */
773 646 : schema_parent = talloc_parent(schema);
774 646 : is_already_parent = (schema_parent == ldb);
775 646 : if (!is_already_parent) {
776 646 : ptr = talloc_reference(ldb, schema);
777 646 : if (ptr == NULL) {
778 0 : return ldb_oom(ldb);
779 : }
780 : }
781 :
782 : /* Make this ldb use local schema preferably */
783 646 : ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
784 646 : if (ret != LDB_SUCCESS) {
785 0 : return ret;
786 : }
787 :
788 646 : ret = ldb_set_opaque(ldb, "dsdb_refresh_fn", NULL);
789 646 : if (ret != LDB_SUCCESS) {
790 0 : return ret;
791 : }
792 :
793 646 : ret = ldb_set_opaque(ldb, "dsdb_refresh_fn_private_data", NULL);
794 646 : if (ret != LDB_SUCCESS) {
795 0 : return ret;
796 : }
797 :
798 646 : ret = dsdb_schema_set_indices_and_attributes(ldb, schema, write_indices_and_attributes);
799 646 : if (ret != LDB_SUCCESS) {
800 0 : return ret;
801 : }
802 :
803 600 : return LDB_SUCCESS;
804 : }
805 :
806 : /**
807 : * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
808 : */
809 226342 : int dsdb_set_global_schema(struct ldb_context *ldb)
810 : {
811 6074 : int ret;
812 226342 : void *use_global_schema = (void *)1;
813 6074 : void *ptr;
814 226342 : struct dsdb_schema *old_schema = ldb_get_opaque(ldb, "dsdb_schema");
815 :
816 226342 : ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", use_global_schema);
817 226342 : if (ret != LDB_SUCCESS) {
818 0 : return ret;
819 : }
820 :
821 226342 : if (global_schema == NULL) {
822 23802 : return LDB_SUCCESS;
823 : }
824 :
825 : /* Remove any pointer to a previous schema */
826 202398 : ret = ldb_set_opaque(ldb, "dsdb_schema", NULL);
827 202398 : if (ret != LDB_SUCCESS) {
828 0 : return ret;
829 : }
830 :
831 : /* Remove the reference to the schema we just overwrote - if there was
832 : * none, NULL is harmless here */
833 202398 : talloc_unlink(ldb, old_schema);
834 :
835 : /* Set the new attributes based on the new schema */
836 : /* Don't write indices and attributes, it's expensive */
837 202398 : ret = dsdb_schema_set_indices_and_attributes(ldb, global_schema, SCHEMA_MEMORY_ONLY);
838 202398 : if (ret == LDB_SUCCESS) {
839 202398 : void *schema_parent = talloc_parent(global_schema);
840 202398 : bool is_already_parent =
841 : (schema_parent == ldb);
842 202398 : if (!is_already_parent) {
843 202398 : ptr = talloc_reference(ldb, global_schema);
844 202398 : if (ptr == NULL) {
845 0 : return ldb_oom(ldb);
846 : }
847 202398 : ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
848 : }
849 : }
850 :
851 196466 : return ret;
852 : }
853 :
854 196657945 : bool dsdb_uses_global_schema(struct ldb_context *ldb)
855 : {
856 196657945 : return (ldb_get_opaque(ldb, "dsdb_use_global_schema") != NULL);
857 : }
858 :
859 : /**
860 : * Find the schema object for this ldb
861 : *
862 : * If reference_ctx is not NULL, then talloc_reference onto that context
863 : */
864 :
865 196483772 : struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb, TALLOC_CTX *reference_ctx)
866 : {
867 14854638 : const void *p;
868 196483772 : struct dsdb_schema *schema_out = NULL;
869 196483772 : struct dsdb_schema *schema_in = NULL;
870 14854638 : dsdb_schema_refresh_fn refresh_fn;
871 14854638 : struct ldb_module *loaded_from_module;
872 14854638 : bool use_global_schema;
873 196483772 : TALLOC_CTX *tmp_ctx = talloc_new(reference_ctx);
874 196483772 : if (tmp_ctx == NULL) {
875 0 : return NULL;
876 : }
877 :
878 : /* see if we have a cached copy */
879 196483772 : use_global_schema = dsdb_uses_global_schema(ldb);
880 196483772 : if (use_global_schema) {
881 163765476 : schema_in = global_schema;
882 : } else {
883 32718296 : p = ldb_get_opaque(ldb, "dsdb_schema");
884 32718296 : if (p != NULL) {
885 32537287 : schema_in = talloc_get_type_abort(p, struct dsdb_schema);
886 : }
887 : }
888 :
889 196483772 : refresh_fn = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn");
890 196483772 : if (refresh_fn) {
891 164100270 : loaded_from_module = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn_private_data");
892 :
893 164100270 : SMB_ASSERT(loaded_from_module && (ldb_module_get_ctx(loaded_from_module) == ldb));
894 : }
895 :
896 196483772 : if (refresh_fn) {
897 : /* We need to guard against recursive calls here */
898 164100270 : if (ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", NULL) != LDB_SUCCESS) {
899 0 : ldb_debug_set(ldb, LDB_DEBUG_FATAL,
900 : "dsdb_get_schema: clearing dsdb_schema_refresh_fn failed");
901 : } else {
902 164100270 : schema_out = refresh_fn(loaded_from_module,
903 : ldb_get_event_context(ldb),
904 : schema_in,
905 : use_global_schema);
906 : }
907 164100270 : if (ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", refresh_fn) != LDB_SUCCESS) {
908 0 : ldb_debug_set(ldb, LDB_DEBUG_FATAL,
909 : "dsdb_get_schema: re-setting dsdb_schema_refresh_fn failed");
910 : }
911 164100270 : if (!schema_out) {
912 0 : schema_out = schema_in;
913 0 : ldb_debug_set(ldb, LDB_DEBUG_FATAL,
914 : "dsdb_get_schema: refresh_fn() failed");
915 : }
916 : } else {
917 28315311 : schema_out = schema_in;
918 : }
919 :
920 : /* This removes the extra reference above */
921 196483772 : talloc_free(tmp_ctx);
922 :
923 : /*
924 : * If ref ctx exists and doesn't already reference schema, then add
925 : * a reference. Otherwise, just return schema.
926 : *
927 : * We must use talloc_parent(), which is not quite free (there
928 : * is no direct parent pointer in talloc, only one on the
929 : * first child within a linked list), but is much cheaper than
930 : * talloc_is_parent() which walks the whole tree up to the top
931 : * looking for a potential grand-grand(etc)-parent.
932 : */
933 196483772 : if (reference_ctx == NULL) {
934 82547814 : return schema_out;
935 : } else {
936 105131152 : void *schema_parent = talloc_parent(schema_out);
937 105131152 : bool is_already_parent =
938 : schema_parent == reference_ctx;
939 105131152 : if (is_already_parent) {
940 0 : return schema_out;
941 : } else {
942 105131152 : return talloc_reference(reference_ctx,
943 : schema_out);
944 : }
945 : }
946 : }
947 :
948 : /**
949 : * Make the schema found on this ldb the 'global' schema
950 : */
951 :
952 20144 : void dsdb_make_schema_global(struct ldb_context *ldb, struct dsdb_schema *schema)
953 : {
954 20144 : if (!schema) {
955 0 : return;
956 : }
957 :
958 20144 : if (global_schema) {
959 17660 : talloc_unlink(NULL, global_schema);
960 : }
961 :
962 : /* we want the schema to be around permanently */
963 20144 : talloc_reparent(ldb, NULL, schema);
964 20144 : global_schema = schema;
965 :
966 : /* This calls the talloc_reference() of the global schema back onto the ldb */
967 20144 : dsdb_set_global_schema(ldb);
968 : }
969 :
970 : /**
971 : * When loading the schema from LDIF files, we don't get the extended DNs.
972 : *
973 : * We need to set these up, so that from the moment we start the provision,
974 : * the defaultObjectCategory links are set up correctly.
975 : */
976 390 : int dsdb_schema_fill_extended_dn(struct ldb_context *ldb, struct dsdb_schema *schema)
977 : {
978 24 : struct dsdb_class *cur;
979 24 : const struct dsdb_class *target_class;
980 102962 : for (cur = schema->classes; cur; cur = cur->next) {
981 6451 : const struct ldb_val *rdn;
982 6451 : struct ldb_val guid;
983 6451 : NTSTATUS status;
984 6451 : int ret;
985 102572 : struct ldb_dn *dn = ldb_dn_new(NULL, ldb, cur->defaultObjectCategory);
986 :
987 102572 : if (!dn) {
988 0 : return LDB_ERR_INVALID_DN_SYNTAX;
989 : }
990 102572 : rdn = ldb_dn_get_component_val(dn, 0);
991 102572 : if (!rdn) {
992 0 : talloc_free(dn);
993 0 : return LDB_ERR_INVALID_DN_SYNTAX;
994 : }
995 102572 : target_class = dsdb_class_by_cn_ldb_val(schema, rdn);
996 102572 : if (!target_class) {
997 0 : talloc_free(dn);
998 0 : return LDB_ERR_CONSTRAINT_VIOLATION;
999 : }
1000 :
1001 102572 : status = GUID_to_ndr_blob(&target_class->objectGUID, dn, &guid);
1002 102572 : if (!NT_STATUS_IS_OK(status)) {
1003 0 : talloc_free(dn);
1004 0 : return ldb_operr(ldb);
1005 : }
1006 102572 : ret = ldb_dn_set_extended_component(dn, "GUID", &guid);
1007 102572 : if (ret != LDB_SUCCESS) {
1008 0 : ret = ldb_error(ldb, ret, "Could not set GUID");
1009 0 : talloc_free(dn);
1010 0 : return ret;
1011 : }
1012 :
1013 102572 : cur->defaultObjectCategory = ldb_dn_get_extended_linearized(cur, dn, 1);
1014 102572 : talloc_free(dn);
1015 : }
1016 366 : return LDB_SUCCESS;
1017 : }
1018 :
1019 : /**
1020 : * @brief Add a new element to the schema and checks if it's a duplicate
1021 : *
1022 : * This function will add a new element to the schema and checks for existing
1023 : * duplicates.
1024 : *
1025 : * @param[in] ldb A pointer to an LDB context
1026 : *
1027 : * @param[in] schema A pointer to the dsdb_schema where the element
1028 : * will be added.
1029 : *
1030 : * @param[in] msg The ldb_message object representing the element
1031 : * to add.
1032 : *
1033 : * @param[in] checkdups A boolean to indicate if checks for duplicates
1034 : * should be done.
1035 : *
1036 : * @return A WERROR code
1037 : */
1038 46809865 : WERROR dsdb_schema_set_el_from_ldb_msg_dups(struct ldb_context *ldb, struct dsdb_schema *schema,
1039 : struct ldb_message *msg, bool checkdups)
1040 : {
1041 574909 : const char* tstring;
1042 574909 : time_t ts;
1043 46809865 : tstring = ldb_msg_find_attr_as_string(msg, "whenChanged", NULL);
1044 : /* keep a trace of the ts of the most recently changed object */
1045 46809865 : if (tstring) {
1046 13956 : ts = ldb_string_to_time(tstring);
1047 13956 : if (ts > schema->ts_last_change) {
1048 28 : schema->ts_last_change = ts;
1049 : }
1050 : }
1051 46809865 : if (samdb_find_attribute(ldb, msg,
1052 : "objectclass", "attributeSchema") != NULL) {
1053 :
1054 39379261 : return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, checkdups);
1055 7430604 : } else if (samdb_find_attribute(ldb, msg,
1056 : "objectclass", "classSchema") != NULL) {
1057 7430373 : return dsdb_set_class_from_ldb_dups(schema, msg, checkdups);
1058 : }
1059 : /* Don't fail on things not classes or attributes */
1060 231 : return WERR_OK;
1061 : }
1062 :
1063 46678409 : WERROR dsdb_schema_set_el_from_ldb_msg(struct ldb_context *ldb,
1064 : struct dsdb_schema *schema,
1065 : struct ldb_message *msg)
1066 : {
1067 46678409 : return dsdb_schema_set_el_from_ldb_msg_dups(ldb, schema, msg, false);
1068 : }
1069 :
1070 : /**
1071 : * Rather than read a schema from the LDB itself, read it from an ldif
1072 : * file. This allows schema to be loaded and used while adding the
1073 : * schema itself to the directory.
1074 : *
1075 : * Should be called with a transaction (or failing that, have no concurrent
1076 : * access while called).
1077 : */
1078 :
1079 390 : WERROR dsdb_set_schema_from_ldif(struct ldb_context *ldb,
1080 : const char *pf, const char *df,
1081 : const char *dn)
1082 : {
1083 24 : struct ldb_ldif *ldif;
1084 24 : struct ldb_message *msg;
1085 24 : TALLOC_CTX *mem_ctx;
1086 24 : WERROR status;
1087 24 : int ret;
1088 24 : struct dsdb_schema *schema;
1089 24 : const struct ldb_val *prefix_val;
1090 24 : const struct ldb_val *info_val;
1091 24 : struct ldb_val info_val_default;
1092 :
1093 :
1094 390 : mem_ctx = talloc_new(ldb);
1095 390 : if (!mem_ctx) {
1096 0 : goto nomem;
1097 : }
1098 :
1099 390 : schema = dsdb_new_schema(mem_ctx);
1100 390 : if (!schema) {
1101 0 : goto nomem;
1102 : }
1103 390 : schema->fsmo.we_are_master = true;
1104 390 : schema->fsmo.update_allowed = true;
1105 390 : schema->fsmo.master_dn = ldb_dn_new(schema, ldb, "@PROVISION_SCHEMA_MASTER");
1106 390 : if (!schema->fsmo.master_dn) {
1107 0 : goto nomem;
1108 : }
1109 :
1110 : /*
1111 : * load the prefixMap attribute from pf
1112 : */
1113 390 : ldif = ldb_ldif_read_string(ldb, &pf);
1114 390 : if (!ldif) {
1115 0 : status = WERR_INVALID_PARAMETER;
1116 0 : goto failed;
1117 : }
1118 390 : talloc_steal(mem_ctx, ldif);
1119 :
1120 390 : ret = ldb_msg_normalize(ldb, mem_ctx, ldif->msg, &msg);
1121 390 : if (ret != LDB_SUCCESS) {
1122 0 : goto nomem;
1123 : }
1124 390 : talloc_free(ldif);
1125 :
1126 390 : prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1127 390 : if (!prefix_val) {
1128 0 : status = WERR_INVALID_PARAMETER;
1129 0 : goto failed;
1130 : }
1131 :
1132 390 : info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1133 390 : if (!info_val) {
1134 390 : status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
1135 390 : W_ERROR_NOT_OK_GOTO(status, failed);
1136 366 : info_val = &info_val_default;
1137 : }
1138 :
1139 390 : status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1140 390 : if (!W_ERROR_IS_OK(status)) {
1141 0 : DEBUG(0,("ERROR: dsdb_load_oid_mappings_ldb() failed with %s\n", win_errstr(status)));
1142 0 : goto failed;
1143 : }
1144 :
1145 390 : schema->ts_last_change = 0;
1146 : /* load the attribute and class definitions out of df */
1147 675295 : while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1148 674905 : talloc_steal(mem_ctx, ldif);
1149 :
1150 674905 : ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &msg);
1151 674905 : if (ret != LDB_SUCCESS) {
1152 0 : goto nomem;
1153 : }
1154 :
1155 674905 : status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, msg);
1156 674905 : talloc_free(ldif);
1157 674905 : if (!W_ERROR_IS_OK(status)) {
1158 0 : goto failed;
1159 : }
1160 : }
1161 :
1162 : /*
1163 : * TODO We may need a transaction here, otherwise this causes races.
1164 : *
1165 : * To do so may require an ldb_in_transaction function. In the
1166 : * meantime, assume that this is always called with a transaction or in
1167 : * isolation.
1168 : */
1169 390 : ret = dsdb_set_schema(ldb, schema, SCHEMA_WRITE);
1170 390 : if (ret != LDB_SUCCESS) {
1171 0 : status = WERR_FOOBAR;
1172 0 : DEBUG(0,("ERROR: dsdb_set_schema() failed with %s / %s\n",
1173 : ldb_strerror(ret), ldb_errstring(ldb)));
1174 0 : goto failed;
1175 : }
1176 :
1177 390 : ret = dsdb_schema_fill_extended_dn(ldb, schema);
1178 390 : if (ret != LDB_SUCCESS) {
1179 0 : status = WERR_FOOBAR;
1180 0 : goto failed;
1181 : }
1182 :
1183 390 : goto done;
1184 :
1185 0 : nomem:
1186 0 : status = WERR_NOT_ENOUGH_MEMORY;
1187 24 : failed:
1188 390 : done:
1189 390 : talloc_free(mem_ctx);
1190 390 : return status;
1191 : }
|