r22472: Commit the start of the DRSUAPI pull replication service.
[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     
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         const struct GUID *ntds_guid;
51         struct drsuapi_DsBindInfo28 *bind_info28;
52
53         service->samdb = samdb_connect(service, service->system_session_info);
54         if (!service->samdb) {
55                 return WERR_DS_SERVICE_UNAVAILABLE;
56         }
57
58         ntds_guid = samdb_ntds_objectGUID(service->samdb);
59         if (!ntds_guid) {
60                 return WERR_DS_SERVICE_UNAVAILABLE;
61         }
62
63         service->ntds_guid = *ntds_guid;
64
65         bind_info28                             = &service->bind_info28;
66         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
67         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
68         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
69         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
70         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
71         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
72         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
73         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
74         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
75 #if 0
76         if (s->domain_behavior_version == 2) {
77                 /* TODO: find out how this is really triggered! */
78                 bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
79         }
80 #endif
81         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
82         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
83         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
84         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
85         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
86         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
87         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
88         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
89         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
90         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_00100000;
91         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
92         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
93         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
94         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
95         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
96         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
97         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
98         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
99         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
100 #if 0 /* we don't support XPRESS compression yet */
101         bind_info28->supported_extensions       |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
102 #endif
103         /* TODO: fill in site_guid */
104         bind_info28->site_guid                  = GUID_zero();
105         /* TODO: find out how this is really triggered! */
106         bind_info28->u1                         = 0;
107         bind_info28->repl_epoch                 = 0;
108
109         return WERR_OK;
110 }
111
112 /*
113   startup the dsdb replicator service task
114 */
115 static void dreplsrv_task_init(struct task_server *task)
116 {
117         WERROR status;
118         struct dreplsrv_service *service;
119
120         switch (lp_server_role()) {
121         case ROLE_STANDALONE:
122                 task_server_terminate(task, "dreplsrv: no DSDB replication required in standalone configuration");
123                 return;
124         case ROLE_DOMAIN_MEMBER:
125                 task_server_terminate(task, "dreplsrv: no DSDB replication required in domain member configuration");
126                 return;
127         case ROLE_DOMAIN_CONTROLLER:
128                 /* Yes, we want DSDB replication */
129                 break;
130         }
131
132         task_server_set_title(task, "task[dreplsrv]");
133
134         service = talloc_zero(task, struct dreplsrv_service);
135         if (!service) {
136                 task_server_terminate(task, "dreplsrv_task_init: out of memory");
137                 return;
138         }
139         service->task           = task;
140         service->startup_time   = timeval_current();
141         task->private           = service;
142
143         status = dreplsrv_init_creds(service);
144         if (!W_ERROR_IS_OK(status)) {
145                 task_server_terminate(task, talloc_asprintf(task,
146                                       "dreplsrv: Failed to obtain server credentials: %s\n",
147                                       win_errstr(status)));
148                 return;
149         }
150
151         status = dreplsrv_connect_samdb(service);
152         if (!W_ERROR_IS_OK(status)) {
153                 task_server_terminate(task, talloc_asprintf(task,
154                                       "dreplsrv: Failed to connect to local samdb: %s\n",
155                                       win_errstr(status)));
156                 return;
157         }
158
159         status = dreplsrv_load_partitions(service);
160         if (!W_ERROR_IS_OK(status)) {
161                 task_server_terminate(task, talloc_asprintf(task,
162                                       "dreplsrv: Failed to load partitions: %s\n",
163                                       win_errstr(status)));
164                 return;
165         }
166
167         service->periodic.interval      = lp_parm_int(-1, "dreplsrv", "periodic_interval", 300); /* in seconds */
168
169         status = dreplsrv_periodic_schedule(service, service->periodic.interval);
170         if (!W_ERROR_IS_OK(status)) {
171                 task_server_terminate(task, talloc_asprintf(task,
172                                       "dreplsrv: Failed to periodic schedule: %s\n",
173                                       win_errstr(status)));
174                 return;
175         }
176
177         irpc_add_name(task->msg_ctx, "dreplsrv");
178 }
179
180 /*
181   initialise the dsdb replicator service
182  */
183 static NTSTATUS dreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
184 {
185         return task_server_startup(event_ctx, model_ops, dreplsrv_task_init);
186 }
187
188 /*
189   register ourselves as a available server
190 */
191 NTSTATUS server_service_drepl_init(void)
192 {
193         return register_server_service("drepl", dreplsrv_init);
194 }