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)
38 struct ldb_dn *schema_dn;
39 struct dsdb_schema *schema;
40 struct ldb_result *schema_res;
41 struct ldb_result *a_res;
42 struct ldb_result *c_res;
43 char *error_string = NULL;
45 static const char *schema_attrs[] = {
52 if (dsdb_get_schema(module->ldb)) {
53 return ldb_next_init(module);
56 schema_dn = samdb_schema_dn(module->ldb);
58 ldb_reset_err_string(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;
71 * setup the prefix mappings and schema info
73 ret = ldb_search(module->ldb, schema_dn,
77 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
78 ldb_reset_err_string(module->ldb);
79 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
80 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
82 return ldb_next_init(module);
83 } else if (ret != LDB_SUCCESS) {
84 ldb_asprintf_errstring(module->ldb,
85 "schema_fsmo_init: failed to search the schema head: %s",
86 ldb_errstring(module->ldb));
90 talloc_steal(mem_ctx, schema_res);
91 if (schema_res->count == 0) {
92 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
93 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
95 return ldb_next_init(module);
96 } else if (schema_res->count > 1) {
97 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
98 "schema_fsmo_init: [%u] schema heads found on a base search",
100 talloc_free(mem_ctx);
101 return LDB_ERR_CONSTRAINT_VIOLATION;
105 * load the attribute definitions
107 ret = ldb_search(module->ldb, schema_dn,
109 "(objectClass=attributeSchema)", NULL,
111 if (ret != LDB_SUCCESS) {
112 ldb_asprintf_errstring(module->ldb,
113 "schema_fsmo_init: failed to search attributeSchema objects: %s",
114 ldb_errstring(module->ldb));
115 talloc_free(mem_ctx);
118 talloc_steal(mem_ctx, a_res);
121 * load the objectClass definitions
123 ret = ldb_search(module->ldb, schema_dn,
125 "(objectClass=classSchema)", NULL,
127 if (ret != LDB_SUCCESS) {
128 ldb_asprintf_errstring(module->ldb,
129 "schema_fsmo_init: failed to search classSchema objects: %s",
130 ldb_errstring(module->ldb));
131 talloc_free(mem_ctx);
134 talloc_steal(mem_ctx, c_res);
136 ret = dsdb_schema_from_ldb_results(mem_ctx, module->ldb,
137 lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
138 schema_res, a_res, c_res, &schema, &error_string);
139 if (ret != LDB_SUCCESS) {
140 ldb_asprintf_errstring(module->ldb,
141 "schema_fsmo_init: dsdb_schema load failed: %s",
143 talloc_free(mem_ctx);
147 /* dsdb_set_schema() steal schema into the ldb_context */
148 ret = dsdb_set_schema(module->ldb, schema);
149 if (ret != LDB_SUCCESS) {
150 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
151 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
152 ret, ldb_strerror(ret));
153 talloc_free(mem_ctx);
157 talloc_free(mem_ctx);
158 return ldb_next_init(module);
161 static int schema_fsmo_add(struct ldb_module *module, struct ldb_request *req)
163 struct dsdb_schema *schema;
164 const char *attributeID = NULL;
165 const char *governsID = NULL;
166 const char *oid_attr = NULL;
167 const char *oid = NULL;
171 schema = dsdb_get_schema(module->ldb);
173 return ldb_next_request(module, req);
176 if (!schema->fsmo.we_are_master) {
177 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
178 "schema_fsmo_add: we are not master: reject request\n");
179 return LDB_ERR_UNWILLING_TO_PERFORM;
182 attributeID = samdb_result_string(req->op.add.message, "attributeID", NULL);
183 governsID = samdb_result_string(req->op.add.message, "governsID", NULL);
186 oid_attr = "attributeID";
188 } else if (governsID) {
189 oid_attr = "governsID";
194 return ldb_next_request(module, req);
197 status = dsdb_map_oid2int(schema, oid, &id32);
198 if (W_ERROR_IS_OK(status)) {
199 return ldb_next_request(module, req);
200 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
201 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
202 "schema_fsmo_add: failed to map %s[%s]: %s\n",
203 oid_attr, oid, win_errstr(status));
204 return LDB_ERR_UNWILLING_TO_PERFORM;
207 status = dsdb_create_prefix_mapping(module->ldb, schema, oid);
208 if (!W_ERROR_IS_OK(status)) {
209 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
210 "schema_fsmo_add: failed to create prefix mapping for %s[%s]: %s\n",
211 oid_attr, oid, win_errstr(status));
212 return LDB_ERR_UNWILLING_TO_PERFORM;
215 return ldb_next_request(module, req);
218 _PUBLIC_ const struct ldb_module_ops ldb_schema_fsmo_module_ops = {
219 .name = "schema_fsmo",
220 .init_context = schema_fsmo_init,
221 .add = schema_fsmo_add