r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / wrepl_server / wrepl_out_helpers.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "lib/socket/socket.h"
26 #include "smbd/service_task.h"
27 #include "smbd/service_stream.h"
28 #include "librpc/gen_ndr/ndr_winsrepl.h"
29 #include "wrepl_server/wrepl_server.h"
30 #include "wrepl_server/wrepl_out_helpers.h"
31 #include "libcli/composite/composite.h"
32 #include "libcli/wrepl/winsrepl.h"
33
34 enum wreplsrv_out_connect_stage {
35         WREPLSRV_OUT_CONNECT_STAGE_WAIT_SOCKET,
36         WREPLSRV_OUT_CONNECT_STAGE_WAIT_ASSOC_CTX,
37         WREPLSRV_OUT_CONNECT_STAGE_DONE
38 };
39
40 struct wreplsrv_out_connect_state {
41         enum wreplsrv_out_connect_stage stage;
42         struct composite_context *c;
43         struct wrepl_request *req;
44         struct composite_context *c_req;
45         struct wrepl_associate assoc_io;
46         enum winsrepl_partner_type type;
47         struct wreplsrv_out_connection *wreplconn;
48 };
49
50 static void wreplsrv_out_connect_handler_creq(struct composite_context *c_req);
51 static void wreplsrv_out_connect_handler_req(struct wrepl_request *req);
52
53 static NTSTATUS wreplsrv_out_connect_wait_socket(struct wreplsrv_out_connect_state *state)
54 {
55         NTSTATUS status;
56
57         status = wrepl_connect_recv(state->c_req);
58         NT_STATUS_NOT_OK_RETURN(status);
59
60         state->req = wrepl_associate_send(state->wreplconn->sock, &state->assoc_io);
61         NT_STATUS_HAVE_NO_MEMORY(state->req);
62
63         state->req->async.fn            = wreplsrv_out_connect_handler_req;
64         state->req->async.private       = state;
65
66         state->stage = WREPLSRV_OUT_CONNECT_STAGE_WAIT_ASSOC_CTX;
67
68         return NT_STATUS_OK;
69 }
70
71 static NTSTATUS wreplsrv_out_connect_wait_assoc_ctx(struct wreplsrv_out_connect_state *state)
72 {
73         NTSTATUS status;
74
75         status = wrepl_associate_recv(state->req, &state->assoc_io);
76         NT_STATUS_NOT_OK_RETURN(status);
77
78         state->wreplconn->assoc_ctx.peer_ctx = state->assoc_io.out.assoc_ctx;
79
80         if (state->type == WINSREPL_PARTNER_PUSH) {
81                 state->wreplconn->partner->push.wreplconn = state->wreplconn;
82                 talloc_steal(state->wreplconn->partner, state->wreplconn);
83         } else if (state->type == WINSREPL_PARTNER_PULL) {
84                 state->wreplconn->partner->pull.wreplconn = state->wreplconn;
85                 talloc_steal(state->wreplconn->partner, state->wreplconn);
86         }
87
88         state->stage = WREPLSRV_OUT_CONNECT_STAGE_DONE;
89
90         return NT_STATUS_OK;
91 }
92
93 static void wreplsrv_out_connect_handler(struct wreplsrv_out_connect_state *state)
94 {
95         struct composite_context *c = state->c;
96
97         switch (state->stage) {
98         case WREPLSRV_OUT_CONNECT_STAGE_WAIT_SOCKET:
99                 c->status = wreplsrv_out_connect_wait_socket(state);
100                 break;
101         case WREPLSRV_OUT_CONNECT_STAGE_WAIT_ASSOC_CTX:
102                 c->status = wreplsrv_out_connect_wait_assoc_ctx(state);
103                 c->state  = COMPOSITE_STATE_DONE;
104                 break;
105         case WREPLSRV_OUT_CONNECT_STAGE_DONE:
106                 c->status = NT_STATUS_INTERNAL_ERROR;
107         }
108
109         if (!NT_STATUS_IS_OK(c->status)) {
110                 c->state = COMPOSITE_STATE_ERROR;
111         }
112
113         if (c->state >= COMPOSITE_STATE_DONE && c->async.fn) {
114                 c->async.fn(c);
115         }
116 }
117
118 static void wreplsrv_out_connect_handler_creq(struct composite_context *creq)
119 {
120         struct wreplsrv_out_connect_state *state = talloc_get_type(creq->async.private_data,
121                                                    struct wreplsrv_out_connect_state);
122         wreplsrv_out_connect_handler(state);
123         return;
124 }
125
126 static void wreplsrv_out_connect_handler_req(struct wrepl_request *req)
127 {
128         struct wreplsrv_out_connect_state *state = talloc_get_type(req->async.private,
129                                                    struct wreplsrv_out_connect_state);
130         wreplsrv_out_connect_handler(state);
131         return;
132 }
133
134 static struct composite_context *wreplsrv_out_connect_send(struct wreplsrv_partner *partner,
135                                                            enum winsrepl_partner_type type,
136                                                            struct wreplsrv_out_connection *wreplconn)
137 {
138         struct composite_context *c = NULL;
139         struct wreplsrv_service *service = partner->service;
140         struct wreplsrv_out_connect_state *state = NULL;
141         struct wreplsrv_out_connection **wreplconnp = &wreplconn;
142         BOOL cached_connection = False;
143
144         c = talloc_zero(partner, struct composite_context);
145         if (!c) goto failed;
146
147         state = talloc_zero(c, struct wreplsrv_out_connect_state);
148         if (!state) goto failed;
149         state->c        = c;
150         state->type     = type;
151
152         c->state        = COMPOSITE_STATE_IN_PROGRESS;
153         c->event_ctx    = service->task->event_ctx;
154         c->private_data = state;
155
156         if (type == WINSREPL_PARTNER_PUSH) {
157                 cached_connection       = True;
158                 wreplconn               = partner->push.wreplconn;
159                 wreplconnp              = &partner->push.wreplconn;
160         } else if (type == WINSREPL_PARTNER_PULL) {
161                 cached_connection       = True;
162                 wreplconn               = partner->pull.wreplconn;
163                 wreplconnp              = &partner->pull.wreplconn;
164         }
165
166         /* we have a connection already, so use it */
167         if (wreplconn) {
168                 if (!wreplconn->sock->dead) {
169                         state->stage    = WREPLSRV_OUT_CONNECT_STAGE_DONE;
170                         state->wreplconn= wreplconn;
171                         composite_done(c);
172                         return c;
173                 } else if (!cached_connection) {
174                         state->stage    = WREPLSRV_OUT_CONNECT_STAGE_DONE;
175                         state->wreplconn= NULL;
176                         composite_done(c);
177                         return c;
178                 } else {
179                         talloc_free(wreplconn);
180                         *wreplconnp = NULL;
181                 }
182         }
183
184         wreplconn = talloc_zero(state, struct wreplsrv_out_connection);
185         if (!wreplconn) goto failed;
186
187         wreplconn->service      = service;
188         wreplconn->partner      = partner;
189         wreplconn->sock         = wrepl_socket_init(wreplconn, service->task->event_ctx);
190         if (!wreplconn->sock) goto failed;
191
192         state->stage    = WREPLSRV_OUT_CONNECT_STAGE_WAIT_SOCKET;
193         state->wreplconn= wreplconn;
194         state->c_req    = wrepl_connect_send(wreplconn->sock,
195                                              partner->our_address,
196                                              partner->address);
197         if (!state->c_req) goto failed;
198
199         state->c_req->async.fn                  = wreplsrv_out_connect_handler_creq;
200         state->c_req->async.private_data        = state;
201
202         return c;
203 failed:
204         talloc_free(c);
205         return NULL;
206 }
207
208 static NTSTATUS wreplsrv_out_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
209                                           struct wreplsrv_out_connection **wreplconn)
210 {
211         NTSTATUS status;
212
213         status = composite_wait(c);
214
215         if (NT_STATUS_IS_OK(status)) {
216                 struct wreplsrv_out_connect_state *state = talloc_get_type(c->private_data,
217                                                            struct wreplsrv_out_connect_state);
218                 if (state->wreplconn) {
219                         *wreplconn = talloc_reference(mem_ctx, state->wreplconn);
220                         if (!*wreplconn) status = NT_STATUS_NO_MEMORY;
221                 } else {
222                         status = NT_STATUS_INVALID_CONNECTION;
223                 }
224         }
225
226         talloc_free(c);
227         return status;
228         
229 }
230
231 enum wreplsrv_pull_table_stage {
232         WREPLSRV_PULL_TABLE_STAGE_WAIT_CONNECTION,
233         WREPLSRV_PULL_TABLE_STAGE_WAIT_TABLE_REPLY,
234         WREPLSRV_PULL_TABLE_STAGE_DONE
235 };
236
237 struct wreplsrv_pull_table_state {
238         enum wreplsrv_pull_table_stage stage;
239         struct composite_context *c;
240         struct wrepl_request *req;
241         struct wrepl_pull_table table_io;
242         struct wreplsrv_pull_table_io *io;
243         struct composite_context *creq;
244         struct wreplsrv_out_connection *wreplconn;
245 };
246
247 static void wreplsrv_pull_table_handler_req(struct wrepl_request *req);
248
249 static NTSTATUS wreplsrv_pull_table_wait_connection(struct wreplsrv_pull_table_state *state)
250 {
251         NTSTATUS status;
252
253         status = wreplsrv_out_connect_recv(state->creq, state, &state->wreplconn);
254         NT_STATUS_NOT_OK_RETURN(status);
255
256         state->table_io.in.assoc_ctx = state->wreplconn->assoc_ctx.peer_ctx;
257         state->req = wrepl_pull_table_send(state->wreplconn->sock, &state->table_io);
258         NT_STATUS_HAVE_NO_MEMORY(state->req);
259
260         state->req->async.fn            = wreplsrv_pull_table_handler_req;
261         state->req->async.private       = state;
262
263         state->stage = WREPLSRV_PULL_TABLE_STAGE_WAIT_TABLE_REPLY;
264
265         return NT_STATUS_OK;
266 }
267
268 static NTSTATUS wreplsrv_pull_table_wait_table_reply(struct wreplsrv_pull_table_state *state)
269 {
270         NTSTATUS status;
271
272         status = wrepl_pull_table_recv(state->req, state, &state->table_io);
273         NT_STATUS_NOT_OK_RETURN(status);
274
275         state->stage = WREPLSRV_PULL_TABLE_STAGE_DONE;
276
277         return NT_STATUS_OK;
278 }
279
280 static void wreplsrv_pull_table_handler(struct wreplsrv_pull_table_state *state)
281 {
282         struct composite_context *c = state->c;
283
284         switch (state->stage) {
285         case WREPLSRV_PULL_TABLE_STAGE_WAIT_CONNECTION:
286                 c->status = wreplsrv_pull_table_wait_connection(state);
287                 break;
288         case WREPLSRV_PULL_TABLE_STAGE_WAIT_TABLE_REPLY:
289                 c->status = wreplsrv_pull_table_wait_table_reply(state);
290                 c->state  = COMPOSITE_STATE_DONE;
291                 break;
292         case WREPLSRV_PULL_TABLE_STAGE_DONE:
293                 c->status = NT_STATUS_INTERNAL_ERROR;
294         }
295
296         if (!NT_STATUS_IS_OK(c->status)) {
297                 c->state = COMPOSITE_STATE_ERROR;
298         }
299
300         if (c->state >= COMPOSITE_STATE_DONE && c->async.fn) {
301                 c->async.fn(c);
302         }
303 }
304
305 static void wreplsrv_pull_table_handler_creq(struct composite_context *creq)
306 {
307         struct wreplsrv_pull_table_state *state = talloc_get_type(creq->async.private_data,
308                                                   struct wreplsrv_pull_table_state);
309         wreplsrv_pull_table_handler(state);
310         return;
311 }
312
313 static void wreplsrv_pull_table_handler_req(struct wrepl_request *req)
314 {
315         struct wreplsrv_pull_table_state *state = talloc_get_type(req->async.private,
316                                                   struct wreplsrv_pull_table_state);
317         wreplsrv_pull_table_handler(state);
318         return;
319 }
320
321 struct composite_context *wreplsrv_pull_table_send(TALLOC_CTX *mem_ctx, struct wreplsrv_pull_table_io *io)
322 {
323         struct composite_context *c = NULL;
324         struct wreplsrv_service *service = io->in.partner->service;
325         struct wreplsrv_pull_table_state *state = NULL;
326
327         c = talloc_zero(mem_ctx, struct composite_context);
328         if (!c) goto failed;
329
330         state = talloc_zero(c, struct wreplsrv_pull_table_state);
331         if (!state) goto failed;
332         state->c        = c;
333         state->io       = io;
334
335         c->state        = COMPOSITE_STATE_IN_PROGRESS;
336         c->event_ctx    = service->task->event_ctx;
337         c->private_data = state;
338
339         if (io->in.num_owners) {
340                 state->table_io.out.num_partners        = io->in.num_owners;
341                 state->table_io.out.partners            = io->in.owners;
342                 state->stage                            = WREPLSRV_PULL_TABLE_STAGE_DONE;
343                 composite_done(c);
344                 return c;
345         }
346
347         state->stage    = WREPLSRV_PULL_TABLE_STAGE_WAIT_CONNECTION;
348         state->creq     = wreplsrv_out_connect_send(io->in.partner, WINSREPL_PARTNER_PULL, NULL);
349         if (!state->creq) goto failed;
350
351         state->creq->async.fn           = wreplsrv_pull_table_handler_creq;
352         state->creq->async.private_data = state;
353
354         return c;
355 failed:
356         talloc_free(c);
357         return NULL;
358 }
359
360 NTSTATUS wreplsrv_pull_table_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
361                                   struct wreplsrv_pull_table_io *io)
362 {
363         NTSTATUS status;
364
365         status = composite_wait(c);
366
367         if (NT_STATUS_IS_OK(status)) {
368                 struct wreplsrv_pull_table_state *state = talloc_get_type(c->private_data,
369                                                           struct wreplsrv_pull_table_state);
370                 io->out.num_owners      = state->table_io.out.num_partners;
371                 io->out.owners          = state->table_io.out.partners;
372                 talloc_reference(mem_ctx, state->table_io.out.partners);
373         }
374
375         talloc_free(c);
376         return status;  
377 }
378
379 enum wreplsrv_pull_names_stage {
380         WREPLSRV_PULL_NAMES_STAGE_WAIT_CONNECTION,
381         WREPLSRV_PULL_NAMES_STAGE_WAIT_SEND_REPLY,
382         WREPLSRV_PULL_NAMES_STAGE_DONE
383 };
384
385 struct wreplsrv_pull_names_state {
386         enum wreplsrv_pull_names_stage stage;
387         struct composite_context *c;
388         struct wrepl_request *req;
389         struct wrepl_pull_names pull_io;
390         struct wreplsrv_pull_names_io *io;
391         struct composite_context *creq;
392         struct wreplsrv_out_connection *wreplconn;
393 };
394
395 static void wreplsrv_pull_names_handler_req(struct wrepl_request *req);
396
397 static NTSTATUS wreplsrv_pull_names_wait_connection(struct wreplsrv_pull_names_state *state)
398 {
399         NTSTATUS status;
400
401         status = wreplsrv_out_connect_recv(state->creq, state, &state->wreplconn);
402         NT_STATUS_NOT_OK_RETURN(status);
403
404         state->pull_io.in.assoc_ctx     = state->wreplconn->assoc_ctx.peer_ctx;
405         state->pull_io.in.partner       = state->io->in.owner;
406         state->req = wrepl_pull_names_send(state->wreplconn->sock, &state->pull_io);
407         NT_STATUS_HAVE_NO_MEMORY(state->req);
408
409         state->req->async.fn            = wreplsrv_pull_names_handler_req;
410         state->req->async.private       = state;
411
412         state->stage = WREPLSRV_PULL_NAMES_STAGE_WAIT_SEND_REPLY;
413
414         return NT_STATUS_OK;
415 }
416
417 static NTSTATUS wreplsrv_pull_names_wait_send_reply(struct wreplsrv_pull_names_state *state)
418 {
419         NTSTATUS status;
420
421         status = wrepl_pull_names_recv(state->req, state, &state->pull_io);
422         NT_STATUS_NOT_OK_RETURN(status);
423
424         state->stage = WREPLSRV_PULL_NAMES_STAGE_DONE;
425
426         return NT_STATUS_OK;
427 }
428
429 static void wreplsrv_pull_names_handler(struct wreplsrv_pull_names_state *state)
430 {
431         struct composite_context *c = state->c;
432
433         switch (state->stage) {
434         case WREPLSRV_PULL_NAMES_STAGE_WAIT_CONNECTION:
435                 c->status = wreplsrv_pull_names_wait_connection(state);
436                 break;
437         case WREPLSRV_PULL_NAMES_STAGE_WAIT_SEND_REPLY:
438                 c->status = wreplsrv_pull_names_wait_send_reply(state);
439                 c->state  = COMPOSITE_STATE_DONE;
440                 break;
441         case WREPLSRV_PULL_NAMES_STAGE_DONE:
442                 c->status = NT_STATUS_INTERNAL_ERROR;
443         }
444
445         if (!NT_STATUS_IS_OK(c->status)) {
446                 c->state = COMPOSITE_STATE_ERROR;
447         }
448
449         if (c->state >= COMPOSITE_STATE_DONE && c->async.fn) {
450                 c->async.fn(c);
451         }
452 }
453
454 static void wreplsrv_pull_names_handler_creq(struct composite_context *creq)
455 {
456         struct wreplsrv_pull_names_state *state = talloc_get_type(creq->async.private_data,
457                                                   struct wreplsrv_pull_names_state);
458         wreplsrv_pull_names_handler(state);
459         return;
460 }
461
462 static void wreplsrv_pull_names_handler_req(struct wrepl_request *req)
463 {
464         struct wreplsrv_pull_names_state *state = talloc_get_type(req->async.private,
465                                                   struct wreplsrv_pull_names_state);
466         wreplsrv_pull_names_handler(state);
467         return;
468 }
469
470 struct composite_context *wreplsrv_pull_names_send(TALLOC_CTX *mem_ctx, struct wreplsrv_pull_names_io *io)
471 {
472         struct composite_context *c = NULL;
473         struct wreplsrv_service *service = io->in.partner->service;
474         struct wreplsrv_pull_names_state *state = NULL;
475         enum winsrepl_partner_type partner_type = WINSREPL_PARTNER_PULL;
476
477         if (io->in.wreplconn) partner_type = WINSREPL_PARTNER_NONE;
478
479         c = talloc_zero(mem_ctx, struct composite_context);
480         if (!c) goto failed;
481
482         state = talloc_zero(c, struct wreplsrv_pull_names_state);
483         if (!state) goto failed;
484         state->c        = c;
485         state->io       = io;
486
487         c->state        = COMPOSITE_STATE_IN_PROGRESS;
488         c->event_ctx    = service->task->event_ctx;
489         c->private_data = state;
490
491         state->stage    = WREPLSRV_PULL_NAMES_STAGE_WAIT_CONNECTION;
492         state->creq     = wreplsrv_out_connect_send(io->in.partner, partner_type, io->in.wreplconn);
493         if (!state->creq) goto failed;
494
495         state->creq->async.fn           = wreplsrv_pull_names_handler_creq;
496         state->creq->async.private_data = state;
497
498         return c;
499 failed:
500         talloc_free(c);
501         return NULL;
502 }
503
504 NTSTATUS wreplsrv_pull_names_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
505                                   struct wreplsrv_pull_names_io *io)
506 {
507         NTSTATUS status;
508
509         status = composite_wait(c);
510
511         if (NT_STATUS_IS_OK(status)) {
512                 struct wreplsrv_pull_names_state *state = talloc_get_type(c->private_data,
513                                                           struct wreplsrv_pull_names_state);
514                 io->out.num_names       = state->pull_io.out.num_names;
515                 io->out.names           = state->pull_io.out.names;
516                 talloc_reference(mem_ctx, state->pull_io.out.names);
517         }
518
519         talloc_free(c);
520         return status;
521         
522 }
523
524 enum wreplsrv_pull_cycle_stage {
525         WREPLSRV_PULL_CYCLE_STAGE_WAIT_TABLE_REPLY,
526         WREPLSRV_PULL_CYCLE_STAGE_WAIT_SEND_REPLIES,
527         WREPLSRV_PULL_CYCLE_STAGE_WAIT_STOP_ASSOC,
528         WREPLSRV_PULL_CYCLE_STAGE_DONE
529 };
530
531 struct wreplsrv_pull_cycle_state {
532         enum wreplsrv_pull_cycle_stage stage;
533         struct composite_context *c;
534         struct wreplsrv_pull_cycle_io *io;
535         struct wreplsrv_pull_table_io table_io;
536         uint32_t current;
537         struct wreplsrv_pull_names_io names_io;
538         struct composite_context *creq;
539         struct wrepl_associate_stop assoc_stop_io;
540         struct wrepl_request *req;
541 };
542
543 static void wreplsrv_pull_cycle_handler_creq(struct composite_context *creq);
544 static void wreplsrv_pull_cycle_handler_req(struct wrepl_request *req);
545
546 static NTSTATUS wreplsrv_pull_cycle_next_owner_do_work(struct wreplsrv_pull_cycle_state *state)
547 {
548         struct wreplsrv_owner *current_owner;
549         struct wreplsrv_owner *local_owner;
550         uint32_t i;
551         uint64_t old_max_version = 0;
552         BOOL do_pull = False;
553
554         for (i=state->current; i < state->table_io.out.num_owners; i++) {
555                 current_owner = wreplsrv_find_owner(state->io->in.partner->pull.table,
556                                                     state->table_io.out.owners[i].address);
557
558                 local_owner = wreplsrv_find_owner(state->io->in.partner->service->table,
559                                                   state->table_io.out.owners[i].address);
560                 /*
561                  * this means we are ourself the current owner,
562                  * and we don't want replicate ourself
563                  */
564                 if (!current_owner) continue;
565
566                 /*
567                  * this means we don't have any records of this owner
568                  * so fetch them
569                  */
570                 if (!local_owner) {
571                         do_pull         = True;
572                         
573                         break;
574                 }
575
576                 /*
577                  * this means the remote partner has some new records of this owner
578                  * fetch them
579                  */
580                 if (current_owner->owner.max_version > local_owner->owner.max_version) {
581                         do_pull         = True;
582                         old_max_version = local_owner->owner.max_version;
583                         break;
584                 }
585         }
586         state->current = i;
587
588         if (do_pull) {
589                 state->names_io.in.partner              = state->io->in.partner;
590                 state->names_io.in.wreplconn            = state->io->in.wreplconn;
591                 state->names_io.in.owner                = current_owner->owner;
592                 state->names_io.in.owner.min_version    = old_max_version + 1;
593                 state->creq = wreplsrv_pull_names_send(state, &state->names_io);
594                 NT_STATUS_HAVE_NO_MEMORY(state->creq);
595
596                 state->creq->async.fn           = wreplsrv_pull_cycle_handler_creq;
597                 state->creq->async.private_data = state;
598
599                 return STATUS_MORE_ENTRIES;
600         }
601
602         return NT_STATUS_OK;
603 }
604
605 static NTSTATUS wreplsrv_pull_cycle_next_owner_wrapper(struct wreplsrv_pull_cycle_state *state)
606 {
607         NTSTATUS status;
608
609         status = wreplsrv_pull_cycle_next_owner_do_work(state);
610         if (NT_STATUS_IS_OK(status)) {
611                 state->stage = WREPLSRV_PULL_CYCLE_STAGE_DONE;
612         } else if (NT_STATUS_EQUAL(STATUS_MORE_ENTRIES, status)) {
613                 state->stage = WREPLSRV_PULL_CYCLE_STAGE_WAIT_SEND_REPLIES;
614                 status = NT_STATUS_OK;
615         }
616
617         if (state->stage == WREPLSRV_PULL_CYCLE_STAGE_DONE && state->io->in.wreplconn) {
618                 state->assoc_stop_io.in.assoc_ctx       = state->io->in.wreplconn->assoc_ctx.peer_ctx;
619                 state->assoc_stop_io.in.reason          = 0;
620                 state->req = wrepl_associate_stop_send(state->io->in.wreplconn->sock, &state->assoc_stop_io);
621                 NT_STATUS_HAVE_NO_MEMORY(state->req);
622
623                 state->req->async.fn            = wreplsrv_pull_cycle_handler_req;
624                 state->req->async.private       = state;
625
626                 state->stage = WREPLSRV_PULL_CYCLE_STAGE_WAIT_STOP_ASSOC;
627         }
628
629         return status;
630 }
631
632 static NTSTATUS wreplsrv_pull_cycle_wait_table_reply(struct wreplsrv_pull_cycle_state *state)
633 {
634         NTSTATUS status;
635         uint32_t i;
636
637         status = wreplsrv_pull_table_recv(state->creq, state, &state->table_io);
638         NT_STATUS_NOT_OK_RETURN(status);
639
640         /* update partner table */
641         for (i=0; i < state->table_io.out.num_owners; i++) {
642                 BOOL is_our_addr;
643
644                 is_our_addr = wreplsrv_is_our_address(state->io->in.partner->service,
645                                                       state->table_io.out.owners[i].address);
646                 if (is_our_addr) continue;
647
648                 status = wreplsrv_add_table(state->io->in.partner->service,
649                                             state->io->in.partner, 
650                                             &state->io->in.partner->pull.table,
651                                             state->table_io.out.owners[i].address,
652                                             state->table_io.out.owners[i].max_version);
653                 NT_STATUS_NOT_OK_RETURN(status);
654         }
655
656         status = wreplsrv_pull_cycle_next_owner_wrapper(state);
657         NT_STATUS_NOT_OK_RETURN(status);
658
659         return status;
660 }
661
662 static NTSTATUS wreplsrv_pull_cycle_apply_records(struct wreplsrv_pull_cycle_state *state)
663 {
664         NTSTATUS status;
665
666         status = wreplsrv_apply_records(state->io->in.partner, &state->names_io);
667         NT_STATUS_NOT_OK_RETURN(status);
668
669         talloc_free(state->names_io.out.names);
670         ZERO_STRUCT(state->names_io);
671
672         return NT_STATUS_OK;
673 }
674
675 static NTSTATUS wreplsrv_pull_cycle_wait_send_replies(struct wreplsrv_pull_cycle_state *state)
676 {
677         NTSTATUS status;
678
679         status = wreplsrv_pull_names_recv(state->creq, state, &state->names_io);
680         NT_STATUS_NOT_OK_RETURN(status);
681
682         /*
683          * TODO: this should maybe an async call,
684          *       because we may need some network access
685          *       for conflict resolving
686          */
687         status = wreplsrv_pull_cycle_apply_records(state);
688         NT_STATUS_NOT_OK_RETURN(status);
689
690         status = wreplsrv_pull_cycle_next_owner_wrapper(state);
691         NT_STATUS_NOT_OK_RETURN(status);
692
693         return status;
694 }
695
696 static NTSTATUS wreplsrv_pull_cycle_wait_stop_assoc(struct wreplsrv_pull_cycle_state *state)
697 {
698         NTSTATUS status;
699
700         status = wrepl_associate_stop_recv(state->req, &state->assoc_stop_io);
701         NT_STATUS_NOT_OK_RETURN(status);
702
703         state->stage = WREPLSRV_PULL_CYCLE_STAGE_DONE;
704
705         return status;
706 }
707
708 static void wreplsrv_pull_cycle_handler(struct wreplsrv_pull_cycle_state *state)
709 {
710         struct composite_context *c = state->c;
711
712         switch (state->stage) {
713         case WREPLSRV_PULL_CYCLE_STAGE_WAIT_TABLE_REPLY:
714                 c->status = wreplsrv_pull_cycle_wait_table_reply(state);
715                 break;
716         case WREPLSRV_PULL_CYCLE_STAGE_WAIT_SEND_REPLIES:
717                 c->status = wreplsrv_pull_cycle_wait_send_replies(state);
718                 break;
719         case WREPLSRV_PULL_CYCLE_STAGE_WAIT_STOP_ASSOC:
720                 c->status = wreplsrv_pull_cycle_wait_stop_assoc(state);
721                 break;
722         case WREPLSRV_PULL_CYCLE_STAGE_DONE:
723                 c->status = NT_STATUS_INTERNAL_ERROR;
724         }
725
726         if (state->stage == WREPLSRV_PULL_CYCLE_STAGE_DONE) {
727                 c->state  = COMPOSITE_STATE_DONE;
728         }
729
730         if (!NT_STATUS_IS_OK(c->status)) {
731                 c->state = COMPOSITE_STATE_ERROR;
732         }
733
734         if (c->state >= COMPOSITE_STATE_DONE && c->async.fn) {
735                 c->async.fn(c);
736         }
737 }
738
739 static void wreplsrv_pull_cycle_handler_creq(struct composite_context *creq)
740 {
741         struct wreplsrv_pull_cycle_state *state = talloc_get_type(creq->async.private_data,
742                                                   struct wreplsrv_pull_cycle_state);
743         wreplsrv_pull_cycle_handler(state);
744         return;
745 }
746
747 static void wreplsrv_pull_cycle_handler_req(struct wrepl_request *req)
748 {
749         struct wreplsrv_pull_cycle_state *state = talloc_get_type(req->async.private,
750                                                   struct wreplsrv_pull_cycle_state);
751         wreplsrv_pull_cycle_handler(state);
752         return;
753 }
754
755 struct composite_context *wreplsrv_pull_cycle_send(TALLOC_CTX *mem_ctx, struct wreplsrv_pull_cycle_io *io)
756 {
757         struct composite_context *c = NULL;
758         struct wreplsrv_service *service = io->in.partner->service;
759         struct wreplsrv_pull_cycle_state *state = NULL;
760
761         c = talloc_zero(mem_ctx, struct composite_context);
762         if (!c) goto failed;
763
764         state = talloc_zero(c, struct wreplsrv_pull_cycle_state);
765         if (!state) goto failed;
766         state->c        = c;
767         state->io       = io;
768
769         c->state        = COMPOSITE_STATE_IN_PROGRESS;
770         c->event_ctx    = service->task->event_ctx;
771         c->private_data = state;
772
773         state->stage    = WREPLSRV_PULL_CYCLE_STAGE_WAIT_TABLE_REPLY;
774         state->table_io.in.partner      = io->in.partner;
775         state->table_io.in.num_owners   = io->in.num_owners;
776         state->table_io.in.owners       = io->in.owners;
777         state->creq = wreplsrv_pull_table_send(state, &state->table_io);
778         if (!state->creq) goto failed;
779
780         state->creq->async.fn           = wreplsrv_pull_cycle_handler_creq;
781         state->creq->async.private_data = state;
782
783         return c;
784 failed:
785         talloc_free(c);
786         return NULL;
787 }
788
789 NTSTATUS wreplsrv_pull_cycle_recv(struct composite_context *c)
790 {
791         NTSTATUS status;
792
793         status = composite_wait(c);
794
795         talloc_free(c);
796         return status;
797 }
798
799 enum wreplsrv_push_notify_stage {
800         WREPLSRV_PUSH_NOTIFY_STAGE_WAIT_CONNECT,
801         WREPLSRV_PUSH_NOTIFY_STAGE_WAIT_INFORM,
802         WREPLSRV_PUSH_NOTIFY_STAGE_DONE
803 };
804
805 struct wreplsrv_push_notify_state {
806         enum wreplsrv_push_notify_stage stage;
807         struct composite_context *c;
808         struct wreplsrv_push_notify_io *io;
809         enum wrepl_replication_cmd command;
810         BOOL full_table;
811         struct wrepl_send_ctrl ctrl;
812         struct wrepl_request *req;
813         struct wrepl_packet req_packet;
814         struct wrepl_packet *rep_packet;
815         struct composite_context *creq;
816         struct wreplsrv_out_connection *wreplconn;
817 };
818
819 static void wreplsrv_push_notify_handler_creq(struct composite_context *creq);
820 static void wreplsrv_push_notify_handler_req(struct wrepl_request *req);
821
822 static NTSTATUS wreplsrv_push_notify_update(struct wreplsrv_push_notify_state *state)
823 {
824         struct wreplsrv_service *service = state->io->in.partner->service;
825         struct wrepl_packet *req = &state->req_packet;
826         struct wrepl_replication *repl_out = &state->req_packet.message.replication;
827         struct wrepl_table *table_out = &state->req_packet.message.replication.info.table;
828         struct wreplsrv_in_connection *wrepl_in;
829         NTSTATUS status;
830         struct socket_context *sock;
831         struct packet_context *packet;
832         uint16_t fde_flags;
833         const char *our_ip;
834
835         /* prepare the outgoing request */
836         req->opcode     = WREPL_OPCODE_BITS;
837         req->assoc_ctx  = state->wreplconn->assoc_ctx.peer_ctx;
838         req->mess_type  = WREPL_REPLICATION;
839
840         repl_out->command = state->command;
841
842         our_ip = socket_get_my_addr(state->wreplconn->sock->sock, state);
843         NT_STATUS_HAVE_NO_MEMORY(our_ip);
844
845         status = wreplsrv_fill_wrepl_table(service, state, table_out,
846                                            our_ip, our_ip, state->full_table);
847         NT_STATUS_NOT_OK_RETURN(status);
848
849         /* queue the request */
850         state->req = wrepl_request_send(state->wreplconn->sock, req, NULL);
851         NT_STATUS_HAVE_NO_MEMORY(state->req);
852
853         /*
854          * now we need to convert the wrepl_socket (client connection)
855          * into a wreplsrv_in_connection (server connection), because
856          * we'll act as a server on this connection after the WREPL_REPL_UPDATE*
857          * message is received by the peer.
858          */
859
860         /* steal the socket_context */
861         sock = state->wreplconn->sock->sock;
862         state->wreplconn->sock->sock = NULL;
863         talloc_steal(state, sock);
864
865         /* 
866          * steal the packet_context
867          * note the request DATA_BLOB we just send on the
868          * wrepl_socket (client connection) is still unter the 
869          * packet context and will be send to the wire
870          */
871         packet = state->wreplconn->sock->packet;
872         state->wreplconn->sock->packet = NULL;
873         talloc_steal(state, packet);
874
875         /*
876          * get the fde_flags of the old fde event,
877          * so that we can later set the same flags to the new one
878          */
879         fde_flags = event_get_fd_flags(state->wreplconn->sock->event.fde);
880
881         /*
882          * free the wrepl_socket (client connection)
883          */
884         talloc_free(state->wreplconn->sock);
885         state->wreplconn->sock = NULL;
886
887         /*
888          * now create a wreplsrv_in_connection,
889          * on which we act as server
890          *
891          * NOTE: sock and packet will be stolen by
892          *       wreplsrv_in_connection_merge()
893          */
894         status = wreplsrv_in_connection_merge(state->io->in.partner,
895                                               sock, packet, &wrepl_in);
896         NT_STATUS_NOT_OK_RETURN(status);
897
898         event_set_fd_flags(wrepl_in->conn->event.fde, fde_flags);
899
900         wrepl_in->assoc_ctx.peer_ctx    = state->wreplconn->assoc_ctx.peer_ctx;
901         wrepl_in->assoc_ctx.our_ctx     = 0;
902
903         /* now we can free the wreplsrv_out_connection */
904         talloc_free(state->wreplconn);
905         state->wreplconn = NULL;
906
907         state->stage = WREPLSRV_PUSH_NOTIFY_STAGE_DONE;
908
909         return NT_STATUS_OK;
910 }
911
912 static NTSTATUS wreplsrv_push_notify_inform(struct wreplsrv_push_notify_state *state)
913 {
914         struct wreplsrv_service *service = state->io->in.partner->service;
915         struct wrepl_packet *req = &state->req_packet;
916         struct wrepl_replication *repl_out = &state->req_packet.message.replication;
917         struct wrepl_table *table_out = &state->req_packet.message.replication.info.table;
918         NTSTATUS status;
919         const char *our_ip;
920
921         req->opcode     = WREPL_OPCODE_BITS;
922         req->assoc_ctx  = state->wreplconn->assoc_ctx.peer_ctx;
923         req->mess_type  = WREPL_REPLICATION;
924
925         repl_out->command = state->command;
926
927         our_ip = socket_get_my_addr(state->wreplconn->sock->sock, state);
928         NT_STATUS_HAVE_NO_MEMORY(our_ip);
929
930         status = wreplsrv_fill_wrepl_table(service, state, table_out,
931                                            our_ip, our_ip, state->full_table);
932         NT_STATUS_NOT_OK_RETURN(status);
933
934         /* we won't get a reply to a inform message */
935         state->ctrl.send_only           = True;
936
937         state->req = wrepl_request_send(state->wreplconn->sock, req, &state->ctrl);
938         NT_STATUS_HAVE_NO_MEMORY(state->req);
939
940         state->req->async.fn            = wreplsrv_push_notify_handler_req;
941         state->req->async.private       = state;
942
943         state->stage = WREPLSRV_PUSH_NOTIFY_STAGE_WAIT_INFORM;
944
945         return NT_STATUS_OK;
946 }
947
948 static NTSTATUS wreplsrv_push_notify_wait_connect(struct wreplsrv_push_notify_state *state)
949 {
950         NTSTATUS status;
951
952         status = wreplsrv_out_connect_recv(state->creq, state, &state->wreplconn);
953         NT_STATUS_NOT_OK_RETURN(status);
954
955         switch (state->command) {
956         case WREPL_REPL_UPDATE:
957                 state->full_table = True;
958                 return wreplsrv_push_notify_update(state);
959         case WREPL_REPL_UPDATE2:
960                 state->full_table = False;
961                 return wreplsrv_push_notify_update(state);
962         case WREPL_REPL_INFORM:
963                 state->full_table = True;
964                 return wreplsrv_push_notify_inform(state);
965         case WREPL_REPL_INFORM2:
966                 state->full_table = False;
967                 return wreplsrv_push_notify_inform(state);
968         default:
969                 return NT_STATUS_INTERNAL_ERROR;
970         }
971
972         return NT_STATUS_INTERNAL_ERROR;
973 }
974
975 static NTSTATUS wreplsrv_push_notify_wait_inform(struct wreplsrv_push_notify_state *state)
976 {
977         NTSTATUS status;
978
979         status =  wrepl_request_recv(state->req, state, NULL);
980         NT_STATUS_NOT_OK_RETURN(status);
981
982         state->stage = WREPLSRV_PUSH_NOTIFY_STAGE_DONE;
983         return status;
984 }
985
986 static void wreplsrv_push_notify_handler(struct wreplsrv_push_notify_state *state)
987 {
988         struct composite_context *c = state->c;
989
990         switch (state->stage) {
991         case WREPLSRV_PUSH_NOTIFY_STAGE_WAIT_CONNECT:
992                 c->status = wreplsrv_push_notify_wait_connect(state);
993                 break;
994         case WREPLSRV_PUSH_NOTIFY_STAGE_WAIT_INFORM:
995                 c->status = wreplsrv_push_notify_wait_inform(state);
996                 break;
997         case WREPLSRV_PUSH_NOTIFY_STAGE_DONE:
998                 c->status = NT_STATUS_INTERNAL_ERROR;
999         }
1000
1001         if (state->stage == WREPLSRV_PUSH_NOTIFY_STAGE_DONE) {
1002                 c->state  = COMPOSITE_STATE_DONE;
1003         }
1004
1005         if (!NT_STATUS_IS_OK(c->status)) {
1006                 c->state = COMPOSITE_STATE_ERROR;
1007         }
1008
1009         if (c->state >= COMPOSITE_STATE_DONE && c->async.fn) {
1010                 c->async.fn(c);
1011         }
1012 }
1013
1014 static void wreplsrv_push_notify_handler_creq(struct composite_context *creq)
1015 {
1016         struct wreplsrv_push_notify_state *state = talloc_get_type(creq->async.private_data,
1017                                                    struct wreplsrv_push_notify_state);
1018         wreplsrv_push_notify_handler(state);
1019         return;
1020 }
1021
1022 static void wreplsrv_push_notify_handler_req(struct wrepl_request *req)
1023 {
1024         struct wreplsrv_push_notify_state *state = talloc_get_type(req->async.private,
1025                                                    struct wreplsrv_push_notify_state);
1026         wreplsrv_push_notify_handler(state);
1027         return;
1028 }
1029
1030 struct composite_context *wreplsrv_push_notify_send(TALLOC_CTX *mem_ctx, struct wreplsrv_push_notify_io *io)
1031 {
1032         struct composite_context *c = NULL;
1033         struct wreplsrv_service *service = io->in.partner->service;
1034         struct wreplsrv_push_notify_state *state = NULL;
1035         enum winsrepl_partner_type partner_type;
1036
1037         c = talloc_zero(mem_ctx, struct composite_context);
1038         if (!c) goto failed;
1039
1040         state = talloc_zero(c, struct wreplsrv_push_notify_state);
1041         if (!state) goto failed;
1042         state->c        = c;
1043         state->io       = io;
1044
1045         if (io->in.inform) {
1046                 /* we can cache the connection in partner->push->wreplconn */
1047                 partner_type = WINSREPL_PARTNER_PUSH;
1048                 if (io->in.propagate) {
1049                         state->command  = WREPL_REPL_INFORM2;
1050                 } else {
1051                         state->command  = WREPL_REPL_INFORM;
1052                 }
1053         } else {
1054                 /* we can NOT cache the connection */
1055                 partner_type = WINSREPL_PARTNER_NONE;
1056                 if (io->in.propagate) {
1057                         state->command  = WREPL_REPL_UPDATE2;
1058                 } else {
1059                         state->command  = WREPL_REPL_UPDATE;
1060                 }       
1061         }
1062
1063         c->state        = COMPOSITE_STATE_IN_PROGRESS;
1064         c->event_ctx    = service->task->event_ctx;
1065         c->private_data = state;
1066
1067         state->stage    = WREPLSRV_PUSH_NOTIFY_STAGE_WAIT_CONNECT;
1068         state->creq     = wreplsrv_out_connect_send(io->in.partner, partner_type, NULL);
1069         if (!state->creq) goto failed;
1070
1071         state->creq->async.fn           = wreplsrv_push_notify_handler_creq;
1072         state->creq->async.private_data = state;
1073
1074         return c;
1075 failed:
1076         talloc_free(c);
1077         return NULL;
1078 }
1079
1080 NTSTATUS wreplsrv_push_notify_recv(struct composite_context *c)
1081 {
1082         NTSTATUS status;
1083
1084         status = composite_wait(c);
1085
1086         talloc_free(c);
1087         return status;
1088 }