4 LDAP semantics mapping module
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 This module relies on ldb_map to do all the real work, but performs
25 some of the trivial mappings between AD semantics and that provided
26 by OpenLDAP and similar servers.
30 #include "ldb/include/ldb.h"
31 #include "ldb/include/ldb_private.h"
32 #include "ldb/include/ldb_errors.h"
33 #include "ldb/ldb_map/ldb_map.h"
35 #include "librpc/gen_ndr/ndr_misc.h"
36 #include "librpc/ndr/libndr.h"
38 struct entryuuid_private {
39 struct ldb_dn **base_dns;
42 static struct ldb_val encode_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
45 NTSTATUS status = GUID_from_string((char *)val->data, &guid);
46 enum ndr_err_code ndr_err;
47 struct ldb_val out = data_blob(NULL, 0);
49 if (!NT_STATUS_IS_OK(status)) {
52 ndr_err = ndr_push_struct_blob(&out, ctx, &guid,
53 (ndr_push_flags_fn_t)ndr_push_GUID);
54 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
61 static struct ldb_val guid_always_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
64 struct ldb_val out = data_blob(NULL, 0);
65 if (val->length >= 32 && val->data[val->length] == '\0') {
66 ldb_handler_copy(module->ldb, ctx, val, &out);
68 enum ndr_err_code ndr_err;
70 guid = talloc(ctx, struct GUID);
74 ndr_err = ndr_pull_struct_blob(val, guid, guid,
75 (ndr_pull_flags_fn_t)ndr_pull_GUID);
76 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
80 out = data_blob_string_const(GUID_string(ctx, guid));
86 static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
89 NTSTATUS status = NS_GUID_from_string((char *)val->data, &guid);
90 enum ndr_err_code ndr_err;
91 struct ldb_val out = data_blob(NULL, 0);
93 if (!NT_STATUS_IS_OK(status)) {
96 ndr_err = ndr_push_struct_blob(&out, ctx, &guid,
97 (ndr_push_flags_fn_t)ndr_push_GUID);
98 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
105 static struct ldb_val guid_ns_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
107 struct ldb_val out = data_blob(NULL, 0);
108 if (val->length >= 32 && val->data[val->length] == '\0') {
110 GUID_from_string((char *)val->data, &guid);
111 out = data_blob_string_const(NS_GUID_string(ctx, &guid));
113 enum ndr_err_code ndr_err;
115 guid_p = talloc(ctx, struct GUID);
116 if (guid_p == NULL) {
119 ndr_err = ndr_pull_struct_blob(val, guid_p, guid_p,
120 (ndr_pull_flags_fn_t)ndr_pull_GUID);
121 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
125 out = data_blob_string_const(NS_GUID_string(ctx, guid_p));
131 /* The backend holds binary sids, so just copy them back */
132 static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
134 struct ldb_val out = data_blob(NULL, 0);
135 ldb_handler_copy(module->ldb, ctx, val, &out);
140 /* Ensure we always convert sids into binary, so the backend doesn't have to know about both forms */
141 static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
143 struct ldb_val out = data_blob(NULL, 0);
144 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectSid");
146 if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
147 return data_blob(NULL, 0);
153 /* Ensure we always convert objectCategory into a DN */
154 static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
156 struct ldb_val out = data_blob(NULL, 0);
157 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectCategory");
159 if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
160 return data_blob(NULL, 0);
166 static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
168 long long int signed_ll = strtoll((const char *)val->data, NULL, 10);
169 if (signed_ll >= 0x80000000LL) {
172 uint32_t unsigned_int;
174 .unsigned_int = strtoul((const char *)val->data, NULL, 10)
177 struct ldb_val out = data_blob_string_const(talloc_asprintf(ctx, "%d", u.signed_int));
180 return val_copy(module, ctx, val);
183 static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
186 unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
187 time_t t = (usn >> 24);
188 out = data_blob_string_const(talloc_asprintf(ctx, "%s#%06x#00#000000", ldb_timestring(ctx, t), (unsigned int)(usn & 0xFFFFFF)));
192 static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val)
194 char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
197 unsigned long long usn;
202 p = strchr(entryCSN, '#');
217 usn = strtol(mod_per_sec, NULL, 16);
219 t = ldb_string_to_time(entryCSN);
221 usn = usn | ((unsigned long long)t <<24);
225 static struct ldb_val entryCSN_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
228 unsigned long long usn = entryCSN_to_usn_int(ctx, val);
229 out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
233 static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
236 unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
237 time_t t = (usn >> 24);
238 out = data_blob_string_const(ldb_timestring(ctx, t));
242 static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
246 unsigned long long usn;
248 t = ldb_string_to_time((const char *)val->data);
250 usn = ((unsigned long long)t <<24);
252 out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
257 static const struct ldb_map_attribute entryuuid_attributes[] =
261 .local_name = "objectGUID",
265 .remote_name = "entryUUID",
266 .convert_local = guid_always_string,
267 .convert_remote = encode_guid,
273 .local_name = "invocationId",
277 .remote_name = "invocationId",
278 .convert_local = guid_always_string,
279 .convert_remote = encode_guid,
285 .local_name = "objectSid",
289 .remote_name = "objectSid",
290 .convert_local = sid_always_binary,
291 .convert_remote = val_copy,
296 .local_name = "name",
300 .remote_name = "samba4RDN"
305 .local_name = "whenCreated",
309 .remote_name = "createTimestamp"
314 .local_name = "whenChanged",
318 .remote_name = "modifyTimestamp"
323 .local_name = "objectClasses",
327 .remote_name = "samba4ObjectClasses"
332 .local_name = "dITContentRules",
336 .remote_name = "samba4DITContentRules"
341 .local_name = "attributeTypes",
345 .remote_name = "samba4AttributeTypes"
350 .local_name = "sambaPassword",
354 .remote_name = "userPassword"
359 .local_name = "objectCategory",
363 .remote_name = "objectCategory",
364 .convert_local = objectCategory_always_dn,
365 .convert_remote = val_copy,
370 .local_name = "distinguishedName",
374 .remote_name = "entryDN"
379 .local_name = "groupType",
383 .remote_name = "groupType",
384 .convert_local = normalise_to_signed32,
385 .convert_remote = val_copy,
390 .local_name = "sAMAccountType",
394 .remote_name = "sAMAccountType",
395 .convert_local = normalise_to_signed32,
396 .convert_remote = val_copy,
401 .local_name = "usnChanged",
405 .remote_name = "entryCSN",
406 .convert_local = usn_to_entryCSN,
407 .convert_remote = entryCSN_to_usn
412 .local_name = "usnCreated",
416 .remote_name = "createTimestamp",
417 .convert_local = usn_to_timestamp,
418 .convert_remote = timestamp_to_usn,
431 /* This objectClass conflicts with builtin classes on OpenLDAP */
432 const struct ldb_map_objectclass entryuuid_objectclasses[] =
435 .local_name = "subSchema",
436 .remote_name = "samba4SubSchema"
443 /* These things do not show up in wildcard searches in OpenLDAP, but
444 * we need them to show up in the AD-like view */
445 static const char * const entryuuid_wildcard_attributes[] = {
454 static const struct ldb_map_attribute nsuniqueid_attributes[] =
458 .local_name = "objectGUID",
462 .remote_name = "nsuniqueid",
463 .convert_local = guid_ns_string,
464 .convert_remote = encode_ns_guid,
470 .local_name = "objectSid",
474 .remote_name = "objectSid",
475 .convert_local = sid_always_binary,
476 .convert_remote = val_copy,
481 .local_name = "whenCreated",
485 .remote_name = "createTimestamp"
490 .local_name = "whenChanged",
494 .remote_name = "modifyTimestamp"
499 .local_name = "sambaPassword",
503 .remote_name = "userPassword"
508 .local_name = "objectCategory",
512 .remote_name = "objectCategory",
513 .convert_local = objectCategory_always_dn,
514 .convert_remote = val_copy,
519 .local_name = "distinguishedName",
523 .remote_name = "entryDN"
528 .local_name = "groupType",
532 .remote_name = "groupType",
533 .convert_local = normalise_to_signed32,
534 .convert_remote = val_copy,
539 .local_name = "sAMAccountType",
543 .remote_name = "sAMAccountType",
544 .convert_local = normalise_to_signed32,
545 .convert_remote = val_copy,
550 .local_name = "usnChanged",
554 .remote_name = "modifyTimestamp",
555 .convert_local = usn_to_timestamp,
556 .convert_remote = timestamp_to_usn,
561 .local_name = "usnCreated",
565 .remote_name = "createTimestamp",
566 .convert_local = usn_to_timestamp,
567 .convert_remote = timestamp_to_usn,
580 /* These things do not show up in wildcard searches in OpenLDAP, but
581 * we need them to show up in the AD-like view */
582 static const char * const nsuniqueid_wildcard_attributes[] = {
591 static int get_remote_rootdse(struct ldb_context *ldb, void *context,
592 struct ldb_reply *ares)
594 struct entryuuid_private *entryuuid_private;
595 entryuuid_private = talloc_get_type(context,
596 struct entryuuid_private);
597 if (ares->type == LDB_REPLY_ENTRY) {
599 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "namingContexts");
600 entryuuid_private->base_dns = talloc_realloc(entryuuid_private, entryuuid_private->base_dns, struct ldb_dn *,
602 for (i=0; i < el->num_values; i++) {
603 if (!entryuuid_private->base_dns) {
604 return LDB_ERR_OPERATIONS_ERROR;
606 entryuuid_private->base_dns[i] = ldb_dn_new(entryuuid_private->base_dns, ldb, (const char *)el->values[i].data);
607 if ( ! ldb_dn_validate(entryuuid_private->base_dns[i])) {
608 return LDB_ERR_OPERATIONS_ERROR;
611 entryuuid_private->base_dns[i] = NULL;
617 static int find_base_dns(struct ldb_module *module,
618 struct entryuuid_private *entryuuid_private)
621 struct ldb_request *req;
622 const char *naming_context_attr[] = {
626 req = talloc(entryuuid_private, struct ldb_request);
628 ldb_set_errstring(module->ldb, "Out of Memory");
629 return LDB_ERR_OPERATIONS_ERROR;
632 req->operation = LDB_SEARCH;
633 req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
634 req->op.search.scope = LDB_SCOPE_BASE;
636 req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
637 if (req->op.search.tree == NULL) {
638 ldb_set_errstring(module->ldb, "Unable to parse search expression");
640 return LDB_ERR_OPERATIONS_ERROR;
643 req->op.search.attrs = naming_context_attr;
644 req->controls = NULL;
645 req->context = entryuuid_private;
646 req->callback = get_remote_rootdse;
647 ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
649 ret = ldb_next_request(module, req);
651 if (ret == LDB_SUCCESS) {
652 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
656 if (ret != LDB_SUCCESS) {
663 /* the context init function */
664 static int entryuuid_init(struct ldb_module *module)
667 struct map_private *map_private;
668 struct entryuuid_private *entryuuid_private;
670 ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, NULL);
671 if (ret != LDB_SUCCESS)
674 map_private = talloc_get_type(module->private_data, struct map_private);
676 entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
677 map_private->caller_private = entryuuid_private;
679 ret = find_base_dns(module, entryuuid_private);
681 return ldb_next_init(module);
684 /* the context init function */
685 static int nsuniqueid_init(struct ldb_module *module)
688 struct map_private *map_private;
689 struct entryuuid_private *entryuuid_private;
691 ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, NULL);
692 if (ret != LDB_SUCCESS)
695 map_private = talloc_get_type(module->private_data, struct map_private);
697 entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
698 map_private->caller_private = entryuuid_private;
700 ret = find_base_dns(module, entryuuid_private);
702 return ldb_next_init(module);
705 static int get_seq(struct ldb_context *ldb, void *context,
706 struct ldb_reply *ares)
708 unsigned long long *max_seq = (unsigned long long *)context;
709 unsigned long long seq;
710 if (ares->type == LDB_REPLY_ENTRY) {
711 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
713 seq = entryCSN_to_usn_int(ares, &el->values[0]);
714 *max_seq = MAX(seq, *max_seq);
721 static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
724 struct map_private *map_private;
725 struct entryuuid_private *entryuuid_private;
726 unsigned long long max_seq = 0;
727 struct ldb_request *search_req;
728 map_private = talloc_get_type(module->private_data, struct map_private);
730 entryuuid_private = talloc_get_type(map_private->caller_private, struct entryuuid_private);
732 /* Search the baseDNs for a sequence number */
733 for (i=0; entryuuid_private &&
734 entryuuid_private->base_dns &&
735 entryuuid_private->base_dns[i];
737 static const char *contextCSN_attr[] = {
740 search_req = talloc(req, struct ldb_request);
741 if (search_req == NULL) {
742 ldb_set_errstring(module->ldb, "Out of Memory");
743 return LDB_ERR_OPERATIONS_ERROR;
746 search_req->operation = LDB_SEARCH;
747 search_req->op.search.base = entryuuid_private->base_dns[i];
748 search_req->op.search.scope = LDB_SCOPE_BASE;
750 search_req->op.search.tree = ldb_parse_tree(search_req, "objectClass=*");
751 if (search_req->op.search.tree == NULL) {
752 ldb_set_errstring(module->ldb, "Unable to parse search expression");
753 talloc_free(search_req);
754 return LDB_ERR_OPERATIONS_ERROR;
757 search_req->op.search.attrs = contextCSN_attr;
758 search_req->controls = NULL;
759 search_req->context = &max_seq;
760 search_req->callback = get_seq;
761 ldb_set_timeout(module->ldb, search_req, 0); /* use default timeout */
763 ret = ldb_next_request(module, search_req);
765 if (ret == LDB_SUCCESS) {
766 ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
769 talloc_free(search_req);
770 if (ret != LDB_SUCCESS) {
775 switch (req->op.seq_num.type) {
776 case LDB_SEQ_HIGHEST_SEQ:
777 req->op.seq_num.seq_num = max_seq;
780 req->op.seq_num.seq_num = max_seq;
781 req->op.seq_num.seq_num++;
783 case LDB_SEQ_HIGHEST_TIMESTAMP:
785 req->op.seq_num.seq_num = (max_seq >> 24);
789 req->op.seq_num.flags = 0;
790 req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
791 req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
795 static struct ldb_module_ops entryuuid_ops = {
797 .init_context = entryuuid_init,
798 .sequence_number = entryuuid_sequence_number
801 static struct ldb_module_ops nsuniqueid_ops = {
802 .name = "nsuniqueid",
803 .init_context = nsuniqueid_init,
804 .sequence_number = entryuuid_sequence_number
807 /* the init function */
808 int ldb_simple_ldap_map_module_init(void)
811 struct ldb_module_ops ops = ldb_map_get_ops();
812 entryuuid_ops.add = ops.add;
813 entryuuid_ops.modify = ops.modify;
814 entryuuid_ops.del = ops.del;
815 entryuuid_ops.rename = ops.rename;
816 entryuuid_ops.search = ops.search;
817 entryuuid_ops.wait = ops.wait;
818 ret = ldb_register_module(&entryuuid_ops);
824 nsuniqueid_ops.add = ops.add;
825 nsuniqueid_ops.modify = ops.modify;
826 nsuniqueid_ops.del = ops.del;
827 nsuniqueid_ops.rename = ops.rename;
828 nsuniqueid_ops.search = ops.search;
829 nsuniqueid_ops.wait = ops.wait;
830 ret = ldb_register_module(&nsuniqueid_ops);