dssync: replace the processing_fn by startup/process/finish ops.
[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         return status;
245 }
246
247 /****************************************************************
248 ****************************************************************/
249
250 static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
251                                         struct dssync_context *ctx)
252 {
253         NTSTATUS status;
254         WERROR werr;
255         int32_t level = 1;
256         union drsuapi_DsNameRequest req;
257         int32_t level_out;
258         struct drsuapi_DsNameString names[1];
259         union drsuapi_DsNameCtr ctr;
260
261         names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
262         NT_STATUS_HAVE_NO_MEMORY(names[0].str);
263
264         req.req1.codepage       = 1252; /* german */
265         req.req1.language       = 0x00000407; /* german */
266         req.req1.count          = 1;
267         req.req1.names          = names;
268         req.req1.format_flags   = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
269         req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_UKNOWN;
270         req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
271
272         status = rpccli_drsuapi_DsCrackNames(ctx->cli, mem_ctx,
273                                              &ctx->bind_handle,
274                                              level,
275                                              &req,
276                                              &level_out,
277                                              &ctr,
278                                              &werr);
279         if (!NT_STATUS_IS_OK(status)) {
280                 ctx->error_message = talloc_asprintf(mem_ctx,
281                         "Failed to lookup DN for domain name: %s",
282                         get_friendly_werror_msg(werr));
283                 return status;
284         }
285
286         if (!W_ERROR_IS_OK(werr)) {
287                 return werror_to_ntstatus(werr);
288         }
289
290         if (ctr.ctr1->count != 1) {
291                 return NT_STATUS_UNSUCCESSFUL;
292         }
293
294         if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
295                 return NT_STATUS_UNSUCCESSFUL;
296         }
297
298         ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
299         NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
300
301         if (!ctx->dns_domain_name) {
302                 ctx->dns_domain_name = talloc_strdup_upper(mem_ctx,
303                         ctr.ctr1->array[0].dns_domain_name);
304                 NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
305         }
306
307         return NT_STATUS_OK;
308 }
309
310 /****************************************************************
311 ****************************************************************/
312
313 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
314                                    struct dssync_context *ctx)
315 {
316         NTSTATUS status;
317
318         status = libnet_dssync_bind(mem_ctx, ctx);
319         if (!NT_STATUS_IS_OK(status)) {
320                 return status;
321         }
322
323         if (!ctx->nc_dn) {
324                 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
325         }
326
327         return status;
328 }
329
330 /****************************************************************
331 ****************************************************************/
332
333 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
334                                       struct dssync_context *ctx)
335 {
336         NTSTATUS status;
337         WERROR werr;
338
339         int32_t level = 8;
340         int32_t level_out = 0;
341         union drsuapi_DsGetNCChangesRequest req;
342         union drsuapi_DsGetNCChangesCtr ctr;
343         struct drsuapi_DsReplicaObjectIdentifier nc;
344         struct dom_sid null_sid;
345
346         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
347         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
348         int32_t out_level = 0;
349         int y;
350
351         ZERO_STRUCT(null_sid);
352         ZERO_STRUCT(req);
353
354         nc.dn = ctx->nc_dn;
355         nc.guid = GUID_zero();
356         nc.sid = null_sid;
357
358         status = ctx->ops->startup(ctx, mem_ctx);
359         if (!NT_STATUS_IS_OK(status)) {
360                 ctx->error_message = talloc_asprintf(mem_ctx,
361                         "Failed to call startup operation: %s",
362                         nt_errstr(status));
363                 goto out;
364         }
365
366         req.req8.naming_context         = &nc;
367         req.req8.replica_flags          = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE |
368                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP |
369                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS |
370                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS |
371                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED;
372         req.req8.max_object_count       = 402;
373         req.req8.max_ndr_size           = 402116;
374
375         for (y=0; ;y++) {
376
377                 bool last_query = true;
378
379                 if (level == 8) {
380                         DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
381                                 (long long)req.req8.highwatermark.tmp_highest_usn,
382                                 (long long)req.req8.highwatermark.highest_usn));
383                 }
384
385                 status = rpccli_drsuapi_DsGetNCChanges(ctx->cli, mem_ctx,
386                                                        &ctx->bind_handle,
387                                                        level,
388                                                        &req,
389                                                        &level_out,
390                                                        &ctr,
391                                                        &werr);
392                 if (!NT_STATUS_IS_OK(status)) {
393                         ctx->error_message = talloc_asprintf(mem_ctx,
394                                 "Failed to get NC Changes: %s",
395                                 get_friendly_werror_msg(werr));
396                         goto out;
397                 }
398
399                 if (!W_ERROR_IS_OK(werr)) {
400                         status = werror_to_ntstatus(werr);
401                         goto out;
402                 }
403
404                 if (level_out == 1) {
405                         out_level = 1;
406                         ctr1 = &ctr.ctr1;
407                 } else if (level_out == 2) {
408                         out_level = 1;
409                         ctr1 = ctr.ctr2.ctr.mszip1.ctr1;
410                 }
411
412                 status = cli_get_session_key(mem_ctx, ctx->cli, &ctx->session_key);
413                 if (!NT_STATUS_IS_OK(status)) {
414                         ctx->error_message = talloc_asprintf(mem_ctx,
415                                 "Failed to get Session Key: %s",
416                                 nt_errstr(status));
417                         return status;
418                 }
419
420                 if (out_level == 1) {
421                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
422                                 (long long)ctr1->new_highwatermark.tmp_highest_usn,
423                                 (long long)ctr1->new_highwatermark.highest_usn));
424
425                         libnet_dssync_decrypt_attributes(mem_ctx,
426                                                          &ctx->session_key,
427                                                          ctr1->first_object);
428
429                         if (ctr1->more_data) {
430                                 req.req5.highwatermark = ctr1->new_highwatermark;
431                                 last_query = false;
432                         }
433
434                         if (ctx->ops->process_objects) {
435                                 status = ctx->ops->process_objects(ctx, mem_ctx,
436                                                                    ctr1->first_object,
437                                                                    &ctr1->mapping_ctr);
438                                 if (!NT_STATUS_IS_OK(status)) {
439                                         ctx->error_message = talloc_asprintf(mem_ctx,
440                                                 "Failed to call processing function: %s",
441                                                 nt_errstr(status));
442                                         goto out;
443                                 }
444                         }
445
446                         if (!last_query) {
447                                 continue;
448                         }
449                 }
450
451                 if (level_out == 6) {
452                         out_level = 6;
453                         ctr6 = &ctr.ctr6;
454                 } else if (level_out == 7
455                            && ctr.ctr7.level == 6
456                            && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
457                         out_level = 6;
458                         ctr6 = ctr.ctr7.ctr.mszip6.ctr6;
459                 }
460
461                 if (out_level == 6) {
462                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
463                                 (long long)ctr6->new_highwatermark.tmp_highest_usn,
464                                 (long long)ctr6->new_highwatermark.highest_usn));
465
466                         libnet_dssync_decrypt_attributes(mem_ctx,
467                                                          &ctx->session_key,
468                                                          ctr6->first_object);
469
470                         if (ctr6->more_data) {
471                                 req.req8.highwatermark = ctr6->new_highwatermark;
472                                 last_query = false;
473                         }
474
475                         if (ctx->ops->process_objects) {
476                                 status = ctx->ops->process_objects(ctx, mem_ctx,
477                                                                    ctr6->first_object,
478                                                                    &ctr6->mapping_ctr);
479                                 if (!NT_STATUS_IS_OK(status)) {
480                                         ctx->error_message = talloc_asprintf(mem_ctx,
481                                                 "Failed to call processing function: %s",
482                                                 nt_errstr(status));
483                                         goto out;
484                                 }
485                         }
486
487                         if (!last_query) {
488                                 continue;
489                         }
490                 }
491
492                 status = ctx->ops->finish(ctx, mem_ctx);
493                 if (!NT_STATUS_IS_OK(status)) {
494                         ctx->error_message = talloc_asprintf(mem_ctx,
495                                 "Failed to call finishing operation: %s",
496                                 nt_errstr(status));
497                         goto out;
498                 }
499
500                 break;
501         }
502
503  out:
504         return status;
505 }
506
507 /****************************************************************
508 ****************************************************************/
509
510 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
511                        struct dssync_context *ctx)
512 {
513         NTSTATUS status;
514
515         status = libnet_dssync_init(mem_ctx, ctx);
516         if (!NT_STATUS_IS_OK(status)) {
517                 goto out;
518         }
519
520         status = libnet_dssync_process(mem_ctx, ctx);
521         if (!NT_STATUS_IS_OK(status)) {
522                 goto out;
523         }
524
525  out:
526         return status;
527 }