bd57913ea6ebdca4ff67bfaa17b2a65d750d162f
[samba.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_out.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce 2005-2008
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb extended dn control module
25  *
26  *  Description: this module builds a special dn for returned search
27  *  results, and fixes some other aspects of the result (returned case issues)
28  *  values.
29  *
30  *  Authors: Simo Sorce
31  *           Andrew Bartlett
32  */
33
34 #include "includes.h"
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
37 #include "ldb/include/ldb_module.h"
38 #include "libcli/security/dom_sid.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "librpc/ndr/libndr.h"
42 #include "dsdb/samdb/samdb.h"
43
44 struct extended_dn_out_private {
45         bool dereference;
46         bool normalise;
47         struct dsdb_openldap_dereference_control *dereference_control;
48 };
49
50 static bool is_attr_in_list(const char * const * attrs, const char *attr)
51 {
52         int i;
53
54         for (i = 0; attrs[i]; i++) {
55                 if (ldb_attr_cmp(attrs[i], attr) == 0)
56                         return true;
57         }
58
59         return false;
60 }
61
62 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
63 {
64         char **nattrs;
65         int i, num;
66
67         for (num = 0; attrs[num]; num++);
68
69         nattrs = talloc_array(mem_ctx, char *, num + 1);
70         if (!nattrs) return NULL;
71
72         for(i = 0; i < num; i++) {
73                 nattrs[i] = talloc_strdup(nattrs, attrs[i]);
74                 if (!nattrs[i]) {
75                         talloc_free(nattrs);
76                         return NULL;
77                 }
78         }
79         nattrs[i] = NULL;
80
81         return nattrs;
82 }
83
84 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
85 {
86         char **nattrs;
87         int num;
88
89         for (num = 0; (*attrs)[num]; num++);
90
91         nattrs = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
92         if (!nattrs) return false;
93
94         *attrs = nattrs;
95
96         nattrs[num] = talloc_strdup(nattrs, attr);
97         if (!nattrs[num]) return false;
98
99         nattrs[num + 1] = NULL;
100
101         return true;
102 }
103
104 /* Fix the DN so that the relative attribute names are in upper case so that the DN:
105    cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
106    CN=Adminstrator,CN=users,DC=samba,DC=example,DC=com
107 */
108
109
110 static int fix_dn(struct ldb_dn *dn) 
111 {
112         int i, ret;
113         char *upper_rdn_attr;
114
115         for (i=0; i < ldb_dn_get_comp_num(dn); i++) {
116                 /* We need the attribute name in upper case */
117                 upper_rdn_attr = strupper_talloc(dn,
118                                                  ldb_dn_get_component_name(dn, i));
119                 if (!upper_rdn_attr) {
120                         return LDB_ERR_OPERATIONS_ERROR;
121                 }
122                 
123                 /* And replace it with CN=foo (we need the attribute in upper case */
124                 ret = ldb_dn_set_component(dn, i, upper_rdn_attr,
125                                            *ldb_dn_get_component_val(dn, i));
126                 talloc_free(upper_rdn_attr);
127                 if (ret != LDB_SUCCESS) {
128                         return ret;
129                 }
130         }
131         return LDB_SUCCESS;
132 }
133
134 /* Inject the extended DN components, so the DN cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
135    <GUID=541203ae-f7d6-47ef-8390-bfcf019f9583>;<SID=S-1-5-21-4177067393-1453636373-93818737-500>;cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com */
136
137 static int inject_extended_dn_out(struct ldb_reply *ares,
138                                   struct ldb_context *ldb,
139                                   int type,
140                                   bool remove_guid,
141                                   bool remove_sid)
142 {
143         int ret;
144         const DATA_BLOB *guid_blob;
145         const DATA_BLOB *sid_blob;
146
147         guid_blob = ldb_msg_find_ldb_val(ares->message, "objectGUID");
148         sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSID");
149
150         if (!guid_blob) {
151                 ldb_set_errstring(ldb, "Did not find objectGUID to inject into extended DN");
152                 return LDB_ERR_OPERATIONS_ERROR;
153         }
154
155         ret = ldb_dn_set_extended_component(ares->message->dn, "GUID", guid_blob);
156         if (ret != LDB_SUCCESS) {
157                 return ret;
158         }
159         if (sid_blob) {
160                 ret = ldb_dn_set_extended_component(ares->message->dn, "SID", sid_blob);
161                 if (ret != LDB_SUCCESS) {
162                         return ret;
163                 }
164         }
165
166         if (remove_guid) {
167                 ldb_msg_remove_attr(ares->message, "objectGUID");
168         }
169
170         if (sid_blob && remove_sid) {
171                 ldb_msg_remove_attr(ares->message, "objectSID");
172         }
173
174         return LDB_SUCCESS;
175 }
176
177 static int handle_dereference_openldap(struct ldb_dn *dn,
178                               struct dsdb_openldap_dereference_result **dereference_attrs, 
179                               const char *attr, const DATA_BLOB *val)
180 {
181         const struct ldb_val *entryUUIDblob, *sid_blob;
182         struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
183         int j;
184         
185         fake_msg.num_elements = 0;
186                         
187         /* Look for this attribute in the returned control */
188         for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
189                 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
190                 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
191                     && data_blob_cmp(&source_dn, val) == 0) {
192                         fake_msg.num_elements = dereference_attrs[j]->num_attributes;
193                         fake_msg.elements = dereference_attrs[j]->attributes;
194                         break;
195                 }
196         }
197         if (!fake_msg.num_elements) {
198                 return LDB_SUCCESS;
199         }
200         /* Look for an OpenLDAP entryUUID */
201         
202         entryUUIDblob = ldb_msg_find_ldb_val(&fake_msg, "entryUUID");
203         if (entryUUIDblob) {
204                 NTSTATUS status;
205                 struct ldb_val guid_blob;
206                 struct GUID guid;
207                 
208                 status = GUID_from_data_blob(entryUUIDblob, &guid);
209                 
210                 if (!NT_STATUS_IS_OK(status)) {
211                         return LDB_ERR_INVALID_DN_SYNTAX;
212                 }
213                 status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
214                 if (!NT_STATUS_IS_OK(status)) {
215                         return LDB_ERR_INVALID_DN_SYNTAX;
216                 }
217                 
218                 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
219         }
220         
221         sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSID");
222         
223         /* Look for the objectSID */
224         if (sid_blob) {
225                 ldb_dn_set_extended_component(dn, "SID", sid_blob);
226         }
227         return LDB_SUCCESS;
228 }
229
230 static int handle_dereference_fds(struct ldb_dn *dn,
231                               struct dsdb_openldap_dereference_result **dereference_attrs, 
232                               const char *attr, const DATA_BLOB *val)
233 {
234         const struct ldb_val *nsUniqueIdBlob, *sidBlob;
235         struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
236         int j;
237         
238         fake_msg.num_elements = 0;
239                         
240         /* Look for this attribute in the returned control */
241         for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
242                 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
243                 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
244                     && data_blob_cmp(&source_dn, val) == 0) {
245                         fake_msg.num_elements = dereference_attrs[j]->num_attributes;
246                         fake_msg.elements = dereference_attrs[j]->attributes;
247                         break;
248                 }
249         }
250         if (!fake_msg.num_elements) {
251                 return LDB_SUCCESS;
252         }
253
254         /* Look for the nsUniqueId */
255         
256         nsUniqueIdBlob = ldb_msg_find_ldb_val(&fake_msg, "nsUniqueId");
257         if (nsUniqueIdBlob) {
258                 NTSTATUS status;
259                 struct ldb_val guid_blob;
260                 struct GUID guid;
261                 
262                 status = NS_GUID_from_string((char *)nsUniqueIdBlob->data, &guid);
263                 
264                 if (!NT_STATUS_IS_OK(status)) {
265                         return LDB_ERR_INVALID_DN_SYNTAX;
266                 }
267                 status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
268                 if (!NT_STATUS_IS_OK(status)) {
269                         return LDB_ERR_INVALID_DN_SYNTAX;
270                 }
271                 
272                 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
273         }
274         
275         /* Look for the objectSID */
276
277         sidBlob = ldb_msg_find_ldb_val(&fake_msg, "sambaSID");
278         if (sidBlob) {
279                 enum ndr_err_code ndr_err;
280
281                 struct ldb_val sid_blob;
282                 struct dom_sid *sid;
283
284                 sid = dom_sid_parse_length(NULL, sidBlob);
285
286                 if (sid == NULL) {
287                         return LDB_ERR_INVALID_DN_SYNTAX;
288                 }
289
290                 ndr_err = ndr_push_struct_blob(&sid_blob, NULL, NULL, sid,
291                                                 (ndr_push_flags_fn_t)ndr_push_dom_sid);
292                 talloc_free(sid);
293                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
294                         return LDB_ERR_INVALID_DN_SYNTAX;
295                 }
296
297                 ldb_dn_set_extended_component(dn, "SID", &sid_blob);
298         }
299         return LDB_SUCCESS;
300 }
301
302 /* search */
303 struct extended_search_context {
304         struct ldb_module *module;
305         const struct dsdb_schema *schema;
306         struct ldb_request *req;
307         bool inject;
308         bool remove_guid;
309         bool remove_sid;
310         int extended_type;
311 };
312
313 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
314                 int (*handle_dereference)(struct ldb_dn *dn,
315                                 struct dsdb_openldap_dereference_result **dereference_attrs, 
316                                 const char *attr, const DATA_BLOB *val))
317 {
318         struct extended_search_context *ac;
319         struct ldb_control *control;
320         struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
321         int ret, i, j;
322         struct ldb_message *msg = ares->message;
323         struct extended_dn_out_private *p;
324         struct ldb_context *ldb;
325         bool have_reveal_control, checked_reveal_control=false;
326
327         ac = talloc_get_type(req->context, struct extended_search_context);
328         p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
329         ldb = ldb_module_get_ctx(ac->module);
330         if (!ares) {
331                 return ldb_module_done(ac->req, NULL, NULL,
332                                         LDB_ERR_OPERATIONS_ERROR);
333         }
334         if (ares->error != LDB_SUCCESS) {
335                 return ldb_module_done(ac->req, ares->controls,
336                                         ares->response, ares->error);
337         }
338
339         switch (ares->type) {
340         case LDB_REPLY_REFERRAL:
341                 return ldb_module_send_referral(ac->req, ares->referral);
342
343         case LDB_REPLY_DONE:
344                 return ldb_module_done(ac->req, ares->controls,
345                                         ares->response, LDB_SUCCESS);
346         case LDB_REPLY_ENTRY:
347                 break;
348         }
349
350         if (p && p->normalise) {
351                 ret = fix_dn(ares->message->dn);
352                 if (ret != LDB_SUCCESS) {
353                         return ldb_module_done(ac->req, NULL, NULL, ret);
354                 }
355         }
356                         
357         if (ac->inject) {
358                 /* for each record returned post-process to add any derived
359                    attributes that have been asked for */
360                 ret = inject_extended_dn_out(ares, ldb,
361                                              ac->extended_type, ac->remove_guid,
362                                              ac->remove_sid);
363                 if (ret != LDB_SUCCESS) {
364                         return ldb_module_done(ac->req, NULL, NULL, ret);
365                 }
366         }
367
368         if ((p && p->normalise) || ac->inject) {
369                 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
370                 if (val) {
371                         ldb_msg_remove_attr(ares->message, "distinguishedName");
372                         if (ac->inject) {
373                                 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName", 
374                                                                ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
375                         } else {
376                                 ret = ldb_msg_add_linearized_dn(ares->message,
377                                                                 "distinguishedName",
378                                                                 ares->message->dn);
379                         }
380                         if (ret != LDB_SUCCESS) {
381                                 ldb_oom(ldb);
382                                 return LDB_ERR_OPERATIONS_ERROR;
383                         }
384                 }
385         }
386
387         if (p && p->dereference) {
388                 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
389         
390                 if (control && control->data) {
391                         dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
392                 }
393         }
394
395         /* Walk the retruned elements (but only if we have a schema to interpret the list with) */
396         for (i = 0; ac->schema && i < msg->num_elements; i++) {
397                 bool make_extended_dn;
398                 const struct dsdb_attribute *attribute;
399                 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
400                 if (!attribute) {
401                         continue;
402                 }
403
404                 if (p->normalise) {
405                         /* If we are also in 'normalise' mode, then
406                          * fix the attribute names to be in the
407                          * correct case */
408                         msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
409                         if (!msg->elements[i].name) {
410                                 ldb_oom(ldb);
411                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
412                         }
413                 }
414
415                 /* distinguishedName has been dealt with above */
416                 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
417                         continue;
418                 }
419
420                 /* Look to see if this attributeSyntax is a DN */
421                 if (dsdb_dn_oid_to_format(attribute->syntax->ldap_oid) == DSDB_INVALID_DN) {
422                         continue;
423                 }
424
425                 make_extended_dn = ac->inject;
426
427                 /* Always show plain DN in case of Object(OR-Name) syntax */
428                 if (make_extended_dn) {
429                         make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
430                 }
431
432                 for (j = 0; j < msg->elements[i].num_values; j++) {
433                         const char *dn_str;
434                         struct ldb_dn *dn;
435                         struct dsdb_dn *dsdb_dn = NULL;
436                         struct ldb_val *plain_dn = &msg->elements[i].values[j];         
437
438                         if (!checked_reveal_control) {
439                                 have_reveal_control =
440                                         ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) != NULL;
441                                 checked_reveal_control = true;
442                         }
443
444                         /* this is a fast method for detecting deleted
445                            linked attributes, working on the unparsed
446                            ldb_val */
447                         if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
448                                 /* it's a deleted linked attribute,
449                                   and we don't have the reveal control */
450                                 memmove(&msg->elements[i].values[j],
451                                         &msg->elements[i].values[j+1],
452                                         (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
453                                 msg->elements[i].num_values--;
454                                 j--;
455                                 continue;
456                         }
457
458
459                         dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
460
461                         if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
462                                 ldb_asprintf_errstring(ldb, 
463                                                        "could not parse %.*s in %s on %s as a %s DN", 
464                                                        (int)plain_dn->length, plain_dn->data,
465                                                        msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
466                                                        attribute->syntax->ldap_oid);
467                                 talloc_free(dsdb_dn);
468                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
469                         }
470                         dn = dsdb_dn->dn;
471
472                         /* don't let users see the internal extended
473                            GUID components */
474                         if (!have_reveal_control) {
475                                 const char *accept[] = { "GUID", "SID", "WKGUID", NULL };
476                                 ldb_dn_extended_filter(dn, accept);
477                         }
478
479                         if (p->normalise) {
480                                 ret = fix_dn(dn);
481                                 if (ret != LDB_SUCCESS) {
482                                         talloc_free(dsdb_dn);
483                                         return ldb_module_done(ac->req, NULL, NULL, ret);
484                                 }
485                         }
486                         
487                         /* If we are running in dereference mode (such
488                          * as against OpenLDAP) then the DN in the msg
489                          * above does not contain the extended values,
490                          * and we need to look in the dereference
491                          * result */
492
493                         /* Look for this value in the attribute */
494
495                         if (dereference_control) {
496                                 ret = handle_dereference(dn, 
497                                                          dereference_control->attributes,
498                                                          msg->elements[i].name,
499                                                          &msg->elements[i].values[j]);
500                                 if (ret != LDB_SUCCESS) {
501                                         talloc_free(dsdb_dn);
502                                         return ldb_module_done(ac->req, NULL, NULL, ret);
503                                 }
504                         }
505                         
506                         if (make_extended_dn) {
507                                 dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
508                                                                          dsdb_dn, ac->extended_type);
509                         } else {
510                                 dn_str = dsdb_dn_get_linearized(msg->elements[i].values, 
511                                                                 dsdb_dn);
512                         }
513                         
514                         if (!dn_str) {
515                                 ldb_oom(ldb);
516                                 talloc_free(dsdb_dn);
517                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
518                         }
519                         msg->elements[i].values[j] = data_blob_string_const(dn_str);
520                         talloc_free(dsdb_dn);
521                 }
522                 if (msg->elements[i].num_values == 0) {
523                         /* we've deleted all of the values from this
524                          * element - remove the element */
525                         memmove(&msg->elements[i],
526                                 &msg->elements[i+1],
527                                 (msg->num_elements-(i+1))*sizeof(struct ldb_message_element));
528                         msg->num_elements--;
529                         i--;
530                 }
531         }
532         return ldb_module_send_entry(ac->req, msg, ares->controls);
533 }
534
535 static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
536 {
537         return extended_callback(req, ares, NULL);
538 }
539
540 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
541 {
542         return extended_callback(req, ares, handle_dereference_openldap);
543 }
544
545 static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
546 {
547         return extended_callback(req, ares, handle_dereference_fds);
548 }
549
550 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
551                 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
552 {
553         struct ldb_control *control;
554         struct ldb_control *storage_format_control;
555         struct ldb_extended_dn_control *extended_ctrl = NULL;
556         struct ldb_control **saved_controls;
557         struct extended_search_context *ac;
558         struct ldb_request *down_req;
559         char **new_attrs;
560         const char * const *const_attrs;
561         struct ldb_context *ldb = ldb_module_get_ctx(module);
562         int ret;
563
564         struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
565
566         /* check if there's an extended dn control */
567         control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
568         if (control && control->data) {
569                 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
570                 if (!extended_ctrl) {
571                         return LDB_ERR_PROTOCOL_ERROR;
572                 }
573         }
574
575         /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
576          * client is after the storage format (to fill in linked
577          * attributes) */
578         storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
579         if (!control && storage_format_control && storage_format_control->data) {
580                 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
581                 if (!extended_ctrl) {
582                         ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
583                         return LDB_ERR_PROTOCOL_ERROR;
584                 }
585         }
586
587         ac = talloc_zero(req, struct extended_search_context);
588         if (ac == NULL) {
589                 ldb_oom(ldb);
590                 return LDB_ERR_OPERATIONS_ERROR;
591         }
592
593         ac->module = module;
594         ac->schema = dsdb_get_schema(ldb);
595         ac->req = req;
596         ac->inject = false;
597         ac->remove_guid = false;
598         ac->remove_sid = false;
599         
600         const_attrs = req->op.search.attrs;
601
602         /* We only need to do special processing if we were asked for
603          * the extended DN, or we are 'store DN+GUID+SID'
604          * (!dereference) mode.  (This is the normal mode for LDB on
605          * tdb). */
606         if (control || (storage_format_control && p && !p->dereference)) {
607                 ac->inject = true;
608                 if (extended_ctrl) {
609                         ac->extended_type = extended_ctrl->type;
610                 } else {
611                         ac->extended_type = 0;
612                 }
613
614                 /* check if attrs only is specified, in that case check wether we need to modify them */
615                 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
616                         if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
617                                 ac->remove_guid = true;
618                         }
619                         if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
620                                 ac->remove_sid = true;
621                         }
622                         if (ac->remove_guid || ac->remove_sid) {
623                                 new_attrs = copy_attrs(ac, req->op.search.attrs);
624                                 if (new_attrs == NULL) {
625                                         ldb_oom(ldb);
626                                         return LDB_ERR_OPERATIONS_ERROR;
627                                 }
628
629                                 if (ac->remove_guid) {
630                                         if (!add_attrs(ac, &new_attrs, "objectGUID"))
631                                                 return LDB_ERR_OPERATIONS_ERROR;
632                                 }
633                                 if (ac->remove_sid) {
634                                         if (!add_attrs(ac, &new_attrs, "objectSID"))
635                                                 return LDB_ERR_OPERATIONS_ERROR;
636                                 }
637                                 const_attrs = (const char * const *)new_attrs;
638                         }
639                 }
640         }
641
642         ret = ldb_build_search_req_ex(&down_req,
643                                       ldb, ac,
644                                       req->op.search.base,
645                                       req->op.search.scope,
646                                       req->op.search.tree,
647                                       const_attrs,
648                                       req->controls,
649                                       ac, callback,
650                                       req);
651         if (ret != LDB_SUCCESS) {
652                 return ret;
653         }
654
655         /* Remove extended DN and storage format controls */
656
657         if (control) {
658                 /* save it locally and remove it from the list */
659                 /* we do not need to replace them later as we
660                  * are keeping the original req intact */
661                 if (!save_controls(control, down_req, &saved_controls)) {
662                         return LDB_ERR_OPERATIONS_ERROR;
663                 }
664         }
665
666         if (storage_format_control) {
667                 /* save it locally and remove it from the list */
668                 /* we do not need to replace them later as we
669                  * are keeping the original req intact */
670                 if (!save_controls(storage_format_control, down_req, &saved_controls)) {
671                         return LDB_ERR_OPERATIONS_ERROR;
672                 }
673         }
674
675         /* Add in dereference control, if we were asked to, we are
676          * using the 'dereference' mode (such as with an OpenLDAP
677          * backend) and have the control prepared */
678         if (control && p && p->dereference && p->dereference_control) {
679                 ret = ldb_request_add_control(down_req,
680                                               DSDB_OPENLDAP_DEREFERENCE_CONTROL,
681                                               false, p->dereference_control);
682                 if (ret != LDB_SUCCESS) {
683                         return ret;
684                 }
685         }
686
687         /* perform the search */
688         return ldb_next_request(module, down_req);
689 }
690
691 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
692 {
693         return extended_dn_out_search(module, req, extended_callback_ldb);
694 }
695
696 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
697 {
698         return extended_dn_out_search(module, req, extended_callback_openldap);
699 }
700
701 static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
702 {
703         return extended_dn_out_search(module, req, extended_callback_fds);
704 }
705
706 static int extended_dn_out_ldb_init(struct ldb_module *module)
707 {
708         int ret;
709
710         struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
711         struct dsdb_extended_dn_store_format *dn_format;
712
713         ldb_module_set_private(module, p);
714
715         if (!p) {
716                 ldb_oom(ldb_module_get_ctx(module));
717                 return LDB_ERR_OPERATIONS_ERROR;
718         }
719
720         dn_format = talloc(p, struct dsdb_extended_dn_store_format);
721         if (!dn_format) {
722                 talloc_free(p);
723                 ldb_oom(ldb_module_get_ctx(module));
724                 return LDB_ERR_OPERATIONS_ERROR;
725         }
726
727         dn_format->store_extended_dn_in_ldb = true;
728         ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
729         if (ret != LDB_SUCCESS) {
730                 talloc_free(p);
731                 return ret;
732         }
733
734         p->dereference = false;
735         p->normalise = false;
736
737         ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
738         if (ret != LDB_SUCCESS) {
739                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
740                         "extended_dn_out: Unable to register control with rootdse!\n");
741                 return LDB_ERR_OPERATIONS_ERROR;
742         }
743
744         return ldb_next_init(module);
745 }
746
747 static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
748 {
749         int ret, i = 0;
750         struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
751         struct dsdb_extended_dn_store_format *dn_format;
752         struct dsdb_openldap_dereference_control *dereference_control;
753         struct dsdb_attribute *cur;
754         struct ldb_context *ldb = ldb_module_get_ctx(module);
755         struct dsdb_schema *schema;
756
757         ldb_module_set_private(module, p);
758
759         if (!p) {
760                 ldb_oom(ldb);
761                 return LDB_ERR_OPERATIONS_ERROR;
762         }
763
764         dn_format = talloc(p, struct dsdb_extended_dn_store_format);
765         if (!dn_format) {
766                 talloc_free(p);
767                 ldb_oom(ldb_module_get_ctx(module));
768                 return LDB_ERR_OPERATIONS_ERROR;
769         }
770
771         dn_format->store_extended_dn_in_ldb = false;
772
773         ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
774         if (ret != LDB_SUCCESS) {
775                 talloc_free(p);
776                 return ret;
777         }
778
779         p->dereference = true;
780
781         /* At the moment, servers that need dereference also need the
782          * DN and attribute names to be normalised */
783         p->normalise = true;
784
785         ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
786         if (ret != LDB_SUCCESS) {
787                 ldb_debug(ldb, LDB_DEBUG_ERROR,
788                         "extended_dn_out: Unable to register control with rootdse!\n");
789                 return LDB_ERR_OPERATIONS_ERROR;
790         }
791
792         ret = ldb_next_init(module);
793
794         if (ret != LDB_SUCCESS) {
795                 return ret;
796         }
797
798         schema = dsdb_get_schema(ldb);
799         if (!schema) {
800                 /* No schema on this DB (yet) */
801                 return LDB_SUCCESS;
802         }
803
804         p->dereference_control = dereference_control
805                 = talloc_zero(p, struct dsdb_openldap_dereference_control);
806
807         if (!p->dereference_control) {
808                 ldb_oom(ldb);
809                 return LDB_ERR_OPERATIONS_ERROR;
810         }
811         
812         for (cur = schema->attributes; cur; cur = cur->next) {
813                 if (dsdb_dn_oid_to_format(cur->syntax->ldap_oid) == DSDB_INVALID_DN) {
814                         continue;
815                 }
816                 dereference_control->dereference
817                         = talloc_realloc(p, dereference_control->dereference,
818                                          struct dsdb_openldap_dereference *, i + 2);
819                 if (!dereference_control) {
820                         ldb_oom(ldb);
821                         return LDB_ERR_OPERATIONS_ERROR;
822                 }
823                 dereference_control->dereference[i] = talloc(dereference_control->dereference,  
824                                          struct dsdb_openldap_dereference);
825                 if (!dereference_control->dereference[i]) {
826                         ldb_oom(ldb);
827                         return LDB_ERR_OPERATIONS_ERROR;
828                 }
829                 dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
830                 dereference_control->dereference[i]->dereference_attribute = attrs;
831                 i++;
832                 dereference_control->dereference[i] = NULL;
833         }
834         return LDB_SUCCESS;
835 }
836
837 static int extended_dn_out_openldap_init(struct ldb_module *module)
838 {
839         static const char *attrs[] = {
840                 "entryUUID",
841                 "objectSID",
842                 NULL
843         };
844
845         return extended_dn_out_dereference_init(module, attrs);
846 }
847
848 static int extended_dn_out_fds_init(struct ldb_module *module)
849 {
850         static const char *attrs[] = {
851                 "nsUniqueId",
852                 "objectSID",
853                 NULL
854         };
855
856         return extended_dn_out_dereference_init(module, attrs);
857 }
858
859 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
860         .name              = "extended_dn_out_ldb",
861         .search            = extended_dn_out_ldb_search,
862         .init_context      = extended_dn_out_ldb_init,
863 };
864
865 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
866         .name              = "extended_dn_out_openldap",
867         .search            = extended_dn_out_openldap_search,
868         .init_context      = extended_dn_out_openldap_init,
869 };
870
871 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
872         .name              = "extended_dn_out_fds",
873         .search            = extended_dn_out_fds_search,
874         .init_context      = extended_dn_out_fds_init,
875 };