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