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_module.h"
31 #include "ldb/ldb_map/ldb_map.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "librpc/ndr/libndr.h"
35 #include "dsdb/samdb/samdb.h"
37 struct entryuuid_private {
38 struct ldb_context *ldb;
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_data_blob(val, &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, NULL, &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)
63 struct ldb_val out = data_blob(NULL, 0);
65 NTSTATUS status = GUID_from_data_blob(val, &guid);
66 if (!NT_STATUS_IS_OK(status)) {
69 return data_blob_string_const(GUID_string(ctx, &guid));
72 static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
75 NTSTATUS status = NS_GUID_from_string((char *)val->data, &guid);
76 enum ndr_err_code ndr_err;
77 struct ldb_val out = data_blob(NULL, 0);
79 if (!NT_STATUS_IS_OK(status)) {
82 ndr_err = ndr_push_struct_blob(&out, ctx, NULL, &guid,
83 (ndr_push_flags_fn_t)ndr_push_GUID);
84 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
91 static struct ldb_val guid_ns_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
93 struct ldb_val out = data_blob(NULL, 0);
95 NTSTATUS status = GUID_from_data_blob(val, &guid);
96 if (!NT_STATUS_IS_OK(status)) {
99 return data_blob_string_const(NS_GUID_string(ctx, &guid));
102 /* The backend holds binary sids, so just copy them back */
103 static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
105 struct ldb_val out = data_blob(NULL, 0);
106 out = ldb_val_dup(ctx, val);
111 /* Ensure we always convert sids into binary, so the backend doesn't have to know about both forms */
112 static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
114 struct ldb_context *ldb = ldb_module_get_ctx(module);
115 struct ldb_val out = data_blob(NULL, 0);
116 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, "objectSid");
118 if (a->syntax->canonicalise_fn(ldb, ctx, val, &out) != LDB_SUCCESS) {
119 return data_blob(NULL, 0);
125 /* Ensure we always convert objectCategory into a DN */
126 static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
128 struct ldb_context *ldb = ldb_module_get_ctx(module);
130 struct ldb_val out = data_blob(NULL, 0);
131 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, "objectCategory");
133 dn = ldb_dn_from_ldb_val(ctx, ldb, val);
134 if (dn && ldb_dn_validate(dn)) {
136 return val_copy(module, ctx, val);
140 if (a->syntax->canonicalise_fn(ldb, ctx, val, &out) != LDB_SUCCESS) {
141 return data_blob(NULL, 0);
147 static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
150 int32_t i = (int32_t) strtol((char *)val->data, NULL, 0);
151 out = data_blob_string_const(talloc_asprintf(ctx, "%d", i));
155 static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
158 unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
159 time_t t = (usn >> 24);
160 out = data_blob_string_const(talloc_asprintf(ctx, "%s#%06x#00#000000", ldb_timestring(ctx, t), (unsigned int)(usn & 0xFFFFFF)));
164 static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val)
166 char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
169 unsigned long long usn;
174 p = strchr(entryCSN, '#');
189 usn = strtol(mod_per_sec, NULL, 16);
191 t = ldb_string_to_time(entryCSN);
193 usn = usn | ((unsigned long long)t <<24);
197 static struct ldb_val entryCSN_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
200 unsigned long long usn = entryCSN_to_usn_int(ctx, val);
201 out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
205 static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
208 unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
209 time_t t = (usn >> 24);
210 out = data_blob_string_const(ldb_timestring(ctx, t));
214 static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
218 unsigned long long usn;
220 t = ldb_string_to_time((const char *)val->data);
222 usn = ((unsigned long long)t <<24);
224 out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
229 static const struct ldb_map_attribute entryuuid_attributes[] =
233 .local_name = "objectGUID",
237 .remote_name = "entryUUID",
238 .convert_local = guid_always_string,
239 .convert_remote = encode_guid,
245 .local_name = "invocationId",
249 .remote_name = "invocationId",
250 .convert_local = guid_always_string,
251 .convert_remote = encode_guid,
257 .local_name = "objectSid",
261 .remote_name = "objectSid",
262 .convert_local = sid_always_binary,
263 .convert_remote = val_copy,
268 .local_name = "name",
272 .remote_name = "samba4RDN"
277 .local_name = "whenCreated",
281 .remote_name = "createTimestamp"
286 .local_name = "whenChanged",
290 .remote_name = "modifyTimestamp"
295 .local_name = "objectClasses",
299 .remote_name = "samba4ObjectClasses"
304 .local_name = "dITContentRules",
308 .remote_name = "samba4DITContentRules"
313 .local_name = "attributeTypes",
317 .remote_name = "samba4AttributeTypes"
322 .local_name = "objectCategory",
326 .remote_name = "objectCategory",
327 .convert_local = objectCategory_always_dn,
328 .convert_remote = val_copy,
333 .local_name = "distinguishedName",
337 .remote_name = "entryDN"
342 .local_name = "groupType",
346 .remote_name = "groupType",
347 .convert_local = normalise_to_signed32,
348 .convert_remote = val_copy,
353 .local_name = "userAccountControl",
357 .remote_name = "userAccountControl",
358 .convert_local = normalise_to_signed32,
359 .convert_remote = val_copy,
364 .local_name = "sAMAccountType",
368 .remote_name = "sAMAccountType",
369 .convert_local = normalise_to_signed32,
370 .convert_remote = val_copy,
375 .local_name = "usnChanged",
379 .remote_name = "entryCSN",
380 .convert_local = usn_to_entryCSN,
381 .convert_remote = entryCSN_to_usn
386 .local_name = "usnCreated",
390 .remote_name = "createTimestamp",
391 .convert_local = usn_to_timestamp,
392 .convert_remote = timestamp_to_usn,
405 /* This objectClass conflicts with builtin classes on OpenLDAP */
406 const struct ldb_map_objectclass entryuuid_objectclasses[] =
409 .local_name = "subSchema",
410 .remote_name = "samba4SubSchema"
417 /* These things do not show up in wildcard searches in OpenLDAP, but
418 * we need them to show up in the AD-like view */
419 static const char * const entryuuid_wildcard_attributes[] = {
429 static const struct ldb_map_attribute nsuniqueid_attributes[] =
433 .local_name = "objectGUID",
437 .remote_name = "nsuniqueid",
438 .convert_local = guid_ns_string,
439 .convert_remote = encode_ns_guid,
445 .local_name = "objectSid",
449 .remote_name = "objectSid",
450 .convert_local = sid_always_binary,
451 .convert_remote = val_copy,
456 .local_name = "whenCreated",
460 .remote_name = "createTimestamp"
465 .local_name = "whenChanged",
469 .remote_name = "modifyTimestamp"
474 .local_name = "objectCategory",
478 .remote_name = "objectCategory",
479 .convert_local = objectCategory_always_dn,
480 .convert_remote = val_copy,
485 .local_name = "distinguishedName",
489 .remote_name = "entryDN"
494 .local_name = "groupType",
498 .remote_name = "groupType",
499 .convert_local = normalise_to_signed32,
500 .convert_remote = val_copy,
505 .local_name = "userAccountControl",
509 .remote_name = "userAccountControl",
510 .convert_local = normalise_to_signed32,
511 .convert_remote = val_copy,
516 .local_name = "sAMAccountType",
520 .remote_name = "sAMAccountType",
521 .convert_local = normalise_to_signed32,
522 .convert_remote = val_copy,
527 .local_name = "usnChanged",
531 .remote_name = "modifyTimestamp",
532 .convert_local = usn_to_timestamp,
533 .convert_remote = timestamp_to_usn,
538 .local_name = "usnCreated",
542 .remote_name = "createTimestamp",
543 .convert_local = usn_to_timestamp,
544 .convert_remote = timestamp_to_usn,
557 /* These things do not show up in wildcard searches in OpenLDAP, but
558 * we need them to show up in the AD-like view */
559 static const char * const nsuniqueid_wildcard_attributes[] = {
568 /* the context init function */
569 static int entryuuid_init(struct ldb_module *module)
572 ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, "samba4Top", NULL);
573 if (ret != LDB_SUCCESS)
576 return ldb_next_init(module);
579 /* the context init function */
580 static int nsuniqueid_init(struct ldb_module *module)
583 ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, "extensibleObject", NULL);
584 if (ret != LDB_SUCCESS)
587 return ldb_next_init(module);
590 static int get_seq_callback(struct ldb_request *req,
591 struct ldb_reply *ares)
593 unsigned long long *seq = (unsigned long long *)req->context;
596 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
598 if (ares->error != LDB_SUCCESS) {
599 return ldb_request_done(req, ares->error);
602 if (ares->type == LDB_REPLY_ENTRY) {
603 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
605 *seq = entryCSN_to_usn_int(ares, &el->values[0]);
609 if (ares->type == LDB_REPLY_DONE) {
610 return ldb_request_done(req, LDB_SUCCESS);
617 static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
619 struct ldb_context *ldb;
621 struct map_private *map_private;
622 struct entryuuid_private *entryuuid_private;
623 unsigned long long seq_num = 0;
624 struct ldb_request *search_req;
626 const struct ldb_control *partition_ctrl;
627 const struct dsdb_control_current_partition *partition;
629 static const char *contextCSN_attr[] = {
633 struct ldb_seqnum_request *seq;
634 struct ldb_seqnum_result *seqr;
635 struct ldb_extended *ext;
637 ldb = ldb_module_get_ctx(module);
639 seq = talloc_get_type(req->op.extended.data, struct ldb_seqnum_request);
641 map_private = talloc_get_type(ldb_module_get_private(module), struct map_private);
643 entryuuid_private = talloc_get_type(map_private->caller_private, struct entryuuid_private);
645 /* All this to get the DN of the parition, so we can search the right thing */
646 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
647 if (!partition_ctrl) {
648 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
649 "entryuuid_sequence_number: no current partition control found");
650 return LDB_ERR_CONSTRAINT_VIOLATION;
653 partition = talloc_get_type(partition_ctrl->data,
654 struct dsdb_control_current_partition);
655 SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
657 ret = ldb_build_search_req(&search_req, ldb, req,
658 partition->dn, LDB_SCOPE_BASE,
659 NULL, contextCSN_attr, NULL,
660 &seq_num, get_seq_callback,
662 if (ret != LDB_SUCCESS) {
666 ret = ldb_next_request(module, search_req);
668 if (ret == LDB_SUCCESS) {
669 ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
672 talloc_free(search_req);
673 if (ret != LDB_SUCCESS) {
677 ext = talloc_zero(req, struct ldb_extended);
679 return LDB_ERR_OPERATIONS_ERROR;
681 seqr = talloc_zero(req, struct ldb_seqnum_result);
684 return LDB_ERR_OPERATIONS_ERROR;
686 ext->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
690 case LDB_SEQ_HIGHEST_SEQ:
691 seqr->seq_num = seq_num;
694 seqr->seq_num = seq_num;
697 case LDB_SEQ_HIGHEST_TIMESTAMP:
699 seqr->seq_num = (seq_num >> 24);
704 seqr->flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
705 seqr->flags |= LDB_SEQ_GLOBAL_SEQUENCE;
707 /* send request done */
708 return ldb_module_done(req, NULL, ext, LDB_SUCCESS);
711 static int entryuuid_extended(struct ldb_module *module, struct ldb_request *req)
713 if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
714 return entryuuid_sequence_number(module, req);
717 return ldb_next_request(module, req);
720 _PUBLIC_ const struct ldb_module_ops ldb_entryuuid_module_ops = {
722 .init_context = entryuuid_init,
723 .extended = entryuuid_extended,
727 _PUBLIC_ const struct ldb_module_ops ldb_nsuniqueid_module_ops = {
728 .name = "nsuniqueid",
729 .init_context = nsuniqueid_init,
730 .extended = entryuuid_extended,