2 Unix SMB/CIFS implementation.
4 libnet_BecomeDC() tests
6 Copyright (C) Stefan (metze) Metzmacher 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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/cmdline/popt_common.h"
25 #include "torture/torture.h"
26 #include "torture/rpc/rpc.h"
27 #include "libnet/libnet.h"
28 #include "lib/events/events.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "lib/util/dlinklist.h"
31 #include "lib/ldb/include/ldb.h"
32 #include "lib/ldb/include/ldb_errors.h"
33 #include "librpc/ndr/libndr.h"
34 #include "librpc/gen_ndr/ndr_drsuapi.h"
35 #include "librpc/gen_ndr/ndr_drsblobs.h"
36 #include "librpc/gen_ndr/ndr_misc.h"
37 #include "system/time.h"
38 #include "auth/auth.h"
39 #include "lib/db_wrap.h"
40 #include "lib/appweb/ejs/ejs.h"
41 #include "lib/appweb/ejs/ejsInternal.h"
42 #include "scripting/ejs/smbcalls.h"
47 static void test_ejs_exception(const char *reason)
49 Ejs *ep = ejsPtr(eid);
50 ejsSetErrorMsg(eid, "%s", reason);
51 fprintf(stderr, "%s", ep->error);
55 static int test_run_ejs(char *script)
60 TALLOC_CTX *mem_ctx = talloc_new(NULL);
61 struct MprVar *return_var;
65 if (ejsOpen(NULL, NULL, NULL) != 0) {
66 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
71 smb_setup_ejs_functions(test_ejs_exception);
73 if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
74 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
79 mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
82 if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
83 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
84 if (ejs_error == 0) ejs_error = 127;
88 return_var = ejsGetReturnValue(eid);
89 ejs_error = mprVarToNumber(return_var);
97 #define TORTURE_NETBIOS_NAME "smbtorturedc"
98 #define TORTURE_SAMDB_LDB "test_samdb.ldb"
100 struct test_become_dc_state {
101 struct libnet_context *ctx;
102 struct test_join *tj;
103 struct cli_credentials *machine_account;
104 struct dsdb_schema *schema;
106 struct ldb_context *ldb;
109 uint32_t object_count;
110 struct drsuapi_DsReplicaObjectListItemEx *first_object;
111 struct drsuapi_DsReplicaObjectListItemEx *last_object;
115 static NTSTATUS test_become_dc_check_options(void *private_data,
116 const struct libnet_BecomeDC_CheckOptions *o)
118 DEBUG(0,("Become DC of Domain[%s]/[%s]\n",
119 o->domain->netbios_name, o->domain->dns_name));
121 DEBUG(0,("Promotion Partner is Server[%s] from Site[%s]\n",
122 o->source_dsa->dns_name, o->source_dsa->site_name));
124 DEBUG(0,("Options:crossRef behavior_version[%u]\n"
125 "\tschema object_version[%u]\n"
126 "\tdomain behavior_version[%u]\n"
127 "\tdomain w2k3_update_revision[%u]\n",
128 o->forest->crossref_behavior_version,
129 o->forest->schema_object_version,
130 o->domain->behavior_version,
131 o->domain->w2k3_update_revision));
136 static NTSTATUS test_become_dc_prepare_db(void *private_data,
137 const struct libnet_BecomeDC_PrepareDB *p)
139 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
144 DEBUG(0,("New Server[%s] in Site[%s]\n",
145 p->dest_dsa->dns_name, p->dest_dsa->site_name));
147 DEBUG(0,("DSA Instance [%s]\n"
149 "\tinvocationId[%s]\n",
150 p->dest_dsa->ntds_dn_str,
151 GUID_string(s, &p->dest_dsa->ntds_guid),
152 GUID_string(s, &p->dest_dsa->invocation_id)));
154 DEBUG(0,("Schema Partition[%s]\n",
155 p->forest->schema_dn_str));
157 DEBUG(0,("Config Partition[%s]\n",
158 p->forest->config_dn_str));
160 DEBUG(0,("Domain Partition[%s]\n",
163 ejs = talloc_asprintf(s,
164 "libinclude(\"base.js\");\n"
165 "libinclude(\"provision.js\");\n"
167 "function message() { print(vsprintf(arguments)); }\n"
169 "var subobj = provision_guess();\n"
170 "subobj.ROOTDN = \"%s\";\n"
171 "subobj.DOMAINDN = \"%s\";\n"
172 "subobj.DOMAINDN_LDB = \"test_domain.ldb\";\n"
173 "subobj.CONFIGDN = \"%s\";\n"
174 "subobj.CONFIGDN_LDB = \"test_config.ldb\";\n"
175 "subobj.SCHEMADN = \"%s\";\n"
176 "subobj.SCHEMADN_LDB = \"test_schema.ldb\";\n"
177 "subobj.HOSTNAME = \"%s\";\n"
178 "subobj.DNSNAME = \"%s\";\n"
179 "subobj.DEFAULTSITE = \"%s\";\n"
181 "modules_list = new Array(\"rootdse\",\n"
183 " \"paged_results\",\n"
184 " \"server_sort\",\n"
185 " \"extended_dn\",\n"
188 " \"password_hash\",\n"
189 " \"operational\",\n"
190 " \"objectclass\",\n"
193 "subobj.MODULES_LIST = join(\",\", modules_list);\n"
194 "subobj.DOMAINDN_MOD = \"repl_meta_data\";\n"
195 "subobj.CONFIGDN_MOD = \"repl_meta_data\";\n"
196 "subobj.SCHEMADN_MOD = \"repl_meta_data\";\n"
198 "var paths = provision_default_paths(subobj);\n"
199 "paths.samdb = \"%s\";\n"
201 "var system_session = system_session();\n"
203 "var ok = provision_become_dc(subobj, message, paths, system_session);\n"
207 p->forest->root_dn_str,
209 p->forest->config_dn_str,
210 p->forest->schema_dn_str,
211 p->dest_dsa->netbios_name,
212 p->dest_dsa->dns_name,
213 p->dest_dsa->site_name,
215 NT_STATUS_HAVE_NO_MEMORY(ejs);
217 ret = test_run_ejs(ejs);
219 DEBUG(0,("Failed to run ejs script: %d:\n%s",
222 return NT_STATUS_FOOBAR;
228 s->ldb = ldb_wrap_connect(s, TORTURE_SAMDB_LDB,
232 DEBUG(0,("Failed to open '%s'\n",
234 return NT_STATUS_INTERNAL_DB_ERROR;
237 ok = samdb_set_ntds_invocation_id(s->ldb, &p->dest_dsa->invocation_id);
239 DEBUG(0,("Failed to set cached ntds invocationId\n"));
240 return NT_STATUS_FOOBAR;
242 ok = samdb_set_ntds_objectGUID(s->ldb, &p->dest_dsa->ntds_guid);
244 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
245 return NT_STATUS_FOOBAR;
251 static NTSTATUS test_apply_schema(struct test_become_dc_state *s,
252 const struct libnet_BecomeDC_StoreChunk *c)
255 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
256 uint32_t total_object_count;
257 uint32_t object_count;
258 struct drsuapi_DsReplicaObjectListItemEx *first_object;
259 struct drsuapi_DsReplicaObjectListItemEx *cur;
260 uint32_t linked_attributes_count;
261 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
262 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
263 struct dsdb_extended_replicated_objects *objs;
264 struct repsFromTo1 *s_dsa;
269 s_dsa = talloc_zero(s, struct repsFromTo1);
270 NT_STATUS_HAVE_NO_MEMORY(s_dsa);
271 s_dsa->other_info = talloc(s_dsa, struct repsFromTo1OtherInfo);
272 NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
274 switch (c->ctr_level) {
276 mapping_ctr = &c->ctr1->mapping_ctr;
277 total_object_count = c->ctr1->total_object_count;
278 object_count = s->schema_part.object_count;
279 first_object = s->schema_part.first_object;
280 linked_attributes_count = 0;
281 linked_attributes = NULL;
282 s_dsa->highwatermark = c->ctr1->new_highwatermark;
283 s_dsa->source_dsa_obj_guid = c->ctr1->source_dsa_guid;
284 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
285 uptodateness_vector = NULL; /* TODO: map it */
288 mapping_ctr = &c->ctr6->mapping_ctr;
289 total_object_count = c->ctr6->total_object_count;
290 object_count = s->schema_part.object_count;
291 first_object = s->schema_part.first_object;
292 linked_attributes_count = 0; /* TODO: ! */
293 linked_attributes = NULL; /* TODO: ! */;
294 s_dsa->highwatermark = c->ctr6->new_highwatermark;
295 s_dsa->source_dsa_obj_guid = c->ctr6->source_dsa_guid;
296 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
297 uptodateness_vector = c->ctr6->uptodateness_vector;
300 return NT_STATUS_INVALID_PARAMETER;
303 s_dsa->replica_flags = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
304 | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
305 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
306 memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
308 tmp_dns_name = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
309 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
310 tmp_dns_name = talloc_asprintf_append(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
311 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
312 s_dsa->other_info->dns_name = tmp_dns_name;
314 for (cur = first_object; cur; cur = cur->next_object) {
315 bool is_attr = false;
316 bool is_class = false;
318 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
319 struct drsuapi_DsReplicaAttribute *a;
321 const char *oid = NULL;
323 a = &cur->object.attribute_ctr.attributes[i];
324 status = dsdb_map_int2oid(s->schema, a->attid, s, &oid);
325 if (!W_ERROR_IS_OK(status)) {
326 return werror_to_ntstatus(status);
330 case DRSUAPI_ATTRIBUTE_objectClass:
331 for (j=0; j < a->value_ctr.num_values; j++) {
332 uint32_t val = 0xFFFFFFFF;
334 if (a->value_ctr.values[i].blob
335 && a->value_ctr.values[i].blob->length == 4) {
336 val = IVAL(a->value_ctr.values[i].blob->data,0);
339 if (val == DRSUAPI_OBJECTCLASS_attributeSchema) {
342 if (val == DRSUAPI_OBJECTCLASS_classSchema) {
354 struct dsdb_attribute *sa;
356 sa = talloc_zero(s->schema, struct dsdb_attribute);
357 NT_STATUS_HAVE_NO_MEMORY(sa);
359 status = dsdb_attribute_from_drsuapi(s->schema, &cur->object, s, sa);
360 if (!W_ERROR_IS_OK(status)) {
361 return werror_to_ntstatus(status);
364 DLIST_ADD_END(s->schema->attributes, sa, struct dsdb_attribute *);
368 struct dsdb_class *sc;
370 sc = talloc_zero(s->schema, struct dsdb_class);
371 NT_STATUS_HAVE_NO_MEMORY(sc);
373 status = dsdb_class_from_drsuapi(s->schema, &cur->object, s, sc);
374 if (!W_ERROR_IS_OK(status)) {
375 return werror_to_ntstatus(status);
378 DLIST_ADD_END(s->schema->classes, sc, struct dsdb_class *);
382 ret = dsdb_set_schema(s->ldb, s->schema);
383 if (ret != LDB_SUCCESS) {
384 return NT_STATUS_FOOBAR;
387 status = dsdb_extended_replicated_objects_commit(s->ldb,
393 linked_attributes_count,
398 if (!W_ERROR_IS_OK(status)) {
399 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
400 return werror_to_ntstatus(status);
403 if (lp_parm_bool(-1, "become dc", "dump objects", False)) {
404 for (i=0; i < objs->num_objects; i++) {
405 struct ldb_ldif ldif;
406 fprintf(stdout, "#\n");
407 ldif.changetype = LDB_CHANGETYPE_NONE;
408 ldif.msg = objs->objects[i].msg;
409 ldb_ldif_write_file(s->ldb, stdout, &ldif);
410 NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
419 static NTSTATUS test_become_dc_schema_chunk(void *private_data,
420 const struct libnet_BecomeDC_StoreChunk *c)
422 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
424 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
425 uint32_t total_object_count;
426 uint32_t object_count;
427 struct drsuapi_DsReplicaObjectListItemEx *first_object;
428 struct drsuapi_DsReplicaObjectListItemEx *cur;
430 switch (c->ctr_level) {
432 mapping_ctr = &c->ctr1->mapping_ctr;
433 total_object_count = c->ctr1->total_object_count;
434 object_count = c->ctr1->object_count;
435 first_object = c->ctr1->first_object;
438 mapping_ctr = &c->ctr6->mapping_ctr;
439 total_object_count = c->ctr6->total_object_count;
440 object_count = c->ctr6->object_count;
441 first_object = c->ctr6->first_object;
444 return NT_STATUS_INVALID_PARAMETER;
447 if (total_object_count) {
448 DEBUG(0,("Schema-DN[%s] objects[%u/%u]\n",
449 c->partition->nc.dn, object_count, total_object_count));
451 DEBUG(0,("Schema-DN[%s] objects[%u]\n",
452 c->partition->nc.dn, object_count));
456 s->schema = talloc_zero(s, struct dsdb_schema);
457 NT_STATUS_HAVE_NO_MEMORY(s->schema);
459 status = dsdb_load_oid_mappings(s->schema, mapping_ctr);
460 if (!W_ERROR_IS_OK(status)) {
461 return werror_to_ntstatus(status);
464 status = dsdb_verify_oid_mappings(s->schema, mapping_ctr);
465 if (!W_ERROR_IS_OK(status)) {
466 return werror_to_ntstatus(status);
470 if (!s->schema_part.first_object) {
471 s->schema_part.object_count = object_count;
472 s->schema_part.first_object = talloc_steal(s, first_object);
474 s->schema_part.object_count += object_count;
475 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
478 for (cur = first_object; cur->next_object; cur = cur->next_object) {}
479 s->schema_part.last_object = cur;
481 if (c->partition->highwatermark.tmp_highest_usn == c->partition->highwatermark.highest_usn) {
482 return test_apply_schema(s, c);
488 static NTSTATUS test_become_dc_store_chunk(void *private_data,
489 const struct libnet_BecomeDC_StoreChunk *c)
491 struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
493 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
494 uint32_t total_object_count;
495 uint32_t object_count;
496 struct drsuapi_DsReplicaObjectListItemEx *first_object;
497 uint32_t linked_attributes_count;
498 struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
499 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
500 struct dsdb_extended_replicated_objects *objs;
501 struct repsFromTo1 *s_dsa;
505 s_dsa = talloc_zero(s, struct repsFromTo1);
506 NT_STATUS_HAVE_NO_MEMORY(s_dsa);
507 s_dsa->other_info = talloc(s_dsa, struct repsFromTo1OtherInfo);
508 NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
510 switch (c->ctr_level) {
512 mapping_ctr = &c->ctr1->mapping_ctr;
513 total_object_count = c->ctr1->total_object_count;
514 object_count = c->ctr1->object_count;
515 first_object = c->ctr1->first_object;
516 linked_attributes_count = 0;
517 linked_attributes = NULL;
518 s_dsa->highwatermark = c->ctr1->new_highwatermark;
519 s_dsa->source_dsa_obj_guid = c->ctr1->source_dsa_guid;
520 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
521 uptodateness_vector = NULL; /* TODO: map it */
524 mapping_ctr = &c->ctr6->mapping_ctr;
525 total_object_count = c->ctr6->total_object_count;
526 object_count = c->ctr6->object_count;
527 first_object = c->ctr6->first_object;
528 linked_attributes_count = c->ctr6->linked_attributes_count;
529 linked_attributes = c->ctr6->linked_attributes;
530 s_dsa->highwatermark = c->ctr6->new_highwatermark;
531 s_dsa->source_dsa_obj_guid = c->ctr6->source_dsa_guid;
532 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
533 uptodateness_vector = c->ctr6->uptodateness_vector;
536 return NT_STATUS_INVALID_PARAMETER;
539 s_dsa->replica_flags = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
540 | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
541 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
542 memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
544 tmp_dns_name = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
545 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
546 tmp_dns_name = talloc_asprintf_append(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
547 NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
548 s_dsa->other_info->dns_name = tmp_dns_name;
550 if (total_object_count) {
551 DEBUG(0,("Partition[%s] objects[%u/%u]\n",
552 c->partition->nc.dn, object_count, total_object_count));
554 DEBUG(0,("Partition[%s] objects[%u]\n",
555 c->partition->nc.dn, object_count));
558 status = dsdb_extended_replicated_objects_commit(s->ldb,
564 linked_attributes_count,
569 if (!W_ERROR_IS_OK(status)) {
570 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
571 return werror_to_ntstatus(status);
574 if (lp_parm_bool(-1, "become dc", "dump objects", False)) {
575 for (i=0; i < objs->num_objects; i++) {
576 struct ldb_ldif ldif;
577 fprintf(stdout, "#\n");
578 ldif.changetype = LDB_CHANGETYPE_NONE;
579 ldif.msg = objs->objects[i].msg;
580 ldb_ldif_write_file(s->ldb, stdout, &ldif);
581 NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
587 for (i=0; i < linked_attributes_count; i++) {
588 const struct dsdb_attribute *sa;
590 if (!linked_attributes[i].identifier) {
591 return NT_STATUS_FOOBAR;
594 if (!linked_attributes[i].value.blob) {
595 return NT_STATUS_FOOBAR;
598 sa = dsdb_attribute_by_attributeID_id(s->schema,
599 linked_attributes[i].attid);
601 return NT_STATUS_FOOBAR;
604 if (lp_parm_bool(-1, "become dc", "dump objects", False)) {
605 DEBUG(0,("# %s\n", sa->lDAPDisplayName));
606 NDR_PRINT_DEBUG(drsuapi_DsReplicaLinkedAttribute, &linked_attributes[i]);
608 linked_attributes[i].value.blob->data,
609 linked_attributes[i].value.blob->length);
616 BOOL torture_net_become_dc(struct torture_context *torture)
620 struct libnet_BecomeDC b;
621 struct libnet_UnbecomeDC u;
622 struct test_become_dc_state *s;
624 s = talloc_zero(torture, struct test_become_dc_state);
625 if (!s) return False;
627 /* Join domain as a member server. */
628 s->tj = torture_join_domain(TORTURE_NETBIOS_NAME,
630 &s->machine_account);
632 DEBUG(0, ("%s failed to join domain as workstation\n",
633 TORTURE_NETBIOS_NAME));
637 s->ctx = libnet_context_init(event_context_init(s));
638 s->ctx->cred = cmdline_credentials;
640 s->ldb = ldb_init(s);
643 b.in.domain_dns_name = torture_join_dom_dns_name(s->tj);
644 b.in.domain_netbios_name = torture_join_dom_netbios_name(s->tj);
645 b.in.domain_sid = torture_join_sid(s->tj);
646 b.in.source_dsa_address = lp_parm_string(-1, "torture", "host");
647 b.in.dest_dsa_netbios_name = TORTURE_NETBIOS_NAME;
649 b.in.callbacks.private_data = s;
650 b.in.callbacks.check_options = test_become_dc_check_options;
651 b.in.callbacks.prepare_db = test_become_dc_prepare_db;
652 b.in.callbacks.schema_chunk = test_become_dc_schema_chunk;
653 b.in.callbacks.config_chunk = test_become_dc_store_chunk;
654 b.in.callbacks.domain_chunk = test_become_dc_store_chunk;
656 status = libnet_BecomeDC(s->ctx, s, &b);
657 if (!NT_STATUS_IS_OK(status)) {
658 printf("libnet_BecomeDC() failed - %s\n", nt_errstr(status));
663 u.in.domain_dns_name = torture_join_dom_dns_name(s->tj);
664 u.in.domain_netbios_name = torture_join_dom_netbios_name(s->tj);
665 u.in.source_dsa_address = lp_parm_string(-1, "torture", "host");
666 u.in.dest_dsa_netbios_name = TORTURE_NETBIOS_NAME;
668 status = libnet_UnbecomeDC(s->ctx, s, &u);
669 if (!NT_STATUS_IS_OK(status)) {
670 printf("libnet_UnbecomeDC() failed - %s\n", nt_errstr(status));
675 torture_leave_domain(s->tj);