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 struct ldb_val out = data_blob(NULL, 0);
48 if (!NT_STATUS_IS_OK(status)) {
51 status = ndr_push_struct_blob(&out, ctx, &guid,
52 (ndr_push_flags_fn_t)ndr_push_GUID);
53 if (!NT_STATUS_IS_OK(status)) {
60 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 guid = talloc(ctx, struct GUID);
72 status = ndr_pull_struct_blob(val, guid, guid,
73 (ndr_pull_flags_fn_t)ndr_pull_GUID);
74 if (!NT_STATUS_IS_OK(status)) {
78 out = data_blob_string_const(GUID_string(ctx, guid));
84 static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
87 NTSTATUS status = NS_GUID_from_string((char *)val->data, &guid);
88 struct ldb_val out = data_blob(NULL, 0);
90 if (!NT_STATUS_IS_OK(status)) {
93 status = ndr_push_struct_blob(&out, ctx, &guid,
94 (ndr_push_flags_fn_t)ndr_push_GUID);
95 if (!NT_STATUS_IS_OK(status)) {
102 static struct ldb_val guid_ns_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
105 struct ldb_val out = data_blob(NULL, 0);
106 if (val->length >= 32 && val->data[val->length] == '\0') {
108 GUID_from_string((char *)val->data, &guid);
109 out = data_blob_string_const(NS_GUID_string(ctx, &guid));
112 guid_p = talloc(ctx, struct GUID);
113 if (guid_p == NULL) {
116 status = ndr_pull_struct_blob(val, guid_p, guid_p,
117 (ndr_pull_flags_fn_t)ndr_pull_GUID);
118 if (!NT_STATUS_IS_OK(status)) {
122 out = data_blob_string_const(NS_GUID_string(ctx, guid_p));
128 /* The backend holds binary sids, so just copy them back */
129 static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
131 struct ldb_val out = data_blob(NULL, 0);
132 ldb_handler_copy(module->ldb, ctx, val, &out);
137 /* Ensure we always convert sids into binary, so the backend doesn't have to know about both forms */
138 static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
140 struct ldb_val out = data_blob(NULL, 0);
141 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectSid");
143 if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
144 return data_blob(NULL, 0);
150 /* Ensure we always convert objectCategory into a DN */
151 static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
153 struct ldb_val out = data_blob(NULL, 0);
154 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectCategory");
156 if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
157 return data_blob(NULL, 0);
163 static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
165 long long int signed_ll = strtoll((const char *)val->data, NULL, 10);
166 if (signed_ll >= 0x80000000LL) {
169 uint32_t unsigned_int;
171 .unsigned_int = strtoul((const char *)val->data, NULL, 10)
174 struct ldb_val out = data_blob_string_const(talloc_asprintf(ctx, "%d", u.signed_int));
177 return val_copy(module, ctx, val);
180 static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
183 unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
184 time_t t = (usn >> 24);
185 out = data_blob_string_const(talloc_asprintf(ctx, "%s#%06x#00#000000", ldb_timestring(ctx, t), (unsigned int)(usn & 0xFFFFFF)));
189 static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val)
191 char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
194 unsigned long long usn;
199 p = strchr(entryCSN, '#');
214 usn = strtol(mod_per_sec, NULL, 16);
216 t = ldb_string_to_time(entryCSN);
218 usn = usn | ((unsigned long long)t <<24);
222 static struct ldb_val entryCSN_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
225 unsigned long long usn = entryCSN_to_usn_int(ctx, val);
226 out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
230 static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
233 unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
234 time_t t = (usn >> 24);
235 out = data_blob_string_const(ldb_timestring(ctx, t));
239 static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
243 unsigned long long usn;
245 t = ldb_string_to_time((const char *)val->data);
247 usn = ((unsigned long long)t <<24);
249 out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
254 static const struct ldb_map_attribute entryUUID_attributes[] =
258 .local_name = "objectGUID",
262 .remote_name = "entryUUID",
263 .convert_local = guid_always_string,
264 .convert_remote = encode_guid,
270 .local_name = "invocationId",
274 .remote_name = "invocationId",
275 .convert_local = guid_always_string,
276 .convert_remote = encode_guid,
282 .local_name = "objectSid",
286 .remote_name = "objectSid",
287 .convert_local = sid_always_binary,
288 .convert_remote = val_copy,
293 .local_name = "whenCreated",
297 .remote_name = "createTimestamp"
302 .local_name = "whenChanged",
306 .remote_name = "modifyTimestamp"
311 .local_name = "objectClasses",
315 .remote_name = "samba4ObjectClasses"
320 .local_name = "dITContentRules",
324 .remote_name = "samba4DITContentRules"
329 .local_name = "attributeTypes",
333 .remote_name = "samba4AttributeTypes"
338 .local_name = "sambaPassword",
342 .remote_name = "userPassword"
347 .local_name = "objectCategory",
351 .remote_name = "objectCategory",
352 .convert_local = objectCategory_always_dn,
353 .convert_remote = val_copy,
358 .local_name = "distinguishedName",
362 .remote_name = "entryDN"
367 .local_name = "groupType",
371 .remote_name = "groupType",
372 .convert_local = normalise_to_signed32,
373 .convert_remote = val_copy,
378 .local_name = "sAMAccountType",
382 .remote_name = "sAMAccountType",
383 .convert_local = normalise_to_signed32,
384 .convert_remote = val_copy,
389 .local_name = "usnChanged",
393 .remote_name = "entryCSN",
394 .convert_local = usn_to_entryCSN,
395 .convert_remote = entryCSN_to_usn
400 .local_name = "usnCreated",
404 .remote_name = "createTimestamp",
405 .convert_local = usn_to_timestamp,
406 .convert_remote = timestamp_to_usn,
419 /* This objectClass conflicts with builtin classes on OpenLDAP */
420 const struct ldb_map_objectclass entryUUID_objectclasses[] =
423 .local_name = "subSchema",
424 .remote_name = "samba4SubSchema"
431 /* These things do not show up in wildcard searches in OpenLDAP, but
432 * we need them to show up in the AD-like view */
433 static const char * const entryUUID_wildcard_attributes[] = {
442 static const struct ldb_map_attribute nsuniqueid_attributes[] =
446 .local_name = "objectGUID",
450 .remote_name = "nsuniqueid",
451 .convert_local = guid_ns_string,
452 .convert_remote = encode_ns_guid,
458 .local_name = "objectSid",
462 .remote_name = "objectSid",
463 .convert_local = sid_always_binary,
464 .convert_remote = val_copy,
469 .local_name = "whenCreated",
473 .remote_name = "createTimestamp"
478 .local_name = "whenChanged",
482 .remote_name = "modifyTimestamp"
487 .local_name = "sambaPassword",
491 .remote_name = "userPassword"
496 .local_name = "objectCategory",
500 .remote_name = "objectCategory",
501 .convert_local = objectCategory_always_dn,
502 .convert_remote = val_copy,
507 .local_name = "distinguishedName",
511 .remote_name = "entryDN"
516 .local_name = "groupType",
520 .remote_name = "groupType",
521 .convert_local = normalise_to_signed32,
522 .convert_remote = val_copy,
527 .local_name = "sAMAccountType",
531 .remote_name = "sAMAccountType",
532 .convert_local = normalise_to_signed32,
533 .convert_remote = val_copy,
538 .local_name = "usnChanged",
542 .remote_name = "modifyTimestamp",
543 .convert_local = usn_to_timestamp,
544 .convert_remote = timestamp_to_usn,
549 .local_name = "usnCreated",
553 .remote_name = "createTimestamp",
554 .convert_local = usn_to_timestamp,
555 .convert_remote = timestamp_to_usn,
568 /* These things do not show up in wildcard searches in OpenLDAP, but
569 * we need them to show up in the AD-like view */
570 static const char * const nsuniqueid_wildcard_attributes[] = {
579 static int get_remote_rootdse(struct ldb_context *ldb, void *context,
580 struct ldb_reply *ares)
582 struct entryUUID_private *entryUUID_private;
583 entryUUID_private = talloc_get_type(context,
584 struct entryUUID_private);
585 if (ares->type == LDB_REPLY_ENTRY) {
587 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "namingContexts");
588 entryUUID_private->base_dns = talloc_realloc(entryUUID_private, entryUUID_private->base_dns, struct ldb_dn *,
590 for (i=0; i < el->num_values; i++) {
591 if (!entryUUID_private->base_dns) {
592 return LDB_ERR_OPERATIONS_ERROR;
594 entryUUID_private->base_dns[i] = ldb_dn_new(entryUUID_private->base_dns, ldb, (const char *)el->values[i].data);
595 if ( ! ldb_dn_validate(entryUUID_private->base_dns[i])) {
596 return LDB_ERR_OPERATIONS_ERROR;
599 entryUUID_private->base_dns[i] = NULL;
605 static int find_base_dns(struct ldb_module *module,
606 struct entryUUID_private *entryUUID_private)
609 struct ldb_request *req;
610 const char *naming_context_attr[] = {
614 req = talloc(entryUUID_private, struct ldb_request);
616 ldb_set_errstring(module->ldb, "Out of Memory");
617 return LDB_ERR_OPERATIONS_ERROR;
620 req->operation = LDB_SEARCH;
621 req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
622 req->op.search.scope = LDB_SCOPE_BASE;
624 req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
625 if (req->op.search.tree == NULL) {
626 ldb_set_errstring(module->ldb, "Unable to parse search expression");
628 return LDB_ERR_OPERATIONS_ERROR;
631 req->op.search.attrs = naming_context_attr;
632 req->controls = NULL;
633 req->context = entryUUID_private;
634 req->callback = get_remote_rootdse;
635 ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
637 ret = ldb_next_request(module, req);
639 if (ret == LDB_SUCCESS) {
640 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
644 if (ret != LDB_SUCCESS) {
651 /* the context init function */
652 static int entryUUID_init(struct ldb_module *module)
655 struct map_private *map_private;
656 struct entryUUID_private *entryUUID_private;
658 ret = ldb_map_init(module, entryUUID_attributes, entryUUID_objectclasses, entryUUID_wildcard_attributes, NULL);
659 if (ret != LDB_SUCCESS)
662 map_private = talloc_get_type(module->private_data, struct map_private);
664 entryUUID_private = talloc_zero(map_private, struct entryUUID_private);
665 map_private->caller_private = entryUUID_private;
667 ret = find_base_dns(module, entryUUID_private);
669 return ldb_next_init(module);
672 /* the context init function */
673 static int nsuniqueid_init(struct ldb_module *module)
676 struct map_private *map_private;
677 struct entryUUID_private *entryUUID_private;
679 ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, NULL);
680 if (ret != LDB_SUCCESS)
683 map_private = talloc_get_type(module->private_data, struct map_private);
685 entryUUID_private = talloc_zero(map_private, struct entryUUID_private);
686 map_private->caller_private = entryUUID_private;
688 ret = find_base_dns(module, entryUUID_private);
690 return ldb_next_init(module);
693 static int get_seq(struct ldb_context *ldb, void *context,
694 struct ldb_reply *ares)
696 unsigned long long *max_seq = (unsigned long long *)context;
697 unsigned long long seq;
698 if (ares->type == LDB_REPLY_ENTRY) {
699 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
701 seq = entryCSN_to_usn_int(ares, &el->values[0]);
702 *max_seq = MAX(seq, *max_seq);
709 static int entryUUID_sequence_number(struct ldb_module *module, struct ldb_request *req)
712 struct map_private *map_private;
713 struct entryUUID_private *entryUUID_private;
714 unsigned long long max_seq = 0;
715 struct ldb_request *search_req;
716 map_private = talloc_get_type(module->private_data, struct map_private);
718 entryUUID_private = talloc_get_type(map_private->caller_private, struct entryUUID_private);
720 /* Search the baseDNs for a sequence number */
721 for (i=0; entryUUID_private &&
722 entryUUID_private->base_dns &&
723 entryUUID_private->base_dns[i];
725 static const char *contextCSN_attr[] = {
728 search_req = talloc(req, struct ldb_request);
729 if (search_req == NULL) {
730 ldb_set_errstring(module->ldb, "Out of Memory");
731 return LDB_ERR_OPERATIONS_ERROR;
734 search_req->operation = LDB_SEARCH;
735 search_req->op.search.base = entryUUID_private->base_dns[i];
736 search_req->op.search.scope = LDB_SCOPE_BASE;
738 search_req->op.search.tree = ldb_parse_tree(search_req, "objectClass=*");
739 if (search_req->op.search.tree == NULL) {
740 ldb_set_errstring(module->ldb, "Unable to parse search expression");
741 talloc_free(search_req);
742 return LDB_ERR_OPERATIONS_ERROR;
745 search_req->op.search.attrs = contextCSN_attr;
746 search_req->controls = NULL;
747 search_req->context = &max_seq;
748 search_req->callback = get_seq;
749 ldb_set_timeout(module->ldb, search_req, 0); /* use default timeout */
751 ret = ldb_next_request(module, search_req);
753 if (ret == LDB_SUCCESS) {
754 ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
757 talloc_free(search_req);
758 if (ret != LDB_SUCCESS) {
763 switch (req->op.seq_num.type) {
764 case LDB_SEQ_HIGHEST_SEQ:
765 req->op.seq_num.seq_num = max_seq;
768 req->op.seq_num.seq_num = max_seq;
769 req->op.seq_num.seq_num++;
771 case LDB_SEQ_HIGHEST_TIMESTAMP:
773 req->op.seq_num.seq_num = (max_seq >> 24);
777 req->op.seq_num.flags = 0;
778 req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
779 req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
783 static struct ldb_module_ops entryUUID_ops = {
785 .init_context = entryUUID_init,
786 .sequence_number = entryUUID_sequence_number
789 static struct ldb_module_ops nsuniqueid_ops = {
790 .name = "nsuniqueid",
791 .init_context = nsuniqueid_init,
792 .sequence_number = entryUUID_sequence_number
795 /* the init function */
796 int ldb_entryUUID_module_init(void)
799 struct ldb_module_ops ops = ldb_map_get_ops();
800 entryUUID_ops.add = ops.add;
801 entryUUID_ops.modify = ops.modify;
802 entryUUID_ops.del = ops.del;
803 entryUUID_ops.rename = ops.rename;
804 entryUUID_ops.search = ops.search;
805 entryUUID_ops.wait = ops.wait;
806 ret = ldb_register_module(&entryUUID_ops);
812 nsuniqueid_ops.add = ops.add;
813 nsuniqueid_ops.modify = ops.modify;
814 nsuniqueid_ops.del = ops.del;
815 nsuniqueid_ops.rename = ops.rename;
816 nsuniqueid_ops.search = ops.search;
817 nsuniqueid_ops.wait = ops.wait;
818 ret = ldb_register_module(&nsuniqueid_ops);