Make Samba4 pass the NET-API-BECOMEDC test against Win2k3 (again).
[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 #include "param/param.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_reset_err_string(module->ldb);
64                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
65                           "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
66                 return ldb_next_init(module);
67         }
68
69         mem_ctx = talloc_new(module);
70         if (!mem_ctx) {
71                 ldb_oom(module->ldb);
72                 return LDB_ERR_OPERATIONS_ERROR;
73         }
74
75         schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
76         if (!schema_fsmo) {
77                 ldb_oom(module->ldb);
78                 return LDB_ERR_OPERATIONS_ERROR;
79         }
80         module->private_data = schema_fsmo;
81
82         schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")));
83         if (!schema) {
84                 ldb_oom(module->ldb);
85                 return LDB_ERR_OPERATIONS_ERROR;
86         }
87
88         /*
89          * setup the prefix mappings and schema info
90          */
91         ret = ldb_search(module->ldb, schema_dn,
92                          LDB_SCOPE_BASE,
93                          NULL, schema_attrs,
94                          &schema_res);
95         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
96                 ldb_reset_err_string(module->ldb);
97                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
98                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
99                 talloc_free(mem_ctx);
100                 return ldb_next_init(module);
101         } else if (ret != LDB_SUCCESS) {
102                 ldb_asprintf_errstring(module->ldb, 
103                                        "schema_fsmo_init: failed to search the schema head: %s",
104                                        ldb_errstring(module->ldb));
105                 talloc_free(mem_ctx);
106                 return ret;
107         }
108         talloc_steal(mem_ctx, schema_res);
109         if (schema_res->count == 0) {
110                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
111                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
112                 talloc_free(mem_ctx);
113                 return ldb_next_init(module);
114         } else if (schema_res->count > 1) {
115                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
116                               "schema_fsmo_init: [%u] schema heads found on a base search",
117                               schema_res->count);
118                 talloc_free(mem_ctx);
119                 return LDB_ERR_CONSTRAINT_VIOLATION;
120         }
121
122         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
123         if (!prefix_val) {
124                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
125                               "schema_fsmo_init: no prefixMap attribute found");
126                 talloc_free(mem_ctx);
127                 return LDB_ERR_CONSTRAINT_VIOLATION;
128         }
129         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
130         if (!info_val) {
131                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
132                 if (!info_val_default.data) {
133                         ldb_oom(module->ldb);
134                         return LDB_ERR_OPERATIONS_ERROR;
135                 }
136                 talloc_steal(mem_ctx, info_val_default.data);
137                 info_val = &info_val_default;
138         }
139
140         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
141         if (!W_ERROR_IS_OK(status)) {
142                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
143                               "schema_fsmo_init: failed to load oid mappings: %s",
144                               win_errstr(status));
145                 talloc_free(mem_ctx);
146                 return LDB_ERR_CONSTRAINT_VIOLATION;
147         }
148
149         /*
150          * load the attribute definitions
151          */
152         ret = ldb_search(module->ldb, schema_dn,
153                          LDB_SCOPE_ONELEVEL,
154                          "(objectClass=attributeSchema)", NULL,
155                          &a_res);
156         if (ret != LDB_SUCCESS) {
157                 ldb_asprintf_errstring(module->ldb, 
158                                        "schema_fsmo_init: failed to search attributeSchema objects: %s",
159                                        ldb_errstring(module->ldb));
160                 talloc_free(mem_ctx);
161                 return ret;
162         }
163         talloc_steal(mem_ctx, a_res);
164
165         for (i=0; i < a_res->count; i++) {
166                 struct dsdb_attribute *sa;
167
168                 sa = talloc_zero(schema, struct dsdb_attribute);
169                 if (!sa) {
170                         ldb_oom(module->ldb);
171                         return LDB_ERR_OPERATIONS_ERROR;
172                 }
173
174                 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
175                 if (!W_ERROR_IS_OK(status)) {
176                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
177                                       "schema_fsmo_init: failed to load attriute definition: %s:%s",
178                                       ldb_dn_get_linearized(a_res->msgs[i]->dn),
179                                       win_errstr(status));
180                         talloc_free(mem_ctx);
181                         return LDB_ERR_CONSTRAINT_VIOLATION;
182                 }
183
184                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
185         }
186         talloc_free(a_res);
187
188         /*
189          * load the objectClass definitions
190          */
191         ret = ldb_search(module->ldb, schema_dn,
192                          LDB_SCOPE_ONELEVEL,
193                          "(objectClass=classSchema)", NULL,
194                          &c_res);
195         if (ret != LDB_SUCCESS) {
196                 ldb_asprintf_errstring(module->ldb, 
197                                        "schema_fsmo_init: failed to search classSchema objects: %s",
198                                        ldb_errstring(module->ldb));
199                 talloc_free(mem_ctx);
200                 return ret;
201         }
202         talloc_steal(mem_ctx, c_res);
203
204         for (i=0; i < c_res->count; i++) {
205                 struct dsdb_class *sc;
206
207                 sc = talloc_zero(schema, struct dsdb_class);
208                 if (!sc) {
209                         ldb_oom(module->ldb);
210                         return LDB_ERR_OPERATIONS_ERROR;
211                 }
212
213                 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
214                 if (!W_ERROR_IS_OK(status)) {
215                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
216                                       "schema_fsmo_init: failed to load class definition: %s:%s",
217                                       ldb_dn_get_linearized(c_res->msgs[i]->dn),
218                                       win_errstr(status));
219                         talloc_free(mem_ctx);
220                         return LDB_ERR_CONSTRAINT_VIOLATION;
221                 }
222
223                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
224         }
225         talloc_free(c_res);
226
227         /* dsdb_set_schema() steal schema into the ldb_context */
228         ret = dsdb_set_schema(module->ldb, schema);
229         if (ret != LDB_SUCCESS) {
230                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
231                               "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
232                               ret, ldb_strerror(ret));
233                 talloc_free(mem_ctx);
234                 return ret;
235         }
236
237         schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
238         if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
239                 schema_fsmo->we_are_master = true;
240         } else {
241                 schema_fsmo->we_are_master = false;
242         }
243
244         if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
245                 ldb_oom(module->ldb);
246                 return LDB_ERR_OPERATIONS_ERROR;
247         }
248
249         talloc_steal(module, schema_fsmo);
250
251         ldb_debug(module->ldb, LDB_DEBUG_TRACE,
252                           "schema_fsmo_init: we are master: %s\n",
253                           (schema_fsmo->we_are_master?"yes":"no"));
254
255         talloc_free(mem_ctx);
256         return ldb_next_init(module);
257 }
258
259 _PUBLIC_ const struct ldb_module_ops ldb_schema_fsmo_module_ops = {
260         .name           = "schema_fsmo",
261         .init_context   = schema_fsmo_init
262 };