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