s4-kcc: add a very simple KCC
[Samba/aatanasov.git] / source4 / dsdb / repl / drepl_service.c
blobeb49da3d41122fdc2e44f33e8b16d1310abdd298
1 /*
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/>.
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 "param/param.h"
36 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
38 NTSTATUS status;
40 status = auth_system_session_info(service, service->task->lp_ctx,
41 &service->system_session_info);
42 if (!NT_STATUS_IS_OK(status)) {
43 return ntstatus_to_werror(status);
46 return WERR_OK;
49 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service, struct loadparm_context *lp_ctx)
51 const struct GUID *ntds_guid;
52 struct drsuapi_DsBindInfo28 *bind_info28;
54 service->samdb = samdb_connect(service, service->task->event_ctx, lp_ctx, service->system_session_info);
55 if (!service->samdb) {
56 return WERR_DS_UNAVAILABLE;
59 ntds_guid = samdb_ntds_objectGUID(service->samdb);
60 if (!ntds_guid) {
61 return WERR_DS_UNAVAILABLE;
64 service->ntds_guid = *ntds_guid;
66 bind_info28 = &service->bind_info28;
67 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
68 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
69 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
70 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
71 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
72 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
73 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
74 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
75 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
76 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
77 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
78 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
79 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
80 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
81 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
82 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
83 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
84 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
85 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
86 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_00100000;
87 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
88 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
89 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
90 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
91 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
92 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
93 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
94 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
95 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
96 #if 0 /* we don't support XPRESS compression yet */
97 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
98 #endif
99 /* TODO: fill in site_guid */
100 bind_info28->site_guid = GUID_zero();
101 /* TODO: find out how this is really triggered! */
102 bind_info28->pid = 0;
103 bind_info28->repl_epoch = 0;
105 return WERR_OK;
109 DsReplicaSync messages from the DRSUAPI server are forwarded here
111 static NTSTATUS drepl_replica_sync(struct irpc_message *msg,
112 struct drsuapi_DsReplicaSync *r)
114 struct dreplsrv_service *service = talloc_get_type(msg->private_data,
115 struct dreplsrv_service);
116 WERROR werr;
117 struct GUID *guid = &r->in.req.req1.naming_context->guid;
119 werr = dreplsrv_schedule_partition_pull_by_guid(service, msg, guid);
120 if (W_ERROR_IS_OK(werr)) {
121 DEBUG(3,("drepl_replica_sync: forcing sync of partition %s\n",
122 GUID_string(msg, guid)));
123 dreplsrv_run_pending_ops(service);
124 } else {
125 DEBUG(3,("drepl_replica_sync: failed setup of sync of partition %s - %s\n",
126 GUID_string(msg, guid), win_errstr(werr)));
127 return NT_STATUS_INTERNAL_ERROR;
129 return NT_STATUS_OK;
133 startup the dsdb replicator service task
135 static void dreplsrv_task_init(struct task_server *task)
137 WERROR status;
138 struct dreplsrv_service *service;
139 uint32_t periodic_startup_interval;
141 switch (lp_server_role(task->lp_ctx)) {
142 case ROLE_STANDALONE:
143 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration");
144 return;
145 case ROLE_DOMAIN_MEMBER:
146 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration");
147 return;
148 case ROLE_DOMAIN_CONTROLLER:
149 /* Yes, we want DSDB replication */
150 break;
153 task_server_set_title(task, "task[dreplsrv]");
155 service = talloc_zero(task, struct dreplsrv_service);
156 if (!service) {
157 task_server_terminate(task, "dreplsrv_task_init: out of memory");
158 return;
160 service->task = task;
161 service->startup_time = timeval_current();
162 task->private_data = service;
164 status = dreplsrv_init_creds(service);
165 if (!W_ERROR_IS_OK(status)) {
166 task_server_terminate(task, talloc_asprintf(task,
167 "dreplsrv: Failed to obtain server credentials: %s\n",
168 win_errstr(status)));
169 return;
172 status = dreplsrv_connect_samdb(service, task->lp_ctx);
173 if (!W_ERROR_IS_OK(status)) {
174 task_server_terminate(task, talloc_asprintf(task,
175 "dreplsrv: Failed to connect to local samdb: %s\n",
176 win_errstr(status)));
177 return;
180 status = dreplsrv_load_partitions(service);
181 if (!W_ERROR_IS_OK(status)) {
182 task_server_terminate(task, talloc_asprintf(task,
183 "dreplsrv: Failed to load partitions: %s\n",
184 win_errstr(status)));
185 return;
188 periodic_startup_interval = lp_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_startup_interval", 15); /* in seconds */
189 service->periodic.interval = lp_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_interval", 300); /* in seconds */
191 status = dreplsrv_periodic_schedule(service, periodic_startup_interval);
192 if (!W_ERROR_IS_OK(status)) {
193 task_server_terminate(task, talloc_asprintf(task,
194 "dreplsrv: Failed to periodic schedule: %s\n",
195 win_errstr(status)));
196 return;
199 irpc_add_name(task->msg_ctx, "dreplsrv");
201 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICASYNC, drepl_replica_sync, service);
205 register ourselves as a available server
207 NTSTATUS server_service_drepl_init(void)
209 return register_server_service("drepl", dreplsrv_task_init);