Correct "extention" typos.
[nivanova/samba-autobuild/.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 "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"
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_NOT_ENOUGH_MEMORY;
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, 0);
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         if (samdb_rodc(service->samdb, &service->am_rodc) != LDB_SUCCESS) {
80                 DEBUG(0,(__location__ ": Failed to determine RODC status\n"));
81                 return WERR_DS_UNAVAILABLE;
82         }
83
84         bind_info28                             = &service->bind_info28;
85         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
86         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
87         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
88         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
89         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
90         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
91         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
92         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
93         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
94         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
95         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
96         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
97         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
98         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
99         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
100         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
101         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
102         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
103         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
104         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
105         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
106         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
107         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
108         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
109         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
110         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
111         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
112         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
113         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
114 #if 0 /* we don't support XPRESS compression yet */
115         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
116 #endif
117         /* TODO: fill in site_guid */
118         bind_info28->site_guid                  = GUID_zero();
119         /* TODO: find out how this is really triggered! */
120         bind_info28->pid                        = 0;
121         bind_info28->repl_epoch                 = 0;
122
123         return WERR_OK;
124 }
125
126
127 /**
128  * Callback for dreplsrv_out_operation operation completion.
129  *
130  * We just need to complete a waiting IRPC message here.
131  * In case pull operation has failed,
132  * caller of this callback will dump
133  * failure information.
134  *
135  * NOTE: cb_data is allocated in IRPC msg's context
136  * and will be freed during irpc_send_reply() call.
137  */
138 static void _drepl_replica_sync_done_cb(struct dreplsrv_service *service,
139                                         WERROR werr,
140                                         enum drsuapi_DsExtendedError ext_err,
141                                         void *cb_data)
142 {
143         struct drepl_replica_sync_cb_data *data = talloc_get_type(cb_data,
144                                                                   struct drepl_replica_sync_cb_data);
145         struct irpc_message *msg = data->msg;
146         struct drsuapi_DsReplicaSync *r = data->r;
147
148         /* store last bad result */
149         if (!W_ERROR_IS_OK(werr)) {
150                 data->werr_last_failure = werr;
151         }
152
153         /* decrement pending ops count */
154         data->ops_count--;
155
156         if (data->ops_count == 0) {
157                 /* Return result to client */
158                 r->out.result = data->werr_last_failure;
159
160                 /* complete IRPC message */
161                 irpc_send_reply(msg, NT_STATUS_OK);
162         }
163 }
164
165 /**
166  * Helper to schedule a replication operation with a source DSA.
167  * If 'data' is valid pointer, then a callback
168  * for the operation is passed and 'data->msg' is
169  * marked as 'deferred' - defer_reply = true
170  */
171 static WERROR _drepl_schedule_replication(struct dreplsrv_service *service,
172                                           struct dreplsrv_partition_source_dsa *dsa,
173                                           struct drsuapi_DsReplicaObjectIdentifier *nc,
174                                           uint32_t rep_options,
175                                           struct drepl_replica_sync_cb_data *data,
176                                           TALLOC_CTX *mem_ctx)
177 {
178         WERROR werr;
179         dreplsrv_extended_callback_t fn_callback = NULL;
180
181         if (data) {
182                 fn_callback = _drepl_replica_sync_done_cb;
183         }
184
185         /* schedule replication item */
186         werr = dreplsrv_schedule_partition_pull_source(service, dsa, rep_options,
187                                                        DRSUAPI_EXOP_NONE, 0,
188                                                        fn_callback, data);
189         if (!W_ERROR_IS_OK(werr)) {
190                 DEBUG(0,("%s: failed setup of sync of partition (%s, %s, %s) - %s\n",
191                          __FUNCTION__,
192                          GUID_string(mem_ctx, &nc->guid),
193                          nc->dn,
194                          dsa->repsFrom1->other_info->dns_name,
195                          win_errstr(werr)));
196                 return werr;
197         }
198         /* log we've scheduled a replication item */
199         DEBUG(3,("%s: forcing sync of partition (%s, %s, %s)\n",
200                  __FUNCTION__,
201                  GUID_string(mem_ctx, &nc->guid),
202                  nc->dn,
203                  dsa->repsFrom1->other_info->dns_name));
204
205         /* mark IRPC message as deferred if necessary */
206         if (data) {
207                 data->ops_count++;
208                 data->msg->defer_reply = true;
209         }
210
211         return WERR_OK;
212 }
213
214 /*
215   DsReplicaSync messages from the DRSUAPI server are forwarded here
216  */
217 static NTSTATUS drepl_replica_sync(struct irpc_message *msg,
218                                    struct drsuapi_DsReplicaSync *r)
219 {
220         WERROR werr;
221         struct dreplsrv_partition *p;
222         struct drepl_replica_sync_cb_data *cb_data;
223         struct dreplsrv_partition_source_dsa *dsa;
224         struct drsuapi_DsReplicaSyncRequest1 *req1;
225         struct drsuapi_DsReplicaObjectIdentifier *nc;
226         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
227                                                            struct dreplsrv_service);
228
229 #define REPLICA_SYNC_FAIL(_msg, _werr) do {\
230                 if (!W_ERROR_IS_OK(_werr)) { \
231                         DEBUG(0,(__location__ ": Failure - %s. werr = %s\n", \
232                                  _msg, win_errstr(_werr))); \
233                         NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync, r); \
234                 } \
235                 r->out.result = _werr; \
236                 goto done;\
237         } while(0)
238
239
240         if (r->in.level != 1) {
241                 REPLICA_SYNC_FAIL("Unsupported level",
242                                   WERR_DS_DRA_INVALID_PARAMETER);
243         }
244
245         req1 = &r->in.req->req1;
246         nc   = req1->naming_context;
247
248         /* Check input parameters */
249         if (!nc) {
250                 REPLICA_SYNC_FAIL("Invalid Naming Context",
251                                   WERR_DS_DRA_INVALID_PARAMETER);
252         }
253
254         /* Find Naming context to be synchronized */
255         werr = dreplsrv_partition_find_for_nc(service,
256                                               &nc->guid, &nc->sid, nc->dn,
257                                               &p);
258         if (!W_ERROR_IS_OK(werr)) {
259                 REPLICA_SYNC_FAIL("Failed to find requested Naming Context",
260                                   werr);
261         }
262
263         /* should we process it asynchronously? */
264         if (req1->options & DRSUAPI_DRS_ASYNC_OP) {
265                 cb_data = NULL;
266         } else {
267                 cb_data = talloc_zero(msg, struct drepl_replica_sync_cb_data);
268                 if (!cb_data) {
269                         REPLICA_SYNC_FAIL("Not enought memory",
270                                           WERR_DS_DRA_INTERNAL_ERROR);
271                 }
272
273                 cb_data->msg = msg;
274                 cb_data->r   = r;
275                 cb_data->werr_last_failure = WERR_OK;
276         }
277
278         /* collect source DSAs to sync with */
279         if (req1->options & DRSUAPI_DRS_SYNC_ALL) {
280                 for (dsa = p->sources; dsa; dsa = dsa->next) {
281                         /* schedule replication item */
282                         werr = _drepl_schedule_replication(service, dsa, nc,
283                                                            req1->options, cb_data, msg);
284                         if (!W_ERROR_IS_OK(werr)) {
285                                 REPLICA_SYNC_FAIL("_drepl_schedule_replication() failed",
286                                                   werr);
287                         }
288                 }
289         } else {
290                 if (req1->options & DRSUAPI_DRS_SYNC_BYNAME) {
291                         /* client should pass at least valid string */
292                         if (!req1->source_dsa_dns) {
293                                 REPLICA_SYNC_FAIL("'source_dsa_dns' is not valid",
294                                                   WERR_DS_DRA_INVALID_PARAMETER);
295                         }
296
297                         werr = dreplsrv_partition_source_dsa_by_dns(p,
298                                                                     req1->source_dsa_dns,
299                                                                     &dsa);
300                 } else {
301                         /* client should pass at least some GUID */
302                         if (GUID_all_zero(&req1->source_dsa_guid)) {
303                                 REPLICA_SYNC_FAIL("'source_dsa_guid' is not valid",
304                                                   WERR_DS_DRA_INVALID_PARAMETER);
305                         }
306
307                         werr = dreplsrv_partition_source_dsa_by_guid(p,
308                                                                      &req1->source_dsa_guid,
309                                                                      &dsa);
310                         if (W_ERROR_EQUAL(werr, WERR_DS_DRA_NO_REPLICA)) {
311                                 /* we don't have this source setup as
312                                    a replication partner. Create a
313                                    temporary dsa structure for this
314                                    replication */
315                                 werr = dreplsrv_partition_source_dsa_temporary(p,
316                                                                                msg,
317                                                                                &req1->source_dsa_guid,
318                                                                                &dsa);
319                         }
320                 }
321                 if (!W_ERROR_IS_OK(werr)) {
322                         REPLICA_SYNC_FAIL("Failed to locate source DSA for given NC",
323                                           werr);
324                 }
325
326                 /* schedule replication item */
327                 werr = _drepl_schedule_replication(service, dsa, nc,
328                                                    req1->options, cb_data, msg);
329                 if (!W_ERROR_IS_OK(werr)) {
330                         REPLICA_SYNC_FAIL("_drepl_schedule_replication() failed",
331                                           werr);
332                 }
333         }
334
335         /* if we got here, everything is OK */
336         r->out.result = WERR_OK;
337
338         /*
339          * schedule replication event to force
340          * replication as soon as possible
341          */
342         dreplsrv_pendingops_schedule(service, 0);
343
344 done:
345         return NT_STATUS_OK;
346 }
347
348 /**
349  * Called when drplsrv should refresh its state.
350  * For example, when KCC change topology, dreplsrv
351  * should update its cache
352  *
353  * @param partition_dn If not empty/NULL, partition to update
354  */
355 static NTSTATUS dreplsrv_refresh(struct irpc_message *msg,
356                                  struct dreplsrv_refresh *r)
357 {
358         struct dreplsrv_service *s = talloc_get_type(msg->private_data,
359                                                      struct dreplsrv_service);
360
361         r->out.result = dreplsrv_refresh_partitions(s);
362
363         return NT_STATUS_OK;
364 }
365
366 /**
367  * Called when the auth code wants us to try and replicate
368  * a users secrets
369  */
370 static NTSTATUS drepl_trigger_repl_secret(struct irpc_message *msg,
371                                           struct drepl_trigger_repl_secret *r)
372 {
373         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
374                                                            struct dreplsrv_service);
375
376
377         drepl_repl_secret(service, r->in.user_dn);
378
379         /* we are not going to be sending a reply to this request */
380         msg->no_reply = true;
381
382         return NT_STATUS_OK;
383 }
384
385
386 /*
387   DsReplicaAdd messages from the DRSUAPI server are forwarded here
388  */
389 static NTSTATUS dreplsrv_replica_add(struct irpc_message *msg,
390                                   struct drsuapi_DsReplicaAdd *r)
391 {
392         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
393                                                            struct dreplsrv_service);
394         return drepl_replica_add(service, r);
395 }
396
397 /*
398   DsReplicaDel messages from the DRSUAPI server are forwarded here
399  */
400 static NTSTATUS dreplsrv_replica_del(struct irpc_message *msg,
401                                   struct drsuapi_DsReplicaDel *r)
402 {
403         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
404                                                            struct dreplsrv_service);
405         return drepl_replica_del(service, r);
406 }
407
408 /*
409   DsReplicaMod messages from the DRSUAPI server are forwarded here
410  */
411 static NTSTATUS dreplsrv_replica_mod(struct irpc_message *msg,
412                                   struct drsuapi_DsReplicaMod *r)
413 {
414         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
415                                                            struct dreplsrv_service);
416         return drepl_replica_mod(service, r);
417 }
418
419
420 /*
421   startup the dsdb replicator service task
422 */
423 static void dreplsrv_task_init(struct task_server *task)
424 {
425         WERROR status;
426         struct dreplsrv_service *service;
427         uint32_t periodic_startup_interval;
428
429         switch (lpcfg_server_role(task->lp_ctx)) {
430         case ROLE_STANDALONE:
431                 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration",
432                                       false);
433                 return;
434         case ROLE_DOMAIN_MEMBER:
435                 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration",
436                                       false);
437                 return;
438         case ROLE_ACTIVE_DIRECTORY_DC:
439                 /* Yes, we want DSDB replication */
440                 break;
441         }
442
443         task_server_set_title(task, "task[dreplsrv]");
444
445         service = talloc_zero(task, struct dreplsrv_service);
446         if (!service) {
447                 task_server_terminate(task, "dreplsrv_task_init: out of memory", true);
448                 return;
449         }
450         service->task           = task;
451         service->startup_time   = timeval_current();
452         task->private_data      = service;
453
454         status = dreplsrv_init_creds(service);
455         if (!W_ERROR_IS_OK(status)) {
456                 task_server_terminate(task, talloc_asprintf(task,
457                                       "dreplsrv: Failed to obtain server credentials: %s\n",
458                                                             win_errstr(status)), true);
459                 return;
460         }
461
462         status = dreplsrv_connect_samdb(service, task->lp_ctx);
463         if (!W_ERROR_IS_OK(status)) {
464                 task_server_terminate(task, talloc_asprintf(task,
465                                       "dreplsrv: Failed to connect to local samdb: %s\n",
466                                                             win_errstr(status)), true);
467                 return;
468         }
469
470         status = dreplsrv_load_partitions(service);
471         if (!W_ERROR_IS_OK(status)) {
472                 task_server_terminate(task, talloc_asprintf(task,
473                                       "dreplsrv: Failed to load partitions: %s\n",
474                                                             win_errstr(status)), true);
475                 return;
476         }
477
478         periodic_startup_interval       = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_startup_interval", 15); /* in seconds */
479         service->periodic.interval      = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_interval", 300); /* in seconds */
480
481         status = dreplsrv_periodic_schedule(service, periodic_startup_interval);
482         if (!W_ERROR_IS_OK(status)) {
483                 task_server_terminate(task, talloc_asprintf(task,
484                                       "dreplsrv: Failed to periodic schedule: %s\n",
485                                                             win_errstr(status)), true);
486                 return;
487         }
488
489         /* if we are a RODC then we do not send DSReplicaSync*/
490         if (!service->am_rodc) {
491                 service->notify.interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv",
492                                                            "notify_interval", 5); /* in seconds */
493                 status = dreplsrv_notify_schedule(service, service->notify.interval);
494                 if (!W_ERROR_IS_OK(status)) {
495                         task_server_terminate(task, talloc_asprintf(task,
496                                                   "dreplsrv: Failed to setup notify schedule: %s\n",
497                                                                         win_errstr(status)), true);
498                         return;
499                 }
500         }
501
502         irpc_add_name(task->msg_ctx, "dreplsrv");
503
504         IRPC_REGISTER(task->msg_ctx, irpc, DREPLSRV_REFRESH, dreplsrv_refresh, service);
505         IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICASYNC, drepl_replica_sync, service);
506         IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICAADD, dreplsrv_replica_add, service);
507         IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICADEL, dreplsrv_replica_del, service);
508         IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICAMOD, dreplsrv_replica_mod, service);
509         IRPC_REGISTER(task->msg_ctx, irpc, DREPL_TAKEFSMOROLE, drepl_take_FSMO_role, service);
510         IRPC_REGISTER(task->msg_ctx, irpc, DREPL_TRIGGER_REPL_SECRET, drepl_trigger_repl_secret, service);
511         imessaging_register(task->msg_ctx, service, MSG_DREPL_ALLOCATE_RID, dreplsrv_allocate_rid);
512 }
513
514 /*
515   register ourselves as a available server
516 */
517 NTSTATUS server_service_drepl_init(void)
518 {
519         return register_server_service("drepl", dreplsrv_task_init);
520 }