pyldb: raise an exception if we can't add the attribute
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / updaterefs.c
blob2d62718f01384ed83e5140d0fca5ff26eb594656
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 "rpc_server/dcerpc_server.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "libcli/security/security.h"
26 #include "libcli/security/session.h"
27 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
28 #include "auth/session.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
31 struct repsTo {
32 uint32_t count;
33 struct repsFromToBlob *r;
37 add a replication destination for a given partition GUID
39 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
40 struct ldb_dn *dn, struct repsFromTo1 *dest,
41 uint32_t options)
43 struct repsTo reps;
44 WERROR werr;
45 unsigned int i;
47 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
48 if (!W_ERROR_IS_OK(werr)) {
49 return werr;
52 for (i=0; i<reps.count; i++) {
53 if (GUID_compare(&dest->source_dsa_obj_guid,
54 &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
55 if (options & DRSUAPI_DRS_GETCHG_CHECK) {
56 return WERR_OK;
57 } else {
58 return WERR_DS_DRA_REF_ALREADY_EXISTS;
63 reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
64 if (reps.r == NULL) {
65 return WERR_DS_DRA_INTERNAL_ERROR;
67 ZERO_STRUCT(reps.r[reps.count]);
68 reps.r[reps.count].version = 1;
69 reps.r[reps.count].ctr.ctr1 = *dest;
70 /* add the GCSPN flag if the client asked for it */
71 reps.r[reps.count].ctr.ctr1.replica_flags |= (options & DRSUAPI_DRS_REF_GCSPN);
72 reps.count++;
74 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
75 if (!W_ERROR_IS_OK(werr)) {
76 return werr;
79 return WERR_OK;
83 delete a replication destination for a given partition GUID
85 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
86 struct ldb_dn *dn, struct GUID *dest_guid,
87 uint32_t options)
89 struct repsTo reps;
90 WERROR werr;
91 unsigned int i;
92 bool found = false;
94 werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
95 if (!W_ERROR_IS_OK(werr)) {
96 return werr;
99 for (i=0; i<reps.count; i++) {
100 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
101 if (i+1 < reps.count) {
102 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
104 reps.count--;
105 found = true;
109 werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
110 if (!W_ERROR_IS_OK(werr)) {
111 return werr;
114 if (!found &&
115 !(options & DRSUAPI_DRS_GETCHG_CHECK) &&
116 !(options & DRSUAPI_DRS_ADD_REF)) {
117 return WERR_DS_DRA_REF_NOT_FOUND;
120 return WERR_OK;
124 drsuapi_DsReplicaUpdateRefs - a non RPC version callable from getncchanges
126 WERROR drsuapi_UpdateRefs(struct drsuapi_bind_state *b_state, TALLOC_CTX *mem_ctx,
127 struct drsuapi_DsReplicaUpdateRefsRequest1 *req)
129 WERROR werr;
130 struct ldb_dn *dn;
131 struct ldb_context *sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
133 DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
134 req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
135 req->options,
136 drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
138 dn = ldb_dn_new(mem_ctx, sam_ctx, req->naming_context->dn);
139 if (dn == NULL) {
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: %s\n",
145 ldb_errstring(sam_ctx)));
146 return WERR_DS_DRA_INTERNAL_ERROR;
149 if (req->options & DRSUAPI_DRS_DEL_REF) {
150 werr = uref_del_dest(sam_ctx, mem_ctx, dn, &req->dest_dsa_guid, req->options);
151 if (!W_ERROR_IS_OK(werr)) {
152 DEBUG(0,("Failed to delete repsTo for %s: %s\n",
153 GUID_string(mem_ctx, &req->dest_dsa_guid),
154 win_errstr(werr)));
155 goto failed;
159 if (req->options & DRSUAPI_DRS_ADD_REF) {
160 struct repsFromTo1 dest;
161 struct repsFromTo1OtherInfo oi;
163 ZERO_STRUCT(dest);
164 ZERO_STRUCT(oi);
166 oi.dns_name = req->dest_dsa_dns_name;
167 dest.other_info = &oi;
168 dest.source_dsa_obj_guid = req->dest_dsa_guid;
169 dest.replica_flags = req->options;
171 werr = uref_add_dest(sam_ctx, mem_ctx, dn, &dest, req->options);
172 if (!W_ERROR_IS_OK(werr)) {
173 DEBUG(0,("Failed to add repsTo for %s: %s\n",
174 GUID_string(mem_ctx, &dest.source_dsa_obj_guid),
175 win_errstr(werr)));
176 goto failed;
180 if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
181 DEBUG(0,(__location__ ": Failed to commit transaction on samdb: %s\n",
182 ldb_errstring(sam_ctx)));
183 return WERR_DS_DRA_INTERNAL_ERROR;
186 return WERR_OK;
188 failed:
189 ldb_transaction_cancel(sam_ctx);
190 return werr;
194 drsuapi_DsReplicaUpdateRefs
196 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
197 struct drsuapi_DsReplicaUpdateRefs *r)
199 struct dcesrv_handle *h;
200 struct drsuapi_bind_state *b_state;
201 struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
202 WERROR werr;
203 int ret;
204 enum security_user_level security_level;
206 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
207 b_state = h->data;
209 if (r->in.level != 1) {
210 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
211 return WERR_DS_DRA_INVALID_PARAMETER;
213 req = &r->in.req.req1;
214 werr = drs_security_access_check(b_state->sam_ctx,
215 mem_ctx,
216 dce_call->conn->auth_state.session_info->security_token,
217 req->naming_context,
218 GUID_DRS_MANAGE_TOPOLOGY);
220 if (!W_ERROR_IS_OK(werr)) {
221 return werr;
224 security_level = security_session_user_level(dce_call->conn->auth_state.session_info, NULL);
225 if (security_level < SECURITY_ADMINISTRATOR) {
226 /* check that they are using an DSA objectGUID that they own */
227 ret = dsdb_validate_dsa_guid(b_state->sam_ctx,
228 &req->dest_dsa_guid,
229 &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX]);
230 if (ret != LDB_SUCCESS) {
231 DEBUG(0,(__location__ ": Refusing DsReplicaUpdateRefs for sid %s with GUID %s\n",
232 dom_sid_string(mem_ctx,
233 &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX]),
234 GUID_string(mem_ctx, &req->dest_dsa_guid)));
235 return WERR_DS_DRA_ACCESS_DENIED;
239 werr = drsuapi_UpdateRefs(b_state, mem_ctx, req);
241 #if 0
242 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsReplicaUpdateRefs, NDR_BOTH, r);
243 #endif
245 return werr;