4 Copyright (C) Simo Sorce 2005-2008
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2008
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.
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.
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/>.
24 * Component: ldb extended dn control module
26 * Description: this module builds a special dn for returned search
27 * results, and fixes some other aspects of the result (returned case issues)
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
37 #include "ldb/include/ldb_module.h"
38 #include "libcli/security/dom_sid.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "librpc/ndr/libndr.h"
42 #include "dsdb/samdb/samdb.h"
44 struct extended_dn_out_private {
47 struct dsdb_openldap_dereference_control *dereference_control;
50 static bool is_attr_in_list(const char * const * attrs, const char *attr)
54 for (i = 0; attrs[i]; i++) {
55 if (ldb_attr_cmp(attrs[i], attr) == 0)
62 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
67 for (num = 0; attrs[num]; num++);
69 nattrs = talloc_array(mem_ctx, char *, num + 1);
70 if (!nattrs) return NULL;
72 for(i = 0; i < num; i++) {
73 nattrs[i] = talloc_strdup(nattrs, attrs[i]);
84 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
89 for (num = 0; (*attrs)[num]; num++);
91 nattrs = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
92 if (!nattrs) return false;
96 nattrs[num] = talloc_strdup(nattrs, attr);
97 if (!nattrs[num]) return false;
99 nattrs[num + 1] = NULL;
104 /* Fix the DN so that the relative attribute names are in upper case so that the DN:
105 cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
106 CN=Adminstrator,CN=users,DC=samba,DC=example,DC=com
110 static int fix_dn(struct ldb_dn *dn)
113 char *upper_rdn_attr;
115 for (i=0; i < ldb_dn_get_comp_num(dn); i++) {
116 /* We need the attribute name in upper case */
117 upper_rdn_attr = strupper_talloc(dn,
118 ldb_dn_get_component_name(dn, i));
119 if (!upper_rdn_attr) {
120 return LDB_ERR_OPERATIONS_ERROR;
123 /* And replace it with CN=foo (we need the attribute in upper case */
124 ret = ldb_dn_set_component(dn, i, upper_rdn_attr,
125 *ldb_dn_get_component_val(dn, i));
126 talloc_free(upper_rdn_attr);
127 if (ret != LDB_SUCCESS) {
134 /* Inject the extended DN components, so the DN cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
135 <GUID=541203ae-f7d6-47ef-8390-bfcf019f9583>;<SID=S-1-5-21-4177067393-1453636373-93818737-500>;cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com */
137 static int inject_extended_dn_out(struct ldb_reply *ares,
138 struct ldb_context *ldb,
144 const DATA_BLOB *guid_blob;
145 const DATA_BLOB *sid_blob;
147 guid_blob = ldb_msg_find_ldb_val(ares->message, "objectGUID");
148 sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSID");
151 ldb_set_errstring(ldb, "Did not find objectGUID to inject into extended DN");
152 return LDB_ERR_OPERATIONS_ERROR;
155 ret = ldb_dn_set_extended_component(ares->message->dn, "GUID", guid_blob);
156 if (ret != LDB_SUCCESS) {
160 ret = ldb_dn_set_extended_component(ares->message->dn, "SID", sid_blob);
161 if (ret != LDB_SUCCESS) {
167 ldb_msg_remove_attr(ares->message, "objectGUID");
170 if (sid_blob && remove_sid) {
171 ldb_msg_remove_attr(ares->message, "objectSID");
177 static int handle_dereference_openldap(struct ldb_dn *dn,
178 struct dsdb_openldap_dereference_result **dereference_attrs,
179 const char *attr, const DATA_BLOB *val)
181 const struct ldb_val *entryUUIDblob, *sid_blob;
182 struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
185 fake_msg.num_elements = 0;
187 /* Look for this attribute in the returned control */
188 for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
189 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
190 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
191 && data_blob_cmp(&source_dn, val) == 0) {
192 fake_msg.num_elements = dereference_attrs[j]->num_attributes;
193 fake_msg.elements = dereference_attrs[j]->attributes;
197 if (!fake_msg.num_elements) {
200 /* Look for an OpenLDAP entryUUID */
202 entryUUIDblob = ldb_msg_find_ldb_val(&fake_msg, "entryUUID");
205 enum ndr_err_code ndr_err;
207 struct ldb_val guid_blob;
210 status = GUID_from_data_blob(entryUUIDblob, &guid);
212 if (!NT_STATUS_IS_OK(status)) {
213 return LDB_ERR_INVALID_DN_SYNTAX;
215 ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
216 (ndr_push_flags_fn_t)ndr_push_GUID);
217 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
218 return LDB_ERR_INVALID_DN_SYNTAX;
221 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
224 sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSID");
226 /* Look for the objectSID */
228 ldb_dn_set_extended_component(dn, "SID", sid_blob);
233 static int handle_dereference_fds(struct ldb_dn *dn,
234 struct dsdb_openldap_dereference_result **dereference_attrs,
235 const char *attr, const DATA_BLOB *val)
237 const struct ldb_val *nsUniqueIdBlob, *sidBlob;
238 struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
241 fake_msg.num_elements = 0;
243 /* Look for this attribute in the returned control */
244 for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
245 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
246 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
247 && data_blob_cmp(&source_dn, val) == 0) {
248 fake_msg.num_elements = dereference_attrs[j]->num_attributes;
249 fake_msg.elements = dereference_attrs[j]->attributes;
253 if (!fake_msg.num_elements) {
257 /* Look for the nsUniqueId */
259 nsUniqueIdBlob = ldb_msg_find_ldb_val(&fake_msg, "nsUniqueId");
260 if (nsUniqueIdBlob) {
262 enum ndr_err_code ndr_err;
264 struct ldb_val guid_blob;
267 status = NS_GUID_from_string((char *)nsUniqueIdBlob->data, &guid);
269 if (!NT_STATUS_IS_OK(status)) {
270 return LDB_ERR_INVALID_DN_SYNTAX;
272 ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
273 (ndr_push_flags_fn_t)ndr_push_GUID);
274 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
275 return LDB_ERR_INVALID_DN_SYNTAX;
278 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
281 /* Look for the objectSID */
283 sidBlob = ldb_msg_find_ldb_val(&fake_msg, "sambaSID");
285 enum ndr_err_code ndr_err;
287 struct ldb_val sid_blob;
290 sid = dom_sid_parse_length(NULL, sidBlob);
293 return LDB_ERR_INVALID_DN_SYNTAX;
296 ndr_err = ndr_push_struct_blob(&sid_blob, NULL, NULL, sid,
297 (ndr_push_flags_fn_t)ndr_push_dom_sid);
299 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
300 return LDB_ERR_INVALID_DN_SYNTAX;
303 ldb_dn_set_extended_component(dn, "SID", &sid_blob);
309 struct extended_search_context {
310 struct ldb_module *module;
311 const struct dsdb_schema *schema;
312 struct ldb_request *req;
319 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
320 int (*handle_dereference)(struct ldb_dn *dn,
321 struct dsdb_openldap_dereference_result **dereference_attrs,
322 const char *attr, const DATA_BLOB *val))
324 struct extended_search_context *ac;
325 struct ldb_control *control;
326 struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
328 struct ldb_message *msg = ares->message;
329 struct extended_dn_out_private *p;
331 ac = talloc_get_type(req->context, struct extended_search_context);
332 p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
335 return ldb_module_done(ac->req, NULL, NULL,
336 LDB_ERR_OPERATIONS_ERROR);
338 if (ares->error != LDB_SUCCESS) {
339 return ldb_module_done(ac->req, ares->controls,
340 ares->response, ares->error);
343 switch (ares->type) {
344 case LDB_REPLY_REFERRAL:
345 return ldb_module_send_referral(ac->req, ares->referral);
348 return ldb_module_done(ac->req, ares->controls,
349 ares->response, LDB_SUCCESS);
350 case LDB_REPLY_ENTRY:
354 if (p && p->normalise) {
355 ret = fix_dn(ares->message->dn);
356 if (ret != LDB_SUCCESS) {
357 return ldb_module_done(ac->req, NULL, NULL, ret);
362 /* for each record returned post-process to add any derived
363 attributes that have been asked for */
364 ret = inject_extended_dn_out(ares, ldb_module_get_ctx(ac->module),
365 ac->extended_type, ac->remove_guid,
367 if (ret != LDB_SUCCESS) {
368 return ldb_module_done(ac->req, NULL, NULL, ret);
372 if ((p && p->normalise) || ac->inject) {
373 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
375 ldb_msg_remove_attr(ares->message, "distinguishedName");
377 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName",
378 ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
380 ret = ldb_msg_add_string(ares->message, "distinguishedName",
381 ldb_dn_get_linearized(ares->message->dn));
383 if (ret != LDB_SUCCESS) {
384 ldb_oom(ldb_module_get_ctx(ac->module));
385 return LDB_ERR_OPERATIONS_ERROR;
390 if (p && p->dereference) {
391 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
393 if (control && control->data) {
394 dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
398 /* Walk the retruned elements (but only if we have a schema to interpret the list with) */
399 for (i = 0; ac->schema && i < msg->num_elements; i++) {
400 const struct dsdb_attribute *attribute;
401 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
407 /* If we are also in 'normalise' mode, then
408 * fix the attribute names to be in the
410 msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
411 if (!msg->elements[i].name) {
412 ldb_oom(ldb_module_get_ctx(ac->module));
413 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
417 /* distinguishedName has been dealt with above */
418 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
422 /* Look to see if this attributeSyntax is a DN */
423 if (strcmp(attribute->attributeSyntax_oid, "2.5.5.1") != 0 &&
424 strcmp(attribute->attributeSyntax_oid, "2.5.5.7") != 0) {
428 for (j = 0; j < msg->elements[i].num_values; j++) {
430 struct ldb_dn *dn = ldb_dn_from_ldb_val(ac, ldb_module_get_ctx(ac->module), &msg->elements[i].values[j]);
431 if (!dn || !ldb_dn_validate(dn)) {
432 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
437 if (ret != LDB_SUCCESS) {
438 return ldb_module_done(ac->req, NULL, NULL, ret);
442 /* If we are running in dereference mode (such
443 * as against OpenLDAP) then the DN in the msg
444 * above does not contain the extended values,
445 * and we need to look in the dereference
448 /* Look for this value in the attribute */
450 if (dereference_control) {
451 ret = handle_dereference(dn,
452 dereference_control->attributes,
453 msg->elements[i].name,
454 &msg->elements[i].values[j]);
455 if (ret != LDB_SUCCESS) {
456 return ldb_module_done(ac->req, NULL, NULL, ret);
461 dn_str = talloc_steal(msg->elements[i].values,
462 ldb_dn_get_linearized(dn));
464 dn_str = talloc_steal(msg->elements[i].values,
465 ldb_dn_get_extended_linearized(msg->elements[i].values,
466 dn, ac->extended_type));
469 ldb_oom(ldb_module_get_ctx(ac->module));
470 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
472 msg->elements[i].values[j] = data_blob_string_const(dn_str);
476 return ldb_module_send_entry(ac->req, msg, ares->controls);
479 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
481 return extended_callback(req, ares, handle_dereference_openldap);
484 static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
486 return extended_callback(req, ares, handle_dereference_fds);
489 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
490 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
492 struct ldb_control *control;
493 struct ldb_control *storage_format_control;
494 struct ldb_extended_dn_control *extended_ctrl = NULL;
495 struct ldb_control **saved_controls;
496 struct extended_search_context *ac;
497 struct ldb_request *down_req;
499 const char * const *const_attrs;
502 struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
504 /* check if there's an extended dn control */
505 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
506 if (control && control->data) {
507 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
508 if (!extended_ctrl) {
509 return LDB_ERR_PROTOCOL_ERROR;
513 /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
514 * client is after the storage format (to fill in linked
516 storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
517 if (!control && storage_format_control && storage_format_control->data) {
518 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
519 if (!extended_ctrl) {
520 ldb_set_errstring(ldb_module_get_ctx(module), "extended_dn_out: extended_ctrl was of the wrong data type");
521 return LDB_ERR_PROTOCOL_ERROR;
525 ac = talloc_zero(req, struct extended_search_context);
527 ldb_oom(ldb_module_get_ctx(module));
528 return LDB_ERR_OPERATIONS_ERROR;
532 ac->schema = dsdb_get_schema(ldb_module_get_ctx(module));
535 ac->remove_guid = false;
536 ac->remove_sid = false;
538 const_attrs = req->op.search.attrs;
540 /* We only need to do special processing if we were asked for
541 * the extended DN, or we are 'store DN+GUID+SID'
542 * (!dereference) mode. (This is the normal mode for LDB on
544 if (control || (storage_format_control && p && !p->dereference)) {
547 ac->extended_type = extended_ctrl->type;
549 ac->extended_type = 0;
552 /* check if attrs only is specified, in that case check wether we need to modify them */
553 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
554 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
555 ac->remove_guid = true;
557 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
558 ac->remove_sid = true;
560 if (ac->remove_guid || ac->remove_sid) {
561 new_attrs = copy_attrs(ac, req->op.search.attrs);
562 if (new_attrs == NULL) {
563 ldb_oom(ldb_module_get_ctx(module));
564 return LDB_ERR_OPERATIONS_ERROR;
567 if (ac->remove_guid) {
568 if (!add_attrs(ac, &new_attrs, "objectGUID"))
569 return LDB_ERR_OPERATIONS_ERROR;
571 if (ac->remove_sid) {
572 if (!add_attrs(ac, &new_attrs, "objectSID"))
573 return LDB_ERR_OPERATIONS_ERROR;
575 const_attrs = (const char * const *)new_attrs;
580 ret = ldb_build_search_req_ex(&down_req,
581 ldb_module_get_ctx(module), ac,
583 req->op.search.scope,
589 if (ret != LDB_SUCCESS) {
593 /* Remove extended DN and storage format controls */
596 /* save it locally and remove it from the list */
597 /* we do not need to replace them later as we
598 * are keeping the original req intact */
599 if (!save_controls(control, down_req, &saved_controls)) {
600 return LDB_ERR_OPERATIONS_ERROR;
604 if (storage_format_control) {
605 /* save it locally and remove it from the list */
606 /* we do not need to replace them later as we
607 * are keeping the original req intact */
608 if (!save_controls(storage_format_control, down_req, &saved_controls)) {
609 return LDB_ERR_OPERATIONS_ERROR;
613 /* Add in dereference control, if we were asked to, we are
614 * using the 'dereference' mode (such as with an OpenLDAP
615 * backend) and have the control prepared */
616 if (control && p && p->dereference && p->dereference_control) {
617 ret = ldb_request_add_control(down_req,
618 DSDB_OPENLDAP_DEREFERENCE_CONTROL,
619 false, p->dereference_control);
620 if (ret != LDB_SUCCESS) {
625 /* perform the search */
626 return ldb_next_request(module, down_req);
629 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
631 return extended_dn_out_search(module, req, extended_callback_openldap);
634 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
636 return extended_dn_out_search(module, req, extended_callback_openldap);
639 static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
641 return extended_dn_out_search(module, req, extended_callback_fds);
644 static int extended_dn_out_ldb_init(struct ldb_module *module)
648 struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
650 ldb_module_set_private(module, p);
653 ldb_oom(ldb_module_get_ctx(module));
654 return LDB_ERR_OPERATIONS_ERROR;
657 p->dereference = false;
658 p->normalise = false;
660 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
661 if (ret != LDB_SUCCESS) {
662 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
663 "extended_dn_out: Unable to register control with rootdse!\n");
664 return LDB_ERR_OPERATIONS_ERROR;
667 return ldb_next_init(module);
670 static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
673 struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
674 struct dsdb_openldap_dereference_control *dereference_control;
675 struct dsdb_attribute *cur;
677 struct dsdb_schema *schema;
679 ldb_module_set_private(module, p);
682 ldb_oom(ldb_module_get_ctx(module));
683 return LDB_ERR_OPERATIONS_ERROR;
686 p->dereference = true;
688 /* At the moment, servers that need dereference also need the
689 * DN and attribute names to be normalised */
692 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
693 if (ret != LDB_SUCCESS) {
694 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
695 "extended_dn_out: Unable to register control with rootdse!\n");
696 return LDB_ERR_OPERATIONS_ERROR;
699 ret = ldb_next_init(module);
701 if (ret != LDB_SUCCESS) {
705 schema = dsdb_get_schema(ldb_module_get_ctx(module));
707 /* No schema on this DB (yet) */
711 p->dereference_control = dereference_control
712 = talloc_zero(p, struct dsdb_openldap_dereference_control);
714 if (!p->dereference_control) {
715 ldb_oom(ldb_module_get_ctx(module));
716 return LDB_ERR_OPERATIONS_ERROR;
719 for (cur = schema->attributes; cur; cur = cur->next) {
720 if (strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.1") != 0 &&
721 strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.7") != 0) {
724 dereference_control->dereference
725 = talloc_realloc(p, dereference_control->dereference,
726 struct dsdb_openldap_dereference *, i + 2);
727 if (!dereference_control) {
728 ldb_oom(ldb_module_get_ctx(module));
729 return LDB_ERR_OPERATIONS_ERROR;
731 dereference_control->dereference[i] = talloc(dereference_control->dereference,
732 struct dsdb_openldap_dereference);
733 if (!dereference_control->dereference[i]) {
734 ldb_oom(ldb_module_get_ctx(module));
735 return LDB_ERR_OPERATIONS_ERROR;
737 dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
738 dereference_control->dereference[i]->dereference_attribute = attrs;
740 dereference_control->dereference[i] = NULL;
745 static int extended_dn_out_openldap_init(struct ldb_module *module)
747 static const char *attrs[] = {
753 return extended_dn_out_dereference_init(module, attrs);
756 static int extended_dn_out_fds_init(struct ldb_module *module)
758 static const char *attrs[] = {
764 return extended_dn_out_dereference_init(module, attrs);
767 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
768 .name = "extended_dn_out_ldb",
769 .search = extended_dn_out_ldb_search,
770 .init_context = extended_dn_out_ldb_init,
773 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
774 .name = "extended_dn_out_openldap",
775 .search = extended_dn_out_openldap_search,
776 .init_context = extended_dn_out_openldap_init,
779 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
780 .name = "extended_dn_out_fds",
781 .search = extended_dn_out_fds_search,
782 .init_context = extended_dn_out_fds_init,