4 Copyright (C) Matthieu Patou <mat@matws.net> 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "ldb/include/ldb.h"
23 #include "ldb/include/ldb_errors.h"
24 #include "ldb/include/ldb_module.h"
25 #include "libcli/security/security.h"
26 #include "librpc/gen_ndr/drsblobs.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/ndr/libndr.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "dsdb/samdb/ldb_modules/util.h"
32 #define LDAP_DIRSYNC_OBJECT_SECURITY 0x01
33 #define LDAP_DIRSYNC_ANCESTORS_FIRST_ORDER 0x800
34 #define LDAP_DIRSYNC_PUBLIC_DATA_ONLY 0x2000
35 #define LDAP_DIRSYNC_INCREMENTAL_VALUES 0x80000000
38 struct dirsync_context {
39 struct ldb_module *module;
40 struct ldb_request *req;
43 * We keep a track of the number of attributes that we
44 * add just for the need of the implementation
45 * it will be usefull to track then entries that needs not to
46 * be returned because there is no real change
49 unsigned int nbDefaultAttrs;
59 const struct GUID *our_invocation_id;
60 const struct dsdb_schema *schema;
61 struct ldb_dn *nc_root;
62 struct drsuapi_DsReplicaCursor *cursors;
66 static int dirsync_filter_entry(struct ldb_request *req,
67 struct ldb_message *msg,
68 struct ldb_control **controls,
69 struct dirsync_context *dsc,
72 struct ldb_context *ldb;
74 enum ndr_err_code ndr_err;
78 struct ldb_val *replMetaData = NULL;
79 struct replPropertyMetaDataBlob rmd;
80 const struct dsdb_attribute *attr;
81 const char **listAttr = NULL;
82 bool namereturned = false;
83 bool nameasked = false;
85 /* Ajustment for the added attributes, it will reduce the number of
86 * expected to be here attributes*/
87 unsigned int delta = 0;
88 const char **myaccept = NULL;
89 const char *emptyaccept[] = { NULL };
90 const char *extendedaccept[] = { "GUID", "SID", "WKGUID", NULL };
91 const char *rdn = NULL;
92 struct ldb_message_element *el;
93 struct ldb_message *newmsg;
96 * Where we asked to do extended dn ?
97 * if so filter out everything bug GUID, SID, WKGUID,
98 * if not filter out everything (just keep the dn).
100 if ( dsc->noextended == true ) {
101 myaccept = emptyaccept;
103 myaccept = extendedaccept;
105 ldb = ldb_module_get_ctx(dsc->module);
107 if (msg->num_elements == 0) {
109 * Entry that we don't really have access to
113 ldb_dn_extended_filter(msg->dn, myaccept);
116 * If the RDN starts with CN then the CN attribute is never returned
118 rdn = ldb_dn_get_rdn_name(msg->dn);
121 * if objectGUID is asked and we are dealing for the referrals entries and
122 * the usn searched is 0 then we didn't count the objectGUID as an automatically
123 * returned attribute, do to so we increament delta.
125 if (referral == true &&
126 ldb_attr_in_list(req->op.search.attrs, "objectGUID") &&
127 dsc->fromreqUSN == 0) {
133 * In terms of big O notation this is not the best algorithm,
134 * but we try our best not to make the worse one.
135 * We are obliged to run through the n message's elements
136 * and through the p elements of the replPropertyMetaData.
138 * It turns out that we are crawling twice the message's elements
139 * the first crawl is to remove the non replicated and generated
140 * attributes. The second one is to remove attributes that haven't
141 * a USN > as the requested one.
143 * In the second crawl we are reading the list of elements in the
144 * replPropertyMetaData for each remaining replicated attribute.
145 * In order to keep the list small
147 * We have a O(n'*p') complexity, in worse case n' = n and p' = p
148 * but in most case n' = n/2 (at least half of returned attributes
149 * are not replicated or generated) and p' is small as we
150 * list only the attribute that have been modified since last interogation
153 newmsg = talloc_zero(dsc->req, struct ldb_message);
154 if (newmsg == NULL) {
157 for (i = msg->num_elements - 1; i >= 0; i--) {
158 attr = dsdb_attribute_by_lDAPDisplayName(dsc->schema, msg->elements[i].name);
159 if (ldb_attr_cmp(msg->elements[i].name, "uSNChanged") == 0) {
160 /* Read the USN it will used at the end of the filtering
161 * to update the max USN in the cookie if we
162 * decide to keep this entry
164 val = strtoull((const char*)msg->elements[i].values[0].data, NULL, 0);
168 if (ldb_attr_cmp(msg->elements[i].name,
169 "replPropertyMetaData") == 0) {
170 replMetaData = (talloc_steal(dsc, &msg->elements[i].values[0]));
175 if (replMetaData == NULL) {
176 bool guidfound = false;
179 * We are in the case of deleted object where we don't have the
182 if (!ldb_msg_find_attr_as_uint(msg, "isDeleted", 0)) {
184 * This is not a deleted item and we don't
185 * have the replPropertyMetaData.
190 newmsg->dn = ldb_dn_new(newmsg, ldb, "");
191 if (newmsg->dn == NULL) {
195 el = ldb_msg_find_element(msg, "objectGUID");
200 * We expect to find the GUID in the object,
201 * if it turns out not to be the case sometime
202 * well will uncomment the code bellow
204 SMB_ASSERT(guidfound == true);
206 if (guidfound == false) {
208 struct ldb_val *new_val;
212 txt = strrchr(txt, ':');
214 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
218 status = GUID_from_string(txt, &guid);
219 if (!NT_STATUS_IS_OK(status)) {
220 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
223 status = GUID_to_ndr_blob(&guid, msg, &guid_blob);
224 if (!NT_STATUS_IS_OK(status)) {
225 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
228 new_val = talloc(msg, struct ldb_val);
229 if (new_val == NULL) {
232 new_val->data = talloc_steal(new_val, guid_blob.data);
233 new_val->length = guid_blob.length;
234 if (ldb_msg_add_value(msg, "objectGUID", new_val, NULL) != 0) {
235 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
239 ldb_msg_add(newmsg, el, LDB_FLAG_MOD_ADD);
240 talloc_steal(newmsg->elements, el->name);
241 talloc_steal(newmsg->elements, el->values);
243 talloc_steal(newmsg->elements, msg);
244 return ldb_module_send_entry(dsc->req, msg, controls);
247 ndr_err = ndr_pull_struct_blob(replMetaData, dsc, &rmd,
248 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
249 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
250 ldb_set_errstring(ldb, "Unable to unmarshall replPropertyMetaData");
251 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
253 if (ldb_attr_in_list(req->op.search.attrs, "name") ||
254 ldb_attr_in_list(req->op.search.attrs, "*")) {
259 * If we don't have an USN and no updateness array then we skip the
260 * test phase this is an optimisation for the case when you
261 * first query the DC without a cookie.
262 * As this query is most probably the one
263 * that will return the biggest answer, skipping this part
264 * will really save time.
266 if (ldb_dn_compare(dsc->nc_root, msg->dn) == 0) {
267 /* If we have name then we expect to have parentGUID,
268 * it will not be the case for the root of the NC
273 if (dsc->fromreqUSN > 0 || dsc->cursors != NULL) {
276 * Allocate an array of size(replMetaData) of char*
277 * we know that it will be oversized but it's a short lived element
279 listAttr = talloc_array(msg, const char*, rmd.ctr.ctr1.count + 1);
280 if (listAttr == NULL) {
283 for (n=0; n < rmd.ctr.ctr1.count; n++) {
284 struct replPropertyMetaData1 *omd = &rmd.ctr.ctr1.array[n];
285 if (omd->local_usn > dsc->fromreqUSN) {
286 const struct dsdb_attribute *a = dsdb_attribute_by_attributeID_id(dsc->schema,
288 if (!dsc->localonly) {
289 struct drsuapi_DsReplicaCursor *tab = dsc->cursors;
291 for (l=0; l < dsc->cursor_size; l++) {
292 if (GUID_equal(&tab[l].source_dsa_invocation_id, &omd->originating_invocation_id) &&
293 tab[l].highest_usn >= omd->originating_usn) {
295 * If we have in the uptodateness vector an entry
296 * with the same invocation id as the originating invocation
297 * and if the usn in the vector is greater or equal to
298 * the one in originating_usn, then it means that this entry
299 * has already been sent (from another DC) to the client
300 * no need to resend it one more time.
305 /* If we are here it's because we have a usn > (max(usn of vectors))*/
307 if (namereturned == false &&
309 ldb_attr_cmp(a->lDAPDisplayName, "name") == 0) {
311 if (ldb_dn_compare(dsc->nc_root, msg->dn) == 0) {
315 listAttr[j] = a->lDAPDisplayName;
324 if (ldb_attr_in_list(req->op.search.attrs, "*") ||
325 ldb_attr_in_list(req->op.search.attrs, "name")) {
332 * Let's loop around the remaining elements
333 * to see which one are in the listAttr.
334 * If they are in this array it means that
335 * their localusn > usn from the request (in the cookie)
336 * if not we remove the attribute.
338 for (i = msg->num_elements - 1; i >= 0; i--) {
339 const char *ldapattrname;
341 el = &(msg->elements[i]);
342 ldapattrname = el->name;
344 attr = dsdb_attribute_by_lDAPDisplayName(dsc->schema,
348 if (attr->linkID & 1) {
350 * Attribute is a backlink so let's remove it
355 if (ldb_attr_cmp(msg->elements[i].name,
356 "replPropertyMetaData") == 0) {
360 if ((attr->systemFlags & (DS_FLAG_ATTR_NOT_REPLICATED | DS_FLAG_ATTR_IS_CONSTRUCTED))) {
361 if (ldb_attr_cmp(attr->lDAPDisplayName, "objectGUID") != 0 &&
362 ldb_attr_cmp(attr->lDAPDisplayName, "parentGUID") != 0) {
364 * Attribute is constructed or not replicated, let's get rid of it
368 /* Let's keep the attribute that we forced to be added
369 * even if they are not in the replicationMetaData
370 * or are just generated
372 if (namereturned == false &&
373 (ldb_attr_cmp(attr->lDAPDisplayName, "parentGUID") == 0)) {
377 if (ldb_msg_add(newmsg, el, LDB_FLAG_MOD_ADD) != LDB_SUCCESS) {
378 return ldb_error(ldb,
379 LDB_ERR_OPERATIONS_ERROR,
380 "Unable to add attribute");
382 talloc_steal(newmsg->elements, el->name);
383 talloc_steal(newmsg->elements, el->values);
388 if (ldb_attr_cmp(msg->elements[i].name, rdn) == 0) {
390 * We have an attribute that is the same as the start of the RDN
391 * (ie. attribute CN with rdn CN=).
396 if (ldb_attr_cmp(attr->lDAPDisplayName, "instanceType") == 0) {
397 if (ldb_msg_add(newmsg, el, LDB_FLAG_MOD_ADD) != LDB_SUCCESS) {
398 return ldb_error(ldb,
399 LDB_ERR_OPERATIONS_ERROR,
400 "Unable to add attribute");
402 talloc_steal(newmsg->elements, el->name);
403 talloc_steal(newmsg->elements, el->values);
406 /* For links, when our functional level > windows 2000
407 * we use the RMD_LOCAL_USN information to decide wether
408 * we return the attribute or not.
409 * For windows 2000 this information is in the replPropertyMetaData
410 * so it will be handled like any other replicated attribute
413 if (dsc->functional_level > DS_DOMAIN_FUNCTION_2000 &&
414 attr->linkID != 0 ) {
417 * Elements for incremental changes on linked attributes
419 struct ldb_message_element *el_incr_add = NULL;
420 struct ldb_message_element *el_incr_del = NULL;
422 * Attribute is a forwardlink so let's remove it
425 for (k = el->num_values -1; k >= 0; k--) {
428 uint32_t tmp_usn = 0;
429 uint32_t tmp_usn2 = 0;
430 struct GUID invocation_id = GUID_zero();
431 struct dsdb_dn *dn = dsdb_dn_parse(msg, ldb, &el->values[k], attr->syntax->ldap_oid);
432 struct ldb_dn *copydn;
434 ldb_set_errstring(ldb, "Cannot parse DN");
435 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
438 copydn = ldb_dn_copy(msg, dn->dn);
439 if (copydn == NULL) {
443 status = dsdb_get_extended_dn_uint32(dn->dn, &tmp_usn, "RMD_LOCAL_USN");
444 if (!NT_STATUS_IS_OK(status)) {
446 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
448 status = dsdb_get_extended_dn_guid(dn->dn, &invocation_id, "RMD_INVOCID");
449 if (!NT_STATUS_IS_OK(status)) {
451 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
454 status = dsdb_get_extended_dn_uint32(dn->dn, &flags, "RMD_FLAGS");
455 if (!NT_STATUS_IS_OK(status)) {
457 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
460 status = dsdb_get_extended_dn_uint32(dn->dn, &tmp_usn2, "RMD_ORIGINATING_USN");
461 if (!NT_STATUS_IS_OK(status)) {
463 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
466 ldb_dn_extended_filter(dn->dn, myaccept);
467 dn_ln = ldb_dn_get_extended_linearized(dn, dn->dn, 1);
471 ldb_set_errstring(ldb, "Cannot linearize dn");
472 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
475 talloc_free(el->values[k].data);
476 el->values[k].data = (uint8_t*)talloc_steal(el->values, dn_ln);
477 if (el->values[k].data == NULL) {
479 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
481 el->values[k].length = strlen(dn_ln);
484 if (tmp_usn > dsc->fromreqUSN) {
485 if (!dsc->localonly) {
486 struct drsuapi_DsReplicaCursor *tab = dsc->cursors;
489 for (l=0; l < dsc->cursor_size; l++) {
490 if (GUID_equal(&tab[l].source_dsa_invocation_id, &invocation_id) &&
491 tab[l].highest_usn >= tmp_usn2) {
493 * If we have in the uptodateness vector an entry
494 * with the same invocation id as the originating invocation
495 * and if the usn in the vector is greater or equal to
496 * the one in originating_usn, then it means that this entry
497 * has already been sent (from another DC) to the client
498 * no need to resend it one more time.
503 /* If we are here it's because we have a usn > (max(usn of vectors))*/
508 /* If we are here it's because the link is more recent than either any
509 * originating usn or local usn
512 if (dsc->linkIncrVal == true) {
513 struct ldb_message_element *tmpel;
514 if (flags & DSDB_RMD_FLAG_DELETED) {
515 /* We have to check that the inactive link still point to an existing object */
520 status = dsdb_get_extended_dn_guid(copydn, &guid, "GUID");
521 if (!NT_STATUS_IS_OK(status)) {
522 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
523 el->name, ldb_dn_get_linearized(copydn)));
524 return ldb_operr(ldb);
526 ret = dsdb_module_dn_by_guid(dsc->module, newmsg, &guid, &tdn, req);
527 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
528 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
529 GUID_string(newmsg, &guid)));
531 } else if (ret != LDB_SUCCESS) {
532 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
533 GUID_string(newmsg, &guid),
543 tmpel = talloc_zero(newmsg, struct ldb_message_element);
547 tmpel->values = talloc_array(tmpel, struct ldb_val, 1);
548 if (tmpel->values == NULL) {
551 if (flags & DSDB_RMD_FLAG_DELETED) {
552 tmpel->name = talloc_asprintf(tmpel,
557 tmpel->name = talloc_asprintf(tmpel,
561 if (tmpel->name == NULL) {
564 tmpel->num_values = 1;
566 tmpel->num_values += 1;
567 tmpel->values = talloc_realloc(tmpel,
571 if (tmpel->values == NULL) {
576 tmpel->values[tmpel->num_values -1].data =talloc_steal(tmpel->values, el->values[k].data);
577 tmpel->values[tmpel->num_values -1].length = el->values[k].length;
579 if (flags & DSDB_RMD_FLAG_DELETED) {
587 if (dsc->linkIncrVal == false) {
588 if (flags & DSDB_RMD_FLAG_DELETED) {
589 if (k < (el->num_values - 1)) {
590 memmove(el->values + k,
591 el->values + (k + 1),
592 ((el->num_values - 1) - k)*sizeof(*el->values));
602 if (dsc->linkIncrVal == false) {
603 if (ldb_msg_add(newmsg, el, LDB_FLAG_MOD_ADD) != LDB_SUCCESS) {
604 return ldb_error(ldb,
605 LDB_ERR_OPERATIONS_ERROR,
606 "Unable to add attribute");
608 talloc_steal(newmsg->elements, el->name);
609 talloc_steal(newmsg->elements, el->values);
612 if (ldb_msg_add(newmsg, el_incr_del, LDB_FLAG_MOD_ADD))
613 return ldb_error(ldb,
614 LDB_ERR_OPERATIONS_ERROR,
615 "Unable to add attribute");
618 if (ldb_msg_add(newmsg, el_incr_add, LDB_FLAG_MOD_ADD))
619 return ldb_error(ldb,
620 LDB_ERR_OPERATIONS_ERROR,
621 "Unable to add attribute");
629 for (j=0; j<size; j++) {
631 * We mark attribute that has already been seen well
632 * as seen. So that after attribute that are still in
633 * listAttr are attributes that has been modified after
634 * the requested USN but not present in the attributes
635 * returned by the ldb search.
636 * That is to say attributes that have been removed
638 if (listAttr[j] && ldb_attr_cmp(listAttr[j], ldapattrname) == 0) {
649 if (ldb_msg_add(newmsg, el, LDB_FLAG_MOD_ADD) != LDB_SUCCESS) {
650 return ldb_error(ldb,
651 LDB_ERR_OPERATIONS_ERROR,
652 "Unable to add attribute");
654 talloc_steal(newmsg->elements, el->name);
655 talloc_steal(newmsg->elements, el->values);
659 talloc_steal(newmsg->elements, msg);
662 * Here we run through the list of attributes returned
663 * in the propertyMetaData.
664 * Entries of this list have usn > requested_usn,
665 * entries that are also present in the message have been
666 * replaced by NULL, so at this moment the list contains
667 * only elements that have a usn > requested_usn and that
668 * haven't been seen. It's attributes that were removed.
669 * We add them to the message like empty elements.
671 for (j=0; j<size; j++) {
673 ldb_attr_in_list(req->op.search.attrs, "*") ||
674 ldb_attr_in_list(req->op.search.attrs, listAttr[j])) &&
675 (ldb_attr_cmp(listAttr[j], rdn) != 0) &&
676 (ldb_attr_cmp(listAttr[j], "instanceType") != 0)) {
677 ldb_msg_add_empty(newmsg, listAttr[j], LDB_FLAG_MOD_DELETE, NULL);
680 talloc_free(listAttr);
682 if ((newmsg->num_elements - ( dsc->nbDefaultAttrs - delta)) > 0) {
684 * After cleaning attributes there is still some attributes that were not added just
685 * for the purpose of the control (objectGUID, instanceType, ...)
688 newmsg->dn = talloc_steal(newmsg, msg->dn);
689 if (val > dsc->highestUSN) {
690 dsc->highestUSN = val;
692 return ldb_module_send_entry(dsc->req, newmsg, controls);
700 static int dirsync_create_vector(struct ldb_request *req,
701 struct ldb_reply *ares,
702 struct dirsync_context *dsc,
703 struct ldapControlDirSyncCookie *cookie,
704 struct ldb_context *ldb)
706 struct ldb_result *resVector;
707 const char* attrVector[] = {"replUpToDateVector", NULL };
708 uint64_t highest_usn;
711 struct drsuapi_DsReplicaCursor *tab;
713 ret = ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
714 if (ret != LDB_SUCCESS) {
715 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "Unable to get highest USN from current NC");
718 /* If we have a full answer then the highest USN
719 * is not the highest USN from the result set but the
720 * highest of the naming context, unless the sequence is not updated yet.
722 if (highest_usn > dsc->highestUSN) {
723 dsc->highestUSN = highest_usn;
727 ret = dsdb_module_search_dn(dsc->module, dsc, &resVector,
730 DSDB_FLAG_NEXT_MODULE, req);
731 if (ret != LDB_SUCCESS) {
732 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
733 "Unable to get replUpToDateVector for current NC");
736 if (resVector->count != 0) {
739 struct ldb_message_element *el = ldb_msg_find_element(resVector->msgs[0], "replUpToDateVector");
741 enum ndr_err_code ndr_err;
742 struct replUpToDateVectorBlob utd;
743 blob.data = el->values[0].data;
744 blob.length = el->values[0].length;
745 ndr_err = ndr_pull_struct_blob(&blob, dsc, &utd,
746 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
748 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
749 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
750 "Unable to pull replUpToDateVectorBlob structure");
754 count += utd.ctr.ctr2.count;
755 tab = talloc_array(cookie, struct drsuapi_DsReplicaCursor, count);
759 for (i=1; i < count; i++) {
760 memset(&tab[i], 0, sizeof(struct drsuapi_DsReplicaCursor));
761 tab[i].highest_usn = utd.ctr.ctr2.cursors[i-1].highest_usn;
762 tab[i].source_dsa_invocation_id = utd.ctr.ctr2.cursors[i-1].source_dsa_invocation_id;
765 tab = talloc_array(cookie, struct drsuapi_DsReplicaCursor, count);
772 * No replUpToDateVector ? it happens quite often (1 DC,
773 * other DCs didn't update ...
775 tab = talloc_array(cookie, struct drsuapi_DsReplicaCursor, count);
780 /* Our vector is always the first */
781 tab[0].highest_usn = dsc->highestUSN;
782 tab[0].source_dsa_invocation_id = *(dsc->our_invocation_id);
785 /* We have to add the updateness vector that we have*/
786 /* Version is always 1 in dirsync cookies */
787 cookie->blob.extra.uptodateness_vector.version = 1;
788 cookie->blob.extra.uptodateness_vector.reserved = 0;
789 cookie->blob.extra.uptodateness_vector.ctr.ctr1.count = count;
790 cookie->blob.extra.uptodateness_vector.ctr.ctr1.reserved = 0;
791 cookie->blob.extra.uptodateness_vector.ctr.ctr1.cursors = tab;
796 static int dirsync_search_callback(struct ldb_request *req, struct ldb_reply *ares)
799 struct dirsync_context *dsc;
800 struct ldb_result *res, *res2;
801 struct ldb_dirsync_control *control;
802 struct ldapControlDirSyncCookie *cookie;
803 struct ldb_context *ldb;
808 const char *attrs[] = { "objectGUID", NULL };
809 enum ndr_err_code ndr_err;
813 dsc = talloc_get_type_abort(req->context, struct dirsync_context);
814 ldb = ldb_module_get_ctx(dsc->module);
816 return ldb_module_done(dsc->req, NULL, NULL,
817 LDB_ERR_OPERATIONS_ERROR);
819 if (ares->error != LDB_SUCCESS) {
820 return ldb_module_done(dsc->req, ares->controls,
821 ares->response, ares->error);
824 switch (ares->type) {
825 case LDB_REPLY_ENTRY:
826 return dirsync_filter_entry(req, ares->message, ares->controls, dsc, false);
828 case LDB_REPLY_REFERRAL:
829 /* Skip the ldap(s):// so up to 8 chars,
830 * we don't care to be precise as the goal is to be in
831 * the name of DC, then we search the next '/'
832 * as it will be the last char before the DN of the referal
834 if (strncmp(ares->referral, "ldap://", 7) == 0) {
835 tmp = ares->referral + 7;
836 } else if (strncmp(ares->referral, "ldaps://", 8) == 0) {
837 tmp = ares->referral + 8;
839 return ldb_operr(ldb);
842 tmp = strchr(tmp, '/');
845 dn = ldb_dn_new(dsc, ldb, tmp);
850 flags = DSDB_FLAG_NEXT_MODULE |
851 DSDB_SEARCH_SHOW_DELETED |
852 DSDB_SEARCH_SHOW_EXTENDED_DN;
855 flags = flags | DSDB_FLAG_AS_SYSTEM;
858 ret = dsdb_module_search_tree(dsc->module, dsc, &res,
861 req->op.search.attrs,
864 if (ret != LDB_SUCCESS) {
869 if (res->count > 1) {
870 char *ldbmsg = talloc_asprintf(dn, "LDB returned more than result for dn: %s", tmp);
872 ldb_set_errstring(ldb, ldbmsg);
875 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
876 } else if (res->count == 0) {
877 /* if nothing is returned then it means that we don't
885 * Fetch the objectGUID of the root of current NC
887 ret = dsdb_module_search_dn(dsc->module, dsc, &res2,
890 DSDB_FLAG_NEXT_MODULE, req);
892 if (ret != LDB_SUCCESS) {
895 if (res2->msgs[0]->num_elements != 1) {
896 ldb_set_errstring(ldb,
897 "More than 1 attribute returned while looking for objectGUID");
898 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
901 val = res2->msgs[0]->elements[0].values;
902 ret = ldb_msg_add_value(res->msgs[0], "parentGUID", val, NULL);
904 * It *very* important to steal otherwise as val is in a subcontext
905 * related to res2, when the value will be one more time stolen
906 * it's elements[x].values that will be stolen, so it's important to
907 * recreate the context hierrachy as if it was done from a ldb_request
909 talloc_steal(res->msgs[0]->elements[0].values, val);
910 if (ret != LDB_SUCCESS) {
913 return dirsync_filter_entry(req, res->msgs[0], res->controls, dsc, true);
917 * Let's add our own control
920 control = talloc_zero(ares->controls, struct ldb_dirsync_control);
921 if (control == NULL) {
926 * When outputing flags is used to say more results.
927 * For the moment we didn't honnor the size info */
932 * max_attribute is unused cf. 3.1.1.3.4.1.3 LDAP_SERVER_DIRSYNC_OID in MS-ADTS
935 control->max_attributes = 0;
936 cookie = talloc_zero(control, struct ldapControlDirSyncCookie);
937 if (cookie == NULL) {
942 ret = dirsync_create_vector(req, ares, dsc, cookie, ldb);
943 if (ret != LDB_SUCCESS) {
944 return ldb_module_done(dsc->req, NULL, NULL, ret);
948 unix_to_nt_time(&now, time(NULL));
949 cookie->blob.time = now;
950 cookie->blob.highwatermark.highest_usn = dsc->highestUSN;
951 cookie->blob.highwatermark.tmp_highest_usn = dsc->highestUSN;
952 cookie->blob.guid1 = *(dsc->our_invocation_id);
954 blob = talloc_zero(control, DATA_BLOB);
959 ndr_err = ndr_push_struct_blob(blob, blob, cookie,
960 (ndr_push_flags_fn_t)ndr_push_ldapControlDirSyncCookie);
962 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
963 ldb_set_errstring(ldb, "Can't marshall ldapControlDirSyncCookie struct");
964 return ldb_module_done(dsc->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
966 control->cookie = (char *)blob->data;
967 control->cookie_len = blob->length;
968 ldb_reply_add_control(ares, LDB_CONTROL_DIRSYNC_OID, true, control);
970 return ldb_module_done(dsc->req, ares->controls,
971 ares->response, LDB_SUCCESS);
977 static int dirsync_ldb_search(struct ldb_module *module, struct ldb_request *req)
979 struct ldb_control *control;
980 struct ldb_result *acl_res;
981 struct ldb_dirsync_control *dirsync_ctl;
982 struct ldb_request *down_req;
983 struct dirsync_context *dsc;
984 struct ldb_context *ldb;
985 struct ldb_parse_tree *new_tree = req->op.search.tree;
987 enum ndr_err_code ndr_err;
993 if (ldb_dn_is_special(req->op.search.base)) {
994 return ldb_next_request(module, req);
998 * check if there's an extended dn control
1000 control = ldb_request_get_control(req, LDB_CONTROL_DIRSYNC_OID);
1001 if (control == NULL) {
1002 /* not found go on */
1003 return ldb_next_request(module, req);
1006 ldb = ldb_module_get_ctx(module);
1008 * This control must always be critical otherwise we return PROTOCOL error
1010 if (!control->critical) {
1011 return ldb_operr(ldb);
1014 dsc = talloc_zero(req, struct dirsync_context);
1016 return ldb_oom(ldb);
1018 dsc->module = module;
1020 dsc->nbDefaultAttrs = 0;
1023 dirsync_ctl = talloc_get_type(control->data, struct ldb_dirsync_control);
1024 if (dirsync_ctl == NULL) {
1025 return ldb_error(ldb, LDB_ERR_PROTOCOL_ERROR, "No data in dirsync control");
1028 ret = dsdb_find_nc_root(ldb, dsc, req->op.search.base, &dsc->nc_root);
1029 if (ret != LDB_SUCCESS) {
1033 if (ldb_dn_compare(dsc->nc_root, req->op.search.base) != 0) {
1034 if (dirsync_ctl->flags & LDAP_DIRSYNC_OBJECT_SECURITY) {
1035 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM,
1036 "DN is not one of the naming context");
1039 return ldb_error(ldb, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS,
1040 "dN is not one of the naming context");
1044 if (!(dirsync_ctl->flags & LDAP_DIRSYNC_OBJECT_SECURITY)) {
1045 struct dom_sid *sid;
1046 struct security_descriptor *sd = NULL;
1047 const char *acl_attrs[] = { "nTSecurityDescriptor", "objectSid", NULL };
1049 * If we don't have the flag and if we have the "replicate directory change" granted
1050 * then we upgrade ourself to system to not be blocked by the acl
1052 /* FIXME we won't check the replicate directory change filtered attribute set
1053 * it should be done so that if attr is not empty then we check that the user
1054 * has also this right
1058 * First change to system to get the SD of the root of current NC
1059 * if we don't the acl_read will forbid us the right to read it ...
1061 ret = dsdb_module_search_dn(module, dsc, &acl_res,
1062 req->op.search.base,
1064 DSDB_FLAG_NEXT_MODULE|DSDB_FLAG_AS_SYSTEM, req);
1066 if (ret != LDB_SUCCESS) {
1070 sid = samdb_result_dom_sid(dsc, acl_res->msgs[0], "objectSid");
1071 /* sid can be null ... */
1072 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), acl_res, acl_res->msgs[0], &sd);
1074 if (ret != LDB_SUCCESS) {
1077 ret = acl_check_extended_right(dsc, sd, acl_user_token(module), GUID_DRS_GET_CHANGES, SEC_ADS_CONTROL_ACCESS, sid);
1079 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
1082 dsc->assystem = true;
1083 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
1085 if (ret != LDB_SUCCESS) {
1088 talloc_free(acl_res);
1090 flags |= DSDB_ACL_CHECKS_DIRSYNC_FLAG;
1092 if (ret != LDB_SUCCESS) {
1098 dsc->functional_level = dsdb_functional_level(ldb);
1100 if (req->op.search.attrs) {
1101 attrs = ldb_attr_list_copy(dsc, req->op.search.attrs);
1102 if (attrs == NULL) {
1103 return ldb_oom(ldb);
1106 * Check if we have only "dn" as attribute, if so then
1107 * treat as if "*" was requested
1109 if (attrs && attrs[0]) {
1110 if (ldb_attr_cmp(attrs[0], "dn") == 0 && !attrs[1]) {
1111 attrs = talloc_array(dsc, const char*, 2);
1112 if (attrs == NULL) {
1113 return ldb_oom(ldb);
1120 * When returning all the attributes return also the SD as
1123 if (ldb_attr_in_list(attrs, "*")) {
1124 struct ldb_sd_flags_control *sdctr = talloc_zero(dsc, struct ldb_sd_flags_control);
1125 sdctr->secinfo_flags = 0xF;
1126 ret = ldb_request_add_control(req, LDB_CONTROL_SD_FLAGS_OID, false, sdctr);
1127 if (ret != LDB_SUCCESS) {
1130 attrs = ldb_attr_list_copy_add(dsc, attrs, "parentGUID");
1131 if (attrs == NULL) {
1132 return ldb_oom(ldb);
1134 attrs = ldb_attr_list_copy_add(dsc, attrs, "replPropertyMetaData");
1135 if (attrs == NULL) {
1136 return ldb_oom(ldb);
1139 * When no attributes are asked we in anycase expect at least 3 attributes:
1145 dsc->nbDefaultAttrs = 3;
1148 * We will need this two attributes in the callback
1150 attrs = ldb_attr_list_copy_add(dsc, attrs, "usnChanged");
1151 if (attrs == NULL) {
1152 return ldb_operr(ldb);
1154 attrs = ldb_attr_list_copy_add(dsc, attrs, "replPropertyMetaData");
1155 if (attrs == NULL) {
1156 return ldb_operr(ldb);
1159 if (!ldb_attr_in_list(attrs, "instanceType")) {
1160 attrs = ldb_attr_list_copy_add(dsc, attrs, "instanceType");
1161 if (attrs == NULL) {
1162 return ldb_operr(ldb);
1164 dsc->nbDefaultAttrs++;
1167 if (!ldb_attr_in_list(attrs, "objectGUID")) {
1168 attrs = ldb_attr_list_copy_add(dsc, attrs, "objectGUID");
1169 if (attrs == NULL) {
1170 return ldb_operr(ldb);
1174 * Always increment the number of asked attributes as we don't care if objectGUID was asked
1175 * or not for counting the number of "real" attributes returned.
1177 dsc->nbDefaultAttrs++;
1179 if (!ldb_attr_in_list(attrs, "parentGUID")) {
1180 attrs = ldb_attr_list_copy_add(dsc, attrs, "parentGUID");
1181 if (attrs == NULL) {
1182 return ldb_operr(ldb);
1185 dsc->nbDefaultAttrs++;
1189 struct ldb_sd_flags_control *sdctr = talloc_zero(dsc, struct ldb_sd_flags_control);
1190 sdctr->secinfo_flags = 0xF;
1191 ret = ldb_request_add_control(req, LDB_CONTROL_SD_FLAGS_OID, false, sdctr);
1192 attrs = talloc_array(dsc, const char*, 4);
1193 if (attrs == NULL) {
1194 return ldb_operr(ldb);
1197 attrs[1] = "parentGUID";
1198 attrs[2] = "replPropertyMetaData";
1200 if (ret != LDB_SUCCESS) {
1204 * When no attributes are asked we in anycase expect at least 3 attributes:
1210 dsc->nbDefaultAttrs = 3;
1213 if (!ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID)) {
1214 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, false, NULL);
1215 if (ret != LDB_SUCCESS) {
1218 dsc->noextended = true;
1221 if (ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) == NULL) {
1222 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
1223 if (ret != LDB_SUCCESS) {
1228 if (ldb_request_get_control(req, LDB_CONTROL_SHOW_RECYCLED_OID) == NULL) {
1229 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
1230 if (ret != LDB_SUCCESS) {
1235 if (ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID) == NULL) {
1236 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, false, NULL);
1237 if (ret != LDB_SUCCESS) {
1242 if (dirsync_ctl->flags & LDAP_DIRSYNC_INCREMENTAL_VALUES) {
1243 dsc->linkIncrVal = true;
1245 dsc->linkIncrVal = false;
1248 dsc->our_invocation_id = samdb_ntds_invocation_id(ldb);
1249 if (dsc->our_invocation_id == NULL) {
1250 return ldb_operr(ldb);
1253 if (dirsync_ctl->cookie_len > 0) {
1254 struct ldapControlDirSyncCookie cookie;
1256 blob.data = (uint8_t *)dirsync_ctl->cookie;
1257 blob.length = dirsync_ctl->cookie_len;
1258 ndr_err = ndr_pull_struct_blob(&blob, dsc, &cookie,
1259 (ndr_pull_flags_fn_t)ndr_pull_ldapControlDirSyncCookie);
1261 /* If we can't unmarshall the cookie into the correct structure we return
1262 * unsupported critical extension
1264 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1265 return ldb_error(ldb, LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION,
1266 "Unable to unmarshall cookie as a ldapControlDirSyncCookie structure");
1270 * Let's search for the max usn withing the cookie
1272 if (GUID_equal(&(cookie.blob.guid1), dsc->our_invocation_id)) {
1274 * Ok, it's our invocation ID so we can treat the demand
1275 * Let's take the highest usn from (tmp)highest_usn
1277 dsc->fromreqUSN = cookie.blob.highwatermark.tmp_highest_usn;
1278 dsc->localonly = true;
1280 if (cookie.blob.highwatermark.highest_usn > cookie.blob.highwatermark.tmp_highest_usn) {
1281 dsc->fromreqUSN = cookie.blob.highwatermark.highest_usn;
1284 dsc->localonly = false;
1286 if (cookie.blob.extra_length > 0 &&
1287 cookie.blob.extra.uptodateness_vector.ctr.ctr1.count > 0) {
1288 struct drsuapi_DsReplicaCursor cursor;
1290 for (p=0; p < cookie.blob.extra.uptodateness_vector.ctr.ctr1.count; p++) {
1291 cursor = cookie.blob.extra.uptodateness_vector.ctr.ctr1.cursors[p];
1292 if (GUID_equal( &(cursor.source_dsa_invocation_id), dsc->our_invocation_id)) {
1293 if (cursor.highest_usn > dsc->fromreqUSN) {
1294 dsc->fromreqUSN = cursor.highest_usn;
1298 dsc->cursors = talloc_steal(dsc,
1299 cookie.blob.extra.uptodateness_vector.ctr.ctr1.cursors);
1300 if (dsc->cursors == NULL) {
1301 return ldb_oom(ldb);
1303 dsc->cursor_size = p;
1307 DEBUG(4, ("Dirsync: searching with min usn > %llu\n",
1308 (long long unsigned int)dsc->fromreqUSN));
1309 if (dsc->fromreqUSN > 0) {
1310 /* FIXME it would be better to use PRId64 */
1311 char *expression = talloc_asprintf(dsc, "(&%s(uSNChanged>=%llu))",
1312 ldb_filter_from_tree(dsc,
1313 req->op.search.tree),
1314 (long long unsigned int)(dsc->fromreqUSN + 1));
1316 if (expression == NULL) {
1317 return ldb_oom(ldb);
1319 new_tree = ldb_parse_tree(req, expression);
1320 if (new_tree == NULL) {
1321 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1322 "Problem while parsing tree");
1327 * Remove our control from the list of controls
1329 if (!ldb_save_controls(control, req, NULL)) {
1330 return ldb_operr(ldb);
1332 dsc->schema = dsdb_get_schema(ldb, dsc);
1334 * At the begining we make the hypothesis that we will return a complete
1338 dsc->partial = false;
1341 * 3.1.1.3.4.1.3 of MS-ADTS.pdf specify that if the scope is not subtree
1342 * we treat the search as if subtree was specified
1345 ret = ldb_build_search_req_ex(&down_req, ldb, dsc,
1346 req->op.search.base,
1351 dsc, dirsync_search_callback,
1353 ldb_req_set_custom_flags(down_req, flags);
1354 LDB_REQ_SET_LOCATION(down_req);
1355 if (ret != LDB_SUCCESS) {
1358 /* perform the search */
1359 return ldb_next_request(module, down_req);
1362 static int dirsync_ldb_init(struct ldb_module *module)
1366 ret = ldb_mod_register_control(module, LDB_CONTROL_DIRSYNC_OID);
1367 if (ret != LDB_SUCCESS) {
1368 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
1369 "dirsync: Unable to register control with rootdse!\n");
1370 return ldb_operr(ldb_module_get_ctx(module));
1373 return ldb_next_init(module);
1376 static const struct ldb_module_ops ldb_dirsync_ldb_module_ops = {
1378 .search = dirsync_ldb_search,
1379 .init_context = dirsync_ldb_init,
1383 initialise the module
1385 _PUBLIC_ int ldb_dirsync_module_init(const char *version)
1388 LDB_MODULE_CHECK_VERSION(version);
1389 ret = ldb_register_module(&ldb_dirsync_ldb_module_ops);