c52953c206c2be71835b0b32d3f6aaf957c63e9f
[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                 enum ndr_err_code ndr_err;
206                 
207                 struct ldb_val guid_blob;
208                 struct GUID guid;
209                 
210                 status = GUID_from_data_blob(entryUUIDblob, &guid);
211                 
212                 if (!NT_STATUS_IS_OK(status)) {
213                         return LDB_ERR_INVALID_DN_SYNTAX;
214                 }
215                 ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
216                                                (ndr_push_flags_fn_t)ndr_push_GUID);
217                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
218                         return LDB_ERR_INVALID_DN_SYNTAX;
219                 }
220                 
221                 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
222         }
223         
224         sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSID");
225         
226         /* Look for the objectSID */
227         if (sid_blob) {
228                 ldb_dn_set_extended_component(dn, "SID", sid_blob);
229         }
230         return LDB_SUCCESS;
231 }
232
233 static int handle_dereference_fds(struct ldb_dn *dn,
234                               struct dsdb_openldap_dereference_result **dereference_attrs, 
235                               const char *attr, const DATA_BLOB *val)
236 {
237         const struct ldb_val *nsUniqueIdBlob, *sidBlob;
238         struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
239         int j;
240         
241         fake_msg.num_elements = 0;
242                         
243         /* Look for this attribute in the returned control */
244         for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
245                 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
246                 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
247                     && data_blob_cmp(&source_dn, val) == 0) {
248                         fake_msg.num_elements = dereference_attrs[j]->num_attributes;
249                         fake_msg.elements = dereference_attrs[j]->attributes;
250                         break;
251                 }
252         }
253         if (!fake_msg.num_elements) {
254                 return LDB_SUCCESS;
255         }
256
257         /* Look for the nsUniqueId */
258         
259         nsUniqueIdBlob = ldb_msg_find_ldb_val(&fake_msg, "nsUniqueId");
260         if (nsUniqueIdBlob) {
261                 NTSTATUS status;
262                 enum ndr_err_code ndr_err;
263                 
264                 struct ldb_val guid_blob;
265                 struct GUID guid;
266                 
267                 status = NS_GUID_from_string((char *)nsUniqueIdBlob->data, &guid);
268                 
269                 if (!NT_STATUS_IS_OK(status)) {
270                         return LDB_ERR_INVALID_DN_SYNTAX;
271                 }
272                 ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
273                                                 (ndr_push_flags_fn_t)ndr_push_GUID);
274                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
275                         return LDB_ERR_INVALID_DN_SYNTAX;
276                 }
277                 
278                 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
279         }
280         
281         /* Look for the objectSID */
282
283         sidBlob = ldb_msg_find_ldb_val(&fake_msg, "sambaSID");
284         if (sidBlob) {
285                 enum ndr_err_code ndr_err;
286
287                 struct ldb_val sid_blob;
288                 struct dom_sid *sid;
289
290                 sid = dom_sid_parse_length(NULL, sidBlob);
291
292                 if (sid == NULL) {
293                         return LDB_ERR_INVALID_DN_SYNTAX;
294                 }
295
296                 ndr_err = ndr_push_struct_blob(&sid_blob, NULL, NULL, sid,
297                                                 (ndr_push_flags_fn_t)ndr_push_dom_sid);
298                 talloc_free(sid);
299                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
300                         return LDB_ERR_INVALID_DN_SYNTAX;
301                 }
302
303                 ldb_dn_set_extended_component(dn, "SID", &sid_blob);
304         }
305         return LDB_SUCCESS;
306 }
307
308 /* search */
309 struct extended_search_context {
310         struct ldb_module *module;
311         const struct dsdb_schema *schema;
312         struct ldb_request *req;
313         bool inject;
314         bool remove_guid;
315         bool remove_sid;
316         int extended_type;
317 };
318
319 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
320                 int (*handle_dereference)(struct ldb_dn *dn,
321                                 struct dsdb_openldap_dereference_result **dereference_attrs, 
322                                 const char *attr, const DATA_BLOB *val))
323 {
324         struct extended_search_context *ac;
325         struct ldb_control *control;
326         struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
327         int ret, i, j;
328         struct ldb_message *msg = ares->message;
329         struct extended_dn_out_private *p;
330         struct ldb_context *ldb;
331         ac = talloc_get_type(req->context, struct extended_search_context);
332         p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
333         ldb = ldb_module_get_ctx(ac->module);
334         if (!ares) {
335                 return ldb_module_done(ac->req, NULL, NULL,
336                                         LDB_ERR_OPERATIONS_ERROR);
337         }
338         if (ares->error != LDB_SUCCESS) {
339                 return ldb_module_done(ac->req, ares->controls,
340                                         ares->response, ares->error);
341         }
342
343         switch (ares->type) {
344         case LDB_REPLY_REFERRAL:
345                 return ldb_module_send_referral(ac->req, ares->referral);
346
347         case LDB_REPLY_DONE:
348                 return ldb_module_done(ac->req, ares->controls,
349                                         ares->response, LDB_SUCCESS);
350         case LDB_REPLY_ENTRY:
351                 break;
352         }
353
354         if (p && p->normalise) {
355                 ret = fix_dn(ares->message->dn);
356                 if (ret != LDB_SUCCESS) {
357                         return ldb_module_done(ac->req, NULL, NULL, ret);
358                 }
359         }
360                         
361         if (ac->inject) {
362                 /* for each record returned post-process to add any derived
363                    attributes that have been asked for */
364                 ret = inject_extended_dn_out(ares, ldb,
365                                              ac->extended_type, ac->remove_guid,
366                                              ac->remove_sid);
367                 if (ret != LDB_SUCCESS) {
368                         return ldb_module_done(ac->req, NULL, NULL, ret);
369                 }
370         }
371
372         if ((p && p->normalise) || ac->inject) {
373                 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
374                 if (val) {
375                         ldb_msg_remove_attr(ares->message, "distinguishedName");
376                         if (ac->inject) {
377                                 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName", 
378                                                                ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
379                         } else {
380                                 ret = ldb_msg_add_dn(ares->message,
381                                                      "distinguishedName",
382                                                      ares->message->dn);
383                         }
384                         if (ret != LDB_SUCCESS) {
385                                 ldb_oom(ldb);
386                                 return LDB_ERR_OPERATIONS_ERROR;
387                         }
388                 }
389         }
390
391         if (p && p->dereference) {
392                 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
393         
394                 if (control && control->data) {
395                         dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
396                 }
397         }
398
399         /* Walk the retruned elements (but only if we have a schema to interpret the list with) */
400         for (i = 0; ac->schema && i < msg->num_elements; i++) {
401                 bool make_extended_dn;
402                 const struct dsdb_attribute *attribute;
403                 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
404                 if (!attribute) {
405                         continue;
406                 }
407
408                 if (p->normalise) {
409                         /* If we are also in 'normalise' mode, then
410                          * fix the attribute names to be in the
411                          * correct case */
412                         msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
413                         if (!msg->elements[i].name) {
414                                 ldb_oom(ldb);
415                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
416                         }
417                 }
418
419                 /* distinguishedName has been dealt with above */
420                 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
421                         continue;
422                 }
423
424                 /* Look to see if this attributeSyntax is a DN */
425                 if (dsdb_dn_oid_to_format(attribute->syntax->ldap_oid) == DSDB_INVALID_DN) {
426                         continue;
427                 }
428
429                 make_extended_dn = ac->inject;
430
431                 /* Always show plain DN in case of Object(OR-Name) syntax */
432                 if (make_extended_dn) {
433                         make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
434                 }
435
436                 for (j = 0; j < msg->elements[i].num_values; j++) {
437                         const char *dn_str;
438                         struct ldb_dn *dn;
439                         struct dsdb_dn *dsdb_dn = NULL;
440                         struct ldb_val *plain_dn = &msg->elements[i].values[j];         
441                         dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
442                         
443                         if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
444                                 ldb_asprintf_errstring(ldb, 
445                                                        "could not parse %.*s in %s on %s as a %s DN", 
446                                                        (int)plain_dn->length, plain_dn->data,
447                                                        msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
448                                                        attribute->syntax->ldap_oid);
449                                 talloc_free(dsdb_dn);
450                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
451                         }
452                         dn = dsdb_dn->dn;
453
454                         if (p->normalise) {
455                                 ret = fix_dn(dn);
456                                 if (ret != LDB_SUCCESS) {
457                                         talloc_free(dsdb_dn);
458                                         return ldb_module_done(ac->req, NULL, NULL, ret);
459                                 }
460                         }
461                         
462                         /* If we are running in dereference mode (such
463                          * as against OpenLDAP) then the DN in the msg
464                          * above does not contain the extended values,
465                          * and we need to look in the dereference
466                          * result */
467
468                         /* Look for this value in the attribute */
469
470                         if (dereference_control) {
471                                 ret = handle_dereference(dn, 
472                                                          dereference_control->attributes,
473                                                          msg->elements[i].name,
474                                                          &msg->elements[i].values[j]);
475                                 if (ret != LDB_SUCCESS) {
476                                         talloc_free(dsdb_dn);
477                                         return ldb_module_done(ac->req, NULL, NULL, ret);
478                                 }
479                         }
480                         
481                         if (make_extended_dn) {
482                                 dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
483                                                                          dsdb_dn, ac->extended_type);
484                         } else {
485                                 dn_str = dsdb_dn_get_linearized(msg->elements[i].values, 
486                                                                 dsdb_dn);
487                         }
488                         
489                         if (!dn_str) {
490                                 ldb_oom(ldb);
491                                 talloc_free(dsdb_dn);
492                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
493                         }
494                         msg->elements[i].values[j] = data_blob_string_const(dn_str);
495                         talloc_free(dsdb_dn);
496                 }
497         }
498         return ldb_module_send_entry(ac->req, msg, ares->controls);
499 }
500
501 static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
502 {
503         return extended_callback(req, ares, NULL);
504 }
505
506 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
507 {
508         return extended_callback(req, ares, handle_dereference_openldap);
509 }
510
511 static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
512 {
513         return extended_callback(req, ares, handle_dereference_fds);
514 }
515
516 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
517                 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
518 {
519         struct ldb_control *control;
520         struct ldb_control *storage_format_control;
521         struct ldb_extended_dn_control *extended_ctrl = NULL;
522         struct ldb_control **saved_controls;
523         struct extended_search_context *ac;
524         struct ldb_request *down_req;
525         char **new_attrs;
526         const char * const *const_attrs;
527         struct ldb_context *ldb = ldb_module_get_ctx(module);
528         int ret;
529
530         struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
531
532         /* check if there's an extended dn control */
533         control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
534         if (control && control->data) {
535                 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
536                 if (!extended_ctrl) {
537                         return LDB_ERR_PROTOCOL_ERROR;
538                 }
539         }
540
541         /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
542          * client is after the storage format (to fill in linked
543          * attributes) */
544         storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
545         if (!control && storage_format_control && storage_format_control->data) {
546                 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
547                 if (!extended_ctrl) {
548                         ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
549                         return LDB_ERR_PROTOCOL_ERROR;
550                 }
551         }
552
553         ac = talloc_zero(req, struct extended_search_context);
554         if (ac == NULL) {
555                 ldb_oom(ldb);
556                 return LDB_ERR_OPERATIONS_ERROR;
557         }
558
559         ac->module = module;
560         ac->schema = dsdb_get_schema(ldb);
561         ac->req = req;
562         ac->inject = false;
563         ac->remove_guid = false;
564         ac->remove_sid = false;
565         
566         const_attrs = req->op.search.attrs;
567
568         /* We only need to do special processing if we were asked for
569          * the extended DN, or we are 'store DN+GUID+SID'
570          * (!dereference) mode.  (This is the normal mode for LDB on
571          * tdb). */
572         if (control || (storage_format_control && p && !p->dereference)) {
573                 ac->inject = true;
574                 if (extended_ctrl) {
575                         ac->extended_type = extended_ctrl->type;
576                 } else {
577                         ac->extended_type = 0;
578                 }
579
580                 /* check if attrs only is specified, in that case check wether we need to modify them */
581                 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
582                         if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
583                                 ac->remove_guid = true;
584                         }
585                         if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
586                                 ac->remove_sid = true;
587                         }
588                         if (ac->remove_guid || ac->remove_sid) {
589                                 new_attrs = copy_attrs(ac, req->op.search.attrs);
590                                 if (new_attrs == NULL) {
591                                         ldb_oom(ldb);
592                                         return LDB_ERR_OPERATIONS_ERROR;
593                                 }
594
595                                 if (ac->remove_guid) {
596                                         if (!add_attrs(ac, &new_attrs, "objectGUID"))
597                                                 return LDB_ERR_OPERATIONS_ERROR;
598                                 }
599                                 if (ac->remove_sid) {
600                                         if (!add_attrs(ac, &new_attrs, "objectSID"))
601                                                 return LDB_ERR_OPERATIONS_ERROR;
602                                 }
603                                 const_attrs = (const char * const *)new_attrs;
604                         }
605                 }
606         }
607
608         ret = ldb_build_search_req_ex(&down_req,
609                                       ldb, ac,
610                                       req->op.search.base,
611                                       req->op.search.scope,
612                                       req->op.search.tree,
613                                       const_attrs,
614                                       req->controls,
615                                       ac, callback,
616                                       req);
617         if (ret != LDB_SUCCESS) {
618                 return ret;
619         }
620
621         /* Remove extended DN and storage format controls */
622
623         if (control) {
624                 /* save it locally and remove it from the list */
625                 /* we do not need to replace them later as we
626                  * are keeping the original req intact */
627                 if (!save_controls(control, down_req, &saved_controls)) {
628                         return LDB_ERR_OPERATIONS_ERROR;
629                 }
630         }
631
632         if (storage_format_control) {
633                 /* save it locally and remove it from the list */
634                 /* we do not need to replace them later as we
635                  * are keeping the original req intact */
636                 if (!save_controls(storage_format_control, down_req, &saved_controls)) {
637                         return LDB_ERR_OPERATIONS_ERROR;
638                 }
639         }
640
641         /* Add in dereference control, if we were asked to, we are
642          * using the 'dereference' mode (such as with an OpenLDAP
643          * backend) and have the control prepared */
644         if (control && p && p->dereference && p->dereference_control) {
645                 ret = ldb_request_add_control(down_req,
646                                               DSDB_OPENLDAP_DEREFERENCE_CONTROL,
647                                               false, p->dereference_control);
648                 if (ret != LDB_SUCCESS) {
649                         return ret;
650                 }
651         }
652
653         /* perform the search */
654         return ldb_next_request(module, down_req);
655 }
656
657 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
658 {
659         return extended_dn_out_search(module, req, extended_callback_ldb);
660 }
661
662 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
663 {
664         return extended_dn_out_search(module, req, extended_callback_openldap);
665 }
666
667 static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
668 {
669         return extended_dn_out_search(module, req, extended_callback_fds);
670 }
671
672 static int extended_dn_out_ldb_init(struct ldb_module *module)
673 {
674         int ret;
675
676         struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
677         struct dsdb_extended_dn_store_format *dn_format;
678
679         ldb_module_set_private(module, p);
680
681         if (!p) {
682                 ldb_oom(ldb_module_get_ctx(module));
683                 return LDB_ERR_OPERATIONS_ERROR;
684         }
685
686         dn_format = talloc(p, struct dsdb_extended_dn_store_format);
687         if (!dn_format) {
688                 talloc_free(p);
689                 ldb_oom(ldb_module_get_ctx(module));
690                 return LDB_ERR_OPERATIONS_ERROR;
691         }
692
693         dn_format->store_extended_dn_in_ldb = true;
694         ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
695         if (ret != LDB_SUCCESS) {
696                 talloc_free(p);
697                 return ret;
698         }
699
700         p->dereference = false;
701         p->normalise = false;
702
703         ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
704         if (ret != LDB_SUCCESS) {
705                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
706                         "extended_dn_out: Unable to register control with rootdse!\n");
707                 return LDB_ERR_OPERATIONS_ERROR;
708         }
709
710         return ldb_next_init(module);
711 }
712
713 static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
714 {
715         int ret, i = 0;
716         struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
717         struct dsdb_extended_dn_store_format *dn_format;
718         struct dsdb_openldap_dereference_control *dereference_control;
719         struct dsdb_attribute *cur;
720         struct ldb_context *ldb = ldb_module_get_ctx(module);
721         struct dsdb_schema *schema;
722
723         ldb_module_set_private(module, p);
724
725         if (!p) {
726                 ldb_oom(ldb);
727                 return LDB_ERR_OPERATIONS_ERROR;
728         }
729
730         dn_format = talloc(p, struct dsdb_extended_dn_store_format);
731         if (!dn_format) {
732                 talloc_free(p);
733                 ldb_oom(ldb_module_get_ctx(module));
734                 return LDB_ERR_OPERATIONS_ERROR;
735         }
736
737         dn_format->store_extended_dn_in_ldb = false;
738
739         ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
740         if (ret != LDB_SUCCESS) {
741                 talloc_free(p);
742                 return ret;
743         }
744
745         p->dereference = true;
746
747         /* At the moment, servers that need dereference also need the
748          * DN and attribute names to be normalised */
749         p->normalise = true;
750
751         ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
752         if (ret != LDB_SUCCESS) {
753                 ldb_debug(ldb, LDB_DEBUG_ERROR,
754                         "extended_dn_out: Unable to register control with rootdse!\n");
755                 return LDB_ERR_OPERATIONS_ERROR;
756         }
757
758         ret = ldb_next_init(module);
759
760         if (ret != LDB_SUCCESS) {
761                 return ret;
762         }
763
764         schema = dsdb_get_schema(ldb);
765         if (!schema) {
766                 /* No schema on this DB (yet) */
767                 return LDB_SUCCESS;
768         }
769
770         p->dereference_control = dereference_control
771                 = talloc_zero(p, struct dsdb_openldap_dereference_control);
772
773         if (!p->dereference_control) {
774                 ldb_oom(ldb);
775                 return LDB_ERR_OPERATIONS_ERROR;
776         }
777         
778         for (cur = schema->attributes; cur; cur = cur->next) {
779                 if (dsdb_dn_oid_to_format(cur->syntax->ldap_oid) == DSDB_INVALID_DN) {
780                         continue;
781                 }
782                 dereference_control->dereference
783                         = talloc_realloc(p, dereference_control->dereference,
784                                          struct dsdb_openldap_dereference *, i + 2);
785                 if (!dereference_control) {
786                         ldb_oom(ldb);
787                         return LDB_ERR_OPERATIONS_ERROR;
788                 }
789                 dereference_control->dereference[i] = talloc(dereference_control->dereference,  
790                                          struct dsdb_openldap_dereference);
791                 if (!dereference_control->dereference[i]) {
792                         ldb_oom(ldb);
793                         return LDB_ERR_OPERATIONS_ERROR;
794                 }
795                 dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
796                 dereference_control->dereference[i]->dereference_attribute = attrs;
797                 i++;
798                 dereference_control->dereference[i] = NULL;
799         }
800         return LDB_SUCCESS;
801 }
802
803 static int extended_dn_out_openldap_init(struct ldb_module *module)
804 {
805         static const char *attrs[] = {
806                 "entryUUID",
807                 "objectSID",
808                 NULL
809         };
810
811         return extended_dn_out_dereference_init(module, attrs);
812 }
813
814 static int extended_dn_out_fds_init(struct ldb_module *module)
815 {
816         static const char *attrs[] = {
817                 "nsUniqueId",
818                 "objectSID",
819                 NULL
820         };
821
822         return extended_dn_out_dereference_init(module, attrs);
823 }
824
825 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
826         .name              = "extended_dn_out_ldb",
827         .search            = extended_dn_out_ldb_search,
828         .init_context      = extended_dn_out_ldb_init,
829 };
830
831 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
832         .name              = "extended_dn_out_openldap",
833         .search            = extended_dn_out_openldap_search,
834         .init_context      = extended_dn_out_openldap_init,
835 };
836
837 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
838         .name              = "extended_dn_out_fds",
839         .search            = extended_dn_out_fds_search,
840         .init_context      = extended_dn_out_fds_init,
841 };