Until the new ldb changes land, make ldb_wait set the error string.
[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 3 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, see <http://www.gnu.org/licenses/>.
21    
22 */
23
24 #include "includes.h"
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
34 static int schema_fsmo_init(struct ldb_module *module)
35 {
36         WERROR status;
37         TALLOC_CTX *mem_ctx;
38         struct ldb_dn *schema_dn;
39         struct dsdb_schema *schema;
40         struct dsdb_schema_fsmo *schema_fsmo;
41         struct ldb_result *schema_res;
42         const struct ldb_val *prefix_val;
43         const struct ldb_val *info_val;
44         struct ldb_val info_val_default;
45         struct ldb_result *a_res;
46         struct ldb_result *c_res;
47         uint32_t i;
48         int ret;
49         static const char *schema_attrs[] = {
50                 "prefixMap",
51                 "schemaInfo",
52                 "fSMORoleOwner",
53                 NULL
54         };
55
56         if (dsdb_get_schema(module->ldb)) {
57                 return ldb_next_init(module);
58         }
59
60         schema_dn = samdb_schema_dn(module->ldb);
61         if (!schema_dn) {
62                 ldb_reset_err_string(module->ldb);
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_ERR_NO_SUCH_OBJECT) {
95                 ldb_reset_err_string(module->ldb);
96                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
97                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
98                 talloc_free(mem_ctx);
99                 return ldb_next_init(module);
100         } else if (ret != LDB_SUCCESS) {
101                 ldb_asprintf_errstring(module->ldb, 
102                                        "schema_fsmo_init: failed to search the schema head: %s",
103                                        ldb_errstring(module->ldb));
104                 talloc_free(mem_ctx);
105                 return ret;
106         }
107         talloc_steal(mem_ctx, schema_res);
108         if (schema_res->count == 0) {
109                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
110                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
111                 talloc_free(mem_ctx);
112                 return ldb_next_init(module);
113         } else if (schema_res->count > 1) {
114                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
115                               "schema_fsmo_init: [%u] schema heads found on a base search",
116                               schema_res->count);
117                 talloc_free(mem_ctx);
118                 return LDB_ERR_CONSTRAINT_VIOLATION;
119         }
120
121         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
122         if (!prefix_val) {
123                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
124                               "schema_fsmo_init: no prefixMap attribute found");
125                 talloc_free(mem_ctx);
126                 return LDB_ERR_CONSTRAINT_VIOLATION;
127         }
128         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
129         if (!info_val) {
130                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
131                 if (!info_val_default.data) {
132                         ldb_oom(module->ldb);
133                         return LDB_ERR_OPERATIONS_ERROR;
134                 }
135                 talloc_steal(mem_ctx, info_val_default.data);
136                 info_val = &info_val_default;
137         }
138
139         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
140         if (!W_ERROR_IS_OK(status)) {
141                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
142                               "schema_fsmo_init: failed to load oid mappings: %s",
143                               win_errstr(status));
144                 talloc_free(mem_ctx);
145                 return LDB_ERR_CONSTRAINT_VIOLATION;
146         }
147
148         /*
149          * load the attribute definitions
150          */
151         ret = ldb_search(module->ldb, schema_dn,
152                          LDB_SCOPE_ONELEVEL,
153                          "(objectClass=attributeSchema)", NULL,
154                          &a_res);
155         if (ret != LDB_SUCCESS) {
156                 ldb_asprintf_errstring(module->ldb, 
157                                        "schema_fsmo_init: failed to search attributeSchema objects: %s",
158                                        ldb_errstring(module->ldb));
159                 talloc_free(mem_ctx);
160                 return ret;
161         }
162         talloc_steal(mem_ctx, a_res);
163
164         for (i=0; i < a_res->count; i++) {
165                 struct dsdb_attribute *sa;
166
167                 sa = talloc_zero(schema, struct dsdb_attribute);
168                 if (!sa) {
169                         ldb_oom(module->ldb);
170                         return LDB_ERR_OPERATIONS_ERROR;
171                 }
172
173                 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
174                 if (!W_ERROR_IS_OK(status)) {
175                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
176                                       "schema_fsmo_init: failed to load attriute definition: %s:%s",
177                                       ldb_dn_get_linearized(a_res->msgs[i]->dn),
178                                       win_errstr(status));
179                         talloc_free(mem_ctx);
180                         return LDB_ERR_CONSTRAINT_VIOLATION;
181                 }
182
183                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
184         }
185         talloc_free(a_res);
186
187         /*
188          * load the objectClass definitions
189          */
190         ret = ldb_search(module->ldb, schema_dn,
191                          LDB_SCOPE_ONELEVEL,
192                          "(objectClass=classSchema)", NULL,
193                          &c_res);
194         if (ret != LDB_SUCCESS) {
195                 ldb_asprintf_errstring(module->ldb, 
196                                        "schema_fsmo_init: failed to search classSchema objects: %s",
197                                        ldb_errstring(module->ldb));
198                 talloc_free(mem_ctx);
199                 return ret;
200         }
201         talloc_steal(mem_ctx, c_res);
202
203         for (i=0; i < c_res->count; i++) {
204                 struct dsdb_class *sc;
205
206                 sc = talloc_zero(schema, struct dsdb_class);
207                 if (!sc) {
208                         ldb_oom(module->ldb);
209                         return LDB_ERR_OPERATIONS_ERROR;
210                 }
211
212                 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
213                 if (!W_ERROR_IS_OK(status)) {
214                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
215                                       "schema_fsmo_init: failed to load class definition: %s:%s",
216                                       ldb_dn_get_linearized(c_res->msgs[i]->dn),
217                                       win_errstr(status));
218                         talloc_free(mem_ctx);
219                         return LDB_ERR_CONSTRAINT_VIOLATION;
220                 }
221
222                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
223         }
224         talloc_free(c_res);
225
226         /* dsdb_set_schema() steal schema into the ldb_context */
227         ret = dsdb_set_schema(module->ldb, schema);
228         if (ret != LDB_SUCCESS) {
229                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
230                               "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
231                               ret, ldb_strerror(ret));
232                 talloc_free(mem_ctx);
233                 return ret;
234         }
235
236         schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
237         if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
238                 schema_fsmo->we_are_master = true;
239         } else {
240                 schema_fsmo->we_are_master = false;
241         }
242
243         if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
244                 ldb_oom(module->ldb);
245                 return LDB_ERR_OPERATIONS_ERROR;
246         }
247
248         talloc_steal(module, schema_fsmo);
249
250         ldb_debug(module->ldb, LDB_DEBUG_TRACE,
251                           "schema_fsmo_init: we are master: %s\n",
252                           (schema_fsmo->we_are_master?"yes":"no"));
253
254         talloc_free(mem_ctx);
255         return ldb_next_init(module);
256 }
257
258 static const struct ldb_module_ops schema_fsmo_ops = {
259         .name           = "schema_fsmo",
260         .init_context   = schema_fsmo_init
261 };
262
263 int schema_fsmo_module_init(void)
264 {
265         return ldb_register_module(&schema_fsmo_ops);
266 }