s4-drs: put the GCSPN flag into the repsTo if requested
[nivanova/samba-autobuild/.git] / source4 / rpc_server / drsuapi / updaterefs.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DRSUpdateRefs call
5
6    Copyright (C) Andrew Tridgell 2009
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
26 #include "libcli/security/security.h"
27 #include "auth/session.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29
30 struct repsTo {
31         uint32_t count;
32         struct repsFromToBlob *r;
33 };
34
35 /*
36   add a replication destination for a given partition GUID
37  */
38 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
39                             struct ldb_dn *dn, struct repsFromTo1 *dest, 
40                             uint32_t options)
41 {
42         struct repsTo reps;
43         WERROR werr;
44         unsigned int i;
45
46         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
47         if (!W_ERROR_IS_OK(werr)) {
48                 return werr;
49         }
50
51         for (i=0; i<reps.count; i++) {
52                 if (GUID_compare(&dest->source_dsa_obj_guid, 
53                                  &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
54                         if (options & DRSUAPI_DRS_GETCHG_CHECK) {
55                                 return WERR_OK;
56                         } else {
57                                 return WERR_DS_DRA_REF_ALREADY_EXISTS;
58                         }
59                 }
60         }
61
62         reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
63         if (reps.r == NULL) {
64                 return WERR_DS_DRA_INTERNAL_ERROR;
65         }
66         ZERO_STRUCT(reps.r[reps.count]);
67         reps.r[reps.count].version = 1;
68         reps.r[reps.count].ctr.ctr1 = *dest;
69         /* add the GCSPN flag if the client asked for it */
70         reps.r[reps.count].ctr.ctr1.replica_flags |= (options & DRSUAPI_DRS_REF_GCSPN);
71         reps.count++;
72
73         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
74         if (!W_ERROR_IS_OK(werr)) {
75                 return werr;
76         }
77
78         return WERR_OK; 
79 }
80
81 /*
82   delete a replication destination for a given partition GUID
83  */
84 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
85                             struct ldb_dn *dn, struct GUID *dest_guid, 
86                             uint32_t options)
87 {
88         struct repsTo reps;
89         WERROR werr;
90         unsigned int i;
91         bool found = false;
92
93         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
94         if (!W_ERROR_IS_OK(werr)) {
95                 return werr;
96         }
97
98         for (i=0; i<reps.count; i++) {
99                 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
100                         if (i+1 < reps.count) {
101                                 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
102                         }
103                         reps.count--;
104                         found = true;
105                 }
106         }
107
108         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
109         if (!W_ERROR_IS_OK(werr)) {
110                 return werr;
111         }
112
113         if (!found &&
114             !(options & DRSUAPI_DRS_GETCHG_CHECK) &&
115             !(options & DRSUAPI_DRS_ADD_REF)) {
116                 return WERR_DS_DRA_REF_NOT_FOUND;
117         }
118
119         return WERR_OK; 
120 }
121
122 /* 
123   drsuapi_DsReplicaUpdateRefs - a non RPC version callable from getncchanges
124 */
125 WERROR drsuapi_UpdateRefs(struct drsuapi_bind_state *b_state, TALLOC_CTX *mem_ctx,
126                           struct drsuapi_DsReplicaUpdateRefsRequest1 *req)
127 {
128         WERROR werr;
129         struct ldb_dn *dn;
130         struct ldb_context *sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
131
132         DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
133                  req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
134                  req->options,
135                  drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
136
137         dn = ldb_dn_new(mem_ctx, sam_ctx, req->naming_context->dn);
138         if (dn == NULL) {
139                 return WERR_DS_INVALID_DN_SYNTAX;
140         }
141
142         if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
143                 DEBUG(0,(__location__ ": Failed to start transaction on samdb: %s\n",
144                          ldb_errstring(sam_ctx)));
145                 return WERR_DS_DRA_INTERNAL_ERROR;              
146         }
147
148         if (req->options & DRSUAPI_DRS_DEL_REF) {
149                 werr = uref_del_dest(sam_ctx, mem_ctx, dn, &req->dest_dsa_guid, req->options);
150                 if (!W_ERROR_IS_OK(werr)) {
151                         DEBUG(0,("Failed to delete repsTo for %s: %s\n",
152                                  GUID_string(mem_ctx, &req->dest_dsa_guid),
153                                  win_errstr(werr)));
154                         goto failed;
155                 }
156         }
157
158         if (req->options & DRSUAPI_DRS_ADD_REF) {
159                 struct repsFromTo1 dest;
160                 struct repsFromTo1OtherInfo oi;
161                 
162                 ZERO_STRUCT(dest);
163                 ZERO_STRUCT(oi);
164
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;
169
170                 werr = uref_add_dest(sam_ctx, mem_ctx, dn, &dest, req->options);
171                 if (!W_ERROR_IS_OK(werr)) {
172                         DEBUG(0,("Failed to add repsTo for %s: %s\n",
173                                  GUID_string(mem_ctx, &dest.source_dsa_obj_guid),
174                                  win_errstr(werr)));
175                         goto failed;
176                 }
177         }
178
179         if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
180                 DEBUG(0,(__location__ ": Failed to commit transaction on samdb: %s\n",
181                          ldb_errstring(sam_ctx)));
182                 return WERR_DS_DRA_INTERNAL_ERROR;              
183         }
184
185         return WERR_OK;
186
187 failed:
188         ldb_transaction_cancel(sam_ctx);
189         return werr;
190 }
191
192 /* 
193   drsuapi_DsReplicaUpdateRefs
194 */
195 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
196                                           struct drsuapi_DsReplicaUpdateRefs *r)
197 {
198         struct dcesrv_handle *h;
199         struct drsuapi_bind_state *b_state;
200         struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
201         WERROR werr;
202         int ret;
203         enum security_user_level security_level;
204
205         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
206         b_state = h->data;
207
208         if (r->in.level != 1) {
209                 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
210                 return WERR_DS_DRA_INVALID_PARAMETER;
211         }
212         req = &r->in.req.req1;
213         werr = drs_security_access_check(b_state->sam_ctx,
214                                          mem_ctx,
215                                          dce_call->conn->auth_state.session_info->security_token,
216                                          req->naming_context,
217                                          GUID_DRS_MANAGE_TOPOLOGY);
218
219         if (!W_ERROR_IS_OK(werr)) {
220                 return werr;
221         }
222
223         security_level = security_session_user_level(dce_call->conn->auth_state.session_info, NULL);
224         if (security_level < SECURITY_ADMINISTRATOR) {
225                 /* check that they are using an DSA objectGUID that they own */
226                 ret = dsdb_validate_dsa_guid(b_state->sam_ctx,
227                                              &req->dest_dsa_guid,
228                                              &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX]);
229                 if (ret != LDB_SUCCESS) {
230                         DEBUG(0,(__location__ ": Refusing DsReplicaUpdateRefs for sid %s with GUID %s\n",
231                                  dom_sid_string(mem_ctx,
232                                                 &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX]),
233                                  GUID_string(mem_ctx, &req->dest_dsa_guid)));
234                         return WERR_DS_DRA_ACCESS_DENIED;
235                 }
236         }
237
238         werr = drsuapi_UpdateRefs(b_state, mem_ctx, req);
239
240 #if 0
241         NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsReplicaUpdateRefs, NDR_BOTH, r);
242 #endif
243
244         return werr;
245 }