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