s4:dsdb/repl: expose drsuapi_DsExtendedError to the caller (e.g. the ridalloc client)
[abartlet/samba.git/.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 {
43         struct dreplsrv_out_operation *op;
44
45         op = talloc_zero(s, struct dreplsrv_out_operation);
46         W_ERROR_HAVE_NO_MEMORY(op);
47
48         op->service     = s;
49         op->source_dsa  = source;
50         op->extended_op = extended_op;
51         op->fsmo_info   = fsmo_info;
52         op->callback    = callback;
53
54         DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);
55
56         return WERR_OK;
57 }
58
59 static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
60                                                struct dreplsrv_partition *p,
61                                                TALLOC_CTX *mem_ctx)
62 {
63         WERROR status;
64         struct dreplsrv_partition_source_dsa *cur;
65
66         for (cur = p->sources; cur; cur = cur->next) {
67                 status = dreplsrv_schedule_partition_pull_source(s, cur, DRSUAPI_EXOP_NONE, 0, NULL);
68                 W_ERROR_NOT_OK_RETURN(status);
69         }
70
71         return WERR_OK;
72 }
73
74 WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
75 {
76         WERROR status;
77         struct dreplsrv_partition *p;
78
79         for (p = s->partitions; p; p = p->next) {
80                 status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
81                 W_ERROR_NOT_OK_RETURN(status);
82         }
83
84         return WERR_OK;
85 }
86
87
88 /* force an immediate of the specified partition by GUID  */
89 WERROR dreplsrv_schedule_partition_pull_by_guid(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx,
90                                                 struct GUID *guid)
91 {
92         struct dreplsrv_partition *p;
93         
94         for (p = s->partitions; p; p = p->next) {
95                 if (GUID_compare(&p->nc.guid, guid) == 0) {
96                         return dreplsrv_schedule_partition_pull(s, p, mem_ctx);
97                 }
98         }
99
100         return WERR_NOT_FOUND;
101 }
102
103 /* force an immediate of the specified partition by Naming Context */
104 WERROR dreplsrv_schedule_partition_pull_by_nc(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx,
105                                               struct drsuapi_DsReplicaObjectIdentifier *nc)
106 {
107         struct dreplsrv_partition *p;
108         bool valid_sid, valid_guid;
109         struct dom_sid null_sid;
110         ZERO_STRUCT(null_sid);
111
112         valid_sid  = !dom_sid_equal(&null_sid, &nc->sid);
113         valid_guid = !GUID_all_zero(&nc->guid);
114
115         if (!valid_sid && !valid_guid && !nc->dn) {
116                 return WERR_DS_DRA_INVALID_PARAMETER;
117         }
118
119         for (p = s->partitions; p; p = p->next) {
120                 if ((valid_guid && GUID_equal(&p->nc.guid, &nc->guid))
121                     || strequal(p->nc.dn, nc->dn)
122                     || (valid_sid && dom_sid_equal(&p->nc.sid, &nc->sid))) {
123                         return dreplsrv_schedule_partition_pull(s, p, mem_ctx);
124                 }
125         }
126
127         return WERR_DS_DRA_BAD_NC;
128 }
129
130
131 static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
132 {
133         struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
134                                             struct dreplsrv_out_operation);
135         struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
136         struct dreplsrv_service *s = op->service;
137         time_t t;
138         NTTIME now;
139
140         t = time(NULL);
141         unix_to_nt_time(&now, t);
142
143         rf->result_last_attempt = dreplsrv_op_pull_source_recv(subreq);
144         TALLOC_FREE(subreq);
145         if (W_ERROR_IS_OK(rf->result_last_attempt)) {
146                 rf->consecutive_sync_failures   = 0;
147                 rf->last_success                = now;
148                 DEBUG(3,("dreplsrv_op_pull_source(%s)\n",
149                         win_errstr(rf->result_last_attempt)));
150                 goto done;
151         }
152
153         rf->consecutive_sync_failures++;
154
155         DEBUG(1,("dreplsrv_op_pull_source(%s/%s) for %s failures[%u]\n",
156                  win_errstr(rf->result_last_attempt),
157                  nt_errstr(werror_to_ntstatus(rf->result_last_attempt)),
158                  ldb_dn_get_linearized(op->source_dsa->partition->dn),
159                  rf->consecutive_sync_failures));
160
161 done:
162         if (op->callback) {
163                 op->callback(s, rf->result_last_attempt, op->extended_ret);
164         }
165         talloc_free(op);
166         s->ops.current = NULL;
167         dreplsrv_run_pending_ops(s);
168         dreplsrv_notify_run_ops(s);
169 }
170
171 void dreplsrv_run_pending_ops(struct dreplsrv_service *s)
172 {
173         struct dreplsrv_out_operation *op;
174         time_t t;
175         NTTIME now;
176         struct tevent_req *subreq;
177
178         if (s->ops.current || s->ops.n_current) {
179                 /* if there's still one running, we're done */
180                 return;
181         }
182
183         if (!s->ops.pending) {
184                 /* if there're no pending operations, we're done */
185                 return;
186         }
187
188         t = time(NULL);
189         unix_to_nt_time(&now, t);
190
191         op = s->ops.pending;
192         s->ops.current = op;
193         DLIST_REMOVE(s->ops.pending, op);
194
195         op->source_dsa->repsFrom1->last_attempt = now;
196
197         subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
198         if (!subreq) {
199                 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
200
201                 rf->result_last_attempt = WERR_NOMEM;
202                 rf->consecutive_sync_failures++;
203                 s->ops.current = NULL;
204
205                 DEBUG(1,("dreplsrv_op_pull_source(%s/%s) failures[%u]\n",
206                         win_errstr(rf->result_last_attempt),
207                         nt_errstr(werror_to_ntstatus(rf->result_last_attempt)),
208                         rf->consecutive_sync_failures));
209                 return;
210         }
211         tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);
212 }