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