Merge branch 'master' of git://git.samba.org/samba
[amitay/samba.git] / source4 / dsdb / repl / drepl_partitions.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB replication service
4    
5    Copyright (C) Stefan Metzmacher 2007
6     
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19    
20 */
21
22 #include "includes.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "auth/auth.h"
25 #include "smbd/service.h"
26 #include "lib/events/events.h"
27 #include "lib/messaging/irpc.h"
28 #include "dsdb/repl/drepl_service.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "../lib/util/dlinklist.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "librpc/gen_ndr/ndr_drsuapi.h"
33 #include "librpc/gen_ndr/ndr_drsblobs.h"
34 #include "param/param.h"
35
36 WERROR dreplsrv_load_partitions(struct dreplsrv_service *s)
37 {
38         WERROR status;
39         struct ldb_dn *basedn;
40         struct ldb_result *r;
41         struct ldb_message_element *el;
42         static const char *attrs[] = { "hasMasterNCs", NULL };
43         uint32_t i;
44         int ret;
45
46         basedn = samdb_ntds_settings_dn(s->samdb);
47         W_ERROR_HAVE_NO_MEMORY(basedn);
48
49         ret = ldb_search(s->samdb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
50                          "(objectClass=*)");
51         if (ret != LDB_SUCCESS) {
52                 return WERR_FOOBAR;
53         } else if (r->count != 1) {
54                 talloc_free(r);
55                 return WERR_FOOBAR;
56         }
57
58         el = ldb_msg_find_element(r->msgs[0], "hasMasterNCs");
59         if (!el) {
60                 return WERR_FOOBAR;
61         }
62
63         for (i=0; el && i < el->num_values; i++) {
64                 const char *v = (const char *)el->values[i].data;
65                 struct ldb_dn *pdn;
66                 struct dreplsrv_partition *p;
67
68                 pdn = ldb_dn_new(s, s->samdb, v);
69                 if (!ldb_dn_validate(pdn)) {
70                         return WERR_FOOBAR;
71                 }
72
73                 p = talloc_zero(s, struct dreplsrv_partition);
74                 W_ERROR_HAVE_NO_MEMORY(p);
75
76                 p->dn = talloc_steal(p, pdn);
77
78                 DLIST_ADD(s->partitions, p);
79
80                 DEBUG(2, ("dreplsrv_partition[%s] loaded\n", v));
81         }
82
83         talloc_free(r);
84
85         status = dreplsrv_refresh_partitions(s);
86         W_ERROR_NOT_OK_RETURN(status);
87
88         return WERR_OK;
89 }
90
91 WERROR dreplsrv_out_connection_attach(struct dreplsrv_service *s,
92                                       const struct repsFromTo1 *rft,
93                                       struct dreplsrv_out_connection **_conn)
94 {
95         struct dreplsrv_out_connection *cur, *conn = NULL;
96         const char *hostname;
97
98         if (!rft->other_info) {
99                 return WERR_FOOBAR;
100         }
101
102         if (!rft->other_info->dns_name) {
103                 return WERR_FOOBAR;
104         }
105
106         hostname = rft->other_info->dns_name;
107
108         for (cur = s->connections; cur; cur = cur->next) {              
109                 if (strcmp(cur->binding->host, hostname) == 0) {
110                         conn = cur;
111                         break;
112                 }
113         }
114
115         if (!conn) {
116                 NTSTATUS nt_status;
117                 char *binding_str;
118
119                 conn = talloc_zero(s, struct dreplsrv_out_connection);
120                 W_ERROR_HAVE_NO_MEMORY(conn);
121
122                 conn->service   = s;
123
124                 binding_str = talloc_asprintf(conn, "ncacn_ip_tcp:%s[krb5,seal]",
125                                               hostname);
126                 W_ERROR_HAVE_NO_MEMORY(binding_str);
127                 nt_status = dcerpc_parse_binding(conn, binding_str, &conn->binding);
128                 talloc_free(binding_str);
129                 if (!NT_STATUS_IS_OK(nt_status)) {
130                         return ntstatus_to_werror(nt_status);
131                 }
132
133                 DLIST_ADD_END(s->connections, conn, struct dreplsrv_out_connection *);
134
135                 DEBUG(2,("dreplsrv_out_connection_attach(%s): create\n", conn->binding->host));
136         } else {
137                 DEBUG(2,("dreplsrv_out_connection_attach(%s): attach\n", conn->binding->host));
138         }
139
140         *_conn = conn;
141         return WERR_OK;
142 }
143
144 static WERROR dreplsrv_partition_add_source_dsa(struct dreplsrv_service *s,
145                                                 struct dreplsrv_partition *p,
146                                                 const struct ldb_val *val)
147 {
148         WERROR status;
149         enum ndr_err_code ndr_err;
150         struct dreplsrv_partition_source_dsa *source, *s2;
151
152         source = talloc_zero(p, struct dreplsrv_partition_source_dsa);
153         W_ERROR_HAVE_NO_MEMORY(source);
154
155         ndr_err = ndr_pull_struct_blob(val, source, 
156                                        lp_iconv_convenience(s->task->lp_ctx), &source->_repsFromBlob,
157                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
158         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
159                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
160                 talloc_free(source);
161                 return ntstatus_to_werror(nt_status);
162         }
163         /* NDR_PRINT_DEBUG(repsFromToBlob, &source->_repsFromBlob); */
164         if (source->_repsFromBlob.version != 1) {
165                 talloc_free(source);
166                 return WERR_DS_DRA_INTERNAL_ERROR;
167         }
168
169         source->partition       = p;
170         source->repsFrom1       = &source->_repsFromBlob.ctr.ctr1;
171
172         status = dreplsrv_out_connection_attach(s, source->repsFrom1, &source->conn);
173         W_ERROR_NOT_OK_RETURN(status);
174
175         /* remove any existing source with the same GUID */
176         for (s2=p->sources; s2; s2=s2->next) {
177                 if (GUID_compare(&s2->repsFrom1->source_dsa_obj_guid, 
178                                  &source->repsFrom1->source_dsa_obj_guid) == 0) {
179                         talloc_free(s2->repsFrom1->other_info);
180                         *s2->repsFrom1 = *source->repsFrom1;
181                         talloc_steal(s2, s2->repsFrom1->other_info);
182                         talloc_free(source);
183                         return WERR_OK;
184                 }
185         }
186
187         DLIST_ADD_END(p->sources, source, struct dreplsrv_partition_source_dsa *);
188         return WERR_OK;
189 }
190
191 /*
192   convert from one udv format to the other
193  */
194 static WERROR udv_convert(TALLOC_CTX *mem_ctx,
195                           const struct replUpToDateVectorCtr2 *udv,
196                           struct drsuapi_DsReplicaCursorCtrEx *udv_ex)
197 {
198         int i;
199
200         udv_ex->version = 2;
201         udv_ex->reserved1 = 0;
202         udv_ex->reserved2 = 0;
203         udv_ex->count = udv->count;
204         udv_ex->cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, udv->count);
205         W_ERROR_HAVE_NO_MEMORY(udv_ex->cursors);
206
207         for (i=0; i<udv->count; i++) {
208                 udv_ex->cursors[i].source_dsa_invocation_id = udv->cursors[i].source_dsa_invocation_id;
209                 udv_ex->cursors[i].highest_usn = udv->cursors[i].highest_usn;
210         }
211
212         return WERR_OK;
213 }
214
215 /*
216   add our local UDV element for the partition
217  */
218 static WERROR add_local_udv(struct dreplsrv_service *s,
219                             struct dreplsrv_partition *p,
220                             const struct GUID *our_invocation_id,
221                             struct drsuapi_DsReplicaCursorCtrEx *udv)
222 {
223         int ret;
224         uint64_t highest_usn;
225         int i;
226
227         ret = dsdb_load_partition_usn(s->samdb, p->dn, &highest_usn);
228         if (ret != LDB_SUCCESS) {
229                 /* nothing to add */
230                 return WERR_OK;
231         }
232
233         for (i=0; i<udv->count; i++) {
234                 if (GUID_equal(our_invocation_id, &udv->cursors[i].source_dsa_invocation_id)) {
235                         udv->cursors[i].highest_usn = highest_usn;
236                         return WERR_OK;
237                 }
238         }
239
240         udv->cursors = talloc_realloc(p, udv->cursors, struct drsuapi_DsReplicaCursor, udv->count+1);
241         W_ERROR_HAVE_NO_MEMORY(udv->cursors);
242
243         udv->cursors[udv->count].source_dsa_invocation_id = *our_invocation_id;
244         udv->cursors[udv->count].highest_usn = highest_usn;
245         udv->count++;
246
247         return WERR_OK;
248 }
249
250 static WERROR dreplsrv_refresh_partition(struct dreplsrv_service *s,
251                                          struct dreplsrv_partition *p)
252 {
253         WERROR status;
254         const struct ldb_val *ouv_value;
255         struct replUpToDateVectorBlob ouv;
256         struct dom_sid *nc_sid;
257         struct ldb_message_element *orf_el = NULL;
258         struct ldb_result *r;
259         uint32_t i;
260         int ret;
261         TALLOC_CTX *mem_ctx = talloc_new(p);
262         static const char *attrs[] = {
263                 "objectSid",
264                 "objectGUID",
265                 "replUpToDateVector",
266                 "repsFrom",
267                 NULL
268         };
269
270         DEBUG(2, ("dreplsrv_refresh_partition(%s)\n",
271                 ldb_dn_get_linearized(p->dn)));
272
273         ret = ldb_search(s->samdb, mem_ctx, &r, p->dn, LDB_SCOPE_BASE, attrs,
274                          "(objectClass=*)");
275         if (ret != LDB_SUCCESS) {
276                 talloc_free(mem_ctx);
277                 return WERR_FOOBAR;
278         } else if (r->count != 1) {
279                 talloc_free(mem_ctx);
280                 return WERR_FOOBAR;
281         }
282         
283         talloc_free(discard_const(p->nc.dn));
284         ZERO_STRUCT(p->nc);
285         p->nc.dn        = ldb_dn_alloc_linearized(p, p->dn);
286         W_ERROR_HAVE_NO_MEMORY(p->nc.dn);
287         p->nc.guid      = samdb_result_guid(r->msgs[0], "objectGUID");
288         nc_sid          = samdb_result_dom_sid(p, r->msgs[0], "objectSid");
289         if (nc_sid) {
290                 p->nc.sid       = *nc_sid;
291                 talloc_free(nc_sid);
292         }
293
294         talloc_free(p->uptodatevector.cursors);
295         talloc_free(p->uptodatevector_ex.cursors);
296         ZERO_STRUCT(p->uptodatevector);
297         ZERO_STRUCT(p->uptodatevector_ex);
298
299         ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
300         if (ouv_value) {
301                 enum ndr_err_code ndr_err;
302                 ndr_err = ndr_pull_struct_blob(ouv_value, mem_ctx, 
303                                                lp_iconv_convenience(s->task->lp_ctx), &ouv,
304                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
305                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
306                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
307                         talloc_free(mem_ctx);
308                         return ntstatus_to_werror(nt_status);
309                 }
310                 /* NDR_PRINT_DEBUG(replUpToDateVectorBlob, &ouv); */
311                 if (ouv.version != 2) {
312                         talloc_free(mem_ctx);
313                         return WERR_DS_DRA_INTERNAL_ERROR;
314                 }
315
316                 p->uptodatevector.count         = ouv.ctr.ctr2.count;
317                 p->uptodatevector.reserved      = ouv.ctr.ctr2.reserved;
318                 p->uptodatevector.cursors       = talloc_steal(p, ouv.ctr.ctr2.cursors);
319
320                 status = udv_convert(p, &p->uptodatevector, &p->uptodatevector_ex);
321                 W_ERROR_NOT_OK_RETURN(status);
322         }
323
324         status = add_local_udv(s, p, samdb_ntds_invocation_id(s->samdb), &p->uptodatevector_ex);
325         W_ERROR_NOT_OK_RETURN(status);
326
327         orf_el = ldb_msg_find_element(r->msgs[0], "repsFrom");
328         if (orf_el) {
329                 for (i=0; i < orf_el->num_values; i++) {
330                         status = dreplsrv_partition_add_source_dsa(s, p, &orf_el->values[i]);
331                         W_ERROR_NOT_OK_RETURN(status);  
332                 }
333         }
334
335         talloc_free(mem_ctx);
336
337         return WERR_OK;
338 }
339
340 WERROR dreplsrv_refresh_partitions(struct dreplsrv_service *s)
341 {
342         WERROR status;
343         struct dreplsrv_partition *p;
344
345         for (p = s->partitions; p; p = p->next) {
346                 status = dreplsrv_refresh_partition(s, p);
347                 W_ERROR_NOT_OK_RETURN(status);
348         }
349
350         return WERR_OK;
351 }