s4-repl: added min_usn to extended replication call
[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   called when a rid allocation request has completed
35  */
36 static void drepl_new_rid_pool_callback(struct dreplsrv_service *service,
37                                         WERROR werr,
38                                         enum drsuapi_DsExtendedError ext_err,
39                                         void *cb_data)
40 {
41         if (!W_ERROR_IS_OK(werr)) {
42                 DEBUG(0,(__location__ ": RID Manager failed RID allocation - %s - extended_ret[0x%X]\n",
43                          win_errstr(werr), ext_err));
44         } else {
45                 DEBUG(3,(__location__ ": RID Manager completed RID allocation OK\n"));
46         }
47
48         service->rid_alloc_in_progress = false;
49 }
50
51 /*
52   schedule a getncchanges request to the RID Manager to ask for a new
53   set of RIDs using DRSUAPI_EXOP_FSMO_RID_ALLOC
54  */
55 static WERROR drepl_request_new_rid_pool(struct dreplsrv_service *service,
56                                          struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn,
57                                          uint64_t alloc_pool)
58 {
59         WERROR werr = drepl_request_extended_op(service,
60                                                 rid_manager_dn,
61                                                 fsmo_role_dn,
62                                                 DRSUAPI_EXOP_FSMO_RID_ALLOC,
63                                                 alloc_pool,
64                                                 0,
65                                                 drepl_new_rid_pool_callback, NULL);
66         if (W_ERROR_IS_OK(werr)) {
67                 service->rid_alloc_in_progress = true;
68         }
69         return werr;
70 }
71
72
73 /*
74   see if we are on the last pool we have
75  */
76 static int drepl_ridalloc_pool_exhausted(struct ldb_context *ldb,
77                                          bool *exhausted,
78                                          uint64_t *_alloc_pool)
79 {
80         struct ldb_dn *server_dn, *machine_dn, *rid_set_dn;
81         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
82         uint64_t alloc_pool;
83         uint64_t prev_pool;
84         uint32_t prev_pool_lo, prev_pool_hi;
85         uint32_t next_rid;
86         static const char * const attrs[] = {
87                 "rIDAllocationPool",
88                 "rIDPreviousAllocationPool",
89                 "rIDNextRid",
90                 NULL
91         };
92         int ret;
93         struct ldb_result *res;
94
95         *exhausted = false;
96         *_alloc_pool = UINT64_MAX;
97
98         server_dn = ldb_dn_get_parent(tmp_ctx, samdb_ntds_settings_dn(ldb));
99         if (!server_dn) {
100                 talloc_free(tmp_ctx);
101                 return ldb_operr(ldb);
102         }
103
104         ret = samdb_reference_dn(ldb, tmp_ctx, server_dn, "serverReference", &machine_dn);
105         if (ret != LDB_SUCCESS) {
106                 DEBUG(0,(__location__ ": Failed to find serverReference in %s - %s",
107                          ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
108                 talloc_free(tmp_ctx);
109                 return ret;
110         }
111
112         ret = samdb_reference_dn(ldb, tmp_ctx, machine_dn, "rIDSetReferences", &rid_set_dn);
113         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
114                 *exhausted = true;
115                 *_alloc_pool = 0;
116                 talloc_free(tmp_ctx);
117                 return LDB_SUCCESS;
118         }
119         if (ret != LDB_SUCCESS) {
120                 DEBUG(0,(__location__ ": Failed to find rIDSetReferences in %s - %s",
121                          ldb_dn_get_linearized(machine_dn), ldb_errstring(ldb)));
122                 talloc_free(tmp_ctx);
123                 return ret;
124         }
125
126         ret = ldb_search(ldb, tmp_ctx, &res, rid_set_dn, LDB_SCOPE_BASE, attrs, NULL);
127         if (ret != LDB_SUCCESS) {
128                 DEBUG(0,(__location__ ": Failed to load RID Set attrs from %s - %s",
129                          ldb_dn_get_linearized(rid_set_dn), ldb_errstring(ldb)));
130                 talloc_free(tmp_ctx);
131                 return ret;
132         }
133
134         alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDAllocationPool", 0);
135         prev_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDPreviousAllocationPool", 0);
136         prev_pool_lo = prev_pool & 0xFFFFFFFF;
137         prev_pool_hi = prev_pool >> 32;
138         next_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "rIDNextRid", 0);
139
140         if (alloc_pool != prev_pool) {
141                 talloc_free(tmp_ctx);
142                 return LDB_SUCCESS;
143         }
144
145         if (next_rid < (prev_pool_hi + prev_pool_lo)/2) {
146                 talloc_free(tmp_ctx);
147                 return LDB_SUCCESS;
148         }
149
150         *exhausted = true;
151         *_alloc_pool = alloc_pool;
152         talloc_free(tmp_ctx);
153         return LDB_SUCCESS;
154 }
155
156
157 /*
158   see if we are low on RIDs in the RID Set rIDAllocationPool. If we
159   are, then schedule a replication call with DRSUAPI_EXOP_FSMO_RID_ALLOC
160   to the RID Manager
161  */
162 WERROR dreplsrv_ridalloc_check_rid_pool(struct dreplsrv_service *service)
163 {
164         struct ldb_dn *rid_manager_dn, *fsmo_role_dn;
165         TALLOC_CTX *tmp_ctx = talloc_new(service);
166         struct ldb_context *ldb = service->samdb;
167         bool exhausted;
168         WERROR werr;
169         int ret;
170         uint64_t alloc_pool;
171
172         if (service->rid_alloc_in_progress) {
173                 talloc_free(tmp_ctx);
174                 return WERR_OK;
175         }
176
177         /*
178           steps:
179             - find who the RID Manager is
180             - if we are the RID Manager then nothing to do
181             - find our RID Set object
182             - load rIDAllocationPool and rIDPreviousAllocationPool
183             - if rIDAllocationPool != rIDPreviousAllocationPool then
184               nothing to do
185             - schedule a getncchanges with DRSUAPI_EXOP_FSMO_RID_ALLOC
186               to the RID Manager
187          */
188
189         /* work out who is the RID Manager */
190         ret = samdb_rid_manager_dn(ldb, tmp_ctx, &rid_manager_dn);
191         if (ret != LDB_SUCCESS) {
192                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
193                 talloc_free(tmp_ctx);
194                 return WERR_DS_DRA_INTERNAL_ERROR;
195         }
196
197         /* find the DN of the RID Manager */
198         ret = samdb_reference_dn(ldb, tmp_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
199         if (ret != LDB_SUCCESS) {
200                 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
201                          ldb_errstring(ldb)));
202                 talloc_free(tmp_ctx);
203                 return WERR_DS_DRA_INTERNAL_ERROR;
204         }
205
206         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) == 0) {
207                 /* we are the RID Manager - no need to do a
208                    DRSUAPI_EXOP_FSMO_RID_ALLOC */
209                 talloc_free(tmp_ctx);
210                 return WERR_OK;
211         }
212
213         ret = drepl_ridalloc_pool_exhausted(ldb, &exhausted, &alloc_pool);
214         if (ret != LDB_SUCCESS) {
215                 talloc_free(tmp_ctx);
216                 return WERR_DS_DRA_INTERNAL_ERROR;
217         }
218
219         if (!exhausted) {
220                 /* don't need a new pool */
221                 talloc_free(tmp_ctx);
222                 return WERR_OK;
223         }
224
225         DEBUG(2,(__location__ ": Requesting more RIDs from RID Manager\n"));
226
227         werr = drepl_request_new_rid_pool(service, rid_manager_dn, fsmo_role_dn, alloc_pool);
228         talloc_free(tmp_ctx);
229         return werr;
230 }
231
232 /* called by the samldb ldb module to tell us to ask for a new RID
233    pool */
234 void dreplsrv_allocate_rid(struct messaging_context *msg, void *private_data,
235                            uint32_t msg_type,
236                            struct server_id server_id, DATA_BLOB *data)
237 {
238         struct dreplsrv_service *service = talloc_get_type(private_data, struct dreplsrv_service);
239         dreplsrv_ridalloc_check_rid_pool(service);
240 }