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"
32 #include "libcli/security/security.h"
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
)
48 werr
= dsdb_loadreps(sam_ctx
, mem_ctx
, dn
, "repsTo", &reps
.r
, &reps
.count
);
49 if (!W_ERROR_IS_OK(werr
)) {
53 reps
.r
= talloc_realloc(mem_ctx
, reps
.r
, struct repsFromToBlob
, reps
.count
+1);
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
;
62 werr
= dsdb_savereps(sam_ctx
, mem_ctx
, dn
, "repsTo", reps
.r
, reps
.count
);
63 if (!W_ERROR_IS_OK(werr
)) {
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
)
80 werr
= dsdb_loadreps(sam_ctx
, mem_ctx
, dn
, "repsTo", &reps
.r
, &reps
.count
);
81 if (!W_ERROR_IS_OK(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)));
94 werr
= dsdb_savereps(sam_ctx
, mem_ctx
, dn
, "repsTo", reps
.r
, reps
.count
);
95 if (!W_ERROR_IS_OK(werr
)) {
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
;
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
),
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
));
134 return WERR_DS_DRA_INTERNAL_ERROR
;
137 dn
= ldb_dn_new(mem_ctx
, sam_ctx
, req
->naming_context
->dn
);
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
)));
158 if (req
->options
& DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE
) {
159 struct repsFromTo1 dest
;
160 struct repsFromTo1OtherInfo 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
)));
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
);
187 ldb_transaction_cancel(sam_ctx
);
188 talloc_free(sam_ctx
);