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_werror_msg(werr));
258 if (!W_ERROR_IS_OK(werr)) {
259 return werror_to_ntstatus(werr);
262 if (ctr.ctr1->count != 1) {
263 return NT_STATUS_UNSUCCESSFUL;
266 if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
267 return NT_STATUS_UNSUCCESSFUL;
270 ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
271 NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
273 if (!ctx->dns_domain_name) {
274 ctx->dns_domain_name = talloc_strdup_upper(mem_ctx,
275 ctr.ctr1->array[0].dns_domain_name);
276 NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
282 /****************************************************************
283 ****************************************************************/
285 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
286 struct dssync_context *ctx)
290 status = libnet_dssync_bind(mem_ctx, ctx);
291 if (!NT_STATUS_IS_OK(status)) {
296 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
302 /****************************************************************
303 ****************************************************************/
305 static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
306 struct dssync_context *ctx,
308 struct replUpToDateVectorBlob *utdv,
310 union drsuapi_DsGetNCChangesRequest *preq)
315 union drsuapi_DsGetNCChangesRequest req;
316 struct dom_sid null_sid;
317 enum drsuapi_DsExtendedOperation extended_op;
318 struct drsuapi_DsReplicaObjectIdentifier *nc = NULL;
319 struct drsuapi_DsReplicaCursorCtrEx *cursors = NULL;
321 uint32_t replica_flags = DRSUAPI_DRS_WRIT_REP |
322 DRSUAPI_DRS_INIT_SYNC |
323 DRSUAPI_DRS_PER_SYNC |
324 DRSUAPI_DRS_GET_ANC |
325 DRSUAPI_DRS_NEVER_SYNCED;
327 ZERO_STRUCT(null_sid);
330 if (ctx->remote_info28.supported_extensions
331 & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
338 nc = TALLOC_ZERO_P(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
340 status = NT_STATUS_NO_MEMORY;
344 nc->guid = GUID_zero();
347 if (!ctx->single_object_replication &&
348 !ctx->force_full_replication && utdv)
350 cursors = TALLOC_ZERO_P(mem_ctx,
351 struct drsuapi_DsReplicaCursorCtrEx);
353 status = NT_STATUS_NO_MEMORY;
357 switch (utdv->version) {
359 cursors->count = utdv->ctr.ctr1.count;
360 cursors->cursors = utdv->ctr.ctr1.cursors;
363 cursors->count = utdv->ctr.ctr2.count;
364 cursors->cursors = talloc_array(cursors,
365 struct drsuapi_DsReplicaCursor,
367 if (!cursors->cursors) {
368 status = NT_STATUS_NO_MEMORY;
371 for (count = 0; count < cursors->count; count++) {
372 cursors->cursors[count].source_dsa_invocation_id =
373 utdv->ctr.ctr2.cursors[count].source_dsa_invocation_id;
374 cursors->cursors[count].highest_usn =
375 utdv->ctr.ctr2.cursors[count].highest_usn;
381 if (ctx->single_object_replication) {
382 extended_op = DRSUAPI_EXOP_REPL_OBJ;
384 extended_op = DRSUAPI_EXOP_NONE;
388 req.req8.naming_context = nc;
389 req.req8.replica_flags = replica_flags;
390 req.req8.max_object_count = 402;
391 req.req8.max_ndr_size = 402116;
392 req.req8.uptodateness_vector = cursors;
393 req.req8.extended_op = extended_op;
394 } else if (level == 5) {
395 req.req5.naming_context = nc;
396 req.req5.replica_flags = replica_flags;
397 req.req5.max_object_count = 402;
398 req.req5.max_ndr_size = 402116;
399 req.req5.uptodateness_vector = cursors;
400 req.req5.extended_op = extended_op;
402 status = NT_STATUS_INVALID_PARAMETER;
418 TALLOC_FREE(cursors);
422 static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
423 struct dssync_context *ctx,
425 union drsuapi_DsGetNCChangesRequest *req,
426 struct replUpToDateVectorBlob **pnew_utdv)
430 union drsuapi_DsGetNCChangesCtr ctr;
431 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
432 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
433 struct replUpToDateVectorBlob *new_utdv = NULL;
434 uint32_t level_out = 0;
435 uint32_t out_level = 0;
438 struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
440 if (!ctx->single_object_replication) {
441 new_utdv = TALLOC_ZERO_P(mem_ctx, struct replUpToDateVectorBlob);
443 status = NT_STATUS_NO_MEMORY;
448 for (y=0, last_query = false; !last_query; y++) {
449 struct drsuapi_DsReplicaObjectListItemEx *first_object = NULL;
450 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr = NULL;
453 DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
454 (long long)req->req8.highwatermark.tmp_highest_usn,
455 (long long)req->req8.highwatermark.highest_usn));
456 } else if (level == 5) {
457 DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
458 (long long)req->req5.highwatermark.tmp_highest_usn,
459 (long long)req->req5.highwatermark.highest_usn));
462 status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
469 if (!NT_STATUS_IS_OK(status)) {
470 ctx->error_message = talloc_asprintf(ctx,
471 "Failed to get NC Changes: %s",
472 get_friendly_werror_msg(werr));
476 if (!W_ERROR_IS_OK(werr)) {
477 status = werror_to_ntstatus(werr);
478 ctx->error_message = talloc_asprintf(ctx,
479 "Failed to get NC Changes: %s",
480 get_friendly_werror_msg(werr));
484 if (level_out == 1) {
487 } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
489 ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
490 } else if (level_out == 6) {
493 } else if (level_out == 7
494 && ctr.ctr7.level == 6
495 && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
496 && ctr.ctr7.ctr.mszip6.ts) {
498 ctr6 = &ctr.ctr7.ctr.mszip6.ts->ctr6;
499 } else if (level_out == 7
500 && ctr.ctr7.level == 6
501 && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
502 && ctr.ctr7.ctr.xpress6.ts) {
504 ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
507 if (out_level == 1) {
508 DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
509 (long long)ctr1->new_highwatermark.tmp_highest_usn,
510 (long long)ctr1->new_highwatermark.highest_usn));
512 first_object = ctr1->first_object;
513 mapping_ctr = &ctr1->mapping_ctr;
515 if (ctr1->more_data) {
516 req->req5.highwatermark = ctr1->new_highwatermark;
519 if (ctr1->uptodateness_vector &&
520 !ctx->single_object_replication)
522 new_utdv->version = 1;
523 new_utdv->ctr.ctr1.count =
524 ctr1->uptodateness_vector->count;
525 new_utdv->ctr.ctr1.cursors =
526 ctr1->uptodateness_vector->cursors;
529 } else if (out_level == 6) {
530 DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
531 (long long)ctr6->new_highwatermark.tmp_highest_usn,
532 (long long)ctr6->new_highwatermark.highest_usn));
534 first_object = ctr6->first_object;
535 mapping_ctr = &ctr6->mapping_ctr;
537 if (ctr6->more_data) {
538 req->req8.highwatermark = ctr6->new_highwatermark;
541 if (ctr6->uptodateness_vector &&
542 !ctx->single_object_replication)
544 new_utdv->version = 2;
545 new_utdv->ctr.ctr2.count =
546 ctr6->uptodateness_vector->count;
547 new_utdv->ctr.ctr2.cursors =
548 ctr6->uptodateness_vector->cursors;
553 status = cli_get_session_key(mem_ctx, ctx->cli, &ctx->session_key);
554 if (!NT_STATUS_IS_OK(status)) {
555 ctx->error_message = talloc_asprintf(ctx,
556 "Failed to get Session Key: %s",
561 libnet_dssync_decrypt_attributes(mem_ctx,
565 if (ctx->ops->process_objects) {
566 status = ctx->ops->process_objects(ctx, mem_ctx,
569 if (!NT_STATUS_IS_OK(status)) {
570 ctx->error_message = talloc_asprintf(ctx,
571 "Failed to call processing function: %s",
578 *pnew_utdv = new_utdv;
584 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
585 struct dssync_context *ctx)
590 union drsuapi_DsGetNCChangesRequest req;
591 struct replUpToDateVectorBlob *old_utdv = NULL;
592 struct replUpToDateVectorBlob *pnew_utdv = NULL;
597 if (ctx->ops->startup) {
598 status = ctx->ops->startup(ctx, mem_ctx, &old_utdv);
599 if (!NT_STATUS_IS_OK(status)) {
600 ctx->error_message = talloc_asprintf(ctx,
601 "Failed to call startup operation: %s",
607 if (ctx->single_object_replication && ctx->object_dns) {
608 dns = ctx->object_dns;
609 dn_count = ctx->object_count;
615 status = NT_STATUS_OK;
617 for (count=0; count < dn_count; count++) {
618 status = libnet_dssync_build_request(mem_ctx, ctx,
622 if (!NT_STATUS_IS_OK(status)) {
626 status = libnet_dssync_getncchanges(mem_ctx, ctx, level, &req,
628 if (!NT_STATUS_IS_OK(status)) {
629 ctx->error_message = talloc_asprintf(ctx,
630 "Failed to call DsGetNCCHanges: %s",
636 if (ctx->ops->finish) {
637 status = ctx->ops->finish(ctx, mem_ctx, pnew_utdv);
638 if (!NT_STATUS_IS_OK(status)) {
639 ctx->error_message = talloc_asprintf(ctx,
640 "Failed to call finishing operation: %s",
650 /****************************************************************
651 ****************************************************************/
653 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
654 struct dssync_context *ctx)
659 tmp_ctx = talloc_new(mem_ctx);
661 return NT_STATUS_NO_MEMORY;
664 status = libnet_dssync_init(tmp_ctx, ctx);
665 if (!NT_STATUS_IS_OK(status)) {
669 status = libnet_dssync_process(tmp_ctx, ctx);
670 if (!NT_STATUS_IS_OK(status)) {
675 TALLOC_FREE(tmp_ctx);