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
37 static WERROR drepl_create_rid_manager_source_dsa(struct dreplsrv_service *service,
38 struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn)
40 struct dreplsrv_partition_source_dsa *sdsa;
41 struct ldb_context *ldb = service->samdb;
45 sdsa = talloc_zero(service, struct dreplsrv_partition_source_dsa);
46 W_ERROR_HAVE_NO_MEMORY(sdsa);
48 sdsa->partition = talloc_zero(sdsa, struct dreplsrv_partition);
49 if (!sdsa->partition) {
54 sdsa->partition->dn = ldb_get_default_basedn(ldb);
55 sdsa->partition->nc.dn = ldb_dn_alloc_linearized(sdsa->partition, rid_manager_dn);
56 ret = dsdb_find_guid_by_dn(ldb, rid_manager_dn, &sdsa->partition->nc.guid);
57 if (ret != LDB_SUCCESS) {
58 DEBUG(0,(__location__ ": Failed to find GUID for %s\n",
59 ldb_dn_get_linearized(rid_manager_dn)));
61 return WERR_DS_DRA_INTERNAL_ERROR;
64 sdsa->repsFrom1 = &sdsa->_repsFromBlob.ctr.ctr1;
65 ret = dsdb_find_guid_attr_by_dn(ldb, fsmo_role_dn, "objectGUID", &sdsa->repsFrom1->source_dsa_obj_guid);
66 if (ret != LDB_SUCCESS) {
67 DEBUG(0,(__location__ ": Failed to find objectGUID for %s\n",
68 ldb_dn_get_linearized(fsmo_role_dn)));
70 return WERR_DS_DRA_INTERNAL_ERROR;
73 sdsa->repsFrom1->other_info = talloc_zero(sdsa, struct repsFromTo1OtherInfo);
74 if (!sdsa->repsFrom1->other_info) {
79 sdsa->repsFrom1->other_info->dns_name =
80 talloc_asprintf(sdsa->repsFrom1->other_info, "%s._msdcs.%s",
81 GUID_string(sdsa->repsFrom1->other_info, &sdsa->repsFrom1->source_dsa_obj_guid),
82 lp_dnsdomain(service->task->lp_ctx));
83 if (!sdsa->repsFrom1->other_info->dns_name) {
89 werr = dreplsrv_out_connection_attach(service, sdsa->repsFrom1, &sdsa->conn);
90 if (!W_ERROR_IS_OK(werr)) {
91 DEBUG(0,(__location__ ": Failed to attach to RID manager connection\n"));
96 service->ridalloc.rid_manager_source_dsa = sdsa;
101 called when a rid allocation request has completed
103 static void drepl_new_rid_pool_callback(struct dreplsrv_service *service, WERROR werr)
105 if (!W_ERROR_IS_OK(werr)) {
106 DEBUG(0,(__location__ ": RID Manager failed RID allocation - %s\n",
109 DEBUG(3,(__location__ ": RID Manager completed RID allocation OK\n"));
112 /* don't keep the connection open to the RID Manager */
113 talloc_free(service->ridalloc.rid_manager_source_dsa);
114 service->ridalloc.rid_manager_source_dsa = NULL;
116 service->ridalloc.in_progress = false;
120 schedule a getncchanges request to the RID Manager to ask for a new
121 set of RIDs using DRSUAPI_EXOP_FSMO_RID_ALLOC
123 static WERROR drepl_request_new_rid_pool(struct dreplsrv_service *service,
124 struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn,
129 if (service->ridalloc.rid_manager_source_dsa == NULL) {
130 /* we need to establish a connection to the RID
132 werr = drepl_create_rid_manager_source_dsa(service, rid_manager_dn, fsmo_role_dn);
133 W_ERROR_NOT_OK_RETURN(werr);
136 service->ridalloc.in_progress = true;
138 werr = dreplsrv_schedule_partition_pull_source(service, service->ridalloc.rid_manager_source_dsa,
139 DRSUAPI_EXOP_FSMO_RID_ALLOC, alloc_pool,
140 drepl_new_rid_pool_callback);
146 see if we are on the last pool we have
148 static int drepl_ridalloc_pool_exhausted(struct ldb_context *ldb, bool *exhausted, uint64_t *alloc_pool)
150 struct ldb_dn *server_dn, *machine_dn, *rid_set_dn;
151 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
152 uint64_t prev_alloc_pool;
153 const char *attrs[] = { "rIDPreviousAllocationPool", "rIDAllocationPool", NULL };
155 struct ldb_result *res;
157 server_dn = ldb_dn_get_parent(tmp_ctx, samdb_ntds_settings_dn(ldb));
159 talloc_free(tmp_ctx);
160 return ldb_operr(ldb);
163 ret = samdb_reference_dn(ldb, tmp_ctx, server_dn, "serverReference", &machine_dn);
164 if (ret != LDB_SUCCESS) {
165 DEBUG(0,(__location__ ": Failed to find serverReference in %s - %s",
166 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
167 talloc_free(tmp_ctx);
171 ret = samdb_reference_dn(ldb, tmp_ctx, machine_dn, "rIDSetReferences", &rid_set_dn);
172 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
174 talloc_free(tmp_ctx);
177 if (ret != LDB_SUCCESS) {
178 DEBUG(0,(__location__ ": Failed to find rIDSetReferences in %s - %s",
179 ldb_dn_get_linearized(machine_dn), ldb_errstring(ldb)));
180 talloc_free(tmp_ctx);
184 ret = ldb_search(ldb, tmp_ctx, &res, rid_set_dn, LDB_SCOPE_BASE, attrs, NULL);
185 if (ret != LDB_SUCCESS) {
186 DEBUG(0,(__location__ ": Failed to load RID Set attrs from %s - %s",
187 ldb_dn_get_linearized(rid_set_dn), ldb_errstring(ldb)));
188 talloc_free(tmp_ctx);
192 *alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDAllocationPool", 0);
193 prev_alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDPreviousAllocationPool", 0);
195 if (*alloc_pool != prev_alloc_pool) {
201 talloc_free(tmp_ctx);
207 see if we are low on RIDs in the RID Set rIDAllocationPool. If we
208 are, then schedule a replication call with DRSUAPI_EXOP_FSMO_RID_ALLOC
211 WERROR dreplsrv_ridalloc_check_rid_pool(struct dreplsrv_service *service)
213 struct ldb_dn *rid_manager_dn, *fsmo_role_dn;
214 TALLOC_CTX *tmp_ctx = talloc_new(service);
215 struct ldb_context *ldb = service->samdb;
221 if (service->ridalloc.in_progress) {
222 talloc_free(tmp_ctx);
228 - find who the RID Manager is
229 - if we are the RID Manager then nothing to do
230 - find our RID Set object
231 - load rIDAllocationPool and rIDPreviousAllocationPool
232 - if rIDAllocationPool != rIDPreviousAllocationPool then
234 - schedule a getncchanges with DRSUAPI_EXOP_FSMO_RID_ALLOC
238 /* work out who is the RID Manager */
239 ret = samdb_rid_manager_dn(ldb, tmp_ctx, &rid_manager_dn);
240 if (ret != LDB_SUCCESS) {
241 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
242 talloc_free(tmp_ctx);
243 return WERR_DS_DRA_INTERNAL_ERROR;
246 /* find the DN of the RID Manager */
247 ret = samdb_reference_dn(ldb, tmp_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
248 if (ret != LDB_SUCCESS) {
249 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
250 ldb_errstring(ldb)));
251 talloc_free(tmp_ctx);
252 return WERR_DS_DRA_INTERNAL_ERROR;
255 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) == 0) {
256 /* we are the RID Manager - no need to do a
257 DRSUAPI_EXOP_FSMO_RID_ALLOC */
258 talloc_free(tmp_ctx);
262 ret = drepl_ridalloc_pool_exhausted(ldb, &exhausted, &alloc_pool);
263 if (ret != LDB_SUCCESS) {
264 talloc_free(tmp_ctx);
265 return WERR_DS_DRA_INTERNAL_ERROR;
268 DEBUG(2,(__location__ ": Requesting more RIDs from RID Manager\n"));
270 werr = drepl_request_new_rid_pool(service, rid_manager_dn, fsmo_role_dn, alloc_pool);
271 talloc_free(tmp_ctx);
275 /* called by the samldb ldb module to tell us to ask for a new RID
277 void dreplsrv_allocate_rid(struct messaging_context *msg, void *private_data,
279 struct server_id server_id, DATA_BLOB *data)
281 struct dreplsrv_service *service = talloc_get_type(private_data, struct dreplsrv_service);
282 dreplsrv_ridalloc_check_rid_pool(service);