r14402: Generate seperate headers for RPC client functions.
[gd/samba-autobuild/.git] / source4 / torture / rpc / testjoin.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    utility code to join/leave a domain
5
6    Copyright (C) Andrew Tridgell 2004
7    
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.
12    
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.
17    
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.
21 */
22
23 /*
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
26 */
27
28 #include "includes.h"
29 #include "torture/torture.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 "smb.h"
35 #include "lib/ldb/include/ldb.h"
36 #include "librpc/gen_ndr/ndr_samr_c.h"
37
38 #include "libcli/auth/libcli_auth.h"
39 #include "torture/rpc/rpc.h"
40 #include "libcli/security/proto.h"
41
42 struct test_join {
43         struct dcerpc_pipe *p;
44         struct policy_handle user_handle;
45         struct libnet_JoinDomain *libnet_r;
46         struct dom_sid *dom_sid;
47         struct dom_sid *user_sid;
48 };
49
50
51 static NTSTATUS DeleteUser_byname(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
52                                   struct policy_handle *handle, const char *name)
53 {
54         NTSTATUS status;
55         struct samr_DeleteUser d;
56         struct policy_handle user_handle;
57         uint32_t rid;
58         struct samr_LookupNames n;
59         struct lsa_String sname;
60         struct samr_OpenUser r;
61
62         sname.string = name;
63
64         n.in.domain_handle = handle;
65         n.in.num_names = 1;
66         n.in.names = &sname;
67
68         status = dcerpc_samr_LookupNames(p, mem_ctx, &n);
69         if (NT_STATUS_IS_OK(status)) {
70                 rid = n.out.rids.ids[0];
71         } else {
72                 return status;
73         }
74
75         r.in.domain_handle = handle;
76         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
77         r.in.rid = rid;
78         r.out.user_handle = &user_handle;
79
80         status = dcerpc_samr_OpenUser(p, mem_ctx, &r);
81         if (!NT_STATUS_IS_OK(status)) {
82                 printf("OpenUser(%s) failed - %s\n", name, nt_errstr(status));
83                 return status;
84         }
85
86         d.in.user_handle = &user_handle;
87         d.out.user_handle = &user_handle;
88         status = dcerpc_samr_DeleteUser(p, mem_ctx, &d);
89         if (!NT_STATUS_IS_OK(status)) {
90                 return status;
91         }
92
93         return NT_STATUS_OK;
94 }
95
96 /*
97   create a test user in the domain
98   an opaque pointer is returned. Pass it to torture_leave_domain() 
99   when finished
100 */
101
102 struct test_join *torture_create_testuser(const char *username, 
103                                           const char *domain,
104                                           uint16_t acct_type,
105                                           const char **random_password)
106 {
107         NTSTATUS status;
108         struct samr_Connect c;
109         struct samr_CreateUser2 r;
110         struct samr_OpenDomain o;
111         struct samr_LookupDomain l;
112         struct samr_GetUserPwInfo pwp;
113         struct samr_SetUserInfo s;
114         union samr_UserInfo u;
115         struct policy_handle handle;
116         struct policy_handle domain_handle;
117         uint32_t access_granted;
118         uint32_t rid;
119         DATA_BLOB session_key;
120         struct lsa_String name;
121         
122         int policy_min_pw_len = 0;
123         struct test_join *join;
124         char *random_pw;
125
126         join = talloc(NULL, struct test_join);
127         if (join == NULL) {
128                 return NULL;
129         }
130
131         ZERO_STRUCTP(join);
132
133         printf("Connecting to SAMR\n");
134
135         status = torture_rpc_connection(join, 
136                                         &join->p, 
137                                         &dcerpc_table_samr);
138         if (!NT_STATUS_IS_OK(status)) {
139                 return NULL;
140         }
141
142         c.in.system_name = NULL;
143         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
144         c.out.connect_handle = &handle;
145
146         status = dcerpc_samr_Connect(join->p, join, &c);
147         if (!NT_STATUS_IS_OK(status)) {
148                 const char *errstr = nt_errstr(status);
149                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
150                         errstr = dcerpc_errstr(join, join->p->last_fault_code);
151                 }
152                 printf("samr_Connect failed - %s\n", errstr);
153                 return NULL;
154         }
155
156         printf("Opening domain %s\n", domain);
157
158         name.string = domain;
159         l.in.connect_handle = &handle;
160         l.in.domain_name = &name;
161
162         status = dcerpc_samr_LookupDomain(join->p, join, &l);
163         if (!NT_STATUS_IS_OK(status)) {
164                 printf("LookupDomain failed - %s\n", nt_errstr(status));
165                 goto failed;
166         }
167
168         talloc_steal(join, l.out.sid);
169         join->dom_sid = l.out.sid;
170
171         o.in.connect_handle = &handle;
172         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
173         o.in.sid = l.out.sid;
174         o.out.domain_handle = &domain_handle;
175
176         status = dcerpc_samr_OpenDomain(join->p, join, &o);
177         if (!NT_STATUS_IS_OK(status)) {
178                 printf("OpenDomain failed - %s\n", nt_errstr(status));
179                 goto failed;
180         }
181
182         printf("Creating account %s\n", username);
183
184 again:
185         name.string = username;
186         r.in.domain_handle = &domain_handle;
187         r.in.account_name = &name;
188         r.in.acct_flags = acct_type;
189         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
190         r.out.user_handle = &join->user_handle;
191         r.out.access_granted = &access_granted;
192         r.out.rid = &rid;
193
194         status = dcerpc_samr_CreateUser2(join->p, join, &r);
195
196         if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
197                 status = DeleteUser_byname(join->p, join, &domain_handle, name.string);
198                 if (NT_STATUS_IS_OK(status)) {
199                         goto again;
200                 }
201         }
202
203         if (!NT_STATUS_IS_OK(status)) {
204                 printf("CreateUser2 failed - %s\n", nt_errstr(status));
205                 goto failed;
206         }
207
208         join->user_sid = dom_sid_add_rid(join, join->dom_sid, rid);
209
210         pwp.in.user_handle = &join->user_handle;
211
212         status = dcerpc_samr_GetUserPwInfo(join->p, join, &pwp);
213         if (NT_STATUS_IS_OK(status)) {
214                 policy_min_pw_len = pwp.out.info.min_password_length;
215         }
216
217         random_pw = generate_random_str(join, MAX(8, policy_min_pw_len));
218
219         printf("Setting account password '%s'\n", random_pw);
220
221         s.in.user_handle = &join->user_handle;
222         s.in.info = &u;
223         s.in.level = 24;
224
225         encode_pw_buffer(u.info24.password.data, random_pw, STR_UNICODE);
226         u.info24.pw_len = strlen(random_pw);
227
228         status = dcerpc_fetch_session_key(join->p, &session_key);
229         if (!NT_STATUS_IS_OK(status)) {
230                 printf("SetUserInfo level %u - no session key - %s\n",
231                        s.in.level, nt_errstr(status));
232                 torture_leave_domain(join);
233                 goto failed;
234         }
235
236         arcfour_crypt_blob(u.info24.password.data, 516, &session_key);
237
238         status = dcerpc_samr_SetUserInfo(join->p, join, &s);
239         if (!NT_STATUS_IS_OK(status)) {
240                 printf("SetUserInfo failed - %s\n", nt_errstr(status));
241                 goto failed;
242         }
243
244         ZERO_STRUCT(u);
245         s.in.user_handle = &join->user_handle;
246         s.in.info = &u;
247         s.in.level = 21;
248
249         u.info21.acct_flags = acct_type;
250         u.info21.fields_present = SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_DESCRIPTION | SAMR_FIELD_COMMENT | SAMR_FIELD_FULL_NAME;
251
252         u.info21.comment.string = talloc_asprintf(join, 
253                                                   "Tortured by Samba4: %s", 
254                                                   timestring(join, time(NULL)));
255         
256         u.info21.full_name.string = talloc_asprintf(join, 
257                                                     "Torture account for Samba4: %s", 
258                                                     timestring(join, time(NULL)));
259         
260         u.info21.description.string = talloc_asprintf(join, 
261                                          "Samba4 torture account created by host %s: %s", 
262                                          lp_netbios_name(), timestring(join, time(NULL)));
263
264         printf("Resetting ACB flags, force pw change time\n");
265
266         status = dcerpc_samr_SetUserInfo(join->p, join, &s);
267         if (!NT_STATUS_IS_OK(status)) {
268                 printf("SetUserInfo failed - %s\n", nt_errstr(status));
269                 goto failed;
270         }
271
272         if (random_password) {
273                 *random_password = random_pw;
274         }
275
276         return join;
277
278 failed:
279         torture_leave_domain(join);
280         return NULL;
281 }
282
283
284 struct test_join *torture_join_domain(const char *machine_name, 
285                                       uint32_t acct_flags,
286                                       struct cli_credentials **machine_credentials)
287 {
288         NTSTATUS status;
289         struct libnet_context *libnet_ctx;
290         struct libnet_JoinDomain *libnet_r;
291         struct test_join *tj;
292         struct samr_SetUserInfo s;
293         union samr_UserInfo u;
294         
295         tj = talloc(NULL, struct test_join);
296         if (!tj) return NULL;
297
298         libnet_r = talloc(tj, struct libnet_JoinDomain);
299         if (!libnet_r) {
300                 talloc_free(tj);
301                 return NULL;
302         }
303         
304         libnet_ctx = libnet_context_init(NULL); 
305         if (!libnet_ctx) {
306                 talloc_free(tj);
307                 return NULL;
308         }
309         
310         tj->libnet_r = libnet_r;
311                 
312         libnet_ctx->cred = cmdline_credentials;
313         libnet_r->in.binding = lp_parm_string(-1, "torture", "binding");
314         if (!libnet_r->in.binding) {
315                 libnet_r->in.binding = talloc_asprintf(libnet_r, "ncacn_np:%s", lp_parm_string(-1, "torture", "host"));
316         }
317         libnet_r->in.level = LIBNET_JOINDOMAIN_SPECIFIED;
318         libnet_r->in.netbios_name = machine_name;
319         libnet_r->in.account_name = talloc_asprintf(libnet_r, "%s$", machine_name);
320         if (!libnet_r->in.account_name) {
321                 talloc_free(tj);
322                 return NULL;
323         }
324         
325         libnet_r->in.acct_type = acct_flags;
326         libnet_r->in.recreate_account = True;
327
328         status = libnet_JoinDomain(libnet_ctx, libnet_r, libnet_r);
329         if (!NT_STATUS_IS_OK(status)) {
330                 if (libnet_r->out.error_string) {
331                         DEBUG(0, ("Domain join failed - %s.\n", libnet_r->out.error_string));
332                 } else {
333                         DEBUG(0, ("Domain join failed - %s.\n", nt_errstr(status)));
334                 }
335                 talloc_free(tj);
336                 return NULL;
337         }
338         tj->p = libnet_r->out.samr_pipe;
339         tj->user_handle = *libnet_r->out.user_handle;
340         tj->dom_sid = libnet_r->out.domain_sid;
341         talloc_steal(tj, libnet_r->out.domain_sid);
342
343         ZERO_STRUCT(u);
344         s.in.user_handle = &tj->user_handle;
345         s.in.info = &u;
346         s.in.level = 21;
347
348         u.info21.fields_present = SAMR_FIELD_DESCRIPTION | SAMR_FIELD_COMMENT | SAMR_FIELD_FULL_NAME;
349         u.info21.comment.string = talloc_asprintf(tj, 
350                                                   "Tortured by Samba4: %s", 
351                                                   timestring(tj, time(NULL)));
352         u.info21.full_name.string = talloc_asprintf(tj, 
353                                                     "Torture account for Samba4: %s", 
354                                                     timestring(tj, time(NULL)));
355         
356         u.info21.description.string = talloc_asprintf(tj, 
357                                                       "Samba4 torture account created by host %s: %s", 
358                                                       lp_netbios_name(), timestring(tj, time(NULL)));
359
360         status = dcerpc_samr_SetUserInfo(tj->p, tj, &s);
361         if (!NT_STATUS_IS_OK(status)) {
362                 printf("SetUserInfo (non-critical) failed - %s\n", nt_errstr(status));
363         }
364
365         *machine_credentials = cli_credentials_init(tj);
366         cli_credentials_set_conf(*machine_credentials);
367         cli_credentials_set_workstation(*machine_credentials, machine_name, CRED_SPECIFIED);
368         cli_credentials_set_domain(*machine_credentials, libnet_r->out.domain_name, CRED_SPECIFIED);
369         if (libnet_r->out.realm) {
370                 cli_credentials_set_realm(*machine_credentials, libnet_r->out.realm, CRED_SPECIFIED);
371         }
372         cli_credentials_set_username(*machine_credentials, libnet_r->in.account_name, CRED_SPECIFIED);
373         cli_credentials_set_password(*machine_credentials, libnet_r->out.join_password, CRED_SPECIFIED);
374         if (acct_flags & ACB_SVRTRUST) {
375                 cli_credentials_set_secure_channel_type(*machine_credentials,
376                                                         SEC_CHAN_BDC);
377         } else if (acct_flags & ACB_WSTRUST) {
378                 cli_credentials_set_secure_channel_type(*machine_credentials,
379                                                         SEC_CHAN_WKSTA);
380         } else {
381                 DEBUG(0, ("Invalid account type specificed to torture_join_domain\n"));
382                 talloc_free(*machine_credentials);
383                 return NULL;
384         }
385
386         return tj;
387 }
388
389 struct dcerpc_pipe *torture_join_samr_pipe(struct test_join *join) 
390 {
391         return join->p;
392 }
393
394 struct policy_handle *torture_join_samr_user_policy(struct test_join *join) 
395 {
396         return &join->user_handle;
397 }
398
399 NTSTATUS torture_leave_ads_domain(TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *libnet_r)
400 {
401         int rtn;
402         TALLOC_CTX *tmp_ctx;
403
404         struct ldb_dn *server_dn;
405         struct ldb_context *ldb_ctx;
406
407         char *remote_ldb_url; 
408          
409         /* Check if we are a domain controller. If not, exit. */
410         if (!libnet_r->out.server_dn_str) {
411                 return NT_STATUS_OK;
412         }
413
414         tmp_ctx = talloc_named(mem_ctx, 0, "torture_leave temporary context");
415         if (!tmp_ctx) {
416                 libnet_r->out.error_string = NULL;
417                 return NT_STATUS_NO_MEMORY;
418         }
419
420         ldb_ctx = ldb_init(tmp_ctx);
421         if (!ldb_ctx) {
422                 libnet_r->out.error_string = NULL;
423                 talloc_free(tmp_ctx);
424                 return NT_STATUS_NO_MEMORY;
425         }
426
427         /* Remove CN=Servers,... entry from the AD. */ 
428         server_dn = ldb_dn_explode(tmp_ctx, libnet_r->out.server_dn_str);
429         if (!server_dn) {
430                 libnet_r->out.error_string = NULL;
431                 talloc_free(tmp_ctx);
432                 return NT_STATUS_NO_MEMORY;
433         }
434
435         remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s", libnet_r->out.samr_binding->host);
436         if (!remote_ldb_url) {
437                 libnet_r->out.error_string = NULL;
438                 talloc_free(tmp_ctx);
439                 return NT_STATUS_NO_MEMORY;
440         }
441
442         rtn = ldb_connect(ldb_ctx, remote_ldb_url, 0, NULL);
443         if (rtn != 0) {
444                 libnet_r->out.error_string = NULL;
445                 talloc_free(tmp_ctx);
446                 return NT_STATUS_UNSUCCESSFUL;
447         }
448
449         rtn = ldb_delete(ldb_ctx, server_dn);
450         if (rtn != 0) {
451                 libnet_r->out.error_string = NULL;
452                 talloc_free(tmp_ctx);
453                 return NT_STATUS_UNSUCCESSFUL;
454         }
455
456         DEBUG(0, ("%s removed successfully.\n", libnet_r->out.server_dn_str));
457
458         talloc_free(tmp_ctx); 
459         return NT_STATUS_OK;
460 }
461
462 /*
463   leave the domain, deleting the machine acct
464 */
465
466 void torture_leave_domain(struct test_join *join)
467 {
468         struct samr_DeleteUser d;
469         NTSTATUS status;
470
471         if (!join) {
472                 return;
473         }
474         d.in.user_handle = &join->user_handle;
475         d.out.user_handle = &join->user_handle;
476                                         
477         /* Delete machine account */                                                                                                                                                                                                                                                                                                                    
478         status = dcerpc_samr_DeleteUser(join->p, join, &d);
479         if (!NT_STATUS_IS_OK(status)) {
480                 printf("Delete of machine account failed\n");
481         } else {
482                 printf("Delete of machine account was successful.\n");
483         }
484
485         if (join->libnet_r) {
486                 status = torture_leave_ads_domain(join, join->libnet_r);
487         }
488         
489         talloc_free(join);
490 }
491
492 /*
493   return the dom sid for a test join
494 */
495 const struct dom_sid *torture_join_sid(struct test_join *join)
496 {
497         return join->dom_sid;
498 }
499
500 const struct dom_sid *torture_join_user_sid(struct test_join *join)
501 {
502         return join->user_sid;
503 }
504
505
506 struct test_join_ads_dc {
507         struct test_join *join;
508 };
509
510 struct test_join_ads_dc *torture_join_domain_ads_dc(const char *machine_name, 
511                                                     const char *domain,
512                                                     struct cli_credentials **machine_credentials)
513 {
514         struct test_join_ads_dc *join;
515
516         join = talloc(NULL, struct test_join_ads_dc);
517         if (join == NULL) {
518                 return NULL;
519         }
520
521         join->join = torture_join_domain(machine_name, 
522                                         ACB_SVRTRUST,
523                                         machine_credentials);
524
525         if (!join->join) {
526                 return NULL;
527         }
528
529         /* do netlogon DrsEnumerateDomainTrusts */
530
531         /* modify userAccountControl from 4096 to 532480 */
532         
533         /* modify RDN to OU=Domain Controllers and skip the $ from server name */
534
535         /* ask objectVersion of Schema Partition */
536
537         /* ask rIDManagerReferenz of the Domain Partition */
538
539         /* ask fsMORoleOwner of the RID-Manager$ object
540          * returns CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ...
541          */
542
543         /* ask for dnsHostName of CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
544
545         /* ask for objectGUID of CN=NTDS Settings,CN=<DC>,CN=Servers,CN=Default-First-Site-Name, ... */
546
547         /* ask for * of CN=Default-First-Site-Name, ... */
548
549         /* search (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<machine_name>$)) in Domain Partition 
550          * attributes : distinguishedName, userAccountControl
551          */
552
553         /* ask * for CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,... 
554          * should fail with noSuchObject
555          */
556
557         /* add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,... 
558          *
559          * objectClass = server
560          * systemFlags = 50000000
561          * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
562          */
563
564         /* ask for * of CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
565          * should fail with noSuchObject
566          */
567
568         /* search for (ncname=<domain_nc>) in CN=Partitions,CN=Configuration,... 
569          * attributes: ncName, dnsRoot
570          */
571
572         /* modify add CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
573          * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
574          * should fail with attributeOrValueExists
575          */
576
577         /* modify replace CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name,...
578          * serverReferenz = CN=<machine_name>,OU=Domain Controllers,...
579          */
580
581         /* DsReplicaAdd to create the CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=Default-First-Site-Name, ...
582          * needs to be tested
583          */
584
585         return join;
586 }
587                 
588 void torture_leave_domain_ads_dc(struct test_join_ads_dc *join)
589 {
590
591         if (join->join) {
592                 torture_leave_domain(join->join);
593         }
594
595         talloc_free(join);
596 }