s4: implemented server side of DSUpdateRefs call
[ira/wip.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 "librpc/gen_ndr/ndr_drsuapi.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "rpc_server/common/common.h"
26 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "lib/ldb/include/ldb_errors.h"
29 #include "param/param.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "auth/auth.h"
32
33 /*
34   load the repsTo structure for a given partition GUID
35  */
36 static WERROR uref_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct GUID *guid,
37                             struct repsTo *reps)
38 {
39         struct ldb_dn *dn;
40         const char *attrs[] = { "repsTo", NULL };
41         struct ldb_result *res;
42         const struct ldb_val *v;
43         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
44
45         if (dsdb_find_dn_by_guid(sam_ctx, tmp_ctx, GUID_string(tmp_ctx, guid), &dn) != LDB_SUCCESS) {
46                 DEBUG(0,("drsuapi_addref: failed to find partition with GUID %s\n",
47                          GUID_string(tmp_ctx, guid)));
48                 talloc_free(tmp_ctx);
49                 return WERR_DS_DRA_BAD_NC;
50         }
51
52         /* TODO: possibly check in the rootDSE to see that this DN is
53          * one of our partition roots */         
54
55         if (ldb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, NULL) != LDB_SUCCESS) {
56                 DEBUG(0,("drsuapi_addref: failed to read partition object\n"));
57                 talloc_free(tmp_ctx);
58                 return WERR_DS_DRA_INTERNAL_ERROR;
59         }
60
61         v = ldb_msg_find_ldb_val(res->msgs[0], "repsTo");
62         if (v == NULL) {
63                 /* treat as empty empty */
64                 ZERO_STRUCTP(reps);
65                 reps->version = REPSTO_VERSION1;
66         } else {
67                 enum ndr_err_code ndr_err;
68                 ndr_err = ndr_pull_struct_blob(v, mem_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
69                                                reps, 
70                                                (ndr_pull_flags_fn_t)ndr_pull_repsTo);
71                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
72                         talloc_free(tmp_ctx);
73                         return WERR_DS_DRA_INTERNAL_ERROR;
74                 }
75         }
76
77         talloc_free(tmp_ctx);
78         
79         return WERR_OK;
80 }
81
82 /*
83   save the repsTo structure for a given partition GUID
84  */
85 static WERROR uref_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct GUID *guid,
86                             struct repsTo *reps)
87 {
88         struct ldb_dn *dn;
89         struct ldb_val v;
90         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
91         enum ndr_err_code ndr_err;
92         struct ldb_message *msg;
93         struct ldb_message_element *el;
94
95         if (dsdb_find_dn_by_guid(sam_ctx, tmp_ctx, GUID_string(tmp_ctx, guid), &dn) != LDB_SUCCESS) {
96                 DEBUG(0,("drsuapi_addref: failed to find partition with GUID %s\n",
97                          GUID_string(tmp_ctx, guid)));
98                 talloc_free(tmp_ctx);
99                 return WERR_DS_DRA_BAD_NC;
100         }
101
102         ndr_err = ndr_push_struct_blob(&v, tmp_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
103                                        reps, 
104                                        (ndr_push_flags_fn_t)ndr_push_repsTo);
105         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106                 goto failed;
107         }
108
109         msg = ldb_msg_new(tmp_ctx);
110         msg->dn = dn;
111         if (ldb_msg_add_empty(msg, "repsTo", LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
112                 goto failed;
113         }
114         el->num_values = 1;
115         el->values = &v;
116
117         if (ldb_modify(sam_ctx, msg) != LDB_SUCCESS) {
118                 DEBUG(0,("Failed to store repsTo - %s\n", ldb_errstring(sam_ctx)));
119                 goto failed;
120         }
121
122         talloc_free(tmp_ctx);
123         
124         return WERR_OK;
125
126 failed:
127         talloc_free(tmp_ctx);
128         return WERR_DS_DRA_INTERNAL_ERROR;
129 }
130
131 /*
132   add a replication destination for a given partition GUID
133  */
134 static WERROR uref_add_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
135                             struct GUID *guid, struct repsToDest *dest)
136 {
137         struct repsTo reps;
138         WERROR werr;
139         struct repsTov1 *rv1;
140
141         werr = uref_loadreps(sam_ctx, mem_ctx, guid, &reps);
142         if (!W_ERROR_IS_OK(werr)) {
143                 return werr;
144         }
145
146         if (reps.version != REPSTO_VERSION1) {
147                 DEBUG(0,("Wrong version number %u on disk\n",
148                          reps.version));
149                 return WERR_DS_DRA_INTERNAL_ERROR;
150         }
151
152         rv1 = &reps.ctr.r;
153         rv1->reps = talloc_realloc(mem_ctx, rv1->reps, struct repsToDest, rv1->count+1);
154         if (rv1->reps == NULL) {
155                 return WERR_DS_DRA_INTERNAL_ERROR;
156         }
157         rv1->reps[rv1->count] = *dest;
158         rv1->count++;
159
160         werr = uref_savereps(sam_ctx, mem_ctx, guid, &reps);
161         if (!W_ERROR_IS_OK(werr)) {
162                 return werr;
163         }
164
165         return WERR_OK; 
166 }
167
168 /*
169   delete a replication destination for a given partition GUID
170  */
171 static WERROR uref_del_dest(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
172                             struct GUID *guid, struct GUID *dest_guid)
173 {
174         struct repsTo reps;
175         WERROR werr;
176         struct repsTov1 *rv1;
177         int i;
178
179         werr = uref_loadreps(sam_ctx, mem_ctx, guid, &reps);
180         if (!W_ERROR_IS_OK(werr)) {
181                 return werr;
182         }
183
184         if (reps.version != REPSTO_VERSION1) {
185                 DEBUG(0,("Wrong version number %u on disk\n", reps.version));
186                 return WERR_DS_DRA_INTERNAL_ERROR;
187         }
188
189         rv1 = &reps.ctr.r;
190
191         for (i=0; i<rv1->count; i++) {
192                 if (GUID_compare(dest_guid, &rv1->reps[i].dest_guid) == 0) {
193                         if (i+1 < rv1->count) {
194                                 memmove(&rv1->reps[i], &rv1->reps[i+1], sizeof(rv1->reps[i])*(rv1->count-(i+1)));
195                         }
196                         rv1->count--;
197                 }
198         }
199
200         werr = uref_savereps(sam_ctx, mem_ctx, guid, &reps);
201         if (!W_ERROR_IS_OK(werr)) {
202                 return werr;
203         }
204
205         return WERR_OK; 
206 }
207
208 /* 
209   drsuapi_DsReplicaUpdateRefs
210 */
211 WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
212                                           struct drsuapi_DsReplicaUpdateRefs *r)
213 {
214         struct drsuapi_DsReplicaUpdateRefsRequest1 *req;
215         struct ldb_context *sam_ctx;
216         WERROR werr;
217
218         if (r->in.level != 1) {
219                 DEBUG(0,("DrReplicUpdateRefs - unsupported level %u\n", r->in.level));
220                 return WERR_DS_DRA_INVALID_PARAMETER;
221         }
222
223         req = &r->in.req.req1;
224         DEBUG(4,("DrReplicUpdateRefs for host '%s' with GUID %s options 0x%08x nc=%s\n",
225                  req->dest_dsa_dns_name, GUID_string(mem_ctx, &req->dest_dsa_guid),
226                  req->options,
227                  drs_ObjectIdentifier_to_string(mem_ctx, req->naming_context)));
228
229         /* TODO: We need to authenticate this operation pretty carefully */
230         sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, 
231                                 system_session(mem_ctx, dce_call->conn->dce_ctx->lp_ctx));
232         if (!sam_ctx) {
233                 return WERR_DS_DRA_INTERNAL_ERROR;              
234         }
235
236         if (ldb_transaction_start(sam_ctx) != LDB_SUCCESS) {
237                 DEBUG(0,(__location__ ": Failed to start transaction on samdb\n"));
238                 return WERR_DS_DRA_INTERNAL_ERROR;              
239         }
240
241         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE) {
242                 werr = uref_del_dest(sam_ctx, mem_ctx, &req->naming_context->guid, &req->dest_dsa_guid);
243                 if (!W_ERROR_IS_OK(werr)) {
244                         DEBUG(0,("Failed to delete repsTo for %s\n",
245                                  GUID_string(dce_call, &req->dest_dsa_guid)));
246                         goto failed;
247                 }
248         }
249
250         if (req->options & DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE) {
251                 struct repsToDest dest;
252
253                 dest.dest_dsa_dns_name = req->dest_dsa_dns_name;
254                 dest.dest_guid         = req->dest_dsa_guid;
255                 dest.options           = req->options;
256
257                 werr = uref_add_dest(sam_ctx, mem_ctx, &req->naming_context->guid, &dest);
258                 if (!W_ERROR_IS_OK(werr)) {
259                         DEBUG(0,("Failed to delete repsTo for %s\n",
260                                  GUID_string(dce_call, &dest.dest_guid)));
261                         goto failed;
262                 }
263         }
264
265         if (ldb_transaction_commit(sam_ctx) != LDB_SUCCESS) {
266                 DEBUG(0,(__location__ ": Failed to commit transaction on samdb\n"));
267                 return WERR_DS_DRA_INTERNAL_ERROR;              
268         }
269
270         talloc_free(sam_ctx);
271         return WERR_OK;
272
273 failed:
274         ldb_transaction_cancel(sam_ctx);
275         talloc_free(sam_ctx);
276         return werr;
277 }
278