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