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