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