2 Unix SMB/CIFS mplementation.
4 DSDB replication service - RID allocation code
6 Copyright (C) Andrew Tridgell 2010
7 Copyright (C) Andrew Bartlett 2010
9 based on drepl_notify.c
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.
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.
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/>.
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"
35 create the RID manager source dsa structure
38 WERROR drepl_create_role_owner_source_dsa(struct dreplsrv_service *service,
39 struct ldb_dn *role_owner_dn, struct ldb_dn *fsmo_role_dn)
41 struct dreplsrv_partition_source_dsa *sdsa;
42 struct ldb_context *ldb = service->samdb;
46 sdsa = talloc_zero(service, struct dreplsrv_partition_source_dsa);
47 W_ERROR_HAVE_NO_MEMORY(sdsa);
49 sdsa->partition = talloc_zero(sdsa, struct dreplsrv_partition);
50 if (!sdsa->partition) {
55 sdsa->partition->dn = ldb_get_default_basedn(ldb);
56 sdsa->partition->nc.dn = ldb_dn_alloc_linearized(sdsa->partition, role_owner_dn);
57 ret = dsdb_find_guid_by_dn(ldb, role_owner_dn, &sdsa->partition->nc.guid);
58 if (ret != LDB_SUCCESS) {
59 DEBUG(0,(__location__ ": Failed to find GUID for %s\n",
60 ldb_dn_get_linearized(role_owner_dn)));
62 return WERR_DS_DRA_INTERNAL_ERROR;
65 sdsa->repsFrom1 = &sdsa->_repsFromBlob.ctr.ctr1;
66 ret = dsdb_find_guid_attr_by_dn(ldb, fsmo_role_dn, "objectGUID", &sdsa->repsFrom1->source_dsa_obj_guid);
67 if (ret != LDB_SUCCESS) {
68 DEBUG(0,(__location__ ": Failed to find objectGUID for %s\n",
69 ldb_dn_get_linearized(fsmo_role_dn)));
71 return WERR_DS_DRA_INTERNAL_ERROR;
74 sdsa->repsFrom1->other_info = talloc_zero(sdsa, struct repsFromTo1OtherInfo);
75 if (!sdsa->repsFrom1->other_info) {
80 sdsa->repsFrom1->other_info->dns_name =
81 talloc_asprintf(sdsa->repsFrom1->other_info, "%s._msdcs.%s",
82 GUID_string(sdsa->repsFrom1->other_info, &sdsa->repsFrom1->source_dsa_obj_guid),
83 lpcfg_dnsdomain(service->task->lp_ctx));
84 if (!sdsa->repsFrom1->other_info->dns_name) {
90 werr = dreplsrv_out_connection_attach(service, sdsa->repsFrom1, &sdsa->conn);
91 if (!W_ERROR_IS_OK(werr)) {
92 DEBUG(0,(__location__ ": Failed to attach connection to %s\n",
93 ldb_dn_get_linearized(role_owner_dn)));
98 service->ncchanges_extended.role_owner_source_dsa = sdsa;
103 schedule a getncchanges request to the role owner for an extended operation
105 WERROR drepl_request_extended_op(struct dreplsrv_service *service,
106 struct ldb_dn *role_owner_dn,
107 struct ldb_dn *fsmo_role_dn,
108 enum drsuapi_DsExtendedOperation extended_op,
110 dreplsrv_fsmo_callback_t callback)
114 if (service->ncchanges_extended.role_owner_source_dsa == NULL) {
115 /* we need to establish a connection to the RID
117 werr = drepl_create_role_owner_source_dsa(service, role_owner_dn, fsmo_role_dn);
118 W_ERROR_NOT_OK_RETURN(werr);
121 service->ncchanges_extended.in_progress = true;
123 werr = dreplsrv_schedule_partition_pull_source(service, service->ncchanges_extended.role_owner_source_dsa,
124 extended_op, alloc_pool,
130 called when a rid allocation request has completed
132 static void drepl_new_rid_pool_callback(struct dreplsrv_service *service,
134 enum drsuapi_DsExtendedError ext_err,
137 if (!W_ERROR_IS_OK(werr)) {
138 DEBUG(0,(__location__ ": RID Manager failed RID allocation - %s - extended_ret[0x%X]\n",
139 win_errstr(werr), ext_err));
141 DEBUG(3,(__location__ ": RID Manager completed RID allocation OK\n"));
144 /* don't keep the connection open to the RID Manager */
145 talloc_free(service->ncchanges_extended.role_owner_source_dsa);
146 service->ncchanges_extended.role_owner_source_dsa = NULL;
148 service->ncchanges_extended.in_progress = false;
152 schedule a getncchanges request to the RID Manager to ask for a new
153 set of RIDs using DRSUAPI_EXOP_FSMO_RID_ALLOC
155 static WERROR drepl_request_new_rid_pool(struct dreplsrv_service *service,
156 struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn,
159 WERROR werr = drepl_request_extended_op(service,
162 DRSUAPI_EXOP_FSMO_RID_ALLOC,
164 drepl_new_rid_pool_callback, NULL);
170 see if we are on the last pool we have
172 static int drepl_ridalloc_pool_exhausted(struct ldb_context *ldb,
174 uint64_t *_alloc_pool)
176 struct ldb_dn *server_dn, *machine_dn, *rid_set_dn;
177 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
180 uint32_t prev_pool_lo, prev_pool_hi;
182 static const char * const attrs[] = {
184 "rIDPreviousAllocationPool",
189 struct ldb_result *res;
192 *_alloc_pool = UINT64_MAX;
194 server_dn = ldb_dn_get_parent(tmp_ctx, samdb_ntds_settings_dn(ldb));
196 talloc_free(tmp_ctx);
197 return ldb_operr(ldb);
200 ret = samdb_reference_dn(ldb, tmp_ctx, server_dn, "serverReference", &machine_dn);
201 if (ret != LDB_SUCCESS) {
202 DEBUG(0,(__location__ ": Failed to find serverReference in %s - %s",
203 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
204 talloc_free(tmp_ctx);
208 ret = samdb_reference_dn(ldb, tmp_ctx, machine_dn, "rIDSetReferences", &rid_set_dn);
209 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
212 talloc_free(tmp_ctx);
215 if (ret != LDB_SUCCESS) {
216 DEBUG(0,(__location__ ": Failed to find rIDSetReferences in %s - %s",
217 ldb_dn_get_linearized(machine_dn), ldb_errstring(ldb)));
218 talloc_free(tmp_ctx);
222 ret = ldb_search(ldb, tmp_ctx, &res, rid_set_dn, LDB_SCOPE_BASE, attrs, NULL);
223 if (ret != LDB_SUCCESS) {
224 DEBUG(0,(__location__ ": Failed to load RID Set attrs from %s - %s",
225 ldb_dn_get_linearized(rid_set_dn), ldb_errstring(ldb)));
226 talloc_free(tmp_ctx);
230 alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDAllocationPool", 0);
231 prev_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDPreviousAllocationPool", 0);
232 prev_pool_lo = prev_pool & 0xFFFFFFFF;
233 prev_pool_hi = prev_pool >> 32;
234 next_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "rIDNextRid", 0);
236 if (alloc_pool != prev_pool) {
237 talloc_free(tmp_ctx);
241 if (next_rid < (prev_pool_hi + prev_pool_lo)/2) {
242 talloc_free(tmp_ctx);
247 *_alloc_pool = alloc_pool;
248 talloc_free(tmp_ctx);
254 see if we are low on RIDs in the RID Set rIDAllocationPool. If we
255 are, then schedule a replication call with DRSUAPI_EXOP_FSMO_RID_ALLOC
258 WERROR dreplsrv_ridalloc_check_rid_pool(struct dreplsrv_service *service)
260 struct ldb_dn *rid_manager_dn, *fsmo_role_dn;
261 TALLOC_CTX *tmp_ctx = talloc_new(service);
262 struct ldb_context *ldb = service->samdb;
268 if (service->ncchanges_extended.in_progress) {
269 talloc_free(tmp_ctx);
275 - find who the RID Manager is
276 - if we are the RID Manager then nothing to do
277 - find our RID Set object
278 - load rIDAllocationPool and rIDPreviousAllocationPool
279 - if rIDAllocationPool != rIDPreviousAllocationPool then
281 - schedule a getncchanges with DRSUAPI_EXOP_FSMO_RID_ALLOC
285 /* work out who is the RID Manager */
286 ret = samdb_rid_manager_dn(ldb, tmp_ctx, &rid_manager_dn);
287 if (ret != LDB_SUCCESS) {
288 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
289 talloc_free(tmp_ctx);
290 return WERR_DS_DRA_INTERNAL_ERROR;
293 /* find the DN of the RID Manager */
294 ret = samdb_reference_dn(ldb, tmp_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
295 if (ret != LDB_SUCCESS) {
296 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
297 ldb_errstring(ldb)));
298 talloc_free(tmp_ctx);
299 return WERR_DS_DRA_INTERNAL_ERROR;
302 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) == 0) {
303 /* we are the RID Manager - no need to do a
304 DRSUAPI_EXOP_FSMO_RID_ALLOC */
305 talloc_free(tmp_ctx);
309 ret = drepl_ridalloc_pool_exhausted(ldb, &exhausted, &alloc_pool);
310 if (ret != LDB_SUCCESS) {
311 talloc_free(tmp_ctx);
312 return WERR_DS_DRA_INTERNAL_ERROR;
316 /* don't need a new pool */
317 talloc_free(tmp_ctx);
321 DEBUG(2,(__location__ ": Requesting more RIDs from RID Manager\n"));
323 werr = drepl_request_new_rid_pool(service, rid_manager_dn, fsmo_role_dn, alloc_pool);
324 talloc_free(tmp_ctx);
328 /* called by the samldb ldb module to tell us to ask for a new RID
330 void dreplsrv_allocate_rid(struct messaging_context *msg, void *private_data,
332 struct server_id server_id, DATA_BLOB *data)
334 struct dreplsrv_service *service = talloc_get_type(private_data, struct dreplsrv_service);
335 dreplsrv_ridalloc_check_rid_pool(service);