r20868: load the pdc_fsmo and naming_fsmo modules
[ira/wip.git] / source / torture / libnet / libnet_BecomeDC.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libnet_BecomeDC() tests
5
6    Copyright (C) Stefan (metze) Metzmacher 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
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"
43
44 static EjsId eid;
45 static int ejs_error;
46
47 static void test_ejs_exception(const char *reason)
48 {
49         Ejs *ep = ejsPtr(eid);
50         ejsSetErrorMsg(eid, "%s", reason);
51         fprintf(stderr, "%s", ep->error);
52         ejs_error = 127;
53 }
54
55 static int test_run_ejs(char *script)
56 {
57         EjsHandle handle = 0;
58         MprVar result;
59         char *emsg;
60         TALLOC_CTX *mem_ctx = talloc_new(NULL);
61         struct MprVar *return_var;
62
63         mprSetCtx(mem_ctx);
64
65         if (ejsOpen(NULL, NULL, NULL) != 0) {
66                 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
67                 ejs_error = 127;
68                 goto failed;
69         }
70
71         smb_setup_ejs_functions(test_ejs_exception);
72
73         if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
74                 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
75                 ejs_error = 127;
76                 goto failed;
77         }
78
79         mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
80
81         /* run the script */
82         if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
83                 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
84                 if (ejs_error == 0) ejs_error = 127;
85                 goto failed;
86         }
87
88         return_var = ejsGetReturnValue(eid);
89         ejs_error = mprVarToNumber(return_var);
90
91 failed:
92         ejsClose();
93         talloc_free(mem_ctx);
94         return ejs_error;
95 }
96
97 #define TORTURE_NETBIOS_NAME "smbtorturedc"
98 #define TORTURE_SAMDB_LDB "test_samdb.ldb"
99 #define TORTURE_SECRETS_LDB "test_secrets.ldb"
100 #define TORTURE_SECRETS_KEYTAB "test_secrets.keytab"
101
102 struct test_become_dc_state {
103         struct libnet_context *ctx;
104         struct test_join *tj;
105         struct cli_credentials *machine_account;
106         struct dsdb_schema *self_made_schema;
107         const struct dsdb_schema *schema;
108
109         struct ldb_context *ldb;
110
111         struct {
112                 uint32_t object_count;
113                 struct drsuapi_DsReplicaObjectListItemEx *first_object;
114                 struct drsuapi_DsReplicaObjectListItemEx *last_object;
115         } schema_part;
116 };
117
118 static NTSTATUS test_become_dc_check_options(void *private_data,
119                                              const struct libnet_BecomeDC_CheckOptions *o)
120 {
121         DEBUG(0,("Become DC of Domain[%s]/[%s]\n",
122                 o->domain->netbios_name, o->domain->dns_name));
123
124         DEBUG(0,("Promotion Partner is Server[%s] from Site[%s]\n",
125                 o->source_dsa->dns_name, o->source_dsa->site_name));
126
127         DEBUG(0,("Options:crossRef behavior_version[%u]\n"
128                        "\tschema object_version[%u]\n"
129                        "\tdomain behavior_version[%u]\n"
130                        "\tdomain w2k3_update_revision[%u]\n", 
131                 o->forest->crossref_behavior_version,
132                 o->forest->schema_object_version,
133                 o->domain->behavior_version,
134                 o->domain->w2k3_update_revision));
135
136         return NT_STATUS_OK;
137 }
138
139 static NTSTATUS test_become_dc_prepare_db(void *private_data,
140                                           const struct libnet_BecomeDC_PrepareDB *p)
141 {
142         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
143         char *ejs;
144         int ret;
145         bool ok;
146
147         DEBUG(0,("New Server[%s] in Site[%s]\n",
148                 p->dest_dsa->dns_name, p->dest_dsa->site_name));
149
150         DEBUG(0,("DSA Instance [%s]\n"
151                 "\tobjectGUID[%s]\n"
152                 "\tinvocationId[%s]\n",
153                 p->dest_dsa->ntds_dn_str,
154                 GUID_string(s, &p->dest_dsa->ntds_guid),
155                 GUID_string(s, &p->dest_dsa->invocation_id)));
156
157         DEBUG(0,("Schema Partition[%s]\n",
158                 p->forest->schema_dn_str));
159
160         DEBUG(0,("Config Partition[%s]\n",
161                 p->forest->config_dn_str));
162
163         DEBUG(0,("Domain Partition[%s]\n",
164                 p->domain->dn_str));
165
166         ejs = talloc_asprintf(s,
167                 "libinclude(\"base.js\");\n"
168                 "libinclude(\"provision.js\");\n"
169                 "\n"
170                 "function message() { print(vsprintf(arguments)); }\n"
171                 "\n"
172                 "var subobj = provision_guess();\n"
173                 "subobj.ROOTDN       = \"%s\";\n"
174                 "subobj.DOMAINDN     = \"%s\";\n"
175                 "subobj.DOMAINDN_LDB = \"test_domain.ldb\";\n"
176                 "subobj.CONFIGDN     = \"%s\";\n"
177                 "subobj.CONFIGDN_LDB = \"test_config.ldb\";\n"
178                 "subobj.SCHEMADN     = \"%s\";\n"
179                 "subobj.SCHEMADN_LDB = \"test_schema.ldb\";\n"
180                 "subobj.HOSTNAME     = \"%s\";\n"
181                 "subobj.DNSNAME      = \"%s\";\n"
182                 "subobj.DEFAULTSITE  = \"%s\";\n"
183                 "\n"
184                 "modules_list        = new Array(\"rootdse\",\n"
185                 "                                \"kludge_acl\",\n"
186                 "                                \"paged_results\",\n"
187                 "                                \"server_sort\",\n"
188                 "                                \"extended_dn\",\n"
189                 "                                \"asq\",\n"
190                 "                                \"samldb\",\n"
191                 "                                \"password_hash\",\n"
192                 "                                \"operational\",\n"
193                 "                                \"objectclass\",\n"
194                 "                                \"rdn_name\",\n"
195                 "                                \"partition\");\n"
196                 "subobj.MODULES_LIST = join(\",\", modules_list);\n"
197                 "subobj.DOMAINDN_MOD = \"pdc_fsmo,repl_meta_data\";\n"
198                 "subobj.CONFIGDN_MOD = \"naming_fsmo,repl_meta_data\";\n"
199                 "subobj.SCHEMADN_MOD = \"schema_fsmo,repl_meta_data\";\n"
200                 "\n"
201                 "subobj.KRBTGTPASS   = \"test\";\n"
202                 "subobj.MACHINEPASS  = \"test\";\n"
203                 "subobj.ADMINPASS    = \"test\";\n"
204                 "\n"
205                 "var paths = provision_default_paths(subobj);\n"
206                 "paths.samdb = \"%s\";\n"
207                 "paths.secrets = \"%s\";\n"
208                 "paths.keytab = \"%s\";\n"
209                 "\n"
210                 "var system_session = system_session();\n"
211                 "\n"
212                 "var ok = provision_become_dc(subobj, message, paths, system_session);\n"
213                 "assert(ok);\n"
214                 "\n"
215                 "return 0;\n",
216                 p->forest->root_dn_str,         /* subobj.ROOTDN */
217                 p->domain->dn_str,              /* subobj.DOMAINDN */
218                 p->forest->config_dn_str,       /* subobj.CONFIGDN */
219                 p->forest->schema_dn_str,       /* subobj.SCHEMADN */
220                 p->dest_dsa->netbios_name,      /* subobj.HOSTNAME */
221                 p->dest_dsa->dns_name,          /* subobj.DNSNAME */
222                 p->dest_dsa->site_name,         /* subobj.DEFAULTSITE */
223                 TORTURE_SAMDB_LDB,              /* paths.samdb */
224                 TORTURE_SECRETS_LDB,            /* paths.secrets */
225                 TORTURE_SECRETS_KEYTAB);        /* paths.keytab */
226         NT_STATUS_HAVE_NO_MEMORY(ejs);
227
228         ret = test_run_ejs(ejs);
229         if (ret != 0) {
230                 DEBUG(0,("Failed to run ejs script: %d:\n%s",
231                         ret, ejs));
232                 talloc_free(ejs);
233                 return NT_STATUS_FOOBAR;
234         }
235         talloc_free(ejs);
236
237         talloc_free(s->ldb);
238
239         DEBUG(0,("Open the SAM LDB with system credentials: %s\n", TORTURE_SAMDB_LDB));
240
241         s->ldb = ldb_wrap_connect(s, TORTURE_SAMDB_LDB,
242                                   system_session(s),
243                                   NULL, 0, NULL);
244         if (!s->ldb) {
245                 DEBUG(0,("Failed to open '%s'\n",
246                         TORTURE_SAMDB_LDB));
247                 return NT_STATUS_INTERNAL_DB_ERROR;
248         }
249
250         ok = samdb_set_ntds_invocation_id(s->ldb, &p->dest_dsa->invocation_id);
251         if (!ok) {
252                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
253                 return NT_STATUS_FOOBAR;
254         }
255         ok = samdb_set_ntds_objectGUID(s->ldb, &p->dest_dsa->ntds_guid);
256         if (!ok) {
257                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
258                 return NT_STATUS_FOOBAR;
259         }
260
261         return NT_STATUS_OK;
262 }
263
264 static NTSTATUS test_apply_schema(struct test_become_dc_state *s,
265                                   const struct libnet_BecomeDC_StoreChunk *c)
266 {
267         WERROR status;
268         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
269         uint32_t total_object_count;
270         uint32_t object_count;
271         struct drsuapi_DsReplicaObjectListItemEx *first_object;
272         struct drsuapi_DsReplicaObjectListItemEx *cur;
273         uint32_t linked_attributes_count;
274         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
275         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
276         struct dsdb_extended_replicated_objects *objs;
277         struct repsFromTo1 *s_dsa;
278         char *tmp_dns_name;
279         struct ldb_message *msg;
280         struct ldb_val prefixMap_val;
281         struct ldb_message_element *prefixMap_el;
282         struct ldb_val schemaInfo_val;
283         uint32_t i;
284         int ret;
285         bool ok;
286
287         DEBUG(0,("Analyze and apply schema objects\n"));
288
289         s_dsa                   = talloc_zero(s, struct repsFromTo1);
290         NT_STATUS_HAVE_NO_MEMORY(s_dsa);
291         s_dsa->other_info       = talloc(s_dsa, struct repsFromTo1OtherInfo);
292         NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
293
294         switch (c->ctr_level) {
295         case 1:
296                 mapping_ctr                     = &c->ctr1->mapping_ctr;
297                 total_object_count              = c->ctr1->total_object_count;
298                 object_count                    = s->schema_part.object_count;
299                 first_object                    = s->schema_part.first_object;
300                 linked_attributes_count         = 0;
301                 linked_attributes               = NULL;
302                 s_dsa->highwatermark            = c->ctr1->new_highwatermark;
303                 s_dsa->source_dsa_obj_guid      = c->ctr1->source_dsa_guid;
304                 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
305                 uptodateness_vector             = NULL; /* TODO: map it */
306                 break;
307         case 6:
308                 mapping_ctr                     = &c->ctr6->mapping_ctr;
309                 total_object_count              = c->ctr6->total_object_count;
310                 object_count                    = s->schema_part.object_count;
311                 first_object                    = s->schema_part.first_object;
312                 linked_attributes_count         = 0; /* TODO: ! */
313                 linked_attributes               = NULL; /* TODO: ! */;
314                 s_dsa->highwatermark            = c->ctr6->new_highwatermark;
315                 s_dsa->source_dsa_obj_guid      = c->ctr6->source_dsa_guid;
316                 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
317                 uptodateness_vector             = c->ctr6->uptodateness_vector;
318                 break;
319         default:
320                 return NT_STATUS_INVALID_PARAMETER;
321         }
322
323         s_dsa->replica_flags            = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
324                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
325                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
326         memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
327
328         tmp_dns_name    = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
329         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
330         tmp_dns_name    = talloc_asprintf_append(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
331         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
332         s_dsa->other_info->dns_name = tmp_dns_name;
333
334         for (cur = first_object; cur; cur = cur->next_object) {
335                 bool is_attr = false;
336                 bool is_class = false;
337
338                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
339                         struct drsuapi_DsReplicaAttribute *a;
340                         uint32_t j;
341                         const char *oid = NULL;
342
343                         a = &cur->object.attribute_ctr.attributes[i];
344                         status = dsdb_map_int2oid(s->self_made_schema, a->attid, s, &oid);
345                         if (!W_ERROR_IS_OK(status)) {
346                                 return werror_to_ntstatus(status);
347                         }
348
349                         switch (a->attid) {
350                         case DRSUAPI_ATTRIBUTE_objectClass:
351                                 for (j=0; j < a->value_ctr.num_values; j++) {
352                                         uint32_t val = 0xFFFFFFFF;
353
354                                         if (a->value_ctr.values[i].blob
355                                             && a->value_ctr.values[i].blob->length == 4) {
356                                                 val = IVAL(a->value_ctr.values[i].blob->data,0);
357                                         }
358
359                                         if (val == DRSUAPI_OBJECTCLASS_attributeSchema) {
360                                                 is_attr = true;
361                                         }
362                                         if (val == DRSUAPI_OBJECTCLASS_classSchema) {
363                                                 is_class = true;
364                                         }
365                                 }
366
367                                 break;
368                         default:
369                                 break;
370                         }
371                 }
372
373                 if (is_attr) {
374                         struct dsdb_attribute *sa;
375
376                         sa = talloc_zero(s->self_made_schema, struct dsdb_attribute);
377                         NT_STATUS_HAVE_NO_MEMORY(sa);
378
379                         status = dsdb_attribute_from_drsuapi(s->self_made_schema, &cur->object, s, sa);
380                         if (!W_ERROR_IS_OK(status)) {
381                                 return werror_to_ntstatus(status);
382                         }
383
384                         DLIST_ADD_END(s->self_made_schema->attributes, sa, struct dsdb_attribute *);
385                 }
386
387                 if (is_class) {
388                         struct dsdb_class *sc;
389
390                         sc = talloc_zero(s->self_made_schema, struct dsdb_class);
391                         NT_STATUS_HAVE_NO_MEMORY(sc);
392
393                         status = dsdb_class_from_drsuapi(s->self_made_schema, &cur->object, s, sc);
394                         if (!W_ERROR_IS_OK(status)) {
395                                 return werror_to_ntstatus(status);
396                         }
397
398                         DLIST_ADD_END(s->self_made_schema->classes, sc, struct dsdb_class *);
399                 }
400         }
401
402         /* attach the schema to the ldb */
403         ret = dsdb_set_schema(s->ldb, s->self_made_schema);
404         if (ret != LDB_SUCCESS) {
405                 return NT_STATUS_FOOBAR;
406         }
407         /* we don't want to access the self made schema anymore */
408         s->self_made_schema = NULL;
409         s->schema = dsdb_get_schema(s->ldb);
410
411         status = dsdb_extended_replicated_objects_commit(s->ldb,
412                                                          c->partition->nc.dn,
413                                                          mapping_ctr,
414                                                          object_count,
415                                                          first_object,
416                                                          linked_attributes_count,
417                                                          linked_attributes,
418                                                          s_dsa,
419                                                          uptodateness_vector,
420                                                          s, &objs);
421         if (!W_ERROR_IS_OK(status)) {
422                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
423                 return werror_to_ntstatus(status);
424         }
425
426         if (lp_parm_bool(-1, "become dc", "dump objects", False)) {
427                 for (i=0; i < objs->num_objects; i++) {
428                         struct ldb_ldif ldif;
429                         fprintf(stdout, "#\n");
430                         ldif.changetype = LDB_CHANGETYPE_NONE;
431                         ldif.msg = objs->objects[i].msg;
432                         ldb_ldif_write_file(s->ldb, stdout, &ldif);
433                         NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
434                 }
435         }
436
437         msg = ldb_msg_new(objs);
438         NT_STATUS_HAVE_NO_MEMORY(msg);
439         msg->dn = objs->partition_dn;
440
441         status = dsdb_get_oid_mappings_ldb(s->schema, msg, &prefixMap_val, &schemaInfo_val);
442         if (!W_ERROR_IS_OK(status)) {
443                 DEBUG(0,("Failed dsdb_get_oid_mappings_ldb(%s)\n", win_errstr(status)));
444                 return werror_to_ntstatus(status);
445         }
446
447         /* we only add prefixMap here, because schemaInfo is a replicated attribute and already applied */
448         ret = ldb_msg_add_value(msg, "prefixMap", &prefixMap_val, &prefixMap_el);
449         if (ret != LDB_SUCCESS) {
450                 return NT_STATUS_FOOBAR;
451         }
452         prefixMap_el->flags = LDB_FLAG_MOD_REPLACE;
453
454         ret = ldb_modify(s->ldb, msg);
455         if (ret != LDB_SUCCESS) {
456                 DEBUG(0,("Failed to add prefixMap and schemaInfo %s\n", ldb_strerror(ret)));
457                 return NT_STATUS_FOOBAR;
458         }
459
460         talloc_free(s_dsa);
461         talloc_free(objs);
462
463         /* reopen the ldb */
464         talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
465         s->schema = NULL;
466
467         DEBUG(0,("Reopen the SAM LDB with system credentials and a already stored schema: %s\n", TORTURE_SAMDB_LDB));
468         s->ldb = ldb_wrap_connect(s, TORTURE_SAMDB_LDB,
469                                   system_session(s),
470                                   NULL, 0, NULL);
471         if (!s->ldb) {
472                 DEBUG(0,("Failed to open '%s'\n",
473                         TORTURE_SAMDB_LDB));
474                 return NT_STATUS_INTERNAL_DB_ERROR;
475         }
476
477         ok = samdb_set_ntds_invocation_id(s->ldb, &c->dest_dsa->invocation_id);
478         if (!ok) {
479                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
480                 return NT_STATUS_FOOBAR;
481         }
482         ok = samdb_set_ntds_objectGUID(s->ldb, &c->dest_dsa->ntds_guid);
483         if (!ok) {
484                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
485                 return NT_STATUS_FOOBAR;
486         }
487
488         s->schema = dsdb_get_schema(s->ldb);
489         if (!s->schema) {
490                 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
491                 return NT_STATUS_FOOBAR;
492         }
493
494         return NT_STATUS_OK;
495 }
496
497 static NTSTATUS test_become_dc_schema_chunk(void *private_data,
498                                             const struct libnet_BecomeDC_StoreChunk *c)
499 {
500         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
501         WERROR status;
502         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
503         uint32_t total_object_count;
504         uint32_t object_count;
505         struct drsuapi_DsReplicaObjectListItemEx *first_object;
506         struct drsuapi_DsReplicaObjectListItemEx *cur;
507
508         switch (c->ctr_level) {
509         case 1:
510                 mapping_ctr             = &c->ctr1->mapping_ctr;
511                 total_object_count      = c->ctr1->total_object_count;
512                 object_count            = c->ctr1->object_count;
513                 first_object            = c->ctr1->first_object;
514                 break;
515         case 6:
516                 mapping_ctr             = &c->ctr6->mapping_ctr;
517                 total_object_count      = c->ctr6->total_object_count;
518                 object_count            = c->ctr6->object_count;
519                 first_object            = c->ctr6->first_object;
520                 break;
521         default:
522                 return NT_STATUS_INVALID_PARAMETER;
523         }
524
525         if (total_object_count) {
526                 DEBUG(0,("Schema-DN[%s] objects[%u/%u]\n",
527                         c->partition->nc.dn, object_count, total_object_count));
528         } else {
529                 DEBUG(0,("Schema-DN[%s] objects[%u]\n",
530                 c->partition->nc.dn, object_count));
531         }
532
533         if (!s->schema) {
534                 s->self_made_schema = talloc_zero(s, struct dsdb_schema);
535                 NT_STATUS_HAVE_NO_MEMORY(s->self_made_schema);
536
537                 status = dsdb_load_oid_mappings_drsuapi(s->self_made_schema, mapping_ctr);
538                 if (!W_ERROR_IS_OK(status)) {
539                         return werror_to_ntstatus(status);
540                 }
541
542                 s->schema = s->self_made_schema;
543         } else {
544                 status = dsdb_verify_oid_mappings_drsuapi(s->schema, mapping_ctr);
545                 if (!W_ERROR_IS_OK(status)) {
546                         return werror_to_ntstatus(status);
547                 }
548         }
549
550         if (!s->schema_part.first_object) {
551                 s->schema_part.object_count = object_count;
552                 s->schema_part.first_object = talloc_steal(s, first_object);
553         } else {
554                 s->schema_part.object_count             += object_count;
555                 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
556                                                                        first_object);
557         }
558         for (cur = first_object; cur->next_object; cur = cur->next_object) {}
559         s->schema_part.last_object = cur;
560
561         if (c->partition->highwatermark.tmp_highest_usn == c->partition->highwatermark.highest_usn) {
562                 return test_apply_schema(s, c);
563         }
564
565         return NT_STATUS_OK;
566 }
567
568 static NTSTATUS test_become_dc_store_chunk(void *private_data,
569                                            const struct libnet_BecomeDC_StoreChunk *c)
570 {
571         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
572         WERROR status;
573         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
574         uint32_t total_object_count;
575         uint32_t object_count;
576         struct drsuapi_DsReplicaObjectListItemEx *first_object;
577         uint32_t linked_attributes_count;
578         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
579         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
580         struct dsdb_extended_replicated_objects *objs;
581         struct repsFromTo1 *s_dsa;
582         char *tmp_dns_name;
583         uint32_t i;
584
585         s_dsa                   = talloc_zero(s, struct repsFromTo1);
586         NT_STATUS_HAVE_NO_MEMORY(s_dsa);
587         s_dsa->other_info       = talloc(s_dsa, struct repsFromTo1OtherInfo);
588         NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
589
590         switch (c->ctr_level) {
591         case 1:
592                 mapping_ctr                     = &c->ctr1->mapping_ctr;
593                 total_object_count              = c->ctr1->total_object_count;
594                 object_count                    = c->ctr1->object_count;
595                 first_object                    = c->ctr1->first_object;
596                 linked_attributes_count         = 0;
597                 linked_attributes               = NULL;
598                 s_dsa->highwatermark            = c->ctr1->new_highwatermark;
599                 s_dsa->source_dsa_obj_guid      = c->ctr1->source_dsa_guid;
600                 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
601                 uptodateness_vector             = NULL; /* TODO: map it */
602                 break;
603         case 6:
604                 mapping_ctr                     = &c->ctr6->mapping_ctr;
605                 total_object_count              = c->ctr6->total_object_count;
606                 object_count                    = c->ctr6->object_count;
607                 first_object                    = c->ctr6->first_object;
608                 linked_attributes_count         = c->ctr6->linked_attributes_count;
609                 linked_attributes               = c->ctr6->linked_attributes;
610                 s_dsa->highwatermark            = c->ctr6->new_highwatermark;
611                 s_dsa->source_dsa_obj_guid      = c->ctr6->source_dsa_guid;
612                 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
613                 uptodateness_vector             = c->ctr6->uptodateness_vector;
614                 break;
615         default:
616                 return NT_STATUS_INVALID_PARAMETER;
617         }
618
619         s_dsa->replica_flags            = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
620                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
621                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
622         memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
623
624         tmp_dns_name    = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
625         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
626         tmp_dns_name    = talloc_asprintf_append(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
627         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
628         s_dsa->other_info->dns_name = tmp_dns_name;
629
630         if (total_object_count) {
631                 DEBUG(0,("Partition[%s] objects[%u/%u]\n",
632                         c->partition->nc.dn, object_count, total_object_count));
633         } else {
634                 DEBUG(0,("Partition[%s] objects[%u]\n",
635                 c->partition->nc.dn, object_count));
636         }
637
638         status = dsdb_extended_replicated_objects_commit(s->ldb,
639                                                          c->partition->nc.dn,
640                                                          mapping_ctr,
641                                                          object_count,
642                                                          first_object,
643                                                          linked_attributes_count,
644                                                          linked_attributes,
645                                                          s_dsa,
646                                                          uptodateness_vector,
647                                                          s, &objs);
648         if (!W_ERROR_IS_OK(status)) {
649                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
650                 return werror_to_ntstatus(status);
651         }
652
653         if (lp_parm_bool(-1, "become dc", "dump objects", False)) {
654                 for (i=0; i < objs->num_objects; i++) {
655                         struct ldb_ldif ldif;
656                         fprintf(stdout, "#\n");
657                         ldif.changetype = LDB_CHANGETYPE_NONE;
658                         ldif.msg = objs->objects[i].msg;
659                         ldb_ldif_write_file(s->ldb, stdout, &ldif);
660                         NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
661                 }
662         }
663         talloc_free(s_dsa);
664         talloc_free(objs);
665
666         for (i=0; i < linked_attributes_count; i++) {
667                 const struct dsdb_attribute *sa;
668
669                 if (!linked_attributes[i].identifier) {
670                         return NT_STATUS_FOOBAR;                
671                 }
672
673                 if (!linked_attributes[i].value.blob) {
674                         return NT_STATUS_FOOBAR;                
675                 }
676
677                 sa = dsdb_attribute_by_attributeID_id(s->schema,
678                                                       linked_attributes[i].attid);
679                 if (!sa) {
680                         return NT_STATUS_FOOBAR;
681                 }
682
683                 if (lp_parm_bool(-1, "become dc", "dump objects", False)) {
684                         DEBUG(0,("# %s\n", sa->lDAPDisplayName));
685                         NDR_PRINT_DEBUG(drsuapi_DsReplicaLinkedAttribute, &linked_attributes[i]);
686                         dump_data(0,
687                                 linked_attributes[i].value.blob->data,
688                                 linked_attributes[i].value.blob->length);
689                 }
690         }
691
692         return NT_STATUS_OK;
693 }
694
695 BOOL torture_net_become_dc(struct torture_context *torture)
696 {
697         BOOL ret = True;
698         NTSTATUS status;
699         struct libnet_BecomeDC b;
700         struct libnet_UnbecomeDC u;
701         struct test_become_dc_state *s;
702
703         s = talloc_zero(torture, struct test_become_dc_state);
704         if (!s) return False;
705
706         /* Join domain as a member server. */
707         s->tj = torture_join_domain(TORTURE_NETBIOS_NAME,
708                                  ACB_WSTRUST,
709                                  &s->machine_account);
710         if (!s->tj) {
711                 DEBUG(0, ("%s failed to join domain as workstation\n",
712                           TORTURE_NETBIOS_NAME));
713                 return False;
714         }
715
716         s->ctx = libnet_context_init(event_context_init(s));
717         s->ctx->cred = cmdline_credentials;
718
719         s->ldb = ldb_init(s);
720
721         ZERO_STRUCT(b);
722         b.in.domain_dns_name            = torture_join_dom_dns_name(s->tj);
723         b.in.domain_netbios_name        = torture_join_dom_netbios_name(s->tj);
724         b.in.domain_sid                 = torture_join_sid(s->tj);
725         b.in.source_dsa_address         = lp_parm_string(-1, "torture", "host");
726         b.in.dest_dsa_netbios_name      = TORTURE_NETBIOS_NAME;
727
728         b.in.callbacks.private_data     = s;
729         b.in.callbacks.check_options    = test_become_dc_check_options;
730         b.in.callbacks.prepare_db       = test_become_dc_prepare_db;
731         b.in.callbacks.schema_chunk     = test_become_dc_schema_chunk;
732         b.in.callbacks.config_chunk     = test_become_dc_store_chunk;
733         b.in.callbacks.domain_chunk     = test_become_dc_store_chunk;
734
735         status = libnet_BecomeDC(s->ctx, s, &b);
736         if (!NT_STATUS_IS_OK(status)) {
737                 printf("libnet_BecomeDC() failed - %s\n", nt_errstr(status));
738                 ret = False;
739         }
740
741         ZERO_STRUCT(u);
742         u.in.domain_dns_name            = torture_join_dom_dns_name(s->tj);
743         u.in.domain_netbios_name        = torture_join_dom_netbios_name(s->tj);
744         u.in.source_dsa_address         = lp_parm_string(-1, "torture", "host");
745         u.in.dest_dsa_netbios_name      = TORTURE_NETBIOS_NAME;
746
747         status = libnet_UnbecomeDC(s->ctx, s, &u);
748         if (!NT_STATUS_IS_OK(status)) {
749                 printf("libnet_UnbecomeDC() failed - %s\n", nt_errstr(status));
750                 ret = False;
751         }
752
753         /* Leave domain. */                          
754         torture_leave_domain(s->tj);
755
756         talloc_free(s);
757         return ret;
758 }