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 "dsdb/samdb/samdb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "param/param.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "auth/auth.h"
31 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
35 struct repsFromToBlob *r;
39 add a replication destination for a given partition GUID
41 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
42 struct ldb_dn *dn, struct repsFromTo1 *dest)
47 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
48 if (!W_ERROR_IS_OK(werr)) {
52 reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
54 return WERR_DS_DRA_INTERNAL_ERROR;
56 ZERO_STRUCT(reps.r[reps.count]);
57 reps.r[reps.count].version = 1;
58 reps.r[reps.count].ctr.ctr1 = *dest;
61 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
62 if (!W_ERROR_IS_OK(werr)) {
70 delete a replication destination for a given partition GUID
72 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
73 struct ldb_dn *dn, struct GUID *dest_guid)
79 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
80 if (!W_ERROR_IS_OK(werr)) {
84 for (i=0; i<reps.count; i++) {
85 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
86 if (i+1 < reps.count) {
87 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
93 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
94 if (!W_ERROR_IS_OK(werr)) {
102 drsuapi_DsReplicaUpdateRefs
104 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
105 struct drsuapi_DsReplicaUpdateRefs *r)
107 struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
108 struct ldb_context *sam_ctx;
112 if (r->in.level != 1) {
113 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
114 return WERR_DS_DRA_INVALID_PARAMETER;
117 req = &r->in.req.req1;
118 DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
119 req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
121 drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
123 /* TODO: We need to authenticate this operation pretty carefully */
124 sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx,
125 system_session(mem_ctx, dce_call->conn->dce_ctx->lp_ctx));
127 return WERR_DS_DRA_INTERNAL_ERROR;
130 dn = ldb_dn_new(mem_ctx, sam_ctx, req->naming_context->dn);
132 talloc_free(sam_ctx);
133 return WERR_DS_INVALID_DN_SYNTAX;
136 if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
137 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
138 talloc_free(sam_ctx);
139 return WERR_DS_DRA_INTERNAL_ERROR;
142 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
143 werr = uref_del_dest(sam_ctx, mem_ctx, dn, &req->dest_dsa_guid);
144 if (!W_ERROR_IS_OK(werr)) {
145 DEBUG(0,("Failed to delete repsTo for %s\n",
146 GUID_string(dce_call, &req->dest_dsa_guid)));
151 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
152 struct repsFromTo1 dest;
153 struct repsFromTo1OtherInfo oi;
158 oi.dns_name = req->dest_dsa_dns_name;
159 dest.other_info = &oi;
160 dest.source_dsa_obj_guid = req->dest_dsa_guid;
161 dest.replica_flags = req->options;
163 werr = uref_add_dest(sam_ctx, mem_ctx, dn, &dest);
164 if (!W_ERROR_IS_OK(werr)) {
165 DEBUG(0,("Failed to delete repsTo for %s\n",
166 GUID_string(dce_call, &dest.source_dsa_obj_guid)));
171 if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
172 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
173 return WERR_DS_DRA_INTERNAL_ERROR;
176 talloc_free(sam_ctx);
180 ldb_transaction_cancel(sam_ctx);
181 talloc_free(sam_ctx);