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