2 Unix SMB/CIFS implementation.
4 implement the DRSUpdateRefs call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
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 #include "librpc/gen_ndr/ndr_drsuapi.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "auth/auth.h"
30 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
31 #include "rpc_server/dcerpc_server_proto.h"
32 #include "../libcli/drsuapi/drsuapi.h"
33 #include "libcli/security/security.h"
36 drsuapi_DsGetNCChanges for one object
38 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
39 struct ldb_message *msg,
40 struct ldb_context *sam_ctx,
41 struct ldb_dn *ncRoot_dn,
42 struct dsdb_schema *schema,
43 DATA_BLOB *session_key,
45 uint32_t replica_flags)
47 const struct ldb_val *md_value;
49 struct ldb_dn *obj_dn;
50 struct replPropertyMetaDataBlob md;
53 enum ndr_err_code ndr_err;
56 const struct dsdb_attribute *rdn_sa;
58 if (ldb_dn_compare(ncRoot_dn, msg->dn) == 0) {
59 obj->is_nc_prefix = true;
60 obj->parent_object_guid = NULL;
62 struct ldb_dn *parent_dn;
63 uint32_t instance_type;
65 instance_type = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
66 if (instance_type & INSTANCE_TYPE_IS_NC_HEAD) {
67 struct ldb_result *res;
69 const char *dnstr = ldb_dn_get_linearized(msg->dn);
70 msg->dn = ldb_dn_new(msg, sam_ctx, dnstr);
71 /* we need to re-search the msg, to avoid the
72 * broken dual message problems with our
73 * partitions implementation */
74 DEBUG(6,(__location__ ": Re-fetching subref %s\n",
75 ldb_dn_get_linearized(msg->dn)));
76 ret = drsuapi_search_with_extended_dn(sam_ctx, msg, &res,
77 msg->dn, LDB_SCOPE_BASE, NULL,
79 if (ret != LDB_SUCCESS || res->count < 1) {
80 DEBUG(0,(__location__ ": Failed to reload subref head %s in %s\n",
81 ldb_dn_get_linearized(msg->dn), ldb_dn_get_linearized(ncRoot_dn)));
82 return WERR_DS_DRA_INTERNAL_ERROR;
87 parent_dn = ldb_dn_copy(msg, msg->dn);
88 obj->is_nc_prefix = false;
89 obj->parent_object_guid = talloc(obj, struct GUID);
90 if (parent_dn == NULL) {
91 return WERR_DS_DRA_INTERNAL_ERROR;
93 if (ldb_dn_remove_child_components(parent_dn, 1) != true) {
94 DEBUG(0,(__location__ ": Unable to remove DN component\n"));
95 return WERR_DS_DRA_INTERNAL_ERROR;
97 if (dsdb_find_guid_by_dn(sam_ctx, parent_dn, obj->parent_object_guid) != LDB_SUCCESS) {
98 DEBUG(0,(__location__ ": Unable to find parent DN %s %s\n",
99 ldb_dn_get_linearized(msg->dn), ldb_dn_get_linearized(parent_dn)));
101 talloc_free(parent_dn);
103 obj->next_object = NULL;
105 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
107 /* nothing to send */
111 ndr_err = ndr_pull_struct_blob(md_value, obj,
112 lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")), &md,
113 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
114 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
115 return WERR_DS_DRA_INTERNAL_ERROR;
118 if (md.version != 1) {
119 return WERR_DS_DRA_INTERNAL_ERROR;
122 rdn = ldb_dn_get_rdn_name(msg->dn);
124 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
125 return WERR_DS_DRA_INTERNAL_ERROR;
128 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
129 if (rdn_sa == NULL) {
130 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
131 rdn, ldb_dn_get_linearized(msg->dn)));
132 return WERR_DS_DRA_INTERNAL_ERROR;
135 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
136 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
138 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
139 for (n=i=0; i<md.ctr.ctr1.count; i++) {
140 /* if the attribute has not changed, and it is not the
141 instanceType then don't include it */
142 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
143 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType) continue;
144 /* don't include the rDN */
145 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
146 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
147 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
148 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
149 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
150 attids[n] = md.ctr.ctr1.array[i].attid;
155 note that if n==0 we still need to send the change, as it
156 could be a rename, which changes the uSNChanged, but not any
157 of the replicated attributes
160 obj->meta_data_ctr->count = n;
162 obj->object.identifier = talloc(obj, struct drsuapi_DsReplicaObjectIdentifier);
163 obj_dn = ldb_msg_find_attr_as_dn(sam_ctx, obj, msg, "distinguishedName");
164 obj->object.identifier->dn = ldb_dn_get_linearized(obj_dn);
165 obj->object.identifier->guid = samdb_result_guid(msg, "objectGUID");
166 sid = samdb_result_dom_sid(obj, msg, "objectSid");
168 dom_sid_split_rid(NULL, sid, NULL, &rid);
169 obj->object.identifier->sid = *sid;
171 ZERO_STRUCT(obj->object.identifier->sid);
174 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
175 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
176 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
177 obj->object.attribute_ctr.num_attributes);
180 * Note that the meta_data array and the attributes array must
181 * be the same size and in the same order
183 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
184 struct ldb_message_element *el;
186 const struct dsdb_attribute *sa;
188 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
190 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
191 return WERR_DS_DRA_INTERNAL_ERROR;
194 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
196 DEBUG(0,("No element '%s' for attributeID %u in message\n",
197 sa->lDAPDisplayName, attids[i]));
198 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
199 obj->object.attribute_ctr.attributes[i].attid = attids[i];
201 werr = dsdb_attribute_ldb_to_drsuapi(sam_ctx, schema, el, obj,
202 &obj->object.attribute_ctr.attributes[i]);
203 if (!W_ERROR_IS_OK(werr)) {
204 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
205 sa->lDAPDisplayName, win_errstr(werr)));
208 /* if DRSUAPI_DS_REPLICA_NEIGHBOUR_SPECIAL_SECRET_PROCESSING is set
209 * check if attribute is secret and send a null value
210 * TODO: check if we can make this in the database layer
212 if ((replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_SPECIAL_SECRET_PROCESSING)
213 == DRSUAPI_DS_REPLICA_NEIGHBOUR_SPECIAL_SECRET_PROCESSING) {
214 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
215 &obj->meta_data_ctr->meta_data[i]);
217 /* some attributes needs to be encrypted
219 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
220 &obj->object.attribute_ctr.attributes[i]);
221 if (!W_ERROR_IS_OK(werr)) {
222 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
223 sa->lDAPDisplayName, win_errstr(werr)));
233 load replUpToDateVector from a DN
235 static WERROR load_udv(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
236 struct ldb_dn *dn, struct replUpToDateVectorBlob *ouv)
238 const char *attrs[] = { "replUpToDateVector", NULL };
239 struct ldb_result *res = NULL;
240 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
241 struct ldb_message_element *el;
242 enum ndr_err_code ndr_err;
246 if (ldb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, NULL) != LDB_SUCCESS ||
248 DEBUG(0,("load_udv: failed to read partition object\n"));
249 talloc_free(tmp_ctx);
250 return WERR_DS_DRA_INTERNAL_ERROR;
253 el = ldb_msg_find_element(res->msgs[0], "replUpToDateVector");
254 if (el == NULL || el->num_values < 1) {
255 talloc_free(tmp_ctx);
260 ndr_err = ndr_pull_struct_blob(&el->values[0],
261 mem_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
263 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
264 talloc_free(tmp_ctx);
265 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
266 DEBUG(0,(__location__ ": Failed to parse replUpToDateVector for %s\n",
267 ldb_dn_get_linearized(dn)));
268 return WERR_DS_DRA_INTERNAL_ERROR;
276 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
278 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
279 struct ldb_dn *ncRoot_dn,
280 struct drsuapi_DsReplicaCursor2CtrEx *udv)
283 struct drsuapi_DsReplicaCursor2 *tmp_cursor;
284 uint64_t highest_commited_usn;
286 time_t t = time(NULL);
288 struct replUpToDateVectorBlob ouv;
290 werr = load_udv(sam_ctx, udv, ncRoot_dn, &ouv);
291 if (!W_ERROR_IS_OK(werr)) {
295 ret = ldb_sequence_number(sam_ctx, LDB_SEQ_HIGHEST_SEQ, &highest_commited_usn);
296 if (ret != LDB_SUCCESS) {
297 return WERR_DS_DRA_INTERNAL_ERROR;
300 tmp_cursor = talloc(udv, struct drsuapi_DsReplicaCursor2);
301 tmp_cursor->source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
302 tmp_cursor->highest_usn = highest_commited_usn;
303 unix_to_nt_time(&now, t);
304 tmp_cursor->last_sync_success = now;
306 udv->count = ouv.ctr.ctr2.count + 1;
307 udv->cursors = talloc_steal(udv, ouv.ctr.ctr2.cursors);
308 udv->cursors = talloc_realloc(udv, udv->cursors, struct drsuapi_DsReplicaCursor2, udv->count);
310 return WERR_DS_DRA_INTERNAL_ERROR;
312 udv->cursors[udv->count - 1] = *tmp_cursor;
314 qsort(udv->cursors, udv->count,
315 sizeof(struct drsuapi_DsReplicaCursor2),
316 (comparison_fn_t)drsuapi_DsReplicaCursor2_compare);
321 /* state of a partially completed getncchanges call */
322 struct drsuapi_getncchanges_state {
323 struct ldb_result *site_res;
325 struct ldb_dn *ncRoot_dn;
330 drsuapi_DsGetNCChanges
332 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
333 struct drsuapi_DsGetNCChanges *r)
335 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
338 struct dsdb_schema *schema;
339 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
340 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
342 DATA_BLOB session_key;
343 const char *attrs[] = { "*", "distinguishedName", NULL };
345 struct dcesrv_handle *h;
346 struct drsuapi_bind_state *b_state;
347 struct drsuapi_getncchanges_state *getnc_state;
349 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
352 *r->out.level_out = 6;
353 /* TODO: linked attributes*/
354 r->out.ctr->ctr6.linked_attributes_count = 0;
355 r->out.ctr->ctr6.linked_attributes = NULL;
357 r->out.ctr->ctr6.object_count = 0;
358 r->out.ctr->ctr6.nc_object_count = 0;
359 r->out.ctr->ctr6.more_data = false;
360 r->out.ctr->ctr6.uptodateness_vector = NULL;
362 /* Check request revision. */
363 if (r->in.level != 8) {
364 return WERR_REVISION_MISMATCH;
367 /* Perform access checks. */
368 if (r->in.req->req8.naming_context == NULL) {
369 return WERR_DS_DRA_INVALID_PARAMETER;
372 ncRoot = r->in.req->req8.naming_context;
373 if (ncRoot == NULL) {
374 return WERR_DS_DRA_BAD_NC;
377 if ((r->in.req->req8.replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_FULL_SYNC_PACKET)
378 == DRSUAPI_DS_REPLICA_NEIGHBOUR_FULL_SYNC_PACKET) {
379 /* Ignore the _in_ uptpdateness vector*/
380 r->in.req->req8.uptodateness_vector = NULL;
383 werr = drs_security_level_check(dce_call, "DsGetNCChanges");
384 if (!W_ERROR_IS_OK(werr)) {
388 getnc_state = b_state->getncchanges_state;
389 if (getnc_state == NULL) {
390 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
391 if (getnc_state == NULL) {
394 b_state->getncchanges_state = getnc_state;
397 /* we need the session key for encrypting password attributes */
398 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
399 if (!NT_STATUS_IS_OK(status)) {
400 DEBUG(0,(__location__ ": Failed to get session key\n"));
401 return WERR_DS_DRA_INTERNAL_ERROR;
404 if (getnc_state->site_res == NULL) {
406 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
408 getnc_state->min_usn = r->in.req->req8.highwatermark.highest_usn;
410 /* Construct response. */
411 search_filter = talloc_asprintf(mem_ctx,
412 "(uSNChanged>=%llu)",
413 (unsigned long long)(getnc_state->min_usn+1));
415 if (r->in.req->req8.replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_CRITICAL_ONLY) {
416 search_filter = talloc_asprintf(mem_ctx,
417 "(&%s(isCriticalSystemObject=TRUE))",
421 getnc_state->ncRoot_dn = ldb_dn_new(getnc_state, b_state->sam_ctx, ncRoot->dn);
422 if (r->in.req->req8.replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_ASYNC_REP) {
423 scope = LDB_SCOPE_BASE;
426 DEBUG(6,(__location__ ": getncchanges on %s using filter %s\n",
427 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
428 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, &getnc_state->site_res,
429 getnc_state->ncRoot_dn, scope, attrs,
432 if (ret != LDB_SUCCESS) {
433 return WERR_DS_DRA_INTERNAL_ERROR;
438 schema = dsdb_get_schema(b_state->sam_ctx);
440 DEBUG(0,("No schema in sam_ctx\n"));
441 return WERR_DS_DRA_INTERNAL_ERROR;
444 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
445 *r->out.ctr->ctr6.naming_context = *ncRoot;
447 if (dsdb_find_guid_by_dn(b_state->sam_ctx, getnc_state->ncRoot_dn,
448 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
449 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
450 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
451 return WERR_DS_DRA_INTERNAL_ERROR;
454 /* find the SID if there is one */
455 dsdb_find_sid_by_dn(b_state->sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
457 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
458 r->out.ctr->ctr6.mapping_ctr = *ctr;
460 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(b_state->sam_ctx));
461 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(b_state->sam_ctx));
463 r->out.ctr->ctr6.old_highwatermark = r->in.req->req8.highwatermark;
464 r->out.ctr->ctr6.new_highwatermark = r->in.req->req8.highwatermark;
466 r->out.ctr->ctr6.first_object = NULL;
467 currentObject = &r->out.ctr->ctr6.first_object;
469 for(i=getnc_state->num_sent;
470 i<getnc_state->site_res->count &&
471 (r->out.ctr->ctr6.object_count < r->in.req->req8.max_object_count);
474 struct drsuapi_DsReplicaObjectListItemEx *obj;
475 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
477 uSN = ldb_msg_find_attr_as_int(getnc_state->site_res->msgs[i], "uSNChanged", -1);
478 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
479 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
482 werr = get_nc_changes_build_object(obj, getnc_state->site_res->msgs[i],
483 b_state->sam_ctx, getnc_state->ncRoot_dn,
484 schema, &session_key, getnc_state->min_usn,
485 r->in.req->req8.replica_flags);
486 if (!W_ERROR_IS_OK(werr)) {
490 if (obj->meta_data_ctr == NULL) {
491 DEBUG(0,(__location__ ": getncchanges skipping send of object %s\n",
492 ldb_dn_get_linearized(getnc_state->site_res->msgs[i]->dn)));
493 /* no attributes to send */
498 r->out.ctr->ctr6.object_count++;
500 *currentObject = obj;
501 currentObject = &obj->next_object;
504 getnc_state->num_sent += r->out.ctr->ctr6.object_count;
506 r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
508 if (i < getnc_state->site_res->count) {
509 r->out.ctr->ctr6.more_data = true;
511 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
512 r->out.ctr->ctr6.uptodateness_vector->version = 2;
513 r->out.ctr->ctr6.uptodateness_vector->reserved1 = 0;
514 r->out.ctr->ctr6.uptodateness_vector->reserved2 = 0;
516 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
518 werr = get_nc_changes_udv(b_state->sam_ctx, getnc_state->ncRoot_dn,
519 r->out.ctr->ctr6.uptodateness_vector);
520 if (!W_ERROR_IS_OK(werr)) {
524 talloc_free(getnc_state);
525 b_state->getncchanges_state = NULL;
528 DEBUG(3,("DsGetNCChanges with uSNChanged >= %llu on %s gave %u objects\n",
529 (unsigned long long)(r->in.req->req8.highwatermark.highest_usn+1),
530 ncRoot->dn, r->out.ctr->ctr6.object_count));