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