s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source4 / rpc_server / drsuapi / updaterefs.c
blob34ff0caa14b197b7d32cbbebff26fbbdc719d386
1 /*
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/>.
22 #include "includes.h"
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"
32 #include "libcli/security/security.h"
34 struct repsTo {
35 uint32_t count;
36 struct repsFromToBlob *r;
40 add a replication destination for a given partition GUID
42 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
43 struct ldb_dn *dn, struct repsFromTo1 *dest)
45 struct repsTo reps;
46 WERROR werr;
48 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
49 if (!W_ERROR_IS_OK(werr)) {
50 return werr;
53 reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
54 if (reps.r == NULL) {
55 return WERR_DS_DRA_INTERNAL_ERROR;
57 ZERO_STRUCT(reps.r[reps.count]);
58 reps.r[reps.count].version = 1;
59 reps.r[reps.count].ctr.ctr1 = *dest;
60 reps.count++;
62 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
63 if (!W_ERROR_IS_OK(werr)) {
64 return werr;
67 return WERR_OK;
71 delete a replication destination for a given partition GUID
73 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
74 struct ldb_dn *dn, struct GUID *dest_guid)
76 struct repsTo reps;
77 WERROR werr;
78 int i;
80 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
81 if (!W_ERROR_IS_OK(werr)) {
82 return werr;
85 for (i=0; i<reps.count; i++) {
86 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
87 if (i+1 < reps.count) {
88 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
90 reps.count--;
94 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
95 if (!W_ERROR_IS_OK(werr)) {
96 return werr;
99 return WERR_OK;
103 drsuapi_DsReplicaUpdateRefs
105 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
106 struct drsuapi_DsReplicaUpdateRefs *r)
108 struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
109 struct ldb_context *sam_ctx;
110 WERROR werr;
111 struct ldb_dn *dn;
113 if (security_session_user_level(dce_call->conn->auth_state.session_info) <
114 SECURITY_DOMAIN_CONTROLLER) {
115 DEBUG(0,("DsReplicaUpdateRefs refused for security token\n"));
116 return WERR_DS_DRA_ACCESS_DENIED;
119 if (r->in.level != 1) {
120 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
121 return WERR_DS_DRA_INVALID_PARAMETER;
124 req = &r->in.req.req1;
125 DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
126 req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
127 req->options,
128 drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
130 /* TODO: We need to authenticate this operation pretty carefully */
131 sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx,
132 system_session(mem_ctx, dce_call->conn->dce_ctx->lp_ctx));
133 if (!sam_ctx) {
134 return WERR_DS_DRA_INTERNAL_ERROR;
137 dn = ldb_dn_new(mem_ctx, sam_ctx, req->naming_context->dn);
138 if (dn == NULL) {
139 talloc_free(sam_ctx);
140 return WERR_DS_INVALID_DN_SYNTAX;
143 if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
144 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
145 talloc_free(sam_ctx);
146 return WERR_DS_DRA_INTERNAL_ERROR;
149 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
150 werr = uref_del_dest(sam_ctx, mem_ctx, dn, &req->dest_dsa_guid);
151 if (!W_ERROR_IS_OK(werr)) {
152 DEBUG(0,("Failed to delete repsTo for %s\n",
153 GUID_string(dce_call, &req->dest_dsa_guid)));
154 goto failed;
158 if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
159 struct repsFromTo1 dest;
160 struct repsFromTo1OtherInfo oi;
162 ZERO_STRUCT(dest);
163 ZERO_STRUCT(oi);
165 oi.dns_name = req->dest_dsa_dns_name;
166 dest.other_info = &oi;
167 dest.source_dsa_obj_guid = req->dest_dsa_guid;
168 dest.replica_flags = req->options;
170 werr = uref_add_dest(sam_ctx, mem_ctx, dn, &dest);
171 if (!W_ERROR_IS_OK(werr)) {
172 DEBUG(0,("Failed to delete repsTo for %s\n",
173 GUID_string(dce_call, &dest.source_dsa_obj_guid)));
174 goto failed;
178 if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
179 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
180 return WERR_DS_DRA_INTERNAL_ERROR;
183 talloc_free(sam_ctx);
184 return WERR_OK;
186 failed:
187 ldb_transaction_cancel(sam_ctx);
188 talloc_free(sam_ctx);
189 return werr;