2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan (metze) Metzmacher 2005
5 Copyright (C) Guenther Deschner 2008
6 Copyright (C) Michael Adam 2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libnet/libnet_dssync.h"
25 #include "../libcli/drsuapi/drsuapi.h"
26 #include "../librpc/gen_ndr/ndr_drsuapi_c.h"
28 /****************************************************************
29 ****************************************************************/
31 static int libnet_dssync_free_context(struct dssync_context *ctx)
34 struct dcerpc_binding_handle *b;
40 if (is_valid_policy_hnd(&ctx->bind_handle) && ctx->cli) {
41 b = ctx->cli->binding_handle;
42 dcerpc_drsuapi_DsUnbind(b, ctx, &ctx->bind_handle, &result);
48 /****************************************************************
49 ****************************************************************/
51 NTSTATUS libnet_dssync_init_context(TALLOC_CTX *mem_ctx,
52 struct dssync_context **ctx_p)
54 struct dssync_context *ctx;
56 ctx = TALLOC_ZERO_P(mem_ctx, struct dssync_context);
57 NT_STATUS_HAVE_NO_MEMORY(ctx);
59 talloc_set_destructor(ctx, libnet_dssync_free_context);
60 ctx->clean_old_entries = false;
67 /****************************************************************
68 ****************************************************************/
70 static void parse_obj_identifier(struct drsuapi_DsReplicaObjectIdentifier *id,
79 if (id->sid.num_auths > 0) {
80 *rid = id->sid.sub_auths[id->sid.num_auths - 1];
84 /****************************************************************
85 ****************************************************************/
87 static void libnet_dssync_decrypt_attributes(TALLOC_CTX *mem_ctx,
88 DATA_BLOB *session_key,
89 struct drsuapi_DsReplicaObjectListItemEx *cur)
91 for (; cur; cur = cur->next_object) {
96 parse_obj_identifier(cur->object.identifier, &rid);
98 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
100 struct drsuapi_DsReplicaAttribute *attr;
102 attr = &cur->object.attribute_ctr.attributes[i];
104 if (attr->value_ctr.num_values < 1) {
108 if (!attr->value_ctr.values[0].blob) {
112 drsuapi_decrypt_attribute(mem_ctx,
119 /****************************************************************
120 ****************************************************************/
122 static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
123 struct dssync_context *ctx)
128 struct GUID bind_guid;
129 struct drsuapi_DsBindInfoCtr bind_info;
130 struct drsuapi_DsBindInfo28 info28;
131 struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
135 GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
137 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
138 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
139 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
140 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
141 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
142 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
143 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
144 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
145 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
146 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
147 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
148 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
149 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
150 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
151 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
152 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
153 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
154 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
155 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
156 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
157 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
158 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
159 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
160 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
161 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
162 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
163 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
164 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
165 info28.site_guid = GUID_zero();
167 info28.repl_epoch = 0;
169 bind_info.length = 28;
170 bind_info.info.info28 = info28;
172 status = dcerpc_drsuapi_DsBind(b, mem_ctx,
178 if (!NT_STATUS_IS_OK(status)) {
182 if (!W_ERROR_IS_OK(werr)) {
183 return werror_to_ntstatus(werr);
186 ZERO_STRUCT(ctx->remote_info28);
187 switch (bind_info.length) {
189 struct drsuapi_DsBindInfo24 *info24;
190 info24 = &bind_info.info.info24;
191 ctx->remote_info28.site_guid = info24->site_guid;
192 ctx->remote_info28.supported_extensions = info24->supported_extensions;
193 ctx->remote_info28.pid = info24->pid;
194 ctx->remote_info28.repl_epoch = 0;
198 ctx->remote_info28 = bind_info.info.info28;
201 struct drsuapi_DsBindInfo48 *info48;
202 info48 = &bind_info.info.info48;
203 ctx->remote_info28.site_guid = info48->site_guid;
204 ctx->remote_info28.supported_extensions = info48->supported_extensions;
205 ctx->remote_info28.pid = info48->pid;
206 ctx->remote_info28.repl_epoch = info48->repl_epoch;
210 DEBUG(1, ("Warning: invalid info length in bind info: %d\n",
218 /****************************************************************
219 ****************************************************************/
221 static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
222 struct dssync_context *ctx)
227 union drsuapi_DsNameRequest req;
229 struct drsuapi_DsNameString names[1];
230 union drsuapi_DsNameCtr ctr;
231 struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
233 names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
234 NT_STATUS_HAVE_NO_MEMORY(names[0].str);
236 req.req1.codepage = 1252; /* german */
237 req.req1.language = 0x00000407; /* german */
239 req.req1.names = names;
240 req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
241 req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
242 req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
244 status = dcerpc_drsuapi_DsCrackNames(b, mem_ctx,
251 if (!NT_STATUS_IS_OK(status)) {
252 ctx->error_message = talloc_asprintf(ctx,
253 "Failed to lookup DN for domain name: %s",
254 get_friendly_nt_error_msg(status));
258 if (!W_ERROR_IS_OK(werr)) {
259 ctx->error_message = talloc_asprintf(ctx,
260 "Failed to lookup DN for domain name: %s",
261 get_friendly_werror_msg(werr));
262 return werror_to_ntstatus(werr);
265 if (ctr.ctr1->count != 1) {
266 return NT_STATUS_UNSUCCESSFUL;
269 if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
270 return NT_STATUS_UNSUCCESSFUL;
273 ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
274 NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
276 if (!ctx->dns_domain_name) {
277 ctx->dns_domain_name = talloc_strdup_upper(mem_ctx,
278 ctr.ctr1->array[0].dns_domain_name);
279 NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
285 /****************************************************************
286 ****************************************************************/
288 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
289 struct dssync_context *ctx)
293 status = libnet_dssync_bind(mem_ctx, ctx);
294 if (!NT_STATUS_IS_OK(status)) {
299 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
305 /****************************************************************
306 ****************************************************************/
308 static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
309 struct dssync_context *ctx,
311 struct replUpToDateVectorBlob *utdv,
313 union drsuapi_DsGetNCChangesRequest *preq)
318 union drsuapi_DsGetNCChangesRequest req;
319 struct dom_sid null_sid;
320 enum drsuapi_DsExtendedOperation extended_op;
321 struct drsuapi_DsReplicaObjectIdentifier *nc = NULL;
322 struct drsuapi_DsReplicaCursorCtrEx *cursors = NULL;
324 uint32_t replica_flags = DRSUAPI_DRS_WRIT_REP |
325 DRSUAPI_DRS_INIT_SYNC |
326 DRSUAPI_DRS_PER_SYNC |
327 DRSUAPI_DRS_GET_ANC |
328 DRSUAPI_DRS_NEVER_SYNCED;
330 ZERO_STRUCT(null_sid);
333 if (ctx->remote_info28.supported_extensions
334 & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
341 nc = TALLOC_ZERO_P(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
343 status = NT_STATUS_NO_MEMORY;
347 nc->guid = GUID_zero();
350 if (!ctx->single_object_replication &&
351 !ctx->force_full_replication && utdv)
353 cursors = TALLOC_ZERO_P(mem_ctx,
354 struct drsuapi_DsReplicaCursorCtrEx);
356 status = NT_STATUS_NO_MEMORY;
360 switch (utdv->version) {
362 cursors->count = utdv->ctr.ctr1.count;
363 cursors->cursors = utdv->ctr.ctr1.cursors;
366 cursors->count = utdv->ctr.ctr2.count;
367 cursors->cursors = talloc_array(cursors,
368 struct drsuapi_DsReplicaCursor,
370 if (!cursors->cursors) {
371 status = NT_STATUS_NO_MEMORY;
374 for (count = 0; count < cursors->count; count++) {
375 cursors->cursors[count].source_dsa_invocation_id =
376 utdv->ctr.ctr2.cursors[count].source_dsa_invocation_id;
377 cursors->cursors[count].highest_usn =
378 utdv->ctr.ctr2.cursors[count].highest_usn;
384 if (ctx->single_object_replication) {
385 extended_op = DRSUAPI_EXOP_REPL_OBJ;
387 extended_op = DRSUAPI_EXOP_NONE;
391 req.req8.naming_context = nc;
392 req.req8.replica_flags = replica_flags;
393 req.req8.max_object_count = 402;
394 req.req8.max_ndr_size = 402116;
395 req.req8.uptodateness_vector = cursors;
396 req.req8.extended_op = extended_op;
397 } else if (level == 5) {
398 req.req5.naming_context = nc;
399 req.req5.replica_flags = replica_flags;
400 req.req5.max_object_count = 402;
401 req.req5.max_ndr_size = 402116;
402 req.req5.uptodateness_vector = cursors;
403 req.req5.extended_op = extended_op;
405 status = NT_STATUS_INVALID_PARAMETER;
421 TALLOC_FREE(cursors);
425 static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
426 struct dssync_context *ctx,
428 union drsuapi_DsGetNCChangesRequest *req,
429 struct replUpToDateVectorBlob **pnew_utdv)
433 union drsuapi_DsGetNCChangesCtr ctr;
434 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
435 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
436 struct replUpToDateVectorBlob *new_utdv = NULL;
437 uint32_t level_out = 0;
438 uint32_t out_level = 0;
441 struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
443 if (!ctx->single_object_replication) {
444 new_utdv = TALLOC_ZERO_P(mem_ctx, struct replUpToDateVectorBlob);
446 status = NT_STATUS_NO_MEMORY;
451 for (y=0, last_query = false; !last_query; y++) {
452 struct drsuapi_DsReplicaObjectListItemEx *first_object = NULL;
453 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr = NULL;
454 uint32_t linked_attributes_count = 0;
455 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes = NULL;
458 DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
459 (long long)req->req8.highwatermark.tmp_highest_usn,
460 (long long)req->req8.highwatermark.highest_usn));
461 } else if (level == 5) {
462 DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
463 (long long)req->req5.highwatermark.tmp_highest_usn,
464 (long long)req->req5.highwatermark.highest_usn));
467 status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
474 if (!NT_STATUS_IS_OK(status)) {
475 ctx->error_message = talloc_asprintf(ctx,
476 "Failed to get NC Changes: %s",
477 get_friendly_nt_error_msg(status));
481 if (!W_ERROR_IS_OK(werr)) {
482 status = werror_to_ntstatus(werr);
483 ctx->error_message = talloc_asprintf(ctx,
484 "Failed to get NC Changes: %s",
485 get_friendly_werror_msg(werr));
489 if (level_out == 1) {
492 } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
494 ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
495 } else if (level_out == 6) {
498 } else if (level_out == 7
499 && ctr.ctr7.level == 6
500 && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
501 && ctr.ctr7.ctr.mszip6.ts) {
503 ctr6 = &ctr.ctr7.ctr.mszip6.ts->ctr6;
504 } else if (level_out == 7
505 && ctr.ctr7.level == 6
506 && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
507 && ctr.ctr7.ctr.xpress6.ts) {
509 ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
512 if (out_level == 1) {
513 DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
514 (long long)ctr1->new_highwatermark.tmp_highest_usn,
515 (long long)ctr1->new_highwatermark.highest_usn));
517 first_object = ctr1->first_object;
518 mapping_ctr = &ctr1->mapping_ctr;
520 if (ctr1->more_data) {
521 req->req5.highwatermark = ctr1->new_highwatermark;
524 if (ctr1->uptodateness_vector &&
525 !ctx->single_object_replication)
527 new_utdv->version = 1;
528 new_utdv->ctr.ctr1.count =
529 ctr1->uptodateness_vector->count;
530 new_utdv->ctr.ctr1.cursors =
531 ctr1->uptodateness_vector->cursors;
534 } else if (out_level == 6) {
535 DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
536 (long long)ctr6->new_highwatermark.tmp_highest_usn,
537 (long long)ctr6->new_highwatermark.highest_usn));
539 first_object = ctr6->first_object;
540 mapping_ctr = &ctr6->mapping_ctr;
542 linked_attributes = ctr6->linked_attributes;
543 linked_attributes_count = ctr6->linked_attributes_count;
545 if (ctr6->more_data) {
546 req->req8.highwatermark = ctr6->new_highwatermark;
549 if (ctr6->uptodateness_vector &&
550 !ctx->single_object_replication)
552 new_utdv->version = 2;
553 new_utdv->ctr.ctr2.count =
554 ctr6->uptodateness_vector->count;
555 new_utdv->ctr.ctr2.cursors =
556 ctr6->uptodateness_vector->cursors;
561 status = cli_get_session_key(mem_ctx, ctx->cli, &ctx->session_key);
562 if (!NT_STATUS_IS_OK(status)) {
563 ctx->error_message = talloc_asprintf(ctx,
564 "Failed to get Session Key: %s",
569 libnet_dssync_decrypt_attributes(mem_ctx,
573 if (ctx->ops->process_objects) {
574 status = ctx->ops->process_objects(ctx, mem_ctx,
577 if (!NT_STATUS_IS_OK(status)) {
578 ctx->error_message = talloc_asprintf(ctx,
579 "Failed to call processing function: %s",
585 if (linked_attributes_count == 0) {
589 if (ctx->ops->process_links) {
590 status = ctx->ops->process_links(ctx, mem_ctx,
591 linked_attributes_count,
594 if (!NT_STATUS_IS_OK(status)) {
595 ctx->error_message = talloc_asprintf(ctx,
596 "Failed to call processing function: %s",
603 *pnew_utdv = new_utdv;
609 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
610 struct dssync_context *ctx)
615 union drsuapi_DsGetNCChangesRequest req;
616 struct replUpToDateVectorBlob *old_utdv = NULL;
617 struct replUpToDateVectorBlob *pnew_utdv = NULL;
622 if (ctx->ops->startup) {
623 status = ctx->ops->startup(ctx, mem_ctx, &old_utdv);
624 if (!NT_STATUS_IS_OK(status)) {
625 ctx->error_message = talloc_asprintf(ctx,
626 "Failed to call startup operation: %s",
632 if (ctx->single_object_replication && ctx->object_dns) {
633 dns = ctx->object_dns;
634 dn_count = ctx->object_count;
640 status = NT_STATUS_OK;
642 for (count=0; count < dn_count; count++) {
643 status = libnet_dssync_build_request(mem_ctx, ctx,
647 if (!NT_STATUS_IS_OK(status)) {
651 status = libnet_dssync_getncchanges(mem_ctx, ctx, level, &req,
653 if (!NT_STATUS_IS_OK(status)) {
654 ctx->error_message = talloc_asprintf(ctx,
655 "Failed to call DsGetNCCHanges: %s",
661 if (ctx->ops->finish) {
662 status = ctx->ops->finish(ctx, mem_ctx, pnew_utdv);
663 if (!NT_STATUS_IS_OK(status)) {
664 ctx->error_message = talloc_asprintf(ctx,
665 "Failed to call finishing operation: %s",
675 /****************************************************************
676 ****************************************************************/
678 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
679 struct dssync_context *ctx)
684 tmp_ctx = talloc_new(mem_ctx);
686 return NT_STATUS_NO_MEMORY;
689 status = libnet_dssync_init(tmp_ctx, ctx);
690 if (!NT_STATUS_IS_OK(status)) {
694 status = libnet_dssync_process(tmp_ctx, ctx);
695 if (!NT_STATUS_IS_OK(status)) {
700 TALLOC_FREE(tmp_ctx);