s4-dsdb/repl/drepl_out_pull.c: Remove unused code
[metze/samba/wip.git] / source4 / dsdb / repl / drepl_out_pull.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB replication service outgoing Pull-Replication
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 "libcli/security/dom_sid.h"
36
37 WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
38                                                struct dreplsrv_partition_source_dsa *source,
39                                                enum drsuapi_DsExtendedOperation extended_op,
40                                                uint64_t fsmo_info,
41                                                dreplsrv_fsmo_callback_t callback,
42                                                void *cb_data)
43 {
44         struct dreplsrv_out_operation *op;
45
46         op = talloc_zero(s, struct dreplsrv_out_operation);
47         W_ERROR_HAVE_NO_MEMORY(op);
48
49         op->service     = s;
50         op->source_dsa  = source;
51         op->extended_op = extended_op;
52         op->fsmo_info   = fsmo_info;
53         op->callback    = callback;
54         op->cb_data     = cb_data;
55
56         DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);
57
58         return WERR_OK;
59 }
60
61 static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
62                                                struct dreplsrv_partition *p,
63                                                TALLOC_CTX *mem_ctx)
64 {
65         WERROR status;
66         struct dreplsrv_partition_source_dsa *cur;
67
68         for (cur = p->sources; cur; cur = cur->next) {
69                 status = dreplsrv_schedule_partition_pull_source(s, cur,
70                                                                  DRSUAPI_EXOP_NONE, 0,
71                                                                  NULL, NULL);
72                 W_ERROR_NOT_OK_RETURN(status);
73         }
74
75         return WERR_OK;
76 }
77
78 WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
79 {
80         WERROR status;
81         struct dreplsrv_partition *p;
82
83         for (p = s->partitions; p; p = p->next) {
84                 status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
85                 W_ERROR_NOT_OK_RETURN(status);
86         }
87
88         return WERR_OK;
89 }
90
91
92 static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
93 {
94         struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
95                                             struct dreplsrv_out_operation);
96         struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
97         struct dreplsrv_service *s = op->service;
98         time_t t;
99         NTTIME now;
100
101         t = time(NULL);
102         unix_to_nt_time(&now, t);
103
104         rf->result_last_attempt = dreplsrv_op_pull_source_recv(subreq);
105         TALLOC_FREE(subreq);
106         if (W_ERROR_IS_OK(rf->result_last_attempt)) {
107                 rf->consecutive_sync_failures   = 0;
108                 rf->last_success                = now;
109                 DEBUG(3,("dreplsrv_op_pull_source(%s)\n",
110                         win_errstr(rf->result_last_attempt)));
111                 goto done;
112         }
113
114         rf->consecutive_sync_failures++;
115
116         DEBUG(1,("dreplsrv_op_pull_source(%s/%s) for %s failures[%u]\n",
117                  win_errstr(rf->result_last_attempt),
118                  nt_errstr(werror_to_ntstatus(rf->result_last_attempt)),
119                  ldb_dn_get_linearized(op->source_dsa->partition->dn),
120                  rf->consecutive_sync_failures));
121
122 done:
123         if (op->callback) {
124                 op->callback(s, rf->result_last_attempt, op->extended_ret, op->cb_data);
125         }
126         talloc_free(op);
127         s->ops.current = NULL;
128         dreplsrv_run_pending_ops(s);
129         dreplsrv_notify_run_ops(s);
130 }
131
132 void dreplsrv_run_pending_ops(struct dreplsrv_service *s)
133 {
134         struct dreplsrv_out_operation *op;
135         time_t t;
136         NTTIME now;
137         struct tevent_req *subreq;
138
139         if (s->ops.current || s->ops.n_current) {
140                 /* if there's still one running, we're done */
141                 return;
142         }
143
144         if (!s->ops.pending) {
145                 /* if there're no pending operations, we're done */
146                 return;
147         }
148
149         t = time(NULL);
150         unix_to_nt_time(&now, t);
151
152         op = s->ops.pending;
153         s->ops.current = op;
154         DLIST_REMOVE(s->ops.pending, op);
155
156         op->source_dsa->repsFrom1->last_attempt = now;
157
158         subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
159         if (!subreq) {
160                 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
161
162                 rf->result_last_attempt = WERR_NOMEM;
163                 rf->consecutive_sync_failures++;
164                 s->ops.current = NULL;
165
166                 DEBUG(1,("dreplsrv_op_pull_source(%s/%s) failures[%u]\n",
167                         win_errstr(rf->result_last_attempt),
168                         nt_errstr(werror_to_ntstatus(rf->result_last_attempt)),
169                         rf->consecutive_sync_failures));
170                 return;
171         }
172         tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);
173 }