s3/net_rpc_printer: replace dup_sec_desc() usage
[Samba.git] / source4 / libnet / libnet_become_dc.c
blob6d00fcc37b1245408c826b0a8fcfcd383062b39b
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,target_hostname=%s]",
1577 s->source_dsa.address,
1578 krb5_str, print_str,
1579 s->source_dsa.dns_name);
1580 if (composite_nomem(binding_str, c)) return;
1581 c->status = dcerpc_parse_binding(s, binding_str, &drsuapi->binding);
1582 talloc_free(binding_str);
1583 if (!composite_is_ok(c)) return;
1586 if (DEBUGLEVEL >= 10) {
1587 c->status = dcerpc_binding_set_flags(drsuapi->binding,
1588 DCERPC_DEBUG_PRINT_BOTH,
1590 if (!composite_is_ok(c)) return;
1593 creq = dcerpc_pipe_connect_b_send(s, drsuapi->binding, &ndr_table_drsuapi,
1594 s->libnet->cred, s->libnet->event_ctx,
1595 s->libnet->lp_ctx);
1596 composite_continue(c, creq, recv_fn, s);
1599 static void becomeDC_drsuapi_bind_send(struct libnet_BecomeDC_state *s,
1600 struct becomeDC_drsuapi *drsuapi,
1601 void (*recv_fn)(struct tevent_req *subreq));
1602 static void becomeDC_drsuapi1_bind_recv(struct tevent_req *subreq);
1604 static void becomeDC_drsuapi1_connect_recv(struct composite_context *req)
1606 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
1607 struct libnet_BecomeDC_state);
1608 struct composite_context *c = s->creq;
1610 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi1.pipe);
1611 if (!composite_is_ok(c)) return;
1613 s->drsuapi1.drsuapi_handle = s->drsuapi1.pipe->binding_handle;
1615 c->status = gensec_session_key(s->drsuapi1.pipe->conn->security_state.generic_state,
1617 &s->drsuapi1.gensec_skey);
1618 if (!composite_is_ok(c)) return;
1620 becomeDC_drsuapi_bind_send(s, &s->drsuapi1, becomeDC_drsuapi1_bind_recv);
1623 static void becomeDC_drsuapi_bind_send(struct libnet_BecomeDC_state *s,
1624 struct becomeDC_drsuapi *drsuapi,
1625 void (*recv_fn)(struct tevent_req *subreq))
1627 struct composite_context *c = s->creq;
1628 struct drsuapi_DsBindInfo28 *bind_info28;
1629 struct tevent_req *subreq;
1631 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &drsuapi->bind_guid);
1633 bind_info28 = &drsuapi->local_info28;
1634 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
1635 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
1636 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
1637 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
1638 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
1639 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
1640 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
1641 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
1642 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
1643 if (s->domain.behavior_version >= DS_DOMAIN_FUNCTION_2003) {
1644 /* TODO: find out how this is really triggered! */
1645 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
1647 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
1648 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
1649 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
1650 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
1651 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
1652 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
1653 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
1654 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
1655 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
1656 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
1657 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
1658 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
1659 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
1660 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
1661 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
1662 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
1663 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
1664 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
1665 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
1666 #if 0 /* we don't support XPRESS compression yet */
1667 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
1668 #endif
1669 bind_info28->site_guid = s->dest_dsa.site_guid;
1670 bind_info28->pid = 0;
1671 bind_info28->repl_epoch = 0;
1673 drsuapi->bind_info_ctr.length = 28;
1674 drsuapi->bind_info_ctr.info.info28 = *bind_info28;
1676 drsuapi->bind_r.in.bind_guid = &drsuapi->bind_guid;
1677 drsuapi->bind_r.in.bind_info = &drsuapi->bind_info_ctr;
1678 drsuapi->bind_r.out.bind_handle = &drsuapi->bind_handle;
1680 subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
1681 drsuapi->drsuapi_handle,
1682 &drsuapi->bind_r);
1683 if (composite_nomem(subreq, c)) return;
1684 tevent_req_set_callback(subreq, recv_fn, s);
1687 static WERROR becomeDC_drsuapi_bind_recv(struct libnet_BecomeDC_state *s,
1688 struct becomeDC_drsuapi *drsuapi)
1690 if (!W_ERROR_IS_OK(drsuapi->bind_r.out.result)) {
1691 return drsuapi->bind_r.out.result;
1694 ZERO_STRUCT(drsuapi->remote_info28);
1695 if (drsuapi->bind_r.out.bind_info) {
1696 switch (drsuapi->bind_r.out.bind_info->length) {
1697 case 24: {
1698 struct drsuapi_DsBindInfo24 *info24;
1699 info24 = &drsuapi->bind_r.out.bind_info->info.info24;
1700 drsuapi->remote_info28.supported_extensions = info24->supported_extensions;
1701 drsuapi->remote_info28.site_guid = info24->site_guid;
1702 drsuapi->remote_info28.pid = info24->pid;
1703 drsuapi->remote_info28.repl_epoch = 0;
1704 break;
1706 case 48: {
1707 struct drsuapi_DsBindInfo48 *info48;
1708 info48 = &drsuapi->bind_r.out.bind_info->info.info48;
1709 drsuapi->remote_info28.supported_extensions = info48->supported_extensions;
1710 drsuapi->remote_info28.site_guid = info48->site_guid;
1711 drsuapi->remote_info28.pid = info48->pid;
1712 drsuapi->remote_info28.repl_epoch = info48->repl_epoch;
1713 break;
1715 case 28:
1716 drsuapi->remote_info28 = drsuapi->bind_r.out.bind_info->info.info28;
1717 break;
1721 return WERR_OK;
1724 static void becomeDC_drsuapi1_add_entry_send(struct libnet_BecomeDC_state *s);
1726 static void becomeDC_drsuapi1_bind_recv(struct tevent_req *subreq)
1728 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
1729 struct libnet_BecomeDC_state);
1730 struct composite_context *c = s->creq;
1731 WERROR status;
1733 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
1734 TALLOC_FREE(subreq);
1735 if (!composite_is_ok(c)) return;
1737 status = becomeDC_drsuapi_bind_recv(s, &s->drsuapi1);
1738 if (!W_ERROR_IS_OK(status)) {
1739 composite_error(c, werror_to_ntstatus(status));
1740 return;
1743 becomeDC_drsuapi1_add_entry_send(s);
1746 static void becomeDC_drsuapi1_add_entry_recv(struct tevent_req *subreq);
1748 static void becomeDC_drsuapi1_add_entry_send(struct libnet_BecomeDC_state *s)
1750 struct composite_context *c = s->creq;
1751 struct drsuapi_DsAddEntry *r;
1752 struct drsuapi_DsReplicaObjectIdentifier *identifier;
1753 uint32_t num_attrs, i = 0;
1754 struct drsuapi_DsReplicaAttribute *attrs;
1755 enum ndr_err_code ndr_err;
1756 bool w2k3;
1757 struct tevent_req *subreq;
1759 /* choose a random invocationId */
1760 s->dest_dsa.invocation_id = GUID_random();
1763 * if the schema version indicates w2k3, then also send some w2k3
1764 * specific attributes.
1766 if (s->forest.schema_object_version >= 30) {
1767 w2k3 = true;
1768 } else {
1769 w2k3 = false;
1772 r = talloc_zero(s, struct drsuapi_DsAddEntry);
1773 if (composite_nomem(r, c)) return;
1775 /* setup identifier */
1776 identifier = talloc(r, struct drsuapi_DsReplicaObjectIdentifier);
1777 if (composite_nomem(identifier, c)) return;
1778 identifier->guid = GUID_zero();
1779 identifier->sid = s->zero_sid;
1780 identifier->dn = talloc_asprintf(identifier, "CN=NTDS Settings,%s",
1781 s->dest_dsa.server_dn_str);
1782 if (composite_nomem(identifier->dn, c)) return;
1784 /* allocate attribute array */
1785 num_attrs = 12;
1786 attrs = talloc_array(r, struct drsuapi_DsReplicaAttribute, num_attrs);
1787 if (composite_nomem(attrs, c)) return;
1789 /* ntSecurityDescriptor */
1791 struct drsuapi_DsAttributeValue *vs;
1792 DATA_BLOB *vd;
1793 struct security_descriptor *v;
1794 struct dom_sid *domain_admins_sid;
1795 const char *domain_admins_sid_str;
1797 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1798 if (composite_nomem(vs, c)) return;
1800 vd = talloc_array(vs, DATA_BLOB, 1);
1801 if (composite_nomem(vd, c)) return;
1803 domain_admins_sid = dom_sid_add_rid(vs, s->domain.sid, DOMAIN_RID_ADMINS);
1804 if (composite_nomem(domain_admins_sid, c)) return;
1806 domain_admins_sid_str = dom_sid_string(domain_admins_sid, domain_admins_sid);
1807 if (composite_nomem(domain_admins_sid_str, c)) return;
1809 v = security_descriptor_dacl_create(vd,
1811 /* owner: domain admins */
1812 domain_admins_sid_str,
1813 /* owner group: domain admins */
1814 domain_admins_sid_str,
1815 /* authenticated users */
1816 SID_NT_AUTHENTICATED_USERS,
1817 SEC_ACE_TYPE_ACCESS_ALLOWED,
1818 SEC_STD_READ_CONTROL |
1819 SEC_ADS_LIST |
1820 SEC_ADS_READ_PROP |
1821 SEC_ADS_LIST_OBJECT,
1823 /* domain admins */
1824 domain_admins_sid_str,
1825 SEC_ACE_TYPE_ACCESS_ALLOWED,
1826 SEC_STD_REQUIRED |
1827 SEC_ADS_CREATE_CHILD |
1828 SEC_ADS_LIST |
1829 SEC_ADS_SELF_WRITE |
1830 SEC_ADS_READ_PROP |
1831 SEC_ADS_WRITE_PROP |
1832 SEC_ADS_DELETE_TREE |
1833 SEC_ADS_LIST_OBJECT |
1834 SEC_ADS_CONTROL_ACCESS,
1836 /* system */
1837 SID_NT_SYSTEM,
1838 SEC_ACE_TYPE_ACCESS_ALLOWED,
1839 SEC_STD_REQUIRED |
1840 SEC_ADS_CREATE_CHILD |
1841 SEC_ADS_DELETE_CHILD |
1842 SEC_ADS_LIST |
1843 SEC_ADS_SELF_WRITE |
1844 SEC_ADS_READ_PROP |
1845 SEC_ADS_WRITE_PROP |
1846 SEC_ADS_DELETE_TREE |
1847 SEC_ADS_LIST_OBJECT |
1848 SEC_ADS_CONTROL_ACCESS,
1850 /* end */
1851 NULL);
1852 if (composite_nomem(v, c)) return;
1854 ndr_err = ndr_push_struct_blob(&vd[0], vd, v,
1855 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
1856 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1857 c->status = ndr_map_error2ntstatus(ndr_err);
1858 if (!composite_is_ok(c)) return;
1861 vs[0].blob = &vd[0];
1863 attrs[i].attid = DRSUAPI_ATTID_ntSecurityDescriptor;
1864 attrs[i].value_ctr.num_values = 1;
1865 attrs[i].value_ctr.values = vs;
1867 i++;
1870 /* objectClass: nTDSDSA */
1872 struct drsuapi_DsAttributeValue *vs;
1873 DATA_BLOB *vd;
1875 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1876 if (composite_nomem(vs, c)) return;
1878 vd = talloc_array(vs, DATA_BLOB, 1);
1879 if (composite_nomem(vd, c)) return;
1881 vd[0] = data_blob_talloc(vd, NULL, 4);
1882 if (composite_nomem(vd[0].data, c)) return;
1884 /* value for nTDSDSA */
1885 SIVAL(vd[0].data, 0, 0x0017002F);
1887 vs[0].blob = &vd[0];
1889 attrs[i].attid = DRSUAPI_ATTID_objectClass;
1890 attrs[i].value_ctr.num_values = 1;
1891 attrs[i].value_ctr.values = vs;
1893 i++;
1896 /* objectCategory: CN=NTDS-DSA,CN=Schema,... or CN=NTDS-DSA-RO,CN=Schema,... */
1898 struct drsuapi_DsAttributeValue *vs;
1899 DATA_BLOB *vd;
1900 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
1902 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1903 if (composite_nomem(vs, c)) return;
1905 vd = talloc_array(vs, DATA_BLOB, 1);
1906 if (composite_nomem(vd, c)) return;
1908 v[0].guid = GUID_zero();
1909 v[0].sid = s->zero_sid;
1911 if (s->rodc_join) {
1912 v[0].dn = talloc_asprintf(vd, "CN=NTDS-DSA-RO,%s",
1913 s->forest.schema_dn_str);
1914 } else {
1915 v[0].dn = talloc_asprintf(vd, "CN=NTDS-DSA,%s",
1916 s->forest.schema_dn_str);
1918 if (composite_nomem(v[0].dn, c)) return;
1920 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
1921 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1922 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1923 c->status = ndr_map_error2ntstatus(ndr_err);
1924 if (!composite_is_ok(c)) return;
1927 vs[0].blob = &vd[0];
1929 attrs[i].attid = DRSUAPI_ATTID_objectCategory;
1930 attrs[i].value_ctr.num_values = 1;
1931 attrs[i].value_ctr.values = vs;
1933 i++;
1936 /* invocationId: random guid */
1938 struct drsuapi_DsAttributeValue *vs;
1939 DATA_BLOB *vd;
1940 const struct GUID *v;
1942 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1943 if (composite_nomem(vs, c)) return;
1945 vd = talloc_array(vs, DATA_BLOB, 1);
1946 if (composite_nomem(vd, c)) return;
1948 v = &s->dest_dsa.invocation_id;
1950 c->status = GUID_to_ndr_blob(v, vd, &vd[0]);
1951 if (!composite_is_ok(c)) return;
1953 vs[0].blob = &vd[0];
1955 attrs[i].attid = DRSUAPI_ATTID_invocationId;
1956 attrs[i].value_ctr.num_values = 1;
1957 attrs[i].value_ctr.values = vs;
1959 i++;
1962 /* hasMasterNCs: ... */
1964 struct drsuapi_DsAttributeValue *vs;
1965 DATA_BLOB *vd;
1966 struct drsuapi_DsReplicaObjectIdentifier3 v[3];
1968 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 3);
1969 if (composite_nomem(vs, c)) return;
1971 vd = talloc_array(vs, DATA_BLOB, 3);
1972 if (composite_nomem(vd, c)) return;
1974 v[0].guid = GUID_zero();
1975 v[0].sid = s->zero_sid;
1976 v[0].dn = s->forest.config_dn_str;
1978 v[1].guid = GUID_zero();
1979 v[1].sid = s->zero_sid;
1980 v[1].dn = s->domain.dn_str;
1982 v[2].guid = GUID_zero();
1983 v[2].sid = s->zero_sid;
1984 v[2].dn = s->forest.schema_dn_str;
1986 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
1987 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1988 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1989 c->status = ndr_map_error2ntstatus(ndr_err);
1990 if (!composite_is_ok(c)) return;
1993 ndr_err = ndr_push_struct_blob(&vd[1], vd, &v[1],
1994 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1995 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1996 c->status = ndr_map_error2ntstatus(ndr_err);
1997 if (!composite_is_ok(c)) return;
2000 ndr_err = ndr_push_struct_blob(&vd[2], vd, &v[2],
2001 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2002 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2003 c->status = ndr_map_error2ntstatus(ndr_err);
2004 if (!composite_is_ok(c)) return;
2007 vs[0].blob = &vd[0];
2008 vs[1].blob = &vd[1];
2009 vs[2].blob = &vd[2];
2011 attrs[i].attid = DRSUAPI_ATTID_hasMasterNCs;
2012 attrs[i].value_ctr.num_values = 3;
2013 attrs[i].value_ctr.values = vs;
2015 i++;
2018 /* msDS-hasMasterNCs: ... */
2019 if (w2k3) {
2020 struct drsuapi_DsAttributeValue *vs;
2021 DATA_BLOB *vd;
2022 struct drsuapi_DsReplicaObjectIdentifier3 v[3];
2024 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 3);
2025 if (composite_nomem(vs, c)) return;
2027 vd = talloc_array(vs, DATA_BLOB, 3);
2028 if (composite_nomem(vd, c)) return;
2030 v[0].guid = GUID_zero();
2031 v[0].sid = s->zero_sid;
2032 v[0].dn = s->forest.config_dn_str;
2034 v[1].guid = GUID_zero();
2035 v[1].sid = s->zero_sid;
2036 v[1].dn = s->domain.dn_str;
2038 v[2].guid = GUID_zero();
2039 v[2].sid = s->zero_sid;
2040 v[2].dn = s->forest.schema_dn_str;
2042 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2043 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2044 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2045 c->status = ndr_map_error2ntstatus(ndr_err);
2046 if (!composite_is_ok(c)) return;
2049 ndr_err = ndr_push_struct_blob(&vd[1], vd, &v[1],
2050 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2051 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2052 c->status = ndr_map_error2ntstatus(ndr_err);
2053 if (!composite_is_ok(c)) return;
2056 ndr_err = ndr_push_struct_blob(&vd[2], vd, &v[2],
2057 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2058 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2059 c->status = ndr_map_error2ntstatus(ndr_err);
2060 if (!composite_is_ok(c)) return;
2063 vs[0].blob = &vd[0];
2064 vs[1].blob = &vd[1];
2065 vs[2].blob = &vd[2];
2067 attrs[i].attid = DRSUAPI_ATTID_msDS_hasMasterNCs;
2068 attrs[i].value_ctr.num_values = 3;
2069 attrs[i].value_ctr.values = vs;
2071 i++;
2074 /* dMDLocation: CN=Schema,... */
2076 struct drsuapi_DsAttributeValue *vs;
2077 DATA_BLOB *vd;
2078 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2080 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2081 if (composite_nomem(vs, c)) return;
2083 vd = talloc_array(vs, DATA_BLOB, 1);
2084 if (composite_nomem(vd, c)) return;
2086 v[0].guid = GUID_zero();
2087 v[0].sid = s->zero_sid;
2088 v[0].dn = s->forest.schema_dn_str;
2090 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2091 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2092 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2093 c->status = ndr_map_error2ntstatus(ndr_err);
2094 if (!composite_is_ok(c)) return;
2097 vs[0].blob = &vd[0];
2099 attrs[i].attid = DRSUAPI_ATTID_dMDLocation;
2100 attrs[i].value_ctr.num_values = 1;
2101 attrs[i].value_ctr.values = vs;
2103 i++;
2106 /* msDS-HasDomainNCs: <domain_partition> */
2107 if (w2k3) {
2108 struct drsuapi_DsAttributeValue *vs;
2109 DATA_BLOB *vd;
2110 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2112 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2113 if (composite_nomem(vs, c)) return;
2115 vd = talloc_array(vs, DATA_BLOB, 1);
2116 if (composite_nomem(vd, c)) return;
2118 v[0].guid = GUID_zero();
2119 v[0].sid = s->zero_sid;
2120 v[0].dn = s->domain.dn_str;
2122 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2123 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2124 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2125 c->status = ndr_map_error2ntstatus(ndr_err);
2126 if (!composite_is_ok(c)) return;
2129 vs[0].blob = &vd[0];
2131 attrs[i].attid = DRSUAPI_ATTID_msDS_HasDomainNCs;
2132 attrs[i].value_ctr.num_values = 1;
2133 attrs[i].value_ctr.values = vs;
2135 i++;
2138 /* msDS-Behavior-Version */
2139 if (w2k3) {
2140 struct drsuapi_DsAttributeValue *vs;
2141 DATA_BLOB *vd;
2143 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2144 if (composite_nomem(vs, c)) return;
2146 vd = talloc_array(vs, DATA_BLOB, 1);
2147 if (composite_nomem(vd, c)) return;
2149 vd[0] = data_blob_talloc(vd, NULL, 4);
2150 if (composite_nomem(vd[0].data, c)) return;
2152 SIVAL(vd[0].data, 0, get_dc_function_level(s->libnet->lp_ctx));
2154 vs[0].blob = &vd[0];
2156 attrs[i].attid = DRSUAPI_ATTID_msDS_Behavior_Version;
2157 attrs[i].value_ctr.num_values = 1;
2158 attrs[i].value_ctr.values = vs;
2160 i++;
2163 /* systemFlags */
2165 struct drsuapi_DsAttributeValue *vs;
2166 DATA_BLOB *vd;
2168 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2169 if (composite_nomem(vs, c)) return;
2171 vd = talloc_array(vs, DATA_BLOB, 1);
2172 if (composite_nomem(vd, c)) return;
2174 vd[0] = data_blob_talloc(vd, NULL, 4);
2175 if (composite_nomem(vd[0].data, c)) return;
2177 if (s->rodc_join) {
2178 SIVAL(vd[0].data, 0, SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
2179 } else {
2180 SIVAL(vd[0].data, 0, SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
2183 vs[0].blob = &vd[0];
2185 attrs[i].attid = DRSUAPI_ATTID_systemFlags;
2186 attrs[i].value_ctr.num_values = 1;
2187 attrs[i].value_ctr.values = vs;
2189 i++;
2192 /* serverReference: ... */
2194 struct drsuapi_DsAttributeValue *vs;
2195 DATA_BLOB *vd;
2196 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2198 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2199 if (composite_nomem(vs, c)) return;
2201 vd = talloc_array(vs, DATA_BLOB, 1);
2202 if (composite_nomem(vd, c)) return;
2204 v[0].guid = GUID_zero();
2205 v[0].sid = s->zero_sid;
2206 v[0].dn = s->dest_dsa.computer_dn_str;
2208 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2209 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2210 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2211 c->status = ndr_map_error2ntstatus(ndr_err);
2212 if (!composite_is_ok(c)) return;
2215 vs[0].blob = &vd[0];
2217 attrs[i].attid = DRSUAPI_ATTID_serverReference;
2218 attrs[i].value_ctr.num_values = 1;
2219 attrs[i].value_ctr.values = vs;
2221 i++;
2224 /* options:... */
2225 if (s->rodc_join) {
2226 struct drsuapi_DsAttributeValue *vs;
2227 DATA_BLOB *vd;
2229 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2230 if (composite_nomem(vs, c)) return;
2232 vd = talloc_array(vs, DATA_BLOB, 1);
2233 if (composite_nomem(vd, c)) return;
2235 vd[0] = data_blob_talloc(vd, NULL, 4);
2236 if (composite_nomem(vd[0].data, c)) return;
2238 SIVAL(vd[0].data, 0, DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL);
2240 vs[0].blob = &vd[0];
2242 attrs[i].attid = DRSUAPI_ATTID_options;
2243 attrs[i].value_ctr.num_values = 1;
2244 attrs[i].value_ctr.values = vs;
2246 i++;
2249 /* truncate the attribute list to the attribute count we have filled in */
2250 num_attrs = i;
2252 /* setup request structure */
2253 r->in.bind_handle = &s->drsuapi1.bind_handle;
2254 r->in.level = 2;
2255 r->in.req = talloc(s, union drsuapi_DsAddEntryRequest);
2256 r->in.req->req2.first_object.next_object = NULL;
2257 r->in.req->req2.first_object.object.identifier = identifier;
2258 r->in.req->req2.first_object.object.flags = 0x00000000;
2259 r->in.req->req2.first_object.object.attribute_ctr.num_attributes= num_attrs;
2260 r->in.req->req2.first_object.object.attribute_ctr.attributes = attrs;
2262 r->out.level_out = talloc(s, uint32_t);
2263 r->out.ctr = talloc(s, union drsuapi_DsAddEntryCtr);
2265 s->ndr_struct_ptr = r;
2266 subreq = dcerpc_drsuapi_DsAddEntry_r_send(s, c->event_ctx,
2267 s->drsuapi1.drsuapi_handle, r);
2268 if (composite_nomem(subreq, c)) return;
2269 tevent_req_set_callback(subreq, becomeDC_drsuapi1_add_entry_recv, s);
2272 static void becomeDC_drsuapi2_connect_recv(struct composite_context *req);
2273 static NTSTATUS becomeDC_prepare_db(struct libnet_BecomeDC_state *s);
2275 static void becomeDC_drsuapi1_add_entry_recv(struct tevent_req *subreq)
2277 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2278 struct libnet_BecomeDC_state);
2279 struct composite_context *c = s->creq;
2280 struct drsuapi_DsAddEntry *r = talloc_get_type_abort(s->ndr_struct_ptr,
2281 struct drsuapi_DsAddEntry);
2282 char *binding_str;
2283 uint32_t assoc_group_id;
2285 s->ndr_struct_ptr = NULL;
2287 c->status = dcerpc_drsuapi_DsAddEntry_r_recv(subreq, r);
2288 TALLOC_FREE(subreq);
2289 if (!composite_is_ok(c)) return;
2291 if (!W_ERROR_IS_OK(r->out.result)) {
2292 composite_error(c, werror_to_ntstatus(r->out.result));
2293 return;
2296 if (*r->out.level_out == 3) {
2297 WERROR status;
2298 union drsuapi_DsAddEntry_ErrData *err_data = r->out.ctr->ctr3.err_data;
2300 /* check for errors */
2301 status = err_data ? err_data->v1.status : WERR_OK;
2302 if (!W_ERROR_IS_OK(status)) {
2303 struct drsuapi_DsAddEntryErrorInfo_Attr_V1 *attr_err;
2304 struct drsuapi_DsAddEntry_AttrErrListItem_V1 *attr_err_li;
2305 struct drsuapi_DsAddEntryErrorInfo_Name_V1 *name_err;
2306 struct drsuapi_DsAddEntryErrorInfo_Referr_V1 *ref_err;
2307 struct drsuapi_DsAddEntry_RefErrListItem_V1 *ref_li;
2309 if (r->out.ctr->ctr3.err_ver != 1) {
2310 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2311 return;
2314 DEBUG(0,("DsAddEntry (R3) of '%s' failed: "
2315 "Errors: dir_err = %d, status = %s;\n",
2316 r->in.req->req3.first_object.object.identifier->dn,
2317 err_data->v1.dir_err,
2318 win_errstr(err_data->v1.status)));
2320 if (!err_data->v1.info) {
2321 DEBUG(0, ("DsAddEntry (R3): no error info returned!\n"));
2322 composite_error(c, werror_to_ntstatus(status));
2323 return;
2326 /* dump more detailed error */
2327 switch (err_data->v1.dir_err) {
2328 case DRSUAPI_DIRERR_ATTRIBUTE:
2329 /* Dump attribute errors */
2330 attr_err = &err_data->v1.info->attr_err;
2331 DEBUGADD(0,(" Attribute Error: object = %s, count = %d;\n",
2332 attr_err->id->dn,
2333 attr_err->count));
2334 attr_err_li = &attr_err->first;
2335 for (; attr_err_li; attr_err_li = attr_err_li->next) {
2336 struct drsuapi_DsAddEntry_AttrErr_V1 *err = &attr_err_li->err_data;
2337 DEBUGADD(0,(" Error: err = %s, problem = 0x%08X, attid = 0x%08X;\n",
2338 win_errstr(err->extended_err),
2339 err->problem,
2340 err->attid));
2341 /* TODO: should we print attribute value here? */
2343 break;
2344 case DRSUAPI_DIRERR_NAME:
2345 /* Dump Name resolution error */
2346 name_err = &err_data->v1.info->name_err;
2347 DEBUGADD(0,(" Name Error: err = %s, problem = 0x%08X, id_matched = %s;\n",
2348 win_errstr(name_err->extended_err),
2349 name_err->problem,
2350 name_err->id_matched->dn));
2351 break;
2352 case DRSUAPI_DIRERR_REFERRAL:
2353 /* Dump Referral errors */
2354 ref_err = &err_data->v1.info->referral_err;
2355 DEBUGADD(0,(" Referral Error: extended_err = %s\n",
2356 win_errstr(ref_err->extended_err)));
2357 ref_li = &ref_err->refer;
2358 for (; ref_li; ref_li = ref_li->next) {
2359 struct drsuapi_DsaAddressListItem_V1 *addr;
2360 DEBUGADD(0,(" Referral: id_target = %s, ref_type = 0x%04X,",
2361 ref_li->id_target->dn,
2362 ref_li->ref_type));
2363 if (ref_li->is_choice_set) {
2364 DEBUGADD(0,(" choice = 0x%02X, ",
2365 ref_li->choice));
2367 DEBUGADD(0,(" add_list ("));
2368 for (addr = ref_li->addr_list; addr; addr = addr->next) {
2369 DEBUGADD(0,("%s", addr->address->string));
2370 if (addr->next) {
2371 DEBUGADD(0,(", "));
2374 DEBUGADD(0,(");\n"));
2376 break;
2377 case DRSUAPI_DIRERR_SECURITY:
2378 /* Dump Security error. */
2379 DEBUGADD(0,(" Security Error: extended_err = %s, problem = 0x%08X\n",
2380 win_errstr(err_data->v1.info->security_err.extended_err),
2381 err_data->v1.info->security_err.problem));
2382 break;
2383 case DRSUAPI_DIRERR_SERVICE:
2384 /* Dump Service error. */
2385 DEBUGADD(0,(" Service Error: extended_err = %s, problem = 0x%08X\n",
2386 win_errstr(err_data->v1.info->service_err.extended_err),
2387 err_data->v1.info->service_err.problem));
2388 break;
2389 case DRSUAPI_DIRERR_UPDATE:
2390 /* Dump Update error. */
2391 DEBUGADD(0,(" Update Error: extended_err = %s, problem = 0x%08X\n",
2392 win_errstr(err_data->v1.info->update_err.extended_err),
2393 err_data->v1.info->update_err.problem));
2394 break;
2395 case DRSUAPI_DIRERR_SYSTEM:
2396 /* System error. */
2397 DEBUGADD(0,(" System Error: extended_err = %s, problem = 0x%08X\n",
2398 win_errstr(err_data->v1.info->system_err.extended_err),
2399 err_data->v1.info->system_err.problem));
2400 break;
2401 case DRSUAPI_DIRERR_OK: /* mute compiler warnings */
2402 default:
2403 DEBUGADD(0,(" Unknown DIRERR error class returned!\n"));
2404 break;
2407 composite_error(c, werror_to_ntstatus(status));
2408 return;
2411 if (1 != r->out.ctr->ctr3.count) {
2412 DEBUG(0,("DsAddEntry - Ctr3: something very wrong had happened - "
2413 "method succeeded but objects returned are %d (expected 1).\n",
2414 r->out.ctr->ctr3.count));
2415 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2416 return;
2419 s->dest_dsa.ntds_guid = r->out.ctr->ctr3.objects[0].guid;
2421 } else if (*r->out.level_out == 2) {
2422 if (DRSUAPI_DIRERR_OK != r->out.ctr->ctr2.dir_err) {
2423 DEBUG(0,("DsAddEntry failed with: dir_err = %d, extended_err = %s\n",
2424 r->out.ctr->ctr2.dir_err,
2425 win_errstr(r->out.ctr->ctr2.extended_err)));
2426 composite_error(c, werror_to_ntstatus(r->out.ctr->ctr2.extended_err));
2427 return;
2430 if (1 != r->out.ctr->ctr2.count) {
2431 DEBUG(0,("DsAddEntry: something very wrong had happened - "
2432 "method succeeded but objects returned are %d (expected 1). "
2433 "Errors: dir_err = %d, extended_err = %s\n",
2434 r->out.ctr->ctr2.count,
2435 r->out.ctr->ctr2.dir_err,
2436 win_errstr(r->out.ctr->ctr2.extended_err)));
2437 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2438 return;
2441 s->dest_dsa.ntds_guid = r->out.ctr->ctr2.objects[0].guid;
2442 } else {
2443 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2444 return;
2447 talloc_free(r);
2449 s->dest_dsa.ntds_dn_str = talloc_asprintf(s, "CN=NTDS Settings,%s",
2450 s->dest_dsa.server_dn_str);
2451 if (composite_nomem(s->dest_dsa.ntds_dn_str, c)) return;
2453 c->status = becomeDC_prepare_db(s);
2454 if (!composite_is_ok(c)) return;
2456 /* this avoids the epmapper lookup on the 2nd connection */
2457 binding_str = dcerpc_binding_string(s, s->drsuapi1.binding);
2458 if (composite_nomem(binding_str, c)) return;
2460 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi2.binding);
2461 talloc_free(binding_str);
2462 if (!composite_is_ok(c)) return;
2464 if (DEBUGLEVEL >= 10) {
2465 c->status = dcerpc_binding_set_flags(s->drsuapi2.binding,
2466 DCERPC_DEBUG_PRINT_BOTH,
2468 if (!composite_is_ok(c)) return;
2471 /* w2k3 uses the same assoc_group_id as on the first connection, so we do */
2472 assoc_group_id = dcerpc_binding_get_assoc_group_id(s->drsuapi1.pipe->binding);
2473 c->status = dcerpc_binding_set_assoc_group_id(s->drsuapi2.binding, assoc_group_id);
2474 if (!composite_is_ok(c)) return;
2476 becomeDC_drsuapi_connect_send(s, &s->drsuapi2, becomeDC_drsuapi2_connect_recv);
2479 static NTSTATUS becomeDC_prepare_db(struct libnet_BecomeDC_state *s)
2481 if (!s->callbacks.prepare_db) return NT_STATUS_OK;
2483 s->_pp.domain = &s->domain;
2484 s->_pp.forest = &s->forest;
2485 s->_pp.source_dsa = &s->source_dsa;
2486 s->_pp.dest_dsa = &s->dest_dsa;
2488 return s->callbacks.prepare_db(s->callbacks.private_data, &s->_pp);
2491 static void becomeDC_drsuapi2_bind_recv(struct tevent_req *subreq);
2493 static void becomeDC_drsuapi2_connect_recv(struct composite_context *req)
2495 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
2496 struct libnet_BecomeDC_state);
2497 struct composite_context *c = s->creq;
2499 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi2.pipe);
2500 if (!composite_is_ok(c)) return;
2502 s->drsuapi2.drsuapi_handle = s->drsuapi2.pipe->binding_handle;
2504 c->status = gensec_session_key(s->drsuapi2.pipe->conn->security_state.generic_state,
2506 &s->drsuapi2.gensec_skey);
2507 if (!composite_is_ok(c)) return;
2509 becomeDC_drsuapi_bind_send(s, &s->drsuapi2, becomeDC_drsuapi2_bind_recv);
2512 static void becomeDC_drsuapi3_connect_recv(struct composite_context *req);
2514 static void becomeDC_drsuapi2_bind_recv(struct tevent_req *subreq)
2516 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2517 struct libnet_BecomeDC_state);
2518 struct composite_context *c = s->creq;
2519 char *binding_str;
2520 uint32_t assoc_group_id;
2521 WERROR status;
2523 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
2524 TALLOC_FREE(subreq);
2525 if (!composite_is_ok(c)) return;
2527 status = becomeDC_drsuapi_bind_recv(s, &s->drsuapi2);
2528 if (!W_ERROR_IS_OK(status)) {
2529 composite_error(c, werror_to_ntstatus(status));
2530 return;
2533 /* this avoids the epmapper lookup on the 3rd connection */
2534 binding_str = dcerpc_binding_string(s, s->drsuapi1.binding);
2535 if (composite_nomem(binding_str, c)) return;
2537 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi3.binding);
2538 talloc_free(binding_str);
2539 if (!composite_is_ok(c)) return;
2541 if (DEBUGLEVEL >= 10) {
2542 c->status = dcerpc_binding_set_flags(s->drsuapi3.binding,
2543 DCERPC_DEBUG_PRINT_BOTH,
2545 if (!composite_is_ok(c)) return;
2548 /* w2k3 uses the same assoc_group_id as on the first connection, so we do */
2549 assoc_group_id = dcerpc_binding_get_assoc_group_id(s->drsuapi1.pipe->binding);
2550 c->status = dcerpc_binding_set_assoc_group_id(s->drsuapi3.binding, assoc_group_id);
2551 if (!composite_is_ok(c)) return;
2552 /* w2k3 uses the concurrent multiplex feature on the 3rd connection, so we do */
2553 c->status = dcerpc_binding_set_flags(s->drsuapi3.binding,
2554 DCERPC_CONCURRENT_MULTIPLEX,
2556 if (!composite_is_ok(c)) return;
2558 becomeDC_drsuapi_connect_send(s, &s->drsuapi3, becomeDC_drsuapi3_connect_recv);
2561 static void becomeDC_drsuapi3_pull_schema_send(struct libnet_BecomeDC_state *s);
2563 static void becomeDC_drsuapi3_connect_recv(struct composite_context *req)
2565 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
2566 struct libnet_BecomeDC_state);
2567 struct composite_context *c = s->creq;
2569 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi3.pipe);
2570 if (!composite_is_ok(c)) return;
2572 s->drsuapi3.drsuapi_handle = s->drsuapi3.pipe->binding_handle;
2574 c->status = gensec_session_key(s->drsuapi3.pipe->conn->security_state.generic_state,
2576 &s->drsuapi3.gensec_skey);
2577 if (!composite_is_ok(c)) return;
2579 becomeDC_drsuapi3_pull_schema_send(s);
2582 static void becomeDC_drsuapi_pull_partition_send(struct libnet_BecomeDC_state *s,
2583 struct becomeDC_drsuapi *drsuapi_h,
2584 struct becomeDC_drsuapi *drsuapi_p,
2585 struct libnet_BecomeDC_Partition *partition,
2586 void (*recv_fn)(struct tevent_req *subreq))
2588 struct composite_context *c = s->creq;
2589 struct drsuapi_DsGetNCChanges *r;
2590 struct tevent_req *subreq;
2592 r = talloc(s, struct drsuapi_DsGetNCChanges);
2593 if (composite_nomem(r, c)) return;
2595 r->out.level_out = talloc(r, uint32_t);
2596 if (composite_nomem(r->out.level_out, c)) return;
2597 r->in.req = talloc(r, union drsuapi_DsGetNCChangesRequest);
2598 if (composite_nomem(r->in.req, c)) return;
2599 r->out.ctr = talloc(r, union drsuapi_DsGetNCChangesCtr);
2600 if (composite_nomem(r->out.ctr, c)) return;
2602 r->in.bind_handle = &drsuapi_h->bind_handle;
2603 if (drsuapi_h->remote_info28.supported_extensions & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8) {
2604 r->in.level = 8;
2605 r->in.req->req8.destination_dsa_guid = partition->destination_dsa_guid;
2606 r->in.req->req8.source_dsa_invocation_id= partition->source_dsa_invocation_id;
2607 r->in.req->req8.naming_context = &partition->nc;
2608 r->in.req->req8.highwatermark = partition->highwatermark;
2609 r->in.req->req8.uptodateness_vector = NULL;
2610 r->in.req->req8.replica_flags = partition->replica_flags;
2611 r->in.req->req8.max_object_count = 133;
2612 r->in.req->req8.max_ndr_size = 1336811;
2613 r->in.req->req8.extended_op = DRSUAPI_EXOP_NONE;
2614 r->in.req->req8.fsmo_info = 0;
2615 r->in.req->req8.partial_attribute_set = NULL;
2616 r->in.req->req8.partial_attribute_set_ex= NULL;
2617 r->in.req->req8.mapping_ctr.num_mappings= 0;
2618 r->in.req->req8.mapping_ctr.mappings = NULL;
2619 } else {
2620 r->in.level = 5;
2621 r->in.req->req5.destination_dsa_guid = partition->destination_dsa_guid;
2622 r->in.req->req5.source_dsa_invocation_id= partition->source_dsa_invocation_id;
2623 r->in.req->req5.naming_context = &partition->nc;
2624 r->in.req->req5.highwatermark = partition->highwatermark;
2625 r->in.req->req5.uptodateness_vector = NULL;
2626 r->in.req->req5.replica_flags = partition->replica_flags;
2627 r->in.req->req5.max_object_count = 133;
2628 r->in.req->req5.max_ndr_size = 1336770;
2629 r->in.req->req5.extended_op = DRSUAPI_EXOP_NONE;
2630 r->in.req->req5.fsmo_info = 0;
2634 * we should try to use the drsuapi_p->pipe here, as w2k3 does
2635 * but it seems that some extra flags in the DCERPC Bind call
2636 * are needed for it. Or the same KRB5 TGS is needed on both
2637 * connections.
2639 s->ndr_struct_ptr = r;
2640 subreq = dcerpc_drsuapi_DsGetNCChanges_r_send(s, c->event_ctx,
2641 drsuapi_p->drsuapi_handle,
2643 if (composite_nomem(subreq, c)) return;
2644 tevent_req_set_callback(subreq, recv_fn, s);
2647 static WERROR becomeDC_drsuapi_pull_partition_recv(struct libnet_BecomeDC_state *s,
2648 struct becomeDC_drsuapi *drsuapi_h,
2649 struct becomeDC_drsuapi *drsuapi_p,
2650 struct libnet_BecomeDC_Partition *partition,
2651 struct drsuapi_DsGetNCChanges *r)
2653 uint32_t req_level = 0;
2654 struct drsuapi_DsGetNCChangesRequest5 *req5 = NULL;
2655 struct drsuapi_DsGetNCChangesRequest8 *req8 = NULL;
2656 struct drsuapi_DsGetNCChangesRequest10 *req10 = NULL;
2657 uint32_t ctr_level = 0;
2658 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
2659 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
2660 struct GUID *source_dsa_guid = NULL;
2661 struct GUID *source_dsa_invocation_id = NULL;
2662 struct drsuapi_DsReplicaHighWaterMark *new_highwatermark = NULL;
2663 bool more_data = false;
2664 NTSTATUS nt_status;
2666 if (!W_ERROR_IS_OK(r->out.result)) {
2667 return r->out.result;
2670 switch (r->in.level) {
2671 case 0:
2672 /* none */
2673 break;
2674 case 5:
2675 req5 = &r->in.req->req5;
2676 break;
2677 case 8:
2678 req8 = &r->in.req->req8;
2679 break;
2680 case 10:
2681 req10 = &r->in.req->req10;
2682 break;
2683 default:
2684 return WERR_INVALID_PARAMETER;
2687 if (*r->out.level_out == 1) {
2688 ctr_level = 1;
2689 ctr1 = &r->out.ctr->ctr1;
2690 } else if (*r->out.level_out == 2 &&
2691 r->out.ctr->ctr2.mszip1.ts) {
2692 ctr_level = 1;
2693 ctr1 = &r->out.ctr->ctr2.mszip1.ts->ctr1;
2694 } else if (*r->out.level_out == 6) {
2695 ctr_level = 6;
2696 ctr6 = &r->out.ctr->ctr6;
2697 } else if (*r->out.level_out == 7 &&
2698 r->out.ctr->ctr7.level == 6 &&
2699 r->out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP &&
2700 r->out.ctr->ctr7.ctr.mszip6.ts) {
2701 ctr_level = 6;
2702 ctr6 = &r->out.ctr->ctr7.ctr.mszip6.ts->ctr6;
2703 } else if (*r->out.level_out == 7 &&
2704 r->out.ctr->ctr7.level == 6 &&
2705 r->out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS &&
2706 r->out.ctr->ctr7.ctr.xpress6.ts) {
2707 ctr_level = 6;
2708 ctr6 = &r->out.ctr->ctr7.ctr.xpress6.ts->ctr6;
2709 } else {
2710 return WERR_BAD_NET_RESP;
2713 if (!ctr1 && ! ctr6) {
2714 return WERR_BAD_NET_RESP;
2717 if (ctr_level == 6) {
2718 if (!W_ERROR_IS_OK(ctr6->drs_error)) {
2719 return ctr6->drs_error;
2723 switch (ctr_level) {
2724 case 1:
2725 source_dsa_guid = &ctr1->source_dsa_guid;
2726 source_dsa_invocation_id = &ctr1->source_dsa_invocation_id;
2727 new_highwatermark = &ctr1->new_highwatermark;
2728 more_data = ctr1->more_data;
2729 break;
2730 case 6:
2731 source_dsa_guid = &ctr6->source_dsa_guid;
2732 source_dsa_invocation_id = &ctr6->source_dsa_invocation_id;
2733 new_highwatermark = &ctr6->new_highwatermark;
2734 more_data = ctr6->more_data;
2735 break;
2738 partition->highwatermark = *new_highwatermark;
2739 partition->source_dsa_guid = *source_dsa_guid;
2740 partition->source_dsa_invocation_id = *source_dsa_invocation_id;
2741 partition->more_data = more_data;
2743 if (!partition->store_chunk) return WERR_OK;
2745 s->_sc.domain = &s->domain;
2746 s->_sc.forest = &s->forest;
2747 s->_sc.source_dsa = &s->source_dsa;
2748 s->_sc.dest_dsa = &s->dest_dsa;
2749 s->_sc.partition = partition;
2750 s->_sc.req_level = req_level;
2751 s->_sc.req5 = req5;
2752 s->_sc.req8 = req8;
2753 s->_sc.req10 = req10;
2754 s->_sc.ctr_level = ctr_level;
2755 s->_sc.ctr1 = ctr1;
2756 s->_sc.ctr6 = ctr6;
2758 * we need to use the drsuapi_p->gensec_skey here,
2759 * when we use drsuapi_p->pipe in the for this request
2761 s->_sc.gensec_skey = &drsuapi_p->gensec_skey;
2763 nt_status = partition->store_chunk(s->callbacks.private_data, &s->_sc);
2764 if (!NT_STATUS_IS_OK(nt_status)) {
2765 return ntstatus_to_werror(nt_status);
2768 return WERR_OK;
2771 static void becomeDC_drsuapi3_pull_schema_recv(struct tevent_req *subreq);
2773 static void becomeDC_drsuapi3_pull_schema_send(struct libnet_BecomeDC_state *s)
2775 s->schema_part.nc.guid = GUID_zero();
2776 s->schema_part.nc.sid = s->zero_sid;
2777 s->schema_part.nc.dn = s->forest.schema_dn_str;
2779 s->schema_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2781 s->schema_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2782 | DRSUAPI_DRS_INIT_SYNC
2783 | DRSUAPI_DRS_PER_SYNC
2784 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2785 | DRSUAPI_DRS_NEVER_SYNCED
2786 | DRSUAPI_DRS_USE_COMPRESSION;
2787 if (s->rodc_join) {
2788 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2791 s->schema_part.store_chunk = s->callbacks.schema_chunk;
2793 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part,
2794 becomeDC_drsuapi3_pull_schema_recv);
2797 static void becomeDC_drsuapi3_pull_config_send(struct libnet_BecomeDC_state *s);
2799 static void becomeDC_drsuapi3_pull_schema_recv(struct tevent_req *subreq)
2801 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2802 struct libnet_BecomeDC_state);
2803 struct composite_context *c = s->creq;
2804 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2805 struct drsuapi_DsGetNCChanges);
2806 WERROR status;
2808 s->ndr_struct_ptr = NULL;
2810 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2811 TALLOC_FREE(subreq);
2812 if (!composite_is_ok(c)) return;
2814 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part, r);
2815 if (!W_ERROR_IS_OK(status)) {
2816 composite_error(c, werror_to_ntstatus(status));
2817 return;
2820 talloc_free(r);
2822 if (s->schema_part.more_data) {
2823 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part,
2824 becomeDC_drsuapi3_pull_schema_recv);
2825 return;
2828 becomeDC_drsuapi3_pull_config_send(s);
2831 static void becomeDC_drsuapi3_pull_config_recv(struct tevent_req *subreq);
2833 static void becomeDC_drsuapi3_pull_config_send(struct libnet_BecomeDC_state *s)
2835 s->config_part.nc.guid = GUID_zero();
2836 s->config_part.nc.sid = s->zero_sid;
2837 s->config_part.nc.dn = s->forest.config_dn_str;
2839 s->config_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2841 s->config_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2842 | DRSUAPI_DRS_INIT_SYNC
2843 | DRSUAPI_DRS_PER_SYNC
2844 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2845 | DRSUAPI_DRS_NEVER_SYNCED
2846 | DRSUAPI_DRS_USE_COMPRESSION;
2847 if (s->rodc_join) {
2848 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2851 s->config_part.store_chunk = s->callbacks.config_chunk;
2853 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->config_part,
2854 becomeDC_drsuapi3_pull_config_recv);
2857 static void becomeDC_drsuapi3_pull_config_recv(struct tevent_req *subreq)
2859 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2860 struct libnet_BecomeDC_state);
2861 struct composite_context *c = s->creq;
2862 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2863 struct drsuapi_DsGetNCChanges);
2864 WERROR status;
2866 s->ndr_struct_ptr = NULL;
2868 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2869 TALLOC_FREE(subreq);
2870 if (!composite_is_ok(c)) return;
2872 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->config_part, r);
2873 if (!W_ERROR_IS_OK(status)) {
2874 composite_error(c, werror_to_ntstatus(status));
2875 return;
2878 talloc_free(r);
2880 if (s->config_part.more_data) {
2881 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->config_part,
2882 becomeDC_drsuapi3_pull_config_recv);
2883 return;
2886 becomeDC_connect_ldap2(s);
2889 static void becomeDC_drsuapi3_pull_domain_recv(struct tevent_req *subreq);
2891 static void becomeDC_drsuapi3_pull_domain_send(struct libnet_BecomeDC_state *s)
2893 s->domain_part.nc.guid = GUID_zero();
2894 s->domain_part.nc.sid = s->zero_sid;
2895 s->domain_part.nc.dn = s->domain.dn_str;
2897 s->domain_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2899 s->domain_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2900 | DRSUAPI_DRS_INIT_SYNC
2901 | DRSUAPI_DRS_PER_SYNC
2902 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2903 | DRSUAPI_DRS_NEVER_SYNCED
2904 | DRSUAPI_DRS_USE_COMPRESSION;
2905 if (s->critical_only) {
2906 s->domain_part.replica_flags |= DRSUAPI_DRS_CRITICAL_ONLY | DRSUAPI_DRS_GET_ANC;
2908 if (s->rodc_join) {
2909 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2912 s->domain_part.store_chunk = s->callbacks.domain_chunk;
2914 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2915 becomeDC_drsuapi3_pull_domain_recv);
2918 static void becomeDC_drsuapi_update_refs_send(struct libnet_BecomeDC_state *s,
2919 struct becomeDC_drsuapi *drsuapi,
2920 struct libnet_BecomeDC_Partition *partition,
2921 void (*recv_fn)(struct tevent_req *subreq));
2922 static void becomeDC_drsuapi2_update_refs_schema_recv(struct tevent_req *subreq);
2924 static void becomeDC_drsuapi3_pull_domain_recv(struct tevent_req *subreq)
2926 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2927 struct libnet_BecomeDC_state);
2928 struct composite_context *c = s->creq;
2929 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2930 struct drsuapi_DsGetNCChanges);
2931 WERROR status;
2933 s->ndr_struct_ptr = NULL;
2935 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2936 TALLOC_FREE(subreq);
2937 if (!composite_is_ok(c)) return;
2939 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part, r);
2940 if (!W_ERROR_IS_OK(status)) {
2941 composite_error(c, werror_to_ntstatus(status));
2942 return;
2945 talloc_free(r);
2947 if (s->domain_part.more_data) {
2948 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2949 becomeDC_drsuapi3_pull_domain_recv);
2950 return;
2953 if (s->critical_only) {
2954 /* Remove the critical and ANC */
2955 s->domain_part.replica_flags ^= DRSUAPI_DRS_CRITICAL_ONLY | DRSUAPI_DRS_GET_ANC;
2956 s->critical_only = false;
2957 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2958 becomeDC_drsuapi3_pull_domain_recv);
2959 return;
2961 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->schema_part,
2962 becomeDC_drsuapi2_update_refs_schema_recv);
2965 static void becomeDC_drsuapi_update_refs_send(struct libnet_BecomeDC_state *s,
2966 struct becomeDC_drsuapi *drsuapi,
2967 struct libnet_BecomeDC_Partition *partition,
2968 void (*recv_fn)(struct tevent_req *subreq))
2970 struct composite_context *c = s->creq;
2971 struct drsuapi_DsReplicaUpdateRefs *r;
2972 const char *ntds_guid_str;
2973 const char *ntds_dns_name;
2974 struct tevent_req *subreq;
2976 r = talloc(s, struct drsuapi_DsReplicaUpdateRefs);
2977 if (composite_nomem(r, c)) return;
2979 ntds_guid_str = GUID_string(r, &s->dest_dsa.ntds_guid);
2980 if (composite_nomem(ntds_guid_str, c)) return;
2982 ntds_dns_name = talloc_asprintf(r, "%s._msdcs.%s",
2983 ntds_guid_str,
2984 s->forest.dns_name);
2985 if (composite_nomem(ntds_dns_name, c)) return;
2987 r->in.bind_handle = &drsuapi->bind_handle;
2988 r->in.level = 1;
2989 r->in.req.req1.naming_context = &partition->nc;
2990 r->in.req.req1.dest_dsa_dns_name= ntds_dns_name;
2991 r->in.req.req1.dest_dsa_guid = s->dest_dsa.ntds_guid;
2992 r->in.req.req1.options = DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_DEL_REF;
2994 /* I think this is how we mark ourselves as a RODC */
2995 if (!lpcfg_parm_bool(s->libnet->lp_ctx, NULL, "repl", "RODC", false)) {
2996 r->in.req.req1.options |= DRSUAPI_DRS_WRIT_REP;
2999 s->ndr_struct_ptr = r;
3000 subreq = dcerpc_drsuapi_DsReplicaUpdateRefs_r_send(s, c->event_ctx,
3001 drsuapi->drsuapi_handle,
3003 if (composite_nomem(subreq, c)) return;
3004 tevent_req_set_callback(subreq, recv_fn, s);
3007 static void becomeDC_drsuapi2_update_refs_config_recv(struct tevent_req *subreq);
3009 static void becomeDC_drsuapi2_update_refs_schema_recv(struct tevent_req *subreq)
3011 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
3012 struct libnet_BecomeDC_state);
3013 struct composite_context *c = s->creq;
3014 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type_abort(s->ndr_struct_ptr,
3015 struct drsuapi_DsReplicaUpdateRefs);
3017 s->ndr_struct_ptr = NULL;
3019 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3020 TALLOC_FREE(subreq);
3021 if (!composite_is_ok(c)) return;
3023 if (!W_ERROR_IS_OK(r->out.result)) {
3024 composite_error(c, werror_to_ntstatus(r->out.result));
3025 return;
3028 talloc_free(r);
3030 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->config_part,
3031 becomeDC_drsuapi2_update_refs_config_recv);
3034 static void becomeDC_drsuapi2_update_refs_domain_recv(struct tevent_req *subreq);
3036 static void becomeDC_drsuapi2_update_refs_config_recv(struct tevent_req *subreq)
3038 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
3039 struct libnet_BecomeDC_state);
3040 struct composite_context *c = s->creq;
3041 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type(s->ndr_struct_ptr,
3042 struct drsuapi_DsReplicaUpdateRefs);
3044 s->ndr_struct_ptr = NULL;
3046 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3047 TALLOC_FREE(subreq);
3048 if (!composite_is_ok(c)) return;
3050 if (!W_ERROR_IS_OK(r->out.result)) {
3051 composite_error(c, werror_to_ntstatus(r->out.result));
3052 return;
3055 talloc_free(r);
3057 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->domain_part,
3058 becomeDC_drsuapi2_update_refs_domain_recv);
3061 static void becomeDC_drsuapi2_update_refs_domain_recv(struct tevent_req *subreq)
3063 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
3064 struct libnet_BecomeDC_state);
3065 struct composite_context *c = s->creq;
3066 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type(s->ndr_struct_ptr,
3067 struct drsuapi_DsReplicaUpdateRefs);
3069 s->ndr_struct_ptr = NULL;
3071 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3072 TALLOC_FREE(subreq);
3073 if (!composite_is_ok(c)) return;
3075 if (!W_ERROR_IS_OK(r->out.result)) {
3076 composite_error(c, werror_to_ntstatus(r->out.result));
3077 return;
3080 talloc_free(r);
3082 /* TODO: use DDNS updates and register dns names */
3083 composite_done(c);
3086 static NTSTATUS becomeDC_ldap2_modify_computer(struct libnet_BecomeDC_state *s)
3088 int ret;
3089 struct ldb_message *msg;
3090 unsigned int i;
3091 uint32_t user_account_control = UF_SERVER_TRUST_ACCOUNT |
3092 UF_TRUSTED_FOR_DELEGATION;
3094 /* as the value is already as we want it to be, we're done */
3095 if (s->dest_dsa.user_account_control == user_account_control) {
3096 return NT_STATUS_OK;
3099 /* make a 'modify' msg, and only for serverReference */
3100 msg = ldb_msg_new(s);
3101 NT_STATUS_HAVE_NO_MEMORY(msg);
3102 msg->dn = ldb_dn_new(msg, s->ldap2.ldb, s->dest_dsa.computer_dn_str);
3103 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
3105 ret = samdb_msg_add_uint(s->ldap2.ldb, msg, msg, "userAccountControl",
3106 user_account_control);
3107 if (ret != LDB_SUCCESS) {
3108 talloc_free(msg);
3109 return NT_STATUS_NO_MEMORY;
3112 /* mark all the message elements (should be just one)
3113 as LDB_FLAG_MOD_REPLACE */
3114 for (i=0;i<msg->num_elements;i++) {
3115 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3118 ret = ldb_modify(s->ldap2.ldb, msg);
3119 talloc_free(msg);
3120 if (ret != LDB_SUCCESS) {
3121 return NT_STATUS_LDAP(ret);
3124 s->dest_dsa.user_account_control = user_account_control;
3126 return NT_STATUS_OK;
3129 static NTSTATUS becomeDC_ldap2_move_computer(struct libnet_BecomeDC_state *s)
3131 int ret;
3132 struct ldb_dn *old_dn;
3133 struct ldb_dn *new_dn;
3135 ret = dsdb_wellknown_dn(s->ldap2.ldb, s,
3136 ldb_get_default_basedn(s->ldap2.ldb),
3137 DS_GUID_DOMAIN_CONTROLLERS_CONTAINER,
3138 &new_dn);
3139 if (ret != LDB_SUCCESS) {
3140 return NT_STATUS_LDAP(ret);
3143 if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) {
3144 talloc_free(new_dn);
3145 return NT_STATUS_NO_MEMORY;
3148 old_dn = ldb_dn_new(new_dn, s->ldap2.ldb, s->dest_dsa.computer_dn_str);
3149 NT_STATUS_HAVE_NO_MEMORY(old_dn);
3151 if (ldb_dn_compare(old_dn, new_dn) == 0) {
3152 /* we don't need to rename if the old and new dn match */
3153 talloc_free(new_dn);
3154 return NT_STATUS_OK;
3157 ret = ldb_rename(s->ldap2.ldb, old_dn, new_dn);
3158 if (ret != LDB_SUCCESS) {
3159 talloc_free(new_dn);
3160 return NT_STATUS_LDAP(ret);
3163 s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn);
3164 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str);
3166 talloc_free(new_dn);
3168 return NT_STATUS_OK;
3171 static void becomeDC_connect_ldap2(struct libnet_BecomeDC_state *s)
3173 struct composite_context *c = s->creq;
3175 c->status = becomeDC_ldap_connect(s, &s->ldap2);
3176 if (!composite_is_ok(c)) return;
3178 c->status = becomeDC_ldap2_modify_computer(s);
3179 if (!composite_is_ok(c)) return;
3181 c->status = becomeDC_ldap2_move_computer(s);
3182 if (!composite_is_ok(c)) return;
3184 s->critical_only = true;
3185 becomeDC_drsuapi3_pull_domain_send(s);
3188 struct composite_context *libnet_BecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3190 struct composite_context *c;
3191 struct libnet_BecomeDC_state *s;
3192 char *tmp_name;
3194 c = composite_create(mem_ctx, ctx->event_ctx);
3195 if (c == NULL) return NULL;
3197 s = talloc_zero(c, struct libnet_BecomeDC_state);
3198 if (composite_nomem(s, c)) return c;
3199 c->private_data = s;
3200 s->creq = c;
3201 s->libnet = ctx;
3203 /* Domain input */
3204 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
3205 if (composite_nomem(s->domain.dns_name, c)) return c;
3206 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
3207 if (composite_nomem(s->domain.netbios_name, c)) return c;
3208 s->domain.sid = dom_sid_dup(s, r->in.domain_sid);
3209 if (composite_nomem(s->domain.sid, c)) return c;
3211 /* Source DSA input */
3212 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
3213 if (composite_nomem(s->source_dsa.address, c)) return c;
3215 /* Destination DSA input */
3216 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
3217 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
3219 /* Destination DSA dns_name construction */
3220 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
3221 if (composite_nomem(tmp_name, c)) return c;
3222 tmp_name = talloc_asprintf_append_buffer(tmp_name, ".%s",s->domain.dns_name);
3223 if (composite_nomem(tmp_name, c)) return c;
3224 s->dest_dsa.dns_name = tmp_name;
3226 /* Callback function pointers */
3227 s->callbacks = r->in.callbacks;
3229 /* RODC join*/
3230 s->rodc_join = r->in.rodc_join;
3232 becomeDC_send_cldap(s);
3233 return c;
3236 NTSTATUS libnet_BecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3238 NTSTATUS status;
3240 status = composite_wait(c);
3242 ZERO_STRUCT(r->out);
3244 talloc_free(c);
3245 return status;
3248 NTSTATUS libnet_BecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3250 NTSTATUS status;
3251 struct composite_context *c;
3252 c = libnet_BecomeDC_send(ctx, mem_ctx, r);
3253 status = libnet_BecomeDC_recv(c, mem_ctx, r);
3254 return status;