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

Generated by: LCOV version 1.14