s4-repl: cleanup the extended op calls in repl server
[anatoliy/anatoliy.git] / source4 / dsdb / repl / drepl_service.c
1 /*
2    Unix SMB/CIFS mplementation.
3    DSDB replication service
4
5    Copyright (C) Stefan Metzmacher 2007
6    Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
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 3 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, see <http://www.gnu.org/licenses/>.
20
21 */
22
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "smbd/service.h"
27 #include "lib/events/events.h"
28 #include "lib/messaging/irpc.h"
29 #include "dsdb/repl/drepl_service.h"
30 #include "lib/ldb/include/ldb_errors.h"
31 #include "../lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 #include "librpc/gen_ndr/ndr_drsblobs.h"
35 #include "librpc/gen_ndr/ndr_irpc.h"
36 #include "param/param.h"
37
38 /**
39  * Call-back data for _drepl_replica_sync_done_cb()
40  */
41 struct drepl_replica_sync_cb_data {
42         struct irpc_message *msg;
43         struct drsuapi_DsReplicaSync *r;
44
45         /* number of ops left to be completed */
46         int ops_count;
47
48         /* last failure error code */
49         WERROR werr_last_failure;
50 };
51
52
53 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
54 {
55         service->system_session_info = system_session(service->task->lp_ctx);
56         if (service->system_session_info == NULL) {
57                 return WERR_NOMEM;
58         }
59
60         return WERR_OK;
61 }
62
63 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service, struct loadparm_context *lp_ctx)
64 {
65         const struct GUID *ntds_guid;
66         struct drsuapi_DsBindInfo28 *bind_info28;
67
68         service->samdb = samdb_connect(service, service->task->event_ctx, lp_ctx, service->system_session_info);
69         if (!service->samdb) {
70                 return WERR_DS_UNAVAILABLE;
71         }
72
73         ntds_guid = samdb_ntds_objectGUID(service->samdb);
74         if (!ntds_guid) {
75                 return WERR_DS_UNAVAILABLE;
76         }
77         service->ntds_guid = *ntds_guid;
78
79         bind_info28                             = &service->bind_info28;
80         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
81         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
82         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
83         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
84         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
85         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
86         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
87         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
88         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
89         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
90         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
91         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
92         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
93         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
94         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
95         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
96         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
97         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
98         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
99         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
100         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
101         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
102         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
103         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
104         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
105         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
106         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
107         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
108         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
109 #if 0 /* we don't support XPRESS compression yet */
110         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
111 #endif
112         /* TODO: fill in site_guid */
113         bind_info28->site_guid                  = GUID_zero();
114         /* TODO: find out how this is really triggered! */
115         bind_info28->pid                        = 0;
116         bind_info28->repl_epoch                 = 0;
117
118         return WERR_OK;
119 }
120
121
122 /**
123  * Callback for dreplsrv_out_operation operation completion.
124  *
125  * We just need to complete a waiting IRPC message here.
126  * In case pull operation has failed,
127  * caller of this callback will dump
128  * failure information.
129  *
130  * NOTE: cb_data is allocated in IRPC msg's context
131  * and will be freed during irpc_send_reply() call.
132  */
133 static void _drepl_replica_sync_done_cb(struct dreplsrv_service *service,
134                                         WERROR werr,
135                                         enum drsuapi_DsExtendedError ext_err,
136                                         void *cb_data)
137 {
138         struct drepl_replica_sync_cb_data *data = talloc_get_type(cb_data,
139                                                                   struct drepl_replica_sync_cb_data);
140         struct irpc_message *msg = data->msg;
141         struct drsuapi_DsReplicaSync *r = data->r;
142
143         /* store last bad result */
144         if (W_ERROR_IS_OK(werr)) {
145                 data->werr_last_failure = werr;
146         }
147
148         /* decrement pending ops count */
149         data->ops_count--;
150
151         if (data->ops_count == 0) {
152                 /* Return result to client */
153                 r->out.result = data->werr_last_failure;
154
155                 /* complete IRPC message */
156                 irpc_send_reply(msg, NT_STATUS_OK);
157         }
158 }
159
160 /**
161  * Helper to schedule a replication operation with a source DSA.
162  * If 'data' is valid pointer, then a callback
163  * for the operation is passed and 'data->msg' is
164  * marked as 'deferred' - defer_reply = true
165  */
166 static WERROR _drepl_schedule_replication(struct dreplsrv_service *service,
167                                           struct dreplsrv_partition_source_dsa *dsa,
168                                           struct drsuapi_DsReplicaObjectIdentifier *nc,
169                                           struct drepl_replica_sync_cb_data *data,
170                                           TALLOC_CTX *mem_ctx)
171 {
172         WERROR werr;
173         dreplsrv_extended_callback_t fn_callback = NULL;
174
175         if (data) {
176                 fn_callback = _drepl_replica_sync_done_cb;
177         }
178
179         /* schedule replication item */
180         werr = dreplsrv_schedule_partition_pull_source(service, dsa,
181                                                        DRSUAPI_EXOP_NONE, 0,
182                                                        fn_callback, data);
183         if (!W_ERROR_IS_OK(werr)) {
184                 DEBUG(0,("%s: failed setup of sync of partition (%s, %s, %s) - %s\n",
185                          __FUNCTION__,
186                          GUID_string(mem_ctx, &nc->guid),
187                          nc->dn,
188                          dsa->repsFrom1->other_info->dns_name,
189                          win_errstr(werr)));
190                 return werr;
191         }
192         /* log we've scheduled a replication item */
193         DEBUG(3,("%s: forcing sync of partition (%s, %s, %s)\n",
194                  __FUNCTION__,
195                  GUID_string(mem_ctx, &nc->guid),
196                  nc->dn,
197                  dsa->repsFrom1->other_info->dns_name));
198
199         /* mark IRPC message as deferred if necessary */
200         if (data) {
201                 data->ops_count++;
202                 data->msg->defer_reply = true;
203         }
204
205         return WERR_OK;
206 }
207
208 /*
209   DsReplicaSync messages from the DRSUAPI server are forwarded here
210  */
211 static NTSTATUS drepl_replica_sync(struct irpc_message *msg,
212                                    struct drsuapi_DsReplicaSync *r)
213 {
214         WERROR werr;
215         struct dreplsrv_partition *p;
216         struct drepl_replica_sync_cb_data *cb_data;
217         struct dreplsrv_partition_source_dsa *dsa;
218         struct drsuapi_DsReplicaSyncRequest1 *req1;
219         struct drsuapi_DsReplicaObjectIdentifier *nc;
220         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
221                                                            struct dreplsrv_service);
222
223 #define REPLICA_SYNC_FAIL(_msg, _werr) do {\
224                 if (!W_ERROR_IS_OK(werr)) { \
225                         DEBUG(0,(__location__ ": Failure - %s. werr = %s\n", \
226                                  _msg, win_errstr(_werr))); \
227                         NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync, r); \
228                 } \
229                 r->out.result = _werr; \
230                 goto done;\
231         } while(0)
232
233
234         if (r->in.level != 1) {
235                 REPLICA_SYNC_FAIL("Unsupported level",
236                                   WERR_DS_DRA_INVALID_PARAMETER);
237         }
238
239         req1 = &r->in.req->req1;
240         nc   = req1->naming_context;
241
242         /* Check input parameters */
243         if (!nc) {
244                 REPLICA_SYNC_FAIL("Invalid Naming Context",
245                                   WERR_DS_DRA_INVALID_PARAMETER);
246         }
247
248         /* Find Naming context to be synchronized */
249         werr = dreplsrv_partition_find_for_nc(service,
250                                               &nc->guid, &nc->sid, nc->dn,
251                                               &p);
252         if (!W_ERROR_IS_OK(werr)) {
253                 REPLICA_SYNC_FAIL("Failed to find requested Naming Context",
254                                   werr);
255         }
256
257         /* should we process it asynchronously? */
258         if (req1->options & DRSUAPI_DRS_ASYNC_OP) {
259                 cb_data = NULL;
260         } else {
261                 cb_data = talloc_zero(msg, struct drepl_replica_sync_cb_data);
262                 if (!cb_data) {
263                         REPLICA_SYNC_FAIL("Not enought memory",
264                                           WERR_DS_DRA_INTERNAL_ERROR);
265                 }
266
267                 cb_data->msg = msg;
268                 cb_data->r   = r;
269                 cb_data->werr_last_failure = WERR_OK;
270         }
271
272         /* collect source DSAs to sync with */
273         if (req1->options & DRSUAPI_DRS_SYNC_ALL) {
274                 for (dsa = p->sources; dsa; dsa = dsa->next) {
275                         /* schedule replication item */
276                         werr = _drepl_schedule_replication(service, dsa, nc, cb_data, msg);
277                         if (!W_ERROR_IS_OK(werr)) {
278                                 REPLICA_SYNC_FAIL("_drepl_schedule_replication() failed",
279                                                   werr);
280                         }
281                 }
282         } else {
283                 if (req1->options & DRSUAPI_DRS_SYNC_BYNAME) {
284                         /* client should pass at least valid string */
285                         if (!req1->source_dsa_dns) {
286                                 REPLICA_SYNC_FAIL("'source_dsa_dns' is not valid",
287                                                   WERR_DS_DRA_INVALID_PARAMETER);
288                         }
289
290                         werr = dreplsrv_partition_source_dsa_by_dns(p,
291                                                                     req1->source_dsa_dns,
292                                                                     &dsa);
293                 } else {
294                         /* client should pass at least some GUID */
295                         if (GUID_all_zero(&req1->source_dsa_guid)) {
296                                 REPLICA_SYNC_FAIL("'source_dsa_guid' is not valid",
297                                                   WERR_DS_DRA_INVALID_PARAMETER);
298                         }
299
300                         werr = dreplsrv_partition_source_dsa_by_guid(p,
301                                                                      &req1->source_dsa_guid,
302                                                                      &dsa);
303                 }
304                 if (!W_ERROR_IS_OK(werr)) {
305                         REPLICA_SYNC_FAIL("Failed to locate source DSA for given NC",
306                                           WERR_DS_DRA_NO_REPLICA);
307                 }
308
309                 /* schedule replication item */
310                 werr = _drepl_schedule_replication(service, dsa, nc, cb_data, msg);
311                 if (!W_ERROR_IS_OK(werr)) {
312                         REPLICA_SYNC_FAIL("_drepl_schedule_replication() failed",
313                                           werr);
314                 }
315         }
316
317         /* if we got here, everything is OK */
318         r->out.result = WERR_OK;
319
320         /* force execution of scheduled replications */
321         dreplsrv_run_pending_ops(service);
322
323 done:
324         return NT_STATUS_OK;
325 }
326
327 /**
328  * Called when drplsrv should refresh its state.
329  * For example, when KCC change topology, dreplsrv
330  * should update its cache
331  *
332  * @param partition_dn If not empty/NULL, partition to update
333  */
334 static NTSTATUS dreplsrv_refresh(struct irpc_message *msg,
335                                  struct dreplsrv_refresh *r)
336 {
337         struct dreplsrv_service *s = talloc_get_type(msg->private_data,
338                                                      struct dreplsrv_service);
339
340         r->out.result = dreplsrv_refresh_partitions(s);
341
342         return NT_STATUS_OK;
343 }
344
345 static NTSTATUS drepl_take_FSMO_role(struct irpc_message *msg,
346                                      struct drepl_takeFSMORole *r)
347 {
348         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
349                                                            struct dreplsrv_service);
350         r->out.result = dreplsrv_fsmo_role_check(service, r->in.role);
351         return NT_STATUS_OK;
352 }
353
354 /**
355  * Called when the auth code wants us to try and replicate
356  * a users secrets
357  */
358 static NTSTATUS drepl_trigger_repl_secret(struct irpc_message *msg,
359                                           struct drepl_trigger_repl_secret *r)
360 {
361         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
362                                                            struct dreplsrv_service);
363
364
365         drepl_repl_secret(service, r->in.user_dn);
366
367         /* we are not going to be sending a reply to this request */
368         msg->no_reply = true;
369
370         return NT_STATUS_OK;
371 }
372
373
374 /*
375   startup the dsdb replicator service task
376 */
377 static void dreplsrv_task_init(struct task_server *task)
378 {
379         WERROR status;
380         struct dreplsrv_service *service;
381         uint32_t periodic_startup_interval;
382         bool am_rodc;
383         int ret;
384
385         switch (lpcfg_server_role(task->lp_ctx)) {
386         case ROLE_STANDALONE:
387                 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration",
388                                       false);
389                 return;
390         case ROLE_DOMAIN_MEMBER:
391                 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration",
392                                       false);
393                 return;
394         case ROLE_DOMAIN_CONTROLLER:
395                 /* Yes, we want DSDB replication */
396                 break;
397         }
398
399         task_server_set_title(task, "task[dreplsrv]");
400
401         service = talloc_zero(task, struct dreplsrv_service);
402         if (!service) {
403                 task_server_terminate(task, "dreplsrv_task_init: out of memory", true);
404                 return;
405         }
406         service->task           = task;
407         service->startup_time   = timeval_current();
408         task->private_data      = service;
409
410         status = dreplsrv_init_creds(service);
411         if (!W_ERROR_IS_OK(status)) {
412                 task_server_terminate(task, talloc_asprintf(task,
413                                       "dreplsrv: Failed to obtain server credentials: %s\n",
414                                                             win_errstr(status)), true);
415                 return;
416         }
417
418         status = dreplsrv_connect_samdb(service, task->lp_ctx);
419         if (!W_ERROR_IS_OK(status)) {
420                 task_server_terminate(task, talloc_asprintf(task,
421                                       "dreplsrv: Failed to connect to local samdb: %s\n",
422                                                             win_errstr(status)), true);
423                 return;
424         }
425
426         status = dreplsrv_load_partitions(service);
427         if (!W_ERROR_IS_OK(status)) {
428                 task_server_terminate(task, talloc_asprintf(task,
429                                       "dreplsrv: Failed to load partitions: %s\n",
430                                                             win_errstr(status)), true);
431                 return;
432         }
433
434         periodic_startup_interval       = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_startup_interval", 15); /* in seconds */
435         service->periodic.interval      = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_interval", 300); /* in seconds */
436
437         status = dreplsrv_periodic_schedule(service, periodic_startup_interval);
438         if (!W_ERROR_IS_OK(status)) {
439                 task_server_terminate(task, talloc_asprintf(task,
440                                       "dreplsrv: Failed to periodic schedule: %s\n",
441                                                             win_errstr(status)), true);
442                 return;
443         }
444
445         /* if we are a RODC then we do not send DSReplicaSync*/
446         ret = samdb_rodc(service->samdb, &am_rodc);
447         if (ret == LDB_SUCCESS && !am_rodc) {
448                 service->notify.interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv",
449                                                            "notify_interval", 5); /* in seconds */
450                 status = dreplsrv_notify_schedule(service, service->notify.interval);
451                 if (!W_ERROR_IS_OK(status)) {
452                         task_server_terminate(task, talloc_asprintf(task,
453                                                   "dreplsrv: Failed to setup notify schedule: %s\n",
454                                                                         win_errstr(status)), true);
455                         return;
456                 }
457         }
458
459         irpc_add_name(task->msg_ctx, "dreplsrv");
460
461         IRPC_REGISTER(task->msg_ctx, irpc, DREPLSRV_REFRESH, dreplsrv_refresh, service);
462         IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICASYNC, drepl_replica_sync, service);
463         IRPC_REGISTER(task->msg_ctx, irpc, DREPL_TAKEFSMOROLE, drepl_take_FSMO_role, service);
464         IRPC_REGISTER(task->msg_ctx, irpc, DREPL_TRIGGER_REPL_SECRET, drepl_trigger_repl_secret, service);
465         messaging_register(task->msg_ctx, service, MSG_DREPL_ALLOCATE_RID, dreplsrv_allocate_rid);
466 }
467
468 /*
469   register ourselves as a available server
470 */
471 NTSTATUS server_service_drepl_init(void)
472 {
473         return register_server_service("drepl", dreplsrv_task_init);
474 }