s4/fsmo: Handle infrastructure, pdc and rid extended ops
[metze/samba/wip.git] / source4 / dsdb / repl / drepl_ridalloc.c
1 /*
2    Unix SMB/CIFS mplementation.
3
4    DSDB replication service - RID allocation code
5
6    Copyright (C) Andrew Tridgell 2010
7    Copyright (C) Andrew Bartlett 2010
8
9    based on drepl_notify.c
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 */
25
26 #include "includes.h"
27 #include "ldb_module.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "smbd/service.h"
30 #include "dsdb/repl/drepl_service.h"
31 #include "param/param.h"
32
33
34 /*
35   create the role owner source dsa structure
36  */
37
38 WERROR drepl_create_role_owner_source_dsa(struct dreplsrv_service *service,
39                                           struct ldb_dn *fsmo_role_dn,
40                                           struct ldb_dn *role_owner_dn)
41 {
42         struct dreplsrv_partition_source_dsa *sdsa;
43         struct ldb_context *ldb = service->samdb;
44         int ret;
45         WERROR werr;
46
47         sdsa = talloc_zero(service, struct dreplsrv_partition_source_dsa);
48         W_ERROR_HAVE_NO_MEMORY(sdsa);
49
50         sdsa->partition = talloc_zero(sdsa, struct dreplsrv_partition);
51         if (!sdsa->partition) {
52                 talloc_free(sdsa);
53                 return WERR_NOMEM;
54         }
55
56         sdsa->partition->dn = ldb_dn_copy(sdsa->partition, fsmo_role_dn);
57         if (!sdsa->partition->dn) {
58                 talloc_free(sdsa);
59                 return WERR_NOMEM;
60         }
61         sdsa->partition->nc.dn = ldb_dn_alloc_linearized(sdsa->partition, fsmo_role_dn);
62         if (!sdsa->partition->nc.dn) {
63                 talloc_free(sdsa);
64                 return WERR_NOMEM;
65         }
66         ret = dsdb_find_guid_by_dn(ldb, fsmo_role_dn, &sdsa->partition->nc.guid);
67         if (ret != LDB_SUCCESS) {
68                 DEBUG(0,(__location__ ": Failed to find GUID for %s\n",
69                          ldb_dn_get_linearized(fsmo_role_dn)));
70                 talloc_free(sdsa);
71                 return WERR_DS_DRA_INTERNAL_ERROR;
72         }
73
74         sdsa->repsFrom1 = &sdsa->_repsFromBlob.ctr.ctr1;
75         ret = dsdb_find_guid_by_dn(ldb, role_owner_dn, &sdsa->repsFrom1->source_dsa_obj_guid);
76         if (ret != LDB_SUCCESS) {
77                 DEBUG(0,(__location__ ": Failed to find objectGUID for %s\n",
78                          ldb_dn_get_linearized(role_owner_dn)));
79                 talloc_free(sdsa);
80                 return WERR_DS_DRA_INTERNAL_ERROR;
81         }
82
83         sdsa->repsFrom1->other_info = talloc_zero(sdsa, struct repsFromTo1OtherInfo);
84         if (!sdsa->repsFrom1->other_info) {
85                 talloc_free(sdsa);
86                 return WERR_NOMEM;
87         }
88
89         sdsa->repsFrom1->other_info->dns_name =
90                 talloc_asprintf(sdsa->repsFrom1->other_info, "%s._msdcs.%s",
91                                 GUID_string(sdsa->repsFrom1->other_info, &sdsa->repsFrom1->source_dsa_obj_guid),
92                                 lpcfg_dnsdomain(service->task->lp_ctx));
93         if (!sdsa->repsFrom1->other_info->dns_name) {
94                 talloc_free(sdsa);
95                 return WERR_NOMEM;
96         }
97
98
99         werr = dreplsrv_out_connection_attach(service, sdsa->repsFrom1, &sdsa->conn);
100         if (!W_ERROR_IS_OK(werr)) {
101                 DEBUG(0,(__location__ ": Failed to attach connection to %s\n",
102                          ldb_dn_get_linearized(fsmo_role_dn)));
103                 talloc_free(sdsa);
104                 return werr;
105         }
106
107         service->ncchanges_extended.role_owner_source_dsa = sdsa;
108         return WERR_OK;
109 }
110
111 /*
112   schedule a getncchanges request to the role owner for an extended operation
113  */
114 WERROR drepl_request_extended_op(struct dreplsrv_service *service,
115                                  struct ldb_dn *fsmo_role_dn,
116                                  struct ldb_dn *role_owner_dn,
117                                  enum drsuapi_DsExtendedOperation extended_op,
118                                  uint64_t alloc_pool,
119                                  dreplsrv_fsmo_callback_t callback)
120 {
121         WERROR werr;
122         if (service->ncchanges_extended.role_owner_source_dsa == NULL) {
123                 /* we need to establish a connection to the role owner */
124                 werr = drepl_create_role_owner_source_dsa(service, fsmo_role_dn, role_owner_dn);
125                 W_ERROR_NOT_OK_RETURN(werr);
126         }
127
128         service->ncchanges_extended.in_progress = true;
129         werr = dreplsrv_schedule_partition_pull_source(service, service->ncchanges_extended.role_owner_source_dsa,
130                                                        extended_op, alloc_pool,
131                                                        callback, NULL);
132         return werr;
133 }
134
135 /*
136   called when a rid allocation request has completed
137  */
138 static void drepl_new_rid_pool_callback(struct dreplsrv_service *service,
139                                         WERROR werr,
140                                         enum drsuapi_DsExtendedError ext_err,
141                                         void *cb_data)
142 {
143         if (!W_ERROR_IS_OK(werr)) {
144                 DEBUG(0,(__location__ ": RID Manager failed RID allocation - %s - extended_ret[0x%X]\n",
145                          win_errstr(werr), ext_err));
146         } else {
147                 DEBUG(3,(__location__ ": RID Manager completed RID allocation OK\n"));
148         }
149
150         /* don't keep the connection open to the RID Manager */
151         talloc_free(service->ncchanges_extended.role_owner_source_dsa);
152         service->ncchanges_extended.role_owner_source_dsa = NULL;
153
154         service->ncchanges_extended.in_progress = false;
155 }
156
157 /*
158   schedule a getncchanges request to the RID Manager to ask for a new
159   set of RIDs using DRSUAPI_EXOP_FSMO_RID_ALLOC
160  */
161 static WERROR drepl_request_new_rid_pool(struct dreplsrv_service *service,
162                                          struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn,
163                                          uint64_t alloc_pool)
164 {
165         WERROR werr = drepl_request_extended_op(service,
166                                                 rid_manager_dn,
167                                                 fsmo_role_dn,
168                                                 DRSUAPI_EXOP_FSMO_RID_ALLOC,
169                                                 alloc_pool,
170                                                 drepl_new_rid_pool_callback);
171         return werr;
172 }
173
174
175 /*
176   see if we are on the last pool we have
177  */
178 static int drepl_ridalloc_pool_exhausted(struct ldb_context *ldb,
179                                          bool *exhausted,
180                                          uint64_t *_alloc_pool)
181 {
182         struct ldb_dn *server_dn, *machine_dn, *rid_set_dn;
183         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
184         uint64_t alloc_pool;
185         uint64_t prev_pool;
186         uint32_t prev_pool_lo, prev_pool_hi;
187         uint32_t next_rid;
188         static const char * const attrs[] = {
189                 "rIDAllocationPool",
190                 "rIDPreviousAllocationPool",
191                 "rIDNextRid",
192                 NULL
193         };
194         int ret;
195         struct ldb_result *res;
196
197         *exhausted = false;
198         *_alloc_pool = UINT64_MAX;
199
200         server_dn = ldb_dn_get_parent(tmp_ctx, samdb_ntds_settings_dn(ldb));
201         if (!server_dn) {
202                 talloc_free(tmp_ctx);
203                 return ldb_operr(ldb);
204         }
205
206         ret = samdb_reference_dn(ldb, tmp_ctx, server_dn, "serverReference", &machine_dn);
207         if (ret != LDB_SUCCESS) {
208                 DEBUG(0,(__location__ ": Failed to find serverReference in %s - %s",
209                          ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
210                 talloc_free(tmp_ctx);
211                 return ret;
212         }
213
214         ret = samdb_reference_dn(ldb, tmp_ctx, machine_dn, "rIDSetReferences", &rid_set_dn);
215         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
216                 *exhausted = true;
217                 *_alloc_pool = 0;
218                 talloc_free(tmp_ctx);
219                 return LDB_SUCCESS;
220         }
221         if (ret != LDB_SUCCESS) {
222                 DEBUG(0,(__location__ ": Failed to find rIDSetReferences in %s - %s",
223                          ldb_dn_get_linearized(machine_dn), ldb_errstring(ldb)));
224                 talloc_free(tmp_ctx);
225                 return ret;
226         }
227
228         ret = ldb_search(ldb, tmp_ctx, &res, rid_set_dn, LDB_SCOPE_BASE, attrs, NULL);
229         if (ret != LDB_SUCCESS) {
230                 DEBUG(0,(__location__ ": Failed to load RID Set attrs from %s - %s",
231                          ldb_dn_get_linearized(rid_set_dn), ldb_errstring(ldb)));
232                 talloc_free(tmp_ctx);
233                 return ret;
234         }
235
236         alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDAllocationPool", 0);
237         prev_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDPreviousAllocationPool", 0);
238         prev_pool_lo = prev_pool & 0xFFFFFFFF;
239         prev_pool_hi = prev_pool >> 32;
240         next_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "rIDNextRid", 0);
241
242         if (alloc_pool != prev_pool) {
243                 talloc_free(tmp_ctx);
244                 return LDB_SUCCESS;
245         }
246
247         if (next_rid < (prev_pool_hi + prev_pool_lo)/2) {
248                 talloc_free(tmp_ctx);
249                 return LDB_SUCCESS;
250         }
251
252         *exhausted = true;
253         *_alloc_pool = alloc_pool;
254         talloc_free(tmp_ctx);
255         return LDB_SUCCESS;
256 }
257
258
259 /*
260   see if we are low on RIDs in the RID Set rIDAllocationPool. If we
261   are, then schedule a replication call with DRSUAPI_EXOP_FSMO_RID_ALLOC
262   to the RID Manager
263  */
264 WERROR dreplsrv_ridalloc_check_rid_pool(struct dreplsrv_service *service)
265 {
266         struct ldb_dn *rid_manager_dn, *fsmo_role_dn;
267         TALLOC_CTX *tmp_ctx = talloc_new(service);
268         struct ldb_context *ldb = service->samdb;
269         bool exhausted;
270         WERROR werr;
271         int ret;
272         uint64_t alloc_pool;
273
274         if (service->ncchanges_extended.in_progress) {
275                 talloc_free(tmp_ctx);
276                 return WERR_OK;
277         }
278
279         /*
280           steps:
281             - find who the RID Manager is
282             - if we are the RID Manager then nothing to do
283             - find our RID Set object
284             - load rIDAllocationPool and rIDPreviousAllocationPool
285             - if rIDAllocationPool != rIDPreviousAllocationPool then
286               nothing to do
287             - schedule a getncchanges with DRSUAPI_EXOP_FSMO_RID_ALLOC
288               to the RID Manager
289          */
290
291         /* work out who is the RID Manager */
292         ret = samdb_rid_manager_dn(ldb, tmp_ctx, &rid_manager_dn);
293         if (ret != LDB_SUCCESS) {
294                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
295                 talloc_free(tmp_ctx);
296                 return WERR_DS_DRA_INTERNAL_ERROR;
297         }
298
299         /* find the DN of the RID Manager */
300         ret = samdb_reference_dn(ldb, tmp_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
301         if (ret != LDB_SUCCESS) {
302                 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
303                          ldb_errstring(ldb)));
304                 talloc_free(tmp_ctx);
305                 return WERR_DS_DRA_INTERNAL_ERROR;
306         }
307
308         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) == 0) {
309                 /* we are the RID Manager - no need to do a
310                    DRSUAPI_EXOP_FSMO_RID_ALLOC */
311                 talloc_free(tmp_ctx);
312                 return WERR_OK;
313         }
314
315         ret = drepl_ridalloc_pool_exhausted(ldb, &exhausted, &alloc_pool);
316         if (ret != LDB_SUCCESS) {
317                 talloc_free(tmp_ctx);
318                 return WERR_DS_DRA_INTERNAL_ERROR;
319         }
320
321         if (!exhausted) {
322                 /* don't need a new pool */
323                 talloc_free(tmp_ctx);
324                 return WERR_OK;
325         }
326
327         DEBUG(2,(__location__ ": Requesting more RIDs from RID Manager\n"));
328
329         werr = drepl_request_new_rid_pool(service, rid_manager_dn, fsmo_role_dn, alloc_pool);
330         talloc_free(tmp_ctx);
331         return werr;
332 }
333
334 /* called by the samldb ldb module to tell us to ask for a new RID
335    pool */
336 void dreplsrv_allocate_rid(struct messaging_context *msg, void *private_data,
337                            uint32_t msg_type,
338                            struct server_id server_id, DATA_BLOB *data)
339 {
340         struct dreplsrv_service *service = talloc_get_type(private_data, struct dreplsrv_service);
341         dreplsrv_ridalloc_check_rid_pool(service);
342 }