idl:drsuapi: Manage all possible lengths of drsuapi_DsBindInfo
[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 "rpc_client/cli_pipe.h"
26 #include "../libcli/drsuapi/drsuapi.h"
27 #include "../librpc/gen_ndr/ndr_drsuapi_c.h"
28
29 /****************************************************************
30 ****************************************************************/
31
32 static int libnet_dssync_free_context(struct dssync_context *ctx)
33 {
34         WERROR result;
35         struct dcerpc_binding_handle *b;
36
37         if (!ctx) {
38                 return 0;
39         }
40
41         if (is_valid_policy_hnd(&ctx->bind_handle) && ctx->cli) {
42                 b = ctx->cli->binding_handle;
43                 dcerpc_drsuapi_DsUnbind(b, ctx, &ctx->bind_handle, &result);
44         }
45
46         return 0;
47 }
48
49 /****************************************************************
50 ****************************************************************/
51
52 NTSTATUS libnet_dssync_init_context(TALLOC_CTX *mem_ctx,
53                                     struct dssync_context **ctx_p)
54 {
55         struct dssync_context *ctx;
56
57         ctx = talloc_zero(mem_ctx, struct dssync_context);
58         NT_STATUS_HAVE_NO_MEMORY(ctx);
59
60         talloc_set_destructor(ctx, libnet_dssync_free_context);
61         ctx->clean_old_entries = false;
62
63         *ctx_p = ctx;
64
65         return NT_STATUS_OK;
66 }
67
68 /****************************************************************
69 ****************************************************************/
70
71 static void parse_obj_identifier(struct drsuapi_DsReplicaObjectIdentifier *id,
72                                  uint32_t *rid)
73 {
74         if (!id || !rid) {
75                 return;
76         }
77
78         *rid = 0;
79
80         if (id->sid.num_auths > 0) {
81                 *rid = id->sid.sub_auths[id->sid.num_auths - 1];
82         }
83 }
84
85 /****************************************************************
86 ****************************************************************/
87
88 static void libnet_dssync_decrypt_attributes(TALLOC_CTX *mem_ctx,
89                                              DATA_BLOB *session_key,
90                                              struct drsuapi_DsReplicaObjectListItemEx *cur)
91 {
92         for (; cur; cur = cur->next_object) {
93
94                 uint32_t i;
95                 uint32_t rid = 0;
96
97                 parse_obj_identifier(cur->object.identifier, &rid);
98
99                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
100
101                         struct drsuapi_DsReplicaAttribute *attr;
102
103                         attr = &cur->object.attribute_ctr.attributes[i];
104
105                         if (attr->value_ctr.num_values < 1) {
106                                 continue;
107                         }
108
109                         if (!attr->value_ctr.values[0].blob) {
110                                 continue;
111                         }
112
113                         drsuapi_decrypt_attribute(mem_ctx,
114                                                   session_key,
115                                                   rid,
116                                                   attr);
117                 }
118         }
119 }
120 /****************************************************************
121 ****************************************************************/
122
123 static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
124                                    struct dssync_context *ctx)
125 {
126         NTSTATUS status;
127         WERROR werr;
128
129         struct GUID bind_guid;
130         struct drsuapi_DsBindInfoCtr bind_info;
131         struct drsuapi_DsBindInfo28 info28;
132         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
133
134         ZERO_STRUCT(info28);
135
136         GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
137
138         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
139         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
140         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
141         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
142         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
143         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
144         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
145         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
146         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
147         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
148         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
149         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
150         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
151         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
152         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
153         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
154         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
155         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
156         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
157         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
158         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
159         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
160         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
161         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
162         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
163         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
164         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
165         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
166         info28.site_guid                = GUID_zero();
167         info28.pid                      = 508;
168         info28.repl_epoch               = 0;
169
170         bind_info.length = 28;
171         bind_info.info.info28 = info28;
172
173         status = dcerpc_drsuapi_DsBind(b, mem_ctx,
174                                        &bind_guid,
175                                        &bind_info,
176                                        &ctx->bind_handle,
177                                        &werr);
178
179         if (!NT_STATUS_IS_OK(status)) {
180                 return status;
181         }
182
183         if (!W_ERROR_IS_OK(werr)) {
184                 return werror_to_ntstatus(werr);
185         }
186
187         ZERO_STRUCT(ctx->remote_info28);
188         switch (bind_info.length) {
189         case 24: {
190                 struct drsuapi_DsBindInfo24 *info24;
191                 info24 = &bind_info.info.info24;
192                 ctx->remote_info28.site_guid            = info24->site_guid;
193                 ctx->remote_info28.supported_extensions = info24->supported_extensions;
194                 ctx->remote_info28.pid                  = info24->pid;
195                 ctx->remote_info28.repl_epoch           = 0;
196                 break;
197         }
198         case 28: {
199                 ctx->remote_info28 = bind_info.info.info28;
200                 break;
201         }
202         case 32: {
203                 struct drsuapi_DsBindInfo32 *info32;
204                 info32 = &bind_info.info.info32;
205                 ctx->remote_info28.site_guid            = info32->site_guid;
206                 ctx->remote_info28.supported_extensions = info32->supported_extensions;
207                 ctx->remote_info28.pid                  = info32->pid;
208                 ctx->remote_info28.repl_epoch           = info32->repl_epoch;
209                 break;
210         }
211         case 48: {
212                 struct drsuapi_DsBindInfo48 *info48;
213                 info48 = &bind_info.info.info48;
214                 ctx->remote_info28.site_guid            = info48->site_guid;
215                 ctx->remote_info28.supported_extensions = info48->supported_extensions;
216                 ctx->remote_info28.pid                  = info48->pid;
217                 ctx->remote_info28.repl_epoch           = info48->repl_epoch;
218                 break;
219         }
220         case 52: {
221                 struct drsuapi_DsBindInfo52 *info52;
222                 info52 = &bind_info.info.info52;
223                 ctx->remote_info28.site_guid            = info52->site_guid;
224                 ctx->remote_info28.supported_extensions = info52->supported_extensions;
225                 ctx->remote_info28.pid                  = info52->pid;
226                 ctx->remote_info28.repl_epoch           = info52->repl_epoch;
227                 break;
228         }
229         default:
230                 DEBUG(1, ("Warning: invalid info length in bind info: %d\n",
231                           bind_info.length));
232                 break;
233         }
234
235         return status;
236 }
237
238 /****************************************************************
239 ****************************************************************/
240
241 static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
242                                         struct dssync_context *ctx)
243 {
244         NTSTATUS status;
245         WERROR werr;
246         uint32_t level = 1;
247         union drsuapi_DsNameRequest req;
248         uint32_t level_out;
249         struct drsuapi_DsNameString names[1];
250         union drsuapi_DsNameCtr ctr;
251         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
252
253         names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
254         NT_STATUS_HAVE_NO_MEMORY(names[0].str);
255
256         req.req1.codepage       = 1252; /* german */
257         req.req1.language       = 0x00000407; /* german */
258         req.req1.count          = 1;
259         req.req1.names          = names;
260         req.req1.format_flags   = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
261         req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
262         req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
263
264         status = dcerpc_drsuapi_DsCrackNames(b, mem_ctx,
265                                              &ctx->bind_handle,
266                                              level,
267                                              &req,
268                                              &level_out,
269                                              &ctr,
270                                              &werr);
271         if (!NT_STATUS_IS_OK(status)) {
272                 ctx->error_message = talloc_asprintf(ctx,
273                         "Failed to lookup DN for domain name: %s",
274                         get_friendly_nt_error_msg(status));
275                 return status;
276         }
277
278         if (!W_ERROR_IS_OK(werr)) {
279                 ctx->error_message = talloc_asprintf(ctx,
280                         "Failed to lookup DN for domain name: %s",
281                         get_friendly_werror_msg(werr));
282                 return werror_to_ntstatus(werr);
283         }
284
285         if (ctr.ctr1->count != 1) {
286                 return NT_STATUS_UNSUCCESSFUL;
287         }
288
289         if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
290                 return NT_STATUS_UNSUCCESSFUL;
291         }
292
293         ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
294         NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
295
296         if (!ctx->dns_domain_name) {
297                 ctx->dns_domain_name = talloc_strdup_upper(mem_ctx,
298                         ctr.ctr1->array[0].dns_domain_name);
299                 NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
300         }
301
302         return NT_STATUS_OK;
303 }
304
305 /****************************************************************
306 ****************************************************************/
307
308 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
309                                    struct dssync_context *ctx)
310 {
311         NTSTATUS status;
312
313         status = libnet_dssync_bind(mem_ctx, ctx);
314         if (!NT_STATUS_IS_OK(status)) {
315                 return status;
316         }
317
318         if (!ctx->nc_dn) {
319                 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
320         }
321
322         return status;
323 }
324
325 /****************************************************************
326 ****************************************************************/
327
328 static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
329                                             struct dssync_context *ctx,
330                                             const char *dn,
331                                             struct replUpToDateVectorBlob *utdv,
332                                             uint32_t *plevel,
333                                             union drsuapi_DsGetNCChangesRequest *preq)
334 {
335         NTSTATUS status;
336         uint32_t count;
337         uint32_t level;
338         union drsuapi_DsGetNCChangesRequest req;
339         struct dom_sid null_sid;
340         enum drsuapi_DsExtendedOperation extended_op;
341         struct drsuapi_DsReplicaObjectIdentifier *nc = NULL;
342         struct drsuapi_DsReplicaCursorCtrEx *cursors = NULL;
343
344         uint32_t replica_flags  = DRSUAPI_DRS_WRIT_REP |
345                                   DRSUAPI_DRS_INIT_SYNC |
346                                   DRSUAPI_DRS_PER_SYNC |
347                                   DRSUAPI_DRS_GET_ANC |
348                                   DRSUAPI_DRS_NEVER_SYNCED;
349
350         ZERO_STRUCT(null_sid);
351         ZERO_STRUCT(req);
352
353         if (ctx->remote_info28.supported_extensions
354             & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
355         {
356                 level = 8;
357         } else {
358                 level = 5;
359         }
360
361         nc = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
362         if (!nc) {
363                 status = NT_STATUS_NO_MEMORY;
364                 goto fail;
365         }
366         nc->dn = dn;
367         nc->guid = GUID_zero();
368         nc->sid = null_sid;
369
370         if (!ctx->single_object_replication &&
371             !ctx->force_full_replication && utdv)
372         {
373                 cursors = talloc_zero(mem_ctx,
374                                          struct drsuapi_DsReplicaCursorCtrEx);
375                 if (!cursors) {
376                         status = NT_STATUS_NO_MEMORY;
377                         goto fail;
378                 }
379
380                 switch (utdv->version) {
381                 case 1:
382                         cursors->count = utdv->ctr.ctr1.count;
383                         cursors->cursors = utdv->ctr.ctr1.cursors;
384                         break;
385                 case 2:
386                         cursors->count = utdv->ctr.ctr2.count;
387                         cursors->cursors = talloc_array(cursors,
388                                                 struct drsuapi_DsReplicaCursor,
389                                                 cursors->count);
390                         if (!cursors->cursors) {
391                                 status = NT_STATUS_NO_MEMORY;
392                                 goto fail;
393                         }
394                         for (count = 0; count < cursors->count; count++) {
395                                 cursors->cursors[count].source_dsa_invocation_id =
396                                         utdv->ctr.ctr2.cursors[count].source_dsa_invocation_id;
397                                 cursors->cursors[count].highest_usn =
398                                         utdv->ctr.ctr2.cursors[count].highest_usn;
399                         }
400                         break;
401                 }
402         }
403
404         if (ctx->single_object_replication) {
405                 extended_op = DRSUAPI_EXOP_REPL_OBJ;
406         } else {
407                 extended_op = DRSUAPI_EXOP_NONE;
408         }
409
410         if (level == 8) {
411                 req.req8.naming_context         = nc;
412                 req.req8.replica_flags          = replica_flags;
413                 req.req8.max_object_count       = 402;
414                 req.req8.max_ndr_size           = 402116;
415                 req.req8.uptodateness_vector    = cursors;
416                 req.req8.extended_op            = extended_op;
417         } else if (level == 5) {
418                 req.req5.naming_context         = nc;
419                 req.req5.replica_flags          = replica_flags;
420                 req.req5.max_object_count       = 402;
421                 req.req5.max_ndr_size           = 402116;
422                 req.req5.uptodateness_vector    = cursors;
423                 req.req5.extended_op            = extended_op;
424         } else {
425                 status = NT_STATUS_INVALID_PARAMETER;
426                 goto fail;
427         }
428
429         if (plevel) {
430                 *plevel = level;
431         }
432
433         if (preq) {
434                 *preq = req;
435         }
436
437         return NT_STATUS_OK;
438
439 fail:
440         TALLOC_FREE(nc);
441         TALLOC_FREE(cursors);
442         return status;
443 }
444
445 static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
446                                            struct dssync_context *ctx,
447                                            uint32_t level,
448                                            union drsuapi_DsGetNCChangesRequest *req,
449                                            struct replUpToDateVectorBlob **pnew_utdv)
450 {
451         NTSTATUS status;
452         WERROR werr;
453         union drsuapi_DsGetNCChangesCtr ctr;
454         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
455         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
456         struct replUpToDateVectorBlob *new_utdv = NULL;
457         uint32_t level_out = 0;
458         uint32_t out_level = 0;
459         int y;
460         bool last_query;
461         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
462
463         if (!ctx->single_object_replication) {
464                 new_utdv = talloc_zero(mem_ctx, struct replUpToDateVectorBlob);
465                 if (!new_utdv) {
466                         status = NT_STATUS_NO_MEMORY;
467                         goto out;
468                 }
469         }
470
471         for (y=0, last_query = false; !last_query; y++) {
472                 struct drsuapi_DsReplicaObjectListItemEx *first_object = NULL;
473                 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr = NULL;
474                 uint32_t linked_attributes_count = 0;
475                 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes = NULL;
476
477                 if (level == 8) {
478                         DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
479                                 (long long)req->req8.highwatermark.tmp_highest_usn,
480                                 (long long)req->req8.highwatermark.highest_usn));
481                 } else if (level == 5) {
482                         DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
483                                 (long long)req->req5.highwatermark.tmp_highest_usn,
484                                 (long long)req->req5.highwatermark.highest_usn));
485                 }
486
487                 status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
488                                                        &ctx->bind_handle,
489                                                        level,
490                                                        req,
491                                                        &level_out,
492                                                        &ctr,
493                                                        &werr);
494                 if (!NT_STATUS_IS_OK(status)) {
495                         ctx->error_message = talloc_asprintf(ctx,
496                                 "Failed to get NC Changes: %s",
497                                 get_friendly_nt_error_msg(status));
498                         goto out;
499                 }
500
501                 if (!W_ERROR_IS_OK(werr)) {
502                         status = werror_to_ntstatus(werr);
503                         ctx->error_message = talloc_asprintf(ctx,
504                                 "Failed to get NC Changes: %s",
505                                 get_friendly_werror_msg(werr));
506                         goto out;
507                 }
508
509                 if (level_out == 1) {
510                         out_level = 1;
511                         ctr1 = &ctr.ctr1;
512                 } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
513                         out_level = 1;
514                         ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
515                 } else if (level_out == 6) {
516                         out_level = 6;
517                         ctr6 = &ctr.ctr6;
518                 } else if (level_out == 7
519                            && ctr.ctr7.level == 6
520                            && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
521                            && ctr.ctr7.ctr.mszip6.ts) {
522                         out_level = 6;
523                         ctr6 = &ctr.ctr7.ctr.mszip6.ts->ctr6;
524                 } else if (level_out == 7
525                            && ctr.ctr7.level == 6
526                            && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
527                            && ctr.ctr7.ctr.xpress6.ts) {
528                         out_level = 6;
529                         ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
530                 }
531
532                 if (out_level == 1) {
533                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
534                                 (long long)ctr1->new_highwatermark.tmp_highest_usn,
535                                 (long long)ctr1->new_highwatermark.highest_usn));
536
537                         first_object = ctr1->first_object;
538                         mapping_ctr = &ctr1->mapping_ctr;
539
540                         if (ctr1->more_data) {
541                                 req->req5.highwatermark = ctr1->new_highwatermark;
542                         } else {
543                                 last_query = true;
544                                 if (ctr1->uptodateness_vector &&
545                                     !ctx->single_object_replication)
546                                 {
547                                         new_utdv->version = 1;
548                                         new_utdv->ctr.ctr1.count =
549                                                 ctr1->uptodateness_vector->count;
550                                         new_utdv->ctr.ctr1.cursors =
551                                                 ctr1->uptodateness_vector->cursors;
552                                 }
553                         }
554                 } else if (out_level == 6) {
555                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
556                                 (long long)ctr6->new_highwatermark.tmp_highest_usn,
557                                 (long long)ctr6->new_highwatermark.highest_usn));
558
559                         first_object = ctr6->first_object;
560                         mapping_ctr = &ctr6->mapping_ctr;
561
562                         linked_attributes = ctr6->linked_attributes;
563                         linked_attributes_count = ctr6->linked_attributes_count;
564
565                         if (ctr6->more_data) {
566                                 req->req8.highwatermark = ctr6->new_highwatermark;
567                         } else {
568                                 last_query = true;
569                                 if (ctr6->uptodateness_vector &&
570                                     !ctx->single_object_replication)
571                                 {
572                                         new_utdv->version = 2;
573                                         new_utdv->ctr.ctr2.count =
574                                                 ctr6->uptodateness_vector->count;
575                                         new_utdv->ctr.ctr2.cursors =
576                                                 ctr6->uptodateness_vector->cursors;
577                                 }
578                         }
579                 }
580
581                 status = cli_get_session_key(mem_ctx, ctx->cli, &ctx->session_key);
582                 if (!NT_STATUS_IS_OK(status)) {
583                         ctx->error_message = talloc_asprintf(ctx,
584                                 "Failed to get Session Key: %s",
585                                 nt_errstr(status));
586                         goto out;
587                 }
588
589                 libnet_dssync_decrypt_attributes(mem_ctx,
590                                                  &ctx->session_key,
591                                                  first_object);
592
593                 if (ctx->ops->process_objects) {
594                         status = ctx->ops->process_objects(ctx, mem_ctx,
595                                                            first_object,
596                                                            mapping_ctr);
597                         if (!NT_STATUS_IS_OK(status)) {
598                                 ctx->error_message = talloc_asprintf(ctx,
599                                         "Failed to call processing function: %s",
600                                         nt_errstr(status));
601                                 goto out;
602                         }
603                 }
604
605                 if (linked_attributes_count == 0) {
606                         continue;
607                 }
608
609                 if (ctx->ops->process_links) {
610                         status = ctx->ops->process_links(ctx, mem_ctx,
611                                                          linked_attributes_count,
612                                                          linked_attributes,
613                                                          mapping_ctr);
614                         if (!NT_STATUS_IS_OK(status)) {
615                                 ctx->error_message = talloc_asprintf(ctx,
616                                         "Failed to call processing function: %s",
617                                         nt_errstr(status));
618                                 goto out;
619                         }
620                 }
621         }
622
623         *pnew_utdv = new_utdv;
624
625 out:
626         return status;
627 }
628
629 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
630                                       struct dssync_context *ctx)
631 {
632         NTSTATUS status;
633
634         uint32_t level = 0;
635         union drsuapi_DsGetNCChangesRequest req;
636         struct replUpToDateVectorBlob *old_utdv = NULL;
637         struct replUpToDateVectorBlob *pnew_utdv = NULL;
638         const char **dns;
639         uint32_t dn_count;
640         uint32_t count;
641
642         if (ctx->ops->startup) {
643                 status = ctx->ops->startup(ctx, mem_ctx, &old_utdv);
644                 if (!NT_STATUS_IS_OK(status)) {
645                         ctx->error_message = talloc_asprintf(ctx,
646                                 "Failed to call startup operation: %s",
647                                 nt_errstr(status));
648                         goto out;
649                 }
650         }
651
652         if (ctx->single_object_replication && ctx->object_dns) {
653                 dns = ctx->object_dns;
654                 dn_count = ctx->object_count;
655         } else {
656                 dns = &ctx->nc_dn;
657                 dn_count = 1;
658         }
659
660         status = NT_STATUS_OK;
661
662         for (count=0; count < dn_count; count++) {
663                 status = libnet_dssync_build_request(mem_ctx, ctx,
664                                                      dns[count],
665                                                      old_utdv, &level,
666                                                      &req);
667                 if (!NT_STATUS_IS_OK(status)) {
668                         goto out;
669                 }
670
671                 status = libnet_dssync_getncchanges(mem_ctx, ctx, level, &req,
672                                                     &pnew_utdv);
673                 if (!NT_STATUS_IS_OK(status)) {
674                         if (!ctx->error_message) {
675                                 ctx->error_message = talloc_asprintf(ctx,
676                                         "Failed to call DsGetNCCHanges: %s",
677                                         nt_errstr(status));
678                         }
679                         goto out;
680                 }
681         }
682
683         if (ctx->ops->finish) {
684                 status = ctx->ops->finish(ctx, mem_ctx, pnew_utdv);
685                 if (!NT_STATUS_IS_OK(status)) {
686                         ctx->error_message = talloc_asprintf(ctx,
687                                 "Failed to call finishing operation: %s",
688                                 nt_errstr(status));
689                         goto out;
690                 }
691         }
692
693  out:
694         return status;
695 }
696
697 /****************************************************************
698 ****************************************************************/
699
700 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
701                        struct dssync_context *ctx)
702 {
703         NTSTATUS status;
704         TALLOC_CTX *tmp_ctx;
705
706         tmp_ctx = talloc_new(mem_ctx);
707         if (!tmp_ctx) {
708                 return NT_STATUS_NO_MEMORY;
709         }
710
711         status = libnet_dssync_init(tmp_ctx, ctx);
712         if (!NT_STATUS_IS_OK(status)) {
713                 goto out;
714         }
715
716         status = libnet_dssync_process(tmp_ctx, ctx);
717         if (!NT_STATUS_IS_OK(status)) {
718                 goto out;
719         }
720
721  out:
722         TALLOC_FREE(tmp_ctx);
723         return status;
724 }
725