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