s3: winbindd: Move calling setup_domain_child() into add_trusted_domain().
[Samba/wip.git] / source4 / libnet / libnet_become_dc.c
blob3e4c7f871cc69eb74bb0c79ca39cbff68dfe061d
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "libnet/libnet.h"
22 #include "libcli/composite/composite.h"
23 #include "libcli/cldap/cldap.h"
24 #include <ldb.h>
25 #include <ldb_errors.h>
26 #include "ldb_wrap.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "../libds/common/flags.h"
29 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
30 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "librpc/gen_ndr/ndr_security.h"
33 #include "librpc/gen_ndr/ndr_nbt.h"
34 #include "librpc/gen_ndr/ndr_drsuapi.h"
35 #include "auth/gensec/gensec.h"
36 #include "param/param.h"
37 #include "lib/tsocket/tsocket.h"
39 /*****************************************************************************
40 * Windows 2003 (w2k3) does the following steps when changing the server role
41 * from domain member to domain controller
43 * We mostly do the same.
44 *****************************************************************************/
47 * lookup DC:
48 * - using nbt name<1C> request and a samlogon mailslot request
49 * or
50 * - using a DNS SRV _ldap._tcp.dc._msdcs. request and a CLDAP netlogon request
52 * see: becomeDC_recv_cldap() and becomeDC_send_cldap()
56 * Open 1st LDAP connection to the DC using admin credentials
58 * see: becomeDC_connect_ldap1() and becomeDC_ldap_connect()
62 * LDAP search 1st LDAP connection:
64 * see: becomeDC_ldap1_rootdse()
66 * Request:
67 * basedn: ""
68 * scope: base
69 * filter: (objectClass=*)
70 * attrs: *
71 * Result:
72 * ""
73 * currentTime: 20061202155100.0Z
74 * subschemaSubentry: CN=Aggregate,CN=Schema,CN=Configuration,<domain_partition>
75 * dsServiceName: CN=<netbios_name>,CN=Servers,CN=<site_name>,CN=Sites,CN=Configuration,<domain_partition>
76 * namingContexts: <domain_partition>
77 * CN=Configuration,<domain_partition>
78 * CN=Schema,CN=Configuration,<domain_partition>
79 * defaultNamingContext: <domain_partition>
80 * schemaNamingContext: CN=Schema,CN=Configuration,<domain_partition>
81 * configurationNamingContext:CN=Configuration,<domain_partition>
82 * rootDomainNamingContext:<domain_partition>
83 * supportedControl: ...
84 * supportedLDAPVersion: 3
85 * 2
86 * supportedLDAPPolicies: ...
87 * highestCommitedUSN: ...
88 * supportedSASLMechanisms:GSSAPI
89 * GSS-SPNEGO
90 * EXTERNAL
91 * DIGEST-MD5
92 * dnsHostName: <dns_host_name>
93 * ldapServiceName: <domain_dns_name>:<netbios_name>$@<REALM>
94 * serverName: CN=Servers,CN=<site_name>,CN=Sites,CN=Configuration,<domain_partition>
95 * supportedCapabilities: ...
96 * isSyncronized: TRUE
97 * isGlobalCatalogReady: TRUE
98 * domainFunctionality: 0
99 * forestFunctionality: 0
100 * domainControllerFunctionality: 2
104 * LDAP search 1st LDAP connection:
106 * see: becomeDC_ldap1_crossref_behavior_version()
108 * Request:
109 * basedn: CN=Configuration,<domain_partition>
110 * scope: one
111 * filter: (cn=Partitions)
112 * attrs: msDS-Behavior-Version
113 * Result:
114 * CN=Partitions,CN=Configuration,<domain_partition>
115 * msDS-Behavior-Version: 0
119 * LDAP search 1st LDAP connection:
121 * NOTE: this seems to be a bug! as the messageID of the LDAP message is corrupted!
123 * not implemented here
125 * Request:
126 * basedn: CN=Schema,CN=Configuration,<domain_partition>
127 * scope: one
128 * filter: (cn=Partitions)
129 * attrs: msDS-Behavior-Version
130 * Result:
131 * <none>
136 * LDAP search 1st LDAP connection:
138 * see: becomeDC_ldap1_domain_behavior_version()
140 * Request:
141 * basedn: <domain_partition>
142 * scope: base
143 * filter: (objectClass=*)
144 * attrs: msDS-Behavior-Version
145 * Result:
146 * <domain_partition>
147 * msDS-Behavior-Version: 0
151 * LDAP search 1st LDAP connection:
153 * see: becomeDC_ldap1_schema_object_version()
155 * Request:
156 * basedn: CN=Schema,CN=Configuration,<domain_partition>
157 * scope: base
158 * filter: (objectClass=*)
159 * attrs: objectVersion
160 * Result:
161 * CN=Schema,CN=Configuration,<domain_partition>
162 * objectVersion: 30
166 * LDAP search 1st LDAP connection:
168 * not implemented, because the information is already there
170 * Request:
171 * basedn: ""
172 * scope: base
173 * filter: (objectClass=*)
174 * attrs: defaultNamingContext
175 * dnsHostName
176 * Result:
177 * ""
178 * defaultNamingContext: <domain_partition>
179 * dnsHostName: <dns_host_name>
183 * LDAP search 1st LDAP connection:
185 * see: becomeDC_ldap1_infrastructure_fsmo()
187 * Request:
188 * basedn: <WKGUID=2fbac1870ade11d297c400c04fd8d5cd,domain_partition>
189 * scope: base
190 * filter: (objectClass=*)
191 * attrs: 1.1
192 * Result:
193 * CN=Infrastructure,<domain_partition>
197 * LDAP search 1st LDAP connection:
199 * see: becomeDC_ldap1_w2k3_update_revision()
201 * Request:
202 * basedn: CN=Windows2003Update,CN=DomainUpdates,CN=System,<domain_partition>
203 * scope: base
204 * filter: (objectClass=*)
205 * attrs: revision
206 * Result:
207 * CN=Windows2003Update,CN=DomainUpdates,CN=System,<domain_partition>
208 * revision: 8
212 * LDAP search 1st LDAP connection:
214 * see: becomeDC_ldap1_infrastructure_fsmo()
216 * Request:
217 * basedn: CN=Infrastructure,<domain_partition>
218 * scope: base
219 * filter: (objectClass=*)
220 * attrs: fSMORoleOwner
221 * Result:
222 * CN=Infrastructure,<domain_partition>
223 * fSMORoleOwner: CN=NTDS Settings,<infrastructure_fsmo_server_object>
227 * LDAP search 1st LDAP connection:
229 * see: becomeDC_ldap1_infrastructure_fsmo()
231 * Request:
232 * basedn: <infrastructure_fsmo_server_object>
233 * scope: base
234 * filter: (objectClass=*)
235 * attrs: dnsHostName
236 * Result:
237 * <infrastructure_fsmo_server_object>
238 * dnsHostName: <dns_host_name>
242 * LDAP search 1st LDAP connection:
244 * see: becomeDC_ldap1_infrastructure_fsmo()
246 * Request:
247 * basedn: CN=NTDS Settings,<infrastructure_fsmo_server_object>
248 * scope: base
249 * filter: (objectClass=*)
250 * attrs: objectGUID
251 * Result:
252 * CN=NTDS Settings,<infrastructure_fsmo_server_object>
253 * objectGUID: <object_guid>
257 * LDAP search 1st LDAP connection:
259 * see: becomeDC_ldap1_rid_manager_fsmo()
261 * Request:
262 * basedn: <domain_partition>
263 * scope: base
264 * filter: (objectClass=*)
265 * attrs: rIDManagerReference
266 * Result:
267 * <domain_partition>
268 * rIDManagerReference: CN=RID Manager$,CN=System,<domain_partition>
272 * LDAP search 1st LDAP connection:
274 * see: becomeDC_ldap1_rid_manager_fsmo()
276 * Request:
277 * basedn: CN=RID Manager$,CN=System,<domain_partition>
278 * scope: base
279 * filter: (objectClass=*)
280 * attrs: fSMORoleOwner
281 * Result:
282 * CN=Infrastructure,<domain_partition>
283 * fSMORoleOwner: CN=NTDS Settings,<rid_manager_fsmo_server_object>
287 * LDAP search 1st LDAP connection:
289 * see: becomeDC_ldap1_rid_manager_fsmo()
291 * Request:
292 * basedn: <rid_manager_fsmo_server_object>
293 * scope: base
294 * filter: (objectClass=*)
295 * attrs: dnsHostName
296 * Result:
297 * <rid_manager_fsmo_server_object>
298 * dnsHostName: <dns_host_name>
302 * LDAP search 1st LDAP connection:
304 * see: becomeDC_ldap1_rid_manager_fsmo()
306 * Request:
307 * basedn: CN=NTDS Settings,<rid_manager_fsmo_server_object>
308 * scope: base
309 * filter: (objectClass=*)
310 * attrs: msDs-ReplicationEpoch
311 * Result:
312 * CN=NTDS Settings,<rid_manager_fsmo_server_object>
316 * LDAP search 1st LDAP connection:
318 * see: becomeDC_ldap1_site_object()
320 * Request:
321 * basedn: CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>
322 * scope: base
323 * filter: (objectClass=*)
324 * attrs:
325 * Result:
326 * CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>
327 * objectClass: top
328 * site
329 * cn: <new_dc_site_name>
330 * distinguishedName:CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>
331 * instanceType: 4
332 * whenCreated: ...
333 * whenChanged: ...
334 * uSNCreated: ...
335 * uSNChanged: ...
336 * showInAdvancedViewOnly: TRUE
337 * name: <new_dc_site_name>
338 * objectGUID: <object_guid>
339 * systemFlags: 1107296256 <0x42000000>
340 * objectCategory: CN=Site,CN=Schema,CN=Configuration,<domain_partition>
343 /***************************************************************
344 * Add this stage we call the check_options() callback function
345 * of the caller, to see if he wants us to continue
347 * see: becomeDC_check_options()
348 ***************************************************************/
351 * LDAP search 1st LDAP connection:
353 * see: becomeDC_ldap1_computer_object()
355 * Request:
356 * basedn: <domain_partition>
357 * scope: sub
358 * filter: (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<new_dc_account_name>))
359 * attrs: distinguishedName
360 * userAccountControl
361 * Result:
362 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
363 * distinguishedName: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
364 * userAccoountControl: 4096 <0x1000>
368 * LDAP search 1st LDAP connection:
370 * see: becomeDC_ldap1_server_object_1()
372 * Request:
373 * basedn: CN=<new_dc_netbios_name>,CN=Servers,CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>
374 * scope: base
375 * filter: (objectClass=*)
376 * attrs:
377 * Result:
378 * <noSuchObject>
379 * <matchedDN:CN=Servers,CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>>
383 * LDAP search 1st LDAP connection:
385 * see: becomeDC_ldap1_server_object_2()
387 * Request:
388 * basedn: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
389 * scope: base
390 * filter: (objectClass=*)
391 * attrs: serverReferenceBL
392 * typesOnly: TRUE!!!
393 * Result:
394 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
398 * LDAP add 1st LDAP connection:
400 * see: becomeDC_ldap1_server_object_add()
402 * Request:
403 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
404 * objectClass: server
405 * systemFlags: 50000000 <0x2FAF080>
406 * serverReference:CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
407 * Result:
408 * <success>
412 * LDAP search 1st LDAP connection:
414 * not implemented, maybe we can add that later
416 * Request:
417 * basedn: CN=NTDS Settings,CN=<new_dc_netbios_name>,CN=Servers,CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>
418 * scope: base
419 * filter: (objectClass=*)
420 * attrs:
421 * Result:
422 * <noSuchObject>
423 * <matchedDN:CN=<new_dc_netbios_name>,CN=Servers,CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>>
427 * LDAP search 1st LDAP connection:
429 * not implemented because it gives no new information
431 * Request:
432 * basedn: CN=Partitions,CN=Configuration,<domain_partition>
433 * scope: sub
434 * filter: (nCName=<domain_partition>)
435 * attrs: nCName
436 * dnsRoot
437 * controls: LDAP_SERVER_EXTENDED_DN_OID:critical=false
438 * Result:
439 * <GUID=<hex_guid>>;CN=<domain_netbios_name>,CN=Partitions,<domain_partition>>
440 * nCName: <GUID=<hex_guid>>;<SID=<hex_sid>>;<domain_partition>>
441 * dnsRoot: <domain_dns_name>
445 * LDAP modify 1st LDAP connection:
447 * see: becomeDC_ldap1_server_object_modify()
449 * Request (add):
450 * CN=<new_dc_netbios_name>,CN=Servers,CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>>
451 * serverReference:CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
452 * Result:
453 * <attributeOrValueExist>
457 * LDAP modify 1st LDAP connection:
459 * see: becomeDC_ldap1_server_object_modify()
461 * Request (replace):
462 * CN=<new_dc_netbios_name>,CN=Servers,CN=<new_dc_site_name>,CN=Sites,CN=Configuration,<domain_partition>>
463 * serverReference:CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
464 * Result:
465 * <success>
469 * Open 1st DRSUAPI connection to the DC using admin credentials
470 * DsBind with DRSUAPI_DS_BIND_GUID_W2K3 ("6afab99c-6e26-464a-975f-f58f105218bc")
471 * (w2k3 does 2 DsBind() calls here..., where is first is unused and contains garbage at the end)
473 * see: becomeDC_drsuapi_connect_send(), becomeDC_drsuapi1_connect_recv(),
474 * becomeDC_drsuapi_bind_send(), becomeDC_drsuapi_bind_recv() and becomeDC_drsuapi1_bind_recv()
478 * DsAddEntry to create the CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
479 * on the 1st DRSUAPI connection
481 * see: becomeDC_drsuapi1_add_entry_send() and becomeDC_drsuapi1_add_entry_recv()
484 /***************************************************************
485 * Add this stage we call the prepare_db() callback function
486 * of the caller, to see if he wants us to continue
488 * see: becomeDC_prepare_db()
489 ***************************************************************/
492 * Open 2nd and 3rd DRSUAPI connection to the DC using admin credentials
493 * - a DsBind with DRSUAPI_DS_BIND_GUID_W2K3 ("6afab99c-6e26-464a-975f-f58f105218bc")
494 * on the 2nd connection
496 * see: becomeDC_drsuapi_connect_send(), becomeDC_drsuapi2_connect_recv(),
497 * becomeDC_drsuapi_bind_send(), becomeDC_drsuapi_bind_recv(), becomeDC_drsuapi2_bind_recv()
498 * and becomeDC_drsuapi3_connect_recv()
502 * replicate CN=Schema,CN=Configuration,...
503 * on the 3rd DRSUAPI connection and the bind_handle from the 2nd connection
505 * see: becomeDC_drsuapi_pull_partition_send(), becomeDC_drsuapi_pull_partition_recv(),
506 * becomeDC_drsuapi3_pull_schema_send() and becomeDC_drsuapi3_pull_schema_recv()
508 ***************************************************************
509 * Add this stage we call the schema_chunk() callback function
510 * for each replication message
511 ***************************************************************/
514 * replicate CN=Configuration,...
515 * on the 3rd DRSUAPI connection and the bind_handle from the 2nd connection
517 * see: becomeDC_drsuapi_pull_partition_send(), becomeDC_drsuapi_pull_partition_recv(),
518 * becomeDC_drsuapi3_pull_config_send() and becomeDC_drsuapi3_pull_config_recv()
520 ***************************************************************
521 * Add this stage we call the config_chunk() callback function
522 * for each replication message
523 ***************************************************************/
526 * LDAP unbind on the 1st LDAP connection
528 * not implemented, because it's not needed...
532 * Open 2nd LDAP connection to the DC using admin credentials
534 * see: becomeDC_connect_ldap2() and becomeDC_ldap_connect()
538 * LDAP search 2nd LDAP connection:
540 * not implemented because it gives no new information
541 * same as becomeDC_ldap1_computer_object()
543 * Request:
544 * basedn: <domain_partition>
545 * scope: sub
546 * filter: (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<new_dc_account_name>))
547 * attrs: distinguishedName
548 * userAccountControl
549 * Result:
550 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
551 * distinguishedName: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
552 * userAccoountControl: 4096 <0x00001000>
556 * LDAP search 2nd LDAP connection:
558 * not implemented because it gives no new information
559 * same as becomeDC_ldap1_computer_object()
561 * Request:
562 * basedn: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
563 * scope: base
564 * filter: (objectClass=*)
565 * attrs: userAccountControl
566 * Result:
567 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
568 * userAccoountControl: 4096 <0x00001000>
572 * LDAP modify 2nd LDAP connection:
574 * see: becomeDC_ldap2_modify_computer()
576 * Request (replace):
577 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
578 * userAccoountControl: 532480 <0x82000>
579 * Result:
580 * <success>
584 * LDAP search 2nd LDAP connection:
586 * see: becomeDC_ldap2_move_computer()
588 * Request:
589 * basedn: <WKGUID=2fbac1870ade11d297c400c04fd8d5cd,<domain_partition>>
590 * scope: base
591 * filter: (objectClass=*)
592 * attrs: 1.1
593 * Result:
594 * CN=Domain Controllers,<domain_partition>
598 * LDAP search 2nd LDAP connection:
600 * not implemented because it gives no new information
602 * Request:
603 * basedn: CN=Domain Controllers,<domain_partition>
604 * scope: base
605 * filter: (objectClass=*)
606 * attrs: distinguishedName
607 * Result:
608 * CN=Domain Controller,<domain_partition>
609 * distinguishedName: CN=Domain Controllers,<domain_partition>
613 * LDAP modifyRDN 2nd LDAP connection:
615 * see: becomeDC_ldap2_move_computer()
617 * Request:
618 * entry: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
619 * newrdn: CN=<new_dc_netbios_name>
620 * deleteoldrdn: TRUE
621 * newparent: CN=Domain Controllers,<domain_partition>
622 * Result:
623 * <success>
627 * LDAP unbind on the 2nd LDAP connection
629 * not implemented, because it's not needed...
633 * replicate Domain Partition
634 * on the 3rd DRSUAPI connection and the bind_handle from the 2nd connection
636 * see: becomeDC_drsuapi_pull_partition_send(), becomeDC_drsuapi_pull_partition_recv(),
637 * becomeDC_drsuapi3_pull_domain_send() and becomeDC_drsuapi3_pull_domain_recv()
639 ***************************************************************
640 * Add this stage we call the domain_chunk() callback function
641 * for each replication message
642 ***************************************************************/
644 /* call DsReplicaUpdateRefs() for all partitions like this:
645 * req1: struct drsuapi_DsReplicaUpdateRefsRequest1
647 * naming_context: struct drsuapi_DsReplicaObjectIdentifier
648 * __ndr_size : 0x000000ae (174)
649 * __ndr_size_sid : 0x00000000 (0)
650 * guid : 00000000-0000-0000-0000-000000000000
651 * sid : S-0-0
652 * dn : 'CN=Schema,CN=Configuration,DC=w2k3,DC=vmnet1,DC=vm,DC=base'
654 * dest_dsa_dns_name : '4a0df188-a0b8-47ea-bbe5-e614723f16dd._msdcs.w2k3.vmnet1.vm.base'
655 * dest_dsa_guid : 4a0df188-a0b8-47ea-bbe5-e614723f16dd
656 * options : 0x0000001c (28)
657 * 0: DRSUAPI_DS_REPLICA_UPDATE_ASYNCHRONOUS_OPERATION
658 * 0: DRSUAPI_DS_REPLICA_UPDATE_WRITEABLE
659 * 1: DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE
660 * 1: DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE
661 * 1: DRSUAPI_DS_REPLICA_UPDATE_0x00000010
663 * 4a0df188-a0b8-47ea-bbe5-e614723f16dd is the objectGUID the DsAddEntry() returned for the
664 * CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
665 * on the 2nd!!! DRSUAPI connection
667 * see: becomeDC_drsuapi_update_refs_send(), becomeDC_drsuapi2_update_refs_schema_recv(),
668 * becomeDC_drsuapi2_update_refs_config_recv() and becomeDC_drsuapi2_update_refs_domain_recv()
672 * Windows does opens the 4th and 5th DRSUAPI connection...
673 * and does a DsBind() with the objectGUID from DsAddEntry() as bind_guid
674 * on the 4th connection
676 * and then 2 full replications of the domain partition on the 5th connection
677 * with the bind_handle from the 4th connection
679 * not implemented because it gives no new information
682 struct libnet_BecomeDC_state {
683 struct composite_context *creq;
685 struct libnet_context *libnet;
687 struct dom_sid zero_sid;
689 struct {
690 struct cldap_socket *sock;
691 struct cldap_netlogon io;
692 struct NETLOGON_SAM_LOGON_RESPONSE_EX netlogon;
693 } cldap;
695 struct becomeDC_ldap {
696 struct ldb_context *ldb;
697 const struct ldb_message *rootdse;
698 } ldap1, ldap2;
700 struct becomeDC_drsuapi {
701 struct libnet_BecomeDC_state *s;
702 struct dcerpc_binding *binding;
703 struct dcerpc_pipe *pipe;
704 struct dcerpc_binding_handle *drsuapi_handle;
705 DATA_BLOB gensec_skey;
706 struct drsuapi_DsBind bind_r;
707 struct GUID bind_guid;
708 struct drsuapi_DsBindInfoCtr bind_info_ctr;
709 struct drsuapi_DsBindInfo28 local_info28;
710 struct drsuapi_DsBindInfo28 remote_info28;
711 struct policy_handle bind_handle;
712 } drsuapi1, drsuapi2, drsuapi3;
714 void *ndr_struct_ptr;
716 struct libnet_BecomeDC_Domain domain;
717 struct libnet_BecomeDC_Forest forest;
718 struct libnet_BecomeDC_SourceDSA source_dsa;
719 struct libnet_BecomeDC_DestDSA dest_dsa;
721 struct libnet_BecomeDC_Partition schema_part, config_part, domain_part;
723 struct becomeDC_fsmo {
724 const char *dns_name;
725 const char *server_dn_str;
726 const char *ntds_dn_str;
727 struct GUID ntds_guid;
728 } infrastructure_fsmo;
730 struct becomeDC_fsmo rid_manager_fsmo;
732 struct libnet_BecomeDC_CheckOptions _co;
733 struct libnet_BecomeDC_PrepareDB _pp;
734 struct libnet_BecomeDC_StoreChunk _sc;
735 struct libnet_BecomeDC_Callbacks callbacks;
737 bool rodc_join;
738 bool critical_only;
741 static int32_t get_dc_function_level(struct loadparm_context *lp_ctx)
743 /* per default we are (Windows) 2008 R2 compatible */
744 return lpcfg_parm_int(lp_ctx, NULL, "ads", "dc function level",
745 DS_DOMAIN_FUNCTION_2008_R2);
748 static void becomeDC_recv_cldap(struct tevent_req *req);
750 static void becomeDC_send_cldap(struct libnet_BecomeDC_state *s)
752 struct composite_context *c = s->creq;
753 struct tevent_req *req;
754 struct tsocket_address *dest_address;
755 int ret;
757 s->cldap.io.in.dest_address = NULL;
758 s->cldap.io.in.dest_port = 0;
759 s->cldap.io.in.realm = s->domain.dns_name;
760 s->cldap.io.in.host = s->dest_dsa.netbios_name;
761 s->cldap.io.in.user = NULL;
762 s->cldap.io.in.domain_guid = NULL;
763 s->cldap.io.in.domain_sid = NULL;
764 s->cldap.io.in.acct_control = -1;
765 s->cldap.io.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
766 s->cldap.io.in.map_response = true;
768 ret = tsocket_address_inet_from_strings(s, "ip",
769 s->source_dsa.address,
770 lpcfg_cldap_port(s->libnet->lp_ctx),
771 &dest_address);
772 if (ret != 0) {
773 c->status = map_nt_error_from_unix_common(errno);
774 if (!composite_is_ok(c)) return;
777 c->status = cldap_socket_init(s, NULL, dest_address, &s->cldap.sock);
778 if (!composite_is_ok(c)) return;
780 req = cldap_netlogon_send(s, s->libnet->event_ctx,
781 s->cldap.sock, &s->cldap.io);
782 if (composite_nomem(req, c)) return;
783 tevent_req_set_callback(req, becomeDC_recv_cldap, s);
786 static void becomeDC_connect_ldap1(struct libnet_BecomeDC_state *s);
788 static void becomeDC_recv_cldap(struct tevent_req *req)
790 struct libnet_BecomeDC_state *s = tevent_req_callback_data(req,
791 struct libnet_BecomeDC_state);
792 struct composite_context *c = s->creq;
794 c->status = cldap_netlogon_recv(req, s, &s->cldap.io);
795 talloc_free(req);
796 if (!composite_is_ok(c)) {
797 DEBUG(0,("Failed to send, receive or parse CLDAP reply from server %s for our host %s: %s\n",
798 s->cldap.io.in.dest_address,
799 s->cldap.io.in.host,
800 nt_errstr(c->status)));
801 return;
803 s->cldap.netlogon = s->cldap.io.out.netlogon.data.nt5_ex;
805 s->domain.dns_name = s->cldap.netlogon.dns_domain;
806 s->domain.netbios_name = s->cldap.netlogon.domain_name;
807 s->domain.guid = s->cldap.netlogon.domain_uuid;
809 s->forest.dns_name = s->cldap.netlogon.forest;
811 s->source_dsa.dns_name = s->cldap.netlogon.pdc_dns_name;
812 s->source_dsa.netbios_name = s->cldap.netlogon.pdc_name;
813 s->source_dsa.site_name = s->cldap.netlogon.server_site;
815 s->dest_dsa.site_name = s->cldap.netlogon.client_site;
817 DEBUG(0,("CLDAP response: forest=%s dns=%s netbios=%s server_site=%s client_site=%s\n",
818 s->forest.dns_name, s->domain.dns_name, s->domain.netbios_name,
819 s->source_dsa.site_name, s->dest_dsa.site_name));
820 if (!s->dest_dsa.site_name || strcmp(s->dest_dsa.site_name, "") == 0) {
821 DEBUG(0,("Got empty client site - using server site name %s\n",
822 s->source_dsa.site_name));
823 s->dest_dsa.site_name = s->source_dsa.site_name;
826 becomeDC_connect_ldap1(s);
829 static NTSTATUS becomeDC_ldap_connect(struct libnet_BecomeDC_state *s,
830 struct becomeDC_ldap *ldap)
832 char *url;
834 url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);
835 NT_STATUS_HAVE_NO_MEMORY(url);
837 ldap->ldb = ldb_wrap_connect(s, s->libnet->event_ctx, s->libnet->lp_ctx, url,
838 NULL,
839 s->libnet->cred,
841 talloc_free(url);
842 if (ldap->ldb == NULL) {
843 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
846 return NT_STATUS_OK;
849 static NTSTATUS becomeDC_ldap1_rootdse(struct libnet_BecomeDC_state *s)
851 int ret;
852 struct ldb_result *r;
853 struct ldb_dn *basedn;
854 static const char *attrs[] = {
855 "*",
856 NULL
859 basedn = ldb_dn_new(s, s->ldap1.ldb, NULL);
860 NT_STATUS_HAVE_NO_MEMORY(basedn);
862 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
863 "(objectClass=*)");
864 talloc_free(basedn);
865 if (ret != LDB_SUCCESS) {
866 return NT_STATUS_LDAP(ret);
867 } else if (r->count != 1) {
868 talloc_free(r);
869 return NT_STATUS_INVALID_NETWORK_RESPONSE;
872 s->ldap1.rootdse = r->msgs[0];
874 s->domain.dn_str = ldb_msg_find_attr_as_string(s->ldap1.rootdse, "defaultNamingContext", NULL);
875 if (!s->domain.dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
877 s->forest.root_dn_str = ldb_msg_find_attr_as_string(s->ldap1.rootdse, "rootDomainNamingContext", NULL);
878 if (!s->forest.root_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
879 s->forest.config_dn_str = ldb_msg_find_attr_as_string(s->ldap1.rootdse, "configurationNamingContext", NULL);
880 if (!s->forest.config_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
881 s->forest.schema_dn_str = ldb_msg_find_attr_as_string(s->ldap1.rootdse, "schemaNamingContext", NULL);
882 if (!s->forest.schema_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
884 s->source_dsa.server_dn_str = ldb_msg_find_attr_as_string(s->ldap1.rootdse, "serverName", NULL);
885 if (!s->source_dsa.server_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
886 s->source_dsa.ntds_dn_str = ldb_msg_find_attr_as_string(s->ldap1.rootdse, "dsServiceName", NULL);
887 if (!s->source_dsa.ntds_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
889 return NT_STATUS_OK;
892 static NTSTATUS becomeDC_ldap1_crossref_behavior_version(struct libnet_BecomeDC_state *s)
894 int ret;
895 struct ldb_result *r;
896 struct ldb_dn *basedn;
897 static const char *attrs[] = {
898 "msDs-Behavior-Version",
899 NULL
902 basedn = ldb_dn_new(s, s->ldap1.ldb, s->forest.config_dn_str);
903 NT_STATUS_HAVE_NO_MEMORY(basedn);
905 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_ONELEVEL, attrs,
906 "(cn=Partitions)");
907 talloc_free(basedn);
908 if (ret != LDB_SUCCESS) {
909 return NT_STATUS_LDAP(ret);
910 } else if (r->count != 1) {
911 talloc_free(r);
912 return NT_STATUS_INVALID_NETWORK_RESPONSE;
915 s->forest.crossref_behavior_version = ldb_msg_find_attr_as_uint(r->msgs[0], "msDs-Behavior-Version", 0);
916 if (s->forest.crossref_behavior_version >
917 get_dc_function_level(s->libnet->lp_ctx)) {
918 talloc_free(r);
919 DEBUG(0,("The servers function level %u is above 'ads:dc function level' of %u\n",
920 s->forest.crossref_behavior_version,
921 get_dc_function_level(s->libnet->lp_ctx)));
922 return NT_STATUS_NOT_SUPPORTED;
925 talloc_free(r);
926 return NT_STATUS_OK;
929 static NTSTATUS becomeDC_ldap1_domain_behavior_version(struct libnet_BecomeDC_state *s)
931 int ret;
932 struct ldb_result *r;
933 struct ldb_dn *basedn;
934 static const char *attrs[] = {
935 "msDs-Behavior-Version",
936 NULL
939 basedn = ldb_dn_new(s, s->ldap1.ldb, s->domain.dn_str);
940 NT_STATUS_HAVE_NO_MEMORY(basedn);
942 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
943 "(objectClass=*)");
944 talloc_free(basedn);
945 if (ret != LDB_SUCCESS) {
946 return NT_STATUS_LDAP(ret);
947 } else if (r->count != 1) {
948 talloc_free(r);
949 return NT_STATUS_INVALID_NETWORK_RESPONSE;
952 s->domain.behavior_version = ldb_msg_find_attr_as_uint(r->msgs[0], "msDs-Behavior-Version", 0);
953 if (s->domain.behavior_version >
954 get_dc_function_level(s->libnet->lp_ctx)) {
955 talloc_free(r);
956 DEBUG(0,("The servers function level %u is above 'ads:dc function level' of %u\n",
957 s->forest.crossref_behavior_version,
958 get_dc_function_level(s->libnet->lp_ctx)));
959 return NT_STATUS_NOT_SUPPORTED;
962 talloc_free(r);
963 return NT_STATUS_OK;
966 static NTSTATUS becomeDC_ldap1_schema_object_version(struct libnet_BecomeDC_state *s)
968 int ret;
969 struct ldb_result *r;
970 struct ldb_dn *basedn;
971 static const char *attrs[] = {
972 "objectVersion",
973 NULL
976 basedn = ldb_dn_new(s, s->ldap1.ldb, s->forest.schema_dn_str);
977 NT_STATUS_HAVE_NO_MEMORY(basedn);
979 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
980 "(objectClass=*)");
981 talloc_free(basedn);
982 if (ret != LDB_SUCCESS) {
983 return NT_STATUS_LDAP(ret);
984 } else if (r->count != 1) {
985 talloc_free(r);
986 return NT_STATUS_INVALID_NETWORK_RESPONSE;
989 s->forest.schema_object_version = ldb_msg_find_attr_as_uint(r->msgs[0], "objectVersion", 0);
991 talloc_free(r);
992 return NT_STATUS_OK;
995 static NTSTATUS becomeDC_ldap1_w2k3_update_revision(struct libnet_BecomeDC_state *s)
997 int ret;
998 struct ldb_result *r;
999 struct ldb_dn *basedn;
1000 static const char *attrs[] = {
1001 "revision",
1002 NULL
1005 basedn = ldb_dn_new_fmt(s, s->ldap1.ldb, "CN=Windows2003Update,CN=DomainUpdates,CN=System,%s",
1006 s->domain.dn_str);
1007 NT_STATUS_HAVE_NO_MEMORY(basedn);
1009 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
1010 "(objectClass=*)");
1011 talloc_free(basedn);
1012 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1013 /* w2k doesn't have this object */
1014 s->domain.w2k3_update_revision = 0;
1015 return NT_STATUS_OK;
1016 } else if (ret != LDB_SUCCESS) {
1017 return NT_STATUS_LDAP(ret);
1018 } else if (r->count != 1) {
1019 talloc_free(r);
1020 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1023 s->domain.w2k3_update_revision = ldb_msg_find_attr_as_uint(r->msgs[0], "revision", 0);
1025 talloc_free(r);
1026 return NT_STATUS_OK;
1029 static NTSTATUS becomeDC_ldap1_infrastructure_fsmo(struct libnet_BecomeDC_state *s)
1031 int ret;
1032 struct ldb_result *r;
1033 struct ldb_dn *basedn;
1034 struct ldb_dn *ntds_dn;
1035 struct ldb_dn *server_dn;
1036 static const char *dns_attrs[] = {
1037 "dnsHostName",
1038 NULL
1040 static const char *guid_attrs[] = {
1041 "objectGUID",
1042 NULL
1045 ret = dsdb_wellknown_dn(s->ldap1.ldb, s,
1046 ldb_get_default_basedn(s->ldap1.ldb),
1047 DS_GUID_INFRASTRUCTURE_CONTAINER,
1048 &basedn);
1049 if (ret != LDB_SUCCESS) {
1050 DEBUG(0,("Failed to get well known DN for DS_GUID_INFRASTRUCTURE_CONTAINER on %s: %s\n",
1051 ldb_dn_get_linearized(ldb_get_default_basedn(s->ldap1.ldb)),
1052 ldb_errstring(s->ldap1.ldb)));
1053 return NT_STATUS_LDAP(ret);
1056 ret = samdb_reference_dn(s->ldap1.ldb, s, basedn, "fSMORoleOwner", &ntds_dn);
1057 if (ret != LDB_SUCCESS) {
1058 DEBUG(0,("Failed to get reference DN from fsmoRoleOwner on %s: %s\n",
1059 ldb_dn_get_linearized(basedn),
1060 ldb_errstring(s->ldap1.ldb)));
1061 talloc_free(basedn);
1062 return NT_STATUS_LDAP(ret);
1065 s->infrastructure_fsmo.ntds_dn_str = ldb_dn_get_linearized(ntds_dn);
1066 NT_STATUS_HAVE_NO_MEMORY(s->infrastructure_fsmo.ntds_dn_str);
1068 server_dn = ldb_dn_get_parent(s, ntds_dn);
1069 NT_STATUS_HAVE_NO_MEMORY(server_dn);
1071 s->infrastructure_fsmo.server_dn_str = ldb_dn_alloc_linearized(s, server_dn);
1072 NT_STATUS_HAVE_NO_MEMORY(s->infrastructure_fsmo.server_dn_str);
1074 ret = ldb_search(s->ldap1.ldb, s, &r, server_dn, LDB_SCOPE_BASE,
1075 dns_attrs, "(objectClass=*)");
1076 if (ret != LDB_SUCCESS) {
1077 DEBUG(0,("Failed to get server DN %s: %s\n",
1078 ldb_dn_get_linearized(server_dn),
1079 ldb_errstring(s->ldap1.ldb)));
1080 return NT_STATUS_LDAP(ret);
1081 } else if (r->count != 1) {
1082 talloc_free(r);
1083 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1086 s->infrastructure_fsmo.dns_name = ldb_msg_find_attr_as_string(r->msgs[0], "dnsHostName", NULL);
1087 if (!s->infrastructure_fsmo.dns_name) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1088 talloc_steal(s, s->infrastructure_fsmo.dns_name);
1090 talloc_free(r);
1092 ldb_dn_remove_extended_components(ntds_dn);
1093 ret = ldb_search(s->ldap1.ldb, s, &r, ntds_dn, LDB_SCOPE_BASE,
1094 guid_attrs, "(objectClass=*)");
1095 if (ret != LDB_SUCCESS) {
1096 DEBUG(0,("Failed to get NTDS Settings DN %s: %s\n",
1097 ldb_dn_get_linearized(ntds_dn),
1098 ldb_errstring(s->ldap1.ldb)));
1099 return NT_STATUS_LDAP(ret);
1100 } else if (r->count != 1) {
1101 talloc_free(r);
1102 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1105 s->infrastructure_fsmo.ntds_guid = samdb_result_guid(r->msgs[0], "objectGUID");
1107 talloc_free(r);
1109 return NT_STATUS_OK;
1112 static NTSTATUS becomeDC_ldap1_rid_manager_fsmo(struct libnet_BecomeDC_state *s)
1114 int ret;
1115 struct ldb_result *r;
1116 struct ldb_dn *basedn;
1117 const char *reference_dn_str;
1118 struct ldb_dn *ntds_dn;
1119 struct ldb_dn *server_dn;
1120 static const char *rid_attrs[] = {
1121 "rIDManagerReference",
1122 NULL
1124 static const char *fsmo_attrs[] = {
1125 "fSMORoleOwner",
1126 NULL
1128 static const char *dns_attrs[] = {
1129 "dnsHostName",
1130 NULL
1132 static const char *guid_attrs[] = {
1133 "objectGUID",
1134 NULL
1137 basedn = ldb_dn_new(s, s->ldap1.ldb, s->domain.dn_str);
1138 NT_STATUS_HAVE_NO_MEMORY(basedn);
1140 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1141 rid_attrs, "(objectClass=*)");
1142 talloc_free(basedn);
1143 if (ret != LDB_SUCCESS) {
1144 return NT_STATUS_LDAP(ret);
1145 } else if (r->count != 1) {
1146 talloc_free(r);
1147 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1150 reference_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "rIDManagerReference", NULL);
1151 if (!reference_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1153 basedn = ldb_dn_new(s, s->ldap1.ldb, reference_dn_str);
1154 NT_STATUS_HAVE_NO_MEMORY(basedn);
1156 talloc_free(r);
1158 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1159 fsmo_attrs, "(objectClass=*)");
1160 talloc_free(basedn);
1161 if (ret != LDB_SUCCESS) {
1162 return NT_STATUS_LDAP(ret);
1163 } else if (r->count != 1) {
1164 talloc_free(r);
1165 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1168 s->rid_manager_fsmo.ntds_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "fSMORoleOwner", NULL);
1169 if (!s->rid_manager_fsmo.ntds_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1170 talloc_steal(s, s->rid_manager_fsmo.ntds_dn_str);
1172 talloc_free(r);
1174 ntds_dn = ldb_dn_new(s, s->ldap1.ldb, s->rid_manager_fsmo.ntds_dn_str);
1175 NT_STATUS_HAVE_NO_MEMORY(ntds_dn);
1177 server_dn = ldb_dn_get_parent(s, ntds_dn);
1178 NT_STATUS_HAVE_NO_MEMORY(server_dn);
1180 s->rid_manager_fsmo.server_dn_str = ldb_dn_alloc_linearized(s, server_dn);
1181 NT_STATUS_HAVE_NO_MEMORY(s->rid_manager_fsmo.server_dn_str);
1183 ret = ldb_search(s->ldap1.ldb, s, &r, server_dn, LDB_SCOPE_BASE,
1184 dns_attrs, "(objectClass=*)");
1185 if (ret != LDB_SUCCESS) {
1186 return NT_STATUS_LDAP(ret);
1187 } else if (r->count != 1) {
1188 talloc_free(r);
1189 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1192 s->rid_manager_fsmo.dns_name = ldb_msg_find_attr_as_string(r->msgs[0], "dnsHostName", NULL);
1193 if (!s->rid_manager_fsmo.dns_name) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1194 talloc_steal(s, s->rid_manager_fsmo.dns_name);
1196 talloc_free(r);
1198 ret = ldb_search(s->ldap1.ldb, s, &r, ntds_dn, LDB_SCOPE_BASE,
1199 guid_attrs, "(objectClass=*)");
1200 if (ret != LDB_SUCCESS) {
1201 return NT_STATUS_LDAP(ret);
1202 } else if (r->count != 1) {
1203 talloc_free(r);
1204 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1207 s->rid_manager_fsmo.ntds_guid = samdb_result_guid(r->msgs[0], "objectGUID");
1209 talloc_free(r);
1211 return NT_STATUS_OK;
1214 static NTSTATUS becomeDC_ldap1_site_object(struct libnet_BecomeDC_state *s)
1216 int ret;
1217 struct ldb_result *r;
1218 struct ldb_dn *basedn;
1220 basedn = ldb_dn_new_fmt(s, s->ldap1.ldb, "CN=%s,CN=Sites,%s",
1221 s->dest_dsa.site_name,
1222 s->forest.config_dn_str);
1223 NT_STATUS_HAVE_NO_MEMORY(basedn);
1225 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1226 NULL, "(objectClass=*)");
1227 talloc_free(basedn);
1228 if (ret != LDB_SUCCESS) {
1229 return NT_STATUS_LDAP(ret);
1230 } else if (r->count != 1) {
1231 talloc_free(r);
1232 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1235 s->dest_dsa.site_guid = samdb_result_guid(r->msgs[0], "objectGUID");
1237 talloc_free(r);
1238 return NT_STATUS_OK;
1241 static NTSTATUS becomeDC_check_options(struct libnet_BecomeDC_state *s)
1243 if (!s->callbacks.check_options) return NT_STATUS_OK;
1245 s->_co.domain = &s->domain;
1246 s->_co.forest = &s->forest;
1247 s->_co.source_dsa = &s->source_dsa;
1249 return s->callbacks.check_options(s->callbacks.private_data, &s->_co);
1252 static NTSTATUS becomeDC_ldap1_computer_object(struct libnet_BecomeDC_state *s)
1254 int ret;
1255 struct ldb_result *r;
1256 struct ldb_dn *basedn;
1257 static const char *attrs[] = {
1258 "distinguishedName",
1259 "userAccountControl",
1260 NULL
1263 basedn = ldb_dn_new(s, s->ldap1.ldb, s->domain.dn_str);
1264 NT_STATUS_HAVE_NO_MEMORY(basedn);
1266 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_SUBTREE, attrs,
1267 "(&(|(objectClass=user)(objectClass=computer))(sAMAccountName=%s$))",
1268 s->dest_dsa.netbios_name);
1269 talloc_free(basedn);
1270 if (ret != LDB_SUCCESS) {
1271 return NT_STATUS_LDAP(ret);
1272 } else if (r->count != 1) {
1273 talloc_free(r);
1274 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1277 s->dest_dsa.computer_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "distinguishedName", NULL);
1278 if (!s->dest_dsa.computer_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1279 talloc_steal(s, s->dest_dsa.computer_dn_str);
1281 s->dest_dsa.user_account_control = ldb_msg_find_attr_as_uint(r->msgs[0], "userAccountControl", 0);
1283 talloc_free(r);
1284 return NT_STATUS_OK;
1287 static NTSTATUS becomeDC_ldap1_server_object_1(struct libnet_BecomeDC_state *s)
1289 int ret;
1290 struct ldb_result *r;
1291 struct ldb_dn *basedn;
1292 const char *server_reference_dn_str;
1293 struct ldb_dn *server_reference_dn;
1294 struct ldb_dn *computer_dn;
1296 basedn = ldb_dn_new_fmt(s, s->ldap1.ldb, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
1297 s->dest_dsa.netbios_name,
1298 s->dest_dsa.site_name,
1299 s->forest.config_dn_str);
1300 NT_STATUS_HAVE_NO_MEMORY(basedn);
1302 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1303 NULL, "(objectClass=*)");
1304 talloc_free(basedn);
1305 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1306 /* if the object doesn't exist, we'll create it later */
1307 return NT_STATUS_OK;
1308 } else if (ret != LDB_SUCCESS) {
1309 return NT_STATUS_LDAP(ret);
1310 } else if (r->count != 1) {
1311 talloc_free(r);
1312 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1315 server_reference_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "serverReference", NULL);
1316 if (server_reference_dn_str) {
1317 server_reference_dn = ldb_dn_new(r, s->ldap1.ldb, server_reference_dn_str);
1318 NT_STATUS_HAVE_NO_MEMORY(server_reference_dn);
1320 computer_dn = ldb_dn_new(r, s->ldap1.ldb, s->dest_dsa.computer_dn_str);
1321 NT_STATUS_HAVE_NO_MEMORY(computer_dn);
1324 * if the server object belongs to another DC in another domain
1325 * in the forest, we should not touch this object!
1327 if (ldb_dn_compare(computer_dn, server_reference_dn) != 0) {
1328 talloc_free(r);
1329 return NT_STATUS_OBJECT_NAME_COLLISION;
1333 /* if the server object is already for the dest_dsa, then we don't need to create it */
1334 s->dest_dsa.server_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "distinguishedName", NULL);
1335 if (!s->dest_dsa.server_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1336 talloc_steal(s, s->dest_dsa.server_dn_str);
1338 talloc_free(r);
1339 return NT_STATUS_OK;
1342 static NTSTATUS becomeDC_ldap1_server_object_2(struct libnet_BecomeDC_state *s)
1344 int ret;
1345 struct ldb_result *r;
1346 struct ldb_dn *basedn;
1347 const char *server_reference_bl_dn_str;
1348 static const char *attrs[] = {
1349 "serverReferenceBL",
1350 NULL
1353 /* if the server_dn_str has a valid value, we skip this lookup */
1354 if (s->dest_dsa.server_dn_str) return NT_STATUS_OK;
1356 basedn = ldb_dn_new(s, s->ldap1.ldb, s->dest_dsa.computer_dn_str);
1357 NT_STATUS_HAVE_NO_MEMORY(basedn);
1359 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1360 attrs, "(objectClass=*)");
1361 talloc_free(basedn);
1362 if (ret != LDB_SUCCESS) {
1363 return NT_STATUS_LDAP(ret);
1364 } else if (r->count != 1) {
1365 talloc_free(r);
1366 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1369 server_reference_bl_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "serverReferenceBL", NULL);
1370 if (!server_reference_bl_dn_str) {
1371 /* if no back link is present, we're done for this function */
1372 talloc_free(r);
1373 return NT_STATUS_OK;
1376 /* if the server object is already for the dest_dsa, then we don't need to create it */
1377 s->dest_dsa.server_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "serverReferenceBL", NULL);
1378 if (s->dest_dsa.server_dn_str) {
1379 /* if a back link is present, we know that the server object is present */
1380 talloc_steal(s, s->dest_dsa.server_dn_str);
1383 talloc_free(r);
1384 return NT_STATUS_OK;
1387 static NTSTATUS becomeDC_ldap1_server_object_add(struct libnet_BecomeDC_state *s)
1389 int ret;
1390 struct ldb_message *msg;
1391 char *server_dn_str;
1393 /* if the server_dn_str has a valid value, we skip this lookup */
1394 if (s->dest_dsa.server_dn_str) return NT_STATUS_OK;
1396 msg = ldb_msg_new(s);
1397 NT_STATUS_HAVE_NO_MEMORY(msg);
1399 msg->dn = ldb_dn_new_fmt(msg, s->ldap1.ldb, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
1400 s->dest_dsa.netbios_name,
1401 s->dest_dsa.site_name,
1402 s->forest.config_dn_str);
1403 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
1405 ret = ldb_msg_add_string(msg, "objectClass", "server");
1406 if (ret != 0) {
1407 talloc_free(msg);
1408 return NT_STATUS_NO_MEMORY;
1410 ret = ldb_msg_add_string(msg, "systemFlags", "50000000");
1411 if (ret != 0) {
1412 talloc_free(msg);
1413 return NT_STATUS_NO_MEMORY;
1415 ret = ldb_msg_add_string(msg, "serverReference", s->dest_dsa.computer_dn_str);
1416 if (ret != 0) {
1417 talloc_free(msg);
1418 return NT_STATUS_NO_MEMORY;
1421 server_dn_str = ldb_dn_alloc_linearized(s, msg->dn);
1422 NT_STATUS_HAVE_NO_MEMORY(server_dn_str);
1424 ret = ldb_add(s->ldap1.ldb, msg);
1425 talloc_free(msg);
1426 if (ret != LDB_SUCCESS) {
1427 talloc_free(server_dn_str);
1428 return NT_STATUS_LDAP(ret);
1431 s->dest_dsa.server_dn_str = server_dn_str;
1433 return NT_STATUS_OK;
1436 static NTSTATUS becomeDC_ldap1_server_object_modify(struct libnet_BecomeDC_state *s)
1438 int ret;
1439 struct ldb_message *msg;
1440 unsigned int i;
1442 /* make a 'modify' msg, and only for serverReference */
1443 msg = ldb_msg_new(s);
1444 NT_STATUS_HAVE_NO_MEMORY(msg);
1445 msg->dn = ldb_dn_new(msg, s->ldap1.ldb, s->dest_dsa.server_dn_str);
1446 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
1448 ret = ldb_msg_add_string(msg, "serverReference", s->dest_dsa.computer_dn_str);
1449 if (ret != 0) {
1450 talloc_free(msg);
1451 return NT_STATUS_NO_MEMORY;
1454 /* mark all the message elements (should be just one)
1455 as LDB_FLAG_MOD_ADD */
1456 for (i=0;i<msg->num_elements;i++) {
1457 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
1460 ret = ldb_modify(s->ldap1.ldb, msg);
1461 if (ret == LDB_SUCCESS) {
1462 talloc_free(msg);
1463 return NT_STATUS_OK;
1464 } else if (ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
1465 /* retry with LDB_FLAG_MOD_REPLACE */
1466 } else {
1467 talloc_free(msg);
1468 return NT_STATUS_LDAP(ret);
1471 /* mark all the message elements (should be just one)
1472 as LDB_FLAG_MOD_REPLACE */
1473 for (i=0;i<msg->num_elements;i++) {
1474 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1477 ret = ldb_modify(s->ldap1.ldb, msg);
1478 talloc_free(msg);
1479 if (ret != LDB_SUCCESS) {
1480 return NT_STATUS_LDAP(ret);
1483 return NT_STATUS_OK;
1486 static void becomeDC_drsuapi_connect_send(struct libnet_BecomeDC_state *s,
1487 struct becomeDC_drsuapi *drsuapi,
1488 void (*recv_fn)(struct composite_context *req));
1489 static void becomeDC_drsuapi1_connect_recv(struct composite_context *req);
1490 static void becomeDC_connect_ldap2(struct libnet_BecomeDC_state *s);
1492 static void becomeDC_connect_ldap1(struct libnet_BecomeDC_state *s)
1494 struct composite_context *c = s->creq;
1496 c->status = becomeDC_ldap_connect(s, &s->ldap1);
1497 if (!composite_is_ok(c)) return;
1499 c->status = becomeDC_ldap1_rootdse(s);
1500 if (!composite_is_ok(c)) return;
1502 c->status = becomeDC_ldap1_crossref_behavior_version(s);
1503 if (!composite_is_ok(c)) return;
1505 c->status = becomeDC_ldap1_domain_behavior_version(s);
1506 if (!composite_is_ok(c)) return;
1508 c->status = becomeDC_ldap1_schema_object_version(s);
1509 if (!composite_is_ok(c)) return;
1511 c->status = becomeDC_ldap1_w2k3_update_revision(s);
1512 if (!composite_is_ok(c)) return;
1514 c->status = becomeDC_ldap1_infrastructure_fsmo(s);
1515 if (!composite_is_ok(c)) return;
1517 c->status = becomeDC_ldap1_rid_manager_fsmo(s);
1518 if (!composite_is_ok(c)) return;
1520 c->status = becomeDC_ldap1_site_object(s);
1521 if (!composite_is_ok(c)) return;
1523 c->status = becomeDC_check_options(s);
1524 if (!composite_is_ok(c)) return;
1526 c->status = becomeDC_ldap1_computer_object(s);
1527 if (!composite_is_ok(c)) return;
1529 c->status = becomeDC_ldap1_server_object_1(s);
1530 if (!composite_is_ok(c)) return;
1532 c->status = becomeDC_ldap1_server_object_2(s);
1533 if (!composite_is_ok(c)) return;
1535 c->status = becomeDC_ldap1_server_object_add(s);
1536 if (!composite_is_ok(c)) return;
1538 c->status = becomeDC_ldap1_server_object_modify(s);
1539 if (!composite_is_ok(c)) return;
1541 becomeDC_drsuapi_connect_send(s, &s->drsuapi1, becomeDC_drsuapi1_connect_recv);
1544 static void becomeDC_drsuapi_connect_send(struct libnet_BecomeDC_state *s,
1545 struct becomeDC_drsuapi *drsuapi,
1546 void (*recv_fn)(struct composite_context *req))
1548 struct composite_context *c = s->creq;
1549 struct composite_context *creq;
1550 char *binding_str;
1552 drsuapi->s = s;
1554 if (!drsuapi->binding) {
1555 const char *krb5_str = "";
1556 const char *print_str = "";
1558 * Note: Replication only works with Windows 2000 when 'krb5' is
1559 * passed as auth_type here. If NTLMSSP is used, Windows
1560 * 2000 returns garbage in the DsGetNCChanges() response
1561 * if encrypted password attributes would be in the
1562 * response. That means the replication of the schema and
1563 * configuration partition works fine, but it fails for
1564 * the domain partition.
1566 if (lpcfg_parm_bool(s->libnet->lp_ctx, NULL, "become_dc",
1567 "force krb5", true))
1569 krb5_str = "krb5,";
1571 if (lpcfg_parm_bool(s->libnet->lp_ctx, NULL, "become_dc",
1572 "print", false))
1574 print_str = "print,";
1576 binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[%s%sseal]",
1577 s->source_dsa.dns_name,
1578 krb5_str, print_str);
1579 if (composite_nomem(binding_str, c)) return;
1580 c->status = dcerpc_parse_binding(s, binding_str, &drsuapi->binding);
1581 talloc_free(binding_str);
1582 if (!composite_is_ok(c)) return;
1585 if (DEBUGLEVEL >= 10) {
1586 drsuapi->binding->flags |= DCERPC_DEBUG_PRINT_BOTH;
1589 creq = dcerpc_pipe_connect_b_send(s, drsuapi->binding, &ndr_table_drsuapi,
1590 s->libnet->cred, s->libnet->event_ctx,
1591 s->libnet->lp_ctx);
1592 composite_continue(c, creq, recv_fn, s);
1595 static void becomeDC_drsuapi_bind_send(struct libnet_BecomeDC_state *s,
1596 struct becomeDC_drsuapi *drsuapi,
1597 void (*recv_fn)(struct tevent_req *subreq));
1598 static void becomeDC_drsuapi1_bind_recv(struct tevent_req *subreq);
1600 static void becomeDC_drsuapi1_connect_recv(struct composite_context *req)
1602 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
1603 struct libnet_BecomeDC_state);
1604 struct composite_context *c = s->creq;
1606 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi1.pipe);
1607 if (!composite_is_ok(c)) return;
1609 s->drsuapi1.drsuapi_handle = s->drsuapi1.pipe->binding_handle;
1611 c->status = gensec_session_key(s->drsuapi1.pipe->conn->security_state.generic_state,
1613 &s->drsuapi1.gensec_skey);
1614 if (!composite_is_ok(c)) return;
1616 becomeDC_drsuapi_bind_send(s, &s->drsuapi1, becomeDC_drsuapi1_bind_recv);
1619 static void becomeDC_drsuapi_bind_send(struct libnet_BecomeDC_state *s,
1620 struct becomeDC_drsuapi *drsuapi,
1621 void (*recv_fn)(struct tevent_req *subreq))
1623 struct composite_context *c = s->creq;
1624 struct drsuapi_DsBindInfo28 *bind_info28;
1625 struct tevent_req *subreq;
1627 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &drsuapi->bind_guid);
1629 bind_info28 = &drsuapi->local_info28;
1630 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
1631 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
1632 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
1633 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
1634 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
1635 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
1636 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
1637 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
1638 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
1639 if (s->domain.behavior_version >= DS_DOMAIN_FUNCTION_2003) {
1640 /* TODO: find out how this is really triggered! */
1641 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
1643 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
1644 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
1645 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
1646 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
1647 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
1648 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
1649 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
1650 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
1651 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
1652 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
1653 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
1654 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
1655 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
1656 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
1657 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
1658 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
1659 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
1660 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
1661 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
1662 #if 0 /* we don't support XPRESS compression yet */
1663 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
1664 #endif
1665 bind_info28->site_guid = s->dest_dsa.site_guid;
1666 bind_info28->pid = 0;
1667 bind_info28->repl_epoch = 0;
1669 drsuapi->bind_info_ctr.length = 28;
1670 drsuapi->bind_info_ctr.info.info28 = *bind_info28;
1672 drsuapi->bind_r.in.bind_guid = &drsuapi->bind_guid;
1673 drsuapi->bind_r.in.bind_info = &drsuapi->bind_info_ctr;
1674 drsuapi->bind_r.out.bind_handle = &drsuapi->bind_handle;
1676 subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
1677 drsuapi->drsuapi_handle,
1678 &drsuapi->bind_r);
1679 if (composite_nomem(subreq, c)) return;
1680 tevent_req_set_callback(subreq, recv_fn, s);
1683 static WERROR becomeDC_drsuapi_bind_recv(struct libnet_BecomeDC_state *s,
1684 struct becomeDC_drsuapi *drsuapi)
1686 if (!W_ERROR_IS_OK(drsuapi->bind_r.out.result)) {
1687 return drsuapi->bind_r.out.result;
1690 ZERO_STRUCT(drsuapi->remote_info28);
1691 if (drsuapi->bind_r.out.bind_info) {
1692 switch (drsuapi->bind_r.out.bind_info->length) {
1693 case 24: {
1694 struct drsuapi_DsBindInfo24 *info24;
1695 info24 = &drsuapi->bind_r.out.bind_info->info.info24;
1696 drsuapi->remote_info28.supported_extensions = info24->supported_extensions;
1697 drsuapi->remote_info28.site_guid = info24->site_guid;
1698 drsuapi->remote_info28.pid = info24->pid;
1699 drsuapi->remote_info28.repl_epoch = 0;
1700 break;
1702 case 48: {
1703 struct drsuapi_DsBindInfo48 *info48;
1704 info48 = &drsuapi->bind_r.out.bind_info->info.info48;
1705 drsuapi->remote_info28.supported_extensions = info48->supported_extensions;
1706 drsuapi->remote_info28.site_guid = info48->site_guid;
1707 drsuapi->remote_info28.pid = info48->pid;
1708 drsuapi->remote_info28.repl_epoch = info48->repl_epoch;
1709 break;
1711 case 28:
1712 drsuapi->remote_info28 = drsuapi->bind_r.out.bind_info->info.info28;
1713 break;
1717 return WERR_OK;
1720 static void becomeDC_drsuapi1_add_entry_send(struct libnet_BecomeDC_state *s);
1722 static void becomeDC_drsuapi1_bind_recv(struct tevent_req *subreq)
1724 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
1725 struct libnet_BecomeDC_state);
1726 struct composite_context *c = s->creq;
1727 WERROR status;
1729 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
1730 TALLOC_FREE(subreq);
1731 if (!composite_is_ok(c)) return;
1733 status = becomeDC_drsuapi_bind_recv(s, &s->drsuapi1);
1734 if (!W_ERROR_IS_OK(status)) {
1735 composite_error(c, werror_to_ntstatus(status));
1736 return;
1739 becomeDC_drsuapi1_add_entry_send(s);
1742 static void becomeDC_drsuapi1_add_entry_recv(struct tevent_req *subreq);
1744 static void becomeDC_drsuapi1_add_entry_send(struct libnet_BecomeDC_state *s)
1746 struct composite_context *c = s->creq;
1747 struct drsuapi_DsAddEntry *r;
1748 struct drsuapi_DsReplicaObjectIdentifier *identifier;
1749 uint32_t num_attrs, i = 0;
1750 struct drsuapi_DsReplicaAttribute *attrs;
1751 enum ndr_err_code ndr_err;
1752 bool w2k3;
1753 struct tevent_req *subreq;
1755 /* choose a random invocationId */
1756 s->dest_dsa.invocation_id = GUID_random();
1759 * if the schema version indicates w2k3, then also send some w2k3
1760 * specific attributes.
1762 if (s->forest.schema_object_version >= 30) {
1763 w2k3 = true;
1764 } else {
1765 w2k3 = false;
1768 r = talloc_zero(s, struct drsuapi_DsAddEntry);
1769 if (composite_nomem(r, c)) return;
1771 /* setup identifier */
1772 identifier = talloc(r, struct drsuapi_DsReplicaObjectIdentifier);
1773 if (composite_nomem(identifier, c)) return;
1774 identifier->guid = GUID_zero();
1775 identifier->sid = s->zero_sid;
1776 identifier->dn = talloc_asprintf(identifier, "CN=NTDS Settings,%s",
1777 s->dest_dsa.server_dn_str);
1778 if (composite_nomem(identifier->dn, c)) return;
1780 /* allocate attribute array */
1781 num_attrs = 12;
1782 attrs = talloc_array(r, struct drsuapi_DsReplicaAttribute, num_attrs);
1783 if (composite_nomem(attrs, c)) return;
1785 /* ntSecurityDescriptor */
1787 struct drsuapi_DsAttributeValue *vs;
1788 DATA_BLOB *vd;
1789 struct security_descriptor *v;
1790 struct dom_sid *domain_admins_sid;
1791 const char *domain_admins_sid_str;
1793 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1794 if (composite_nomem(vs, c)) return;
1796 vd = talloc_array(vs, DATA_BLOB, 1);
1797 if (composite_nomem(vd, c)) return;
1799 domain_admins_sid = dom_sid_add_rid(vs, s->domain.sid, DOMAIN_RID_ADMINS);
1800 if (composite_nomem(domain_admins_sid, c)) return;
1802 domain_admins_sid_str = dom_sid_string(domain_admins_sid, domain_admins_sid);
1803 if (composite_nomem(domain_admins_sid_str, c)) return;
1805 v = security_descriptor_dacl_create(vd,
1807 /* owner: domain admins */
1808 domain_admins_sid_str,
1809 /* owner group: domain admins */
1810 domain_admins_sid_str,
1811 /* authenticated users */
1812 SID_NT_AUTHENTICATED_USERS,
1813 SEC_ACE_TYPE_ACCESS_ALLOWED,
1814 SEC_STD_READ_CONTROL |
1815 SEC_ADS_LIST |
1816 SEC_ADS_READ_PROP |
1817 SEC_ADS_LIST_OBJECT,
1819 /* domain admins */
1820 domain_admins_sid_str,
1821 SEC_ACE_TYPE_ACCESS_ALLOWED,
1822 SEC_STD_REQUIRED |
1823 SEC_ADS_CREATE_CHILD |
1824 SEC_ADS_LIST |
1825 SEC_ADS_SELF_WRITE |
1826 SEC_ADS_READ_PROP |
1827 SEC_ADS_WRITE_PROP |
1828 SEC_ADS_DELETE_TREE |
1829 SEC_ADS_LIST_OBJECT |
1830 SEC_ADS_CONTROL_ACCESS,
1832 /* system */
1833 SID_NT_SYSTEM,
1834 SEC_ACE_TYPE_ACCESS_ALLOWED,
1835 SEC_STD_REQUIRED |
1836 SEC_ADS_CREATE_CHILD |
1837 SEC_ADS_DELETE_CHILD |
1838 SEC_ADS_LIST |
1839 SEC_ADS_SELF_WRITE |
1840 SEC_ADS_READ_PROP |
1841 SEC_ADS_WRITE_PROP |
1842 SEC_ADS_DELETE_TREE |
1843 SEC_ADS_LIST_OBJECT |
1844 SEC_ADS_CONTROL_ACCESS,
1846 /* end */
1847 NULL);
1848 if (composite_nomem(v, c)) return;
1850 ndr_err = ndr_push_struct_blob(&vd[0], vd, v,
1851 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
1852 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1853 c->status = ndr_map_error2ntstatus(ndr_err);
1854 if (!composite_is_ok(c)) return;
1857 vs[0].blob = &vd[0];
1859 attrs[i].attid = DRSUAPI_ATTID_ntSecurityDescriptor;
1860 attrs[i].value_ctr.num_values = 1;
1861 attrs[i].value_ctr.values = vs;
1863 i++;
1866 /* objectClass: nTDSDSA */
1868 struct drsuapi_DsAttributeValue *vs;
1869 DATA_BLOB *vd;
1871 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1872 if (composite_nomem(vs, c)) return;
1874 vd = talloc_array(vs, DATA_BLOB, 1);
1875 if (composite_nomem(vd, c)) return;
1877 vd[0] = data_blob_talloc(vd, NULL, 4);
1878 if (composite_nomem(vd[0].data, c)) return;
1880 /* value for nTDSDSA */
1881 SIVAL(vd[0].data, 0, 0x0017002F);
1883 vs[0].blob = &vd[0];
1885 attrs[i].attid = DRSUAPI_ATTID_objectClass;
1886 attrs[i].value_ctr.num_values = 1;
1887 attrs[i].value_ctr.values = vs;
1889 i++;
1892 /* objectCategory: CN=NTDS-DSA,CN=Schema,... or CN=NTDS-DSA-RO,CN=Schema,... */
1894 struct drsuapi_DsAttributeValue *vs;
1895 DATA_BLOB *vd;
1896 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
1898 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1899 if (composite_nomem(vs, c)) return;
1901 vd = talloc_array(vs, DATA_BLOB, 1);
1902 if (composite_nomem(vd, c)) return;
1904 v[0].guid = GUID_zero();
1905 v[0].sid = s->zero_sid;
1907 if (s->rodc_join) {
1908 v[0].dn = talloc_asprintf(vd, "CN=NTDS-DSA-RO,%s",
1909 s->forest.schema_dn_str);
1910 } else {
1911 v[0].dn = talloc_asprintf(vd, "CN=NTDS-DSA,%s",
1912 s->forest.schema_dn_str);
1914 if (composite_nomem(v[0].dn, c)) return;
1916 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
1917 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1918 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1919 c->status = ndr_map_error2ntstatus(ndr_err);
1920 if (!composite_is_ok(c)) return;
1923 vs[0].blob = &vd[0];
1925 attrs[i].attid = DRSUAPI_ATTID_objectCategory;
1926 attrs[i].value_ctr.num_values = 1;
1927 attrs[i].value_ctr.values = vs;
1929 i++;
1932 /* invocationId: random guid */
1934 struct drsuapi_DsAttributeValue *vs;
1935 DATA_BLOB *vd;
1936 const struct GUID *v;
1938 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1939 if (composite_nomem(vs, c)) return;
1941 vd = talloc_array(vs, DATA_BLOB, 1);
1942 if (composite_nomem(vd, c)) return;
1944 v = &s->dest_dsa.invocation_id;
1946 c->status = GUID_to_ndr_blob(v, vd, &vd[0]);
1947 if (!composite_is_ok(c)) return;
1949 vs[0].blob = &vd[0];
1951 attrs[i].attid = DRSUAPI_ATTID_invocationId;
1952 attrs[i].value_ctr.num_values = 1;
1953 attrs[i].value_ctr.values = vs;
1955 i++;
1958 /* hasMasterNCs: ... */
1960 struct drsuapi_DsAttributeValue *vs;
1961 DATA_BLOB *vd;
1962 struct drsuapi_DsReplicaObjectIdentifier3 v[3];
1964 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 3);
1965 if (composite_nomem(vs, c)) return;
1967 vd = talloc_array(vs, DATA_BLOB, 3);
1968 if (composite_nomem(vd, c)) return;
1970 v[0].guid = GUID_zero();
1971 v[0].sid = s->zero_sid;
1972 v[0].dn = s->forest.config_dn_str;
1974 v[1].guid = GUID_zero();
1975 v[1].sid = s->zero_sid;
1976 v[1].dn = s->domain.dn_str;
1978 v[2].guid = GUID_zero();
1979 v[2].sid = s->zero_sid;
1980 v[2].dn = s->forest.schema_dn_str;
1982 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
1983 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1984 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1985 c->status = ndr_map_error2ntstatus(ndr_err);
1986 if (!composite_is_ok(c)) return;
1989 ndr_err = ndr_push_struct_blob(&vd[1], vd, &v[1],
1990 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1991 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1992 c->status = ndr_map_error2ntstatus(ndr_err);
1993 if (!composite_is_ok(c)) return;
1996 ndr_err = ndr_push_struct_blob(&vd[2], vd, &v[2],
1997 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1998 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1999 c->status = ndr_map_error2ntstatus(ndr_err);
2000 if (!composite_is_ok(c)) return;
2003 vs[0].blob = &vd[0];
2004 vs[1].blob = &vd[1];
2005 vs[2].blob = &vd[2];
2007 attrs[i].attid = DRSUAPI_ATTID_hasMasterNCs;
2008 attrs[i].value_ctr.num_values = 3;
2009 attrs[i].value_ctr.values = vs;
2011 i++;
2014 /* msDS-hasMasterNCs: ... */
2015 if (w2k3) {
2016 struct drsuapi_DsAttributeValue *vs;
2017 DATA_BLOB *vd;
2018 struct drsuapi_DsReplicaObjectIdentifier3 v[3];
2020 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 3);
2021 if (composite_nomem(vs, c)) return;
2023 vd = talloc_array(vs, DATA_BLOB, 3);
2024 if (composite_nomem(vd, c)) return;
2026 v[0].guid = GUID_zero();
2027 v[0].sid = s->zero_sid;
2028 v[0].dn = s->forest.config_dn_str;
2030 v[1].guid = GUID_zero();
2031 v[1].sid = s->zero_sid;
2032 v[1].dn = s->domain.dn_str;
2034 v[2].guid = GUID_zero();
2035 v[2].sid = s->zero_sid;
2036 v[2].dn = s->forest.schema_dn_str;
2038 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2039 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2040 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2041 c->status = ndr_map_error2ntstatus(ndr_err);
2042 if (!composite_is_ok(c)) return;
2045 ndr_err = ndr_push_struct_blob(&vd[1], vd, &v[1],
2046 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2047 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2048 c->status = ndr_map_error2ntstatus(ndr_err);
2049 if (!composite_is_ok(c)) return;
2052 ndr_err = ndr_push_struct_blob(&vd[2], vd, &v[2],
2053 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2054 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2055 c->status = ndr_map_error2ntstatus(ndr_err);
2056 if (!composite_is_ok(c)) return;
2059 vs[0].blob = &vd[0];
2060 vs[1].blob = &vd[1];
2061 vs[2].blob = &vd[2];
2063 attrs[i].attid = DRSUAPI_ATTID_msDS_hasMasterNCs;
2064 attrs[i].value_ctr.num_values = 3;
2065 attrs[i].value_ctr.values = vs;
2067 i++;
2070 /* dMDLocation: CN=Schema,... */
2072 struct drsuapi_DsAttributeValue *vs;
2073 DATA_BLOB *vd;
2074 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2076 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2077 if (composite_nomem(vs, c)) return;
2079 vd = talloc_array(vs, DATA_BLOB, 1);
2080 if (composite_nomem(vd, c)) return;
2082 v[0].guid = GUID_zero();
2083 v[0].sid = s->zero_sid;
2084 v[0].dn = s->forest.schema_dn_str;
2086 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2087 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2088 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2089 c->status = ndr_map_error2ntstatus(ndr_err);
2090 if (!composite_is_ok(c)) return;
2093 vs[0].blob = &vd[0];
2095 attrs[i].attid = DRSUAPI_ATTID_dMDLocation;
2096 attrs[i].value_ctr.num_values = 1;
2097 attrs[i].value_ctr.values = vs;
2099 i++;
2102 /* msDS-HasDomainNCs: <domain_partition> */
2103 if (w2k3) {
2104 struct drsuapi_DsAttributeValue *vs;
2105 DATA_BLOB *vd;
2106 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2108 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2109 if (composite_nomem(vs, c)) return;
2111 vd = talloc_array(vs, DATA_BLOB, 1);
2112 if (composite_nomem(vd, c)) return;
2114 v[0].guid = GUID_zero();
2115 v[0].sid = s->zero_sid;
2116 v[0].dn = s->domain.dn_str;
2118 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2119 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2120 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2121 c->status = ndr_map_error2ntstatus(ndr_err);
2122 if (!composite_is_ok(c)) return;
2125 vs[0].blob = &vd[0];
2127 attrs[i].attid = DRSUAPI_ATTID_msDS_HasDomainNCs;
2128 attrs[i].value_ctr.num_values = 1;
2129 attrs[i].value_ctr.values = vs;
2131 i++;
2134 /* msDS-Behavior-Version */
2135 if (w2k3) {
2136 struct drsuapi_DsAttributeValue *vs;
2137 DATA_BLOB *vd;
2139 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2140 if (composite_nomem(vs, c)) return;
2142 vd = talloc_array(vs, DATA_BLOB, 1);
2143 if (composite_nomem(vd, c)) return;
2145 vd[0] = data_blob_talloc(vd, NULL, 4);
2146 if (composite_nomem(vd[0].data, c)) return;
2148 SIVAL(vd[0].data, 0, get_dc_function_level(s->libnet->lp_ctx));
2150 vs[0].blob = &vd[0];
2152 attrs[i].attid = DRSUAPI_ATTID_msDS_Behavior_Version;
2153 attrs[i].value_ctr.num_values = 1;
2154 attrs[i].value_ctr.values = vs;
2156 i++;
2159 /* systemFlags */
2161 struct drsuapi_DsAttributeValue *vs;
2162 DATA_BLOB *vd;
2164 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2165 if (composite_nomem(vs, c)) return;
2167 vd = talloc_array(vs, DATA_BLOB, 1);
2168 if (composite_nomem(vd, c)) return;
2170 vd[0] = data_blob_talloc(vd, NULL, 4);
2171 if (composite_nomem(vd[0].data, c)) return;
2173 if (s->rodc_join) {
2174 SIVAL(vd[0].data, 0, SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
2175 } else {
2176 SIVAL(vd[0].data, 0, SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
2179 vs[0].blob = &vd[0];
2181 attrs[i].attid = DRSUAPI_ATTID_systemFlags;
2182 attrs[i].value_ctr.num_values = 1;
2183 attrs[i].value_ctr.values = vs;
2185 i++;
2188 /* serverReference: ... */
2190 struct drsuapi_DsAttributeValue *vs;
2191 DATA_BLOB *vd;
2192 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2194 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2195 if (composite_nomem(vs, c)) return;
2197 vd = talloc_array(vs, DATA_BLOB, 1);
2198 if (composite_nomem(vd, c)) return;
2200 v[0].guid = GUID_zero();
2201 v[0].sid = s->zero_sid;
2202 v[0].dn = s->dest_dsa.computer_dn_str;
2204 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2205 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2206 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2207 c->status = ndr_map_error2ntstatus(ndr_err);
2208 if (!composite_is_ok(c)) return;
2211 vs[0].blob = &vd[0];
2213 attrs[i].attid = DRSUAPI_ATTID_serverReference;
2214 attrs[i].value_ctr.num_values = 1;
2215 attrs[i].value_ctr.values = vs;
2217 i++;
2220 /* options:... */
2221 if (s->rodc_join) {
2222 struct drsuapi_DsAttributeValue *vs;
2223 DATA_BLOB *vd;
2225 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2226 if (composite_nomem(vs, c)) return;
2228 vd = talloc_array(vs, DATA_BLOB, 1);
2229 if (composite_nomem(vd, c)) return;
2231 vd[0] = data_blob_talloc(vd, NULL, 4);
2232 if (composite_nomem(vd[0].data, c)) return;
2234 SIVAL(vd[0].data, 0, DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL);
2236 vs[0].blob = &vd[0];
2238 attrs[i].attid = DRSUAPI_ATTID_options;
2239 attrs[i].value_ctr.num_values = 1;
2240 attrs[i].value_ctr.values = vs;
2242 i++;
2245 /* truncate the attribute list to the attribute count we have filled in */
2246 num_attrs = i;
2248 /* setup request structure */
2249 r->in.bind_handle = &s->drsuapi1.bind_handle;
2250 r->in.level = 2;
2251 r->in.req = talloc(s, union drsuapi_DsAddEntryRequest);
2252 r->in.req->req2.first_object.next_object = NULL;
2253 r->in.req->req2.first_object.object.identifier = identifier;
2254 r->in.req->req2.first_object.object.flags = 0x00000000;
2255 r->in.req->req2.first_object.object.attribute_ctr.num_attributes= num_attrs;
2256 r->in.req->req2.first_object.object.attribute_ctr.attributes = attrs;
2258 r->out.level_out = talloc(s, uint32_t);
2259 r->out.ctr = talloc(s, union drsuapi_DsAddEntryCtr);
2261 s->ndr_struct_ptr = r;
2262 subreq = dcerpc_drsuapi_DsAddEntry_r_send(s, c->event_ctx,
2263 s->drsuapi1.drsuapi_handle, r);
2264 if (composite_nomem(subreq, c)) return;
2265 tevent_req_set_callback(subreq, becomeDC_drsuapi1_add_entry_recv, s);
2268 static void becomeDC_drsuapi2_connect_recv(struct composite_context *req);
2269 static NTSTATUS becomeDC_prepare_db(struct libnet_BecomeDC_state *s);
2271 static void becomeDC_drsuapi1_add_entry_recv(struct tevent_req *subreq)
2273 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2274 struct libnet_BecomeDC_state);
2275 struct composite_context *c = s->creq;
2276 struct drsuapi_DsAddEntry *r = talloc_get_type_abort(s->ndr_struct_ptr,
2277 struct drsuapi_DsAddEntry);
2278 char *binding_str;
2280 s->ndr_struct_ptr = NULL;
2282 c->status = dcerpc_drsuapi_DsAddEntry_r_recv(subreq, r);
2283 TALLOC_FREE(subreq);
2284 if (!composite_is_ok(c)) return;
2286 if (!W_ERROR_IS_OK(r->out.result)) {
2287 composite_error(c, werror_to_ntstatus(r->out.result));
2288 return;
2291 if (*r->out.level_out == 3) {
2292 WERROR status;
2293 union drsuapi_DsAddEntry_ErrData *err_data = r->out.ctr->ctr3.err_data;
2295 /* check for errors */
2296 status = err_data ? err_data->v1.status : WERR_OK;
2297 if (!W_ERROR_IS_OK(status)) {
2298 struct drsuapi_DsAddEntryErrorInfo_Attr_V1 *attr_err;
2299 struct drsuapi_DsAddEntry_AttrErrListItem_V1 *attr_err_li;
2300 struct drsuapi_DsAddEntryErrorInfo_Name_V1 *name_err;
2301 struct drsuapi_DsAddEntryErrorInfo_Referr_V1 *ref_err;
2302 struct drsuapi_DsAddEntry_RefErrListItem_V1 *ref_li;
2304 if (r->out.ctr->ctr3.err_ver != 1) {
2305 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2306 return;
2309 DEBUG(0,("DsAddEntry (R3) of '%s' failed: "
2310 "Errors: dir_err = %d, status = %s;\n",
2311 r->in.req->req3.first_object.object.identifier->dn,
2312 err_data->v1.dir_err,
2313 win_errstr(err_data->v1.status)));
2315 if (!err_data->v1.info) {
2316 DEBUG(0, ("DsAddEntry (R3): no error info returned!\n"));
2317 composite_error(c, werror_to_ntstatus(status));
2318 return;
2321 /* dump more detailed error */
2322 switch (err_data->v1.dir_err) {
2323 case DRSUAPI_DIRERR_ATTRIBUTE:
2324 /* Dump attribute errors */
2325 attr_err = &err_data->v1.info->attr_err;
2326 DEBUGADD(0,(" Attribute Error: object = %s, count = %d;\n",
2327 attr_err->id->dn,
2328 attr_err->count));
2329 attr_err_li = &attr_err->first;
2330 for (; attr_err_li; attr_err_li = attr_err_li->next) {
2331 struct drsuapi_DsAddEntry_AttrErr_V1 *err = &attr_err_li->err_data;
2332 DEBUGADD(0,(" Error: err = %s, problem = 0x%08X, attid = 0x%08X;\n",
2333 win_errstr(err->extended_err),
2334 err->problem,
2335 err->attid));
2336 /* TODO: should we print attribute value here? */
2338 break;
2339 case DRSUAPI_DIRERR_NAME:
2340 /* Dump Name resolution error */
2341 name_err = &err_data->v1.info->name_err;
2342 DEBUGADD(0,(" Name Error: err = %s, problem = 0x%08X, id_matched = %s;\n",
2343 win_errstr(name_err->extended_err),
2344 name_err->problem,
2345 name_err->id_matched->dn));
2346 break;
2347 case DRSUAPI_DIRERR_REFERRAL:
2348 /* Dump Referral errors */
2349 ref_err = &err_data->v1.info->referral_err;
2350 DEBUGADD(0,(" Referral Error: extended_err = %s\n",
2351 win_errstr(ref_err->extended_err)));
2352 ref_li = &ref_err->refer;
2353 for (; ref_li; ref_li = ref_li->next) {
2354 struct drsuapi_DsaAddressListItem_V1 *addr;
2355 DEBUGADD(0,(" Referral: id_target = %s, ref_type = 0x%04X,",
2356 ref_li->id_target->dn,
2357 ref_li->ref_type));
2358 if (ref_li->is_choice_set) {
2359 DEBUGADD(0,(" choice = 0x%02X, ",
2360 ref_li->choice));
2362 DEBUGADD(0,(" add_list ("));
2363 for (addr = ref_li->addr_list; addr; addr = addr->next) {
2364 DEBUGADD(0,("%s", addr->address->string));
2365 if (addr->next) {
2366 DEBUGADD(0,(", "));
2369 DEBUGADD(0,(");\n"));
2371 break;
2372 case DRSUAPI_DIRERR_SECURITY:
2373 /* Dump Security error. */
2374 DEBUGADD(0,(" Security Error: extended_err = %s, problem = 0x%08X\n",
2375 win_errstr(err_data->v1.info->security_err.extended_err),
2376 err_data->v1.info->security_err.problem));
2377 break;
2378 case DRSUAPI_DIRERR_SERVICE:
2379 /* Dump Service error. */
2380 DEBUGADD(0,(" Service Error: extended_err = %s, problem = 0x%08X\n",
2381 win_errstr(err_data->v1.info->service_err.extended_err),
2382 err_data->v1.info->service_err.problem));
2383 break;
2384 case DRSUAPI_DIRERR_UPDATE:
2385 /* Dump Update error. */
2386 DEBUGADD(0,(" Update Error: extended_err = %s, problem = 0x%08X\n",
2387 win_errstr(err_data->v1.info->update_err.extended_err),
2388 err_data->v1.info->update_err.problem));
2389 break;
2390 case DRSUAPI_DIRERR_SYSTEM:
2391 /* System error. */
2392 DEBUGADD(0,(" System Error: extended_err = %s, problem = 0x%08X\n",
2393 win_errstr(err_data->v1.info->system_err.extended_err),
2394 err_data->v1.info->system_err.problem));
2395 break;
2396 case DRSUAPI_DIRERR_OK: /* mute compiler warnings */
2397 default:
2398 DEBUGADD(0,(" Unknown DIRERR error class returned!\n"));
2399 break;
2402 composite_error(c, werror_to_ntstatus(status));
2403 return;
2406 if (1 != r->out.ctr->ctr3.count) {
2407 DEBUG(0,("DsAddEntry - Ctr3: something very wrong had happened - "
2408 "method succeeded but objects returned are %d (expected 1).\n",
2409 r->out.ctr->ctr3.count));
2410 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2411 return;
2414 s->dest_dsa.ntds_guid = r->out.ctr->ctr3.objects[0].guid;
2416 } else if (*r->out.level_out == 2) {
2417 if (DRSUAPI_DIRERR_OK != r->out.ctr->ctr2.dir_err) {
2418 DEBUG(0,("DsAddEntry failed with: dir_err = %d, extended_err = %s\n",
2419 r->out.ctr->ctr2.dir_err,
2420 win_errstr(r->out.ctr->ctr2.extended_err)));
2421 composite_error(c, werror_to_ntstatus(r->out.ctr->ctr2.extended_err));
2422 return;
2425 if (1 != r->out.ctr->ctr2.count) {
2426 DEBUG(0,("DsAddEntry: something very wrong had happened - "
2427 "method succeeded but objects returned are %d (expected 1). "
2428 "Errors: dir_err = %d, extended_err = %s\n",
2429 r->out.ctr->ctr2.count,
2430 r->out.ctr->ctr2.dir_err,
2431 win_errstr(r->out.ctr->ctr2.extended_err)));
2432 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2433 return;
2436 s->dest_dsa.ntds_guid = r->out.ctr->ctr2.objects[0].guid;
2437 } else {
2438 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2439 return;
2442 talloc_free(r);
2444 s->dest_dsa.ntds_dn_str = talloc_asprintf(s, "CN=NTDS Settings,%s",
2445 s->dest_dsa.server_dn_str);
2446 if (composite_nomem(s->dest_dsa.ntds_dn_str, c)) return;
2448 c->status = becomeDC_prepare_db(s);
2449 if (!composite_is_ok(c)) return;
2451 /* this avoids the epmapper lookup on the 2nd connection */
2452 binding_str = dcerpc_binding_string(s, s->drsuapi1.binding);
2453 if (composite_nomem(binding_str, c)) return;
2455 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi2.binding);
2456 talloc_free(binding_str);
2457 if (!composite_is_ok(c)) return;
2459 if (DEBUGLEVEL >= 10) {
2460 s->drsuapi2.binding->flags |= DCERPC_DEBUG_PRINT_BOTH;
2463 /* w2k3 uses the same assoc_group_id as on the first connection, so we do */
2464 s->drsuapi2.binding->assoc_group_id = s->drsuapi1.pipe->assoc_group_id;
2466 becomeDC_drsuapi_connect_send(s, &s->drsuapi2, becomeDC_drsuapi2_connect_recv);
2469 static NTSTATUS becomeDC_prepare_db(struct libnet_BecomeDC_state *s)
2471 if (!s->callbacks.prepare_db) return NT_STATUS_OK;
2473 s->_pp.domain = &s->domain;
2474 s->_pp.forest = &s->forest;
2475 s->_pp.source_dsa = &s->source_dsa;
2476 s->_pp.dest_dsa = &s->dest_dsa;
2478 return s->callbacks.prepare_db(s->callbacks.private_data, &s->_pp);
2481 static void becomeDC_drsuapi2_bind_recv(struct tevent_req *subreq);
2483 static void becomeDC_drsuapi2_connect_recv(struct composite_context *req)
2485 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
2486 struct libnet_BecomeDC_state);
2487 struct composite_context *c = s->creq;
2489 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi2.pipe);
2490 if (!composite_is_ok(c)) return;
2492 s->drsuapi2.drsuapi_handle = s->drsuapi2.pipe->binding_handle;
2494 c->status = gensec_session_key(s->drsuapi2.pipe->conn->security_state.generic_state,
2496 &s->drsuapi2.gensec_skey);
2497 if (!composite_is_ok(c)) return;
2499 becomeDC_drsuapi_bind_send(s, &s->drsuapi2, becomeDC_drsuapi2_bind_recv);
2502 static void becomeDC_drsuapi3_connect_recv(struct composite_context *req);
2504 static void becomeDC_drsuapi2_bind_recv(struct tevent_req *subreq)
2506 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2507 struct libnet_BecomeDC_state);
2508 struct composite_context *c = s->creq;
2509 char *binding_str;
2510 WERROR status;
2512 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
2513 TALLOC_FREE(subreq);
2514 if (!composite_is_ok(c)) return;
2516 status = becomeDC_drsuapi_bind_recv(s, &s->drsuapi2);
2517 if (!W_ERROR_IS_OK(status)) {
2518 composite_error(c, werror_to_ntstatus(status));
2519 return;
2522 /* this avoids the epmapper lookup on the 3rd connection */
2523 binding_str = dcerpc_binding_string(s, s->drsuapi1.binding);
2524 if (composite_nomem(binding_str, c)) return;
2526 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi3.binding);
2527 talloc_free(binding_str);
2528 if (!composite_is_ok(c)) return;
2530 if (DEBUGLEVEL >= 10) {
2531 s->drsuapi3.binding->flags |= DCERPC_DEBUG_PRINT_BOTH;
2534 /* w2k3 uses the same assoc_group_id as on the first connection, so we do */
2535 s->drsuapi3.binding->assoc_group_id = s->drsuapi1.pipe->assoc_group_id;
2536 /* w2k3 uses the concurrent multiplex feature on the 3rd connection, so we do */
2537 s->drsuapi3.binding->flags |= DCERPC_CONCURRENT_MULTIPLEX;
2539 becomeDC_drsuapi_connect_send(s, &s->drsuapi3, becomeDC_drsuapi3_connect_recv);
2542 static void becomeDC_drsuapi3_pull_schema_send(struct libnet_BecomeDC_state *s);
2544 static void becomeDC_drsuapi3_connect_recv(struct composite_context *req)
2546 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
2547 struct libnet_BecomeDC_state);
2548 struct composite_context *c = s->creq;
2550 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi3.pipe);
2551 if (!composite_is_ok(c)) return;
2553 s->drsuapi3.drsuapi_handle = s->drsuapi3.pipe->binding_handle;
2555 c->status = gensec_session_key(s->drsuapi3.pipe->conn->security_state.generic_state,
2557 &s->drsuapi3.gensec_skey);
2558 if (!composite_is_ok(c)) return;
2560 becomeDC_drsuapi3_pull_schema_send(s);
2563 static void becomeDC_drsuapi_pull_partition_send(struct libnet_BecomeDC_state *s,
2564 struct becomeDC_drsuapi *drsuapi_h,
2565 struct becomeDC_drsuapi *drsuapi_p,
2566 struct libnet_BecomeDC_Partition *partition,
2567 void (*recv_fn)(struct tevent_req *subreq))
2569 struct composite_context *c = s->creq;
2570 struct drsuapi_DsGetNCChanges *r;
2571 struct tevent_req *subreq;
2573 r = talloc(s, struct drsuapi_DsGetNCChanges);
2574 if (composite_nomem(r, c)) return;
2576 r->out.level_out = talloc(r, uint32_t);
2577 if (composite_nomem(r->out.level_out, c)) return;
2578 r->in.req = talloc(r, union drsuapi_DsGetNCChangesRequest);
2579 if (composite_nomem(r->in.req, c)) return;
2580 r->out.ctr = talloc(r, union drsuapi_DsGetNCChangesCtr);
2581 if (composite_nomem(r->out.ctr, c)) return;
2583 r->in.bind_handle = &drsuapi_h->bind_handle;
2584 if (drsuapi_h->remote_info28.supported_extensions & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8) {
2585 r->in.level = 8;
2586 r->in.req->req8.destination_dsa_guid = partition->destination_dsa_guid;
2587 r->in.req->req8.source_dsa_invocation_id= partition->source_dsa_invocation_id;
2588 r->in.req->req8.naming_context = &partition->nc;
2589 r->in.req->req8.highwatermark = partition->highwatermark;
2590 r->in.req->req8.uptodateness_vector = NULL;
2591 r->in.req->req8.replica_flags = partition->replica_flags;
2592 r->in.req->req8.max_object_count = 133;
2593 r->in.req->req8.max_ndr_size = 1336811;
2594 r->in.req->req8.extended_op = DRSUAPI_EXOP_NONE;
2595 r->in.req->req8.fsmo_info = 0;
2596 r->in.req->req8.partial_attribute_set = NULL;
2597 r->in.req->req8.partial_attribute_set_ex= NULL;
2598 r->in.req->req8.mapping_ctr.num_mappings= 0;
2599 r->in.req->req8.mapping_ctr.mappings = NULL;
2600 } else {
2601 r->in.level = 5;
2602 r->in.req->req5.destination_dsa_guid = partition->destination_dsa_guid;
2603 r->in.req->req5.source_dsa_invocation_id= partition->source_dsa_invocation_id;
2604 r->in.req->req5.naming_context = &partition->nc;
2605 r->in.req->req5.highwatermark = partition->highwatermark;
2606 r->in.req->req5.uptodateness_vector = NULL;
2607 r->in.req->req5.replica_flags = partition->replica_flags;
2608 r->in.req->req5.max_object_count = 133;
2609 r->in.req->req5.max_ndr_size = 1336770;
2610 r->in.req->req5.extended_op = DRSUAPI_EXOP_NONE;
2611 r->in.req->req5.fsmo_info = 0;
2615 * we should try to use the drsuapi_p->pipe here, as w2k3 does
2616 * but it seems that some extra flags in the DCERPC Bind call
2617 * are needed for it. Or the same KRB5 TGS is needed on both
2618 * connections.
2620 s->ndr_struct_ptr = r;
2621 subreq = dcerpc_drsuapi_DsGetNCChanges_r_send(s, c->event_ctx,
2622 drsuapi_p->drsuapi_handle,
2624 if (composite_nomem(subreq, c)) return;
2625 tevent_req_set_callback(subreq, recv_fn, s);
2628 static WERROR becomeDC_drsuapi_pull_partition_recv(struct libnet_BecomeDC_state *s,
2629 struct becomeDC_drsuapi *drsuapi_h,
2630 struct becomeDC_drsuapi *drsuapi_p,
2631 struct libnet_BecomeDC_Partition *partition,
2632 struct drsuapi_DsGetNCChanges *r)
2634 uint32_t req_level = 0;
2635 struct drsuapi_DsGetNCChangesRequest5 *req5 = NULL;
2636 struct drsuapi_DsGetNCChangesRequest8 *req8 = NULL;
2637 struct drsuapi_DsGetNCChangesRequest10 *req10 = NULL;
2638 uint32_t ctr_level = 0;
2639 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
2640 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
2641 struct GUID *source_dsa_guid = NULL;
2642 struct GUID *source_dsa_invocation_id = NULL;
2643 struct drsuapi_DsReplicaHighWaterMark *new_highwatermark = NULL;
2644 bool more_data = false;
2645 NTSTATUS nt_status;
2647 if (!W_ERROR_IS_OK(r->out.result)) {
2648 return r->out.result;
2651 switch (r->in.level) {
2652 case 0:
2653 /* none */
2654 break;
2655 case 5:
2656 req5 = &r->in.req->req5;
2657 break;
2658 case 8:
2659 req8 = &r->in.req->req8;
2660 break;
2661 case 10:
2662 req10 = &r->in.req->req10;
2663 break;
2664 default:
2665 return WERR_INVALID_PARAMETER;
2668 if (*r->out.level_out == 1) {
2669 ctr_level = 1;
2670 ctr1 = &r->out.ctr->ctr1;
2671 } else if (*r->out.level_out == 2 &&
2672 r->out.ctr->ctr2.mszip1.ts) {
2673 ctr_level = 1;
2674 ctr1 = &r->out.ctr->ctr2.mszip1.ts->ctr1;
2675 } else if (*r->out.level_out == 6) {
2676 ctr_level = 6;
2677 ctr6 = &r->out.ctr->ctr6;
2678 } else if (*r->out.level_out == 7 &&
2679 r->out.ctr->ctr7.level == 6 &&
2680 r->out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP &&
2681 r->out.ctr->ctr7.ctr.mszip6.ts) {
2682 ctr_level = 6;
2683 ctr6 = &r->out.ctr->ctr7.ctr.mszip6.ts->ctr6;
2684 } else if (*r->out.level_out == 7 &&
2685 r->out.ctr->ctr7.level == 6 &&
2686 r->out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS &&
2687 r->out.ctr->ctr7.ctr.xpress6.ts) {
2688 ctr_level = 6;
2689 ctr6 = &r->out.ctr->ctr7.ctr.xpress6.ts->ctr6;
2690 } else {
2691 return WERR_BAD_NET_RESP;
2694 if (!ctr1 && ! ctr6) {
2695 return WERR_BAD_NET_RESP;
2698 if (ctr_level == 6) {
2699 if (!W_ERROR_IS_OK(ctr6->drs_error)) {
2700 return ctr6->drs_error;
2704 switch (ctr_level) {
2705 case 1:
2706 source_dsa_guid = &ctr1->source_dsa_guid;
2707 source_dsa_invocation_id = &ctr1->source_dsa_invocation_id;
2708 new_highwatermark = &ctr1->new_highwatermark;
2709 more_data = ctr1->more_data;
2710 break;
2711 case 6:
2712 source_dsa_guid = &ctr6->source_dsa_guid;
2713 source_dsa_invocation_id = &ctr6->source_dsa_invocation_id;
2714 new_highwatermark = &ctr6->new_highwatermark;
2715 more_data = ctr6->more_data;
2716 break;
2719 partition->highwatermark = *new_highwatermark;
2720 partition->source_dsa_guid = *source_dsa_guid;
2721 partition->source_dsa_invocation_id = *source_dsa_invocation_id;
2722 partition->more_data = more_data;
2724 if (!partition->store_chunk) return WERR_OK;
2726 s->_sc.domain = &s->domain;
2727 s->_sc.forest = &s->forest;
2728 s->_sc.source_dsa = &s->source_dsa;
2729 s->_sc.dest_dsa = &s->dest_dsa;
2730 s->_sc.partition = partition;
2731 s->_sc.req_level = req_level;
2732 s->_sc.req5 = req5;
2733 s->_sc.req8 = req8;
2734 s->_sc.req10 = req10;
2735 s->_sc.ctr_level = ctr_level;
2736 s->_sc.ctr1 = ctr1;
2737 s->_sc.ctr6 = ctr6;
2739 * we need to use the drsuapi_p->gensec_skey here,
2740 * when we use drsuapi_p->pipe in the for this request
2742 s->_sc.gensec_skey = &drsuapi_p->gensec_skey;
2744 nt_status = partition->store_chunk(s->callbacks.private_data, &s->_sc);
2745 if (!NT_STATUS_IS_OK(nt_status)) {
2746 return ntstatus_to_werror(nt_status);
2749 return WERR_OK;
2752 static void becomeDC_drsuapi3_pull_schema_recv(struct tevent_req *subreq);
2754 static void becomeDC_drsuapi3_pull_schema_send(struct libnet_BecomeDC_state *s)
2756 s->schema_part.nc.guid = GUID_zero();
2757 s->schema_part.nc.sid = s->zero_sid;
2758 s->schema_part.nc.dn = s->forest.schema_dn_str;
2760 s->schema_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2762 s->schema_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2763 | DRSUAPI_DRS_INIT_SYNC
2764 | DRSUAPI_DRS_PER_SYNC
2765 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2766 | DRSUAPI_DRS_NEVER_SYNCED
2767 | DRSUAPI_DRS_USE_COMPRESSION;
2768 if (s->rodc_join) {
2769 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2772 s->schema_part.store_chunk = s->callbacks.schema_chunk;
2774 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part,
2775 becomeDC_drsuapi3_pull_schema_recv);
2778 static void becomeDC_drsuapi3_pull_config_send(struct libnet_BecomeDC_state *s);
2780 static void becomeDC_drsuapi3_pull_schema_recv(struct tevent_req *subreq)
2782 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2783 struct libnet_BecomeDC_state);
2784 struct composite_context *c = s->creq;
2785 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2786 struct drsuapi_DsGetNCChanges);
2787 WERROR status;
2789 s->ndr_struct_ptr = NULL;
2791 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2792 TALLOC_FREE(subreq);
2793 if (!composite_is_ok(c)) return;
2795 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part, r);
2796 if (!W_ERROR_IS_OK(status)) {
2797 composite_error(c, werror_to_ntstatus(status));
2798 return;
2801 talloc_free(r);
2803 if (s->schema_part.more_data) {
2804 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part,
2805 becomeDC_drsuapi3_pull_schema_recv);
2806 return;
2809 becomeDC_drsuapi3_pull_config_send(s);
2812 static void becomeDC_drsuapi3_pull_config_recv(struct tevent_req *subreq);
2814 static void becomeDC_drsuapi3_pull_config_send(struct libnet_BecomeDC_state *s)
2816 s->config_part.nc.guid = GUID_zero();
2817 s->config_part.nc.sid = s->zero_sid;
2818 s->config_part.nc.dn = s->forest.config_dn_str;
2820 s->config_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2822 s->config_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2823 | DRSUAPI_DRS_INIT_SYNC
2824 | DRSUAPI_DRS_PER_SYNC
2825 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2826 | DRSUAPI_DRS_NEVER_SYNCED
2827 | DRSUAPI_DRS_USE_COMPRESSION;
2828 if (s->rodc_join) {
2829 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2832 s->config_part.store_chunk = s->callbacks.config_chunk;
2834 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->config_part,
2835 becomeDC_drsuapi3_pull_config_recv);
2838 static void becomeDC_drsuapi3_pull_config_recv(struct tevent_req *subreq)
2840 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2841 struct libnet_BecomeDC_state);
2842 struct composite_context *c = s->creq;
2843 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2844 struct drsuapi_DsGetNCChanges);
2845 WERROR status;
2847 s->ndr_struct_ptr = NULL;
2849 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2850 TALLOC_FREE(subreq);
2851 if (!composite_is_ok(c)) return;
2853 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->config_part, r);
2854 if (!W_ERROR_IS_OK(status)) {
2855 composite_error(c, werror_to_ntstatus(status));
2856 return;
2859 talloc_free(r);
2861 if (s->config_part.more_data) {
2862 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->config_part,
2863 becomeDC_drsuapi3_pull_config_recv);
2864 return;
2867 becomeDC_connect_ldap2(s);
2870 static void becomeDC_drsuapi3_pull_domain_recv(struct tevent_req *subreq);
2872 static void becomeDC_drsuapi3_pull_domain_send(struct libnet_BecomeDC_state *s)
2874 s->domain_part.nc.guid = GUID_zero();
2875 s->domain_part.nc.sid = s->zero_sid;
2876 s->domain_part.nc.dn = s->domain.dn_str;
2878 s->domain_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2880 s->domain_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2881 | DRSUAPI_DRS_INIT_SYNC
2882 | DRSUAPI_DRS_PER_SYNC
2883 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2884 | DRSUAPI_DRS_NEVER_SYNCED
2885 | DRSUAPI_DRS_USE_COMPRESSION;
2886 if (s->critical_only) {
2887 s->domain_part.replica_flags |= DRSUAPI_DRS_CRITICAL_ONLY | DRSUAPI_DRS_GET_ANC;
2889 if (s->rodc_join) {
2890 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2893 s->domain_part.store_chunk = s->callbacks.domain_chunk;
2895 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2896 becomeDC_drsuapi3_pull_domain_recv);
2899 static void becomeDC_drsuapi_update_refs_send(struct libnet_BecomeDC_state *s,
2900 struct becomeDC_drsuapi *drsuapi,
2901 struct libnet_BecomeDC_Partition *partition,
2902 void (*recv_fn)(struct tevent_req *subreq));
2903 static void becomeDC_drsuapi2_update_refs_schema_recv(struct tevent_req *subreq);
2905 static void becomeDC_drsuapi3_pull_domain_recv(struct tevent_req *subreq)
2907 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2908 struct libnet_BecomeDC_state);
2909 struct composite_context *c = s->creq;
2910 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2911 struct drsuapi_DsGetNCChanges);
2912 WERROR status;
2914 s->ndr_struct_ptr = NULL;
2916 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2917 TALLOC_FREE(subreq);
2918 if (!composite_is_ok(c)) return;
2920 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part, r);
2921 if (!W_ERROR_IS_OK(status)) {
2922 composite_error(c, werror_to_ntstatus(status));
2923 return;
2926 talloc_free(r);
2928 if (s->domain_part.more_data) {
2929 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2930 becomeDC_drsuapi3_pull_domain_recv);
2931 return;
2934 if (s->critical_only) {
2935 /* Remove the critical and ANC */
2936 s->domain_part.replica_flags ^= DRSUAPI_DRS_CRITICAL_ONLY | DRSUAPI_DRS_GET_ANC;
2937 s->critical_only = false;
2938 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2939 becomeDC_drsuapi3_pull_domain_recv);
2940 return;
2942 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->schema_part,
2943 becomeDC_drsuapi2_update_refs_schema_recv);
2946 static void becomeDC_drsuapi_update_refs_send(struct libnet_BecomeDC_state *s,
2947 struct becomeDC_drsuapi *drsuapi,
2948 struct libnet_BecomeDC_Partition *partition,
2949 void (*recv_fn)(struct tevent_req *subreq))
2951 struct composite_context *c = s->creq;
2952 struct drsuapi_DsReplicaUpdateRefs *r;
2953 const char *ntds_guid_str;
2954 const char *ntds_dns_name;
2955 struct tevent_req *subreq;
2957 r = talloc(s, struct drsuapi_DsReplicaUpdateRefs);
2958 if (composite_nomem(r, c)) return;
2960 ntds_guid_str = GUID_string(r, &s->dest_dsa.ntds_guid);
2961 if (composite_nomem(ntds_guid_str, c)) return;
2963 ntds_dns_name = talloc_asprintf(r, "%s._msdcs.%s",
2964 ntds_guid_str,
2965 s->forest.dns_name);
2966 if (composite_nomem(ntds_dns_name, c)) return;
2968 r->in.bind_handle = &drsuapi->bind_handle;
2969 r->in.level = 1;
2970 r->in.req.req1.naming_context = &partition->nc;
2971 r->in.req.req1.dest_dsa_dns_name= ntds_dns_name;
2972 r->in.req.req1.dest_dsa_guid = s->dest_dsa.ntds_guid;
2973 r->in.req.req1.options = DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_DEL_REF;
2975 /* I think this is how we mark ourselves as a RODC */
2976 if (!lpcfg_parm_bool(s->libnet->lp_ctx, NULL, "repl", "RODC", false)) {
2977 r->in.req.req1.options |= DRSUAPI_DRS_WRIT_REP;
2980 s->ndr_struct_ptr = r;
2981 subreq = dcerpc_drsuapi_DsReplicaUpdateRefs_r_send(s, c->event_ctx,
2982 drsuapi->drsuapi_handle,
2984 if (composite_nomem(subreq, c)) return;
2985 tevent_req_set_callback(subreq, recv_fn, s);
2988 static void becomeDC_drsuapi2_update_refs_config_recv(struct tevent_req *subreq);
2990 static void becomeDC_drsuapi2_update_refs_schema_recv(struct tevent_req *subreq)
2992 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2993 struct libnet_BecomeDC_state);
2994 struct composite_context *c = s->creq;
2995 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type_abort(s->ndr_struct_ptr,
2996 struct drsuapi_DsReplicaUpdateRefs);
2998 s->ndr_struct_ptr = NULL;
3000 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3001 TALLOC_FREE(subreq);
3002 if (!composite_is_ok(c)) return;
3004 if (!W_ERROR_IS_OK(r->out.result)) {
3005 composite_error(c, werror_to_ntstatus(r->out.result));
3006 return;
3009 talloc_free(r);
3011 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->config_part,
3012 becomeDC_drsuapi2_update_refs_config_recv);
3015 static void becomeDC_drsuapi2_update_refs_domain_recv(struct tevent_req *subreq);
3017 static void becomeDC_drsuapi2_update_refs_config_recv(struct tevent_req *subreq)
3019 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
3020 struct libnet_BecomeDC_state);
3021 struct composite_context *c = s->creq;
3022 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type(s->ndr_struct_ptr,
3023 struct drsuapi_DsReplicaUpdateRefs);
3025 s->ndr_struct_ptr = NULL;
3027 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3028 TALLOC_FREE(subreq);
3029 if (!composite_is_ok(c)) return;
3031 if (!W_ERROR_IS_OK(r->out.result)) {
3032 composite_error(c, werror_to_ntstatus(r->out.result));
3033 return;
3036 talloc_free(r);
3038 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->domain_part,
3039 becomeDC_drsuapi2_update_refs_domain_recv);
3042 static void becomeDC_drsuapi2_update_refs_domain_recv(struct tevent_req *subreq)
3044 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
3045 struct libnet_BecomeDC_state);
3046 struct composite_context *c = s->creq;
3047 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type(s->ndr_struct_ptr,
3048 struct drsuapi_DsReplicaUpdateRefs);
3050 s->ndr_struct_ptr = NULL;
3052 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3053 TALLOC_FREE(subreq);
3054 if (!composite_is_ok(c)) return;
3056 if (!W_ERROR_IS_OK(r->out.result)) {
3057 composite_error(c, werror_to_ntstatus(r->out.result));
3058 return;
3061 talloc_free(r);
3063 /* TODO: use DDNS updates and register dns names */
3064 composite_done(c);
3067 static NTSTATUS becomeDC_ldap2_modify_computer(struct libnet_BecomeDC_state *s)
3069 int ret;
3070 struct ldb_message *msg;
3071 unsigned int i;
3072 uint32_t user_account_control = UF_SERVER_TRUST_ACCOUNT |
3073 UF_TRUSTED_FOR_DELEGATION;
3075 /* as the value is already as we want it to be, we're done */
3076 if (s->dest_dsa.user_account_control == user_account_control) {
3077 return NT_STATUS_OK;
3080 /* make a 'modify' msg, and only for serverReference */
3081 msg = ldb_msg_new(s);
3082 NT_STATUS_HAVE_NO_MEMORY(msg);
3083 msg->dn = ldb_dn_new(msg, s->ldap2.ldb, s->dest_dsa.computer_dn_str);
3084 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
3086 ret = samdb_msg_add_uint(s->ldap2.ldb, msg, msg, "userAccountControl",
3087 user_account_control);
3088 if (ret != LDB_SUCCESS) {
3089 talloc_free(msg);
3090 return NT_STATUS_NO_MEMORY;
3093 /* mark all the message elements (should be just one)
3094 as LDB_FLAG_MOD_REPLACE */
3095 for (i=0;i<msg->num_elements;i++) {
3096 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3099 ret = ldb_modify(s->ldap2.ldb, msg);
3100 talloc_free(msg);
3101 if (ret != LDB_SUCCESS) {
3102 return NT_STATUS_LDAP(ret);
3105 s->dest_dsa.user_account_control = user_account_control;
3107 return NT_STATUS_OK;
3110 static NTSTATUS becomeDC_ldap2_move_computer(struct libnet_BecomeDC_state *s)
3112 int ret;
3113 struct ldb_dn *old_dn;
3114 struct ldb_dn *new_dn;
3116 ret = dsdb_wellknown_dn(s->ldap2.ldb, s,
3117 ldb_get_default_basedn(s->ldap2.ldb),
3118 DS_GUID_DOMAIN_CONTROLLERS_CONTAINER,
3119 &new_dn);
3120 if (ret != LDB_SUCCESS) {
3121 return NT_STATUS_LDAP(ret);
3124 if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) {
3125 talloc_free(new_dn);
3126 return NT_STATUS_NO_MEMORY;
3129 old_dn = ldb_dn_new(new_dn, s->ldap2.ldb, s->dest_dsa.computer_dn_str);
3130 NT_STATUS_HAVE_NO_MEMORY(old_dn);
3132 if (ldb_dn_compare(old_dn, new_dn) == 0) {
3133 /* we don't need to rename if the old and new dn match */
3134 talloc_free(new_dn);
3135 return NT_STATUS_OK;
3138 ret = ldb_rename(s->ldap2.ldb, old_dn, new_dn);
3139 if (ret != LDB_SUCCESS) {
3140 talloc_free(new_dn);
3141 return NT_STATUS_LDAP(ret);
3144 s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn);
3145 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str);
3147 talloc_free(new_dn);
3149 return NT_STATUS_OK;
3152 static void becomeDC_connect_ldap2(struct libnet_BecomeDC_state *s)
3154 struct composite_context *c = s->creq;
3156 c->status = becomeDC_ldap_connect(s, &s->ldap2);
3157 if (!composite_is_ok(c)) return;
3159 c->status = becomeDC_ldap2_modify_computer(s);
3160 if (!composite_is_ok(c)) return;
3162 c->status = becomeDC_ldap2_move_computer(s);
3163 if (!composite_is_ok(c)) return;
3165 s->critical_only = true;
3166 becomeDC_drsuapi3_pull_domain_send(s);
3169 struct composite_context *libnet_BecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3171 struct composite_context *c;
3172 struct libnet_BecomeDC_state *s;
3173 char *tmp_name;
3175 c = composite_create(mem_ctx, ctx->event_ctx);
3176 if (c == NULL) return NULL;
3178 s = talloc_zero(c, struct libnet_BecomeDC_state);
3179 if (composite_nomem(s, c)) return c;
3180 c->private_data = s;
3181 s->creq = c;
3182 s->libnet = ctx;
3184 /* Domain input */
3185 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
3186 if (composite_nomem(s->domain.dns_name, c)) return c;
3187 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
3188 if (composite_nomem(s->domain.netbios_name, c)) return c;
3189 s->domain.sid = dom_sid_dup(s, r->in.domain_sid);
3190 if (composite_nomem(s->domain.sid, c)) return c;
3192 /* Source DSA input */
3193 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
3194 if (composite_nomem(s->source_dsa.address, c)) return c;
3196 /* Destination DSA input */
3197 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
3198 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
3200 /* Destination DSA dns_name construction */
3201 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
3202 if (composite_nomem(tmp_name, c)) return c;
3203 tmp_name = talloc_asprintf_append_buffer(tmp_name, ".%s",s->domain.dns_name);
3204 if (composite_nomem(tmp_name, c)) return c;
3205 s->dest_dsa.dns_name = tmp_name;
3207 /* Callback function pointers */
3208 s->callbacks = r->in.callbacks;
3210 /* RODC join*/
3211 s->rodc_join = r->in.rodc_join;
3213 becomeDC_send_cldap(s);
3214 return c;
3217 NTSTATUS libnet_BecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3219 NTSTATUS status;
3221 status = composite_wait(c);
3223 ZERO_STRUCT(r->out);
3225 talloc_free(c);
3226 return status;
3229 NTSTATUS libnet_BecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3231 NTSTATUS status;
3232 struct composite_context *c;
3233 c = libnet_BecomeDC_send(ctx, mem_ctx, r);
3234 status = libnet_BecomeDC_recv(c, mem_ctx, r);
3235 return status;