r23560: - Activate metze's schema modules (from metze's schema-loading-13 patch).
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / schema_fsmo.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    The module that handles the Schema FSMO Role Owner
5    checkings, it also loads the dsdb_schema.
6    
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8     
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.
13    
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.
18    
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.
22    
23 */
24
25 #include "includes.h"
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"
34
35 static int schema_fsmo_init(struct ldb_module *module)
36 {
37         WERROR status;
38         TALLOC_CTX *mem_ctx;
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;
48         uint32_t i;
49         int ret;
50         static const char *schema_attrs[] = {
51                 "prefixMap",
52                 "schemaInfo",
53                 "fSMORoleOwner",
54                 NULL
55         };
56
57         if (dsdb_get_schema(module->ldb)) {
58           return ldb_next_init(module);
59         }
60
61         schema_dn = samdb_schema_dn(module->ldb);
62         if (!schema_dn) {
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);
66         }
67
68         mem_ctx = talloc_new(module);
69         if (!mem_ctx) {
70                 ldb_oom(module->ldb);
71                 return LDB_ERR_OPERATIONS_ERROR;
72         }
73
74         schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
75         if (!schema_fsmo) {
76                 ldb_oom(module->ldb);
77                 return LDB_ERR_OPERATIONS_ERROR;
78         }
79         module->private_data = schema_fsmo;
80
81         schema = talloc_zero(mem_ctx, struct dsdb_schema);
82         if (!schema) {
83                 ldb_oom(module->ldb);
84                 return LDB_ERR_OPERATIONS_ERROR;
85         }
86
87         /*
88          * setup the prefix mappings and schema info
89          */
90         ret = ldb_search(module->ldb, schema_dn,
91                          LDB_SCOPE_BASE,
92                          NULL, schema_attrs,
93                          &schema_res);
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));
98                 talloc_free(mem_ctx);
99                 return ret;
100         }
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",
110                               schema_res->count);
111                 talloc_free(mem_ctx);
112                 return LDB_ERR_CONSTRAINT_VIOLATION;
113         }
114
115         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
116         if (!prefix_val) {
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;
121         }
122         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
123         if (!info_val) {
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;
128                 }
129                 talloc_steal(mem_ctx, info_val_default.data);
130                 info_val = &info_val_default;
131         }
132
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",
137                               win_errstr(status));
138                 talloc_free(mem_ctx);
139                 return LDB_ERR_CONSTRAINT_VIOLATION;
140         }
141
142         /*
143          * load the attribute definitions
144          */
145         ret = ldb_search(module->ldb, schema_dn,
146                          LDB_SCOPE_ONELEVEL,
147                          "(objectClass=attributeSchema)", NULL,
148                          &a_res);
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);
154                 return ret;
155         }
156         talloc_steal(mem_ctx, a_res);
157
158         for (i=0; i < a_res->count; i++) {
159                 struct dsdb_attribute *sa;
160
161                 sa = talloc_zero(schema, struct dsdb_attribute);
162                 if (!sa) {
163                         ldb_oom(module->ldb);
164                         return LDB_ERR_OPERATIONS_ERROR;
165                 }
166
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),
172                                       win_errstr(status));
173                         talloc_free(mem_ctx);
174                         return LDB_ERR_CONSTRAINT_VIOLATION;
175                 }
176
177                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
178         }
179         talloc_free(a_res);
180
181         /*
182          * load the objectClass definitions
183          */
184         ret = ldb_search(module->ldb, schema_dn,
185                          LDB_SCOPE_ONELEVEL,
186                          "(objectClass=classSchema)", NULL,
187                          &c_res);
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);
193                 return ret;
194         }
195         talloc_steal(mem_ctx, c_res);
196
197         for (i=0; i < c_res->count; i++) {
198                 struct dsdb_class *sc;
199
200                 sc = talloc_zero(schema, struct dsdb_class);
201                 if (!sc) {
202                         ldb_oom(module->ldb);
203                         return LDB_ERR_OPERATIONS_ERROR;
204                 }
205
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),
211                                       win_errstr(status));
212                         talloc_free(mem_ctx);
213                         return LDB_ERR_CONSTRAINT_VIOLATION;
214                 }
215
216                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
217         }
218         talloc_free(c_res);
219
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);
227                 return ret;
228         }
229
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;
233         } else {
234                 schema_fsmo->we_are_master = false;
235         }
236
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;
240         }
241
242         talloc_steal(module, schema_fsmo);
243
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"));
247
248         talloc_free(mem_ctx);
249         return ldb_next_init(module);
250 }
251
252 static const struct ldb_module_ops schema_fsmo_ops = {
253         .name           = "schema_fsmo",
254         .init_context   = schema_fsmo_init
255 };
256
257 int schema_fsmo_module_init(void)
258 {
259         return ldb_register_module(&schema_fsmo_ops);
260 }