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