s4-drs: implement more of DsUpdateRefs
[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 "auth/auth.h"
26 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
27 #include "libcli/security/security.h"
28
29 struct repsTo {
30         uint32_t count;
31         struct repsFromToBlob *r;
32 };
33
34 /*
35   add a replication destination for a given partition GUID
36  */
37 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
38                             struct ldb_dn *dn, struct repsFromTo1 *dest, 
39                             uint32_t options)
40 {
41         struct repsTo reps;
42         WERROR werr;
43         int i;
44
45         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
46         if (!W_ERROR_IS_OK(werr)) {
47                 return werr;
48         }
49
50         for (i=0; i<reps.count; i++) {
51                 if (GUID_compare(&dest->source_dsa_obj_guid, 
52                                  &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
53                         if (options & DRSUAPI_DS_REPLICA_UPDATE_GETCHG_CHECK) {
54                                 return WERR_OK;
55                         } else {
56                                 return WERR_DS_DRA_REF_ALREADY_EXISTS;
57                         }
58                 }
59         }
60
61         reps.r = talloc_realloc(mem_ctx, reps.r, struct repsFromToBlob, reps.count+1);
62         if (reps.r == NULL) {
63                 return WERR_DS_DRA_INTERNAL_ERROR;
64         }
65         ZERO_STRUCT(reps.r[reps.count]);
66         reps.r[reps.count].version = 1;
67         reps.r[reps.count].ctr.ctr1 = *dest;
68         reps.count++;
69
70         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
71         if (!W_ERROR_IS_OK(werr)) {
72                 return werr;
73         }
74
75         return WERR_OK; 
76 }
77
78 /*
79   delete a replication destination for a given partition GUID
80  */
81 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
82                             struct ldb_dn *dn, struct GUID *dest_guid, 
83                             uint32_t options)
84 {
85         struct repsTo reps;
86         WERROR werr;
87         int i;
88         bool found = false;
89
90         werr = dsdb_loadreps(sam_ctx, mem_ctx, dn, "repsTo", &reps.r, &reps.count);
91         if (!W_ERROR_IS_OK(werr)) {
92                 return werr;
93         }
94
95         for (i=0; i<reps.count; i++) {
96                 if (GUID_compare(dest_guid, &reps.r[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
97                         if (i+1 < reps.count) {
98                                 memmove(&reps.r[i], &reps.r[i+1], sizeof(reps.r[i])*(reps.count-(i+1)));
99                         }
100                         reps.count--;
101                         found = true;
102                 }
103         }
104
105         werr = dsdb_savereps(sam_ctx, mem_ctx, dn, "repsTo", reps.r, reps.count);
106         if (!W_ERROR_IS_OK(werr)) {
107                 return werr;
108         }
109
110         if (!found && !(options & DRSUAPI_DS_REPLICA_UPDATE_GETCHG_CHECK)) {
111                 return WERR_DS_DRA_REF_NOT_FOUND;
112         }
113
114         return WERR_OK; 
115 }
116
117 /* 
118   drsuapi_DsReplicaUpdateRefs - a non RPC version callable from getncchanges
119 */
120 WERROR drsuapi_UpdateRefs(struct drsuapi_bind_state *b_state, TALLOC_CTX *mem_ctx,
121                           struct drsuapi_DsReplicaUpdateRefsRequest1 *req)
122 {
123         WERROR werr;
124         struct ldb_dn *dn;
125
126         DEBUG(4,("DsReplicaUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
127                  req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
128                  req->options,
129                  drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
130
131         dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, req->naming_context->dn);
132         if (dn == NULL) {
133                 return WERR_DS_INVALID_DN_SYNTAX;
134         }
135
136         if (ldb_transaction_start(b_state->sam_ctx) != LDB_SUCCESS) {
137                 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
138                 return WERR_DS_DRA_INTERNAL_ERROR;              
139         }
140
141         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
142                 werr = uref_del_dest(b_state->sam_ctx, mem_ctx, dn, &req->dest_dsa_guid, req->options);
143                 if (!W_ERROR_IS_OK(werr)) {
144                         DEBUG(0,("Failed to delete repsTo for %s\n",
145                                  GUID_string(mem_ctx, &req->dest_dsa_guid)));
146                         goto failed;
147                 }
148         }
149
150         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
151                 struct repsFromTo1 dest;
152                 struct repsFromTo1OtherInfo oi;
153                 
154                 ZERO_STRUCT(dest);
155                 ZERO_STRUCT(oi);
156
157                 oi.dns_name = req->dest_dsa_dns_name;
158                 dest.other_info          = &oi;
159                 dest.source_dsa_obj_guid = req->dest_dsa_guid;
160                 dest.replica_flags       = req->options;
161
162                 werr = uref_add_dest(b_state->sam_ctx, mem_ctx, dn, &dest, req->options);
163                 if (!W_ERROR_IS_OK(werr)) {
164                         DEBUG(0,("Failed to add repsTo for %s\n",
165                                  GUID_string(mem_ctx, &dest.source_dsa_obj_guid)));
166                         goto failed;
167                 }
168         }
169
170         if (ldb_transaction_commit(b_state->sam_ctx) != LDB_SUCCESS) {
171                 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
172                 return WERR_DS_DRA_INTERNAL_ERROR;              
173         }
174
175         return WERR_OK;
176
177 failed:
178         ldb_transaction_cancel(b_state->sam_ctx);
179         return werr;
180 }
181
182 /* 
183   drsuapi_DsReplicaUpdateRefs
184 */
185 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
186                                           struct drsuapi_DsReplicaUpdateRefs *r)
187 {
188         struct dcesrv_handle *h;
189         struct drsuapi_bind_state *b_state;
190         struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
191         WERROR werr;
192
193         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
194         b_state = h->data;
195
196         werr = drs_security_level_check(dce_call, "DsReplicaUpdateRefs");
197         if (!W_ERROR_IS_OK(werr)) {
198                 return werr;
199         }
200
201         if (r->in.level != 1) {
202                 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
203                 return WERR_DS_DRA_INVALID_PARAMETER;
204         }
205
206         req = &r->in.req.req1;
207
208         return drsuapi_UpdateRefs(b_state, mem_ctx, req);
209 }
210
211