r25803: Make our security descriptor acl manipulation methods more generic so that we
[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 %s\n", nt_errstr(status));
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(global_loadparm, 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 /*
1863  * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1864  * NT_STATUS_NO_SAM_ACCOUNT
1865  */
1866
1867 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1868 {
1869         TALLOC_CTX *mem_ctx;
1870         struct dcerpc_pipe *net_pipe;
1871         char *wksname;
1872         bool result = false;
1873         NTSTATUS status;
1874         struct netr_ServerReqChallenge r;
1875         struct netr_Credential netr_cli_creds;
1876         struct netr_Credential netr_srv_creds;
1877         uint32_t negotiate_flags;
1878         struct netr_ServerAuthenticate2 a;
1879         struct creds_CredentialState *creds_state;
1880         struct netr_Credential netr_cred;
1881         struct samr_Password mach_pw;
1882         struct smbcli_state *cli;
1883
1884         if (!(mem_ctx = talloc_new(torture))) {
1885                 d_printf("talloc_new failed\n");
1886                 return false;
1887         }
1888
1889         if (!(wksname = generate_random_str_list(
1890                       mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
1891                 d_printf("generate_random_str_list failed\n");
1892                 goto done;
1893         }
1894
1895         if (!(torture_open_connection_share(
1896                       mem_ctx, &cli,
1897                       torture_setting_string(torture, "host", NULL),
1898                       "IPC$", NULL))) {
1899                 d_printf("IPC$ connection failed\n");
1900                 goto done;
1901         }
1902
1903         if (!(net_pipe = dcerpc_pipe_init(
1904                       mem_ctx, cli->transport->socket->event.ctx))) {
1905                 d_printf("dcerpc_pipe_init failed\n");
1906                 goto done;
1907         }
1908
1909         status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1910         if (!NT_STATUS_IS_OK(status)) {
1911                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1912                          nt_errstr(status));
1913                 goto done;
1914         }
1915
1916         status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1917         if (!NT_STATUS_IS_OK(status)) {
1918                 d_printf("dcerpc_bind_auth_none failed: %s\n",
1919                          nt_errstr(status));
1920                 goto done;
1921         }
1922
1923         r.in.computer_name = wksname;
1924         r.in.server_name = talloc_asprintf(
1925                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1926         if (r.in.server_name == NULL) {
1927                 d_printf("talloc_asprintf failed\n");
1928                 goto done;
1929         }
1930         generate_random_buffer(netr_cli_creds.data,
1931                                sizeof(netr_cli_creds.data));
1932         r.in.credentials = &netr_cli_creds;
1933         r.out.credentials = &netr_srv_creds;
1934
1935         status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
1936         if (!NT_STATUS_IS_OK(status)) {
1937                 d_printf("netr_ServerReqChallenge failed: %s\n",
1938                          nt_errstr(status));
1939                 goto done;
1940         }
1941
1942         negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
1943         E_md4hash("foobar", mach_pw.hash);
1944
1945         creds_state = talloc(mem_ctx, struct creds_CredentialState);
1946         creds_client_init(creds_state, r.in.credentials,
1947                           r.out.credentials, &mach_pw,
1948                           &netr_cred, negotiate_flags);
1949
1950         a.in.server_name = talloc_asprintf(
1951                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1952         a.in.account_name = talloc_asprintf(
1953                 mem_ctx, "%s$", wksname);
1954         a.in.computer_name = wksname;
1955         a.in.secure_channel_type = SEC_CHAN_WKSTA;
1956         a.in.negotiate_flags = &negotiate_flags;
1957         a.out.negotiate_flags = &negotiate_flags;
1958         a.in.credentials = &netr_cred;
1959         a.out.credentials = &netr_cred;
1960
1961         status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
1962
1963         if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
1964                 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
1965                          "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
1966                          nt_errstr(status));
1967                 goto done;
1968         }
1969
1970         result = true;
1971  done:
1972         talloc_free(mem_ctx);
1973         return result;
1974 }
1975
1976 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1977                                                 struct smbcli_session *sess,
1978                                                 const char *sharename)
1979 {
1980         struct smbcli_tree *tree;
1981         TALLOC_CTX *tmp_ctx;
1982         struct dcerpc_pipe *p;
1983         NTSTATUS status;
1984         struct srvsvc_NetShareGetInfo r;
1985         struct security_descriptor *result;
1986
1987         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1988                 d_printf("talloc_new failed\n");
1989                 return NULL;
1990         }
1991
1992         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1993                 d_printf("secondary_tcon failed\n");
1994                 talloc_free(tmp_ctx);
1995                 return NULL;
1996         }
1997
1998         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1999                                &ndr_table_srvsvc, &p);
2000         if (!NT_STATUS_IS_OK(status)) {
2001                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2002                          __location__, nt_errstr(status));
2003                 talloc_free(tmp_ctx);
2004                 return NULL;
2005         }
2006
2007 #if 0
2008         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2009 #endif
2010
2011         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2012                                           dcerpc_server_name(p));
2013         r.in.share_name = sharename;
2014         r.in.level = 502;
2015
2016         status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2017         if (!NT_STATUS_IS_OK(status)) {
2018                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2019                          nt_errstr(status));
2020                 talloc_free(tmp_ctx);
2021                 return NULL;
2022         }
2023
2024         result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2025         talloc_free(tmp_ctx);
2026         return result;
2027 }
2028
2029 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2030                              struct smbcli_session *sess,
2031                              const char *sharename,
2032                              struct security_descriptor *sd)
2033 {
2034         struct smbcli_tree *tree;
2035         TALLOC_CTX *tmp_ctx;
2036         struct dcerpc_pipe *p;
2037         NTSTATUS status;
2038         struct sec_desc_buf i;
2039         struct srvsvc_NetShareSetInfo r;
2040         uint32_t error = 0;
2041
2042         if (!(tmp_ctx = talloc_new(mem_ctx))) {
2043                 d_printf("talloc_new failed\n");
2044                 return NT_STATUS_NO_MEMORY;
2045         }
2046
2047         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2048                 d_printf("secondary_tcon failed\n");
2049                 talloc_free(tmp_ctx);
2050                 return NT_STATUS_UNSUCCESSFUL;
2051         }
2052
2053         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
2054                                &ndr_table_srvsvc, &p);
2055         if (!NT_STATUS_IS_OK(status)) {
2056                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2057                          __location__, nt_errstr(status));
2058                 talloc_free(tmp_ctx);
2059                 return NT_STATUS_UNSUCCESSFUL;
2060         }
2061
2062 #if 0
2063         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2064 #endif
2065
2066         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2067                                           dcerpc_server_name(p));
2068         r.in.share_name = sharename;
2069         r.in.level = 1501;
2070         i.sd = sd;
2071         r.in.info.info1501 = &i;
2072         r.in.parm_error = &error;
2073
2074         status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2075         if (!NT_STATUS_IS_OK(status)) {
2076                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2077                          nt_errstr(status));
2078         }
2079
2080         talloc_free(tmp_ctx);
2081         return status;
2082 }
2083
2084 bool try_tcon(TALLOC_CTX *mem_ctx,
2085               struct security_descriptor *orig_sd,
2086               struct smbcli_session *session,
2087               const char *sharename, const struct dom_sid *user_sid,
2088               unsigned int access_mask, NTSTATUS expected_tcon,
2089               NTSTATUS expected_mkdir)
2090 {
2091         TALLOC_CTX *tmp_ctx;
2092         struct smbcli_tree *rmdir_tree, *tree;
2093         struct dom_sid *domain_sid;
2094         uint32_t rid;
2095         struct security_descriptor *sd;
2096         NTSTATUS status;
2097         bool ret = true;
2098
2099         if (!(tmp_ctx = talloc_new(mem_ctx))) {
2100                 d_printf("talloc_new failed\n");
2101                 return false;
2102         }
2103
2104         status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2105         if (!NT_STATUS_IS_OK(status)) {
2106                 d_printf("first tcon to delete dir failed\n");
2107                 talloc_free(tmp_ctx);
2108                 return false;
2109         }
2110
2111         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2112
2113         if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2114                                                &domain_sid, &rid))) {
2115                 d_printf("dom_sid_split_rid failed\n");
2116                 talloc_free(tmp_ctx);
2117                 return false;
2118         }
2119
2120         sd = security_descriptor_dacl_create(
2121                 tmp_ctx, 0, "S-1-5-32-544",
2122                 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2123                                                         DOMAIN_RID_USERS)),
2124                 dom_sid_string(mem_ctx, user_sid),
2125                 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2126         if (sd == NULL) {
2127                 d_printf("security_descriptor_dacl_create failed\n");
2128                 talloc_free(tmp_ctx);
2129                 return false;
2130         }
2131
2132         status = set_sharesec(mem_ctx, session, sharename, sd);
2133         if (!NT_STATUS_IS_OK(status)) {
2134                 d_printf("custom set_sharesec failed: %s\n",
2135                          nt_errstr(status));
2136                 talloc_free(tmp_ctx);
2137                 return false;
2138         }
2139
2140         status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2141         if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2142                 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2143                          nt_errstr(status));
2144                 ret = false;
2145                 goto done;
2146         }
2147
2148         if (!NT_STATUS_IS_OK(status)) {
2149                 /* An expected non-access, no point in trying to write */
2150                 goto done;
2151         }
2152
2153         status = smbcli_mkdir(tree, "sharesec_testdir");
2154         if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2155                 d_printf("(%s) Expected %s, got %s\n", __location__,
2156                          nt_errstr(expected_mkdir), nt_errstr(status));
2157                 ret = false;
2158         }
2159
2160  done:
2161         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2162
2163         status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2164         if (!NT_STATUS_IS_OK(status)) {
2165                 d_printf("custom set_sharesec failed: %s\n",
2166                          nt_errstr(status));
2167                 talloc_free(tmp_ctx);
2168                 return false;
2169         }
2170
2171         talloc_free(tmp_ctx);
2172         return ret;
2173 }
2174
2175 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2176 {
2177         TALLOC_CTX *mem_ctx;
2178         bool ret = true;
2179         struct smbcli_state *cli;
2180         struct security_descriptor *sd;
2181         struct dom_sid *user_sid;
2182
2183         if (!(mem_ctx = talloc_new(torture))) {
2184                 return false;
2185         }
2186
2187         if (!(torture_open_connection_share(
2188                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2189                       "IPC$", NULL))) {
2190                 d_printf("IPC$ connection failed\n");
2191                 talloc_free(mem_ctx);
2192                 return false;
2193         }
2194
2195         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2196                 d_printf("whoami failed\n");
2197                 talloc_free(mem_ctx);
2198                 return false;
2199         }
2200
2201         sd = get_sharesec(mem_ctx, cli->session, torture_setting_string(torture,
2202                                                                 "share", NULL));
2203
2204         ret &= try_tcon(mem_ctx, sd, cli->session,
2205                         torture_setting_string(torture, "share", NULL),
2206                         user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2207
2208         ret &= try_tcon(mem_ctx, sd, cli->session,
2209                         torture_setting_string(torture, "share", NULL),
2210                         user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2211                         NT_STATUS_MEDIA_WRITE_PROTECTED);
2212
2213         ret &= try_tcon(mem_ctx, sd, cli->session,
2214                         torture_setting_string(torture, "share", NULL),
2215                         user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2216
2217         talloc_free(mem_ctx);
2218         return ret;
2219 }
2220
2221 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2222 {
2223         TALLOC_CTX *mem_ctx;
2224         bool ret = true;
2225         struct smbcli_state *cli;
2226         struct dcerpc_pipe *p;
2227         struct policy_handle lsa_handle;
2228         NTSTATUS status;
2229         struct dom_sid *domain_sid;
2230
2231         if (!(mem_ctx = talloc_new(torture))) {
2232                 return false;
2233         }
2234
2235         if (!(torture_open_connection_share(
2236                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2237                       "IPC$", NULL))) {
2238                 d_printf("IPC$ connection failed\n");
2239                 talloc_free(mem_ctx);
2240                 return false;
2241         }
2242
2243         status = pipe_bind_smb(mem_ctx, cli->tree, "\\lsarpc",
2244                                &ndr_table_lsarpc, &p);
2245         if (!NT_STATUS_IS_OK(status)) {
2246                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2247                          nt_errstr(status));
2248                 talloc_free(mem_ctx);
2249                 return false;
2250         }
2251
2252         {
2253                 struct lsa_ObjectAttribute attr;
2254                 struct lsa_OpenPolicy2 o;
2255                 o.in.system_name = talloc_asprintf(
2256                         mem_ctx, "\\\\%s", dcerpc_server_name(p));
2257                 ZERO_STRUCT(attr);
2258                 o.in.attr = &attr;
2259                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2260                 o.out.handle = &lsa_handle;
2261                 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2262                 if (!NT_STATUS_IS_OK(status)) {
2263                         d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2264                                  __location__, nt_errstr(status));
2265                         talloc_free(mem_ctx);
2266                         return false;
2267                 }
2268         }
2269
2270 #if 0
2271         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2272 #endif
2273
2274         {
2275                 int i;
2276                 int levels[] = { 2,3,5,6 };
2277
2278                 for (i=0; i<ARRAY_SIZE(levels); i++) {
2279                         struct lsa_QueryInfoPolicy r;
2280                         r.in.handle = &lsa_handle;
2281                         r.in.level = levels[i];
2282                         status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2283                         if (!NT_STATUS_IS_OK(status)) {
2284                                 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2285                                          "failed: %s\n", __location__,
2286                                          levels[i], nt_errstr(status));
2287                                 talloc_free(mem_ctx);
2288                                 return false;
2289                         }
2290                         if (levels[i] == 5) {
2291                                 domain_sid = r.out.info->account_domain.sid;
2292                         }
2293                 }
2294         }
2295
2296         return ret;
2297 }
2298
2299 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2300                                char **name)
2301 {
2302         struct rap_WserverGetInfo r;
2303         NTSTATUS status;
2304         char servername[17];
2305
2306         r.in.level = 0;
2307         r.in.bufsize = 0xffff;
2308
2309         status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
2310         if (!NT_STATUS_IS_OK(status)) {
2311                 return status;
2312         }
2313
2314         memcpy(servername, r.out.info.info0.name, 16);
2315         servername[16] = '\0';
2316
2317         if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2318                 return NT_STATUS_NO_MEMORY;
2319         }
2320
2321         return NT_STATUS_OK;
2322 }
2323
2324
2325 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct smbcli_tree *tree,
2326                               const char ***printers, int *num_printers)
2327 {
2328         TALLOC_CTX *mem_ctx;
2329         NTSTATUS status;
2330         struct dcerpc_pipe *p;
2331         struct srvsvc_NetShareEnum r;
2332         struct srvsvc_NetShareCtr1 c1_in;
2333         struct srvsvc_NetShareCtr1 *c1;
2334         int i;
2335
2336         mem_ctx = talloc_new(ctx);
2337         if (mem_ctx == NULL) {
2338                 return NT_STATUS_NO_MEMORY;
2339         }
2340
2341         status = pipe_bind_smb(mem_ctx, tree, "\\srvsvc", &ndr_table_srvsvc,
2342                                &p);
2343         if (!NT_STATUS_IS_OK(status)) {
2344                 d_printf("could not bind to srvsvc pipe\n");
2345                 talloc_free(mem_ctx);
2346                 return status;
2347         }
2348
2349         r.in.server_unc = talloc_asprintf(
2350                 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2351         r.in.level = 1;
2352         ZERO_STRUCT(c1_in);
2353         r.in.ctr.ctr1 = &c1_in;
2354         r.in.max_buffer = (uint32_t)-1;
2355         r.in.resume_handle = NULL;
2356
2357         status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2358         if (!NT_STATUS_IS_OK(status)) {
2359                 d_printf("NetShareEnum level %u failed - %s\n",
2360                          r.in.level, nt_errstr(status));
2361                 talloc_free(mem_ctx);
2362                 return status;
2363         }
2364
2365         *printers = NULL;
2366         *num_printers = 0;
2367         c1 = r.out.ctr.ctr1;
2368         for (i=0; i<c1->count; i++) {
2369                 if (c1->array[i].type != STYPE_PRINTQ) {
2370                         continue;
2371                 }
2372                 if (!add_string_to_array(ctx, c1->array[i].name,
2373                                          printers, num_printers)) {
2374                         talloc_free(ctx);
2375                         return NT_STATUS_NO_MEMORY;
2376                 }
2377         }
2378
2379         talloc_free(mem_ctx);
2380         return NT_STATUS_OK;
2381 }
2382
2383 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2384                          const char *servername, int level, int *num_printers)
2385 {
2386         struct spoolss_EnumPrinters r;
2387         NTSTATUS status;
2388         DATA_BLOB blob;
2389
2390         r.in.flags = PRINTER_ENUM_LOCAL;
2391         r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2392         r.in.level = level;
2393         r.in.buffer = NULL;
2394         r.in.offered = 0;
2395
2396         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2397         if (!NT_STATUS_IS_OK(status)) {
2398                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2399                          __location__, nt_errstr(status));
2400                 return false;
2401         }
2402
2403         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2404                 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2405                          "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2406                          win_errstr(r.out.result));
2407                 return false;
2408         }
2409
2410         blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2411         if (blob.data == NULL) {
2412                 d_printf("(%s) data_blob_talloc failed\n", __location__);
2413                 return false;
2414         }
2415
2416         r.in.buffer = &blob;
2417         r.in.offered = r.out.needed;
2418
2419         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2420         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2421                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2422                          "%s\n", __location__, nt_errstr(status),
2423                          win_errstr(r.out.result));
2424                 return false;
2425         }
2426
2427         *num_printers = r.out.count;
2428
2429         return true;
2430 }
2431
2432 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2433                                struct policy_handle *handle, int level,
2434                                union spoolss_PrinterInfo **res)
2435 {
2436         TALLOC_CTX *mem_ctx;
2437         struct spoolss_GetPrinter r;
2438         DATA_BLOB blob;
2439         NTSTATUS status;
2440
2441         mem_ctx = talloc_new(ctx);
2442         if (mem_ctx == NULL) {
2443                 return NT_STATUS_NO_MEMORY;
2444         }
2445
2446         r.in.handle = handle;
2447         r.in.level = level;
2448         r.in.buffer = NULL;
2449         r.in.offered = 0;
2450
2451         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2452         if (!NT_STATUS_IS_OK(status)) {
2453                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2454                          __location__, nt_errstr(status));
2455                 talloc_free(mem_ctx);
2456                 return status;
2457         }
2458
2459         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2460                 printf("GetPrinter unexpected return code %s, should "
2461                        "be WERR_INSUFFICIENT_BUFFER\n",
2462                        win_errstr(r.out.result));
2463                 talloc_free(mem_ctx);
2464                 return NT_STATUS_UNSUCCESSFUL;
2465         }
2466
2467         r.in.handle = handle;
2468         r.in.level = level;
2469         blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2470         if (blob.data == NULL) {
2471                 talloc_free(mem_ctx);
2472                 return NT_STATUS_NO_MEMORY;
2473         }
2474         memset(blob.data, 0, blob.length);
2475         r.in.buffer = &blob;
2476         r.in.offered = r.out.needed;
2477
2478         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2479         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2480                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2481                          "%s\n", __location__, nt_errstr(status),
2482                          win_errstr(r.out.result));
2483                 talloc_free(mem_ctx);
2484                 return NT_STATUS_IS_OK(status) ?
2485                         NT_STATUS_UNSUCCESSFUL : status;
2486         }
2487
2488         if (res != NULL) {
2489                 *res = talloc_steal(ctx, r.out.info);
2490         }
2491
2492         talloc_free(mem_ctx);
2493         return NT_STATUS_OK;
2494 }
2495
2496 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2497 {
2498         TALLOC_CTX *mem_ctx;
2499         bool ret = true;
2500         struct smbcli_state *cli;
2501         struct dcerpc_pipe *p;
2502         NTSTATUS status;
2503         struct policy_handle server_handle, printer_handle;
2504         const char **printers;
2505         int num_printers;
2506         struct spoolss_UserLevel1 userlevel1;
2507         char *servername;
2508
2509         if (!(mem_ctx = talloc_new(torture))) {
2510                 return false;
2511         }
2512
2513         if (!(torture_open_connection_share(
2514                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2515                       "IPC$", NULL))) {
2516                 d_printf("IPC$ connection failed\n");
2517                 talloc_free(mem_ctx);
2518                 return false;
2519         }
2520
2521         status = get_servername(mem_ctx, cli->tree, &servername);
2522         if (!NT_STATUS_IS_OK(status)) {
2523                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2524                           __location__, nt_errstr(status));
2525                 talloc_free(mem_ctx);
2526                 return false;
2527         }
2528
2529         if (!NT_STATUS_IS_OK(find_printers(mem_ctx, cli->tree,
2530                                            &printers, &num_printers))) {
2531                 talloc_free(mem_ctx);
2532                 return false;
2533         }
2534
2535         if (num_printers == 0) {
2536                 d_printf("Did not find printers\n");
2537                 talloc_free(mem_ctx);
2538                 return true;
2539         }
2540
2541         status = pipe_bind_smb(mem_ctx, cli->tree, "\\spoolss",
2542                                &ndr_table_spoolss, &p);
2543         if (!NT_STATUS_IS_OK(status)) {
2544                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2545                          nt_errstr(status));
2546                 talloc_free(mem_ctx);
2547                 return false;
2548         }
2549
2550         ZERO_STRUCT(userlevel1);
2551         userlevel1.client = talloc_asprintf(
2552                 mem_ctx, "\\\\%s", lp_netbios_name(global_loadparm));
2553         userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2554         userlevel1.build = 2600;
2555         userlevel1.major = 3;
2556         userlevel1.minor = 0;
2557         userlevel1.processor = 0;
2558
2559         {
2560                 struct spoolss_OpenPrinterEx r;
2561
2562                 ZERO_STRUCT(r);
2563                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2564                                                    servername);
2565                 r.in.datatype = NULL;
2566                 r.in.access_mask = 0;
2567                 r.in.level = 1;
2568                 r.in.userlevel.level1 = &userlevel1;
2569                 r.out.handle = &server_handle;
2570
2571                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2572                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2573                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2574                                  "%s, %s\n", __location__, nt_errstr(status),
2575                                  win_errstr(r.out.result));
2576                         talloc_free(mem_ctx);
2577                         return false;
2578                 }
2579         }
2580
2581         {
2582                 struct spoolss_ClosePrinter r;
2583
2584                 r.in.handle = &server_handle;
2585                 r.out.handle = &server_handle;
2586
2587                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2588                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2589                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2590                                  "%s, %s\n", __location__, nt_errstr(status),
2591                                  win_errstr(r.out.result));
2592                         talloc_free(mem_ctx);
2593                         return false;
2594                 }
2595         }
2596
2597         {
2598                 struct spoolss_OpenPrinterEx r;
2599
2600                 ZERO_STRUCT(r);
2601                 r.in.printername = talloc_asprintf(
2602                         mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2603                 r.in.datatype = NULL;
2604                 r.in.access_mask = 0;
2605                 r.in.level = 1;
2606                 r.in.userlevel.level1 = &userlevel1;
2607                 r.out.handle = &printer_handle;
2608
2609                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2610                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2611                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2612                                  "%s, %s\n", __location__, nt_errstr(status),
2613                                  win_errstr(r.out.result));
2614                         talloc_free(mem_ctx);
2615                         return false;
2616                 }
2617         }
2618
2619         {
2620                 int i;
2621
2622                 for (i=0; i<8; i++) {
2623                         status = getprinterinfo(mem_ctx, p, &printer_handle,
2624                                                 i, NULL);
2625                         if (!NT_STATUS_IS_OK(status)) {
2626                                 d_printf("(%s) getprinterinfo %d failed: %s\n",
2627                                          __location__, i, nt_errstr(status));
2628                                 ret = false;
2629                         }
2630                 }
2631         }
2632
2633         {
2634                 struct spoolss_ClosePrinter r;
2635
2636                 r.in.handle = &printer_handle;
2637                 r.out.handle = &printer_handle;
2638
2639                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2640                 if (!NT_STATUS_IS_OK(status)) {
2641                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2642                                  "%s\n", __location__, nt_errstr(status));
2643                         talloc_free(mem_ctx);
2644                         return false;
2645                 }
2646         }
2647
2648         {
2649                 int num_enumerated;
2650                 if (!enumprinters(mem_ctx, p, servername, 1,
2651                                   &num_enumerated)) {
2652                         d_printf("(%s) enumprinters failed\n", __location__);
2653                         talloc_free(mem_ctx);
2654                         return false;
2655                 }
2656                 if (num_printers != num_enumerated) {
2657                         d_printf("(%s) netshareenum gave %d printers, "
2658                                  "enumprinters lvl 1 gave %d\n", __location__,
2659                                  num_printers, num_enumerated);
2660                         talloc_free(mem_ctx);
2661                         return false;
2662                 }
2663         }
2664
2665         {
2666                 int num_enumerated;
2667                 if (!enumprinters(mem_ctx, p, servername, 2,
2668                                   &num_enumerated)) {
2669                         d_printf("(%s) enumprinters failed\n", __location__);
2670                         talloc_free(mem_ctx);
2671                         return false;
2672                 }
2673                 if (num_printers != num_enumerated) {
2674                         d_printf("(%s) netshareenum gave %d printers, "
2675                                  "enumprinters lvl 2 gave %d\n", __location__,
2676                                  num_printers, num_enumerated);
2677                         talloc_free(mem_ctx);
2678                         return false;
2679                 }
2680         }
2681
2682         talloc_free(mem_ctx);
2683
2684         return ret;
2685 }
2686
2687 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2688 {
2689         TALLOC_CTX *mem_ctx;
2690         struct smbcli_state *cli;
2691         struct dcerpc_pipe *p;
2692         NTSTATUS status;
2693         char *servername;
2694
2695         if (!(mem_ctx = talloc_new(torture))) {
2696                 return false;
2697         }
2698
2699         if (!(torture_open_connection_share(
2700                       mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2701                       "IPC$", NULL))) {
2702                 d_printf("IPC$ connection failed\n");
2703                 talloc_free(mem_ctx);
2704                 return false;
2705         }
2706
2707         status = get_servername(mem_ctx, cli->tree, &servername);
2708         if (!NT_STATUS_IS_OK(status)) {
2709                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2710                           __location__, nt_errstr(status));
2711                 talloc_free(mem_ctx);
2712                 return false;
2713         }
2714
2715         status = pipe_bind_smb(mem_ctx, cli->tree, "\\wkssvc",
2716                                &ndr_table_wkssvc, &p);
2717         if (!NT_STATUS_IS_OK(status)) {
2718                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2719                          nt_errstr(status));
2720                 talloc_free(mem_ctx);
2721                 return false;
2722         }
2723
2724         {
2725                 struct wkssvc_NetWkstaInfo100 wks100;
2726                 union wkssvc_NetWkstaInfo info;
2727                 struct wkssvc_NetWkstaGetInfo r;
2728
2729                 r.in.server_name = "\\foo";
2730                 r.in.level = 100;
2731                 info.info100 = &wks100;
2732                 r.out.info = &info;
2733
2734                 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2735                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2736                         d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2737                                  "%s, %s\n", __location__, nt_errstr(status),
2738                                  win_errstr(r.out.result));
2739                         talloc_free(mem_ctx);
2740                         return false;
2741                 }
2742
2743                 if (strcmp(servername,
2744                            r.out.info->info100->server_name) != 0) {
2745                         d_printf("(%s) servername inconsistency: RAP=%s, "
2746                                  "dcerpc_wkssvc_NetWksGetInfo=%s",
2747                                  __location__, servername,
2748                                  r.out.info->info100->server_name);
2749                         talloc_free(mem_ctx);
2750                         return false;
2751                 }
2752         }
2753
2754         talloc_free(mem_ctx);
2755         return true;
2756 }
2757
2758 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2759                              struct policy_handle *handle)
2760 {
2761         struct winreg_CloseKey c;
2762         NTSTATUS status;
2763         TALLOC_CTX *mem_ctx;
2764
2765         c.in.handle = c.out.handle = handle;
2766
2767         if (!(mem_ctx = talloc_new(p))) {
2768                 return NT_STATUS_NO_MEMORY;
2769         }
2770
2771         status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2772         talloc_free(mem_ctx);
2773
2774         if (!NT_STATUS_IS_OK(status)) {
2775                 return status;
2776         }
2777
2778         if (!W_ERROR_IS_OK(c.out.result)) {
2779                 return werror_to_ntstatus(c.out.result);
2780         }
2781
2782         return NT_STATUS_OK;
2783 }
2784
2785 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2786                            TALLOC_CTX *mem_ctx)
2787 {
2788         uint32_t enum_index = 0;
2789
2790         while (1) {
2791                 struct winreg_EnumValue r;
2792                 struct winreg_StringBuf name;
2793                 enum winreg_Type type = 0;
2794                 uint8_t buf8[1024];
2795                 NTSTATUS status;
2796                 uint32_t size, length;
2797                 
2798                 r.in.handle = handle;
2799                 r.in.enum_index = enum_index;
2800                 name.name = "";
2801                 name.size = 1024;
2802                 r.in.name = r.out.name = &name;
2803                 size = 1024;
2804                 length = 5;
2805                 r.in.type = &type;
2806                 r.in.value = buf8;
2807                 r.in.size = &size;
2808                 r.in.length = &length;
2809
2810                 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2811                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2812                         return NT_STATUS_OK;
2813                 }
2814                 enum_index += 1;
2815         }
2816 }
2817
2818 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle, 
2819                          TALLOC_CTX *mem_ctx, int depth)
2820 {
2821         struct winreg_EnumKey r;
2822         struct winreg_StringBuf class, name;
2823         NTSTATUS status;
2824         NTTIME t = 0;
2825
2826         if (depth <= 0) {
2827                 return NT_STATUS_OK;
2828         }
2829
2830         class.name   = "";
2831         class.size   = 1024;
2832
2833         r.in.handle = handle;
2834         r.in.enum_index = 0;
2835         r.in.name = &name;
2836         r.in.keyclass = &class;
2837         r.out.name = &name;
2838         r.in.last_changed_time = &t;
2839
2840         do {
2841                 TALLOC_CTX *tmp_ctx;
2842                 struct winreg_OpenKey o;
2843                 struct policy_handle key_handle;
2844                 int i;
2845
2846                 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2847                         return NT_STATUS_NO_MEMORY;
2848                 }
2849
2850                 name.name = NULL;
2851                 name.size = 1024;
2852
2853                 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2854                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2855                         /* We're done enumerating */
2856                         talloc_free(tmp_ctx);
2857                         return NT_STATUS_OK;
2858                 }
2859
2860                 for (i=0; i<10-depth; i++)
2861                         printf(" ");
2862                 printf("%s\n", r.out.name->name);
2863                         
2864
2865                 o.in.parent_handle = handle;
2866                 o.in.keyname.name = r.out.name->name;
2867                 o.in.unknown = 0;
2868                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2869                 o.out.handle = &key_handle;
2870
2871                 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2872                 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2873                         enumkeys(p, &key_handle, tmp_ctx, depth-1);
2874                         enumvalues(p, &key_handle, tmp_ctx);
2875                         status = winreg_close(p, &key_handle);
2876                         if (!NT_STATUS_IS_OK(status)) {
2877                                 return status;
2878                         }
2879                 }
2880
2881                 talloc_free(tmp_ctx);
2882
2883                 r.in.enum_index += 1;
2884         } while(true);
2885
2886         return NT_STATUS_OK;
2887 }
2888
2889 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
2890
2891 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
2892                        const char *name, winreg_open_fn open_fn)
2893 {
2894         struct policy_handle handle;
2895         struct winreg_OpenHKLM r;
2896         NTSTATUS status;
2897
2898         r.in.system_name = 0;
2899         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2900         r.out.handle = &handle;
2901         
2902         status = open_fn(p, mem_ctx, &r);
2903         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2904                 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
2905                          nt_errstr(status), win_errstr(r.out.result));
2906                 return false;
2907         }
2908
2909         enumkeys(p, &handle, mem_ctx, 4);
2910
2911         status = winreg_close(p, &handle);
2912         if (!NT_STATUS_IS_OK(status)) {
2913                 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
2914                          __location__, nt_errstr(status));
2915                 return false;
2916         }
2917
2918         return true;
2919 }
2920
2921 bool torture_samba3_rpc_winreg(struct torture_context *torture)
2922 {
2923         NTSTATUS status;
2924         struct dcerpc_pipe *p;
2925         TALLOC_CTX *mem_ctx;
2926         bool ret = true;
2927         struct {
2928                 const char *name;
2929                 winreg_open_fn fn;
2930         } open_fns[] = {
2931                 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
2932                 {"OpenHKU",  (winreg_open_fn)dcerpc_winreg_OpenHKU },
2933                 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
2934                 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
2935                 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
2936 #if 0
2937         int i;
2938 #endif
2939
2940         mem_ctx = talloc_init("torture_rpc_winreg");
2941
2942         status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
2943
2944         if (!NT_STATUS_IS_OK(status)) {
2945                 talloc_free(mem_ctx);
2946                 return false;
2947         }
2948
2949 #if 1
2950         ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
2951 #else
2952         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
2953                 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
2954                         ret = false;
2955         }
2956 #endif
2957
2958         talloc_free(mem_ctx);
2959
2960         return ret;
2961 }