2 Unix SMB/CIFS implementation.
4 implement the DRSUpdateRefs call
6 Copyright (C) Andrew Tridgell 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "librpc/gen_ndr/ndr_drsuapi.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "rpc_server/common/common.h"
26 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "lib/ldb/include/ldb_errors.h"
29 #include "param/param.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "auth/auth.h"
34 load the repsTo structure for a given partition GUID
36 static WERROR uref_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct GUID *guid,
40 const char *attrs[] = { "repsTo", NULL };
41 struct ldb_result *res;
42 const struct ldb_val *v;
43 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
45 if (dsdb_find_dn_by_guid(sam_ctx, tmp_ctx, GUID_string(tmp_ctx, guid), &dn) != LDB_SUCCESS) {
46 DEBUG(0,("drsuapi_addref: failed to find partition with GUID %s\n",
47 GUID_string(tmp_ctx, guid)));
49 return WERR_DS_DRA_BAD_NC;
52 /* TODO: possibly check in the rootDSE to see that this DN is
53 * one of our partition roots */
55 if (ldb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, NULL) != LDB_SUCCESS) {
56 DEBUG(0,("drsuapi_addref: failed to read partition object\n"));
58 return WERR_DS_DRA_INTERNAL_ERROR;
61 v = ldb_msg_find_ldb_val(res->msgs[0], "repsTo");
63 /* treat as empty empty */
65 reps->version = REPSTO_VERSION1;
67 enum ndr_err_code ndr_err;
68 ndr_err = ndr_pull_struct_blob(v, mem_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
70 (ndr_pull_flags_fn_t)ndr_pull_repsTo);
71 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
73 return WERR_DS_DRA_INTERNAL_ERROR;
83 save the repsTo structure for a given partition GUID
85 static WERROR uref_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct GUID *guid,
90 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
91 enum ndr_err_code ndr_err;
92 struct ldb_message *msg;
93 struct ldb_message_element *el;
95 if (dsdb_find_dn_by_guid(sam_ctx, tmp_ctx, GUID_string(tmp_ctx, guid), &dn) != LDB_SUCCESS) {
96 DEBUG(0,("drsuapi_addref: failed to find partition with GUID %s\n",
97 GUID_string(tmp_ctx, guid)));
99 return WERR_DS_DRA_BAD_NC;
102 ndr_err = ndr_push_struct_blob(&v, tmp_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
104 (ndr_push_flags_fn_t)ndr_push_repsTo);
105 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
109 msg = ldb_msg_new(tmp_ctx);
111 if (ldb_msg_add_empty(msg, "repsTo", LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
117 if (ldb_modify(sam_ctx, msg) != LDB_SUCCESS) {
118 DEBUG(0,("Failed to store repsTo - %s\n", ldb_errstring(sam_ctx)));
122 talloc_free(tmp_ctx);
127 talloc_free(tmp_ctx);
128 return WERR_DS_DRA_INTERNAL_ERROR;
132 add a replication destination for a given partition GUID
134 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
135 struct GUID *guid, struct repsToDest *dest)
139 struct repsTov1 *rv1;
141 werr = uref_loadreps(sam_ctx, mem_ctx, guid, &reps);
142 if (!W_ERROR_IS_OK(werr)) {
146 if (reps.version != REPSTO_VERSION1) {
147 DEBUG(0,("Wrong version number %u on disk\n",
149 return WERR_DS_DRA_INTERNAL_ERROR;
153 rv1->reps = talloc_realloc(mem_ctx, rv1->reps, struct repsToDest, rv1->count+1);
154 if (rv1->reps == NULL) {
155 return WERR_DS_DRA_INTERNAL_ERROR;
157 rv1->reps[rv1->count] = *dest;
160 werr = uref_savereps(sam_ctx, mem_ctx, guid, &reps);
161 if (!W_ERROR_IS_OK(werr)) {
169 delete a replication destination for a given partition GUID
171 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
172 struct GUID *guid, struct GUID *dest_guid)
176 struct repsTov1 *rv1;
179 werr = uref_loadreps(sam_ctx, mem_ctx, guid, &reps);
180 if (!W_ERROR_IS_OK(werr)) {
184 if (reps.version != REPSTO_VERSION1) {
185 DEBUG(0,("Wrong version number %u on disk\n", reps.version));
186 return WERR_DS_DRA_INTERNAL_ERROR;
191 for (i=0; i<rv1->count; i++) {
192 if (GUID_compare(dest_guid, &rv1->reps[i].dest_guid) == 0) {
193 if (i+1 < rv1->count) {
194 memmove(&rv1->reps[i], &rv1->reps[i+1], sizeof(rv1->reps[i])*(rv1->count-(i+1)));
200 werr = uref_savereps(sam_ctx, mem_ctx, guid, &reps);
201 if (!W_ERROR_IS_OK(werr)) {
209 drsuapi_DsReplicaUpdateRefs
211 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
212 struct drsuapi_DsReplicaUpdateRefs *r)
214 struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
215 struct ldb_context *sam_ctx;
218 if (r->in.level != 1) {
219 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
220 return WERR_DS_DRA_INVALID_PARAMETER;
223 req = &r->in.req.req1;
224 DEBUG(4,("DrReplicUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
225 req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
227 drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
229 /* TODO: We need to authenticate this operation pretty carefully */
230 sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx,
231 system_session(mem_ctx, dce_call->conn->dce_ctx->lp_ctx));
233 return WERR_DS_DRA_INTERNAL_ERROR;
236 if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
237 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
238 return WERR_DS_DRA_INTERNAL_ERROR;
241 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
242 werr = uref_del_dest(sam_ctx, mem_ctx, &req->naming_context->guid, &req->dest_dsa_guid);
243 if (!W_ERROR_IS_OK(werr)) {
244 DEBUG(0,("Failed to delete repsTo for %s\n",
245 GUID_string(dce_call, &req->dest_dsa_guid)));
250 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
251 struct repsToDest dest;
253 dest.dest_dsa_dns_name = req->dest_dsa_dns_name;
254 dest.dest_guid = req->dest_dsa_guid;
255 dest.options = req->options;
257 werr = uref_add_dest(sam_ctx, mem_ctx, &req->naming_context->guid, &dest);
258 if (!W_ERROR_IS_OK(werr)) {
259 DEBUG(0,("Failed to delete repsTo for %s\n",
260 GUID_string(dce_call, &dest.dest_guid)));
265 if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
266 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
267 return WERR_DS_DRA_INTERNAL_ERROR;
270 talloc_free(sam_ctx);
274 ldb_transaction_cancel(sam_ctx);
275 talloc_free(sam_ctx);