s4-drepl: use the partition UDV and hwm for extended getncchanges ops
[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 "libcli/security/dom_sid.h"
35 #include "param/param.h"
36
37 WERROR dreplsrv_load_partitions(struct dreplsrv_service *s)
38 {
39         WERROR status;
40         struct ldb_dn *basedn;
41         struct ldb_result *r;
42         struct ldb_message_element *el;
43         static const char *attrs[] = { "hasMasterNCs", "msDS-hasFullReplicaNCs", NULL };
44         unsigned int i;
45         int ret;
46
47         basedn = samdb_ntds_settings_dn(s->samdb);
48         W_ERROR_HAVE_NO_MEMORY(basedn);
49
50         ret = ldb_search(s->samdb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
51                          "(objectClass=*)");
52         if (ret != LDB_SUCCESS) {
53                 return WERR_FOOBAR;
54         } else if (r->count != 1) {
55                 talloc_free(r);
56                 return WERR_FOOBAR;
57         }
58
59
60
61         el = ldb_msg_find_element(r->msgs[0], "hasMasterNCs");
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         el = ldb_msg_find_element(r->msgs[0], "msDS-hasFullReplicaNCs");
84
85         for (i=0; el && i < el->num_values; i++) {
86                 const char *v = (const char *)el->values[i].data;
87                 struct ldb_dn *pdn;
88                 struct dreplsrv_partition *p;
89
90                 pdn = ldb_dn_new(s, s->samdb, v);
91                 if (!ldb_dn_validate(pdn)) {
92                         return WERR_FOOBAR;
93                 }
94
95                 p = talloc_zero(s, struct dreplsrv_partition);
96                 W_ERROR_HAVE_NO_MEMORY(p);
97
98                 p->dn = talloc_steal(p, pdn);
99                 p->incoming_only = true;
100
101                 DLIST_ADD(s->partitions, p);
102
103                 DEBUG(2, ("dreplsrv_partition[%s] loaded (incoming only)\n", v));
104         }
105
106         talloc_free(r);
107
108         status = dreplsrv_refresh_partitions(s);
109         W_ERROR_NOT_OK_RETURN(status);
110
111         return WERR_OK;
112 }
113
114 WERROR dreplsrv_out_connection_attach(struct dreplsrv_service *s,
115                                       const struct repsFromTo1 *rft,
116                                       struct dreplsrv_out_connection **_conn)
117 {
118         struct dreplsrv_out_connection *cur, *conn = NULL;
119         const char *hostname;
120
121         if (!rft->other_info) {
122                 return WERR_FOOBAR;
123         }
124
125         if (!rft->other_info->dns_name) {
126                 return WERR_FOOBAR;
127         }
128
129         hostname = rft->other_info->dns_name;
130
131         for (cur = s->connections; cur; cur = cur->next) {              
132                 if (strcmp(cur->binding->host, hostname) == 0) {
133                         conn = cur;
134                         break;
135                 }
136         }
137
138         if (!conn) {
139                 NTSTATUS nt_status;
140                 char *binding_str;
141
142                 conn = talloc_zero(s, struct dreplsrv_out_connection);
143                 W_ERROR_HAVE_NO_MEMORY(conn);
144
145                 conn->service   = s;
146
147                 binding_str = talloc_asprintf(conn, "ncacn_ip_tcp:%s[krb5,seal]",
148                                               hostname);
149                 W_ERROR_HAVE_NO_MEMORY(binding_str);
150                 nt_status = dcerpc_parse_binding(conn, binding_str, &conn->binding);
151                 talloc_free(binding_str);
152                 if (!NT_STATUS_IS_OK(nt_status)) {
153                         return ntstatus_to_werror(nt_status);
154                 }
155
156                 DLIST_ADD_END(s->connections, conn, struct dreplsrv_out_connection *);
157
158                 DEBUG(2,("dreplsrv_out_connection_attach(%s): create\n", conn->binding->host));
159         } else {
160                 DEBUG(2,("dreplsrv_out_connection_attach(%s): attach\n", conn->binding->host));
161         }
162
163         *_conn = conn;
164         return WERR_OK;
165 }
166
167 static WERROR dreplsrv_partition_add_source_dsa(struct dreplsrv_service *s,
168                                                 struct dreplsrv_partition *p,
169                                                 const struct ldb_val *val)
170 {
171         WERROR status;
172         enum ndr_err_code ndr_err;
173         struct dreplsrv_partition_source_dsa *source, *s2;
174
175         source = talloc_zero(p, struct dreplsrv_partition_source_dsa);
176         W_ERROR_HAVE_NO_MEMORY(source);
177
178         ndr_err = ndr_pull_struct_blob(val, source, 
179                                        &source->_repsFromBlob,
180                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
181         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
182                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
183                 talloc_free(source);
184                 return ntstatus_to_werror(nt_status);
185         }
186         /* NDR_PRINT_DEBUG(repsFromToBlob, &source->_repsFromBlob); */
187         if (source->_repsFromBlob.version != 1) {
188                 talloc_free(source);
189                 return WERR_DS_DRA_INTERNAL_ERROR;
190         }
191
192         source->partition       = p;
193         source->repsFrom1       = &source->_repsFromBlob.ctr.ctr1;
194
195         status = dreplsrv_out_connection_attach(s, source->repsFrom1, &source->conn);
196         W_ERROR_NOT_OK_RETURN(status);
197
198         /* remove any existing source with the same GUID */
199         for (s2=p->sources; s2; s2=s2->next) {
200                 if (GUID_compare(&s2->repsFrom1->source_dsa_obj_guid, 
201                                  &source->repsFrom1->source_dsa_obj_guid) == 0) {
202                         talloc_free(s2->repsFrom1->other_info);
203                         *s2->repsFrom1 = *source->repsFrom1;
204                         talloc_steal(s2, s2->repsFrom1->other_info);
205                         talloc_free(source);
206                         return WERR_OK;
207                 }
208         }
209
210         DLIST_ADD_END(p->sources, source, struct dreplsrv_partition_source_dsa *);
211         return WERR_OK;
212 }
213
214 WERROR dreplsrv_partition_find_for_nc(struct dreplsrv_service *s,
215                                       const struct GUID *nc_guid,
216                                       const struct dom_sid *nc_sid,
217                                       const char *nc_dn_str,
218                                       struct dreplsrv_partition **_p)
219 {
220         struct dreplsrv_partition *p;
221         bool valid_sid, valid_guid;
222         struct dom_sid null_sid;
223         ZERO_STRUCT(null_sid);
224
225         SMB_ASSERT(_p);
226
227         valid_sid  = nc_sid && !dom_sid_equal(&null_sid, nc_sid);
228         valid_guid = nc_guid && !GUID_all_zero(nc_guid);
229
230         if (!valid_sid && !valid_guid && !nc_dn_str) {
231                 return WERR_DS_DRA_INVALID_PARAMETER;
232         }
233
234         for (p = s->partitions; p; p = p->next) {
235                 if ((valid_guid && GUID_equal(&p->nc.guid, nc_guid))
236                     || strequal(p->nc.dn, nc_dn_str)
237                     || (valid_sid && dom_sid_equal(&p->nc.sid, nc_sid)))
238                 {
239                         *_p = p;
240                         return WERR_OK;
241                 }
242         }
243
244         return WERR_DS_DRA_BAD_NC;
245 }
246
247 WERROR dreplsrv_partition_source_dsa_by_guid(struct dreplsrv_partition *p,
248                                              const struct GUID *dsa_guid,
249                                              struct dreplsrv_partition_source_dsa **_dsa)
250 {
251         struct dreplsrv_partition_source_dsa *dsa;
252
253         SMB_ASSERT(dsa_guid != NULL);
254         SMB_ASSERT(!GUID_all_zero(dsa_guid));
255         SMB_ASSERT(_dsa);
256
257         for (dsa = p->sources; dsa; dsa = dsa->next) {
258                 if (GUID_equal(dsa_guid, &dsa->repsFrom1->source_dsa_obj_guid)) {
259                         *_dsa = dsa;
260                         return WERR_OK;
261                 }
262         }
263
264         return WERR_DS_DRA_NO_REPLICA;
265 }
266
267 WERROR dreplsrv_partition_source_dsa_by_dns(const struct dreplsrv_partition *p,
268                                             const char *dsa_dns,
269                                             struct dreplsrv_partition_source_dsa **_dsa)
270 {
271         struct dreplsrv_partition_source_dsa *dsa;
272
273         SMB_ASSERT(dsa_dns != NULL);
274         SMB_ASSERT(_dsa);
275
276         for (dsa = p->sources; dsa; dsa = dsa->next) {
277                 if (strequal(dsa_dns, dsa->repsFrom1->other_info->dns_name)) {
278                         *_dsa = dsa;
279                         return WERR_OK;
280                 }
281         }
282
283         return WERR_DS_DRA_NO_REPLICA;
284 }
285
286
287 static WERROR dreplsrv_refresh_partition(struct dreplsrv_service *s,
288                                          struct dreplsrv_partition *p)
289 {
290         WERROR status;
291         struct dom_sid *nc_sid;
292         struct ldb_message_element *orf_el = NULL;
293         struct ldb_result *r;
294         unsigned int i;
295         int ret;
296         TALLOC_CTX *mem_ctx = talloc_new(p);
297         static const char *attrs[] = {
298                 "objectSid",
299                 "objectGUID",
300                 "repsFrom",
301                 NULL
302         };
303
304         DEBUG(2, ("dreplsrv_refresh_partition(%s)\n",
305                 ldb_dn_get_linearized(p->dn)));
306
307         ret = ldb_search(s->samdb, mem_ctx, &r, p->dn, LDB_SCOPE_BASE, attrs,
308                          "(objectClass=*)");
309         if (ret != LDB_SUCCESS) {
310                 talloc_free(mem_ctx);
311                 return WERR_FOOBAR;
312         }
313         
314         talloc_free(discard_const(p->nc.dn));
315         ZERO_STRUCT(p->nc);
316         p->nc.dn        = ldb_dn_alloc_linearized(p, p->dn);
317         W_ERROR_HAVE_NO_MEMORY(p->nc.dn);
318         p->nc.guid      = samdb_result_guid(r->msgs[0], "objectGUID");
319         nc_sid          = samdb_result_dom_sid(p, r->msgs[0], "objectSid");
320         if (nc_sid) {
321                 p->nc.sid       = *nc_sid;
322                 talloc_free(nc_sid);
323         }
324
325         talloc_free(p->uptodatevector.cursors);
326         talloc_free(p->uptodatevector_ex.cursors);
327         ZERO_STRUCT(p->uptodatevector);
328         ZERO_STRUCT(p->uptodatevector_ex);
329
330         ret = dsdb_load_udv_v2(s->samdb, p->dn, p, &p->uptodatevector.cursors, &p->uptodatevector.count);
331         if (ret != LDB_SUCCESS) {
332                 DEBUG(4,(__location__ ": no UDV available for %s\n", ldb_dn_get_linearized(p->dn)));
333         }
334
335         orf_el = ldb_msg_find_element(r->msgs[0], "repsFrom");
336         if (orf_el) {
337                 for (i=0; i < orf_el->num_values; i++) {
338                         status = dreplsrv_partition_add_source_dsa(s, p, &orf_el->values[i]);
339                         W_ERROR_NOT_OK_RETURN(status);  
340                 }
341         }
342
343         talloc_free(mem_ctx);
344
345         return WERR_OK;
346 }
347
348 WERROR dreplsrv_refresh_partitions(struct dreplsrv_service *s)
349 {
350         WERROR status;
351         struct dreplsrv_partition *p;
352
353         for (p = s->partitions; p; p = p->next) {
354                 status = dreplsrv_refresh_partition(s, p);
355                 W_ERROR_NOT_OK_RETURN(status);
356         }
357
358         return WERR_OK;
359 }