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