r20974: add basic infrastructure for a DSDB replication service
[ira/wip.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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 "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"
35
36 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
37 {
38         NTSTATUS status;
39
40         status = auth_system_session_info(service, &service->system_session_info);
41         if (!NT_STATUS_IS_OK(status)) {
42                 return ntstatus_to_werror(status);
43         }
44
45         return WERR_OK;
46 }
47
48 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service)
49 {
50         service->samdb = samdb_connect(service, service->system_session_info);
51         if (!service->samdb) {
52                 return WERR_DS_SERVICE_UNAVAILABLE;
53         }
54
55         return WERR_OK;
56 }
57
58 static void dreplsrv_periodic_handler_te(struct event_context *ev, struct timed_event *te,
59                                          struct timeval t, void *ptr)
60 {
61         struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
62         WERROR status;
63
64         service->periodic.te = NULL;
65
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));
69                 return;
70         }
71 }
72
73 WERROR dreplsrv_periodic_schedule(struct dreplsrv_service *service, uint32_t next_interval)
74 {
75         TALLOC_CTX *tmp_mem;
76         struct timed_event *new_te;
77         struct timeval next_time;
78
79         /* prevent looping */
80         if (next_interval == 0) next_interval = 1;
81
82         next_time = timeval_current_ofs(next_interval, 5000);
83
84         if (service->periodic.te) {
85                 /*
86                  * if the timestamp of the new event is higher,
87                  * as current next we don't need to reschedule
88                  */
89                 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
90                         return WERR_OK;
91                 }
92         }
93
94         /* reset the next scheduled timestamp */
95         service->periodic.next_event = next_time;
96
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);
101
102         tmp_mem = talloc_new(service);
103         DEBUG(4,("dreplsrv_periodic_schedule(%u) %sscheduled for: %s\n",
104                 next_interval,
105                 (service->periodic.te?"re":""),
106                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
107         talloc_free(tmp_mem);
108
109         talloc_free(service->periodic.te);
110         service->periodic.te = new_te;
111
112         return WERR_OK;
113 }
114 /*
115   startup the dsdb replicator service task
116 */
117 static void dreplsrv_task_init(struct task_server *task)
118 {
119         WERROR status;
120         struct dreplsrv_service *service;
121
122         switch (lp_server_role()) {
123         case ROLE_STANDALONE:
124                 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration");
125                 return;
126         case ROLE_DOMAIN_MEMBER:
127                 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration");
128                 return;
129         case ROLE_DOMAIN_CONTROLLER:
130                 /* Yes, we want DSDB replication */
131                 break;
132         }
133
134         task_server_set_title(task, "task[dreplsrv]");
135
136         service = talloc_zero(task, struct dreplsrv_service);
137         if (!service) {
138                 task_server_terminate(task, "dreplsrv_task_init: out of memory");
139                 return;
140         }
141         service->task           = task;
142         service->startup_time   = timeval_current();
143         task->private           = service;
144
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)));
150                 return;
151         }
152
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)));
158                 return;
159         }
160
161         service->periodic.interval      = 30; /* in seconds */
162
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)));
168                 return;
169         }
170
171         irpc_add_name(task->msg_ctx, "dreplsrv");
172 }
173
174 /*
175   initialise the dsdb replicator service
176  */
177 static NTSTATUS dreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
178 {
179         return task_server_startup(event_ctx, model_ops, dreplsrv_task_init);
180 }
181
182 /*
183   register ourselves as a available server
184 */
185 NTSTATUS server_service_drepl_init(void)
186 {
187         return register_server_service("drepl", dreplsrv_init);
188 }