2 Unix SMB/CIFS mplementation.
4 The module that handles the Schema FSMO Role Owner
5 checkings, it also loads the dsdb_schema.
7 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
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 2 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "lib/ldb/include/ldb_private.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "librpc/gen_ndr/ndr_drsuapi.h"
32 #include "librpc/gen_ndr/ndr_drsblobs.h"
33 #include "lib/util/dlinklist.h"
35 static int schema_fsmo_init(struct ldb_module *module)
39 struct ldb_dn *schema_dn;
40 struct dsdb_schema *schema;
41 struct dsdb_schema_fsmo *schema_fsmo;
42 struct ldb_result *schema_res;
43 const struct ldb_val *prefix_val;
44 const struct ldb_val *info_val;
45 struct ldb_val info_val_default;
46 struct ldb_result *a_res;
47 struct ldb_result *c_res;
50 static const char *schema_attrs[] = {
57 schema_dn = samdb_schema_dn(module->ldb);
59 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
60 "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
61 return ldb_next_init(module);
64 mem_ctx = talloc_new(module);
67 return LDB_ERR_OPERATIONS_ERROR;
70 schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
73 return LDB_ERR_OPERATIONS_ERROR;
75 module->private_data = schema_fsmo;
77 schema = talloc_zero(mem_ctx, struct dsdb_schema);
80 return LDB_ERR_OPERATIONS_ERROR;
84 * setup the prefix mappings and schema info
86 ret = ldb_search(module->ldb, schema_dn,
90 if (ret != LDB_SUCCESS) {
91 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
92 "schema_fsmo_init: failed to search the schema head: %d:%s\n",
93 ret, ldb_strerror(ret));
97 talloc_steal(mem_ctx, schema_res);
98 if (schema_res->count == 0) {
99 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
100 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
101 talloc_free(mem_ctx);
102 return ldb_next_init(module);
103 } else if (schema_res->count > 1) {
104 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
105 "schema_fsmo_init: [%u] schema heads found on a base search\n",
107 talloc_free(mem_ctx);
108 return LDB_ERR_CONSTRAINT_VIOLATION;
111 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
113 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
114 "schema_fsmo_init: no prefixMap attribute found\n");
115 talloc_free(mem_ctx);
116 return LDB_ERR_CONSTRAINT_VIOLATION;
118 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
120 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
121 if (!info_val_default.data) {
122 ldb_oom(module->ldb);
123 return LDB_ERR_OPERATIONS_ERROR;
125 talloc_steal(mem_ctx, info_val_default.data);
126 info_val = &info_val_default;
129 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
130 if (!W_ERROR_IS_OK(status)) {
131 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
132 "schema_fsmo_init: failed to load oid mappings: %s\n",
134 talloc_free(mem_ctx);
135 return LDB_ERR_CONSTRAINT_VIOLATION;
139 * load the attribute definitions
141 ret = ldb_search(module->ldb, schema_dn,
143 "(objectClass=attributeSchema)", NULL,
145 if (ret != LDB_SUCCESS) {
146 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
147 "schema_fsmo_init: failed to search attributeSchema objects: %d:%s\n",
148 ret, ldb_strerror(ret));
149 talloc_free(mem_ctx);
152 talloc_steal(mem_ctx, a_res);
154 for (i=0; i < a_res->count; i++) {
155 struct dsdb_attribute *sa;
157 sa = talloc_zero(schema, struct dsdb_attribute);
159 ldb_oom(module->ldb);
160 return LDB_ERR_OPERATIONS_ERROR;
163 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
164 if (!W_ERROR_IS_OK(status)) {
165 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
166 "schema_fsmo_init: failed to load attriute definition: %s:%s\n",
167 ldb_dn_get_linearized(a_res->msgs[i]->dn),
169 talloc_free(mem_ctx);
170 return LDB_ERR_CONSTRAINT_VIOLATION;
173 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
178 * load the objectClass definitions
180 ret = ldb_search(module->ldb, schema_dn,
182 "(objectClass=classSchema)", NULL,
184 if (ret != LDB_SUCCESS) {
185 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
186 "schema_fsmo_init: failed to search classSchema objects: %d:%s\n",
187 ret, ldb_strerror(ret));
188 talloc_free(mem_ctx);
191 talloc_steal(mem_ctx, c_res);
193 for (i=0; i < c_res->count; i++) {
194 struct dsdb_class *sc;
196 sc = talloc_zero(schema, struct dsdb_class);
198 ldb_oom(module->ldb);
199 return LDB_ERR_OPERATIONS_ERROR;
202 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
203 if (!W_ERROR_IS_OK(status)) {
204 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
205 "schema_fsmo_init: failed to load class definition: %s:%s\n",
206 ldb_dn_get_linearized(c_res->msgs[i]->dn),
208 talloc_free(mem_ctx);
209 return LDB_ERR_CONSTRAINT_VIOLATION;
212 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
216 /* dsdb_set_schema() steal schema into the ldb_context */
217 ret = dsdb_set_schema(module->ldb, schema);
218 if (ret != LDB_SUCCESS) {
219 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
220 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s\n",
221 ret, ldb_strerror(ret));
222 talloc_free(mem_ctx);
226 schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
227 if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
228 schema_fsmo->we_are_master = true;
230 schema_fsmo->we_are_master = false;
233 if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
234 ldb_oom(module->ldb);
235 return LDB_ERR_OPERATIONS_ERROR;
238 talloc_steal(module, schema_fsmo);
240 ldb_debug(module->ldb, LDB_DEBUG_TRACE,
241 "schema_fsmo_init: we are master: %s\n",
242 (schema_fsmo->we_are_master?"yes":"no"));
244 talloc_free(mem_ctx);
245 return ldb_next_init(module);
248 static const struct ldb_module_ops schema_fsmo_ops = {
249 .name = "schema_fsmo",
250 .init_context = schema_fsmo_init
253 int schema_fsmo_module_init(void)
255 return ldb_register_module(&schema_fsmo_ops);