LCOV - code coverage report
Current view: top level - source4/dsdb/schema - schema_set.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 387 483 80.1 %
Date: 2024-06-13 04:01:37 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   648878026 : const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb,
      44             :                                                                    void *private_data,
      45             :                                                                    const char *name)
      46             : {
      47   648878026 :         struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
      48   648878026 :         const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
      49   648878026 :         if (a == NULL) {
      50             :                 /* this will fall back to ldb internal handling */
      51     2229590 :                 return NULL;
      52             :         }
      53   646648436 :         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      147495 : int dsdb_schema_set_indices_and_attributes(struct ldb_context *ldb,
      63             :                                            struct dsdb_schema *schema,
      64             :                                            enum schema_set_enum mode)
      65             : {
      66      147495 :         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      110206 :         struct loadparm_context *lp_ctx =
      76      147495 :                 talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
      77             :                                 struct loadparm_context);
      78      147495 :         bool guid_indexing = true;
      79      147495 :         bool declare_ordered_integer_in_attributes = true;
      80             :         uint32_t pack_format_override;
      81      147495 :         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      147179 :                 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      147179 :                 pack_format_override =
     108      147179 :                         (intptr_t)ldb_get_opaque(ldb, "pack_format_override");
     109      147179 :                 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      147495 :         ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
     116      147495 :         ldb_schema_set_override_indexlist(ldb, true);
     117      147495 :         if (guid_indexing) {
     118      147495 :                 ldb_schema_set_override_GUID_index(ldb, "objectGUID", "GUID");
     119             :         }
     120             : 
     121      147495 :         if (mode == SCHEMA_MEMORY_ONLY) {
     122      142661 :                 return ret;
     123             :         }
     124             : 
     125        4834 :         mem_ctx = talloc_new(ldb);
     126        4834 :         if (!mem_ctx) {
     127           0 :                 return ldb_oom(ldb);
     128             :         }
     129             : 
     130        4834 :         msg = ldb_msg_new(mem_ctx);
     131        4834 :         if (!msg) {
     132           0 :                 ldb_oom(ldb);
     133           0 :                 goto op_error;
     134             :         }
     135        4834 :         msg_idx = ldb_msg_new(mem_ctx);
     136        4834 :         if (!msg_idx) {
     137           0 :                 ldb_oom(ldb);
     138           0 :                 goto op_error;
     139             :         }
     140        4834 :         msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
     141        4834 :         if (!msg->dn) {
     142           0 :                 ldb_oom(ldb);
     143           0 :                 goto op_error;
     144             :         }
     145        4834 :         msg_idx->dn = ldb_dn_new(msg_idx, ldb, "@INDEXLIST");
     146        4834 :         if (!msg_idx->dn) {
     147           0 :                 ldb_oom(ldb);
     148           0 :                 goto op_error;
     149             :         }
     150             : 
     151        4834 :         ret = ldb_msg_add_string(msg_idx, "@IDXONE", "1");
     152        4834 :         if (ret != LDB_SUCCESS) {
     153           0 :                 goto op_error;
     154             :         }
     155             : 
     156        4834 :         if (guid_indexing) {
     157        4834 :                 ret = ldb_msg_add_string(msg_idx, "@IDXGUID", "objectGUID");
     158        4834 :                 if (ret != LDB_SUCCESS) {
     159           0 :                         goto op_error;
     160             :                 }
     161             : 
     162        4834 :                 ret = ldb_msg_add_string(msg_idx, "@IDX_DN_GUID", "GUID");
     163        4834 :                 if (ret != LDB_SUCCESS) {
     164           0 :                         goto op_error;
     165             :                 }
     166             :         }
     167             : 
     168        4834 :         ret = ldb_msg_add_string(msg_idx, "@SAMDB_INDEXING_VERSION", SAMDB_INDEXING_VERSION);
     169        4834 :         if (ret != LDB_SUCCESS) {
     170           0 :                 goto op_error;
     171             :         }
     172             : 
     173        4834 :         ret = ldb_msg_add_string(msg_idx, SAMBA_FEATURES_SUPPORTED_FLAG, "1");
     174        4834 :         if (ret != LDB_SUCCESS) {
     175           0 :                 goto op_error;
     176             :         }
     177             : 
     178     6966555 :         for (attr = schema->attributes; attr; attr = attr->next) {
     179     6961721 :                 const char *syntax = attr->syntax->ldb_syntax;
     180             : 
     181     6961721 :                 if (!syntax) {
     182     3894824 :                         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     6961721 :                 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
     191           0 :                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
     192     6961721 :                 } else if (strcmp(syntax, LDB_SYNTAX_ORDERED_INTEGER) == 0) {
     193      453228 :                         if (declare_ordered_integer_in_attributes &&
     194             :                             guid_indexing) {
     195             :                                 /*
     196             :                                  * The normal production case
     197             :                                  */
     198      453228 :                                 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     6508493 :                 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
     217     2556167 :                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName,
     218             :                                                  "CASE_INSENSITIVE");
     219             :                 }
     220     6961721 :                 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     7584612 :                 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX &&
     230      699195 :                     !(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      699195 :                         if (strcmp(syntax, LDB_SYNTAX_ORDERED_INTEGER) == 0
     237       39250 :                             && !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      699195 :                                 ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
     251      699195 :                                 if (ret != LDB_SUCCESS) {
     252           0 :                                         break;
     253             :                                 }
     254             :                         }
     255             :                 }
     256             :         }
     257             : 
     258        4834 :         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        4834 :         ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL,
     268             :                          NULL);
     269        4834 :         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
     270         147 :                 if (mode == SCHEMA_COMPARE) {
     271             :                         /* We are probably not in a transaction */
     272           0 :                         goto wrong_mode;
     273             :                 }
     274         147 :                 ret = ldb_add(ldb, msg);
     275        4687 :         } else if (ret != LDB_SUCCESS) {
     276        4278 :         } 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        4272 :                 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
     285             : 
     286        4272 :                 ret = ldb_msg_difference(ldb, mem_ctx,
     287        4272 :                                          res->msgs[0], msg, &mod_msg);
     288        4272 :                 if (ret != LDB_SUCCESS) {
     289           0 :                         goto op_error;
     290             :                 }
     291        4272 :                 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         161 :                         if (mode == SCHEMA_COMPARE) {
     298             :                                 /* We are probably not in a transaction */
     299          23 :                                 goto wrong_mode;
     300             :                         }
     301         138 :                         ret = dsdb_modify(ldb, mod_msg, 0);
     302             :                 }
     303        4249 :                 talloc_free(mod_msg);
     304             :         }
     305             : 
     306        4811 :         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         409 :                 ret = LDB_SUCCESS;
     309             :         }
     310        4811 :         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        4811 :         ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE,
     320             :                          NULL, NULL);
     321        4811 :         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
     322         147 :                 if (mode == SCHEMA_COMPARE) {
     323             :                         /* We are probably not in a transaction */
     324           0 :                         goto wrong_mode;
     325             :                 }
     326         147 :                 ret = ldb_add(ldb, msg_idx);
     327        4664 :         } else if (ret != LDB_SUCCESS) {
     328        4255 :         } 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        4249 :                 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
     337             : 
     338        4249 :                 ret = ldb_msg_difference(ldb, mem_ctx,
     339        4249 :                                          res_idx->msgs[0], msg_idx, &mod_msg);
     340        4249 :                 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        4249 :                 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        4249 :                 } 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        4247 :                 talloc_free(mod_msg);
     377             :         }
     378        4809 :         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         409 :                 ret = LDB_SUCCESS;
     381             :         }
     382             : 
     383        4809 :         if (ret != LDB_SUCCESS) {
     384           0 :                 DBG_ERR("Failed to set schema into @INDEXLIST: %s\n",
     385             :                         ldb_errstring(ldb));
     386             :         }
     387             : 
     388        4809 :         talloc_free(mem_ctx);
     389        4809 :         return ret;
     390             : 
     391           0 : op_error:
     392           0 :         talloc_free(mem_ctx);
     393           0 :         return ldb_operr(ldb);
     394             : 
     395          25 : wrong_mode:
     396          25 :         talloc_free(mem_ctx);
     397          25 :         return LDB_ERR_BUSY;
     398             : }
     399             : 
     400             : 
     401             : /*
     402             :   create extra attribute shortcuts
     403             :  */
     404       19927 : 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    27913282 :         for (attribute=schema->attributes; attribute; attribute=attribute->next) {
     410    27893355 :                 attribute->dn_format = dsdb_dn_oid_to_format(attribute->syntax->ldap_oid);
     411             : 
     412    27893355 :                 if (attribute->dn_format == DSDB_INVALID_DN) {
     413    24126256 :                         attribute->one_way_link = false;
     414    24126256 :                         continue;
     415             :                 }
     416             : 
     417             :                 /* these are not considered to be one way links for
     418             :                    the purpose of DN link fixups */
     419     6590568 :                 if (ldb_attr_cmp("distinguishedName", attribute->lDAPDisplayName) == 0 ||
     420     3747172 :                     ldb_attr_cmp("objectCategory", attribute->lDAPDisplayName) == 0) {
     421       39854 :                         attribute->one_way_link = false;
     422       39854 :                         continue;
     423             :                 }
     424             : 
     425     3727245 :                 if (attribute->linkID == 0) {
     426     1479929 :                         attribute->one_way_link = true;
     427     1479929 :                         continue;
     428             :                 }
     429             :                 /* handle attributes with a linkID but no backlink */
     430     3548234 :                 if ((attribute->linkID & 1) == 0 &&
     431     1300918 :                     dsdb_attribute_by_linkID(schema, attribute->linkID + 1) == NULL) {
     432      354520 :                         attribute->one_way_link = true;
     433      354520 :                         continue;
     434             :                 }
     435     1892796 :                 attribute->one_way_link = false;
     436             :         }
     437       19927 : }
     438             : 
     439   526431427 : static int uint32_cmp(uint32_t c1, uint32_t c2)
     440             : {
     441   526431427 :         if (c1 == c2) return 0;
     442   398173805 :         return c1 > c2 ? 1 : -1;
     443             : }
     444             : 
     445    35141408 : static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
     446             : {
     447    35141408 :         return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
     448             : }
     449    35202217 : static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
     450             : {
     451    35202217 :         return uint32_cmp((*c1)->governsID_id, (*c2)->governsID_id);
     452             : }
     453    35228682 : static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
     454             : {
     455    35228682 :         return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
     456             : }
     457    35160134 : static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
     458             : {
     459    35160134 :         return strcasecmp((*c1)->cn, (*c2)->cn);
     460             : }
     461             : 
     462   255539007 : static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     463             : {
     464   255539007 :         return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
     465             : }
     466   256738606 : static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     467             : {
     468   256738606 :         return uint32_cmp((*a1)->attributeID_id, (*a2)->attributeID_id);
     469             : }
     470      606425 : static int dsdb_compare_attribute_by_msDS_IntId(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     471             : {
     472      606425 :         return uint32_cmp((*a1)->msDS_IntId, (*a2)->msDS_IntId);
     473             : }
     474   256661341 : static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     475             : {
     476   256661341 :         return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
     477             : }
     478   233884179 : static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
     479             : {
     480   233884179 :         return uint32_cmp((*a1)->linkID, (*a2)->linkID);
     481             : }
     482             : 
     483             : /**
     484             :  * Clean up Classes and Attributes accessor arrays
     485             :  */
     486       19927 : static void dsdb_sorted_accessors_free(struct dsdb_schema *schema)
     487             : {
     488             :         /* free classes accessors */
     489       19927 :         TALLOC_FREE(schema->classes_by_lDAPDisplayName);
     490       19927 :         TALLOC_FREE(schema->classes_by_governsID_id);
     491       19927 :         TALLOC_FREE(schema->classes_by_governsID_oid);
     492       19927 :         TALLOC_FREE(schema->classes_by_cn);
     493             :         /* free attribute accessors */
     494       19927 :         TALLOC_FREE(schema->attributes_by_lDAPDisplayName);
     495       19927 :         TALLOC_FREE(schema->attributes_by_attributeID_id);
     496       19927 :         TALLOC_FREE(schema->attributes_by_msDS_IntId);
     497       19927 :         TALLOC_FREE(schema->attributes_by_attributeID_oid);
     498       19927 :         TALLOC_FREE(schema->attributes_by_linkID);
     499       19927 : }
     500             : 
     501             : /*
     502             :   create the sorted accessor arrays for the schema
     503             :  */
     504       19927 : 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       19927 :         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       19927 :         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       19927 :         TALLOC_FREE(schema->classes_to_remove);
     523       19927 :         schema->classes_to_remove_size = 0;
     524       19927 :         TALLOC_FREE(schema->attributes_to_remove);
     525       19927 :         schema->attributes_to_remove_size = 0;
     526             : 
     527             :         /* free all caches */
     528       19927 :         dsdb_sorted_accessors_free(schema);
     529             : 
     530             :         /* count the classes */
     531     1356781 :         for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
     532       19927 :         schema->num_classes = i;
     533             : 
     534             :         /* setup classes_by_* */
     535       19927 :         schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
     536       19927 :         schema->classes_by_governsID_id    = talloc_array(schema, struct dsdb_class *, i);
     537       19927 :         schema->classes_by_governsID_oid   = talloc_array(schema, struct dsdb_class *, i);
     538       19927 :         schema->classes_by_cn              = talloc_array(schema, struct dsdb_class *, i);
     539       34601 :         if (schema->classes_by_lDAPDisplayName == NULL ||
     540       34601 :             schema->classes_by_governsID_id == NULL ||
     541       34601 :             schema->classes_by_governsID_oid == NULL ||
     542       19927 :             schema->classes_by_cn == NULL) {
     543           0 :                 goto failed;
     544             :         }
     545             : 
     546     5223345 :         for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
     547     5203418 :                 schema->classes_by_lDAPDisplayName[i] = cur;
     548     5203418 :                 schema->classes_by_governsID_id[i]    = cur;
     549     5203418 :                 schema->classes_by_governsID_oid[i]   = cur;
     550     5203418 :                 schema->classes_by_cn[i]              = cur;
     551             :         }
     552             : 
     553             :         /* sort the arrays */
     554       19927 :         TYPESAFE_QSORT(schema->classes_by_lDAPDisplayName, schema->num_classes, dsdb_compare_class_by_lDAPDisplayName);
     555       19927 :         TYPESAFE_QSORT(schema->classes_by_governsID_id, schema->num_classes, dsdb_compare_class_by_governsID_id);
     556       19927 :         TYPESAFE_QSORT(schema->classes_by_governsID_oid, schema->num_classes, dsdb_compare_class_by_governsID_oid);
     557       19927 :         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       19927 :         num_int_id = 0;
     564    27913282 :         for (i=0, a=schema->attributes; a; i++, a=a->next) {
     565    27893355 :                 if (a->msDS_IntId != 0) {
     566      186701 :                         num_int_id++;
     567             :                 }
     568             :         }
     569       19927 :         schema->num_attributes = i;
     570       19927 :         schema->num_int_id_attr = num_int_id;
     571             : 
     572             :         /* setup attributes_by_* */
     573       19927 :         schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
     574       19927 :         schema->attributes_by_attributeID_id    = talloc_array(schema, struct dsdb_attribute *, i);
     575       19927 :         schema->attributes_by_msDS_IntId        = talloc_array(schema,
     576             :                                                                struct dsdb_attribute *, num_int_id);
     577       19927 :         schema->attributes_by_attributeID_oid   = talloc_array(schema, struct dsdb_attribute *, i);
     578       19927 :         schema->attributes_by_linkID              = talloc_array(schema, struct dsdb_attribute *, i);
     579       34601 :         if (schema->attributes_by_lDAPDisplayName == NULL ||
     580       34601 :             schema->attributes_by_attributeID_id == NULL ||
     581       34601 :             schema->attributes_by_msDS_IntId == NULL ||
     582       34601 :             schema->attributes_by_attributeID_oid == NULL ||
     583       19927 :             schema->attributes_by_linkID == NULL) {
     584           0 :                 goto failed;
     585             :         }
     586             : 
     587       19927 :         num_int_id = 0;
     588    27913282 :         for (i=0, a=schema->attributes; a; i++, a=a->next) {
     589    27893355 :                 schema->attributes_by_lDAPDisplayName[i] = a;
     590    27893355 :                 schema->attributes_by_attributeID_id[i]    = a;
     591    27893355 :                 schema->attributes_by_attributeID_oid[i]   = a;
     592    27893355 :                 schema->attributes_by_linkID[i]          = a;
     593             :                 /* append attr-by-msDS-IntId values */
     594    27893355 :                 if (a->msDS_IntId != 0) {
     595      186701 :                         schema->attributes_by_msDS_IntId[num_int_id] = a;
     596      186701 :                         num_int_id++;
     597             :                 }
     598             :         }
     599       19927 :         SMB_ASSERT(num_int_id == schema->num_int_id_attr);
     600             : 
     601             :         /* sort the arrays */
     602       19927 :         TYPESAFE_QSORT(schema->attributes_by_lDAPDisplayName, schema->num_attributes, dsdb_compare_attribute_by_lDAPDisplayName);
     603       19927 :         TYPESAFE_QSORT(schema->attributes_by_attributeID_id, schema->num_attributes, dsdb_compare_attribute_by_attributeID_id);
     604       19927 :         TYPESAFE_QSORT(schema->attributes_by_msDS_IntId, schema->num_int_id_attr, dsdb_compare_attribute_by_msDS_IntId);
     605       19927 :         TYPESAFE_QSORT(schema->attributes_by_attributeID_oid, schema->num_attributes, dsdb_compare_attribute_by_attributeID_oid);
     606       19927 :         TYPESAFE_QSORT(schema->attributes_by_linkID, schema->num_attributes, dsdb_compare_attribute_by_linkID);
     607             : 
     608       19927 :         dsdb_setup_attribute_shortcuts(ldb, schema);
     609             : 
     610       19927 :         ret = schema_fill_constructed(schema);
     611       19927 :         if (ret != LDB_SUCCESS) {
     612           0 :                 dsdb_sorted_accessors_free(schema);
     613           0 :                 return ret;
     614             :         }
     615             : 
     616       19927 :         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      107866 : int dsdb_set_schema_refresh_function(struct ldb_context *ldb,
     628             :                                      dsdb_schema_refresh_fn refresh_fn,
     629             :                                      struct ldb_module *module)
     630             : {
     631      107866 :         int ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", refresh_fn);
     632      107866 :         if (ret != LDB_SUCCESS) {
     633           0 :                 return ret;
     634             :         }
     635             : 
     636      107866 :         ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn_private_data", module);
     637      107866 :         if (ret != LDB_SUCCESS) {
     638           0 :                 return ret;
     639             :         }
     640      107866 :         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       19626 : 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       19626 :         ret = dsdb_setup_sorted_accessors(ldb, schema);
     655       19626 :         if (ret != LDB_SUCCESS) {
     656           0 :                 return ret;
     657             :         }
     658             : 
     659       19626 :         old_schema = ldb_get_opaque(ldb, "dsdb_schema");
     660             : 
     661       19626 :         ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
     662       19626 :         if (ret != LDB_SUCCESS) {
     663           0 :                 return ret;
     664             :         }
     665             : 
     666       19626 :         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
     667       19626 :         if (ret != LDB_SUCCESS) {
     668           0 :                 return ret;
     669             :         }
     670             : 
     671       19626 :         talloc_steal(ldb, schema);
     672             : 
     673             :         /* Set the new attributes based on the new schema */
     674       19626 :         ret = dsdb_schema_set_indices_and_attributes(ldb, schema,
     675             :                                                      write_indices_and_attributes);
     676       19626 :         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       19626 :         if (old_schema != schema) {
     685       19626 :                 talloc_unlink(ldb, old_schema);
     686             :         }
     687             : 
     688       19626 :         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         522 : 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         522 :         void *schema_parent = NULL;
     709             :         bool is_already_parent;
     710             :         struct dsdb_schema *old_schema;
     711         522 :         old_schema = ldb_get_opaque(ldb, "dsdb_schema");
     712         522 :         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
     713         522 :         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         522 :         talloc_unlink(ldb, old_schema);
     720             : 
     721             :         /* Reference schema on ldb if it wasn't done already */
     722         522 :         schema_parent = talloc_parent(schema);
     723         522 :         is_already_parent = (schema_parent == ldb);
     724         522 :         if (!is_already_parent) {
     725         522 :                 ptr = talloc_reference(ldb, schema);
     726         522 :                 if (ptr == NULL) {
     727           0 :                         return ldb_oom(ldb);
     728             :                 }
     729             :         }
     730             : 
     731             :         /* Make this ldb use local schema preferably */
     732         522 :         ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
     733         522 :         if (ret != LDB_SUCCESS) {
     734           0 :                 return ret;
     735             :         }
     736             : 
     737         522 :         ret = ldb_set_opaque(ldb, "dsdb_refresh_fn", NULL);
     738         522 :         if (ret != LDB_SUCCESS) {
     739           0 :                 return ret;
     740             :         }
     741             : 
     742         522 :         ret = ldb_set_opaque(ldb, "dsdb_refresh_fn_private_data", NULL);
     743         522 :         if (ret != LDB_SUCCESS) {
     744           0 :                 return ret;
     745             :         }
     746             : 
     747         522 :         ret = dsdb_schema_set_indices_and_attributes(ldb, schema, write_indices_and_attributes);
     748         522 :         if (ret != LDB_SUCCESS) {
     749           0 :                 return ret;
     750             :         }
     751             : 
     752         522 :         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      141507 : int dsdb_set_global_schema(struct ldb_context *ldb)
     759             : {
     760             :         int ret;
     761      141507 :         void *use_global_schema = (void *)1;
     762             :         void *ptr;
     763      141507 :         struct dsdb_schema *old_schema = ldb_get_opaque(ldb, "dsdb_schema");
     764             : 
     765      141507 :         ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", use_global_schema);
     766      141507 :         if (ret != LDB_SUCCESS) {
     767           0 :                 return ret;
     768             :         }
     769             : 
     770      141507 :         if (global_schema == NULL) {
     771       18356 :                 return LDB_SUCCESS;
     772             :         }
     773             : 
     774             :         /* Remove any pointer to a previous schema */
     775      123151 :         ret = ldb_set_opaque(ldb, "dsdb_schema", NULL);
     776      123151 :         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      123151 :         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      123151 :         ret = dsdb_schema_set_indices_and_attributes(ldb, global_schema, SCHEMA_MEMORY_ONLY);
     787      123151 :         if (ret == LDB_SUCCESS) {
     788      123151 :                 void *schema_parent = talloc_parent(global_schema);
     789      123151 :                 bool is_already_parent =
     790             :                         (schema_parent == ldb);
     791      123151 :                 if (!is_already_parent) {
     792      123151 :                         ptr = talloc_reference(ldb, global_schema);
     793      123151 :                         if (ptr == NULL) {
     794           0 :                                 return ldb_oom(ldb);
     795             :                         }
     796      123151 :                         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
     797             :                 }
     798             :         }
     799             : 
     800      123151 :         return ret;
     801             : }
     802             : 
     803   120343788 : bool dsdb_uses_global_schema(struct ldb_context *ldb)
     804             : {
     805   120343788 :         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   120239409 : struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb, TALLOC_CTX *reference_ctx)
     815             : {
     816             :         const void *p;
     817   120239409 :         struct dsdb_schema *schema_out = NULL;
     818   120239409 :         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   120239409 :         TALLOC_CTX *tmp_ctx = talloc_new(reference_ctx);
     823   120239409 :         if (tmp_ctx == NULL) {
     824           0 :                 return NULL;
     825             :         }
     826             : 
     827             :         /* see if we have a cached copy */
     828   120239409 :         use_global_schema = dsdb_uses_global_schema(ldb);
     829   120239409 :         if (use_global_schema) {
     830   100642648 :                 schema_in = global_schema;
     831             :         } else {
     832    19596761 :                 p = ldb_get_opaque(ldb, "dsdb_schema");
     833    19596761 :                 if (p != NULL) {
     834    19511082 :                         schema_in = talloc_get_type_abort(p, struct dsdb_schema);
     835             :                 }
     836             :         }
     837             : 
     838   120239409 :         refresh_fn = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn");
     839   120239409 :         if (refresh_fn) {
     840   101335101 :                 loaded_from_module = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn_private_data");
     841             : 
     842   101335101 :                 SMB_ASSERT(loaded_from_module && (ldb_module_get_ctx(loaded_from_module) == ldb));
     843             :         }
     844             : 
     845   120239409 :         if (refresh_fn) {
     846             :                 /* We need to guard against recursive calls here */
     847   101335101 :                 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   101335101 :                         schema_out = refresh_fn(loaded_from_module,
     852             :                                                 ldb_get_event_context(ldb),
     853             :                                                 schema_in,
     854             :                                                 use_global_schema);
     855             :                 }
     856   101335101 :                 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   101335101 :                 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    18904308 :                 schema_out = schema_in;
     867             :         }
     868             : 
     869             :         /* This removes the extra reference above */
     870   120239409 :         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   120239409 :         if (reference_ctx == NULL) {
     883    55742602 :                 return schema_out;
     884             :         } else {
     885    64496807 :                 void *schema_parent = talloc_parent(schema_out);
     886    64496807 :                 bool is_already_parent =
     887             :                         schema_parent == reference_ctx;
     888    64496807 :                 if (is_already_parent) {
     889           0 :                         return schema_out;
     890             :                 } else {
     891    64496807 :                         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       15736 : void dsdb_make_schema_global(struct ldb_context *ldb, struct dsdb_schema *schema)
     902             : {
     903       15736 :         if (!schema) {
     904           0 :                 return;
     905             :         }
     906             : 
     907       15736 :         if (global_schema) {
     908       14008 :                 talloc_unlink(NULL, global_schema);
     909             :         }
     910             : 
     911             :         /* we want the schema to be around permanently */
     912       15736 :         talloc_reparent(ldb, NULL, schema);
     913       15736 :         global_schema = schema;
     914             : 
     915             :         /* This calls the talloc_reference() of the global schema back onto the ldb */
     916       15736 :         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         319 : 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      155875 :         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       83226 :                 struct ldb_dn *dn = ldb_dn_new(NULL, ldb, cur->defaultObjectCategory);
     935             : 
     936       83226 :                 if (!dn) {
     937           0 :                         return LDB_ERR_INVALID_DN_SYNTAX;
     938             :                 }
     939       83226 :                 rdn = ldb_dn_get_component_val(dn, 0);
     940       83226 :                 if (!rdn) {
     941           0 :                         talloc_free(dn);
     942           0 :                         return LDB_ERR_INVALID_DN_SYNTAX;
     943             :                 }
     944       83226 :                 target_class = dsdb_class_by_cn_ldb_val(schema, rdn);
     945       83226 :                 if (!target_class) {
     946           0 :                         talloc_free(dn);
     947           0 :                         return LDB_ERR_CONSTRAINT_VIOLATION;
     948             :                 }
     949             : 
     950       83226 :                 status = GUID_to_ndr_blob(&target_class->objectGUID, dn, &guid);
     951       83226 :                 if (!NT_STATUS_IS_OK(status)) {
     952           0 :                         talloc_free(dn);
     953           0 :                         return ldb_operr(ldb);
     954             :                 }
     955       83226 :                 ret = ldb_dn_set_extended_component(dn, "GUID", &guid);
     956       83226 :                 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       83226 :                 cur->defaultObjectCategory = ldb_dn_get_extended_linearized(cur, dn, 1);
     963       83226 :                 talloc_free(dn);
     964             :         }
     965         319 :         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    32684508 : 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    32684508 :         tstring = ldb_msg_find_attr_as_string(msg, "whenChanged", NULL);
     993             :         /* keep a trace of the ts of the most recently changed object */
     994    32684508 :         if (tstring) {
     995       15090 :                 ts = ldb_string_to_time(tstring);
     996       15090 :                 if (ts > schema->ts_last_change) {
     997          39 :                         schema->ts_last_change = ts;
     998             :                 }
     999             :         }
    1000    32684508 :         if (samdb_find_attribute(ldb, msg,
    1001             :                                  "objectclass", "attributeSchema") != NULL) {
    1002             : 
    1003    27544412 :                 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, checkdups);
    1004     5140096 :         } else if (samdb_find_attribute(ldb, msg,
    1005             :                                  "objectclass", "classSchema") != NULL) {
    1006     5139876 :                 return dsdb_set_class_from_ldb_dups(schema, msg, checkdups);
    1007             :         }
    1008             :         /* Don't fail on things not classes or attributes */
    1009         220 :         return WERR_OK;
    1010             : }
    1011             : 
    1012    32563134 : WERROR dsdb_schema_set_el_from_ldb_msg(struct ldb_context *ldb,
    1013             :                                        struct dsdb_schema *schema,
    1014             :                                        struct ldb_message *msg)
    1015             : {
    1016    32563134 :         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         319 : 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         319 :         mem_ctx = talloc_new(ldb);
    1044         319 :         if (!mem_ctx) {
    1045           0 :                 goto nomem;
    1046             :         }
    1047             : 
    1048         319 :         schema = dsdb_new_schema(mem_ctx);
    1049         319 :         if (!schema) {
    1050           0 :                 goto nomem;
    1051             :         }
    1052         319 :         schema->fsmo.we_are_master = true;
    1053         319 :         schema->fsmo.update_allowed = true;
    1054         319 :         schema->fsmo.master_dn = ldb_dn_new(schema, ldb, "@PROVISION_SCHEMA_MASTER");
    1055         319 :         if (!schema->fsmo.master_dn) {
    1056           0 :                 goto nomem;
    1057             :         }
    1058             : 
    1059             :         /*
    1060             :          * load the prefixMap attribute from pf
    1061             :          */
    1062         319 :         ldif = ldb_ldif_read_string(ldb, &pf);
    1063         319 :         if (!ldif) {
    1064           0 :                 status = WERR_INVALID_PARAMETER;
    1065           0 :                 goto failed;
    1066             :         }
    1067         319 :         talloc_steal(mem_ctx, ldif);
    1068             : 
    1069         319 :         ret = ldb_msg_normalize(ldb, mem_ctx, ldif->msg, &msg);
    1070         319 :         if (ret != LDB_SUCCESS) {
    1071           0 :                 goto nomem;
    1072             :         }
    1073         319 :         talloc_free(ldif);
    1074             : 
    1075         319 :         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
    1076         319 :         if (!prefix_val) {
    1077           0 :                 status = WERR_INVALID_PARAMETER;
    1078           0 :                 goto failed;
    1079             :         }
    1080             : 
    1081         319 :         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
    1082         319 :         if (!info_val) {
    1083         319 :                 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
    1084         319 :                 W_ERROR_NOT_OK_GOTO(status, failed);
    1085         319 :                 info_val = &info_val_default;
    1086             :         }
    1087             : 
    1088         319 :         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
    1089         319 :         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         319 :         schema->ts_last_change = 0;
    1095             :         /* load the attribute and class definitions out of df */
    1096      548467 :         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
    1097      547872 :                 talloc_steal(mem_ctx, ldif);
    1098             : 
    1099      547872 :                 ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &msg);
    1100      547872 :                 if (ret != LDB_SUCCESS) {
    1101           0 :                         goto nomem;
    1102             :                 }
    1103             : 
    1104      547872 :                 status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, msg);
    1105      547872 :                 talloc_free(ldif);
    1106      547872 :                 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         319 :         ret = dsdb_set_schema(ldb, schema, SCHEMA_WRITE);
    1119         319 :         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         319 :         ret = dsdb_schema_fill_extended_dn(ldb, schema);
    1127         319 :         if (ret != LDB_SUCCESS) {
    1128           0 :                 status = WERR_FOOBAR;
    1129           0 :                 goto failed;
    1130             :         }
    1131             : 
    1132         319 :         goto done;
    1133             : 
    1134           0 : nomem:
    1135           0 :         status = WERR_NOT_ENOUGH_MEMORY;
    1136         276 : failed:
    1137         319 : done:
    1138         319 :         talloc_free(mem_ctx);
    1139         319 :         return status;
    1140             : }

Generated by: LCOV version 1.13