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