r20715: set the dsdb_schema on the ldb_context
[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
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;
105
106         struct ldb_context *ldb;
107
108         struct {
109                 uint32_t object_count;
110                 struct drsuapi_DsReplicaObjectListItemEx *first_object;
111                 struct drsuapi_DsReplicaObjectListItemEx *last_object;
112         } schema_part;
113 };
114
115 static NTSTATUS test_become_dc_check_options(void *private_data,
116                                              const struct libnet_BecomeDC_CheckOptions *o)
117 {
118         DEBUG(0,("Become DC of Domain[%s]/[%s]\n",
119                 o->domain->netbios_name, o->domain->dns_name));
120
121         DEBUG(0,("Promotion Partner is Server[%s] from Site[%s]\n",
122                 o->source_dsa->dns_name, o->source_dsa->site_name));
123
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));
132
133         return NT_STATUS_OK;
134 }
135
136 static NTSTATUS test_become_dc_prepare_db(void *private_data,
137                                           const struct libnet_BecomeDC_PrepareDB *p)
138 {
139         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
140         char *ejs;
141         int ret;
142         bool ok;
143
144         DEBUG(0,("New Server[%s] in Site[%s]\n",
145                 p->dest_dsa->dns_name, p->dest_dsa->site_name));
146
147         DEBUG(0,("DSA Instance [%s]\n"
148                 "\tobjectGUID[%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)));
153
154         DEBUG(0,("Schema Partition[%s]\n",
155                 p->forest->schema_dn_str));
156
157         DEBUG(0,("Config Partition[%s]\n",
158                 p->forest->config_dn_str));
159
160         DEBUG(0,("Domain Partition[%s]\n",
161                 p->domain->dn_str));
162
163         ejs = talloc_asprintf(s,
164                 "libinclude(\"base.js\");\n"
165                 "libinclude(\"provision.js\");\n"
166                 "\n"
167                 "function message() { print(vsprintf(arguments)); }\n"
168                 "\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"
180                 "\n"
181                 "modules_list        = new Array(\"rootdse\",\n"
182                 "                                \"kludge_acl\",\n"
183                 "                                \"paged_results\",\n"
184                 "                                \"server_sort\",\n"
185                 "                                \"extended_dn\",\n"
186                 "                                \"asq\",\n"
187                 "                                \"samldb\",\n"
188                 "                                \"password_hash\",\n"
189                 "                                \"operational\",\n"
190                 "                                \"objectclass\",\n"
191                 "                                \"rdn_name\",\n"
192                 "                                \"partition\");\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"
197                 "\n"
198                 "var paths = provision_default_paths(subobj);\n"
199                 "paths.samdb = \"%s\";\n"
200                 "\n"
201                 "var system_session = system_session();\n"
202                 "\n"
203                 "var ok = provision_become_dc(subobj, message, paths, system_session);\n"
204                 "assert(ok);\n"
205                 "\n"
206                 "return 0;\n",
207                 p->forest->root_dn_str,
208                 p->domain->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,
214                 TORTURE_SAMDB_LDB);
215         NT_STATUS_HAVE_NO_MEMORY(ejs);
216
217         ret = test_run_ejs(ejs);
218         if (ret != 0) {
219                 DEBUG(0,("Failed to run ejs script: %d:\n%s",
220                         ret, ejs));
221                 talloc_free(ejs);
222                 return NT_STATUS_FOOBAR;
223         }
224         talloc_free(ejs);
225
226         talloc_free(s->ldb);
227
228         s->ldb = ldb_wrap_connect(s, TORTURE_SAMDB_LDB,
229                                   system_session(s),
230                                   NULL, 0, NULL);
231         if (!s->ldb) {
232                 DEBUG(0,("Failed to open '%s'\n",
233                         TORTURE_SAMDB_LDB));
234                 return NT_STATUS_INTERNAL_DB_ERROR;
235         }
236
237         ok = samdb_set_ntds_invocation_id(s->ldb, &p->dest_dsa->invocation_id);
238         if (!ok) {
239                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
240                 return NT_STATUS_FOOBAR;
241         }
242         ok = samdb_set_ntds_objectGUID(s->ldb, &p->dest_dsa->ntds_guid);
243         if (!ok) {
244                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
245                 return NT_STATUS_FOOBAR;
246         }
247
248         return NT_STATUS_OK;
249 }
250
251 static NTSTATUS test_apply_schema(struct test_become_dc_state *s,
252                                   const struct libnet_BecomeDC_StoreChunk *c)
253 {
254         WERROR status;
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;
265         char *tmp_dns_name;
266         uint32_t i;
267         int ret;
268
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);
273
274         switch (c->ctr_level) {
275         case 1:
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 */
286                 break;
287         case 6:
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;
298                 break;
299         default:
300                 return NT_STATUS_INVALID_PARAMETER;
301         }
302
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));
307
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;
313
314         for (cur = first_object; cur; cur = cur->next_object) {
315                 bool is_attr = false;
316                 bool is_class = false;
317
318                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
319                         struct drsuapi_DsReplicaAttribute *a;
320                         uint32_t j;
321                         const char *oid = NULL;
322
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);
327                         }
328
329                         switch (a->attid) {
330                         case DRSUAPI_ATTRIBUTE_objectClass:
331                                 for (j=0; j < a->value_ctr.num_values; j++) {
332                                         uint32_t val = 0xFFFFFFFF;
333
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);
337                                         }
338
339                                         if (val == DRSUAPI_OBJECTCLASS_attributeSchema) {
340                                                 is_attr = true;
341                                         }
342                                         if (val == DRSUAPI_OBJECTCLASS_classSchema) {
343                                                 is_class = true;
344                                         }
345                                 }
346
347                                 break;
348                         default:
349                                 break;
350                         }
351                 }
352
353                 if (is_attr) {
354                         struct dsdb_attribute *sa;
355
356                         sa = talloc_zero(s->schema, struct dsdb_attribute);
357                         NT_STATUS_HAVE_NO_MEMORY(sa);
358
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);
362                         }
363
364                         DLIST_ADD_END(s->schema->attributes, sa, struct dsdb_attribute *);
365                 }
366
367                 if (is_class) {
368                         struct dsdb_class *sc;
369
370                         sc = talloc_zero(s->schema, struct dsdb_class);
371                         NT_STATUS_HAVE_NO_MEMORY(sc);
372
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);
376                         }
377
378                         DLIST_ADD_END(s->schema->classes, sc, struct dsdb_class *);
379                 }
380         }
381
382         ret = dsdb_set_schema(s->ldb, s->schema);
383         if (ret != LDB_SUCCESS) {
384                 return NT_STATUS_FOOBAR;
385         }
386
387         status = dsdb_extended_replicated_objects_commit(s->ldb,
388                                                          c->partition->nc.dn,
389                                                          s->schema,
390                                                          mapping_ctr,
391                                                          object_count,
392                                                          first_object,
393                                                          linked_attributes_count,
394                                                          linked_attributes,
395                                                          s_dsa,
396                                                          uptodateness_vector,
397                                                          s, &objs);
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);
401         }
402
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);
411                 }
412         }
413
414         talloc_free(s_dsa);
415         talloc_free(objs);
416         return NT_STATUS_OK;
417 }
418
419 static NTSTATUS test_become_dc_schema_chunk(void *private_data,
420                                             const struct libnet_BecomeDC_StoreChunk *c)
421 {
422         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
423         WERROR status;
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;
429
430         switch (c->ctr_level) {
431         case 1:
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;
436                 break;
437         case 6:
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;
442                 break;
443         default:
444                 return NT_STATUS_INVALID_PARAMETER;
445         }
446
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));
450         } else {
451                 DEBUG(0,("Schema-DN[%s] objects[%u]\n",
452                 c->partition->nc.dn, object_count));
453         }
454
455         if (!s->schema) {
456                 s->schema = talloc_zero(s, struct dsdb_schema);
457                 NT_STATUS_HAVE_NO_MEMORY(s->schema);
458
459                 status = dsdb_load_oid_mappings(s->schema, mapping_ctr);
460                 if (!W_ERROR_IS_OK(status)) {
461                         return werror_to_ntstatus(status);
462                 }
463         } else {
464                 status = dsdb_verify_oid_mappings(s->schema, mapping_ctr);
465                 if (!W_ERROR_IS_OK(status)) {
466                         return werror_to_ntstatus(status);
467                 }
468         }
469
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);
473         } else {
474                 s->schema_part.object_count             += object_count;
475                 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
476                                                                        first_object);
477         }
478         for (cur = first_object; cur->next_object; cur = cur->next_object) {}
479         s->schema_part.last_object = cur;
480
481         if (c->partition->highwatermark.tmp_highest_usn == c->partition->highwatermark.highest_usn) {
482                 return test_apply_schema(s, c);
483         }
484
485         return NT_STATUS_OK;
486 }
487
488 static NTSTATUS test_become_dc_store_chunk(void *private_data,
489                                            const struct libnet_BecomeDC_StoreChunk *c)
490 {
491         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
492         WERROR status;
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;
502         char *tmp_dns_name;
503         uint32_t i;
504
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);
509
510         switch (c->ctr_level) {
511         case 1:
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 */
522                 break;
523         case 6:
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;
534                 break;
535         default:
536                 return NT_STATUS_INVALID_PARAMETER;
537         }
538
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));
543
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;
549
550         if (total_object_count) {
551                 DEBUG(0,("Partition[%s] objects[%u/%u]\n",
552                         c->partition->nc.dn, object_count, total_object_count));
553         } else {
554                 DEBUG(0,("Partition[%s] objects[%u]\n",
555                 c->partition->nc.dn, object_count));
556         }
557
558         status = dsdb_extended_replicated_objects_commit(s->ldb,
559                                                          c->partition->nc.dn,
560                                                          s->schema,
561                                                          mapping_ctr,
562                                                          object_count,
563                                                          first_object,
564                                                          linked_attributes_count,
565                                                          linked_attributes,
566                                                          s_dsa,
567                                                          uptodateness_vector,
568                                                          s, &objs);
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);
572         }
573
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);
582                 }
583         }
584         talloc_free(s_dsa);
585         talloc_free(objs);
586
587         for (i=0; i < linked_attributes_count; i++) {
588                 const struct dsdb_attribute *sa;
589
590                 if (!linked_attributes[i].identifier) {
591                         return NT_STATUS_FOOBAR;                
592                 }
593
594                 if (!linked_attributes[i].value.blob) {
595                         return NT_STATUS_FOOBAR;                
596                 }
597
598                 sa = dsdb_attribute_by_attributeID_id(s->schema,
599                                                       linked_attributes[i].attid);
600                 if (!sa) {
601                         return NT_STATUS_FOOBAR;
602                 }
603
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]);
607                         dump_data(0,
608                                 linked_attributes[i].value.blob->data,
609                                 linked_attributes[i].value.blob->length);
610                 }
611         }
612
613         return NT_STATUS_OK;
614 }
615
616 BOOL torture_net_become_dc(struct torture_context *torture)
617 {
618         BOOL ret = True;
619         NTSTATUS status;
620         struct libnet_BecomeDC b;
621         struct libnet_UnbecomeDC u;
622         struct test_become_dc_state *s;
623
624         s = talloc_zero(torture, struct test_become_dc_state);
625         if (!s) return False;
626
627         /* Join domain as a member server. */
628         s->tj = torture_join_domain(TORTURE_NETBIOS_NAME,
629                                  ACB_WSTRUST,
630                                  &s->machine_account);
631         if (!s->tj) {
632                 DEBUG(0, ("%s failed to join domain as workstation\n",
633                           TORTURE_NETBIOS_NAME));
634                 return False;
635         }
636
637         s->ctx = libnet_context_init(event_context_init(s));
638         s->ctx->cred = cmdline_credentials;
639
640         s->ldb = ldb_init(s);
641
642         ZERO_STRUCT(b);
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;
648
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;
655
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));
659                 ret = False;
660         }
661
662         ZERO_STRUCT(u);
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;
667
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));
671                 ret = False;
672         }
673
674         /* Leave domain. */                          
675         torture_leave_domain(s->tj);
676
677         talloc_free(s);
678         return ret;
679 }