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"
33 #include "param/param.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_reset_err_string(module->ldb);
64 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
65 "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
66 return ldb_next_init(module);
69 mem_ctx = talloc_new(module);
72 return LDB_ERR_OPERATIONS_ERROR;
75 schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
78 return LDB_ERR_OPERATIONS_ERROR;
80 module->private_data = schema_fsmo;
82 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")));
85 return LDB_ERR_OPERATIONS_ERROR;
89 * setup the prefix mappings and schema info
91 ret = ldb_search(module->ldb, schema_dn,
95 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
96 ldb_reset_err_string(module->ldb);
97 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
98 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
100 return ldb_next_init(module);
101 } else if (ret != LDB_SUCCESS) {
102 ldb_asprintf_errstring(module->ldb,
103 "schema_fsmo_init: failed to search the schema head: %s",
104 ldb_errstring(module->ldb));
105 talloc_free(mem_ctx);
108 talloc_steal(mem_ctx, schema_res);
109 if (schema_res->count == 0) {
110 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
111 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
112 talloc_free(mem_ctx);
113 return ldb_next_init(module);
114 } else if (schema_res->count > 1) {
115 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
116 "schema_fsmo_init: [%u] schema heads found on a base search",
118 talloc_free(mem_ctx);
119 return LDB_ERR_CONSTRAINT_VIOLATION;
122 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
124 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
125 "schema_fsmo_init: no prefixMap attribute found");
126 talloc_free(mem_ctx);
127 return LDB_ERR_CONSTRAINT_VIOLATION;
129 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
131 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
132 if (!info_val_default.data) {
133 ldb_oom(module->ldb);
134 return LDB_ERR_OPERATIONS_ERROR;
136 talloc_steal(mem_ctx, info_val_default.data);
137 info_val = &info_val_default;
140 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
141 if (!W_ERROR_IS_OK(status)) {
142 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
143 "schema_fsmo_init: failed to load oid mappings: %s",
145 talloc_free(mem_ctx);
146 return LDB_ERR_CONSTRAINT_VIOLATION;
150 * load the attribute definitions
152 ret = ldb_search(module->ldb, schema_dn,
154 "(objectClass=attributeSchema)", NULL,
156 if (ret != LDB_SUCCESS) {
157 ldb_asprintf_errstring(module->ldb,
158 "schema_fsmo_init: failed to search attributeSchema objects: %s",
159 ldb_errstring(module->ldb));
160 talloc_free(mem_ctx);
163 talloc_steal(mem_ctx, a_res);
165 for (i=0; i < a_res->count; i++) {
166 struct dsdb_attribute *sa;
168 sa = talloc_zero(schema, struct dsdb_attribute);
170 ldb_oom(module->ldb);
171 return LDB_ERR_OPERATIONS_ERROR;
174 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
175 if (!W_ERROR_IS_OK(status)) {
176 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
177 "schema_fsmo_init: failed to load attriute definition: %s:%s",
178 ldb_dn_get_linearized(a_res->msgs[i]->dn),
180 talloc_free(mem_ctx);
181 return LDB_ERR_CONSTRAINT_VIOLATION;
184 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
189 * load the objectClass definitions
191 ret = ldb_search(module->ldb, schema_dn,
193 "(objectClass=classSchema)", NULL,
195 if (ret != LDB_SUCCESS) {
196 ldb_asprintf_errstring(module->ldb,
197 "schema_fsmo_init: failed to search classSchema objects: %s",
198 ldb_errstring(module->ldb));
199 talloc_free(mem_ctx);
202 talloc_steal(mem_ctx, c_res);
204 for (i=0; i < c_res->count; i++) {
205 struct dsdb_class *sc;
207 sc = talloc_zero(schema, struct dsdb_class);
209 ldb_oom(module->ldb);
210 return LDB_ERR_OPERATIONS_ERROR;
213 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
214 if (!W_ERROR_IS_OK(status)) {
215 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
216 "schema_fsmo_init: failed to load class definition: %s:%s",
217 ldb_dn_get_linearized(c_res->msgs[i]->dn),
219 talloc_free(mem_ctx);
220 return LDB_ERR_CONSTRAINT_VIOLATION;
223 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
227 /* dsdb_set_schema() steal schema into the ldb_context */
228 ret = dsdb_set_schema(module->ldb, schema);
229 if (ret != LDB_SUCCESS) {
230 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
231 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
232 ret, ldb_strerror(ret));
233 talloc_free(mem_ctx);
237 schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
238 if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
239 schema_fsmo->we_are_master = true;
241 schema_fsmo->we_are_master = false;
244 if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
245 ldb_oom(module->ldb);
246 return LDB_ERR_OPERATIONS_ERROR;
249 talloc_steal(module, schema_fsmo);
251 ldb_debug(module->ldb, LDB_DEBUG_TRACE,
252 "schema_fsmo_init: we are master: %s\n",
253 (schema_fsmo->we_are_master?"yes":"no"));
255 talloc_free(mem_ctx);
256 return ldb_next_init(module);
259 _PUBLIC_ const struct ldb_module_ops ldb_schema_fsmo_module_ops = {
260 .name = "schema_fsmo",
261 .init_context = schema_fsmo_init