Merge branch 'master' of ssh://git.samba.org/data/git/samba
[kai/samba.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 generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
36                                   const struct dsdb_schema *schema);
37 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
38                                    const struct dsdb_schema *schema);
39 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
40                                     const struct dsdb_schema *schema);
41
42 static const struct {
43         const char *attr;
44         int (*fn)(struct ldb_context *, struct ldb_message *, const struct dsdb_schema *);
45 } generated_attrs[] = {
46         {
47                 .attr = "objectClasses",
48                 .fn = generate_objectClasses
49         },
50         {
51                 .attr = "attributeTypes",
52                 .fn = generate_attributeTypes
53         },
54         {
55                 .attr = "dITContentRules",
56                 .fn = generate_dITContentRules
57         }
58 };
59
60 struct schema_fsmo_private_data {
61         struct ldb_dn *aggregate_dn;
62 };
63
64 struct schema_fsmo_search_data {
65         struct schema_fsmo_private_data *module_context;
66         struct ldb_request *orig_req;
67 };
68
69 static int schema_fsmo_init(struct ldb_module *module)
70 {
71         TALLOC_CTX *mem_ctx;
72         struct ldb_dn *schema_dn;
73         struct dsdb_schema *schema;
74         char *error_string = NULL;
75         int ret;
76         struct schema_fsmo_private_data *data;
77
78         schema_dn = samdb_schema_dn(module->ldb);
79         if (!schema_dn) {
80                 ldb_reset_err_string(module->ldb);
81                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
82                           "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
83                 return ldb_next_init(module);
84         }
85
86         data = talloc(module, struct schema_fsmo_private_data);
87         if (data == NULL) {
88                 ldb_oom(module->ldb);
89                 return LDB_ERR_OPERATIONS_ERROR;
90         }
91
92         /* Check to see if this is a result on the CN=Aggregate schema */
93         data->aggregate_dn = ldb_dn_copy(data, schema_dn);
94         if (!ldb_dn_add_child_fmt(data->aggregate_dn, "CN=Aggregate")) {
95                 ldb_oom(module->ldb);
96                 return LDB_ERR_OPERATIONS_ERROR;
97         }
98
99         module->private_data = data;
100
101         if (dsdb_get_schema(module->ldb)) {
102                 return ldb_next_init(module);
103         }
104
105         mem_ctx = talloc_new(module);
106         if (!mem_ctx) {
107                 ldb_oom(module->ldb);
108                 return LDB_ERR_OPERATIONS_ERROR;
109         }
110
111         ret = dsdb_schema_from_schema_dn(mem_ctx, module->ldb,
112                                          lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
113                                          schema_dn, &schema, &error_string);
114
115         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
116                 ldb_reset_err_string(module->ldb);
117                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
118                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
119                 talloc_free(mem_ctx);
120                 return ldb_next_init(module);
121         }
122
123         if (ret != LDB_SUCCESS) {
124                 ldb_asprintf_errstring(module->ldb, 
125                                        "schema_fsmo_init: dsdb_schema load failed: %s",
126                                        error_string);
127                 talloc_free(mem_ctx);
128                 return ret;
129         }
130
131         /* dsdb_set_schema() steal schema into the ldb_context */
132         ret = dsdb_set_schema(module->ldb, schema);
133         if (ret != LDB_SUCCESS) {
134                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
135                               "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
136                               ret, ldb_strerror(ret));
137                 talloc_free(mem_ctx);
138                 return ret;
139         }
140
141         talloc_free(mem_ctx);
142         return ldb_next_init(module);
143 }
144
145 static int schema_fsmo_add(struct ldb_module *module, struct ldb_request *req)
146 {
147         struct dsdb_schema *schema;
148         const char *attributeID = NULL;
149         const char *governsID = NULL;
150         const char *oid_attr = NULL;
151         const char *oid = NULL;
152         uint32_t id32;
153         WERROR status;
154
155         schema = dsdb_get_schema(module->ldb);
156         if (!schema) {
157                 return ldb_next_request(module, req);
158         }
159
160         if (!schema->fsmo.we_are_master) {
161                 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
162                           "schema_fsmo_add: we are not master: reject request\n");
163                 return LDB_ERR_UNWILLING_TO_PERFORM;
164         }
165
166         attributeID = samdb_result_string(req->op.add.message, "attributeID", NULL);
167         governsID = samdb_result_string(req->op.add.message, "governsID", NULL);
168
169         if (attributeID) {
170                 oid_attr = "attributeID";
171                 oid = attributeID;
172         } else if (governsID) {
173                 oid_attr = "governsID";
174                 oid = governsID;
175         }
176
177         if (!oid) {
178                 return ldb_next_request(module, req);
179         }
180
181         status = dsdb_map_oid2int(schema, oid, &id32);
182         if (W_ERROR_IS_OK(status)) {
183                 return ldb_next_request(module, req);
184         } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
185                 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
186                           "schema_fsmo_add: failed to map %s[%s]: %s\n",
187                           oid_attr, oid, win_errstr(status));
188                 return LDB_ERR_UNWILLING_TO_PERFORM;
189         }
190
191         status = dsdb_create_prefix_mapping(module->ldb, schema, oid);
192         if (!W_ERROR_IS_OK(status)) {
193                 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
194                           "schema_fsmo_add: failed to create prefix mapping for %s[%s]: %s\n",
195                           oid_attr, oid, win_errstr(status));
196                 return LDB_ERR_UNWILLING_TO_PERFORM;
197         }
198
199         return ldb_next_request(module, req);
200 }
201
202 static int schema_fsmo_extended(struct ldb_module *module, struct ldb_request *req)
203 {
204         struct ldb_dn *schema_dn;
205         struct dsdb_schema *schema;
206         char *error_string = NULL;
207         int ret;
208         TALLOC_CTX *mem_ctx;
209         
210         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) != 0) {
211                 return ldb_next_request(module, req);
212         }
213         
214         schema_dn = samdb_schema_dn(module->ldb);
215         if (!schema_dn) {
216                 ldb_reset_err_string(module->ldb);
217                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
218                           "schema_fsmo_extended: no schema dn present: (skip schema loading)\n");
219                 return ldb_next_request(module, req);
220         }
221         
222         mem_ctx = talloc_new(module);
223         if (!mem_ctx) {
224                 ldb_oom(module->ldb);
225                 return LDB_ERR_OPERATIONS_ERROR;
226         }
227         
228         ret = dsdb_schema_from_schema_dn(mem_ctx, module->ldb,
229                                          lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
230                                          schema_dn, &schema, &error_string);
231
232         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
233                 ldb_reset_err_string(module->ldb);
234                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
235                           "schema_fsmo_extended: no schema head present: (skip schema loading)\n");
236                 talloc_free(mem_ctx);
237                 return ldb_next_request(module, req);
238         }
239
240         if (ret != LDB_SUCCESS) {
241                 ldb_asprintf_errstring(module->ldb, 
242                                        "schema_fsmo_extended: dsdb_schema load failed: %s",
243                                        error_string);
244                 talloc_free(mem_ctx);
245                 return ldb_next_request(module, req);
246         }
247
248         /* Replace the old schema*/
249         ret = dsdb_set_schema(module->ldb, schema);
250         if (ret != LDB_SUCCESS) {
251                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
252                               "schema_fsmo_extended: dsdb_set_schema() failed: %d:%s",
253                               ret, ldb_strerror(ret));
254                 talloc_free(mem_ctx);
255                 return ret;
256         }
257
258         talloc_free(mem_ctx);
259         return LDB_SUCCESS;
260 }
261
262 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
263                                   const struct dsdb_schema *schema) 
264 {
265         const struct dsdb_class *class;
266         int ret;
267
268         for (class = schema->classes; class; class = class->next) {
269                 ret = ldb_msg_add_string(msg, "objectClasses", schema_class_to_description(msg, class));
270                 if (ret != LDB_SUCCESS) {
271                         return ret;
272                 }
273         }
274         return LDB_SUCCESS;
275 }
276 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
277                                   const struct dsdb_schema *schema) 
278 {
279         const struct dsdb_attribute *attribute;
280         int ret;
281         
282         for (attribute = schema->attributes; attribute; attribute = attribute->next) {
283                 ret = ldb_msg_add_string(msg, "attributeTypes", schema_attribute_to_description(msg, attribute));
284                 if (ret != LDB_SUCCESS) {
285                         return ret;
286                 }
287         }
288         return LDB_SUCCESS;
289 }
290
291 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
292                                     const struct dsdb_schema *schema) 
293 {
294         const struct dsdb_class *class;
295         int ret;
296
297         for (class = schema->classes; class; class = class->next) {
298                 if (class->auxiliaryClass || class->systemAuxiliaryClass) {
299                         char *ditcontentrule = schema_class_to_dITContentRule(msg, class, schema);
300                         if (!ditcontentrule) {
301                                 ldb_oom(ldb);
302                                 return LDB_ERR_OPERATIONS_ERROR;
303                         }
304                         ret = ldb_msg_add_steal_string(msg, "dITContentRules", ditcontentrule);
305                         if (ret != LDB_SUCCESS) {
306                                 return ret;
307                         }
308                 }
309         }
310         return LDB_SUCCESS;
311 }
312
313
314
315 /* Add objectClasses, attributeTypes and dITContentRules from the
316    schema object (they are not stored in the database)
317  */
318 static int schema_fsmo_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares) 
319 {
320         const struct dsdb_schema *schema = dsdb_get_schema(ldb);
321         struct schema_fsmo_search_data *search_data = talloc_get_type(context, struct schema_fsmo_search_data);
322         struct ldb_request *orig_req = search_data->orig_req;
323         TALLOC_CTX *mem_ctx;
324         int i, ret;
325
326         /* Only entries are interesting, and we handle the case of the parent seperatly */
327         if (ares->type != LDB_REPLY_ENTRY) {
328                 return orig_req->callback(ldb, orig_req->context, ares);
329         }
330
331         if (ldb_dn_compare(ares->message->dn, search_data->module_context->aggregate_dn) != 0) {
332                 talloc_free(mem_ctx);
333                 return orig_req->callback(ldb, orig_req->context, ares);
334         }
335
336         mem_ctx = talloc_new(ares);
337         if (!mem_ctx) {
338                 ldb_oom(ldb);
339                 return LDB_ERR_OPERATIONS_ERROR;
340         }
341
342         for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
343                 if (ldb_attr_in_list(orig_req->op.search.attrs, generated_attrs[i].attr)) {
344                         ret = generated_attrs[i].fn(ldb, ares->message, schema);
345                         if (ret != LDB_SUCCESS) {
346                                 return ret;
347                         }
348                 }
349         }
350
351         talloc_free(mem_ctx);
352         return orig_req->callback(ldb, orig_req->context, ares);
353 }
354
355 /* search */
356 static int schema_fsmo_search(struct ldb_module *module, struct ldb_request *req)
357 {
358         int i, ret;
359         struct schema_fsmo_search_data *search_context;
360         struct ldb_request *down_req;
361         struct dsdb_schema *schema = dsdb_get_schema(module->ldb);
362
363         if (!schema || !module->private_data) {
364                 /* If there is no schema, there is little we can do */
365                 return ldb_next_request(module, req);
366         }
367         for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
368                 if (ldb_attr_in_list(req->op.search.attrs, generated_attrs[i].attr)) {
369                         break;
370                 }
371         }
372         if (i == ARRAY_SIZE(generated_attrs)) {
373                 /* No request for a generated attr found, nothing to
374                  * see here, move along... */
375                 return ldb_next_request(module, req);
376         }
377
378         search_context = talloc(req, struct schema_fsmo_search_data);
379         if (!search_context) {
380                 ldb_oom(module->ldb);
381                 return LDB_ERR_OPERATIONS_ERROR;
382         }
383         down_req = talloc(req, struct ldb_request);     
384         if (!down_req) {
385                 ldb_oom(module->ldb);
386                 return LDB_ERR_OPERATIONS_ERROR;
387         }
388         
389         *down_req = *req;
390         search_context->orig_req = req;
391         search_context->module_context = talloc_get_type(module->private_data, struct schema_fsmo_private_data);
392         down_req->context = search_context;
393
394         down_req->callback = schema_fsmo_search_callback;
395
396         ret = ldb_next_request(module, down_req);
397
398         /* do not free down_req as the call results may be linked to it,
399          * it will be freed when the upper level request get freed */
400         if (ret == LDB_SUCCESS) {
401                 req->handle = down_req->handle;
402         }
403         return ret;
404 }
405
406
407 _PUBLIC_ const struct ldb_module_ops ldb_schema_fsmo_module_ops = {
408         .name           = "schema_fsmo",
409         .init_context   = schema_fsmo_init,
410         .add            = schema_fsmo_add,
411         .extended       = schema_fsmo_extended,
412         .search         = schema_fsmo_search
413 };