80b398ef5c93ba49d37e34b1c8efec3e94ec50f0
[kai/samba-autobuild/.git] / source4 / dsdb / repl / drepl_out_helpers.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB replication service helper function for outgoing traffic
4    
5    Copyright (C) Stefan Metzmacher 2007
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 "dsdb/samdb/samdb.h"
24 #include "auth/auth.h"
25 #include "smbd/service.h"
26 #include "lib/events/events.h"
27 #include "lib/messaging/irpc.h"
28 #include "dsdb/repl/drepl_service.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "lib/util/dlinklist.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "librpc/gen_ndr/ndr_drsuapi.h"
33 #include "librpc/gen_ndr/ndr_drsblobs.h"
34 #include "libcli/composite/composite.h"
35 #include "auth/gensec/gensec.h"
36
37 struct dreplsrv_out_drsuapi_state {
38         struct composite_context *creq;
39
40         struct dreplsrv_out_connection *conn;
41
42         struct dreplsrv_drsuapi_connection *drsuapi;
43
44         struct drsuapi_DsBindInfoCtr bind_info_ctr;
45         struct drsuapi_DsBind bind_r;
46 };
47
48 static void dreplsrv_out_drsuapi_connect_recv(struct composite_context *creq);
49
50 static struct composite_context *dreplsrv_out_drsuapi_send(struct dreplsrv_out_connection *conn)
51 {
52         struct composite_context *c;
53         struct composite_context *creq;
54         struct dreplsrv_out_drsuapi_state *st;
55
56         c = composite_create(conn, conn->service->task->event_ctx);
57         if (c == NULL) return NULL;
58
59         st = talloc_zero(c, struct dreplsrv_out_drsuapi_state);
60         if (composite_nomem(st, c)) return c;
61
62         c->private_data = st;
63
64         st->creq        = c;
65         st->conn        = conn;
66         st->drsuapi     = conn->drsuapi;
67
68         if (st->drsuapi && !st->drsuapi->pipe->conn->dead) {
69                 composite_done(c);
70                 return c;
71         } else if (st->drsuapi && st->drsuapi->pipe->conn->dead) {
72                 talloc_free(st->drsuapi);
73                 conn->drsuapi = NULL;
74         }
75
76         st->drsuapi     = talloc_zero(st, struct dreplsrv_drsuapi_connection);
77         if (composite_nomem(st->drsuapi, c)) return c;
78
79         creq = dcerpc_pipe_connect_b_send(st, conn->binding, &ndr_table_drsuapi,
80                                           conn->service->system_session_info->credentials,
81                                           c->event_ctx, conn->service->task->lp_ctx);
82         composite_continue(c, creq, dreplsrv_out_drsuapi_connect_recv, st);
83
84         return c;
85 }
86
87 static void dreplsrv_out_drsuapi_bind_send(struct dreplsrv_out_drsuapi_state *st);
88
89 static void dreplsrv_out_drsuapi_connect_recv(struct composite_context *creq)
90 {
91         struct dreplsrv_out_drsuapi_state *st = talloc_get_type(creq->async.private_data,
92                                                 struct dreplsrv_out_drsuapi_state);
93         struct composite_context *c = st->creq;
94
95         c->status = dcerpc_pipe_connect_b_recv(creq, st->drsuapi, &st->drsuapi->pipe);
96         if (!composite_is_ok(c)) return;
97
98         c->status = gensec_session_key(st->drsuapi->pipe->conn->security_state.generic_state,
99                                        &st->drsuapi->gensec_skey);
100         if (!composite_is_ok(c)) return;
101
102         dreplsrv_out_drsuapi_bind_send(st);
103 }
104
105 static void dreplsrv_out_drsuapi_bind_recv(struct rpc_request *req);
106
107 static void dreplsrv_out_drsuapi_bind_send(struct dreplsrv_out_drsuapi_state *st)
108 {
109         struct composite_context *c = st->creq;
110         struct rpc_request *req;
111
112         st->bind_info_ctr.length        = 28;
113         st->bind_info_ctr.info.info28   = st->conn->service->bind_info28;
114
115         st->bind_r.in.bind_guid = &st->conn->service->ntds_guid;
116         st->bind_r.in.bind_info = &st->bind_info_ctr;
117         st->bind_r.out.bind_handle = &st->drsuapi->bind_handle;
118
119         req = dcerpc_drsuapi_DsBind_send(st->drsuapi->pipe, st, &st->bind_r);
120         composite_continue_rpc(c, req, dreplsrv_out_drsuapi_bind_recv, st);
121 }
122
123 static void dreplsrv_out_drsuapi_bind_recv(struct rpc_request *req)
124 {
125         struct dreplsrv_out_drsuapi_state *st = talloc_get_type(req->async.private_data,
126                                                 struct dreplsrv_out_drsuapi_state);
127         struct composite_context *c = st->creq;
128
129         c->status = dcerpc_ndr_request_recv(req);
130         if (!composite_is_ok(c)) return;
131
132         if (!W_ERROR_IS_OK(st->bind_r.out.result)) {
133                 composite_error(c, werror_to_ntstatus(st->bind_r.out.result));
134                 return;
135         }
136
137         ZERO_STRUCT(st->drsuapi->remote_info28);
138         if (st->bind_r.out.bind_info) {
139                 switch (st->bind_r.out.bind_info->length) {
140                 case 24: {
141                         struct drsuapi_DsBindInfo24 *info24;
142                         info24 = &st->bind_r.out.bind_info->info.info24;
143                         st->drsuapi->remote_info28.supported_extensions = info24->supported_extensions;
144                         st->drsuapi->remote_info28.site_guid            = info24->site_guid;
145                         st->drsuapi->remote_info28.pid                  = info24->pid;
146                         st->drsuapi->remote_info28.repl_epoch           = 0;
147                         break;
148                 }
149                 case 48: {
150                         struct drsuapi_DsBindInfo48 *info48;
151                         info48 = &st->bind_r.out.bind_info->info.info48;
152                         st->drsuapi->remote_info28.supported_extensions = info48->supported_extensions;
153                         st->drsuapi->remote_info28.site_guid            = info48->site_guid;
154                         st->drsuapi->remote_info28.pid                  = info48->pid;
155                         st->drsuapi->remote_info28.repl_epoch           = info48->repl_epoch;
156                         break;
157                 }
158                 case 28:
159                         st->drsuapi->remote_info28 = st->bind_r.out.bind_info->info.info28;
160                         break;
161                 }
162         }
163
164         composite_done(c);
165 }
166
167 static NTSTATUS dreplsrv_out_drsuapi_recv(struct composite_context *c)
168 {
169         NTSTATUS status;
170         struct dreplsrv_out_drsuapi_state *st = talloc_get_type(c->private_data,
171                                                 struct dreplsrv_out_drsuapi_state);
172
173         status = composite_wait(c);
174
175         if (NT_STATUS_IS_OK(status)) {
176                 st->conn->drsuapi = talloc_steal(st->conn, st->drsuapi);
177         }
178
179         talloc_free(c);
180         return status;
181 }
182
183 struct dreplsrv_op_pull_source_state {
184         struct composite_context *creq;
185
186         struct dreplsrv_out_operation *op;
187
188         struct dreplsrv_drsuapi_connection *drsuapi;
189
190         bool have_all;
191
192         uint32_t ctr_level;
193         struct drsuapi_DsGetNCChangesCtr1 *ctr1;
194         struct drsuapi_DsGetNCChangesCtr6 *ctr6;
195 };
196
197 static void dreplsrv_op_pull_source_connect_recv(struct composite_context *creq);
198
199 struct composite_context *dreplsrv_op_pull_source_send(struct dreplsrv_out_operation *op)
200 {
201         struct composite_context *c;
202         struct composite_context *creq;
203         struct dreplsrv_op_pull_source_state *st;
204
205         c = composite_create(op, op->service->task->event_ctx);
206         if (c == NULL) return NULL;
207
208         st = talloc_zero(c, struct dreplsrv_op_pull_source_state);
209         if (composite_nomem(st, c)) return c;
210
211         st->creq        = c;
212         st->op          = op;
213
214         creq = dreplsrv_out_drsuapi_send(op->source_dsa->conn);
215         composite_continue(c, creq, dreplsrv_op_pull_source_connect_recv, st);
216
217         return c;
218 }
219
220 static void dreplsrv_op_pull_source_get_changes_send(struct dreplsrv_op_pull_source_state *st);
221
222 static void dreplsrv_op_pull_source_connect_recv(struct composite_context *creq)
223 {
224         struct dreplsrv_op_pull_source_state *st = talloc_get_type(creq->async.private_data,
225                                                    struct dreplsrv_op_pull_source_state);
226         struct composite_context *c = st->creq;
227
228         c->status = dreplsrv_out_drsuapi_recv(creq);
229         if (!composite_is_ok(c)) return;
230
231         dreplsrv_op_pull_source_get_changes_send(st);
232 }
233
234 static void dreplsrv_op_pull_source_get_changes_recv(struct rpc_request *req);
235
236 static void dreplsrv_op_pull_source_get_changes_send(struct dreplsrv_op_pull_source_state *st)
237 {
238         struct composite_context *c = st->creq;
239         struct repsFromTo1 *rf1 = st->op->source_dsa->repsFrom1;
240         struct dreplsrv_service *service = st->op->service;
241         struct dreplsrv_partition *partition = st->op->source_dsa->partition;
242         struct dreplsrv_drsuapi_connection *drsuapi = st->op->source_dsa->conn->drsuapi;
243         struct rpc_request *req;
244         struct drsuapi_DsGetNCChanges *r;
245
246         r = talloc(st, struct drsuapi_DsGetNCChanges);
247         if (composite_nomem(r, c)) return;
248
249         r->in.level = talloc(r, int32_t);
250         if (composite_nomem(r->in.level, c)) return;
251         r->out.level = talloc(r, int32_t);
252         if (composite_nomem(r->out.level, c)) return;
253
254         r->in.bind_handle       = &drsuapi->bind_handle;
255         if (drsuapi->remote_info28.supported_extensions & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8) {
256                 *r->in.level                            = 8;
257                 r->in.req.req8.destination_dsa_guid     = service->ntds_guid;
258                 r->in.req.req8.source_dsa_invocation_id = rf1->source_dsa_invocation_id;
259                 r->in.req.req8.naming_context           = &partition->nc;
260                 r->in.req.req8.highwatermark            = rf1->highwatermark;
261                 r->in.req.req8.uptodateness_vector      = NULL;/*&partition->uptodatevector_ex;*/
262                 r->in.req.req8.replica_flags            = rf1->replica_flags;
263                 r->in.req.req8.max_object_count         = 133;
264                 r->in.req.req8.max_ndr_size             = 1336811;
265                 r->in.req.req8.extended_op              = DRSUAPI_EXOP_NONE;
266                 r->in.req.req8.fsmo_info                = 0;
267                 r->in.req.req8.partial_attribute_set    = NULL;
268                 r->in.req.req8.partial_attribute_set_ex = NULL;
269                 r->in.req.req8.mapping_ctr.num_mappings = 0;
270                 r->in.req.req8.mapping_ctr.mappings     = NULL;
271         } else {
272                 *r->in.level                            = 5;
273                 r->in.req.req5.destination_dsa_guid     = service->ntds_guid;
274                 r->in.req.req5.source_dsa_invocation_id = rf1->source_dsa_invocation_id;
275                 r->in.req.req5.naming_context           = &partition->nc;
276                 r->in.req.req5.highwatermark            = rf1->highwatermark;
277                 r->in.req.req5.uptodateness_vector      = NULL;/*&partition->uptodatevector_ex;*/
278                 r->in.req.req5.replica_flags            = rf1->replica_flags;
279                 r->in.req.req5.max_object_count         = 133;
280                 r->in.req.req5.max_ndr_size             = 1336770;
281                 r->in.req.req5.extended_op              = DRSUAPI_EXOP_NONE;
282                 r->in.req.req5.fsmo_info                = 0;
283         }
284
285         req = dcerpc_drsuapi_DsGetNCChanges_send(drsuapi->pipe, r, r);
286         composite_continue_rpc(c, req, dreplsrv_op_pull_source_get_changes_recv, st);
287 }
288
289 static void dreplsrv_op_pull_source_apply_changes_send(struct dreplsrv_op_pull_source_state *st,
290                                                        struct drsuapi_DsGetNCChanges *r,
291                                                        uint32_t ctr_level,
292                                                        struct drsuapi_DsGetNCChangesCtr1 *ctr1,
293                                                        struct drsuapi_DsGetNCChangesCtr6 *ctr6);
294
295 static void dreplsrv_op_pull_source_get_changes_recv(struct rpc_request *req)
296 {
297         struct dreplsrv_op_pull_source_state *st = talloc_get_type(req->async.private_data,
298                                                    struct dreplsrv_op_pull_source_state);
299         struct composite_context *c = st->creq;
300         struct drsuapi_DsGetNCChanges *r = talloc_get_type(req->ndr.struct_ptr,
301                                            struct drsuapi_DsGetNCChanges);
302         uint32_t ctr_level = 0;
303         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
304         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
305
306         c->status = dcerpc_ndr_request_recv(req);
307         if (!composite_is_ok(c)) return;
308
309         if (!W_ERROR_IS_OK(r->out.result)) {
310                 composite_error(c, werror_to_ntstatus(r->out.result));
311                 return;
312         }
313
314         if (*r->out.level == 1) {
315                 ctr_level = 1;
316                 ctr1 = &r->out.ctr.ctr1;
317         } else if (*r->out.level == 2 &&
318                    r->out.ctr.ctr2.mszip1.ts) {
319                 ctr_level = 1;
320                 ctr1 = &r->out.ctr.ctr2.mszip1.ts->ctr1;
321         } else if (*r->out.level == 6) {
322                 ctr_level = 6;
323                 ctr6 = &r->out.ctr.ctr6;
324         } else if (*r->out.level == 7 &&
325                    r->out.ctr.ctr7.level == 6 &&
326                    r->out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP &&
327                    r->out.ctr.ctr7.ctr.mszip6.ts) {
328                 ctr_level = 6;
329                 ctr6 = &r->out.ctr.ctr7.ctr.mszip6.ts->ctr6;
330         } else if (*r->out.level == 7 &&
331                    r->out.ctr.ctr7.level == 6 &&
332                    r->out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS &&
333                    r->out.ctr.ctr7.ctr.xpress6.ts) {
334                 ctr_level = 6;
335                 ctr6 = &r->out.ctr.ctr7.ctr.xpress6.ts->ctr6;
336         } else {
337                 composite_error(c, werror_to_ntstatus(WERR_BAD_NET_RESP));
338                 return;
339         }
340
341         if (!ctr1 && !ctr6) {
342                 composite_error(c, werror_to_ntstatus(WERR_BAD_NET_RESP));
343                 return;
344         }
345
346         if (ctr_level == 6) {
347                 if (!W_ERROR_IS_OK(ctr6->drs_error)) {
348                         composite_error(c, werror_to_ntstatus(ctr6->drs_error));
349                         return;
350                 }
351         }
352
353         dreplsrv_op_pull_source_apply_changes_send(st, r, ctr_level, ctr1, ctr6);
354 }
355
356 static void dreplsrv_op_pull_source_apply_changes_send(struct dreplsrv_op_pull_source_state *st,
357                                                        struct drsuapi_DsGetNCChanges *r,
358                                                        uint32_t ctr_level,
359                                                        struct drsuapi_DsGetNCChangesCtr1 *ctr1,
360                                                        struct drsuapi_DsGetNCChangesCtr6 *ctr6)
361 {
362         struct composite_context *c = st->creq;
363         struct repsFromTo1 rf1 = *st->op->source_dsa->repsFrom1;
364         struct dreplsrv_service *service = st->op->service;
365         struct dreplsrv_partition *partition = st->op->source_dsa->partition;
366         struct dreplsrv_drsuapi_connection *drsuapi = st->op->source_dsa->conn->drsuapi;
367         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
368         uint32_t object_count;
369         struct drsuapi_DsReplicaObjectListItemEx *first_object;
370         uint32_t linked_attributes_count;
371         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
372         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
373         bool more_data = false;
374         WERROR status;
375
376         switch (ctr_level) {
377         case 1:
378                 mapping_ctr                     = &ctr1->mapping_ctr;
379                 object_count                    = ctr1->object_count;
380                 first_object                    = ctr1->first_object;
381                 linked_attributes_count         = 0;
382                 linked_attributes               = NULL;
383                 rf1.highwatermark               = ctr1->new_highwatermark;
384                 uptodateness_vector             = NULL; /* TODO: map it */
385                 more_data                       = ctr1->more_data;
386                 break;
387         case 6:
388                 mapping_ctr                     = &ctr6->mapping_ctr;
389                 object_count                    = ctr6->object_count;
390                 first_object                    = ctr6->first_object;
391                 linked_attributes_count         = ctr6->linked_attributes_count;
392                 linked_attributes               = ctr6->linked_attributes;
393                 rf1.highwatermark               = ctr6->new_highwatermark;
394                 uptodateness_vector             = ctr6->uptodateness_vector;
395                 more_data                       = ctr6->more_data;
396                 break;
397         default:
398                 composite_error(c, werror_to_ntstatus(WERR_BAD_NET_RESP));
399                 return;
400         }
401
402         status = dsdb_extended_replicated_objects_commit(service->samdb,
403                                                          partition->nc.dn,
404                                                          mapping_ctr,
405                                                          object_count,
406                                                          first_object,
407                                                          linked_attributes_count,
408                                                          linked_attributes,
409                                                          &rf1,
410                                                          uptodateness_vector,
411                                                          &drsuapi->gensec_skey,
412                                                          st, NULL);
413         if (!W_ERROR_IS_OK(status)) {
414                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
415                 composite_error(c, werror_to_ntstatus(status));
416                 return;
417         }
418
419         /* if it applied fine, we need to update the highwatermark */
420         *st->op->source_dsa->repsFrom1 = rf1;
421
422         /*
423          * TODO: update our uptodatevector!
424          */
425
426         if (more_data) {
427                 dreplsrv_op_pull_source_get_changes_send(st);
428                 return;
429         }
430
431         composite_done(c);
432 }
433
434 WERROR dreplsrv_op_pull_source_recv(struct composite_context *c)
435 {
436         NTSTATUS status;
437
438         status = composite_wait(c);
439
440         talloc_free(c);
441         return ntstatus_to_werror(status);
442 }