r17065: NT4 does not like "0" here, W2k3 does not care...
[jelmer/samba4-debian.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;
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.in.handle = &handle;
133         close.out.handle = &handle;
134
135         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close);
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;
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.in.handle = &handle;
259         close.out.handle = &handle;
260
261         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close);
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 *wks_name,
328                                char **domain,
329                                struct dcerpc_pipe **result_pipe,
330                                struct policy_handle **result_handle)
331 {
332         struct dcerpc_pipe *samr_pipe;
333         NTSTATUS status;
334         struct policy_handle conn_handle;
335         struct policy_handle domain_handle;
336         struct policy_handle *user_handle;
337         struct samr_Connect2 conn;
338         struct samr_EnumDomains enumdom;
339         uint32_t resume_handle = 0;
340         struct samr_LookupDomain l;
341         int dom_idx;
342         struct lsa_String domain_name;
343         struct lsa_String user_name;
344         struct samr_OpenDomain o;
345         struct samr_CreateUser2 c;
346         uint32_t user_rid,access_granted;
347
348         samr_pipe = dcerpc_pipe_init(mem_ctx,
349                                      cli->transport->socket->event.ctx);
350         if (samr_pipe == NULL) {
351                 d_printf("dcerpc_pipe_init failed\n");
352                 status = NT_STATUS_NO_MEMORY;
353                 goto fail;
354         }
355
356         status = dcerpc_pipe_open_smb(samr_pipe->conn, cli->tree, "\\samr");
357         if (!NT_STATUS_IS_OK(status)) {
358                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
359                          nt_errstr(status));
360                 goto fail;
361         }
362
363         if (admin_creds != NULL) {
364                 status = dcerpc_bind_auth(samr_pipe, &dcerpc_table_samr,
365                                           admin_creds, auth_type, auth_level,
366                                           NULL);
367                 if (!NT_STATUS_IS_OK(status)) {
368                         d_printf("dcerpc_bind_auth failed: %s\n",
369                                  nt_errstr(status));
370                         goto fail;
371                 }
372         } else {
373                 /* We must have an authenticated SMB connection */
374                 status = dcerpc_bind_auth_none(samr_pipe, &dcerpc_table_samr);
375                 if (!NT_STATUS_IS_OK(status)) {
376                         d_printf("dcerpc_bind_auth_none failed: %s\n",
377                                  nt_errstr(status));
378                         goto fail;
379                 }
380         }
381
382         conn.in.system_name = talloc_asprintf(
383                 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
384         conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
385         conn.out.connect_handle = &conn_handle;
386
387         status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
388         if (!NT_STATUS_IS_OK(status)) {
389                 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
390                 goto fail;
391         }
392
393         enumdom.in.connect_handle = &conn_handle;
394         enumdom.in.resume_handle = &resume_handle;
395         enumdom.in.buf_size = (uint32_t)-1;
396         enumdom.out.resume_handle = &resume_handle;
397
398         status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
399         if (!NT_STATUS_IS_OK(status)) {
400                 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
401                 goto fail;
402         }
403
404         if (enumdom.out.num_entries != 2) {
405                 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
406                          enumdom.out.num_entries);
407                 status = NT_STATUS_UNSUCCESSFUL;
408                 goto fail;
409         }
410
411         dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
412                            "builtin") ? 1:0;
413
414         l.in.connect_handle = &conn_handle;
415         domain_name.string = enumdom.out.sam->entries[0].name.string;
416         *domain = talloc_strdup(mem_ctx, domain_name.string);
417         l.in.domain_name = &domain_name;
418
419         status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
420         if (!NT_STATUS_IS_OK(status)) {
421                 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
422                 goto fail;
423         }
424
425         o.in.connect_handle = &conn_handle;
426         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
427         o.in.sid = l.out.sid;
428         o.out.domain_handle = &domain_handle;
429
430         status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
431         if (!NT_STATUS_IS_OK(status)) {
432                 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
433                 goto fail;
434         }
435
436         c.in.domain_handle = &domain_handle;
437         user_name.string = talloc_asprintf(mem_ctx, "%s$", wks_name);
438         c.in.account_name = &user_name;
439         c.in.acct_flags = ACB_WSTRUST;
440         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
441         user_handle = talloc(mem_ctx, struct policy_handle);
442         c.out.user_handle = user_handle;
443         c.out.access_granted = &access_granted;
444         c.out.rid = &user_rid;
445
446         status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
447
448         if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
449                 struct samr_LookupNames ln;
450                 struct samr_OpenUser ou;
451
452                 ln.in.domain_handle = &domain_handle;
453                 ln.in.num_names = 1;
454                 ln.in.names = &user_name;
455
456                 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
457                 if (!NT_STATUS_IS_OK(status)) {
458                         d_printf("samr_LookupNames failed: %s\n",
459                                  nt_errstr(status));
460                         goto fail;
461                 }
462
463                 ou.in.domain_handle = &domain_handle;
464                 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
465                 ou.in.rid = ln.out.rids.ids[0];
466                 ou.out.user_handle = user_handle;
467
468                 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
469                 if (!NT_STATUS_IS_OK(status)) {
470                         d_printf("samr_OpenUser failed: %s\n",
471                                  nt_errstr(status));
472                         goto fail;
473                 }
474         }
475
476         if (!NT_STATUS_IS_OK(status)) {
477                 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
478                 goto fail;
479         }
480
481         *result_pipe = samr_pipe;
482         *result_handle = user_handle;
483         return NT_STATUS_OK;
484
485  fail:
486         return status;
487 }
488
489 /*
490  * Do a Samba3-style join
491  */
492
493 static BOOL join3(struct smbcli_state *cli,
494                   BOOL use_level25,
495                   struct cli_credentials *admin_creds,
496                   struct cli_credentials *wks_creds)
497 {
498         TALLOC_CTX *mem_ctx;
499         NTSTATUS status;
500         char *dom_name;
501         struct dcerpc_pipe *samr_pipe;
502         struct policy_handle *wks_handle;
503         BOOL ret = False;
504
505         if ((mem_ctx = talloc_init("join3")) == NULL) {
506                 d_printf("talloc_init failed\n");
507                 return False;
508         }
509
510         status = get_usr_handle(cli, mem_ctx, admin_creds,
511                                 DCERPC_AUTH_TYPE_NTLMSSP,
512                                 DCERPC_AUTH_LEVEL_PRIVACY,
513                                 cli_credentials_get_workstation(wks_creds),
514                                 &dom_name, &samr_pipe, &wks_handle);
515
516         if (!NT_STATUS_IS_OK(status)) {
517                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
518                 goto done;
519         }
520
521         cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
522
523         if (use_level25) {
524                 struct samr_SetUserInfo2 sui2;
525                 union samr_UserInfo u_info;
526                 struct samr_UserInfo21 *i21 = &u_info.info25.info;
527                 DATA_BLOB session_key;
528                 DATA_BLOB confounded_session_key = data_blob_talloc(
529                         mem_ctx, NULL, 16);
530                 struct MD5Context ctx;
531                 uint8_t confounder[16];
532
533                 ZERO_STRUCT(u_info);
534
535                 i21->full_name.string = talloc_asprintf(
536                         mem_ctx, "%s$",
537                         cli_credentials_get_workstation(wks_creds));
538                 i21->acct_flags = ACB_WSTRUST;
539                 i21->fields_present = SAMR_FIELD_FULL_NAME |
540                         SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
541
542                 encode_pw_buffer(u_info.info25.password.data,
543                                  cli_credentials_get_password(wks_creds),
544                                  STR_UNICODE);
545                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
546                 if (!NT_STATUS_IS_OK(status)) {
547                         d_printf("dcerpc_fetch_session_key failed: %s\n",
548                                  nt_errstr(status));
549                         goto done;
550                 }
551                 generate_random_buffer((uint8_t *)confounder, 16);
552
553                 MD5Init(&ctx);
554                 MD5Update(&ctx, confounder, 16);
555                 MD5Update(&ctx, session_key.data, session_key.length);
556                 MD5Final(confounded_session_key.data, &ctx);
557
558                 arcfour_crypt_blob(u_info.info25.password.data, 516,
559                                    &confounded_session_key);
560                 memcpy(&u_info.info25.password.data[516], confounder, 16);
561
562                 sui2.in.user_handle = wks_handle;
563                 sui2.in.level = 25;
564                 sui2.in.info = &u_info;
565
566                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
567                 if (!NT_STATUS_IS_OK(status)) {
568                         d_printf("samr_SetUserInfo2(25) failed: %s\n",
569                                  nt_errstr(status));
570                         goto done;
571                 }
572         } else {
573                 struct samr_SetUserInfo2 sui2;
574                 struct samr_SetUserInfo sui;
575                 union samr_UserInfo u_info;
576                 DATA_BLOB session_key;
577
578                 encode_pw_buffer(u_info.info24.password.data,
579                                  cli_credentials_get_password(wks_creds),
580                                  STR_UNICODE);
581                 u_info.info24.pw_len =
582                         strlen_m(cli_credentials_get_password(wks_creds))*2;
583
584                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
585                 if (!NT_STATUS_IS_OK(status)) {
586                         d_printf("dcerpc_fetch_session_key failed\n");
587                         goto done;
588                 }
589                 arcfour_crypt_blob(u_info.info24.password.data, 516,
590                                    &session_key);
591                 sui2.in.user_handle = wks_handle;
592                 sui2.in.info = &u_info;
593                 sui2.in.level = 24;
594
595                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
596                 if (!NT_STATUS_IS_OK(status)) {
597                         d_printf("samr_SetUserInfo(24) failed\n");
598                         goto done;
599                 }
600
601                 u_info.info16.acct_flags = ACB_WSTRUST;
602                 sui.in.user_handle = wks_handle;
603                 sui.in.info = &u_info;
604                 sui.in.level = 16;
605
606                 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
607                 if (!NT_STATUS_IS_OK(status)) {
608                         d_printf("samr_SetUserInfo(16) failed\n");
609                         goto done;
610                 }
611         }
612
613         ret = True;
614
615  done:
616         talloc_free(mem_ctx);
617         return ret;
618 }
619
620 /*
621  * Do a ReqChallenge/Auth2 and get the wks creds
622  */
623
624 static BOOL auth2(struct smbcli_state *cli,
625                   struct cli_credentials *wks_cred)
626 {
627         TALLOC_CTX *mem_ctx;
628         struct dcerpc_pipe *net_pipe;
629         BOOL result = False;
630         NTSTATUS status;
631         struct netr_ServerReqChallenge r;
632         struct netr_Credential netr_cli_creds;
633         struct netr_Credential netr_srv_creds;
634         uint32_t negotiate_flags;
635         struct netr_ServerAuthenticate2 a;
636         struct creds_CredentialState *creds_state;
637         struct netr_Credential netr_cred;
638         struct samr_Password mach_pw;
639
640         mem_ctx = talloc_new(NULL);
641         if (mem_ctx == NULL) {
642                 d_printf("talloc_new failed\n");
643                 return False;
644         }
645
646         net_pipe = dcerpc_pipe_init(mem_ctx,
647                                     cli->transport->socket->event.ctx);
648         if (net_pipe == NULL) {
649                 d_printf("dcerpc_pipe_init failed\n");
650                 goto done;
651         }
652
653         status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
654         if (!NT_STATUS_IS_OK(status)) {
655                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
656                          nt_errstr(status));
657                 goto done;
658         }
659
660         status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
661         if (!NT_STATUS_IS_OK(status)) {
662                 d_printf("dcerpc_bind_auth_none failed: %s\n",
663                          nt_errstr(status));
664                 goto done;
665         }
666
667         r.in.computer_name = cli_credentials_get_workstation(wks_cred);
668         r.in.server_name = talloc_asprintf(
669                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
670         if (r.in.server_name == NULL) {
671                 d_printf("talloc_asprintf failed\n");
672                 goto done;
673         }
674         generate_random_buffer(netr_cli_creds.data,
675                                sizeof(netr_cli_creds.data));
676         r.in.credentials = &netr_cli_creds;
677         r.out.credentials = &netr_srv_creds;
678
679         status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
680         if (!NT_STATUS_IS_OK(status)) {
681                 d_printf("netr_ServerReqChallenge failed: %s\n",
682                          nt_errstr(status));
683                 goto done;
684         }
685
686         negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
687         E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
688
689         creds_state = talloc(mem_ctx, struct creds_CredentialState);
690         creds_client_init(creds_state, r.in.credentials,
691                           r.out.credentials, &mach_pw,
692                           &netr_cred, negotiate_flags);
693
694         a.in.server_name = talloc_asprintf(
695                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
696         a.in.account_name = talloc_asprintf(
697                 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
698         a.in.computer_name = cli_credentials_get_workstation(wks_cred);
699         a.in.secure_channel_type = SEC_CHAN_WKSTA;
700         a.in.negotiate_flags = &negotiate_flags;
701         a.out.negotiate_flags = &negotiate_flags;
702         a.in.credentials = &netr_cred;
703         a.out.credentials = &netr_cred;
704
705         status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
706         if (!NT_STATUS_IS_OK(status)) {
707                 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
708                          nt_errstr(status));
709                 goto done;
710         }
711
712         if (!creds_client_check(creds_state, a.out.credentials)) {
713                 d_printf("creds_client_check failed\n");
714                 goto done;
715         }
716
717         cli_credentials_set_netlogon_creds(wks_cred, creds_state);
718
719         result = True;
720
721  done:
722         talloc_free(mem_ctx);
723         return result;
724 }
725
726 /*
727  * Do a couple of channel protected Netlogon ops: Interactive and Network
728  * login, and change the wks password
729  */
730
731 static BOOL schan(struct smbcli_state *cli,
732                   struct cli_credentials *wks_creds,
733                   struct cli_credentials *user_creds)
734 {
735         TALLOC_CTX *mem_ctx;
736         NTSTATUS status;
737         BOOL ret = False;
738         struct dcerpc_pipe *net_pipe;
739         int i;
740         
741         mem_ctx = talloc_new(NULL);
742         if (mem_ctx == NULL) {
743                 d_printf("talloc_new failed\n");
744                 return False;
745         }
746
747         net_pipe = dcerpc_pipe_init(mem_ctx,
748                                     cli->transport->socket->event.ctx);
749         if (net_pipe == NULL) {
750                 d_printf("dcerpc_pipe_init failed\n");
751                 goto done;
752         }
753
754         status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
755         if (!NT_STATUS_IS_OK(status)) {
756                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
757                          nt_errstr(status));
758                 goto done;
759         }
760
761 #if 0
762         net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
763                 DCERPC_DEBUG_PRINT_OUT;
764 #endif
765 #if 1
766         net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
767         status = dcerpc_bind_auth(net_pipe, &dcerpc_table_netlogon,
768                                   wks_creds, DCERPC_AUTH_TYPE_SCHANNEL,
769                                   DCERPC_AUTH_LEVEL_PRIVACY,
770                                   NULL);
771 #else
772         status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
773 #endif
774         if (!NT_STATUS_IS_OK(status)) {
775                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
776                 goto done;
777         }
778
779
780         for (i=2; i<4; i++) {
781                 int flags;
782                 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
783                 struct creds_CredentialState *creds_state;
784                 struct netr_Authenticator netr_auth, netr_auth2;
785                 struct netr_NetworkInfo ninfo;
786                 struct netr_PasswordInfo pinfo;
787                 struct netr_LogonSamLogon r;
788
789                 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
790                         CLI_CRED_NTLMv2_AUTH;
791
792                 chal = data_blob_talloc(mem_ctx, NULL, 8);
793                 if (chal.data == NULL) {
794                         d_printf("data_blob_talloc failed\n");
795                         goto done;
796                 }
797
798                 generate_random_buffer(chal.data, chal.length);
799                 names_blob = NTLMv2_generate_names_blob(
800                         mem_ctx, cli_credentials_get_workstation(user_creds),
801                         cli_credentials_get_domain(user_creds));
802                 status = cli_credentials_get_ntlm_response(
803                         user_creds, mem_ctx, &flags, chal, names_blob,
804                         &lm_resp, &nt_resp, NULL, NULL);
805                 if (!NT_STATUS_IS_OK(status)) {
806                         d_printf("cli_credentials_get_ntlm_response failed:"
807                                  " %s\n", nt_errstr(status));
808                         goto done;
809                 }
810
811                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
812                 creds_client_authenticator(creds_state, &netr_auth);
813
814                 ninfo.identity_info.account_name.string =
815                         cli_credentials_get_username(user_creds);
816                 ninfo.identity_info.domain_name.string =
817                         cli_credentials_get_domain(user_creds);
818                 ninfo.identity_info.parameter_control = 0;
819                 ninfo.identity_info.logon_id_low = 0;
820                 ninfo.identity_info.logon_id_high = 0;
821                 ninfo.identity_info.workstation.string =
822                         cli_credentials_get_workstation(user_creds);
823                 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
824                 ninfo.nt.length = nt_resp.length;
825                 ninfo.nt.data = nt_resp.data;
826                 ninfo.lm.length = lm_resp.length;
827                 ninfo.lm.data = lm_resp.data;
828
829                 r.in.server_name = talloc_asprintf(
830                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
831                 ZERO_STRUCT(netr_auth2);
832                 r.in.computer_name =
833                         cli_credentials_get_workstation(wks_creds);
834                 r.in.credential = &netr_auth;
835                 r.in.return_authenticator = &netr_auth2;
836                 r.in.logon_level = 2;
837                 r.in.validation_level = i;
838                 r.in.logon.network = &ninfo;
839                 r.out.return_authenticator = NULL;
840
841                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
842                 if (!NT_STATUS_IS_OK(status)) {
843                         d_printf("netr_LogonSamLogon failed: %s\n",
844                                  nt_errstr(status));
845                         goto done;
846                 }
847
848                 if ((r.out.return_authenticator == NULL) ||
849                     (!creds_client_check(creds_state,
850                                          &r.out.return_authenticator->cred))) {
851                         d_printf("Credentials check failed!\n");
852                         goto done;
853                 }
854
855                 creds_client_authenticator(creds_state, &netr_auth);
856
857                 pinfo.identity_info = ninfo.identity_info;
858                 ZERO_STRUCT(pinfo.lmpassword.hash);
859                 E_md4hash(cli_credentials_get_password(user_creds),
860                           pinfo.ntpassword.hash);
861                 session_key = data_blob_talloc(mem_ctx,
862                                                creds_state->session_key, 16);
863                 arcfour_crypt_blob(pinfo.ntpassword.hash,
864                                    sizeof(pinfo.ntpassword.hash),
865                                    &session_key);
866
867                 r.in.logon_level = 1;
868                 r.in.logon.password = &pinfo;
869                 r.out.return_authenticator = NULL;
870
871                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
872                 if (!NT_STATUS_IS_OK(status)) {
873                         d_printf("netr_LogonSamLogon failed: %s\n",
874                                  nt_errstr(status));
875                         goto done;
876                 }
877
878                 if ((r.out.return_authenticator == NULL) ||
879                     (!creds_client_check(creds_state,
880                                          &r.out.return_authenticator->cred))) {
881                         d_printf("Credentials check failed!\n");
882                         goto done;
883                 }
884         }
885
886         {
887                 struct netr_ServerPasswordSet s;
888                 char *password = generate_random_str(wks_creds, 8);
889                 struct creds_CredentialState *creds_state;
890
891                 s.in.server_name = talloc_asprintf(
892                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
893                 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
894                 s.in.account_name = talloc_asprintf(
895                         mem_ctx, "%s$", s.in.computer_name);
896                 s.in.secure_channel_type = SEC_CHAN_WKSTA;
897                 E_md4hash(password, s.in.new_password.hash);
898
899                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
900                 creds_des_encrypt(creds_state, &s.in.new_password);
901                 creds_client_authenticator(creds_state, &s.in.credential);
902
903                 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
904                 if (!NT_STATUS_IS_OK(status)) {
905                         printf("ServerPasswordSet - %s\n", nt_errstr(status));
906                         goto done;
907                 }
908
909                 if (!creds_client_check(creds_state,
910                                         &s.out.return_authenticator.cred)) {
911                         printf("Credential chaining failed\n");
912                 }
913
914                 cli_credentials_set_password(wks_creds, password,
915                                              CRED_SPECIFIED);
916         }
917
918         ret = True;
919  done:
920         talloc_free(mem_ctx);
921         return ret;
922 }
923
924 /*
925  * Delete the wks account again
926  */
927
928 static BOOL leave(struct smbcli_state *cli,
929                   struct cli_credentials *admin_creds,
930                   struct cli_credentials *wks_creds)
931 {
932         TALLOC_CTX *mem_ctx;
933         NTSTATUS status;
934         char *dom_name;
935         struct dcerpc_pipe *samr_pipe;
936         struct policy_handle *wks_handle;
937         BOOL ret = False;
938
939         if ((mem_ctx = talloc_init("leave")) == NULL) {
940                 d_printf("talloc_init failed\n");
941                 return False;
942         }
943
944         status = get_usr_handle(cli, mem_ctx, admin_creds,
945                                 DCERPC_AUTH_TYPE_NTLMSSP,
946                                 DCERPC_AUTH_LEVEL_PRIVACY,
947                                 cli_credentials_get_workstation(wks_creds),
948                                 &dom_name, &samr_pipe, &wks_handle);
949
950         if (!NT_STATUS_IS_OK(status)) {
951                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
952                 goto done;
953         }
954
955         {
956                 struct samr_DeleteUser d;
957
958                 d.in.user_handle = wks_handle;
959                 d.out.user_handle = wks_handle;
960
961                 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
962                 if (!NT_STATUS_IS_OK(status)) {
963                         d_printf("samr_DeleteUser failed\n");
964                         goto done;
965                 }
966         }
967
968         ret = True;
969
970  done:
971         talloc_free(mem_ctx);
972         return ret;
973 }
974
975 /*
976  * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
977  */
978
979 BOOL torture_netlogon_samba3(struct torture_context *torture)
980 {
981         TALLOC_CTX *mem_ctx;
982         NTSTATUS status;
983         BOOL ret = False;
984         struct smbcli_state *cli;
985         struct cli_credentials *anon_creds;
986         struct cli_credentials *wks_creds;
987         const char *wks_name;
988         int i;
989
990         wks_name = lp_parm_string(-1, "torture", "wksname");
991         if (wks_name == NULL) {
992                 wks_name = get_myname();
993         }
994
995         mem_ctx = talloc_init("torture_bind_authcontext");
996
997         if (mem_ctx == NULL) {
998                 d_printf("talloc_init failed\n");
999                 return False;
1000         }
1001
1002         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1003                 d_printf("create_anon_creds failed\n");
1004                 goto done;
1005         }
1006
1007         status = smbcli_full_connection(mem_ctx, &cli,
1008                                         lp_parm_string(-1, "torture", "host"),
1009                                         "IPC$", NULL, anon_creds, NULL);
1010         if (!NT_STATUS_IS_OK(status)) {
1011                 d_printf("smbcli_full_connection failed: %s\n",
1012                          nt_errstr(status));
1013                 goto done;
1014         }
1015
1016         wks_creds = cli_credentials_init(mem_ctx);
1017         if (wks_creds == NULL) {
1018                 d_printf("cli_credentials_init failed\n");
1019                 goto done;
1020         }
1021
1022         cli_credentials_set_conf(wks_creds);
1023         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1024         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1025         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1026         cli_credentials_set_password(wks_creds, "", CRED_SPECIFIED);
1027
1028         if (!join3(cli, False, cmdline_credentials, wks_creds)) {
1029                 d_printf("join failed\n");
1030                 goto done;
1031         }
1032
1033         cli_credentials_set_domain(
1034                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1035                 CRED_SPECIFIED);
1036
1037         for (i=0; i<2; i++) {
1038
1039                 /* Do this more than once, the routine "schan" changes
1040                  * the workstation password using the netlogon
1041                  * password change routine */
1042
1043                 int j;
1044
1045                 if (!auth2(cli, wks_creds)) {
1046                         d_printf("auth2 failed\n");
1047                         goto done;
1048                 }
1049
1050                 for (j=0; j<2; j++) {
1051                         if (!schan(cli, wks_creds, cmdline_credentials)) {
1052                                 d_printf("schan failed\n");
1053                                 goto done;
1054                         }
1055                 }
1056         }
1057
1058         if (!leave(cli, cmdline_credentials, wks_creds)) {
1059                 d_printf("leave failed\n");
1060                 goto done;
1061         }
1062
1063         ret = True;
1064
1065  done:
1066         talloc_free(mem_ctx);
1067         return ret;
1068 }
1069
1070 /*
1071  * Do a simple join, testjoin and leave using specified smb and samr
1072  * credentials
1073  */
1074
1075 static BOOL test_join3(TALLOC_CTX *mem_ctx,
1076                        BOOL use_level25,
1077                        struct cli_credentials *smb_creds,
1078                        struct cli_credentials *samr_creds,
1079                        const char *wks_name)
1080 {
1081         NTSTATUS status;
1082         BOOL ret = False;
1083         struct smbcli_state *cli;
1084         struct cli_credentials *wks_creds;
1085
1086         status = smbcli_full_connection(mem_ctx, &cli,
1087                                         lp_parm_string(-1, "torture", "host"),
1088                                         "IPC$", NULL, smb_creds, NULL);
1089         if (!NT_STATUS_IS_OK(status)) {
1090                 d_printf("smbcli_full_connection failed: %s\n",
1091                          nt_errstr(status));
1092                 goto done;
1093         }
1094
1095         wks_creds = cli_credentials_init(cli);
1096         if (wks_creds == NULL) {
1097                 d_printf("cli_credentials_init failed\n");
1098                 goto done;
1099         }
1100
1101         cli_credentials_set_conf(wks_creds);
1102         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1103         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1104         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1105         cli_credentials_set_password(wks_creds,
1106                                      generate_random_str(wks_creds, 8),
1107                                      CRED_SPECIFIED);
1108
1109         if (!join3(cli, use_level25, samr_creds, wks_creds)) {
1110                 d_printf("join failed\n");
1111                 goto done;
1112         }
1113
1114         cli_credentials_set_domain(
1115                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1116                 CRED_SPECIFIED);
1117
1118         if (!auth2(cli, wks_creds)) {
1119                 d_printf("auth2 failed\n");
1120                 goto done;
1121         }
1122
1123         if (!leave(cli, samr_creds, wks_creds)) {
1124                 d_printf("leave failed\n");
1125                 goto done;
1126         }
1127
1128         talloc_free(cli);
1129
1130         ret = True;
1131
1132  done:
1133         return ret;
1134 }
1135
1136 /*
1137  * Test the different session key variants. Do it by joining, this uses the
1138  * session key in the setpassword routine. Test the join by doing the auth2.
1139  */
1140
1141 BOOL torture_samba3_sessionkey(struct torture_context *torture)
1142 {
1143         TALLOC_CTX *mem_ctx;
1144         BOOL ret = False;
1145         struct cli_credentials *anon_creds;
1146         const char *wks_name;
1147
1148         wks_name = lp_parm_string(-1, "torture", "wksname");
1149         if (wks_name == NULL) {
1150                 wks_name = get_myname();
1151         }
1152
1153         mem_ctx = talloc_init("torture_samba3_sessionkey");
1154
1155         if (mem_ctx == NULL) {
1156                 d_printf("talloc_init failed\n");
1157                 return False;
1158         }
1159
1160         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1161                 d_printf("create_anon_creds failed\n");
1162                 goto done;
1163         }
1164
1165         ret = True;
1166
1167         if (!lp_parm_bool(-1, "target", "samba3", False)) {
1168
1169                 /* Samba3 in the build farm right now does this happily. Need
1170                  * to fix :-) */
1171
1172                 if (test_join3(mem_ctx, False, anon_creds, NULL, wks_name)) {
1173                         d_printf("join using anonymous bind on an anonymous smb "
1174                                  "connection succeeded -- HUH??\n");
1175                         ret = False;
1176                 }
1177         }
1178
1179         if (!test_join3(mem_ctx, False, anon_creds, cmdline_credentials,
1180                         wks_name)) {
1181                 d_printf("join using ntlmssp bind on an anonymous smb "
1182                          "connection failed\n");
1183                 ret = False;
1184         }
1185
1186         if (!test_join3(mem_ctx, False, cmdline_credentials, NULL, wks_name)) {
1187                 d_printf("join using anonymous bind on an authenticated smb "
1188                          "connection failed\n");
1189                 ret = False;
1190         }
1191
1192         if (!test_join3(mem_ctx, False, cmdline_credentials,
1193                         cmdline_credentials,
1194                         wks_name)) {
1195                 d_printf("join using ntlmssp bind on an authenticated smb "
1196                          "connection failed\n");
1197                 ret = False;
1198         }
1199
1200         /*
1201          * The following two are tests for setuserinfolevel 25
1202          */
1203
1204         if (!test_join3(mem_ctx, True, anon_creds, cmdline_credentials,
1205                         wks_name)) {
1206                 d_printf("join using ntlmssp bind on an anonymous smb "
1207                          "connection failed\n");
1208                 ret = False;
1209         }
1210
1211         if (!test_join3(mem_ctx, True, cmdline_credentials, NULL, wks_name)) {
1212                 d_printf("join using anonymous bind on an authenticated smb "
1213                          "connection failed\n");
1214                 ret = False;
1215         }
1216
1217  done:
1218
1219         return ret;
1220 }
1221
1222 /*
1223  * open pipe and bind, given an IPC$ context
1224  */
1225
1226 static struct dcerpc_pipe *pipe_bind_smb(TALLOC_CTX *mem_ctx,
1227                                          struct smbcli_tree *tree,
1228                                          const char *pipe_name,
1229                                          const struct dcerpc_interface_table *iface)
1230 {
1231         struct dcerpc_pipe *result;
1232         NTSTATUS status;
1233
1234         if (!(result = dcerpc_pipe_init(
1235                       mem_ctx, tree->session->transport->socket->event.ctx))) {
1236                 return NULL;
1237         }
1238
1239         status = dcerpc_pipe_open_smb(result->conn, tree, pipe_name);
1240         if (!NT_STATUS_IS_OK(status)) {
1241                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1242                          nt_errstr(status));
1243                 talloc_free(result);
1244                 return NULL;
1245         }
1246
1247         status = dcerpc_bind_auth_none(result, iface);
1248         if (!NT_STATUS_IS_OK(status)) {
1249                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1250                 talloc_free(result);
1251                 return NULL;
1252         }
1253
1254         return result;
1255 }
1256
1257 /*
1258  * Sane wrapper around lsa_LookupNames
1259  */
1260
1261 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1262                                 struct dcerpc_pipe *p,
1263                                 const char *name,
1264                                 const char *domain)
1265 {
1266         struct lsa_ObjectAttribute attr;
1267         struct lsa_QosInfo qos;
1268         struct lsa_OpenPolicy2 r;
1269         struct lsa_Close c;
1270         NTSTATUS status;
1271         struct policy_handle handle;
1272         struct lsa_LookupNames l;
1273         struct lsa_TransSidArray sids;
1274         struct lsa_String lsa_name;
1275         uint32_t count = 0;
1276         struct dom_sid *result;
1277         TALLOC_CTX *tmp_ctx;
1278
1279         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1280                 return NULL;
1281         }
1282
1283         qos.len = 0;
1284         qos.impersonation_level = 2;
1285         qos.context_mode = 1;
1286         qos.effective_only = 0;
1287
1288         attr.len = 0;
1289         attr.root_dir = NULL;
1290         attr.object_name = NULL;
1291         attr.attributes = 0;
1292         attr.sec_desc = NULL;
1293         attr.sec_qos = &qos;
1294
1295         r.in.system_name = "\\";
1296         r.in.attr = &attr;
1297         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1298         r.out.handle = &handle;
1299
1300         status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1301         if (!NT_STATUS_IS_OK(status)) {
1302                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1303                 talloc_free(tmp_ctx);
1304                 return NULL;
1305         }
1306
1307         sids.count = 0;
1308         sids.sids = NULL;
1309
1310         lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1311
1312         l.in.handle = &handle;
1313         l.in.num_names = 1;
1314         l.in.names = &lsa_name;
1315         l.in.sids = &sids;
1316         l.in.level = 1;
1317         l.in.count = &count;
1318         l.out.count = &count;
1319         l.out.sids = &sids;
1320
1321         status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1322         if (!NT_STATUS_IS_OK(status)) {
1323                 printf("LookupNames failed - %s\n", nt_errstr(status));
1324                 talloc_free(tmp_ctx);
1325                 return False;
1326         }
1327
1328         result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1329                                  l.out.sids->sids[0].rid);
1330
1331         c.in.handle = &handle;
1332         c.out.handle = &handle;
1333
1334         status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1335         if (!NT_STATUS_IS_OK(status)) {
1336                 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1337                 talloc_free(tmp_ctx);
1338                 return NULL;
1339         }
1340         
1341         talloc_free(tmp_ctx);
1342         return result;
1343 }
1344
1345 /*
1346  * Find out the user SID on this connection
1347  */
1348
1349 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1350 {
1351         struct dcerpc_pipe *lsa;
1352         struct lsa_GetUserName r;
1353         NTSTATUS status;
1354         struct lsa_StringPointer authority_name_p;
1355         struct dom_sid *result;
1356
1357         if (!(lsa = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1358                                   &dcerpc_table_lsarpc))) {
1359                 d_printf("Could not bind to LSA\n");
1360                 return NULL;
1361         }
1362
1363         r.in.system_name = "\\";
1364         r.in.account_name = NULL;
1365         authority_name_p.string = NULL;
1366         r.in.authority_name = &authority_name_p;
1367
1368         status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1369
1370         if (!NT_STATUS_IS_OK(status)) {
1371                 printf("GetUserName failed - %s\n", nt_errstr(status));
1372                 talloc_free(lsa);
1373                 return NULL;
1374         }
1375
1376         result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1377                           r.out.authority_name->string->string);
1378
1379         talloc_free(lsa);
1380         return result;
1381 }
1382
1383 /*
1384  * Test the getusername behaviour
1385  */
1386
1387 BOOL torture_samba3_rpc_getusername(struct torture_context *torture)
1388 {
1389         NTSTATUS status;
1390         struct smbcli_state *cli;
1391         TALLOC_CTX *mem_ctx;
1392         BOOL ret = True;
1393         struct dom_sid *user_sid;
1394         struct cli_credentials *anon_creds;
1395
1396         if (!(mem_ctx = talloc_new(torture))) {
1397                 return False;
1398         }
1399
1400         status = smbcli_full_connection(
1401                 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1402                 "IPC$", NULL, cmdline_credentials, NULL);
1403         if (!NT_STATUS_IS_OK(status)) {
1404                 d_printf("smbcli_full_connection failed: %s\n",
1405                          nt_errstr(status));
1406                 ret = False;
1407                 goto done;
1408         }
1409
1410         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1411                 d_printf("whoami on auth'ed connection failed\n");
1412                 ret = False;
1413         }
1414
1415         talloc_free(cli);
1416
1417         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1418                 d_printf("create_anon_creds failed\n");
1419                 ret = False;
1420                 goto done;
1421         }
1422
1423         status = smbcli_full_connection(
1424                 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1425                 "IPC$", NULL, anon_creds, NULL);
1426         if (!NT_STATUS_IS_OK(status)) {
1427                 d_printf("anon smbcli_full_connection failed: %s\n",
1428                          nt_errstr(status));
1429                 ret = False;
1430                 goto done;
1431         }
1432
1433         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1434                 d_printf("whoami on anon connection failed\n");
1435                 ret = False;
1436                 goto done;
1437         }
1438
1439         if (!dom_sid_equal(user_sid,
1440                            dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1441                 d_printf("Anon lsa_GetUserName returned %s, expected S-1-5-7",
1442                          dom_sid_string(mem_ctx, user_sid));
1443                 ret = False;
1444         }
1445
1446  done:
1447         talloc_free(mem_ctx);
1448         return ret;
1449 }
1450
1451 static BOOL test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1452                                  const char *sharename)
1453 {
1454         NTSTATUS status;
1455         struct srvsvc_NetShareGetInfo r;
1456         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1457         int i;
1458         BOOL ret = True;
1459
1460         r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1461                                           dcerpc_server_name(p));
1462         r.in.share_name = sharename;
1463
1464         for (i=0;i<ARRAY_SIZE(levels);i++) {
1465                 r.in.level = levels[i];
1466
1467                 ZERO_STRUCT(r.out);
1468
1469                 printf("testing NetShareGetInfo level %u on share '%s'\n", 
1470                        r.in.level, r.in.share_name);
1471
1472                 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1473                 if (!NT_STATUS_IS_OK(status)) {
1474                         printf("NetShareGetInfo level %u on share '%s' failed"
1475                                " - %s\n", r.in.level, r.in.share_name,
1476                                nt_errstr(status));
1477                         ret = False;
1478                         continue;
1479                 }
1480                 if (!W_ERROR_IS_OK(r.out.result)) {
1481                         printf("NetShareGetInfo level %u on share '%s' failed "
1482                                "- %s\n", r.in.level, r.in.share_name,
1483                                win_errstr(r.out.result));
1484                         ret = False;
1485                         continue;
1486                 }
1487         }
1488
1489         return ret;
1490 }
1491
1492 static BOOL test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1493                               const char **one_sharename)
1494 {
1495         NTSTATUS status;
1496         struct srvsvc_NetShareEnum r;
1497         struct srvsvc_NetShareCtr0 c0;
1498         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1499         int i;
1500         BOOL ret = True;
1501
1502         r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1503         r.in.ctr.ctr0 = &c0;
1504         r.in.ctr.ctr0->count = 0;
1505         r.in.ctr.ctr0->array = NULL;
1506         r.in.max_buffer = (uint32_t)-1;
1507         r.in.resume_handle = NULL;
1508
1509         for (i=0;i<ARRAY_SIZE(levels);i++) {
1510                 r.in.level = levels[i];
1511
1512                 ZERO_STRUCT(r.out);
1513
1514                 printf("testing NetShareEnum level %u\n", r.in.level);
1515                 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1516                 if (!NT_STATUS_IS_OK(status)) {
1517                         printf("NetShareEnum level %u failed - %s\n",
1518                                r.in.level, nt_errstr(status));
1519                         ret = False;
1520                         continue;
1521                 }
1522                 if (!W_ERROR_IS_OK(r.out.result)) {
1523                         printf("NetShareEnum level %u failed - %s\n",
1524                                r.in.level, win_errstr(r.out.result));
1525                         continue;
1526                 }
1527                 if (r.in.level == 0) {
1528                         struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1529                         if (ctr->count > 0) {
1530                                 *one_sharename = ctr->array[0].name;
1531                         }
1532                 }
1533         }
1534
1535         return ret;
1536 }
1537
1538 BOOL torture_samba3_rpc_srvsvc(struct torture_context *torture)
1539 {
1540         struct dcerpc_pipe *p;
1541         TALLOC_CTX *mem_ctx;
1542         BOOL ret = True;
1543         const char *sharename = NULL;
1544         struct smbcli_state *cli;
1545
1546         if (!(mem_ctx = talloc_new(torture))) {
1547                 return False;
1548         }
1549
1550         if (!(torture_open_connection_share(
1551                       mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1552                       "IPC$", NULL))) {
1553                 talloc_free(mem_ctx);
1554                 return False;
1555         }
1556
1557         if (!(p = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1558                                 &dcerpc_table_srvsvc))) {
1559                 d_printf("could not bind to srvsvc pipe\n");
1560                 ret = False;
1561                 goto done;
1562         }
1563
1564         ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1565         if (sharename == NULL) {
1566                 printf("did not get sharename\n");
1567         } else {
1568                 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1569         }
1570  done:
1571         talloc_free(mem_ctx);
1572         return ret;
1573 }