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