LCOV - code coverage report
Current view: top level - source4/dsdb/schema - schema_set.c (source / functions) Hit Total Coverage
Test: coverage report for recycleplus df22b230 Lines: 385 482 79.9 %
Date: 2024-02-14 10:14:15 Functions: 26 26 100.0 %

          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   712081550 : const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb,
      44             :                                                                    void *private_data,
      45             :                                                                    const char *name)
      46             : {
      47   712081550 :         struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
      48   712081550 :         const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
      49   712081550 :         if (a == NULL) {
      50             :                 /* this will fall back to ldb internal handling */
      51     2243815 :                 return NULL;
      52             :         }
      53   709837735 :         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      160734 : int dsdb_schema_set_indices_and_attributes(struct ldb_context *ldb,
      63             :                                            struct dsdb_schema *schema,
      64             :                                            enum schema_set_enum mode)
      65             : {
      66      160734 :         int ret = LDB_SUCCESS;
      67             :         struct ldb_result *res;
      68             :         struct ldb_result *res_idx;
      69             :         struct dsdb_attribute *attr;
      70             :         struct ldb_message *mod_msg;
      71             :         TALLOC_CTX *mem_ctx;
      72             :         struct ldb_message *msg;
      73             :         struct ldb_message *msg_idx;
      74             : 
      75             :         struct loadparm_context *lp_ctx =
      76      160734 :                 talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
      77             :                                 struct loadparm_context);
      78      160734 :         bool guid_indexing = true;
      79      160734 :         bool declare_ordered_integer_in_attributes = true;
      80             :         uint32_t pack_format_override;
      81      160734 :         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      160412 :                 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      160412 :                 pack_format_override =
     108      160412 :                         (intptr_t)ldb_get_opaque(ldb, "pack_format_override");
     109      160412 :                 if (pack_format_override == LDB_PACKING_FORMAT ||
     110             :                     pack_format_override == LDB_PACKING_FORMAT_NODN) {
     111           0 :                         declare_ordered_integer_in_attributes = false;
     112             :                 }
     113             :         }
     114             :         /* setup our own attribute name to schema handler */
     115      160734 :         ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
     116      160734 :         ldb_schema_set_override_indexlist(ldb, true);
     117      160734 :         if (guid_indexing) {
     118      160734 :                 ldb_schema_set_override_GUID_index(ldb, "objectGUID", "GUID");
     119             :         }
     120             : 
     121      160734 :         if (mode == SCHEMA_MEMORY_ONLY) {
     122      155989 :                 return ret;
     123             :         }
     124             : 
     125        4745 :         mem_ctx = talloc_new(ldb);
     126        4745 :         if (!mem_ctx) {
     127           0 :                 return ldb_oom(ldb);
     128             :         }
     129             : 
     130        4745 :         msg = ldb_msg_new(mem_ctx);
     131        4745 :         if (!msg) {
     132           0 :                 ldb_oom(ldb);
     133           0 :                 goto op_error;
     134             :         }
     135        4745 :         msg_idx = ldb_msg_new(mem_ctx);
     136        4745 :         if (!msg_idx) {
     137           0 :                 ldb_oom(ldb);
     138           0 :                 goto op_error;
     139             :         }
     140        4745 :         msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
     141        4745 :         if (!msg->dn) {
     142           0 :                 ldb_oom(ldb);
     143           0 :                 goto op_error;
     144             :         }
     145        4745 :         msg_idx->dn = ldb_dn_new(msg_idx, ldb, "@INDEXLIST");
     146        4745 :         if (!msg_idx->dn) {
     147           0 :                 ldb_oom(ldb);
     148           0 :                 goto op_error;
     149             :         }
     150             : 
     151        4745 :         ret = ldb_msg_add_string(msg_idx, "@IDXONE", "1");
     152        4745 :         if (ret != LDB_SUCCESS) {
     153           0 :                 goto op_error;
     154             :         }
     155             : 
     156        4745 :         if (guid_indexing) {
     157        4745 :                 ret = ldb_msg_add_string(msg_idx, "@IDXGUID", "objectGUID");
     158        4745 :                 if (ret != LDB_SUCCESS) {
     159           0 :                         goto op_error;
     160             :                 }
     161             : 
     162        4745 :                 ret = ldb_msg_add_string(msg_idx, "@IDX_DN_GUID", "GUID");
     163        4745 :                 if (ret != LDB_SUCCESS) {
     164           0 :                         goto op_error;
     165             :                 }
     166             :         }
     167             : 
     168        4745 :         ret = ldb_msg_add_string(msg_idx, "@SAMDB_INDEXING_VERSION", SAMDB_INDEXING_VERSION);
     169        4745 :         if (ret != LDB_SUCCESS) {
     170           0 :                 goto op_error;
     171             :         }
     172             : 
     173        4745 :         ret = ldb_msg_add_string(msg_idx, SAMBA_FEATURES_SUPPORTED_FLAG, "1");
     174        4745 :         if (ret != LDB_SUCCESS) {
     175           0 :                 goto op_error;
     176             :         }
     177             : 
     178     6839320 :         for (attr = schema->attributes; attr; attr = attr->next) {
     179     6834575 :                 const char *syntax = attr->syntax->ldb_syntax;
     180             : 
     181     6834575 :                 if (!syntax) {
     182     3823713 :                         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     6834575 :                 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
     191           0 :                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
     192     6834575 :                 } else if (strcmp(syntax, LDB_SYNTAX_ORDERED_INTEGER) == 0) {
     193      444944 :                         if (declare_ordered_integer_in_attributes &&
     194             :                             guid_indexing) {
     195             :                                 /*
     196             :                                  * The normal production case
     197             :                                  */
     198      444944 :                                 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           0 :                                 ret = ldb_msg_add_string(msg,
     213             :                                                          attr->lDAPDisplayName,
     214             :                                                          "INTEGER");
     215             :                         }
     216     6389631 :                 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
     217     2509361 :                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName,
     218             :                                                  "CASE_INSENSITIVE");
     219             :                 }
     220     6834575 :                 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     6834575 :                 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX &&
     230      686562 :                     !(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      686562 :                         if (strcmp(syntax, LDB_SYNTAX_ORDERED_INTEGER) == 0
     237       38553 :                             && !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      686562 :                                 ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
     251      686562 :                                 if (ret != LDB_SUCCESS) {
     252           0 :                                         break;
     253             :                                 }
     254             :                         }
     255             :                 }
     256             :         }
     257             : 
     258        4745 :         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        4745 :         ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL,
     268             :                          NULL);
     269        4745 :         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
     270         149 :                 if (mode == SCHEMA_COMPARE) {
     271             :                         /* We are probably not in a transaction */
     272           0 :                         goto wrong_mode;
     273             :                 }
     274         149 :                 ret = ldb_add(ldb, msg);
     275        4596 :         } else if (ret != LDB_SUCCESS) {
     276        4179 :         } 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        4173 :                 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
     285             : 
     286        4173 :                 ret = ldb_msg_difference(ldb, mem_ctx,
     287        4173 :                                          res->msgs[0], msg, &mod_msg);
     288        4173 :                 if (ret != LDB_SUCCESS) {
     289           0 :                         goto op_error;
     290             :                 }
     291        4173 :                 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         164 :                         if (mode == SCHEMA_COMPARE) {
     298             :                                 /* We are probably not in a transaction */
     299          26 :                                 goto wrong_mode;
     300             :                         }
     301         138 :                         ret = dsdb_modify(ldb, mod_msg, 0);
     302             :                 }
     303        4147 :                 talloc_free(mod_msg);
     304             :         }
     305             : 
     306        4719 :         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         417 :                 ret = LDB_SUCCESS;
     309             :         }
     310        4719 :         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        4719 :         ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE,
     320             :                          NULL, NULL);
     321        4719 :         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
     322         149 :                 if (mode == SCHEMA_COMPARE) {
     323             :                         /* We are probably not in a transaction */
     324           0 :                         goto wrong_mode;
     325             :                 }
     326         149 :                 ret = ldb_add(ldb, msg_idx);
     327        4570 :         } else if (ret != LDB_SUCCESS) {
     328        4153 :         } 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        4147 :                 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
     337             : 
     338        4147 :                 ret = ldb_msg_difference(ldb, mem_ctx,
     339        4147 :                                          res_idx->msgs[0], msg_idx, &mod_msg);
     340        4147 :                 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        4147 :                 if (mod_msg->num_elements == 1
     357          65 :                     && 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        4147 :                 } 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          76 :                         if (mode == SCHEMA_COMPARE) {
     371             :                                 /* We are probably not in a transaction */
     372           2 :                                 goto wrong_mode;
     373             :                         }
     374          74 :                         ret = dsdb_modify(ldb, mod_msg, 0);
     375             :                 }
     376        4145 :                 talloc_free(mod_msg);
     377             :         }
     378        4717 :         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         417 :                 ret = LDB_SUCCESS;
     381             :         }
     382             : 
     383        4717 :         if (ret != LDB_SUCCESS) {
     384           0 :                 DBG_ERR("Failed to set schema into @INDEXLIST: %s\n",
     385             :                         ldb_errstring(ldb));
     386             :         }
     387             : 
     388        4717 :         talloc_free(mem_ctx);
     389        4717 :         return ret;
     390             : 
     391           0 : op_error:
     392           0 :         talloc_free(mem_ctx);
     393           0 :         return ldb_operr(ldb);
     394             : 
     395          28 : wrong_mode:
     396          28 :         talloc_free(mem_ctx);
     397          28 :         return LDB_ERR_BUSY;
     398             : }
     399             : 
     400             : 
     401             : /*
     402             :   create extra attribute shortcuts
     403             :  */
     404       21802 : static void dsdb_setup_attribute_shortcuts(struct ldb_context *ldb, struct dsdb_schema *schema)
     405             : {
     406             :         struct dsdb_attribute *attribute;
     407             : 
     408             :         /* setup fast access to one_way_link and DN format */
     409    30409169 :         for (attribute=schema->attributes; attribute; attribute=attribute->next) {
     410    30387367 :                 attribute->dn_format = dsdb_dn_oid_to_format(attribute->syntax->ldap_oid);
     411             : 
     412    30387367 :                 if (attribute->dn_format == DSDB_INVALID_DN) {
     413    26294574 :                         attribute->one_way_link = false;
     414    26294574 :                         continue;
     415             :                 }
     416             : 
     417             :                 /* these are not considered to be one way links for
     418             :                    the purpose of DN link fixups */
     419     4092793 :                 if (ldb_attr_cmp("distinguishedName", attribute->lDAPDisplayName) == 0 ||
     420     4070991 :                     ldb_attr_cmp("objectCategory", attribute->lDAPDisplayName) == 0) {
     421       43604 :                         attribute->one_way_link = false;
     422       43604 :                         continue;
     423             :                 }
     424             : 
     425     4049189 :                 if (attribute->linkID == 0) {
     426     1614537 :                         attribute->one_way_link = true;
     427     1614537 :                         continue;
     428             :                 }
     429             :                 /* handle attributes with a linkID but no backlink */
     430     3845117 :                 if ((attribute->linkID & 1) == 0 &&
     431     1410465 :                     dsdb_attribute_by_linkID(schema, attribute->linkID + 1) == NULL) {
     432      386278 :                         attribute->one_way_link = true;
     433      386278 :                         continue;
     434             :                 }
     435     2048374 :                 attribute->one_way_link = false;
     436             :         }
     437       21802 : }
     438             : 
     439   572421422 : static int uint32_cmp(uint32_t c1, uint32_t c2)
     440             : {
     441   572421422 :         if (c1 == c2) return 0;
     442   432560961 :         return c1 > c2 ? 1 : -1;
     443             : }
     444             : 
     445    38469487 : static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
     446             : {
     447    38469487 :         return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
     448             : }
     449    38633165 : static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
     450             : {
     451    38633165 :         return uint32_cmp((*c1)->governsID_id, (*c2)->governsID_id);
     452             : }
     453    38659805 : static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
     454             : {
     455    38659805 :         return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
     456             : }
     457    38454472 : static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
     458             : {
     459    38454472 :         return strcasecmp((*c1)->cn, (*c2)->cn);
     460             : }
     461             : 
     462   278381717 : static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     463             : {
     464   278381717 :         return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
     465             : }
     466   279418870 : static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     467             : {
     468   279418870 :         return uint32_cmp((*a1)->attributeID_id, (*a2)->attributeID_id);
     469             : }
     470      699004 : static int dsdb_compare_attribute_by_msDS_IntId(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     471             : {
     472      699004 :         return uint32_cmp((*a1)->msDS_IntId, (*a2)->msDS_IntId);
     473             : }
     474   279334883 : static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     475             : {
     476   279334883 :         return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
     477             : }
     478   253670383 : static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     479             : {
     480   253670383 :         return uint32_cmp((*a1)->linkID, (*a2)->linkID);
     481             : }
     482             : 
     483             : /**
     484             :  * Clean up Classes and Attributes accessor arrays
     485             :  */
     486       21802 : static void dsdb_sorted_accessors_free(struct dsdb_schema *schema)
     487             : {
     488             :         /* free classes accessors */
     489       21802 :         TALLOC_FREE(schema->classes_by_lDAPDisplayName);
     490       21802 :         TALLOC_FREE(schema->classes_by_governsID_id);
     491       21802 :         TALLOC_FREE(schema->classes_by_governsID_oid);
     492       21802 :         TALLOC_FREE(schema->classes_by_cn);
     493             :         /* free attribute accessors */
     494       21802 :         TALLOC_FREE(schema->attributes_by_lDAPDisplayName);
     495       21802 :         TALLOC_FREE(schema->attributes_by_attributeID_id);
     496       21802 :         TALLOC_FREE(schema->attributes_by_msDS_IntId);
     497       21802 :         TALLOC_FREE(schema->attributes_by_attributeID_oid);
     498       21802 :         TALLOC_FREE(schema->attributes_by_linkID);
     499       21802 : }
     500             : 
     501             : /*
     502             :   create the sorted accessor arrays for the schema
     503             :  */
     504       21802 : int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
     505             :                                 struct dsdb_schema *schema)
     506             : {
     507             :         struct dsdb_class *cur;
     508             :         struct dsdb_attribute *a;
     509             :         unsigned int i;
     510             :         unsigned int num_int_id;
     511             :         int ret;
     512             : 
     513       21802 :         for (i=0; i < schema->classes_to_remove_size; i++) {
     514           0 :                 DLIST_REMOVE(schema->classes, schema->classes_to_remove[i]);
     515           0 :                 TALLOC_FREE(schema->classes_to_remove[i]);
     516             :         }
     517       21802 :         for (i=0; i < schema->attributes_to_remove_size; i++) {
     518           0 :                 DLIST_REMOVE(schema->attributes, schema->attributes_to_remove[i]);
     519           0 :                 TALLOC_FREE(schema->attributes_to_remove[i]);
     520             :         }
     521             : 
     522       21802 :         TALLOC_FREE(schema->classes_to_remove);
     523       21802 :         schema->classes_to_remove_size = 0;
     524       21802 :         TALLOC_FREE(schema->attributes_to_remove);
     525       21802 :         schema->attributes_to_remove_size = 0;
     526             : 
     527             :         /* free all caches */
     528       21802 :         dsdb_sorted_accessors_free(schema);
     529             : 
     530             :         /* count the classes */
     531     5723444 :         for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
     532       21802 :         schema->num_classes = i;
     533             : 
     534             :         /* setup classes_by_* */
     535       21802 :         schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
     536       21802 :         schema->classes_by_governsID_id    = talloc_array(schema, struct dsdb_class *, i);
     537       21802 :         schema->classes_by_governsID_oid   = talloc_array(schema, struct dsdb_class *, i);
     538       21802 :         schema->classes_by_cn              = talloc_array(schema, struct dsdb_class *, i);
     539       21802 :         if (schema->classes_by_lDAPDisplayName == NULL ||
     540       21802 :             schema->classes_by_governsID_id == NULL ||
     541       21802 :             schema->classes_by_governsID_oid == NULL ||
     542       21802 :             schema->classes_by_cn == NULL) {
     543           0 :                 goto failed;
     544             :         }
     545             : 
     546     5723444 :         for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
     547     5701642 :                 schema->classes_by_lDAPDisplayName[i] = cur;
     548     5701642 :                 schema->classes_by_governsID_id[i]    = cur;
     549     5701642 :                 schema->classes_by_governsID_oid[i]   = cur;
     550     5701642 :                 schema->classes_by_cn[i]              = cur;
     551             :         }
     552             : 
     553             :         /* sort the arrays */
     554       21802 :         TYPESAFE_QSORT(schema->classes_by_lDAPDisplayName, schema->num_classes, dsdb_compare_class_by_lDAPDisplayName);
     555       21802 :         TYPESAFE_QSORT(schema->classes_by_governsID_id, schema->num_classes, dsdb_compare_class_by_governsID_id);
     556       21802 :         TYPESAFE_QSORT(schema->classes_by_governsID_oid, schema->num_classes, dsdb_compare_class_by_governsID_oid);
     557       21802 :         TYPESAFE_QSORT(schema->classes_by_cn, schema->num_classes, dsdb_compare_class_by_cn);
     558             : 
     559             :         /* now build the attribute accessor arrays */
     560             : 
     561             :         /* count the attributes
     562             :          * and attributes with msDS-IntId set */
     563       21802 :         num_int_id = 0;
     564    30409169 :         for (i=0, a=schema->attributes; a; i++, a=a->next) {
     565    30387367 :                 if (a->msDS_IntId != 0) {
     566      219650 :                         num_int_id++;
     567             :                 }
     568             :         }
     569       21802 :         schema->num_attributes = i;
     570       21802 :         schema->num_int_id_attr = num_int_id;
     571             : 
     572             :         /* setup attributes_by_* */
     573       21802 :         schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
     574       21802 :         schema->attributes_by_attributeID_id    = talloc_array(schema, struct dsdb_attribute *, i);
     575       21802 :         schema->attributes_by_msDS_IntId        = talloc_array(schema,
     576             :                                                                struct dsdb_attribute *, num_int_id);
     577       21802 :         schema->attributes_by_attributeID_oid   = talloc_array(schema, struct dsdb_attribute *, i);
     578       21802 :         schema->attributes_by_linkID              = talloc_array(schema, struct dsdb_attribute *, i);
     579       21802 :         if (schema->attributes_by_lDAPDisplayName == NULL ||
     580       21802 :             schema->attributes_by_attributeID_id == NULL ||
     581       21802 :             schema->attributes_by_msDS_IntId == NULL ||
     582       21802 :             schema->attributes_by_attributeID_oid == NULL ||
     583       21802 :             schema->attributes_by_linkID == NULL) {
     584           0 :                 goto failed;
     585             :         }
     586             : 
     587       21802 :         num_int_id = 0;
     588    30409169 :         for (i=0, a=schema->attributes; a; i++, a=a->next) {
     589    30387367 :                 schema->attributes_by_lDAPDisplayName[i] = a;
     590    30387367 :                 schema->attributes_by_attributeID_id[i]    = a;
     591    30387367 :                 schema->attributes_by_attributeID_oid[i]   = a;
     592    30387367 :                 schema->attributes_by_linkID[i]          = a;
     593             :                 /* append attr-by-msDS-IntId values */
     594    30387367 :                 if (a->msDS_IntId != 0) {
     595      219650 :                         schema->attributes_by_msDS_IntId[num_int_id] = a;
     596      219650 :                         num_int_id++;
     597             :                 }
     598             :         }
     599       21802 :         SMB_ASSERT(num_int_id == schema->num_int_id_attr);
     600             : 
     601             :         /* sort the arrays */
     602       21802 :         TYPESAFE_QSORT(schema->attributes_by_lDAPDisplayName, schema->num_attributes, dsdb_compare_attribute_by_lDAPDisplayName);
     603       21802 :         TYPESAFE_QSORT(schema->attributes_by_attributeID_id, schema->num_attributes, dsdb_compare_attribute_by_attributeID_id);
     604       21802 :         TYPESAFE_QSORT(schema->attributes_by_msDS_IntId, schema->num_int_id_attr, dsdb_compare_attribute_by_msDS_IntId);
     605       21802 :         TYPESAFE_QSORT(schema->attributes_by_attributeID_oid, schema->num_attributes, dsdb_compare_attribute_by_attributeID_oid);
     606       21802 :         TYPESAFE_QSORT(schema->attributes_by_linkID, schema->num_attributes, dsdb_compare_attribute_by_linkID);
     607             : 
     608       21802 :         dsdb_setup_attribute_shortcuts(ldb, schema);
     609             : 
     610       21802 :         ret = schema_fill_constructed(schema);
     611       21802 :         if (ret != LDB_SUCCESS) {
     612           0 :                 dsdb_sorted_accessors_free(schema);
     613           0 :                 return ret;
     614             :         }
     615             : 
     616       21802 :         return LDB_SUCCESS;
     617             : 
     618           0 : failed:
     619           0 :         dsdb_sorted_accessors_free(schema);
     620           0 :         return ldb_oom(ldb);
     621             : }
     622             : 
     623             : /**
     624             :  * Attach the schema to an opaque pointer on the ldb,
     625             :  * so ldb modules can find it
     626             :  */
     627      117306 : int dsdb_set_schema_refresh_function(struct ldb_context *ldb,
     628             :                                      dsdb_schema_refresh_fn refresh_fn,
     629             :                                      struct ldb_module *module)
     630             : {
     631      117306 :         int ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", refresh_fn);
     632      117306 :         if (ret != LDB_SUCCESS) {
     633           0 :                 return ret;
     634             :         }
     635             : 
     636      117306 :         ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn_private_data", module);
     637      117306 :         if (ret != LDB_SUCCESS) {
     638           0 :                 return ret;
     639             :         }
     640      117306 :         return LDB_SUCCESS;
     641             : }
     642             : 
     643             : /**
     644             :  * Attach the schema to an opaque pointer on the ldb,
     645             :  * so ldb modules can find it
     646             :  */
     647       21513 : int dsdb_set_schema(struct ldb_context *ldb,
     648             :                     struct dsdb_schema *schema,
     649             :                     enum schema_set_enum write_indices_and_attributes)
     650             : {
     651             :         struct dsdb_schema *old_schema;
     652             :         int ret;
     653             : 
     654       21513 :         ret = dsdb_setup_sorted_accessors(ldb, schema);
     655       21513 :         if (ret != LDB_SUCCESS) {
     656           0 :                 return ret;
     657             :         }
     658             : 
     659       21513 :         old_schema = ldb_get_opaque(ldb, "dsdb_schema");
     660             : 
     661       21513 :         ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
     662       21513 :         if (ret != LDB_SUCCESS) {
     663           0 :                 return ret;
     664             :         }
     665             : 
     666       21513 :         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
     667       21513 :         if (ret != LDB_SUCCESS) {
     668           0 :                 return ret;
     669             :         }
     670             : 
     671       21513 :         talloc_steal(ldb, schema);
     672             : 
     673             :         /* Set the new attributes based on the new schema */
     674       21513 :         ret = dsdb_schema_set_indices_and_attributes(ldb, schema,
     675             :                                                      write_indices_and_attributes);
     676       21513 :         if (ret != LDB_SUCCESS) {
     677           0 :                 return ret;
     678             :         }
     679             : 
     680             :         /*
     681             :          * Remove the reference to the schema we just overwrote - if there was
     682             :          * none, NULL is harmless here.
     683             :          */
     684       21513 :         if (old_schema != schema) {
     685       21513 :                 talloc_unlink(ldb, old_schema);
     686             :         }
     687             : 
     688       21513 :         return ret;
     689             : }
     690             : 
     691             : /**
     692             :  * Global variable to hold one copy of the schema, used to avoid memory bloat
     693             :  */
     694             : static struct dsdb_schema *global_schema;
     695             : 
     696             : /**
     697             :  * Make this ldb use a specified schema, already fully calculated and belonging to another ldb
     698             :  *
     699             :  * The write_indices_and_attributes controls writing of the @ records
     700             :  * because we cannot write to a database that does not yet exist on
     701             :  * disk.
     702             :  */
     703         521 : int dsdb_reference_schema(struct ldb_context *ldb, struct dsdb_schema *schema,
     704             :                           enum schema_set_enum write_indices_and_attributes)
     705             : {
     706             :         int ret;
     707             :         void *ptr;
     708         521 :         void *schema_parent = NULL;
     709             :         bool is_already_parent;
     710             :         struct dsdb_schema *old_schema;
     711         521 :         old_schema = ldb_get_opaque(ldb, "dsdb_schema");
     712         521 :         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
     713         521 :         if (ret != LDB_SUCCESS) {
     714           0 :                 return ret;
     715             :         }
     716             : 
     717             :         /* Remove the reference to the schema we just overwrote - if there was
     718             :          * none, NULL is harmless here */
     719         521 :         talloc_unlink(ldb, old_schema);
     720             : 
     721             :         /* Reference schema on ldb if it wasn't done already */
     722         521 :         schema_parent = talloc_parent(schema);
     723         521 :         is_already_parent = (schema_parent == ldb);
     724         521 :         if (!is_already_parent) {
     725         521 :                 ptr = talloc_reference(ldb, schema);
     726         521 :                 if (ptr == NULL) {
     727           0 :                         return ldb_oom(ldb);
     728             :                 }
     729             :         }
     730             : 
     731             :         /* Make this ldb use local schema preferably */
     732         521 :         ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
     733         521 :         if (ret != LDB_SUCCESS) {
     734           0 :                 return ret;
     735             :         }
     736             : 
     737         521 :         ret = ldb_set_opaque(ldb, "dsdb_refresh_fn", NULL);
     738         521 :         if (ret != LDB_SUCCESS) {
     739           0 :                 return ret;
     740             :         }
     741             : 
     742         521 :         ret = ldb_set_opaque(ldb, "dsdb_refresh_fn_private_data", NULL);
     743         521 :         if (ret != LDB_SUCCESS) {
     744           0 :                 return ret;
     745             :         }
     746             : 
     747         521 :         ret = dsdb_schema_set_indices_and_attributes(ldb, schema, write_indices_and_attributes);
     748         521 :         if (ret != LDB_SUCCESS) {
     749           0 :                 return ret;
     750             :         }
     751             : 
     752         521 :         return LDB_SUCCESS;
     753             : }
     754             : 
     755             : /**
     756             :  * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
     757             :  */
     758      155561 : int dsdb_set_global_schema(struct ldb_context *ldb)
     759             : {
     760             :         int ret;
     761      155561 :         void *use_global_schema = (void *)1;
     762             :         void *ptr;
     763      155561 :         struct dsdb_schema *old_schema = ldb_get_opaque(ldb, "dsdb_schema");
     764             : 
     765      155561 :         ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", use_global_schema);
     766      155561 :         if (ret != LDB_SUCCESS) {
     767           0 :                 return ret;
     768             :         }
     769             : 
     770      155561 :         if (global_schema == NULL) {
     771       20956 :                 return LDB_SUCCESS;
     772             :         }
     773             : 
     774             :         /* Remove any pointer to a previous schema */
     775      134605 :         ret = ldb_set_opaque(ldb, "dsdb_schema", NULL);
     776      134605 :         if (ret != LDB_SUCCESS) {
     777           0 :                 return ret;
     778             :         }
     779             : 
     780             :         /* Remove the reference to the schema we just overwrote - if there was
     781             :          * none, NULL is harmless here */
     782      134605 :         talloc_unlink(ldb, old_schema);
     783             : 
     784             :         /* Set the new attributes based on the new schema */
     785             :         /* Don't write indices and attributes, it's expensive */
     786      134605 :         ret = dsdb_schema_set_indices_and_attributes(ldb, global_schema, SCHEMA_MEMORY_ONLY);
     787      134605 :         if (ret == LDB_SUCCESS) {
     788      134605 :                 void *schema_parent = talloc_parent(global_schema);
     789      134605 :                 bool is_already_parent =
     790             :                         (schema_parent == ldb);
     791      134605 :                 if (!is_already_parent) {
     792      134605 :                         ptr = talloc_reference(ldb, global_schema);
     793      134605 :                         if (ptr == NULL) {
     794           0 :                                 return ldb_oom(ldb);
     795             :                         }
     796      134605 :                         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
     797             :                 }
     798             :         }
     799             : 
     800      134605 :         return ret;
     801             : }
     802             : 
     803   122910390 : bool dsdb_uses_global_schema(struct ldb_context *ldb)
     804             : {
     805   122910390 :         return (ldb_get_opaque(ldb, "dsdb_use_global_schema") != NULL);
     806             : }
     807             : 
     808             : /**
     809             :  * Find the schema object for this ldb
     810             :  *
     811             :  * If reference_ctx is not NULL, then talloc_reference onto that context
     812             :  */
     813             : 
     814   122796477 : struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb, TALLOC_CTX *reference_ctx)
     815             : {
     816             :         const void *p;
     817   122796477 :         struct dsdb_schema *schema_out = NULL;
     818   122796477 :         struct dsdb_schema *schema_in = NULL;
     819             :         dsdb_schema_refresh_fn refresh_fn;
     820             :         struct ldb_module *loaded_from_module;
     821             :         bool use_global_schema;
     822   122796477 :         TALLOC_CTX *tmp_ctx = talloc_new(reference_ctx);
     823   122796477 :         if (tmp_ctx == NULL) {
     824           0 :                 return NULL;
     825             :         }
     826             : 
     827             :         /* see if we have a cached copy */
     828   122796477 :         use_global_schema = dsdb_uses_global_schema(ldb);
     829   122796477 :         if (use_global_schema) {
     830   101612462 :                 schema_in = global_schema;
     831             :         } else {
     832    21184015 :                 p = ldb_get_opaque(ldb, "dsdb_schema");
     833    21184015 :                 if (p != NULL) {
     834    21096577 :                         schema_in = talloc_get_type_abort(p, struct dsdb_schema);
     835             :                 }
     836             :         }
     837             : 
     838   122796477 :         refresh_fn = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn");
     839   122796477 :         if (refresh_fn) {
     840   101956682 :                 loaded_from_module = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn_private_data");
     841             : 
     842   101956682 :                 SMB_ASSERT(loaded_from_module && (ldb_module_get_ctx(loaded_from_module) == ldb));
     843             :         }
     844             : 
     845   122796477 :         if (refresh_fn) {
     846             :                 /* We need to guard against recursive calls here */
     847   101956682 :                 if (ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", NULL) != LDB_SUCCESS) {
     848           0 :                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     849             :                                       "dsdb_get_schema: clearing dsdb_schema_refresh_fn failed");
     850             :                 } else {
     851   101956682 :                         schema_out = refresh_fn(loaded_from_module,
     852             :                                                 ldb_get_event_context(ldb),
     853             :                                                 schema_in,
     854             :                                                 use_global_schema);
     855             :                 }
     856   101956682 :                 if (ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", refresh_fn) != LDB_SUCCESS) {
     857           0 :                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     858             :                                       "dsdb_get_schema: re-setting dsdb_schema_refresh_fn failed");
     859             :                 }
     860   101956682 :                 if (!schema_out) {
     861           0 :                         schema_out = schema_in;
     862           0 :                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     863             :                                       "dsdb_get_schema: refresh_fn() failed");
     864             :                 }
     865             :         } else {
     866    20839795 :                 schema_out = schema_in;
     867             :         }
     868             : 
     869             :         /* This removes the extra reference above */
     870   122796477 :         talloc_free(tmp_ctx);
     871             : 
     872             :         /*
     873             :          * If ref ctx exists and doesn't already reference schema, then add
     874             :          * a reference.  Otherwise, just return schema.
     875             :          *
     876             :          * We must use talloc_parent(), which is not quite free (there
     877             :          * is no direct parent pointer in talloc, only one on the
     878             :          * first child within a linked list), but is much cheaper than
     879             :          * talloc_is_parent() which walks the whole tree up to the top
     880             :          * looking for a potential grand-grand(etc)-parent.
     881             :          */
     882   122796477 :         if (reference_ctx == NULL) {
     883    56756222 :                 return schema_out;
     884             :         } else {
     885    66040255 :                 void *schema_parent = talloc_parent(schema_out);
     886    66040255 :                 bool is_already_parent =
     887             :                         schema_parent == reference_ctx;
     888    66040255 :                 if (is_already_parent) {
     889           0 :                         return schema_out;
     890             :                 } else {
     891    66040255 :                         return talloc_reference(reference_ctx,
     892             :                                                 schema_out);
     893             :                 }
     894             :         }
     895             : }
     896             : 
     897             : /**
     898             :  * Make the schema found on this ldb the 'global' schema
     899             :  */
     900             : 
     901       17651 : void dsdb_make_schema_global(struct ldb_context *ldb, struct dsdb_schema *schema)
     902             : {
     903       17651 :         if (!schema) {
     904           0 :                 return;
     905             :         }
     906             : 
     907       17651 :         if (global_schema) {
     908       16022 :                 talloc_unlink(NULL, global_schema);
     909             :         }
     910             : 
     911             :         /* we want the schema to be around permanently */
     912       17651 :         talloc_reparent(ldb, NULL, schema);
     913       17651 :         global_schema = schema;
     914             : 
     915             :         /* This calls the talloc_reference() of the global schema back onto the ldb */
     916       17651 :         dsdb_set_global_schema(ldb);
     917             : }
     918             : 
     919             : /**
     920             :  * When loading the schema from LDIF files, we don't get the extended DNs.
     921             :  *
     922             :  * We need to set these up, so that from the moment we start the provision,
     923             :  * the defaultObjectCategory links are set up correctly.
     924             :  */
     925         325 : int dsdb_schema_fill_extended_dn(struct ldb_context *ldb, struct dsdb_schema *schema)
     926             : {
     927             :         struct dsdb_class *cur;
     928             :         const struct dsdb_class *target_class;
     929       85135 :         for (cur = schema->classes; cur; cur = cur->next) {
     930             :                 const struct ldb_val *rdn;
     931             :                 struct ldb_val guid;
     932             :                 NTSTATUS status;
     933             :                 int ret;
     934       84810 :                 struct ldb_dn *dn = ldb_dn_new(NULL, ldb, cur->defaultObjectCategory);
     935             : 
     936       84810 :                 if (!dn) {
     937           0 :                         return LDB_ERR_INVALID_DN_SYNTAX;
     938             :                 }
     939       84810 :                 rdn = ldb_dn_get_component_val(dn, 0);
     940       84810 :                 if (!rdn) {
     941           0 :                         talloc_free(dn);
     942           0 :                         return LDB_ERR_INVALID_DN_SYNTAX;
     943             :                 }
     944       84810 :                 target_class = dsdb_class_by_cn_ldb_val(schema, rdn);
     945       84810 :                 if (!target_class) {
     946           0 :                         talloc_free(dn);
     947           0 :                         return LDB_ERR_CONSTRAINT_VIOLATION;
     948             :                 }
     949             : 
     950       84810 :                 status = GUID_to_ndr_blob(&target_class->objectGUID, dn, &guid);
     951       84810 :                 if (!NT_STATUS_IS_OK(status)) {
     952           0 :                         talloc_free(dn);
     953           0 :                         return ldb_operr(ldb);
     954             :                 }
     955       84810 :                 ret = ldb_dn_set_extended_component(dn, "GUID", &guid);
     956       84810 :                 if (ret != LDB_SUCCESS) {
     957           0 :                         ret = ldb_error(ldb, ret, "Could not set GUID");
     958           0 :                         talloc_free(dn);
     959           0 :                         return ret;
     960             :                 }
     961             : 
     962       84810 :                 cur->defaultObjectCategory = ldb_dn_get_extended_linearized(cur, dn, 1);
     963       84810 :                 talloc_free(dn);
     964             :         }
     965         325 :         return LDB_SUCCESS;
     966             : }
     967             : 
     968             : /**
     969             :  * @brief Add a new element to the schema and checks if it's a duplicate
     970             :  *
     971             :  * This function will add a new element to the schema and checks for existing
     972             :  * duplicates.
     973             :  *
     974             :  * @param[in]  ldb                A pointer to an LDB context
     975             :  *
     976             :  * @param[in]  schema             A pointer to the dsdb_schema where the element
     977             :  *                                will be added.
     978             :  *
     979             :  * @param[in]  msg                The ldb_message object representing the element
     980             :  *                                to add.
     981             :  *
     982             :  * @param[in]  checkdups          A boolean to indicate if checks for duplicates
     983             :  *                                should be done.
     984             :  *
     985             :  * @return                        A WERROR code
     986             :  */
     987    35682313 : WERROR dsdb_schema_set_el_from_ldb_msg_dups(struct ldb_context *ldb, struct dsdb_schema *schema,
     988             :                                             struct ldb_message *msg, bool checkdups)
     989             : {
     990             :         const char* tstring;
     991             :         time_t ts;
     992    35682313 :         tstring = ldb_msg_find_attr_as_string(msg, "whenChanged", NULL);
     993             :         /* keep a trace of the ts of the most recently changed object */
     994    35682313 :         if (tstring) {
     995       15090 :                 ts = ldb_string_to_time(tstring);
     996       15090 :                 if (ts > schema->ts_last_change) {
     997          47 :                         schema->ts_last_change = ts;
     998             :                 }
     999             :         }
    1000    35682313 :         if (samdb_find_attribute(ldb, msg,
    1001             :                                  "objectclass", "attributeSchema") != NULL) {
    1002             : 
    1003    30043077 :                 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, checkdups);
    1004     5639236 :         } else if (samdb_find_attribute(ldb, msg,
    1005             :                                  "objectclass", "classSchema") != NULL) {
    1006     5639021 :                 return dsdb_set_class_from_ldb_dups(schema, msg, checkdups);
    1007             :         }
    1008             :         /* Don't fail on things not classes or attributes */
    1009         215 :         return WERR_OK;
    1010             : }
    1011             : 
    1012    35557470 : WERROR dsdb_schema_set_el_from_ldb_msg(struct ldb_context *ldb,
    1013             :                                        struct dsdb_schema *schema,
    1014             :                                        struct ldb_message *msg)
    1015             : {
    1016    35557470 :         return dsdb_schema_set_el_from_ldb_msg_dups(ldb, schema, msg, false);
    1017             : }
    1018             : 
    1019             : /**
    1020             :  * Rather than read a schema from the LDB itself, read it from an ldif
    1021             :  * file.  This allows schema to be loaded and used while adding the
    1022             :  * schema itself to the directory.
    1023             :  *
    1024             :  * Should be called with a transaction (or failing that, have no concurrent
    1025             :  * access while called).
    1026             :  */
    1027             : 
    1028         325 : WERROR dsdb_set_schema_from_ldif(struct ldb_context *ldb,
    1029             :                                  const char *pf, const char *df,
    1030             :                                  const char *dn)
    1031             : {
    1032             :         struct ldb_ldif *ldif;
    1033             :         struct ldb_message *msg;
    1034             :         TALLOC_CTX *mem_ctx;
    1035             :         WERROR status;
    1036             :         int ret;
    1037             :         struct dsdb_schema *schema;
    1038             :         const struct ldb_val *prefix_val;
    1039             :         const struct ldb_val *info_val;
    1040             :         struct ldb_val info_val_default;
    1041             : 
    1042             : 
    1043         325 :         mem_ctx = talloc_new(ldb);
    1044         325 :         if (!mem_ctx) {
    1045           0 :                 goto nomem;
    1046             :         }
    1047             : 
    1048         325 :         schema = dsdb_new_schema(mem_ctx);
    1049         325 :         if (!schema) {
    1050           0 :                 goto nomem;
    1051             :         }
    1052         325 :         schema->fsmo.we_are_master = true;
    1053         325 :         schema->fsmo.update_allowed = true;
    1054         325 :         schema->fsmo.master_dn = ldb_dn_new(schema, ldb, "@PROVISION_SCHEMA_MASTER");
    1055         325 :         if (!schema->fsmo.master_dn) {
    1056           0 :                 goto nomem;
    1057             :         }
    1058             : 
    1059             :         /*
    1060             :          * load the prefixMap attribute from pf
    1061             :          */
    1062         325 :         ldif = ldb_ldif_read_string(ldb, &pf);
    1063         325 :         if (!ldif) {
    1064           0 :                 status = WERR_INVALID_PARAMETER;
    1065           0 :                 goto failed;
    1066             :         }
    1067         325 :         talloc_steal(mem_ctx, ldif);
    1068             : 
    1069         325 :         ret = ldb_msg_normalize(ldb, mem_ctx, ldif->msg, &msg);
    1070         325 :         if (ret != LDB_SUCCESS) {
    1071           0 :                 goto nomem;
    1072             :         }
    1073         325 :         talloc_free(ldif);
    1074             : 
    1075         325 :         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
    1076         325 :         if (!prefix_val) {
    1077           0 :                 status = WERR_INVALID_PARAMETER;
    1078           0 :                 goto failed;
    1079             :         }
    1080             : 
    1081         325 :         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
    1082         325 :         if (!info_val) {
    1083         325 :                 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
    1084         325 :                 W_ERROR_NOT_OK_GOTO(status, failed);
    1085         325 :                 info_val = &info_val_default;
    1086             :         }
    1087             : 
    1088         325 :         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
    1089         325 :         if (!W_ERROR_IS_OK(status)) {
    1090           0 :                 DEBUG(0,("ERROR: dsdb_load_oid_mappings_ldb() failed with %s\n", win_errstr(status)));
    1091           0 :                 goto failed;
    1092             :         }
    1093             : 
    1094         325 :         schema->ts_last_change = 0;
    1095             :         /* load the attribute and class definitions out of df */
    1096      558619 :         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
    1097      558294 :                 talloc_steal(mem_ctx, ldif);
    1098             : 
    1099      558294 :                 ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &msg);
    1100      558294 :                 if (ret != LDB_SUCCESS) {
    1101           0 :                         goto nomem;
    1102             :                 }
    1103             : 
    1104      558294 :                 status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, msg);
    1105      558294 :                 talloc_free(ldif);
    1106      558294 :                 if (!W_ERROR_IS_OK(status)) {
    1107           0 :                         goto failed;
    1108             :                 }
    1109             :         }
    1110             : 
    1111             :         /*
    1112             :          * TODO We may need a transaction here, otherwise this causes races.
    1113             :          *
    1114             :          * To do so may require an ldb_in_transaction function. In the
    1115             :          * meantime, assume that this is always called with a transaction or in
    1116             :          * isolation.
    1117             :          */
    1118         325 :         ret = dsdb_set_schema(ldb, schema, SCHEMA_WRITE);
    1119         325 :         if (ret != LDB_SUCCESS) {
    1120           0 :                 status = WERR_FOOBAR;
    1121           0 :                 DEBUG(0,("ERROR: dsdb_set_schema() failed with %s / %s\n",
    1122             :                          ldb_strerror(ret), ldb_errstring(ldb)));
    1123           0 :                 goto failed;
    1124             :         }
    1125             : 
    1126         325 :         ret = dsdb_schema_fill_extended_dn(ldb, schema);
    1127         325 :         if (ret != LDB_SUCCESS) {
    1128           0 :                 status = WERR_FOOBAR;
    1129           0 :                 goto failed;
    1130             :         }
    1131             : 
    1132         325 :         goto done;
    1133             : 
    1134           0 : nomem:
    1135           0 :         status = WERR_NOT_ENOUGH_MEMORY;
    1136           0 : failed:
    1137         325 : done:
    1138         325 :         talloc_free(mem_ctx);
    1139         325 :         return status;
    1140             : }

Generated by: LCOV version 1.14