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