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