2 Unix SMB/CIFS mplementation.
3 DSDB replication service
5 Copyright (C) Stefan Metzmacher 2007
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.
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.
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/>.
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 "librpc/gen_ndr/ndr_irpc.h"
35 #include "param/param.h"
37 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
39 service->system_session_info = system_session(service->task->lp_ctx);
40 if (service->system_session_info == NULL) {
47 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service, struct loadparm_context *lp_ctx)
49 const struct GUID *ntds_guid;
50 struct drsuapi_DsBindInfo28 *bind_info28;
52 service->samdb = samdb_connect(service, service->task->event_ctx, lp_ctx, service->system_session_info);
53 if (!service->samdb) {
54 return WERR_DS_UNAVAILABLE;
57 ntds_guid = samdb_ntds_objectGUID(service->samdb);
59 return WERR_DS_UNAVAILABLE;
62 service->ntds_guid = *ntds_guid;
64 bind_info28 = &service->bind_info28;
65 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
66 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
67 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
68 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
69 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
70 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
71 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
72 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
73 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
74 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
75 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
76 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
77 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
78 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
79 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
80 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
81 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
82 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
83 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
84 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
85 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
86 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
87 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
88 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
89 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
90 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
91 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
92 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
93 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
94 #if 0 /* we don't support XPRESS compression yet */
95 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
97 /* TODO: fill in site_guid */
98 bind_info28->site_guid = GUID_zero();
99 /* TODO: find out how this is really triggered! */
100 bind_info28->pid = 0;
101 bind_info28->repl_epoch = 0;
108 DsReplicaSync messages from the DRSUAPI server are forwarded here
110 static NTSTATUS drepl_replica_sync(struct irpc_message *msg,
111 struct drsuapi_DsReplicaSync *r)
114 struct dreplsrv_partition *p;
115 struct dreplsrv_partition_source_dsa *dsa;
116 struct drsuapi_DsReplicaSyncRequest1 *req1;
117 struct drsuapi_DsReplicaObjectIdentifier *nc;
118 struct dreplsrv_service *service = talloc_get_type(msg->private_data,
119 struct dreplsrv_service);
121 #define REPLICA_SYNC_FAIL(_werr) do {r->out.result = _werr; goto done;} while(0)
123 if (r->in.level != 1) {
124 DEBUG(0,("%s: Level %d is not supported yet.\n",
125 __FUNCTION__, r->in.level));
126 REPLICA_SYNC_FAIL(WERR_DS_DRA_INVALID_PARAMETER);
129 req1 = &r->in.req->req1;
130 nc = req1->naming_context;
132 /* Check input parameters */
134 REPLICA_SYNC_FAIL(WERR_DS_DRA_INVALID_PARAMETER);
137 /* Find Naming context to be synchronized */
138 werr = dreplsrv_partition_find_for_nc(service,
139 &nc->guid, &nc->sid, nc->dn,
141 if (!W_ERROR_IS_OK(werr)) {
142 DEBUG(0,("%s: failed to find NC for (%s, %s) - %s\n",
144 GUID_string(msg, &nc->guid),
147 REPLICA_SYNC_FAIL(werr);
150 /* collect source DSAs to sync with */
151 if (req1->options & DRSUAPI_DRS_SYNC_ALL) {
152 for (dsa = p->sources; dsa; dsa = dsa->next) {
153 /* schedule replication item */
154 werr = dreplsrv_schedule_partition_pull_source(service, dsa,
155 DRSUAPI_EXOP_NONE, 0,
157 if (!W_ERROR_IS_OK(werr)) {
158 DEBUG(0,("%s: failed setup of sync of partition (%s, %s, %s) - %s\n",
160 GUID_string(msg, &nc->guid),
162 dsa->repsFrom1->other_info->dns_name,
164 REPLICA_SYNC_FAIL(werr);
166 /* log we've scheduled replication item */
167 DEBUG(3,("%s: forcing sync of partition (%s, %s, %s)\n",
169 GUID_string(msg, &nc->guid),
171 dsa->repsFrom1->other_info->dns_name));
174 if (req1->options & DRSUAPI_DRS_SYNC_BYNAME) {
175 /* client should pass at least valid string */
176 if (!req1->source_dsa_dns) {
177 REPLICA_SYNC_FAIL(WERR_DS_DRA_INVALID_PARAMETER);
180 werr = dreplsrv_partition_source_dsa_by_dns(p,
181 req1->source_dsa_dns,
184 /* client should pass at least some GUID */
185 if (GUID_all_zero(&req1->source_dsa_guid)) {
186 REPLICA_SYNC_FAIL(WERR_DS_DRA_INVALID_PARAMETER);
189 werr = dreplsrv_partition_source_dsa_by_guid(p,
190 &req1->source_dsa_guid,
193 if (!W_ERROR_IS_OK(werr)) {
194 DEBUG(0,("%s: Failed to locate source DSA %s for NC %s.\n",
196 (req1->options & DRSUAPI_DRS_SYNC_BYNAME)
197 ? req1->source_dsa_dns
198 : GUID_string(r, &req1->source_dsa_guid),
200 REPLICA_SYNC_FAIL(WERR_DS_DRA_NO_REPLICA);
203 /* schedule replication item */
204 werr = dreplsrv_schedule_partition_pull_source(service, dsa,
205 DRSUAPI_EXOP_NONE, 0,
207 if (!W_ERROR_IS_OK(werr)) {
208 DEBUG(0,("%s: failed setup of sync of partition (%s, %s, %s) - %s\n",
210 GUID_string(msg, &nc->guid),
212 dsa->repsFrom1->other_info->dns_name,
214 REPLICA_SYNC_FAIL(werr);
216 /* log we've scheduled replication item */
217 DEBUG(3,("%s: forcing sync of partition (%s, %s, %s)\n",
219 GUID_string(msg, &nc->guid),
221 dsa->repsFrom1->other_info->dns_name));
224 /* if we got here, everything is OK */
225 r->out.result = WERR_OK;
227 /* force execution of scheduled replications */
228 dreplsrv_run_pending_ops(service);
235 * Called when drplsrv should refresh its state.
236 * For example, when KCC change topology, dreplsrv
237 * should update its cache
239 * @param partition_dn If not empty/NULL, partition to update
241 static NTSTATUS dreplsrv_refresh(struct irpc_message *msg,
242 struct dreplsrv_refresh *r)
244 struct dreplsrv_service *s = talloc_get_type(msg->private_data,
245 struct dreplsrv_service);
247 r->out.result = dreplsrv_refresh_partitions(s);
253 startup the dsdb replicator service task
255 static void dreplsrv_task_init(struct task_server *task)
258 struct dreplsrv_service *service;
259 uint32_t periodic_startup_interval;
263 switch (lpcfg_server_role(task->lp_ctx)) {
264 case ROLE_STANDALONE:
265 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration",
268 case ROLE_DOMAIN_MEMBER:
269 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration",
272 case ROLE_DOMAIN_CONTROLLER:
273 /* Yes, we want DSDB replication */
277 task_server_set_title(task, "task[dreplsrv]");
279 service = talloc_zero(task, struct dreplsrv_service);
281 task_server_terminate(task, "dreplsrv_task_init: out of memory", true);
284 service->task = task;
285 service->startup_time = timeval_current();
286 task->private_data = service;
288 status = dreplsrv_init_creds(service);
289 if (!W_ERROR_IS_OK(status)) {
290 task_server_terminate(task, talloc_asprintf(task,
291 "dreplsrv: Failed to obtain server credentials: %s\n",
292 win_errstr(status)), true);
296 status = dreplsrv_connect_samdb(service, task->lp_ctx);
297 if (!W_ERROR_IS_OK(status)) {
298 task_server_terminate(task, talloc_asprintf(task,
299 "dreplsrv: Failed to connect to local samdb: %s\n",
300 win_errstr(status)), true);
304 status = dreplsrv_load_partitions(service);
305 if (!W_ERROR_IS_OK(status)) {
306 task_server_terminate(task, talloc_asprintf(task,
307 "dreplsrv: Failed to load partitions: %s\n",
308 win_errstr(status)), true);
312 periodic_startup_interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_startup_interval", 15); /* in seconds */
313 service->periodic.interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_interval", 300); /* in seconds */
315 status = dreplsrv_periodic_schedule(service, periodic_startup_interval);
316 if (!W_ERROR_IS_OK(status)) {
317 task_server_terminate(task, talloc_asprintf(task,
318 "dreplsrv: Failed to periodic schedule: %s\n",
319 win_errstr(status)), true);
323 /* if we are a RODC then we do not send DSReplicaSync*/
324 ret = samdb_rodc(service->samdb, &am_rodc);
325 if (ret == LDB_SUCCESS && !am_rodc) {
326 service->notify.interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv",
327 "notify_interval", 5); /* in seconds */
328 status = dreplsrv_notify_schedule(service, service->notify.interval);
329 if (!W_ERROR_IS_OK(status)) {
330 task_server_terminate(task, talloc_asprintf(task,
331 "dreplsrv: Failed to setup notify schedule: %s\n",
332 win_errstr(status)), true);
337 irpc_add_name(task->msg_ctx, "dreplsrv");
339 IRPC_REGISTER(task->msg_ctx, irpc, DREPLSRV_REFRESH, dreplsrv_refresh, service);
340 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICASYNC, drepl_replica_sync, service);
341 messaging_register(task->msg_ctx, service, MSG_DREPL_ALLOCATE_RID, dreplsrv_allocate_rid);
345 register ourselves as a available server
347 NTSTATUS server_service_drepl_init(void)
349 return register_server_service("drepl", dreplsrv_task_init);