s4-messaging Rename messaging -> imessaging
[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 <ldb.h>
25 #include <ldb_module.h>
26 #include "system/time.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "version.h"
29 #include "dsdb/samdb/ldb_modules/util.h"
30 #include "libcli/security/security.h"
31 #include "librpc/ndr/libndr.h"
32 #include "auth/auth.h"
33 #include "param/param.h"
34 #include "lib/messaging/irpc.h"
35 #include "librpc/gen_ndr/ndr_irpc_c.h"
36
37 struct private_data {
38         unsigned int num_controls;
39         char **controls;
40         unsigned int num_partitions;
41         struct ldb_dn **partitions;
42         bool block_anonymous;
43 };
44
45 /*
46   return 1 if a specific attribute has been requested
47 */
48 static int do_attribute(const char * const *attrs, const char *name)
49 {
50         return attrs == NULL ||
51                 ldb_attr_in_list(attrs, name) ||
52                 ldb_attr_in_list(attrs, "*");
53 }
54
55 static int do_attribute_explicit(const char * const *attrs, const char *name)
56 {
57         return attrs != NULL && ldb_attr_in_list(attrs, name);
58 }
59
60
61 /*
62   expand a DN attribute to include extended DN information if requested
63  */
64 static int expand_dn_in_message(struct ldb_module *module, struct ldb_message *msg,
65                                 const char *attrname, struct ldb_control *edn_control,
66                                 struct ldb_request *req)
67 {
68         struct ldb_dn *dn, *dn2;
69         struct ldb_val *v;
70         int ret;
71         struct ldb_request *req2;
72         char *dn_string;
73         const char *no_attrs[] = { NULL };
74         struct ldb_result *res;
75         struct ldb_extended_dn_control *edn;
76         TALLOC_CTX *tmp_ctx = talloc_new(req);
77         struct ldb_context *ldb;
78         int edn_type = 0;
79
80         ldb = ldb_module_get_ctx(module);
81
82         edn = talloc_get_type(edn_control->data, struct ldb_extended_dn_control);
83         if (edn) {
84                 edn_type = edn->type;
85         }
86
87         v = discard_const_p(struct ldb_val, ldb_msg_find_ldb_val(msg, attrname));
88         if (v == NULL) {
89                 talloc_free(tmp_ctx);
90                 return LDB_SUCCESS;
91         }
92
93         dn_string = talloc_strndup(tmp_ctx, (const char *)v->data, v->length);
94         if (dn_string == NULL) {
95                 talloc_free(tmp_ctx);
96                 return ldb_operr(ldb);
97         }
98
99         res = talloc_zero(tmp_ctx, struct ldb_result);
100         if (res == NULL) {
101                 talloc_free(tmp_ctx);
102                 return ldb_operr(ldb);
103         }
104
105         dn = ldb_dn_new(tmp_ctx, ldb, dn_string);
106         if (dn == NULL) {
107                 talloc_free(tmp_ctx);
108                 return ldb_operr(ldb);
109         }
110
111         ret = ldb_build_search_req(&req2, ldb, tmp_ctx,
112                                    dn,
113                                    LDB_SCOPE_BASE,
114                                    NULL,
115                                    no_attrs,
116                                    NULL,
117                                    res, ldb_search_default_callback,
118                                    req);
119         LDB_REQ_SET_LOCATION(req2);
120         if (ret != LDB_SUCCESS) {
121                 talloc_free(tmp_ctx);
122                 return ret;
123         }
124
125
126         ret = ldb_request_add_control(req2,
127                                       LDB_CONTROL_EXTENDED_DN_OID,
128                                       edn_control->critical, edn);
129         if (ret != LDB_SUCCESS) {
130                 talloc_free(tmp_ctx);
131                 return ret;
132         }
133
134         ret = ldb_next_request(module, req2);
135         if (ret == LDB_SUCCESS) {
136                 ret = ldb_wait(req2->handle, LDB_WAIT_ALL);
137         }
138         if (ret != LDB_SUCCESS) {
139                 talloc_free(tmp_ctx);
140                 return ret;
141         }
142
143         if (!res || res->count != 1) {
144                 talloc_free(tmp_ctx);
145                 return ldb_operr(ldb);
146         }
147
148         dn2 = res->msgs[0]->dn;
149
150         v->data = (uint8_t *)ldb_dn_get_extended_linearized(msg->elements, dn2, edn_type);
151         if (v->data == NULL) {
152                 talloc_free(tmp_ctx);
153                 return ldb_operr(ldb);
154         }
155         v->length = strlen((char *)v->data);
156
157
158         talloc_free(tmp_ctx);
159
160         return LDB_SUCCESS;
161 }
162
163
164 /*
165   add dynamically generated attributes to rootDSE result
166 */
167 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg,
168                                const char * const *attrs, struct ldb_request *req)
169 {
170         struct ldb_context *ldb;
171         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
172         char **server_sasl;
173         const struct dsdb_schema *schema;
174         int *val;
175         struct ldb_control *edn_control;
176         const char *dn_attrs[] = {
177                 "configurationNamingContext",
178                 "defaultNamingContext",
179                 "dsServiceName",
180                 "rootDomainNamingContext",
181                 "schemaNamingContext",
182                 "serverName",
183                 NULL
184         };
185
186         ldb = ldb_module_get_ctx(module);
187         schema = dsdb_get_schema(ldb, NULL);
188
189         msg->dn = ldb_dn_new(msg, ldb, NULL);
190
191         /* don't return the distinguishedName, cn and name attributes */
192         ldb_msg_remove_attr(msg, "distinguishedName");
193         ldb_msg_remove_attr(msg, "cn");
194         ldb_msg_remove_attr(msg, "name");
195
196         if (do_attribute(attrs, "serverName")) {
197                 if (ldb_msg_add_linearized_dn(msg, "serverName",
198                         samdb_server_dn(ldb, msg)) != LDB_SUCCESS) {
199                         goto failed;
200                 }
201         }
202
203         if (do_attribute(attrs, "dnsHostName")) {
204                 struct ldb_result *res;
205                 int ret;
206                 const char *dns_attrs[] = { "dNSHostName", NULL };
207                 ret = dsdb_module_search_dn(module, msg, &res, samdb_server_dn(ldb, msg),
208                                             dns_attrs, DSDB_FLAG_NEXT_MODULE, req);
209                 if (ret == LDB_SUCCESS) {
210                         const char *hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
211                         if (hostname != NULL) {
212                                 if (ldb_msg_add_string(msg, "dNSHostName", hostname)) {
213                                         goto failed;
214                                 }
215                         }
216                 }
217         }
218
219         if (do_attribute(attrs, "ldapServiceName")) {
220                 struct loadparm_context *lp_ctx
221                         = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
222                                           struct loadparm_context);
223                 char *ldap_service_name, *hostname;
224
225                 hostname = talloc_strdup(msg, lpcfg_netbios_name(lp_ctx));
226                 if (hostname == NULL) {
227                         goto failed;
228                 }
229                 strlower_m(hostname);
230
231                 ldap_service_name = talloc_asprintf(msg, "%s:%s$@%s",
232                                                     samdb_forest_name(ldb, msg),
233                                                     hostname, lpcfg_realm(lp_ctx));
234                 if (ldap_service_name == NULL) {
235                         goto failed;
236                 }
237
238                 if (ldb_msg_add_string(msg, "ldapServiceName",
239                                        ldap_service_name) != LDB_SUCCESS) {
240                         goto failed;
241                 }
242         }
243
244         if (do_attribute(attrs, "currentTime")) {
245                 if (ldb_msg_add_steal_string(msg, "currentTime",
246                                              ldb_timestring(msg, time(NULL))) != LDB_SUCCESS) {
247                         goto failed;
248                 }
249         }
250
251         if (priv && do_attribute(attrs, "supportedControl")) {
252                 unsigned int i;
253                 for (i = 0; i < priv->num_controls; i++) {
254                         char *control = talloc_strdup(msg, priv->controls[i]);
255                         if (!control) {
256                                 goto failed;
257                         }
258                         if (ldb_msg_add_steal_string(msg, "supportedControl",
259                                                      control) != LDB_SUCCESS) {
260                                 goto failed;
261                         }
262                 }
263         }
264
265         if (priv && do_attribute(attrs, "namingContexts")) {
266                 unsigned int i;
267                 for (i = 0; i < priv->num_partitions; i++) {
268                         struct ldb_dn *dn = priv->partitions[i];
269                         if (ldb_msg_add_steal_string(msg, "namingContexts",
270                                                      ldb_dn_alloc_linearized(msg, dn)) != LDB_SUCCESS) {
271                                 goto failed;
272                         }
273                 }
274         }
275
276         server_sasl = talloc_get_type(ldb_get_opaque(ldb, "supportedSASLMechanisms"),
277                                        char *);
278         if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
279                 unsigned int i;
280                 for (i = 0; server_sasl && server_sasl[i]; i++) {
281                         char *sasl_name = talloc_strdup(msg, server_sasl[i]);
282                         if (!sasl_name) {
283                                 goto failed;
284                         }
285                         if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
286                                                      sasl_name) != LDB_SUCCESS) {
287                                 goto failed;
288                         }
289                 }
290         }
291
292         if (do_attribute(attrs, "highestCommittedUSN")) {
293                 uint64_t seq_num;
294                 int ret = ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &seq_num);
295                 if (ret == LDB_SUCCESS) {
296                         if (samdb_msg_add_uint64(ldb, msg, msg,
297                                                  "highestCommittedUSN",
298                                                  seq_num) != LDB_SUCCESS) {
299                                 goto failed;
300                         }
301                 }
302         }
303
304         if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
305                 struct dsdb_attribute *cur;
306                 unsigned int n = 0;
307
308                 for (cur = schema->attributes; cur; cur = cur->next) {
309                         n++;
310                 }
311
312                 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaAttrCount",
313                                        n) != LDB_SUCCESS) {
314                         goto failed;
315                 }
316         }
317
318         if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
319                 struct dsdb_class *cur;
320                 unsigned int n = 0;
321
322                 for (cur = schema->classes; cur; cur = cur->next) {
323                         n++;
324                 }
325
326                 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaClassCount",
327                                        n) != LDB_SUCCESS) {
328                         goto failed;
329                 }
330         }
331
332         if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
333                 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaPrefixCount",
334                                        schema->prefixmap->length) != LDB_SUCCESS) {
335                         goto failed;
336                 }
337         }
338
339         if (do_attribute_explicit(attrs, "validFSMOs")) {
340                 const struct dsdb_naming_fsmo *naming_fsmo;
341                 const struct dsdb_pdc_fsmo *pdc_fsmo;
342                 const char *dn_str;
343
344                 if (schema && schema->fsmo.we_are_master) {
345                         dn_str = ldb_dn_get_linearized(ldb_get_schema_basedn(ldb));
346                         if (dn_str && dn_str[0]) {
347                                 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
348                                         goto failed;
349                                 }
350                         }
351                 }
352
353                 naming_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_naming_fsmo"),
354                                               struct dsdb_naming_fsmo);
355                 if (naming_fsmo && naming_fsmo->we_are_master) {
356                         dn_str = ldb_dn_get_linearized(samdb_partitions_dn(ldb, msg));
357                         if (dn_str && dn_str[0]) {
358                                 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
359                                         goto failed;
360                                 }
361                         }
362                 }
363
364                 pdc_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_pdc_fsmo"),
365                                            struct dsdb_pdc_fsmo);
366                 if (pdc_fsmo && pdc_fsmo->we_are_master) {
367                         dn_str = ldb_dn_get_linearized(ldb_get_default_basedn(ldb));
368                         if (dn_str && dn_str[0]) {
369                                 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
370                                         goto failed;
371                                 }
372                         }
373                 }
374         }
375
376         if (do_attribute_explicit(attrs, "vendorVersion")) {
377                 if (ldb_msg_add_fmt(msg, "vendorVersion",
378                                     "%s", SAMBA_VERSION_STRING) != LDB_SUCCESS) {
379                         goto failed;
380                 }
381         }
382
383         if (do_attribute(attrs, "domainFunctionality")) {
384                 if (samdb_msg_add_int(ldb, msg, msg, "domainFunctionality",
385                                       dsdb_functional_level(ldb)) != LDB_SUCCESS) {
386                         goto failed;
387                 }
388         }
389
390         if (do_attribute(attrs, "forestFunctionality")) {
391                 if (samdb_msg_add_int(ldb, msg, msg, "forestFunctionality",
392                                       dsdb_forest_functional_level(ldb)) != LDB_SUCCESS) {
393                         goto failed;
394                 }
395         }
396
397         if (do_attribute(attrs, "domainControllerFunctionality")
398             && (val = talloc_get_type(ldb_get_opaque(ldb, "domainControllerFunctionality"), int))) {
399                 if (samdb_msg_add_int(ldb, msg, msg,
400                                       "domainControllerFunctionality",
401                                       *val) != LDB_SUCCESS) {
402                         goto failed;
403                 }
404         }
405
406         if (do_attribute(attrs, "isGlobalCatalogReady")) {
407                 /* MS-ADTS 3.1.1.3.2.10
408                    Note, we should only return true here is we have
409                    completed at least one synchronisation. As both
410                    provision and vampire do a full sync, this means we
411                    can return true is the gc bit is set in the NTDSDSA
412                    options */
413                 if (ldb_msg_add_fmt(msg, "isGlobalCatalogReady",
414                                     "%s", samdb_is_gc(ldb)?"TRUE":"FALSE") != LDB_SUCCESS) {
415                         goto failed;
416                 }
417         }
418
419         if (do_attribute_explicit(attrs, "tokenGroups")) {
420                 unsigned int i;
421                 /* Obtain the user's session_info */
422                 struct auth_session_info *session_info
423                         = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
424                 if (session_info && session_info->security_token) {
425                         /* The list of groups this user is in */
426                         for (i = 0; i < session_info->security_token->num_sids; i++) {
427                                 if (samdb_msg_add_dom_sid(ldb, msg, msg,
428                                                           "tokenGroups",
429                                                           &session_info->security_token->sids[i]) != LDB_SUCCESS) {
430                                         goto failed;
431                                 }
432                         }
433                 }
434         }
435
436         /* TODO: lots more dynamic attributes should be added here */
437
438         edn_control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
439
440         /* if the client sent us the EXTENDED_DN control then we need
441            to expand the DNs to have GUID and SID. W2K8 join relies on
442            this */
443         if (edn_control) {
444                 unsigned int i;
445                 int ret;
446                 for (i=0; dn_attrs[i]; i++) {
447                         if (!do_attribute(attrs, dn_attrs[i])) continue;
448                         ret = expand_dn_in_message(module, msg, dn_attrs[i],
449                                                    edn_control, req);
450                         if (ret != LDB_SUCCESS) {
451                                 DEBUG(0,(__location__ ": Failed to expand DN in rootDSE for %s\n",
452                                          dn_attrs[i]));
453                                 goto failed;
454                         }
455                 }
456         }
457
458         return LDB_SUCCESS;
459
460 failed:
461         return ldb_operr(ldb);
462 }
463
464 /*
465   handle search requests
466 */
467
468 struct rootdse_context {
469         struct ldb_module *module;
470         struct ldb_request *req;
471 };
472
473 static struct rootdse_context *rootdse_init_context(struct ldb_module *module,
474                                                     struct ldb_request *req)
475 {
476         struct ldb_context *ldb;
477         struct rootdse_context *ac;
478
479         ldb = ldb_module_get_ctx(module);
480
481         ac = talloc_zero(req, struct rootdse_context);
482         if (ac == NULL) {
483                 ldb_set_errstring(ldb, "Out of Memory");
484                 return NULL;
485         }
486
487         ac->module = module;
488         ac->req = req;
489
490         return ac;
491 }
492
493 static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
494 {
495         struct rootdse_context *ac;
496         int ret;
497
498         ac = talloc_get_type(req->context, struct rootdse_context);
499
500         if (!ares) {
501                 return ldb_module_done(ac->req, NULL, NULL,
502                                         LDB_ERR_OPERATIONS_ERROR);
503         }
504         if (ares->error != LDB_SUCCESS) {
505                 return ldb_module_done(ac->req, ares->controls,
506                                         ares->response, ares->error);
507         }
508
509         switch (ares->type) {
510         case LDB_REPLY_ENTRY:
511                 /*
512                  * if the client explicit asks for the 'netlogon' attribute
513                  * the reply_entry needs to be skipped
514                  */
515                 if (ac->req->op.search.attrs &&
516                     ldb_attr_in_list(ac->req->op.search.attrs, "netlogon")) {
517                         talloc_free(ares);
518                         return LDB_SUCCESS;
519                 }
520
521                 /* for each record returned post-process to add any dynamic
522                    attributes that have been asked for */
523                 ret = rootdse_add_dynamic(ac->module, ares->message,
524                                           ac->req->op.search.attrs, ac->req);
525                 if (ret != LDB_SUCCESS) {
526                         talloc_free(ares);
527                         return ldb_module_done(ac->req, NULL, NULL, ret);
528                 }
529
530                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
531
532         case LDB_REPLY_REFERRAL:
533                 /* should we allow the backend to return referrals in this case
534                  * ?? */
535                 break;
536
537         case LDB_REPLY_DONE:
538                 return ldb_module_done(ac->req, ares->controls,
539                                         ares->response, ares->error);
540         }
541
542         talloc_free(ares);
543         return LDB_SUCCESS;
544 }
545
546 /*
547   filter from controls from clients in several ways
548
549   1) mark our registered controls as non-critical in the request
550
551     This is needed as clients may mark controls as critical even if
552     they are not needed at all in a request. For example, the centrify
553     client sets the SD_FLAGS control as critical on ldap modify
554     requests which are setting the dNSHostName attribute on the
555     machine account. That request doesn't need SD_FLAGS at all, but
556     centrify adds it on all ldap requests.
557
558   2) if this request is untrusted then remove any non-registered
559      controls that are non-critical
560
561     This is used on ldap:// connections to prevent remote users from
562     setting an internal control that may be dangerous
563
564   3) if this request is untrusted then fail any request that includes
565      a critical non-registered control
566  */
567 static int rootdse_filter_controls(struct ldb_module *module, struct ldb_request *req)
568 {
569         unsigned int i, j;
570         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
571         bool is_untrusted;
572
573         if (!req->controls) {
574                 return LDB_SUCCESS;
575         }
576
577         is_untrusted = ldb_req_is_untrusted(req);
578
579         for (i=0; req->controls[i]; i++) {
580                 bool is_registered = false;
581                 bool is_critical = (req->controls[i]->critical != 0);
582
583                 if (req->controls[i]->oid == NULL) {
584                         continue;
585                 }
586
587                 if (is_untrusted || is_critical) {
588                         for (j=0; j<priv->num_controls; j++) {
589                                 if (strcasecmp(priv->controls[j], req->controls[i]->oid) == 0) {
590                                         is_registered = true;
591                                         break;
592                                 }
593                         }
594                 }
595
596                 if (is_untrusted && !is_registered) {
597                         if (!is_critical) {
598                                 /* remove it by marking the oid NULL */
599                                 req->controls[i]->oid = NULL;
600                                 req->controls[i]->data = NULL;
601                                 req->controls[i]->critical = 0;
602                                 continue;
603                         }
604                         /* its a critical unregistered control - give
605                            an error */
606                         ldb_asprintf_errstring(ldb_module_get_ctx(module),
607                                                "Attempt to use critical non-registered control '%s'",
608                                                req->controls[i]->oid);
609                         return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
610                 }
611
612                 if (!is_critical) {
613                         continue;
614                 }
615
616                 if (is_registered) {
617                         req->controls[i]->critical = 0;
618                 }
619         }
620
621         return LDB_SUCCESS;
622 }
623
624 /* Ensure that anonymous users are not allowed to make anything other than rootDSE search operations */
625
626 static int rootdse_filter_operations(struct ldb_module *module, struct ldb_request *req)
627 {
628         struct auth_session_info *session_info;
629         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
630         bool is_untrusted = ldb_req_is_untrusted(req);
631         bool is_anonymous = true;
632         if (is_untrusted == false) {
633                 return LDB_SUCCESS;
634         }
635
636         session_info = (struct auth_session_info *)ldb_get_opaque(ldb_module_get_ctx(module), "sessionInfo");
637         if (session_info) {
638                 is_anonymous = security_token_is_anonymous(session_info->security_token);
639         }
640         
641         if (is_anonymous == false || (priv && priv->block_anonymous == false)) {
642                 return LDB_SUCCESS;
643         }
644         
645         if (req->operation == LDB_SEARCH) {
646                 if (req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base)) {
647                         return LDB_SUCCESS;
648                 }
649         }
650         ldb_set_errstring(ldb_module_get_ctx(module), "Operation unavailable without authentication");
651         return LDB_ERR_OPERATIONS_ERROR;
652 }
653
654 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
655 {
656         struct ldb_context *ldb;
657         struct rootdse_context *ac;
658         struct ldb_request *down_req;
659         int ret;
660
661         ret = rootdse_filter_operations(module, req);
662         if (ret != LDB_SUCCESS) {
663                 return ret;
664         }
665
666         ret = rootdse_filter_controls(module, req);
667         if (ret != LDB_SUCCESS) {
668                 return ret;
669         }
670
671         ldb = ldb_module_get_ctx(module);
672
673         /* see if its for the rootDSE - only a base search on the "" DN qualifies */
674         if (!(req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base))) {
675                 /* Otherwise, pass down to the rest of the stack */
676                 return ldb_next_request(module, req);
677         }
678
679         ac = rootdse_init_context(module, req);
680         if (ac == NULL) {
681                 return ldb_operr(ldb);
682         }
683
684         /* in our db we store the rootDSE with a DN of @ROOTDSE */
685         ret = ldb_build_search_req(&down_req, ldb, ac,
686                                         ldb_dn_new(ac, ldb, "@ROOTDSE"),
687                                         LDB_SCOPE_BASE,
688                                         NULL,
689                                         req->op.search.attrs,
690                                         NULL,/* for now skip the controls from the client */
691                                         ac, rootdse_callback,
692                                         req);
693         LDB_REQ_SET_LOCATION(down_req);
694         if (ret != LDB_SUCCESS) {
695                 return ret;
696         }
697
698         return ldb_next_request(module, down_req);
699 }
700
701 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
702 {
703         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
704         char **list;
705
706         list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
707         if (!list) {
708                 return ldb_oom(ldb_module_get_ctx(module));
709         }
710
711         list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
712         if (!list[priv->num_controls]) {
713                 return ldb_oom(ldb_module_get_ctx(module));
714         }
715
716         priv->num_controls += 1;
717         priv->controls = list;
718
719         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
720 }
721
722 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
723 {
724         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
725         struct ldb_dn **list;
726
727         list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
728         if (!list) {
729                 return ldb_oom(ldb_module_get_ctx(module));
730         }
731
732         list[priv->num_partitions] = ldb_dn_copy(list, req->op.reg_partition.dn);
733         if (!list[priv->num_partitions]) {
734                 return ldb_operr(ldb_module_get_ctx(module));
735         }
736
737         priv->num_partitions += 1;
738         priv->partitions = list;
739
740         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
741 }
742
743
744 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
745 {
746         switch (req->operation) {
747
748         case LDB_REQ_REGISTER_CONTROL:
749                 return rootdse_register_control(module, req);
750         case LDB_REQ_REGISTER_PARTITION:
751                 return rootdse_register_partition(module, req);
752
753         default:
754                 break;
755         }
756         return ldb_next_request(module, req);
757 }
758
759 static int rootdse_init(struct ldb_module *module)
760 {
761         int ret;
762         struct ldb_context *ldb;
763         struct ldb_result *res;
764         struct private_data *data;
765         const char *attrs[] = { "msDS-Behavior-Version", NULL };
766         const char *ds_attrs[] = { "dsServiceName", NULL };
767         TALLOC_CTX *mem_ctx;
768
769         ldb = ldb_module_get_ctx(module);
770
771         data = talloc_zero(module, struct private_data);
772         if (data == NULL) {
773                 return ldb_oom(ldb);
774         }
775
776         data->num_controls = 0;
777         data->controls = NULL;
778         data->num_partitions = 0;
779         data->partitions = NULL;
780         data->block_anonymous = true;
781
782         ldb_module_set_private(module, data);
783
784         ldb_set_default_dns(ldb);
785
786         ret = ldb_next_init(module);
787
788         if (ret != LDB_SUCCESS) {
789                 return ret;
790         }
791
792         mem_ctx = talloc_new(data);
793         if (!mem_ctx) {
794                 return ldb_oom(ldb);
795         }
796
797         /* Now that the partitions are set up, do a search for:
798            - domainControllerFunctionality
799            - domainFunctionality
800            - forestFunctionality
801
802            Then stuff these values into an opaque
803         */
804         ret = dsdb_module_search(module, mem_ctx, &res,
805                                  ldb_get_default_basedn(ldb),
806                                  LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
807         if (ret == LDB_SUCCESS && res->count == 1) {
808                 int domain_behaviour_version
809                         = ldb_msg_find_attr_as_int(res->msgs[0],
810                                                    "msDS-Behavior-Version", -1);
811                 if (domain_behaviour_version != -1) {
812                         int *val = talloc(ldb, int);
813                         if (!val) {
814                                 talloc_free(mem_ctx);
815                                 return ldb_oom(ldb);
816                         }
817                         *val = domain_behaviour_version;
818                         ret = ldb_set_opaque(ldb, "domainFunctionality", val);
819                         if (ret != LDB_SUCCESS) {
820                                 talloc_free(mem_ctx);
821                                 return ret;
822                         }
823                 }
824         }
825
826         ret = dsdb_module_search(module, mem_ctx, &res,
827                                  samdb_partitions_dn(ldb, mem_ctx),
828                                  LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
829         if (ret == LDB_SUCCESS && res->count == 1) {
830                 int forest_behaviour_version
831                         = ldb_msg_find_attr_as_int(res->msgs[0],
832                                                    "msDS-Behavior-Version", -1);
833                 if (forest_behaviour_version != -1) {
834                         int *val = talloc(ldb, int);
835                         if (!val) {
836                                 talloc_free(mem_ctx);
837                                 return ldb_oom(ldb);
838                         }
839                         *val = forest_behaviour_version;
840                         ret = ldb_set_opaque(ldb, "forestFunctionality", val);
841                         if (ret != LDB_SUCCESS) {
842                                 talloc_free(mem_ctx);
843                                 return ret;
844                         }
845                 }
846         }
847
848         /* For now, our own server's location in the DB is recorded in
849          * the @ROOTDSE record */
850         ret = dsdb_module_search(module, mem_ctx, &res,
851                                  ldb_dn_new(mem_ctx, ldb, "@ROOTDSE"),
852                                  LDB_SCOPE_BASE, ds_attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
853         if (ret == LDB_SUCCESS && res->count == 1) {
854                 struct ldb_dn *ds_dn
855                         = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0],
856                                                   "dsServiceName");
857                 if (ds_dn) {
858                         ret = dsdb_module_search(module, mem_ctx, &res, ds_dn,
859                                                  LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
860                         if (ret == LDB_SUCCESS && res->count == 1) {
861                                 int domain_controller_behaviour_version
862                                         = ldb_msg_find_attr_as_int(res->msgs[0],
863                                                                    "msDS-Behavior-Version", -1);
864                                 if (domain_controller_behaviour_version != -1) {
865                                         int *val = talloc(ldb, int);
866                                         if (!val) {
867                                                 talloc_free(mem_ctx);
868                                                 return ldb_oom(ldb);
869                                         }
870                                         *val = domain_controller_behaviour_version;
871                                         ret = ldb_set_opaque(ldb,
872                                                              "domainControllerFunctionality", val);
873                                         if (ret != LDB_SUCCESS) {
874                                                 talloc_free(mem_ctx);
875                                                 return ret;
876                                         }
877                                 }
878                         }
879                 }
880         }
881
882         data->block_anonymous = dsdb_block_anonymous_ops(module, NULL);
883
884         talloc_free(mem_ctx);
885
886         return LDB_SUCCESS;
887 }
888
889 /*
890  * This function gets the string SCOPE_DN:OPTIONAL_FEATURE_GUID and parse it
891  * to a DN and a GUID object
892  */
893 static int get_optional_feature_dn_guid(struct ldb_request *req, struct ldb_context *ldb,
894                                                 TALLOC_CTX *mem_ctx,
895                                                 struct ldb_dn **op_feature_scope_dn,
896                                                 struct GUID *op_feature_guid)
897 {
898         const struct ldb_message *msg = req->op.mod.message;
899         const char *ldb_val_str;
900         char *dn, *guid;
901         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
902         NTSTATUS status;
903
904         ldb_val_str = ldb_msg_find_attr_as_string(msg, "enableOptionalFeature", NULL);
905         if (!ldb_val_str) {
906                 ldb_set_errstring(ldb,
907                                   "rootdse: unable to find 'enableOptionalFeature'!");
908                 return LDB_ERR_UNWILLING_TO_PERFORM;
909         }
910
911         guid = strchr(ldb_val_str, ':');
912         if (!guid) {
913                 ldb_set_errstring(ldb,
914                                   "rootdse: unable to find GUID in 'enableOptionalFeature'!");
915                 return LDB_ERR_UNWILLING_TO_PERFORM;
916         }
917         status = GUID_from_string(guid+1, op_feature_guid);
918         if (!NT_STATUS_IS_OK(status)) {
919                 ldb_set_errstring(ldb,
920                                   "rootdse: bad GUID in 'enableOptionalFeature'!");
921                 return LDB_ERR_UNWILLING_TO_PERFORM;
922         }
923
924         dn = talloc_strndup(tmp_ctx, ldb_val_str, guid-ldb_val_str);
925         if (!dn) {
926                 ldb_set_errstring(ldb,
927                                   "rootdse: bad DN in 'enableOptionalFeature'!");
928                 return LDB_ERR_UNWILLING_TO_PERFORM;
929         }
930
931         *op_feature_scope_dn = ldb_dn_new(mem_ctx, ldb, dn);
932
933         talloc_free(tmp_ctx);
934         return LDB_SUCCESS;
935 }
936
937 /*
938  * This function gets the OPTIONAL_FEATURE_GUID and looks for the optional feature
939  * ldb_message object.
940  */
941 static int dsdb_find_optional_feature(struct ldb_module *module, struct ldb_context *ldb,
942                                       TALLOC_CTX *mem_ctx, struct GUID op_feature_guid, struct ldb_message **msg,
943                                       struct ldb_request *parent)
944 {
945         struct ldb_result *res;
946         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
947         int ret;
948
949         ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
950                                  NULL,
951                                  DSDB_FLAG_NEXT_MODULE |
952                                  DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
953                                  parent,
954                                  "(&(objectClass=msDS-OptionalFeature)"
955                                  "(msDS-OptionalFeatureGUID=%s))",GUID_string(tmp_ctx, &op_feature_guid));
956
957         if (ret != LDB_SUCCESS) {
958                 talloc_free(tmp_ctx);
959                 return ret;
960         }
961         if (res->count == 0) {
962                 talloc_free(tmp_ctx);
963                 return LDB_ERR_NO_SUCH_OBJECT;
964         }
965         if (res->count != 1) {
966                 ldb_asprintf_errstring(ldb,
967                                        "More than one object found matching optional feature GUID %s\n",
968                                        GUID_string(tmp_ctx, &op_feature_guid));
969                 talloc_free(tmp_ctx);
970                 return LDB_ERR_OPERATIONS_ERROR;
971         }
972
973         *msg = talloc_steal(mem_ctx, res->msgs[0]);
974
975         talloc_free(tmp_ctx);
976         return LDB_SUCCESS;
977 }
978
979 static int rootdse_enable_recycle_bin(struct ldb_module *module,struct ldb_context *ldb,
980                                       TALLOC_CTX *mem_ctx, struct ldb_dn *op_feature_scope_dn,
981                                       struct ldb_message *op_feature_msg, struct ldb_request *parent)
982 {
983         int ret;
984         const int domain_func_level = dsdb_functional_level(ldb);
985         struct ldb_dn *ntds_settings_dn;
986         TALLOC_CTX *tmp_ctx;
987         unsigned int el_count = 0;
988         struct ldb_message *msg;
989
990         ret = ldb_msg_find_attr_as_int(op_feature_msg, "msDS-RequiredForestBehaviorVersion", 0);
991         if (domain_func_level < ret){
992                 ldb_asprintf_errstring(ldb,
993                                        "rootdse_enable_recycle_bin: Domain functional level must be at least %d\n",
994                                        ret);
995                 return LDB_ERR_UNWILLING_TO_PERFORM;
996         }
997
998         tmp_ctx = talloc_new(mem_ctx);
999         ntds_settings_dn = samdb_ntds_settings_dn(ldb);
1000         if (!ntds_settings_dn) {
1001                 talloc_free(tmp_ctx);
1002                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "Failed to find NTDS settings DN");
1003         }
1004
1005         ntds_settings_dn = ldb_dn_copy(tmp_ctx, ntds_settings_dn);
1006         if (!ntds_settings_dn) {
1007                 talloc_free(tmp_ctx);
1008                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "Failed to copy NTDS settings DN");
1009         }
1010
1011         msg = ldb_msg_new(tmp_ctx);
1012         msg->dn = ntds_settings_dn;
1013
1014         ldb_msg_add_linearized_dn(msg, "msDS-EnabledFeature", op_feature_msg->dn);
1015         msg->elements[el_count++].flags = LDB_FLAG_MOD_ADD;
1016
1017         ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE, parent);
1018         if (ret != LDB_SUCCESS) {
1019                 ldb_asprintf_errstring(ldb,
1020                                        "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
1021                                        ldb_dn_get_linearized(ntds_settings_dn),
1022                                        ldb_errstring(ldb));
1023                 talloc_free(tmp_ctx);
1024                 return ret;
1025         }
1026
1027         msg->dn = op_feature_scope_dn;
1028         ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE, parent);
1029         if (ret != LDB_SUCCESS) {
1030                 ldb_asprintf_errstring(ldb,
1031                                        "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
1032                                        ldb_dn_get_linearized(op_feature_scope_dn),
1033                                        ldb_errstring(ldb));
1034                 talloc_free(tmp_ctx);
1035                 return ret;
1036         }
1037
1038         return LDB_SUCCESS;
1039 }
1040
1041 static int rootdse_enableoptionalfeature(struct ldb_module *module, struct ldb_request *req)
1042 {
1043         /*
1044           steps:
1045                - check for system (only system can enable features)
1046                - extract GUID from the request
1047                - find the feature object
1048                - check functional level, must be at least msDS-RequiredForestBehaviorVersion
1049                - check if it is already enabled (if enabled return LDAP_ATTRIBUTE_OR_VALUE_EXISTS) - probably not needed, just return error from the add/modify
1050                - add/modify objects (see ntdsconnection code for an example)
1051          */
1052
1053         struct ldb_context *ldb = ldb_module_get_ctx(module);
1054         struct GUID op_feature_guid;
1055         struct ldb_dn *op_feature_scope_dn;
1056         struct ldb_message *op_feature_msg;
1057         struct auth_session_info *session_info =
1058                                 (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
1059         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1060         int ret;
1061         const char *guid_string;
1062
1063         if (security_session_user_level(session_info, NULL) != SECURITY_SYSTEM) {
1064                 ldb_set_errstring(ldb, "rootdse: Insufficient rights for enableoptionalfeature");
1065                 return LDB_ERR_UNWILLING_TO_PERFORM;
1066         }
1067
1068         ret = get_optional_feature_dn_guid(req, ldb, tmp_ctx, &op_feature_scope_dn, &op_feature_guid);
1069         if (ret != LDB_SUCCESS) {
1070                 talloc_free(tmp_ctx);
1071                 return ret;
1072         }
1073
1074         guid_string = GUID_string(tmp_ctx, &op_feature_guid);
1075         if (!guid_string) {
1076                 ldb_set_errstring(ldb, "rootdse: bad optional feature GUID");
1077                 return LDB_ERR_UNWILLING_TO_PERFORM;
1078         }
1079
1080         ret = dsdb_find_optional_feature(module, ldb, tmp_ctx, op_feature_guid, &op_feature_msg, req);
1081         if (ret != LDB_SUCCESS) {
1082                 ldb_asprintf_errstring(ldb,
1083                                        "rootdse: unable to find optional feature for %s - %s",
1084                                        guid_string, ldb_errstring(ldb));
1085                 talloc_free(tmp_ctx);
1086                 return ret;
1087         }
1088
1089         if (strcasecmp(DS_GUID_FEATURE_RECYCLE_BIN, guid_string) == 0) {
1090                         ret = rootdse_enable_recycle_bin(module, ldb,
1091                                                          tmp_ctx, op_feature_scope_dn,
1092                                                          op_feature_msg, req);
1093         } else {
1094                 ldb_asprintf_errstring(ldb,
1095                                        "rootdse: unknown optional feature %s",
1096                                        guid_string);
1097                 talloc_free(tmp_ctx);
1098                 return LDB_ERR_UNWILLING_TO_PERFORM;
1099         }
1100         if (ret != LDB_SUCCESS) {
1101                 ldb_asprintf_errstring(ldb,
1102                                        "rootdse: failed to set optional feature for %s - %s",
1103                                        guid_string, ldb_errstring(ldb));
1104                 talloc_free(tmp_ctx);
1105                 return ret;
1106         }
1107
1108         talloc_free(tmp_ctx);
1109         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);;
1110 }
1111
1112 static int rootdse_schemaupdatenow(struct ldb_module *module, struct ldb_request *req)
1113 {
1114         struct ldb_context *ldb = ldb_module_get_ctx(module);
1115         struct ldb_result *ext_res;
1116         int ret;
1117         struct ldb_dn *schema_dn;
1118
1119         schema_dn = ldb_get_schema_basedn(ldb);
1120         if (!schema_dn) {
1121                 ldb_reset_err_string(ldb);
1122                 ldb_debug(ldb, LDB_DEBUG_WARNING,
1123                           "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
1124                 return ldb_next_request(module, req);
1125         }
1126
1127         ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, schema_dn, &ext_res);
1128         if (ret != LDB_SUCCESS) {
1129                 return ldb_operr(ldb);
1130         }
1131
1132         talloc_free(ext_res);
1133         return ldb_module_done(req, NULL, NULL, ret);
1134 }
1135
1136 static int rootdse_add(struct ldb_module *module, struct ldb_request *req)
1137 {
1138         struct ldb_context *ldb = ldb_module_get_ctx(module);
1139         int ret;
1140
1141         ret = rootdse_filter_operations(module, req);
1142         if (ret != LDB_SUCCESS) {
1143                 return ret;
1144         }
1145
1146         ret = rootdse_filter_controls(module, req);
1147         if (ret != LDB_SUCCESS) {
1148                 return ret;
1149         }
1150
1151         /*
1152                 If dn is not "" we should let it pass through
1153         */
1154         if (!ldb_dn_is_null(req->op.add.message->dn)) {
1155                 return ldb_next_request(module, req);
1156         }
1157
1158         ldb_set_errstring(ldb, "rootdse_add: you cannot add a new rootdse entry!");
1159         return LDB_ERR_NAMING_VIOLATION;
1160 }
1161
1162 struct fsmo_transfer_state {
1163         struct ldb_context *ldb;
1164         struct ldb_request *req;
1165 };
1166
1167 /*
1168   called when a FSMO transfer operation has completed
1169  */
1170 static void rootdse_fsmo_transfer_callback(struct tevent_req *treq)
1171 {
1172         struct fsmo_transfer_state *fsmo = tevent_req_callback_data(treq, struct fsmo_transfer_state);
1173         NTSTATUS status;
1174         WERROR werr;
1175         struct ldb_request *req = fsmo->req;
1176         struct ldb_context *ldb = fsmo->ldb;
1177
1178         status = dcerpc_drepl_takeFSMORole_recv(treq, fsmo, &werr);
1179         talloc_free(fsmo);
1180         if (!NT_STATUS_IS_OK(status)) {
1181                 ldb_asprintf_errstring(ldb, "Failed FSMO transfer: %s", nt_errstr(status));
1182                 ldb_module_done(req, NULL, NULL, LDB_ERR_UNAVAILABLE);
1183                 return;
1184         }
1185         if (!W_ERROR_IS_OK(werr)) {
1186                 ldb_asprintf_errstring(ldb, "Failed FSMO transfer: %s", win_errstr(werr));
1187                 ldb_module_done(req, NULL, NULL, LDB_ERR_UNAVAILABLE);
1188                 return;
1189         }
1190
1191         ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1192 }
1193
1194 static int rootdse_become_master(struct ldb_module *module,
1195                                  struct ldb_request *req,
1196                                  enum drepl_role_master role)
1197 {
1198         struct imessaging_context *msg;
1199         struct ldb_context *ldb = ldb_module_get_ctx(module);
1200         TALLOC_CTX *tmp_ctx = talloc_new(req);
1201         struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm");
1202         bool am_rodc;
1203         struct dcerpc_binding_handle *irpc_handle;
1204         int ret;
1205         struct auth_session_info *session_info;
1206         enum security_user_level level;
1207         struct fsmo_transfer_state *fsmo;
1208         struct tevent_req *treq;
1209
1210         session_info = (struct auth_session_info *)ldb_get_opaque(ldb_module_get_ctx(module), "sessionInfo");
1211         level = security_session_user_level(session_info, NULL);
1212         if (level < SECURITY_ADMINISTRATOR) {
1213                 return ldb_error(ldb, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS, "Denied rootDSE modify for non-administrator");
1214         }
1215
1216         ret = samdb_rodc(ldb, &am_rodc);
1217         if (ret != LDB_SUCCESS) {
1218                 return ldb_error(ldb, ret, "Could not determine if server is RODC.");
1219         }
1220
1221         if (am_rodc) {
1222                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM,
1223                                  "RODC cannot become a role master.");
1224         }
1225
1226         msg = imessaging_client_init(tmp_ctx, lpcfg_imessaging_path(tmp_ctx, lp_ctx),
1227                                     ldb_get_event_context(ldb));
1228         if (!msg) {
1229                 ldb_asprintf_errstring(ldb, "Failed to generate client messaging context in %s", lpcfg_imessaging_path(tmp_ctx, lp_ctx));
1230                 return LDB_ERR_OPERATIONS_ERROR;
1231         }
1232         irpc_handle = irpc_binding_handle_by_name(tmp_ctx, msg,
1233                                                   "dreplsrv",
1234                                                   &ndr_table_irpc);
1235         if (irpc_handle == NULL) {
1236                 return ldb_oom(ldb);
1237         }
1238         fsmo = talloc_zero(req, struct fsmo_transfer_state);
1239         if (fsmo == NULL) {
1240                 return ldb_oom(ldb);
1241         }
1242         fsmo->ldb = ldb;
1243         fsmo->req = req;
1244
1245         /* we send the call asynchronously, as the ldap client is
1246          * expecting to get an error back if the role transfer fails
1247          */
1248
1249         treq = dcerpc_drepl_takeFSMORole_send(req, ldb_get_event_context(ldb), irpc_handle, role);
1250         if (treq == NULL) {
1251                 return ldb_oom(ldb);
1252         }
1253
1254         tevent_req_set_callback(treq, rootdse_fsmo_transfer_callback, fsmo);
1255         return LDB_SUCCESS;
1256 }
1257
1258 static int rootdse_modify(struct ldb_module *module, struct ldb_request *req)
1259 {
1260         struct ldb_context *ldb = ldb_module_get_ctx(module);
1261         int ret;
1262
1263         ret = rootdse_filter_operations(module, req);
1264         if (ret != LDB_SUCCESS) {
1265                 return ret;
1266         }
1267
1268         ret = rootdse_filter_controls(module, req);
1269         if (ret != LDB_SUCCESS) {
1270                 return ret;
1271         }
1272
1273         /*
1274                 If dn is not "" we should let it pass through
1275         */
1276         if (!ldb_dn_is_null(req->op.mod.message->dn)) {
1277                 return ldb_next_request(module, req);
1278         }
1279
1280         /*
1281                 dn is empty so check for schemaUpdateNow attribute
1282                 "The type of modification and values specified in the LDAP modify operation do not matter." MSDN
1283         */
1284         if (ldb_msg_find_element(req->op.mod.message, "schemaUpdateNow")) {
1285                 return rootdse_schemaupdatenow(module, req);
1286         }
1287         if (ldb_msg_find_element(req->op.mod.message, "becomeDomainMaster")) {
1288                 return rootdse_become_master(module, req, DREPL_NAMING_MASTER);
1289         }
1290         if (ldb_msg_find_element(req->op.mod.message, "becomeInfrastructureMaster")) {
1291                 return rootdse_become_master(module, req, DREPL_INFRASTRUCTURE_MASTER);
1292         }
1293         if (ldb_msg_find_element(req->op.mod.message, "becomeRidMaster")) {
1294                 return rootdse_become_master(module, req, DREPL_RID_MASTER);
1295         }
1296         if (ldb_msg_find_element(req->op.mod.message, "becomeSchemaMaster")) {
1297                 return rootdse_become_master(module, req, DREPL_SCHEMA_MASTER);
1298         }
1299         if (ldb_msg_find_element(req->op.mod.message, "becomePdc")) {
1300                 return rootdse_become_master(module, req, DREPL_PDC_MASTER);
1301         }
1302         if (ldb_msg_find_element(req->op.mod.message, "enableOptionalFeature")) {
1303                 return rootdse_enableoptionalfeature(module, req);
1304         }
1305
1306         ldb_set_errstring(ldb, "rootdse_modify: unknown attribute to change!");
1307         return LDB_ERR_UNWILLING_TO_PERFORM;
1308 }
1309
1310 static int rootdse_rename(struct ldb_module *module, struct ldb_request *req)
1311 {
1312         struct ldb_context *ldb = ldb_module_get_ctx(module);
1313         int ret;
1314
1315         ret = rootdse_filter_operations(module, req);
1316         if (ret != LDB_SUCCESS) {
1317                 return ret;
1318         }
1319
1320         ret = rootdse_filter_controls(module, req);
1321         if (ret != LDB_SUCCESS) {
1322                 return ret;
1323         }
1324
1325         /*
1326                 If dn is not "" we should let it pass through
1327         */
1328         if (!ldb_dn_is_null(req->op.rename.olddn)) {
1329                 return ldb_next_request(module, req);
1330         }
1331
1332         ldb_set_errstring(ldb, "rootdse_remove: you cannot rename the rootdse entry!");
1333         return LDB_ERR_NO_SUCH_OBJECT;
1334 }
1335
1336 static int rootdse_delete(struct ldb_module *module, struct ldb_request *req)
1337 {
1338         struct ldb_context *ldb = ldb_module_get_ctx(module);
1339         int ret;
1340
1341         ret = rootdse_filter_operations(module, req);
1342         if (ret != LDB_SUCCESS) {
1343                 return ret;
1344         }
1345
1346         ret = rootdse_filter_controls(module, req);
1347         if (ret != LDB_SUCCESS) {
1348                 return ret;
1349         }
1350
1351         /*
1352                 If dn is not "" we should let it pass through
1353         */
1354         if (!ldb_dn_is_null(req->op.del.dn)) {
1355                 return ldb_next_request(module, req);
1356         }
1357
1358         ldb_set_errstring(ldb, "rootdse_remove: you cannot delete the rootdse entry!");
1359         return LDB_ERR_NO_SUCH_OBJECT;
1360 }
1361
1362 static int rootdse_extended(struct ldb_module *module, struct ldb_request *req)
1363 {
1364         int ret;
1365
1366         ret = rootdse_filter_operations(module, req);
1367         if (ret != LDB_SUCCESS) {
1368                 return ret;
1369         }
1370
1371         ret = rootdse_filter_controls(module, req);
1372         if (ret != LDB_SUCCESS) {
1373                 return ret;
1374         }
1375
1376         return ldb_next_request(module, req);
1377 }
1378
1379 static const struct ldb_module_ops ldb_rootdse_module_ops = {
1380         .name           = "rootdse",
1381         .init_context   = rootdse_init,
1382         .search         = rootdse_search,
1383         .request        = rootdse_request,
1384         .add            = rootdse_add,
1385         .modify         = rootdse_modify,
1386         .rename         = rootdse_rename,
1387         .extended       = rootdse_extended,
1388         .del            = rootdse_delete
1389 };
1390
1391 int ldb_rootdse_module_init(const char *version)
1392 {
1393         LDB_MODULE_CHECK_VERSION(version);
1394         return ldb_register_module(&ldb_rootdse_module_ops);
1395 }