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

Generated by: LCOV version 1.14