s4-dsdb: added isGlobalCatalogReady
[samba.git] / source4 / dsdb / samdb / ldb_modules / rootdse.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    rootDSE ldb module
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Simo Sorce 2005-2008
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 #include "includes.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_module.h"
26 #include "system/time.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "version.h"
29
30 struct private_data {
31         int num_controls;
32         char **controls;
33         int num_partitions;
34         struct ldb_dn **partitions;
35 };
36
37 /*
38   return 1 if a specific attribute has been requested
39 */
40 static int do_attribute(const char * const *attrs, const char *name)
41 {
42         return attrs == NULL ||
43                 ldb_attr_in_list(attrs, name) ||
44                 ldb_attr_in_list(attrs, "*");
45 }
46
47 static int do_attribute_explicit(const char * const *attrs, const char *name)
48 {
49         return attrs != NULL && ldb_attr_in_list(attrs, name);
50 }
51
52
53 /*
54   expand a DN attribute to include extended DN information if requested
55  */
56 static int expand_dn_in_message(struct ldb_module *module, struct ldb_message *msg,
57                                 const char *attrname, struct ldb_control *edn_control,
58                                 struct ldb_request *req)
59 {
60         struct ldb_dn *dn, *dn2;
61         struct ldb_val *v;
62         int ret;
63         struct ldb_request *req2;
64         char *dn_string;
65         const char *no_attrs[] = { NULL };
66         struct ldb_result *res;
67         struct ldb_extended_dn_control *edn;
68         TALLOC_CTX *tmp_ctx = talloc_new(req);
69         struct ldb_context *ldb;
70         int edn_type = 0;
71
72         ldb = ldb_module_get_ctx(module);
73
74         edn = talloc_get_type(edn_control->data, struct ldb_extended_dn_control);
75         if (edn) {
76                 edn_type = edn->type;
77         }
78
79         v = discard_const_p(struct ldb_val, ldb_msg_find_ldb_val(msg, attrname));
80         if (v == NULL) {
81                 talloc_free(tmp_ctx);
82                 return 0;
83         }
84
85         dn_string = talloc_strndup(tmp_ctx, (const char *)v->data, v->length);
86         if (dn_string == NULL) {
87                 talloc_free(tmp_ctx);
88                 return LDB_ERR_OPERATIONS_ERROR;
89         }
90
91         res = talloc_zero(tmp_ctx, struct ldb_result);
92         if (res == NULL) {
93                 talloc_free(tmp_ctx);
94                 return LDB_ERR_OPERATIONS_ERROR;
95         }
96
97         dn = ldb_dn_new(tmp_ctx, ldb, dn_string);
98         if (!ldb_dn_validate(dn)) {
99                 talloc_free(tmp_ctx);
100                 return LDB_ERR_OPERATIONS_ERROR;
101         }
102
103         ret = ldb_build_search_req(&req2, ldb, tmp_ctx,
104                                    dn,
105                                    LDB_SCOPE_BASE,
106                                    NULL,
107                                    no_attrs,
108                                    NULL,
109                                    res, ldb_search_default_callback,
110                                    req);
111         if (ret != LDB_SUCCESS) {
112                 talloc_free(tmp_ctx);
113                 return ret;
114         }
115
116
117         ret = ldb_request_add_control(req2,
118                                       LDB_CONTROL_EXTENDED_DN_OID,
119                                       edn_control->critical, edn);
120         if (ret != LDB_SUCCESS) {
121                 talloc_free(tmp_ctx);
122                 return ret;
123         }
124
125         ret = ldb_next_request(module, req2);
126         if (ret == LDB_SUCCESS) {
127                 ret = ldb_wait(req2->handle, LDB_WAIT_ALL);
128         }
129         if (ret != LDB_SUCCESS) {
130                 talloc_free(tmp_ctx);
131                 return ret;
132         }
133
134         if (!res || res->count != 1) {
135                 talloc_free(tmp_ctx);
136                 return LDB_ERR_OPERATIONS_ERROR;
137         }
138
139         dn2 = res->msgs[0]->dn;
140
141         v->data = (uint8_t *)ldb_dn_get_extended_linearized(msg->elements, dn2, edn_type);
142         v->length = strlen((char *)v->data);
143
144         if (v->data == NULL) {
145                 talloc_free(tmp_ctx);
146                 return LDB_ERR_OPERATIONS_ERROR;
147         }
148
149         talloc_free(tmp_ctx);
150
151         return 0;
152 }       
153                         
154
155 /*
156   add dynamically generated attributes to rootDSE result
157 */
158 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg, 
159                                const char * const *attrs, struct ldb_request *req)
160 {
161         struct ldb_context *ldb;
162         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
163         char **server_sasl;
164         const struct dsdb_schema *schema;
165         int *val;
166         struct ldb_control *edn_control;
167         const char *dn_attrs[] = {
168                 "configurationNamingContext",
169                 "defaultNamingContext",
170                 "dsServiceName",
171                 "rootDomainNamingContext",
172                 "schemaNamingContext",
173                 "serverName",
174                 NULL
175         };
176
177         ldb = ldb_module_get_ctx(module);
178         schema = dsdb_get_schema(ldb);
179
180         msg->dn = ldb_dn_new(msg, ldb, NULL);
181
182         /* don't return the distinduishedName, cn and name attributes */
183         ldb_msg_remove_attr(msg, "distinguishedName");
184         ldb_msg_remove_attr(msg, "cn");
185         ldb_msg_remove_attr(msg, "name");
186
187         if (do_attribute(attrs, "currentTime")) {
188                 if (ldb_msg_add_steal_string(msg, "currentTime", 
189                                              ldb_timestring(msg, time(NULL))) != 0) {
190                         goto failed;
191                 }
192         }
193
194         if (priv && do_attribute(attrs, "supportedControl")) {
195                 int i;
196                 for (i = 0; i < priv->num_controls; i++) {
197                         char *control = talloc_strdup(msg, priv->controls[i]);
198                         if (!control) {
199                                 goto failed;
200                         }
201                         if (ldb_msg_add_steal_string(msg, "supportedControl",
202                                                      control) != 0) {
203                                 goto failed;
204                         }
205                 }
206         }
207
208         if (priv && do_attribute(attrs, "namingContexts")) {
209                 int i;
210                 for (i = 0; i < priv->num_partitions; i++) {
211                         struct ldb_dn *dn = priv->partitions[i];
212                         if (ldb_msg_add_steal_string(msg, "namingContexts",
213                                                      ldb_dn_alloc_linearized(msg, dn)) != 0) {
214                                 goto failed;
215                         }
216                 }
217         }
218
219         server_sasl = talloc_get_type(ldb_get_opaque(ldb, "supportedSASLMechanims"), 
220                                        char *);
221         if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
222                 int i;
223                 for (i = 0; server_sasl && server_sasl[i]; i++) {
224                         char *sasl_name = talloc_strdup(msg, server_sasl[i]);
225                         if (!sasl_name) {
226                                 goto failed;
227                         }
228                         if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
229                                                      sasl_name) != 0) {
230                                 goto failed;
231                         }
232                 }
233         }
234
235         if (do_attribute(attrs, "highestCommittedUSN")) {
236                 uint64_t seq_num;
237                 int ret = ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &seq_num);
238                 if (ret == LDB_SUCCESS) {
239                         if (ldb_msg_add_fmt(msg, "highestCommittedUSN", 
240                                             "%llu", (unsigned long long)seq_num) != 0) {
241                                 goto failed;
242                         }
243                 }
244         }
245
246         if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
247                 struct dsdb_attribute *cur;
248                 uint32_t n = 0;
249
250                 for (cur = schema->attributes; cur; cur = cur->next) {
251                         n++;
252                 }
253
254                 if (ldb_msg_add_fmt(msg, "dsSchemaAttrCount", 
255                                     "%u", n) != 0) {
256                         goto failed;
257                 }
258         }
259
260         if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
261                 struct dsdb_class *cur;
262                 uint32_t n = 0;
263
264                 for (cur = schema->classes; cur; cur = cur->next) {
265                         n++;
266                 }
267
268                 if (ldb_msg_add_fmt(msg, "dsSchemaClassCount", 
269                                     "%u", n) != 0) {
270                         goto failed;
271                 }
272         }
273
274         if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
275                 if (ldb_msg_add_fmt(msg, "dsSchemaPrefixCount", 
276                                     "%u", schema->prefixmap->length) != 0) {
277                         goto failed;
278                 }
279         }
280
281         if (do_attribute_explicit(attrs, "validFSMOs")) {
282                 const struct dsdb_naming_fsmo *naming_fsmo;
283                 const struct dsdb_pdc_fsmo *pdc_fsmo;
284                 const char *dn_str;
285
286                 if (schema && schema->fsmo.we_are_master) {
287                         dn_str = ldb_dn_get_linearized(samdb_schema_dn(ldb));
288                         if (dn_str && dn_str[0]) {
289                                 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
290                                         goto failed;
291                                 }
292                         }
293                 }
294
295                 naming_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_naming_fsmo"),
296                                               struct dsdb_naming_fsmo);
297                 if (naming_fsmo && naming_fsmo->we_are_master) {
298                         dn_str = ldb_dn_get_linearized(samdb_partitions_dn(ldb, msg));
299                         if (dn_str && dn_str[0]) {
300                                 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
301                                         goto failed;
302                                 }
303                         }
304                 }
305
306                 pdc_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_pdc_fsmo"),
307                                            struct dsdb_pdc_fsmo);
308                 if (pdc_fsmo && pdc_fsmo->we_are_master) {
309                         dn_str = ldb_dn_get_linearized(samdb_base_dn(ldb));
310                         if (dn_str && dn_str[0]) {
311                                 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
312                                         goto failed;
313                                 }
314                         }
315                 }
316         }
317
318         if (do_attribute_explicit(attrs, "vendorVersion")) {
319                 if (ldb_msg_add_fmt(msg, "vendorVersion", 
320                                     "%s", SAMBA_VERSION_STRING) != 0) {
321                         goto failed;
322                 }
323         }
324
325         if (priv && do_attribute(attrs, "domainFunctionality")) {
326                 if (ldb_msg_add_fmt(msg, "domainFunctionality", 
327                                     "%d", dsdb_functional_level(ldb)) != 0) {
328                         goto failed;
329                 }
330         }
331
332         if (priv && do_attribute(attrs, "forestFunctionality")
333             && (val = talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int))) {
334                 if (ldb_msg_add_fmt(msg, "forestFunctionality", 
335                                     "%d", *val) != 0) {
336                         goto failed;
337                 }
338         }
339
340         if (priv && do_attribute(attrs, "domainControllerFunctionality")
341             && (val = talloc_get_type(ldb_get_opaque(ldb, "domainControllerFunctionality"), int))) {
342                 if (ldb_msg_add_fmt(msg, "domainControllerFunctionality", 
343                                     "%d", *val) != 0) {
344                         goto failed;
345                 }
346         }
347
348         edn_control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
349
350         /* if the client sent us the EXTENDED_DN control then we need
351            to expand the DNs to have GUID and SID. W2K8 join relies on
352            this */
353         if (edn_control) {
354                 int i, ret;
355                 for (i=0; dn_attrs[i]; i++) {
356                         if (!do_attribute(attrs, dn_attrs[i])) continue;
357                         ret = expand_dn_in_message(module, msg, dn_attrs[i],
358                                                    edn_control, req);
359                         if (ret != LDB_SUCCESS) {
360                                 DEBUG(0,(__location__ ": Failed to expand DN in rootDSE for %s\n",
361                                          dn_attrs[i]));
362                                 goto failed;
363                         }
364                 }
365         }
366
367         if (do_attribute_explicit(attrs, "isGlobalCatalogReady")) {
368                 /* MS-ADTS 3.1.1.3.2.10
369                    Note, we should only return true here is we have
370                    completed at least one synchronisation. As both
371                    provision and vampire do a full sync, this means we
372                    can return true is the gc bit is set in the NTDSDSA
373                    options */
374                 if (ldb_msg_add_fmt(msg, "isGlobalCatalogReady",
375                                     "%s", samdb_is_gc(ldb)?"TRUE":"FALSE") != 0) {
376                         goto failed;
377                 }
378         }
379
380         /* TODO: lots more dynamic attributes should be added here */
381
382         return LDB_SUCCESS;
383
384 failed:
385         return LDB_ERR_OPERATIONS_ERROR;
386 }
387
388 /*
389   handle search requests
390 */
391
392 struct rootdse_context {
393         struct ldb_module *module;
394         struct ldb_request *req;
395 };
396
397 static struct rootdse_context *rootdse_init_context(struct ldb_module *module,
398                                                     struct ldb_request *req)
399 {
400         struct ldb_context *ldb;
401         struct rootdse_context *ac;
402
403         ldb = ldb_module_get_ctx(module);
404
405         ac = talloc_zero(req, struct rootdse_context);
406         if (ac == NULL) {
407                 ldb_set_errstring(ldb, "Out of Memory");
408                 return NULL;
409         }
410
411         ac->module = module;
412         ac->req = req;
413
414         return ac;
415 }
416
417 static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
418 {
419         struct rootdse_context *ac;
420         int ret;
421
422         ac = talloc_get_type(req->context, struct rootdse_context);
423
424         if (!ares) {
425                 return ldb_module_done(ac->req, NULL, NULL,
426                                         LDB_ERR_OPERATIONS_ERROR);
427         }
428         if (ares->error != LDB_SUCCESS) {
429                 return ldb_module_done(ac->req, ares->controls,
430                                         ares->response, ares->error);
431         }
432
433         switch (ares->type) {
434         case LDB_REPLY_ENTRY:
435                 /*
436                  * if the client explicit asks for the 'netlogon' attribute
437                  * the reply_entry needs to be skipped
438                  */
439                 if (ac->req->op.search.attrs &&
440                     ldb_attr_in_list(ac->req->op.search.attrs, "netlogon")) {
441                         talloc_free(ares);
442                         return LDB_SUCCESS;
443                 }
444
445                 /* for each record returned post-process to add any dynamic
446                    attributes that have been asked for */
447                 ret = rootdse_add_dynamic(ac->module, ares->message,
448                                           ac->req->op.search.attrs, ac->req);
449                 if (ret != LDB_SUCCESS) {
450                         talloc_free(ares);
451                         return ldb_module_done(ac->req, NULL, NULL, ret);
452                 }
453
454                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
455
456         case LDB_REPLY_REFERRAL:
457                 /* should we allow the backend to return referrals in this case
458                  * ?? */
459                 break;
460
461         case LDB_REPLY_DONE:
462                 return ldb_module_done(ac->req, ares->controls,
463                                         ares->response, ares->error);
464         }
465
466         talloc_free(ares);
467         return LDB_SUCCESS;
468 }
469
470 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
471 {
472         struct ldb_context *ldb;
473         struct rootdse_context *ac;
474         struct ldb_request *down_req;
475         int ret;
476
477         ldb = ldb_module_get_ctx(module);
478
479         /* see if its for the rootDSE - only a base search on the "" DN qualifies */
480         if (!(req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base))) {
481                 /* Otherwise, pass down to the rest of the stack */
482                 return ldb_next_request(module, req);
483         }
484
485         ac = rootdse_init_context(module, req);
486         if (ac == NULL) {
487                 return LDB_ERR_OPERATIONS_ERROR;
488         }
489
490         /* in our db we store the rootDSE with a DN of @ROOTDSE */
491         ret = ldb_build_search_req(&down_req, ldb, ac,
492                                         ldb_dn_new(ac, ldb, "@ROOTDSE"),
493                                         LDB_SCOPE_BASE,
494                                         NULL,
495                                         req->op.search.attrs,
496                                         NULL,/* for now skip the controls from the client */
497                                         ac, rootdse_callback,
498                                         req);
499         if (ret != LDB_SUCCESS) {
500                 return ret;
501         }
502
503         return ldb_next_request(module, down_req);
504 }
505
506 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
507 {
508         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
509         char **list;
510
511         list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
512         if (!list) {
513                 return LDB_ERR_OPERATIONS_ERROR;
514         }
515
516         list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
517         if (!list[priv->num_controls]) {
518                 return LDB_ERR_OPERATIONS_ERROR;
519         }
520
521         priv->num_controls += 1;
522         priv->controls = list;
523
524         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
525 }
526
527 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
528 {
529         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
530         struct ldb_dn **list;
531
532         list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
533         if (!list) {
534                 return LDB_ERR_OPERATIONS_ERROR;
535         }
536
537         list[priv->num_partitions] = ldb_dn_copy(list, req->op.reg_partition.dn);
538         if (!list[priv->num_partitions]) {
539                 return LDB_ERR_OPERATIONS_ERROR;
540         }
541
542         priv->num_partitions += 1;
543         priv->partitions = list;
544
545         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
546 }
547
548
549 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
550 {
551         switch (req->operation) {
552
553         case LDB_REQ_REGISTER_CONTROL:
554                 return rootdse_register_control(module, req);
555         case LDB_REQ_REGISTER_PARTITION:
556                 return rootdse_register_partition(module, req);
557
558         default:
559                 break;
560         }
561         return ldb_next_request(module, req);
562 }
563
564 static int rootdse_init(struct ldb_module *module)
565 {
566         int ret;
567         struct ldb_context *ldb;
568         struct ldb_result *res;
569         struct private_data *data;
570         const char *attrs[] = { "msDS-Behavior-Version", NULL };
571         const char *ds_attrs[] = { "dsServiceName", NULL };
572         TALLOC_CTX *mem_ctx;
573
574         ldb = ldb_module_get_ctx(module);
575
576         data = talloc_zero(module, struct private_data);
577         if (data == NULL) {
578                 return -1;
579         }
580
581         data->num_controls = 0;
582         data->controls = NULL;
583         data->num_partitions = 0;
584         data->partitions = NULL;
585         ldb_module_set_private(module, data);
586
587         ldb_set_default_dns(ldb);
588
589         ret = ldb_next_init(module);
590
591         if (ret) {
592                 return ret;
593         }
594
595         mem_ctx = talloc_new(data);
596         if (!mem_ctx) {
597                 ldb_oom(ldb);
598                 return LDB_ERR_OPERATIONS_ERROR;
599         }
600
601         /* Now that the partitions are set up, do a search for:
602            - domainControllerFunctionality
603            - domainFunctionality
604            - forestFunctionality
605
606            Then stuff these values into an opaque
607         */
608         ret = ldb_search(ldb, mem_ctx, &res,
609                          ldb_get_default_basedn(ldb),
610                          LDB_SCOPE_BASE, attrs, NULL);
611         if (ret == LDB_SUCCESS && res->count == 1) {
612                 int domain_behaviour_version
613                         = ldb_msg_find_attr_as_int(res->msgs[0], 
614                                                    "msDS-Behavior-Version", -1);
615                 if (domain_behaviour_version != -1) {
616                         int *val = talloc(ldb, int);
617                         if (!val) {
618                                 ldb_oom(ldb);
619                                 talloc_free(mem_ctx);
620                                 return LDB_ERR_OPERATIONS_ERROR;
621                         }
622                         *val = domain_behaviour_version;
623                         ret = ldb_set_opaque(ldb, "domainFunctionality", val);
624                         if (ret != LDB_SUCCESS) {
625                                 talloc_free(mem_ctx);
626                                 return ret;
627                         }
628                 }
629         }
630
631         ret = ldb_search(ldb, mem_ctx, &res,
632                          samdb_partitions_dn(ldb, mem_ctx),
633                          LDB_SCOPE_BASE, attrs, NULL);
634         if (ret == LDB_SUCCESS && res->count == 1) {
635                 int forest_behaviour_version
636                         = ldb_msg_find_attr_as_int(res->msgs[0], 
637                                                    "msDS-Behavior-Version", -1);
638                 if (forest_behaviour_version != -1) {
639                         int *val = talloc(ldb, int);
640                         if (!val) {
641                                 ldb_oom(ldb);
642                                 talloc_free(mem_ctx);
643                                 return LDB_ERR_OPERATIONS_ERROR;
644                         }
645                         *val = forest_behaviour_version;
646                         ret = ldb_set_opaque(ldb, "forestFunctionality", val);
647                         if (ret != LDB_SUCCESS) {
648                                 talloc_free(mem_ctx);
649                                 return ret;
650                         }
651                 }
652         }
653
654         ret = ldb_search(ldb, mem_ctx, &res,
655                          ldb_dn_new(mem_ctx, ldb, ""),
656                          LDB_SCOPE_BASE, ds_attrs, NULL);
657         if (ret == LDB_SUCCESS && res->count == 1) {
658                 struct ldb_dn *ds_dn
659                         = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], 
660                                                   "dsServiceName");
661                 if (ds_dn) {
662                         ret = ldb_search(ldb, mem_ctx, &res, ds_dn, 
663                                          LDB_SCOPE_BASE, attrs, NULL);
664                         if (ret == LDB_SUCCESS && res->count == 1) {
665                                 int domain_controller_behaviour_version
666                                         = ldb_msg_find_attr_as_int(res->msgs[0], 
667                                                                    "msDS-Behavior-Version", -1);
668                                 if (domain_controller_behaviour_version != -1) {
669                                         int *val = talloc(ldb, int);
670                                         if (!val) {
671                                                 ldb_oom(ldb);
672                                                 talloc_free(mem_ctx);
673                                         return LDB_ERR_OPERATIONS_ERROR;
674                                         }
675                                         *val = domain_controller_behaviour_version;
676                                         ret = ldb_set_opaque(ldb, 
677                                                              "domainControllerFunctionality", val);
678                                         if (ret != LDB_SUCCESS) {
679                                                 talloc_free(mem_ctx);
680                                                 return ret;
681                                         }
682                                 }
683                         }
684                 }
685         }
686
687         talloc_free(mem_ctx);
688         
689         return LDB_SUCCESS;
690 }
691
692 static int rootdse_modify(struct ldb_module *module, struct ldb_request *req)
693 {
694         struct ldb_context *ldb;
695         struct ldb_result *ext_res;
696         int ret;
697         struct ldb_dn *schema_dn;
698         struct ldb_message_element *schemaUpdateNowAttr;
699         
700         /*
701                 If dn is not "" we should let it pass through
702         */
703         if (!ldb_dn_is_null(req->op.mod.message->dn)) {
704                 return ldb_next_request(module, req);
705         }
706
707         ldb = ldb_module_get_ctx(module);
708
709         /*
710                 dn is empty so check for schemaUpdateNow attribute
711                 "The type of modification and values specified in the LDAP modify operation do not matter." MSDN
712         */
713         schemaUpdateNowAttr = ldb_msg_find_element(req->op.mod.message, "schemaUpdateNow");
714         if (!schemaUpdateNowAttr) {
715                 return LDB_ERR_OPERATIONS_ERROR;
716         }
717
718         schema_dn = samdb_schema_dn(ldb);
719         if (!schema_dn) {
720                 ldb_reset_err_string(ldb);
721                 ldb_debug(ldb, LDB_DEBUG_WARNING,
722                           "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
723                 return ldb_next_request(module, req);
724         }
725
726         ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, schema_dn, &ext_res);
727         if (ret != LDB_SUCCESS) {
728                 return LDB_ERR_OPERATIONS_ERROR;
729         }
730         
731         talloc_free(ext_res);
732         return ldb_module_done(req, NULL, NULL, ret);
733 }
734
735 _PUBLIC_ const struct ldb_module_ops ldb_rootdse_module_ops = {
736         .name           = "rootdse",
737         .init_context   = rootdse_init,
738         .search         = rootdse_search,
739         .request        = rootdse_request,
740         .modify         = rootdse_modify
741 };