r17104: Rename function parameters and variables to avoid shadowing global
[ira/wip.git] / source / torture / rpc / samba3rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc torture tests, designed to walk Samba3 code paths
5
6    Copyright (C) Volker Lendecke 2006
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 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "torture/torture.h"
26 #include "torture/util.h"
27 #include "librpc/gen_ndr/ndr_lsa.h"
28 #include "librpc/gen_ndr/ndr_lsa_c.h"
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
32 #include "librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "librpc/gen_ndr/ndr_srvsvc.h"
34 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
35 #include "lib/cmdline/popt_common.h"
36 #include "librpc/rpc/dcerpc.h"
37 #include "torture/rpc/rpc.h"
38 #include "libcli/libcli.h"
39 #include "libcli/composite/composite.h"
40 #include "libcli/smb_composite/smb_composite.h"
41 #include "libcli/auth/libcli_auth.h"
42 #include "libcli/auth/credentials.h"
43 #include "lib/crypto/crypto.h"
44 #include "libcli/security/proto.h"
45
46 static struct cli_credentials *create_anon_creds(TALLOC_CTX *mem_ctx)
47 {
48         struct cli_credentials *result;
49
50         if (!(result = cli_credentials_init(mem_ctx))) {
51                 return NULL;
52         }
53
54         cli_credentials_set_conf(result);
55         cli_credentials_set_anonymous(result);
56
57         return result;
58 }
59
60 /*
61  * This tests a RPC call using an invalid vuid
62  */
63
64 BOOL torture_bind_authcontext(struct torture_context *torture) 
65 {
66         TALLOC_CTX *mem_ctx;
67         NTSTATUS status;
68         BOOL ret = False;
69         struct lsa_ObjectAttribute objectattr;
70         struct lsa_OpenPolicy2 openpolicy;
71         struct policy_handle handle;
72         struct lsa_Close close_handle;
73         struct smbcli_session *tmp;
74         struct smbcli_session *session2;
75         struct smbcli_state *cli;
76         struct dcerpc_pipe *lsa_pipe;
77         struct cli_credentials *anon_creds;
78         struct smb_composite_sesssetup setup;
79
80         mem_ctx = talloc_init("torture_bind_authcontext");
81
82         if (mem_ctx == NULL) {
83                 d_printf("talloc_init failed\n");
84                 return False;
85         }
86
87         status = smbcli_full_connection(mem_ctx, &cli,
88                                         lp_parm_string(-1, "torture", "host"),
89                                         "IPC$", NULL, cmdline_credentials,
90                                         NULL);
91         if (!NT_STATUS_IS_OK(status)) {
92                 d_printf("smbcli_full_connection failed: %s\n",
93                          nt_errstr(status));
94                 goto done;
95         }
96
97         lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx);
98         if (lsa_pipe == NULL) {
99                 d_printf("dcerpc_pipe_init failed\n");
100                 goto done;
101         }
102
103         status = dcerpc_pipe_open_smb(lsa_pipe->conn, cli->tree, "\\lsarpc");
104         if (!NT_STATUS_IS_OK(status)) {
105                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
106                          nt_errstr(status));
107                 goto done;
108         }
109
110         status = dcerpc_bind_auth_none(lsa_pipe, &dcerpc_table_lsarpc);
111         if (!NT_STATUS_IS_OK(status)) {
112                 d_printf("dcerpc_bind_auth_none failed: %s\n",
113                          nt_errstr(status));
114                 goto done;
115         }
116
117         openpolicy.in.system_name =talloc_asprintf(
118                 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
119         ZERO_STRUCT(objectattr);
120         openpolicy.in.attr = &objectattr;
121         openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
122         openpolicy.out.handle = &handle;
123
124         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
125
126         if (!NT_STATUS_IS_OK(status)) {
127                 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
128                          nt_errstr(status));
129                 goto done;
130         }
131
132         close_handle.in.handle = &handle;
133         close_handle.out.handle = &handle;
134
135         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
136         if (!NT_STATUS_IS_OK(status)) {
137                 d_printf("dcerpc_lsa_Close failed: %s\n",
138                          nt_errstr(status));
139                 goto done;
140         }
141
142         session2 = smbcli_session_init(cli->transport, mem_ctx, False);
143         if (session2 == NULL) {
144                 d_printf("smbcli_session_init failed\n");
145                 goto done;
146         }
147
148         if (!(anon_creds = create_anon_creds(mem_ctx))) {
149                 d_printf("create_anon_creds failed\n");
150                 goto done;
151         }
152
153         setup.in.sesskey = cli->transport->negotiate.sesskey;
154         setup.in.capabilities = cli->transport->negotiate.capabilities;
155         setup.in.workgroup = "";
156         setup.in.credentials = anon_creds;
157
158         status = smb_composite_sesssetup(session2, &setup);
159         if (!NT_STATUS_IS_OK(status)) {
160                 d_printf("anon session setup failed: %s\n",
161                          nt_errstr(status));
162                 goto done;
163         }
164
165         tmp = cli->tree->session;
166         cli->tree->session = session2;
167
168         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
169
170         cli->tree->session = tmp;
171         talloc_free(lsa_pipe);
172         lsa_pipe = NULL;
173
174         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
175                 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
176                          "expected NT_STATUS_INVALID_HANDLE\n",
177                          nt_errstr(status));
178                 goto done;
179         }
180
181         ret = True;
182  done:
183         talloc_free(mem_ctx);
184         return ret;
185 }
186
187 /*
188  * Bind to lsa using a specific auth method
189  */
190
191 static BOOL bindtest(struct smbcli_state *cli,
192                      struct cli_credentials *credentials,
193                      uint8_t auth_type, uint8_t auth_level)
194 {
195         TALLOC_CTX *mem_ctx;
196         BOOL ret = False;
197         NTSTATUS status;
198
199         struct dcerpc_pipe *lsa_pipe;
200         struct lsa_ObjectAttribute objectattr;
201         struct lsa_OpenPolicy2 openpolicy;
202         struct lsa_QueryInfoPolicy query;
203         struct policy_handle handle;
204         struct lsa_Close close_handle;
205
206         if ((mem_ctx = talloc_init("bindtest")) == NULL) {
207                 d_printf("talloc_init failed\n");
208                 return False;
209         }
210
211         lsa_pipe = dcerpc_pipe_init(mem_ctx,
212                                     cli->transport->socket->event.ctx);
213         if (lsa_pipe == NULL) {
214                 d_printf("dcerpc_pipe_init failed\n");
215                 goto done;
216         }
217
218         status = dcerpc_pipe_open_smb(lsa_pipe->conn, cli->tree, "\\lsarpc");
219         if (!NT_STATUS_IS_OK(status)) {
220                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
221                          nt_errstr(status));
222                 goto done;
223         }
224
225         status = dcerpc_bind_auth(lsa_pipe, &dcerpc_table_lsarpc,
226                                   credentials, auth_type, auth_level,
227                                   NULL);
228         if (!NT_STATUS_IS_OK(status)) {
229                 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
230                 goto done;
231         }
232
233         openpolicy.in.system_name =talloc_asprintf(
234                 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
235         ZERO_STRUCT(objectattr);
236         openpolicy.in.attr = &objectattr;
237         openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
238         openpolicy.out.handle = &handle;
239
240         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
241
242         if (!NT_STATUS_IS_OK(status)) {
243                 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
244                          nt_errstr(status));
245                 goto done;
246         }
247
248         query.in.handle = &handle;
249         query.in.level = LSA_POLICY_INFO_DOMAIN;
250
251         status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
252         if (!NT_STATUS_IS_OK(status)) {
253                 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
254                          nt_errstr(status));
255                 goto done;
256         }
257
258         close_handle.in.handle = &handle;
259         close_handle.out.handle = &handle;
260
261         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
262         if (!NT_STATUS_IS_OK(status)) {
263                 d_printf("dcerpc_lsa_Close failed: %s\n",
264                          nt_errstr(status));
265                 goto done;
266         }
267
268         ret = True;
269  done:
270         talloc_free(mem_ctx);
271         return ret;
272 }
273
274 /*
275  * test authenticated RPC binds with the variants Samba3 does support
276  */
277
278 BOOL torture_bind_samba3(struct torture_context *torture) 
279 {
280         TALLOC_CTX *mem_ctx;
281         NTSTATUS status;
282         BOOL ret = False;
283         struct smbcli_state *cli;
284
285         mem_ctx = talloc_init("torture_bind_authcontext");
286
287         if (mem_ctx == NULL) {
288                 d_printf("talloc_init failed\n");
289                 return False;
290         }
291
292         status = smbcli_full_connection(mem_ctx, &cli,
293                                         lp_parm_string(-1, "torture", "host"),
294                                         "IPC$", NULL, cmdline_credentials,
295                                         NULL);
296         if (!NT_STATUS_IS_OK(status)) {
297                 d_printf("smbcli_full_connection failed: %s\n",
298                          nt_errstr(status));
299                 goto done;
300         }
301
302         ret = True;
303
304         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
305                         DCERPC_AUTH_LEVEL_INTEGRITY);
306         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
307                         DCERPC_AUTH_LEVEL_PRIVACY);
308         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
309                         DCERPC_AUTH_LEVEL_INTEGRITY);
310         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
311                         DCERPC_AUTH_LEVEL_PRIVACY);
312
313  done:
314         talloc_free(mem_ctx);
315         return ret;
316 }
317
318 /*
319  * Lookup or create a user and return all necessary info
320  */
321
322 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
323                                TALLOC_CTX *mem_ctx,
324                                struct cli_credentials *admin_creds,
325                                uint8_t auth_type,
326                                uint8_t auth_level,
327                                const char *username,
328                                char **domain,
329                                struct dcerpc_pipe **result_pipe,
330                                struct policy_handle **result_handle,
331                                struct dom_sid **sid)
332 {
333         struct dcerpc_pipe *samr_pipe;
334         NTSTATUS status;
335         struct policy_handle conn_handle;
336         struct policy_handle domain_handle;
337         struct policy_handle *user_handle;
338         struct samr_Connect2 conn;
339         struct samr_EnumDomains enumdom;
340         uint32_t resume_handle = 0;
341         struct samr_LookupDomain l;
342         int dom_idx;
343         struct lsa_String domain_name;
344         struct lsa_String user_name;
345         struct samr_OpenDomain o;
346         struct samr_CreateUser2 c;
347         uint32_t user_rid,access_granted;
348
349         samr_pipe = dcerpc_pipe_init(mem_ctx,
350                                      cli->transport->socket->event.ctx);
351         if (samr_pipe == NULL) {
352                 d_printf("dcerpc_pipe_init failed\n");
353                 status = NT_STATUS_NO_MEMORY;
354                 goto fail;
355         }
356
357         status = dcerpc_pipe_open_smb(samr_pipe->conn, cli->tree, "\\samr");
358         if (!NT_STATUS_IS_OK(status)) {
359                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
360                          nt_errstr(status));
361                 goto fail;
362         }
363
364         if (admin_creds != NULL) {
365                 status = dcerpc_bind_auth(samr_pipe, &dcerpc_table_samr,
366                                           admin_creds, auth_type, auth_level,
367                                           NULL);
368                 if (!NT_STATUS_IS_OK(status)) {
369                         d_printf("dcerpc_bind_auth failed: %s\n",
370                                  nt_errstr(status));
371                         goto fail;
372                 }
373         } else {
374                 /* We must have an authenticated SMB connection */
375                 status = dcerpc_bind_auth_none(samr_pipe, &dcerpc_table_samr);
376                 if (!NT_STATUS_IS_OK(status)) {
377                         d_printf("dcerpc_bind_auth_none failed: %s\n",
378                                  nt_errstr(status));
379                         goto fail;
380                 }
381         }
382
383         conn.in.system_name = talloc_asprintf(
384                 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
385         conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
386         conn.out.connect_handle = &conn_handle;
387
388         status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
389         if (!NT_STATUS_IS_OK(status)) {
390                 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
391                 goto fail;
392         }
393
394         enumdom.in.connect_handle = &conn_handle;
395         enumdom.in.resume_handle = &resume_handle;
396         enumdom.in.buf_size = (uint32_t)-1;
397         enumdom.out.resume_handle = &resume_handle;
398
399         status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
400         if (!NT_STATUS_IS_OK(status)) {
401                 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
402                 goto fail;
403         }
404
405         if (enumdom.out.num_entries != 2) {
406                 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
407                          enumdom.out.num_entries);
408                 status = NT_STATUS_UNSUCCESSFUL;
409                 goto fail;
410         }
411
412         dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
413                            "builtin") ? 1:0;
414
415         l.in.connect_handle = &conn_handle;
416         domain_name.string = enumdom.out.sam->entries[0].name.string;
417         *domain = talloc_strdup(mem_ctx, domain_name.string);
418         l.in.domain_name = &domain_name;
419
420         status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
421         if (!NT_STATUS_IS_OK(status)) {
422                 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
423                 goto fail;
424         }
425
426         o.in.connect_handle = &conn_handle;
427         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
428         o.in.sid = l.out.sid;
429         o.out.domain_handle = &domain_handle;
430
431         status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
432         if (!NT_STATUS_IS_OK(status)) {
433                 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
434                 goto fail;
435         }
436
437         c.in.domain_handle = &domain_handle;
438         user_name.string = username;
439         c.in.account_name = &user_name;
440         c.in.acct_flags = ACB_NORMAL;
441         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
442         user_handle = talloc(mem_ctx, struct policy_handle);
443         c.out.user_handle = user_handle;
444         c.out.access_granted = &access_granted;
445         c.out.rid = &user_rid;
446
447         status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
448
449         if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
450                 struct samr_LookupNames ln;
451                 struct samr_OpenUser ou;
452
453                 ln.in.domain_handle = &domain_handle;
454                 ln.in.num_names = 1;
455                 ln.in.names = &user_name;
456
457                 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
458                 if (!NT_STATUS_IS_OK(status)) {
459                         d_printf("samr_LookupNames failed: %s\n",
460                                  nt_errstr(status));
461                         goto fail;
462                 }
463
464                 ou.in.domain_handle = &domain_handle;
465                 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
466                 user_rid = ou.in.rid = ln.out.rids.ids[0];
467                 ou.out.user_handle = user_handle;
468
469                 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
470                 if (!NT_STATUS_IS_OK(status)) {
471                         d_printf("samr_OpenUser failed: %s\n",
472                                  nt_errstr(status));
473                         goto fail;
474                 }
475         }
476
477         if (!NT_STATUS_IS_OK(status)) {
478                 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
479                 goto fail;
480         }
481
482         *result_pipe = samr_pipe;
483         *result_handle = user_handle;
484         if (sid != NULL) {
485                 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
486         }
487         return NT_STATUS_OK;
488
489  fail:
490         return status;
491 }
492
493 /*
494  * Create a test user
495  */
496
497 static BOOL create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
498                         struct cli_credentials *admin_creds,
499                         const char *username, const char *password,
500                         char **domain_name,
501                         struct dom_sid **user_sid)
502 {
503         TALLOC_CTX *tmp_ctx;
504         NTSTATUS status;
505         struct dcerpc_pipe *samr_pipe;
506         struct policy_handle *wks_handle;
507         BOOL ret = False;
508
509         if (!(tmp_ctx = talloc_new(mem_ctx))) {
510                 d_printf("talloc_init failed\n");
511                 return False;
512         }
513
514         status = get_usr_handle(cli, tmp_ctx, admin_creds,
515                                 DCERPC_AUTH_TYPE_NTLMSSP,
516                                 DCERPC_AUTH_LEVEL_INTEGRITY,
517                                 username, domain_name, &samr_pipe, &wks_handle,
518                                 user_sid);
519         if (!NT_STATUS_IS_OK(status)) {
520                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
521                 goto done;
522         }
523
524         {
525                 struct samr_SetUserInfo2 sui2;
526                 struct samr_SetUserInfo sui;
527                 struct samr_QueryUserInfo qui;
528                 union samr_UserInfo u_info;
529                 DATA_BLOB session_key;
530
531                 encode_pw_buffer(u_info.info24.password.data, password,
532                                  STR_UNICODE);
533                 u_info.info24.pw_len =  strlen_m(password)*2;
534
535                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
536                 if (!NT_STATUS_IS_OK(status)) {
537                         d_printf("dcerpc_fetch_session_key failed\n");
538                         goto done;
539                 }
540                 arcfour_crypt_blob(u_info.info24.password.data, 516,
541                                    &session_key);
542                 sui2.in.user_handle = wks_handle;
543                 sui2.in.info = &u_info;
544                 sui2.in.level = 24;
545
546                 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
547                 if (!NT_STATUS_IS_OK(status)) {
548                         d_printf("samr_SetUserInfo(24) failed: %s\n",
549                                  nt_errstr(status));
550                         goto done;
551                 }
552
553                 u_info.info16.acct_flags = ACB_NORMAL;
554                 sui.in.user_handle = wks_handle;
555                 sui.in.info = &u_info;
556                 sui.in.level = 16;
557
558                 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
559                 if (!NT_STATUS_IS_OK(status)) {
560                         d_printf("samr_SetUserInfo(16) failed\n");
561                         goto done;
562                 }
563
564                 qui.in.user_handle = wks_handle;
565                 qui.in.level = 21;
566
567                 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
568                 if (!NT_STATUS_IS_OK(status)) {
569                         d_printf("samr_QueryUserInfo(21) failed\n");
570                         goto done;
571                 }
572
573                 qui.out.info->info21.allow_password_change = 0;
574                 qui.out.info->info21.force_password_change = 0;
575                 qui.out.info->info21.account_name.string = NULL;
576                 qui.out.info->info21.rid = 0;
577                 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
578
579                 u_info.info21 = qui.out.info->info21;
580                 sui.in.user_handle = wks_handle;
581                 sui.in.info = &u_info;
582                 sui.in.level = 21;
583
584                 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
585                 if (!NT_STATUS_IS_OK(status)) {
586                         d_printf("samr_SetUserInfo(21) failed\n");
587                         goto done;
588                 }
589         }
590
591         *domain_name= talloc_steal(mem_ctx, *domain_name);
592         *user_sid = talloc_steal(mem_ctx, *user_sid);
593         ret = True;
594  done:
595         talloc_free(tmp_ctx);
596         return ret;
597 }
598
599 /*
600  * Delete a test user
601  */
602
603 static BOOL delete_user(struct smbcli_state *cli,
604                         struct cli_credentials *admin_creds,
605                         const char *username)
606 {
607         TALLOC_CTX *mem_ctx;
608         NTSTATUS status;
609         char *dom_name;
610         struct dcerpc_pipe *samr_pipe;
611         struct policy_handle *user_handle;
612         BOOL ret = False;
613
614         if ((mem_ctx = talloc_init("leave")) == NULL) {
615                 d_printf("talloc_init failed\n");
616                 return False;
617         }
618
619         status = get_usr_handle(cli, mem_ctx, admin_creds,
620                                 DCERPC_AUTH_TYPE_NTLMSSP,
621                                 DCERPC_AUTH_LEVEL_INTEGRITY,
622                                 username, &dom_name, &samr_pipe,
623                                 &user_handle, NULL);
624
625         if (!NT_STATUS_IS_OK(status)) {
626                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
627                 goto done;
628         }
629
630         {
631                 struct samr_DeleteUser d;
632
633                 d.in.user_handle = user_handle;
634                 d.out.user_handle = user_handle;
635
636                 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
637                 if (!NT_STATUS_IS_OK(status)) {
638                         d_printf("samr_DeleteUser failed\n");
639                         goto done;
640                 }
641         }
642
643         ret = True;
644
645  done:
646         talloc_free(mem_ctx);
647         return ret;
648 }
649
650 /*
651  * Do a Samba3-style join
652  */
653
654 static BOOL join3(struct smbcli_state *cli,
655                   BOOL use_level25,
656                   struct cli_credentials *admin_creds,
657                   struct cli_credentials *wks_creds)
658 {
659         TALLOC_CTX *mem_ctx;
660         NTSTATUS status;
661         char *dom_name;
662         struct dcerpc_pipe *samr_pipe;
663         struct policy_handle *wks_handle;
664         BOOL ret = False;
665
666         if ((mem_ctx = talloc_init("join3")) == NULL) {
667                 d_printf("talloc_init failed\n");
668                 return False;
669         }
670
671         status = get_usr_handle(
672                 cli, mem_ctx, admin_creds,
673                 DCERPC_AUTH_TYPE_NTLMSSP,
674                 DCERPC_AUTH_LEVEL_PRIVACY,
675                 talloc_asprintf(mem_ctx, "%s$",
676                                 cli_credentials_get_workstation(wks_creds)),
677                 &dom_name, &samr_pipe, &wks_handle, NULL);
678
679         if (!NT_STATUS_IS_OK(status)) {
680                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
681                 goto done;
682         }
683
684         cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
685
686         if (use_level25) {
687                 struct samr_SetUserInfo2 sui2;
688                 union samr_UserInfo u_info;
689                 struct samr_UserInfo21 *i21 = &u_info.info25.info;
690                 DATA_BLOB session_key;
691                 DATA_BLOB confounded_session_key = data_blob_talloc(
692                         mem_ctx, NULL, 16);
693                 struct MD5Context ctx;
694                 uint8_t confounder[16];
695
696                 ZERO_STRUCT(u_info);
697
698                 i21->full_name.string = talloc_asprintf(
699                         mem_ctx, "%s$",
700                         cli_credentials_get_workstation(wks_creds));
701                 i21->acct_flags = ACB_WSTRUST;
702                 i21->fields_present = SAMR_FIELD_FULL_NAME |
703                         SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
704
705                 encode_pw_buffer(u_info.info25.password.data,
706                                  cli_credentials_get_password(wks_creds),
707                                  STR_UNICODE);
708                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
709                 if (!NT_STATUS_IS_OK(status)) {
710                         d_printf("dcerpc_fetch_session_key failed: %s\n",
711                                  nt_errstr(status));
712                         goto done;
713                 }
714                 generate_random_buffer((uint8_t *)confounder, 16);
715
716                 MD5Init(&ctx);
717                 MD5Update(&ctx, confounder, 16);
718                 MD5Update(&ctx, session_key.data, session_key.length);
719                 MD5Final(confounded_session_key.data, &ctx);
720
721                 arcfour_crypt_blob(u_info.info25.password.data, 516,
722                                    &confounded_session_key);
723                 memcpy(&u_info.info25.password.data[516], confounder, 16);
724
725                 sui2.in.user_handle = wks_handle;
726                 sui2.in.level = 25;
727                 sui2.in.info = &u_info;
728
729                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
730                 if (!NT_STATUS_IS_OK(status)) {
731                         d_printf("samr_SetUserInfo2(25) failed: %s\n",
732                                  nt_errstr(status));
733                         goto done;
734                 }
735         } else {
736                 struct samr_SetUserInfo2 sui2;
737                 struct samr_SetUserInfo sui;
738                 union samr_UserInfo u_info;
739                 DATA_BLOB session_key;
740
741                 encode_pw_buffer(u_info.info24.password.data,
742                                  cli_credentials_get_password(wks_creds),
743                                  STR_UNICODE);
744                 u_info.info24.pw_len =
745                         strlen_m(cli_credentials_get_password(wks_creds))*2;
746
747                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
748                 if (!NT_STATUS_IS_OK(status)) {
749                         d_printf("dcerpc_fetch_session_key failed\n");
750                         goto done;
751                 }
752                 arcfour_crypt_blob(u_info.info24.password.data, 516,
753                                    &session_key);
754                 sui2.in.user_handle = wks_handle;
755                 sui2.in.info = &u_info;
756                 sui2.in.level = 24;
757
758                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
759                 if (!NT_STATUS_IS_OK(status)) {
760                         d_printf("samr_SetUserInfo(24) failed: %s\n",
761                                  nt_errstr(status));
762                         goto done;
763                 }
764
765                 u_info.info16.acct_flags = ACB_WSTRUST;
766                 sui.in.user_handle = wks_handle;
767                 sui.in.info = &u_info;
768                 sui.in.level = 16;
769
770                 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
771                 if (!NT_STATUS_IS_OK(status)) {
772                         d_printf("samr_SetUserInfo(16) failed\n");
773                         goto done;
774                 }
775         }
776
777         ret = True;
778
779  done:
780         talloc_free(mem_ctx);
781         return ret;
782 }
783
784 /*
785  * Do a ReqChallenge/Auth2 and get the wks creds
786  */
787
788 static BOOL auth2(struct smbcli_state *cli,
789                   struct cli_credentials *wks_cred)
790 {
791         TALLOC_CTX *mem_ctx;
792         struct dcerpc_pipe *net_pipe;
793         BOOL result = False;
794         NTSTATUS status;
795         struct netr_ServerReqChallenge r;
796         struct netr_Credential netr_cli_creds;
797         struct netr_Credential netr_srv_creds;
798         uint32_t negotiate_flags;
799         struct netr_ServerAuthenticate2 a;
800         struct creds_CredentialState *creds_state;
801         struct netr_Credential netr_cred;
802         struct samr_Password mach_pw;
803
804         mem_ctx = talloc_new(NULL);
805         if (mem_ctx == NULL) {
806                 d_printf("talloc_new failed\n");
807                 return False;
808         }
809
810         net_pipe = dcerpc_pipe_init(mem_ctx,
811                                     cli->transport->socket->event.ctx);
812         if (net_pipe == NULL) {
813                 d_printf("dcerpc_pipe_init failed\n");
814                 goto done;
815         }
816
817         status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
818         if (!NT_STATUS_IS_OK(status)) {
819                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
820                          nt_errstr(status));
821                 goto done;
822         }
823
824         status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
825         if (!NT_STATUS_IS_OK(status)) {
826                 d_printf("dcerpc_bind_auth_none failed: %s\n",
827                          nt_errstr(status));
828                 goto done;
829         }
830
831         r.in.computer_name = cli_credentials_get_workstation(wks_cred);
832         r.in.server_name = talloc_asprintf(
833                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
834         if (r.in.server_name == NULL) {
835                 d_printf("talloc_asprintf failed\n");
836                 goto done;
837         }
838         generate_random_buffer(netr_cli_creds.data,
839                                sizeof(netr_cli_creds.data));
840         r.in.credentials = &netr_cli_creds;
841         r.out.credentials = &netr_srv_creds;
842
843         status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
844         if (!NT_STATUS_IS_OK(status)) {
845                 d_printf("netr_ServerReqChallenge failed: %s\n",
846                          nt_errstr(status));
847                 goto done;
848         }
849
850         negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
851         E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
852
853         creds_state = talloc(mem_ctx, struct creds_CredentialState);
854         creds_client_init(creds_state, r.in.credentials,
855                           r.out.credentials, &mach_pw,
856                           &netr_cred, negotiate_flags);
857
858         a.in.server_name = talloc_asprintf(
859                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
860         a.in.account_name = talloc_asprintf(
861                 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
862         a.in.computer_name = cli_credentials_get_workstation(wks_cred);
863         a.in.secure_channel_type = SEC_CHAN_WKSTA;
864         a.in.negotiate_flags = &negotiate_flags;
865         a.out.negotiate_flags = &negotiate_flags;
866         a.in.credentials = &netr_cred;
867         a.out.credentials = &netr_cred;
868
869         status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
870         if (!NT_STATUS_IS_OK(status)) {
871                 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
872                          nt_errstr(status));
873                 goto done;
874         }
875
876         if (!creds_client_check(creds_state, a.out.credentials)) {
877                 d_printf("creds_client_check failed\n");
878                 goto done;
879         }
880
881         cli_credentials_set_netlogon_creds(wks_cred, creds_state);
882
883         result = True;
884
885  done:
886         talloc_free(mem_ctx);
887         return result;
888 }
889
890 /*
891  * Do a couple of schannel protected Netlogon ops: Interactive and Network
892  * login, and change the wks password
893  */
894
895 static BOOL schan(struct smbcli_state *cli,
896                   struct cli_credentials *wks_creds,
897                   struct cli_credentials *user_creds)
898 {
899         TALLOC_CTX *mem_ctx;
900         NTSTATUS status;
901         BOOL ret = False;
902         struct dcerpc_pipe *net_pipe;
903         int i;
904         
905         mem_ctx = talloc_new(NULL);
906         if (mem_ctx == NULL) {
907                 d_printf("talloc_new failed\n");
908                 return False;
909         }
910
911         net_pipe = dcerpc_pipe_init(mem_ctx,
912                                     cli->transport->socket->event.ctx);
913         if (net_pipe == NULL) {
914                 d_printf("dcerpc_pipe_init failed\n");
915                 goto done;
916         }
917
918         status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
919         if (!NT_STATUS_IS_OK(status)) {
920                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
921                          nt_errstr(status));
922                 goto done;
923         }
924
925 #if 0
926         net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
927                 DCERPC_DEBUG_PRINT_OUT;
928 #endif
929 #if 1
930         net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
931         status = dcerpc_bind_auth(net_pipe, &dcerpc_table_netlogon,
932                                   wks_creds, DCERPC_AUTH_TYPE_SCHANNEL,
933                                   DCERPC_AUTH_LEVEL_PRIVACY,
934                                   NULL);
935 #else
936         status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
937 #endif
938         if (!NT_STATUS_IS_OK(status)) {
939                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
940                 goto done;
941         }
942
943
944         for (i=2; i<4; i++) {
945                 int flags;
946                 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
947                 struct creds_CredentialState *creds_state;
948                 struct netr_Authenticator netr_auth, netr_auth2;
949                 struct netr_NetworkInfo ninfo;
950                 struct netr_PasswordInfo pinfo;
951                 struct netr_LogonSamLogon r;
952
953                 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
954                         CLI_CRED_NTLMv2_AUTH;
955
956                 chal = data_blob_talloc(mem_ctx, NULL, 8);
957                 if (chal.data == NULL) {
958                         d_printf("data_blob_talloc failed\n");
959                         goto done;
960                 }
961
962                 generate_random_buffer(chal.data, chal.length);
963                 names_blob = NTLMv2_generate_names_blob(
964                         mem_ctx, cli_credentials_get_workstation(user_creds),
965                         cli_credentials_get_domain(user_creds));
966                 status = cli_credentials_get_ntlm_response(
967                         user_creds, mem_ctx, &flags, chal, names_blob,
968                         &lm_resp, &nt_resp, NULL, NULL);
969                 if (!NT_STATUS_IS_OK(status)) {
970                         d_printf("cli_credentials_get_ntlm_response failed:"
971                                  " %s\n", nt_errstr(status));
972                         goto done;
973                 }
974
975                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
976                 creds_client_authenticator(creds_state, &netr_auth);
977
978                 ninfo.identity_info.account_name.string =
979                         cli_credentials_get_username(user_creds);
980                 ninfo.identity_info.domain_name.string =
981                         cli_credentials_get_domain(user_creds);
982                 ninfo.identity_info.parameter_control = 0;
983                 ninfo.identity_info.logon_id_low = 0;
984                 ninfo.identity_info.logon_id_high = 0;
985                 ninfo.identity_info.workstation.string =
986                         cli_credentials_get_workstation(user_creds);
987                 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
988                 ninfo.nt.length = nt_resp.length;
989                 ninfo.nt.data = nt_resp.data;
990                 ninfo.lm.length = lm_resp.length;
991                 ninfo.lm.data = lm_resp.data;
992
993                 r.in.server_name = talloc_asprintf(
994                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
995                 ZERO_STRUCT(netr_auth2);
996                 r.in.computer_name =
997                         cli_credentials_get_workstation(wks_creds);
998                 r.in.credential = &netr_auth;
999                 r.in.return_authenticator = &netr_auth2;
1000                 r.in.logon_level = 2;
1001                 r.in.validation_level = i;
1002                 r.in.logon.network = &ninfo;
1003                 r.out.return_authenticator = NULL;
1004
1005                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1006                 if (!NT_STATUS_IS_OK(status)) {
1007                         d_printf("netr_LogonSamLogon failed: %s\n",
1008                                  nt_errstr(status));
1009                         goto done;
1010                 }
1011
1012                 if ((r.out.return_authenticator == NULL) ||
1013                     (!creds_client_check(creds_state,
1014                                          &r.out.return_authenticator->cred))) {
1015                         d_printf("Credentials check failed!\n");
1016                         goto done;
1017                 }
1018
1019                 creds_client_authenticator(creds_state, &netr_auth);
1020
1021                 pinfo.identity_info = ninfo.identity_info;
1022                 ZERO_STRUCT(pinfo.lmpassword.hash);
1023                 E_md4hash(cli_credentials_get_password(user_creds),
1024                           pinfo.ntpassword.hash);
1025                 session_key = data_blob_talloc(mem_ctx,
1026                                                creds_state->session_key, 16);
1027                 arcfour_crypt_blob(pinfo.ntpassword.hash,
1028                                    sizeof(pinfo.ntpassword.hash),
1029                                    &session_key);
1030
1031                 r.in.logon_level = 1;
1032                 r.in.logon.password = &pinfo;
1033                 r.out.return_authenticator = NULL;
1034
1035                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1036                 if (!NT_STATUS_IS_OK(status)) {
1037                         d_printf("netr_LogonSamLogon failed: %s\n",
1038                                  nt_errstr(status));
1039                         goto done;
1040                 }
1041
1042                 if ((r.out.return_authenticator == NULL) ||
1043                     (!creds_client_check(creds_state,
1044                                          &r.out.return_authenticator->cred))) {
1045                         d_printf("Credentials check failed!\n");
1046                         goto done;
1047                 }
1048         }
1049
1050         {
1051                 struct netr_ServerPasswordSet s;
1052                 char *password = generate_random_str(wks_creds, 8);
1053                 struct creds_CredentialState *creds_state;
1054
1055                 s.in.server_name = talloc_asprintf(
1056                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1057                 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1058                 s.in.account_name = talloc_asprintf(
1059                         mem_ctx, "%s$", s.in.computer_name);
1060                 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1061                 E_md4hash(password, s.in.new_password.hash);
1062
1063                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1064                 creds_des_encrypt(creds_state, &s.in.new_password);
1065                 creds_client_authenticator(creds_state, &s.in.credential);
1066
1067                 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1068                 if (!NT_STATUS_IS_OK(status)) {
1069                         printf("ServerPasswordSet - %s\n", nt_errstr(status));
1070                         goto done;
1071                 }
1072
1073                 if (!creds_client_check(creds_state,
1074                                         &s.out.return_authenticator.cred)) {
1075                         printf("Credential chaining failed\n");
1076                 }
1077
1078                 cli_credentials_set_password(wks_creds, password,
1079                                              CRED_SPECIFIED);
1080         }
1081
1082         ret = True;
1083  done:
1084         talloc_free(mem_ctx);
1085         return ret;
1086 }
1087
1088 /*
1089  * Delete the wks account again
1090  */
1091
1092 static BOOL leave(struct smbcli_state *cli,
1093                   struct cli_credentials *admin_creds,
1094                   struct cli_credentials *wks_creds)
1095 {
1096         char *wks_name = talloc_asprintf(
1097                 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1098         BOOL ret;
1099
1100         ret = delete_user(cli, admin_creds, wks_name);
1101         talloc_free(wks_name);
1102         return ret;
1103 }
1104
1105 /*
1106  * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1107  */
1108
1109 BOOL torture_netlogon_samba3(struct torture_context *torture)
1110 {
1111         TALLOC_CTX *mem_ctx;
1112         NTSTATUS status;
1113         BOOL ret = False;
1114         struct smbcli_state *cli;
1115         struct cli_credentials *anon_creds;
1116         struct cli_credentials *wks_creds;
1117         const char *wks_name;
1118         int i;
1119
1120         wks_name = lp_parm_string(-1, "torture", "wksname");
1121         if (wks_name == NULL) {
1122                 wks_name = get_myname();
1123         }
1124
1125         mem_ctx = talloc_init("torture_bind_authcontext");
1126
1127         if (mem_ctx == NULL) {
1128                 d_printf("talloc_init failed\n");
1129                 return False;
1130         }
1131
1132         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1133                 d_printf("create_anon_creds failed\n");
1134                 goto done;
1135         }
1136
1137         status = smbcli_full_connection(mem_ctx, &cli,
1138                                         lp_parm_string(-1, "torture", "host"),
1139                                         "IPC$", NULL, anon_creds, NULL);
1140         if (!NT_STATUS_IS_OK(status)) {
1141                 d_printf("smbcli_full_connection failed: %s\n",
1142                          nt_errstr(status));
1143                 goto done;
1144         }
1145
1146         wks_creds = cli_credentials_init(mem_ctx);
1147         if (wks_creds == NULL) {
1148                 d_printf("cli_credentials_init failed\n");
1149                 goto done;
1150         }
1151
1152         cli_credentials_set_conf(wks_creds);
1153         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1154         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1155         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1156         cli_credentials_set_password(wks_creds,
1157                                      generate_random_str(wks_creds, 8),
1158                                      CRED_SPECIFIED);
1159
1160         if (!join3(cli, False, cmdline_credentials, wks_creds)) {
1161                 d_printf("join failed\n");
1162                 goto done;
1163         }
1164
1165         cli_credentials_set_domain(
1166                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1167                 CRED_SPECIFIED);
1168
1169         for (i=0; i<2; i++) {
1170
1171                 /* Do this more than once, the routine "schan" changes
1172                  * the workstation password using the netlogon
1173                  * password change routine */
1174
1175                 int j;
1176
1177                 if (!auth2(cli, wks_creds)) {
1178                         d_printf("auth2 failed\n");
1179                         goto done;
1180                 }
1181
1182                 for (j=0; j<2; j++) {
1183                         if (!schan(cli, wks_creds, cmdline_credentials)) {
1184                                 d_printf("schan failed\n");
1185                                 goto done;
1186                         }
1187                 }
1188         }
1189
1190         if (!leave(cli, cmdline_credentials, wks_creds)) {
1191                 d_printf("leave failed\n");
1192                 goto done;
1193         }
1194
1195         ret = True;
1196
1197  done:
1198         talloc_free(mem_ctx);
1199         return ret;
1200 }
1201
1202 /*
1203  * Do a simple join, testjoin and leave using specified smb and samr
1204  * credentials
1205  */
1206
1207 static BOOL test_join3(TALLOC_CTX *mem_ctx,
1208                        BOOL use_level25,
1209                        struct cli_credentials *smb_creds,
1210                        struct cli_credentials *samr_creds,
1211                        const char *wks_name)
1212 {
1213         NTSTATUS status;
1214         BOOL ret = False;
1215         struct smbcli_state *cli;
1216         struct cli_credentials *wks_creds;
1217
1218         status = smbcli_full_connection(mem_ctx, &cli,
1219                                         lp_parm_string(-1, "torture", "host"),
1220                                         "IPC$", NULL, smb_creds, NULL);
1221         if (!NT_STATUS_IS_OK(status)) {
1222                 d_printf("smbcli_full_connection failed: %s\n",
1223                          nt_errstr(status));
1224                 goto done;
1225         }
1226
1227         wks_creds = cli_credentials_init(cli);
1228         if (wks_creds == NULL) {
1229                 d_printf("cli_credentials_init failed\n");
1230                 goto done;
1231         }
1232
1233         cli_credentials_set_conf(wks_creds);
1234         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1235         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1236         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1237         cli_credentials_set_password(wks_creds,
1238                                      generate_random_str(wks_creds, 8),
1239                                      CRED_SPECIFIED);
1240
1241         if (!join3(cli, use_level25, samr_creds, wks_creds)) {
1242                 d_printf("join failed\n");
1243                 goto done;
1244         }
1245
1246         cli_credentials_set_domain(
1247                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1248                 CRED_SPECIFIED);
1249
1250         if (!auth2(cli, wks_creds)) {
1251                 d_printf("auth2 failed\n");
1252                 goto done;
1253         }
1254
1255         if (!leave(cli, samr_creds, wks_creds)) {
1256                 d_printf("leave failed\n");
1257                 goto done;
1258         }
1259
1260         talloc_free(cli);
1261
1262         ret = True;
1263
1264  done:
1265         return ret;
1266 }
1267
1268 /*
1269  * Test the different session key variants. Do it by joining, this uses the
1270  * session key in the setpassword routine. Test the join by doing the auth2.
1271  */
1272
1273 BOOL torture_samba3_sessionkey(struct torture_context *torture)
1274 {
1275         TALLOC_CTX *mem_ctx;
1276         BOOL ret = False;
1277         struct cli_credentials *anon_creds;
1278         const char *wks_name;
1279
1280         wks_name = lp_parm_string(-1, "torture", "wksname");
1281         if (wks_name == NULL) {
1282                 wks_name = get_myname();
1283         }
1284
1285         mem_ctx = talloc_init("torture_samba3_sessionkey");
1286
1287         if (mem_ctx == NULL) {
1288                 d_printf("talloc_init failed\n");
1289                 return False;
1290         }
1291
1292         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1293                 d_printf("create_anon_creds failed\n");
1294                 goto done;
1295         }
1296
1297         ret = True;
1298
1299         if (!lp_parm_bool(-1, "target", "samba3", False)) {
1300
1301                 /* Samba3 in the build farm right now does this happily. Need
1302                  * to fix :-) */
1303
1304                 if (test_join3(mem_ctx, False, anon_creds, NULL, wks_name)) {
1305                         d_printf("join using anonymous bind on an anonymous smb "
1306                                  "connection succeeded -- HUH??\n");
1307                         ret = False;
1308                 }
1309         }
1310
1311         if (!test_join3(mem_ctx, False, anon_creds, cmdline_credentials,
1312                         wks_name)) {
1313                 d_printf("join using ntlmssp bind on an anonymous smb "
1314                          "connection failed\n");
1315                 ret = False;
1316         }
1317
1318         if (!test_join3(mem_ctx, False, cmdline_credentials, NULL, wks_name)) {
1319                 d_printf("join using anonymous bind on an authenticated smb "
1320                          "connection failed\n");
1321                 ret = False;
1322         }
1323
1324         if (!test_join3(mem_ctx, False, cmdline_credentials,
1325                         cmdline_credentials,
1326                         wks_name)) {
1327                 d_printf("join using ntlmssp bind on an authenticated smb "
1328                          "connection failed\n");
1329                 ret = False;
1330         }
1331
1332         /*
1333          * The following two are tests for setuserinfolevel 25
1334          */
1335
1336         if (!test_join3(mem_ctx, True, anon_creds, cmdline_credentials,
1337                         wks_name)) {
1338                 d_printf("join using ntlmssp bind on an anonymous smb "
1339                          "connection failed\n");
1340                 ret = False;
1341         }
1342
1343         if (!test_join3(mem_ctx, True, cmdline_credentials, NULL, wks_name)) {
1344                 d_printf("join using anonymous bind on an authenticated smb "
1345                          "connection failed\n");
1346                 ret = False;
1347         }
1348
1349  done:
1350
1351         return ret;
1352 }
1353
1354 /*
1355  * open pipe and bind, given an IPC$ context
1356  */
1357
1358 static struct dcerpc_pipe *pipe_bind_smb(TALLOC_CTX *mem_ctx,
1359                                          struct smbcli_tree *tree,
1360                                          const char *pipe_name,
1361                                          const struct dcerpc_interface_table *iface)
1362 {
1363         struct dcerpc_pipe *result;
1364         NTSTATUS status;
1365
1366         if (!(result = dcerpc_pipe_init(
1367                       mem_ctx, tree->session->transport->socket->event.ctx))) {
1368                 return NULL;
1369         }
1370
1371         status = dcerpc_pipe_open_smb(result->conn, tree, pipe_name);
1372         if (!NT_STATUS_IS_OK(status)) {
1373                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1374                          nt_errstr(status));
1375                 talloc_free(result);
1376                 return NULL;
1377         }
1378
1379         status = dcerpc_bind_auth_none(result, iface);
1380         if (!NT_STATUS_IS_OK(status)) {
1381                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1382                 talloc_free(result);
1383                 return NULL;
1384         }
1385
1386         return result;
1387 }
1388
1389 /*
1390  * Sane wrapper around lsa_LookupNames
1391  */
1392
1393 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1394                                 struct dcerpc_pipe *p,
1395                                 const char *name,
1396                                 const char *domain)
1397 {
1398         struct lsa_ObjectAttribute attr;
1399         struct lsa_QosInfo qos;
1400         struct lsa_OpenPolicy2 r;
1401         struct lsa_Close c;
1402         NTSTATUS status;
1403         struct policy_handle handle;
1404         struct lsa_LookupNames l;
1405         struct lsa_TransSidArray sids;
1406         struct lsa_String lsa_name;
1407         uint32_t count = 0;
1408         struct dom_sid *result;
1409         TALLOC_CTX *tmp_ctx;
1410
1411         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1412                 return NULL;
1413         }
1414
1415         qos.len = 0;
1416         qos.impersonation_level = 2;
1417         qos.context_mode = 1;
1418         qos.effective_only = 0;
1419
1420         attr.len = 0;
1421         attr.root_dir = NULL;
1422         attr.object_name = NULL;
1423         attr.attributes = 0;
1424         attr.sec_desc = NULL;
1425         attr.sec_qos = &qos;
1426
1427         r.in.system_name = "\\";
1428         r.in.attr = &attr;
1429         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1430         r.out.handle = &handle;
1431
1432         status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1433         if (!NT_STATUS_IS_OK(status)) {
1434                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1435                 talloc_free(tmp_ctx);
1436                 return NULL;
1437         }
1438
1439         sids.count = 0;
1440         sids.sids = NULL;
1441
1442         lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1443
1444         l.in.handle = &handle;
1445         l.in.num_names = 1;
1446         l.in.names = &lsa_name;
1447         l.in.sids = &sids;
1448         l.in.level = 1;
1449         l.in.count = &count;
1450         l.out.count = &count;
1451         l.out.sids = &sids;
1452
1453         status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1454         if (!NT_STATUS_IS_OK(status)) {
1455                 printf("LookupNames failed - %s\n", nt_errstr(status));
1456                 talloc_free(tmp_ctx);
1457                 return False;
1458         }
1459
1460         result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1461                                  l.out.sids->sids[0].rid);
1462
1463         c.in.handle = &handle;
1464         c.out.handle = &handle;
1465
1466         status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1467         if (!NT_STATUS_IS_OK(status)) {
1468                 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1469                 talloc_free(tmp_ctx);
1470                 return NULL;
1471         }
1472         
1473         talloc_free(tmp_ctx);
1474         return result;
1475 }
1476
1477 /*
1478  * Find out the user SID on this connection
1479  */
1480
1481 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1482 {
1483         struct dcerpc_pipe *lsa;
1484         struct lsa_GetUserName r;
1485         NTSTATUS status;
1486         struct lsa_StringPointer authority_name_p;
1487         struct dom_sid *result;
1488
1489         if (!(lsa = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1490                                   &dcerpc_table_lsarpc))) {
1491                 d_printf("Could not bind to LSA\n");
1492                 return NULL;
1493         }
1494
1495         r.in.system_name = "\\";
1496         r.in.account_name = NULL;
1497         authority_name_p.string = NULL;
1498         r.in.authority_name = &authority_name_p;
1499
1500         status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1501
1502         if (!NT_STATUS_IS_OK(status)) {
1503                 printf("GetUserName failed - %s\n", nt_errstr(status));
1504                 talloc_free(lsa);
1505                 return NULL;
1506         }
1507
1508         result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1509                           r.out.authority_name->string->string);
1510
1511         talloc_free(lsa);
1512         return result;
1513 }
1514
1515 /*
1516  * Do a tcon, given a session
1517  */
1518
1519 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1520                         struct smbcli_session *session,
1521                         const char *sharename,
1522                         struct smbcli_tree **res)
1523 {
1524         struct smbcli_tree *result;
1525         TALLOC_CTX *tmp_ctx;
1526         union smb_tcon tcon;
1527         NTSTATUS status;
1528
1529         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1530                 return NT_STATUS_NO_MEMORY;
1531         }
1532
1533         if (!(result = smbcli_tree_init(session, mem_ctx, False))) {
1534                 talloc_free(tmp_ctx);
1535                 return NT_STATUS_NO_MEMORY;
1536         }
1537
1538         tcon.generic.level = RAW_TCON_TCONX;
1539         tcon.tconx.in.flags = 0;
1540         tcon.tconx.in.password = data_blob(NULL, 0);
1541         tcon.tconx.in.path = sharename;
1542         tcon.tconx.in.device = "?????";
1543
1544         status = smb_raw_tcon(result, tmp_ctx, &tcon);
1545         if (!NT_STATUS_IS_OK(status)) {
1546                 d_printf("smb_raw_tcon failed: %s\n", nt_errstr(status));
1547                 talloc_free(tmp_ctx);
1548                 return status;
1549         }
1550
1551         result->tid = tcon.tconx.out.tid;
1552         result = talloc_steal(mem_ctx, result);
1553         talloc_free(tmp_ctx);
1554         *res = result;
1555         return NT_STATUS_OK;
1556 }
1557
1558 /*
1559  * Test the getusername behaviour
1560  */
1561
1562 BOOL torture_samba3_rpc_getusername(struct torture_context *torture)
1563 {
1564         NTSTATUS status;
1565         struct smbcli_state *cli;
1566         TALLOC_CTX *mem_ctx;
1567         BOOL ret = True;
1568         struct dom_sid *user_sid;
1569         struct dom_sid *created_sid;
1570         struct cli_credentials *anon_creds;
1571         struct cli_credentials *user_creds;
1572         char *domain_name;
1573
1574         if (!(mem_ctx = talloc_new(torture))) {
1575                 return False;
1576         }
1577
1578         status = smbcli_full_connection(
1579                 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1580                 "IPC$", NULL, cmdline_credentials, NULL);
1581         if (!NT_STATUS_IS_OK(status)) {
1582                 d_printf("smbcli_full_connection failed: %s\n",
1583                          nt_errstr(status));
1584                 ret = False;
1585                 goto done;
1586         }
1587
1588         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1589                 d_printf("whoami on auth'ed connection failed\n");
1590                 ret = False;
1591         }
1592
1593         talloc_free(cli);
1594
1595         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1596                 d_printf("create_anon_creds failed\n");
1597                 ret = False;
1598                 goto done;
1599         }
1600
1601         status = smbcli_full_connection(
1602                 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1603                 "IPC$", NULL, anon_creds, NULL);
1604         if (!NT_STATUS_IS_OK(status)) {
1605                 d_printf("anon smbcli_full_connection failed: %s\n",
1606                          nt_errstr(status));
1607                 ret = False;
1608                 goto done;
1609         }
1610
1611         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1612                 d_printf("whoami on anon connection failed\n");
1613                 ret = False;
1614                 goto done;
1615         }
1616
1617         if (!dom_sid_equal(user_sid,
1618                            dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1619                 d_printf("Anon lsa_GetUserName returned %s, expected S-1-5-7",
1620                          dom_sid_string(mem_ctx, user_sid));
1621                 ret = False;
1622         }
1623
1624         if (!(user_creds = cli_credentials_init(mem_ctx))) {
1625                 d_printf("cli_credentials_init failed\n");
1626                 ret = False;
1627                 goto done;
1628         }
1629
1630         cli_credentials_set_conf(user_creds);
1631         cli_credentials_set_username(user_creds, "torture_username",
1632                                      CRED_SPECIFIED);
1633         cli_credentials_set_password(user_creds,
1634                                      generate_random_str(user_creds, 8),
1635                                      CRED_SPECIFIED);
1636
1637         if (!create_user(mem_ctx, cli, cmdline_credentials,
1638                          cli_credentials_get_username(user_creds),
1639                          cli_credentials_get_password(user_creds),
1640                          &domain_name, &created_sid)) {
1641                 d_printf("create_user failed\n");
1642                 ret = False;
1643                 goto done;
1644         }
1645
1646         cli_credentials_set_domain(user_creds, domain_name,
1647                                    CRED_SPECIFIED);
1648
1649         {
1650                 struct smbcli_session *session2;
1651                 struct smb_composite_sesssetup setup;
1652                 struct smbcli_tree *tree;
1653
1654                 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
1655                 if (session2 == NULL) {
1656                         d_printf("smbcli_session_init failed\n");
1657                         goto done;
1658                 }
1659
1660                 setup.in.sesskey = cli->transport->negotiate.sesskey;
1661                 setup.in.capabilities = cli->transport->negotiate.capabilities;
1662                 setup.in.workgroup = "";
1663                 setup.in.credentials = user_creds;
1664
1665                 status = smb_composite_sesssetup(session2, &setup);
1666                 if (!NT_STATUS_IS_OK(status)) {
1667                         d_printf("anon session setup failed: %s\n",
1668                                  nt_errstr(status));
1669                         ret = False;
1670                         goto done;
1671                 }
1672
1673                 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1674                                                     "IPC$", &tree))) {
1675                         d_printf("secondary_tcon failed\n");
1676                         ret = False;
1677                         goto done;
1678                 }
1679
1680                 if (!(user_sid = whoami(mem_ctx, tree))) {
1681                         d_printf("whoami on user connection failed\n");
1682                         ret = False;
1683                         goto delete;
1684                 }
1685
1686                 talloc_free(tree);
1687         }
1688
1689         d_printf("Created %s, found %s\n",
1690                  dom_sid_string(mem_ctx, created_sid),
1691                  dom_sid_string(mem_ctx, user_sid));
1692
1693         if (!dom_sid_equal(created_sid, user_sid)) {
1694                 ret = False;
1695         }
1696
1697  delete:
1698         if (!delete_user(cli, cmdline_credentials,
1699                          cli_credentials_get_username(user_creds))) {
1700                 d_printf("delete_user failed\n");
1701                 ret = False;
1702         }
1703
1704  done:
1705         talloc_free(mem_ctx);
1706         return ret;
1707 }
1708
1709 static BOOL test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1710                                  const char *sharename)
1711 {
1712         NTSTATUS status;
1713         struct srvsvc_NetShareGetInfo r;
1714         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1715         int i;
1716         BOOL ret = True;
1717
1718         r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1719                                           dcerpc_server_name(p));
1720         r.in.share_name = sharename;
1721
1722         for (i=0;i<ARRAY_SIZE(levels);i++) {
1723                 r.in.level = levels[i];
1724
1725                 ZERO_STRUCT(r.out);
1726
1727                 printf("testing NetShareGetInfo level %u on share '%s'\n", 
1728                        r.in.level, r.in.share_name);
1729
1730                 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1731                 if (!NT_STATUS_IS_OK(status)) {
1732                         printf("NetShareGetInfo level %u on share '%s' failed"
1733                                " - %s\n", r.in.level, r.in.share_name,
1734                                nt_errstr(status));
1735                         ret = False;
1736                         continue;
1737                 }
1738                 if (!W_ERROR_IS_OK(r.out.result)) {
1739                         printf("NetShareGetInfo level %u on share '%s' failed "
1740                                "- %s\n", r.in.level, r.in.share_name,
1741                                win_errstr(r.out.result));
1742                         ret = False;
1743                         continue;
1744                 }
1745         }
1746
1747         return ret;
1748 }
1749
1750 static BOOL test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1751                               const char **one_sharename)
1752 {
1753         NTSTATUS status;
1754         struct srvsvc_NetShareEnum r;
1755         struct srvsvc_NetShareCtr0 c0;
1756         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1757         int i;
1758         BOOL ret = True;
1759
1760         r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1761         r.in.ctr.ctr0 = &c0;
1762         r.in.ctr.ctr0->count = 0;
1763         r.in.ctr.ctr0->array = NULL;
1764         r.in.max_buffer = (uint32_t)-1;
1765         r.in.resume_handle = NULL;
1766
1767         for (i=0;i<ARRAY_SIZE(levels);i++) {
1768                 r.in.level = levels[i];
1769
1770                 ZERO_STRUCT(r.out);
1771
1772                 printf("testing NetShareEnum level %u\n", r.in.level);
1773                 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1774                 if (!NT_STATUS_IS_OK(status)) {
1775                         printf("NetShareEnum level %u failed - %s\n",
1776                                r.in.level, nt_errstr(status));
1777                         ret = False;
1778                         continue;
1779                 }
1780                 if (!W_ERROR_IS_OK(r.out.result)) {
1781                         printf("NetShareEnum level %u failed - %s\n",
1782                                r.in.level, win_errstr(r.out.result));
1783                         continue;
1784                 }
1785                 if (r.in.level == 0) {
1786                         struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1787                         if (ctr->count > 0) {
1788                                 *one_sharename = ctr->array[0].name;
1789                         }
1790                 }
1791         }
1792
1793         return ret;
1794 }
1795
1796 BOOL torture_samba3_rpc_srvsvc(struct torture_context *torture)
1797 {
1798         struct dcerpc_pipe *p;
1799         TALLOC_CTX *mem_ctx;
1800         BOOL ret = True;
1801         const char *sharename = NULL;
1802         struct smbcli_state *cli;
1803
1804         if (!(mem_ctx = talloc_new(torture))) {
1805                 return False;
1806         }
1807
1808         if (!(torture_open_connection_share(
1809                       mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1810                       "IPC$", NULL))) {
1811                 talloc_free(mem_ctx);
1812                 return False;
1813         }
1814
1815         if (!(p = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1816                                 &dcerpc_table_srvsvc))) {
1817                 d_printf("could not bind to srvsvc pipe\n");
1818                 ret = False;
1819                 goto done;
1820         }
1821
1822         ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1823         if (sharename == NULL) {
1824                 printf("did not get sharename\n");
1825         } else {
1826                 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1827         }
1828
1829  done:
1830         talloc_free(mem_ctx);
1831         return ret;
1832 }
1833
1834 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1835                                                 struct smbcli_session *sess,
1836                                                 const char *sharename)
1837 {
1838         struct smbcli_tree *tree;
1839         TALLOC_CTX *tmp_ctx;
1840         struct dcerpc_pipe *p;
1841         NTSTATUS status;
1842         struct srvsvc_NetShareGetInfo r;
1843         struct security_descriptor *result;
1844
1845         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1846                 d_printf("talloc_new failed\n");
1847                 return NULL;
1848         }
1849
1850         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1851                 d_printf("secondary_tcon failed\n");
1852                 talloc_free(tmp_ctx);
1853                 return NULL;
1854         }
1855
1856         if (!(p = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1857                                 &dcerpc_table_srvsvc))) {
1858                 d_printf("could not bind to srvsvc pipe\n");
1859                 talloc_free(tmp_ctx);
1860                 return NULL;
1861         }
1862
1863 #if 0
1864         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1865 #endif
1866
1867         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1868                                           dcerpc_server_name(p));
1869         r.in.share_name = sharename;
1870         r.in.level = 502;
1871
1872         status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
1873         if (!NT_STATUS_IS_OK(status)) {
1874                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1875                          nt_errstr(status));
1876                 talloc_free(tmp_ctx);
1877                 return NULL;
1878         }
1879
1880         result = talloc_steal(mem_ctx, r.out.info.info502->sd);
1881         talloc_free(tmp_ctx);
1882         return result;
1883 }
1884
1885 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
1886                              struct smbcli_session *sess,
1887                              const char *sharename,
1888                              struct security_descriptor *sd)
1889 {
1890         struct smbcli_tree *tree;
1891         TALLOC_CTX *tmp_ctx;
1892         struct dcerpc_pipe *p;
1893         NTSTATUS status;
1894         struct sec_desc_buf i;
1895         struct srvsvc_NetShareSetInfo r;
1896         uint32_t error = 0;
1897
1898         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1899                 d_printf("talloc_new failed\n");
1900                 return NT_STATUS_NO_MEMORY;
1901         }
1902
1903         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1904                 d_printf("secondary_tcon failed\n");
1905                 talloc_free(tmp_ctx);
1906                 return NT_STATUS_UNSUCCESSFUL;
1907         }
1908
1909         if (!(p = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1910                                 &dcerpc_table_srvsvc))) {
1911                 d_printf("could not bind to srvsvc pipe\n");
1912                 talloc_free(tmp_ctx);
1913                 return NT_STATUS_UNSUCCESSFUL;
1914         }
1915
1916 #if 0
1917         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1918 #endif
1919
1920         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1921                                           dcerpc_server_name(p));
1922         r.in.share_name = sharename;
1923         r.in.level = 1501;
1924         i.sd = sd;
1925         r.in.info.info1501 = &i;
1926         r.in.parm_error = &error;
1927
1928         status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
1929         if (!NT_STATUS_IS_OK(status)) {
1930                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1931                          nt_errstr(status));
1932         }
1933
1934         talloc_free(tmp_ctx);
1935         return status;
1936 }
1937
1938 BOOL try_tcon(TALLOC_CTX *mem_ctx,
1939               struct security_descriptor *orig_sd,
1940               struct smbcli_session *session,
1941               const char *sharename, const struct dom_sid *user_sid,
1942               unsigned int access_mask, NTSTATUS expected_tcon,
1943               NTSTATUS expected_mkdir)
1944 {
1945         TALLOC_CTX *tmp_ctx;
1946         struct smbcli_tree *rmdir_tree, *tree;
1947         struct dom_sid *domain_sid;
1948         uint32_t rid;
1949         struct security_descriptor *sd;
1950         NTSTATUS status;
1951         BOOL ret = True;
1952
1953         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1954                 d_printf("talloc_new failed\n");
1955                 return False;
1956         }
1957
1958         status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
1959         if (!NT_STATUS_IS_OK(status)) {
1960                 d_printf("first tcon to delete dir failed\n");
1961                 talloc_free(tmp_ctx);
1962                 return False;
1963         }
1964
1965         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
1966
1967         if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
1968                                                &domain_sid, &rid))) {
1969                 d_printf("dom_sid_split_rid failed\n");
1970                 talloc_free(tmp_ctx);
1971                 return False;
1972         }
1973
1974         sd = security_descriptor_create(
1975                 tmp_ctx, "S-1-5-32-544",
1976                 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
1977                                                         DOMAIN_RID_USERS)),
1978                 dom_sid_string(mem_ctx, user_sid),
1979                 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
1980         if (sd == NULL) {
1981                 d_printf("security_descriptor_create failed\n");
1982                 talloc_free(tmp_ctx);
1983                 return False;
1984         }
1985
1986         status = set_sharesec(mem_ctx, session, sharename, sd);
1987         if (!NT_STATUS_IS_OK(status)) {
1988                 d_printf("custom set_sharesec failed: %s\n",
1989                          nt_errstr(status));
1990                 talloc_free(tmp_ctx);
1991                 return False;
1992         }
1993
1994         status = secondary_tcon(tmp_ctx, session, sharename, &tree);
1995         if (!NT_STATUS_EQUAL(status, expected_tcon)) {
1996                 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
1997                          nt_errstr(status));
1998                 ret = False;
1999                 goto done;
2000         }
2001
2002         if (!NT_STATUS_IS_OK(status)) {
2003                 /* An expected non-access, no point in trying to write */
2004                 goto done;
2005         }
2006
2007         status = smbcli_mkdir(tree, "sharesec_testdir");
2008         if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2009                 d_printf("Expected %s, got %s\n", nt_errstr(expected_mkdir),
2010                          nt_errstr(status));
2011                 ret = False;
2012         }
2013
2014  done:
2015         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2016
2017         status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2018         if (!NT_STATUS_IS_OK(status)) {
2019                 d_printf("custom set_sharesec failed: %s\n",
2020                          nt_errstr(status));
2021                 talloc_free(tmp_ctx);
2022                 return False;
2023         }
2024
2025         talloc_free(tmp_ctx);
2026         return ret;
2027 }
2028
2029 BOOL torture_samba3_rpc_sharesec(struct torture_context *torture)
2030 {
2031         TALLOC_CTX *mem_ctx;
2032         BOOL ret = True;
2033         struct smbcli_state *cli;
2034         struct security_descriptor *sd;
2035         struct dom_sid *user_sid;
2036
2037         if (!(mem_ctx = talloc_new(torture))) {
2038                 return False;
2039         }
2040
2041         if (!(torture_open_connection_share(
2042                       mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
2043                       "IPC$", NULL))) {
2044                 d_printf("IPC$ connection failed\n");
2045                 talloc_free(mem_ctx);
2046                 return False;
2047         }
2048
2049         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2050                 d_printf("whoami failed\n");
2051                 talloc_free(mem_ctx);
2052                 return False;
2053         }
2054
2055         sd = get_sharesec(mem_ctx, cli->session, lp_parm_string(-1, "torture",
2056                                                                 "share"));
2057
2058         ret &= try_tcon(mem_ctx, sd, cli->session,
2059                         lp_parm_string(-1, "torture", "share"),
2060                         user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2061
2062         ret &= try_tcon(mem_ctx, sd, cli->session,
2063                         lp_parm_string(-1, "torture", "share"),
2064                         user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2065                         NT_STATUS_NETWORK_ACCESS_DENIED);
2066
2067         ret &= try_tcon(mem_ctx, sd, cli->session,
2068                         lp_parm_string(-1, "torture", "share"),
2069                         user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2070
2071         talloc_free(mem_ctx);
2072         return ret;
2073 }