2 Unix SMB/CIFS implementation.
4 libnet_BecomeDC() tests
6 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
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.
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.
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/>.
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 "auth/auth.h"
38 #include "lib/ldb_wrap.h"
39 #include "lib/appweb/ejs/ejs.h"
40 #include "lib/appweb/ejs/ejsInternal.h"
41 #include "scripting/ejs/smbcalls.h"
46 static void test_ejs_exception(const char *reason)
48 Ejs *ep = ejsPtr(eid);
49 ejsSetErrorMsg(eid, "%s", reason);
50 fprintf(stderr, "%s", ep->error);
54 static int test_run_ejs(char *script)
59 TALLOC_CTX *mem_ctx = talloc_new(NULL);
60 struct MprVar *return_var;
64 if (ejsOpen(NULL, NULL, NULL) != 0) {
65 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
70 smb_setup_ejs_functions(test_ejs_exception);
72 if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
73 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
78 mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
81 if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
82 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
83 if (ejs_error == 0) ejs_error = 127;
87 return_var = ejsGetReturnValue(eid);
88 ejs_error = mprVarToNumber(return_var);
96 struct test_become_dc_state {
97 struct libnet_context *ctx;
98 const char *netbios_name;
100 struct cli_credentials *machine_account;
101 struct dsdb_schema *self_made_schema;
102 const struct dsdb_schema *schema;
104 struct ldb_context *ldb;
107 uint32_t object_count;
108 struct drsuapi_DsReplicaObjectListItemEx *first_object;
109 struct drsuapi_DsReplicaObjectListItemEx *last_object;
113 const char *samdb_ldb;
114 const char *domaindn_ldb;
115 const char *configdn_ldb;
116 const char *schemadn_ldb;
117 const char *secrets_ldb;
118 const char *secrets_keytab;
122 static NTSTATUS test_become_dc_check_options(void *private_data,
123 const struct libnet_BecomeDC_CheckOptions *o)
125 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
127 DEBUG(0,("Become DC [%s] of Domain[%s]/[%s]\n",
129 o->domain->netbios_name, o->domain->dns_name));
131 DEBUG(0,("Promotion Partner is Server[%s] from Site[%s]\n",
132 o->source_dsa->dns_name, o->source_dsa->site_name));
134 DEBUG(0,("Options:crossRef behavior_version[%u]\n"
135 "\tschema object_version[%u]\n"
136 "\tdomain behavior_version[%u]\n"
137 "\tdomain w2k3_update_revision[%u]\n",
138 o->forest->crossref_behavior_version,
139 o->forest->schema_object_version,
140 o->domain->behavior_version,
141 o->domain->w2k3_update_revision));
146 static NTSTATUS test_become_dc_prepare_db(void *private_data,
147 const struct libnet_BecomeDC_PrepareDB *p)
149 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
154 DEBUG(0,("New Server[%s] in Site[%s]\n",
155 p->dest_dsa->dns_name, p->dest_dsa->site_name));
157 DEBUG(0,("DSA Instance [%s]\n"
159 "\tinvocationId[%s]\n",
160 p->dest_dsa->ntds_dn_str,
161 GUID_string(s, &p->dest_dsa->ntds_guid),
162 GUID_string(s, &p->dest_dsa->invocation_id)));
164 DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
165 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
166 lp_private_dir(global_loadparm),
169 s->path.secrets_keytab));
171 DEBUG(0,("Schema Partition[%s => %s]\n",
172 p->forest->schema_dn_str, s->path.schemadn_ldb));
174 DEBUG(0,("Config Partition[%s => %s]\n",
175 p->forest->config_dn_str, s->path.configdn_ldb));
177 DEBUG(0,("Domain Partition[%s => %s]\n",
178 p->domain->dn_str, s->path.domaindn_ldb));
180 ejs = talloc_asprintf(s,
181 "libinclude(\"base.js\");\n"
182 "libinclude(\"provision.js\");\n"
184 "function message() { print(vsprintf(arguments)); }\n"
186 "var subobj = provision_guess();\n"
187 "subobj.ROOTDN = \"%s\";\n"
188 "subobj.DOMAINDN = \"%s\";\n"
189 "subobj.DOMAINDN_LDB = \"%s\";\n"
190 "subobj.CONFIGDN = \"%s\";\n"
191 "subobj.CONFIGDN_LDB = \"%s\";\n"
192 "subobj.SCHEMADN = \"%s\";\n"
193 "subobj.SCHEMADN_LDB = \"%s\";\n"
194 "subobj.HOSTNAME = \"%s\";\n"
195 "subobj.DNSNAME = \"%s\";\n"
196 "subobj.DEFAULTSITE = \"%s\";\n"
198 "modules_list = new Array(\"rootdse\",\n"
200 " \"paged_results\",\n"
201 " \"server_sort\",\n"
202 " \"extended_dn\",\n"
205 " \"operational\",\n"
206 " \"objectclass\",\n"
208 " \"show_deleted\",\n"
210 "subobj.MODULES_LIST = join(\",\", modules_list);\n"
211 "subobj.DOMAINDN_MOD = \"pdc_fsmo,password_hash,repl_meta_data\";\n"
212 "subobj.CONFIGDN_MOD = \"naming_fsmo,repl_meta_data\";\n"
213 "subobj.SCHEMADN_MOD = \"schema_fsmo,repl_meta_data\";\n"
215 "subobj.KRBTGTPASS = \"_NOT_USED_\";\n"
216 "subobj.MACHINEPASS = \"%s\";\n"
217 "subobj.ADMINPASS = \"_NOT_USED_\";\n"
219 "var paths = provision_default_paths(subobj);\n"
220 "paths.samdb = \"%s\";\n"
221 "paths.secrets = \"%s\";\n"
222 "paths.keytab = \"%s\";\n"
224 "var system_session = system_session();\n"
226 "var ok = provision_become_dc(subobj, message, true, paths, system_session);\n"
230 p->forest->root_dn_str, /* subobj.ROOTDN */
231 p->domain->dn_str, /* subobj.DOMAINDN */
232 s->path.domaindn_ldb, /* subobj.DOMAINDN_LDB */
233 p->forest->config_dn_str, /* subobj.CONFIGDN */
234 s->path.configdn_ldb, /* subobj.CONFIGDN_LDB */
235 p->forest->schema_dn_str, /* subobj.SCHEMADN */
236 s->path.schemadn_ldb, /* subobj.SCHEMADN_LDB */
237 p->dest_dsa->netbios_name, /* subobj.HOSTNAME */
238 p->dest_dsa->dns_name, /* subobj.DNSNAME */
239 p->dest_dsa->site_name, /* subobj.DEFAULTSITE */
240 cli_credentials_get_password(s->machine_account),/* subobj.MACHINEPASS */
241 s->path.samdb_ldb, /* paths.samdb */
242 s->path.secrets_ldb, /* paths.secrets */
243 s->path.secrets_keytab); /* paths.keytab */
244 NT_STATUS_HAVE_NO_MEMORY(ejs);
246 ret = test_run_ejs(ejs);
248 DEBUG(0,("Failed to run ejs script: %d:\n%s",
251 return NT_STATUS_FOOBAR;
257 DEBUG(0,("Open the SAM LDB with system credentials: %s\n", s->path.samdb_ldb));
259 s->ldb = ldb_wrap_connect(s, global_loadparm, s->path.samdb_ldb,
263 DEBUG(0,("Failed to open '%s'\n",
265 return NT_STATUS_INTERNAL_DB_ERROR;
268 ok = samdb_set_ntds_invocation_id(s->ldb, &p->dest_dsa->invocation_id);
270 DEBUG(0,("Failed to set cached ntds invocationId\n"));
271 return NT_STATUS_FOOBAR;
273 ok = samdb_set_ntds_objectGUID(s->ldb, &p->dest_dsa->ntds_guid);
275 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
276 return NT_STATUS_FOOBAR;
282 static NTSTATUS test_apply_schema(struct test_become_dc_state *s,
283 const struct libnet_BecomeDC_StoreChunk *c)
286 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
287 uint32_t total_object_count;
288 uint32_t object_count;
289 struct drsuapi_DsReplicaObjectListItemEx *first_object;
290 struct drsuapi_DsReplicaObjectListItemEx *cur;
291 uint32_t linked_attributes_count;
292 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
293 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
294 struct dsdb_extended_replicated_objects *objs;
295 struct repsFromTo1 *s_dsa;
297 struct ldb_message *msg;
298 struct ldb_val prefixMap_val;
299 struct ldb_message_element *prefixMap_el;
300 struct ldb_val schemaInfo_val;
305 DEBUG(0,("Analyze and apply schema objects\n"));
307 s_dsa = talloc_zero(s, struct repsFromTo1);
308 NT_STATUS_HAVE_NO_MEMORY(s_dsa);
309 s_dsa->other_info = talloc(s_dsa, struct repsFromTo1OtherInfo);
310 NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
312 switch (c->ctr_level) {
314 mapping_ctr = &c->ctr1->mapping_ctr;
315 total_object_count = c->ctr1->total_object_count;
316 object_count = s->schema_part.object_count;
317 first_object = s->schema_part.first_object;
318 linked_attributes_count = 0;
319 linked_attributes = NULL;
320 s_dsa->highwatermark = c->ctr1->new_highwatermark;
321 s_dsa->source_dsa_obj_guid = c->ctr1->source_dsa_guid;
322 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
323 uptodateness_vector = NULL; /* TODO: map it */
326 mapping_ctr = &c->ctr6->mapping_ctr;
327 total_object_count = c->ctr6->total_object_count;
328 object_count = s->schema_part.object_count;
329 first_object = s->schema_part.first_object;
330 linked_attributes_count = 0; /* TODO: ! */
331 linked_attributes = NULL; /* TODO: ! */;
332 s_dsa->highwatermark = c->ctr6->new_highwatermark;
333 s_dsa->source_dsa_obj_guid = c->ctr6->source_dsa_guid;
334 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
335 uptodateness_vector = c->ctr6->uptodateness_vector;
338 return NT_STATUS_INVALID_PARAMETER;
341 s_dsa->replica_flags = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
342 | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
343 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
344 memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
346 tmp_dns_name = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
347 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
348 tmp_dns_name = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
349 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
350 s_dsa->other_info->dns_name = tmp_dns_name;
352 for (cur = first_object; cur; cur = cur->next_object) {
353 bool is_attr = false;
354 bool is_class = false;
356 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
357 struct drsuapi_DsReplicaAttribute *a;
359 const char *oid = NULL;
361 a = &cur->object.attribute_ctr.attributes[i];
362 status = dsdb_map_int2oid(s->self_made_schema, a->attid, s, &oid);
363 if (!W_ERROR_IS_OK(status)) {
364 return werror_to_ntstatus(status);
368 case DRSUAPI_ATTRIBUTE_objectClass:
369 for (j=0; j < a->value_ctr.num_values; j++) {
370 uint32_t val = 0xFFFFFFFF;
372 if (a->value_ctr.values[i].blob
373 && a->value_ctr.values[i].blob->length == 4) {
374 val = IVAL(a->value_ctr.values[i].blob->data,0);
377 if (val == DRSUAPI_OBJECTCLASS_attributeSchema) {
380 if (val == DRSUAPI_OBJECTCLASS_classSchema) {
392 struct dsdb_attribute *sa;
394 sa = talloc_zero(s->self_made_schema, struct dsdb_attribute);
395 NT_STATUS_HAVE_NO_MEMORY(sa);
397 status = dsdb_attribute_from_drsuapi(s->self_made_schema, &cur->object, s, sa);
398 if (!W_ERROR_IS_OK(status)) {
399 return werror_to_ntstatus(status);
402 DLIST_ADD_END(s->self_made_schema->attributes, sa, struct dsdb_attribute *);
406 struct dsdb_class *sc;
408 sc = talloc_zero(s->self_made_schema, struct dsdb_class);
409 NT_STATUS_HAVE_NO_MEMORY(sc);
411 status = dsdb_class_from_drsuapi(s->self_made_schema, &cur->object, s, sc);
412 if (!W_ERROR_IS_OK(status)) {
413 return werror_to_ntstatus(status);
416 DLIST_ADD_END(s->self_made_schema->classes, sc, struct dsdb_class *);
420 /* attach the schema to the ldb */
421 ret = dsdb_set_schema(s->ldb, s->self_made_schema);
422 if (ret != LDB_SUCCESS) {
423 return NT_STATUS_FOOBAR;
425 /* we don't want to access the self made schema anymore */
426 s->self_made_schema = NULL;
427 s->schema = dsdb_get_schema(s->ldb);
429 status = dsdb_extended_replicated_objects_commit(s->ldb,
434 linked_attributes_count,
440 if (!W_ERROR_IS_OK(status)) {
441 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
442 return werror_to_ntstatus(status);
445 if (lp_parm_bool(global_loadparm, NULL, "become dc", "dump objects", false)) {
446 for (i=0; i < objs->num_objects; i++) {
447 struct ldb_ldif ldif;
448 fprintf(stdout, "#\n");
449 ldif.changetype = LDB_CHANGETYPE_NONE;
450 ldif.msg = objs->objects[i].msg;
451 ldb_ldif_write_file(s->ldb, stdout, &ldif);
452 NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
456 msg = ldb_msg_new(objs);
457 NT_STATUS_HAVE_NO_MEMORY(msg);
458 msg->dn = objs->partition_dn;
460 status = dsdb_get_oid_mappings_ldb(s->schema, msg, &prefixMap_val, &schemaInfo_val);
461 if (!W_ERROR_IS_OK(status)) {
462 DEBUG(0,("Failed dsdb_get_oid_mappings_ldb(%s)\n", win_errstr(status)));
463 return werror_to_ntstatus(status);
466 /* we only add prefixMap here, because schemaInfo is a replicated attribute and already applied */
467 ret = ldb_msg_add_value(msg, "prefixMap", &prefixMap_val, &prefixMap_el);
468 if (ret != LDB_SUCCESS) {
469 return NT_STATUS_FOOBAR;
471 prefixMap_el->flags = LDB_FLAG_MOD_REPLACE;
473 ret = ldb_modify(s->ldb, msg);
474 if (ret != LDB_SUCCESS) {
475 DEBUG(0,("Failed to add prefixMap and schemaInfo %s\n", ldb_strerror(ret)));
476 return NT_STATUS_FOOBAR;
483 talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
486 DEBUG(0,("Reopen the SAM LDB with system credentials and a already stored schema: %s\n", s->path.samdb_ldb));
487 s->ldb = ldb_wrap_connect(s, global_loadparm, s->path.samdb_ldb,
491 DEBUG(0,("Failed to open '%s'\n",
493 return NT_STATUS_INTERNAL_DB_ERROR;
496 ok = samdb_set_ntds_invocation_id(s->ldb, &c->dest_dsa->invocation_id);
498 DEBUG(0,("Failed to set cached ntds invocationId\n"));
499 return NT_STATUS_FOOBAR;
501 ok = samdb_set_ntds_objectGUID(s->ldb, &c->dest_dsa->ntds_guid);
503 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
504 return NT_STATUS_FOOBAR;
507 s->schema = dsdb_get_schema(s->ldb);
509 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
510 return NT_STATUS_FOOBAR;
516 static NTSTATUS test_become_dc_schema_chunk(void *private_data,
517 const struct libnet_BecomeDC_StoreChunk *c)
519 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
521 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
522 uint32_t total_object_count;
523 uint32_t object_count;
524 struct drsuapi_DsReplicaObjectListItemEx *first_object;
525 struct drsuapi_DsReplicaObjectListItemEx *cur;
527 switch (c->ctr_level) {
529 mapping_ctr = &c->ctr1->mapping_ctr;
530 total_object_count = c->ctr1->total_object_count;
531 object_count = c->ctr1->object_count;
532 first_object = c->ctr1->first_object;
535 mapping_ctr = &c->ctr6->mapping_ctr;
536 total_object_count = c->ctr6->total_object_count;
537 object_count = c->ctr6->object_count;
538 first_object = c->ctr6->first_object;
541 return NT_STATUS_INVALID_PARAMETER;
544 if (total_object_count) {
545 DEBUG(0,("Schema-DN[%s] objects[%u/%u]\n",
546 c->partition->nc.dn, object_count, total_object_count));
548 DEBUG(0,("Schema-DN[%s] objects[%u]\n",
549 c->partition->nc.dn, object_count));
553 s->self_made_schema = talloc_zero(s, struct dsdb_schema);
554 NT_STATUS_HAVE_NO_MEMORY(s->self_made_schema);
556 status = dsdb_load_oid_mappings_drsuapi(s->self_made_schema, mapping_ctr);
557 if (!W_ERROR_IS_OK(status)) {
558 return werror_to_ntstatus(status);
561 s->schema = s->self_made_schema;
563 status = dsdb_verify_oid_mappings_drsuapi(s->schema, mapping_ctr);
564 if (!W_ERROR_IS_OK(status)) {
565 return werror_to_ntstatus(status);
569 if (!s->schema_part.first_object) {
570 s->schema_part.object_count = object_count;
571 s->schema_part.first_object = talloc_steal(s, first_object);
573 s->schema_part.object_count += object_count;
574 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
577 for (cur = first_object; cur->next_object; cur = cur->next_object) {}
578 s->schema_part.last_object = cur;
580 if (c->partition->highwatermark.tmp_highest_usn == c->partition->highwatermark.highest_usn) {
581 return test_apply_schema(s, c);
587 static NTSTATUS test_become_dc_store_chunk(void *private_data,
588 const struct libnet_BecomeDC_StoreChunk *c)
590 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
592 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
593 uint32_t total_object_count;
594 uint32_t object_count;
595 struct drsuapi_DsReplicaObjectListItemEx *first_object;
596 uint32_t linked_attributes_count;
597 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
598 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
599 struct dsdb_extended_replicated_objects *objs;
600 struct repsFromTo1 *s_dsa;
604 s_dsa = talloc_zero(s, struct repsFromTo1);
605 NT_STATUS_HAVE_NO_MEMORY(s_dsa);
606 s_dsa->other_info = talloc(s_dsa, struct repsFromTo1OtherInfo);
607 NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
609 switch (c->ctr_level) {
611 mapping_ctr = &c->ctr1->mapping_ctr;
612 total_object_count = c->ctr1->total_object_count;
613 object_count = c->ctr1->object_count;
614 first_object = c->ctr1->first_object;
615 linked_attributes_count = 0;
616 linked_attributes = NULL;
617 s_dsa->highwatermark = c->ctr1->new_highwatermark;
618 s_dsa->source_dsa_obj_guid = c->ctr1->source_dsa_guid;
619 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
620 uptodateness_vector = NULL; /* TODO: map it */
623 mapping_ctr = &c->ctr6->mapping_ctr;
624 total_object_count = c->ctr6->total_object_count;
625 object_count = c->ctr6->object_count;
626 first_object = c->ctr6->first_object;
627 linked_attributes_count = c->ctr6->linked_attributes_count;
628 linked_attributes = c->ctr6->linked_attributes;
629 s_dsa->highwatermark = c->ctr6->new_highwatermark;
630 s_dsa->source_dsa_obj_guid = c->ctr6->source_dsa_guid;
631 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
632 uptodateness_vector = c->ctr6->uptodateness_vector;
635 return NT_STATUS_INVALID_PARAMETER;
638 s_dsa->replica_flags = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
639 | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
640 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
641 memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
643 tmp_dns_name = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
644 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
645 tmp_dns_name = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
646 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
647 s_dsa->other_info->dns_name = tmp_dns_name;
649 if (total_object_count) {
650 DEBUG(0,("Partition[%s] objects[%u/%u]\n",
651 c->partition->nc.dn, object_count, total_object_count));
653 DEBUG(0,("Partition[%s] objects[%u]\n",
654 c->partition->nc.dn, object_count));
657 status = dsdb_extended_replicated_objects_commit(s->ldb,
662 linked_attributes_count,
668 if (!W_ERROR_IS_OK(status)) {
669 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
670 return werror_to_ntstatus(status);
673 if (lp_parm_bool(global_loadparm, NULL, "become dc", "dump objects", false)) {
674 for (i=0; i < objs->num_objects; i++) {
675 struct ldb_ldif ldif;
676 fprintf(stdout, "#\n");
677 ldif.changetype = LDB_CHANGETYPE_NONE;
678 ldif.msg = objs->objects[i].msg;
679 ldb_ldif_write_file(s->ldb, stdout, &ldif);
680 NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
686 for (i=0; i < linked_attributes_count; i++) {
687 const struct dsdb_attribute *sa;
689 if (!linked_attributes[i].identifier) {
690 return NT_STATUS_FOOBAR;
693 if (!linked_attributes[i].value.blob) {
694 return NT_STATUS_FOOBAR;
697 sa = dsdb_attribute_by_attributeID_id(s->schema,
698 linked_attributes[i].attid);
700 return NT_STATUS_FOOBAR;
703 if (lp_parm_bool(global_loadparm, NULL, "become dc", "dump objects", false)) {
704 DEBUG(0,("# %s\n", sa->lDAPDisplayName));
705 NDR_PRINT_DEBUG(drsuapi_DsReplicaLinkedAttribute, &linked_attributes[i]);
707 linked_attributes[i].value.blob->data,
708 linked_attributes[i].value.blob->length);
715 bool torture_net_become_dc(struct torture_context *torture)
719 struct libnet_BecomeDC b;
720 struct libnet_UnbecomeDC u;
721 struct test_become_dc_state *s;
722 struct ldb_message *msg;
726 s = talloc_zero(torture, struct test_become_dc_state);
727 if (!s) return false;
729 s->netbios_name = lp_parm_string(global_loadparm, NULL, "become dc", "smbtorture dc");
730 if (!s->netbios_name || !s->netbios_name[0]) {
731 s->netbios_name = "smbtorturedc";
734 s->path.samdb_ldb = talloc_asprintf(s, "%s_samdb.ldb", s->netbios_name);
735 if (!s->path.samdb_ldb) return false;
736 s->path.domaindn_ldb = talloc_asprintf(s, "%s_domain.ldb", s->netbios_name);
737 if (!s->path.domaindn_ldb) return false;
738 s->path.configdn_ldb = talloc_asprintf(s, "%s_config.ldb", s->netbios_name);
739 if (!s->path.configdn_ldb) return false;
740 s->path.schemadn_ldb = talloc_asprintf(s, "%s_schema.ldb", s->netbios_name);
741 if (!s->path.schemadn_ldb) return false;
742 s->path.secrets_ldb = talloc_asprintf(s, "%s_secrets.ldb", s->netbios_name);
743 if (!s->path.secrets_ldb) return false;
744 s->path.secrets_keytab = talloc_asprintf(s, "%s_secrets.keytab", s->netbios_name);
745 if (!s->path.secrets_keytab) return false;
747 /* Join domain as a member server. */
748 s->tj = torture_join_domain(s->netbios_name,
750 &s->machine_account);
752 DEBUG(0, ("%s failed to join domain as workstation\n",
757 s->ctx = libnet_context_init(torture->ev);
758 s->ctx->cred = cmdline_credentials;
760 s->ldb = ldb_init(s);
763 b.in.domain_dns_name = torture_join_dom_dns_name(s->tj);
764 b.in.domain_netbios_name = torture_join_dom_netbios_name(s->tj);
765 b.in.domain_sid = torture_join_sid(s->tj);
766 b.in.source_dsa_address = torture_setting_string(torture, "host", NULL);
767 b.in.dest_dsa_netbios_name = s->netbios_name;
769 b.in.callbacks.private_data = s;
770 b.in.callbacks.check_options = test_become_dc_check_options;
771 b.in.callbacks.prepare_db = test_become_dc_prepare_db;
772 b.in.callbacks.schema_chunk = test_become_dc_schema_chunk;
773 b.in.callbacks.config_chunk = test_become_dc_store_chunk;
774 b.in.callbacks.domain_chunk = test_become_dc_store_chunk;
776 status = libnet_BecomeDC(s->ctx, s, &b);
777 if (!NT_STATUS_IS_OK(status)) {
778 printf("libnet_BecomeDC() failed - %s\n", nt_errstr(status));
783 msg = ldb_msg_new(s);
785 printf("ldb_msg_new() failed\n");
789 msg->dn = ldb_dn_new(msg, s->ldb, "@ROOTDSE");
791 printf("ldb_msg_new(@ROOTDSE) failed\n");
796 ldb_ret = ldb_msg_add_string(msg, "isSynchronized", "TRUE");
797 if (ldb_ret != LDB_SUCCESS) {
798 printf("ldb_msg_add_string(msg, isSynchronized, TRUE) failed: %d\n", ldb_ret);
803 for (i=0; i < msg->num_elements; i++) {
804 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
807 printf("mark ROOTDSE with isSynchronized=TRUE\n");
808 ldb_ret = ldb_modify(s->ldb, msg);
809 if (ldb_ret != LDB_SUCCESS) {
810 printf("ldb_modify() failed: %d\n", ldb_ret);
816 talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
819 DEBUG(0,("Reopen the SAM LDB with system credentials and all replicated data: %s\n", s->path.samdb_ldb));
820 s->ldb = ldb_wrap_connect(s, global_loadparm, s->path.samdb_ldb,
824 DEBUG(0,("Failed to open '%s'\n",
830 s->schema = dsdb_get_schema(s->ldb);
832 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
837 if (lp_parm_bool(global_loadparm, NULL, "become dc", "do not unjoin", false)) {
844 u.in.domain_dns_name = torture_join_dom_dns_name(s->tj);
845 u.in.domain_netbios_name = torture_join_dom_netbios_name(s->tj);
846 u.in.source_dsa_address = torture_setting_string(torture, "host", NULL);
847 u.in.dest_dsa_netbios_name = s->netbios_name;
849 status = libnet_UnbecomeDC(s->ctx, s, &u);
850 if (!NT_STATUS_IS_OK(status)) {
851 printf("libnet_UnbecomeDC() failed - %s\n", nt_errstr(status));
856 torture_leave_domain(s->tj);