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