s4-repl: fill in GUID and SID from partition information
[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 "dsdb/repl/drepl_service.h"
28 #include <ldb_errors.h>
29 #include "../lib/util/dlinklist.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "librpc/gen_ndr/ndr_drsuapi.h"
32 #include "librpc/gen_ndr/ndr_drsblobs.h"
33 #include "libcli/security/security.h"
34 #include "param/param.h"
35 #include "dsdb/common/util.h"
36
37 WERROR dreplsrv_load_partitions(struct dreplsrv_service *s)
38 {
39         WERROR status;
40         static const char *attrs[] = { "hasMasterNCs", "hasPartialReplicaNCs", NULL };
41         unsigned int i;
42         int ret;
43         TALLOC_CTX *tmp_ctx;
44         struct ldb_result *res;
45         struct ldb_message_element *el;
46         struct ldb_dn *ntds_dn;
47
48         tmp_ctx = talloc_new(s);
49         W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
50
51         ntds_dn = samdb_ntds_settings_dn(s->samdb);
52         if (!ntds_dn) {
53                 DEBUG(1,(__location__ ": Unable to find ntds_dn: %s\n", ldb_errstring(s->samdb)));
54                 talloc_free(tmp_ctx);
55                 return WERR_DS_DRA_INTERNAL_ERROR;
56         }
57
58         ret = dsdb_search_dn(s->samdb, tmp_ctx, &res, ntds_dn, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN);
59         if (ret != LDB_SUCCESS) {
60                 DEBUG(1,("Searching for hasMasterNCs in NTDS DN failed: %s\n", ldb_errstring(s->samdb)));
61                 talloc_free(tmp_ctx);
62                 return WERR_DS_DRA_INTERNAL_ERROR;
63         }
64
65         el = ldb_msg_find_element(res->msgs[0], "hasMasterNCs");
66         if (!el) {
67                 DEBUG(1,("Finding hasMasterNCs element in root_res failed: %s\n",
68                          ldb_errstring(s->samdb)));
69                 talloc_free(tmp_ctx);
70                 return WERR_DS_DRA_INTERNAL_ERROR;
71         }
72
73         for (i=0; i<el->num_values; i++) {
74                 struct ldb_dn *pdn;
75                 struct dreplsrv_partition *p;
76
77                 pdn = ldb_dn_from_ldb_val(tmp_ctx, s->samdb, &el->values[i]);
78                 if (pdn == NULL) {
79                         talloc_free(tmp_ctx);
80                         return WERR_DS_DRA_INTERNAL_ERROR;
81                 }
82                 if (!ldb_dn_validate(pdn)) {
83                         return WERR_DS_DRA_INTERNAL_ERROR;
84                 }
85
86                 p = talloc_zero(s, struct dreplsrv_partition);
87                 W_ERROR_HAVE_NO_MEMORY(p);
88
89                 p->dn = talloc_steal(p, pdn);
90                 p->service = s;
91
92                 DLIST_ADD(s->partitions, p);
93
94                 DEBUG(2, ("dreplsrv_partition[%s] loaded\n", ldb_dn_get_linearized(p->dn)));
95         }
96
97         el = ldb_msg_find_element(res->msgs[0], "hasPartialReplicaNCs");
98
99         for (i=0; el && i<el->num_values; i++) {
100                 struct ldb_dn *pdn;
101                 struct dreplsrv_partition *p;
102
103                 pdn = ldb_dn_from_ldb_val(tmp_ctx, s->samdb, &el->values[i]);
104                 if (pdn == NULL) {
105                         talloc_free(tmp_ctx);
106                         return WERR_DS_DRA_INTERNAL_ERROR;
107                 }
108                 if (!ldb_dn_validate(pdn)) {
109                         return WERR_DS_DRA_INTERNAL_ERROR;
110                 }
111
112                 p = talloc_zero(s, struct dreplsrv_partition);
113                 W_ERROR_HAVE_NO_MEMORY(p);
114
115                 p->dn = talloc_steal(p, pdn);
116                 p->partial_replica = true;
117                 p->service = s;
118
119                 DLIST_ADD(s->partitions, p);
120
121                 DEBUG(2, ("dreplsrv_partition[%s] loaded (partial replica)\n", ldb_dn_get_linearized(p->dn)));
122         }
123
124         talloc_free(tmp_ctx);
125
126         status = dreplsrv_refresh_partitions(s);
127         W_ERROR_NOT_OK_RETURN(status);
128
129         return WERR_OK;
130 }
131
132 /*
133   work out the principal to use for DRS replication connections
134  */
135 NTSTATUS dreplsrv_get_target_principal(struct dreplsrv_service *s,
136                                        TALLOC_CTX *mem_ctx,
137                                        const struct repsFromTo1 *rft,
138                                        const char **target_principal)
139 {
140         TALLOC_CTX *tmp_ctx;
141         struct ldb_result *res;
142         const char *attrs[] = { "dNSHostName", NULL };
143         int ret;
144         const char *hostname;
145         struct ldb_dn *dn;
146         struct ldb_dn *forest_dn;
147
148         *target_principal = NULL;
149
150         tmp_ctx = talloc_new(mem_ctx);
151
152         /* we need to find their hostname */
153         ret = dsdb_find_dn_by_guid(s->samdb, tmp_ctx, &rft->source_dsa_obj_guid, &dn);
154         if (ret != LDB_SUCCESS) {
155                 talloc_free(tmp_ctx);
156                 /* its OK for their NTDSDSA DN not to be in our database */
157                 return NT_STATUS_OK;
158         }
159
160         /* strip off the NTDS Settings */
161         if (!ldb_dn_remove_child_components(dn, 1)) {
162                 talloc_free(tmp_ctx);
163                 return NT_STATUS_OK;
164         }
165
166         ret = dsdb_search_dn(s->samdb, tmp_ctx, &res, dn, attrs, 0);
167         if (ret != LDB_SUCCESS) {
168                 talloc_free(tmp_ctx);
169                 /* its OK for their account DN not to be in our database */
170                 return NT_STATUS_OK;
171         }
172
173         hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
174         if (hostname == NULL) {
175                 talloc_free(tmp_ctx);
176                 /* its OK to not have a dnshostname */
177                 return NT_STATUS_OK;
178         }
179
180         /* All DCs have the GC/hostname/realm name, but if some of the
181          * preconditions are not satisfied, then we will fall back to
182          * the
183          * E3514235-4B06-11D1-AB04-00C04FC2DCD2/${NTDSGUID}/${DNSDOMAIN}
184          * name.  This means that if a AD server has a dnsHostName set
185          * on it's record, it must also have GC/hostname/realm
186          * servicePrincipalName */
187
188         forest_dn = ldb_get_root_basedn(s->samdb);
189         if (forest_dn == NULL) {
190                 talloc_free(tmp_ctx);
191                 return NT_STATUS_OK;
192         }
193
194         *target_principal = talloc_asprintf(mem_ctx, "GC/%s/%s",
195                                             hostname,
196                                             samdb_dn_to_dns_domain(tmp_ctx, forest_dn));
197         talloc_free(tmp_ctx);
198         return NT_STATUS_OK;
199 }
200
201
202 WERROR dreplsrv_out_connection_attach(struct dreplsrv_service *s,
203                                       const struct repsFromTo1 *rft,
204                                       struct dreplsrv_out_connection **_conn)
205 {
206         struct dreplsrv_out_connection *cur, *conn = NULL;
207         const char *hostname;
208
209         if (!rft->other_info) {
210                 return WERR_FOOBAR;
211         }
212
213         if (!rft->other_info->dns_name) {
214                 return WERR_FOOBAR;
215         }
216
217         hostname = rft->other_info->dns_name;
218
219         for (cur = s->connections; cur; cur = cur->next) {              
220                 if (strcmp(cur->binding->host, hostname) == 0) {
221                         conn = cur;
222                         break;
223                 }
224         }
225
226         if (!conn) {
227                 NTSTATUS nt_status;
228                 char *binding_str;
229
230                 conn = talloc_zero(s, struct dreplsrv_out_connection);
231                 W_ERROR_HAVE_NO_MEMORY(conn);
232
233                 conn->service   = s;
234
235                 binding_str = talloc_asprintf(conn, "ncacn_ip_tcp:%s[krb5,seal]",
236                                               hostname);
237                 W_ERROR_HAVE_NO_MEMORY(binding_str);
238                 nt_status = dcerpc_parse_binding(conn, binding_str, &conn->binding);
239                 talloc_free(binding_str);
240                 if (!NT_STATUS_IS_OK(nt_status)) {
241                         return ntstatus_to_werror(nt_status);
242                 }
243
244                 /* use the GC principal for DRS replication */
245                 nt_status = dreplsrv_get_target_principal(s, conn->binding,
246                                                           rft, &conn->binding->target_principal);
247                 if (!NT_STATUS_IS_OK(nt_status)) {
248                         return ntstatus_to_werror(nt_status);
249                 }
250
251                 DLIST_ADD_END(s->connections, conn, struct dreplsrv_out_connection *);
252
253                 DEBUG(4,("dreplsrv_out_connection_attach(%s): create\n", conn->binding->host));
254         } else {
255                 DEBUG(4,("dreplsrv_out_connection_attach(%s): attach\n", conn->binding->host));
256         }
257
258         *_conn = conn;
259         return WERR_OK;
260 }
261
262 /*
263   find an existing source dsa in a list
264  */
265 static struct dreplsrv_partition_source_dsa *dreplsrv_find_source_dsa(struct dreplsrv_partition_source_dsa *list,
266                                                                       struct GUID *guid)
267 {
268         struct dreplsrv_partition_source_dsa *s;
269         for (s=list; s; s=s->next) {
270                 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
271                         return s;
272                 }
273         }
274         return NULL;    
275 }
276
277
278
279 static WERROR dreplsrv_partition_add_source_dsa(struct dreplsrv_service *s,
280                                                 struct dreplsrv_partition *p,
281                                                 struct dreplsrv_partition_source_dsa **listp,
282                                                 struct dreplsrv_partition_source_dsa *check_list,
283                                                 const struct ldb_val *val)
284 {
285         WERROR status;
286         enum ndr_err_code ndr_err;
287         struct dreplsrv_partition_source_dsa *source, *s2;
288
289         source = talloc_zero(p, struct dreplsrv_partition_source_dsa);
290         W_ERROR_HAVE_NO_MEMORY(source);
291
292         ndr_err = ndr_pull_struct_blob(val, source, 
293                                        &source->_repsFromBlob,
294                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
295         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
296                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
297                 talloc_free(source);
298                 return ntstatus_to_werror(nt_status);
299         }
300         /* NDR_PRINT_DEBUG(repsFromToBlob, &source->_repsFromBlob); */
301         if (source->_repsFromBlob.version != 1) {
302                 talloc_free(source);
303                 return WERR_DS_DRA_INTERNAL_ERROR;
304         }
305
306         source->partition       = p;
307         source->repsFrom1       = &source->_repsFromBlob.ctr.ctr1;
308
309         status = dreplsrv_out_connection_attach(s, source->repsFrom1, &source->conn);
310         W_ERROR_NOT_OK_RETURN(status);
311
312         if (check_list && 
313             dreplsrv_find_source_dsa(check_list, &source->repsFrom1->source_dsa_obj_guid)) {
314                 /* its in the check list, don't add it again */
315                 talloc_free(source);
316                 return WERR_OK;
317         }
318
319         /* re-use an existing source if found */
320         for (s2=*listp; s2; s2=s2->next) {
321                 if (GUID_compare(&s2->repsFrom1->source_dsa_obj_guid, 
322                                  &source->repsFrom1->source_dsa_obj_guid) == 0) {
323                         talloc_free(s2->repsFrom1->other_info);
324                         *s2->repsFrom1 = *source->repsFrom1;
325                         talloc_steal(s2, s2->repsFrom1->other_info);
326                         talloc_free(source);
327                         return WERR_OK;
328                 }
329         }
330
331         DLIST_ADD_END(*listp, source, struct dreplsrv_partition_source_dsa *);
332         return WERR_OK;
333 }
334
335 WERROR dreplsrv_partition_find_for_nc(struct dreplsrv_service *s,
336                                       struct GUID *nc_guid,
337                                       struct dom_sid *nc_sid,
338                                       const char *nc_dn_str,
339                                       struct dreplsrv_partition **_p)
340 {
341         struct dreplsrv_partition *p;
342         bool valid_sid, valid_guid;
343         struct dom_sid null_sid;
344         ZERO_STRUCT(null_sid);
345
346         SMB_ASSERT(_p);
347
348         valid_sid  = nc_sid && !dom_sid_equal(&null_sid, nc_sid);
349         valid_guid = nc_guid && !GUID_all_zero(nc_guid);
350
351         if (!valid_sid && !valid_guid && !nc_dn_str) {
352                 return WERR_DS_DRA_INVALID_PARAMETER;
353         }
354
355         for (p = s->partitions; p; p = p->next) {
356                 if ((valid_guid && GUID_equal(&p->nc.guid, nc_guid))
357                     || strequal(p->nc.dn, nc_dn_str)
358                     || (valid_sid && dom_sid_equal(&p->nc.sid, nc_sid)))
359                 {
360                         /* fill in he right guid and sid if possible */
361                         if (nc_guid && !valid_guid) {
362                                 dsdb_get_extended_dn_guid(p->dn, nc_guid, "GUID");
363                         }
364                         if (nc_sid && !valid_sid) {
365                                 dsdb_get_extended_dn_sid(p->dn, nc_sid, "SID");
366                         }
367                         *_p = p;
368                         return WERR_OK;
369                 }
370         }
371
372         return WERR_DS_DRA_BAD_NC;
373 }
374
375 WERROR dreplsrv_partition_source_dsa_by_guid(struct dreplsrv_partition *p,
376                                              const struct GUID *dsa_guid,
377                                              struct dreplsrv_partition_source_dsa **_dsa)
378 {
379         struct dreplsrv_partition_source_dsa *dsa;
380
381         SMB_ASSERT(dsa_guid != NULL);
382         SMB_ASSERT(!GUID_all_zero(dsa_guid));
383         SMB_ASSERT(_dsa);
384
385         for (dsa = p->sources; dsa; dsa = dsa->next) {
386                 if (GUID_equal(dsa_guid, &dsa->repsFrom1->source_dsa_obj_guid)) {
387                         *_dsa = dsa;
388                         return WERR_OK;
389                 }
390         }
391
392         return WERR_DS_DRA_NO_REPLICA;
393 }
394
395 WERROR dreplsrv_partition_source_dsa_by_dns(const struct dreplsrv_partition *p,
396                                             const char *dsa_dns,
397                                             struct dreplsrv_partition_source_dsa **_dsa)
398 {
399         struct dreplsrv_partition_source_dsa *dsa;
400
401         SMB_ASSERT(dsa_dns != NULL);
402         SMB_ASSERT(_dsa);
403
404         for (dsa = p->sources; dsa; dsa = dsa->next) {
405                 if (strequal(dsa_dns, dsa->repsFrom1->other_info->dns_name)) {
406                         *_dsa = dsa;
407                         return WERR_OK;
408                 }
409         }
410
411         return WERR_DS_DRA_NO_REPLICA;
412 }
413
414
415 /*
416   create a temporary dsa structure for a replication. This is needed
417   for the initial replication of a new partition, such as when a new
418   domain NC is created and we are a global catalog server
419  */
420 WERROR dreplsrv_partition_source_dsa_temporary(struct dreplsrv_partition *p,
421                                                TALLOC_CTX *mem_ctx,
422                                                const struct GUID *dsa_guid,
423                                                struct dreplsrv_partition_source_dsa **_dsa)
424 {
425         struct dreplsrv_partition_source_dsa *dsa;
426         WERROR werr;
427
428         dsa = talloc_zero(mem_ctx, struct dreplsrv_partition_source_dsa);
429         W_ERROR_HAVE_NO_MEMORY(dsa);
430
431         dsa->partition = p;
432         dsa->repsFrom1 = &dsa->_repsFromBlob.ctr.ctr1;
433         dsa->repsFrom1->replica_flags = 0;
434         dsa->repsFrom1->source_dsa_obj_guid = *dsa_guid;
435
436         dsa->repsFrom1->other_info = talloc_zero(dsa, struct repsFromTo1OtherInfo);
437         W_ERROR_HAVE_NO_MEMORY(dsa->repsFrom1->other_info);
438
439         dsa->repsFrom1->other_info->dns_name = samdb_ntds_msdcs_dns_name(p->service->samdb,
440                                                                          dsa->repsFrom1->other_info, dsa_guid);
441         W_ERROR_HAVE_NO_MEMORY(dsa->repsFrom1->other_info->dns_name);
442
443         werr = dreplsrv_out_connection_attach(p->service, dsa->repsFrom1, &dsa->conn);
444         if (!W_ERROR_IS_OK(werr)) {
445                 DEBUG(0,(__location__ ": Failed to attach connection to %s\n",
446                          ldb_dn_get_linearized(p->dn)));
447                 talloc_free(dsa);
448                 return werr;
449         }
450
451         *_dsa = dsa;
452
453         return WERR_OK;
454 }
455
456
457 static WERROR dreplsrv_refresh_partition(struct dreplsrv_service *s,
458                                          struct dreplsrv_partition *p)
459 {
460         WERROR status;
461         NTSTATUS ntstatus;
462         struct ldb_message_element *orf_el = NULL;
463         struct ldb_result *r = NULL;
464         unsigned int i;
465         int ret;
466         TALLOC_CTX *mem_ctx = talloc_new(p);
467         static const char *attrs[] = {
468                 "repsFrom",
469                 "repsTo",
470                 NULL
471         };
472         struct ldb_dn *dn;
473
474         DEBUG(4, ("dreplsrv_refresh_partition(%s)\n",
475                 ldb_dn_get_linearized(p->dn)));
476
477         ret = dsdb_search_dn(s->samdb, mem_ctx, &r, p->dn, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN);
478         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
479                 /* we haven't replicated the partition yet, but we
480                  * can fill in the guid, sid etc from the partition DN */
481                 dn = p->dn;
482         } else if (ret != LDB_SUCCESS) {
483                 talloc_free(mem_ctx);
484                 return WERR_FOOBAR;
485         } else {
486                 dn = r->msgs[0]->dn;
487         }
488         
489         talloc_free(discard_const(p->nc.dn));
490         ZERO_STRUCT(p->nc);
491         p->nc.dn        = ldb_dn_alloc_linearized(p, dn);
492         W_ERROR_HAVE_NO_MEMORY(p->nc.dn);
493         ntstatus = dsdb_get_extended_dn_guid(dn, &p->nc.guid, "GUID");
494         if (!NT_STATUS_IS_OK(ntstatus)) {
495                 DEBUG(0,(__location__ ": unable to get GUID for %s: %s\n",
496                          p->nc.dn, nt_errstr(ntstatus)));
497                 talloc_free(mem_ctx);
498                 return WERR_DS_DRA_INTERNAL_ERROR;
499         }
500         dsdb_get_extended_dn_sid(dn, &p->nc.sid, "SID");
501
502         talloc_free(p->uptodatevector.cursors);
503         talloc_free(p->uptodatevector_ex.cursors);
504         ZERO_STRUCT(p->uptodatevector);
505         ZERO_STRUCT(p->uptodatevector_ex);
506
507         ret = dsdb_load_udv_v2(s->samdb, p->dn, p, &p->uptodatevector.cursors, &p->uptodatevector.count);
508         if (ret != LDB_SUCCESS) {
509                 DEBUG(4,(__location__ ": no UDV available for %s\n", ldb_dn_get_linearized(p->dn)));
510         }
511
512         status = WERR_OK;
513
514         if (r != NULL && (orf_el = ldb_msg_find_element(r->msgs[0], "repsFrom"))) {
515                 for (i=0; i < orf_el->num_values; i++) {
516                         status = dreplsrv_partition_add_source_dsa(s, p, &p->sources, 
517                                                                    NULL, &orf_el->values[i]);
518                         W_ERROR_NOT_OK_GOTO_DONE(status);
519                 }
520         }
521
522         if (r != NULL && (orf_el = ldb_msg_find_element(r->msgs[0], "repsTo"))) {
523                 for (i=0; i < orf_el->num_values; i++) {
524                         status = dreplsrv_partition_add_source_dsa(s, p, &p->notifies, 
525                                                                    p->sources, &orf_el->values[i]);
526                         W_ERROR_NOT_OK_GOTO_DONE(status);
527                 }
528         }
529
530 done:
531         talloc_free(mem_ctx);
532         return status;
533 }
534
535 WERROR dreplsrv_refresh_partitions(struct dreplsrv_service *s)
536 {
537         WERROR status;
538         struct dreplsrv_partition *p;
539
540         for (p = s->partitions; p; p = p->next) {
541                 status = dreplsrv_refresh_partition(s, p);
542                 W_ERROR_NOT_OK_RETURN(status);
543         }
544
545         return WERR_OK;
546 }