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