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