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 "rpc_server/dcerpc_server.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
27 #include "libcli/security/security.h"
31 struct repsFromToBlob *r;
35 add a replication destination for a given partition GUID
37 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
38 struct ldb_dn *dn, struct repsFromTo1 *dest)
43 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
44 if (!W_ERROR_IS_OK(werr)) {
48 reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
50 return WERR_DS_DRA_INTERNAL_ERROR;
52 ZERO_STRUCT(reps.r[reps.count]);
53 reps.r[reps.count].version = 1;
54 reps.r[reps.count].ctr.ctr1 = *dest;
57 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
58 if (!W_ERROR_IS_OK(werr)) {
66 delete a replication destination for a given partition GUID
68 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
69 struct ldb_dn *dn, struct GUID *dest_guid)
75 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
76 if (!W_ERROR_IS_OK(werr)) {
80 for (i=0; i<reps.count; i++) {
81 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
82 if (i+1 < reps.count) {
83 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
89 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
90 if (!W_ERROR_IS_OK(werr)) {
98 drsuapi_DsReplicaUpdateRefs
100 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
101 struct drsuapi_DsReplicaUpdateRefs *r)
103 struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
104 struct ldb_context *sam_ctx;
108 if (security_session_user_level(dce_call->conn->auth_state.session_info) <
109 SECURITY_DOMAIN_CONTROLLER) {
110 DEBUG(0,("DsReplicaUpdateRefs refused for security token\n"));
111 return WERR_DS_DRA_ACCESS_DENIED;
114 if (r->in.level != 1) {
115 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
116 return WERR_DS_DRA_INVALID_PARAMETER;
119 req = &r->in.req.req1;
120 DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
121 req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
123 drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
125 /* TODO: We need to authenticate this operation pretty carefully */
126 sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx,
127 system_session(mem_ctx, dce_call->conn->dce_ctx->lp_ctx));
129 return WERR_DS_DRA_INTERNAL_ERROR;
132 dn = ldb_dn_new(mem_ctx, sam_ctx, req->naming_context->dn);
134 talloc_free(sam_ctx);
135 return WERR_DS_INVALID_DN_SYNTAX;
138 if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
139 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
140 talloc_free(sam_ctx);
141 return WERR_DS_DRA_INTERNAL_ERROR;
144 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
145 werr = uref_del_dest(sam_ctx, mem_ctx, dn, &req->dest_dsa_guid);
146 if (!W_ERROR_IS_OK(werr)) {
147 DEBUG(0,("Failed to delete repsTo for %s\n",
148 GUID_string(dce_call, &req->dest_dsa_guid)));
153 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
154 struct repsFromTo1 dest;
155 struct repsFromTo1OtherInfo oi;
160 oi.dns_name = req->dest_dsa_dns_name;
161 dest.other_info = &oi;
162 dest.source_dsa_obj_guid = req->dest_dsa_guid;
163 dest.replica_flags = req->options;
165 werr = uref_add_dest(sam_ctx, mem_ctx, dn, &dest);
166 if (!W_ERROR_IS_OK(werr)) {
167 DEBUG(0,("Failed to delete repsTo for %s\n",
168 GUID_string(dce_call, &dest.source_dsa_obj_guid)));
173 if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
174 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
175 return WERR_DS_DRA_INTERNAL_ERROR;
178 talloc_free(sam_ctx);
182 ldb_transaction_cancel(sam_ctx);
183 talloc_free(sam_ctx);