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"
33 struct dcerpc_pipe *p;
34 struct policy_handle user_handle;
38 static NTSTATUS DeleteUser_byname(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
39 struct policy_handle *handle, const char *name)
42 struct samr_DeleteUser d;
43 struct policy_handle user_handle;
45 struct samr_LookupNames n;
46 struct samr_String sname;
47 struct samr_OpenUser r;
51 n.in.domain_handle = handle;
55 status = dcerpc_samr_LookupNames(p, mem_ctx, &n);
56 if (NT_STATUS_IS_OK(status)) {
57 rid = n.out.rids.ids[0];
62 r.in.domain_handle = handle;
63 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65 r.out.user_handle = &user_handle;
67 status = dcerpc_samr_OpenUser(p, mem_ctx, &r);
68 if (!NT_STATUS_IS_OK(status)) {
69 printf("OpenUser(%s) failed - %s\n", name, nt_errstr(status));
73 d.in.user_handle = &user_handle;
74 d.out.user_handle = &user_handle;
75 status = dcerpc_samr_DeleteUser(p, mem_ctx, &d);
76 if (!NT_STATUS_IS_OK(status)) {
84 create a test user in the domain
85 an opaque pointer is returned. Pass it to torture_leave_domain()
89 struct test_join *torture_create_testuser(const char *username,
92 const char **random_password)
95 struct samr_Connect c;
96 struct samr_CreateUser2 r;
97 struct samr_OpenDomain o;
98 struct samr_LookupDomain l;
99 struct samr_GetUserPwInfo pwp;
100 struct samr_SetUserInfo s;
101 union samr_UserInfo u;
102 struct policy_handle handle;
103 struct policy_handle domain_handle;
104 uint32_t access_granted;
106 DATA_BLOB session_key;
107 struct samr_String name;
108 struct samr_String comment;
109 struct samr_String full_name;
111 int policy_min_pw_len = 0;
112 struct test_join *join;
115 join = talloc(NULL, struct test_join);
122 printf("Connecting to SAMR\n");
124 status = torture_rpc_connection(join,
128 DCERPC_SAMR_VERSION);
129 if (!NT_STATUS_IS_OK(status)) {
133 c.in.system_name = NULL;
134 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
135 c.out.connect_handle = &handle;
137 status = dcerpc_samr_Connect(join->p, join, &c);
138 if (!NT_STATUS_IS_OK(status)) {
139 const char *errstr = nt_errstr(status);
140 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
141 errstr = dcerpc_errstr(join, join->p->last_fault_code);
143 printf("samr_Connect failed - %s\n", errstr);
147 printf("Opening domain %s\n", domain);
149 name.string = domain;
150 l.in.connect_handle = &handle;
151 l.in.domain_name = &name;
153 status = dcerpc_samr_LookupDomain(join->p, join, &l);
154 if (!NT_STATUS_IS_OK(status)) {
155 printf("LookupDomain failed - %s\n", nt_errstr(status));
159 o.in.connect_handle = &handle;
160 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
161 o.in.sid = l.out.sid;
162 o.out.domain_handle = &domain_handle;
164 status = dcerpc_samr_OpenDomain(join->p, join, &o);
165 if (!NT_STATUS_IS_OK(status)) {
166 printf("OpenDomain failed - %s\n", nt_errstr(status));
170 printf("Creating account %s\n", username);
173 name.string = username;
174 r.in.domain_handle = &domain_handle;
175 r.in.account_name = &name;
176 r.in.acct_flags = acct_type;
177 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
178 r.out.user_handle = &join->user_handle;
179 r.out.access_granted = &access_granted;
182 status = dcerpc_samr_CreateUser2(join->p, join, &r);
184 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
185 status = DeleteUser_byname(join->p, join, &domain_handle, name.string);
186 if (NT_STATUS_IS_OK(status)) {
191 if (!NT_STATUS_IS_OK(status)) {
192 printf("CreateUser2 failed - %s\n", nt_errstr(status));
196 pwp.in.user_handle = &join->user_handle;
198 status = dcerpc_samr_GetUserPwInfo(join->p, join, &pwp);
199 if (NT_STATUS_IS_OK(status)) {
200 policy_min_pw_len = pwp.out.info.min_password_length;
203 random_pw = generate_random_str(join, MAX(8, policy_min_pw_len));
205 printf("Setting account password '%s'\n", random_pw);
207 s.in.user_handle = &join->user_handle;
211 encode_pw_buffer(u.info24.password.data, random_pw, STR_UNICODE);
212 u.info24.pw_len = strlen(random_pw);
214 status = dcerpc_fetch_session_key(join->p, &session_key);
215 if (!NT_STATUS_IS_OK(status)) {
216 printf("SetUserInfo level %u - no session key - %s\n",
217 s.in.level, nt_errstr(status));
218 torture_leave_domain(join);
222 arcfour_crypt_blob(u.info24.password.data, 516, &session_key);
224 status = dcerpc_samr_SetUserInfo(join->p, join, &s);
225 if (!NT_STATUS_IS_OK(status)) {
226 printf("SetUserInfo failed - %s\n", nt_errstr(status));
231 s.in.user_handle = &join->user_handle;
235 u.info21.acct_flags = acct_type;
236 u.info21.fields_present = SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_DESCRIPTION | SAMR_FIELD_COMMENT | SAMR_FIELD_FULL_NAME;
237 comment.string = talloc_asprintf(join,
238 "Tortured by Samba4: %s",
239 timestring(join, time(NULL)));
240 u.info21.comment = comment;
241 full_name.string = talloc_asprintf(join,
242 "Torture account for Samba4: %s",
243 timestring(join, time(NULL)));
244 u.info21.full_name = full_name;
246 u.info21.description.string = talloc_asprintf(join,
247 "Samba4 torture account created by host %s: %s",
248 lp_netbios_name(), timestring(join, time(NULL)));
250 printf("Resetting ACB flags, force pw change time\n");
252 status = dcerpc_samr_SetUserInfo(join->p, join, &s);
253 if (!NT_STATUS_IS_OK(status)) {
254 printf("SetUserInfo failed - %s\n", nt_errstr(status));
258 if (random_password) {
259 *random_password = random_pw;
265 torture_leave_domain(join);
270 struct test_join *torture_join_domain(const char *machine_name,
273 const char **machine_password)
275 char *username = talloc_asprintf(NULL, "%s$", machine_name);
276 struct test_join *tj = torture_create_testuser(username, domain, acct_flags, machine_password);
277 talloc_free(username);
281 struct dcerpc_pipe *torture_join_samr_pipe(struct test_join *join)
286 struct policy_handle *torture_join_samr_user_policy(struct test_join *join)
288 return &join->user_handle;
292 leave the domain, deleting the machine acct
294 void torture_leave_domain(struct test_join *join)
296 struct samr_DeleteUser d;
299 if (!GUID_all_zero(&join->user_handle.uuid)) {
300 d.in.user_handle = &join->user_handle;
301 d.out.user_handle = &join->user_handle;
303 status = dcerpc_samr_DeleteUser(join->p, join, &d);
304 if (!NT_STATUS_IS_OK(status)) {
305 printf("Delete of machine account failed\n");
313 struct test_join_ads_dc {
314 struct test_join *join;
317 struct test_join_ads_dc *torture_join_domain_ads_dc(const char *machine_name,
319 const char **machine_password)
321 struct test_join_ads_dc *join;
323 join = talloc(NULL, struct test_join_ads_dc);
328 join->join = torture_join_domain(machine_name, domain,
336 /* do netlogon DrsEnumerateDomainTrusts */
338 /* modify userAccountControl from 4096 to 532480 */
340 /* modify RDN to OU=Domain Controllers and skip the $ from server name */
342 /* ask objectVersion of Schema Partition */
344 /* ask rIDManagerReferenz of the Domain Partition */
346 /* ask fsMORoleOwner of the RID-Manager$ object
347 * returns CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ...
350 /* ask for dnsHostName of CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
352 /* ask for objectGUID of CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
354 /* ask for * of CN=Default-First-Site-Name, ... */
356 /* search (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<machine_name>$)) in Domain Partition
357 * attributes : distinguishedName, userAccountControl
360 /* ask * for CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
361 * should fail with noSuchObject
364 /* add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
366 * objectClass = server
367 * systemFlags = 50000000
368 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
371 /* ask for * of CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
372 * should fail with noSuchObject
375 /* search for (ncname=<domain_nc>) in CN=Partitions,CN=Configuration,...
376 * attributes: ncName, dnsRoot
379 /* modify add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
380 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
381 * should fail with attributeOrValueExists
384 /* modify replace CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
385 * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
388 /* DsReplicaAdd to create the CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
395 void torture_leave_domain_ads_dc(struct test_join_ads_dc *join)
399 torture_leave_domain(join->join);