s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[garming/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     
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 "param/param.h"
35
36 static WERROR dreplsrv_init_creds(struct dreplsrv_service *service)
37 {
38         service->system_session_info = system_session(service->task->lp_ctx);
39         if (service->system_session_info == NULL) {
40                 return WERR_NOMEM;
41         }
42
43         return WERR_OK;
44 }
45
46 static WERROR dreplsrv_connect_samdb(struct dreplsrv_service *service, struct loadparm_context *lp_ctx)
47 {
48         const struct GUID *ntds_guid;
49         struct drsuapi_DsBindInfo28 *bind_info28;
50
51         service->samdb = samdb_connect(service, service->task->event_ctx, lp_ctx, service->system_session_info);
52         if (!service->samdb) {
53                 return WERR_DS_UNAVAILABLE;
54         }
55
56         ntds_guid = samdb_ntds_objectGUID(service->samdb);
57         if (!ntds_guid) {
58                 return WERR_DS_UNAVAILABLE;
59         }
60
61         service->ntds_guid = *ntds_guid;
62
63         bind_info28                             = &service->bind_info28;
64         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
65         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
66         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
67         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
68         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
69         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
70         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
71         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
72         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
73         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
74         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
75         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
76         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
77         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
78         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
79         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
80         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
81         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
82         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
83         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
84         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
85         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
86         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
87         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
88         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
89         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
90         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
91         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
92         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
93 #if 0 /* we don't support XPRESS compression yet */
94         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
95 #endif
96         /* TODO: fill in site_guid */
97         bind_info28->site_guid                  = GUID_zero();
98         /* TODO: find out how this is really triggered! */
99         bind_info28->pid                        = 0;
100         bind_info28->repl_epoch                 = 0;
101
102         return WERR_OK;
103 }
104
105 /*
106   DsReplicaSync messages from the DRSUAPI server are forwarded here
107  */
108 static NTSTATUS drepl_replica_sync(struct irpc_message *msg, 
109                                    struct drsuapi_DsReplicaSync *r)
110 {
111         struct dreplsrv_service *service = talloc_get_type(msg->private_data,
112                                                            struct dreplsrv_service);
113         struct drsuapi_DsReplicaObjectIdentifier *nc = r->in.req->req1.naming_context;
114
115         r->out.result = dreplsrv_schedule_partition_pull_by_nc(service, msg, nc);
116         if (W_ERROR_IS_OK(r->out.result)) {
117                 DEBUG(3,("drepl_replica_sync: forcing sync of partition (%s, %s)\n",
118                          GUID_string(msg, &nc->guid),
119                          nc->dn));
120                 dreplsrv_run_pending_ops(service);
121         } else {
122                 DEBUG(3,("drepl_replica_sync: failed setup of sync of partition (%s, %s) - %s\n",
123                          GUID_string(msg, &nc->guid),
124                          nc->dn,
125                          win_errstr(r->out.result)));
126         }
127         return NT_STATUS_OK;
128 }
129
130 /*
131   startup the dsdb replicator service task
132 */
133 static void dreplsrv_task_init(struct task_server *task)
134 {
135         WERROR status;
136         struct dreplsrv_service *service;
137         uint32_t periodic_startup_interval;
138         bool am_rodc;
139         int ret;
140
141         switch (lpcfg_server_role(task->lp_ctx)) {
142         case ROLE_STANDALONE:
143                 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration", 
144                                       false);
145                 return;
146         case ROLE_DOMAIN_MEMBER:
147                 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration", 
148                                       false);
149                 return;
150         case ROLE_DOMAIN_CONTROLLER:
151                 /* Yes, we want DSDB replication */
152                 break;
153         }
154
155         task_server_set_title(task, "task[dreplsrv]");
156
157         service = talloc_zero(task, struct dreplsrv_service);
158         if (!service) {
159                 task_server_terminate(task, "dreplsrv_task_init: out of memory", true);
160                 return;
161         }
162         service->task           = task;
163         service->startup_time   = timeval_current();
164         task->private_data      = service;
165
166         status = dreplsrv_init_creds(service);
167         if (!W_ERROR_IS_OK(status)) {
168                 task_server_terminate(task, talloc_asprintf(task,
169                                       "dreplsrv: Failed to obtain server credentials: %s\n",
170                                                             win_errstr(status)), true);
171                 return;
172         }
173
174         status = dreplsrv_connect_samdb(service, task->lp_ctx);
175         if (!W_ERROR_IS_OK(status)) {
176                 task_server_terminate(task, talloc_asprintf(task,
177                                       "dreplsrv: Failed to connect to local samdb: %s\n",
178                                                             win_errstr(status)), true);
179                 return;
180         }
181
182         status = dreplsrv_load_partitions(service);
183         if (!W_ERROR_IS_OK(status)) {
184                 task_server_terminate(task, talloc_asprintf(task,
185                                       "dreplsrv: Failed to load partitions: %s\n",
186                                                             win_errstr(status)), true);
187                 return;
188         }
189
190         periodic_startup_interval       = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_startup_interval", 15); /* in seconds */
191         service->periodic.interval      = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv", "periodic_interval", 300); /* in seconds */
192
193         status = dreplsrv_periodic_schedule(service, periodic_startup_interval);
194         if (!W_ERROR_IS_OK(status)) {
195                 task_server_terminate(task, talloc_asprintf(task,
196                                       "dreplsrv: Failed to periodic schedule: %s\n",
197                                                             win_errstr(status)), true);
198                 return;
199         }
200
201         /* if we are a RODC then we do not send DSReplicaSync*/
202         ret = samdb_rodc(service->samdb, &am_rodc);
203         if (ret == LDB_SUCCESS && !am_rodc) {
204                 service->notify.interval = lpcfg_parm_int(task->lp_ctx, NULL, "dreplsrv",
205                                                            "notify_interval", 5); /* in seconds */
206                 status = dreplsrv_notify_schedule(service, service->notify.interval);
207                 if (!W_ERROR_IS_OK(status)) {
208                         task_server_terminate(task, talloc_asprintf(task,
209                                                   "dreplsrv: Failed to setup notify schedule: %s\n",
210                                                                         win_errstr(status)), true);
211                         return;
212                 }
213         }
214
215         irpc_add_name(task->msg_ctx, "dreplsrv");
216
217         IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICASYNC, drepl_replica_sync, service);
218         messaging_register(task->msg_ctx, service, MSG_DREPL_ALLOCATE_RID, dreplsrv_allocate_rid);
219 }
220
221 /*
222   register ourselves as a available server
223 */
224 NTSTATUS server_service_drepl_init(void)
225 {
226         return register_server_service("drepl", dreplsrv_task_init);
227 }