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
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "ldb_module.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "dsdb/samdb/ldb_modules/util.h"
35 Given an LDB module (pointing at the schema DB), and the DN, set the populated schema
38 static int dsdb_schema_from_schema_dn(TALLOC_CTX
*mem_ctx
, struct ldb_module
*module
,
39 struct smb_iconv_convenience
*iconv_convenience
,
40 struct ldb_dn
*schema_dn
,
41 struct dsdb_schema
**schema
)
46 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
47 struct ldb_result
*schema_res
;
48 struct ldb_result
*a_res
;
49 struct ldb_result
*c_res
;
50 static const char *schema_attrs
[] = {
58 tmp_ctx
= talloc_new(mem_ctx
);
61 return LDB_ERR_OPERATIONS_ERROR
;
64 /* we don't want to trace the schema load */
65 flags
= ldb_get_flags(ldb
);
66 ldb_set_flags(ldb
, flags
& ~LDB_FLG_ENABLE_TRACING
);
69 * setup the prefix mappings and schema info
71 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &schema_res
,
72 schema_dn
, schema_attrs
);
73 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
75 } else if (ret
!= LDB_SUCCESS
) {
76 ldb_asprintf_errstring(ldb
,
77 "dsdb_schema: failed to search the schema head: %s",
83 * load the attribute definitions
85 ret
= dsdb_module_search(module
, tmp_ctx
, &a_res
,
86 schema_dn
, LDB_SCOPE_ONELEVEL
, NULL
,
87 "(objectClass=attributeSchema)");
88 if (ret
!= LDB_SUCCESS
) {
89 ldb_asprintf_errstring(ldb
,
90 "dsdb_schema: failed to search attributeSchema objects: %s",
96 * load the objectClass definitions
98 ret
= dsdb_module_search(module
, tmp_ctx
, &c_res
,
99 schema_dn
, LDB_SCOPE_ONELEVEL
, NULL
,
100 "(objectClass=classSchema)");
101 if (ret
!= LDB_SUCCESS
) {
102 ldb_asprintf_errstring(ldb
,
103 "dsdb_schema: failed to search attributeSchema objects: %s",
108 ret
= dsdb_schema_from_ldb_results(tmp_ctx
, ldb
,
109 lp_iconv_convenience(ldb_get_opaque(ldb
, "loadparm")),
110 schema_res
, a_res
, c_res
, schema
, &error_string
);
111 if (ret
!= LDB_SUCCESS
) {
112 ldb_asprintf_errstring(ldb
,
113 "dsdb_schema load failed: %s",
117 talloc_steal(mem_ctx
, *schema
);
120 if (flags
& LDB_FLG_ENABLE_TRACING
) {
121 flags
= ldb_get_flags(ldb
);
122 ldb_set_flags(ldb
, flags
| LDB_FLG_ENABLE_TRACING
);
124 talloc_free(tmp_ctx
);
129 static int schema_load_init(struct ldb_module
*module
)
131 struct ldb_context
*ldb
;
133 struct ldb_dn
*schema_dn
;
134 struct dsdb_schema
*schema
;
137 ret
= ldb_next_init(module
);
138 if (ret
!= LDB_SUCCESS
) {
142 ldb
= ldb_module_get_ctx(module
);
143 schema_dn
= samdb_schema_dn(ldb
);
145 ldb_reset_err_string(ldb
);
146 ldb_debug(ldb
, LDB_DEBUG_WARNING
,
147 "schema_load_init: no schema dn present: (skip schema loading)\n");
151 if (dsdb_get_schema(ldb
)) {
155 mem_ctx
= talloc_new(module
);
158 return LDB_ERR_OPERATIONS_ERROR
;
161 ret
= dsdb_schema_from_schema_dn(mem_ctx
, module
,
162 lp_iconv_convenience(ldb_get_opaque(ldb
, "loadparm")),
165 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
166 ldb_reset_err_string(ldb
);
167 ldb_debug(ldb
, LDB_DEBUG_WARNING
,
168 "schema_load_init: no schema head present: (skip schema loading)\n");
169 talloc_free(mem_ctx
);
173 if (ret
!= LDB_SUCCESS
) {
174 talloc_free(mem_ctx
);
178 /* dsdb_set_schema() steal schema into the ldb_context */
179 ret
= dsdb_set_schema(ldb
, schema
);
180 if (ret
!= LDB_SUCCESS
) {
181 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
182 "schema_load_init: dsdb_set_schema() failed: %d:%s",
183 ret
, ldb_strerror(ret
));
184 talloc_free(mem_ctx
);
188 talloc_free(mem_ctx
);
192 static int schema_load_extended(struct ldb_module
*module
, struct ldb_request
*req
)
194 struct ldb_context
*ldb
;
195 struct ldb_dn
*schema_dn
;
196 struct dsdb_schema
*schema
;
200 ldb
= ldb_module_get_ctx(module
);
202 if (strcmp(req
->op
.extended
.oid
, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID
) != 0) {
203 return ldb_next_request(module
, req
);
206 schema_dn
= samdb_schema_dn(ldb
);
208 ldb_reset_err_string(ldb
);
209 ldb_debug(ldb
, LDB_DEBUG_WARNING
,
210 "schema_load_extended: no schema dn present: (skip schema loading)\n");
211 return ldb_next_request(module
, req
);
214 mem_ctx
= talloc_new(module
);
217 return LDB_ERR_OPERATIONS_ERROR
;
220 ret
= dsdb_schema_from_schema_dn(mem_ctx
, module
,
221 lp_iconv_convenience(ldb_get_opaque(ldb
, "loadparm")),
224 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
225 ldb_reset_err_string(ldb
);
226 ldb_debug(ldb
, LDB_DEBUG_WARNING
,
227 "schema_load_extended: no schema head present: (skip schema loading)\n");
228 talloc_free(mem_ctx
);
229 return ldb_next_request(module
, req
);
232 if (ret
!= LDB_SUCCESS
) {
233 talloc_free(mem_ctx
);
234 return ldb_next_request(module
, req
);
237 /* Replace the old schema*/
238 ret
= dsdb_set_schema(ldb
, schema
);
239 if (ret
!= LDB_SUCCESS
) {
240 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
241 "schema_load_extended: dsdb_set_schema() failed: %d:%s",
242 ret
, ldb_strerror(ret
));
243 talloc_free(mem_ctx
);
247 dsdb_make_schema_global(ldb
);
249 talloc_free(mem_ctx
);
254 _PUBLIC_
const struct ldb_module_ops ldb_schema_load_module_ops
= {
255 .name
= "schema_load",
256 .init_context
= schema_load_init
,
257 .extended
= schema_load_extended
,