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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "smbd/service.h"
27 #include "lib/events/events.h"
28 #include "lib/messaging/irpc.h"
29 #include "dsdb/repl/drepl_service.h"
30 #include "lib/ldb/include/ldb_errors.h"
31 #include "lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 #include "librpc/gen_ndr/ndr_drsblobs.h"
36 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
40 status = auth_system_session_info(service, &service->system_session_info);
41 if (!NT_STATUS_IS_OK(status)) {
42 return ntstatus_to_werror(status);
48 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service)
50 service->samdb = samdb_connect(service, service->system_session_info);
51 if (!service->samdb) {
52 return WERR_DS_SERVICE_UNAVAILABLE;
58 static void dreplsrv_periodic_handler_te(struct event_context *ev, struct timed_event *te,
59 struct timeval t, void *ptr)
61 struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
64 service->periodic.te = NULL;
66 status = dreplsrv_periodic_schedule(service, service->periodic.interval);
67 if (!W_ERROR_IS_OK(status)) {
68 task_server_terminate(service->task, win_errstr(status));
73 WERROR dreplsrv_periodic_schedule(struct dreplsrv_service *service, uint32_t next_interval)
76 struct timed_event *new_te;
77 struct timeval next_time;
80 if (next_interval == 0) next_interval = 1;
82 next_time = timeval_current_ofs(next_interval, 5000);
84 if (service->periodic.te) {
86 * if the timestamp of the new event is higher,
87 * as current next we don't need to reschedule
89 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
94 /* reset the next scheduled timestamp */
95 service->periodic.next_event = next_time;
97 new_te = event_add_timed(service->task->event_ctx, service,
98 service->periodic.next_event,
99 dreplsrv_periodic_handler_te, service);
100 W_ERROR_HAVE_NO_MEMORY(new_te);
102 tmp_mem = talloc_new(service);
103 DEBUG(6,("dreplsrv_periodic_schedule(%u) %sscheduled for: %s\n",
105 (service->periodic.te?"re":""),
106 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
107 talloc_free(tmp_mem);
109 talloc_free(service->periodic.te);
110 service->periodic.te = new_te;
115 startup the dsdb replicator service task
117 static void dreplsrv_task_init(struct task_server *task)
120 struct dreplsrv_service *service;
122 switch (lp_server_role()) {
123 case ROLE_STANDALONE:
124 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration");
126 case ROLE_DOMAIN_MEMBER:
127 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration");
129 case ROLE_DOMAIN_CONTROLLER:
130 /* Yes, we want DSDB replication */
134 task_server_set_title(task, "task[dreplsrv]");
136 service = talloc_zero(task, struct dreplsrv_service);
138 task_server_terminate(task, "dreplsrv_task_init: out of memory");
141 service->task = task;
142 service->startup_time = timeval_current();
143 task->private = service;
145 status = dreplsrv_init_creds(service);
146 if (!W_ERROR_IS_OK(status)) {
147 task_server_terminate(task, talloc_asprintf(task,
148 "dreplsrv: Failed to obtain server credentials: %s\n",
149 win_errstr(status)));
153 status = dreplsrv_connect_samdb(service);
154 if (!W_ERROR_IS_OK(status)) {
155 task_server_terminate(task, talloc_asprintf(task,
156 "dreplsrv: Failed to connect to local samdb: %s\n",
157 win_errstr(status)));
161 service->periodic.interval = 30; /* in seconds */
163 status = dreplsrv_periodic_schedule(service, service->periodic.interval);
164 if (!W_ERROR_IS_OK(status)) {
165 task_server_terminate(task, talloc_asprintf(task,
166 "dreplsrv: Failed to periodic schedule: %s\n",
167 win_errstr(status)));
171 irpc_add_name(task->msg_ctx, "dreplsrv");
175 initialise the dsdb replicator service
177 static NTSTATUS dreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
179 return task_server_startup(event_ctx, model_ops, dreplsrv_task_init);
183 register ourselves as a available server
185 NTSTATUS server_service_drepl_init(void)
187 return register_server_service("drepl", dreplsrv_init);