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