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 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 ldb_result *schema_res;
42 const struct ldb_val *prefix_val;
43 const struct ldb_val *info_val;
44 struct ldb_result *a_res;
45 struct ldb_result *c_res;
48 static const char *schema_attrs[] = {
54 schema_dn = samdb_schema_dn(module->ldb);
56 ldb_debug(module->ldb, LDB_DEBUG_TRACE,
57 "schema_fsmo_init: no schema dn present: (skip schema loading)");
58 return ldb_next_init(module);
61 mem_ctx = talloc_new(module);
64 return LDB_ERR_OPERATIONS_ERROR;
67 schema = talloc_zero(mem_ctx, struct dsdb_schema);
70 return LDB_ERR_OPERATIONS_ERROR;
74 * setup the prefix mappings and schema info
76 ret = ldb_search(module->ldb, schema_dn,
80 if (ret != LDB_SUCCESS) {
81 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
82 "schema_fsmo_init: failed to search the schema head: %d:%s",
83 ret, ldb_strerror(ret));
87 talloc_steal(mem_ctx, schema_res);
88 if (schema_res->count == 0) {
89 ldb_debug(module->ldb, LDB_DEBUG_TRACE,
90 "schema_fsmo_init: no schema head present: (skip schema loading)");
92 return ldb_next_init(module);
93 } else if (schema_res->count > 1) {
94 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
95 "schema_fsmo_init: [%u] schema heads found on a base search",
98 return LDB_ERR_CONSTRAINT_VIOLATION;
101 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
103 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
104 "schema_fsmo_init: no prefixMap attribute found");
105 talloc_free(mem_ctx);
106 return LDB_ERR_CONSTRAINT_VIOLATION;
108 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
110 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
111 "schema_fsmo_init: no schemaInfo attribute found");
112 talloc_free(mem_ctx);
113 return LDB_ERR_CONSTRAINT_VIOLATION;
116 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
117 if (!W_ERROR_IS_OK(status)) {
118 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
119 "schema_fsmo_init: failed to load oid mappings: %s",
121 talloc_free(mem_ctx);
122 return LDB_ERR_CONSTRAINT_VIOLATION;
126 * load the attribute definitions
128 ret = ldb_search(module->ldb, schema_dn,
130 "(objectClass=attributeSchema)", NULL,
132 if (ret != LDB_SUCCESS) {
133 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
134 "schema_fsmo_init: failed to search attributeSchema objects: %d:%s",
135 ret, ldb_strerror(ret));
136 talloc_free(mem_ctx);
139 talloc_steal(mem_ctx, a_res);
141 for (i=0; i < a_res->count; i++) {
142 struct dsdb_attribute *sa;
144 sa = talloc_zero(schema, struct dsdb_attribute);
146 ldb_oom(module->ldb);
147 return LDB_ERR_OPERATIONS_ERROR;
150 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
151 if (!W_ERROR_IS_OK(status)) {
152 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
153 "schema_fsmo_init: failed to load attriute definition: %s:%s",
154 ldb_dn_get_linearized(a_res->msgs[i]->dn),
156 talloc_free(mem_ctx);
157 return LDB_ERR_CONSTRAINT_VIOLATION;
160 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
165 * load the objectClass definitions
167 ret = ldb_search(module->ldb, schema_dn,
169 "(objectClass=classSchema)", NULL,
171 if (ret != LDB_SUCCESS) {
172 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
173 "schema_fsmo_init: failed to search classSchema objects: %d:%s",
174 ret, ldb_strerror(ret));
175 talloc_free(mem_ctx);
178 talloc_steal(mem_ctx, c_res);
180 for (i=0; i < c_res->count; i++) {
181 struct dsdb_class *sc;
183 sc = talloc_zero(schema, struct dsdb_class);
185 ldb_oom(module->ldb);
186 return LDB_ERR_OPERATIONS_ERROR;
189 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
190 if (!W_ERROR_IS_OK(status)) {
191 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
192 "schema_fsmo_init: failed to load class definition: %s:%s",
193 ldb_dn_get_linearized(c_res->msgs[i]->dn),
195 talloc_free(mem_ctx);
196 return LDB_ERR_CONSTRAINT_VIOLATION;
199 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
203 ret = dsdb_set_schema(module->ldb, schema);
204 if (ret != LDB_SUCCESS) {
205 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
206 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
207 ret, ldb_strerror(ret));
208 talloc_free(mem_ctx);
212 talloc_steal(module, schema);
213 talloc_free(mem_ctx);
214 return ldb_next_init(module);
217 static const struct ldb_module_ops schema_fsmo_ops = {
218 .name = "schema_fsmo",
219 .init_context = schema_fsmo_init
222 int schema_fsmo_module_init(void)
224 return ldb_register_module(&schema_fsmo_ops);