s4:dsdb Change dsdb_get_schema() callers to use new talloc argument
[bbaumbach/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/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_DRS_WRIT_REP
174                                         | DRSUAPI_DRS_INIT_SYNC
175                                         | DRSUAPI_DRS_PER_SYNC;
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->ldb, 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, s);
261
262         status = dsdb_extended_replicated_objects_convert(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);
273         if (!W_ERROR_IS_OK(status)) {
274                 DEBUG(0,("Failed to convert 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         status = dsdb_extended_replicated_objects_commit(s->ldb,
290                                                          objs, &seq_num);
291         if (!W_ERROR_IS_OK(status)) {
292                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
293                 return werror_to_ntstatus(status);
294         }
295
296         msg = ldb_msg_new(objs);
297         NT_STATUS_HAVE_NO_MEMORY(msg);
298         msg->dn = objs->partition_dn;
299
300         status = dsdb_get_oid_mappings_ldb(s->schema, msg, &prefixMap_val, &schemaInfo_val);
301         if (!W_ERROR_IS_OK(status)) {
302                 DEBUG(0,("Failed dsdb_get_oid_mappings_ldb(%s)\n", win_errstr(status)));
303                 return werror_to_ntstatus(status);
304         }
305
306         /* we only add prefixMap here, because schemaInfo is a replicated attribute and already applied */
307         ret = ldb_msg_add_value(msg, "prefixMap", &prefixMap_val, &prefixMap_el);
308         if (ret != LDB_SUCCESS) {
309                 return NT_STATUS_FOOBAR;
310         }
311         prefixMap_el->flags = LDB_FLAG_MOD_REPLACE;
312
313         ret = ldb_modify(s->ldb, msg);
314         if (ret != LDB_SUCCESS) {
315                 DEBUG(0,("Failed to add prefixMap and schemaInfo %s\n", ldb_strerror(ret)));
316                 return NT_STATUS_FOOBAR;
317         }
318
319         talloc_free(s_dsa);
320         talloc_free(objs);
321
322         /* reopen the ldb */
323         talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
324         s->schema = NULL;
325
326         sam_ldb_path = talloc_asprintf(s, "%s/%s", s->targetdir, "private/sam.ldb");
327         DEBUG(0,("Reopen the SAM LDB with system credentials and a already stored schema: %s\n", sam_ldb_path));
328         s->ldb = ldb_wrap_connect(s, s->tctx->ev, s->tctx->lp_ctx, sam_ldb_path,
329                                   system_session(s->tctx->lp_ctx),
330                                   NULL, 0);
331         if (!s->ldb) {
332                 DEBUG(0,("Failed to open '%s'\n",
333                         sam_ldb_path));
334                 return NT_STATUS_INTERNAL_DB_ERROR;
335         }
336
337         ok = samdb_set_ntds_invocation_id(s->ldb, &c->dest_dsa->invocation_id);
338         if (!ok) {
339                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
340                 return NT_STATUS_FOOBAR;
341         }
342         ok = samdb_set_ntds_objectGUID(s->ldb, &c->dest_dsa->ntds_guid);
343         if (!ok) {
344                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
345                 return NT_STATUS_FOOBAR;
346         }
347
348         s->schema = dsdb_get_schema(s->ldb, s);
349         if (!s->schema) {
350                 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
351                 return NT_STATUS_FOOBAR;
352         }
353
354         return NT_STATUS_OK;
355 }
356
357 static NTSTATUS test_become_dc_schema_chunk(void *private_data,
358                                             const struct libnet_BecomeDC_StoreChunk *c)
359 {
360         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
361         WERROR status;
362         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
363         uint32_t nc_object_count;
364         uint32_t object_count;
365         struct drsuapi_DsReplicaObjectListItemEx *first_object;
366         struct drsuapi_DsReplicaObjectListItemEx *cur;
367         uint32_t nc_linked_attributes_count;
368         uint32_t linked_attributes_count;
369
370         switch (c->ctr_level) {
371         case 1:
372                 mapping_ctr                     = &c->ctr1->mapping_ctr;
373                 nc_object_count                 = c->ctr1->extended_ret; /* maybe w2k send this unexpected? */
374                 object_count                    = c->ctr1->object_count;
375                 first_object                    = c->ctr1->first_object;
376                 nc_linked_attributes_count      = 0;
377                 linked_attributes_count         = 0;
378                 break;
379         case 6:
380                 mapping_ctr                     = &c->ctr6->mapping_ctr;
381                 nc_object_count                 = c->ctr6->nc_object_count;
382                 object_count                    = c->ctr6->object_count;
383                 first_object                    = c->ctr6->first_object;
384                 nc_linked_attributes_count      = c->ctr6->nc_linked_attributes_count;
385                 linked_attributes_count         = c->ctr6->linked_attributes_count;
386                 break;
387         default:
388                 return NT_STATUS_INVALID_PARAMETER;
389         }
390
391         if (nc_object_count) {
392                 DEBUG(0,("Schema-DN[%s] objects[%u/%u] linked_values[%u/%u]\n",
393                         c->partition->nc.dn, object_count, nc_object_count,
394                         linked_attributes_count, nc_linked_attributes_count));
395         } else {
396                 DEBUG(0,("Schema-DN[%s] objects[%u] linked_values[%u\n",
397                 c->partition->nc.dn, object_count, linked_attributes_count));
398         }
399
400         if (!s->schema) {
401                 s->self_made_schema = dsdb_new_schema(s, lp_iconv_convenience(s->lp_ctx));
402
403                 NT_STATUS_HAVE_NO_MEMORY(s->self_made_schema);
404
405                 status = dsdb_load_prefixmap_from_drsuapi(s->self_made_schema, mapping_ctr);
406                 if (!W_ERROR_IS_OK(status)) {
407                         return werror_to_ntstatus(status);
408                 }
409
410                 s->schema = s->self_made_schema;
411         } else {
412                 status = dsdb_schema_pfm_contains_drsuapi_pfm(s->schema->prefixmap, mapping_ctr);
413                 if (!W_ERROR_IS_OK(status)) {
414                         return werror_to_ntstatus(status);
415                 }
416         }
417
418         if (!s->schema_part.first_object) {
419                 s->schema_part.object_count = object_count;
420                 s->schema_part.first_object = talloc_steal(s, first_object);
421         } else {
422                 s->schema_part.object_count             += object_count;
423                 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
424                                                                        first_object);
425         }
426         for (cur = first_object; cur->next_object; cur = cur->next_object) {}
427         s->schema_part.last_object = cur;
428
429         if (!c->partition->more_data) {
430                 return test_apply_schema(s, c);
431         }
432
433         return NT_STATUS_OK;
434 }
435
436 static NTSTATUS test_become_dc_store_chunk(void *private_data,
437                                            const struct libnet_BecomeDC_StoreChunk *c)
438 {
439         struct test_become_dc_state *s = talloc_get_type(private_data, struct test_become_dc_state);
440         WERROR status;
441         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
442         uint32_t nc_object_count;
443         uint32_t object_count;
444         struct drsuapi_DsReplicaObjectListItemEx *first_object;
445         uint32_t nc_linked_attributes_count;
446         uint32_t linked_attributes_count;
447         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
448         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
449         struct dsdb_extended_replicated_objects *objs;
450         struct repsFromTo1 *s_dsa;
451         char *tmp_dns_name;
452         uint32_t i;
453         uint64_t seq_num;
454
455         s_dsa                   = talloc_zero(s, struct repsFromTo1);
456         NT_STATUS_HAVE_NO_MEMORY(s_dsa);
457         s_dsa->other_info       = talloc(s_dsa, struct repsFromTo1OtherInfo);
458         NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
459
460         switch (c->ctr_level) {
461         case 1:
462                 mapping_ctr                     = &c->ctr1->mapping_ctr;
463                 nc_object_count                 = c->ctr1->extended_ret; /* maybe w2k send this unexpected? */
464                 object_count                    = c->ctr1->object_count;
465                 first_object                    = c->ctr1->first_object;
466                 nc_linked_attributes_count      = 0;
467                 linked_attributes_count         = 0;
468                 linked_attributes               = NULL;
469                 s_dsa->highwatermark            = c->ctr1->new_highwatermark;
470                 s_dsa->source_dsa_obj_guid      = c->ctr1->source_dsa_guid;
471                 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
472                 uptodateness_vector             = NULL; /* TODO: map it */
473                 break;
474         case 6:
475                 mapping_ctr                     = &c->ctr6->mapping_ctr;
476                 nc_object_count                 = c->ctr6->nc_object_count;
477                 object_count                    = c->ctr6->object_count;
478                 first_object                    = c->ctr6->first_object;
479                 nc_linked_attributes_count      = c->ctr6->nc_linked_attributes_count;
480                 linked_attributes_count         = c->ctr6->linked_attributes_count;
481                 linked_attributes               = c->ctr6->linked_attributes;
482                 s_dsa->highwatermark            = c->ctr6->new_highwatermark;
483                 s_dsa->source_dsa_obj_guid      = c->ctr6->source_dsa_guid;
484                 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
485                 uptodateness_vector             = c->ctr6->uptodateness_vector;
486                 break;
487         default:
488                 return NT_STATUS_INVALID_PARAMETER;
489         }
490
491         s_dsa->replica_flags            = DRSUAPI_DRS_WRIT_REP
492                                         | DRSUAPI_DRS_INIT_SYNC
493                                         | DRSUAPI_DRS_PER_SYNC;
494         memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
495
496         tmp_dns_name    = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
497         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
498         tmp_dns_name    = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
499         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
500         s_dsa->other_info->dns_name = tmp_dns_name;
501
502         if (nc_object_count) {
503                 DEBUG(0,("Partition[%s] objects[%u/%u] linked_values[%u/%u]\n",
504                         c->partition->nc.dn, object_count, nc_object_count,
505                         linked_attributes_count, nc_linked_attributes_count));
506         } else {
507                 DEBUG(0,("Partition[%s] objects[%u] linked_values[%u\n",
508                 c->partition->nc.dn, object_count, linked_attributes_count));
509         }
510
511         status = dsdb_extended_replicated_objects_convert(s->ldb,
512                                                           c->partition->nc.dn,
513                                                           mapping_ctr,
514                                                           object_count,
515                                                           first_object,
516                                                           linked_attributes_count,
517                                                           linked_attributes,
518                                                           s_dsa,
519                                                           uptodateness_vector,
520                                                           c->gensec_skey,
521                                                           s, &objs);
522         if (!W_ERROR_IS_OK(status)) {
523                 DEBUG(0,("Failed to convert objects: %s\n", win_errstr(status)));
524                 return werror_to_ntstatus(status);
525         }
526
527         if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
528                 for (i=0; i < objs->num_objects; i++) {
529                         struct ldb_ldif ldif;
530                         fprintf(stdout, "#\n");
531                         ldif.changetype = LDB_CHANGETYPE_NONE;
532                         ldif.msg = objs->objects[i].msg;
533                         ldb_ldif_write_file(s->ldb, stdout, &ldif);
534                         NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
535                 }
536         }
537         status = dsdb_extended_replicated_objects_commit(s->ldb, objs, &seq_num);
538         talloc_free(s_dsa);
539         talloc_free(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         for (i=0; i < linked_attributes_count; i++) {
546                 const struct dsdb_attribute *sa;
547
548                 if (!linked_attributes[i].identifier) {
549                         return NT_STATUS_FOOBAR;                
550                 }
551
552                 if (!linked_attributes[i].value.blob) {
553                         return NT_STATUS_FOOBAR;                
554                 }
555
556                 sa = dsdb_attribute_by_attributeID_id(s->schema,
557                                                       linked_attributes[i].attid);
558                 if (!sa) {
559                         return NT_STATUS_FOOBAR;
560                 }
561
562                 if (lp_parm_bool(s->tctx->lp_ctx, NULL, "become dc", "dump objects", false)) {
563                         DEBUG(0,("# %s\n", sa->lDAPDisplayName));
564                         NDR_PRINT_DEBUG(drsuapi_DsReplicaLinkedAttribute, &linked_attributes[i]);
565                         dump_data(0,
566                                 linked_attributes[i].value.blob->data,
567                                 linked_attributes[i].value.blob->length);
568                 }
569         }
570
571         return NT_STATUS_OK;
572 }
573
574 bool torture_net_become_dc(struct torture_context *torture)
575 {
576         bool ret = true;
577         NTSTATUS status;
578         struct libnet_BecomeDC b;
579         struct libnet_UnbecomeDC u;
580         struct test_become_dc_state *s;
581         struct ldb_message *msg;
582         int ldb_ret;
583         uint32_t i;
584         char *sam_ldb_path;
585         const char *address;
586         struct nbt_name name;
587
588         char *location = NULL;
589         torture_assert_ntstatus_ok(torture, torture_temp_dir(torture, "libnet_BecomeDC", &location), 
590                                    "torture_temp_dir should return NT_STATUS_OK" );
591
592         s = talloc_zero(torture, struct test_become_dc_state);
593         torture_assert(torture, s, "talloc_zero");
594
595         s->tctx = torture;
596         s->lp_ctx = torture->lp_ctx;
597
598         make_nbt_name_server(&name, torture_setting_string(torture, "host", NULL));
599
600         /* do an initial name resolution to find its IP */
601         status = resolve_name(lp_resolve_context(torture->lp_ctx),
602                               &name, torture, &address, torture->ev);
603         torture_assert_ntstatus_ok(torture, status, talloc_asprintf(torture,
604                                    "Failed to resolve %s - %s\n",
605                                    name.name, nt_errstr(status)));
606
607         s->netbios_name = lp_parm_string(torture->lp_ctx, NULL, "become dc", "smbtorture dc");
608         if (!s->netbios_name || !s->netbios_name[0]) {
609                 s->netbios_name = "smbtorturedc";
610         }
611
612         s->targetdir = location;
613
614         /* Join domain as a member server. */
615         s->tj = torture_join_domain(torture, s->netbios_name,
616                                  ACB_WSTRUST,
617                                  &s->machine_account);
618         torture_assert(torture, s->tj, talloc_asprintf(torture,
619                        "%s failed to join domain as workstation\n",
620                        s->netbios_name));
621
622         s->ctx = libnet_context_init(torture->ev, torture->lp_ctx);
623         s->ctx->cred = cmdline_credentials;
624
625         s->ldb = ldb_init(s, torture->ev);
626
627         ZERO_STRUCT(b);
628         b.in.domain_dns_name            = torture_join_dom_dns_name(s->tj);
629         b.in.domain_netbios_name        = torture_join_dom_netbios_name(s->tj);
630         b.in.domain_sid                 = torture_join_sid(s->tj);
631         b.in.source_dsa_address         = address;
632         b.in.dest_dsa_netbios_name      = s->netbios_name;
633
634         b.in.callbacks.private_data     = s;
635         b.in.callbacks.check_options    = test_become_dc_check_options;
636         b.in.callbacks.prepare_db = test_become_dc_prepare_db;
637         b.in.callbacks.schema_chunk     = test_become_dc_schema_chunk;
638         b.in.callbacks.config_chunk     = test_become_dc_store_chunk;
639         b.in.callbacks.domain_chunk     = test_become_dc_store_chunk;
640
641         status = libnet_BecomeDC(s->ctx, s, &b);
642         torture_assert_ntstatus_ok_goto(torture, status, ret, cleanup, talloc_asprintf(torture,
643                                    "libnet_BecomeDC() failed - %s %s\n",
644                                    nt_errstr(status), b.out.error_string));
645         msg = ldb_msg_new(s);
646         torture_assert_int_equal_goto(torture, (msg?1:0), 1, ret, cleanup,
647                                       "ldb_msg_new() failed\n");
648         msg->dn = ldb_dn_new(msg, s->ldb, "@ROOTDSE");
649         torture_assert_int_equal_goto(torture, (msg->dn?1:0), 1, ret, cleanup,
650                                       "ldb_msg_new(@ROOTDSE) failed\n");
651
652         ldb_ret = ldb_msg_add_string(msg, "isSynchronized", "TRUE");
653         torture_assert_int_equal_goto(torture, ldb_ret, LDB_SUCCESS, ret, cleanup,
654                                       "ldb_msg_add_string(msg, isSynchronized, TRUE) failed\n");
655
656         for (i=0; i < msg->num_elements; i++) {
657                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
658         }
659
660         torture_comment(torture, "mark ROOTDSE with isSynchronized=TRUE\n");
661         ldb_ret = ldb_modify(s->ldb, msg);
662         torture_assert_int_equal_goto(torture, ldb_ret, LDB_SUCCESS, ret, cleanup,
663                                       "ldb_modify() failed\n");
664         
665         /* reopen the ldb */
666         talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
667         s->schema = NULL;
668
669         sam_ldb_path = talloc_asprintf(s, "%s/%s", s->targetdir, "private/sam.ldb");
670         torture_comment(torture, "Reopen the SAM LDB with system credentials and all replicated data: %s\n", sam_ldb_path);
671         s->ldb = ldb_wrap_connect(s, s->tctx->ev, s->lp_ctx, sam_ldb_path,
672                                   system_session(s->lp_ctx),
673                                   NULL, 0);
674         torture_assert_int_equal_goto(torture, (s->ldb?1:0), 1, ret, cleanup,
675                                       talloc_asprintf(torture,
676                                       "Failed to open '%s'\n", sam_ldb_path));
677
678         s->schema = dsdb_get_schema(s->ldb, s);
679         torture_assert_int_equal_goto(torture, (s->schema?1:0), 1, ret, cleanup,
680                                       "Failed to get loaded dsdb_schema\n");
681
682         /* Make sure we get this from the command line */
683         if (lp_parm_bool(torture->lp_ctx, NULL, "become dc", "do not unjoin", false)) {
684                 talloc_free(s);
685                 return ret;
686         }
687
688 cleanup:
689         ZERO_STRUCT(u);
690         u.in.domain_dns_name            = torture_join_dom_dns_name(s->tj);
691         u.in.domain_netbios_name        = torture_join_dom_netbios_name(s->tj);
692         u.in.source_dsa_address         = address;
693         u.in.dest_dsa_netbios_name      = s->netbios_name;
694
695         status = libnet_UnbecomeDC(s->ctx, s, &u);
696         torture_assert_ntstatus_ok(torture, status, talloc_asprintf(torture,
697                                    "libnet_UnbecomeDC() failed - %s %s\n",
698                                    nt_errstr(status), u.out.error_string));
699
700         /* Leave domain. */                          
701         torture_leave_domain(torture, s->tj);
702
703         talloc_free(s);
704         return ret;
705 }