]> git.samba.org - obnox/samba/samba-obnox.git/blob - source4/dsdb/repl/drepl_service.c
s4-idl: redefine dreplsrv_refresh() to be alike other RPC function definitions
[obnox/samba/samba-obnox.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     
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 "librpc/gen_ndr/ndr_irpc.h"
35 #include "param/param.h"
36
37 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
38 {
39         service->system_session_info = system_session(service->task->lp_ctx);
40         if (service->system_session_info == NULL) {
41                 return WERR_NOMEM;
42         }
43
44         return WERR_OK;
45 }
46
47 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service, struct loadparm_context *lp_ctx)
48 {
49         const struct GUID *ntds_guid;
50         struct drsuapi_DsBindInfo28 *bind_info28;
51
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;
55         }
56
57         ntds_guid = samdb_ntds_objectGUID(service->samdb);
58         if (!ntds_guid) {
59                 return WERR_DS_UNAVAILABLE;
60         }
61
62         service->ntds_guid = *ntds_guid;
63
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;
96 #endif
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;
102
103         return WERR_OK;
104 }
105
106
107 /*
108   DsReplicaSync messages from the DRSUAPI server are forwarded here
109  */
110 static NTSTATUS drepl_replica_sync(struct irpc_message *msg, 
111                                    struct drsuapi_DsReplicaSync *r)
112 {
113         WERROR werr;
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);
120
121 #define REPLICA_SYNC_FAIL(_werr) do {r->out.result = _werr; goto done;} while(0)
122
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);
127         }
128
129         req1 = &r->in.req->req1;
130         nc   = req1->naming_context;
131
132         /* Check input parameters */
133         if (!nc) {
134                 REPLICA_SYNC_FAIL(WERR_DS_DRA_INVALID_PARAMETER);
135         }
136
137         /* Find Naming context to be synchronized */
138         werr = dreplsrv_partition_find_for_nc(service,
139                                               &nc->guid, &nc->sid, nc->dn,
140                                               &p);
141         if (!W_ERROR_IS_OK(werr)) {
142                 DEBUG(0,("%s: failed to find NC for (%s, %s) - %s\n",
143                          __FUNCTION__,
144                          GUID_string(msg, &nc->guid),
145                          nc->dn,
146                          win_errstr(werr)));
147                 REPLICA_SYNC_FAIL(werr);
148         }
149
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,
156                                                                        NULL, NULL);
157                         if (!W_ERROR_IS_OK(werr)) {
158                                 DEBUG(0,("%s: failed setup of sync of partition (%s, %s, %s) - %s\n",
159                                          __FUNCTION__,
160                                          GUID_string(msg, &nc->guid),
161                                          nc->dn,
162                                          dsa->repsFrom1->other_info->dns_name,
163                                          win_errstr(werr)));
164                                 REPLICA_SYNC_FAIL(werr);
165                         }
166                         /* log we've scheduled replication item */
167                         DEBUG(3,("%s: forcing sync of partition (%s, %s, %s)\n",
168                                  __FUNCTION__,
169                                  GUID_string(msg, &nc->guid),
170                                  nc->dn,
171                                  dsa->repsFrom1->other_info->dns_name));
172                 }
173         } else {
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);
178                         }
179
180                         werr = dreplsrv_partition_source_dsa_by_dns(p,
181                                                                     req1->source_dsa_dns,
182                                                                     &dsa);
183                 } else {
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);
187                         }
188
189                         werr = dreplsrv_partition_source_dsa_by_guid(p,
190                                                                      &req1->source_dsa_guid,
191                                                                      &dsa);
192                 }
193                 if (!W_ERROR_IS_OK(werr)) {
194                         DEBUG(0,("%s: Failed to locate source DSA %s for NC %s.\n",
195                                  __FUNCTION__,
196                                  (req1->options & DRSUAPI_DRS_SYNC_BYNAME)
197                                          ? req1->source_dsa_dns
198                                          : GUID_string(r, &req1->source_dsa_guid),
199                                  nc->dn));
200                         REPLICA_SYNC_FAIL(WERR_DS_DRA_NO_REPLICA);
201                 }
202
203                 /* schedule replication item */
204                 werr = dreplsrv_schedule_partition_pull_source(service, dsa,
205                                                                DRSUAPI_EXOP_NONE, 0,
206                                                                NULL, NULL);
207                 if (!W_ERROR_IS_OK(werr)) {
208                         DEBUG(0,("%s: failed setup of sync of partition (%s, %s, %s) - %s\n",
209                                  __FUNCTION__,
210                                  GUID_string(msg, &nc->guid),
211                                  nc->dn,
212                                  dsa->repsFrom1->other_info->dns_name,
213                                  win_errstr(werr)));
214                         REPLICA_SYNC_FAIL(werr);
215                 }
216                 /* log we've scheduled replication item */
217                 DEBUG(3,("%s: forcing sync of partition (%s, %s, %s)\n",
218                          __FUNCTION__,
219                          GUID_string(msg, &nc->guid),
220                          nc->dn,
221                          dsa->repsFrom1->other_info->dns_name));
222         }
223
224         /* if we got here, everything is OK */
225         r->out.result = WERR_OK;
226
227         /* force execution of scheduled replications */
228         dreplsrv_run_pending_ops(service);
229
230 done:
231         return NT_STATUS_OK;
232 }
233
234 /**
235  * Called when drplsrv should refresh its state.
236  * For example, when KCC change topology, dreplsrv
237  * should update its cache
238  *
239  * @param partition_dn If not empty/NULL, partition to update
240  */
241 static NTSTATUS dreplsrv_refresh(struct irpc_message *msg,
242                                  struct dreplsrv_refresh *r)
243 {
244         struct dreplsrv_service *s = talloc_get_type(msg->private_data,
245                                                      struct dreplsrv_service);
246
247         r->out.result = dreplsrv_refresh_partitions(s);
248
249         return NT_STATUS_OK;
250 }
251
252 /*
253   startup the dsdb replicator service task
254 */
255 static void dreplsrv_task_init(struct task_server *task)
256 {
257         WERROR status;
258         struct dreplsrv_service *service;
259         uint32_t periodic_startup_interval;
260         bool am_rodc;
261         int ret;
262
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", 
266                                       false);
267                 return;
268         case ROLE_DOMAIN_MEMBER:
269                 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration", 
270                                       false);
271                 return;
272         case ROLE_DOMAIN_CONTROLLER:
273                 /* Yes, we want DSDB replication */
274                 break;
275         }
276
277         task_server_set_title(task, "task[dreplsrv]");
278
279         service = talloc_zero(task, struct dreplsrv_service);
280         if (!service) {
281                 task_server_terminate(task, "dreplsrv_task_init: out of memory", true);
282                 return;
283         }
284         service->task           = task;
285         service->startup_time   = timeval_current();
286         task->private_data      = service;
287
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);
293                 return;
294         }
295
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);
301                 return;
302         }
303
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);
309                 return;
310         }
311
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 */
314
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);
320                 return;
321         }
322
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);
333                         return;
334                 }
335         }
336
337         irpc_add_name(task->msg_ctx, "dreplsrv");
338
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);
342 }
343
344 /*
345   register ourselves as a available server
346 */
347 NTSTATUS server_service_drepl_init(void)
348 {
349         return register_server_service("drepl", dreplsrv_task_init);
350 }