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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 this code is used by other torture modules to join/leave a domain
25 as either a member, bdc or thru a trust relationship
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "system/time.h"
31 #include "lib/crypto/crypto.h"
32 #include "libnet/libnet.h"
33 #include "lib/cmdline/popt_common.h"
34 #include "lib/ldb/include/ldb.h"
38 struct dcerpc_pipe *p;
39 struct policy_handle user_handle;
40 struct libnet_JoinDomain *libnet_r;
45 static NTSTATUS DeleteUser_byname(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
46 struct policy_handle *handle, const char *name)
49 struct samr_DeleteUser d;
50 struct policy_handle user_handle;
52 struct samr_LookupNames n;
53 struct lsa_String sname;
54 struct samr_OpenUser r;
58 n.in.domain_handle = handle;
62 status = dcerpc_samr_LookupNames(p, mem_ctx, &n);
63 if (NT_STATUS_IS_OK(status)) {
64 rid = n.out.rids.ids[0];
69 r.in.domain_handle = handle;
70 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
72 r.out.user_handle = &user_handle;
74 status = dcerpc_samr_OpenUser(p, mem_ctx, &r);
75 if (!NT_STATUS_IS_OK(status)) {
76 printf("OpenUser(%s) failed - %s\n", name, nt_errstr(status));
80 d.in.user_handle = &user_handle;
81 d.out.user_handle = &user_handle;
82 status = dcerpc_samr_DeleteUser(p, mem_ctx, &d);
83 if (!NT_STATUS_IS_OK(status)) {
91 create a test user in the domain
92 an opaque pointer is returned. Pass it to torture_leave_domain()
96 struct test_join *torture_create_testuser(const char *username,
99 const char **random_password)
102 struct samr_Connect c;
103 struct samr_CreateUser2 r;
104 struct samr_OpenDomain o;
105 struct samr_LookupDomain l;
106 struct samr_GetUserPwInfo pwp;
107 struct samr_SetUserInfo s;
108 union samr_UserInfo u;
109 struct policy_handle handle;
110 struct policy_handle domain_handle;
111 uint32_t access_granted;
113 DATA_BLOB session_key;
114 struct lsa_String name;
115 struct lsa_String comment;
116 struct lsa_String full_name;
118 int policy_min_pw_len = 0;
119 struct test_join *join;
122 join = talloc(NULL, struct test_join);
129 printf("Connecting to SAMR\n");
131 status = torture_rpc_connection(join,
135 DCERPC_SAMR_VERSION);
136 if (!NT_STATUS_IS_OK(status)) {
140 c.in.system_name = NULL;
141 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
142 c.out.connect_handle = &handle;
144 status = dcerpc_samr_Connect(join->p, join, &c);
145 if (!NT_STATUS_IS_OK(status)) {
146 const char *errstr = nt_errstr(status);
147 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
148 errstr = dcerpc_errstr(join, join->p->last_fault_code);
150 printf("samr_Connect failed - %s\n", errstr);
154 printf("Opening domain %s\n", domain);
156 name.string = domain;
157 l.in.connect_handle = &handle;
158 l.in.domain_name = &name;
160 status = dcerpc_samr_LookupDomain(join->p, join, &l);
161 if (!NT_STATUS_IS_OK(status)) {
162 printf("LookupDomain failed - %s\n", nt_errstr(status));
166 join->dom_sid = dom_sid_string(join, l.out.sid);
168 o.in.connect_handle = &handle;
169 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
170 o.in.sid = l.out.sid;
171 o.out.domain_handle = &domain_handle;
173 status = dcerpc_samr_OpenDomain(join->p, join, &o);
174 if (!NT_STATUS_IS_OK(status)) {
175 printf("OpenDomain failed - %s\n", nt_errstr(status));
179 printf("Creating account %s\n", username);
182 name.string = username;
183 r.in.domain_handle = &domain_handle;
184 r.in.account_name = &name;
185 r.in.acct_flags = acct_type;
186 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
187 r.out.user_handle = &join->user_handle;
188 r.out.access_granted = &access_granted;
191 status = dcerpc_samr_CreateUser2(join->p, join, &r);
193 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
194 status = DeleteUser_byname(join->p, join, &domain_handle, name.string);
195 if (NT_STATUS_IS_OK(status)) {
200 if (!NT_STATUS_IS_OK(status)) {
201 printf("CreateUser2 failed - %s\n", nt_errstr(status));
205 pwp.in.user_handle = &join->user_handle;
207 status = dcerpc_samr_GetUserPwInfo(join->p, join, &pwp);
208 if (NT_STATUS_IS_OK(status)) {
209 policy_min_pw_len = pwp.out.info.min_password_length;
212 random_pw = generate_random_str(join, MAX(8, policy_min_pw_len));
214 printf("Setting account password '%s'\n", random_pw);
216 s.in.user_handle = &join->user_handle;
220 encode_pw_buffer(u.info24.password.data, random_pw, STR_UNICODE);
221 u.info24.pw_len = strlen(random_pw);
223 status = dcerpc_fetch_session_key(join->p, &session_key);
224 if (!NT_STATUS_IS_OK(status)) {
225 printf("SetUserInfo level %u - no session key - %s\n",
226 s.in.level, nt_errstr(status));
227 torture_leave_domain(join);
231 arcfour_crypt_blob(u.info24.password.data, 516, &session_key);
233 status = dcerpc_samr_SetUserInfo(join->p, join, &s);
234 if (!NT_STATUS_IS_OK(status)) {
235 printf("SetUserInfo failed - %s\n", nt_errstr(status));
240 s.in.user_handle = &join->user_handle;
244 u.info21.acct_flags = acct_type;
245 u.info21.fields_present = SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_DESCRIPTION | SAMR_FIELD_COMMENT | SAMR_FIELD_FULL_NAME;
246 comment.string = talloc_asprintf(join,
247 "Tortured by Samba4: %s",
248 timestring(join, time(NULL)));
249 u.info21.comment = comment;
250 full_name.string = talloc_asprintf(join,
251 "Torture account for Samba4: %s",
252 timestring(join, time(NULL)));
253 u.info21.full_name = full_name;
255 u.info21.description.string = talloc_asprintf(join,
256 "Samba4 torture account created by host %s: %s",
257 lp_netbios_name(), timestring(join, time(NULL)));
259 printf("Resetting ACB flags, force pw change time\n");
261 status = dcerpc_samr_SetUserInfo(join->p, join, &s);
262 if (!NT_STATUS_IS_OK(status)) {
263 printf("SetUserInfo failed - %s\n", nt_errstr(status));
267 if (random_password) {
268 *random_password = random_pw;
274 torture_leave_domain(join);
279 struct test_join *torture_join_domain(const char *machine_name,
281 struct cli_credentials **machine_credentials)
284 struct libnet_context *libnet_ctx;
285 struct libnet_JoinDomain *libnet_r;
286 struct test_join *tj;
287 struct samr_SetUserInfo s;
288 union samr_UserInfo u;
289 struct lsa_String comment;
290 struct lsa_String full_name;
292 tj = talloc(NULL, struct test_join);
293 if (!tj) return NULL;
295 libnet_r = talloc(tj, struct libnet_JoinDomain);
301 libnet_ctx = libnet_context_init(NULL);
307 tj->libnet_r = libnet_r;
309 libnet_ctx->cred = cmdline_credentials;
310 libnet_r->in.binding = lp_parm_string(-1, "torture", "binding");
311 if (!libnet_r->in.binding) {
312 libnet_r->in.binding = talloc_asprintf(libnet_r, "ncacn_np:%s", lp_parm_string(-1, "torture", "host"));
314 libnet_r->in.level = LIBNET_JOINDOMAIN_SPECIFIED;
315 libnet_r->in.netbios_name = machine_name;
316 libnet_r->in.account_name = talloc_asprintf(libnet_r, "%s$", machine_name);
317 if (!libnet_r->in.account_name) {
322 libnet_r->in.acct_type = acct_flags;
324 status = libnet_JoinDomain(libnet_ctx, libnet_r, libnet_r);
325 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
326 struct samr_DeleteUser d;
327 d.in.user_handle = libnet_r->out.user_handle;
328 d.out.user_handle = libnet_r->out.user_handle;
330 /* Delete machine account */
331 status = dcerpc_samr_DeleteUser(libnet_r->out.samr_pipe, tj, &d);
332 if (!NT_STATUS_IS_OK(status)) {
333 printf("Delete of machine account failed\n");
335 printf("Delete of machine account was successful.\n");
337 status = libnet_JoinDomain(libnet_ctx, libnet_r, libnet_r);
340 if (!NT_STATUS_IS_OK(status)) {
341 if (libnet_r->out.error_string) {
342 DEBUG(0, ("Domain join failed - %s.\n", libnet_r->out.error_string));
344 DEBUG(0, ("Domain join failed - %s.\n", nt_errstr(status)));
349 tj->p = libnet_r->out.samr_pipe;
350 tj->user_handle = *libnet_r->out.user_handle;
351 tj->dom_sid = dom_sid_string(tj, libnet_r->out.domain_sid);
354 s.in.user_handle = &tj->user_handle;
358 u.info21.fields_present = SAMR_FIELD_DESCRIPTION | SAMR_FIELD_COMMENT | SAMR_FIELD_FULL_NAME;
359 comment.string = talloc_asprintf(tj,
360 "Tortured by Samba4: %s",
361 timestring(tj, time(NULL)));
362 u.info21.comment = comment;
363 full_name.string = talloc_asprintf(tj,
364 "Torture account for Samba4: %s",
365 timestring(tj, time(NULL)));
366 u.info21.full_name = full_name;
368 u.info21.description.string = talloc_asprintf(tj,
369 "Samba4 torture account created by host %s: %s",
370 lp_netbios_name(), timestring(tj, time(NULL)));
372 status = dcerpc_samr_SetUserInfo(tj->p, tj, &s);
373 if (!NT_STATUS_IS_OK(status)) {
374 printf("SetUserInfo (non-critical) failed - %s\n", nt_errstr(status));
377 DEBUG(0, ("%s joined domain %s (%s).\n",
378 libnet_r->in.netbios_name,
379 libnet_r->out.domain_name,
382 *machine_credentials = cli_credentials_init(tj);
383 cli_credentials_set_conf(*machine_credentials);
384 cli_credentials_set_workstation(*machine_credentials, machine_name, CRED_SPECIFIED);
385 cli_credentials_set_domain(*machine_credentials, libnet_r->out.domain_name, CRED_SPECIFIED);
386 if (libnet_r->out.realm) {
387 cli_credentials_set_realm(*machine_credentials, libnet_r->out.realm, CRED_SPECIFIED);
389 cli_credentials_set_username(*machine_credentials, libnet_r->in.account_name, CRED_SPECIFIED);
390 cli_credentials_set_password(*machine_credentials, libnet_r->out.join_password, CRED_SPECIFIED);
391 if (acct_flags & ACB_SVRTRUST) {
392 cli_credentials_set_secure_channel_type(*machine_credentials,
394 } else if (acct_flags & ACB_WSTRUST) {
395 cli_credentials_set_secure_channel_type(*machine_credentials,
398 DEBUG(0, ("Invalid account type specificed to torture_join_domain\n"));
399 talloc_free(*machine_credentials);
406 struct dcerpc_pipe *torture_join_samr_pipe(struct test_join *join)
411 struct policy_handle *torture_join_samr_user_policy(struct test_join *join)
413 return &join->user_handle;
416 NTSTATUS torture_leave_ads_domain(TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *libnet_r)
422 struct ldb_dn *server_dn;
423 struct ldb_context *ldb_ctx;
425 char *remote_ldb_url;
427 /* Check if we are a domain controller. If not, exit. */
428 if (!libnet_r->out.server_dn_str) {
432 tmp_ctx = talloc_named(mem_ctx, 0, "torture_leave temporary context");
434 libnet_r->out.error_string = NULL;
435 return NT_STATUS_NO_MEMORY;
438 ldb_ctx = ldb_init(tmp_ctx);
440 libnet_r->out.error_string = NULL;
441 talloc_free(tmp_ctx);
442 return NT_STATUS_NO_MEMORY;
445 /* Remove CN=Servers,... entry from the AD. */
446 server_dn = ldb_dn_explode(tmp_ctx, libnet_r->out.server_dn_str);
448 libnet_r->out.error_string = NULL;
449 talloc_free(tmp_ctx);
450 return NT_STATUS_NO_MEMORY;
453 remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s", libnet_r->out.samr_binding->host);
454 if (!remote_ldb_url) {
455 libnet_r->out.error_string = NULL;
456 talloc_free(tmp_ctx);
457 return NT_STATUS_NO_MEMORY;
460 rtn = ldb_connect(ldb_ctx, remote_ldb_url, 0, NULL);
462 libnet_r->out.error_string = NULL;
463 talloc_free(tmp_ctx);
464 return NT_STATUS_UNSUCCESSFUL;
467 rtn = ldb_delete(ldb_ctx, server_dn);
469 libnet_r->out.error_string = NULL;
470 talloc_free(tmp_ctx);
471 return NT_STATUS_UNSUCCESSFUL;
474 DEBUG(0, ("%s removed successfully.\n", libnet_r->out.server_dn_str));
476 talloc_free(tmp_ctx);
481 leave the domain, deleting the machine acct
484 void torture_leave_domain(struct test_join *join)
486 struct samr_DeleteUser d;
492 d.in.user_handle = &join->user_handle;
493 d.out.user_handle = &join->user_handle;
495 /* Delete machine account */
496 status = dcerpc_samr_DeleteUser(join->p, join, &d);
497 if (!NT_STATUS_IS_OK(status)) {
498 printf("Delete of machine account failed\n");
500 printf("Delete of machine account was successful.\n");
503 if (join->libnet_r) {
504 status = torture_leave_ads_domain(join, join->libnet_r);
511 return the dom sid for a test join
513 const char *torture_join_sid(struct test_join *join)
515 return join->dom_sid;
519 struct test_join_ads_dc {
520 struct test_join *join;
523 struct test_join_ads_dc *torture_join_domain_ads_dc(const char *machine_name,
525 struct cli_credentials **machine_credentials)
527 struct test_join_ads_dc *join;
529 join = talloc(NULL, struct test_join_ads_dc);
534 join->join = torture_join_domain(machine_name,
536 machine_credentials);
542 /* do netlogon DrsEnumerateDomainTrusts */
544 /* modify userAccountControl from 4096 to 532480 */
546 /* modify RDN to OU=Domain Controllers and skip the $ from server name */
548 /* ask objectVersion of Schema Partition */
550 /* ask rIDManagerReferenz of the Domain Partition */
552 /* ask fsMORoleOwner of the RID-Manager$ object
553 * returns CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ...
556 /* ask for dnsHostName of CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
558 /* ask for objectGUID of CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
560 /* ask for * of CN=Default-First-Site-Name, ... */
562 /* search (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<machine_name>$)) in Domain Partition
563 * attributes : distinguishedName, userAccountControl
566 /* ask * for CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
567 * should fail with noSuchObject
570 /* add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
572 * objectClass = server
573 * systemFlags = 50000000
574 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
577 /* ask for * of CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
578 * should fail with noSuchObject
581 /* search for (ncname=<domain_nc>) in CN=Partitions,CN=Configuration,...
582 * attributes: ncName, dnsRoot
585 /* modify add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
586 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
587 * should fail with attributeOrValueExists
590 /* modify replace CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
591 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
594 /* DsReplicaAdd to create the CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
601 void torture_leave_domain_ads_dc(struct test_join_ads_dc *join)
605 torture_leave_domain(join->join);