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