Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[samba.git] / source4 / torture / libnet / libnet_BecomeDC.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libnet_BecomeDC() tests
5
6    Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
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 "lib/cmdline/popt_common.h"
24 #include "torture/torture.h"
25 #include "torture/rpc/rpc.h"
26 #include "libnet/libnet.h"
27 #include "lib/events/events.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "lib/util/dlinklist.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "lib/ldb/include/ldb_errors.h"
32 #include "librpc/ndr/libndr.h"
33 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 #include "librpc/gen_ndr/ndr_drsblobs.h"
35 #include "librpc/gen_ndr/ndr_misc.h"
36 #include "system/time.h"
37 #include "lib/ldb_wrap.h"
38 #include "auth/auth.h"
39 #include "param/param.h"
40 #include "torture/util.h"
41
42 struct test_become_dc_state {
43         struct libnet_context *ctx;
44         struct torture_context *tctx;
45         const char *netbios_name;
46         struct test_join *tj;
47         struct cli_credentials *machine_account;
48         struct dsdb_schema *self_made_schema;
49         const struct dsdb_schema *schema;
50
51         struct ldb_context *ldb;
52
53         struct {
54                 uint32_t object_count;
55                 struct drsuapi_DsReplicaObjectListItemEx *first_object;
56                 struct drsuapi_DsReplicaObjectListItemEx *last_object;
57         } schema_part;
58
59         struct {
60                 const char *samdb_ldb;
61                 const char *domaindn_ldb;
62                 const char *configdn_ldb;
63                 const char *schemadn_ldb;
64                 const char *secrets_ldb;
65                 const char *templates_ldb;
66                 const char *secrets_keytab;
67                 const char *dns_keytab;
68         } path;
69 };
70
71 static NTSTATUS test_become_dc_prepare_db(void *private_data,
72                                               const struct libnet_BecomeDC_PrepareDB *p)
73 {
74         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
75         struct provision_settings settings;
76
77         settings.dns_name = p->dest_dsa->dns_name;
78         settings.site_name = p->dest_dsa->site_name;
79         settings.root_dn_str = p->forest->root_dn_str;
80         settings.domain_dn_str = p->domain->dn_str;
81         settings.config_dn_str = p->forest->config_dn_str;
82         settings.schema_dn_str = p->forest->schema_dn_str;
83         settings.invocation_id = &p->dest_dsa->invocation_id;
84         settings.netbios_name = p->dest_dsa->netbios_name;
85         settings.realm = torture_join_dom_dns_name(s->tj);
86         settings.domain = torture_join_dom_netbios_name(s->tj);
87         settings.ntds_guid = &p->dest_dsa->ntds_guid;
88         settings.ntds_dn_str = p->dest_dsa->ntds_dn_str;
89         settings.machine_password = cli_credentials_get_password(s->machine_account);
90         settings.samdb_ldb = s->path.samdb_ldb;
91         settings.secrets_ldb = s->path.secrets_ldb;
92         settings.secrets_keytab = s->path.secrets_keytab;
93         settings.schemadn_ldb = s->path.schemadn_ldb;
94         settings.configdn_ldb = s->path.configdn_ldb;
95         settings.domaindn_ldb = s->path.domaindn_ldb;
96
97         return provision_bare(s, s->tctx->lp_ctx, &settings);
98 }
99
100 static NTSTATUS test_become_dc_check_options(void *private_data,
101                                              const struct libnet_BecomeDC_CheckOptions *o)
102 {
103         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
104
105         DEBUG(0,("Become DC [%s] of Domain[%s]/[%s]\n",
106                 s->netbios_name,
107                 o->domain->netbios_name, o->domain->dns_name));
108
109         DEBUG(0,("Promotion Partner is Server[%s] from Site[%s]\n",
110                 o->source_dsa->dns_name, o->source_dsa->site_name));
111
112         DEBUG(0,("Options:crossRef behavior_version[%u]\n"
113                        "\tschema object_version[%u]\n"
114                        "\tdomain behavior_version[%u]\n"
115                        "\tdomain w2k3_update_revision[%u]\n", 
116                 o->forest->crossref_behavior_version,
117                 o->forest->schema_object_version,
118                 o->domain->behavior_version,
119                 o->domain->w2k3_update_revision));
120
121         return NT_STATUS_OK;
122 }
123
124 static NTSTATUS test_apply_schema(struct test_become_dc_state *s,
125                                   const struct libnet_BecomeDC_StoreChunk *c)
126 {
127         WERROR status;
128         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
129         uint32_t total_object_count;
130         uint32_t object_count;
131         struct drsuapi_DsReplicaObjectListItemEx *first_object;
132         struct drsuapi_DsReplicaObjectListItemEx *cur;
133         uint32_t linked_attributes_count;
134         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
135         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
136         struct dsdb_extended_replicated_objects *objs;
137         struct repsFromTo1 *s_dsa;
138         char *tmp_dns_name;
139         struct ldb_message *msg;
140         struct ldb_val prefixMap_val;
141         struct ldb_message_element *prefixMap_el;
142         struct ldb_val schemaInfo_val;
143         uint32_t i;
144         int ret;
145         bool ok;
146
147         DEBUG(0,("Analyze and apply schema objects\n"));
148
149         s_dsa                   = talloc_zero(s, struct repsFromTo1);
150         NT_STATUS_HAVE_NO_MEMORY(s_dsa);
151         s_dsa->other_info       = talloc(s_dsa, struct repsFromTo1OtherInfo);
152         NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
153
154         switch (c->ctr_level) {
155         case 1:
156                 mapping_ctr                     = &c->ctr1->mapping_ctr;
157                 total_object_count              = c->ctr1->total_object_count;
158                 object_count                    = s->schema_part.object_count;
159                 first_object                    = s->schema_part.first_object;
160                 linked_attributes_count         = 0;
161                 linked_attributes               = NULL;
162                 s_dsa->highwatermark            = c->ctr1->new_highwatermark;
163                 s_dsa->source_dsa_obj_guid      = c->ctr1->source_dsa_guid;
164                 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
165                 uptodateness_vector             = NULL; /* TODO: map it */
166                 break;
167         case 6:
168                 mapping_ctr                     = &c->ctr6->mapping_ctr;
169                 total_object_count              = c->ctr6->total_object_count;
170                 object_count                    = s->schema_part.object_count;
171                 first_object                    = s->schema_part.first_object;
172                 linked_attributes_count         = 0; /* TODO: ! */
173                 linked_attributes               = NULL; /* TODO: ! */;
174                 s_dsa->highwatermark            = c->ctr6->new_highwatermark;
175                 s_dsa->source_dsa_obj_guid      = c->ctr6->source_dsa_guid;
176                 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
177                 uptodateness_vector             = c->ctr6->uptodateness_vector;
178                 break;
179         default:
180                 return NT_STATUS_INVALID_PARAMETER;
181         }
182
183         s_dsa->replica_flags            = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
184                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
185                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
186         memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
187
188         tmp_dns_name    = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
189         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
190         tmp_dns_name    = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
191         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
192         s_dsa->other_info->dns_name = tmp_dns_name;
193
194         for (cur = first_object; cur; cur = cur->next_object) {
195                 bool is_attr = false;
196                 bool is_class = false;
197
198                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
199                         struct drsuapi_DsReplicaAttribute *a;
200                         uint32_t j;
201                         const char *oid = NULL;
202
203                         a = &cur->object.attribute_ctr.attributes[i];
204                         status = dsdb_map_int2oid(s->self_made_schema, a->attid, s, &oid);
205                         if (!W_ERROR_IS_OK(status)) {
206                                 return werror_to_ntstatus(status);
207                         }
208
209                         switch (a->attid) {
210                         case DRSUAPI_ATTRIBUTE_objectClass:
211                                 for (j=0; j < a->value_ctr.num_values; j++) {
212                                         uint32_t val = 0xFFFFFFFF;
213
214                                         if (a->value_ctr.values[i].blob
215                                             && a->value_ctr.values[i].blob->length == 4) {
216                                                 val = IVAL(a->value_ctr.values[i].blob->data,0);
217                                         }
218
219                                         if (val == DRSUAPI_OBJECTCLASS_attributeSchema) {
220                                                 is_attr = true;
221                                         }
222                                         if (val == DRSUAPI_OBJECTCLASS_classSchema) {
223                                                 is_class = true;
224                                         }
225                                 }
226
227                                 break;
228                         default:
229                                 break;
230                         }
231                 }
232
233                 if (is_attr) {
234                         struct dsdb_attribute *sa;
235
236                         sa = talloc_zero(s->self_made_schema, struct dsdb_attribute);
237                         NT_STATUS_HAVE_NO_MEMORY(sa);
238
239                         status = dsdb_attribute_from_drsuapi(s->self_made_schema, &cur->object, s, sa);
240                         if (!W_ERROR_IS_OK(status)) {
241                                 return werror_to_ntstatus(status);
242                         }
243
244                         DLIST_ADD_END(s->self_made_schema->attributes, sa, struct dsdb_attribute *);
245                 }
246
247                 if (is_class) {
248                         struct dsdb_class *sc;
249
250                         sc = talloc_zero(s->self_made_schema, struct dsdb_class);
251                         NT_STATUS_HAVE_NO_MEMORY(sc);
252
253                         status = dsdb_class_from_drsuapi(s->self_made_schema, &cur->object, s, sc);
254                         if (!W_ERROR_IS_OK(status)) {
255                                 return werror_to_ntstatus(status);
256                         }
257
258                         DLIST_ADD_END(s->self_made_schema->classes, sc, struct dsdb_class *);
259                 }
260         }
261
262         /* attach the schema to the ldb */
263         ret = dsdb_set_schema(s->ldb, s->self_made_schema);
264         if (ret != LDB_SUCCESS) {
265                 return NT_STATUS_FOOBAR;
266         }
267         /* we don't want to access the self made schema anymore */
268         s->self_made_schema = NULL;
269         s->schema = dsdb_get_schema(s->ldb);
270
271         status = dsdb_extended_replicated_objects_commit(s->ldb,
272                                                          c->partition->nc.dn,
273                                                          mapping_ctr,
274                                                          object_count,
275                                                          first_object,
276                                                          linked_attributes_count,
277                                                          linked_attributes,
278                                                          s_dsa,
279                                                          uptodateness_vector,
280                                                          c->gensec_skey,
281                                                          s, &objs);
282         if (!W_ERROR_IS_OK(status)) {
283                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
284                 return werror_to_ntstatus(status);
285         }
286
287         if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
288                 for (i=0; i < objs->num_objects; i++) {
289                         struct ldb_ldif ldif;
290                         fprintf(stdout, "#\n");
291                         ldif.changetype = LDB_CHANGETYPE_NONE;
292                         ldif.msg = objs->objects[i].msg;
293                         ldb_ldif_write_file(s->ldb, stdout, &ldif);
294                         NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
295                 }
296         }
297
298         msg = ldb_msg_new(objs);
299         NT_STATUS_HAVE_NO_MEMORY(msg);
300         msg->dn = objs->partition_dn;
301
302         status = dsdb_get_oid_mappings_ldb(s->schema, msg, &prefixMap_val, &schemaInfo_val);
303         if (!W_ERROR_IS_OK(status)) {
304                 DEBUG(0,("Failed dsdb_get_oid_mappings_ldb(%s)\n", win_errstr(status)));
305                 return werror_to_ntstatus(status);
306         }
307
308         /* we only add prefixMap here, because schemaInfo is a replicated attribute and already applied */
309         ret = ldb_msg_add_value(msg, "prefixMap", &prefixMap_val, &prefixMap_el);
310         if (ret != LDB_SUCCESS) {
311                 return NT_STATUS_FOOBAR;
312         }
313         prefixMap_el->flags = LDB_FLAG_MOD_REPLACE;
314
315         ret = ldb_modify(s->ldb, msg);
316         if (ret != LDB_SUCCESS) {
317                 DEBUG(0,("Failed to add prefixMap and schemaInfo %s\n", ldb_strerror(ret)));
318                 return NT_STATUS_FOOBAR;
319         }
320
321         talloc_free(s_dsa);
322         talloc_free(objs);
323
324         /* reopen the ldb */
325         talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
326         s->schema = NULL;
327
328         DEBUG(0,("Reopen the SAM LDB with system credentials and a already stored schema: %s\n", s->path.samdb_ldb));
329         s->ldb = ldb_wrap_connect(s, s->tctx->lp_ctx, s->path.samdb_ldb,
330                                   system_session(s, s->tctx->lp_ctx),
331                                   NULL, 0, NULL);
332         if (!s->ldb) {
333                 DEBUG(0,("Failed to open '%s'\n",
334                         s->path.samdb_ldb));
335                 return NT_STATUS_INTERNAL_DB_ERROR;
336         }
337
338         ok = samdb_set_ntds_invocation_id(s->ldb, &c->dest_dsa->invocation_id);
339         if (!ok) {
340                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
341                 return NT_STATUS_FOOBAR;
342         }
343         ok = samdb_set_ntds_objectGUID(s->ldb, &c->dest_dsa->ntds_guid);
344         if (!ok) {
345                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
346                 return NT_STATUS_FOOBAR;
347         }
348
349         s->schema = dsdb_get_schema(s->ldb);
350         if (!s->schema) {
351                 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
352                 return NT_STATUS_FOOBAR;
353         }
354
355         return NT_STATUS_OK;
356 }
357
358 static NTSTATUS test_become_dc_schema_chunk(void *private_data,
359                                             const struct libnet_BecomeDC_StoreChunk *c)
360 {
361         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
362         WERROR status;
363         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
364         uint32_t total_object_count;
365         uint32_t object_count;
366         struct drsuapi_DsReplicaObjectListItemEx *first_object;
367         struct drsuapi_DsReplicaObjectListItemEx *cur;
368
369         switch (c->ctr_level) {
370         case 1:
371                 mapping_ctr             = &c->ctr1->mapping_ctr;
372                 total_object_count      = c->ctr1->total_object_count;
373                 object_count            = c->ctr1->object_count;
374                 first_object            = c->ctr1->first_object;
375                 break;
376         case 6:
377                 mapping_ctr             = &c->ctr6->mapping_ctr;
378                 total_object_count      = c->ctr6->total_object_count;
379                 object_count            = c->ctr6->object_count;
380                 first_object            = c->ctr6->first_object;
381                 break;
382         default:
383                 return NT_STATUS_INVALID_PARAMETER;
384         }
385
386         if (total_object_count) {
387                 DEBUG(0,("Schema-DN[%s] objects[%u/%u]\n",
388                         c->partition->nc.dn, object_count, total_object_count));
389         } else {
390                 DEBUG(0,("Schema-DN[%s] objects[%u]\n",
391                 c->partition->nc.dn, object_count));
392         }
393
394         if (!s->schema) {
395                 s->self_made_schema = talloc_zero(s, struct dsdb_schema);
396                 NT_STATUS_HAVE_NO_MEMORY(s->self_made_schema);
397
398                 status = dsdb_load_oid_mappings_drsuapi(s->self_made_schema, mapping_ctr);
399                 if (!W_ERROR_IS_OK(status)) {
400                         return werror_to_ntstatus(status);
401                 }
402
403                 s->schema = s->self_made_schema;
404         } else {
405                 status = dsdb_verify_oid_mappings_drsuapi(s->schema, mapping_ctr);
406                 if (!W_ERROR_IS_OK(status)) {
407                         return werror_to_ntstatus(status);
408                 }
409         }
410
411         if (!s->schema_part.first_object) {
412                 s->schema_part.object_count = object_count;
413                 s->schema_part.first_object = talloc_steal(s, first_object);
414         } else {
415                 s->schema_part.object_count             += object_count;
416                 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
417                                                                        first_object);
418         }
419         for (cur = first_object; cur->next_object; cur = cur->next_object) {}
420         s->schema_part.last_object = cur;
421
422         if (c->partition->highwatermark.tmp_highest_usn == c->partition->highwatermark.highest_usn) {
423                 return test_apply_schema(s, c);
424         }
425
426         return NT_STATUS_OK;
427 }
428
429 static NTSTATUS test_become_dc_store_chunk(void *private_data,
430                                            const struct libnet_BecomeDC_StoreChunk *c)
431 {
432         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
433         WERROR status;
434         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
435         uint32_t total_object_count;
436         uint32_t object_count;
437         struct drsuapi_DsReplicaObjectListItemEx *first_object;
438         uint32_t linked_attributes_count;
439         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
440         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
441         struct dsdb_extended_replicated_objects *objs;
442         struct repsFromTo1 *s_dsa;
443         char *tmp_dns_name;
444         uint32_t i;
445
446         s_dsa                   = talloc_zero(s, struct repsFromTo1);
447         NT_STATUS_HAVE_NO_MEMORY(s_dsa);
448         s_dsa->other_info       = talloc(s_dsa, struct repsFromTo1OtherInfo);
449         NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
450
451         switch (c->ctr_level) {
452         case 1:
453                 mapping_ctr                     = &c->ctr1->mapping_ctr;
454                 total_object_count              = c->ctr1->total_object_count;
455                 object_count                    = c->ctr1->object_count;
456                 first_object                    = c->ctr1->first_object;
457                 linked_attributes_count         = 0;
458                 linked_attributes               = NULL;
459                 s_dsa->highwatermark            = c->ctr1->new_highwatermark;
460                 s_dsa->source_dsa_obj_guid      = c->ctr1->source_dsa_guid;
461                 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
462                 uptodateness_vector             = NULL; /* TODO: map it */
463                 break;
464         case 6:
465                 mapping_ctr                     = &c->ctr6->mapping_ctr;
466                 total_object_count              = c->ctr6->total_object_count;
467                 object_count                    = c->ctr6->object_count;
468                 first_object                    = c->ctr6->first_object;
469                 linked_attributes_count         = c->ctr6->linked_attributes_count;
470                 linked_attributes               = c->ctr6->linked_attributes;
471                 s_dsa->highwatermark            = c->ctr6->new_highwatermark;
472                 s_dsa->source_dsa_obj_guid      = c->ctr6->source_dsa_guid;
473                 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
474                 uptodateness_vector             = c->ctr6->uptodateness_vector;
475                 break;
476         default:
477                 return NT_STATUS_INVALID_PARAMETER;
478         }
479
480         s_dsa->replica_flags            = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
481                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
482                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
483         memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
484
485         tmp_dns_name    = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
486         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
487         tmp_dns_name    = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
488         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
489         s_dsa->other_info->dns_name = tmp_dns_name;
490
491         if (total_object_count) {
492                 DEBUG(0,("Partition[%s] objects[%u/%u]\n",
493                         c->partition->nc.dn, object_count, total_object_count));
494         } else {
495                 DEBUG(0,("Partition[%s] objects[%u]\n",
496                 c->partition->nc.dn, object_count));
497         }
498
499         status = dsdb_extended_replicated_objects_commit(s->ldb,
500                                                          c->partition->nc.dn,
501                                                          mapping_ctr,
502                                                          object_count,
503                                                          first_object,
504                                                          linked_attributes_count,
505                                                          linked_attributes,
506                                                          s_dsa,
507                                                          uptodateness_vector,
508                                                          c->gensec_skey,
509                                                          s, &objs);
510         if (!W_ERROR_IS_OK(status)) {
511                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
512                 return werror_to_ntstatus(status);
513         }
514
515         if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
516                 for (i=0; i < objs->num_objects; i++) {
517                         struct ldb_ldif ldif;
518                         fprintf(stdout, "#\n");
519                         ldif.changetype = LDB_CHANGETYPE_NONE;
520                         ldif.msg = objs->objects[i].msg;
521                         ldb_ldif_write_file(s->ldb, stdout, &ldif);
522                         NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
523                 }
524         }
525         talloc_free(s_dsa);
526         talloc_free(objs);
527
528         for (i=0; i < linked_attributes_count; i++) {
529                 const struct dsdb_attribute *sa;
530
531                 if (!linked_attributes[i].identifier) {
532                         return NT_STATUS_FOOBAR;                
533                 }
534
535                 if (!linked_attributes[i].value.blob) {
536                         return NT_STATUS_FOOBAR;                
537                 }
538
539                 sa = dsdb_attribute_by_attributeID_id(s->schema,
540                                                       linked_attributes[i].attid);
541                 if (!sa) {
542                         return NT_STATUS_FOOBAR;
543                 }
544
545                 if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
546                         DEBUG(0,("# %s\n", sa->lDAPDisplayName));
547                         NDR_PRINT_DEBUG(drsuapi_DsReplicaLinkedAttribute, &linked_attributes[i]);
548                         dump_data(0,
549                                 linked_attributes[i].value.blob->data,
550                                 linked_attributes[i].value.blob->length);
551                 }
552         }
553
554         return NT_STATUS_OK;
555 }
556
557 bool torture_net_become_dc(struct torture_context *torture)
558 {
559         bool ret = true;
560         NTSTATUS status;
561         struct libnet_BecomeDC b;
562         struct libnet_UnbecomeDC u;
563         struct test_become_dc_state *s;
564         struct ldb_message *msg;
565         int ldb_ret;
566         uint32_t i;
567
568         s = talloc_zero(torture, struct test_become_dc_state);
569         if (!s) return false;
570
571         s->tctx = torture;
572
573         s->netbios_name = lp_parm_string(torture->lp_ctx, NULL, "become dc", "smbtorture dc");
574         if (!s->netbios_name || !s->netbios_name[0]) {
575                 s->netbios_name = "smbtorturedc";
576         }
577
578         s->path.samdb_ldb       = talloc_asprintf(s, "%s_samdb.ldb", s->netbios_name);
579         if (!s->path.samdb_ldb) return false;
580         s->path.domaindn_ldb    = talloc_asprintf(s, "%s_domain.ldb", s->netbios_name);
581         if (!s->path.domaindn_ldb) return false;
582         s->path.configdn_ldb    = talloc_asprintf(s, "%s_config.ldb", s->netbios_name);
583         if (!s->path.configdn_ldb) return false;
584         s->path.schemadn_ldb    = talloc_asprintf(s, "%s_schema.ldb", s->netbios_name);
585         if (!s->path.schemadn_ldb) return false;
586         s->path.secrets_ldb     = talloc_asprintf(s, "%s_secrets.ldb", s->netbios_name);
587         if (!s->path.secrets_ldb) return false;
588         s->path.templates_ldb   = talloc_asprintf(s, "%s_templates.ldb", s->netbios_name);
589         if (!s->path.templates_ldb) return false;
590         s->path.secrets_keytab  = talloc_asprintf(s, "%s_secrets.keytab", s->netbios_name);
591         if (!s->path.secrets_keytab) return false;
592         s->path.dns_keytab      = talloc_asprintf(s, "%s_dns.keytab", s->netbios_name);
593         if (!s->path.dns_keytab) return false;
594
595         /* Join domain as a member server. */
596         s->tj = torture_join_domain(torture, s->netbios_name,
597                                  ACB_WSTRUST,
598                                  &s->machine_account);
599         if (!s->tj) {
600                 DEBUG(0, ("%s failed to join domain as workstation\n",
601                           s->netbios_name));
602                 return false;
603         }
604
605         s->ctx = libnet_context_init(torture->ev, torture->lp_ctx);
606         s->ctx->cred = cmdline_credentials;
607
608         s->ldb = ldb_init(s);
609
610         ZERO_STRUCT(b);
611         b.in.domain_dns_name            = torture_join_dom_dns_name(s->tj);
612         b.in.domain_netbios_name        = torture_join_dom_netbios_name(s->tj);
613         b.in.domain_sid                 = torture_join_sid(s->tj);
614         b.in.source_dsa_address         = torture_setting_string(torture, "host", NULL);
615         b.in.dest_dsa_netbios_name      = s->netbios_name;
616
617         b.in.callbacks.private_data     = s;
618         b.in.callbacks.check_options    = test_become_dc_check_options;
619         b.in.callbacks.prepare_db = test_become_dc_prepare_db;
620         b.in.callbacks.schema_chunk     = test_become_dc_schema_chunk;
621         b.in.callbacks.config_chunk     = test_become_dc_store_chunk;
622         b.in.callbacks.domain_chunk     = test_become_dc_store_chunk;
623
624         status = libnet_BecomeDC(s->ctx, s, &b);
625         if (!NT_STATUS_IS_OK(status)) {
626                 printf("libnet_BecomeDC() failed - %s\n", nt_errstr(status));
627                 ret = false;
628                 goto cleanup;
629         }
630
631         msg = ldb_msg_new(s);
632         if (!msg) {
633                 printf("ldb_msg_new() failed\n");
634                 ret = false;
635                 goto cleanup;
636         }
637         msg->dn = ldb_dn_new(msg, s->ldb, "@ROOTDSE");
638         if (!msg->dn) {
639                 printf("ldb_msg_new(@ROOTDSE) failed\n");
640                 ret = false;
641                 goto cleanup;
642         }
643
644         ldb_ret = ldb_msg_add_string(msg, "isSynchronized", "TRUE");
645         if (ldb_ret != LDB_SUCCESS) {
646                 printf("ldb_msg_add_string(msg, isSynchronized, TRUE) failed: %d\n", ldb_ret);
647                 ret = false;
648                 goto cleanup;
649         }
650
651         for (i=0; i < msg->num_elements; i++) {
652                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
653         }
654
655         printf("mark ROOTDSE with isSynchronized=TRUE\n");
656         ldb_ret = ldb_modify(s->ldb, msg);
657         if (ldb_ret != LDB_SUCCESS) {
658                 printf("ldb_modify() failed: %d\n", ldb_ret);
659                 ret = false;
660                 goto cleanup;
661         }
662         
663         /* reopen the ldb */
664         talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
665         s->schema = NULL;
666
667         DEBUG(0,("Reopen the SAM LDB with system credentials and all replicated data: %s\n", s->path.samdb_ldb));
668         s->ldb = ldb_wrap_connect(s, torture->lp_ctx, s->path.samdb_ldb,
669                                   system_session(s, torture->lp_ctx),
670                                   NULL, 0, NULL);
671         if (!s->ldb) {
672                 DEBUG(0,("Failed to open '%s'\n",
673                         s->path.samdb_ldb));
674                 ret = false;
675                 goto cleanup;
676         }
677
678         s->schema = dsdb_get_schema(s->ldb);
679         if (!s->schema) {
680                 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
681                 ret = false;
682                 goto cleanup;
683         }
684
685         if (lp_parm_bool(torture->lp_ctx, NULL, "become dc", "do not unjoin", false)) {
686                 talloc_free(s);
687                 return ret;
688         }
689
690 cleanup:
691         ZERO_STRUCT(u);
692         u.in.domain_dns_name            = torture_join_dom_dns_name(s->tj);
693         u.in.domain_netbios_name        = torture_join_dom_netbios_name(s->tj);
694         u.in.source_dsa_address         = torture_setting_string(torture, "host", NULL);
695         u.in.dest_dsa_netbios_name      = s->netbios_name;
696
697         status = libnet_UnbecomeDC(s->ctx, s, &u);
698         if (!NT_STATUS_IS_OK(status)) {
699                 printf("libnet_UnbecomeDC() failed - %s\n", nt_errstr(status));
700                 ret = false;
701         }
702
703         /* Leave domain. */                          
704         torture_leave_domain(s->tj);
705
706         talloc_free(s);
707         return ret;
708 }