64a4df0f7f4cddd595e2066f49ea0edbb4b8e8e4
[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_nt_error_msg(status));
255                 return status;
256         }
257
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);
263         }
264
265         if (ctr.ctr1->count != 1) {
266                 return NT_STATUS_UNSUCCESSFUL;
267         }
268
269         if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
270                 return NT_STATUS_UNSUCCESSFUL;
271         }
272
273         ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
274         NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
275
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);
280         }
281
282         return NT_STATUS_OK;
283 }
284
285 /****************************************************************
286 ****************************************************************/
287
288 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
289                                    struct dssync_context *ctx)
290 {
291         NTSTATUS status;
292
293         status = libnet_dssync_bind(mem_ctx, ctx);
294         if (!NT_STATUS_IS_OK(status)) {
295                 return status;
296         }
297
298         if (!ctx->nc_dn) {
299                 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
300         }
301
302         return status;
303 }
304
305 /****************************************************************
306 ****************************************************************/
307
308 static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
309                                             struct dssync_context *ctx,
310                                             const char *dn,
311                                             struct replUpToDateVectorBlob *utdv,
312                                             uint32_t *plevel,
313                                             union drsuapi_DsGetNCChangesRequest *preq)
314 {
315         NTSTATUS status;
316         uint32_t count;
317         uint32_t level;
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;
323
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;
329
330         ZERO_STRUCT(null_sid);
331         ZERO_STRUCT(req);
332
333         if (ctx->remote_info28.supported_extensions
334             & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
335         {
336                 level = 8;
337         } else {
338                 level = 5;
339         }
340
341         nc = TALLOC_ZERO_P(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
342         if (!nc) {
343                 status = NT_STATUS_NO_MEMORY;
344                 goto fail;
345         }
346         nc->dn = dn;
347         nc->guid = GUID_zero();
348         nc->sid = null_sid;
349
350         if (!ctx->single_object_replication &&
351             !ctx->force_full_replication && utdv)
352         {
353                 cursors = TALLOC_ZERO_P(mem_ctx,
354                                          struct drsuapi_DsReplicaCursorCtrEx);
355                 if (!cursors) {
356                         status = NT_STATUS_NO_MEMORY;
357                         goto fail;
358                 }
359
360                 switch (utdv->version) {
361                 case 1:
362                         cursors->count = utdv->ctr.ctr1.count;
363                         cursors->cursors = utdv->ctr.ctr1.cursors;
364                         break;
365                 case 2:
366                         cursors->count = utdv->ctr.ctr2.count;
367                         cursors->cursors = talloc_array(cursors,
368                                                 struct drsuapi_DsReplicaCursor,
369                                                 cursors->count);
370                         if (!cursors->cursors) {
371                                 status = NT_STATUS_NO_MEMORY;
372                                 goto fail;
373                         }
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;
379                         }
380                         break;
381                 }
382         }
383
384         if (ctx->single_object_replication) {
385                 extended_op = DRSUAPI_EXOP_REPL_OBJ;
386         } else {
387                 extended_op = DRSUAPI_EXOP_NONE;
388         }
389
390         if (level == 8) {
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;
404         } else {
405                 status = NT_STATUS_INVALID_PARAMETER;
406                 goto fail;
407         }
408
409         if (plevel) {
410                 *plevel = level;
411         }
412
413         if (preq) {
414                 *preq = req;
415         }
416
417         return NT_STATUS_OK;
418
419 fail:
420         TALLOC_FREE(nc);
421         TALLOC_FREE(cursors);
422         return status;
423 }
424
425 static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
426                                            struct dssync_context *ctx,
427                                            uint32_t level,
428                                            union drsuapi_DsGetNCChangesRequest *req,
429                                            struct replUpToDateVectorBlob **pnew_utdv)
430 {
431         NTSTATUS status;
432         WERROR werr;
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;
439         int y;
440         bool last_query;
441         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
442
443         if (!ctx->single_object_replication) {
444                 new_utdv = TALLOC_ZERO_P(mem_ctx, struct replUpToDateVectorBlob);
445                 if (!new_utdv) {
446                         status = NT_STATUS_NO_MEMORY;
447                         goto out;
448                 }
449         }
450
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;
456
457                 if (level == 8) {
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));
465                 }
466
467                 status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
468                                                        &ctx->bind_handle,
469                                                        level,
470                                                        req,
471                                                        &level_out,
472                                                        &ctr,
473                                                        &werr);
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));
478                         goto out;
479                 }
480
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));
486                         goto out;
487                 }
488
489                 if (level_out == 1) {
490                         out_level = 1;
491                         ctr1 = &ctr.ctr1;
492                 } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
493                         out_level = 1;
494                         ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
495                 } else if (level_out == 6) {
496                         out_level = 6;
497                         ctr6 = &ctr.ctr6;
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) {
502                         out_level = 6;
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) {
508                         out_level = 6;
509                         ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
510                 }
511
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));
516
517                         first_object = ctr1->first_object;
518                         mapping_ctr = &ctr1->mapping_ctr;
519
520                         if (ctr1->more_data) {
521                                 req->req5.highwatermark = ctr1->new_highwatermark;
522                         } else {
523                                 last_query = true;
524                                 if (ctr1->uptodateness_vector &&
525                                     !ctx->single_object_replication)
526                                 {
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;
532                                 }
533                         }
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));
538
539                         first_object = ctr6->first_object;
540                         mapping_ctr = &ctr6->mapping_ctr;
541
542                         linked_attributes = ctr6->linked_attributes;
543                         linked_attributes_count = ctr6->linked_attributes_count;
544
545                         if (ctr6->more_data) {
546                                 req->req8.highwatermark = ctr6->new_highwatermark;
547                         } else {
548                                 last_query = true;
549                                 if (ctr6->uptodateness_vector &&
550                                     !ctx->single_object_replication)
551                                 {
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;
557                                 }
558                         }
559                 }
560
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",
565                                 nt_errstr(status));
566                         goto out;
567                 }
568
569                 libnet_dssync_decrypt_attributes(mem_ctx,
570                                                  &ctx->session_key,
571                                                  first_object);
572
573                 if (ctx->ops->process_objects) {
574                         status = ctx->ops->process_objects(ctx, mem_ctx,
575                                                            first_object,
576                                                            mapping_ctr);
577                         if (!NT_STATUS_IS_OK(status)) {
578                                 ctx->error_message = talloc_asprintf(ctx,
579                                         "Failed to call processing function: %s",
580                                         nt_errstr(status));
581                                 goto out;
582                         }
583                 }
584
585                 if (linked_attributes_count == 0) {
586                         continue;
587                 }
588
589                 if (ctx->ops->process_links) {
590                         status = ctx->ops->process_links(ctx, mem_ctx,
591                                                          linked_attributes_count,
592                                                          linked_attributes,
593                                                          mapping_ctr);
594                         if (!NT_STATUS_IS_OK(status)) {
595                                 ctx->error_message = talloc_asprintf(ctx,
596                                         "Failed to call processing function: %s",
597                                         nt_errstr(status));
598                                 goto out;
599                         }
600                 }
601         }
602
603         *pnew_utdv = new_utdv;
604
605 out:
606         return status;
607 }
608
609 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
610                                       struct dssync_context *ctx)
611 {
612         NTSTATUS status;
613
614         uint32_t level = 0;
615         union drsuapi_DsGetNCChangesRequest req;
616         struct replUpToDateVectorBlob *old_utdv = NULL;
617         struct replUpToDateVectorBlob *pnew_utdv = NULL;
618         const char **dns;
619         uint32_t dn_count;
620         uint32_t count;
621
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",
627                                 nt_errstr(status));
628                         goto out;
629                 }
630         }
631
632         if (ctx->single_object_replication && ctx->object_dns) {
633                 dns = ctx->object_dns;
634                 dn_count = ctx->object_count;
635         } else {
636                 dns = &ctx->nc_dn;
637                 dn_count = 1;
638         }
639
640         status = NT_STATUS_OK;
641
642         for (count=0; count < dn_count; count++) {
643                 status = libnet_dssync_build_request(mem_ctx, ctx,
644                                                      dns[count],
645                                                      old_utdv, &level,
646                                                      &req);
647                 if (!NT_STATUS_IS_OK(status)) {
648                         goto out;
649                 }
650
651                 status = libnet_dssync_getncchanges(mem_ctx, ctx, level, &req,
652                                                     &pnew_utdv);
653                 if (!NT_STATUS_IS_OK(status)) {
654                         ctx->error_message = talloc_asprintf(ctx,
655                                 "Failed to call DsGetNCCHanges: %s",
656                                 nt_errstr(status));
657                         goto out;
658                 }
659         }
660
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",
666                                 nt_errstr(status));
667                         goto out;
668                 }
669         }
670
671  out:
672         return status;
673 }
674
675 /****************************************************************
676 ****************************************************************/
677
678 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
679                        struct dssync_context *ctx)
680 {
681         NTSTATUS status;
682         TALLOC_CTX *tmp_ctx;
683
684         tmp_ctx = talloc_new(mem_ctx);
685         if (!tmp_ctx) {
686                 return NT_STATUS_NO_MEMORY;
687         }
688
689         status = libnet_dssync_init(tmp_ctx, ctx);
690         if (!NT_STATUS_IS_OK(status)) {
691                 goto out;
692         }
693
694         status = libnet_dssync_process(tmp_ctx, ctx);
695         if (!NT_STATUS_IS_OK(status)) {
696                 goto out;
697         }
698
699  out:
700         TALLOC_FREE(tmp_ctx);
701         return status;
702 }
703