s3-rpc_server: run minimal_includes.pl.
[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_P(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         case 48: {
202                 struct drsuapi_DsBindInfo48 *info48;
203                 info48 = &bind_info.info.info48;
204                 ctx->remote_info28.site_guid            = info48->site_guid;
205                 ctx->remote_info28.supported_extensions = info48->supported_extensions;
206                 ctx->remote_info28.pid                  = info48->pid;
207                 ctx->remote_info28.repl_epoch           = info48->repl_epoch;
208                 break;
209         }
210         default:
211                 DEBUG(1, ("Warning: invalid info length in bind info: %d\n",
212                           bind_info.length));
213                 break;
214         }
215
216         return status;
217 }
218
219 /****************************************************************
220 ****************************************************************/
221
222 static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
223                                         struct dssync_context *ctx)
224 {
225         NTSTATUS status;
226         WERROR werr;
227         uint32_t level = 1;
228         union drsuapi_DsNameRequest req;
229         uint32_t level_out;
230         struct drsuapi_DsNameString names[1];
231         union drsuapi_DsNameCtr ctr;
232         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
233
234         names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
235         NT_STATUS_HAVE_NO_MEMORY(names[0].str);
236
237         req.req1.codepage       = 1252; /* german */
238         req.req1.language       = 0x00000407; /* german */
239         req.req1.count          = 1;
240         req.req1.names          = names;
241         req.req1.format_flags   = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
242         req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
243         req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
244
245         status = dcerpc_drsuapi_DsCrackNames(b, mem_ctx,
246                                              &ctx->bind_handle,
247                                              level,
248                                              &req,
249                                              &level_out,
250                                              &ctr,
251                                              &werr);
252         if (!NT_STATUS_IS_OK(status)) {
253                 ctx->error_message = talloc_asprintf(ctx,
254                         "Failed to lookup DN for domain name: %s",
255                         get_friendly_nt_error_msg(status));
256                 return status;
257         }
258
259         if (!W_ERROR_IS_OK(werr)) {
260                 ctx->error_message = talloc_asprintf(ctx,
261                         "Failed to lookup DN for domain name: %s",
262                         get_friendly_werror_msg(werr));
263                 return werror_to_ntstatus(werr);
264         }
265
266         if (ctr.ctr1->count != 1) {
267                 return NT_STATUS_UNSUCCESSFUL;
268         }
269
270         if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
271                 return NT_STATUS_UNSUCCESSFUL;
272         }
273
274         ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
275         NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
276
277         if (!ctx->dns_domain_name) {
278                 ctx->dns_domain_name = talloc_strdup_upper(mem_ctx,
279                         ctr.ctr1->array[0].dns_domain_name);
280                 NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
281         }
282
283         return NT_STATUS_OK;
284 }
285
286 /****************************************************************
287 ****************************************************************/
288
289 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
290                                    struct dssync_context *ctx)
291 {
292         NTSTATUS status;
293
294         status = libnet_dssync_bind(mem_ctx, ctx);
295         if (!NT_STATUS_IS_OK(status)) {
296                 return status;
297         }
298
299         if (!ctx->nc_dn) {
300                 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
301         }
302
303         return status;
304 }
305
306 /****************************************************************
307 ****************************************************************/
308
309 static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
310                                             struct dssync_context *ctx,
311                                             const char *dn,
312                                             struct replUpToDateVectorBlob *utdv,
313                                             uint32_t *plevel,
314                                             union drsuapi_DsGetNCChangesRequest *preq)
315 {
316         NTSTATUS status;
317         uint32_t count;
318         uint32_t level;
319         union drsuapi_DsGetNCChangesRequest req;
320         struct dom_sid null_sid;
321         enum drsuapi_DsExtendedOperation extended_op;
322         struct drsuapi_DsReplicaObjectIdentifier *nc = NULL;
323         struct drsuapi_DsReplicaCursorCtrEx *cursors = NULL;
324
325         uint32_t replica_flags  = DRSUAPI_DRS_WRIT_REP |
326                                   DRSUAPI_DRS_INIT_SYNC |
327                                   DRSUAPI_DRS_PER_SYNC |
328                                   DRSUAPI_DRS_GET_ANC |
329                                   DRSUAPI_DRS_NEVER_SYNCED;
330
331         ZERO_STRUCT(null_sid);
332         ZERO_STRUCT(req);
333
334         if (ctx->remote_info28.supported_extensions
335             & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
336         {
337                 level = 8;
338         } else {
339                 level = 5;
340         }
341
342         nc = TALLOC_ZERO_P(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
343         if (!nc) {
344                 status = NT_STATUS_NO_MEMORY;
345                 goto fail;
346         }
347         nc->dn = dn;
348         nc->guid = GUID_zero();
349         nc->sid = null_sid;
350
351         if (!ctx->single_object_replication &&
352             !ctx->force_full_replication && utdv)
353         {
354                 cursors = TALLOC_ZERO_P(mem_ctx,
355                                          struct drsuapi_DsReplicaCursorCtrEx);
356                 if (!cursors) {
357                         status = NT_STATUS_NO_MEMORY;
358                         goto fail;
359                 }
360
361                 switch (utdv->version) {
362                 case 1:
363                         cursors->count = utdv->ctr.ctr1.count;
364                         cursors->cursors = utdv->ctr.ctr1.cursors;
365                         break;
366                 case 2:
367                         cursors->count = utdv->ctr.ctr2.count;
368                         cursors->cursors = talloc_array(cursors,
369                                                 struct drsuapi_DsReplicaCursor,
370                                                 cursors->count);
371                         if (!cursors->cursors) {
372                                 status = NT_STATUS_NO_MEMORY;
373                                 goto fail;
374                         }
375                         for (count = 0; count < cursors->count; count++) {
376                                 cursors->cursors[count].source_dsa_invocation_id =
377                                         utdv->ctr.ctr2.cursors[count].source_dsa_invocation_id;
378                                 cursors->cursors[count].highest_usn =
379                                         utdv->ctr.ctr2.cursors[count].highest_usn;
380                         }
381                         break;
382                 }
383         }
384
385         if (ctx->single_object_replication) {
386                 extended_op = DRSUAPI_EXOP_REPL_OBJ;
387         } else {
388                 extended_op = DRSUAPI_EXOP_NONE;
389         }
390
391         if (level == 8) {
392                 req.req8.naming_context         = nc;
393                 req.req8.replica_flags          = replica_flags;
394                 req.req8.max_object_count       = 402;
395                 req.req8.max_ndr_size           = 402116;
396                 req.req8.uptodateness_vector    = cursors;
397                 req.req8.extended_op            = extended_op;
398         } else if (level == 5) {
399                 req.req5.naming_context         = nc;
400                 req.req5.replica_flags          = replica_flags;
401                 req.req5.max_object_count       = 402;
402                 req.req5.max_ndr_size           = 402116;
403                 req.req5.uptodateness_vector    = cursors;
404                 req.req5.extended_op            = extended_op;
405         } else {
406                 status = NT_STATUS_INVALID_PARAMETER;
407                 goto fail;
408         }
409
410         if (plevel) {
411                 *plevel = level;
412         }
413
414         if (preq) {
415                 *preq = req;
416         }
417
418         return NT_STATUS_OK;
419
420 fail:
421         TALLOC_FREE(nc);
422         TALLOC_FREE(cursors);
423         return status;
424 }
425
426 static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
427                                            struct dssync_context *ctx,
428                                            uint32_t level,
429                                            union drsuapi_DsGetNCChangesRequest *req,
430                                            struct replUpToDateVectorBlob **pnew_utdv)
431 {
432         NTSTATUS status;
433         WERROR werr;
434         union drsuapi_DsGetNCChangesCtr ctr;
435         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
436         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
437         struct replUpToDateVectorBlob *new_utdv = NULL;
438         uint32_t level_out = 0;
439         uint32_t out_level = 0;
440         int y;
441         bool last_query;
442         struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
443
444         if (!ctx->single_object_replication) {
445                 new_utdv = TALLOC_ZERO_P(mem_ctx, struct replUpToDateVectorBlob);
446                 if (!new_utdv) {
447                         status = NT_STATUS_NO_MEMORY;
448                         goto out;
449                 }
450         }
451
452         for (y=0, last_query = false; !last_query; y++) {
453                 struct drsuapi_DsReplicaObjectListItemEx *first_object = NULL;
454                 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr = NULL;
455                 uint32_t linked_attributes_count = 0;
456                 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes = NULL;
457
458                 if (level == 8) {
459                         DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
460                                 (long long)req->req8.highwatermark.tmp_highest_usn,
461                                 (long long)req->req8.highwatermark.highest_usn));
462                 } else if (level == 5) {
463                         DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
464                                 (long long)req->req5.highwatermark.tmp_highest_usn,
465                                 (long long)req->req5.highwatermark.highest_usn));
466                 }
467
468                 status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
469                                                        &ctx->bind_handle,
470                                                        level,
471                                                        req,
472                                                        &level_out,
473                                                        &ctr,
474                                                        &werr);
475                 if (!NT_STATUS_IS_OK(status)) {
476                         ctx->error_message = talloc_asprintf(ctx,
477                                 "Failed to get NC Changes: %s",
478                                 get_friendly_nt_error_msg(status));
479                         goto out;
480                 }
481
482                 if (!W_ERROR_IS_OK(werr)) {
483                         status = werror_to_ntstatus(werr);
484                         ctx->error_message = talloc_asprintf(ctx,
485                                 "Failed to get NC Changes: %s",
486                                 get_friendly_werror_msg(werr));
487                         goto out;
488                 }
489
490                 if (level_out == 1) {
491                         out_level = 1;
492                         ctr1 = &ctr.ctr1;
493                 } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
494                         out_level = 1;
495                         ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
496                 } else if (level_out == 6) {
497                         out_level = 6;
498                         ctr6 = &ctr.ctr6;
499                 } else if (level_out == 7
500                            && ctr.ctr7.level == 6
501                            && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
502                            && ctr.ctr7.ctr.mszip6.ts) {
503                         out_level = 6;
504                         ctr6 = &ctr.ctr7.ctr.mszip6.ts->ctr6;
505                 } else if (level_out == 7
506                            && ctr.ctr7.level == 6
507                            && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
508                            && ctr.ctr7.ctr.xpress6.ts) {
509                         out_level = 6;
510                         ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
511                 }
512
513                 if (out_level == 1) {
514                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
515                                 (long long)ctr1->new_highwatermark.tmp_highest_usn,
516                                 (long long)ctr1->new_highwatermark.highest_usn));
517
518                         first_object = ctr1->first_object;
519                         mapping_ctr = &ctr1->mapping_ctr;
520
521                         if (ctr1->more_data) {
522                                 req->req5.highwatermark = ctr1->new_highwatermark;
523                         } else {
524                                 last_query = true;
525                                 if (ctr1->uptodateness_vector &&
526                                     !ctx->single_object_replication)
527                                 {
528                                         new_utdv->version = 1;
529                                         new_utdv->ctr.ctr1.count =
530                                                 ctr1->uptodateness_vector->count;
531                                         new_utdv->ctr.ctr1.cursors =
532                                                 ctr1->uptodateness_vector->cursors;
533                                 }
534                         }
535                 } else if (out_level == 6) {
536                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
537                                 (long long)ctr6->new_highwatermark.tmp_highest_usn,
538                                 (long long)ctr6->new_highwatermark.highest_usn));
539
540                         first_object = ctr6->first_object;
541                         mapping_ctr = &ctr6->mapping_ctr;
542
543                         linked_attributes = ctr6->linked_attributes;
544                         linked_attributes_count = ctr6->linked_attributes_count;
545
546                         if (ctr6->more_data) {
547                                 req->req8.highwatermark = ctr6->new_highwatermark;
548                         } else {
549                                 last_query = true;
550                                 if (ctr6->uptodateness_vector &&
551                                     !ctx->single_object_replication)
552                                 {
553                                         new_utdv->version = 2;
554                                         new_utdv->ctr.ctr2.count =
555                                                 ctr6->uptodateness_vector->count;
556                                         new_utdv->ctr.ctr2.cursors =
557                                                 ctr6->uptodateness_vector->cursors;
558                                 }
559                         }
560                 }
561
562                 status = cli_get_session_key(mem_ctx, ctx->cli, &ctx->session_key);
563                 if (!NT_STATUS_IS_OK(status)) {
564                         ctx->error_message = talloc_asprintf(ctx,
565                                 "Failed to get Session Key: %s",
566                                 nt_errstr(status));
567                         goto out;
568                 }
569
570                 libnet_dssync_decrypt_attributes(mem_ctx,
571                                                  &ctx->session_key,
572                                                  first_object);
573
574                 if (ctx->ops->process_objects) {
575                         status = ctx->ops->process_objects(ctx, mem_ctx,
576                                                            first_object,
577                                                            mapping_ctr);
578                         if (!NT_STATUS_IS_OK(status)) {
579                                 ctx->error_message = talloc_asprintf(ctx,
580                                         "Failed to call processing function: %s",
581                                         nt_errstr(status));
582                                 goto out;
583                         }
584                 }
585
586                 if (linked_attributes_count == 0) {
587                         continue;
588                 }
589
590                 if (ctx->ops->process_links) {
591                         status = ctx->ops->process_links(ctx, mem_ctx,
592                                                          linked_attributes_count,
593                                                          linked_attributes,
594                                                          mapping_ctr);
595                         if (!NT_STATUS_IS_OK(status)) {
596                                 ctx->error_message = talloc_asprintf(ctx,
597                                         "Failed to call processing function: %s",
598                                         nt_errstr(status));
599                                 goto out;
600                         }
601                 }
602         }
603
604         *pnew_utdv = new_utdv;
605
606 out:
607         return status;
608 }
609
610 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
611                                       struct dssync_context *ctx)
612 {
613         NTSTATUS status;
614
615         uint32_t level = 0;
616         union drsuapi_DsGetNCChangesRequest req;
617         struct replUpToDateVectorBlob *old_utdv = NULL;
618         struct replUpToDateVectorBlob *pnew_utdv = NULL;
619         const char **dns;
620         uint32_t dn_count;
621         uint32_t count;
622
623         if (ctx->ops->startup) {
624                 status = ctx->ops->startup(ctx, mem_ctx, &old_utdv);
625                 if (!NT_STATUS_IS_OK(status)) {
626                         ctx->error_message = talloc_asprintf(ctx,
627                                 "Failed to call startup operation: %s",
628                                 nt_errstr(status));
629                         goto out;
630                 }
631         }
632
633         if (ctx->single_object_replication && ctx->object_dns) {
634                 dns = ctx->object_dns;
635                 dn_count = ctx->object_count;
636         } else {
637                 dns = &ctx->nc_dn;
638                 dn_count = 1;
639         }
640
641         status = NT_STATUS_OK;
642
643         for (count=0; count < dn_count; count++) {
644                 status = libnet_dssync_build_request(mem_ctx, ctx,
645                                                      dns[count],
646                                                      old_utdv, &level,
647                                                      &req);
648                 if (!NT_STATUS_IS_OK(status)) {
649                         goto out;
650                 }
651
652                 status = libnet_dssync_getncchanges(mem_ctx, ctx, level, &req,
653                                                     &pnew_utdv);
654                 if (!NT_STATUS_IS_OK(status)) {
655                         ctx->error_message = talloc_asprintf(ctx,
656                                 "Failed to call DsGetNCCHanges: %s",
657                                 nt_errstr(status));
658                         goto out;
659                 }
660         }
661
662         if (ctx->ops->finish) {
663                 status = ctx->ops->finish(ctx, mem_ctx, pnew_utdv);
664                 if (!NT_STATUS_IS_OK(status)) {
665                         ctx->error_message = talloc_asprintf(ctx,
666                                 "Failed to call finishing operation: %s",
667                                 nt_errstr(status));
668                         goto out;
669                 }
670         }
671
672  out:
673         return status;
674 }
675
676 /****************************************************************
677 ****************************************************************/
678
679 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
680                        struct dssync_context *ctx)
681 {
682         NTSTATUS status;
683         TALLOC_CTX *tmp_ctx;
684
685         tmp_ctx = talloc_new(mem_ctx);
686         if (!tmp_ctx) {
687                 return NT_STATUS_NO_MEMORY;
688         }
689
690         status = libnet_dssync_init(tmp_ctx, ctx);
691         if (!NT_STATUS_IS_OK(status)) {
692                 goto out;
693         }
694
695         status = libnet_dssync_process(tmp_ctx, ctx);
696         if (!NT_STATUS_IS_OK(status)) {
697                 goto out;
698         }
699
700  out:
701         TALLOC_FREE(tmp_ctx);
702         return status;
703 }
704