s4:drepl_out_helpers: don't look at the internals of 'struct rpc_request'
[ira/wip.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 "dsdb/samdb/samdb.h"
28 #include "smbd/service.h"
29 #include "dsdb/repl/drepl_service.h"
30 #include "param/param.h"
31
32
33 /*
34   create the RID manager source dsa structure
35  */
36 static WERROR drepl_create_rid_manager_source_dsa(struct dreplsrv_service *service,
37                                                   struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn)
38 {
39         struct dreplsrv_partition_source_dsa *sdsa;
40         struct ldb_context *ldb = service->samdb;
41         int ret;
42         WERROR werr;
43
44         sdsa = talloc_zero(service, struct dreplsrv_partition_source_dsa);
45         W_ERROR_HAVE_NO_MEMORY(sdsa);
46
47         sdsa->partition = talloc_zero(sdsa, struct dreplsrv_partition);
48         if (!sdsa->partition) {
49                 talloc_free(sdsa);
50                 return WERR_NOMEM;
51         }
52
53         sdsa->partition->dn = samdb_base_dn(ldb);
54         sdsa->partition->nc.dn = ldb_dn_alloc_linearized(sdsa->partition, rid_manager_dn);
55         ret = dsdb_find_guid_by_dn(ldb, rid_manager_dn, &sdsa->partition->nc.guid);
56         if (ret != LDB_SUCCESS) {
57                 DEBUG(0,(__location__ ": Failed to find GUID for %s\n",
58                          ldb_dn_get_linearized(rid_manager_dn)));
59                 talloc_free(sdsa);
60                 return WERR_DS_DRA_INTERNAL_ERROR;
61         }
62
63         sdsa->repsFrom1 = &sdsa->_repsFromBlob.ctr.ctr1;
64         ret = dsdb_find_guid_attr_by_dn(ldb, fsmo_role_dn, "objectGUID", &sdsa->repsFrom1->source_dsa_obj_guid);
65         if (ret != LDB_SUCCESS) {
66                 DEBUG(0,(__location__ ": Failed to find objectGUID for %s\n",
67                          ldb_dn_get_linearized(fsmo_role_dn)));
68                 talloc_free(sdsa);
69                 return WERR_DS_DRA_INTERNAL_ERROR;
70         }
71
72         sdsa->repsFrom1->other_info = talloc_zero(sdsa, struct repsFromTo1OtherInfo);
73         if (!sdsa->repsFrom1->other_info) {
74                 talloc_free(sdsa);
75                 return WERR_NOMEM;
76         }
77
78         sdsa->repsFrom1->other_info->dns_name =
79                 talloc_asprintf(sdsa->repsFrom1->other_info, "%s._msdcs.%s",
80                                 GUID_string(sdsa->repsFrom1->other_info, &sdsa->repsFrom1->source_dsa_obj_guid),
81                                 lp_dnsdomain(service->task->lp_ctx));
82         if (!sdsa->repsFrom1->other_info->dns_name) {
83                 talloc_free(sdsa);
84                 return WERR_NOMEM;
85         }
86
87
88         werr = dreplsrv_out_connection_attach(service, sdsa->repsFrom1, &sdsa->conn);
89         if (!W_ERROR_IS_OK(werr)) {
90                 DEBUG(0,(__location__ ": Failed to attach to RID manager connection\n"));
91                 talloc_free(sdsa);
92                 return werr;
93         }
94
95         service->ridalloc.rid_manager_source_dsa = sdsa;
96         return WERR_OK;
97 }
98
99 /*
100   called when a rid allocation request has completed
101  */
102 static void drepl_new_rid_pool_callback(struct dreplsrv_service *service, WERROR werr)
103 {
104         if (!W_ERROR_IS_OK(werr)) {
105                 DEBUG(0,(__location__ ": RID Manager failed RID allocation - %s\n",
106                          win_errstr(werr)));
107         } else {
108                 DEBUG(3,(__location__ ": RID Manager completed RID allocation OK\n"));
109         }
110
111         /* don't keep the connection open to the RID Manager */
112         talloc_free(service->ridalloc.rid_manager_source_dsa);
113         service->ridalloc.rid_manager_source_dsa = NULL;
114
115         service->ridalloc.in_progress = false;
116 }
117
118 /*
119   schedule a getncchanges request to the RID Manager to ask for a new
120   set of RIDs using DRSUAPI_EXOP_FSMO_RID_ALLOC
121  */
122 static WERROR drepl_request_new_rid_pool(struct dreplsrv_service *service,
123                                          struct ldb_dn *rid_manager_dn, struct ldb_dn *fsmo_role_dn,
124                                          uint64_t alloc_pool)
125 {
126         WERROR werr;
127
128         if (service->ridalloc.rid_manager_source_dsa == NULL) {
129                 /* we need to establish a connection to the RID
130                    Manager */
131                 werr = drepl_create_rid_manager_source_dsa(service, rid_manager_dn, fsmo_role_dn);
132                 W_ERROR_NOT_OK_RETURN(werr);
133         }
134
135         service->ridalloc.in_progress = true;
136
137         werr = dreplsrv_schedule_partition_pull_source(service, service->ridalloc.rid_manager_source_dsa,
138                                                        DRSUAPI_EXOP_FSMO_RID_ALLOC, alloc_pool,
139                                                        drepl_new_rid_pool_callback);
140         return werr;
141 }
142
143
144 /*
145   see if we are on the last pool we have
146  */
147 static int drepl_ridalloc_pool_exhausted(struct ldb_context *ldb, bool *exhausted, uint64_t *alloc_pool)
148 {
149         struct ldb_dn *server_dn, *machine_dn, *rid_set_dn;
150         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
151         uint64_t prev_alloc_pool;
152         const char *attrs[] = { "rIDPreviousAllocationPool", "rIDAllocationPool", NULL };
153         int ret;
154         struct ldb_result *res;
155
156         server_dn = ldb_dn_get_parent(tmp_ctx, samdb_ntds_settings_dn(ldb));
157         if (!server_dn) {
158                 talloc_free(tmp_ctx);
159                 return LDB_ERR_OPERATIONS_ERROR;
160         }
161
162         ret = samdb_reference_dn(ldb, tmp_ctx, server_dn, "serverReference", &machine_dn);
163         if (ret != LDB_SUCCESS) {
164                 DEBUG(0,(__location__ ": Failed to find serverReference in %s - %s",
165                          ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
166                 talloc_free(tmp_ctx);
167                 return ret;
168         }
169
170         ret = samdb_reference_dn(ldb, tmp_ctx, machine_dn, "rIDSetReferences", &rid_set_dn);
171         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
172                 *exhausted = true;
173                 talloc_free(tmp_ctx);
174                 return LDB_SUCCESS;
175         }
176         if (ret != LDB_SUCCESS) {
177                 DEBUG(0,(__location__ ": Failed to find rIDSetReferences in %s - %s",
178                          ldb_dn_get_linearized(machine_dn), ldb_errstring(ldb)));
179                 talloc_free(tmp_ctx);
180                 return ret;
181         }
182
183         ret = ldb_search(ldb, tmp_ctx, &res, rid_set_dn, LDB_SCOPE_BASE, attrs, NULL);
184         if (ret != LDB_SUCCESS) {
185                 DEBUG(0,(__location__ ": Failed to load RID Set attrs from %s - %s",
186                          ldb_dn_get_linearized(rid_set_dn), ldb_errstring(ldb)));
187                 talloc_free(tmp_ctx);
188                 return ret;
189         }
190
191         *alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDAllocationPool", 0);
192         prev_alloc_pool = ldb_msg_find_attr_as_uint64(res->msgs[0], "rIDPreviousAllocationPool", 0);
193
194         if (*alloc_pool != prev_alloc_pool) {
195                 *exhausted = false;
196         } else {
197                 *exhausted = true;
198         }
199
200         talloc_free(tmp_ctx);
201         return LDB_SUCCESS;
202 }
203
204
205 /*
206   see if we are low on RIDs in the RID Set rIDAllocationPool. If we
207   are, then schedule a replication call with DRSUAPI_EXOP_FSMO_RID_ALLOC
208   to the RID Manager
209  */
210 WERROR dreplsrv_ridalloc_check_rid_pool(struct dreplsrv_service *service)
211 {
212         struct ldb_dn *rid_manager_dn, *fsmo_role_dn;
213         TALLOC_CTX *tmp_ctx = talloc_new(service);
214         struct ldb_context *ldb = service->samdb;
215         bool exhausted;
216         WERROR werr;
217         int ret;
218         uint64_t alloc_pool;
219
220         if (service->ridalloc.in_progress) {
221                 talloc_free(tmp_ctx);
222                 return WERR_OK;
223         }
224
225         /*
226           steps:
227             - find who the RID Manager is
228             - if we are the RID Manager then nothing to do
229             - find our RID Set object
230             - load rIDAllocationPool and rIDPreviousAllocationPool
231             - if rIDAllocationPool != rIDPreviousAllocationPool then
232               nothing to do
233             - schedule a getncchanges with DRSUAPI_EXOP_FSMO_RID_ALLOC
234               to the RID Manager
235          */
236
237         /* work out who is the RID Manager */
238         ret = samdb_rid_manager_dn(ldb, tmp_ctx, &rid_manager_dn);
239         if (ret != LDB_SUCCESS) {
240                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
241                 talloc_free(tmp_ctx);
242                 return WERR_DS_DRA_INTERNAL_ERROR;
243         }
244
245         /* find the DN of the RID Manager */
246         ret = samdb_reference_dn(ldb, tmp_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
247         if (ret != LDB_SUCCESS) {
248                 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
249                          ldb_errstring(ldb)));
250                 talloc_free(tmp_ctx);
251                 return WERR_DS_DRA_INTERNAL_ERROR;
252         }
253
254         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) == 0) {
255                 /* we are the RID Manager - no need to do a
256                    DRSUAPI_EXOP_FSMO_RID_ALLOC */
257                 talloc_free(tmp_ctx);
258                 return WERR_OK;
259         }
260
261         ret = drepl_ridalloc_pool_exhausted(ldb, &exhausted, &alloc_pool);
262         if (ret != LDB_SUCCESS) {
263                 talloc_free(tmp_ctx);
264                 return WERR_DS_DRA_INTERNAL_ERROR;
265         }
266
267         DEBUG(2,(__location__ ": Requesting more RIDs from RID Manager\n"));
268
269         werr = drepl_request_new_rid_pool(service, rid_manager_dn, fsmo_role_dn, alloc_pool);
270         talloc_free(tmp_ctx);
271         return werr;
272 }
273
274 /* called by the samldb ldb module to tell us to ask for a new RID
275    pool */
276 void dreplsrv_allocate_rid(struct messaging_context *msg, void *private_data,
277                            uint32_t msg_type,
278                            struct server_id server_id, DATA_BLOB *data)
279 {
280         struct dreplsrv_service *service = talloc_get_type(private_data, struct dreplsrv_service);
281         dreplsrv_ridalloc_check_rid_pool(service);
282 }