2 Unix SMB/CIFS implementation.
4 utility code to join/leave a domain
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 this code is used by other torture modules to join/leave a domain
24 as either a member, bdc or thru a trust relationship
28 #include "system/time.h"
29 #include "../lib/crypto/crypto.h"
30 #include "libnet/libnet.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
34 #include "libcli/auth/libcli_auth.h"
35 #include "torture/rpc/rpc.h"
36 #include "libcli/security/security.h"
37 #include "param/param.h"
40 struct dcerpc_pipe
*p
;
41 struct policy_handle user_handle
;
42 struct libnet_JoinDomain
*libnet_r
;
43 struct dom_sid
*dom_sid
;
44 const char *dom_netbios_name
;
45 const char *dom_dns_name
;
46 struct dom_sid
*user_sid
;
47 struct GUID user_guid
;
48 const char *netbios_name
;
52 static NTSTATUS
DeleteUser_byname(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
53 struct policy_handle
*handle
, const char *name
)
56 struct samr_DeleteUser d
;
57 struct policy_handle user_handle
;
59 struct samr_LookupNames n
;
60 struct samr_Ids rids
, types
;
61 struct lsa_String sname
;
62 struct samr_OpenUser r
;
66 n
.in
.domain_handle
= handle
;
72 status
= dcerpc_samr_LookupNames(p
, mem_ctx
, &n
);
73 if (NT_STATUS_IS_OK(status
)) {
74 rid
= n
.out
.rids
->ids
[0];
79 r
.in
.domain_handle
= handle
;
80 r
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
82 r
.out
.user_handle
= &user_handle
;
84 status
= dcerpc_samr_OpenUser(p
, mem_ctx
, &r
);
85 if (!NT_STATUS_IS_OK(status
)) {
86 printf("OpenUser(%s) failed - %s\n", name
, nt_errstr(status
));
90 d
.in
.user_handle
= &user_handle
;
91 d
.out
.user_handle
= &user_handle
;
92 status
= dcerpc_samr_DeleteUser(p
, mem_ctx
, &d
);
93 if (!NT_STATUS_IS_OK(status
)) {
101 create a test user in the domain
102 an opaque pointer is returned. Pass it to torture_leave_domain()
106 struct test_join
*torture_create_testuser(struct torture_context
*torture
,
107 const char *username
,
110 const char **random_password
)
113 struct samr_Connect c
;
114 struct samr_CreateUser2 r
;
115 struct samr_OpenDomain o
;
116 struct samr_LookupDomain l
;
117 struct dom_sid2
*sid
= NULL
;
118 struct samr_GetUserPwInfo pwp
;
119 struct samr_PwInfo info
;
120 struct samr_SetUserInfo s
;
121 union samr_UserInfo u
;
122 struct policy_handle handle
;
123 struct policy_handle domain_handle
;
124 uint32_t access_granted
;
126 DATA_BLOB session_key
;
127 struct lsa_String name
;
129 int policy_min_pw_len
= 0;
130 struct test_join
*join
;
132 const char *dc_binding
= torture_setting_string(torture
, "dc_binding", NULL
);
134 join
= talloc(NULL
, struct test_join
);
141 printf("Connecting to SAMR\n");
144 status
= dcerpc_pipe_connect(join
,
148 cmdline_credentials
, NULL
, torture
->lp_ctx
);
151 status
= torture_rpc_connection(torture
,
155 if (!NT_STATUS_IS_OK(status
)) {
159 c
.in
.system_name
= NULL
;
160 c
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
161 c
.out
.connect_handle
= &handle
;
163 status
= dcerpc_samr_Connect(join
->p
, join
, &c
);
164 if (!NT_STATUS_IS_OK(status
)) {
165 const char *errstr
= nt_errstr(status
);
166 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
167 errstr
= dcerpc_errstr(join
, join
->p
->last_fault_code
);
169 printf("samr_Connect failed - %s\n", errstr
);
173 printf("Opening domain %s\n", domain
);
175 name
.string
= domain
;
176 l
.in
.connect_handle
= &handle
;
177 l
.in
.domain_name
= &name
;
180 status
= dcerpc_samr_LookupDomain(join
->p
, join
, &l
);
181 if (!NT_STATUS_IS_OK(status
)) {
182 printf("LookupDomain failed - %s\n", nt_errstr(status
));
186 talloc_steal(join
, *l
.out
.sid
);
187 join
->dom_sid
= *l
.out
.sid
;
188 join
->dom_netbios_name
= talloc_strdup(join
, domain
);
189 if (!join
->dom_netbios_name
) goto failed
;
191 o
.in
.connect_handle
= &handle
;
192 o
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
193 o
.in
.sid
= *l
.out
.sid
;
194 o
.out
.domain_handle
= &domain_handle
;
196 status
= dcerpc_samr_OpenDomain(join
->p
, join
, &o
);
197 if (!NT_STATUS_IS_OK(status
)) {
198 printf("OpenDomain failed - %s\n", nt_errstr(status
));
202 printf("Creating account %s\n", username
);
205 name
.string
= username
;
206 r
.in
.domain_handle
= &domain_handle
;
207 r
.in
.account_name
= &name
;
208 r
.in
.acct_flags
= acct_type
;
209 r
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
210 r
.out
.user_handle
= &join
->user_handle
;
211 r
.out
.access_granted
= &access_granted
;
214 status
= dcerpc_samr_CreateUser2(join
->p
, join
, &r
);
216 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
217 status
= DeleteUser_byname(join
->p
, join
, &domain_handle
, name
.string
);
218 if (NT_STATUS_IS_OK(status
)) {
223 if (!NT_STATUS_IS_OK(status
)) {
224 printf("CreateUser2 failed - %s\n", nt_errstr(status
));
228 join
->user_sid
= dom_sid_add_rid(join
, join
->dom_sid
, rid
);
230 pwp
.in
.user_handle
= &join
->user_handle
;
231 pwp
.out
.info
= &info
;
233 status
= dcerpc_samr_GetUserPwInfo(join
->p
, join
, &pwp
);
234 if (NT_STATUS_IS_OK(status
)) {
235 policy_min_pw_len
= pwp
.out
.info
->min_password_length
;
238 random_pw
= generate_random_password(join
, MAX(8, policy_min_pw_len
), 255);
240 printf("Setting account password '%s'\n", random_pw
);
243 s
.in
.user_handle
= &join
->user_handle
;
247 encode_pw_buffer(u
.info24
.password
.data
, random_pw
, STR_UNICODE
);
248 u
.info24
.password_expired
= 0;
250 status
= dcerpc_fetch_session_key(join
->p
, &session_key
);
251 if (!NT_STATUS_IS_OK(status
)) {
252 printf("SetUserInfo level %u - no session key - %s\n",
253 s
.in
.level
, nt_errstr(status
));
254 torture_leave_domain(torture
, join
);
258 arcfour_crypt_blob(u
.info24
.password
.data
, 516, &session_key
);
260 status
= dcerpc_samr_SetUserInfo(join
->p
, join
, &s
);
261 if (!NT_STATUS_IS_OK(status
)) {
262 printf("SetUserInfo failed - %s\n", nt_errstr(status
));
267 s
.in
.user_handle
= &join
->user_handle
;
271 u
.info21
.acct_flags
= acct_type
| ACB_PWNOEXP
;
272 u
.info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
| SAMR_FIELD_DESCRIPTION
| SAMR_FIELD_COMMENT
| SAMR_FIELD_FULL_NAME
;
274 u
.info21
.comment
.string
= talloc_asprintf(join
,
275 "Tortured by Samba4: %s",
276 timestring(join
, time(NULL
)));
278 u
.info21
.full_name
.string
= talloc_asprintf(join
,
279 "Torture account for Samba4: %s",
280 timestring(join
, time(NULL
)));
282 u
.info21
.description
.string
= talloc_asprintf(join
,
283 "Samba4 torture account created by host %s: %s",
284 lp_netbios_name(torture
->lp_ctx
),
285 timestring(join
, time(NULL
)));
287 printf("Resetting ACB flags, force pw change time\n");
289 status
= dcerpc_samr_SetUserInfo(join
->p
, join
, &s
);
290 if (!NT_STATUS_IS_OK(status
)) {
291 printf("SetUserInfo failed - %s\n", nt_errstr(status
));
295 if (random_password
) {
296 *random_password
= random_pw
;
302 torture_leave_domain(torture
, join
);
307 _PUBLIC_
struct test_join
*torture_join_domain(struct torture_context
*tctx
,
308 const char *machine_name
,
310 struct cli_credentials
**machine_credentials
)
313 struct libnet_context
*libnet_ctx
;
314 struct libnet_JoinDomain
*libnet_r
;
315 struct test_join
*tj
;
316 struct samr_SetUserInfo s
;
317 union samr_UserInfo u
;
319 tj
= talloc(tctx
, struct test_join
);
320 if (!tj
) return NULL
;
322 libnet_r
= talloc(tj
, struct libnet_JoinDomain
);
328 libnet_ctx
= libnet_context_init(tctx
->ev
, tctx
->lp_ctx
);
334 tj
->libnet_r
= libnet_r
;
336 libnet_ctx
->cred
= cmdline_credentials
;
337 libnet_r
->in
.binding
= torture_setting_string(tctx
, "binding", NULL
);
338 if (!libnet_r
->in
.binding
) {
339 libnet_r
->in
.binding
= talloc_asprintf(libnet_r
, "ncacn_np:%s", torture_setting_string(tctx
, "host", NULL
));
341 libnet_r
->in
.level
= LIBNET_JOINDOMAIN_SPECIFIED
;
342 libnet_r
->in
.netbios_name
= machine_name
;
343 libnet_r
->in
.account_name
= talloc_asprintf(libnet_r
, "%s$", machine_name
);
344 if (!libnet_r
->in
.account_name
) {
349 libnet_r
->in
.acct_type
= acct_flags
;
350 libnet_r
->in
.recreate_account
= true;
352 status
= libnet_JoinDomain(libnet_ctx
, libnet_r
, libnet_r
);
353 if (!NT_STATUS_IS_OK(status
)) {
354 if (libnet_r
->out
.error_string
) {
355 DEBUG(0, ("Domain join failed - %s\n", libnet_r
->out
.error_string
));
357 DEBUG(0, ("Domain join failed - %s\n", nt_errstr(status
)));
362 tj
->p
= libnet_r
->out
.samr_pipe
;
363 tj
->user_handle
= *libnet_r
->out
.user_handle
;
364 tj
->dom_sid
= libnet_r
->out
.domain_sid
;
365 talloc_steal(tj
, libnet_r
->out
.domain_sid
);
366 tj
->dom_netbios_name
= libnet_r
->out
.domain_name
;
367 talloc_steal(tj
, libnet_r
->out
.domain_name
);
368 tj
->dom_dns_name
= libnet_r
->out
.realm
;
369 talloc_steal(tj
, libnet_r
->out
.realm
);
370 tj
->user_guid
= libnet_r
->out
.account_guid
;
371 tj
->netbios_name
= talloc_strdup(tj
, machine_name
);
372 if (!tj
->netbios_name
) {
378 s
.in
.user_handle
= &tj
->user_handle
;
382 u
.info21
.fields_present
= SAMR_FIELD_DESCRIPTION
| SAMR_FIELD_COMMENT
| SAMR_FIELD_FULL_NAME
;
383 u
.info21
.comment
.string
= talloc_asprintf(tj
,
384 "Tortured by Samba4: %s",
385 timestring(tj
, time(NULL
)));
386 u
.info21
.full_name
.string
= talloc_asprintf(tj
,
387 "Torture account for Samba4: %s",
388 timestring(tj
, time(NULL
)));
390 u
.info21
.description
.string
= talloc_asprintf(tj
,
391 "Samba4 torture account created by host %s: %s",
392 lp_netbios_name(tctx
->lp_ctx
), timestring(tj
, time(NULL
)));
394 status
= dcerpc_samr_SetUserInfo(tj
->p
, tj
, &s
);
395 if (!NT_STATUS_IS_OK(status
)) {
396 printf("SetUserInfo (non-critical) failed - %s\n", nt_errstr(status
));
399 *machine_credentials
= cli_credentials_init(tj
);
400 cli_credentials_set_conf(*machine_credentials
, tctx
->lp_ctx
);
401 cli_credentials_set_workstation(*machine_credentials
, machine_name
, CRED_SPECIFIED
);
402 cli_credentials_set_domain(*machine_credentials
, libnet_r
->out
.domain_name
, CRED_SPECIFIED
);
403 if (libnet_r
->out
.realm
) {
404 cli_credentials_set_realm(*machine_credentials
, libnet_r
->out
.realm
, CRED_SPECIFIED
);
406 cli_credentials_set_username(*machine_credentials
, libnet_r
->in
.account_name
, CRED_SPECIFIED
);
407 cli_credentials_set_password(*machine_credentials
, libnet_r
->out
.join_password
, CRED_SPECIFIED
);
408 cli_credentials_set_kvno(*machine_credentials
, libnet_r
->out
.kvno
);
409 if (acct_flags
& ACB_SVRTRUST
) {
410 cli_credentials_set_secure_channel_type(*machine_credentials
,
412 } else if (acct_flags
& ACB_WSTRUST
) {
413 cli_credentials_set_secure_channel_type(*machine_credentials
,
416 DEBUG(0, ("Invalid account type specificed to torture_join_domain\n"));
417 talloc_free(*machine_credentials
);
424 struct dcerpc_pipe
*torture_join_samr_pipe(struct test_join
*join
)
429 struct policy_handle
*torture_join_samr_user_policy(struct test_join
*join
)
431 return &join
->user_handle
;
434 static NTSTATUS
torture_leave_ads_domain(struct torture_context
*torture
,
436 struct libnet_JoinDomain
*libnet_r
)
441 struct ldb_dn
*server_dn
;
442 struct ldb_context
*ldb_ctx
;
444 char *remote_ldb_url
;
446 /* Check if we are a domain controller. If not, exit. */
447 if (!libnet_r
->out
.server_dn_str
) {
451 tmp_ctx
= talloc_named(mem_ctx
, 0, "torture_leave temporary context");
453 libnet_r
->out
.error_string
= NULL
;
454 return NT_STATUS_NO_MEMORY
;
457 ldb_ctx
= ldb_init(tmp_ctx
, torture
->ev
);
459 libnet_r
->out
.error_string
= NULL
;
460 talloc_free(tmp_ctx
);
461 return NT_STATUS_NO_MEMORY
;
464 /* Remove CN=Servers,... entry from the AD. */
465 server_dn
= ldb_dn_new(tmp_ctx
, ldb_ctx
, libnet_r
->out
.server_dn_str
);
466 if (! ldb_dn_validate(server_dn
)) {
467 libnet_r
->out
.error_string
= NULL
;
468 talloc_free(tmp_ctx
);
469 return NT_STATUS_NO_MEMORY
;
472 remote_ldb_url
= talloc_asprintf(tmp_ctx
, "ldap://%s", libnet_r
->out
.samr_binding
->host
);
473 if (!remote_ldb_url
) {
474 libnet_r
->out
.error_string
= NULL
;
475 talloc_free(tmp_ctx
);
476 return NT_STATUS_NO_MEMORY
;
479 ldb_set_opaque(ldb_ctx
, "credentials", cmdline_credentials
);
480 ldb_set_opaque(ldb_ctx
, "loadparm", cmdline_lp_ctx
);
482 rtn
= ldb_connect(ldb_ctx
, remote_ldb_url
, 0, NULL
);
484 libnet_r
->out
.error_string
= NULL
;
485 talloc_free(tmp_ctx
);
486 return NT_STATUS_UNSUCCESSFUL
;
489 rtn
= ldb_delete(ldb_ctx
, server_dn
);
491 libnet_r
->out
.error_string
= NULL
;
492 talloc_free(tmp_ctx
);
493 return NT_STATUS_UNSUCCESSFUL
;
496 DEBUG(0, ("%s removed successfully.\n", libnet_r
->out
.server_dn_str
));
498 talloc_free(tmp_ctx
);
503 leave the domain, deleting the machine acct
506 _PUBLIC_
void torture_leave_domain(struct torture_context
*torture
, struct test_join
*join
)
508 struct samr_DeleteUser d
;
514 d
.in
.user_handle
= &join
->user_handle
;
515 d
.out
.user_handle
= &join
->user_handle
;
517 /* Delete machine account */
518 status
= dcerpc_samr_DeleteUser(join
->p
, join
, &d
);
519 if (!NT_STATUS_IS_OK(status
)) {
520 printf("Delete of machine account %s failed\n",
523 printf("Delete of machine account %s was successful.\n",
527 if (join
->libnet_r
) {
528 status
= torture_leave_ads_domain(torture
, join
, join
->libnet_r
);
535 return the dom sid for a test join
537 _PUBLIC_
const struct dom_sid
*torture_join_sid(struct test_join
*join
)
539 return join
->dom_sid
;
542 const struct dom_sid
*torture_join_user_sid(struct test_join
*join
)
544 return join
->user_sid
;
547 const char *torture_join_netbios_name(struct test_join
*join
)
549 return join
->netbios_name
;
552 const struct GUID
*torture_join_user_guid(struct test_join
*join
)
554 return &join
->user_guid
;
557 const char *torture_join_dom_netbios_name(struct test_join
*join
)
559 return join
->dom_netbios_name
;
562 const char *torture_join_dom_dns_name(struct test_join
*join
)
564 return join
->dom_dns_name
;
567 const char *torture_join_server_dn_str(struct test_join
*join
)
569 if (join
->libnet_r
) {
570 return join
->libnet_r
->out
.server_dn_str
;
576 #if 0 /* Left as the documentation of the join process, but see new implementation in libnet_become_dc.c */
577 struct test_join_ads_dc
{
578 struct test_join
*join
;
581 struct test_join_ads_dc
*torture_join_domain_ads_dc(const char *machine_name
,
583 struct cli_credentials
**machine_credentials
)
585 struct test_join_ads_dc
*join
;
587 join
= talloc(NULL
, struct test_join_ads_dc
);
592 join
->join
= torture_join_domain(machine_name
,
594 machine_credentials
);
601 /* W2K: modify userAccountControl from 4096 to 532480 */
603 /* W2K: modify RDN to OU=Domain Controllers and skip the $ from server name */
605 /* ask objectVersion of Schema Partition */
607 /* ask rIDManagerReferenz of the Domain Partition */
609 /* ask fsMORoleOwner of the RID-Manager$ object
610 * returns CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ...
613 /* ask for dnsHostName of CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
615 /* ask for objectGUID of CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
617 /* ask for * of CN=Default-First-Site-Name, ... */
619 /* search (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<machine_name>$)) in Domain Partition
620 * attributes : distinguishedName, userAccountControl
623 /* ask * for CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
624 * should fail with noSuchObject
627 /* add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
629 * objectClass = server
630 * systemFlags = 50000000
631 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
634 /* ask for * of CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
635 * should fail with noSuchObject
638 /* search for (ncname=<domain_nc>) in CN=Partitions,CN=Configuration,...
639 * attributes: ncName, dnsRoot
642 /* modify add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
643 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
644 * should fail with attributeOrValueExists
647 /* modify replace CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
648 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
651 /* DsAddEntry to create the CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
655 /* replicate CN=Schema,CN=Configuration,...
656 * using DRSUAPI_DS_BIND_GUID_W2K ("6abec3d1-3054-41c8-a362-5a0c5b7d5d71")
660 /* replicate CN=Configuration,...
661 * using DRSUAPI_DS_BIND_GUID_W2K ("6abec3d1-3054-41c8-a362-5a0c5b7d5d71")
665 /* replicate Domain Partition
666 * using DRSUAPI_DS_BIND_GUID_W2K ("6abec3d1-3054-41c8-a362-5a0c5b7d5d71")
670 /* call DsReplicaUpdateRefs() for all partitions like this:
671 * req1: struct drsuapi_DsReplicaUpdateRefsRequest1
673 * naming_context: struct drsuapi_DsReplicaObjectIdentifier
674 * __ndr_size : 0x000000ae (174)
675 * __ndr_size_sid : 0x00000000 (0)
676 * guid : 00000000-0000-0000-0000-000000000000
678 * dn : 'CN=Schema,CN=Configuration,DC=w2k3,DC=vmnet1,DC=vm,DC=base'
679 * dest_dsa_dns_name : *
680 * dest_dsa_dns_name : '4a0df188-a0b8-47ea-bbe5-e614723f16dd._msdcs.w2k3.vmnet1.vm.base'
681 * dest_dsa_guid : 4a0df188-a0b8-47ea-bbe5-e614723f16dd
682 * options : 0x0000001c (28)
683 * 0: DRSUAPI_DS_REPLICA_UPDATE_ASYNCHRONOUS_OPERATION
684 * 0: DRSUAPI_DS_REPLICA_UPDATE_WRITEABLE
685 * 1: DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE
686 * 1: DRSUAPI_DS_REPLICA_UPDATE_DELETE_REFERENCE
687 * 1: DRSUAPI_DS_REPLICA_UPDATE_0x00000010
689 * 4a0df188-a0b8-47ea-bbe5-e614723f16dd is the objectGUID the DsAddEntry() returned for the
690 * CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
693 /* W2K3: see libnet/libnet_become_dc.c */