wafsamba: remove unused Build.BuildContext.pre_build overload
[Samba.git] / source4 / dsdb / repl / drepl_service.c
blob350ed611aea6114064a6b50f726083d1daa1c593
1 /*
2 Unix SMB/CIFS mplementation.
3 DSDB replication service
5 Copyright (C) Stefan Metzmacher 2007
6 Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
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.
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.
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/>.
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 "dsdb/repl/drepl_service.h"
29 #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 "librpc/gen_ndr/ndr_irpc.h"
35 #include "param/param.h"
36 #include "libds/common/roles.h"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_DRS_REPL
41 /**
42 * Call-back data for _drepl_replica_sync_done_cb()
44 struct drepl_replica_sync_cb_data {
45 struct irpc_message *msg;
46 struct drsuapi_DsReplicaSync *r;
48 /* number of ops left to be completed */
49 int ops_count;
51 /* last failure error code */
52 WERROR werr_last_failure;
56 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
58 service->system_session_info = system_session(service->task->lp_ctx);
59 if (service->system_session_info == NULL) {
60 return WERR_NOT_ENOUGH_MEMORY;
63 return WERR_OK;
66 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service, struct loadparm_context *lp_ctx)
68 const struct GUID *ntds_guid;
69 struct drsuapi_DsBindInfo28 *bind_info28;
71 service->samdb = samdb_connect(service,
72 service->task->event_ctx,
73 lp_ctx,
74 service->system_session_info,
75 NULL,
76 0);
77 if (!service->samdb) {
78 return WERR_DS_UNAVAILABLE;
81 ntds_guid = samdb_ntds_objectGUID(service->samdb);
82 if (!ntds_guid) {
83 return WERR_DS_UNAVAILABLE;
85 service->ntds_guid = *ntds_guid;
87 if (samdb_rodc(service->samdb, &service->am_rodc) != LDB_SUCCESS) {
88 DEBUG(0,(__location__ ": Failed to determine RODC status\n"));
89 return WERR_DS_UNAVAILABLE;
92 bind_info28 = &service->bind_info28;
93 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
94 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
95 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
96 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
97 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
98 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
99 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
100 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
101 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
102 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
103 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
104 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
105 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
106 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
107 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
108 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
109 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
110 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
111 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
112 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
113 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
114 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
115 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
116 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
117 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
118 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
119 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
120 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
121 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
122 #if 0 /* we don't support XPRESS compression yet */
123 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
124 #endif
125 /* TODO: fill in site_guid */
126 bind_info28->site_guid = GUID_zero();
127 /* TODO: find out how this is really triggered! */
128 bind_info28->pid = 0;
129 bind_info28->repl_epoch = 0;
131 return WERR_OK;
136 * Callback for dreplsrv_out_operation operation completion.
138 * We just need to complete a waiting IRPC message here.
139 * In case pull operation has failed,
140 * caller of this callback will dump
141 * failure information.
143 * NOTE: cb_data is allocated in IRPC msg's context
144 * and will be freed during irpc_send_reply() call.
146 static void _drepl_replica_sync_done_cb(struct dreplsrv_service *service,
147 WERROR werr,
148 enum drsuapi_DsExtendedError ext_err,
149 void *cb_data)
151 struct drepl_replica_sync_cb_data *data = talloc_get_type(cb_data,
152 struct drepl_replica_sync_cb_data);
153 struct irpc_message *msg = data->msg;
154 struct drsuapi_DsReplicaSync *r = data->r;
156 /* store last bad result */
157 if (!W_ERROR_IS_OK(werr)) {
158 data->werr_last_failure = werr;
161 /* decrement pending ops count */
162 data->ops_count--;
164 if (data->ops_count == 0) {
165 /* Return result to client */
166 r->out.result = data->werr_last_failure;
168 /* complete IRPC message */
169 irpc_send_reply(msg, NT_STATUS_OK);
174 * Helper to schedule a replication operation with a source DSA.
175 * If 'data' is valid pointer, then a callback
176 * for the operation is passed and 'data->msg' is
177 * marked as 'deferred' - defer_reply = true
179 static WERROR _drepl_schedule_replication(struct dreplsrv_service *service,
180 struct dreplsrv_partition_source_dsa *dsa,
181 struct drsuapi_DsReplicaObjectIdentifier *nc,
182 uint32_t rep_options,
183 struct drepl_replica_sync_cb_data *data,
184 TALLOC_CTX *mem_ctx)
186 WERROR werr;
187 dreplsrv_extended_callback_t fn_callback = NULL;
189 if (data) {
190 fn_callback = _drepl_replica_sync_done_cb;
193 /* schedule replication item */
194 werr = dreplsrv_schedule_partition_pull_source(service, dsa, rep_options,
195 DRSUAPI_EXOP_NONE, 0,
196 fn_callback, data);
197 if (!W_ERROR_IS_OK(werr)) {
198 DEBUG(0,("%s: failed setup of sync of partition (%s, %s, %s) - %s\n",
199 __FUNCTION__,
200 GUID_string(mem_ctx, &nc->guid),
201 nc->dn,
202 dsa->repsFrom1->other_info->dns_name,
203 win_errstr(werr)));
204 return werr;
206 /* log we've scheduled a replication item */
207 DEBUG(3,("%s: forcing sync of partition (%s, %s, %s)\n",
208 __FUNCTION__,
209 GUID_string(mem_ctx, &nc->guid),
210 nc->dn,
211 dsa->repsFrom1->other_info->dns_name));
213 /* mark IRPC message as deferred if necessary */
214 if (data) {
215 data->ops_count++;
216 data->msg->defer_reply = true;
219 return WERR_OK;
223 DsReplicaSync messages from the DRSUAPI server are forwarded here
225 static NTSTATUS drepl_replica_sync(struct irpc_message *msg,
226 struct drsuapi_DsReplicaSync *r)
228 WERROR werr;
229 struct dreplsrv_partition *p;
230 struct drepl_replica_sync_cb_data *cb_data;
231 struct dreplsrv_partition_source_dsa *dsa;
232 struct drsuapi_DsReplicaSyncRequest1 *req1;
233 struct drsuapi_DsReplicaObjectIdentifier *nc;
234 struct dreplsrv_service *service = talloc_get_type(msg->private_data,
235 struct dreplsrv_service);
237 #define REPLICA_SYNC_FAIL(_msg, _werr) do {\
238 if (!W_ERROR_IS_OK(_werr)) { \
239 DEBUG(0,(__location__ ": Failure - %s. werr = %s\n", \
240 _msg, win_errstr(_werr))); \
241 NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync, r); \
243 r->out.result = _werr; \
244 goto done;\
245 } while(0)
248 if (r->in.level != 1) {
249 REPLICA_SYNC_FAIL("Unsupported level",
250 WERR_DS_DRA_INVALID_PARAMETER);
253 req1 = &r->in.req->req1;
254 nc = req1->naming_context;
256 /* Check input parameters */
257 if (!nc) {
258 REPLICA_SYNC_FAIL("Invalid Naming Context",
259 WERR_DS_DRA_INVALID_PARAMETER);
262 /* Find Naming context to be synchronized */
263 werr = dreplsrv_partition_find_for_nc(service,
264 &nc->guid, &nc->sid, nc->dn,
265 &p);
266 if (!W_ERROR_IS_OK(werr)) {
267 REPLICA_SYNC_FAIL("Failed to find requested Naming Context",
268 werr);
271 /* should we process it asynchronously? */
272 if (req1->options & DRSUAPI_DRS_ASYNC_OP) {
273 cb_data = NULL;
274 } else {
275 cb_data = talloc_zero(msg, struct drepl_replica_sync_cb_data);
276 if (!cb_data) {
277 REPLICA_SYNC_FAIL("Not enough memory",
278 WERR_DS_DRA_INTERNAL_ERROR);
281 cb_data->msg = msg;
282 cb_data->r = r;
283 cb_data->werr_last_failure = WERR_OK;
286 /* collect source DSAs to sync with */
287 if (req1->options & DRSUAPI_DRS_SYNC_ALL) {
288 for (dsa = p->sources; dsa; dsa = dsa->next) {
289 /* schedule replication item */
290 werr = _drepl_schedule_replication(service, dsa, nc,
291 req1->options, cb_data, msg);
292 if (!W_ERROR_IS_OK(werr)) {
293 REPLICA_SYNC_FAIL("_drepl_schedule_replication() failed",
294 werr);
297 } else {
298 if (req1->options & DRSUAPI_DRS_SYNC_BYNAME) {
299 /* client should pass at least valid string */
300 if (!req1->source_dsa_dns) {
301 REPLICA_SYNC_FAIL("'source_dsa_dns' is not valid",
302 WERR_DS_DRA_INVALID_PARAMETER);
305 werr = dreplsrv_partition_source_dsa_by_dns(p,
306 req1->source_dsa_dns,
307 &dsa);
308 } else {
309 /* client should pass at least some GUID */
310 if (GUID_all_zero(&req1->source_dsa_guid)) {
311 REPLICA_SYNC_FAIL("'source_dsa_guid' is not valid",
312 WERR_DS_DRA_INVALID_PARAMETER);
315 werr = dreplsrv_partition_source_dsa_by_guid(p,
316 &req1->source_dsa_guid,
317 &dsa);
318 if (W_ERROR_EQUAL(werr, WERR_DS_DRA_NO_REPLICA)) {
319 /* we don't have this source setup as
320 a replication partner. Create a
321 temporary dsa structure for this
322 replication */
323 werr = dreplsrv_partition_source_dsa_temporary(p,
324 msg,
325 &req1->source_dsa_guid,
326 &dsa);
329 if (!W_ERROR_IS_OK(werr)) {
330 REPLICA_SYNC_FAIL("Failed to locate source DSA for given NC",
331 werr);
334 /* schedule replication item */
335 werr = _drepl_schedule_replication(service, dsa, nc,
336 req1->options, cb_data, msg);
337 if (!W_ERROR_IS_OK(werr)) {
338 REPLICA_SYNC_FAIL("_drepl_schedule_replication() failed",
339 werr);
343 /* if we got here, everything is OK */
344 r->out.result = WERR_OK;
347 * schedule replication event to force
348 * replication as soon as possible
350 dreplsrv_pendingops_schedule_pull_now(service);
352 done:
353 return NT_STATUS_OK;
357 * Called when drplsrv should refresh its state.
358 * For example, when KCC change topology, dreplsrv
359 * should update its cache
361 * @param partition_dn If not empty/NULL, partition to update
363 static NTSTATUS dreplsrv_refresh(struct irpc_message *msg,
364 struct dreplsrv_refresh *r)
366 struct dreplsrv_service *s = talloc_get_type(msg->private_data,
367 struct dreplsrv_service);
369 r->out.result = dreplsrv_refresh_partitions(s);
371 return NT_STATUS_OK;
375 * Called when the auth code wants us to try and replicate
376 * a users secrets
378 static NTSTATUS drepl_trigger_repl_secret(struct irpc_message *msg,
379 struct drepl_trigger_repl_secret *r)
381 struct dreplsrv_service *service = talloc_get_type(msg->private_data,
382 struct dreplsrv_service);
385 drepl_repl_secret(service, r->in.user_dn);
387 /* we are not going to be sending a reply to this request */
388 msg->no_reply = true;
390 return NT_STATUS_OK;
395 DsReplicaAdd messages from the DRSUAPI server are forwarded here
397 static NTSTATUS dreplsrv_replica_add(struct irpc_message *msg,
398 struct drsuapi_DsReplicaAdd *r)
400 struct dreplsrv_service *service = talloc_get_type(msg->private_data,
401 struct dreplsrv_service);
402 return drepl_replica_add(service, r);
406 DsReplicaDel messages from the DRSUAPI server are forwarded here
408 static NTSTATUS dreplsrv_replica_del(struct irpc_message *msg,
409 struct drsuapi_DsReplicaDel *r)
411 struct dreplsrv_service *service = talloc_get_type(msg->private_data,
412 struct dreplsrv_service);
413 return drepl_replica_del(service, r);
417 DsReplicaMod messages from the DRSUAPI server are forwarded here
419 static NTSTATUS dreplsrv_replica_mod(struct irpc_message *msg,
420 struct drsuapi_DsReplicaMod *r)
422 struct dreplsrv_service *service = talloc_get_type(msg->private_data,
423 struct dreplsrv_service);
424 return drepl_replica_mod(service, r);
429 startup the dsdb replicator service task
431 static NTSTATUS dreplsrv_task_init(struct task_server *task)
433 WERROR status;
434 struct dreplsrv_service *service;
435 uint32_t periodic_startup_interval;
437 switch (lpcfg_server_role(task->lp_ctx)) {
438 case ROLE_STANDALONE:
439 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration",
440 false);
441 return NT_STATUS_INVALID_DOMAIN_ROLE;
442 case ROLE_DOMAIN_MEMBER:
443 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration",
444 false);
445 return NT_STATUS_INVALID_DOMAIN_ROLE;
446 case ROLE_ACTIVE_DIRECTORY_DC:
447 /* Yes, we want DSDB replication */
448 break;
451 task_server_set_title(task, "task[dreplsrv]");
453 service = talloc_zero(task, struct dreplsrv_service);
454 if (!service) {
455 task_server_terminate(task, "dreplsrv_task_init: out of memory", true);
456 return NT_STATUS_NO_MEMORY;
458 service->task = task;
459 service->startup_time = timeval_current();
460 task->private_data = service;
462 status = dreplsrv_init_creds(service);
463 if (!W_ERROR_IS_OK(status)) {
464 task_server_terminate(task, talloc_asprintf(task,
465 "dreplsrv: Failed to obtain server credentials: %s\n",
466 win_errstr(status)), true);
467 return werror_to_ntstatus(status);
470 status = dreplsrv_connect_samdb(service, task->lp_ctx);
471 if (!W_ERROR_IS_OK(status)) {
472 task_server_terminate(task, talloc_asprintf(task,
473 "dreplsrv: Failed to connect to local samdb: %s\n",
474 win_errstr(status)), true);
475 return werror_to_ntstatus(status);
478 status = dreplsrv_load_partitions(service);
479 if (!W_ERROR_IS_OK(status)) {
480 task_server_terminate(task, talloc_asprintf(task,
481 "dreplsrv: Failed to load partitions: %s\n",
482 win_errstr(status)), true);
483 return werror_to_ntstatus(status);
486 periodic_startup_interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_startup_interval", 15); /* in seconds */
487 service->periodic.interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_interval", 300); /* in seconds */
489 status = dreplsrv_periodic_schedule(service, periodic_startup_interval);
490 if (!W_ERROR_IS_OK(status)) {
491 task_server_terminate(task, talloc_asprintf(task,
492 "dreplsrv: Failed to periodic schedule: %s\n",
493 win_errstr(status)), true);
494 return werror_to_ntstatus(status);
497 service->pending.im = tevent_create_immediate(service);
498 if (service->pending.im == NULL) {
499 task_server_terminate(task,
500 "dreplsrv: Failed to create immediate "
501 "task for future DsReplicaSync\n",
502 true);
503 return NT_STATUS_NO_MEMORY;
506 /* if we are a RODC then we do not send DSReplicaSync*/
507 if (!service->am_rodc) {
508 service->notify.interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv",
509 "notify_interval", 5); /* in seconds */
510 status = dreplsrv_notify_schedule(service, service->notify.interval);
511 if (!W_ERROR_IS_OK(status)) {
512 task_server_terminate(task, talloc_asprintf(task,
513 "dreplsrv: Failed to setup notify schedule: %s\n",
514 win_errstr(status)), true);
515 return werror_to_ntstatus(status);
519 irpc_add_name(task->msg_ctx, "dreplsrv");
521 IRPC_REGISTER(task->msg_ctx, irpc, DREPLSRV_REFRESH, dreplsrv_refresh, service);
522 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICASYNC, drepl_replica_sync, service);
523 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICAADD, dreplsrv_replica_add, service);
524 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICADEL, dreplsrv_replica_del, service);
525 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICAMOD, dreplsrv_replica_mod, service);
526 IRPC_REGISTER(task->msg_ctx, irpc, DREPL_TAKEFSMOROLE, drepl_take_FSMO_role, service);
527 IRPC_REGISTER(task->msg_ctx, irpc, DREPL_TRIGGER_REPL_SECRET, drepl_trigger_repl_secret, service);
528 imessaging_register(task->msg_ctx, service, MSG_DREPL_ALLOCATE_RID, dreplsrv_allocate_rid);
530 return NT_STATUS_OK;
534 register ourselves as a available server
536 NTSTATUS server_service_drepl_init(TALLOC_CTX *ctx)
538 static const struct service_details details = {
539 .inhibit_fork_on_accept = true,
540 .inhibit_pre_fork = true,
541 .task_init = dreplsrv_task_init,
542 .post_fork = NULL,
544 return register_server_service(ctx, "drepl", &details);