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