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 if (dsdb_get_schema(module->ldb)) {
58 return ldb_next_init(module);
61 schema_dn = samdb_schema_dn(module->ldb);
63 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
64 "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
65 return ldb_next_init(module);
68 mem_ctx = talloc_new(module);
71 return LDB_ERR_OPERATIONS_ERROR;
74 schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
77 return LDB_ERR_OPERATIONS_ERROR;
79 module->private_data = schema_fsmo;
81 schema = talloc_zero(mem_ctx, struct dsdb_schema);
84 return LDB_ERR_OPERATIONS_ERROR;
88 * setup the prefix mappings and schema info
90 ret = ldb_search(module->ldb, schema_dn,
94 if (ret != LDB_SUCCESS) {
95 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
96 "schema_fsmo_init: failed to search the schema head: %d:%s\n",
97 ret, ldb_strerror(ret));
101 talloc_steal(mem_ctx, schema_res);
102 if (schema_res->count == 0) {
103 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
104 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
105 talloc_free(mem_ctx);
106 return ldb_next_init(module);
107 } else if (schema_res->count > 1) {
108 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
109 "schema_fsmo_init: [%u] schema heads found on a base search\n",
111 talloc_free(mem_ctx);
112 return LDB_ERR_CONSTRAINT_VIOLATION;
115 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
117 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
118 "schema_fsmo_init: no prefixMap attribute found\n");
119 talloc_free(mem_ctx);
120 return LDB_ERR_CONSTRAINT_VIOLATION;
122 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
124 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
125 if (!info_val_default.data) {
126 ldb_oom(module->ldb);
127 return LDB_ERR_OPERATIONS_ERROR;
129 talloc_steal(mem_ctx, info_val_default.data);
130 info_val = &info_val_default;
133 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
134 if (!W_ERROR_IS_OK(status)) {
135 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
136 "schema_fsmo_init: failed to load oid mappings: %s\n",
138 talloc_free(mem_ctx);
139 return LDB_ERR_CONSTRAINT_VIOLATION;
143 * load the attribute definitions
145 ret = ldb_search(module->ldb, schema_dn,
147 "(objectClass=attributeSchema)", NULL,
149 if (ret != LDB_SUCCESS) {
150 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
151 "schema_fsmo_init: failed to search attributeSchema objects: %d:%s\n",
152 ret, ldb_strerror(ret));
153 talloc_free(mem_ctx);
156 talloc_steal(mem_ctx, a_res);
158 for (i=0; i < a_res->count; i++) {
159 struct dsdb_attribute *sa;
161 sa = talloc_zero(schema, struct dsdb_attribute);
163 ldb_oom(module->ldb);
164 return LDB_ERR_OPERATIONS_ERROR;
167 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
168 if (!W_ERROR_IS_OK(status)) {
169 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
170 "schema_fsmo_init: failed to load attriute definition: %s:%s\n",
171 ldb_dn_get_linearized(a_res->msgs[i]->dn),
173 talloc_free(mem_ctx);
174 return LDB_ERR_CONSTRAINT_VIOLATION;
177 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
182 * load the objectClass definitions
184 ret = ldb_search(module->ldb, schema_dn,
186 "(objectClass=classSchema)", NULL,
188 if (ret != LDB_SUCCESS) {
189 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
190 "schema_fsmo_init: failed to search classSchema objects: %d:%s\n",
191 ret, ldb_strerror(ret));
192 talloc_free(mem_ctx);
195 talloc_steal(mem_ctx, c_res);
197 for (i=0; i < c_res->count; i++) {
198 struct dsdb_class *sc;
200 sc = talloc_zero(schema, struct dsdb_class);
202 ldb_oom(module->ldb);
203 return LDB_ERR_OPERATIONS_ERROR;
206 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
207 if (!W_ERROR_IS_OK(status)) {
208 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
209 "schema_fsmo_init: failed to load class definition: %s:%s\n",
210 ldb_dn_get_linearized(c_res->msgs[i]->dn),
212 talloc_free(mem_ctx);
213 return LDB_ERR_CONSTRAINT_VIOLATION;
216 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
220 /* dsdb_set_schema() steal schema into the ldb_context */
221 ret = dsdb_set_schema(module->ldb, schema);
222 if (ret != LDB_SUCCESS) {
223 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
224 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s\n",
225 ret, ldb_strerror(ret));
226 talloc_free(mem_ctx);
230 schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
231 if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
232 schema_fsmo->we_are_master = true;
234 schema_fsmo->we_are_master = false;
237 if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
238 ldb_oom(module->ldb);
239 return LDB_ERR_OPERATIONS_ERROR;
242 talloc_steal(module, schema_fsmo);
244 ldb_debug(module->ldb, LDB_DEBUG_TRACE,
245 "schema_fsmo_init: we are master: %s\n",
246 (schema_fsmo->we_are_master?"yes":"no"));
248 talloc_free(mem_ctx);
249 return ldb_next_init(module);
252 static const struct ldb_module_ops schema_fsmo_ops = {
253 .name = "schema_fsmo",
254 .init_context = schema_fsmo_init
257 int schema_fsmo_module_init(void)
259 return ldb_register_module(&schema_fsmo_ops);