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