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