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"
40 struct test_become_dc_state {
41 struct libnet_context *ctx;
42 struct torture_context *tctx;
43 const char *netbios_name;
45 struct cli_credentials *machine_account;
46 struct dsdb_schema *self_made_schema;
47 const struct dsdb_schema *schema;
49 struct ldb_context *ldb;
52 uint32_t object_count;
53 struct drsuapi_DsReplicaObjectListItemEx *first_object;
54 struct drsuapi_DsReplicaObjectListItemEx *last_object;
58 const char *samdb_ldb;
59 const char *domaindn_ldb;
60 const char *configdn_ldb;
61 const char *schemadn_ldb;
62 const char *secrets_ldb;
63 const char *templates_ldb;
64 const char *secrets_keytab;
65 const char *dns_keytab;
69 static NTSTATUS test_become_dc_check_options(void *private_data,
70 const struct libnet_BecomeDC_CheckOptions *o)
72 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
74 DEBUG(0,("Become DC [%s] of Domain[%s]/[%s]\n",
76 o->domain->netbios_name, o->domain->dns_name));
78 DEBUG(0,("Promotion Partner is Server[%s] from Site[%s]\n",
79 o->source_dsa->dns_name, o->source_dsa->site_name));
81 DEBUG(0,("Options:crossRef behavior_version[%u]\n"
82 "\tschema object_version[%u]\n"
83 "\tdomain behavior_version[%u]\n"
84 "\tdomain w2k3_update_revision[%u]\n",
85 o->forest->crossref_behavior_version,
86 o->forest->schema_object_version,
87 o->domain->behavior_version,
88 o->domain->w2k3_update_revision));
93 #include "lib/appweb/ejs/ejs.h"
94 #include "lib/appweb/ejs/ejsInternal.h"
95 #include "scripting/ejs/smbcalls.h"
100 static void test_ejs_exception(const char *reason)
102 Ejs *ep = ejsPtr(eid);
103 ejsSetErrorMsg(eid, "%s", reason);
104 fprintf(stderr, "%s", ep->error);
108 static int test_run_ejs(char *script)
110 EjsHandle handle = 0;
113 TALLOC_CTX *mem_ctx = talloc_new(NULL);
114 struct MprVar *return_var;
118 if (ejsOpen(NULL, NULL, NULL) != 0) {
119 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
124 smb_setup_ejs_functions(test_ejs_exception);
126 if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
127 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
132 mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
135 if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
136 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
137 if (ejs_error == 0) ejs_error = 127;
141 return_var = ejsGetReturnValue(eid);
142 ejs_error = mprVarToNumber(return_var);
146 talloc_free(mem_ctx);
150 static NTSTATUS test_become_dc_prepare_db_ejs(void *private_data,
151 const struct libnet_BecomeDC_PrepareDB *p)
153 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
158 DEBUG(0,("Provision for Become-DC test using EJS\n"));
160 DEBUG(0,("New Server[%s] in Site[%s]\n",
161 p->dest_dsa->dns_name, p->dest_dsa->site_name));
163 DEBUG(0,("DSA Instance [%s]\n"
165 "\tinvocationId[%s]\n",
166 p->dest_dsa->ntds_dn_str,
167 GUID_string(s, &p->dest_dsa->ntds_guid),
168 GUID_string(s, &p->dest_dsa->invocation_id)));
170 DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
171 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
172 lp_private_dir(s->tctx->lp_ctx),
175 s->path.secrets_keytab));
177 DEBUG(0,("Schema Partition[%s => %s]\n",
178 p->forest->schema_dn_str, s->path.schemadn_ldb));
180 DEBUG(0,("Config Partition[%s => %s]\n",
181 p->forest->config_dn_str, s->path.configdn_ldb));
183 DEBUG(0,("Domain Partition[%s => %s]\n",
184 p->domain->dn_str, s->path.domaindn_ldb));
186 ejs = talloc_asprintf(s,
187 "libinclude(\"base.js\");\n"
188 "libinclude(\"provision.js\");\n"
190 "function message() { print(vsprintf(arguments)); }\n"
192 "var subobj = provision_guess();\n"
193 "subobj.ROOTDN = \"%s\";\n"
194 "subobj.DOMAINDN = \"%s\";\n"
195 "subobj.DOMAINDN_LDB = \"%s\";\n"
196 "subobj.CONFIGDN = \"%s\";\n"
197 "subobj.CONFIGDN_LDB = \"%s\";\n"
198 "subobj.SCHEMADN = \"%s\";\n"
199 "subobj.SCHEMADN_LDB = \"%s\";\n"
200 "subobj.HOSTNAME = \"%s\";\n"
201 "subobj.REALM = \"%s\";\n"
202 "subobj.DOMAIN = \"%s\";\n"
203 "subobj.DEFAULTSITE = \"%s\";\n"
205 "subobj.KRBTGTPASS = \"_NOT_USED_\";\n"
206 "subobj.MACHINEPASS = \"%s\";\n"
207 "subobj.ADMINPASS = \"_NOT_USED_\";\n"
209 "var paths = provision_default_paths(subobj);\n"
210 "paths.samdb = \"%s\";\n"
211 "paths.secrets = \"%s\";\n"
212 "paths.templates = \"%s\";\n"
213 "paths.keytab = \"%s\";\n"
214 "paths.dns_keytab = \"%s\";\n"
216 "var system_session = system_session();\n"
218 "var ok = provision_become_dc(subobj, message, true, paths, system_session);\n"
222 p->forest->root_dn_str, /* subobj.ROOTDN */
223 p->domain->dn_str, /* subobj.DOMAINDN */
224 s->path.domaindn_ldb, /* subobj.DOMAINDN_LDB */
225 p->forest->config_dn_str, /* subobj.CONFIGDN */
226 s->path.configdn_ldb, /* subobj.CONFIGDN_LDB */
227 p->forest->schema_dn_str, /* subobj.SCHEMADN */
228 s->path.schemadn_ldb, /* subobj.SCHEMADN_LDB */
229 p->dest_dsa->netbios_name, /* subobj.HOSTNAME */
230 torture_join_dom_dns_name(s->tj),/* subobj.REALM */
231 torture_join_dom_netbios_name(s->tj),/* subobj.DOMAIN */
232 p->dest_dsa->site_name, /* subobj.DEFAULTSITE */
233 cli_credentials_get_password(s->machine_account),/* subobj.MACHINEPASS */
234 s->path.samdb_ldb, /* paths.samdb */
235 s->path.templates_ldb, /* paths.templates */
236 s->path.secrets_ldb, /* paths.secrets */
237 s->path.secrets_keytab, /* paths.keytab */
238 s->path.dns_keytab); /* paths.dns_keytab */
239 NT_STATUS_HAVE_NO_MEMORY(ejs);
241 ret = test_run_ejs(ejs);
243 DEBUG(0,("Failed to run ejs script: %d:\n%s",
246 return NT_STATUS_FOOBAR;
252 DEBUG(0,("Open the SAM LDB with system credentials: %s\n",
255 s->ldb = ldb_wrap_connect(s, s->tctx->lp_ctx, s->path.samdb_ldb,
256 system_session(s, s->tctx->lp_ctx),
259 DEBUG(0,("Failed to open '%s'\n",
261 return NT_STATUS_INTERNAL_DB_ERROR;
264 ok = samdb_set_ntds_invocation_id(s->ldb, &p->dest_dsa->invocation_id);
266 DEBUG(0,("Failed to set cached ntds invocationId\n"));
267 return NT_STATUS_FOOBAR;
269 ok = samdb_set_ntds_objectGUID(s->ldb, &p->dest_dsa->ntds_guid);
271 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
272 return NT_STATUS_FOOBAR;
278 #include "param/param.h"
280 #include "scripting/python/modules.h"
282 static NTSTATUS test_become_dc_prepare_db_py(void *private_data,
283 const struct libnet_BecomeDC_PrepareDB *p)
285 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
287 PyObject *provision_fn, *result, *parameters;
289 DEBUG(0,("Provision for Become-DC test using PYTHON\n"));
291 py_load_samba_modules();
294 py_update_path("bin"); /* FIXME: Can't assume this always runs in source/... */
296 provision_fn = PyImport_Import(PyString_FromString("samba.provision.provision"));
298 if (provision_fn == NULL) {
299 DEBUG(0, ("Unable to import provision Python module.\n"));
300 return NT_STATUS_UNSUCCESSFUL;
303 DEBUG(0,("New Server[%s] in Site[%s]\n",
304 p->dest_dsa->dns_name, p->dest_dsa->site_name));
306 DEBUG(0,("DSA Instance [%s]\n"
308 "\tinvocationId[%s]\n",
309 p->dest_dsa->ntds_dn_str,
310 GUID_string(s, &p->dest_dsa->ntds_guid),
311 GUID_string(s, &p->dest_dsa->invocation_id)));
313 DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
314 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
315 lp_private_dir(s->tctx->lp_ctx),
318 s->path.secrets_keytab));
320 DEBUG(0,("Schema Partition[%s => %s]\n",
321 p->forest->schema_dn_str, s->path.schemadn_ldb));
323 DEBUG(0,("Config Partition[%s => %s]\n",
324 p->forest->config_dn_str, s->path.configdn_ldb));
326 DEBUG(0,("Domain Partition[%s => %s]\n",
327 p->domain->dn_str, s->path.domaindn_ldb));
329 parameters = PyDict_New();
331 PyDict_SetItemString(parameters, "rootdn", PyString_FromString(p->forest->root_dn_str));
332 PyDict_SetItemString(parameters, "domaindn", PyString_FromString(p->domain->dn_str));
333 PyDict_SetItemString(parameters, "domaindn_ldb", PyString_FromString(s->path.domaindn_ldb));
334 PyDict_SetItemString(parameters, "configdn", PyString_FromString(p->forest->config_dn_str));
335 PyDict_SetItemString(parameters, "configdn_ldb", PyString_FromString(s->path.configdn_ldb));
336 PyDict_SetItemString(parameters, "schema_dn_str", PyString_FromString(p->forest->schema_dn_str));
337 PyDict_SetItemString(parameters, "schemadn_ldb", PyString_FromString(s->path.schemadn_ldb));
338 PyDict_SetItemString(parameters, "netbios_name", PyString_FromString(p->dest_dsa->netbios_name));
339 PyDict_SetItemString(parameters, "dnsname", PyString_FromString(p->dest_dsa->dns_name));
340 PyDict_SetItemString(parameters, "defaultsite", PyString_FromString(p->dest_dsa->site_name));
341 PyDict_SetItemString(parameters, "machinepass", PyString_FromString(cli_credentials_get_password(s->machine_account)));
342 PyDict_SetItemString(parameters, "samdb", PyString_FromString(s->path.samdb_ldb));
343 PyDict_SetItemString(parameters, "secrets_ldb", PyString_FromString(s->path.secrets_ldb));
344 PyDict_SetItemString(parameters, "secrets_keytab", PyString_FromString(s->path.secrets_keytab));
346 result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
348 Py_DECREF(parameters);
350 if (result == NULL) {
353 return NT_STATUS_UNSUCCESSFUL;
358 DEBUG(0,("Open the SAM LDB with system credentials: %s\n",
361 s->ldb = ldb_wrap_connect(s, s->tctx->lp_ctx, s->path.samdb_ldb,
362 system_session(s, s->tctx->lp_ctx),
365 DEBUG(0,("Failed to open '%s'\n",
367 return NT_STATUS_INTERNAL_DB_ERROR;
370 ok = samdb_set_ntds_invocation_id(s->ldb, &p->dest_dsa->invocation_id);
372 DEBUG(0,("Failed to set cached ntds invocationId\n"));
373 return NT_STATUS_FOOBAR;
375 ok = samdb_set_ntds_objectGUID(s->ldb, &p->dest_dsa->ntds_guid);
377 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
378 return NT_STATUS_FOOBAR;
384 static NTSTATUS test_apply_schema(struct test_become_dc_state *s,
385 const struct libnet_BecomeDC_StoreChunk *c)
388 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
389 uint32_t total_object_count;
390 uint32_t object_count;
391 struct drsuapi_DsReplicaObjectListItemEx *first_object;
392 struct drsuapi_DsReplicaObjectListItemEx *cur;
393 uint32_t linked_attributes_count;
394 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
395 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
396 struct dsdb_extended_replicated_objects *objs;
397 struct repsFromTo1 *s_dsa;
399 struct ldb_message *msg;
400 struct ldb_val prefixMap_val;
401 struct ldb_message_element *prefixMap_el;
402 struct ldb_val schemaInfo_val;
407 DEBUG(0,("Analyze and apply schema objects\n"));
409 s_dsa = talloc_zero(s, struct repsFromTo1);
410 NT_STATUS_HAVE_NO_MEMORY(s_dsa);
411 s_dsa->other_info = talloc(s_dsa, struct repsFromTo1OtherInfo);
412 NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
414 switch (c->ctr_level) {
416 mapping_ctr = &c->ctr1->mapping_ctr;
417 total_object_count = c->ctr1->total_object_count;
418 object_count = s->schema_part.object_count;
419 first_object = s->schema_part.first_object;
420 linked_attributes_count = 0;
421 linked_attributes = NULL;
422 s_dsa->highwatermark = c->ctr1->new_highwatermark;
423 s_dsa->source_dsa_obj_guid = c->ctr1->source_dsa_guid;
424 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
425 uptodateness_vector = NULL; /* TODO: map it */
428 mapping_ctr = &c->ctr6->mapping_ctr;
429 total_object_count = c->ctr6->total_object_count;
430 object_count = s->schema_part.object_count;
431 first_object = s->schema_part.first_object;
432 linked_attributes_count = 0; /* TODO: ! */
433 linked_attributes = NULL; /* TODO: ! */;
434 s_dsa->highwatermark = c->ctr6->new_highwatermark;
435 s_dsa->source_dsa_obj_guid = c->ctr6->source_dsa_guid;
436 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
437 uptodateness_vector = c->ctr6->uptodateness_vector;
440 return NT_STATUS_INVALID_PARAMETER;
443 s_dsa->replica_flags = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
444 | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
445 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
446 memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
448 tmp_dns_name = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
449 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
450 tmp_dns_name = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
451 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
452 s_dsa->other_info->dns_name = tmp_dns_name;
454 for (cur = first_object; cur; cur = cur->next_object) {
455 bool is_attr = false;
456 bool is_class = false;
458 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
459 struct drsuapi_DsReplicaAttribute *a;
461 const char *oid = NULL;
463 a = &cur->object.attribute_ctr.attributes[i];
464 status = dsdb_map_int2oid(s->self_made_schema, a->attid, s, &oid);
465 if (!W_ERROR_IS_OK(status)) {
466 return werror_to_ntstatus(status);
470 case DRSUAPI_ATTRIBUTE_objectClass:
471 for (j=0; j < a->value_ctr.num_values; j++) {
472 uint32_t val = 0xFFFFFFFF;
474 if (a->value_ctr.values[i].blob
475 && a->value_ctr.values[i].blob->length == 4) {
476 val = IVAL(a->value_ctr.values[i].blob->data,0);
479 if (val == DRSUAPI_OBJECTCLASS_attributeSchema) {
482 if (val == DRSUAPI_OBJECTCLASS_classSchema) {
494 struct dsdb_attribute *sa;
496 sa = talloc_zero(s->self_made_schema, struct dsdb_attribute);
497 NT_STATUS_HAVE_NO_MEMORY(sa);
499 status = dsdb_attribute_from_drsuapi(s->self_made_schema, &cur->object, s, sa);
500 if (!W_ERROR_IS_OK(status)) {
501 return werror_to_ntstatus(status);
504 DLIST_ADD_END(s->self_made_schema->attributes, sa, struct dsdb_attribute *);
508 struct dsdb_class *sc;
510 sc = talloc_zero(s->self_made_schema, struct dsdb_class);
511 NT_STATUS_HAVE_NO_MEMORY(sc);
513 status = dsdb_class_from_drsuapi(s->self_made_schema, &cur->object, s, sc);
514 if (!W_ERROR_IS_OK(status)) {
515 return werror_to_ntstatus(status);
518 DLIST_ADD_END(s->self_made_schema->classes, sc, struct dsdb_class *);
522 /* attach the schema to the ldb */
523 ret = dsdb_set_schema(s->ldb, s->self_made_schema);
524 if (ret != LDB_SUCCESS) {
525 return NT_STATUS_FOOBAR;
527 /* we don't want to access the self made schema anymore */
528 s->self_made_schema = NULL;
529 s->schema = dsdb_get_schema(s->ldb);
531 status = dsdb_extended_replicated_objects_commit(s->ldb,
536 linked_attributes_count,
542 if (!W_ERROR_IS_OK(status)) {
543 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
544 return werror_to_ntstatus(status);
547 if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
548 for (i=0; i < objs->num_objects; i++) {
549 struct ldb_ldif ldif;
550 fprintf(stdout, "#\n");
551 ldif.changetype = LDB_CHANGETYPE_NONE;
552 ldif.msg = objs->objects[i].msg;
553 ldb_ldif_write_file(s->ldb, stdout, &ldif);
554 NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
558 msg = ldb_msg_new(objs);
559 NT_STATUS_HAVE_NO_MEMORY(msg);
560 msg->dn = objs->partition_dn;
562 status = dsdb_get_oid_mappings_ldb(s->schema, msg, &prefixMap_val, &schemaInfo_val);
563 if (!W_ERROR_IS_OK(status)) {
564 DEBUG(0,("Failed dsdb_get_oid_mappings_ldb(%s)\n", win_errstr(status)));
565 return werror_to_ntstatus(status);
568 /* we only add prefixMap here, because schemaInfo is a replicated attribute and already applied */
569 ret = ldb_msg_add_value(msg, "prefixMap", &prefixMap_val, &prefixMap_el);
570 if (ret != LDB_SUCCESS) {
571 return NT_STATUS_FOOBAR;
573 prefixMap_el->flags = LDB_FLAG_MOD_REPLACE;
575 ret = ldb_modify(s->ldb, msg);
576 if (ret != LDB_SUCCESS) {
577 DEBUG(0,("Failed to add prefixMap and schemaInfo %s\n", ldb_strerror(ret)));
578 return NT_STATUS_FOOBAR;
585 talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
588 DEBUG(0,("Reopen the SAM LDB with system credentials and a already stored schema: %s\n", s->path.samdb_ldb));
589 s->ldb = ldb_wrap_connect(s, s->tctx->lp_ctx, s->path.samdb_ldb,
590 system_session(s, s->tctx->lp_ctx),
593 DEBUG(0,("Failed to open '%s'\n",
595 return NT_STATUS_INTERNAL_DB_ERROR;
598 ok = samdb_set_ntds_invocation_id(s->ldb, &c->dest_dsa->invocation_id);
600 DEBUG(0,("Failed to set cached ntds invocationId\n"));
601 return NT_STATUS_FOOBAR;
603 ok = samdb_set_ntds_objectGUID(s->ldb, &c->dest_dsa->ntds_guid);
605 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
606 return NT_STATUS_FOOBAR;
609 s->schema = dsdb_get_schema(s->ldb);
611 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
612 return NT_STATUS_FOOBAR;
618 static NTSTATUS test_become_dc_schema_chunk(void *private_data,
619 const struct libnet_BecomeDC_StoreChunk *c)
621 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
623 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
624 uint32_t total_object_count;
625 uint32_t object_count;
626 struct drsuapi_DsReplicaObjectListItemEx *first_object;
627 struct drsuapi_DsReplicaObjectListItemEx *cur;
629 switch (c->ctr_level) {
631 mapping_ctr = &c->ctr1->mapping_ctr;
632 total_object_count = c->ctr1->total_object_count;
633 object_count = c->ctr1->object_count;
634 first_object = c->ctr1->first_object;
637 mapping_ctr = &c->ctr6->mapping_ctr;
638 total_object_count = c->ctr6->total_object_count;
639 object_count = c->ctr6->object_count;
640 first_object = c->ctr6->first_object;
643 return NT_STATUS_INVALID_PARAMETER;
646 if (total_object_count) {
647 DEBUG(0,("Schema-DN[%s] objects[%u/%u]\n",
648 c->partition->nc.dn, object_count, total_object_count));
650 DEBUG(0,("Schema-DN[%s] objects[%u]\n",
651 c->partition->nc.dn, object_count));
655 s->self_made_schema = talloc_zero(s, struct dsdb_schema);
656 NT_STATUS_HAVE_NO_MEMORY(s->self_made_schema);
658 status = dsdb_load_oid_mappings_drsuapi(s->self_made_schema, mapping_ctr);
659 if (!W_ERROR_IS_OK(status)) {
660 return werror_to_ntstatus(status);
663 s->schema = s->self_made_schema;
665 status = dsdb_verify_oid_mappings_drsuapi(s->schema, mapping_ctr);
666 if (!W_ERROR_IS_OK(status)) {
667 return werror_to_ntstatus(status);
671 if (!s->schema_part.first_object) {
672 s->schema_part.object_count = object_count;
673 s->schema_part.first_object = talloc_steal(s, first_object);
675 s->schema_part.object_count += object_count;
676 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
679 for (cur = first_object; cur->next_object; cur = cur->next_object) {}
680 s->schema_part.last_object = cur;
682 if (c->partition->highwatermark.tmp_highest_usn == c->partition->highwatermark.highest_usn) {
683 return test_apply_schema(s, c);
689 static NTSTATUS test_become_dc_store_chunk(void *private_data,
690 const struct libnet_BecomeDC_StoreChunk *c)
692 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
694 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
695 uint32_t total_object_count;
696 uint32_t object_count;
697 struct drsuapi_DsReplicaObjectListItemEx *first_object;
698 uint32_t linked_attributes_count;
699 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
700 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
701 struct dsdb_extended_replicated_objects *objs;
702 struct repsFromTo1 *s_dsa;
706 s_dsa = talloc_zero(s, struct repsFromTo1);
707 NT_STATUS_HAVE_NO_MEMORY(s_dsa);
708 s_dsa->other_info = talloc(s_dsa, struct repsFromTo1OtherInfo);
709 NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
711 switch (c->ctr_level) {
713 mapping_ctr = &c->ctr1->mapping_ctr;
714 total_object_count = c->ctr1->total_object_count;
715 object_count = c->ctr1->object_count;
716 first_object = c->ctr1->first_object;
717 linked_attributes_count = 0;
718 linked_attributes = NULL;
719 s_dsa->highwatermark = c->ctr1->new_highwatermark;
720 s_dsa->source_dsa_obj_guid = c->ctr1->source_dsa_guid;
721 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
722 uptodateness_vector = NULL; /* TODO: map it */
725 mapping_ctr = &c->ctr6->mapping_ctr;
726 total_object_count = c->ctr6->total_object_count;
727 object_count = c->ctr6->object_count;
728 first_object = c->ctr6->first_object;
729 linked_attributes_count = c->ctr6->linked_attributes_count;
730 linked_attributes = c->ctr6->linked_attributes;
731 s_dsa->highwatermark = c->ctr6->new_highwatermark;
732 s_dsa->source_dsa_obj_guid = c->ctr6->source_dsa_guid;
733 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
734 uptodateness_vector = c->ctr6->uptodateness_vector;
737 return NT_STATUS_INVALID_PARAMETER;
740 s_dsa->replica_flags = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
741 | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
742 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
743 memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
745 tmp_dns_name = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
746 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
747 tmp_dns_name = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
748 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
749 s_dsa->other_info->dns_name = tmp_dns_name;
751 if (total_object_count) {
752 DEBUG(0,("Partition[%s] objects[%u/%u]\n",
753 c->partition->nc.dn, object_count, total_object_count));
755 DEBUG(0,("Partition[%s] objects[%u]\n",
756 c->partition->nc.dn, object_count));
759 status = dsdb_extended_replicated_objects_commit(s->ldb,
764 linked_attributes_count,
770 if (!W_ERROR_IS_OK(status)) {
771 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
772 return werror_to_ntstatus(status);
775 if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
776 for (i=0; i < objs->num_objects; i++) {
777 struct ldb_ldif ldif;
778 fprintf(stdout, "#\n");
779 ldif.changetype = LDB_CHANGETYPE_NONE;
780 ldif.msg = objs->objects[i].msg;
781 ldb_ldif_write_file(s->ldb, stdout, &ldif);
782 NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
788 for (i=0; i < linked_attributes_count; i++) {
789 const struct dsdb_attribute *sa;
791 if (!linked_attributes[i].identifier) {
792 return NT_STATUS_FOOBAR;
795 if (!linked_attributes[i].value.blob) {
796 return NT_STATUS_FOOBAR;
799 sa = dsdb_attribute_by_attributeID_id(s->schema,
800 linked_attributes[i].attid);
802 return NT_STATUS_FOOBAR;
805 if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
806 DEBUG(0,("# %s\n", sa->lDAPDisplayName));
807 NDR_PRINT_DEBUG(drsuapi_DsReplicaLinkedAttribute, &linked_attributes[i]);
809 linked_attributes[i].value.blob->data,
810 linked_attributes[i].value.blob->length);
817 bool torture_net_become_dc(struct torture_context *torture)
821 struct libnet_BecomeDC b;
822 struct libnet_UnbecomeDC u;
823 struct test_become_dc_state *s;
824 struct ldb_message *msg;
828 s = talloc_zero(torture, struct test_become_dc_state);
829 if (!s) return false;
833 s->netbios_name = lp_parm_string(torture->lp_ctx, NULL, "become dc", "smbtorture dc");
834 if (!s->netbios_name || !s->netbios_name[0]) {
835 s->netbios_name = "smbtorturedc";
838 s->path.samdb_ldb = talloc_asprintf(s, "%s_samdb.ldb", s->netbios_name);
839 if (!s->path.samdb_ldb) return false;
840 s->path.domaindn_ldb = talloc_asprintf(s, "%s_domain.ldb", s->netbios_name);
841 if (!s->path.domaindn_ldb) return false;
842 s->path.configdn_ldb = talloc_asprintf(s, "%s_config.ldb", s->netbios_name);
843 if (!s->path.configdn_ldb) return false;
844 s->path.schemadn_ldb = talloc_asprintf(s, "%s_schema.ldb", s->netbios_name);
845 if (!s->path.schemadn_ldb) return false;
846 s->path.secrets_ldb = talloc_asprintf(s, "%s_secrets.ldb", s->netbios_name);
847 if (!s->path.secrets_ldb) return false;
848 s->path.templates_ldb = talloc_asprintf(s, "%s_templates.ldb", s->netbios_name);
849 if (!s->path.templates_ldb) return false;
850 s->path.secrets_keytab = talloc_asprintf(s, "%s_secrets.keytab", s->netbios_name);
851 if (!s->path.secrets_keytab) return false;
852 s->path.dns_keytab = talloc_asprintf(s, "%s_dns.keytab", s->netbios_name);
853 if (!s->path.dns_keytab) return false;
855 /* Join domain as a member server. */
856 s->tj = torture_join_domain(torture, s->netbios_name,
858 &s->machine_account);
860 DEBUG(0, ("%s failed to join domain as workstation\n",
865 s->ctx = libnet_context_init(torture->ev, torture->lp_ctx);
866 s->ctx->cred = cmdline_credentials;
868 s->ldb = ldb_init(s);
871 b.in.domain_dns_name = torture_join_dom_dns_name(s->tj);
872 b.in.domain_netbios_name = torture_join_dom_netbios_name(s->tj);
873 b.in.domain_sid = torture_join_sid(s->tj);
874 b.in.source_dsa_address = torture_setting_string(torture, "host", NULL);
875 b.in.dest_dsa_netbios_name = s->netbios_name;
877 b.in.callbacks.private_data = s;
878 b.in.callbacks.check_options = test_become_dc_check_options;
879 b.in.callbacks.prepare_db = test_become_dc_prepare_db_py;
880 if (getenv("PROVISION_EJS")) {
881 b.in.callbacks.prepare_db = test_become_dc_prepare_db_ejs;
883 b.in.callbacks.schema_chunk = test_become_dc_schema_chunk;
884 b.in.callbacks.config_chunk = test_become_dc_store_chunk;
885 b.in.callbacks.domain_chunk = test_become_dc_store_chunk;
887 status = libnet_BecomeDC(s->ctx, s, &b);
888 if (!NT_STATUS_IS_OK(status)) {
889 printf("libnet_BecomeDC() failed - %s\n", nt_errstr(status));
894 msg = ldb_msg_new(s);
896 printf("ldb_msg_new() failed\n");
900 msg->dn = ldb_dn_new(msg, s->ldb, "@ROOTDSE");
902 printf("ldb_msg_new(@ROOTDSE) failed\n");
907 ldb_ret = ldb_msg_add_string(msg, "isSynchronized", "TRUE");
908 if (ldb_ret != LDB_SUCCESS) {
909 printf("ldb_msg_add_string(msg, isSynchronized, TRUE) failed: %d\n", ldb_ret);
914 for (i=0; i < msg->num_elements; i++) {
915 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
918 printf("mark ROOTDSE with isSynchronized=TRUE\n");
919 ldb_ret = ldb_modify(s->ldb, msg);
920 if (ldb_ret != LDB_SUCCESS) {
921 printf("ldb_modify() failed: %d\n", ldb_ret);
927 talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
930 DEBUG(0,("Reopen the SAM LDB with system credentials and all replicated data: %s\n", s->path.samdb_ldb));
931 s->ldb = ldb_wrap_connect(s, torture->lp_ctx, s->path.samdb_ldb,
932 system_session(s, torture->lp_ctx),
935 DEBUG(0,("Failed to open '%s'\n",
941 s->schema = dsdb_get_schema(s->ldb);
943 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
948 if (lp_parm_bool(torture->lp_ctx, NULL, "become dc", "do not unjoin", false)) {
955 u.in.domain_dns_name = torture_join_dom_dns_name(s->tj);
956 u.in.domain_netbios_name = torture_join_dom_netbios_name(s->tj);
957 u.in.source_dsa_address = torture_setting_string(torture, "host", NULL);
958 u.in.dest_dsa_netbios_name = s->netbios_name;
960 status = libnet_UnbecomeDC(s->ctx, s, &u);
961 if (!NT_STATUS_IS_OK(status)) {
962 printf("libnet_UnbecomeDC() failed - %s\n", nt_errstr(status));
967 torture_leave_domain(s->tj);