dsdb: Fix warning about unused var
[metze/samba/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 "dsdb/samdb/ldb_modules/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 (cur->dn_format != 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                             bool is_deleted_objects, bool *remove_value)
358 {
359         struct GUID guid;
360         NTSTATUS status;
361         int ret;
362         struct ldb_dn *real_dn;
363         uint32_t search_flags;
364         TALLOC_CTX *tmp_ctx = talloc_new(ac);
365         const char *attrs[] = { NULL };
366         struct ldb_result *res;
367
368         (*remove_value) = false;
369
370         status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
371         if (!NT_STATUS_IS_OK(status)) {
372                 /* this is a strange DN that doesn't have a GUID! just
373                    return the current DN string?? */
374                 talloc_free(tmp_ctx);
375                 return LDB_SUCCESS;
376         }
377
378         search_flags = DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SEARCH_ALL_PARTITIONS | DSDB_SEARCH_ONE_ONLY;
379
380         if (ldb_request_get_control(ac->req, LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID) ||
381             is_deleted_objects) {
382                 search_flags |= DSDB_SEARCH_SHOW_DELETED;
383         }
384
385         ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
386                                  search_flags, ac->req, "objectguid=%s", GUID_string(tmp_ctx, &guid));
387         if (ret != LDB_SUCCESS || res->count != 1) {
388                 /* if we can't resolve this GUID, then we don't
389                    display the link. This could be a link to a NC that we don't
390                    have, or it could be a link to a deleted object
391                 */
392                 (*remove_value) = true;
393                 talloc_free(tmp_ctx);
394                 return LDB_SUCCESS;
395         }
396         real_dn = res->msgs[0]->dn;
397
398         if (strcmp(ldb_dn_get_linearized(dn), ldb_dn_get_linearized(real_dn)) == 0) {
399                 /* its already correct */
400                 talloc_free(tmp_ctx);
401                 return LDB_SUCCESS;
402         }
403
404         /* fix the DN by replacing its components with those from the
405          * real DN
406          */
407         if (!ldb_dn_replace_components(dn, real_dn)) {
408                 talloc_free(tmp_ctx);
409                 return ldb_operr(ldb_module_get_ctx(ac->module));
410         }
411         talloc_free(tmp_ctx);
412
413         return LDB_SUCCESS;
414 }
415
416
417 /*
418   this is called to post-process the results from the search
419  */
420 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
421                 int (*handle_dereference)(struct ldb_dn *dn,
422                                 struct dsdb_openldap_dereference_result **dereference_attrs, 
423                                 const char *attr, const DATA_BLOB *val))
424 {
425         struct extended_search_context *ac;
426         struct ldb_control *control;
427         struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
428         int ret;
429         unsigned int i, j;
430         struct ldb_message *msg;
431         struct extended_dn_out_private *p;
432         struct ldb_context *ldb;
433         bool have_reveal_control=false, checked_reveal_control=false;
434
435         ac = talloc_get_type(req->context, struct extended_search_context);
436         p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
437         ldb = ldb_module_get_ctx(ac->module);
438         if (!ares) {
439                 return ldb_module_done(ac->req, NULL, NULL,
440                                         LDB_ERR_OPERATIONS_ERROR);
441         }
442         if (ares->error != LDB_SUCCESS) {
443                 return ldb_module_done(ac->req, ares->controls,
444                                         ares->response, ares->error);
445         }
446
447         msg = ares->message;
448
449         switch (ares->type) {
450         case LDB_REPLY_REFERRAL:
451                 return ldb_module_send_referral(ac->req, ares->referral);
452
453         case LDB_REPLY_DONE:
454                 return ldb_module_done(ac->req, ares->controls,
455                                         ares->response, LDB_SUCCESS);
456         case LDB_REPLY_ENTRY:
457                 break;
458         }
459
460         if (p && p->normalise) {
461                 ret = fix_dn(ldb, ares->message->dn);
462                 if (ret != LDB_SUCCESS) {
463                         return ldb_module_done(ac->req, NULL, NULL, ret);
464                 }
465         }
466                         
467         if (ac->inject) {
468                 /* for each record returned post-process to add any derived
469                    attributes that have been asked for */
470                 ret = inject_extended_dn_out(ares, ldb,
471                                              ac->extended_type, ac->remove_guid,
472                                              ac->remove_sid);
473                 if (ret != LDB_SUCCESS) {
474                         return ldb_module_done(ac->req, NULL, NULL, ret);
475                 }
476         }
477
478         if ((p && p->normalise) || ac->inject) {
479                 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
480                 if (val) {
481                         ldb_msg_remove_attr(ares->message, "distinguishedName");
482                         if (ac->inject) {
483                                 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName", 
484                                                                ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
485                         } else {
486                                 ret = ldb_msg_add_linearized_dn(ares->message,
487                                                                 "distinguishedName",
488                                                                 ares->message->dn);
489                         }
490                         if (ret != LDB_SUCCESS) {
491                                 return ldb_oom(ldb);
492                         }
493                 }
494         }
495
496         if (p && p->dereference) {
497                 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
498         
499                 if (control && control->data) {
500                         dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
501                 }
502         }
503
504         /* Walk the returned elements (but only if we have a schema to
505          * interpret the list with) */
506         for (i = 0; ac->schema && i < msg->num_elements; i++) {
507                 bool make_extended_dn;
508                 const struct dsdb_attribute *attribute;
509
510                 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
511                 if (!attribute) {
512                         continue;
513                 }
514
515                 if (p->normalise) {
516                         /* If we are also in 'normalise' mode, then
517                          * fix the attribute names to be in the
518                          * correct case */
519                         msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
520                         if (!msg->elements[i].name) {
521                                 ldb_oom(ldb);
522                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
523                         }
524                 }
525
526                 /* distinguishedName has been dealt with above */
527                 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
528                         continue;
529                 }
530
531                 /* Look to see if this attributeSyntax is a DN */
532                 if (attribute->dn_format == DSDB_INVALID_DN) {
533                         continue;
534                 }
535
536                 make_extended_dn = ac->inject;
537
538                 /* Always show plain DN in case of Object(OR-Name) syntax */
539                 if (make_extended_dn) {
540                         make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
541                 }
542
543                 for (j = 0; j < msg->elements[i].num_values; j++) {
544                         const char *dn_str;
545                         struct ldb_dn *dn;
546                         struct dsdb_dn *dsdb_dn = NULL;
547                         struct ldb_val *plain_dn = &msg->elements[i].values[j];         
548                         bool is_deleted_objects = false;
549
550                         if (!checked_reveal_control) {
551                                 have_reveal_control =
552                                         ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) != NULL;
553                                 checked_reveal_control = true;
554                         }
555
556                         /* this is a fast method for detecting deleted
557                            linked attributes, working on the unparsed
558                            ldb_val */
559                         if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
560                                 /* it's a deleted linked attribute,
561                                   and we don't have the reveal control */
562                                 memmove(&msg->elements[i].values[j],
563                                         &msg->elements[i].values[j+1],
564                                         (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
565                                 msg->elements[i].num_values--;
566                                 j--;
567                                 continue;
568                         }
569
570
571                         dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
572
573                         if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
574                                 ldb_asprintf_errstring(ldb, 
575                                                        "could not parse %.*s in %s on %s as a %s DN", 
576                                                        (int)plain_dn->length, plain_dn->data,
577                                                        msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
578                                                        attribute->syntax->ldap_oid);
579                                 talloc_free(dsdb_dn);
580                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
581                         }
582                         dn = dsdb_dn->dn;
583
584                         /* we need to know if this is a link to the
585                            deleted objects container for fixing one way
586                            links */
587                         if (dsdb_dn->extra_part.length == 16) {
588                                 char *hex_string = data_blob_hex_string_upper(req, &dsdb_dn->extra_part);
589                                 if (hex_string && strcmp(hex_string, DS_GUID_DELETED_OBJECTS_CONTAINER) == 0) {
590                                         is_deleted_objects = true;
591                                 }
592                                 talloc_free(hex_string);
593                         }
594
595                         /* don't let users see the internal extended
596                            GUID components */
597                         if (!have_reveal_control) {
598                                 const char *accept[] = { "GUID", "SID", NULL };
599                                 ldb_dn_extended_filter(dn, accept);
600                         }
601
602                         if (p->normalise) {
603                                 ret = fix_dn(ldb, dn);
604                                 if (ret != LDB_SUCCESS) {
605                                         talloc_free(dsdb_dn);
606                                         return ldb_module_done(ac->req, NULL, NULL, ret);
607                                 }
608                         }
609                         
610                         /* If we are running in dereference mode (such
611                          * as against OpenLDAP) then the DN in the msg
612                          * above does not contain the extended values,
613                          * and we need to look in the dereference
614                          * result */
615
616                         /* Look for this value in the attribute */
617
618                         if (dereference_control) {
619                                 ret = handle_dereference(dn, 
620                                                          dereference_control->attributes,
621                                                          msg->elements[i].name,
622                                                          &msg->elements[i].values[j]);
623                                 if (ret != LDB_SUCCESS) {
624                                         talloc_free(dsdb_dn);
625                                         return ldb_module_done(ac->req, NULL, NULL, ret);
626                                 }
627                         }
628
629                         /* note that we don't fixup objectCategory as
630                            it should not be possible to move
631                            objectCategory elements in the schema */
632                         if (attribute->one_way_link &&
633                             strcasecmp(attribute->lDAPDisplayName, "objectCategory") != 0) {
634                                 bool remove_value;
635                                 ret = fix_one_way_link(ac, dn, is_deleted_objects, &remove_value);
636                                 if (ret != LDB_SUCCESS) {
637                                         talloc_free(dsdb_dn);
638                                         return ldb_module_done(ac->req, NULL, NULL, ret);
639                                 }
640                                 if (remove_value &&
641                                     !ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS)) {
642                                         /* we show these with REVEAL
643                                            to allow dbcheck to find and
644                                            cleanup these orphaned links */
645                                         memmove(&msg->elements[i].values[j],
646                                                 &msg->elements[i].values[j+1],
647                                                 (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
648                                         msg->elements[i].num_values--;
649                                         j--;
650                                         continue;
651                                 }
652                         }
653                         
654                         if (make_extended_dn) {
655                                 dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
656                                                                          dsdb_dn, ac->extended_type);
657                         } else {
658                                 dn_str = dsdb_dn_get_linearized(msg->elements[i].values, 
659                                                                 dsdb_dn);
660                         }
661                         
662                         if (!dn_str) {
663                                 ldb_oom(ldb);
664                                 talloc_free(dsdb_dn);
665                                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
666                         }
667                         msg->elements[i].values[j] = data_blob_string_const(dn_str);
668                         talloc_free(dsdb_dn);
669                 }
670                 if (msg->elements[i].num_values == 0) {
671                         /* we've deleted all of the values from this
672                          * element - remove the element */
673                         memmove(&msg->elements[i],
674                                 &msg->elements[i+1],
675                                 (msg->num_elements-(i+1))*sizeof(struct ldb_message_element));
676                         msg->num_elements--;
677                         i--;
678                 }
679         }
680         return ldb_module_send_entry(ac->req, msg, ares->controls);
681 }
682
683 static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
684 {
685         return extended_callback(req, ares, NULL);
686 }
687
688 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
689 {
690         return extended_callback(req, ares, handle_dereference_openldap);
691 }
692
693 static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
694 {
695         return extended_callback(req, ares, handle_dereference_fds);
696 }
697
698 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
699                 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
700 {
701         struct ldb_control *control;
702         struct ldb_control *storage_format_control;
703         struct ldb_extended_dn_control *extended_ctrl = NULL;
704         struct extended_search_context *ac;
705         struct ldb_request *down_req;
706         char **new_attrs;
707         const char * const *const_attrs;
708         struct ldb_context *ldb = ldb_module_get_ctx(module);
709         int ret;
710
711         struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
712
713         /* The schema manipulation does not apply to special DNs */
714         if (ldb_dn_is_special(req->op.search.base)) {
715                 return ldb_next_request(module, req);
716         }
717
718         /* check if there's an extended dn control */
719         control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
720         if (control && control->data) {
721                 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
722                 if (!extended_ctrl) {
723                         return LDB_ERR_PROTOCOL_ERROR;
724                 }
725         }
726
727         /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
728          * client is after the storage format (to fill in linked
729          * attributes) */
730         storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
731         if (!control && storage_format_control && storage_format_control->data) {
732                 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
733                 if (!extended_ctrl) {
734                         ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
735                         return LDB_ERR_PROTOCOL_ERROR;
736                 }
737         }
738
739         ac = talloc_zero(req, struct extended_search_context);
740         if (ac == NULL) {
741                 return ldb_oom(ldb);
742         }
743
744         ac->module = module;
745         ac->schema = dsdb_get_schema(ldb, ac);
746         ac->req = req;
747         ac->inject = false;
748         ac->remove_guid = false;
749         ac->remove_sid = false;
750         
751         const_attrs = req->op.search.attrs;
752
753         /* We only need to do special processing if we were asked for
754          * the extended DN, or we are 'store DN+GUID+SID'
755          * (!dereference) mode.  (This is the normal mode for LDB on
756          * tdb). */
757         if (control || (storage_format_control && p && !p->dereference)) {
758                 ac->inject = true;
759                 if (extended_ctrl) {
760                         ac->extended_type = extended_ctrl->type;
761                 } else {
762                         ac->extended_type = 0;
763                 }
764
765                 /* check if attrs only is specified, in that case check wether we need to modify them */
766                 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
767                         if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
768                                 ac->remove_guid = true;
769                         }
770                         if (! is_attr_in_list(req->op.search.attrs, "objectSid")) {
771                                 ac->remove_sid = true;
772                         }
773                         if (ac->remove_guid || ac->remove_sid) {
774                                 new_attrs = copy_attrs(ac, req->op.search.attrs);
775                                 if (new_attrs == NULL) {
776                                         return ldb_oom(ldb);
777                                 }
778
779                                 if (ac->remove_guid) {
780                                         if (!add_attrs(ac, &new_attrs, "objectGUID"))
781                                                 return ldb_operr(ldb);
782                                 }
783                                 if (ac->remove_sid) {
784                                         if (!add_attrs(ac, &new_attrs, "objectSid"))
785                                                 return ldb_operr(ldb);
786                                 }
787                                 const_attrs = (const char * const *)new_attrs;
788                         }
789                 }
790         }
791
792         ret = ldb_build_search_req_ex(&down_req,
793                                       ldb, ac,
794                                       req->op.search.base,
795                                       req->op.search.scope,
796                                       req->op.search.tree,
797                                       const_attrs,
798                                       req->controls,
799                                       ac, callback,
800                                       req);
801         LDB_REQ_SET_LOCATION(down_req);
802         if (ret != LDB_SUCCESS) {
803                 return ret;
804         }
805
806         /* mark extended DN and storage format controls as done */
807         if (control) {
808                 control->critical = 0;
809         }
810
811         if (storage_format_control) {
812                 storage_format_control->critical = 0;
813         }
814
815         /* Add in dereference control, if we were asked to, we are
816          * using the 'dereference' mode (such as with an OpenLDAP
817          * backend) and have the control prepared */
818         if (control && p && p->dereference) {
819                 ret = extended_dn_out_dereference_setup_control(ldb, p);
820                 if (ret != LDB_SUCCESS) {
821                         return ret;
822                 }
823
824                 /* We should always have this, but before the schema
825                  * is with us, things get tricky */
826                 if (p->dereference_control) {
827
828                         /* This control must *not* be critical,
829                          * because if this particular request did not
830                          * return any dereferencable attributes in the
831                          * end, then OpenLDAP will reply with
832                          * unavailableCriticalExtension, rather than
833                          * just an empty return control */
834                         ret = ldb_request_add_control(down_req,
835                                                       DSDB_OPENLDAP_DEREFERENCE_CONTROL,
836                                                       false, p->dereference_control);
837                         if (ret != LDB_SUCCESS) {
838                                 return ret;
839                         }
840                 }
841         }
842
843         /* perform the search */
844         return ldb_next_request(module, down_req);
845 }
846
847 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
848 {
849         return extended_dn_out_search(module, req, extended_callback_ldb);
850 }
851
852 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
853 {
854         return extended_dn_out_search(module, req, extended_callback_openldap);
855 }
856
857 static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
858 {
859         return extended_dn_out_search(module, req, extended_callback_fds);
860 }
861
862 static int extended_dn_out_ldb_init(struct ldb_module *module)
863 {
864         int ret;
865
866         struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
867         struct dsdb_extended_dn_store_format *dn_format;
868
869         ldb_module_set_private(module, p);
870
871         if (!p) {
872                 return ldb_oom(ldb_module_get_ctx(module));
873         }
874
875         dn_format = talloc(p, struct dsdb_extended_dn_store_format);
876         if (!dn_format) {
877                 talloc_free(p);
878                 return ldb_oom(ldb_module_get_ctx(module));
879         }
880
881         dn_format->store_extended_dn_in_ldb = true;
882         ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
883         if (ret != LDB_SUCCESS) {
884                 talloc_free(p);
885                 return ret;
886         }
887
888         p->dereference = false;
889         p->normalise = false;
890
891         ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
892         if (ret != LDB_SUCCESS) {
893                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
894                         "extended_dn_out: Unable to register control with rootdse!\n");
895                 return ldb_operr(ldb_module_get_ctx(module));
896         }
897
898         return ldb_next_init(module);
899 }
900
901 static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
902 {
903         int ret;
904         struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
905         struct dsdb_extended_dn_store_format *dn_format;
906
907         ldb_module_set_private(module, p);
908
909         if (!p) {
910                 return ldb_module_oom(module);
911         }
912
913         dn_format = talloc(p, struct dsdb_extended_dn_store_format);
914         if (!dn_format) {
915                 talloc_free(p);
916                 return ldb_module_oom(module);
917         }
918
919         dn_format->store_extended_dn_in_ldb = false;
920
921         ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
922         if (ret != LDB_SUCCESS) {
923                 talloc_free(p);
924                 return ret;
925         }
926
927         p->dereference = true;
928
929         p->attrs = attrs;
930         /* At the moment, servers that need dereference also need the
931          * DN and attribute names to be normalised */
932         p->normalise = true;
933
934         ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
935         if (ret != LDB_SUCCESS) {
936                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
937                           "extended_dn_out: Unable to register control with rootdse!\n");
938                 return ldb_operr(ldb_module_get_ctx(module));
939         }
940
941         return ldb_next_init(module);
942 }
943
944 static int extended_dn_out_openldap_init(struct ldb_module *module)
945 {
946         static const char *attrs[] = {
947                 "entryUUID",
948                 "objectSid",
949                 NULL
950         };
951
952         return extended_dn_out_dereference_init(module, attrs);
953 }
954
955 static int extended_dn_out_fds_init(struct ldb_module *module)
956 {
957         static const char *attrs[] = {
958                 "nsUniqueId",
959                 "sambaSID",
960                 NULL
961         };
962
963         return extended_dn_out_dereference_init(module, attrs);
964 }
965
966 static const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
967         .name              = "extended_dn_out_ldb",
968         .search            = extended_dn_out_ldb_search,
969         .init_context      = extended_dn_out_ldb_init,
970 };
971
972 static const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
973         .name              = "extended_dn_out_openldap",
974         .search            = extended_dn_out_openldap_search,
975         .init_context      = extended_dn_out_openldap_init,
976 };
977
978 static const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
979         .name              = "extended_dn_out_fds",
980         .search            = extended_dn_out_fds_search,
981         .init_context      = extended_dn_out_fds_init,
982 };
983
984 /*
985   initialise the module
986  */
987 _PUBLIC_ int ldb_extended_dn_out_module_init(const char *version)
988 {
989         int ret;
990         LDB_MODULE_CHECK_VERSION(version);
991         ret = ldb_register_module(&ldb_extended_dn_out_ldb_module_ops);
992         if (ret != LDB_SUCCESS) {
993                 return ret;
994         }
995         ret = ldb_register_module(&ldb_extended_dn_out_openldap_module_ops);
996         if (ret != LDB_SUCCESS) {
997                 return ret;
998         }
999         ret = ldb_register_module(&ldb_extended_dn_out_fds_module_ops);
1000         if (ret != LDB_SUCCESS) {
1001                 return ret;
1002         }
1003         return LDB_SUCCESS;
1004 }