s3-drsuapi: prefer dcerpc_drsuapi_X functions.
[samba.git] / source3 / libnet / libnet_dssync.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan (metze) Metzmacher 2005
5    Copyright (C) Guenther Deschner 2008
6    Copyright (C) Michael Adam 2008
7
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.
12
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.
17
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/>.
20 */
21
22
23 #include "includes.h"
24 #include "libnet/libnet_dssync.h"
25 #include "../libcli/drsuapi/drsuapi.h"
26 #include "../librpc/gen_ndr/ndr_drsuapi_c.h"
27
28 /****************************************************************
29 ****************************************************************/
30
31 static int libnet_dssync_free_context(struct dssync_context *ctx)
32 {
33         WERROR result;
34         struct dcerpc_binding_handle *b;
35
36         if (!ctx) {
37                 return 0;
38         }
39
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);
43         }
44
45         return 0;
46 }
47
48 /****************************************************************
49 ****************************************************************/
50
51 NTSTATUS libnet_dssync_init_context(TALLOC_CTX *mem_ctx,
52                                     struct dssync_context **ctx_p)
53 {
54         struct dssync_context *ctx;
55
56         ctx = TALLOC_ZERO_P(mem_ctx, struct dssync_context);
57         NT_STATUS_HAVE_NO_MEMORY(ctx);
58
59         talloc_set_destructor(ctx, libnet_dssync_free_context);
60         ctx->clean_old_entries = false;
61
62         *ctx_p = ctx;
63
64         return NT_STATUS_OK;
65 }
66
67 /****************************************************************
68 ****************************************************************/
69
70 static void parse_obj_identifier(struct drsuapi_DsReplicaObjectIdentifier *id,
71                                  uint32_t *rid)
72 {
73         if (!id || !rid) {
74                 return;
75         }
76
77         *rid = 0;
78
79         if (id->sid.num_auths > 0) {
80                 *rid = id->sid.sub_auths[id->sid.num_auths - 1];
81         }
82 }
83
84 /****************************************************************
85 ****************************************************************/
86
87 static void libnet_dssync_decrypt_attributes(TALLOC_CTX *mem_ctx,
88                                              DATA_BLOB *session_key,
89                                              struct drsuapi_DsReplicaObjectListItemEx *cur)
90 {
91         for (; cur; cur = cur->next_object) {
92
93                 uint32_t i;
94                 uint32_t rid = 0;
95
96                 parse_obj_identifier(cur->object.identifier, &rid);
97
98                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
99
100                         struct drsuapi_DsReplicaAttribute *attr;
101
102                         attr = &cur->object.attribute_ctr.attributes[i];
103
104                         if (attr->value_ctr.num_values < 1) {
105                                 continue;
106                         }
107
108                         if (!attr->value_ctr.values[0].blob) {
109                                 continue;
110                         }
111
112                         drsuapi_decrypt_attribute(mem_ctx,
113                                                   session_key,
114                                                   rid,
115                                                   attr);
116                 }
117         }
118 }
119 /****************************************************************
120 ****************************************************************/
121
122 static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
123                                    struct dssync_context *ctx)
124 {
125         NTSTATUS status;
126         WERROR werr;
127
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;
132
133         ZERO_STRUCT(info28);
134
135         GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
136
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();
166         info28.pid                      = 508;
167         info28.repl_epoch               = 0;
168
169         bind_info.length = 28;
170         bind_info.info.info28 = info28;
171
172         status = dcerpc_drsuapi_DsBind(b, mem_ctx,
173                                        &bind_guid,
174                                        &bind_info,
175                                        &ctx->bind_handle,
176                                        &werr);
177
178         if (!NT_STATUS_IS_OK(status)) {
179                 return status;
180         }
181
182         if (!W_ERROR_IS_OK(werr)) {
183                 return werror_to_ntstatus(werr);
184         }
185
186         ZERO_STRUCT(ctx->remote_info28);
187         switch (bind_info.length) {
188         case 24: {
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;
195                 break;
196         }
197         case 28:
198                 ctx->remote_info28 = bind_info.info.info28;
199                 break;
200         case 48: {
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;
207                 break;
208         }
209         default:
210                 DEBUG(1, ("Warning: invalid info length in bind info: %d\n",
211                           bind_info.length));
212                 break;
213         }
214
215         return status;
216 }
217
218 /****************************************************************
219 ****************************************************************/
220
221 static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
222                                         struct dssync_context *ctx)
223 {
224         NTSTATUS status;
225         WERROR werr;
226         uint32_t level = 1;
227         union drsuapi_DsNameRequest req;
228         uint32_t level_out;
229         struct drsuapi_DsNameString names[1];
230         union drsuapi_DsNameCtr ctr;
231         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
232
233         names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
234         NT_STATUS_HAVE_NO_MEMORY(names[0].str);
235
236         req.req1.codepage       = 1252; /* german */
237         req.req1.language       = 0x00000407; /* german */
238         req.req1.count          = 1;
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;
243
244         status = dcerpc_drsuapi_DsCrackNames(b, mem_ctx,
245                                              &ctx->bind_handle,
246                                              level,
247                                              &req,
248                                              &level_out,
249                                              &ctr,
250                                              &werr);
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));
255                 return status;
256         }
257
258         if (!W_ERROR_IS_OK(werr)) {
259                 return werror_to_ntstatus(werr);
260         }
261
262         if (ctr.ctr1->count != 1) {
263                 return NT_STATUS_UNSUCCESSFUL;
264         }
265
266         if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
267                 return NT_STATUS_UNSUCCESSFUL;
268         }
269
270         ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
271         NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
272
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);
277         }
278
279         return NT_STATUS_OK;
280 }
281
282 /****************************************************************
283 ****************************************************************/
284
285 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
286                                    struct dssync_context *ctx)
287 {
288         NTSTATUS status;
289
290         status = libnet_dssync_bind(mem_ctx, ctx);
291         if (!NT_STATUS_IS_OK(status)) {
292                 return status;
293         }
294
295         if (!ctx->nc_dn) {
296                 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
297         }
298
299         return status;
300 }
301
302 /****************************************************************
303 ****************************************************************/
304
305 static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
306                                             struct dssync_context *ctx,
307                                             const char *dn,
308                                             struct replUpToDateVectorBlob *utdv,
309                                             uint32_t *plevel,
310                                             union drsuapi_DsGetNCChangesRequest *preq)
311 {
312         NTSTATUS status;
313         uint32_t count;
314         uint32_t level;
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;
320
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;
326
327         ZERO_STRUCT(null_sid);
328         ZERO_STRUCT(req);
329
330         if (ctx->remote_info28.supported_extensions
331             & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
332         {
333                 level = 8;
334         } else {
335                 level = 5;
336         }
337
338         nc = TALLOC_ZERO_P(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
339         if (!nc) {
340                 status = NT_STATUS_NO_MEMORY;
341                 goto fail;
342         }
343         nc->dn = dn;
344         nc->guid = GUID_zero();
345         nc->sid = null_sid;
346
347         if (!ctx->single_object_replication &&
348             !ctx->force_full_replication && utdv)
349         {
350                 cursors = TALLOC_ZERO_P(mem_ctx,
351                                          struct drsuapi_DsReplicaCursorCtrEx);
352                 if (!cursors) {
353                         status = NT_STATUS_NO_MEMORY;
354                         goto fail;
355                 }
356
357                 switch (utdv->version) {
358                 case 1:
359                         cursors->count = utdv->ctr.ctr1.count;
360                         cursors->cursors = utdv->ctr.ctr1.cursors;
361                         break;
362                 case 2:
363                         cursors->count = utdv->ctr.ctr2.count;
364                         cursors->cursors = talloc_array(cursors,
365                                                 struct drsuapi_DsReplicaCursor,
366                                                 cursors->count);
367                         if (!cursors->cursors) {
368                                 status = NT_STATUS_NO_MEMORY;
369                                 goto fail;
370                         }
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;
376                         }
377                         break;
378                 }
379         }
380
381         if (ctx->single_object_replication) {
382                 extended_op = DRSUAPI_EXOP_REPL_OBJ;
383         } else {
384                 extended_op = DRSUAPI_EXOP_NONE;
385         }
386
387         if (level == 8) {
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;
401         } else {
402                 status = NT_STATUS_INVALID_PARAMETER;
403                 goto fail;
404         }
405
406         if (plevel) {
407                 *plevel = level;
408         }
409
410         if (preq) {
411                 *preq = req;
412         }
413
414         return NT_STATUS_OK;
415
416 fail:
417         TALLOC_FREE(nc);
418         TALLOC_FREE(cursors);
419         return status;
420 }
421
422 static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
423                                            struct dssync_context *ctx,
424                                            uint32_t level,
425                                            union drsuapi_DsGetNCChangesRequest *req,
426                                            struct replUpToDateVectorBlob **pnew_utdv)
427 {
428         NTSTATUS status;
429         WERROR werr;
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;
436         int y;
437         bool last_query;
438         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
439
440         if (!ctx->single_object_replication) {
441                 new_utdv = TALLOC_ZERO_P(mem_ctx, struct replUpToDateVectorBlob);
442                 if (!new_utdv) {
443                         status = NT_STATUS_NO_MEMORY;
444                         goto out;
445                 }
446         }
447
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;
451
452                 if (level == 8) {
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));
460                 }
461
462                 status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
463                                                        &ctx->bind_handle,
464                                                        level,
465                                                        req,
466                                                        &level_out,
467                                                        &ctr,
468                                                        &werr);
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));
473                         goto out;
474                 }
475
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));
481                         goto out;
482                 }
483
484                 if (level_out == 1) {
485                         out_level = 1;
486                         ctr1 = &ctr.ctr1;
487                 } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
488                         out_level = 1;
489                         ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
490                 } else if (level_out == 6) {
491                         out_level = 6;
492                         ctr6 = &ctr.ctr6;
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) {
497                         out_level = 6;
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) {
503                         out_level = 6;
504                         ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
505                 }
506
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));
511
512                         first_object = ctr1->first_object;
513                         mapping_ctr = &ctr1->mapping_ctr;
514
515                         if (ctr1->more_data) {
516                                 req->req5.highwatermark = ctr1->new_highwatermark;
517                         } else {
518                                 last_query = true;
519                                 if (ctr1->uptodateness_vector &&
520                                     !ctx->single_object_replication)
521                                 {
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;
527                                 }
528                         }
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));
533
534                         first_object = ctr6->first_object;
535                         mapping_ctr = &ctr6->mapping_ctr;
536
537                         if (ctr6->more_data) {
538                                 req->req8.highwatermark = ctr6->new_highwatermark;
539                         } else {
540                                 last_query = true;
541                                 if (ctr6->uptodateness_vector &&
542                                     !ctx->single_object_replication)
543                                 {
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;
549                                 }
550                         }
551                 }
552
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",
557                                 nt_errstr(status));
558                         goto out;
559                 }
560
561                 libnet_dssync_decrypt_attributes(mem_ctx,
562                                                  &ctx->session_key,
563                                                  first_object);
564
565                 if (ctx->ops->process_objects) {
566                         status = ctx->ops->process_objects(ctx, mem_ctx,
567                                                            first_object,
568                                                            mapping_ctr);
569                         if (!NT_STATUS_IS_OK(status)) {
570                                 ctx->error_message = talloc_asprintf(ctx,
571                                         "Failed to call processing function: %s",
572                                         nt_errstr(status));
573                                 goto out;
574                         }
575                 }
576         }
577
578         *pnew_utdv = new_utdv;
579
580 out:
581         return status;
582 }
583
584 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
585                                       struct dssync_context *ctx)
586 {
587         NTSTATUS status;
588
589         uint32_t level = 0;
590         union drsuapi_DsGetNCChangesRequest req;
591         struct replUpToDateVectorBlob *old_utdv = NULL;
592         struct replUpToDateVectorBlob *pnew_utdv = NULL;
593         const char **dns;
594         uint32_t dn_count;
595         uint32_t count;
596
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",
602                                 nt_errstr(status));
603                         goto out;
604                 }
605         }
606
607         if (ctx->single_object_replication && ctx->object_dns) {
608                 dns = ctx->object_dns;
609                 dn_count = ctx->object_count;
610         } else {
611                 dns = &ctx->nc_dn;
612                 dn_count = 1;
613         }
614
615         status = NT_STATUS_OK;
616
617         for (count=0; count < dn_count; count++) {
618                 status = libnet_dssync_build_request(mem_ctx, ctx,
619                                                      dns[count],
620                                                      old_utdv, &level,
621                                                      &req);
622                 if (!NT_STATUS_IS_OK(status)) {
623                         goto out;
624                 }
625
626                 status = libnet_dssync_getncchanges(mem_ctx, ctx, level, &req,
627                                                     &pnew_utdv);
628                 if (!NT_STATUS_IS_OK(status)) {
629                         ctx->error_message = talloc_asprintf(ctx,
630                                 "Failed to call DsGetNCCHanges: %s",
631                                 nt_errstr(status));
632                         goto out;
633                 }
634         }
635
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",
641                                 nt_errstr(status));
642                         goto out;
643                 }
644         }
645
646  out:
647         return status;
648 }
649
650 /****************************************************************
651 ****************************************************************/
652
653 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
654                        struct dssync_context *ctx)
655 {
656         NTSTATUS status;
657         TALLOC_CTX *tmp_ctx;
658
659         tmp_ctx = talloc_new(mem_ctx);
660         if (!tmp_ctx) {
661                 return NT_STATUS_NO_MEMORY;
662         }
663
664         status = libnet_dssync_init(tmp_ctx, ctx);
665         if (!NT_STATUS_IS_OK(status)) {
666                 goto out;
667         }
668
669         status = libnet_dssync_process(tmp_ctx, ctx);
670         if (!NT_STATUS_IS_OK(status)) {
671                 goto out;
672         }
673
674  out:
675         TALLOC_FREE(tmp_ctx);
676         return status;
677 }
678