Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into registry
[kamenim/samba.git] / source4 / libnet / libnet_vampire.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Extract the user/system database from a remote server
5
6    Copyright (C) Stefan Metzmacher      2004-2006
7    Copyright (C) Brad Henry 2005
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2008
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24
25 #include "includes.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 "param/provision.h"
41
42 /* 
43 List of tasks vampire.py must perform:
44 - Domain Join
45  - but don't write the secrets.ldb
46  - results for this should be enough to handle the provision
47 - if vampire method is samsync 
48  - Provision using these results 
49   - do we still want to support this NT4 technology?
50 - Start samsync with libnet code
51  - provision in the callback 
52 - Write out the secrets database, using the code from libnet_Join
53
54 */
55 struct vampire_state {
56         const char *netbios_name;
57         struct libnet_JoinDomain *join;
58         struct cli_credentials *machine_account;
59         struct dsdb_schema *self_made_schema;
60         const struct dsdb_schema *schema;
61
62         struct ldb_context *ldb;
63
64         struct {
65                 uint32_t object_count;
66                 struct drsuapi_DsReplicaObjectListItemEx *first_object;
67                 struct drsuapi_DsReplicaObjectListItemEx *last_object;
68         } schema_part;
69
70         const char *targetdir;
71
72         struct loadparm_context *lp_ctx;
73         struct event_context *event_ctx;
74 };
75
76 static NTSTATUS vampire_prepare_db(void *private_data,
77                                               const struct libnet_BecomeDC_PrepareDB *p)
78 {
79         struct vampire_state *s = talloc_get_type(private_data, struct vampire_state);
80         struct provision_settings settings;
81         struct provision_result result;
82         NTSTATUS status;
83
84         settings.site_name = p->dest_dsa->site_name;
85         settings.root_dn_str = p->forest->root_dn_str;
86         settings.domain_dn_str = p->domain->dn_str;
87         settings.config_dn_str = p->forest->config_dn_str;
88         settings.schema_dn_str = p->forest->schema_dn_str;
89         settings.netbios_name = p->dest_dsa->netbios_name;
90         settings.realm = s->join->out.realm;
91         settings.domain = s->join->out.domain_name;
92         settings.server_dn_str = p->dest_dsa->server_dn_str;
93         settings.machine_password = generate_random_str(s, 16);
94         settings.targetdir = s->targetdir;
95
96         status = provision_bare(s, s->lp_ctx, &settings, &result);
97
98         if (!NT_STATUS_IS_OK(status)) {
99                 return status;
100         }
101
102         s->ldb = result.samdb;
103         s->lp_ctx = result.lp_ctx;
104
105         return NT_STATUS_OK;
106
107
108 }
109
110 static NTSTATUS vampire_check_options(void *private_data,
111                                              const struct libnet_BecomeDC_CheckOptions *o)
112 {
113         struct vampire_state *s = talloc_get_type(private_data, struct vampire_state);
114
115         DEBUG(0,("Become DC [%s] of Domain[%s]/[%s]\n",
116                 s->netbios_name,
117                 o->domain->netbios_name, o->domain->dns_name));
118
119         DEBUG(0,("Promotion Partner is Server[%s] from Site[%s]\n",
120                 o->source_dsa->dns_name, o->source_dsa->site_name));
121
122         DEBUG(0,("Options:crossRef behavior_version[%u]\n"
123                        "\tschema object_version[%u]\n"
124                        "\tdomain behavior_version[%u]\n"
125                        "\tdomain w2k3_update_revision[%u]\n", 
126                 o->forest->crossref_behavior_version,
127                 o->forest->schema_object_version,
128                 o->domain->behavior_version,
129                 o->domain->w2k3_update_revision));
130
131         return NT_STATUS_OK;
132 }
133
134 static NTSTATUS vampire_apply_schema(struct vampire_state *s,
135                                   const struct libnet_BecomeDC_StoreChunk *c)
136 {
137         WERROR status;
138         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
139         uint32_t total_object_count;
140         uint32_t object_count;
141         struct drsuapi_DsReplicaObjectListItemEx *first_object;
142         struct drsuapi_DsReplicaObjectListItemEx *cur;
143         uint32_t linked_attributes_count;
144         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
145         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
146         struct dsdb_extended_replicated_objects *objs;
147         struct repsFromTo1 *s_dsa;
148         char *tmp_dns_name;
149         struct ldb_message *msg;
150         struct ldb_val prefixMap_val;
151         struct ldb_message_element *prefixMap_el;
152         struct ldb_val schemaInfo_val;
153         uint32_t i;
154         int ret;
155         bool ok;
156
157         DEBUG(0,("Analyze and apply schema objects\n"));
158
159         s_dsa                   = talloc_zero(s, struct repsFromTo1);
160         NT_STATUS_HAVE_NO_MEMORY(s_dsa);
161         s_dsa->other_info       = talloc(s_dsa, struct repsFromTo1OtherInfo);
162         NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
163
164         switch (c->ctr_level) {
165         case 1:
166                 mapping_ctr                     = &c->ctr1->mapping_ctr;
167                 total_object_count              = c->ctr1->total_object_count;
168                 object_count                    = s->schema_part.object_count;
169                 first_object                    = s->schema_part.first_object;
170                 linked_attributes_count         = 0;
171                 linked_attributes               = NULL;
172                 s_dsa->highwatermark            = c->ctr1->new_highwatermark;
173                 s_dsa->source_dsa_obj_guid      = c->ctr1->source_dsa_guid;
174                 s_dsa->source_dsa_invocation_id = c->ctr1->source_dsa_invocation_id;
175                 uptodateness_vector             = NULL; /* TODO: map it */
176                 break;
177         case 6:
178                 mapping_ctr                     = &c->ctr6->mapping_ctr;
179                 total_object_count              = c->ctr6->total_object_count;
180                 object_count                    = s->schema_part.object_count;
181                 first_object                    = s->schema_part.first_object;
182                 linked_attributes_count         = 0; /* TODO: ! */
183                 linked_attributes               = NULL; /* TODO: ! */;
184                 s_dsa->highwatermark            = c->ctr6->new_highwatermark;
185                 s_dsa->source_dsa_obj_guid      = c->ctr6->source_dsa_guid;
186                 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
187                 uptodateness_vector             = c->ctr6->uptodateness_vector;
188                 break;
189         default:
190                 return NT_STATUS_INVALID_PARAMETER;
191         }
192
193         s_dsa->replica_flags            = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
194                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
195                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
196         memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
197
198         tmp_dns_name    = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
199         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
200         tmp_dns_name    = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
201         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
202         s_dsa->other_info->dns_name = tmp_dns_name;
203
204         for (cur = first_object; cur; cur = cur->next_object) {
205                 bool is_attr = false;
206                 bool is_class = false;
207
208                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
209                         struct drsuapi_DsReplicaAttribute *a;
210                         uint32_t j;
211                         const char *oid = NULL;
212
213                         a = &cur->object.attribute_ctr.attributes[i];
214                         status = dsdb_map_int2oid(s->self_made_schema, a->attid, s, &oid);
215                         if (!W_ERROR_IS_OK(status)) {
216                                 return werror_to_ntstatus(status);
217                         }
218
219                         switch (a->attid) {
220                         case DRSUAPI_ATTRIBUTE_objectClass:
221                                 for (j=0; j < a->value_ctr.num_values; j++) {
222                                         uint32_t val = 0xFFFFFFFF;
223
224                                         if (a->value_ctr.values[i].blob
225                                             && a->value_ctr.values[i].blob->length == 4) {
226                                                 val = IVAL(a->value_ctr.values[i].blob->data,0);
227                                         }
228
229                                         if (val == DRSUAPI_OBJECTCLASS_attributeSchema) {
230                                                 is_attr = true;
231                                         }
232                                         if (val == DRSUAPI_OBJECTCLASS_classSchema) {
233                                                 is_class = true;
234                                         }
235                                 }
236
237                                 break;
238                         default:
239                                 break;
240                         }
241                 }
242
243                 if (is_attr) {
244                         struct dsdb_attribute *sa;
245
246                         sa = talloc_zero(s->self_made_schema, struct dsdb_attribute);
247                         NT_STATUS_HAVE_NO_MEMORY(sa);
248
249                         status = dsdb_attribute_from_drsuapi(s->self_made_schema, &cur->object, s, sa);
250                         if (!W_ERROR_IS_OK(status)) {
251                                 return werror_to_ntstatus(status);
252                         }
253
254                         DLIST_ADD_END(s->self_made_schema->attributes, sa, struct dsdb_attribute *);
255                 }
256
257                 if (is_class) {
258                         struct dsdb_class *sc;
259
260                         sc = talloc_zero(s->self_made_schema, struct dsdb_class);
261                         NT_STATUS_HAVE_NO_MEMORY(sc);
262
263                         status = dsdb_class_from_drsuapi(s->self_made_schema, &cur->object, s, sc);
264                         if (!W_ERROR_IS_OK(status)) {
265                                 return werror_to_ntstatus(status);
266                         }
267
268                         DLIST_ADD_END(s->self_made_schema->classes, sc, struct dsdb_class *);
269                 }
270         }
271
272         /* attach the schema to the ldb */
273         ret = dsdb_set_schema(s->ldb, s->self_made_schema);
274         if (ret != LDB_SUCCESS) {
275                 return NT_STATUS_FOOBAR;
276         }
277         /* we don't want to access the self made schema anymore */
278         s->self_made_schema = NULL;
279         s->schema = dsdb_get_schema(s->ldb);
280
281         status = dsdb_extended_replicated_objects_commit(s->ldb,
282                                                          c->partition->nc.dn,
283                                                          mapping_ctr,
284                                                          object_count,
285                                                          first_object,
286                                                          linked_attributes_count,
287                                                          linked_attributes,
288                                                          s_dsa,
289                                                          uptodateness_vector,
290                                                          c->gensec_skey,
291                                                          s, &objs);
292         if (!W_ERROR_IS_OK(status)) {
293                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
294                 return werror_to_ntstatus(status);
295         }
296
297         if (lp_parm_bool(s->lp_ctx, NULL, "become dc", "dump objects", false)) {
298                 for (i=0; i < objs->num_objects; i++) {
299                         struct ldb_ldif ldif;
300                         fprintf(stdout, "#\n");
301                         ldif.changetype = LDB_CHANGETYPE_NONE;
302                         ldif.msg = objs->objects[i].msg;
303                         ldb_ldif_write_file(s->ldb, stdout, &ldif);
304                         NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
305                 }
306         }
307
308         msg = ldb_msg_new(objs);
309         NT_STATUS_HAVE_NO_MEMORY(msg);
310         msg->dn = objs->partition_dn;
311
312         status = dsdb_get_oid_mappings_ldb(s->schema, msg, &prefixMap_val, &schemaInfo_val);
313         if (!W_ERROR_IS_OK(status)) {
314                 DEBUG(0,("Failed dsdb_get_oid_mappings_ldb(%s)\n", win_errstr(status)));
315                 return werror_to_ntstatus(status);
316         }
317
318         /* we only add prefixMap here, because schemaInfo is a replicated attribute and already applied */
319         ret = ldb_msg_add_value(msg, "prefixMap", &prefixMap_val, &prefixMap_el);
320         if (ret != LDB_SUCCESS) {
321                 return NT_STATUS_FOOBAR;
322         }
323         prefixMap_el->flags = LDB_FLAG_MOD_REPLACE;
324
325         ret = ldb_modify(s->ldb, msg);
326         if (ret != LDB_SUCCESS) {
327                 DEBUG(0,("Failed to add prefixMap and schemaInfo %s\n", ldb_strerror(ret)));
328                 return NT_STATUS_FOOBAR;
329         }
330
331         talloc_free(s_dsa);
332         talloc_free(objs);
333
334         /* reopen the ldb */
335         talloc_free(s->ldb); /* this also free's the s->schema, because dsdb_set_schema() steals it */
336         s->schema = NULL;
337
338         DEBUG(0,("Reopen the SAM LDB with system credentials and a already stored schema\n"));
339         s->ldb = samdb_connect(s, s->event_ctx, s->lp_ctx, 
340                                system_session(s, s->lp_ctx));
341         if (!s->ldb) {
342                 DEBUG(0,("Failed to reopen sam.ldb\n"));
343                 return NT_STATUS_INTERNAL_DB_ERROR;
344         }
345
346         /* We must set these up to ensure the replMetaData is written correctly, before our NTDS Settings entry is replicated */
347         ok = samdb_set_ntds_invocation_id(s->ldb, &c->dest_dsa->invocation_id);
348         if (!ok) {
349                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
350                 return NT_STATUS_FOOBAR;
351         }
352         ok = samdb_set_ntds_objectGUID(s->ldb, &c->dest_dsa->ntds_guid);
353         if (!ok) {
354                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
355                 return NT_STATUS_FOOBAR;
356         }
357
358         s->schema = dsdb_get_schema(s->ldb);
359         if (!s->schema) {
360                 DEBUG(0,("Failed to get loaded dsdb_schema\n"));
361                 return NT_STATUS_FOOBAR;
362         }
363
364         return NT_STATUS_OK;
365 }
366
367 static NTSTATUS vampire_schema_chunk(void *private_data,
368                                             const struct libnet_BecomeDC_StoreChunk *c)
369 {
370         struct vampire_state *s = talloc_get_type(private_data, struct vampire_state);
371         WERROR status;
372         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
373         uint32_t total_object_count;
374         uint32_t object_count;
375         struct drsuapi_DsReplicaObjectListItemEx *first_object;
376         struct drsuapi_DsReplicaObjectListItemEx *cur;
377
378         switch (c->ctr_level) {
379         case 1:
380                 mapping_ctr             = &c->ctr1->mapping_ctr;
381                 total_object_count      = c->ctr1->total_object_count;
382                 object_count            = c->ctr1->object_count;
383                 first_object            = c->ctr1->first_object;
384                 break;
385         case 6:
386                 mapping_ctr             = &c->ctr6->mapping_ctr;
387                 total_object_count      = c->ctr6->total_object_count;
388                 object_count            = c->ctr6->object_count;
389                 first_object            = c->ctr6->first_object;
390                 break;
391         default:
392                 return NT_STATUS_INVALID_PARAMETER;
393         }
394
395         if (total_object_count) {
396                 DEBUG(0,("Schema-DN[%s] objects[%u/%u]\n",
397                         c->partition->nc.dn, object_count, total_object_count));
398         } else {
399                 DEBUG(0,("Schema-DN[%s] objects[%u]\n",
400                 c->partition->nc.dn, object_count));
401         }
402
403         if (!s->schema) {
404                 s->self_made_schema = dsdb_new_schema(s, lp_iconv_convenience(s->lp_ctx));
405
406                 NT_STATUS_HAVE_NO_MEMORY(s->self_made_schema);
407
408                 status = dsdb_load_oid_mappings_drsuapi(s->self_made_schema, mapping_ctr);
409                 if (!W_ERROR_IS_OK(status)) {
410                         return werror_to_ntstatus(status);
411                 }
412
413                 s->schema = s->self_made_schema;
414         } else {
415                 status = dsdb_verify_oid_mappings_drsuapi(s->schema, mapping_ctr);
416                 if (!W_ERROR_IS_OK(status)) {
417                         return werror_to_ntstatus(status);
418                 }
419         }
420
421         if (!s->schema_part.first_object) {
422                 s->schema_part.object_count = object_count;
423                 s->schema_part.first_object = talloc_steal(s, first_object);
424         } else {
425                 s->schema_part.object_count             += object_count;
426                 s->schema_part.last_object->next_object = talloc_steal(s->schema_part.last_object,
427                                                                        first_object);
428         }
429         for (cur = first_object; cur->next_object; cur = cur->next_object) {}
430         s->schema_part.last_object = cur;
431
432         if (c->partition->highwatermark.tmp_highest_usn == c->partition->highwatermark.highest_usn) {
433                 return vampire_apply_schema(s, c);
434         }
435
436         return NT_STATUS_OK;
437 }
438
439 static NTSTATUS vampire_store_chunk(void *private_data,
440                                            const struct libnet_BecomeDC_StoreChunk *c)
441 {
442         struct vampire_state *s = talloc_get_type(private_data, struct vampire_state);
443         WERROR status;
444         const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
445         uint32_t total_object_count;
446         uint32_t object_count;
447         struct drsuapi_DsReplicaObjectListItemEx *first_object;
448         uint32_t linked_attributes_count;
449         struct drsuapi_DsReplicaLinkedAttribute *linked_attributes;
450         const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector;
451         struct dsdb_extended_replicated_objects *objs;
452         struct repsFromTo1 *s_dsa;
453         char *tmp_dns_name;
454         uint32_t i;
455
456         s_dsa                   = talloc_zero(s, struct repsFromTo1);
457         NT_STATUS_HAVE_NO_MEMORY(s_dsa);
458         s_dsa->other_info       = talloc(s_dsa, struct repsFromTo1OtherInfo);
459         NT_STATUS_HAVE_NO_MEMORY(s_dsa->other_info);
460
461         switch (c->ctr_level) {
462         case 1:
463                 mapping_ctr                     = &c->ctr1->mapping_ctr;
464                 total_object_count              = c->ctr1->total_object_count;
465                 object_count                    = c->ctr1->object_count;
466                 first_object                    = c->ctr1->first_object;
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                 total_object_count              = c->ctr6->total_object_count;
477                 object_count                    = c->ctr6->object_count;
478                 first_object                    = c->ctr6->first_object;
479                 linked_attributes_count         = c->ctr6->linked_attributes_count;
480                 linked_attributes               = c->ctr6->linked_attributes;
481                 s_dsa->highwatermark            = c->ctr6->new_highwatermark;
482                 s_dsa->source_dsa_obj_guid      = c->ctr6->source_dsa_guid;
483                 s_dsa->source_dsa_invocation_id = c->ctr6->source_dsa_invocation_id;
484                 uptodateness_vector             = c->ctr6->uptodateness_vector;
485                 break;
486         default:
487                 return NT_STATUS_INVALID_PARAMETER;
488         }
489
490         s_dsa->replica_flags            = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE
491                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
492                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
493         memset(s_dsa->schedule, 0x11, sizeof(s_dsa->schedule));
494
495         tmp_dns_name    = GUID_string(s_dsa->other_info, &s_dsa->source_dsa_obj_guid);
496         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
497         tmp_dns_name    = talloc_asprintf_append_buffer(tmp_dns_name, "._msdcs.%s", c->forest->dns_name);
498         NT_STATUS_HAVE_NO_MEMORY(tmp_dns_name);
499         s_dsa->other_info->dns_name = tmp_dns_name;
500
501         if (total_object_count) {
502                 DEBUG(0,("Partition[%s] objects[%u/%u]\n",
503                         c->partition->nc.dn, object_count, total_object_count));
504         } else {
505                 DEBUG(0,("Partition[%s] objects[%u]\n",
506                 c->partition->nc.dn, object_count));
507         }
508
509         status = dsdb_extended_replicated_objects_commit(s->ldb,
510                                                          c->partition->nc.dn,
511                                                          mapping_ctr,
512                                                          object_count,
513                                                          first_object,
514                                                          linked_attributes_count,
515                                                          linked_attributes,
516                                                          s_dsa,
517                                                          uptodateness_vector,
518                                                          c->gensec_skey,
519                                                          s, &objs);
520         if (!W_ERROR_IS_OK(status)) {
521                 DEBUG(0,("Failed to commit objects: %s\n", win_errstr(status)));
522                 return werror_to_ntstatus(status);
523         }
524
525         if (lp_parm_bool(s->lp_ctx, NULL, "become dc", "dump objects", false)) {
526                 for (i=0; i < objs->num_objects; i++) {
527                         struct ldb_ldif ldif;
528                         fprintf(stdout, "#\n");
529                         ldif.changetype = LDB_CHANGETYPE_NONE;
530                         ldif.msg = objs->objects[i].msg;
531                         ldb_ldif_write_file(s->ldb, stdout, &ldif);
532                         NDR_PRINT_DEBUG(replPropertyMetaDataBlob, objs->objects[i].meta_data);
533                 }
534         }
535         talloc_free(s_dsa);
536         talloc_free(objs);
537
538         for (i=0; i < linked_attributes_count; i++) {
539                 const struct dsdb_attribute *sa;
540
541                 if (!linked_attributes[i].identifier) {
542                         return NT_STATUS_FOOBAR;                
543                 }
544
545                 if (!linked_attributes[i].value.blob) {
546                         return NT_STATUS_FOOBAR;                
547                 }
548
549                 sa = dsdb_attribute_by_attributeID_id(s->schema,
550                                                       linked_attributes[i].attid);
551                 if (!sa) {
552                         return NT_STATUS_FOOBAR;
553                 }
554
555                 if (lp_parm_bool(s->lp_ctx, NULL, "become dc", "dump objects", false)) {
556                         DEBUG(0,("# %s\n", sa->lDAPDisplayName));
557                         NDR_PRINT_DEBUG(drsuapi_DsReplicaLinkedAttribute, &linked_attributes[i]);
558                         dump_data(0,
559                                 linked_attributes[i].value.blob->data,
560                                 linked_attributes[i].value.blob->length);
561                 }
562         }
563
564         return NT_STATUS_OK;
565 }
566
567 NTSTATUS libnet_Vampire(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, 
568                         struct libnet_Vampire *r)
569 {
570         struct libnet_JoinDomain *join;
571         struct libnet_set_join_secrets *set_secrets;
572         struct libnet_BecomeDC b;
573         struct vampire_state *s;
574         struct ldb_message *msg;
575         int ldb_ret;
576         uint32_t i;
577         NTSTATUS status;
578
579         const char *account_name;
580         const char *netbios_name;
581         
582         r->out.error_string = NULL;
583
584         s = talloc_zero(mem_ctx, struct vampire_state);
585         if (!s) {
586                 return NT_STATUS_NO_MEMORY;
587         }
588
589         s->lp_ctx = ctx->lp_ctx;
590         s->event_ctx = ctx->event_ctx;
591
592         join = talloc_zero(s, struct libnet_JoinDomain);
593         if (!join) {
594                 return NT_STATUS_NO_MEMORY;
595         }
596                 
597         if (r->in.netbios_name != NULL) {
598                 netbios_name = r->in.netbios_name;
599         } else {
600                 netbios_name = talloc_reference(join, lp_netbios_name(ctx->lp_ctx));
601                 if (!netbios_name) {
602                         r->out.error_string = NULL;
603                         talloc_free(s);
604                         return NT_STATUS_NO_MEMORY;
605                 }
606         }
607
608         account_name = talloc_asprintf(join, "%s$", netbios_name);
609         if (!account_name) {
610                 r->out.error_string = NULL;
611                 talloc_free(s);
612                 return NT_STATUS_NO_MEMORY;
613         }
614         
615         join->in.domain_name    = r->in.domain_name;
616         join->in.account_name   = account_name;
617         join->in.netbios_name   = netbios_name;
618         join->in.level          = LIBNET_JOINDOMAIN_AUTOMATIC;
619         join->in.acct_type      = ACB_WSTRUST;
620         join->in.recreate_account = false;
621         status = libnet_JoinDomain(ctx, join, join);
622         if (!NT_STATUS_IS_OK(status)) {
623                 r->out.error_string = talloc_steal(mem_ctx, join->out.error_string);
624                 talloc_free(s);
625                 return status;
626         }
627         
628         s->join = join;
629
630         s->targetdir = r->in.targetdir;
631
632         ZERO_STRUCT(b);
633         b.in.domain_dns_name            = join->out.realm;
634         b.in.domain_netbios_name        = join->out.domain_name;
635         b.in.domain_sid                 = join->out.domain_sid;
636         b.in.source_dsa_address         = join->out.samr_binding->host;
637         b.in.dest_dsa_netbios_name      = netbios_name;
638
639         b.in.callbacks.private_data     = s;
640         b.in.callbacks.check_options    = vampire_check_options;
641         b.in.callbacks.prepare_db       = vampire_prepare_db;
642         b.in.callbacks.schema_chunk     = vampire_schema_chunk;
643         b.in.callbacks.config_chunk     = vampire_store_chunk;
644         b.in.callbacks.domain_chunk     = vampire_store_chunk;
645
646         status = libnet_BecomeDC(ctx, s, &b);
647         if (!NT_STATUS_IS_OK(status)) {
648                 printf("libnet_BecomeDC() failed - %s\n", nt_errstr(status));
649                 talloc_free(s);
650                 return status;
651         }
652
653         msg = ldb_msg_new(s);
654         if (!msg) {
655                 printf("ldb_msg_new() failed\n");
656                 talloc_free(s);
657                 return NT_STATUS_NO_MEMORY;
658         }
659         msg->dn = ldb_dn_new(msg, s->ldb, "@ROOTDSE");
660         if (!msg->dn) {
661                 printf("ldb_msg_new(@ROOTDSE) failed\n");
662                 talloc_free(s);
663                 return NT_STATUS_NO_MEMORY;
664         }
665
666         ldb_ret = ldb_msg_add_string(msg, "isSynchronized", "TRUE");
667         if (ldb_ret != LDB_SUCCESS) {
668                 printf("ldb_msg_add_string(msg, isSynchronized, TRUE) failed: %d\n", ldb_ret);
669                 talloc_free(s);
670                 return NT_STATUS_NO_MEMORY;
671         }
672
673         for (i=0; i < msg->num_elements; i++) {
674                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
675         }
676
677         printf("mark ROOTDSE with isSynchronized=TRUE\n");
678         ldb_ret = ldb_modify(s->ldb, msg);
679         if (ldb_ret != LDB_SUCCESS) {
680                 printf("ldb_modify() failed: %d\n", ldb_ret);
681                 talloc_free(s);
682                 return NT_STATUS_INTERNAL_DB_ERROR;
683         }
684
685         set_secrets = talloc_zero(s, struct libnet_set_join_secrets);
686         if (!set_secrets) {
687                 return NT_STATUS_NO_MEMORY;
688         }
689                 
690         set_secrets->in.domain_name = join->out.domain_name;
691         set_secrets->in.realm = join->out.realm;
692         set_secrets->in.account_name = account_name;
693         set_secrets->in.netbios_name = netbios_name;
694         set_secrets->in.join_type = SEC_CHAN_BDC;
695         set_secrets->in.join_password = join->out.join_password;
696         set_secrets->in.kvno = join->out.kvno;
697         set_secrets->in.domain_sid = join->out.domain_sid;
698         
699         status = libnet_set_join_secrets(ctx, set_secrets, set_secrets);
700         if (!NT_STATUS_IS_OK(status)) {
701                 r->out.error_string = talloc_steal(mem_ctx, set_secrets->out.error_string);
702                 talloc_free(s);
703                 return status;
704         }
705
706         r->out.domain_name = talloc_steal(r, join->out.domain_name);
707         r->out.domain_sid = talloc_steal(r, join->out.domain_sid);
708         talloc_free(s);
709         
710         return NT_STATUS_OK;
711
712 }