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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "librpc/gen_ndr/ndr_drsuapi.h"
31 #include "librpc/gen_ndr/ndr_drsblobs.h"
32 #include "lib/util/dlinklist.h"
34 static int schema_fsmo_init(struct ldb_module *module)
38 struct ldb_dn *schema_dn;
39 struct dsdb_schema *schema;
40 struct dsdb_schema_fsmo *schema_fsmo;
41 struct ldb_result *schema_res;
42 const struct ldb_val *prefix_val;
43 const struct ldb_val *info_val;
44 struct ldb_val info_val_default;
45 struct ldb_result *a_res;
46 struct ldb_result *c_res;
49 static const char *schema_attrs[] = {
56 if (dsdb_get_schema(module->ldb)) {
57 return ldb_next_init(module);
60 schema_dn = samdb_schema_dn(module->ldb);
62 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
63 "schema_fsmo_init: no schema dn present: (skip schema loading)");
64 return ldb_next_init(module);
67 mem_ctx = talloc_new(module);
70 return LDB_ERR_OPERATIONS_ERROR;
73 schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
76 return LDB_ERR_OPERATIONS_ERROR;
78 module->private_data = schema_fsmo;
80 schema = talloc_zero(mem_ctx, struct dsdb_schema);
83 return LDB_ERR_OPERATIONS_ERROR;
87 * setup the prefix mappings and schema info
89 ret = ldb_search(module->ldb, schema_dn,
93 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
94 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
95 "schema_fsmo_init: no schema head present: (skip schema loading)");
97 return ldb_next_init(module);
98 } else if (ret != LDB_SUCCESS) {
99 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
100 "schema_fsmo_init: failed to search the schema head: %d:%s",
101 ret, ldb_strerror(ret));
102 talloc_free(mem_ctx);
105 talloc_steal(mem_ctx, schema_res);
106 if (schema_res->count == 0) {
107 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
108 "schema_fsmo_init: no schema head present: (skip schema loading)");
109 talloc_free(mem_ctx);
110 return ldb_next_init(module);
111 } else if (schema_res->count > 1) {
112 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
113 "schema_fsmo_init: [%u] schema heads found on a base search",
115 talloc_free(mem_ctx);
116 return LDB_ERR_CONSTRAINT_VIOLATION;
119 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
121 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
122 "schema_fsmo_init: no prefixMap attribute found");
123 talloc_free(mem_ctx);
124 return LDB_ERR_CONSTRAINT_VIOLATION;
126 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
128 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
129 if (!info_val_default.data) {
130 ldb_oom(module->ldb);
131 return LDB_ERR_OPERATIONS_ERROR;
133 talloc_steal(mem_ctx, info_val_default.data);
134 info_val = &info_val_default;
137 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
138 if (!W_ERROR_IS_OK(status)) {
139 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
140 "schema_fsmo_init: failed to load oid mappings: %s",
142 talloc_free(mem_ctx);
143 return LDB_ERR_CONSTRAINT_VIOLATION;
147 * load the attribute definitions
149 ret = ldb_search(module->ldb, schema_dn,
151 "(objectClass=attributeSchema)", NULL,
153 if (ret != LDB_SUCCESS) {
154 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
155 "schema_fsmo_init: failed to search attributeSchema objects: %d:%s",
156 ret, ldb_strerror(ret));
157 talloc_free(mem_ctx);
160 talloc_steal(mem_ctx, a_res);
162 for (i=0; i < a_res->count; i++) {
163 struct dsdb_attribute *sa;
165 sa = talloc_zero(schema, struct dsdb_attribute);
167 ldb_oom(module->ldb);
168 return LDB_ERR_OPERATIONS_ERROR;
171 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
172 if (!W_ERROR_IS_OK(status)) {
173 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
174 "schema_fsmo_init: failed to load attriute definition: %s:%s",
175 ldb_dn_get_linearized(a_res->msgs[i]->dn),
177 talloc_free(mem_ctx);
178 return LDB_ERR_CONSTRAINT_VIOLATION;
181 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
186 * load the objectClass definitions
188 ret = ldb_search(module->ldb, schema_dn,
190 "(objectClass=classSchema)", NULL,
192 if (ret != LDB_SUCCESS) {
193 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
194 "schema_fsmo_init: failed to search classSchema objects: %d:%s",
195 ret, ldb_strerror(ret));
196 talloc_free(mem_ctx);
199 talloc_steal(mem_ctx, c_res);
201 for (i=0; i < c_res->count; i++) {
202 struct dsdb_class *sc;
204 sc = talloc_zero(schema, struct dsdb_class);
206 ldb_oom(module->ldb);
207 return LDB_ERR_OPERATIONS_ERROR;
210 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
211 if (!W_ERROR_IS_OK(status)) {
212 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
213 "schema_fsmo_init: failed to load class definition: %s:%s",
214 ldb_dn_get_linearized(c_res->msgs[i]->dn),
216 talloc_free(mem_ctx);
217 return LDB_ERR_CONSTRAINT_VIOLATION;
220 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
224 /* dsdb_set_schema() steal schema into the ldb_context */
225 ret = dsdb_set_schema(module->ldb, schema);
226 if (ret != LDB_SUCCESS) {
227 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
228 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
229 ret, ldb_strerror(ret));
230 talloc_free(mem_ctx);
234 schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
235 if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
236 schema_fsmo->we_are_master = true;
238 schema_fsmo->we_are_master = false;
241 if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
242 ldb_oom(module->ldb);
243 return LDB_ERR_OPERATIONS_ERROR;
246 talloc_steal(module, schema_fsmo);
248 ldb_debug(module->ldb, LDB_DEBUG_TRACE,
249 "schema_fsmo_init: we are master: %s",
250 (schema_fsmo->we_are_master?"yes":"no"));
252 talloc_free(mem_ctx);
253 return ldb_next_init(module);
256 static const struct ldb_module_ops schema_fsmo_ops = {
257 .name = "schema_fsmo",
258 .init_context = schema_fsmo_init
261 int schema_fsmo_module_init(void)
263 return ldb_register_module(&schema_fsmo_ops);