Merge branch 'master' of ssh://git.samba.org/data/git/samba
[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, lp_iconv_convenience(lp_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, iconv_convenience, 
2424                               name, servername) < 0) {
2425                 return NT_STATUS_NO_MEMORY;
2426         }
2427
2428         return NT_STATUS_OK;
2429 }
2430
2431
2432 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2433                               struct smbcli_tree *tree,
2434                               const char ***printers, int *num_printers)
2435 {
2436         TALLOC_CTX *mem_ctx;
2437         NTSTATUS status;
2438         struct dcerpc_pipe *p;
2439         struct srvsvc_NetShareEnum r;
2440         struct srvsvc_NetShareCtr1 c1_in;
2441         struct srvsvc_NetShareCtr1 *c1;
2442         int i;
2443
2444         mem_ctx = talloc_new(ctx);
2445         if (mem_ctx == NULL) {
2446                 return NT_STATUS_NO_MEMORY;
2447         }
2448
2449         status = pipe_bind_smb(mem_ctx, lp_ctx, 
2450                                tree, "\\srvsvc", &ndr_table_srvsvc,
2451                                &p);
2452         if (!NT_STATUS_IS_OK(status)) {
2453                 d_printf("could not bind to srvsvc pipe\n");
2454                 talloc_free(mem_ctx);
2455                 return status;
2456         }
2457
2458         r.in.server_unc = talloc_asprintf(
2459                 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2460         r.in.level = 1;
2461         ZERO_STRUCT(c1_in);
2462         r.in.ctr.ctr1 = &c1_in;
2463         r.in.max_buffer = (uint32_t)-1;
2464         r.in.resume_handle = NULL;
2465
2466         status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2467         if (!NT_STATUS_IS_OK(status)) {
2468                 d_printf("NetShareEnum level %u failed - %s\n",
2469                          r.in.level, nt_errstr(status));
2470                 talloc_free(mem_ctx);
2471                 return status;
2472         }
2473
2474         *printers = NULL;
2475         *num_printers = 0;
2476         c1 = r.out.ctr.ctr1;
2477         for (i=0; i<c1->count; i++) {
2478                 if (c1->array[i].type != STYPE_PRINTQ) {
2479                         continue;
2480                 }
2481                 if (!add_string_to_array(ctx, c1->array[i].name,
2482                                          printers, num_printers)) {
2483                         talloc_free(ctx);
2484                         return NT_STATUS_NO_MEMORY;
2485                 }
2486         }
2487
2488         talloc_free(mem_ctx);
2489         return NT_STATUS_OK;
2490 }
2491
2492 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2493                          const char *servername, int level, int *num_printers)
2494 {
2495         struct spoolss_EnumPrinters r;
2496         NTSTATUS status;
2497         DATA_BLOB blob;
2498
2499         r.in.flags = PRINTER_ENUM_LOCAL;
2500         r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2501         r.in.level = level;
2502         r.in.buffer = NULL;
2503         r.in.offered = 0;
2504
2505         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2506         if (!NT_STATUS_IS_OK(status)) {
2507                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2508                          __location__, nt_errstr(status));
2509                 return false;
2510         }
2511
2512         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2513                 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2514                          "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2515                          win_errstr(r.out.result));
2516                 return false;
2517         }
2518
2519         blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2520         if (blob.data == NULL) {
2521                 d_printf("(%s) data_blob_talloc failed\n", __location__);
2522                 return false;
2523         }
2524
2525         r.in.buffer = &blob;
2526         r.in.offered = r.out.needed;
2527
2528         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2529         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2530                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2531                          "%s\n", __location__, nt_errstr(status),
2532                          win_errstr(r.out.result));
2533                 return false;
2534         }
2535
2536         *num_printers = r.out.count;
2537
2538         return true;
2539 }
2540
2541 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2542                                struct policy_handle *handle, int level,
2543                                union spoolss_PrinterInfo **res)
2544 {
2545         TALLOC_CTX *mem_ctx;
2546         struct spoolss_GetPrinter r;
2547         DATA_BLOB blob;
2548         NTSTATUS status;
2549
2550         mem_ctx = talloc_new(ctx);
2551         if (mem_ctx == NULL) {
2552                 return NT_STATUS_NO_MEMORY;
2553         }
2554
2555         r.in.handle = handle;
2556         r.in.level = level;
2557         r.in.buffer = NULL;
2558         r.in.offered = 0;
2559
2560         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2561         if (!NT_STATUS_IS_OK(status)) {
2562                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2563                          __location__, nt_errstr(status));
2564                 talloc_free(mem_ctx);
2565                 return status;
2566         }
2567
2568         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2569                 printf("GetPrinter unexpected return code %s, should "
2570                        "be WERR_INSUFFICIENT_BUFFER\n",
2571                        win_errstr(r.out.result));
2572                 talloc_free(mem_ctx);
2573                 return NT_STATUS_UNSUCCESSFUL;
2574         }
2575
2576         r.in.handle = handle;
2577         r.in.level = level;
2578         blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2579         if (blob.data == NULL) {
2580                 talloc_free(mem_ctx);
2581                 return NT_STATUS_NO_MEMORY;
2582         }
2583         memset(blob.data, 0, blob.length);
2584         r.in.buffer = &blob;
2585         r.in.offered = r.out.needed;
2586
2587         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2588         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2589                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2590                          "%s\n", __location__, nt_errstr(status),
2591                          win_errstr(r.out.result));
2592                 talloc_free(mem_ctx);
2593                 return NT_STATUS_IS_OK(status) ?
2594                         NT_STATUS_UNSUCCESSFUL : status;
2595         }
2596
2597         if (res != NULL) {
2598                 *res = talloc_steal(ctx, r.out.info);
2599         }
2600
2601         talloc_free(mem_ctx);
2602         return NT_STATUS_OK;
2603 }
2604
2605 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2606 {
2607         TALLOC_CTX *mem_ctx;
2608         bool ret = true;
2609         struct smbcli_state *cli;
2610         struct dcerpc_pipe *p;
2611         NTSTATUS status;
2612         struct policy_handle server_handle, printer_handle;
2613         const char **printers;
2614         int num_printers;
2615         struct spoolss_UserLevel1 userlevel1;
2616         char *servername;
2617
2618         if (!(mem_ctx = talloc_new(torture))) {
2619                 return false;
2620         }
2621
2622         if (!(torture_open_connection_share(
2623                       mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2624                       "IPC$", torture->ev))) {
2625                 d_printf("IPC$ connection failed\n");
2626                 talloc_free(mem_ctx);
2627                 return false;
2628         }
2629
2630         status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2631         if (!NT_STATUS_IS_OK(status)) {
2632                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2633                           __location__, nt_errstr(status));
2634                 talloc_free(mem_ctx);
2635                 return false;
2636         }
2637
2638         if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2639                                            &printers, &num_printers))) {
2640                 talloc_free(mem_ctx);
2641                 return false;
2642         }
2643
2644         if (num_printers == 0) {
2645                 d_printf("Did not find printers\n");
2646                 talloc_free(mem_ctx);
2647                 return true;
2648         }
2649
2650         status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2651                                &ndr_table_spoolss, &p);
2652         if (!NT_STATUS_IS_OK(status)) {
2653                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2654                          nt_errstr(status));
2655                 talloc_free(mem_ctx);
2656                 return false;
2657         }
2658
2659         ZERO_STRUCT(userlevel1);
2660         userlevel1.client = talloc_asprintf(
2661                 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2662         userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2663         userlevel1.build = 2600;
2664         userlevel1.major = 3;
2665         userlevel1.minor = 0;
2666         userlevel1.processor = 0;
2667
2668         {
2669                 struct spoolss_OpenPrinterEx r;
2670
2671                 ZERO_STRUCT(r);
2672                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2673                                                    servername);
2674                 r.in.datatype = NULL;
2675                 r.in.access_mask = 0;
2676                 r.in.level = 1;
2677                 r.in.userlevel.level1 = &userlevel1;
2678                 r.out.handle = &server_handle;
2679
2680                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2681                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2682                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2683                                  "%s, %s\n", __location__, nt_errstr(status),
2684                                  win_errstr(r.out.result));
2685                         talloc_free(mem_ctx);
2686                         return false;
2687                 }
2688         }
2689
2690         {
2691                 struct spoolss_ClosePrinter r;
2692
2693                 r.in.handle = &server_handle;
2694                 r.out.handle = &server_handle;
2695
2696                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2697                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2698                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2699                                  "%s, %s\n", __location__, nt_errstr(status),
2700                                  win_errstr(r.out.result));
2701                         talloc_free(mem_ctx);
2702                         return false;
2703                 }
2704         }
2705
2706         {
2707                 struct spoolss_OpenPrinterEx r;
2708
2709                 ZERO_STRUCT(r);
2710                 r.in.printername = talloc_asprintf(
2711                         mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2712                 r.in.datatype = NULL;
2713                 r.in.access_mask = 0;
2714                 r.in.level = 1;
2715                 r.in.userlevel.level1 = &userlevel1;
2716                 r.out.handle = &printer_handle;
2717
2718                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2719                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2720                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2721                                  "%s, %s\n", __location__, nt_errstr(status),
2722                                  win_errstr(r.out.result));
2723                         talloc_free(mem_ctx);
2724                         return false;
2725                 }
2726         }
2727
2728         {
2729                 int i;
2730
2731                 for (i=0; i<8; i++) {
2732                         status = getprinterinfo(mem_ctx, p, &printer_handle,
2733                                                 i, NULL);
2734                         if (!NT_STATUS_IS_OK(status)) {
2735                                 d_printf("(%s) getprinterinfo %d failed: %s\n",
2736                                          __location__, i, nt_errstr(status));
2737                                 ret = false;
2738                         }
2739                 }
2740         }
2741
2742         {
2743                 struct spoolss_ClosePrinter r;
2744
2745                 r.in.handle = &printer_handle;
2746                 r.out.handle = &printer_handle;
2747
2748                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2749                 if (!NT_STATUS_IS_OK(status)) {
2750                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2751                                  "%s\n", __location__, nt_errstr(status));
2752                         talloc_free(mem_ctx);
2753                         return false;
2754                 }
2755         }
2756
2757         {
2758                 int num_enumerated;
2759                 if (!enumprinters(mem_ctx, p, servername, 1,
2760                                   &num_enumerated)) {
2761                         d_printf("(%s) enumprinters failed\n", __location__);
2762                         talloc_free(mem_ctx);
2763                         return false;
2764                 }
2765                 if (num_printers != num_enumerated) {
2766                         d_printf("(%s) netshareenum gave %d printers, "
2767                                  "enumprinters lvl 1 gave %d\n", __location__,
2768                                  num_printers, num_enumerated);
2769                         talloc_free(mem_ctx);
2770                         return false;
2771                 }
2772         }
2773
2774         {
2775                 int num_enumerated;
2776                 if (!enumprinters(mem_ctx, p, servername, 2,
2777                                   &num_enumerated)) {
2778                         d_printf("(%s) enumprinters failed\n", __location__);
2779                         talloc_free(mem_ctx);
2780                         return false;
2781                 }
2782                 if (num_printers != num_enumerated) {
2783                         d_printf("(%s) netshareenum gave %d printers, "
2784                                  "enumprinters lvl 2 gave %d\n", __location__,
2785                                  num_printers, num_enumerated);
2786                         talloc_free(mem_ctx);
2787                         return false;
2788                 }
2789         }
2790
2791         talloc_free(mem_ctx);
2792
2793         return ret;
2794 }
2795
2796 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2797 {
2798         TALLOC_CTX *mem_ctx;
2799         struct smbcli_state *cli;
2800         struct dcerpc_pipe *p;
2801         NTSTATUS status;
2802         char *servername;
2803
2804         if (!(mem_ctx = talloc_new(torture))) {
2805                 return false;
2806         }
2807
2808         if (!(torture_open_connection_share(
2809                       mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2810                       "IPC$", torture->ev))) {
2811                 d_printf("IPC$ connection failed\n");
2812                 talloc_free(mem_ctx);
2813                 return false;
2814         }
2815
2816         status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2817         if (!NT_STATUS_IS_OK(status)) {
2818                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2819                           __location__, nt_errstr(status));
2820                 talloc_free(mem_ctx);
2821                 return false;
2822         }
2823
2824         status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2825                                &ndr_table_wkssvc, &p);
2826         if (!NT_STATUS_IS_OK(status)) {
2827                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2828                          nt_errstr(status));
2829                 talloc_free(mem_ctx);
2830                 return false;
2831         }
2832
2833         {
2834                 struct wkssvc_NetWkstaInfo100 wks100;
2835                 union wkssvc_NetWkstaInfo info;
2836                 struct wkssvc_NetWkstaGetInfo r;
2837
2838                 r.in.server_name = "\\foo";
2839                 r.in.level = 100;
2840                 info.info100 = &wks100;
2841                 r.out.info = &info;
2842
2843                 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2844                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2845                         d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2846                                  "%s, %s\n", __location__, nt_errstr(status),
2847                                  win_errstr(r.out.result));
2848                         talloc_free(mem_ctx);
2849                         return false;
2850                 }
2851
2852                 if (strcmp(servername,
2853                            r.out.info->info100->server_name) != 0) {
2854                         d_printf("(%s) servername inconsistency: RAP=%s, "
2855                                  "dcerpc_wkssvc_NetWksGetInfo=%s",
2856                                  __location__, servername,
2857                                  r.out.info->info100->server_name);
2858                         talloc_free(mem_ctx);
2859                         return false;
2860                 }
2861         }
2862
2863         talloc_free(mem_ctx);
2864         return true;
2865 }
2866
2867 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2868                              struct policy_handle *handle)
2869 {
2870         struct winreg_CloseKey c;
2871         NTSTATUS status;
2872         TALLOC_CTX *mem_ctx;
2873
2874         c.in.handle = c.out.handle = handle;
2875
2876         if (!(mem_ctx = talloc_new(p))) {
2877                 return NT_STATUS_NO_MEMORY;
2878         }
2879
2880         status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2881         talloc_free(mem_ctx);
2882
2883         if (!NT_STATUS_IS_OK(status)) {
2884                 return status;
2885         }
2886
2887         if (!W_ERROR_IS_OK(c.out.result)) {
2888                 return werror_to_ntstatus(c.out.result);
2889         }
2890
2891         return NT_STATUS_OK;
2892 }
2893
2894 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2895                            TALLOC_CTX *mem_ctx)
2896 {
2897         uint32_t enum_index = 0;
2898
2899         while (1) {
2900                 struct winreg_EnumValue r;
2901                 struct winreg_StringBuf name;
2902                 enum winreg_Type type = 0;
2903                 uint8_t buf8[1024];
2904                 NTSTATUS status;
2905                 uint32_t size, length;
2906                 
2907                 r.in.handle = handle;
2908                 r.in.enum_index = enum_index;
2909                 name.name = "";
2910                 name.size = 1024;
2911                 r.in.name = r.out.name = &name;
2912                 size = 1024;
2913                 length = 5;
2914                 r.in.type = &type;
2915                 r.in.value = buf8;
2916                 r.in.size = &size;
2917                 r.in.length = &length;
2918
2919                 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2920                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2921                         return NT_STATUS_OK;
2922                 }
2923                 enum_index += 1;
2924         }
2925 }
2926
2927 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle, 
2928                          TALLOC_CTX *mem_ctx, int depth)
2929 {
2930         struct winreg_EnumKey r;
2931         struct winreg_StringBuf class, name;
2932         NTSTATUS status;
2933         NTTIME t = 0;
2934
2935         if (depth <= 0) {
2936                 return NT_STATUS_OK;
2937         }
2938
2939         class.name   = "";
2940         class.size   = 1024;
2941
2942         r.in.handle = handle;
2943         r.in.enum_index = 0;
2944         r.in.name = &name;
2945         r.in.keyclass = &class;
2946         r.out.name = &name;
2947         r.in.last_changed_time = &t;
2948
2949         do {
2950                 TALLOC_CTX *tmp_ctx;
2951                 struct winreg_OpenKey o;
2952                 struct policy_handle key_handle;
2953                 int i;
2954
2955                 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2956                         return NT_STATUS_NO_MEMORY;
2957                 }
2958
2959                 name.name = NULL;
2960                 name.size = 1024;
2961
2962                 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2963                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2964                         /* We're done enumerating */
2965                         talloc_free(tmp_ctx);
2966                         return NT_STATUS_OK;
2967                 }
2968
2969                 for (i=0; i<10-depth; i++)
2970                         printf(" ");
2971                 printf("%s\n", r.out.name->name);
2972                         
2973
2974                 o.in.parent_handle = handle;
2975                 o.in.keyname.name = r.out.name->name;
2976                 o.in.unknown = 0;
2977                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2978                 o.out.handle = &key_handle;
2979
2980                 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2981                 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2982                         enumkeys(p, &key_handle, tmp_ctx, depth-1);
2983                         enumvalues(p, &key_handle, tmp_ctx);
2984                         status = winreg_close(p, &key_handle);
2985                         if (!NT_STATUS_IS_OK(status)) {
2986                                 return status;
2987                         }
2988                 }
2989
2990                 talloc_free(tmp_ctx);
2991
2992                 r.in.enum_index += 1;
2993         } while(true);
2994
2995         return NT_STATUS_OK;
2996 }
2997
2998 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
2999
3000 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
3001                        const char *name, winreg_open_fn open_fn)
3002 {
3003         struct policy_handle handle;
3004         struct winreg_OpenHKLM r;
3005         NTSTATUS status;
3006
3007         r.in.system_name = 0;
3008         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3009         r.out.handle = &handle;
3010         
3011         status = open_fn(p, mem_ctx, &r);
3012         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3013                 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3014                          nt_errstr(status), win_errstr(r.out.result));
3015                 return false;
3016         }
3017
3018         enumkeys(p, &handle, mem_ctx, 4);
3019
3020         status = winreg_close(p, &handle);
3021         if (!NT_STATUS_IS_OK(status)) {
3022                 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3023                          __location__, nt_errstr(status));
3024                 return false;
3025         }
3026
3027         return true;
3028 }
3029
3030 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3031 {
3032         NTSTATUS status;
3033         struct dcerpc_pipe *p;
3034         TALLOC_CTX *mem_ctx;
3035         bool ret = true;
3036         struct {
3037                 const char *name;
3038                 winreg_open_fn fn;
3039         } open_fns[] = {
3040                 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3041                 {"OpenHKU",  (winreg_open_fn)dcerpc_winreg_OpenHKU },
3042                 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3043                 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3044                 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3045 #if 0
3046         int i;
3047 #endif
3048
3049         mem_ctx = talloc_init("torture_rpc_winreg");
3050
3051         status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3052
3053         if (!NT_STATUS_IS_OK(status)) {
3054                 talloc_free(mem_ctx);
3055                 return false;
3056         }
3057
3058 #if 1
3059         ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3060 #else
3061         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3062                 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3063                         ret = false;
3064         }
3065 #endif
3066
3067         talloc_free(mem_ctx);
3068
3069         return ret;
3070 }
3071
3072 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3073                               struct loadparm_context *lp_ctx,
3074                               struct smbcli_state *cli,
3075                               const char *share,
3076                               struct srvsvc_NetShareInfo502 **info)
3077 {
3078         struct smbcli_tree *ipc;
3079         struct dcerpc_pipe *p;
3080         struct srvsvc_NetShareGetInfo r;
3081         NTSTATUS status;
3082
3083         if (!(p = dcerpc_pipe_init(cli,
3084                                    cli->transport->socket->event.ctx,
3085                                    lp_iconv_convenience(lp_ctx)))) {
3086                 status = NT_STATUS_NO_MEMORY;
3087                 goto fail;
3088         }
3089
3090         status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3091         if (!NT_STATUS_IS_OK(status)) {
3092                 goto fail;
3093         }
3094
3095         status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3096         if (!NT_STATUS_IS_OK(status)) {
3097                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3098                          nt_errstr(status));
3099                 goto fail;
3100         }
3101
3102         status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3103         if (!NT_STATUS_IS_OK(status)) {
3104                 d_printf("dcerpc_bind_auth_none failed: %s\n",
3105                          nt_errstr(status));
3106                 goto fail;
3107         }
3108
3109         r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3110                                           dcerpc_server_name(p));
3111         r.in.share_name = share;
3112         r.in.level = 502;
3113
3114         status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3115         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3116                 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3117                          nt_errstr(status), win_errstr(r.out.result));
3118                 goto fail;
3119         }
3120
3121         *info = talloc_move(mem_ctx, &r.out.info.info502);
3122         return NT_STATUS_OK;
3123
3124  fail:
3125         talloc_free(p);
3126         return status;
3127 }
3128
3129 /*
3130  * Get us a handle on HKLM\
3131  */
3132
3133 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3134                                 struct smbcli_state *cli,
3135                                 struct smb_iconv_convenience *iconv_convenience,
3136                                 struct dcerpc_pipe **pipe_p,
3137                                 struct policy_handle **handle)
3138 {
3139         struct smbcli_tree *ipc;
3140         struct dcerpc_pipe *p;
3141         struct winreg_OpenHKLM r;
3142         NTSTATUS status;
3143         struct policy_handle *result;
3144
3145         result = talloc(mem_ctx, struct policy_handle);
3146
3147         if (result == NULL) {
3148                 return NT_STATUS_NO_MEMORY;
3149         }