6d4b0da04be0b416b9b4ac1bd9bd13355783a100
[samba.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 RID manager source dsa structure
36  */
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)
39 {
40         struct dreplsrv_partition_source_dsa *sdsa;
41         struct ldb_context *ldb = service->samdb;
42         int ret;
43         WERROR werr;
44
45         sdsa = talloc_zero(service, struct dreplsrv_partition_source_dsa);
46         W_ERROR_HAVE_NO_MEMORY(sdsa);
47
48         sdsa->partition = talloc_zero(sdsa, struct dreplsrv_partition);
49         if (!sdsa->partition) {
50                 talloc_free(sdsa);
51                 return WERR_NOMEM;
52         }
53
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)));
60                 talloc_free(sdsa);
61                 return WERR_DS_DRA_INTERNAL_ERROR;
62         }
63
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)));
69                 talloc_free(sdsa);
70                 return WERR_DS_DRA_INTERNAL_ERROR;
71         }
72
73         sdsa->repsFrom1->other_info = talloc_zero(sdsa, struct repsFromTo1OtherInfo);
74         if (!sdsa->repsFrom1->other_info) {
75                 talloc_free(sdsa);
76                 return WERR_NOMEM;
77         }
78
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                                 lpcfg_dnsdomain(service->task->lp_ctx));
83         if (!sdsa->repsFrom1->other_info->dns_name) {
84                 talloc_free(sdsa);
85                 return WERR_NOMEM;
86         }
87
88
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"));
92                 talloc_free(sdsa);
93                 return werr;
94         }
95
96         service->ridalloc.rid_manager_source_dsa = sdsa;
97         return WERR_OK;
98 }
99
100 /*
101   called when a rid allocation request has completed
102  */
103 static void drepl_new_rid_pool_callback(struct dreplsrv_service *service,
104                                         WERROR werr,
105                                         enum drsuapi_DsExtendedError ext_err,
106                                         void *cb_data)
107 {
108         if (!W_ERROR_IS_OK(werr)) {
109                 DEBUG(0,(__location__ ": RID Manager failed RID allocation - %s - extended_ret[0x%X]\n",
110                          win_errstr(werr), ext_err));
111         } else {
112                 DEBUG(3,(__location__ ": RID Manager completed RID allocation OK\n"));
113         }
114
115         /* don't keep the connection open to the RID Manager */
116         talloc_free(service->ridalloc.rid_manager_source_dsa);
117         service->ridalloc.rid_manager_source_dsa = NULL;
118
119         service->ridalloc.in_progress = false;
120 }
121
122 /*
123   schedule a getncchanges request to the RID Manager to ask for a new
124   set of RIDs using DRSUAPI_EXOP_FSMO_RID_ALLOC
125  */
126 static WERROR drepl_request_new_rid_pool(struct dreplsrv_service *service,
127                                          struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn,
128                                          uint64_t alloc_pool)
129 {
130         WERROR werr;
131
132         if (service->ridalloc.rid_manager_source_dsa == NULL) {
133                 /* we need to establish a connection to the RID
134                    Manager */
135                 werr = drepl_create_rid_manager_source_dsa(service, rid_manager_dn, fsmo_role_dn);
136                 W_ERROR_NOT_OK_RETURN(werr);
137         }
138
139         service->ridalloc.in_progress = true;
140
141         werr = dreplsrv_schedule_partition_pull_source(service, service->ridalloc.rid_manager_source_dsa,
142                                                        DRSUAPI_EXOP_FSMO_RID_ALLOC, alloc_pool,
143                                                        drepl_new_rid_pool_callback, NULL);
144         return werr;
145 }
146
147
148 /*
149   see if we are on the last pool we have
150  */
151 static int drepl_ridalloc_pool_exhausted(struct ldb_context *ldb,
152                                          bool *exhausted,
153                                          uint64_t *_alloc_pool)
154 {
155         struct ldb_dn *server_dn, *machine_dn, *rid_set_dn;
156         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
157         uint64_t alloc_pool;
158         uint64_t prev_pool;
159         uint32_t prev_pool_lo, prev_pool_hi;
160         uint32_t next_rid;
161         static const char * const attrs[] = {
162                 "rIDAllocationPool",
163                 "rIDPreviousAllocationPool",
164                 "rIDNextRid",
165                 NULL
166         };
167         int ret;
168         struct ldb_result *res;
169
170         *exhausted = false;
171         *_alloc_pool = UINT64_MAX;
172
173         server_dn = ldb_dn_get_parent(tmp_ctx, samdb_ntds_settings_dn(ldb));
174         if (!server_dn) {
175                 talloc_free(tmp_ctx);
176                 return ldb_operr(ldb);
177         }
178
179         ret = samdb_reference_dn(ldb, tmp_ctx, server_dn, "serverReference", &machine_dn);
180         if (ret != LDB_SUCCESS) {
181                 DEBUG(0,(__location__ ": Failed to find serverReference in %s - %s",
182                          ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
183                 talloc_free(tmp_ctx);
184                 return ret;
185         }
186
187         ret = samdb_reference_dn(ldb, tmp_ctx, machine_dn, "rIDSetReferences", &rid_set_dn);
188         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
189                 *exhausted = true;
190                 *_alloc_pool = 0;
191                 talloc_free(tmp_ctx);
192                 return LDB_SUCCESS;
193         }
194         if (ret != LDB_SUCCESS) {
195                 DEBUG(0,(__location__ ": Failed to find rIDSetReferences in %s - %s",
196                          ldb_dn_get_linearized(machine_dn), ldb_errstring(ldb)));
197                 talloc_free(tmp_ctx);
198                 return ret;
199         }
200
201         ret = ldb_search(ldb, tmp_ctx, &res, rid_set_dn, LDB_SCOPE_BASE, attrs, NULL);
202         if (ret != LDB_SUCCESS) {
203                 DEBUG(0,(__location__ ": Failed to load RID Set attrs from %s - %s",
204                          ldb_dn_get_linearized(rid_set_dn), ldb_errstring(ldb)));
205                 talloc_free(tmp_ctx);
206                 return ret;
207         }
208
209         alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDAllocationPool", 0);
210         prev_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDPreviousAllocationPool", 0);
211         prev_pool_lo = prev_pool & 0xFFFFFFFF;
212         prev_pool_hi = prev_pool >> 32;
213         next_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "rIDNextRid", 0);
214
215         if (alloc_pool != prev_pool) {
216                 talloc_free(tmp_ctx);
217                 return LDB_SUCCESS;
218         }
219
220         if (next_rid < (prev_pool_hi + prev_pool_lo)/2) {
221                 talloc_free(tmp_ctx);
222                 return LDB_SUCCESS;
223         }
224
225         *exhausted = true;
226         *_alloc_pool = alloc_pool;
227         talloc_free(tmp_ctx);
228         return LDB_SUCCESS;
229 }
230
231
232 /*
233   see if we are low on RIDs in the RID Set rIDAllocationPool. If we
234   are, then schedule a replication call with DRSUAPI_EXOP_FSMO_RID_ALLOC
235   to the RID Manager
236  */
237 WERROR dreplsrv_ridalloc_check_rid_pool(struct dreplsrv_service *service)
238 {
239         struct ldb_dn *rid_manager_dn, *fsmo_role_dn;
240         TALLOC_CTX *tmp_ctx = talloc_new(service);
241         struct ldb_context *ldb = service->samdb;
242         bool exhausted;
243         WERROR werr;
244         int ret;
245         uint64_t alloc_pool;
246
247         if (service->ridalloc.in_progress) {
248                 talloc_free(tmp_ctx);
249                 return WERR_OK;
250         }
251
252         /*
253           steps:
254             - find who the RID Manager is
255             - if we are the RID Manager then nothing to do
256             - find our RID Set object
257             - load rIDAllocationPool and rIDPreviousAllocationPool
258             - if rIDAllocationPool != rIDPreviousAllocationPool then
259               nothing to do
260             - schedule a getncchanges with DRSUAPI_EXOP_FSMO_RID_ALLOC
261               to the RID Manager
262          */
263
264         /* work out who is the RID Manager */
265         ret = samdb_rid_manager_dn(ldb, tmp_ctx, &rid_manager_dn);
266         if (ret != LDB_SUCCESS) {
267                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
268                 talloc_free(tmp_ctx);
269                 return WERR_DS_DRA_INTERNAL_ERROR;
270         }
271
272         /* find the DN of the RID Manager */
273         ret = samdb_reference_dn(ldb, tmp_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
274         if (ret != LDB_SUCCESS) {
275                 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
276                          ldb_errstring(ldb)));
277                 talloc_free(tmp_ctx);
278                 return WERR_DS_DRA_INTERNAL_ERROR;
279         }
280
281         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) == 0) {
282                 /* we are the RID Manager - no need to do a
283                    DRSUAPI_EXOP_FSMO_RID_ALLOC */
284                 talloc_free(tmp_ctx);
285                 return WERR_OK;
286         }
287
288         ret = drepl_ridalloc_pool_exhausted(ldb, &exhausted, &alloc_pool);
289         if (ret != LDB_SUCCESS) {
290                 talloc_free(tmp_ctx);
291                 return WERR_DS_DRA_INTERNAL_ERROR;
292         }
293
294         if (!exhausted) {
295                 /* don't need a new pool */
296                 talloc_free(tmp_ctx);
297                 return WERR_OK;
298         }
299
300         DEBUG(2,(__location__ ": Requesting more RIDs from RID Manager\n"));
301
302         werr = drepl_request_new_rid_pool(service, rid_manager_dn, fsmo_role_dn, alloc_pool);
303         talloc_free(tmp_ctx);
304         return werr;
305 }
306
307 /* called by the samldb ldb module to tell us to ask for a new RID
308    pool */
309 void dreplsrv_allocate_rid(struct messaging_context *msg, void *private_data,
310                            uint32_t msg_type,
311                            struct server_id server_id, DATA_BLOB *data)
312 {
313         struct dreplsrv_service *service = talloc_get_type(private_data, struct dreplsrv_service);
314         dreplsrv_ridalloc_check_rid_pool(service);
315 }