75a7d760d91008eee2642866a42d0e56c8fe3342
[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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/rap/rap.h"
25 #include "torture/torture.h"
26 #include "torture/util.h"
27 #include "torture/rap/proto.h"
28 #include "librpc/gen_ndr/ndr_lsa.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_samr.h"
31 #include "librpc/gen_ndr/ndr_samr_c.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #include "librpc/gen_ndr/ndr_netlogon_c.h"
34 #include "librpc/gen_ndr/ndr_srvsvc.h"
35 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
36 #include "librpc/gen_ndr/ndr_spoolss.h"
37 #include "librpc/gen_ndr/ndr_spoolss_c.h"
38 #include "librpc/gen_ndr/ndr_winreg.h"
39 #include "librpc/gen_ndr/ndr_winreg_c.h"
40 #include "librpc/gen_ndr/ndr_wkssvc.h"
41 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
42 #include "lib/cmdline/popt_common.h"
43 #include "librpc/rpc/dcerpc.h"
44 #include "torture/rpc/rpc.h"
45 #include "libcli/libcli.h"
46 #include "libcli/composite/composite.h"
47 #include "libcli/smb_composite/smb_composite.h"
48 #include "libcli/auth/libcli_auth.h"
49 #include "lib/crypto/crypto.h"
50 #include "libcli/security/proto.h"
51 #include "param/param.h"
52
53 static struct cli_credentials *create_anon_creds(TALLOC_CTX *mem_ctx)
54 {
55         struct cli_credentials *result;
56
57         if (!(result = cli_credentials_init(mem_ctx))) {
58                 return NULL;
59         }
60
61         cli_credentials_set_conf(result, global_loadparm);
62         cli_credentials_set_anonymous(result);
63
64         return result;
65 }
66
67 /*
68  * This tests a RPC call using an invalid vuid
69  */
70
71 BOOL torture_bind_authcontext(struct torture_context *torture) 
72 {
73         TALLOC_CTX *mem_ctx;
74         NTSTATUS status;
75         BOOL ret = False;
76         struct lsa_ObjectAttribute objectattr;
77         struct lsa_OpenPolicy2 openpolicy;
78         struct policy_handle handle;
79         struct lsa_Close close_handle;
80         struct smbcli_session *tmp;
81         struct smbcli_session *session2;
82         struct smbcli_state *cli;
83         struct dcerpc_pipe *lsa_pipe;
84         struct cli_credentials *anon_creds;
85         struct smb_composite_sesssetup setup;
86
87         mem_ctx = talloc_init("torture_bind_authcontext");
88
89         if (mem_ctx == NULL) {
90                 d_printf("talloc_init failed\n");
91                 return False;
92         }
93
94         status = smbcli_full_connection(mem_ctx, &cli,
95                                         torture_setting_string(torture, "host", NULL),
96                                         "IPC$", NULL, cmdline_credentials,
97                                         NULL);
98         if (!NT_STATUS_IS_OK(status)) {
99                 d_printf("smbcli_full_connection failed: %s\n",
100                          nt_errstr(status));
101                 goto done;
102         }
103
104         lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx);
105         if (lsa_pipe == NULL) {
106                 d_printf("dcerpc_pipe_init failed\n");
107                 goto done;
108         }
109
110         status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
111         if (!NT_STATUS_IS_OK(status)) {
112                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
113                          nt_errstr(status));
114                 goto done;
115         }
116
117         status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
118         if (!NT_STATUS_IS_OK(status)) {
119                 d_printf("dcerpc_bind_auth_none failed: %s\n",
120                          nt_errstr(status));
121                 goto done;
122         }
123
124         openpolicy.in.system_name =talloc_asprintf(
125                 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
126         ZERO_STRUCT(objectattr);
127         openpolicy.in.attr = &objectattr;
128         openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
129         openpolicy.out.handle = &handle;
130
131         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
132
133         if (!NT_STATUS_IS_OK(status)) {
134                 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
135                          nt_errstr(status));
136                 goto done;
137         }
138
139         close_handle.in.handle = &handle;
140         close_handle.out.handle = &handle;
141
142         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
143         if (!NT_STATUS_IS_OK(status)) {
144                 d_printf("dcerpc_lsa_Close failed: %s\n",
145                          nt_errstr(status));
146                 goto done;
147         }
148
149         session2 = smbcli_session_init(cli->transport, mem_ctx, False);
150         if (session2 == NULL) {
151                 d_printf("smbcli_session_init failed\n");
152                 goto done;
153         }
154
155         if (!(anon_creds = create_anon_creds(mem_ctx))) {
156                 d_printf("create_anon_creds failed\n");
157                 goto done;
158         }
159
160         setup.in.sesskey = cli->transport->negotiate.sesskey;
161         setup.in.capabilities = cli->transport->negotiate.capabilities;
162         setup.in.workgroup = "";
163         setup.in.credentials = anon_creds;
164
165         status = smb_composite_sesssetup(session2, &setup);
166         if (!NT_STATUS_IS_OK(status)) {
167                 d_printf("anon session setup failed: %s\n",
168                          nt_errstr(status));
169                 goto done;
170         }
171         session2->vuid = setup.out.vuid;
172
173         tmp = cli->tree->session;
174         cli->tree->session = session2;
175
176         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
177
178         cli->tree->session = tmp;
179         talloc_free(lsa_pipe);
180         lsa_pipe = NULL;
181
182         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
183                 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
184                          "expected NT_STATUS_INVALID_HANDLE\n",
185                          nt_errstr(status));
186                 goto done;
187         }
188
189         ret = True;
190  done:
191         talloc_free(mem_ctx);
192         return ret;
193 }
194
195 /*
196  * Bind to lsa using a specific auth method
197  */
198
199 static BOOL bindtest(struct smbcli_state *cli,
200                      struct cli_credentials *credentials,
201                      uint8_t auth_type, uint8_t auth_level)
202 {
203         TALLOC_CTX *mem_ctx;
204         BOOL ret = False;
205         NTSTATUS status;
206
207         struct dcerpc_pipe *lsa_pipe;
208         struct lsa_ObjectAttribute objectattr;
209         struct lsa_OpenPolicy2 openpolicy;
210         struct lsa_QueryInfoPolicy query;
211         struct policy_handle handle;
212         struct lsa_Close close_handle;
213
214         if ((mem_ctx = talloc_init("bindtest")) == NULL) {
215                 d_printf("talloc_init failed\n");
216                 return False;
217         }
218
219         lsa_pipe = dcerpc_pipe_init(mem_ctx,
220                                     cli->transport->socket->event.ctx);
221         if (lsa_pipe == NULL) {
222                 d_printf("dcerpc_pipe_init failed\n");
223                 goto done;
224         }
225
226         status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
227         if (!NT_STATUS_IS_OK(status)) {
228                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
229                          nt_errstr(status));
230                 goto done;
231         }
232
233         status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
234                                   credentials, auth_type, auth_level,
235                                   NULL);
236         if (!NT_STATUS_IS_OK(status)) {
237                 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
238                 goto done;
239         }
240
241         openpolicy.in.system_name =talloc_asprintf(
242                 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
243         ZERO_STRUCT(objectattr);
244         openpolicy.in.attr = &objectattr;
245         openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
246         openpolicy.out.handle = &handle;
247
248         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
249
250         if (!NT_STATUS_IS_OK(status)) {
251                 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
252                          nt_errstr(status));
253                 goto done;
254         }
255
256         query.in.handle = &handle;
257         query.in.level = LSA_POLICY_INFO_DOMAIN;
258
259         status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
260         if (!NT_STATUS_IS_OK(status)) {
261                 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
262                          nt_errstr(status));
263                 goto done;
264         }
265
266         close_handle.in.handle = &handle;
267         close_handle.out.handle = &handle;
268
269         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
270         if (!NT_STATUS_IS_OK(status)) {
271                 d_printf("dcerpc_lsa_Close failed: %s\n",
272                          nt_errstr(status));
273                 goto done;
274         }
275
276         ret = True;
277  done:
278         talloc_free(mem_ctx);
279         return ret;
280 }
281
282 /*
283  * test authenticated RPC binds with the variants Samba3 does support
284  */
285
286 BOOL torture_bind_samba3(struct torture_context *torture) 
287 {
288         TALLOC_CTX *mem_ctx;
289         NTSTATUS status;
290         BOOL ret = False;
291         struct smbcli_state *cli;
292
293         mem_ctx = talloc_init("torture_bind_authcontext");
294
295         if (mem_ctx == NULL) {
296                 d_printf("talloc_init failed\n");
297                 return False;
298         }
299
300         status = smbcli_full_connection(mem_ctx, &cli,
301                                         torture_setting_string(torture, "host", NULL),
302                                         "IPC$", NULL, cmdline_credentials,
303                                         NULL);
304         if (!NT_STATUS_IS_OK(status)) {
305                 d_printf("smbcli_full_connection failed: %s\n",
306                          nt_errstr(status));
307                 goto done;
308         }
309
310         ret = True;
311
312         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
313                         DCERPC_AUTH_LEVEL_INTEGRITY);
314         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
315                         DCERPC_AUTH_LEVEL_PRIVACY);
316         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
317                         DCERPC_AUTH_LEVEL_INTEGRITY);
318         ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
319                         DCERPC_AUTH_LEVEL_PRIVACY);
320
321  done:
322         talloc_free(mem_ctx);
323         return ret;
324 }
325
326 /*
327  * Lookup or create a user and return all necessary info
328  */
329
330 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
331                                TALLOC_CTX *mem_ctx,
332                                struct cli_credentials *admin_creds,
333                                uint8_t auth_type,
334                                uint8_t auth_level,
335                                const char *username,
336                                char **domain,
337                                struct dcerpc_pipe **result_pipe,
338                                struct policy_handle **result_handle,
339                                struct dom_sid **sid)
340 {
341         struct dcerpc_pipe *samr_pipe;
342         NTSTATUS status;
343         struct policy_handle conn_handle;
344         struct policy_handle domain_handle;
345         struct policy_handle *user_handle;
346         struct samr_Connect2 conn;
347         struct samr_EnumDomains enumdom;
348         uint32_t resume_handle = 0;
349         struct samr_LookupDomain l;
350         int dom_idx;
351         struct lsa_String domain_name;
352         struct lsa_String user_name;
353         struct samr_OpenDomain o;
354         struct samr_CreateUser2 c;
355         uint32_t user_rid,access_granted;
356
357         samr_pipe = dcerpc_pipe_init(mem_ctx,
358                                      cli->transport->socket->event.ctx);
359         if (samr_pipe == NULL) {
360                 d_printf("dcerpc_pipe_init failed\n");
361                 status = NT_STATUS_NO_MEMORY;
362                 goto fail;
363         }
364
365         status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
366         if (!NT_STATUS_IS_OK(status)) {
367                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
368                          nt_errstr(status));
369                 goto fail;
370         }
371
372         if (admin_creds != NULL) {
373                 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
374                                           admin_creds, auth_type, auth_level,
375                                           NULL);
376                 if (!NT_STATUS_IS_OK(status)) {
377                         d_printf("dcerpc_bind_auth failed: %s\n",
378                                  nt_errstr(status));
379                         goto fail;
380                 }
381         } else {
382                 /* We must have an authenticated SMB connection */
383                 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
384                 if (!NT_STATUS_IS_OK(status)) {
385                         d_printf("dcerpc_bind_auth_none failed: %s\n",
386                                  nt_errstr(status));
387                         goto fail;
388                 }
389         }
390
391         conn.in.system_name = talloc_asprintf(
392                 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
393         conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
394         conn.out.connect_handle = &conn_handle;
395
396         status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
397         if (!NT_STATUS_IS_OK(status)) {
398                 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
399                 goto fail;
400         }
401
402         enumdom.in.connect_handle = &conn_handle;
403         enumdom.in.resume_handle = &resume_handle;
404         enumdom.in.buf_size = (uint32_t)-1;
405         enumdom.out.resume_handle = &resume_handle;
406
407         status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
408         if (!NT_STATUS_IS_OK(status)) {
409                 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
410                 goto fail;
411         }
412
413         if (enumdom.out.num_entries != 2) {
414                 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
415                          enumdom.out.num_entries);
416                 status = NT_STATUS_UNSUCCESSFUL;
417                 goto fail;
418         }
419
420         dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
421                            "builtin") ? 1:0;
422
423         l.in.connect_handle = &conn_handle;
424         domain_name.string = enumdom.out.sam->entries[0].name.string;
425         *domain = talloc_strdup(mem_ctx, domain_name.string);
426         l.in.domain_name = &domain_name;
427
428         status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
429         if (!NT_STATUS_IS_OK(status)) {
430                 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
431                 goto fail;
432         }
433
434         o.in.connect_handle = &conn_handle;
435         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
436         o.in.sid = l.out.sid;
437         o.out.domain_handle = &domain_handle;
438
439         status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
440         if (!NT_STATUS_IS_OK(status)) {
441                 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
442                 goto fail;
443         }
444
445         c.in.domain_handle = &domain_handle;
446         user_name.string = username;
447         c.in.account_name = &user_name;
448         c.in.acct_flags = ACB_NORMAL;
449         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
450         user_handle = talloc(mem_ctx, struct policy_handle);
451         c.out.user_handle = user_handle;
452         c.out.access_granted = &access_granted;
453         c.out.rid = &user_rid;
454
455         status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
456
457         if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
458                 struct samr_LookupNames ln;
459                 struct samr_OpenUser ou;
460
461                 ln.in.domain_handle = &domain_handle;
462                 ln.in.num_names = 1;
463                 ln.in.names = &user_name;
464
465                 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
466                 if (!NT_STATUS_IS_OK(status)) {
467                         d_printf("samr_LookupNames failed: %s\n",
468                                  nt_errstr(status));
469                         goto fail;
470                 }
471
472                 ou.in.domain_handle = &domain_handle;
473                 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
474                 user_rid = ou.in.rid = ln.out.rids.ids[0];
475                 ou.out.user_handle = user_handle;
476
477                 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
478                 if (!NT_STATUS_IS_OK(status)) {
479                         d_printf("samr_OpenUser failed: %s\n",
480                                  nt_errstr(status));
481                         goto fail;
482                 }
483         }
484
485         if (!NT_STATUS_IS_OK(status)) {
486                 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
487                 goto fail;
488         }
489
490         *result_pipe = samr_pipe;
491         *result_handle = user_handle;
492         if (sid != NULL) {
493                 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
494         }
495         return NT_STATUS_OK;
496
497  fail:
498         return status;
499 }
500
501 /*
502  * Create a test user
503  */
504
505 static BOOL create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
506                         struct cli_credentials *admin_creds,
507                         const char *username, const char *password,
508                         char **domain_name,
509                         struct dom_sid **user_sid)
510 {
511         TALLOC_CTX *tmp_ctx;
512         NTSTATUS status;
513         struct dcerpc_pipe *samr_pipe;
514         struct policy_handle *wks_handle;
515         BOOL ret = False;
516
517         if (!(tmp_ctx = talloc_new(mem_ctx))) {
518                 d_printf("talloc_init failed\n");
519                 return False;
520         }
521
522         status = get_usr_handle(cli, tmp_ctx, admin_creds,
523                                 DCERPC_AUTH_TYPE_NTLMSSP,
524                                 DCERPC_AUTH_LEVEL_INTEGRITY,
525                                 username, domain_name, &samr_pipe, &wks_handle,
526                                 user_sid);
527         if (!NT_STATUS_IS_OK(status)) {
528                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
529                 goto done;
530         }
531
532         {
533                 struct samr_SetUserInfo2 sui2;
534                 struct samr_SetUserInfo sui;
535                 struct samr_QueryUserInfo qui;
536                 union samr_UserInfo u_info;
537                 DATA_BLOB session_key;
538
539
540                 ZERO_STRUCT(u_info);
541                 encode_pw_buffer(u_info.info23.password.data, password,
542                                  STR_UNICODE);
543
544                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
545                 if (!NT_STATUS_IS_OK(status)) {
546                         d_printf("dcerpc_fetch_session_key failed\n");
547                         goto done;
548                 }
549                 arcfour_crypt_blob(u_info.info23.password.data, 516,
550                                    &session_key);
551                 u_info.info23.info.password_expired = 0;
552                 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD | 
553                                                     SAMR_FIELD_PASSWORD2 |
554                                                     SAMR_FIELD_EXPIRED_FLAG;
555                 sui2.in.user_handle = wks_handle;
556                 sui2.in.info = &u_info;
557                 sui2.in.level = 23;
558
559                 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
560                 if (!NT_STATUS_IS_OK(status)) {
561                         d_printf("samr_SetUserInfo(23) failed: %s\n",
562                                  nt_errstr(status));
563                         goto done;
564                 }
565
566                 u_info.info16.acct_flags = ACB_NORMAL;
567                 sui.in.user_handle = wks_handle;
568                 sui.in.info = &u_info;
569                 sui.in.level = 16;
570
571                 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
572                 if (!NT_STATUS_IS_OK(status)) {
573                         d_printf("samr_SetUserInfo(16) failed\n");
574                         goto done;
575                 }
576
577                 qui.in.user_handle = wks_handle;
578                 qui.in.level = 21;
579
580                 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
581                 if (!NT_STATUS_IS_OK(status)) {
582                         d_printf("samr_QueryUserInfo(21) failed\n");
583                         goto done;
584                 }
585
586                 qui.out.info->info21.allow_password_change = 0;
587                 qui.out.info->info21.force_password_change = 0;
588                 qui.out.info->info21.account_name.string = NULL;
589                 qui.out.info->info21.rid = 0;
590                 qui.out.info->info21.acct_expiry = 0;
591                 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
592
593                 u_info.info21 = qui.out.info->info21;
594                 sui.in.user_handle = wks_handle;
595                 sui.in.info = &u_info;
596                 sui.in.level = 21;
597
598                 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
599                 if (!NT_STATUS_IS_OK(status)) {
600                         d_printf("samr_SetUserInfo(21) failed\n");
601                         goto done;
602                 }
603         }
604
605         *domain_name= talloc_steal(mem_ctx, *domain_name);
606         *user_sid = talloc_steal(mem_ctx, *user_sid);
607         ret = True;
608  done:
609         talloc_free(tmp_ctx);
610         return ret;
611 }
612
613 /*
614  * Delete a test user
615  */
616
617 static BOOL delete_user(struct smbcli_state *cli,
618                         struct cli_credentials *admin_creds,
619                         const char *username)
620 {
621         TALLOC_CTX *mem_ctx;
622         NTSTATUS status;
623         char *dom_name;
624         struct dcerpc_pipe *samr_pipe;
625         struct policy_handle *user_handle;
626         BOOL ret = False;
627
628         if ((mem_ctx = talloc_init("leave")) == NULL) {
629                 d_printf("talloc_init failed\n");
630                 return False;
631         }
632
633         status = get_usr_handle(cli, mem_ctx, admin_creds,
634                                 DCERPC_AUTH_TYPE_NTLMSSP,
635                                 DCERPC_AUTH_LEVEL_INTEGRITY,
636                                 username, &dom_name, &samr_pipe,
637                                 &user_handle, NULL);
638
639         if (!NT_STATUS_IS_OK(status)) {
640                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
641                 goto done;
642         }
643
644         {
645                 struct samr_DeleteUser d;
646
647                 d.in.user_handle = user_handle;
648                 d.out.user_handle = user_handle;
649
650                 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
651                 if (!NT_STATUS_IS_OK(status)) {
652                         d_printf("samr_DeleteUser failed\n");
653                         goto done;
654                 }
655         }
656
657         ret = True;
658
659  done:
660         talloc_free(mem_ctx);
661         return ret;
662 }
663
664 /*
665  * Do a Samba3-style join
666  */
667
668 static BOOL join3(struct smbcli_state *cli,
669                   BOOL use_level25,
670                   struct cli_credentials *admin_creds,
671                   struct cli_credentials *wks_creds)
672 {
673         TALLOC_CTX *mem_ctx;
674         NTSTATUS status;
675         char *dom_name;
676         struct dcerpc_pipe *samr_pipe;
677         struct policy_handle *wks_handle;
678         BOOL ret = False;
679
680         if ((mem_ctx = talloc_init("join3")) == NULL) {
681                 d_printf("talloc_init failed\n");
682                 return False;
683         }
684
685         status = get_usr_handle(
686                 cli, mem_ctx, admin_creds,
687                 DCERPC_AUTH_TYPE_NTLMSSP,
688                 DCERPC_AUTH_LEVEL_PRIVACY,
689                 talloc_asprintf(mem_ctx, "%s$",
690                                 cli_credentials_get_workstation(wks_creds)),
691                 &dom_name, &samr_pipe, &wks_handle, NULL);
692
693         if (!NT_STATUS_IS_OK(status)) {
694                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
695                 goto done;
696         }
697
698         cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
699
700         if (use_level25) {
701                 struct samr_SetUserInfo2 sui2;
702                 union samr_UserInfo u_info;
703                 struct samr_UserInfo21 *i21 = &u_info.info25.info;
704                 DATA_BLOB session_key;
705                 DATA_BLOB confounded_session_key = data_blob_talloc(
706                         mem_ctx, NULL, 16);
707                 struct MD5Context ctx;
708                 uint8_t confounder[16];
709
710                 ZERO_STRUCT(u_info);
711
712                 i21->full_name.string = talloc_asprintf(
713                         mem_ctx, "%s$",
714                         cli_credentials_get_workstation(wks_creds));
715                 i21->acct_flags = ACB_WSTRUST;
716                 i21->fields_present = SAMR_FIELD_FULL_NAME |
717                         SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
718
719                 encode_pw_buffer(u_info.info25.password.data,
720                                  cli_credentials_get_password(wks_creds),
721                                  STR_UNICODE);
722                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
723                 if (!NT_STATUS_IS_OK(status)) {
724                         d_printf("dcerpc_fetch_session_key failed: %s\n",
725                                  nt_errstr(status));
726                         goto done;
727                 }
728                 generate_random_buffer((uint8_t *)confounder, 16);
729
730                 MD5Init(&ctx);
731                 MD5Update(&ctx, confounder, 16);
732                 MD5Update(&ctx, session_key.data, session_key.length);
733                 MD5Final(confounded_session_key.data, &ctx);
734
735                 arcfour_crypt_blob(u_info.info25.password.data, 516,
736                                    &confounded_session_key);
737                 memcpy(&u_info.info25.password.data[516], confounder, 16);
738
739                 sui2.in.user_handle = wks_handle;
740                 sui2.in.level = 25;
741                 sui2.in.info = &u_info;
742
743                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
744                 if (!NT_STATUS_IS_OK(status)) {
745                         d_printf("samr_SetUserInfo2(25) failed: %s\n",
746                                  nt_errstr(status));
747                         goto done;
748                 }
749         } else {
750                 struct samr_SetUserInfo2 sui2;
751                 struct samr_SetUserInfo sui;
752                 union samr_UserInfo u_info;
753                 DATA_BLOB session_key;
754
755                 encode_pw_buffer(u_info.info24.password.data,
756                                  cli_credentials_get_password(wks_creds),
757                                  STR_UNICODE);
758                 u_info.info24.pw_len =
759                         strlen_m(cli_credentials_get_password(wks_creds))*2;
760
761                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
762                 if (!NT_STATUS_IS_OK(status)) {
763                         d_printf("dcerpc_fetch_session_key failed\n");
764                         goto done;
765                 }
766                 arcfour_crypt_blob(u_info.info24.password.data, 516,
767                                    &session_key);
768                 sui2.in.user_handle = wks_handle;
769                 sui2.in.info = &u_info;
770                 sui2.in.level = 24;
771
772                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
773                 if (!NT_STATUS_IS_OK(status)) {
774                         d_printf("samr_SetUserInfo(24) failed: %s\n",
775                                  nt_errstr(status));
776                         goto done;
777                 }
778
779                 u_info.info16.acct_flags = ACB_WSTRUST;
780                 sui.in.user_handle = wks_handle;
781                 sui.in.info = &u_info;
782                 sui.in.level = 16;
783
784                 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
785                 if (!NT_STATUS_IS_OK(status)) {
786                         d_printf("samr_SetUserInfo(16) failed\n");
787                         goto done;
788                 }
789         }
790
791         ret = True;
792
793  done:
794         talloc_free(mem_ctx);
795         return ret;
796 }
797
798 /*
799  * Do a ReqChallenge/Auth2 and get the wks creds
800  */
801
802 static BOOL auth2(struct smbcli_state *cli,
803                   struct cli_credentials *wks_cred)
804 {
805         TALLOC_CTX *mem_ctx;
806         struct dcerpc_pipe *net_pipe;
807         BOOL result = False;
808         NTSTATUS status;
809         struct netr_ServerReqChallenge r;
810         struct netr_Credential netr_cli_creds;
811         struct netr_Credential netr_srv_creds;
812         uint32_t negotiate_flags;
813         struct netr_ServerAuthenticate2 a;
814         struct creds_CredentialState *creds_state;
815         struct netr_Credential netr_cred;
816         struct samr_Password mach_pw;
817
818         mem_ctx = talloc_new(NULL);
819         if (mem_ctx == NULL) {
820                 d_printf("talloc_new failed\n");
821                 return False;
822         }
823
824         net_pipe = dcerpc_pipe_init(mem_ctx,
825                                     cli->transport->socket->event.ctx);
826         if (net_pipe == NULL) {
827                 d_printf("dcerpc_pipe_init failed\n");
828                 goto done;
829         }
830
831         status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
832         if (!NT_STATUS_IS_OK(status)) {
833                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
834                          nt_errstr(status));
835                 goto done;
836         }
837
838         status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
839         if (!NT_STATUS_IS_OK(status)) {
840                 d_printf("dcerpc_bind_auth_none failed: %s\n",
841                          nt_errstr(status));
842                 goto done;
843         }
844
845         r.in.computer_name = cli_credentials_get_workstation(wks_cred);
846         r.in.server_name = talloc_asprintf(
847                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
848         if (r.in.server_name == NULL) {
849                 d_printf("talloc_asprintf failed\n");
850                 goto done;
851         }
852         generate_random_buffer(netr_cli_creds.data,
853                                sizeof(netr_cli_creds.data));
854         r.in.credentials = &netr_cli_creds;
855         r.out.credentials = &netr_srv_creds;
856
857         status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
858         if (!NT_STATUS_IS_OK(status)) {
859                 d_printf("netr_ServerReqChallenge failed: %s\n",
860                          nt_errstr(status));
861                 goto done;
862         }
863
864         negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
865         E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
866
867         creds_state = talloc(mem_ctx, struct creds_CredentialState);
868         creds_client_init(creds_state, r.in.credentials,
869                           r.out.credentials, &mach_pw,
870                           &netr_cred, negotiate_flags);
871
872         a.in.server_name = talloc_asprintf(
873                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
874         a.in.account_name = talloc_asprintf(
875                 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
876         a.in.computer_name = cli_credentials_get_workstation(wks_cred);
877         a.in.secure_channel_type = SEC_CHAN_WKSTA;
878         a.in.negotiate_flags = &negotiate_flags;
879         a.out.negotiate_flags = &negotiate_flags;
880         a.in.credentials = &netr_cred;
881         a.out.credentials = &netr_cred;
882
883         status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
884         if (!NT_STATUS_IS_OK(status)) {
885                 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
886                          nt_errstr(status));
887                 goto done;
888         }
889
890         if (!creds_client_check(creds_state, a.out.credentials)) {
891                 d_printf("creds_client_check failed\n");
892                 goto done;
893         }
894
895         cli_credentials_set_netlogon_creds(wks_cred, creds_state);
896
897         result = True;
898
899  done:
900         talloc_free(mem_ctx);
901         return result;
902 }
903
904 /*
905  * Do a couple of schannel protected Netlogon ops: Interactive and Network
906  * login, and change the wks password
907  */
908
909 static BOOL schan(struct smbcli_state *cli,
910                   struct cli_credentials *wks_creds,
911                   struct cli_credentials *user_creds)
912 {
913         TALLOC_CTX *mem_ctx;
914         NTSTATUS status;
915         BOOL ret = False;
916         struct dcerpc_pipe *net_pipe;
917         int i;
918         
919         mem_ctx = talloc_new(NULL);
920         if (mem_ctx == NULL) {
921                 d_printf("talloc_new failed\n");
922                 return False;
923         }
924
925         net_pipe = dcerpc_pipe_init(mem_ctx,
926                                     cli->transport->socket->event.ctx);
927         if (net_pipe == NULL) {
928                 d_printf("dcerpc_pipe_init failed\n");
929                 goto done;
930         }
931
932         status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
933         if (!NT_STATUS_IS_OK(status)) {
934                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
935                          nt_errstr(status));
936                 goto done;
937         }
938
939 #if 0
940         net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
941                 DCERPC_DEBUG_PRINT_OUT;
942 #endif
943 #if 1
944         net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
945         status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
946                                   wks_creds, DCERPC_AUTH_TYPE_SCHANNEL,
947                                   DCERPC_AUTH_LEVEL_PRIVACY,
948                                   NULL);
949 #else
950         status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
951 #endif
952         if (!NT_STATUS_IS_OK(status)) {
953                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
954                 goto done;
955         }
956
957
958         for (i=2; i<4; i++) {
959                 int flags;
960                 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
961                 struct creds_CredentialState *creds_state;
962                 struct netr_Authenticator netr_auth, netr_auth2;
963                 struct netr_NetworkInfo ninfo;
964                 struct netr_PasswordInfo pinfo;
965                 struct netr_LogonSamLogon r;
966
967                 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
968                         CLI_CRED_NTLMv2_AUTH;
969
970                 chal = data_blob_talloc(mem_ctx, NULL, 8);
971                 if (chal.data == NULL) {
972                         d_printf("data_blob_talloc failed\n");
973                         goto done;
974                 }
975
976                 generate_random_buffer(chal.data, chal.length);
977                 names_blob = NTLMv2_generate_names_blob(
978                         mem_ctx, cli_credentials_get_workstation(user_creds),
979                         cli_credentials_get_domain(user_creds));
980                 status = cli_credentials_get_ntlm_response(
981                         user_creds, mem_ctx, &flags, chal, names_blob,
982                         &lm_resp, &nt_resp, NULL, NULL);
983                 if (!NT_STATUS_IS_OK(status)) {
984                         d_printf("cli_credentials_get_ntlm_response failed:"
985                                  " %s\n", nt_errstr(status));
986                         goto done;
987                 }
988
989                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
990                 creds_client_authenticator(creds_state, &netr_auth);
991
992                 ninfo.identity_info.account_name.string =
993                         cli_credentials_get_username(user_creds);
994                 ninfo.identity_info.domain_name.string =
995                         cli_credentials_get_domain(user_creds);
996                 ninfo.identity_info.parameter_control = 0;
997                 ninfo.identity_info.logon_id_low = 0;
998                 ninfo.identity_info.logon_id_high = 0;
999                 ninfo.identity_info.workstation.string =
1000                         cli_credentials_get_workstation(user_creds);
1001                 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1002                 ninfo.nt.length = nt_resp.length;
1003                 ninfo.nt.data = nt_resp.data;
1004                 ninfo.lm.length = lm_resp.length;
1005                 ninfo.lm.data = lm_resp.data;
1006
1007                 r.in.server_name = talloc_asprintf(
1008                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1009                 ZERO_STRUCT(netr_auth2);
1010                 r.in.computer_name =
1011                         cli_credentials_get_workstation(wks_creds);
1012                 r.in.credential = &netr_auth;
1013                 r.in.return_authenticator = &netr_auth2;
1014                 r.in.logon_level = 2;
1015                 r.in.validation_level = i;
1016                 r.in.logon.network = &ninfo;
1017                 r.out.return_authenticator = NULL;
1018
1019                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1020                 if (!NT_STATUS_IS_OK(status)) {
1021                         d_printf("netr_LogonSamLogon failed: %s\n",
1022                                  nt_errstr(status));
1023                         goto done;
1024                 }
1025
1026                 if ((r.out.return_authenticator == NULL) ||
1027                     (!creds_client_check(creds_state,
1028                                          &r.out.return_authenticator->cred))) {
1029                         d_printf("Credentials check failed!\n");
1030                         goto done;
1031                 }
1032
1033                 creds_client_authenticator(creds_state, &netr_auth);
1034
1035                 pinfo.identity_info = ninfo.identity_info;
1036                 ZERO_STRUCT(pinfo.lmpassword.hash);
1037                 E_md4hash(cli_credentials_get_password(user_creds),
1038                           pinfo.ntpassword.hash);
1039                 session_key = data_blob_talloc(mem_ctx,
1040                                                creds_state->session_key, 16);
1041                 arcfour_crypt_blob(pinfo.ntpassword.hash,
1042                                    sizeof(pinfo.ntpassword.hash),
1043                                    &session_key);
1044
1045                 r.in.logon_level = 1;
1046                 r.in.logon.password = &pinfo;
1047                 r.out.return_authenticator = NULL;
1048
1049                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1050                 if (!NT_STATUS_IS_OK(status)) {
1051                         d_printf("netr_LogonSamLogon failed: %s\n",
1052                                  nt_errstr(status));
1053                         goto done;
1054                 }
1055
1056                 if ((r.out.return_authenticator == NULL) ||
1057                     (!creds_client_check(creds_state,
1058                                          &r.out.return_authenticator->cred))) {
1059                         d_printf("Credentials check failed!\n");
1060                         goto done;
1061                 }
1062         }
1063
1064         {
1065                 struct netr_ServerPasswordSet s;
1066                 char *password = generate_random_str(wks_creds, 8);
1067                 struct creds_CredentialState *creds_state;
1068
1069                 s.in.server_name = talloc_asprintf(
1070                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1071                 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1072                 s.in.account_name = talloc_asprintf(
1073                         mem_ctx, "%s$", s.in.computer_name);
1074                 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1075                 E_md4hash(password, s.in.new_password.hash);
1076
1077                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1078                 creds_des_encrypt(creds_state, &s.in.new_password);
1079                 creds_client_authenticator(creds_state, &s.in.credential);
1080
1081                 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1082                 if (!NT_STATUS_IS_OK(status)) {
1083                         printf("ServerPasswordSet - %s\n", nt_errstr(status));
1084                         goto done;
1085                 }
1086
1087                 if (!creds_client_check(creds_state,
1088                                         &s.out.return_authenticator.cred)) {
1089                         printf("Credential chaining failed\n");
1090                 }
1091
1092                 cli_credentials_set_password(wks_creds, password,
1093                                              CRED_SPECIFIED);
1094         }
1095
1096         ret = True;
1097  done:
1098         talloc_free(mem_ctx);
1099         return ret;
1100 }
1101
1102 /*
1103  * Delete the wks account again
1104  */
1105
1106 static BOOL leave(struct smbcli_state *cli,
1107                   struct cli_credentials *admin_creds,
1108                   struct cli_credentials *wks_creds)
1109 {
1110         char *wks_name = talloc_asprintf(
1111                 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1112         BOOL ret;
1113
1114         ret = delete_user(cli, admin_creds, wks_name);
1115         talloc_free(wks_name);
1116         return ret;
1117 }
1118
1119 /*
1120  * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1121  */
1122
1123 BOOL torture_netlogon_samba3(struct torture_context *torture)
1124 {
1125         TALLOC_CTX *mem_ctx;
1126         NTSTATUS status;
1127         BOOL ret = False;
1128         struct smbcli_state *cli;
1129         struct cli_credentials *anon_creds;
1130         struct cli_credentials *wks_creds;
1131         const char *wks_name;
1132         int i;
1133
1134         wks_name = torture_setting_string(torture, "wksname", NULL);
1135         if (wks_name == NULL) {
1136                 wks_name = get_myname();
1137         }
1138
1139         mem_ctx = talloc_init("torture_netlogon_samba3");
1140
1141         if (mem_ctx == NULL) {
1142                 d_printf("talloc_init failed\n");
1143                 return False;
1144         }
1145
1146         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1147                 d_printf("create_anon_creds failed\n");
1148                 goto done;
1149         }
1150
1151         status = smbcli_full_connection(mem_ctx, &cli,
1152                                         torture_setting_string(torture, "host", NULL),
1153                                         "IPC$", NULL, anon_creds, NULL);
1154         if (!NT_STATUS_IS_OK(status)) {
1155                 d_printf("smbcli_full_connection failed: %s\n",
1156                          nt_errstr(status));
1157                 goto done;
1158         }
1159
1160         wks_creds = cli_credentials_init(mem_ctx);
1161         if (wks_creds == NULL) {
1162                 d_printf("cli_credentials_init failed\n");
1163                 goto done;
1164         }
1165
1166         cli_credentials_set_conf(wks_creds, global_loadparm);
1167         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1168         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1169         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1170         cli_credentials_set_password(wks_creds,
1171                                      generate_random_str(wks_creds, 8),
1172                                      CRED_SPECIFIED);
1173
1174         if (!join3(cli, False, cmdline_credentials, wks_creds)) {
1175                 d_printf("join failed\n");
1176                 goto done;
1177         }
1178
1179         cli_credentials_set_domain(
1180                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1181                 CRED_SPECIFIED);
1182
1183         for (i=0; i<2; i++) {
1184
1185                 /* Do this more than once, the routine "schan" changes
1186                  * the workstation password using the netlogon
1187                  * password change routine */
1188
1189                 int j;
1190
1191                 if (!auth2(cli, wks_creds)) {
1192                         d_printf("auth2 failed\n");
1193                         goto done;
1194                 }
1195
1196                 for (j=0; j<2; j++) {
1197                         if (!schan(cli, wks_creds, cmdline_credentials)) {
1198                                 d_printf("schan failed\n");
1199                                 goto done;
1200                         }
1201                 }
1202         }
1203
1204         if (!leave(cli, cmdline_credentials, wks_creds)) {
1205                 d_printf("leave failed\n");
1206                 goto done;
1207         }
1208
1209         ret = True;
1210
1211  done:
1212         talloc_free(mem_ctx);
1213         return ret;
1214 }
1215
1216 /*
1217  * Do a simple join, testjoin and leave using specified smb and samr
1218  * credentials
1219  */
1220
1221 static BOOL test_join3(TALLOC_CTX *mem_ctx,
1222                        BOOL use_level25,
1223                        struct cli_credentials *smb_creds,
1224                        struct cli_credentials *samr_creds,
1225                        const char *wks_name)
1226 {
1227         NTSTATUS status;
1228         BOOL ret = False;
1229         struct smbcli_state *cli;
1230         struct cli_credentials *wks_creds;
1231
1232         status = smbcli_full_connection(mem_ctx, &cli,
1233                                         lp_parm_string(NULL, "torture", "host"),
1234                                         "IPC$", NULL, smb_creds, NULL);
1235         if (!NT_STATUS_IS_OK(status)) {
1236                 d_printf("smbcli_full_connection failed: %s\n",
1237                          nt_errstr(status));
1238                 goto done;
1239         }
1240
1241         wks_creds = cli_credentials_init(cli);
1242         if (wks_creds == NULL) {
1243                 d_printf("cli_credentials_init failed\n");
1244                 goto done;
1245         }
1246
1247         cli_credentials_set_conf(wks_creds, global_loadparm);
1248         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1249         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1250         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1251         cli_credentials_set_password(wks_creds,
1252                                      generate_random_str(wks_creds, 8),
1253                                      CRED_SPECIFIED);
1254
1255         if (!join3(cli, use_level25, samr_creds, wks_creds)) {
1256                 d_printf("join failed\n");
1257                 goto done;
1258         }
1259
1260         cli_credentials_set_domain(
1261                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1262                 CRED_SPECIFIED);
1263
1264         if (!auth2(cli, wks_creds)) {
1265                 d_printf("auth2 failed\n");
1266                 goto done;
1267         }
1268
1269         if (!leave(cli, samr_creds, wks_creds)) {
1270                 d_printf("leave failed\n");
1271                 goto done;
1272         }
1273
1274         talloc_free(cli);
1275
1276         ret = True;
1277
1278  done:
1279         return ret;
1280 }
1281
1282 /*
1283  * Test the different session key variants. Do it by joining, this uses the
1284  * session key in the setpassword routine. Test the join by doing the auth2.
1285  */
1286
1287 BOOL torture_samba3_sessionkey(struct torture_context *torture)
1288 {
1289         TALLOC_CTX *mem_ctx;
1290         BOOL ret = False;
1291         struct cli_credentials *anon_creds;
1292         const char *wks_name;
1293
1294         wks_name = torture_setting_string(torture, "wksname", get_myname());
1295
1296         mem_ctx = talloc_init("torture_samba3_sessionkey");
1297
1298         if (mem_ctx == NULL) {
1299                 d_printf("talloc_init failed\n");
1300                 return False;
1301         }
1302
1303         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1304                 d_printf("create_anon_creds failed\n");
1305                 goto done;
1306         }
1307
1308         ret = True;
1309
1310         if (!torture_setting_bool(torture, "samba3", False)) {
1311
1312                 /* Samba3 in the build farm right now does this happily. Need
1313                  * to fix :-) */
1314
1315                 if (test_join3(mem_ctx, False, anon_creds, NULL, wks_name)) {
1316                         d_printf("join using anonymous bind on an anonymous smb "
1317                                  "connection succeeded -- HUH??\n");
1318                         ret = False;
1319                 }
1320         }
1321
1322         if (!test_join3(mem_ctx, False, anon_creds, cmdline_credentials,
1323                         wks_name)) {
1324                 d_printf("join using ntlmssp bind on an anonymous smb "
1325                          "connection failed\n");
1326                 ret = False;
1327         }
1328
1329         if (!test_join3(mem_ctx, False, cmdline_credentials, NULL, wks_name)) {
1330                 d_printf("join using anonymous bind on an authenticated smb "
1331                          "connection failed\n");
1332                 ret = False;
1333         }
1334
1335         if (!test_join3(mem_ctx, False, cmdline_credentials,
1336                         cmdline_credentials,
1337                         wks_name)) {
1338                 d_printf("join using ntlmssp bind on an authenticated smb "
1339                          "connection failed\n");
1340                 ret = False;
1341         }
1342
1343         /*
1344          * The following two are tests for setuserinfolevel 25
1345          */
1346
1347         if (!test_join3(mem_ctx, True, anon_creds, cmdline_credentials,
1348                         wks_name)) {
1349                 d_printf("join using ntlmssp bind on an anonymous smb "
1350                          "connection failed\n");
1351                 ret = False;
1352         }
1353
1354         if (!test_join3(mem_ctx, True, cmdline_credentials, NULL, wks_name)) {
1355                 d_printf("join using anonymous bind on an authenticated smb "
1356                          "connection failed\n");
1357                 ret = False;
1358         }
1359
1360  done:
1361
1362         return ret;
1363 }
1364
1365 /*
1366  * open pipe and bind, given an IPC$ context
1367  */
1368
1369 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1370                               struct smbcli_tree *tree,
1371                               const char *pipe_name,
1372                               const struct ndr_interface_table *iface,
1373                               struct dcerpc_pipe **p)
1374 {
1375         struct dcerpc_pipe *result;
1376         NTSTATUS status;
1377
1378         if (!(result = dcerpc_pipe_init(
1379                       mem_ctx, tree->session->transport->socket->event.ctx))) {
1380                 return NT_STATUS_NO_MEMORY;
1381         }
1382
1383         status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1384         if (!NT_STATUS_IS_OK(status)) {
1385                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1386                          nt_errstr(status));
1387                 talloc_free(result);
1388                 return status;
1389         }
1390
1391         status = dcerpc_bind_auth_none(result, iface);
1392         if (!NT_STATUS_IS_OK(status)) {
1393                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1394                 talloc_free(result);
1395                 return status;
1396         }
1397
1398         *p = result;
1399         return NT_STATUS_OK;
1400 }
1401
1402 /*
1403  * Sane wrapper around lsa_LookupNames
1404  */
1405
1406 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1407                                 struct dcerpc_pipe *p,
1408                                 const char *name,
1409                                 const char *domain)
1410 {
1411         struct lsa_ObjectAttribute attr;
1412         struct lsa_QosInfo qos;
1413         struct lsa_OpenPolicy2 r;
1414         struct lsa_Close c;
1415         NTSTATUS status;
1416         struct policy_handle handle;
1417         struct lsa_LookupNames l;
1418         struct lsa_TransSidArray sids;
1419         struct lsa_String lsa_name;
1420         uint32_t count = 0;
1421         struct dom_sid *result;
1422         TALLOC_CTX *tmp_ctx;
1423
1424         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1425                 return NULL;
1426         }
1427
1428         qos.len = 0;
1429         qos.impersonation_level = 2;
1430         qos.context_mode = 1;
1431         qos.effective_only = 0;
1432
1433         attr.len = 0;
1434         attr.root_dir = NULL;
1435         attr.object_name = NULL;
1436         attr.attributes = 0;
1437         attr.sec_desc = NULL;
1438         attr.sec_qos = &qos;
1439
1440         r.in.system_name = "\\";
1441         r.in.attr = &attr;
1442         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1443         r.out.handle = &handle;
1444
1445         status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1446         if (!NT_STATUS_IS_OK(status)) {
1447                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1448                 talloc_free(tmp_ctx);
1449                 return NULL;
1450         }
1451
1452         sids.count = 0;
1453         sids.sids = NULL;
1454
1455         lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1456
1457         l.in.handle = &handle;
1458         l.in.num_names = 1;
1459         l.in.names = &lsa_name;
1460         l.in.sids = &sids;
1461         l.in.level = 1;
1462         l.in.count = &count;
1463         l.out.count = &count;
1464         l.out.sids = &sids;
1465
1466         status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1467         if (!NT_STATUS_IS_OK(status)) {
1468                 printf("LookupNames of %s failed - %s\n", lsa_name.string, 
1469                        nt_errstr(status));
1470                 talloc_free(tmp_ctx);
1471                 return NULL;
1472         }
1473
1474         result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1475                                  l.out.sids->sids[0].rid);
1476
1477         c.in.handle = &handle;
1478         c.out.handle = &handle;
1479
1480         status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1481         if (!NT_STATUS_IS_OK(status)) {
1482                 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1483                 talloc_free(tmp_ctx);
1484                 return NULL;
1485         }
1486         
1487         talloc_free(tmp_ctx);
1488         return result;
1489 }
1490
1491 /*
1492  * Find out the user SID on this connection
1493  */
1494
1495 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1496 {
1497         struct dcerpc_pipe *lsa;
1498         struct lsa_GetUserName r;
1499         NTSTATUS status;
1500         struct lsa_StringPointer authority_name_p;
1501         struct dom_sid *result;
1502
1503         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1504                                &ndr_table_lsarpc, &lsa);
1505         if (!NT_STATUS_IS_OK(status)) {
1506                 d_printf("(%s) Could not bind to LSA: %s\n",
1507                          __location__, nt_errstr(status));
1508                 return NULL;
1509         }
1510
1511         r.in.system_name = "\\";
1512         r.in.account_name = NULL;
1513         authority_name_p.string = NULL;
1514         r.in.authority_name = &authority_name_p;
1515
1516         status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1517
1518         if (!NT_STATUS_IS_OK(status)) {
1519                 printf("(%s) GetUserName failed - %s\n",
1520                        __location__, nt_errstr(status));
1521                 talloc_free(lsa);
1522                 return NULL;
1523         }
1524
1525         result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1526                           r.out.authority_name->string->string);
1527
1528         talloc_free(lsa);
1529         return result;
1530 }
1531
1532 /*
1533  * Do a tcon, given a session
1534  */
1535
1536 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1537                         struct smbcli_session *session,
1538                         const char *sharename,
1539                         struct smbcli_tree **res)
1540 {
1541         struct smbcli_tree *result;
1542         TALLOC_CTX *tmp_ctx;
1543         union smb_tcon tcon;
1544         NTSTATUS status;
1545
1546         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1547                 return NT_STATUS_NO_MEMORY;
1548         }
1549
1550         if (!(result = smbcli_tree_init(session, mem_ctx, False))) {
1551                 talloc_free(tmp_ctx);
1552                 return NT_STATUS_NO_MEMORY;
1553         }
1554
1555         tcon.generic.level = RAW_TCON_TCONX;
1556         tcon.tconx.in.flags = 0;
1557         tcon.tconx.in.password = data_blob(NULL, 0);
1558         tcon.tconx.in.path = sharename;
1559         tcon.tconx.in.device = "?????";
1560
1561         status = smb_raw_tcon(result, tmp_ctx, &tcon);
1562         if (!NT_STATUS_IS_OK(status)) {
1563                 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1564                          nt_errstr(status));
1565                 talloc_free(tmp_ctx);
1566                 return status;
1567         }
1568
1569         result->tid = tcon.tconx.out.tid;
1570         result = talloc_steal(mem_ctx, result);
1571         talloc_free(tmp_ctx);
1572         *res = result;
1573         return NT_STATUS_OK;
1574 }
1575
1576 /*
1577  * Test the getusername behaviour
1578  */
1579
1580 BOOL torture_samba3_rpc_getusername(struct torture_context *torture)
1581 {
1582         NTSTATUS status;
1583         struct smbcli_state *cli;
1584         TALLOC_CTX *mem_ctx;
1585         BOOL ret = True;
1586         struct dom_sid *user_sid;
1587         struct dom_sid *created_sid;
1588         struct cli_credentials *anon_creds;
1589         struct cli_credentials *user_creds;
1590         char *domain_name;
1591
1592         if (!(mem_ctx = talloc_new(torture))) {
1593                 return False;
1594         }
1595
1596         status = smbcli_full_connection(
1597                 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1598                 "IPC$", NULL, cmdline_credentials, NULL);
1599         if (!NT_STATUS_IS_OK(status)) {
1600                 d_printf("(%s) smbcli_full_connection failed: %s\n",
1601                          __location__, nt_errstr(status));
1602                 ret = False;
1603                 goto done;
1604         }
1605
1606         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1607                 d_printf("(%s) whoami on auth'ed connection failed\n",
1608                          __location__);
1609                 ret = False;
1610         }
1611
1612         talloc_free(cli);
1613
1614         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1615                 d_printf("(%s) create_anon_creds failed\n", __location__);
1616                 ret = False;
1617                 goto done;
1618         }
1619
1620         status = smbcli_full_connection(
1621                 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1622                 "IPC$", NULL, anon_creds, NULL);
1623         if (!NT_STATUS_IS_OK(status)) {
1624                 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1625                          __location__, nt_errstr(status));
1626                 ret = False;
1627                 goto done;
1628         }
1629
1630         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1631                 d_printf("(%s) whoami on anon connection failed\n",
1632                          __location__);
1633                 ret = False;
1634                 goto done;
1635         }
1636
1637         if (!dom_sid_equal(user_sid,
1638                            dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1639                 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1640                          "S-1-5-7", __location__,
1641                          dom_sid_string(mem_ctx, user_sid));
1642                 ret = False;
1643         }
1644
1645         if (!(user_creds = cli_credentials_init(mem_ctx))) {
1646                 d_printf("(%s) cli_credentials_init failed\n", __location__);
1647                 ret = False;
1648                 goto done;
1649         }
1650
1651         cli_credentials_set_conf(user_creds, global_loadparm);
1652         cli_credentials_set_username(user_creds, "torture_username",
1653                                      CRED_SPECIFIED);
1654         cli_credentials_set_password(user_creds,
1655                                      generate_random_str(user_creds, 8),
1656                                      CRED_SPECIFIED);
1657
1658         if (!create_user(mem_ctx, cli, cmdline_credentials,
1659                          cli_credentials_get_username(user_creds),
1660                          cli_credentials_get_password(user_creds),
1661                          &domain_name, &created_sid)) {
1662                 d_printf("(%s) create_user failed\n", __location__);
1663                 ret = False;
1664                 goto done;
1665         }
1666
1667         cli_credentials_set_domain(user_creds, domain_name,
1668                                    CRED_SPECIFIED);
1669
1670         {
1671                 struct smbcli_session *session2;
1672                 struct smb_composite_sesssetup setup;
1673                 struct smbcli_tree *tree;
1674
1675                 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
1676                 if (session2 == NULL) {
1677                         d_printf("(%s) smbcli_session_init failed\n",
1678                                  __location__);
1679                         goto done;
1680                 }
1681
1682                 setup.in.sesskey = cli->transport->negotiate.sesskey;
1683                 setup.in.capabilities = cli->transport->negotiate.capabilities;
1684                 setup.in.workgroup = "";
1685                 setup.in.credentials = user_creds;
1686
1687                 status = smb_composite_sesssetup(session2, &setup);
1688                 if (!NT_STATUS_IS_OK(status)) {
1689                         d_printf("(%s) session setup with new user failed: "
1690                                  "%s\n", __location__, nt_errstr(status));
1691                         ret = False;
1692                         goto done;
1693                 }
1694                 session2->vuid = setup.out.vuid;
1695
1696                 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1697                                                     "IPC$", &tree))) {
1698                         d_printf("(%s) secondary_tcon failed\n",
1699                                  __location__);
1700                         ret = False;
1701                         goto done;
1702                 }
1703
1704                 if (!(user_sid = whoami(mem_ctx, tree))) {
1705                         d_printf("(%s) whoami on user connection failed\n",
1706                                  __location__);
1707                         ret = False;
1708                         goto delete;
1709                 }
1710
1711                 talloc_free(tree);
1712         }
1713
1714         d_printf("Created %s, found %s\n",
1715                  dom_sid_string(mem_ctx, created_sid),
1716                  dom_sid_string(mem_ctx, user_sid));
1717
1718         if (!dom_sid_equal(created_sid, user_sid)) {
1719                 ret = False;
1720         }
1721
1722  delete:
1723         if (!delete_user(cli, cmdline_credentials,
1724                          cli_credentials_get_username(user_creds))) {
1725                 d_printf("(%s) delete_user failed\n", __location__);
1726                 ret = False;
1727         }
1728
1729  done:
1730         talloc_free(mem_ctx);
1731         return ret;
1732 }
1733
1734 static BOOL test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1735                                  const char *sharename)
1736 {
1737         NTSTATUS status;
1738         struct srvsvc_NetShareGetInfo r;
1739         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1740         int i;
1741         BOOL ret = True;
1742
1743         r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1744                                           dcerpc_server_name(p));
1745         r.in.share_name = sharename;
1746
1747         for (i=0;i<ARRAY_SIZE(levels);i++) {
1748                 r.in.level = levels[i];
1749
1750                 ZERO_STRUCT(r.out);
1751
1752                 printf("testing NetShareGetInfo level %u on share '%s'\n", 
1753                        r.in.level, r.in.share_name);
1754
1755                 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1756                 if (!NT_STATUS_IS_OK(status)) {
1757                         printf("NetShareGetInfo level %u on share '%s' failed"
1758                                " - %s\n", r.in.level, r.in.share_name,
1759                                nt_errstr(status));
1760                         ret = False;
1761                         continue;
1762                 }
1763                 if (!W_ERROR_IS_OK(r.out.result)) {
1764                         printf("NetShareGetInfo level %u on share '%s' failed "
1765                                "- %s\n", r.in.level, r.in.share_name,
1766                                win_errstr(r.out.result));
1767                         ret = False;
1768                         continue;
1769                 }
1770         }
1771
1772         return ret;
1773 }
1774
1775 static BOOL test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1776                               const char **one_sharename)
1777 {
1778         NTSTATUS status;
1779         struct srvsvc_NetShareEnum r;
1780         struct srvsvc_NetShareCtr0 c0;
1781         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1782         int i;
1783         BOOL ret = True;
1784
1785         r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1786         r.in.ctr.ctr0 = &c0;
1787         r.in.ctr.ctr0->count = 0;
1788         r.in.ctr.ctr0->array = NULL;
1789         r.in.max_buffer = (uint32_t)-1;
1790         r.in.resume_handle = NULL;
1791
1792         for (i=0;i<ARRAY_SIZE(levels);i++) {
1793                 r.in.level = levels[i];
1794
1795                 ZERO_STRUCT(r.out);
1796
1797                 printf("testing NetShareEnum level %u\n", r.in.level);
1798                 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1799                 if (!NT_STATUS_IS_OK(status)) {
1800                         printf("NetShareEnum level %u failed - %s\n",
1801                                r.in.level, nt_errstr(status));
1802                         ret = False;
1803                         continue;
1804                 }
1805                 if (!W_ERROR_IS_OK(r.out.result)) {
1806                         printf("NetShareEnum level %u failed - %s\n",
1807                                r.in.level, win_errstr(r.out.result));
1808                         continue;
1809                 }
1810                 if (r.in.level == 0) {
1811                         struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1812                         if (ctr->count > 0) {
1813                                 *one_sharename = ctr->array[0].name;
1814                         }
1815                 }
1816         }
1817
1818         return ret;
1819 }
1820
1821 BOOL torture_samba3_rpc_srvsvc(struct torture_context *torture)
1822 {
1823         struct dcerpc_pipe *p;
1824         TALLOC_CTX *mem_ctx;
1825         BOOL ret = True;
1826         const char *sharename = NULL;
1827         struct smbcli_state *cli;
1828         NTSTATUS status;
1829
1830         if (!(mem_ctx = talloc_new(torture))) {
1831                 return False;
1832         }
1833
1834         if (!(torture_open_connection_share(
1835                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1836                       "IPC$", NULL))) {
1837                 talloc_free(mem_ctx);
1838                 return False;
1839         }
1840
1841         status = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1842                                &ndr_table_srvsvc, &p);
1843         if (!NT_STATUS_IS_OK(status)) {
1844                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1845                          __location__, nt_errstr(status));
1846                 ret = False;
1847                 goto done;
1848         }
1849
1850         ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1851         if (sharename == NULL) {
1852                 printf("did not get sharename\n");
1853         } else {
1854                 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1855         }
1856
1857  done:
1858         talloc_free(mem_ctx);
1859         return ret;
1860 }
1861
1862 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1863                                                 struct smbcli_session *sess,
1864                                                 const char *sharename)
1865 {
1866         struct smbcli_tree *tree;
1867         TALLOC_CTX *tmp_ctx;
1868         struct dcerpc_pipe *p;
1869         NTSTATUS status;
1870         struct srvsvc_NetShareGetInfo r;
1871         struct security_descriptor *result;
1872
1873         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1874                 d_printf("talloc_new failed\n");
1875                 return NULL;
1876         }
1877
1878         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1879                 d_printf("secondary_tcon failed\n");
1880                 talloc_free(tmp_ctx);
1881                 return NULL;
1882         }
1883
1884         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1885                                &ndr_table_srvsvc, &p);
1886         if (!NT_STATUS_IS_OK(status)) {
1887                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1888                          __location__, nt_errstr(status));
1889                 talloc_free(tmp_ctx);
1890                 return NULL;
1891         }
1892
1893 #if 0
1894         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1895 #endif
1896
1897         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1898                                           dcerpc_server_name(p));
1899         r.in.share_name = sharename;
1900         r.in.level = 502;
1901
1902         status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
1903         if (!NT_STATUS_IS_OK(status)) {
1904                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1905                          nt_errstr(status));
1906                 talloc_free(tmp_ctx);
1907                 return NULL;
1908         }
1909
1910         result = talloc_steal(mem_ctx, r.out.info.info502->sd);
1911         talloc_free(tmp_ctx);
1912         return result;
1913 }
1914
1915 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
1916                              struct smbcli_session *sess,
1917                              const char *sharename,
1918                              struct security_descriptor *sd)
1919 {
1920         struct smbcli_tree *tree;
1921         TALLOC_CTX *tmp_ctx;
1922         struct dcerpc_pipe *p;
1923         NTSTATUS status;
1924         struct sec_desc_buf i;
1925         struct srvsvc_NetShareSetInfo r;
1926         uint32_t error = 0;
1927
1928         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1929                 d_printf("talloc_new failed\n");
1930                 return NT_STATUS_NO_MEMORY;
1931         }
1932
1933         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1934                 d_printf("secondary_tcon failed\n");
1935                 talloc_free(tmp_ctx);
1936                 return NT_STATUS_UNSUCCESSFUL;
1937         }
1938
1939         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1940                                &ndr_table_srvsvc, &p);
1941         if (!NT_STATUS_IS_OK(status)) {
1942                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1943                          __location__, nt_errstr(status));
1944                 talloc_free(tmp_ctx);
1945                 return NT_STATUS_UNSUCCESSFUL;
1946         }
1947
1948 #if 0
1949         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1950 #endif
1951
1952         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1953                                           dcerpc_server_name(p));
1954         r.in.share_name = sharename;
1955         r.in.level = 1501;
1956         i.sd = sd;
1957         r.in.info.info1501 = &i;
1958         r.in.parm_error = &error;
1959
1960         status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
1961         if (!NT_STATUS_IS_OK(status)) {
1962                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1963                          nt_errstr(status));
1964         }
1965
1966         talloc_free(tmp_ctx);
1967         return status;
1968 }
1969
1970 BOOL try_tcon(TALLOC_CTX *mem_ctx,
1971               struct security_descriptor *orig_sd,
1972               struct smbcli_session *session,
1973               const char *sharename, const struct dom_sid *user_sid,
1974               unsigned int access_mask, NTSTATUS expected_tcon,
1975               NTSTATUS expected_mkdir)
1976 {
1977         TALLOC_CTX *tmp_ctx;
1978         struct smbcli_tree *rmdir_tree, *tree;
1979         struct dom_sid *domain_sid;
1980         uint32_t rid;
1981         struct security_descriptor *sd;
1982         NTSTATUS status;
1983         BOOL ret = True;
1984
1985         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1986                 d_printf("talloc_new failed\n");
1987                 return False;
1988         }
1989
1990         status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
1991         if (!NT_STATUS_IS_OK(status)) {
1992                 d_printf("first tcon to delete dir failed\n");
1993                 talloc_free(tmp_ctx);
1994                 return False;
1995         }
1996
1997         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
1998
1999         if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2000                                                &domain_sid, &rid))) {
2001                 d_printf("dom_sid_split_rid failed\n");
2002                 talloc_free(tmp_ctx);
2003                 return False;
2004         }
2005
2006         sd = security_descriptor_create(
2007                 tmp_ctx, "S-1-5-32-544",
2008                 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2009                                                         DOMAIN_RID_USERS)),
2010                 dom_sid_string(mem_ctx, user_sid),
2011                 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2012         if (sd == NULL) {
2013                 d_printf("security_descriptor_create failed\n");
2014                 talloc_free(tmp_ctx);
2015                 return False;
2016         }
2017
2018         status = set_sharesec(mem_ctx, session, sharename, sd);
2019         if (!NT_STATUS_IS_OK(status)) {
2020                 d_printf("custom set_sharesec failed: %s\n",
2021                          nt_errstr(status));
2022                 talloc_free(tmp_ctx);
2023                 return False;
2024         }
2025
2026         status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2027         if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2028                 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2029                          nt_errstr(status));
2030                 ret = False;
2031                 goto done;
2032         }
2033
2034         if (!NT_STATUS_IS_OK(status)) {
2035                 /* An expected non-access, no point in trying to write */
2036                 goto done;
2037         }
2038
2039         status = smbcli_mkdir(tree, "sharesec_testdir");
2040         if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2041                 d_printf("(%s) Expected %s, got %s\n", __location__,
2042                          nt_errstr(expected_mkdir), nt_errstr(status));
2043                 ret = False;
2044         }
2045
2046  done:
2047         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2048
2049         status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2050         if (!NT_STATUS_IS_OK(status)) {
2051                 d_printf("custom set_sharesec failed: %s\n",
2052                          nt_errstr(status));
2053                 talloc_free(tmp_ctx);
2054                 return False;
2055         }
2056
2057         talloc_free(tmp_ctx);
2058         return ret;
2059 }
2060
2061 BOOL torture_samba3_rpc_sharesec(struct torture_context *torture)
2062 {
2063         TALLOC_CTX *mem_ctx;
2064         BOOL ret = True;
2065         struct smbcli_state *cli;
2066         struct security_descriptor *sd;
2067         struct dom_sid *user_sid;
2068
2069         if (!(mem_ctx = talloc_new(torture))) {
2070                 return False;
2071         }
2072
2073         if (!(torture_open_connection_share(
2074                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2075                       "IPC$", NULL))) {
2076                 d_printf("IPC$ connection failed\n");
2077                 talloc_free(mem_ctx);
2078                 return False;
2079         }
2080
2081         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2082                 d_printf("whoami failed\n");
2083                 talloc_free(mem_ctx);
2084                 return False;
2085         }
2086
2087         sd = get_sharesec(mem_ctx, cli->session, torture_setting_string(torture,
2088                                                                 "share", NULL));
2089
2090         ret &= try_tcon(mem_ctx, sd, cli->session,
2091                         torture_setting_string(torture, "share", NULL),
2092                         user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2093
2094         ret &= try_tcon(mem_ctx, sd, cli->session,
2095                         torture_setting_string(torture, "share", NULL),
2096                         user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2097                         NT_STATUS_MEDIA_WRITE_PROTECTED);
2098
2099         ret &= try_tcon(mem_ctx, sd, cli->session,
2100                         torture_setting_string(torture, "share", NULL),
2101                         user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2102
2103         talloc_free(mem_ctx);
2104         return ret;
2105 }
2106
2107 BOOL torture_samba3_rpc_lsa(struct torture_context *torture)
2108 {
2109         TALLOC_CTX *mem_ctx;
2110         BOOL ret = True;
2111         struct smbcli_state *cli;
2112         struct dcerpc_pipe *p;
2113         struct policy_handle lsa_handle;
2114         NTSTATUS status;
2115         struct dom_sid *domain_sid;
2116
2117         if (!(mem_ctx = talloc_new(torture))) {
2118                 return False;
2119         }
2120
2121         if (!(torture_open_connection_share(
2122                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2123                       "IPC$", NULL))) {
2124                 d_printf("IPC$ connection failed\n");
2125                 talloc_free(mem_ctx);
2126                 return False;
2127         }
2128
2129         status = pipe_bind_smb(mem_ctx, cli->tree, "\\lsarpc",
2130                                &ndr_table_lsarpc, &p);
2131         if (!NT_STATUS_IS_OK(status)) {
2132                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2133                          nt_errstr(status));
2134                 talloc_free(mem_ctx);
2135                 return False;
2136         }
2137
2138         {
2139                 struct lsa_ObjectAttribute attr;
2140                 struct lsa_OpenPolicy2 o;
2141                 o.in.system_name = talloc_asprintf(
2142                         mem_ctx, "\\\\%s", dcerpc_server_name(p));
2143                 ZERO_STRUCT(attr);
2144                 o.in.attr = &attr;
2145                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2146                 o.out.handle = &lsa_handle;
2147                 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2148                 if (!NT_STATUS_IS_OK(status)) {
2149                         d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2150                                  __location__, nt_errstr(status));
2151                         talloc_free(mem_ctx);
2152                         return False;
2153                 }
2154         }
2155
2156 #if 0
2157         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2158 #endif
2159
2160         {
2161                 int i;
2162                 int levels[] = { 2,3,5,6 };
2163
2164                 for (i=0; i<ARRAY_SIZE(levels); i++) {
2165                         struct lsa_QueryInfoPolicy r;
2166                         r.in.handle = &lsa_handle;
2167                         r.in.level = levels[i];
2168                         status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2169                         if (!NT_STATUS_IS_OK(status)) {
2170                                 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2171                                          "failed: %s\n", __location__,
2172                                          levels[i], nt_errstr(status));
2173                                 talloc_free(mem_ctx);
2174                                 return False;
2175                         }
2176                         if (levels[i] == 5) {
2177                                 domain_sid = r.out.info->account_domain.sid;
2178                         }
2179                 }
2180         }
2181
2182         return ret;
2183 }
2184
2185 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2186                                char **name)
2187 {
2188         struct rap_WserverGetInfo r;
2189         NTSTATUS status;
2190         char servername[17];
2191
2192         r.in.level = 0;
2193         r.in.bufsize = 0xffff;
2194
2195         status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
2196         if (!NT_STATUS_IS_OK(status)) {
2197                 return status;
2198         }
2199
2200         memcpy(servername, r.out.info.info0.name, 16);
2201         servername[16] = '\0';
2202
2203         if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2204                 return NT_STATUS_NO_MEMORY;
2205         }
2206
2207         return NT_STATUS_OK;
2208 }
2209
2210
2211 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct smbcli_tree *tree,
2212                               const char ***printers, int *num_printers)
2213 {
2214         TALLOC_CTX *mem_ctx;
2215         NTSTATUS status;
2216         struct dcerpc_pipe *p;
2217         struct srvsvc_NetShareEnum r;
2218         struct srvsvc_NetShareCtr1 c1_in;
2219         struct srvsvc_NetShareCtr1 *c1;
2220         int i;
2221
2222         mem_ctx = talloc_new(ctx);
2223         if (mem_ctx == NULL) {
2224                 return NT_STATUS_NO_MEMORY;
2225         }
2226
2227         status = pipe_bind_smb(mem_ctx, tree, "\\srvsvc", &ndr_table_srvsvc,
2228                                &p);
2229         if (!NT_STATUS_IS_OK(status)) {
2230                 d_printf("could not bind to srvsvc pipe\n");
2231                 talloc_free(mem_ctx);
2232                 return status;
2233         }
2234
2235         r.in.server_unc = talloc_asprintf(
2236                 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2237         r.in.level = 1;
2238         ZERO_STRUCT(c1_in);
2239         r.in.ctr.ctr1 = &c1_in;
2240         r.in.max_buffer = (uint32_t)-1;
2241         r.in.resume_handle = NULL;
2242
2243         status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2244         if (!NT_STATUS_IS_OK(status)) {
2245                 d_printf("NetShareEnum level %u failed - %s\n",
2246                          r.in.level, nt_errstr(status));
2247                 talloc_free(mem_ctx);
2248                 return status;
2249         }
2250
2251         *printers = NULL;
2252         *num_printers = 0;
2253         c1 = r.out.ctr.ctr1;
2254         for (i=0; i<c1->count; i++) {
2255                 if (c1->array[i].type != STYPE_PRINTQ) {
2256                         continue;
2257                 }
2258                 if (!add_string_to_array(ctx, c1->array[i].name,
2259                                          printers, num_printers)) {
2260                         talloc_free(ctx);
2261                         return NT_STATUS_NO_MEMORY;
2262                 }
2263         }
2264
2265         talloc_free(mem_ctx);
2266         return NT_STATUS_OK;
2267 }
2268
2269 static BOOL enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2270                          const char *servername, int level, int *num_printers)
2271 {
2272         struct spoolss_EnumPrinters r;
2273         NTSTATUS status;
2274         DATA_BLOB blob;
2275
2276         r.in.flags = PRINTER_ENUM_LOCAL;
2277         r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2278         r.in.level = level;
2279         r.in.buffer = NULL;
2280         r.in.offered = 0;
2281
2282         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2283         if (!NT_STATUS_IS_OK(status)) {
2284                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2285                          __location__, nt_errstr(status));
2286                 return False;
2287         }
2288
2289         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2290                 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2291                          "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2292                          win_errstr(r.out.result));
2293                 return False;
2294         }
2295
2296         blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2297         if (blob.data == NULL) {
2298                 d_printf("(%s) data_blob_talloc failed\n", __location__);
2299                 return False;
2300         }
2301
2302         r.in.buffer = &blob;
2303         r.in.offered = r.out.needed;
2304
2305         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2306         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2307                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2308                          "%s\n", __location__, nt_errstr(status),
2309                          win_errstr(r.out.result));
2310                 return False;
2311         }
2312
2313         *num_printers = r.out.count;
2314
2315         return True;
2316 }
2317
2318 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2319                                struct policy_handle *handle, int level,
2320                                union spoolss_PrinterInfo **res)
2321 {
2322         TALLOC_CTX *mem_ctx;
2323         struct spoolss_GetPrinter r;
2324         DATA_BLOB blob;
2325         NTSTATUS status;
2326
2327         mem_ctx = talloc_new(ctx);
2328         if (mem_ctx == NULL) {
2329                 return NT_STATUS_NO_MEMORY;
2330         }
2331
2332         r.in.handle = handle;
2333         r.in.level = level;
2334         r.in.buffer = NULL;
2335         r.in.offered = 0;
2336
2337         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2338         if (!NT_STATUS_IS_OK(status)) {
2339                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2340                          __location__, nt_errstr(status));
2341                 talloc_free(mem_ctx);
2342                 return status;
2343         }
2344
2345         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2346                 printf("GetPrinter unexpected return code %s, should "
2347                        "be WERR_INSUFFICIENT_BUFFER\n",
2348                        win_errstr(r.out.result));
2349                 talloc_free(mem_ctx);
2350                 return NT_STATUS_UNSUCCESSFUL;
2351         }
2352
2353         r.in.handle = handle;
2354         r.in.level = level;
2355         blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2356         if (blob.data == NULL) {
2357                 talloc_free(mem_ctx);
2358                 return NT_STATUS_NO_MEMORY;
2359         }
2360         memset(blob.data, 0, blob.length);
2361         r.in.buffer = &blob;
2362         r.in.offered = r.out.needed;
2363
2364         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2365         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2366                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2367                          "%s\n", __location__, nt_errstr(status),
2368                          win_errstr(r.out.result));
2369                 talloc_free(mem_ctx);
2370                 return NT_STATUS_IS_OK(status) ?
2371                         NT_STATUS_UNSUCCESSFUL : status;
2372         }
2373
2374         if (res != NULL) {
2375                 *res = talloc_steal(ctx, r.out.info);
2376         }
2377
2378         talloc_free(mem_ctx);
2379         return NT_STATUS_OK;
2380 }
2381
2382 BOOL torture_samba3_rpc_spoolss(struct torture_context *torture)
2383 {
2384         TALLOC_CTX *mem_ctx;
2385         BOOL ret = True;
2386         struct smbcli_state *cli;
2387         struct dcerpc_pipe *p;
2388         NTSTATUS status;
2389         struct policy_handle server_handle, printer_handle;
2390         const char **printers;
2391         int num_printers;
2392         struct spoolss_UserLevel1 userlevel1;
2393         char *servername;
2394
2395         if (!(mem_ctx = talloc_new(torture))) {
2396                 return False;
2397         }
2398
2399         if (!(torture_open_connection_share(
2400                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2401                       "IPC$", NULL))) {
2402                 d_printf("IPC$ connection failed\n");
2403                 talloc_free(mem_ctx);
2404                 return False;
2405         }
2406
2407         status = get_servername(mem_ctx, cli->tree, &servername);
2408         if (!NT_STATUS_IS_OK(status)) {
2409                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2410                           __location__, nt_errstr(status));
2411                 talloc_free(mem_ctx);
2412                 return False;
2413         }
2414
2415         if (!NT_STATUS_IS_OK(find_printers(mem_ctx, cli->tree,
2416                                            &printers, &num_printers))) {
2417                 talloc_free(mem_ctx);
2418                 return False;
2419         }
2420
2421         if (num_printers == 0) {
2422                 d_printf("Did not find printers\n");
2423                 talloc_free(mem_ctx);
2424                 return True;
2425         }
2426
2427         status = pipe_bind_smb(mem_ctx, cli->tree, "\\spoolss",
2428                                &ndr_table_spoolss, &p);
2429         if (!NT_STATUS_IS_OK(status)) {
2430                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2431                          nt_errstr(status));
2432                 talloc_free(mem_ctx);
2433                 return False;
2434         }
2435
2436         ZERO_STRUCT(userlevel1);
2437         userlevel1.client = talloc_asprintf(
2438                 mem_ctx, "\\\\%s", lp_netbios_name(global_loadparm));
2439         userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2440         userlevel1.build = 2600;
2441         userlevel1.major = 3;
2442         userlevel1.minor = 0;
2443         userlevel1.processor = 0;
2444
2445         {
2446                 struct spoolss_OpenPrinterEx r;
2447
2448                 ZERO_STRUCT(r);
2449                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2450                                                    servername);
2451                 r.in.datatype = NULL;
2452                 r.in.access_mask = 0;
2453                 r.in.level = 1;
2454                 r.in.userlevel.level1 = &userlevel1;
2455                 r.out.handle = &server_handle;
2456
2457                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2458                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2459                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2460                                  "%s, %s\n", __location__, nt_errstr(status),
2461                                  win_errstr(r.out.result));
2462                         talloc_free(mem_ctx);
2463                         return False;
2464                 }
2465         }
2466
2467         {
2468                 struct spoolss_ClosePrinter r;
2469
2470                 r.in.handle = &server_handle;
2471                 r.out.handle = &server_handle;
2472
2473                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2474                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2475                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2476                                  "%s, %s\n", __location__, nt_errstr(status),
2477                                  win_errstr(r.out.result));
2478                         talloc_free(mem_ctx);
2479                         return False;
2480                 }
2481         }
2482
2483         {
2484                 struct spoolss_OpenPrinterEx r;
2485
2486                 ZERO_STRUCT(r);
2487                 r.in.printername = talloc_asprintf(
2488                         mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2489                 r.in.datatype = NULL;
2490                 r.in.access_mask = 0;
2491                 r.in.level = 1;
2492                 r.in.userlevel.level1 = &userlevel1;
2493                 r.out.handle = &printer_handle;
2494
2495                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2496                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2497                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2498                                  "%s, %s\n", __location__, nt_errstr(status),
2499                                  win_errstr(r.out.result));
2500                         talloc_free(mem_ctx);
2501                         return False;
2502                 }
2503         }
2504
2505         {
2506                 int i;
2507
2508                 for (i=0; i<8; i++) {
2509                         status = getprinterinfo(mem_ctx, p, &printer_handle,
2510                                                 i, NULL);
2511                         if (!NT_STATUS_IS_OK(status)) {
2512                                 d_printf("(%s) getprinterinfo %d failed: %s\n",
2513                                          __location__, i, nt_errstr(status));
2514                                 ret = False;
2515                         }
2516                 }
2517         }
2518
2519         {
2520                 struct spoolss_ClosePrinter r;
2521
2522                 r.in.handle = &printer_handle;
2523                 r.out.handle = &printer_handle;
2524
2525                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2526                 if (!NT_STATUS_IS_OK(status)) {
2527                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2528                                  "%s\n", __location__, nt_errstr(status));
2529                         talloc_free(mem_ctx);
2530                         return False;
2531                 }
2532         }
2533
2534         {
2535                 int num_enumerated;
2536                 if (!enumprinters(mem_ctx, p, servername, 1,
2537                                   &num_enumerated)) {
2538                         d_printf("(%s) enumprinters failed\n", __location__);
2539                         talloc_free(mem_ctx);
2540                         return False;
2541                 }
2542                 if (num_printers != num_enumerated) {
2543                         d_printf("(%s) netshareenum gave %d printers, "
2544                                  "enumprinters lvl 1 gave %d\n", __location__,
2545                                  num_printers, num_enumerated);
2546                         talloc_free(mem_ctx);
2547                         return False;
2548                 }
2549         }
2550
2551         {
2552                 int num_enumerated;
2553                 if (!enumprinters(mem_ctx, p, servername, 2,
2554                                   &num_enumerated)) {
2555                         d_printf("(%s) enumprinters failed\n", __location__);
2556                         talloc_free(mem_ctx);
2557                         return False;
2558                 }
2559                 if (num_printers != num_enumerated) {
2560                         d_printf("(%s) netshareenum gave %d printers, "
2561                                  "enumprinters lvl 2 gave %d\n", __location__,
2562                                  num_printers, num_enumerated);
2563                         talloc_free(mem_ctx);
2564                         return False;
2565                 }
2566         }
2567
2568         talloc_free(mem_ctx);
2569
2570         return ret;
2571 }
2572
2573 BOOL torture_samba3_rpc_wkssvc(struct torture_context *torture)
2574 {
2575         TALLOC_CTX *mem_ctx;
2576         struct smbcli_state *cli;
2577         struct dcerpc_pipe *p;
2578         NTSTATUS status;
2579         char *servername;
2580
2581         if (!(mem_ctx = talloc_new(torture))) {
2582                 return False;
2583         }
2584
2585         if (!(torture_open_connection_share(
2586                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2587                       "IPC$", NULL))) {
2588                 d_printf("IPC$ connection failed\n");
2589                 talloc_free(mem_ctx);
2590                 return False;
2591         }
2592
2593         status = get_servername(mem_ctx, cli->tree, &servername);
2594         if (!NT_STATUS_IS_OK(status)) {
2595                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2596                           __location__, nt_errstr(status));
2597                 talloc_free(mem_ctx);
2598                 return False;
2599         }
2600
2601         status = pipe_bind_smb(mem_ctx, cli->tree, "\\wkssvc",
2602                                &ndr_table_wkssvc, &p);
2603         if (!NT_STATUS_IS_OK(status)) {
2604                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2605                          nt_errstr(status));
2606                 talloc_free(mem_ctx);
2607                 return False;
2608         }
2609
2610         {
2611                 struct wkssvc_NetWkstaInfo100 wks100;
2612                 union wkssvc_NetWkstaInfo info;
2613                 struct wkssvc_NetWkstaGetInfo r;
2614
2615                 r.in.server_name = "\\foo";
2616                 r.in.level = 100;
2617                 info.info100 = &wks100;
2618                 r.out.info = &info;
2619
2620                 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2621                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2622                         d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2623                                  "%s, %s\n", __location__, nt_errstr(status),
2624                                  win_errstr(r.out.result));
2625                         talloc_free(mem_ctx);
2626                         return False;
2627                 }
2628
2629                 if (strcmp(servername,
2630                            r.out.info->info100->server_name) != 0) {
2631                         d_printf("(%s) servername inconsistency: RAP=%s, "
2632                                  "dcerpc_wkssvc_NetWksGetInfo=%s",
2633                                  __location__, servername,
2634                                  r.out.info->info100->server_name);
2635                         talloc_free(mem_ctx);
2636                         return False;
2637                 }
2638         }
2639
2640         talloc_free(mem_ctx);
2641         return True;
2642 }
2643
2644 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2645                              struct policy_handle *handle)
2646 {
2647         struct winreg_CloseKey c;
2648         NTSTATUS status;
2649         TALLOC_CTX *mem_ctx;
2650
2651         c.in.handle = c.out.handle = handle;
2652
2653         if (!(mem_ctx = talloc_new(p))) {
2654                 return NT_STATUS_NO_MEMORY;
2655         }
2656
2657         status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2658         talloc_free(mem_ctx);
2659
2660         if (!NT_STATUS_IS_OK(status)) {
2661                 return status;
2662         }
2663
2664         if (!W_ERROR_IS_OK(c.out.result)) {
2665                 return werror_to_ntstatus(c.out.result);
2666         }
2667
2668         return NT_STATUS_OK;
2669 }
2670
2671 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2672                            TALLOC_CTX *mem_ctx)
2673 {
2674         uint32_t enum_index = 0;
2675
2676         while (1) {
2677                 struct winreg_EnumValue r;
2678                 struct winreg_StringBuf name;
2679                 enum winreg_Type type = 0;
2680                 uint8_t buf8[1024];
2681                 NTSTATUS status;
2682                 uint32_t size, length;
2683                 
2684                 r.in.handle = handle;
2685                 r.in.enum_index = enum_index;
2686                 name.name = "";
2687                 name.size = 1024;
2688                 r.in.name = r.out.name = &name;
2689                 size = 1024;
2690                 length = 5;
2691                 r.in.type = &type;
2692                 r.in.value = buf8;
2693                 r.in.size = &size;
2694                 r.in.length = &length;
2695
2696                 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2697                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2698                         return NT_STATUS_OK;
2699                 }
2700                 enum_index += 1;
2701         }
2702 }
2703
2704 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle, 
2705                          TALLOC_CTX *mem_ctx, int depth)
2706 {
2707         struct winreg_EnumKey r;
2708         struct winreg_StringBuf class, name;
2709         NTSTATUS status;
2710         NTTIME t = 0;
2711
2712         if (depth <= 0) {
2713                 return NT_STATUS_OK;
2714         }
2715
2716         class.name   = "";
2717         class.size   = 1024;
2718
2719         r.in.handle = handle;
2720         r.in.enum_index = 0;
2721         r.in.name = &name;
2722         r.in.keyclass = &class;
2723         r.out.name = &name;
2724         r.in.last_changed_time = &t;
2725
2726         do {
2727                 TALLOC_CTX *tmp_ctx;
2728                 struct winreg_OpenKey o;
2729                 struct policy_handle key_handle;
2730                 int i;
2731
2732                 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2733                         return NT_STATUS_NO_MEMORY;
2734                 }
2735
2736                 name.name = NULL;
2737                 name.size = 1024;
2738
2739                 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2740                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2741                         /* We're done enumerating */
2742                         talloc_free(tmp_ctx);
2743                         return NT_STATUS_OK;
2744                 }
2745
2746                 for (i=0; i<10-depth; i++)
2747                         printf(" ");
2748                 printf("%s\n", r.out.name->name);
2749                         
2750
2751                 o.in.parent_handle = handle;
2752                 o.in.keyname.name = r.out.name->name;
2753                 o.in.unknown = 0;
2754                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2755                 o.out.handle = &key_handle;
2756
2757                 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2758                 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2759                         enumkeys(p, &key_handle, tmp_ctx, depth-1);
2760                         enumvalues(p, &key_handle, tmp_ctx);
2761                         status = winreg_close(p, &key_handle);
2762                         if (!NT_STATUS_IS_OK(status)) {
2763                                 return status;
2764                         }
2765                 }
2766
2767                 talloc_free(tmp_ctx);
2768
2769                 r.in.enum_index += 1;
2770         } while(True);
2771
2772         return NT_STATUS_OK;
2773 }
2774
2775 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
2776
2777 static BOOL test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
2778                        const char *name, winreg_open_fn open_fn)
2779 {
2780         struct policy_handle handle;
2781         struct winreg_OpenHKLM r;
2782         NTSTATUS status;
2783
2784         r.in.system_name = 0;
2785         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2786         r.out.handle = &handle;
2787         
2788         status = open_fn(p, mem_ctx, &r);
2789         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2790                 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
2791                          nt_errstr(status), win_errstr(r.out.result));
2792                 return False;
2793         }
2794
2795         enumkeys(p, &handle, mem_ctx, 4);
2796
2797         status = winreg_close(p, &handle);
2798         if (!NT_STATUS_IS_OK(status)) {
2799                 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
2800                          __location__, nt_errstr(status));
2801                 return False;
2802         }
2803
2804         return True;
2805 }
2806
2807 BOOL torture_samba3_rpc_winreg(struct torture_context *torture)
2808 {
2809         NTSTATUS status;
2810         struct dcerpc_pipe *p;
2811         TALLOC_CTX *mem_ctx;
2812         BOOL ret = True;
2813         struct {
2814                 const char *name;
2815                 winreg_open_fn fn;
2816         } open_fns[] = {
2817                 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
2818                 {"OpenHKU",  (winreg_open_fn)dcerpc_winreg_OpenHKU },
2819                 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
2820                 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
2821                 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
2822 #if 0
2823         int i;
2824 #endif
2825
2826         mem_ctx = talloc_init("torture_rpc_winreg");
2827
2828         status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
2829
2830         if (!NT_STATUS_IS_OK(status)) {
2831                 talloc_free(mem_ctx);
2832                 return False;
2833         }
2834
2835 #if 1
2836         ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
2837 #else
2838         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
2839                 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
2840                         ret = False;
2841         }
2842 #endif
2843
2844         talloc_free(mem_ctx);
2845
2846         return ret;
2847 }