s3:smbd: make typedef write_cache private to fileio.c
[Samba.git] / source4 / libnet / libnet_become_dc.c
blob28bb5a1e0ee0ef6385422dc169db033a85910d7f
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 return NT_STATUS_LDAP(ret);
1053 ret = samdb_reference_dn(s->ldap1.ldb, s, basedn, "fSMORoleOwner", &ntds_dn);
1054 if (ret != LDB_SUCCESS) {
1055 talloc_free(basedn);
1056 return NT_STATUS_LDAP(ret);
1059 s->infrastructure_fsmo.ntds_dn_str = ldb_dn_get_linearized(ntds_dn);
1060 NT_STATUS_HAVE_NO_MEMORY(s->infrastructure_fsmo.ntds_dn_str);
1062 server_dn = ldb_dn_get_parent(s, ntds_dn);
1063 NT_STATUS_HAVE_NO_MEMORY(server_dn);
1065 s->infrastructure_fsmo.server_dn_str = ldb_dn_alloc_linearized(s, server_dn);
1066 NT_STATUS_HAVE_NO_MEMORY(s->infrastructure_fsmo.server_dn_str);
1068 ret = ldb_search(s->ldap1.ldb, s, &r, server_dn, LDB_SCOPE_BASE,
1069 dns_attrs, "(objectClass=*)");
1070 if (ret != LDB_SUCCESS) {
1071 return NT_STATUS_LDAP(ret);
1072 } else if (r->count != 1) {
1073 talloc_free(r);
1074 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1077 s->infrastructure_fsmo.dns_name = ldb_msg_find_attr_as_string(r->msgs[0], "dnsHostName", NULL);
1078 if (!s->infrastructure_fsmo.dns_name) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1079 talloc_steal(s, s->infrastructure_fsmo.dns_name);
1081 talloc_free(r);
1083 ret = ldb_search(s->ldap1.ldb, s, &r, ntds_dn, LDB_SCOPE_BASE,
1084 guid_attrs, "(objectClass=*)");
1085 if (ret != LDB_SUCCESS) {
1086 return NT_STATUS_LDAP(ret);
1087 } else if (r->count != 1) {
1088 talloc_free(r);
1089 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1092 s->infrastructure_fsmo.ntds_guid = samdb_result_guid(r->msgs[0], "objectGUID");
1094 talloc_free(r);
1096 return NT_STATUS_OK;
1099 static NTSTATUS becomeDC_ldap1_rid_manager_fsmo(struct libnet_BecomeDC_state *s)
1101 int ret;
1102 struct ldb_result *r;
1103 struct ldb_dn *basedn;
1104 const char *reference_dn_str;
1105 struct ldb_dn *ntds_dn;
1106 struct ldb_dn *server_dn;
1107 static const char *rid_attrs[] = {
1108 "rIDManagerReference",
1109 NULL
1111 static const char *fsmo_attrs[] = {
1112 "fSMORoleOwner",
1113 NULL
1115 static const char *dns_attrs[] = {
1116 "dnsHostName",
1117 NULL
1119 static const char *guid_attrs[] = {
1120 "objectGUID",
1121 NULL
1124 basedn = ldb_dn_new(s, s->ldap1.ldb, s->domain.dn_str);
1125 NT_STATUS_HAVE_NO_MEMORY(basedn);
1127 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1128 rid_attrs, "(objectClass=*)");
1129 talloc_free(basedn);
1130 if (ret != LDB_SUCCESS) {
1131 return NT_STATUS_LDAP(ret);
1132 } else if (r->count != 1) {
1133 talloc_free(r);
1134 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1137 reference_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "rIDManagerReference", NULL);
1138 if (!reference_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1140 basedn = ldb_dn_new(s, s->ldap1.ldb, reference_dn_str);
1141 NT_STATUS_HAVE_NO_MEMORY(basedn);
1143 talloc_free(r);
1145 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1146 fsmo_attrs, "(objectClass=*)");
1147 talloc_free(basedn);
1148 if (ret != LDB_SUCCESS) {
1149 return NT_STATUS_LDAP(ret);
1150 } else if (r->count != 1) {
1151 talloc_free(r);
1152 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1155 s->rid_manager_fsmo.ntds_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "fSMORoleOwner", NULL);
1156 if (!s->rid_manager_fsmo.ntds_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1157 talloc_steal(s, s->rid_manager_fsmo.ntds_dn_str);
1159 talloc_free(r);
1161 ntds_dn = ldb_dn_new(s, s->ldap1.ldb, s->rid_manager_fsmo.ntds_dn_str);
1162 NT_STATUS_HAVE_NO_MEMORY(ntds_dn);
1164 server_dn = ldb_dn_get_parent(s, ntds_dn);
1165 NT_STATUS_HAVE_NO_MEMORY(server_dn);
1167 s->rid_manager_fsmo.server_dn_str = ldb_dn_alloc_linearized(s, server_dn);
1168 NT_STATUS_HAVE_NO_MEMORY(s->rid_manager_fsmo.server_dn_str);
1170 ret = ldb_search(s->ldap1.ldb, s, &r, server_dn, LDB_SCOPE_BASE,
1171 dns_attrs, "(objectClass=*)");
1172 if (ret != LDB_SUCCESS) {
1173 return NT_STATUS_LDAP(ret);
1174 } else if (r->count != 1) {
1175 talloc_free(r);
1176 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1179 s->rid_manager_fsmo.dns_name = ldb_msg_find_attr_as_string(r->msgs[0], "dnsHostName", NULL);
1180 if (!s->rid_manager_fsmo.dns_name) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1181 talloc_steal(s, s->rid_manager_fsmo.dns_name);
1183 talloc_free(r);
1185 ret = ldb_search(s->ldap1.ldb, s, &r, ntds_dn, LDB_SCOPE_BASE,
1186 guid_attrs, "(objectClass=*)");
1187 if (ret != LDB_SUCCESS) {
1188 return NT_STATUS_LDAP(ret);
1189 } else if (r->count != 1) {
1190 talloc_free(r);
1191 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1194 s->rid_manager_fsmo.ntds_guid = samdb_result_guid(r->msgs[0], "objectGUID");
1196 talloc_free(r);
1198 return NT_STATUS_OK;
1201 static NTSTATUS becomeDC_ldap1_site_object(struct libnet_BecomeDC_state *s)
1203 int ret;
1204 struct ldb_result *r;
1205 struct ldb_dn *basedn;
1207 basedn = ldb_dn_new_fmt(s, s->ldap1.ldb, "CN=%s,CN=Sites,%s",
1208 s->dest_dsa.site_name,
1209 s->forest.config_dn_str);
1210 NT_STATUS_HAVE_NO_MEMORY(basedn);
1212 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1213 NULL, "(objectClass=*)");
1214 talloc_free(basedn);
1215 if (ret != LDB_SUCCESS) {
1216 return NT_STATUS_LDAP(ret);
1217 } else if (r->count != 1) {
1218 talloc_free(r);
1219 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1222 s->dest_dsa.site_guid = samdb_result_guid(r->msgs[0], "objectGUID");
1224 talloc_free(r);
1225 return NT_STATUS_OK;
1228 static NTSTATUS becomeDC_check_options(struct libnet_BecomeDC_state *s)
1230 if (!s->callbacks.check_options) return NT_STATUS_OK;
1232 s->_co.domain = &s->domain;
1233 s->_co.forest = &s->forest;
1234 s->_co.source_dsa = &s->source_dsa;
1236 return s->callbacks.check_options(s->callbacks.private_data, &s->_co);
1239 static NTSTATUS becomeDC_ldap1_computer_object(struct libnet_BecomeDC_state *s)
1241 int ret;
1242 struct ldb_result *r;
1243 struct ldb_dn *basedn;
1244 static const char *attrs[] = {
1245 "distinguishedName",
1246 "userAccountControl",
1247 NULL
1250 basedn = ldb_dn_new(s, s->ldap1.ldb, s->domain.dn_str);
1251 NT_STATUS_HAVE_NO_MEMORY(basedn);
1253 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_SUBTREE, attrs,
1254 "(&(|(objectClass=user)(objectClass=computer))(sAMAccountName=%s$))",
1255 s->dest_dsa.netbios_name);
1256 talloc_free(basedn);
1257 if (ret != LDB_SUCCESS) {
1258 return NT_STATUS_LDAP(ret);
1259 } else if (r->count != 1) {
1260 talloc_free(r);
1261 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1264 s->dest_dsa.computer_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "distinguishedName", NULL);
1265 if (!s->dest_dsa.computer_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1266 talloc_steal(s, s->dest_dsa.computer_dn_str);
1268 s->dest_dsa.user_account_control = ldb_msg_find_attr_as_uint(r->msgs[0], "userAccountControl", 0);
1270 talloc_free(r);
1271 return NT_STATUS_OK;
1274 static NTSTATUS becomeDC_ldap1_server_object_1(struct libnet_BecomeDC_state *s)
1276 int ret;
1277 struct ldb_result *r;
1278 struct ldb_dn *basedn;
1279 const char *server_reference_dn_str;
1280 struct ldb_dn *server_reference_dn;
1281 struct ldb_dn *computer_dn;
1283 basedn = ldb_dn_new_fmt(s, s->ldap1.ldb, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
1284 s->dest_dsa.netbios_name,
1285 s->dest_dsa.site_name,
1286 s->forest.config_dn_str);
1287 NT_STATUS_HAVE_NO_MEMORY(basedn);
1289 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1290 NULL, "(objectClass=*)");
1291 talloc_free(basedn);
1292 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1293 /* if the object doesn't exist, we'll create it later */
1294 return NT_STATUS_OK;
1295 } else if (ret != LDB_SUCCESS) {
1296 return NT_STATUS_LDAP(ret);
1297 } else if (r->count != 1) {
1298 talloc_free(r);
1299 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1302 server_reference_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "serverReference", NULL);
1303 if (server_reference_dn_str) {
1304 server_reference_dn = ldb_dn_new(r, s->ldap1.ldb, server_reference_dn_str);
1305 NT_STATUS_HAVE_NO_MEMORY(server_reference_dn);
1307 computer_dn = ldb_dn_new(r, s->ldap1.ldb, s->dest_dsa.computer_dn_str);
1308 NT_STATUS_HAVE_NO_MEMORY(computer_dn);
1311 * if the server object belongs to another DC in another domain
1312 * in the forest, we should not touch this object!
1314 if (ldb_dn_compare(computer_dn, server_reference_dn) != 0) {
1315 talloc_free(r);
1316 return NT_STATUS_OBJECT_NAME_COLLISION;
1320 /* if the server object is already for the dest_dsa, then we don't need to create it */
1321 s->dest_dsa.server_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "distinguishedName", NULL);
1322 if (!s->dest_dsa.server_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
1323 talloc_steal(s, s->dest_dsa.server_dn_str);
1325 talloc_free(r);
1326 return NT_STATUS_OK;
1329 static NTSTATUS becomeDC_ldap1_server_object_2(struct libnet_BecomeDC_state *s)
1331 int ret;
1332 struct ldb_result *r;
1333 struct ldb_dn *basedn;
1334 const char *server_reference_bl_dn_str;
1335 static const char *attrs[] = {
1336 "serverReferenceBL",
1337 NULL
1340 /* if the server_dn_str has a valid value, we skip this lookup */
1341 if (s->dest_dsa.server_dn_str) return NT_STATUS_OK;
1343 basedn = ldb_dn_new(s, s->ldap1.ldb, s->dest_dsa.computer_dn_str);
1344 NT_STATUS_HAVE_NO_MEMORY(basedn);
1346 ret = ldb_search(s->ldap1.ldb, s, &r, basedn, LDB_SCOPE_BASE,
1347 attrs, "(objectClass=*)");
1348 talloc_free(basedn);
1349 if (ret != LDB_SUCCESS) {
1350 return NT_STATUS_LDAP(ret);
1351 } else if (r->count != 1) {
1352 talloc_free(r);
1353 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1356 server_reference_bl_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "serverReferenceBL", NULL);
1357 if (!server_reference_bl_dn_str) {
1358 /* if no back link is present, we're done for this function */
1359 talloc_free(r);
1360 return NT_STATUS_OK;
1363 /* if the server object is already for the dest_dsa, then we don't need to create it */
1364 s->dest_dsa.server_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "serverReferenceBL", NULL);
1365 if (s->dest_dsa.server_dn_str) {
1366 /* if a back link is present, we know that the server object is present */
1367 talloc_steal(s, s->dest_dsa.server_dn_str);
1370 talloc_free(r);
1371 return NT_STATUS_OK;
1374 static NTSTATUS becomeDC_ldap1_server_object_add(struct libnet_BecomeDC_state *s)
1376 int ret;
1377 struct ldb_message *msg;
1378 char *server_dn_str;
1380 /* if the server_dn_str has a valid value, we skip this lookup */
1381 if (s->dest_dsa.server_dn_str) return NT_STATUS_OK;
1383 msg = ldb_msg_new(s);
1384 NT_STATUS_HAVE_NO_MEMORY(msg);
1386 msg->dn = ldb_dn_new_fmt(msg, s->ldap1.ldb, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
1387 s->dest_dsa.netbios_name,
1388 s->dest_dsa.site_name,
1389 s->forest.config_dn_str);
1390 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
1392 ret = ldb_msg_add_string(msg, "objectClass", "server");
1393 if (ret != 0) {
1394 talloc_free(msg);
1395 return NT_STATUS_NO_MEMORY;
1397 ret = ldb_msg_add_string(msg, "systemFlags", "50000000");
1398 if (ret != 0) {
1399 talloc_free(msg);
1400 return NT_STATUS_NO_MEMORY;
1402 ret = ldb_msg_add_string(msg, "serverReference", s->dest_dsa.computer_dn_str);
1403 if (ret != 0) {
1404 talloc_free(msg);
1405 return NT_STATUS_NO_MEMORY;
1408 server_dn_str = ldb_dn_alloc_linearized(s, msg->dn);
1409 NT_STATUS_HAVE_NO_MEMORY(server_dn_str);
1411 ret = ldb_add(s->ldap1.ldb, msg);
1412 talloc_free(msg);
1413 if (ret != LDB_SUCCESS) {
1414 talloc_free(server_dn_str);
1415 return NT_STATUS_LDAP(ret);
1418 s->dest_dsa.server_dn_str = server_dn_str;
1420 return NT_STATUS_OK;
1423 static NTSTATUS becomeDC_ldap1_server_object_modify(struct libnet_BecomeDC_state *s)
1425 int ret;
1426 struct ldb_message *msg;
1427 unsigned int i;
1429 /* make a 'modify' msg, and only for serverReference */
1430 msg = ldb_msg_new(s);
1431 NT_STATUS_HAVE_NO_MEMORY(msg);
1432 msg->dn = ldb_dn_new(msg, s->ldap1.ldb, s->dest_dsa.server_dn_str);
1433 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
1435 ret = ldb_msg_add_string(msg, "serverReference", s->dest_dsa.computer_dn_str);
1436 if (ret != 0) {
1437 talloc_free(msg);
1438 return NT_STATUS_NO_MEMORY;
1441 /* mark all the message elements (should be just one)
1442 as LDB_FLAG_MOD_ADD */
1443 for (i=0;i<msg->num_elements;i++) {
1444 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
1447 ret = ldb_modify(s->ldap1.ldb, msg);
1448 if (ret == LDB_SUCCESS) {
1449 talloc_free(msg);
1450 return NT_STATUS_OK;
1451 } else if (ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
1452 /* retry with LDB_FLAG_MOD_REPLACE */
1453 } else {
1454 talloc_free(msg);
1455 return NT_STATUS_LDAP(ret);
1458 /* mark all the message elements (should be just one)
1459 as LDB_FLAG_MOD_REPLACE */
1460 for (i=0;i<msg->num_elements;i++) {
1461 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1464 ret = ldb_modify(s->ldap1.ldb, msg);
1465 talloc_free(msg);
1466 if (ret != LDB_SUCCESS) {
1467 return NT_STATUS_LDAP(ret);
1470 return NT_STATUS_OK;
1473 static void becomeDC_drsuapi_connect_send(struct libnet_BecomeDC_state *s,
1474 struct becomeDC_drsuapi *drsuapi,
1475 void (*recv_fn)(struct composite_context *req));
1476 static void becomeDC_drsuapi1_connect_recv(struct composite_context *req);
1477 static void becomeDC_connect_ldap2(struct libnet_BecomeDC_state *s);
1479 static void becomeDC_connect_ldap1(struct libnet_BecomeDC_state *s)
1481 struct composite_context *c = s->creq;
1483 c->status = becomeDC_ldap_connect(s, &s->ldap1);
1484 if (!composite_is_ok(c)) return;
1486 c->status = becomeDC_ldap1_rootdse(s);
1487 if (!composite_is_ok(c)) return;
1489 c->status = becomeDC_ldap1_crossref_behavior_version(s);
1490 if (!composite_is_ok(c)) return;
1492 c->status = becomeDC_ldap1_domain_behavior_version(s);
1493 if (!composite_is_ok(c)) return;
1495 c->status = becomeDC_ldap1_schema_object_version(s);
1496 if (!composite_is_ok(c)) return;
1498 c->status = becomeDC_ldap1_w2k3_update_revision(s);
1499 if (!composite_is_ok(c)) return;
1501 c->status = becomeDC_ldap1_infrastructure_fsmo(s);
1502 if (!composite_is_ok(c)) return;
1504 c->status = becomeDC_ldap1_rid_manager_fsmo(s);
1505 if (!composite_is_ok(c)) return;
1507 c->status = becomeDC_ldap1_site_object(s);
1508 if (!composite_is_ok(c)) return;
1510 c->status = becomeDC_check_options(s);
1511 if (!composite_is_ok(c)) return;
1513 c->status = becomeDC_ldap1_computer_object(s);
1514 if (!composite_is_ok(c)) return;
1516 c->status = becomeDC_ldap1_server_object_1(s);
1517 if (!composite_is_ok(c)) return;
1519 c->status = becomeDC_ldap1_server_object_2(s);
1520 if (!composite_is_ok(c)) return;
1522 c->status = becomeDC_ldap1_server_object_add(s);
1523 if (!composite_is_ok(c)) return;
1525 c->status = becomeDC_ldap1_server_object_modify(s);
1526 if (!composite_is_ok(c)) return;
1528 becomeDC_drsuapi_connect_send(s, &s->drsuapi1, becomeDC_drsuapi1_connect_recv);
1531 static void becomeDC_drsuapi_connect_send(struct libnet_BecomeDC_state *s,
1532 struct becomeDC_drsuapi *drsuapi,
1533 void (*recv_fn)(struct composite_context *req))
1535 struct composite_context *c = s->creq;
1536 struct composite_context *creq;
1537 char *binding_str;
1539 drsuapi->s = s;
1541 if (!drsuapi->binding) {
1542 const char *krb5_str = "";
1543 const char *print_str = "";
1545 * Note: Replication only works with Windows 2000 when 'krb5' is
1546 * passed as auth_type here. If NTLMSSP is used, Windows
1547 * 2000 returns garbage in the DsGetNCChanges() response
1548 * if encrypted password attributes would be in the
1549 * response. That means the replication of the schema and
1550 * configuration partition works fine, but it fails for
1551 * the domain partition.
1553 if (lpcfg_parm_bool(s->libnet->lp_ctx, NULL, "become_dc",
1554 "force krb5", true))
1556 krb5_str = "krb5,";
1558 if (lpcfg_parm_bool(s->libnet->lp_ctx, NULL, "become_dc",
1559 "print", false))
1561 print_str = "print,";
1563 binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[%s%sseal]",
1564 s->source_dsa.dns_name,
1565 krb5_str, print_str);
1566 if (composite_nomem(binding_str, c)) return;
1567 c->status = dcerpc_parse_binding(s, binding_str, &drsuapi->binding);
1568 talloc_free(binding_str);
1569 if (!composite_is_ok(c)) return;
1572 if (DEBUGLEVEL >= 10) {
1573 drsuapi->binding->flags |= DCERPC_DEBUG_PRINT_BOTH;
1576 creq = dcerpc_pipe_connect_b_send(s, drsuapi->binding, &ndr_table_drsuapi,
1577 s->libnet->cred, s->libnet->event_ctx,
1578 s->libnet->lp_ctx);
1579 composite_continue(c, creq, recv_fn, s);
1582 static void becomeDC_drsuapi_bind_send(struct libnet_BecomeDC_state *s,
1583 struct becomeDC_drsuapi *drsuapi,
1584 void (*recv_fn)(struct tevent_req *subreq));
1585 static void becomeDC_drsuapi1_bind_recv(struct tevent_req *subreq);
1587 static void becomeDC_drsuapi1_connect_recv(struct composite_context *req)
1589 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
1590 struct libnet_BecomeDC_state);
1591 struct composite_context *c = s->creq;
1593 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi1.pipe);
1594 if (!composite_is_ok(c)) return;
1596 s->drsuapi1.drsuapi_handle = s->drsuapi1.pipe->binding_handle;
1598 c->status = gensec_session_key(s->drsuapi1.pipe->conn->security_state.generic_state,
1600 &s->drsuapi1.gensec_skey);
1601 if (!composite_is_ok(c)) return;
1603 becomeDC_drsuapi_bind_send(s, &s->drsuapi1, becomeDC_drsuapi1_bind_recv);
1606 static void becomeDC_drsuapi_bind_send(struct libnet_BecomeDC_state *s,
1607 struct becomeDC_drsuapi *drsuapi,
1608 void (*recv_fn)(struct tevent_req *subreq))
1610 struct composite_context *c = s->creq;
1611 struct drsuapi_DsBindInfo28 *bind_info28;
1612 struct tevent_req *subreq;
1614 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &drsuapi->bind_guid);
1616 bind_info28 = &drsuapi->local_info28;
1617 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
1618 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
1619 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
1620 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
1621 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
1622 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
1623 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
1624 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
1625 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
1626 if (s->domain.behavior_version >= DS_DOMAIN_FUNCTION_2003) {
1627 /* TODO: find out how this is really triggered! */
1628 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
1630 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
1631 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
1632 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
1633 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
1634 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
1635 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
1636 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
1637 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
1638 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
1639 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
1640 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
1641 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
1642 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
1643 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
1644 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
1645 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
1646 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
1647 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
1648 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
1649 #if 0 /* we don't support XPRESS compression yet */
1650 bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
1651 #endif
1652 bind_info28->site_guid = s->dest_dsa.site_guid;
1653 bind_info28->pid = 0;
1654 bind_info28->repl_epoch = 0;
1656 drsuapi->bind_info_ctr.length = 28;
1657 drsuapi->bind_info_ctr.info.info28 = *bind_info28;
1659 drsuapi->bind_r.in.bind_guid = &drsuapi->bind_guid;
1660 drsuapi->bind_r.in.bind_info = &drsuapi->bind_info_ctr;
1661 drsuapi->bind_r.out.bind_handle = &drsuapi->bind_handle;
1663 subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
1664 drsuapi->drsuapi_handle,
1665 &drsuapi->bind_r);
1666 if (composite_nomem(subreq, c)) return;
1667 tevent_req_set_callback(subreq, recv_fn, s);
1670 static WERROR becomeDC_drsuapi_bind_recv(struct libnet_BecomeDC_state *s,
1671 struct becomeDC_drsuapi *drsuapi)
1673 if (!W_ERROR_IS_OK(drsuapi->bind_r.out.result)) {
1674 return drsuapi->bind_r.out.result;
1677 ZERO_STRUCT(drsuapi->remote_info28);
1678 if (drsuapi->bind_r.out.bind_info) {
1679 switch (drsuapi->bind_r.out.bind_info->length) {
1680 case 24: {
1681 struct drsuapi_DsBindInfo24 *info24;
1682 info24 = &drsuapi->bind_r.out.bind_info->info.info24;
1683 drsuapi->remote_info28.supported_extensions = info24->supported_extensions;
1684 drsuapi->remote_info28.site_guid = info24->site_guid;
1685 drsuapi->remote_info28.pid = info24->pid;
1686 drsuapi->remote_info28.repl_epoch = 0;
1687 break;
1689 case 48: {
1690 struct drsuapi_DsBindInfo48 *info48;
1691 info48 = &drsuapi->bind_r.out.bind_info->info.info48;
1692 drsuapi->remote_info28.supported_extensions = info48->supported_extensions;
1693 drsuapi->remote_info28.site_guid = info48->site_guid;
1694 drsuapi->remote_info28.pid = info48->pid;
1695 drsuapi->remote_info28.repl_epoch = info48->repl_epoch;
1696 break;
1698 case 28:
1699 drsuapi->remote_info28 = drsuapi->bind_r.out.bind_info->info.info28;
1700 break;
1704 return WERR_OK;
1707 static void becomeDC_drsuapi1_add_entry_send(struct libnet_BecomeDC_state *s);
1709 static void becomeDC_drsuapi1_bind_recv(struct tevent_req *subreq)
1711 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
1712 struct libnet_BecomeDC_state);
1713 struct composite_context *c = s->creq;
1714 WERROR status;
1716 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
1717 TALLOC_FREE(subreq);
1718 if (!composite_is_ok(c)) return;
1720 status = becomeDC_drsuapi_bind_recv(s, &s->drsuapi1);
1721 if (!W_ERROR_IS_OK(status)) {
1722 composite_error(c, werror_to_ntstatus(status));
1723 return;
1726 becomeDC_drsuapi1_add_entry_send(s);
1729 static void becomeDC_drsuapi1_add_entry_recv(struct tevent_req *subreq);
1731 static void becomeDC_drsuapi1_add_entry_send(struct libnet_BecomeDC_state *s)
1733 struct composite_context *c = s->creq;
1734 struct drsuapi_DsAddEntry *r;
1735 struct drsuapi_DsReplicaObjectIdentifier *identifier;
1736 uint32_t num_attrs, i = 0;
1737 struct drsuapi_DsReplicaAttribute *attrs;
1738 enum ndr_err_code ndr_err;
1739 bool w2k3;
1740 struct tevent_req *subreq;
1742 /* choose a random invocationId */
1743 s->dest_dsa.invocation_id = GUID_random();
1746 * if the schema version indicates w2k3, then also send some w2k3
1747 * specific attributes.
1749 if (s->forest.schema_object_version >= 30) {
1750 w2k3 = true;
1751 } else {
1752 w2k3 = false;
1755 r = talloc_zero(s, struct drsuapi_DsAddEntry);
1756 if (composite_nomem(r, c)) return;
1758 /* setup identifier */
1759 identifier = talloc(r, struct drsuapi_DsReplicaObjectIdentifier);
1760 if (composite_nomem(identifier, c)) return;
1761 identifier->guid = GUID_zero();
1762 identifier->sid = s->zero_sid;
1763 identifier->dn = talloc_asprintf(identifier, "CN=NTDS Settings,%s",
1764 s->dest_dsa.server_dn_str);
1765 if (composite_nomem(identifier->dn, c)) return;
1767 /* allocate attribute array */
1768 num_attrs = 12;
1769 attrs = talloc_array(r, struct drsuapi_DsReplicaAttribute, num_attrs);
1770 if (composite_nomem(attrs, c)) return;
1772 /* ntSecurityDescriptor */
1774 struct drsuapi_DsAttributeValue *vs;
1775 DATA_BLOB *vd;
1776 struct security_descriptor *v;
1777 struct dom_sid *domain_admins_sid;
1778 const char *domain_admins_sid_str;
1780 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1781 if (composite_nomem(vs, c)) return;
1783 vd = talloc_array(vs, DATA_BLOB, 1);
1784 if (composite_nomem(vd, c)) return;
1786 domain_admins_sid = dom_sid_add_rid(vs, s->domain.sid, DOMAIN_RID_ADMINS);
1787 if (composite_nomem(domain_admins_sid, c)) return;
1789 domain_admins_sid_str = dom_sid_string(domain_admins_sid, domain_admins_sid);
1790 if (composite_nomem(domain_admins_sid_str, c)) return;
1792 v = security_descriptor_dacl_create(vd,
1794 /* owner: domain admins */
1795 domain_admins_sid_str,
1796 /* owner group: domain admins */
1797 domain_admins_sid_str,
1798 /* authenticated users */
1799 SID_NT_AUTHENTICATED_USERS,
1800 SEC_ACE_TYPE_ACCESS_ALLOWED,
1801 SEC_STD_READ_CONTROL |
1802 SEC_ADS_LIST |
1803 SEC_ADS_READ_PROP |
1804 SEC_ADS_LIST_OBJECT,
1806 /* domain admins */
1807 domain_admins_sid_str,
1808 SEC_ACE_TYPE_ACCESS_ALLOWED,
1809 SEC_STD_REQUIRED |
1810 SEC_ADS_CREATE_CHILD |
1811 SEC_ADS_LIST |
1812 SEC_ADS_SELF_WRITE |
1813 SEC_ADS_READ_PROP |
1814 SEC_ADS_WRITE_PROP |
1815 SEC_ADS_DELETE_TREE |
1816 SEC_ADS_LIST_OBJECT |
1817 SEC_ADS_CONTROL_ACCESS,
1819 /* system */
1820 SID_NT_SYSTEM,
1821 SEC_ACE_TYPE_ACCESS_ALLOWED,
1822 SEC_STD_REQUIRED |
1823 SEC_ADS_CREATE_CHILD |
1824 SEC_ADS_DELETE_CHILD |
1825 SEC_ADS_LIST |
1826 SEC_ADS_SELF_WRITE |
1827 SEC_ADS_READ_PROP |
1828 SEC_ADS_WRITE_PROP |
1829 SEC_ADS_DELETE_TREE |
1830 SEC_ADS_LIST_OBJECT |
1831 SEC_ADS_CONTROL_ACCESS,
1833 /* end */
1834 NULL);
1835 if (composite_nomem(v, c)) return;
1837 ndr_err = ndr_push_struct_blob(&vd[0], vd, v,
1838 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
1839 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1840 c->status = ndr_map_error2ntstatus(ndr_err);
1841 if (!composite_is_ok(c)) return;
1844 vs[0].blob = &vd[0];
1846 attrs[i].attid = DRSUAPI_ATTID_ntSecurityDescriptor;
1847 attrs[i].value_ctr.num_values = 1;
1848 attrs[i].value_ctr.values = vs;
1850 i++;
1853 /* objectClass: nTDSDSA */
1855 struct drsuapi_DsAttributeValue *vs;
1856 DATA_BLOB *vd;
1858 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1859 if (composite_nomem(vs, c)) return;
1861 vd = talloc_array(vs, DATA_BLOB, 1);
1862 if (composite_nomem(vd, c)) return;
1864 vd[0] = data_blob_talloc(vd, NULL, 4);
1865 if (composite_nomem(vd[0].data, c)) return;
1867 /* value for nTDSDSA */
1868 SIVAL(vd[0].data, 0, 0x0017002F);
1870 vs[0].blob = &vd[0];
1872 attrs[i].attid = DRSUAPI_ATTID_objectClass;
1873 attrs[i].value_ctr.num_values = 1;
1874 attrs[i].value_ctr.values = vs;
1876 i++;
1879 /* objectCategory: CN=NTDS-DSA,CN=Schema,... or CN=NTDS-DSA-RO,CN=Schema,... */
1881 struct drsuapi_DsAttributeValue *vs;
1882 DATA_BLOB *vd;
1883 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
1885 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1886 if (composite_nomem(vs, c)) return;
1888 vd = talloc_array(vs, DATA_BLOB, 1);
1889 if (composite_nomem(vd, c)) return;
1891 v[0].guid = GUID_zero();
1892 v[0].sid = s->zero_sid;
1894 if (s->rodc_join) {
1895 v[0].dn = talloc_asprintf(vd, "CN=NTDS-DSA-RO,%s",
1896 s->forest.schema_dn_str);
1897 } else {
1898 v[0].dn = talloc_asprintf(vd, "CN=NTDS-DSA,%s",
1899 s->forest.schema_dn_str);
1901 if (composite_nomem(v[0].dn, c)) return;
1903 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
1904 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1905 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1906 c->status = ndr_map_error2ntstatus(ndr_err);
1907 if (!composite_is_ok(c)) return;
1910 vs[0].blob = &vd[0];
1912 attrs[i].attid = DRSUAPI_ATTID_objectCategory;
1913 attrs[i].value_ctr.num_values = 1;
1914 attrs[i].value_ctr.values = vs;
1916 i++;
1919 /* invocationId: random guid */
1921 struct drsuapi_DsAttributeValue *vs;
1922 DATA_BLOB *vd;
1923 const struct GUID *v;
1925 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
1926 if (composite_nomem(vs, c)) return;
1928 vd = talloc_array(vs, DATA_BLOB, 1);
1929 if (composite_nomem(vd, c)) return;
1931 v = &s->dest_dsa.invocation_id;
1933 c->status = GUID_to_ndr_blob(v, vd, &vd[0]);
1934 if (!composite_is_ok(c)) return;
1936 vs[0].blob = &vd[0];
1938 attrs[i].attid = DRSUAPI_ATTID_invocationId;
1939 attrs[i].value_ctr.num_values = 1;
1940 attrs[i].value_ctr.values = vs;
1942 i++;
1945 /* hasMasterNCs: ... */
1947 struct drsuapi_DsAttributeValue *vs;
1948 DATA_BLOB *vd;
1949 struct drsuapi_DsReplicaObjectIdentifier3 v[3];
1951 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 3);
1952 if (composite_nomem(vs, c)) return;
1954 vd = talloc_array(vs, DATA_BLOB, 3);
1955 if (composite_nomem(vd, c)) return;
1957 v[0].guid = GUID_zero();
1958 v[0].sid = s->zero_sid;
1959 v[0].dn = s->forest.config_dn_str;
1961 v[1].guid = GUID_zero();
1962 v[1].sid = s->zero_sid;
1963 v[1].dn = s->domain.dn_str;
1965 v[2].guid = GUID_zero();
1966 v[2].sid = s->zero_sid;
1967 v[2].dn = s->forest.schema_dn_str;
1969 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
1970 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1971 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1972 c->status = ndr_map_error2ntstatus(ndr_err);
1973 if (!composite_is_ok(c)) return;
1976 ndr_err = ndr_push_struct_blob(&vd[1], vd, &v[1],
1977 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1978 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1979 c->status = ndr_map_error2ntstatus(ndr_err);
1980 if (!composite_is_ok(c)) return;
1983 ndr_err = ndr_push_struct_blob(&vd[2], vd, &v[2],
1984 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1985 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1986 c->status = ndr_map_error2ntstatus(ndr_err);
1987 if (!composite_is_ok(c)) return;
1990 vs[0].blob = &vd[0];
1991 vs[1].blob = &vd[1];
1992 vs[2].blob = &vd[2];
1994 attrs[i].attid = DRSUAPI_ATTID_hasMasterNCs;
1995 attrs[i].value_ctr.num_values = 3;
1996 attrs[i].value_ctr.values = vs;
1998 i++;
2001 /* msDS-hasMasterNCs: ... */
2002 if (w2k3) {
2003 struct drsuapi_DsAttributeValue *vs;
2004 DATA_BLOB *vd;
2005 struct drsuapi_DsReplicaObjectIdentifier3 v[3];
2007 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 3);
2008 if (composite_nomem(vs, c)) return;
2010 vd = talloc_array(vs, DATA_BLOB, 3);
2011 if (composite_nomem(vd, c)) return;
2013 v[0].guid = GUID_zero();
2014 v[0].sid = s->zero_sid;
2015 v[0].dn = s->forest.config_dn_str;
2017 v[1].guid = GUID_zero();
2018 v[1].sid = s->zero_sid;
2019 v[1].dn = s->domain.dn_str;
2021 v[2].guid = GUID_zero();
2022 v[2].sid = s->zero_sid;
2023 v[2].dn = s->forest.schema_dn_str;
2025 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2026 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2027 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2028 c->status = ndr_map_error2ntstatus(ndr_err);
2029 if (!composite_is_ok(c)) return;
2032 ndr_err = ndr_push_struct_blob(&vd[1], vd, &v[1],
2033 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2034 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2035 c->status = ndr_map_error2ntstatus(ndr_err);
2036 if (!composite_is_ok(c)) return;
2039 ndr_err = ndr_push_struct_blob(&vd[2], vd, &v[2],
2040 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2041 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2042 c->status = ndr_map_error2ntstatus(ndr_err);
2043 if (!composite_is_ok(c)) return;
2046 vs[0].blob = &vd[0];
2047 vs[1].blob = &vd[1];
2048 vs[2].blob = &vd[2];
2050 attrs[i].attid = DRSUAPI_ATTID_msDS_hasMasterNCs;
2051 attrs[i].value_ctr.num_values = 3;
2052 attrs[i].value_ctr.values = vs;
2054 i++;
2057 /* dMDLocation: CN=Schema,... */
2059 struct drsuapi_DsAttributeValue *vs;
2060 DATA_BLOB *vd;
2061 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2063 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2064 if (composite_nomem(vs, c)) return;
2066 vd = talloc_array(vs, DATA_BLOB, 1);
2067 if (composite_nomem(vd, c)) return;
2069 v[0].guid = GUID_zero();
2070 v[0].sid = s->zero_sid;
2071 v[0].dn = s->forest.schema_dn_str;
2073 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2074 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2075 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2076 c->status = ndr_map_error2ntstatus(ndr_err);
2077 if (!composite_is_ok(c)) return;
2080 vs[0].blob = &vd[0];
2082 attrs[i].attid = DRSUAPI_ATTID_dMDLocation;
2083 attrs[i].value_ctr.num_values = 1;
2084 attrs[i].value_ctr.values = vs;
2086 i++;
2089 /* msDS-HasDomainNCs: <domain_partition> */
2090 if (w2k3) {
2091 struct drsuapi_DsAttributeValue *vs;
2092 DATA_BLOB *vd;
2093 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2095 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2096 if (composite_nomem(vs, c)) return;
2098 vd = talloc_array(vs, DATA_BLOB, 1);
2099 if (composite_nomem(vd, c)) return;
2101 v[0].guid = GUID_zero();
2102 v[0].sid = s->zero_sid;
2103 v[0].dn = s->domain.dn_str;
2105 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2106 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2107 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2108 c->status = ndr_map_error2ntstatus(ndr_err);
2109 if (!composite_is_ok(c)) return;
2112 vs[0].blob = &vd[0];
2114 attrs[i].attid = DRSUAPI_ATTID_msDS_HasDomainNCs;
2115 attrs[i].value_ctr.num_values = 1;
2116 attrs[i].value_ctr.values = vs;
2118 i++;
2121 /* msDS-Behavior-Version */
2122 if (w2k3) {
2123 struct drsuapi_DsAttributeValue *vs;
2124 DATA_BLOB *vd;
2126 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2127 if (composite_nomem(vs, c)) return;
2129 vd = talloc_array(vs, DATA_BLOB, 1);
2130 if (composite_nomem(vd, c)) return;
2132 vd[0] = data_blob_talloc(vd, NULL, 4);
2133 if (composite_nomem(vd[0].data, c)) return;
2135 SIVAL(vd[0].data, 0, get_dc_function_level(s->libnet->lp_ctx));
2137 vs[0].blob = &vd[0];
2139 attrs[i].attid = DRSUAPI_ATTID_msDS_Behavior_Version;
2140 attrs[i].value_ctr.num_values = 1;
2141 attrs[i].value_ctr.values = vs;
2143 i++;
2146 /* systemFlags */
2148 struct drsuapi_DsAttributeValue *vs;
2149 DATA_BLOB *vd;
2151 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2152 if (composite_nomem(vs, c)) return;
2154 vd = talloc_array(vs, DATA_BLOB, 1);
2155 if (composite_nomem(vd, c)) return;
2157 vd[0] = data_blob_talloc(vd, NULL, 4);
2158 if (composite_nomem(vd[0].data, c)) return;
2160 if (s->rodc_join) {
2161 SIVAL(vd[0].data, 0, SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
2162 } else {
2163 SIVAL(vd[0].data, 0, SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
2166 vs[0].blob = &vd[0];
2168 attrs[i].attid = DRSUAPI_ATTID_systemFlags;
2169 attrs[i].value_ctr.num_values = 1;
2170 attrs[i].value_ctr.values = vs;
2172 i++;
2175 /* serverReference: ... */
2177 struct drsuapi_DsAttributeValue *vs;
2178 DATA_BLOB *vd;
2179 struct drsuapi_DsReplicaObjectIdentifier3 v[1];
2181 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2182 if (composite_nomem(vs, c)) return;
2184 vd = talloc_array(vs, DATA_BLOB, 1);
2185 if (composite_nomem(vd, c)) return;
2187 v[0].guid = GUID_zero();
2188 v[0].sid = s->zero_sid;
2189 v[0].dn = s->dest_dsa.computer_dn_str;
2191 ndr_err = ndr_push_struct_blob(&vd[0], vd, &v[0],
2192 (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
2193 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2194 c->status = ndr_map_error2ntstatus(ndr_err);
2195 if (!composite_is_ok(c)) return;
2198 vs[0].blob = &vd[0];
2200 attrs[i].attid = DRSUAPI_ATTID_serverReference;
2201 attrs[i].value_ctr.num_values = 1;
2202 attrs[i].value_ctr.values = vs;
2204 i++;
2207 /* options:... */
2208 if (s->rodc_join) {
2209 struct drsuapi_DsAttributeValue *vs;
2210 DATA_BLOB *vd;
2212 vs = talloc_array(attrs, struct drsuapi_DsAttributeValue, 1);
2213 if (composite_nomem(vs, c)) return;
2215 vd = talloc_array(vs, DATA_BLOB, 1);
2216 if (composite_nomem(vd, c)) return;
2218 vd[0] = data_blob_talloc(vd, NULL, 4);
2219 if (composite_nomem(vd[0].data, c)) return;
2221 SIVAL(vd[0].data, 0, DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL);
2223 vs[0].blob = &vd[0];
2225 attrs[i].attid = DRSUAPI_ATTID_options;
2226 attrs[i].value_ctr.num_values = 1;
2227 attrs[i].value_ctr.values = vs;
2229 i++;
2232 /* truncate the attribute list to the attribute count we have filled in */
2233 num_attrs = i;
2235 /* setup request structure */
2236 r->in.bind_handle = &s->drsuapi1.bind_handle;
2237 r->in.level = 2;
2238 r->in.req = talloc(s, union drsuapi_DsAddEntryRequest);
2239 r->in.req->req2.first_object.next_object = NULL;
2240 r->in.req->req2.first_object.object.identifier = identifier;
2241 r->in.req->req2.first_object.object.flags = 0x00000000;
2242 r->in.req->req2.first_object.object.attribute_ctr.num_attributes= num_attrs;
2243 r->in.req->req2.first_object.object.attribute_ctr.attributes = attrs;
2245 r->out.level_out = talloc(s, uint32_t);
2246 r->out.ctr = talloc(s, union drsuapi_DsAddEntryCtr);
2248 s->ndr_struct_ptr = r;
2249 subreq = dcerpc_drsuapi_DsAddEntry_r_send(s, c->event_ctx,
2250 s->drsuapi1.drsuapi_handle, r);
2251 if (composite_nomem(subreq, c)) return;
2252 tevent_req_set_callback(subreq, becomeDC_drsuapi1_add_entry_recv, s);
2255 static void becomeDC_drsuapi2_connect_recv(struct composite_context *req);
2256 static NTSTATUS becomeDC_prepare_db(struct libnet_BecomeDC_state *s);
2258 static void becomeDC_drsuapi1_add_entry_recv(struct tevent_req *subreq)
2260 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2261 struct libnet_BecomeDC_state);
2262 struct composite_context *c = s->creq;
2263 struct drsuapi_DsAddEntry *r = talloc_get_type_abort(s->ndr_struct_ptr,
2264 struct drsuapi_DsAddEntry);
2265 char *binding_str;
2267 s->ndr_struct_ptr = NULL;
2269 c->status = dcerpc_drsuapi_DsAddEntry_r_recv(subreq, r);
2270 TALLOC_FREE(subreq);
2271 if (!composite_is_ok(c)) return;
2273 if (!W_ERROR_IS_OK(r->out.result)) {
2274 composite_error(c, werror_to_ntstatus(r->out.result));
2275 return;
2278 if (*r->out.level_out == 3) {
2279 WERROR status;
2280 union drsuapi_DsAddEntry_ErrData *err_data = r->out.ctr->ctr3.err_data;
2282 /* check for errors */
2283 status = err_data ? err_data->v1.status : WERR_OK;
2284 if (!W_ERROR_IS_OK(status)) {
2285 struct drsuapi_DsAddEntryErrorInfo_Attr_V1 *attr_err;
2286 struct drsuapi_DsAddEntry_AttrErrListItem_V1 *attr_err_li;
2287 struct drsuapi_DsAddEntryErrorInfo_Name_V1 *name_err;
2288 struct drsuapi_DsAddEntryErrorInfo_Referr_V1 *ref_err;
2289 struct drsuapi_DsAddEntry_RefErrListItem_V1 *ref_li;
2291 if (r->out.ctr->ctr3.err_ver != 1) {
2292 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2293 return;
2296 DEBUG(0,("DsAddEntry (R3) of '%s' failed: "
2297 "Errors: dir_err = %d, status = %s;\n",
2298 r->in.req->req3.first_object.object.identifier->dn,
2299 err_data->v1.dir_err,
2300 win_errstr(err_data->v1.status)));
2302 if (!err_data->v1.info) {
2303 DEBUG(0, ("DsAddEntry (R3): no error info returned!\n"));
2304 composite_error(c, werror_to_ntstatus(status));
2305 return;
2308 /* dump more detailed error */
2309 switch (err_data->v1.dir_err) {
2310 case DRSUAPI_DIRERR_ATTRIBUTE:
2311 /* Dump attribute errors */
2312 attr_err = &err_data->v1.info->attr_err;
2313 DEBUGADD(0,(" Attribute Error: object = %s, count = %d;\n",
2314 attr_err->id->dn,
2315 attr_err->count));
2316 attr_err_li = &attr_err->first;
2317 for (; attr_err_li; attr_err_li = attr_err_li->next) {
2318 struct drsuapi_DsAddEntry_AttrErr_V1 *err = &attr_err_li->err_data;
2319 DEBUGADD(0,(" Error: err = %s, problem = 0x%08X, attid = 0x%08X;\n",
2320 win_errstr(err->extended_err),
2321 err->problem,
2322 err->attid));
2323 /* TODO: should we print attribute value here? */
2325 break;
2326 case DRSUAPI_DIRERR_NAME:
2327 /* Dump Name resolution error */
2328 name_err = &err_data->v1.info->name_err;
2329 DEBUGADD(0,(" Name Error: err = %s, problem = 0x%08X, id_matched = %s;\n",
2330 win_errstr(name_err->extended_err),
2331 name_err->problem,
2332 name_err->id_matched->dn));
2333 break;
2334 case DRSUAPI_DIRERR_REFERRAL:
2335 /* Dump Referral errors */
2336 ref_err = &err_data->v1.info->referral_err;
2337 DEBUGADD(0,(" Referral Error: extended_err = %s\n",
2338 win_errstr(ref_err->extended_err)));
2339 ref_li = &ref_err->refer;
2340 for (; ref_li; ref_li = ref_li->next) {
2341 struct drsuapi_DsaAddressListItem_V1 *addr;
2342 DEBUGADD(0,(" Referral: id_target = %s, ref_type = 0x%04X,",
2343 ref_li->id_target->dn,
2344 ref_li->ref_type));
2345 if (ref_li->is_choice_set) {
2346 DEBUGADD(0,(" choice = 0x%02X, ",
2347 ref_li->choice));
2349 DEBUGADD(0,(" add_list ("));
2350 for (addr = ref_li->addr_list; addr; addr = addr->next) {
2351 DEBUGADD(0,("%s", addr->address->string));
2352 if (addr->next) {
2353 DEBUGADD(0,(", "));
2356 DEBUGADD(0,(");\n"));
2358 break;
2359 case DRSUAPI_DIRERR_SECURITY:
2360 /* Dump Security error. */
2361 DEBUGADD(0,(" Security Error: extended_err = %s, problem = 0x%08X\n",
2362 win_errstr(err_data->v1.info->security_err.extended_err),
2363 err_data->v1.info->security_err.problem));
2364 break;
2365 case DRSUAPI_DIRERR_SERVICE:
2366 /* Dump Service error. */
2367 DEBUGADD(0,(" Service Error: extended_err = %s, problem = 0x%08X\n",
2368 win_errstr(err_data->v1.info->service_err.extended_err),
2369 err_data->v1.info->service_err.problem));
2370 break;
2371 case DRSUAPI_DIRERR_UPDATE:
2372 /* Dump Update error. */
2373 DEBUGADD(0,(" Update Error: extended_err = %s, problem = 0x%08X\n",
2374 win_errstr(err_data->v1.info->update_err.extended_err),
2375 err_data->v1.info->update_err.problem));
2376 break;
2377 case DRSUAPI_DIRERR_SYSTEM:
2378 /* System error. */
2379 DEBUGADD(0,(" System Error: extended_err = %s, problem = 0x%08X\n",
2380 win_errstr(err_data->v1.info->system_err.extended_err),
2381 err_data->v1.info->system_err.problem));
2382 break;
2383 case DRSUAPI_DIRERR_OK: /* mute compiler warnings */
2384 default:
2385 DEBUGADD(0,(" Unknown DIRERR error class returned!\n"));
2386 break;
2389 composite_error(c, werror_to_ntstatus(status));
2390 return;
2393 if (1 != r->out.ctr->ctr3.count) {
2394 DEBUG(0,("DsAddEntry - Ctr3: something very wrong had happened - "
2395 "method succeeded but objects returned are %d (expected 1).\n",
2396 r->out.ctr->ctr3.count));
2397 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2398 return;
2401 s->dest_dsa.ntds_guid = r->out.ctr->ctr3.objects[0].guid;
2403 } else if (*r->out.level_out == 2) {
2404 if (DRSUAPI_DIRERR_OK != r->out.ctr->ctr2.dir_err) {
2405 DEBUG(0,("DsAddEntry failed with: dir_err = %d, extended_err = %s\n",
2406 r->out.ctr->ctr2.dir_err,
2407 win_errstr(r->out.ctr->ctr2.extended_err)));
2408 composite_error(c, werror_to_ntstatus(r->out.ctr->ctr2.extended_err));
2409 return;
2412 if (1 != r->out.ctr->ctr2.count) {
2413 DEBUG(0,("DsAddEntry: something very wrong had happened - "
2414 "method succeeded but objects returned are %d (expected 1). "
2415 "Errors: dir_err = %d, extended_err = %s\n",
2416 r->out.ctr->ctr2.count,
2417 r->out.ctr->ctr2.dir_err,
2418 win_errstr(r->out.ctr->ctr2.extended_err)));
2419 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2420 return;
2423 s->dest_dsa.ntds_guid = r->out.ctr->ctr2.objects[0].guid;
2424 } else {
2425 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
2426 return;
2429 talloc_free(r);
2431 s->dest_dsa.ntds_dn_str = talloc_asprintf(s, "CN=NTDS Settings,%s",
2432 s->dest_dsa.server_dn_str);
2433 if (composite_nomem(s->dest_dsa.ntds_dn_str, c)) return;
2435 c->status = becomeDC_prepare_db(s);
2436 if (!composite_is_ok(c)) return;
2438 /* this avoids the epmapper lookup on the 2nd connection */
2439 binding_str = dcerpc_binding_string(s, s->drsuapi1.binding);
2440 if (composite_nomem(binding_str, c)) return;
2442 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi2.binding);
2443 talloc_free(binding_str);
2444 if (!composite_is_ok(c)) return;
2446 if (DEBUGLEVEL >= 10) {
2447 s->drsuapi2.binding->flags |= DCERPC_DEBUG_PRINT_BOTH;
2450 /* w2k3 uses the same assoc_group_id as on the first connection, so we do */
2451 s->drsuapi2.binding->assoc_group_id = s->drsuapi1.pipe->assoc_group_id;
2453 becomeDC_drsuapi_connect_send(s, &s->drsuapi2, becomeDC_drsuapi2_connect_recv);
2456 static NTSTATUS becomeDC_prepare_db(struct libnet_BecomeDC_state *s)
2458 if (!s->callbacks.prepare_db) return NT_STATUS_OK;
2460 s->_pp.domain = &s->domain;
2461 s->_pp.forest = &s->forest;
2462 s->_pp.source_dsa = &s->source_dsa;
2463 s->_pp.dest_dsa = &s->dest_dsa;
2465 return s->callbacks.prepare_db(s->callbacks.private_data, &s->_pp);
2468 static void becomeDC_drsuapi2_bind_recv(struct tevent_req *subreq);
2470 static void becomeDC_drsuapi2_connect_recv(struct composite_context *req)
2472 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
2473 struct libnet_BecomeDC_state);
2474 struct composite_context *c = s->creq;
2476 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi2.pipe);
2477 if (!composite_is_ok(c)) return;
2479 s->drsuapi2.drsuapi_handle = s->drsuapi2.pipe->binding_handle;
2481 c->status = gensec_session_key(s->drsuapi2.pipe->conn->security_state.generic_state,
2483 &s->drsuapi2.gensec_skey);
2484 if (!composite_is_ok(c)) return;
2486 becomeDC_drsuapi_bind_send(s, &s->drsuapi2, becomeDC_drsuapi2_bind_recv);
2489 static void becomeDC_drsuapi3_connect_recv(struct composite_context *req);
2491 static void becomeDC_drsuapi2_bind_recv(struct tevent_req *subreq)
2493 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2494 struct libnet_BecomeDC_state);
2495 struct composite_context *c = s->creq;
2496 char *binding_str;
2497 WERROR status;
2499 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
2500 TALLOC_FREE(subreq);
2501 if (!composite_is_ok(c)) return;
2503 status = becomeDC_drsuapi_bind_recv(s, &s->drsuapi2);
2504 if (!W_ERROR_IS_OK(status)) {
2505 composite_error(c, werror_to_ntstatus(status));
2506 return;
2509 /* this avoids the epmapper lookup on the 3rd connection */
2510 binding_str = dcerpc_binding_string(s, s->drsuapi1.binding);
2511 if (composite_nomem(binding_str, c)) return;
2513 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi3.binding);
2514 talloc_free(binding_str);
2515 if (!composite_is_ok(c)) return;
2517 if (DEBUGLEVEL >= 10) {
2518 s->drsuapi3.binding->flags |= DCERPC_DEBUG_PRINT_BOTH;
2521 /* w2k3 uses the same assoc_group_id as on the first connection, so we do */
2522 s->drsuapi3.binding->assoc_group_id = s->drsuapi1.pipe->assoc_group_id;
2523 /* w2k3 uses the concurrent multiplex feature on the 3rd connection, so we do */
2524 s->drsuapi3.binding->flags |= DCERPC_CONCURRENT_MULTIPLEX;
2526 becomeDC_drsuapi_connect_send(s, &s->drsuapi3, becomeDC_drsuapi3_connect_recv);
2529 static void becomeDC_drsuapi3_pull_schema_send(struct libnet_BecomeDC_state *s);
2531 static void becomeDC_drsuapi3_connect_recv(struct composite_context *req)
2533 struct libnet_BecomeDC_state *s = talloc_get_type(req->async.private_data,
2534 struct libnet_BecomeDC_state);
2535 struct composite_context *c = s->creq;
2537 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi3.pipe);
2538 if (!composite_is_ok(c)) return;
2540 s->drsuapi3.drsuapi_handle = s->drsuapi3.pipe->binding_handle;
2542 c->status = gensec_session_key(s->drsuapi3.pipe->conn->security_state.generic_state,
2544 &s->drsuapi3.gensec_skey);
2545 if (!composite_is_ok(c)) return;
2547 becomeDC_drsuapi3_pull_schema_send(s);
2550 static void becomeDC_drsuapi_pull_partition_send(struct libnet_BecomeDC_state *s,
2551 struct becomeDC_drsuapi *drsuapi_h,
2552 struct becomeDC_drsuapi *drsuapi_p,
2553 struct libnet_BecomeDC_Partition *partition,
2554 void (*recv_fn)(struct tevent_req *subreq))
2556 struct composite_context *c = s->creq;
2557 struct drsuapi_DsGetNCChanges *r;
2558 struct tevent_req *subreq;
2560 r = talloc(s, struct drsuapi_DsGetNCChanges);
2561 if (composite_nomem(r, c)) return;
2563 r->out.level_out = talloc(r, uint32_t);
2564 if (composite_nomem(r->out.level_out, c)) return;
2565 r->in.req = talloc(r, union drsuapi_DsGetNCChangesRequest);
2566 if (composite_nomem(r->in.req, c)) return;
2567 r->out.ctr = talloc(r, union drsuapi_DsGetNCChangesCtr);
2568 if (composite_nomem(r->out.ctr, c)) return;
2570 r->in.bind_handle = &drsuapi_h->bind_handle;
2571 if (drsuapi_h->remote_info28.supported_extensions & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8) {
2572 r->in.level = 8;
2573 r->in.req->req8.destination_dsa_guid = partition->destination_dsa_guid;
2574 r->in.req->req8.source_dsa_invocation_id= partition->source_dsa_invocation_id;
2575 r->in.req->req8.naming_context = &partition->nc;
2576 r->in.req->req8.highwatermark = partition->highwatermark;
2577 r->in.req->req8.uptodateness_vector = NULL;
2578 r->in.req->req8.replica_flags = partition->replica_flags;
2579 r->in.req->req8.max_object_count = 133;
2580 r->in.req->req8.max_ndr_size = 1336811;
2581 r->in.req->req8.extended_op = DRSUAPI_EXOP_NONE;
2582 r->in.req->req8.fsmo_info = 0;
2583 r->in.req->req8.partial_attribute_set = NULL;
2584 r->in.req->req8.partial_attribute_set_ex= NULL;
2585 r->in.req->req8.mapping_ctr.num_mappings= 0;
2586 r->in.req->req8.mapping_ctr.mappings = NULL;
2587 } else {
2588 r->in.level = 5;
2589 r->in.req->req5.destination_dsa_guid = partition->destination_dsa_guid;
2590 r->in.req->req5.source_dsa_invocation_id= partition->source_dsa_invocation_id;
2591 r->in.req->req5.naming_context = &partition->nc;
2592 r->in.req->req5.highwatermark = partition->highwatermark;
2593 r->in.req->req5.uptodateness_vector = NULL;
2594 r->in.req->req5.replica_flags = partition->replica_flags;
2595 r->in.req->req5.max_object_count = 133;
2596 r->in.req->req5.max_ndr_size = 1336770;
2597 r->in.req->req5.extended_op = DRSUAPI_EXOP_NONE;
2598 r->in.req->req5.fsmo_info = 0;
2602 * we should try to use the drsuapi_p->pipe here, as w2k3 does
2603 * but it seems that some extra flags in the DCERPC Bind call
2604 * are needed for it. Or the same KRB5 TGS is needed on both
2605 * connections.
2607 s->ndr_struct_ptr = r;
2608 subreq = dcerpc_drsuapi_DsGetNCChanges_r_send(s, c->event_ctx,
2609 drsuapi_p->drsuapi_handle,
2611 if (composite_nomem(subreq, c)) return;
2612 tevent_req_set_callback(subreq, recv_fn, s);
2615 static WERROR becomeDC_drsuapi_pull_partition_recv(struct libnet_BecomeDC_state *s,
2616 struct becomeDC_drsuapi *drsuapi_h,
2617 struct becomeDC_drsuapi *drsuapi_p,
2618 struct libnet_BecomeDC_Partition *partition,
2619 struct drsuapi_DsGetNCChanges *r)
2621 uint32_t req_level = 0;
2622 struct drsuapi_DsGetNCChangesRequest5 *req5 = NULL;
2623 struct drsuapi_DsGetNCChangesRequest8 *req8 = NULL;
2624 struct drsuapi_DsGetNCChangesRequest10 *req10 = NULL;
2625 uint32_t ctr_level = 0;
2626 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
2627 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
2628 struct GUID *source_dsa_guid = NULL;
2629 struct GUID *source_dsa_invocation_id = NULL;
2630 struct drsuapi_DsReplicaHighWaterMark *new_highwatermark = NULL;
2631 bool more_data = false;
2632 NTSTATUS nt_status;
2634 if (!W_ERROR_IS_OK(r->out.result)) {
2635 return r->out.result;
2638 switch (r->in.level) {
2639 case 0:
2640 /* none */
2641 break;
2642 case 5:
2643 req5 = &r->in.req->req5;
2644 break;
2645 case 8:
2646 req8 = &r->in.req->req8;
2647 break;
2648 case 10:
2649 req10 = &r->in.req->req10;
2650 break;
2651 default:
2652 return WERR_INVALID_PARAMETER;
2655 if (*r->out.level_out == 1) {
2656 ctr_level = 1;
2657 ctr1 = &r->out.ctr->ctr1;
2658 } else if (*r->out.level_out == 2 &&
2659 r->out.ctr->ctr2.mszip1.ts) {
2660 ctr_level = 1;
2661 ctr1 = &r->out.ctr->ctr2.mszip1.ts->ctr1;
2662 } else if (*r->out.level_out == 6) {
2663 ctr_level = 6;
2664 ctr6 = &r->out.ctr->ctr6;
2665 } else if (*r->out.level_out == 7 &&
2666 r->out.ctr->ctr7.level == 6 &&
2667 r->out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP &&
2668 r->out.ctr->ctr7.ctr.mszip6.ts) {
2669 ctr_level = 6;
2670 ctr6 = &r->out.ctr->ctr7.ctr.mszip6.ts->ctr6;
2671 } else if (*r->out.level_out == 7 &&
2672 r->out.ctr->ctr7.level == 6 &&
2673 r->out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS &&
2674 r->out.ctr->ctr7.ctr.xpress6.ts) {
2675 ctr_level = 6;
2676 ctr6 = &r->out.ctr->ctr7.ctr.xpress6.ts->ctr6;
2677 } else {
2678 return WERR_BAD_NET_RESP;
2681 if (!ctr1 && ! ctr6) {
2682 return WERR_BAD_NET_RESP;
2685 if (ctr_level == 6) {
2686 if (!W_ERROR_IS_OK(ctr6->drs_error)) {
2687 return ctr6->drs_error;
2691 switch (ctr_level) {
2692 case 1:
2693 source_dsa_guid = &ctr1->source_dsa_guid;
2694 source_dsa_invocation_id = &ctr1->source_dsa_invocation_id;
2695 new_highwatermark = &ctr1->new_highwatermark;
2696 more_data = ctr1->more_data;
2697 break;
2698 case 6:
2699 source_dsa_guid = &ctr6->source_dsa_guid;
2700 source_dsa_invocation_id = &ctr6->source_dsa_invocation_id;
2701 new_highwatermark = &ctr6->new_highwatermark;
2702 more_data = ctr6->more_data;
2703 break;
2706 partition->highwatermark = *new_highwatermark;
2707 partition->source_dsa_guid = *source_dsa_guid;
2708 partition->source_dsa_invocation_id = *source_dsa_invocation_id;
2709 partition->more_data = more_data;
2711 if (!partition->store_chunk) return WERR_OK;
2713 s->_sc.domain = &s->domain;
2714 s->_sc.forest = &s->forest;
2715 s->_sc.source_dsa = &s->source_dsa;
2716 s->_sc.dest_dsa = &s->dest_dsa;
2717 s->_sc.partition = partition;
2718 s->_sc.req_level = req_level;
2719 s->_sc.req5 = req5;
2720 s->_sc.req8 = req8;
2721 s->_sc.req10 = req10;
2722 s->_sc.ctr_level = ctr_level;
2723 s->_sc.ctr1 = ctr1;
2724 s->_sc.ctr6 = ctr6;
2726 * we need to use the drsuapi_p->gensec_skey here,
2727 * when we use drsuapi_p->pipe in the for this request
2729 s->_sc.gensec_skey = &drsuapi_p->gensec_skey;
2731 nt_status = partition->store_chunk(s->callbacks.private_data, &s->_sc);
2732 if (!NT_STATUS_IS_OK(nt_status)) {
2733 return ntstatus_to_werror(nt_status);
2736 return WERR_OK;
2739 static void becomeDC_drsuapi3_pull_schema_recv(struct tevent_req *subreq);
2741 static void becomeDC_drsuapi3_pull_schema_send(struct libnet_BecomeDC_state *s)
2743 s->schema_part.nc.guid = GUID_zero();
2744 s->schema_part.nc.sid = s->zero_sid;
2745 s->schema_part.nc.dn = s->forest.schema_dn_str;
2747 s->schema_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2749 s->schema_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2750 | DRSUAPI_DRS_INIT_SYNC
2751 | DRSUAPI_DRS_PER_SYNC
2752 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2753 | DRSUAPI_DRS_NEVER_SYNCED
2754 | DRSUAPI_DRS_USE_COMPRESSION;
2755 if (s->rodc_join) {
2756 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2759 s->schema_part.store_chunk = s->callbacks.schema_chunk;
2761 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part,
2762 becomeDC_drsuapi3_pull_schema_recv);
2765 static void becomeDC_drsuapi3_pull_config_send(struct libnet_BecomeDC_state *s);
2767 static void becomeDC_drsuapi3_pull_schema_recv(struct tevent_req *subreq)
2769 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2770 struct libnet_BecomeDC_state);
2771 struct composite_context *c = s->creq;
2772 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2773 struct drsuapi_DsGetNCChanges);
2774 WERROR status;
2776 s->ndr_struct_ptr = NULL;
2778 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2779 TALLOC_FREE(subreq);
2780 if (!composite_is_ok(c)) return;
2782 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part, r);
2783 if (!W_ERROR_IS_OK(status)) {
2784 composite_error(c, werror_to_ntstatus(status));
2785 return;
2788 talloc_free(r);
2790 if (s->schema_part.more_data) {
2791 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->schema_part,
2792 becomeDC_drsuapi3_pull_schema_recv);
2793 return;
2796 becomeDC_drsuapi3_pull_config_send(s);
2799 static void becomeDC_drsuapi3_pull_config_recv(struct tevent_req *subreq);
2801 static void becomeDC_drsuapi3_pull_config_send(struct libnet_BecomeDC_state *s)
2803 s->config_part.nc.guid = GUID_zero();
2804 s->config_part.nc.sid = s->zero_sid;
2805 s->config_part.nc.dn = s->forest.config_dn_str;
2807 s->config_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2809 s->config_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2810 | DRSUAPI_DRS_INIT_SYNC
2811 | DRSUAPI_DRS_PER_SYNC
2812 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2813 | DRSUAPI_DRS_NEVER_SYNCED
2814 | DRSUAPI_DRS_USE_COMPRESSION;
2815 if (s->rodc_join) {
2816 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2819 s->config_part.store_chunk = s->callbacks.config_chunk;
2821 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->config_part,
2822 becomeDC_drsuapi3_pull_config_recv);
2825 static void becomeDC_drsuapi3_pull_config_recv(struct tevent_req *subreq)
2827 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2828 struct libnet_BecomeDC_state);
2829 struct composite_context *c = s->creq;
2830 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2831 struct drsuapi_DsGetNCChanges);
2832 WERROR status;
2834 s->ndr_struct_ptr = NULL;
2836 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2837 TALLOC_FREE(subreq);
2838 if (!composite_is_ok(c)) return;
2840 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->config_part, r);
2841 if (!W_ERROR_IS_OK(status)) {
2842 composite_error(c, werror_to_ntstatus(status));
2843 return;
2846 talloc_free(r);
2848 if (s->config_part.more_data) {
2849 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->config_part,
2850 becomeDC_drsuapi3_pull_config_recv);
2851 return;
2854 becomeDC_connect_ldap2(s);
2857 static void becomeDC_drsuapi3_pull_domain_recv(struct tevent_req *subreq);
2859 static void becomeDC_drsuapi3_pull_domain_send(struct libnet_BecomeDC_state *s)
2861 s->domain_part.nc.guid = GUID_zero();
2862 s->domain_part.nc.sid = s->zero_sid;
2863 s->domain_part.nc.dn = s->domain.dn_str;
2865 s->domain_part.destination_dsa_guid = s->drsuapi2.bind_guid;
2867 s->domain_part.replica_flags = DRSUAPI_DRS_WRIT_REP
2868 | DRSUAPI_DRS_INIT_SYNC
2869 | DRSUAPI_DRS_PER_SYNC
2870 | DRSUAPI_DRS_FULL_SYNC_IN_PROGRESS
2871 | DRSUAPI_DRS_NEVER_SYNCED
2872 | DRSUAPI_DRS_USE_COMPRESSION;
2873 if (s->critical_only) {
2874 s->domain_part.replica_flags |= DRSUAPI_DRS_CRITICAL_ONLY | DRSUAPI_DRS_GET_ANC;
2876 if (s->rodc_join) {
2877 s->schema_part.replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2880 s->domain_part.store_chunk = s->callbacks.domain_chunk;
2882 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2883 becomeDC_drsuapi3_pull_domain_recv);
2886 static void becomeDC_drsuapi_update_refs_send(struct libnet_BecomeDC_state *s,
2887 struct becomeDC_drsuapi *drsuapi,
2888 struct libnet_BecomeDC_Partition *partition,
2889 void (*recv_fn)(struct tevent_req *subreq));
2890 static void becomeDC_drsuapi2_update_refs_schema_recv(struct tevent_req *subreq);
2892 static void becomeDC_drsuapi3_pull_domain_recv(struct tevent_req *subreq)
2894 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2895 struct libnet_BecomeDC_state);
2896 struct composite_context *c = s->creq;
2897 struct drsuapi_DsGetNCChanges *r = talloc_get_type_abort(s->ndr_struct_ptr,
2898 struct drsuapi_DsGetNCChanges);
2899 WERROR status;
2901 s->ndr_struct_ptr = NULL;
2903 c->status = dcerpc_drsuapi_DsGetNCChanges_r_recv(subreq, r);
2904 TALLOC_FREE(subreq);
2905 if (!composite_is_ok(c)) return;
2907 status = becomeDC_drsuapi_pull_partition_recv(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part, r);
2908 if (!W_ERROR_IS_OK(status)) {
2909 composite_error(c, werror_to_ntstatus(status));
2910 return;
2913 talloc_free(r);
2915 if (s->domain_part.more_data) {
2916 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2917 becomeDC_drsuapi3_pull_domain_recv);
2918 return;
2921 if (s->critical_only) {
2922 /* Remove the critical and ANC */
2923 s->domain_part.replica_flags ^= DRSUAPI_DRS_CRITICAL_ONLY | DRSUAPI_DRS_GET_ANC;
2924 s->critical_only = false;
2925 becomeDC_drsuapi_pull_partition_send(s, &s->drsuapi2, &s->drsuapi3, &s->domain_part,
2926 becomeDC_drsuapi3_pull_domain_recv);
2927 return;
2929 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->schema_part,
2930 becomeDC_drsuapi2_update_refs_schema_recv);
2933 static void becomeDC_drsuapi_update_refs_send(struct libnet_BecomeDC_state *s,
2934 struct becomeDC_drsuapi *drsuapi,
2935 struct libnet_BecomeDC_Partition *partition,
2936 void (*recv_fn)(struct tevent_req *subreq))
2938 struct composite_context *c = s->creq;
2939 struct drsuapi_DsReplicaUpdateRefs *r;
2940 const char *ntds_guid_str;
2941 const char *ntds_dns_name;
2942 struct tevent_req *subreq;
2944 r = talloc(s, struct drsuapi_DsReplicaUpdateRefs);
2945 if (composite_nomem(r, c)) return;
2947 ntds_guid_str = GUID_string(r, &s->dest_dsa.ntds_guid);
2948 if (composite_nomem(ntds_guid_str, c)) return;
2950 ntds_dns_name = talloc_asprintf(r, "%s._msdcs.%s",
2951 ntds_guid_str,
2952 s->forest.dns_name);
2953 if (composite_nomem(ntds_dns_name, c)) return;
2955 r->in.bind_handle = &drsuapi->bind_handle;
2956 r->in.level = 1;
2957 r->in.req.req1.naming_context = &partition->nc;
2958 r->in.req.req1.dest_dsa_dns_name= ntds_dns_name;
2959 r->in.req.req1.dest_dsa_guid = s->dest_dsa.ntds_guid;
2960 r->in.req.req1.options = DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_DEL_REF;
2962 /* I think this is how we mark ourselves as a RODC */
2963 if (!lpcfg_parm_bool(s->libnet->lp_ctx, NULL, "repl", "RODC", false)) {
2964 r->in.req.req1.options |= DRSUAPI_DRS_WRIT_REP;
2967 s->ndr_struct_ptr = r;
2968 subreq = dcerpc_drsuapi_DsReplicaUpdateRefs_r_send(s, c->event_ctx,
2969 drsuapi->drsuapi_handle,
2971 if (composite_nomem(subreq, c)) return;
2972 tevent_req_set_callback(subreq, recv_fn, s);
2975 static void becomeDC_drsuapi2_update_refs_config_recv(struct tevent_req *subreq);
2977 static void becomeDC_drsuapi2_update_refs_schema_recv(struct tevent_req *subreq)
2979 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
2980 struct libnet_BecomeDC_state);
2981 struct composite_context *c = s->creq;
2982 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type_abort(s->ndr_struct_ptr,
2983 struct drsuapi_DsReplicaUpdateRefs);
2985 s->ndr_struct_ptr = NULL;
2987 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
2988 TALLOC_FREE(subreq);
2989 if (!composite_is_ok(c)) return;
2991 if (!W_ERROR_IS_OK(r->out.result)) {
2992 composite_error(c, werror_to_ntstatus(r->out.result));
2993 return;
2996 talloc_free(r);
2998 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->config_part,
2999 becomeDC_drsuapi2_update_refs_config_recv);
3002 static void becomeDC_drsuapi2_update_refs_domain_recv(struct tevent_req *subreq);
3004 static void becomeDC_drsuapi2_update_refs_config_recv(struct tevent_req *subreq)
3006 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
3007 struct libnet_BecomeDC_state);
3008 struct composite_context *c = s->creq;
3009 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type(s->ndr_struct_ptr,
3010 struct drsuapi_DsReplicaUpdateRefs);
3012 s->ndr_struct_ptr = NULL;
3014 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3015 TALLOC_FREE(subreq);
3016 if (!composite_is_ok(c)) return;
3018 if (!W_ERROR_IS_OK(r->out.result)) {
3019 composite_error(c, werror_to_ntstatus(r->out.result));
3020 return;
3023 talloc_free(r);
3025 becomeDC_drsuapi_update_refs_send(s, &s->drsuapi2, &s->domain_part,
3026 becomeDC_drsuapi2_update_refs_domain_recv);
3029 static void becomeDC_drsuapi2_update_refs_domain_recv(struct tevent_req *subreq)
3031 struct libnet_BecomeDC_state *s = tevent_req_callback_data(subreq,
3032 struct libnet_BecomeDC_state);
3033 struct composite_context *c = s->creq;
3034 struct drsuapi_DsReplicaUpdateRefs *r = talloc_get_type(s->ndr_struct_ptr,
3035 struct drsuapi_DsReplicaUpdateRefs);
3037 s->ndr_struct_ptr = NULL;
3039 c->status = dcerpc_drsuapi_DsReplicaUpdateRefs_r_recv(subreq, r);
3040 TALLOC_FREE(subreq);
3041 if (!composite_is_ok(c)) return;
3043 if (!W_ERROR_IS_OK(r->out.result)) {
3044 composite_error(c, werror_to_ntstatus(r->out.result));
3045 return;
3048 talloc_free(r);
3050 /* TODO: use DDNS updates and register dns names */
3051 composite_done(c);
3054 static NTSTATUS becomeDC_ldap2_modify_computer(struct libnet_BecomeDC_state *s)
3056 int ret;
3057 struct ldb_message *msg;
3058 unsigned int i;
3059 uint32_t user_account_control = UF_SERVER_TRUST_ACCOUNT |
3060 UF_TRUSTED_FOR_DELEGATION;
3062 /* as the value is already as we want it to be, we're done */
3063 if (s->dest_dsa.user_account_control == user_account_control) {
3064 return NT_STATUS_OK;
3067 /* make a 'modify' msg, and only for serverReference */
3068 msg = ldb_msg_new(s);
3069 NT_STATUS_HAVE_NO_MEMORY(msg);
3070 msg->dn = ldb_dn_new(msg, s->ldap2.ldb, s->dest_dsa.computer_dn_str);
3071 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
3073 ret = samdb_msg_add_uint(s->ldap2.ldb, msg, msg, "userAccountControl",
3074 user_account_control);
3075 if (ret != LDB_SUCCESS) {
3076 talloc_free(msg);
3077 return NT_STATUS_NO_MEMORY;
3080 /* mark all the message elements (should be just one)
3081 as LDB_FLAG_MOD_REPLACE */
3082 for (i=0;i<msg->num_elements;i++) {
3083 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3086 ret = ldb_modify(s->ldap2.ldb, msg);
3087 talloc_free(msg);
3088 if (ret != LDB_SUCCESS) {
3089 return NT_STATUS_LDAP(ret);
3092 s->dest_dsa.user_account_control = user_account_control;
3094 return NT_STATUS_OK;
3097 static NTSTATUS becomeDC_ldap2_move_computer(struct libnet_BecomeDC_state *s)
3099 int ret;
3100 struct ldb_dn *old_dn;
3101 struct ldb_dn *new_dn;
3103 ret = dsdb_wellknown_dn(s->ldap2.ldb, s,
3104 ldb_get_default_basedn(s->ldap2.ldb),
3105 DS_GUID_DOMAIN_CONTROLLERS_CONTAINER,
3106 &new_dn);
3107 if (ret != LDB_SUCCESS) {
3108 return NT_STATUS_LDAP(ret);
3111 if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) {
3112 talloc_free(new_dn);
3113 return NT_STATUS_NO_MEMORY;
3116 old_dn = ldb_dn_new(new_dn, s->ldap2.ldb, s->dest_dsa.computer_dn_str);
3117 NT_STATUS_HAVE_NO_MEMORY(old_dn);
3119 if (ldb_dn_compare(old_dn, new_dn) == 0) {
3120 /* we don't need to rename if the old and new dn match */
3121 talloc_free(new_dn);
3122 return NT_STATUS_OK;
3125 ret = ldb_rename(s->ldap2.ldb, old_dn, new_dn);
3126 if (ret != LDB_SUCCESS) {
3127 talloc_free(new_dn);
3128 return NT_STATUS_LDAP(ret);
3131 s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn);
3132 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str);
3134 talloc_free(new_dn);
3136 return NT_STATUS_OK;
3139 static void becomeDC_connect_ldap2(struct libnet_BecomeDC_state *s)
3141 struct composite_context *c = s->creq;
3143 c->status = becomeDC_ldap_connect(s, &s->ldap2);
3144 if (!composite_is_ok(c)) return;
3146 c->status = becomeDC_ldap2_modify_computer(s);
3147 if (!composite_is_ok(c)) return;
3149 c->status = becomeDC_ldap2_move_computer(s);
3150 if (!composite_is_ok(c)) return;
3152 s->critical_only = true;
3153 becomeDC_drsuapi3_pull_domain_send(s);
3156 struct composite_context *libnet_BecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3158 struct composite_context *c;
3159 struct libnet_BecomeDC_state *s;
3160 char *tmp_name;
3162 c = composite_create(mem_ctx, ctx->event_ctx);
3163 if (c == NULL) return NULL;
3165 s = talloc_zero(c, struct libnet_BecomeDC_state);
3166 if (composite_nomem(s, c)) return c;
3167 c->private_data = s;
3168 s->creq = c;
3169 s->libnet = ctx;
3171 /* Domain input */
3172 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
3173 if (composite_nomem(s->domain.dns_name, c)) return c;
3174 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
3175 if (composite_nomem(s->domain.netbios_name, c)) return c;
3176 s->domain.sid = dom_sid_dup(s, r->in.domain_sid);
3177 if (composite_nomem(s->domain.sid, c)) return c;
3179 /* Source DSA input */
3180 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
3181 if (composite_nomem(s->source_dsa.address, c)) return c;
3183 /* Destination DSA input */
3184 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
3185 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
3187 /* Destination DSA dns_name construction */
3188 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
3189 if (composite_nomem(tmp_name, c)) return c;
3190 tmp_name = talloc_asprintf_append_buffer(tmp_name, ".%s",s->domain.dns_name);
3191 if (composite_nomem(tmp_name, c)) return c;
3192 s->dest_dsa.dns_name = tmp_name;
3194 /* Callback function pointers */
3195 s->callbacks = r->in.callbacks;
3197 /* RODC join*/
3198 s->rodc_join = r->in.rodc_join;
3200 becomeDC_send_cldap(s);
3201 return c;
3204 NTSTATUS libnet_BecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3206 NTSTATUS status;
3208 status = composite_wait(c);
3210 ZERO_STRUCT(r->out);
3212 talloc_free(c);
3213 return status;
3216 NTSTATUS libnet_BecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_BecomeDC *r)
3218 NTSTATUS status;
3219 struct composite_context *c;
3220 c = libnet_BecomeDC_send(ctx, mem_ctx, r);
3221 status = libnet_BecomeDC_recv(c, mem_ctx, r);
3222 return status;