Rewrite torture_samba3_rpc_sharesec() to use a non-privileged user for share security...
[Samba/id10ts.git] / source4 / torture / rpc / samba3rpc.c
blobf7968b1c56e663081a36256c937c2d1b1b56c769
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc torture tests, designed to walk Samba3 code paths
6 Copyright (C) Volker Lendecke 2006
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.
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.
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/>.
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 "../source3/libsmb/smb2cli.h"
50 #include "libcli/smb/smbXcli_base.h"
53 * This tests a RPC call using an invalid vuid
56 bool torture_bind_authcontext(struct torture_context *torture)
58 TALLOC_CTX *mem_ctx;
59 NTSTATUS status;
60 bool ret = false;
61 struct lsa_ObjectAttribute objectattr;
62 struct lsa_OpenPolicy2 openpolicy;
63 struct policy_handle handle;
64 struct lsa_Close close_handle;
65 struct smbcli_session *tmp;
66 struct smbcli_session *session2;
67 struct smbcli_state *cli;
68 struct dcerpc_pipe *lsa_pipe;
69 struct dcerpc_binding_handle *lsa_handle;
70 struct cli_credentials *anon_creds;
71 struct smb_composite_sesssetup setup;
72 struct smbcli_options options;
73 struct smbcli_session_options session_options;
75 mem_ctx = talloc_init("torture_bind_authcontext");
77 if (mem_ctx == NULL) {
78 torture_comment(torture, "talloc_init failed\n");
79 return false;
82 lpcfg_smbcli_options(torture->lp_ctx, &options);
83 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
85 status = smbcli_full_connection(mem_ctx, &cli,
86 torture_setting_string(torture, "host", NULL),
87 lpcfg_smb_ports(torture->lp_ctx),
88 "IPC$", NULL,
89 lpcfg_socket_options(torture->lp_ctx),
90 cmdline_credentials,
91 lpcfg_resolve_context(torture->lp_ctx),
92 torture->ev, &options, &session_options,
93 lpcfg_gensec_settings(torture, torture->lp_ctx));
94 if (!NT_STATUS_IS_OK(status)) {
95 torture_comment(torture, "smbcli_full_connection failed: %s\n",
96 nt_errstr(status));
97 goto done;
100 lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
101 if (lsa_pipe == NULL) {
102 torture_comment(torture, "dcerpc_pipe_init failed\n");
103 goto done;
105 lsa_handle = lsa_pipe->binding_handle;
107 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
108 if (!NT_STATUS_IS_OK(status)) {
109 torture_comment(torture, "dcerpc_pipe_open_smb failed: %s\n",
110 nt_errstr(status));
111 goto done;
114 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
115 if (!NT_STATUS_IS_OK(status)) {
116 torture_comment(torture, "dcerpc_bind_auth_none failed: %s\n",
117 nt_errstr(status));
118 goto done;
121 openpolicy.in.system_name =talloc_asprintf(
122 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
123 ZERO_STRUCT(objectattr);
124 openpolicy.in.attr = &objectattr;
125 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
126 openpolicy.out.handle = &handle;
128 status = dcerpc_lsa_OpenPolicy2_r(lsa_handle, mem_ctx, &openpolicy);
130 if (!NT_STATUS_IS_OK(status)) {
131 torture_comment(torture, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
132 nt_errstr(status));
133 goto done;
135 if (!NT_STATUS_IS_OK(openpolicy.out.result)) {
136 torture_comment(torture, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
137 nt_errstr(openpolicy.out.result));
138 goto done;
141 close_handle.in.handle = &handle;
142 close_handle.out.handle = &handle;
144 status = dcerpc_lsa_Close_r(lsa_handle, mem_ctx, &close_handle);
145 if (!NT_STATUS_IS_OK(status)) {
146 torture_comment(torture, "dcerpc_lsa_Close failed: %s\n",
147 nt_errstr(status));
148 goto done;
150 if (!NT_STATUS_IS_OK(close_handle.out.result)) {
151 torture_comment(torture, "dcerpc_lsa_Close failed: %s\n",
152 nt_errstr(close_handle.out.result));
153 goto done;
156 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
157 if (session2 == NULL) {
158 torture_comment(torture, "smbcli_session_init failed\n");
159 goto done;
162 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
163 torture_comment(torture, "create_anon_creds failed\n");
164 goto done;
167 setup.in.sesskey = cli->transport->negotiate.sesskey;
168 setup.in.capabilities = cli->transport->negotiate.capabilities;
169 setup.in.workgroup = "";
170 setup.in.credentials = anon_creds;
171 setup.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
173 status = smb_composite_sesssetup(session2, &setup);
174 if (!NT_STATUS_IS_OK(status)) {
175 torture_comment(torture, "anon session setup failed: %s\n",
176 nt_errstr(status));
177 goto done;
179 session2->vuid = setup.out.vuid;
181 tmp = cli->tree->session;
182 cli->tree->session = session2;
184 status = dcerpc_lsa_OpenPolicy2_r(lsa_handle, mem_ctx, &openpolicy);
186 cli->tree->session = tmp;
187 talloc_free(lsa_pipe);
188 lsa_pipe = NULL;
190 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
191 torture_comment(torture, "dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
192 "expected NT_STATUS_INVALID_HANDLE\n",
193 nt_errstr(status));
194 goto done;
197 ret = true;
198 done:
199 talloc_free(mem_ctx);
200 return ret;
204 * Bind to lsa using a specific auth method
207 static bool bindtest(struct torture_context *tctx,
208 struct smbcli_state *cli,
209 struct cli_credentials *credentials,
210 uint8_t auth_type, uint8_t auth_level)
212 TALLOC_CTX *mem_ctx;
213 bool ret = false;
214 NTSTATUS status;
216 struct dcerpc_pipe *lsa_pipe;
217 struct dcerpc_binding_handle *lsa_handle;
218 struct lsa_ObjectAttribute objectattr;
219 struct lsa_OpenPolicy2 openpolicy;
220 struct lsa_QueryInfoPolicy query;
221 union lsa_PolicyInformation *info = NULL;
222 struct policy_handle handle;
223 struct lsa_Close close_handle;
225 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
226 torture_comment(tctx, "talloc_init failed\n");
227 return false;
230 lsa_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
231 if (lsa_pipe == NULL) {
232 torture_comment(tctx, "dcerpc_pipe_init failed\n");
233 goto done;
235 lsa_handle = lsa_pipe->binding_handle;
237 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
238 if (!NT_STATUS_IS_OK(status)) {
239 torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
240 nt_errstr(status));
241 goto done;
244 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
245 credentials, lpcfg_gensec_settings(tctx->lp_ctx, tctx->lp_ctx), auth_type, auth_level,
246 NULL);
247 if (!NT_STATUS_IS_OK(status)) {
248 torture_comment(tctx, "dcerpc_bind_auth failed: %s\n", nt_errstr(status));
249 goto done;
252 openpolicy.in.system_name =talloc_asprintf(
253 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
254 ZERO_STRUCT(objectattr);
255 openpolicy.in.attr = &objectattr;
256 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
257 openpolicy.out.handle = &handle;
259 status = dcerpc_lsa_OpenPolicy2_r(lsa_handle, mem_ctx, &openpolicy);
261 if (!NT_STATUS_IS_OK(status)) {
262 torture_comment(tctx, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
263 nt_errstr(status));
264 goto done;
266 if (!NT_STATUS_IS_OK(openpolicy.out.result)) {
267 torture_comment(tctx, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
268 nt_errstr(openpolicy.out.result));
269 goto done;
272 query.in.handle = &handle;
273 query.in.level = LSA_POLICY_INFO_DOMAIN;
274 query.out.info = &info;
276 status = dcerpc_lsa_QueryInfoPolicy_r(lsa_handle, mem_ctx, &query);
277 if (!NT_STATUS_IS_OK(status)) {
278 torture_comment(tctx, "dcerpc_lsa_QueryInfoPolicy failed: %s\n",
279 nt_errstr(status));
280 goto done;
282 if (!NT_STATUS_IS_OK(query.out.result)) {
283 torture_comment(tctx, "dcerpc_lsa_QueryInfoPolicy failed: %s\n",
284 nt_errstr(query.out.result));
285 goto done;
288 close_handle.in.handle = &handle;
289 close_handle.out.handle = &handle;
291 status = dcerpc_lsa_Close_r(lsa_handle, mem_ctx, &close_handle);
292 if (!NT_STATUS_IS_OK(status)) {
293 torture_comment(tctx, "dcerpc_lsa_Close failed: %s\n",
294 nt_errstr(status));
295 goto done;
297 if (!NT_STATUS_IS_OK(close_handle.out.result)) {
298 torture_comment(tctx, "dcerpc_lsa_Close failed: %s\n",
299 nt_errstr(close_handle.out.result));
300 goto done;
304 ret = true;
305 done:
306 talloc_free(mem_ctx);
307 return ret;
311 * test authenticated RPC binds with the variants Samba3 does support
314 static bool torture_bind_samba3(struct torture_context *torture)
316 TALLOC_CTX *mem_ctx;
317 NTSTATUS status;
318 bool ret = false;
319 struct smbcli_state *cli;
320 struct smbcli_options options;
321 struct smbcli_session_options session_options;
323 mem_ctx = talloc_init("torture_bind_authcontext");
325 if (mem_ctx == NULL) {
326 torture_comment(torture, "talloc_init failed\n");
327 return false;
330 lpcfg_smbcli_options(torture->lp_ctx, &options);
331 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
333 status = smbcli_full_connection(mem_ctx, &cli,
334 torture_setting_string(torture, "host", NULL),
335 lpcfg_smb_ports(torture->lp_ctx),
336 "IPC$", NULL,
337 lpcfg_socket_options(torture->lp_ctx),
338 cmdline_credentials,
339 lpcfg_resolve_context(torture->lp_ctx),
340 torture->ev, &options, &session_options,
341 lpcfg_gensec_settings(torture, torture->lp_ctx));
342 if (!NT_STATUS_IS_OK(status)) {
343 torture_comment(torture, "smbcli_full_connection failed: %s\n",
344 nt_errstr(status));
345 goto done;
348 ret = true;
350 ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
351 DCERPC_AUTH_LEVEL_INTEGRITY);
352 ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
353 DCERPC_AUTH_LEVEL_PRIVACY);
354 ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
355 DCERPC_AUTH_LEVEL_INTEGRITY);
356 ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
357 DCERPC_AUTH_LEVEL_PRIVACY);
359 done:
360 talloc_free(mem_ctx);
361 return ret;
365 * Lookup or create a user and return all necessary info
368 static bool get_usr_handle(struct torture_context *tctx,
369 struct smbcli_state *cli,
370 TALLOC_CTX *mem_ctx,
371 struct cli_credentials *admin_creds,
372 uint8_t auth_type,
373 uint8_t auth_level,
374 const char *username,
375 char **domain,
376 struct dcerpc_pipe **result_pipe,
377 struct policy_handle **result_handle,
378 struct dom_sid **sid_p)
380 struct dcerpc_pipe *samr_pipe;
381 struct dcerpc_binding_handle *samr_handle;
382 NTSTATUS status;
383 struct policy_handle conn_handle;
384 struct policy_handle domain_handle;
385 struct policy_handle *user_handle;
386 struct samr_Connect2 conn;
387 struct samr_EnumDomains enumdom;
388 uint32_t resume_handle = 0;
389 uint32_t num_entries = 0;
390 struct samr_SamArray *sam = NULL;
391 struct samr_LookupDomain l;
392 struct dom_sid2 *sid = NULL;
393 int dom_idx;
394 struct lsa_String domain_name;
395 struct lsa_String user_name;
396 struct samr_OpenDomain o;
397 struct samr_CreateUser2 c;
398 uint32_t user_rid,access_granted;
400 samr_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
401 torture_assert(tctx, samr_pipe, "dcerpc_pipe_init failed");
403 samr_handle = samr_pipe->binding_handle;
405 torture_assert_ntstatus_ok(tctx,
406 dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr"),
407 "dcerpc_pipe_open_smb failed");
409 if (admin_creds != NULL) {
410 torture_assert_ntstatus_ok(tctx,
411 dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
412 admin_creds, lpcfg_gensec_settings(tctx->lp_ctx, tctx->lp_ctx), auth_type, auth_level,
413 NULL),
414 "dcerpc_bind_auth failed");
415 } else {
416 /* We must have an authenticated SMB connection */
417 torture_assert_ntstatus_ok(tctx,
418 dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr),
419 "dcerpc_bind_auth_none failed");
422 conn.in.system_name = talloc_asprintf(
423 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
424 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
425 conn.out.connect_handle = &conn_handle;
427 torture_assert_ntstatus_ok(tctx,
428 dcerpc_samr_Connect2_r(samr_handle, mem_ctx, &conn),
429 "samr_Connect2 failed");
430 torture_assert_ntstatus_ok(tctx, conn.out.result,
431 "samr_Connect2 failed");
433 enumdom.in.connect_handle = &conn_handle;
434 enumdom.in.resume_handle = &resume_handle;
435 enumdom.in.buf_size = (uint32_t)-1;
436 enumdom.out.resume_handle = &resume_handle;
437 enumdom.out.num_entries = &num_entries;
438 enumdom.out.sam = &sam;
440 torture_assert_ntstatus_ok(tctx,
441 dcerpc_samr_EnumDomains_r(samr_handle, mem_ctx, &enumdom),
442 "samr_EnumDomains failed");
443 torture_assert_ntstatus_ok(tctx, enumdom.out.result,
444 "samr_EnumDomains failed");
446 torture_assert_int_equal(tctx, *enumdom.out.num_entries, 2,
447 "samr_EnumDomains returned unexpected num_entries");
449 dom_idx = strequal(sam->entries[0].name.string,
450 "builtin") ? 1:0;
452 l.in.connect_handle = &conn_handle;
453 domain_name.string = sam->entries[dom_idx].name.string;
454 *domain = talloc_strdup(mem_ctx, domain_name.string);
455 l.in.domain_name = &domain_name;
456 l.out.sid = &sid;
458 torture_assert_ntstatus_ok(tctx,
459 dcerpc_samr_LookupDomain_r(samr_handle, mem_ctx, &l),
460 "samr_LookupDomain failed");
461 torture_assert_ntstatus_ok(tctx, l.out.result,
462 "samr_LookupDomain failed");
464 o.in.connect_handle = &conn_handle;
465 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
466 o.in.sid = *l.out.sid;
467 o.out.domain_handle = &domain_handle;
469 torture_assert_ntstatus_ok(tctx,
470 dcerpc_samr_OpenDomain_r(samr_handle, mem_ctx, &o),
471 "samr_OpenDomain failed");
472 torture_assert_ntstatus_ok(tctx, o.out.result,
473 "samr_OpenDomain failed");
475 c.in.domain_handle = &domain_handle;
476 user_name.string = username;
477 c.in.account_name = &user_name;
478 c.in.acct_flags = ACB_NORMAL;
479 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
480 user_handle = talloc(mem_ctx, struct policy_handle);
481 c.out.user_handle = user_handle;
482 c.out.access_granted = &access_granted;
483 c.out.rid = &user_rid;
485 torture_assert_ntstatus_ok(tctx,
486 dcerpc_samr_CreateUser2_r(samr_handle, mem_ctx, &c),
487 "samr_CreateUser2 failed");
489 if (NT_STATUS_EQUAL(c.out.result, NT_STATUS_USER_EXISTS)) {
490 struct samr_LookupNames ln;
491 struct samr_OpenUser ou;
492 struct samr_Ids rids, types;
494 ln.in.domain_handle = &domain_handle;
495 ln.in.num_names = 1;
496 ln.in.names = &user_name;
497 ln.out.rids = &rids;
498 ln.out.types = &types;
500 torture_assert_ntstatus_ok(tctx,
501 dcerpc_samr_LookupNames_r(samr_handle, mem_ctx, &ln),
502 "samr_LookupNames failed");
503 torture_assert_ntstatus_ok(tctx, ln.out.result,
504 "samr_LookupNames failed");
506 ou.in.domain_handle = &domain_handle;
507 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
508 user_rid = ou.in.rid = ln.out.rids->ids[0];
509 ou.out.user_handle = user_handle;
511 torture_assert_ntstatus_ok(tctx,
512 dcerpc_samr_OpenUser_r(samr_handle, mem_ctx, &ou),
513 "samr_OpenUser failed");
514 status = ou.out.result;
515 } else {
516 status = c.out.result;
519 torture_assert_ntstatus_ok(tctx, status,
520 "samr_CreateUser failed");
522 *result_pipe = samr_pipe;
523 *result_handle = user_handle;
524 if (sid_p != NULL) {
525 *sid_p = dom_sid_add_rid(mem_ctx, *l.out.sid, user_rid);
527 return true;
532 * Create a test user
535 static bool create_user(struct torture_context *tctx,
536 TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
537 struct cli_credentials *admin_creds,
538 const char *username, const char *password,
539 char **domain_name,
540 struct dom_sid **user_sid)
542 TALLOC_CTX *tmp_ctx;
543 NTSTATUS status;
544 struct dcerpc_pipe *samr_pipe;
545 struct dcerpc_binding_handle *samr_handle;
546 struct policy_handle *wks_handle;
547 bool ret = false;
549 if (!(tmp_ctx = talloc_new(mem_ctx))) {
550 torture_comment(tctx, "talloc_init failed\n");
551 return false;
554 ret = get_usr_handle(tctx, cli, tmp_ctx, admin_creds,
555 DCERPC_AUTH_TYPE_NTLMSSP,
556 DCERPC_AUTH_LEVEL_INTEGRITY,
557 username, domain_name, &samr_pipe, &wks_handle,
558 user_sid);
559 if (ret == false) {
560 torture_comment(tctx, "get_usr_handle failed\n");
561 goto done;
563 samr_handle = samr_pipe->binding_handle;
566 struct samr_SetUserInfo2 sui2;
567 struct samr_SetUserInfo sui;
568 struct samr_QueryUserInfo qui;
569 union samr_UserInfo u_info;
570 union samr_UserInfo *info;
571 DATA_BLOB session_key;
574 ZERO_STRUCT(u_info);
575 encode_pw_buffer(u_info.info23.password.data, password,
576 STR_UNICODE);
578 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
579 if (!NT_STATUS_IS_OK(status)) {
580 torture_comment(tctx, "dcerpc_fetch_session_key failed\n");
581 goto done;
583 arcfour_crypt_blob(u_info.info23.password.data, 516,
584 &session_key);
585 u_info.info23.info.password_expired = 0;
586 u_info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
587 SAMR_FIELD_LM_PASSWORD_PRESENT |
588 SAMR_FIELD_EXPIRED_FLAG;
589 sui2.in.user_handle = wks_handle;
590 sui2.in.info = &u_info;
591 sui2.in.level = 23;
593 status = dcerpc_samr_SetUserInfo2_r(samr_handle, tmp_ctx, &sui2);
594 if (!NT_STATUS_IS_OK(status)) {
595 torture_comment(tctx, "samr_SetUserInfo(23) failed: %s\n",
596 nt_errstr(status));
597 goto done;
599 if (!NT_STATUS_IS_OK(sui2.out.result)) {
600 torture_comment(tctx, "samr_SetUserInfo(23) failed: %s\n",
601 nt_errstr(sui2.out.result));
602 goto done;
605 u_info.info16.acct_flags = ACB_NORMAL;
606 sui.in.user_handle = wks_handle;
607 sui.in.info = &u_info;
608 sui.in.level = 16;
610 status = dcerpc_samr_SetUserInfo_r(samr_handle, tmp_ctx, &sui);
611 if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(sui.out.result)) {
612 torture_comment(tctx, "samr_SetUserInfo(16) failed\n");
613 goto done;
616 qui.in.user_handle = wks_handle;
617 qui.in.level = 21;
618 qui.out.info = &info;
620 status = dcerpc_samr_QueryUserInfo_r(samr_handle, tmp_ctx, &qui);
621 if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(qui.out.result)) {
622 torture_comment(tctx, "samr_QueryUserInfo(21) failed\n");
623 goto done;
626 info->info21.allow_password_change = 0;
627 info->info21.force_password_change = 0;
628 info->info21.account_name.string = NULL;
629 info->info21.rid = 0;
630 info->info21.acct_expiry = 0;
631 info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
633 u_info.info21 = info->info21;
634 sui.in.user_handle = wks_handle;
635 sui.in.info = &u_info;
636 sui.in.level = 21;
638 status = dcerpc_samr_SetUserInfo_r(samr_handle, tmp_ctx, &sui);
639 if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(sui.out.result)) {
640 torture_comment(tctx, "samr_SetUserInfo(21) failed\n");
641 goto done;
645 *domain_name= talloc_steal(mem_ctx, *domain_name);
646 *user_sid = talloc_steal(mem_ctx, *user_sid);
647 ret = true;
648 done:
649 talloc_free(tmp_ctx);
650 return ret;
654 * Delete a test user
657 static bool delete_user(struct torture_context *tctx,
658 struct smbcli_state *cli,
659 struct cli_credentials *admin_creds,
660 const char *username)
662 TALLOC_CTX *mem_ctx;
663 NTSTATUS status;
664 char *dom_name;
665 struct dcerpc_pipe *samr_pipe;
666 struct dcerpc_binding_handle *samr_handle;
667 struct policy_handle *user_handle;
668 bool ret = false;
670 if ((mem_ctx = talloc_init("leave")) == NULL) {
671 torture_comment(tctx, "talloc_init failed\n");
672 return false;
675 ret = get_usr_handle(tctx, cli, mem_ctx, admin_creds,
676 DCERPC_AUTH_TYPE_NTLMSSP,
677 DCERPC_AUTH_LEVEL_INTEGRITY,
678 username, &dom_name, &samr_pipe,
679 &user_handle, NULL);
680 if (ret == false) {
681 torture_comment(tctx, "get_wks_handle failed\n");
682 goto done;
684 samr_handle = samr_pipe->binding_handle;
687 struct samr_DeleteUser d;
689 d.in.user_handle = user_handle;
690 d.out.user_handle = user_handle;
692 status = dcerpc_samr_DeleteUser_r(samr_handle, mem_ctx, &d);
693 if (!NT_STATUS_IS_OK(status)) {
694 torture_comment(tctx, "samr_DeleteUser failed %s\n", nt_errstr(status));
695 goto done;
697 if (!NT_STATUS_IS_OK(d.out.result)) {
698 torture_comment(tctx, "samr_DeleteUser failed %s\n", nt_errstr(d.out.result));
699 goto done;
704 ret = true;
706 done:
707 talloc_free(mem_ctx);
708 return ret;
712 * Do a Samba3-style join
715 static bool join3(struct torture_context *tctx,
716 struct smbcli_state *cli,
717 bool use_level25,
718 struct cli_credentials *admin_creds,
719 struct cli_credentials *wks_creds)
721 TALLOC_CTX *mem_ctx;
722 NTSTATUS status;
723 char *dom_name;
724 struct dcerpc_pipe *samr_pipe;
725 struct dcerpc_binding_handle *samr_handle;
726 struct policy_handle *wks_handle;
727 bool ret = false;
728 NTTIME last_password_change;
730 if ((mem_ctx = talloc_init("join3")) == NULL) {
731 torture_comment(tctx, "talloc_init failed\n");
732 return false;
735 ret = get_usr_handle(
736 tctx, cli, mem_ctx, admin_creds,
737 DCERPC_AUTH_TYPE_NTLMSSP,
738 DCERPC_AUTH_LEVEL_PRIVACY,
739 talloc_asprintf(mem_ctx, "%s$",
740 cli_credentials_get_workstation(wks_creds)),
741 &dom_name, &samr_pipe, &wks_handle, NULL);
742 if (ret == false) {
743 torture_comment(tctx, "get_wks_handle failed\n");
744 goto done;
746 samr_handle = samr_pipe->binding_handle;
749 struct samr_QueryUserInfo q;
750 union samr_UserInfo *info;
752 q.in.user_handle = wks_handle;
753 q.in.level = 21;
754 q.out.info = &info;
756 status = dcerpc_samr_QueryUserInfo_r(samr_handle, mem_ctx, &q);
757 if (!NT_STATUS_IS_OK(status)) {
758 torture_warning(tctx, "QueryUserInfo failed: %s\n",
759 nt_errstr(status));
760 goto done;
762 if (!NT_STATUS_IS_OK(q.out.result)) {
763 torture_warning(tctx, "QueryUserInfo failed: %s\n",
764 nt_errstr(q.out.result));
765 goto done;
769 last_password_change = info->info21.last_password_change;
772 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
774 if (use_level25) {
775 struct samr_SetUserInfo2 sui2;
776 union samr_UserInfo u_info;
777 struct samr_UserInfo21 *i21 = &u_info.info25.info;
778 DATA_BLOB session_key;
779 DATA_BLOB confounded_session_key = data_blob_talloc(
780 mem_ctx, NULL, 16);
781 struct MD5Context ctx;
782 uint8_t confounder[16];
784 ZERO_STRUCT(u_info);
786 i21->full_name.string = talloc_asprintf(
787 mem_ctx, "%s$",
788 cli_credentials_get_workstation(wks_creds));
789 i21->acct_flags = ACB_WSTRUST;
790 i21->fields_present = SAMR_FIELD_FULL_NAME |
791 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_NT_PASSWORD_PRESENT;
792 /* this would break the test result expectations
793 i21->fields_present |= SAMR_FIELD_EXPIRED_FLAG;
794 i21->password_expired = 1;
797 encode_pw_buffer(u_info.info25.password.data,
798 cli_credentials_get_password(wks_creds),
799 STR_UNICODE);
800 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
801 if (!NT_STATUS_IS_OK(status)) {
802 torture_comment(tctx, "dcerpc_fetch_session_key failed: %s\n",
803 nt_errstr(status));
804 goto done;
806 generate_random_buffer((uint8_t *)confounder, 16);
808 MD5Init(&ctx);
809 MD5Update(&ctx, confounder, 16);
810 MD5Update(&ctx, session_key.data, session_key.length);
811 MD5Final(confounded_session_key.data, &ctx);
813 arcfour_crypt_blob(u_info.info25.password.data, 516,
814 &confounded_session_key);
815 memcpy(&u_info.info25.password.data[516], confounder, 16);
817 sui2.in.user_handle = wks_handle;
818 sui2.in.level = 25;
819 sui2.in.info = &u_info;
821 status = dcerpc_samr_SetUserInfo2_r(samr_handle, mem_ctx, &sui2);
822 if (!NT_STATUS_IS_OK(status)) {
823 torture_comment(tctx, "samr_SetUserInfo2(25) failed: %s\n",
824 nt_errstr(status));
825 goto done;
827 if (!NT_STATUS_IS_OK(sui2.out.result)) {
828 torture_comment(tctx, "samr_SetUserInfo2(25) failed: %s\n",
829 nt_errstr(sui2.out.result));
830 goto done;
832 } else {
833 struct samr_SetUserInfo2 sui2;
834 struct samr_SetUserInfo sui;
835 union samr_UserInfo u_info;
836 DATA_BLOB session_key;
838 encode_pw_buffer(u_info.info24.password.data,
839 cli_credentials_get_password(wks_creds),
840 STR_UNICODE);
841 /* just to make this test pass */
842 u_info.info24.password_expired = 1;
844 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
845 if (!NT_STATUS_IS_OK(status)) {
846 torture_comment(tctx, "dcerpc_fetch_session_key failed\n");
847 goto done;
849 arcfour_crypt_blob(u_info.info24.password.data, 516,
850 &session_key);
851 sui2.in.user_handle = wks_handle;
852 sui2.in.info = &u_info;
853 sui2.in.level = 24;
855 status = dcerpc_samr_SetUserInfo2_r(samr_handle, mem_ctx, &sui2);
856 if (!NT_STATUS_IS_OK(status)) {
857 torture_comment(tctx, "samr_SetUserInfo(24) failed: %s\n",
858 nt_errstr(status));
859 goto done;
861 if (!NT_STATUS_IS_OK(sui2.out.result)) {
862 torture_comment(tctx, "samr_SetUserInfo(24) failed: %s\n",
863 nt_errstr(sui2.out.result));
864 goto done;
867 u_info.info16.acct_flags = ACB_WSTRUST;
868 sui.in.user_handle = wks_handle;
869 sui.in.info = &u_info;
870 sui.in.level = 16;
872 status = dcerpc_samr_SetUserInfo_r(samr_handle, mem_ctx, &sui);
873 if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(sui.out.result)) {
874 torture_comment(tctx, "samr_SetUserInfo(16) failed\n");
875 goto done;
880 struct samr_QueryUserInfo q;
881 union samr_UserInfo *info;
883 q.in.user_handle = wks_handle;
884 q.in.level = 21;
885 q.out.info = &info;
887 status = dcerpc_samr_QueryUserInfo_r(samr_handle, mem_ctx, &q);
888 if (!NT_STATUS_IS_OK(status)) {
889 torture_warning(tctx, "QueryUserInfo failed: %s\n",
890 nt_errstr(status));
891 goto done;
893 if (!NT_STATUS_IS_OK(q.out.result)) {
894 torture_warning(tctx, "QueryUserInfo failed: %s\n",
895 nt_errstr(q.out.result));
896 goto done;
899 if (use_level25) {
900 if (last_password_change
901 == info->info21.last_password_change) {
902 torture_warning(tctx, "last_password_change unchanged "
903 "during join, level25 must change "
904 "it\n");
905 goto done;
908 else {
909 if (last_password_change
910 != info->info21.last_password_change) {
911 torture_warning(tctx, "last_password_change changed "
912 "during join, level24 doesn't "
913 "change it\n");
914 goto done;
919 ret = true;
921 done:
922 talloc_free(mem_ctx);
923 return ret;
927 * Do a ReqChallenge/Auth2 and get the wks creds
930 static bool auth2(struct torture_context *tctx,
931 struct smbcli_state *cli,
932 struct cli_credentials *wks_cred)
934 TALLOC_CTX *mem_ctx;
935 struct dcerpc_pipe *net_pipe;
936 struct dcerpc_binding_handle *net_handle;
937 bool result = false;
938 NTSTATUS status;
939 struct netr_ServerReqChallenge r;
940 struct netr_Credential netr_cli_creds;
941 struct netr_Credential netr_srv_creds;
942 uint32_t negotiate_flags;
943 struct netr_ServerAuthenticate2 a;
944 struct netlogon_creds_CredentialState *creds_state;
945 struct netr_Credential netr_cred;
946 struct samr_Password mach_pw;
948 mem_ctx = talloc_new(NULL);
949 if (mem_ctx == NULL) {
950 torture_comment(tctx, "talloc_new failed\n");
951 return false;
954 net_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
955 if (net_pipe == NULL) {
956 torture_comment(tctx, "dcerpc_pipe_init failed\n");
957 goto done;
959 net_handle = net_pipe->binding_handle;
961 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
962 if (!NT_STATUS_IS_OK(status)) {
963 torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
964 nt_errstr(status));
965 goto done;
968 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
969 if (!NT_STATUS_IS_OK(status)) {
970 torture_comment(tctx, "dcerpc_bind_auth_none failed: %s\n",
971 nt_errstr(status));
972 goto done;
975 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
976 r.in.server_name = talloc_asprintf(
977 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
978 if (r.in.server_name == NULL) {
979 torture_comment(tctx, "talloc_asprintf failed\n");
980 goto done;
982 generate_random_buffer(netr_cli_creds.data,
983 sizeof(netr_cli_creds.data));
984 r.in.credentials = &netr_cli_creds;
985 r.out.return_credentials = &netr_srv_creds;
987 status = dcerpc_netr_ServerReqChallenge_r(net_handle, mem_ctx, &r);
988 if (!NT_STATUS_IS_OK(status)) {
989 torture_comment(tctx, "netr_ServerReqChallenge failed: %s\n",
990 nt_errstr(status));
991 goto done;
993 if (!NT_STATUS_IS_OK(r.out.result)) {
994 torture_comment(tctx, "netr_ServerReqChallenge failed: %s\n",
995 nt_errstr(r.out.result));
996 goto done;
999 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
1000 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
1002 a.in.server_name = talloc_asprintf(
1003 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1004 a.in.account_name = talloc_asprintf(
1005 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
1006 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
1007 a.in.secure_channel_type = SEC_CHAN_WKSTA;
1008 a.in.negotiate_flags = &negotiate_flags;
1009 a.out.negotiate_flags = &negotiate_flags;
1010 a.in.credentials = &netr_cred;
1011 a.out.return_credentials = &netr_cred;
1013 creds_state = netlogon_creds_client_init(mem_ctx,
1014 a.in.account_name,
1015 a.in.computer_name,
1016 r.in.credentials,
1017 r.out.return_credentials, &mach_pw,
1018 &netr_cred, negotiate_flags);
1019 torture_assert(tctx, (creds_state != NULL), "memory allocation failed");
1021 status = dcerpc_netr_ServerAuthenticate2_r(net_handle, mem_ctx, &a);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 torture_comment(tctx, "netr_ServerServerAuthenticate2 failed: %s\n",
1024 nt_errstr(status));
1025 goto done;
1027 if (!NT_STATUS_IS_OK(a.out.result)) {
1028 torture_comment(tctx, "netr_ServerServerAuthenticate2 failed: %s\n",
1029 nt_errstr(a.out.result));
1030 goto done;
1033 if (!netlogon_creds_client_check(creds_state, a.out.return_credentials)) {
1034 torture_comment(tctx, "creds_client_check failed\n");
1035 goto done;
1038 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
1040 result = true;
1042 done:
1043 talloc_free(mem_ctx);
1044 return result;
1048 * Do a couple of schannel protected Netlogon ops: Interactive and Network
1049 * login, and change the wks password
1052 static bool schan(struct torture_context *tctx,
1053 struct smbcli_state *cli,
1054 struct cli_credentials *wks_creds,
1055 struct cli_credentials *user_creds)
1057 TALLOC_CTX *mem_ctx;
1058 NTSTATUS status;
1059 bool ret = false;
1060 struct dcerpc_pipe *net_pipe;
1061 struct dcerpc_binding_handle *net_handle;
1062 int i;
1064 mem_ctx = talloc_new(NULL);
1065 if (mem_ctx == NULL) {
1066 torture_comment(tctx, "talloc_new failed\n");
1067 return false;
1070 net_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
1071 if (net_pipe == NULL) {
1072 torture_comment(tctx, "dcerpc_pipe_init failed\n");
1073 goto done;
1075 net_handle = net_pipe->binding_handle;
1077 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1078 if (!NT_STATUS_IS_OK(status)) {
1079 torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
1080 nt_errstr(status));
1081 goto done;
1084 #if 0
1085 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1086 DCERPC_DEBUG_PRINT_OUT;
1087 #endif
1088 #if 1
1089 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1090 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1091 wks_creds, lpcfg_gensec_settings(tctx->lp_ctx, tctx->lp_ctx), DCERPC_AUTH_TYPE_SCHANNEL,
1092 DCERPC_AUTH_LEVEL_PRIVACY,
1093 NULL);
1094 #else
1095 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1096 #endif
1097 if (!NT_STATUS_IS_OK(status)) {
1098 torture_comment(tctx, "schannel bind failed: %s\n", nt_errstr(status));
1099 goto done;
1103 for (i=2; i<4; i++) {
1104 int flags;
1105 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1106 struct netlogon_creds_CredentialState *creds_state;
1107 struct netr_Authenticator netr_auth, netr_auth2;
1108 struct netr_NetworkInfo ninfo;
1109 struct netr_PasswordInfo pinfo;
1110 struct netr_LogonSamLogon r;
1111 union netr_LogonLevel logon;
1112 union netr_Validation validation;
1113 uint8_t authoritative;
1114 struct netr_Authenticator return_authenticator;
1116 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1117 CLI_CRED_NTLMv2_AUTH;
1119 chal = data_blob_talloc(mem_ctx, NULL, 8);
1120 if (chal.data == NULL) {
1121 torture_comment(tctx, "data_blob_talloc failed\n");
1122 goto done;
1125 generate_random_buffer(chal.data, chal.length);
1126 names_blob = NTLMv2_generate_names_blob(
1127 mem_ctx,
1128 cli_credentials_get_workstation(user_creds),
1129 cli_credentials_get_domain(user_creds));
1130 status = cli_credentials_get_ntlm_response(
1131 user_creds, mem_ctx, &flags, chal, names_blob,
1132 &lm_resp, &nt_resp, NULL, NULL);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 torture_comment(tctx, "cli_credentials_get_ntlm_response failed:"
1135 " %s\n", nt_errstr(status));
1136 goto done;
1139 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1140 netlogon_creds_client_authenticator(creds_state, &netr_auth);
1142 ninfo.identity_info.account_name.string =
1143 cli_credentials_get_username(user_creds);
1144 ninfo.identity_info.domain_name.string =
1145 cli_credentials_get_domain(user_creds);
1146 ninfo.identity_info.parameter_control = 0;
1147 ninfo.identity_info.logon_id_low = 0;
1148 ninfo.identity_info.logon_id_high = 0;
1149 ninfo.identity_info.workstation.string =
1150 cli_credentials_get_workstation(user_creds);
1151 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1152 ninfo.nt.length = nt_resp.length;
1153 ninfo.nt.data = nt_resp.data;
1154 ninfo.lm.length = lm_resp.length;
1155 ninfo.lm.data = lm_resp.data;
1157 logon.network = &ninfo;
1159 r.in.server_name = talloc_asprintf(
1160 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1161 ZERO_STRUCT(netr_auth2);
1162 r.in.computer_name =
1163 cli_credentials_get_workstation(wks_creds);
1164 r.in.credential = &netr_auth;
1165 r.in.return_authenticator = &netr_auth2;
1166 r.in.logon_level = 2;
1167 r.in.validation_level = i;
1168 r.in.logon = &logon;
1169 r.out.validation = &validation;
1170 r.out.authoritative = &authoritative;
1171 r.out.return_authenticator = &return_authenticator;
1173 status = dcerpc_netr_LogonSamLogon_r(net_handle, mem_ctx, &r);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
1176 nt_errstr(status));
1177 goto done;
1179 if (!NT_STATUS_IS_OK(r.out.result)) {
1180 torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
1181 nt_errstr(r.out.result));
1182 goto done;
1185 if ((r.out.return_authenticator == NULL) ||
1186 (!netlogon_creds_client_check(creds_state,
1187 &r.out.return_authenticator->cred))) {
1188 torture_comment(tctx, "Credentials check failed!\n");
1189 goto done;
1192 netlogon_creds_client_authenticator(creds_state, &netr_auth);
1194 pinfo.identity_info = ninfo.identity_info;
1195 ZERO_STRUCT(pinfo.lmpassword.hash);
1196 E_md4hash(cli_credentials_get_password(user_creds),
1197 pinfo.ntpassword.hash);
1198 session_key = data_blob_talloc(mem_ctx,
1199 creds_state->session_key, 16);
1200 arcfour_crypt_blob(pinfo.ntpassword.hash,
1201 sizeof(pinfo.ntpassword.hash),
1202 &session_key);
1204 logon.password = &pinfo;
1206 r.in.logon_level = 1;
1207 r.in.logon = &logon;
1208 r.out.return_authenticator = &return_authenticator;
1210 status = dcerpc_netr_LogonSamLogon_r(net_handle, mem_ctx, &r);
1211 if (!NT_STATUS_IS_OK(status)) {
1212 torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
1213 nt_errstr(status));
1214 goto done;
1216 if (!NT_STATUS_IS_OK(r.out.result)) {
1217 torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
1218 nt_errstr(r.out.result));
1219 goto done;
1222 if ((r.out.return_authenticator == NULL) ||
1223 (!netlogon_creds_client_check(creds_state,
1224 &r.out.return_authenticator->cred))) {
1225 torture_comment(tctx, "Credentials check failed!\n");
1226 goto done;
1231 struct netr_ServerPasswordSet s;
1232 char *password = generate_random_password(wks_creds, 8, 255);
1233 struct netlogon_creds_CredentialState *creds_state;
1234 struct netr_Authenticator credential, return_authenticator;
1235 struct samr_Password new_password;
1237 s.in.server_name = talloc_asprintf(
1238 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1239 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1240 s.in.account_name = talloc_asprintf(
1241 mem_ctx, "%s$", s.in.computer_name);
1242 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1243 s.in.credential = &credential;
1244 s.in.new_password = &new_password;
1245 s.out.return_authenticator = &return_authenticator;
1247 E_md4hash(password, new_password.hash);
1249 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1250 netlogon_creds_des_encrypt(creds_state, &new_password);
1251 netlogon_creds_client_authenticator(creds_state, &credential);
1253 status = dcerpc_netr_ServerPasswordSet_r(net_handle, mem_ctx, &s);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 torture_comment(tctx, "ServerPasswordSet - %s\n", nt_errstr(status));
1256 goto done;
1258 if (!NT_STATUS_IS_OK(s.out.result)) {
1259 torture_comment(tctx, "ServerPasswordSet - %s\n", nt_errstr(s.out.result));
1260 goto done;
1263 if (!netlogon_creds_client_check(creds_state,
1264 &s.out.return_authenticator->cred)) {
1265 torture_comment(tctx, "Credential chaining failed\n");
1268 cli_credentials_set_password(wks_creds, password,
1269 CRED_SPECIFIED);
1272 ret = true;
1273 done:
1274 talloc_free(mem_ctx);
1275 return ret;
1279 * Delete the wks account again
1282 static bool leave(struct torture_context *tctx,
1283 struct smbcli_state *cli,
1284 struct cli_credentials *admin_creds,
1285 struct cli_credentials *wks_creds)
1287 char *wks_name = talloc_asprintf(
1288 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1289 bool ret;
1291 ret = delete_user(tctx, cli, admin_creds, wks_name);
1292 talloc_free(wks_name);
1293 return ret;
1297 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1300 static bool torture_netlogon_samba3(struct torture_context *torture)
1302 NTSTATUS status;
1303 struct smbcli_state *cli;
1304 struct cli_credentials *anon_creds;
1305 struct cli_credentials *wks_creds;
1306 const char *wks_name;
1307 int i;
1308 struct smbcli_options options;
1309 struct smbcli_session_options session_options;
1311 wks_name = torture_setting_string(torture, "wksname", NULL);
1312 if (wks_name == NULL) {
1313 wks_name = get_myname(torture);
1316 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1317 torture_fail(torture, "create_anon_creds failed\n");
1320 lpcfg_smbcli_options(torture->lp_ctx, &options);
1321 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
1323 status = smbcli_full_connection(torture, &cli,
1324 torture_setting_string(torture, "host", NULL),
1325 lpcfg_smb_ports(torture->lp_ctx),
1326 "IPC$", NULL,
1327 lpcfg_socket_options(torture->lp_ctx),
1328 anon_creds,
1329 lpcfg_resolve_context(torture->lp_ctx),
1330 torture->ev, &options, &session_options,
1331 lpcfg_gensec_settings(torture, torture->lp_ctx));
1332 torture_assert_ntstatus_ok(torture, status, "smbcli_full_connection failed\n");
1334 wks_creds = cli_credentials_init(torture);
1335 if (wks_creds == NULL) {
1336 torture_fail(torture, "cli_credentials_init failed\n");
1339 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1340 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1341 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1342 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1343 cli_credentials_set_password(wks_creds,
1344 generate_random_password(wks_creds, 8, 255),
1345 CRED_SPECIFIED);
1347 torture_assert(torture,
1348 join3(torture, cli, false, cmdline_credentials, wks_creds),
1349 "join failed");
1351 cli_credentials_set_domain(
1352 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1353 CRED_SPECIFIED);
1355 for (i=0; i<2; i++) {
1357 /* Do this more than once, the routine "schan" changes
1358 * the workstation password using the netlogon
1359 * password change routine */
1361 int j;
1363 torture_assert(torture,
1364 auth2(torture, cli, wks_creds),
1365 "auth2 failed");
1367 for (j=0; j<2; j++) {
1368 torture_assert(torture,
1369 schan(torture, cli, wks_creds, cmdline_credentials),
1370 "schan failed");
1374 torture_assert(torture,
1375 leave(torture, cli, cmdline_credentials, wks_creds),
1376 "leave failed");
1378 return true;
1382 * Do a simple join, testjoin and leave using specified smb and samr
1383 * credentials
1386 static bool test_join3(struct torture_context *tctx,
1387 bool use_level25,
1388 struct cli_credentials *smb_creds,
1389 struct cli_credentials *samr_creds,
1390 const char *wks_name)
1392 NTSTATUS status;
1393 struct smbcli_state *cli;
1394 struct cli_credentials *wks_creds;
1395 struct smbcli_options options;
1396 struct smbcli_session_options session_options;
1398 lpcfg_smbcli_options(tctx->lp_ctx, &options);
1399 lpcfg_smbcli_session_options(tctx->lp_ctx, &session_options);
1401 status = smbcli_full_connection(tctx, &cli,
1402 torture_setting_string(tctx, "host", NULL),
1403 lpcfg_smb_ports(tctx->lp_ctx),
1404 "IPC$", NULL, lpcfg_socket_options(tctx->lp_ctx),
1405 smb_creds, lpcfg_resolve_context(tctx->lp_ctx),
1406 tctx->ev, &options, &session_options,
1407 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
1408 torture_assert_ntstatus_ok(tctx, status,
1409 "smbcli_full_connection failed");
1411 wks_creds = cli_credentials_init(cli);
1412 torture_assert(tctx, wks_creds, "cli_credentials_init failed");
1414 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1415 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1416 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1417 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1418 cli_credentials_set_password(wks_creds,
1419 generate_random_password(wks_creds, 8, 255),
1420 CRED_SPECIFIED);
1422 torture_assert(tctx,
1423 join3(tctx, cli, use_level25, samr_creds, wks_creds),
1424 "join failed");
1426 cli_credentials_set_domain(
1427 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1428 CRED_SPECIFIED);
1430 torture_assert(tctx,
1431 auth2(tctx, cli, wks_creds),
1432 "auth2 failed");
1434 torture_assert(tctx,
1435 leave(tctx, cli, samr_creds, wks_creds),
1436 "leave failed");
1438 talloc_free(cli);
1440 return true;
1444 * Test the different session key variants. Do it by joining, this uses the
1445 * session key in the setpassword routine. Test the join by doing the auth2.
1448 static bool torture_samba3_sessionkey(struct torture_context *torture)
1450 struct cli_credentials *anon_creds;
1451 const char *wks_name;
1453 wks_name = torture_setting_string(torture, "wksname", get_myname(torture));
1455 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1456 torture_fail(torture, "create_anon_creds failed\n");
1459 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1462 if (!torture_setting_bool(torture, "samba3", false)) {
1464 /* Samba3 in the build farm right now does this happily. Need
1465 * to fix :-) */
1467 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1468 torture_fail(torture, "join using anonymous bind on an anonymous smb "
1469 "connection succeeded -- HUH??\n");
1473 torture_assert(torture,
1474 test_join3(torture, false, anon_creds, cmdline_credentials, wks_name),
1475 "join using ntlmssp bind on an anonymous smb connection failed");
1477 torture_assert(torture,
1478 test_join3(torture, false, cmdline_credentials, NULL, wks_name),
1479 "join using anonymous bind on an authenticated smb connection failed");
1481 torture_assert(torture,
1482 test_join3(torture, false, cmdline_credentials, cmdline_credentials, wks_name),
1483 "join using ntlmssp bind on an authenticated smb connection failed");
1486 * The following two are tests for setuserinfolevel 25
1489 torture_assert(torture,
1490 test_join3(torture, true, anon_creds, cmdline_credentials, wks_name),
1491 "join using ntlmssp bind on an anonymous smb connection failed");
1493 torture_assert(torture,
1494 test_join3(torture, true, cmdline_credentials, NULL, wks_name),
1495 "join using anonymous bind on an authenticated smb connection failed");
1497 return true;
1501 * open pipe and bind, given an IPC$ context
1504 static NTSTATUS pipe_bind_smb(struct torture_context *tctx,
1505 TALLOC_CTX *mem_ctx,
1506 struct smbcli_tree *tree,
1507 const char *pipe_name,
1508 const struct ndr_interface_table *iface,
1509 struct dcerpc_pipe **p)
1511 struct dcerpc_pipe *result;
1512 NTSTATUS status;
1514 if (!(result = dcerpc_pipe_init(mem_ctx, tctx->ev))) {
1515 return NT_STATUS_NO_MEMORY;
1518 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1519 if (!NT_STATUS_IS_OK(status)) {
1520 torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
1521 nt_errstr(status));
1522 talloc_free(result);
1523 return status;
1526 status = dcerpc_bind_auth_none(result, iface);
1527 if (!NT_STATUS_IS_OK(status)) {
1528 torture_comment(tctx, "schannel bind failed: %s\n", nt_errstr(status));
1529 talloc_free(result);
1530 return status;
1533 *p = result;
1534 return NT_STATUS_OK;
1538 * Sane wrapper around lsa_LookupNames
1541 static struct dom_sid *name2sid(struct torture_context *tctx,
1542 TALLOC_CTX *mem_ctx,
1543 struct dcerpc_pipe *p,
1544 const char *name,
1545 const char *domain)
1547 struct lsa_ObjectAttribute attr;
1548 struct lsa_QosInfo qos;
1549 struct lsa_OpenPolicy2 r;
1550 struct lsa_Close c;
1551 NTSTATUS status;
1552 struct policy_handle handle;
1553 struct lsa_LookupNames l;
1554 struct lsa_TransSidArray sids;
1555 struct lsa_RefDomainList *domains = NULL;
1556 struct lsa_String lsa_name;
1557 uint32_t count = 0;
1558 struct dom_sid *result;
1559 TALLOC_CTX *tmp_ctx;
1560 struct dcerpc_binding_handle *b = p->binding_handle;
1562 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1563 return NULL;
1566 qos.len = 0;
1567 qos.impersonation_level = 2;
1568 qos.context_mode = 1;
1569 qos.effective_only = 0;
1571 attr.len = 0;
1572 attr.root_dir = NULL;
1573 attr.object_name = NULL;
1574 attr.attributes = 0;
1575 attr.sec_desc = NULL;
1576 attr.sec_qos = &qos;
1578 r.in.system_name = "\\";
1579 r.in.attr = &attr;
1580 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1581 r.out.handle = &handle;
1583 status = dcerpc_lsa_OpenPolicy2_r(b, tmp_ctx, &r);
1584 if (!NT_STATUS_IS_OK(status)) {
1585 torture_comment(tctx, "OpenPolicy2 failed - %s\n", nt_errstr(status));
1586 talloc_free(tmp_ctx);
1587 return NULL;
1589 if (!NT_STATUS_IS_OK(r.out.result)) {
1590 torture_comment(tctx, "OpenPolicy2 failed - %s\n", nt_errstr(r.out.result));
1591 talloc_free(tmp_ctx);
1592 return NULL;
1595 sids.count = 0;
1596 sids.sids = NULL;
1598 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1600 l.in.handle = &handle;
1601 l.in.num_names = 1;
1602 l.in.names = &lsa_name;
1603 l.in.sids = &sids;
1604 l.in.level = 1;
1605 l.in.count = &count;
1606 l.out.count = &count;
1607 l.out.sids = &sids;
1608 l.out.domains = &domains;
1610 status = dcerpc_lsa_LookupNames_r(b, tmp_ctx, &l);
1611 if (!NT_STATUS_IS_OK(status)) {
1612 torture_comment(tctx, "LookupNames of %s failed - %s\n", lsa_name.string,
1613 nt_errstr(status));
1614 talloc_free(tmp_ctx);
1615 return NULL;
1617 if (!NT_STATUS_IS_OK(l.out.result)) {
1618 torture_comment(tctx, "LookupNames of %s failed - %s\n", lsa_name.string,
1619 nt_errstr(l.out.result));
1620 talloc_free(tmp_ctx);
1621 return NULL;
1624 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1625 l.out.sids->sids[0].rid);
1627 c.in.handle = &handle;
1628 c.out.handle = &handle;
1630 status = dcerpc_lsa_Close_r(b, tmp_ctx, &c);
1631 if (!NT_STATUS_IS_OK(status)) {
1632 torture_comment(tctx, "dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1633 talloc_free(tmp_ctx);
1634 return NULL;
1636 if (!NT_STATUS_IS_OK(c.out.result)) {
1637 torture_comment(tctx, "dcerpc_lsa_Close failed - %s\n", nt_errstr(c.out.result));
1638 talloc_free(tmp_ctx);
1639 return NULL;
1642 talloc_free(tmp_ctx);
1643 return result;
1647 * Find out the user SID on this connection
1650 static struct dom_sid *whoami(struct torture_context *tctx,
1651 TALLOC_CTX *mem_ctx,
1652 struct smbcli_tree *tree)
1654 struct dcerpc_pipe *lsa;
1655 struct dcerpc_binding_handle *lsa_handle;
1656 struct lsa_GetUserName r;
1657 NTSTATUS status;
1658 struct lsa_String *authority_name_p = NULL;
1659 struct lsa_String *account_name_p = NULL;
1660 struct dom_sid *result;
1662 status = pipe_bind_smb(tctx, mem_ctx, tree, "\\pipe\\lsarpc",
1663 &ndr_table_lsarpc, &lsa);
1664 if (!NT_STATUS_IS_OK(status)) {
1665 torture_warning(tctx, "Could not bind to LSA: %s\n",
1666 nt_errstr(status));
1667 return NULL;
1669 lsa_handle = lsa->binding_handle;
1671 r.in.system_name = "\\";
1672 r.in.account_name = &account_name_p;
1673 r.in.authority_name = &authority_name_p;
1674 r.out.account_name = &account_name_p;
1676 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
1678 authority_name_p = *r.out.authority_name;
1680 if (!NT_STATUS_IS_OK(status)) {
1681 torture_warning(tctx, "GetUserName failed - %s\n",
1682 nt_errstr(status));
1683 talloc_free(lsa);
1684 return NULL;
1686 if (!NT_STATUS_IS_OK(r.out.result)) {
1687 torture_warning(tctx, "GetUserName failed - %s\n",
1688 nt_errstr(r.out.result));
1689 talloc_free(lsa);
1690 return NULL;
1693 result = name2sid(tctx, mem_ctx, lsa, account_name_p->string,
1694 authority_name_p->string);
1696 talloc_free(lsa);
1697 return result;
1700 static int destroy_tree(struct smbcli_tree *tree)
1702 smb_tree_disconnect(tree);
1703 return 0;
1707 * Do a tcon, given a session
1710 static NTSTATUS secondary_tcon(struct torture_context *tctx,
1711 TALLOC_CTX *mem_ctx,
1712 struct smbcli_session *session,
1713 const char *sharename,
1714 struct smbcli_tree **res)
1716 struct smbcli_tree *result;
1717 TALLOC_CTX *tmp_ctx;
1718 union smb_tcon tcon;
1719 NTSTATUS status;
1721 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1722 return NT_STATUS_NO_MEMORY;
1725 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1726 talloc_free(tmp_ctx);
1727 return NT_STATUS_NO_MEMORY;
1730 tcon.generic.level = RAW_TCON_TCONX;
1731 tcon.tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
1732 tcon.tconx.in.flags |= TCONX_FLAG_EXTENDED_SIGNATURES;
1733 tcon.tconx.in.password = data_blob(NULL, 0);
1734 tcon.tconx.in.path = sharename;
1735 tcon.tconx.in.device = "?????";
1737 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1738 if (!NT_STATUS_IS_OK(status)) {
1739 torture_warning(tctx, "smb_raw_tcon failed: %s\n",
1740 nt_errstr(status));
1741 talloc_free(tmp_ctx);
1742 return status;
1745 result->tid = tcon.tconx.out.tid;
1747 if (tcon.tconx.out.options & SMB_EXTENDED_SIGNATURES) {
1748 smb1cli_session_protect_session_key(result->session->smbXcli);
1751 result = talloc_steal(mem_ctx, result);
1752 talloc_set_destructor(result, destroy_tree);
1753 talloc_free(tmp_ctx);
1754 *res = result;
1755 return NT_STATUS_OK;
1759 * Test the getusername behaviour
1762 static bool torture_samba3_rpc_getusername(struct torture_context *torture)
1764 NTSTATUS status;
1765 struct smbcli_state *cli;
1766 bool ret = true;
1767 struct dom_sid *user_sid;
1768 struct dom_sid *created_sid;
1769 struct cli_credentials *anon_creds;
1770 struct cli_credentials *user_creds;
1771 char *domain_name;
1772 struct smbcli_options options;
1773 struct smbcli_session_options session_options;
1775 lpcfg_smbcli_options(torture->lp_ctx, &options);
1776 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
1778 status = smbcli_full_connection(
1779 torture, &cli, torture_setting_string(torture, "host", NULL),
1780 lpcfg_smb_ports(torture->lp_ctx),
1781 "IPC$", NULL, lpcfg_socket_options(torture->lp_ctx), cmdline_credentials,
1782 lpcfg_resolve_context(torture->lp_ctx), torture->ev, &options,
1783 &session_options, lpcfg_gensec_settings(torture, torture->lp_ctx));
1784 torture_assert_ntstatus_ok(torture, status, "smbcli_full_connection failed\n");
1786 if (!(user_sid = whoami(torture, torture, cli->tree))) {
1787 torture_fail(torture, "whoami on auth'ed connection failed\n");
1790 talloc_free(cli);
1792 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1793 torture_fail(torture, "create_anon_creds failed\n");
1796 status = smbcli_full_connection(
1797 torture, &cli, torture_setting_string(torture, "host", NULL),
1798 lpcfg_smb_ports(torture->lp_ctx), "IPC$", NULL,
1799 lpcfg_socket_options(torture->lp_ctx), anon_creds,
1800 lpcfg_resolve_context(torture->lp_ctx),
1801 torture->ev, &options, &session_options,
1802 lpcfg_gensec_settings(torture, torture->lp_ctx));
1803 torture_assert_ntstatus_ok(torture, status, "anon smbcli_full_connection failed\n");
1805 if (!(user_sid = whoami(torture, torture, cli->tree))) {
1806 torture_fail(torture, "whoami on anon connection failed\n");
1809 torture_assert_sid_equal(torture, user_sid, dom_sid_parse_talloc(torture, "s-1-5-7"),
1810 "Anon lsa_GetUserName returned unexpected SID");
1812 if (!(user_creds = cli_credentials_init(torture))) {
1813 torture_fail(torture, "cli_credentials_init failed\n");
1816 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1817 cli_credentials_set_username(user_creds, "torture_username",
1818 CRED_SPECIFIED);
1819 cli_credentials_set_password(user_creds,
1820 generate_random_password(user_creds, 8, 255),
1821 CRED_SPECIFIED);
1823 if (!create_user(torture, torture, cli, cmdline_credentials,
1824 cli_credentials_get_username(user_creds),
1825 cli_credentials_get_password(user_creds),
1826 &domain_name, &created_sid)) {
1827 torture_fail(torture, "create_user failed\n");
1830 cli_credentials_set_domain(user_creds, domain_name,
1831 CRED_SPECIFIED);
1834 struct smbcli_session *session2;
1835 struct smb_composite_sesssetup setup;
1836 struct smbcli_tree *tree;
1838 session2 = smbcli_session_init(cli->transport, torture, false, session_options);
1839 if (session2 == NULL) {
1840 torture_fail(torture, "smbcli_session_init failed\n");
1843 setup.in.sesskey = cli->transport->negotiate.sesskey;
1844 setup.in.capabilities = cli->transport->negotiate.capabilities;
1845 setup.in.workgroup = "";
1846 setup.in.credentials = user_creds;
1847 setup.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
1849 status = smb_composite_sesssetup(session2, &setup);
1850 torture_assert_ntstatus_ok(torture, status, "session setup with new user failed");
1852 session2->vuid = setup.out.vuid;
1854 if (!NT_STATUS_IS_OK(secondary_tcon(torture, torture, session2,
1855 "IPC$", &tree))) {
1856 torture_fail(torture, "secondary_tcon failed\n");
1859 if (!(user_sid = whoami(torture, torture, tree))) {
1860 torture_fail_goto(torture, del, "whoami on user connection failed\n");
1861 ret = false;
1862 goto del;
1865 talloc_free(tree);
1868 torture_comment(torture, "Created %s, found %s\n",
1869 dom_sid_string(torture, created_sid),
1870 dom_sid_string(torture, user_sid));
1872 if (!dom_sid_equal(created_sid, user_sid)) {
1873 ret = false;
1876 del:
1877 if (!delete_user(torture, cli,
1878 cmdline_credentials,
1879 cli_credentials_get_username(user_creds))) {
1880 torture_fail(torture, "delete_user failed\n");
1883 return ret;
1886 static bool test_NetShareGetInfo(struct torture_context *tctx,
1887 struct dcerpc_pipe *p,
1888 const char *sharename)
1890 NTSTATUS status;
1891 struct srvsvc_NetShareGetInfo r;
1892 union srvsvc_NetShareInfo info;
1893 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1894 int i;
1895 bool ret = true;
1896 struct dcerpc_binding_handle *b = p->binding_handle;
1898 r.in.server_unc = talloc_asprintf(tctx, "\\\\%s",
1899 dcerpc_server_name(p));
1900 r.in.share_name = sharename;
1901 r.out.info = &info;
1903 for (i=0;i<ARRAY_SIZE(levels);i++) {
1904 r.in.level = levels[i];
1906 torture_comment(tctx, "Testing NetShareGetInfo level %u on share '%s'\n",
1907 r.in.level, r.in.share_name);
1909 status = dcerpc_srvsvc_NetShareGetInfo_r(b, tctx, &r);
1910 if (!NT_STATUS_IS_OK(status)) {
1911 torture_warning(tctx, "NetShareGetInfo level %u on share '%s' failed"
1912 " - %s\n", r.in.level, r.in.share_name,
1913 nt_errstr(status));
1914 ret = false;
1915 continue;
1917 if (!W_ERROR_IS_OK(r.out.result)) {
1918 torture_warning(tctx, "NetShareGetInfo level %u on share '%s' failed "
1919 "- %s\n", r.in.level, r.in.share_name,
1920 win_errstr(r.out.result));
1921 ret = false;
1922 continue;
1926 return ret;
1929 static bool test_NetShareEnum(struct torture_context *tctx,
1930 struct dcerpc_pipe *p,
1931 const char **one_sharename)
1933 NTSTATUS status;
1934 struct srvsvc_NetShareEnum r;
1935 struct srvsvc_NetShareInfoCtr info_ctr;
1936 struct srvsvc_NetShareCtr0 c0;
1937 struct srvsvc_NetShareCtr1 c1;
1938 struct srvsvc_NetShareCtr2 c2;
1939 struct srvsvc_NetShareCtr501 c501;
1940 struct srvsvc_NetShareCtr502 c502;
1941 struct srvsvc_NetShareCtr1004 c1004;
1942 struct srvsvc_NetShareCtr1005 c1005;
1943 struct srvsvc_NetShareCtr1006 c1006;
1944 struct srvsvc_NetShareCtr1007 c1007;
1945 uint32_t totalentries = 0;
1946 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1947 int i;
1948 bool ret = true;
1949 struct dcerpc_binding_handle *b = p->binding_handle;
1951 ZERO_STRUCT(info_ctr);
1953 r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
1954 r.in.info_ctr = &info_ctr;
1955 r.in.max_buffer = (uint32_t)-1;
1956 r.in.resume_handle = NULL;
1957 r.out.totalentries = &totalentries;
1958 r.out.info_ctr = &info_ctr;
1960 for (i=0;i<ARRAY_SIZE(levels);i++) {
1961 info_ctr.level = levels[i];
1963 switch (info_ctr.level) {
1964 case 0:
1965 ZERO_STRUCT(c0);
1966 info_ctr.ctr.ctr0 = &c0;
1967 break;
1968 case 1:
1969 ZERO_STRUCT(c1);
1970 info_ctr.ctr.ctr1 = &c1;
1971 break;
1972 case 2:
1973 ZERO_STRUCT(c2);
1974 info_ctr.ctr.ctr2 = &c2;
1975 break;
1976 case 501:
1977 ZERO_STRUCT(c501);
1978 info_ctr.ctr.ctr501 = &c501;
1979 break;
1980 case 502:
1981 ZERO_STRUCT(c502);
1982 info_ctr.ctr.ctr502 = &c502;
1983 break;
1984 case 1004:
1985 ZERO_STRUCT(c1004);
1986 info_ctr.ctr.ctr1004 = &c1004;
1987 break;
1988 case 1005:
1989 ZERO_STRUCT(c1005);
1990 info_ctr.ctr.ctr1005 = &c1005;
1991 break;
1992 case 1006:
1993 ZERO_STRUCT(c1006);
1994 info_ctr.ctr.ctr1006 = &c1006;
1995 break;
1996 case 1007:
1997 ZERO_STRUCT(c1007);
1998 info_ctr.ctr.ctr1007 = &c1007;
1999 break;
2002 torture_comment(tctx, "Testing NetShareEnum level %u\n", info_ctr.level);
2004 status = dcerpc_srvsvc_NetShareEnum_r(b, tctx, &r);
2005 if (!NT_STATUS_IS_OK(status)) {
2006 torture_warning(tctx, "NetShareEnum level %u failed - %s\n",
2007 info_ctr.level, nt_errstr(status));
2008 ret = false;
2009 continue;
2011 if (!W_ERROR_IS_OK(r.out.result)) {
2012 torture_warning(tctx, "NetShareEnum level %u failed - %s\n",
2013 info_ctr.level, win_errstr(r.out.result));
2014 continue;
2016 if (info_ctr.level == 0) {
2017 struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
2018 if (ctr->count > 0) {
2019 *one_sharename = ctr->array[0].name;
2024 return ret;
2027 static bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
2029 struct dcerpc_pipe *p;
2030 const char *sharename = NULL;
2031 bool ret = true;
2033 torture_assert_ntstatus_ok(torture,
2034 torture_rpc_connection(torture, &p, &ndr_table_srvsvc),
2035 "failed to open srvsvc");
2037 ret &= test_NetShareEnum(torture, p, &sharename);
2038 if (sharename == NULL) {
2039 torture_comment(torture, "did not get sharename\n");
2040 } else {
2041 ret &= test_NetShareGetInfo(torture, p, sharename);
2044 return ret;
2048 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
2049 * NT_STATUS_NO_SAM_ACCOUNT
2052 static bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
2054 TALLOC_CTX *mem_ctx;
2055 struct dcerpc_pipe *net_pipe;
2056 struct dcerpc_binding_handle *net_handle;
2057 char *wksname;
2058 bool result = false;
2059 NTSTATUS status;
2060 struct netr_ServerReqChallenge r;
2061 struct netr_Credential netr_cli_creds;
2062 struct netr_Credential netr_srv_creds;
2063 uint32_t negotiate_flags;
2064 struct netr_ServerAuthenticate2 a;
2065 struct netlogon_creds_CredentialState *creds_state;
2066 struct netr_Credential netr_cred;
2067 struct samr_Password mach_pw;
2068 struct smbcli_state *cli;
2070 if (!(mem_ctx = talloc_new(torture))) {
2071 torture_comment(torture, "talloc_new failed\n");
2072 return false;
2075 if (!(wksname = generate_random_str_list(
2076 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2077 torture_comment(torture, "generate_random_str_list failed\n");
2078 goto done;
2081 if (!(torture_open_connection_share(
2082 mem_ctx, &cli,
2083 torture, torture_setting_string(torture, "host", NULL),
2084 "IPC$", torture->ev))) {
2085 torture_comment(torture, "IPC$ connection failed\n");
2086 goto done;
2089 if (!(net_pipe = dcerpc_pipe_init(mem_ctx, torture->ev))) {
2090 torture_comment(torture, "dcerpc_pipe_init failed\n");
2091 goto done;
2093 net_handle = net_pipe->binding_handle;
2095 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2096 if (!NT_STATUS_IS_OK(status)) {
2097 torture_comment(torture, "dcerpc_pipe_open_smb failed: %s\n",
2098 nt_errstr(status));
2099 goto done;
2102 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2103 if (!NT_STATUS_IS_OK(status)) {
2104 torture_comment(torture, "dcerpc_bind_auth_none failed: %s\n",
2105 nt_errstr(status));
2106 goto done;
2109 r.in.computer_name = wksname;
2110 r.in.server_name = talloc_asprintf(
2111 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2112 if (r.in.server_name == NULL) {
2113 torture_comment(torture, "talloc_asprintf failed\n");
2114 goto done;
2116 generate_random_buffer(netr_cli_creds.data,
2117 sizeof(netr_cli_creds.data));
2118 r.in.credentials = &netr_cli_creds;
2119 r.out.return_credentials = &netr_srv_creds;
2121 status = dcerpc_netr_ServerReqChallenge_r(net_handle, mem_ctx, &r);
2122 if (!NT_STATUS_IS_OK(status)) {
2123 torture_comment(torture, "netr_ServerReqChallenge failed: %s\n",
2124 nt_errstr(status));
2125 goto done;
2127 if (!NT_STATUS_IS_OK(r.out.result)) {
2128 torture_comment(torture, "netr_ServerReqChallenge failed: %s\n",
2129 nt_errstr(r.out.result));
2130 goto done;
2133 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2134 E_md4hash("foobar", mach_pw.hash);
2136 a.in.server_name = talloc_asprintf(
2137 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2138 a.in.account_name = talloc_asprintf(
2139 mem_ctx, "%s$", wksname);
2140 a.in.computer_name = wksname;
2141 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2142 a.in.negotiate_flags = &negotiate_flags;
2143 a.out.negotiate_flags = &negotiate_flags;
2144 a.in.credentials = &netr_cred;
2145 a.out.return_credentials = &netr_cred;
2147 creds_state = netlogon_creds_client_init(mem_ctx,
2148 a.in.account_name,
2149 a.in.computer_name,
2150 r.in.credentials,
2151 r.out.return_credentials, &mach_pw,
2152 &netr_cred, negotiate_flags);
2153 torture_assert(torture, (creds_state != NULL), "memory allocation failed");
2155 status = dcerpc_netr_ServerAuthenticate2_r(net_handle, mem_ctx, &a);
2156 if (!NT_STATUS_IS_OK(status)) {
2157 goto done;
2159 if (!NT_STATUS_EQUAL(a.out.result, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2160 torture_comment(torture, "dcerpc_netr_ServerAuthenticate2 returned %s, "
2161 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2162 nt_errstr(a.out.result));
2163 goto done;
2166 result = true;
2167 done:
2168 talloc_free(mem_ctx);
2169 return result;
2172 static struct security_descriptor *get_sharesec(struct torture_context *tctx,
2173 TALLOC_CTX *mem_ctx,
2174 struct smbcli_session *sess,
2175 const char *sharename)
2177 struct smbcli_tree *tree;
2178 TALLOC_CTX *tmp_ctx;
2179 struct dcerpc_pipe *p;
2180 struct dcerpc_binding_handle *b;
2181 NTSTATUS status;
2182 struct srvsvc_NetShareGetInfo r;
2183 union srvsvc_NetShareInfo info;
2184 struct security_descriptor *result;
2186 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2187 torture_comment(tctx, "talloc_new failed\n");
2188 return NULL;
2191 if (!NT_STATUS_IS_OK(secondary_tcon(tctx, tmp_ctx, sess, "IPC$", &tree))) {
2192 torture_comment(tctx, "secondary_tcon failed\n");
2193 talloc_free(tmp_ctx);
2194 return NULL;
2197 status = pipe_bind_smb(tctx, mem_ctx, tree, "\\pipe\\srvsvc",
2198 &ndr_table_srvsvc, &p);
2199 if (!NT_STATUS_IS_OK(status)) {
2200 torture_warning(tctx, "could not bind to srvsvc pipe: %s\n",
2201 nt_errstr(status));
2202 talloc_free(tmp_ctx);
2203 return NULL;
2205 b = p->binding_handle;
2207 #if 0
2208 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2209 #endif
2211 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2212 dcerpc_server_name(p));
2213 r.in.share_name = sharename;
2214 r.in.level = 502;
2215 r.out.info = &info;
2217 status = dcerpc_srvsvc_NetShareGetInfo_r(b, tmp_ctx, &r);
2218 if (!NT_STATUS_IS_OK(status)) {
2219 torture_comment(tctx, "srvsvc_NetShareGetInfo failed: %s\n",
2220 nt_errstr(status));
2221 talloc_free(tmp_ctx);
2222 return NULL;
2224 if (!W_ERROR_IS_OK(r.out.result)) {
2225 torture_comment(tctx, "srvsvc_NetShareGetInfo failed: %s\n",
2226 win_errstr(r.out.result));
2227 talloc_free(tmp_ctx);
2228 return NULL;
2231 result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
2232 talloc_free(tmp_ctx);
2233 return result;
2236 static NTSTATUS set_sharesec(struct torture_context *tctx,
2237 TALLOC_CTX *mem_ctx,
2238 struct smbcli_session *sess,
2239 const char *sharename,
2240 struct security_descriptor *sd)
2242 struct smbcli_tree *tree;
2243 TALLOC_CTX *tmp_ctx;
2244 struct dcerpc_pipe *p;
2245 struct dcerpc_binding_handle *b;
2246 NTSTATUS status;
2247 struct sec_desc_buf i;
2248 struct srvsvc_NetShareSetInfo r;
2249 union srvsvc_NetShareInfo info;
2250 uint32_t error = 0;
2252 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2253 torture_comment(tctx, "talloc_new failed\n");
2254 return NT_STATUS_NO_MEMORY;
2257 if (!NT_STATUS_IS_OK(secondary_tcon(tctx, tmp_ctx, sess, "IPC$", &tree))) {
2258 torture_comment(tctx, "secondary_tcon failed\n");
2259 talloc_free(tmp_ctx);
2260 return NT_STATUS_UNSUCCESSFUL;
2263 status = pipe_bind_smb(tctx, mem_ctx, tree, "\\pipe\\srvsvc",
2264 &ndr_table_srvsvc, &p);
2265 if (!NT_STATUS_IS_OK(status)) {
2266 torture_warning(tctx, "could not bind to srvsvc pipe: %s\n",
2267 nt_errstr(status));
2268 talloc_free(tmp_ctx);
2269 return NT_STATUS_UNSUCCESSFUL;
2271 b = p->binding_handle;
2273 #if 0
2274 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2275 #endif
2277 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2278 dcerpc_server_name(p));
2279 r.in.share_name = sharename;
2280 r.in.level = 1501;
2281 i.sd = sd;
2282 info.info1501 = &i;
2283 r.in.info = &info;
2284 r.in.parm_error = &error;
2286 status = dcerpc_srvsvc_NetShareSetInfo_r(b, tmp_ctx, &r);
2287 if (!NT_STATUS_IS_OK(status)) {
2288 torture_comment(tctx, "srvsvc_NetShareSetInfo failed: %s\n",
2289 nt_errstr(status));
2291 if (!W_ERROR_IS_OK(r.out.result)) {
2292 torture_comment(tctx, "srvsvc_NetShareSetInfo failed: %s\n",
2293 win_errstr(r.out.result));
2294 status = werror_to_ntstatus(r.out.result);
2296 talloc_free(tmp_ctx);
2297 return status;
2300 bool try_tcon(struct torture_context *tctx,
2301 TALLOC_CTX *mem_ctx,
2302 struct security_descriptor *orig_sd,
2303 struct smbcli_session *session,
2304 const char *sharename, const struct dom_sid *user_sid,
2305 unsigned int access_mask, NTSTATUS expected_tcon,
2306 NTSTATUS expected_mkdir)
2308 TALLOC_CTX *tmp_ctx;
2309 struct smbcli_tree *rmdir_tree, *tree;
2310 struct dom_sid *domain_sid;
2311 uint32_t rid;
2312 struct security_descriptor *sd;
2313 NTSTATUS status;
2314 bool ret = true;
2316 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2317 torture_comment(tctx, "talloc_new failed\n");
2318 return false;
2321 status = secondary_tcon(tctx, tmp_ctx, session, sharename, &rmdir_tree);
2322 if (!NT_STATUS_IS_OK(status)) {
2323 torture_comment(tctx, "first tcon to delete dir failed\n");
2324 talloc_free(tmp_ctx);
2325 return false;
2328 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2330 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2331 &domain_sid, &rid))) {
2332 torture_comment(tctx, "dom_sid_split_rid failed\n");
2333 talloc_free(tmp_ctx);
2334 return false;
2337 sd = security_descriptor_dacl_create(
2338 tmp_ctx, 0, "S-1-5-32-544",
2339 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2340 DOMAIN_RID_USERS)),
2341 dom_sid_string(mem_ctx, user_sid),
2342 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2343 if (sd == NULL) {
2344 torture_comment(tctx, "security_descriptor_dacl_create failed\n");
2345 talloc_free(tmp_ctx);
2346 return false;
2349 status = set_sharesec(tctx, mem_ctx, session, sharename, sd);
2350 if (!NT_STATUS_IS_OK(status)) {
2351 torture_comment(tctx, "custom set_sharesec failed: %s\n",
2352 nt_errstr(status));
2353 talloc_free(tmp_ctx);
2354 return false;
2357 status = secondary_tcon(tctx, tmp_ctx, session, sharename, &tree);
2358 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2359 torture_comment(tctx, "Expected %s, got %s\n", nt_errstr(expected_tcon),
2360 nt_errstr(status));
2361 ret = false;
2362 goto done;
2365 if (!NT_STATUS_IS_OK(status)) {
2366 /* An expected non-access, no point in trying to write */
2367 goto done;
2370 status = smbcli_mkdir(tree, "sharesec_testdir");
2371 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2372 torture_warning(tctx, "Expected %s, got %s\n",
2373 nt_errstr(expected_mkdir), nt_errstr(status));
2374 ret = false;
2377 done:
2378 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2380 status = set_sharesec(tctx, mem_ctx, session, sharename, orig_sd);
2381 if (!NT_STATUS_IS_OK(status)) {
2382 torture_comment(tctx, "custom set_sharesec failed: %s\n",
2383 nt_errstr(status));
2384 talloc_free(tmp_ctx);
2385 return false;
2388 talloc_free(tmp_ctx);
2389 return ret;
2392 static bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2394 struct smbcli_state *cli = NULL;
2395 struct security_descriptor *sd = NULL;
2396 struct dom_sid *user_sid = NULL;
2397 const char *testuser_passwd = NULL;
2398 struct cli_credentials *test_credentials = NULL;
2399 struct smbcli_options options;
2400 struct smbcli_session_options session_options;
2401 NTSTATUS status;
2402 struct test_join *tj = NULL;
2403 struct dcerpc_pipe *lsa_pipe = NULL;
2404 const char *priv_array[1];
2406 /* Create a new user. The normal user has SeBackup and SeRestore
2407 privs so we can't lock them out with a share security descriptor. */
2408 tj = torture_create_testuser(torture,
2409 "sharesec_user",
2410 torture_setting_string(torture, "workgroup", NULL),
2411 ACB_NORMAL,
2412 &testuser_passwd);
2413 if (!tj) {
2414 torture_fail(torture, "Creating sharesec_user failed\n");
2417 /* Give them SeDiskOperatorPrivilege but no other privs. */
2418 status = torture_rpc_connection(torture, &lsa_pipe, &ndr_table_lsarpc);
2419 if (!NT_STATUS_IS_OK(status)) {
2420 torture_delete_testuser(torture, tj, "sharesec_user");
2421 talloc_free(tj);
2422 torture_fail(torture, "Error connecting to LSA pipe");
2425 priv_array[0] = "SeDiskOperatorPrivilege";
2426 if (!torture_setup_privs(torture,
2427 lsa_pipe,
2429 priv_array,
2430 torture_join_user_sid(tj))) {
2431 talloc_free(lsa_pipe);
2432 torture_delete_testuser(torture, tj, "sharesec_user");
2433 talloc_free(tj);
2434 torture_fail(torture, "Failed to setup privs\n");
2436 talloc_free(lsa_pipe);
2438 test_credentials = cli_credentials_init(torture);
2439 cli_credentials_set_workstation(test_credentials, "localhost", CRED_SPECIFIED);
2440 cli_credentials_set_domain(test_credentials, lpcfg_workgroup(torture->lp_ctx),
2441 CRED_SPECIFIED);
2442 cli_credentials_set_username(test_credentials, "sharesec_user", CRED_SPECIFIED);
2443 cli_credentials_set_password(test_credentials, testuser_passwd, CRED_SPECIFIED);
2445 ZERO_STRUCT(options);
2446 ZERO_STRUCT(session_options);
2447 lpcfg_smbcli_options(torture->lp_ctx, &options);
2448 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
2450 status = smbcli_full_connection(torture,
2451 &cli,
2452 torture_setting_string(torture, "host", NULL),
2453 lpcfg_smb_ports(torture->lp_ctx),
2454 "IPC$",
2455 NULL,
2456 lpcfg_socket_options(torture->lp_ctx),
2457 test_credentials,
2458 lpcfg_resolve_context(torture->lp_ctx),
2459 torture->ev,
2460 &options,
2461 &session_options,
2462 lpcfg_gensec_settings(torture, torture->lp_ctx));
2463 if (!NT_STATUS_IS_OK(status)) {
2464 talloc_free(cli);
2465 torture_delete_testuser(torture, tj, "sharesec_user");
2466 talloc_free(tj);
2467 torture_fail(torture, "Failed to open connection\n");
2470 if (!(user_sid = whoami(torture, torture, cli->tree))) {
2471 talloc_free(cli);
2472 torture_delete_testuser(torture, tj, "sharesec_user");
2473 talloc_free(tj);
2474 torture_fail(torture, "whoami failed\n");
2477 sd = get_sharesec(torture, torture, cli->session,
2478 torture_setting_string(torture, "share", NULL));
2480 if (!try_tcon(torture, torture, sd, cli->session,
2481 torture_setting_string(torture, "share", NULL),
2482 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK)) {
2483 talloc_free(cli);
2484 torture_delete_testuser(torture, tj, "sharesec_user");
2485 talloc_free(tj);
2486 torture_fail(torture, "failed to test tcon with 0 access_mask");
2489 if (!try_tcon(torture, torture, sd, cli->session,
2490 torture_setting_string(torture, "share", NULL),
2491 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2492 NT_STATUS_MEDIA_WRITE_PROTECTED)) {
2493 talloc_free(cli);
2494 torture_delete_testuser(torture, tj, "sharesec_user");
2495 talloc_free(tj);
2496 torture_fail(torture, "failed to test tcon with SEC_FILE_READ_DATA access_mask");
2499 /* sharesec_user doesn't have any rights on the underlying file system.
2500 Go back to the normal user. */
2502 talloc_free(cli);
2503 cli = NULL;
2504 torture_delete_testuser(torture, tj, "sharesec_user");
2505 talloc_free(tj);
2506 tj = NULL;
2508 if (!(torture_open_connection_share(
2509 torture, &cli, torture, torture_setting_string(torture, "host", NULL),
2510 "IPC$", torture->ev))) {
2511 torture_fail(torture, "IPC$ connection failed\n");
2514 if (!(user_sid = whoami(torture, torture, cli->tree))) {
2515 torture_fail(torture, "whoami failed\n");
2517 torture_assert(torture, try_tcon(
2518 torture, torture, sd, cli->session,
2519 torture_setting_string(torture, "share", NULL),
2520 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK),
2521 "failed to test tcon with SEC_FILE_ALL access_mask")
2523 return true;
2526 static bool torture_samba3_rpc_lsa(struct torture_context *torture)
2528 struct dcerpc_pipe *p;
2529 struct dcerpc_binding_handle *b;
2530 struct policy_handle lsa_handle;
2532 torture_assert_ntstatus_ok(torture,
2533 torture_rpc_connection(torture, &p, &ndr_table_lsarpc),
2534 "failed to setup lsarpc");
2536 b = p->binding_handle;
2539 struct lsa_ObjectAttribute attr;
2540 struct lsa_OpenPolicy2 o;
2541 o.in.system_name = talloc_asprintf(
2542 torture, "\\\\%s", dcerpc_server_name(p));
2543 ZERO_STRUCT(attr);
2544 o.in.attr = &attr;
2545 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2546 o.out.handle = &lsa_handle;
2548 torture_assert_ntstatus_ok(torture,
2549 dcerpc_lsa_OpenPolicy2_r(b, torture, &o),
2550 "dcerpc_lsa_OpenPolicy2 failed");
2551 torture_assert_ntstatus_ok(torture, o.out.result,
2552 "dcerpc_lsa_OpenPolicy2 failed");
2556 int i;
2557 int levels[] = { 2,3,5,6 };
2559 for (i=0; i<ARRAY_SIZE(levels); i++) {
2560 struct lsa_QueryInfoPolicy r;
2561 union lsa_PolicyInformation *info = NULL;
2562 r.in.handle = &lsa_handle;
2563 r.in.level = levels[i];
2564 r.out.info = &info;
2566 torture_assert_ntstatus_ok(torture,
2567 dcerpc_lsa_QueryInfoPolicy_r(b, torture, &r),
2568 talloc_asprintf(torture, "dcerpc_lsa_QueryInfoPolicy level %d failed", levels[i]));
2569 torture_assert_ntstatus_ok(torture, r.out.result,
2570 talloc_asprintf(torture, "dcerpc_lsa_QueryInfoPolicy level %d failed", levels[i]));
2574 return true;
2577 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2578 char **name)
2580 struct rap_WserverGetInfo r;
2581 NTSTATUS status;
2582 char servername[17];
2583 size_t converted_size;
2585 r.in.level = 0;
2586 r.in.bufsize = 0xffff;
2588 status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
2589 if (!NT_STATUS_IS_OK(status)) {
2590 return status;
2593 memcpy(servername, r.out.info.info0.name, 16);
2594 servername[16] = '\0';
2596 if (!pull_ascii_talloc(mem_ctx, name, servername, &converted_size)) {
2597 return NT_STATUS_NO_MEMORY;
2600 return NT_STATUS_OK;
2603 static bool rap_get_servername(struct torture_context *tctx,
2604 char **servername)
2606 struct smbcli_state *cli;
2608 torture_assert(tctx,
2609 torture_open_connection_share(tctx, &cli, tctx, torture_setting_string(tctx, "host", NULL),
2610 "IPC$", tctx->ev),
2611 "IPC$ connection failed");
2613 torture_assert_ntstatus_ok(tctx,
2614 get_servername(tctx, cli->tree, servername),
2615 "get_servername failed");
2617 talloc_free(cli);
2619 return true;
2622 static bool find_printers(struct torture_context *tctx,
2623 struct dcerpc_pipe *p,
2624 const char ***printers,
2625 int *num_printers)
2627 struct srvsvc_NetShareEnum r;
2628 struct srvsvc_NetShareInfoCtr info_ctr;
2629 struct srvsvc_NetShareCtr1 c1_in;
2630 struct srvsvc_NetShareCtr1 *c1;
2631 uint32_t totalentries = 0;
2632 int i;
2633 struct dcerpc_binding_handle *b = p->binding_handle;
2635 ZERO_STRUCT(c1_in);
2636 info_ctr.level = 1;
2637 info_ctr.ctr.ctr1 = &c1_in;
2639 r.in.server_unc = talloc_asprintf(
2640 tctx, "\\\\%s", dcerpc_server_name(p));
2641 r.in.info_ctr = &info_ctr;
2642 r.in.max_buffer = (uint32_t)-1;
2643 r.in.resume_handle = NULL;
2644 r.out.totalentries = &totalentries;
2645 r.out.info_ctr = &info_ctr;
2647 torture_assert_ntstatus_ok(tctx,
2648 dcerpc_srvsvc_NetShareEnum_r(b, tctx, &r),
2649 "NetShareEnum level 1 failed");
2650 torture_assert_werr_ok(tctx, r.out.result,
2651 "NetShareEnum level 1 failed");
2653 *printers = NULL;
2654 *num_printers = 0;
2655 c1 = r.out.info_ctr->ctr.ctr1;
2656 for (i=0; i<c1->count; i++) {
2657 if (c1->array[i].type != STYPE_PRINTQ) {
2658 continue;
2660 if (!add_string_to_array(tctx, c1->array[i].name,
2661 printers, num_printers)) {
2662 return false;
2666 return true;
2669 static bool enumprinters(struct torture_context *tctx,
2670 struct dcerpc_binding_handle *b,
2671 const char *servername, int level, int *num_printers)
2673 struct spoolss_EnumPrinters r;
2674 DATA_BLOB blob;
2675 uint32_t needed;
2676 uint32_t count;
2677 union spoolss_PrinterInfo *info;
2679 r.in.flags = PRINTER_ENUM_LOCAL;
2680 r.in.server = talloc_asprintf(tctx, "\\\\%s", servername);
2681 r.in.level = level;
2682 r.in.buffer = NULL;
2683 r.in.offered = 0;
2684 r.out.needed = &needed;
2685 r.out.count = &count;
2686 r.out.info = &info;
2688 torture_assert_ntstatus_ok(tctx,
2689 dcerpc_spoolss_EnumPrinters_r(b, tctx, &r),
2690 "dcerpc_spoolss_EnumPrinters failed");
2691 torture_assert_werr_equal(tctx, r.out.result, WERR_INSUFFICIENT_BUFFER,
2692 "EnumPrinters unexpected return code should be WERR_INSUFFICIENT_BUFFER");
2694 blob = data_blob_talloc_zero(tctx, needed);
2695 if (blob.data == NULL) {
2696 return false;
2699 r.in.buffer = &blob;
2700 r.in.offered = needed;
2702 torture_assert_ntstatus_ok(tctx,
2703 dcerpc_spoolss_EnumPrinters_r(b, tctx, &r),
2704 "dcerpc_spoolss_EnumPrinters failed");
2705 torture_assert_werr_ok(tctx, r.out.result,
2706 "dcerpc_spoolss_EnumPrinters failed");
2708 *num_printers = count;
2710 return true;
2713 static bool getprinterinfo(struct torture_context *tctx,
2714 struct dcerpc_binding_handle *b,
2715 struct policy_handle *handle, int level,
2716 union spoolss_PrinterInfo **res)
2718 struct spoolss_GetPrinter r;
2719 DATA_BLOB blob;
2720 uint32_t needed;
2722 r.in.handle = handle;
2723 r.in.level = level;
2724 r.in.buffer = NULL;
2725 r.in.offered = 0;
2726 r.out.needed = &needed;
2728 torture_assert_ntstatus_ok(tctx,
2729 dcerpc_spoolss_GetPrinter_r(b, tctx, &r),
2730 "dcerpc_spoolss_GetPrinter failed");
2731 torture_assert_werr_equal(tctx, r.out.result, WERR_INSUFFICIENT_BUFFER,
2732 "GetPrinter unexpected return code should be WERR_INSUFFICIENT_BUFFER");
2734 r.in.handle = handle;
2735 r.in.level = level;
2736 blob = data_blob_talloc_zero(tctx, needed);
2737 if (blob.data == NULL) {
2738 return false;
2740 r.in.buffer = &blob;
2741 r.in.offered = needed;
2743 torture_assert_ntstatus_ok(tctx,
2744 dcerpc_spoolss_GetPrinter_r(b, tctx, &r),
2745 "dcerpc_spoolss_GetPrinter failed");
2746 torture_assert_werr_ok(tctx, r.out.result,
2747 "dcerpc_spoolss_GetPrinter failed");
2749 if (res != NULL) {
2750 *res = talloc_steal(tctx, r.out.info);
2753 return true;
2756 static bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2758 struct dcerpc_pipe *p, *p2;
2759 struct dcerpc_binding_handle *b;
2760 struct policy_handle server_handle, printer_handle;
2761 const char **printers;
2762 int num_printers;
2763 struct spoolss_UserLevel1 userlevel1;
2764 char *servername;
2766 torture_assert(torture,
2767 rap_get_servername(torture, &servername),
2768 "failed to rap servername");
2770 torture_assert_ntstatus_ok(torture,
2771 torture_rpc_connection(torture, &p2, &ndr_table_srvsvc),
2772 "failed to setup srvsvc");
2774 torture_assert(torture,
2775 find_printers(torture, p2, &printers, &num_printers),
2776 "failed to find printers via srvsvc");
2778 talloc_free(p2);
2780 if (num_printers == 0) {
2781 torture_skip(torture, "Did not find printers\n");
2782 return true;
2785 torture_assert_ntstatus_ok(torture,
2786 torture_rpc_connection(torture, &p, &ndr_table_spoolss),
2787 "failed to setup spoolss");
2789 b = p->binding_handle;
2791 ZERO_STRUCT(userlevel1);
2792 userlevel1.client = talloc_asprintf(
2793 torture, "\\\\%s", lpcfg_netbios_name(torture->lp_ctx));
2794 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2795 userlevel1.build = 2600;
2796 userlevel1.major = 3;
2797 userlevel1.minor = 0;
2798 userlevel1.processor = 0;
2801 struct spoolss_OpenPrinterEx r;
2803 ZERO_STRUCT(r);
2804 r.in.printername = talloc_asprintf(torture, "\\\\%s",
2805 servername);
2806 r.in.datatype = NULL;
2807 r.in.access_mask = 0;
2808 r.in.level = 1;
2809 r.in.userlevel.level1 = &userlevel1;
2810 r.out.handle = &server_handle;
2812 torture_assert_ntstatus_ok(torture,
2813 dcerpc_spoolss_OpenPrinterEx_r(b, torture, &r),
2814 "dcerpc_spoolss_OpenPrinterEx failed");
2815 torture_assert_werr_ok(torture, r.out.result,
2816 "dcerpc_spoolss_OpenPrinterEx failed");
2820 struct spoolss_ClosePrinter r;
2822 r.in.handle = &server_handle;
2823 r.out.handle = &server_handle;
2825 torture_assert_ntstatus_ok(torture,
2826 dcerpc_spoolss_ClosePrinter_r(b, torture, &r),
2827 "dcerpc_spoolss_ClosePrinter failed");
2828 torture_assert_werr_ok(torture, r.out.result,
2829 "dcerpc_spoolss_ClosePrinter failed");
2833 struct spoolss_OpenPrinterEx r;
2835 ZERO_STRUCT(r);
2836 r.in.printername = talloc_asprintf(
2837 torture, "\\\\%s\\%s", servername, printers[0]);
2838 r.in.datatype = NULL;
2839 r.in.access_mask = 0;
2840 r.in.level = 1;
2841 r.in.userlevel.level1 = &userlevel1;
2842 r.out.handle = &printer_handle;
2844 torture_assert_ntstatus_ok(torture,
2845 dcerpc_spoolss_OpenPrinterEx_r(b, torture, &r),
2846 "dcerpc_spoolss_OpenPrinterEx failed");
2847 torture_assert_werr_ok(torture, r.out.result,
2848 "dcerpc_spoolss_OpenPrinterEx failed");
2852 int i;
2854 for (i=0; i<8; i++) {
2855 torture_assert(torture,
2856 getprinterinfo(torture, b, &printer_handle, i, NULL),
2857 talloc_asprintf(torture, "getprinterinfo %d failed", i));
2862 struct spoolss_ClosePrinter r;
2864 r.in.handle = &printer_handle;
2865 r.out.handle = &printer_handle;
2867 torture_assert_ntstatus_ok(torture,
2868 dcerpc_spoolss_ClosePrinter_r(b, torture, &r),
2869 "dcerpc_spoolss_ClosePrinter failed");
2870 torture_assert_werr_ok(torture, r.out.result,
2871 "dcerpc_spoolss_ClosePrinter failed");
2875 int num_enumerated;
2877 torture_assert(torture,
2878 enumprinters(torture, b, servername, 1, &num_enumerated),
2879 "enumprinters failed");
2881 torture_assert_int_equal(torture, num_printers, num_enumerated,
2882 "netshareenum / enumprinters lvl 1 numprinter mismatch");
2886 int num_enumerated;
2888 torture_assert(torture,
2889 enumprinters(torture, b, servername, 2, &num_enumerated),
2890 "enumprinters failed");
2892 torture_assert_int_equal(torture, num_printers, num_enumerated,
2893 "netshareenum / enumprinters lvl 2 numprinter mismatch");
2896 return true;
2899 static bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2901 struct dcerpc_pipe *p;
2902 struct dcerpc_binding_handle *b;
2903 char *servername;
2905 torture_assert(torture,
2906 rap_get_servername(torture, &servername),
2907 "failed to rap servername");
2909 torture_assert_ntstatus_ok(torture,
2910 torture_rpc_connection(torture, &p, &ndr_table_wkssvc),
2911 "failed to setup wkssvc");
2913 b = p->binding_handle;
2916 struct wkssvc_NetWkstaInfo100 wks100;
2917 union wkssvc_NetWkstaInfo info;
2918 struct wkssvc_NetWkstaGetInfo r;
2920 r.in.server_name = "\\foo";
2921 r.in.level = 100;
2922 info.info100 = &wks100;
2923 r.out.info = &info;
2925 torture_assert_ntstatus_ok(torture,
2926 dcerpc_wkssvc_NetWkstaGetInfo_r(b, torture, &r),
2927 "dcerpc_wkssvc_NetWksGetInfo failed");
2928 torture_assert_werr_ok(torture, r.out.result,
2929 "dcerpc_wkssvc_NetWksGetInfo failed");
2931 torture_assert_str_equal(torture, servername, r.out.info->info100->server_name,
2932 "servername RAP / DCERPC inconsistency");
2935 return true;
2938 static bool winreg_close(struct torture_context *tctx,
2939 struct dcerpc_binding_handle *b,
2940 struct policy_handle *handle)
2942 struct winreg_CloseKey c;
2944 c.in.handle = c.out.handle = handle;
2946 torture_assert_ntstatus_ok(tctx,
2947 dcerpc_winreg_CloseKey_r(b, tctx, &c),
2948 "winreg_CloseKey failed");
2949 torture_assert_werr_ok(tctx, c.out.result,
2950 "winreg_CloseKey failed");
2952 return true;
2955 static bool enumvalues(struct torture_context *tctx,
2956 struct dcerpc_binding_handle *b,
2957 struct policy_handle *handle)
2959 uint32_t enum_index = 0;
2961 while (1) {
2962 struct winreg_EnumValue r;
2963 struct winreg_ValNameBuf name;
2964 enum winreg_Type type = 0;
2965 uint8_t buf8[1024];
2966 NTSTATUS status;
2967 uint32_t size, length;
2969 r.in.handle = handle;
2970 r.in.enum_index = enum_index;
2971 name.name = "";
2972 name.size = 1024;
2973 r.in.name = r.out.name = &name;
2974 size = 1024;
2975 length = 5;
2976 r.in.type = &type;
2977 r.in.value = buf8;
2978 r.in.size = &size;
2979 r.in.length = &length;
2981 status = dcerpc_winreg_EnumValue_r(b, tctx, &r);
2982 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2983 return true;
2985 enum_index += 1;
2989 static bool enumkeys(struct torture_context *tctx,
2990 struct dcerpc_binding_handle *b,
2991 struct policy_handle *handle,
2992 int depth)
2994 struct winreg_EnumKey r;
2995 struct winreg_StringBuf kclass, name;
2996 NTSTATUS status;
2997 NTTIME t = 0;
2999 if (depth <= 0) {
3000 return true;
3003 kclass.name = "";
3004 kclass.size = 1024;
3006 r.in.handle = handle;
3007 r.in.enum_index = 0;
3008 r.in.name = &name;
3009 r.in.keyclass = &kclass;
3010 r.out.name = &name;
3011 r.in.last_changed_time = &t;
3013 do {
3014 struct winreg_OpenKey o;
3015 struct policy_handle key_handle;
3016 int i;
3018 name.name = NULL;
3019 name.size = 1024;
3021 status = dcerpc_winreg_EnumKey_r(b, tctx, &r);
3022 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3023 /* We're done enumerating */
3024 return true;
3027 for (i=0; i<10-depth; i++) {
3028 torture_comment(tctx, " ");
3030 torture_comment(tctx, "%s\n", r.out.name->name);
3032 o.in.parent_handle = handle;
3033 o.in.keyname.name = r.out.name->name;
3034 o.in.options = 0;
3035 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3036 o.out.handle = &key_handle;
3038 status = dcerpc_winreg_OpenKey_r(b, tctx, &o);
3039 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3040 enumkeys(tctx, b, &key_handle, depth-1);
3041 enumvalues(tctx, b, &key_handle);
3042 torture_assert(tctx, winreg_close(tctx, b, &key_handle), "");
3045 r.in.enum_index += 1;
3046 } while(true);
3048 return true;
3051 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_binding_handle *, TALLOC_CTX *, void *);
3053 static bool test_Open3(struct torture_context *tctx,
3054 struct dcerpc_binding_handle *b,
3055 const char *name, winreg_open_fn open_fn)
3057 struct policy_handle handle;
3058 struct winreg_OpenHKLM r;
3060 r.in.system_name = 0;
3061 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3062 r.out.handle = &handle;
3064 torture_assert_ntstatus_ok(tctx,
3065 open_fn(b, tctx, &r),
3066 talloc_asprintf(tctx, "%s failed", name));
3067 torture_assert_werr_ok(tctx, r.out.result,
3068 talloc_asprintf(tctx, "%s failed", name));
3070 enumkeys(tctx, b, &handle, 4);
3072 torture_assert(tctx,
3073 winreg_close(tctx, b, &handle),
3074 "dcerpc_CloseKey failed");
3076 return true;
3079 static bool torture_samba3_rpc_winreg(struct torture_context *torture)
3081 struct dcerpc_pipe *p;
3082 struct dcerpc_binding_handle *b;
3083 bool ret = true;
3084 struct {
3085 const char *name;
3086 winreg_open_fn fn;
3087 } open_fns[] = {
3088 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM_r },
3089 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU_r },
3090 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD_r },
3091 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT_r },
3092 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR_r }};
3093 #if 0
3094 int i;
3095 #endif
3097 torture_assert_ntstatus_ok(torture,
3098 torture_rpc_connection(torture, &p, &ndr_table_winreg),
3099 "failed to setup winreg");
3101 b = p->binding_handle;
3103 #if 1
3104 ret = test_Open3(torture, b, open_fns[0].name, open_fns[0].fn);
3105 #else
3106 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3107 if (!test_Open3(torture, b, open_fns[i].name, open_fns[i].fn))
3108 ret = false;
3110 #endif
3111 return ret;
3114 static bool get_shareinfo(struct torture_context *tctx,
3115 struct dcerpc_binding_handle *b,
3116 const char *servername,
3117 const char *share,
3118 struct srvsvc_NetShareInfo502 **info502)
3120 struct srvsvc_NetShareGetInfo r;
3121 union srvsvc_NetShareInfo info;
3123 r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", servername);
3124 r.in.share_name = share;
3125 r.in.level = 502;
3126 r.out.info = &info;
3128 torture_assert_ntstatus_ok(tctx,
3129 dcerpc_srvsvc_NetShareGetInfo_r(b, tctx, &r),
3130 "srvsvc_NetShareGetInfo failed");
3131 torture_assert_werr_ok(tctx, r.out.result,
3132 "srvsvc_NetShareGetInfo failed");
3134 *info502 = talloc_move(tctx, &info.info502);
3136 return true;
3140 * Get us a handle on HKLM\
3143 static bool get_hklm_handle(struct torture_context *tctx,
3144 struct dcerpc_binding_handle *b,
3145 struct policy_handle *handle)
3147 struct winreg_OpenHKLM r;
3148 struct policy_handle result;
3150 r.in.system_name = 0;
3151 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3152 r.out.handle = &result;
3154 torture_assert_ntstatus_ok(tctx,
3155 dcerpc_winreg_OpenHKLM_r(b, tctx, &r),
3156 "OpenHKLM failed");
3157 torture_assert_werr_ok(tctx, r.out.result,
3158 "OpenHKLM failed");
3160 *handle = result;
3162 return true;
3165 static bool torture_samba3_createshare(struct torture_context *tctx,
3166 struct dcerpc_binding_handle *b,
3167 const char *sharename)
3169 struct policy_handle hklm;
3170 struct policy_handle new_handle;
3171 struct winreg_CreateKey c;
3172 struct winreg_CloseKey cl;
3173 enum winreg_CreateAction action_taken;
3175 c.in.handle = &hklm;
3176 c.in.name.name = talloc_asprintf(
3177 tctx, "software\\samba\\smbconf\\%s", sharename);
3178 torture_assert(tctx, c.in.name.name, "talloc_asprintf failed");
3180 c.in.keyclass.name = "";
3181 c.in.options = 0;
3182 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3183 c.in.secdesc = NULL;
3184 c.in.action_taken = &action_taken;
3185 c.out.new_handle = &new_handle;
3186 c.out.action_taken = &action_taken;
3188 torture_assert_ntstatus_ok(tctx,
3189 dcerpc_winreg_CreateKey_r(b, tctx, &c),
3190 "OpenKey failed");
3191 torture_assert_werr_ok(tctx, c.out.result,
3192 "OpenKey failed");
3194 cl.in.handle = &new_handle;
3195 cl.out.handle = &new_handle;
3197 torture_assert_ntstatus_ok(tctx,
3198 dcerpc_winreg_CloseKey_r(b, tctx, &cl),
3199 "CloseKey failed");
3200 torture_assert_werr_ok(tctx, cl.out.result,
3201 "CloseKey failed");
3203 return true;
3206 static bool torture_samba3_deleteshare(struct torture_context *tctx,
3207 struct dcerpc_binding_handle *b,
3208 const char *sharename)
3210 struct policy_handle hklm;
3211 struct winreg_DeleteKey d;
3213 torture_assert(tctx,
3214 get_hklm_handle(tctx, b, &hklm),
3215 "get_hklm_handle failed");
3217 d.in.handle = &hklm;
3218 d.in.key.name = talloc_asprintf(
3219 tctx, "software\\samba\\smbconf\\%s", sharename);
3220 torture_assert(tctx, d.in.key.name, "talloc_asprintf failed");
3222 torture_assert_ntstatus_ok(tctx,
3223 dcerpc_winreg_DeleteKey_r(b, tctx, &d),
3224 "DeleteKey failed");
3225 torture_assert_werr_ok(tctx, d.out.result,
3226 "DeleteKey failed");
3228 return true;
3231 static bool torture_samba3_setconfig(struct torture_context *tctx,
3232 struct dcerpc_binding_handle *b,
3233 const char *sharename,
3234 const char *parameter,
3235 const char *value)
3237 struct policy_handle hklm, key_handle;
3238 struct winreg_OpenKey o;
3239 struct winreg_SetValue s;
3240 uint32_t type;
3241 DATA_BLOB val;
3243 torture_assert(tctx,
3244 get_hklm_handle(tctx, b, &hklm),
3245 "get_hklm_handle failed");
3247 o.in.parent_handle = &hklm;
3248 o.in.keyname.name = talloc_asprintf(
3249 tctx, "software\\samba\\smbconf\\%s", sharename);
3250 torture_assert(tctx, o.in.keyname.name, "talloc_asprintf failed");
3252 o.in.options = 0;
3253 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3254 o.out.handle = &key_handle;
3256 torture_assert_ntstatus_ok(tctx,
3257 dcerpc_winreg_OpenKey_r(b, tctx, &o),
3258 "OpenKey failed");
3259 torture_assert_werr_ok(tctx, o.out.result,
3260 "OpenKey failed");
3262 torture_assert(tctx,
3263 reg_string_to_val(tctx, "REG_SZ", value, &type, &val),
3264 "reg_string_to_val failed");
3266 s.in.handle = &key_handle;
3267 s.in.name.name = parameter;
3268 s.in.type = type;
3269 s.in.data = val.data;
3270 s.in.size = val.length;
3272 torture_assert_ntstatus_ok(tctx,
3273 dcerpc_winreg_SetValue_r(b, tctx, &s),
3274 "SetValue failed");
3275 torture_assert_werr_ok(tctx, s.out.result,
3276 "SetValue failed");
3278 return true;
3281 static bool torture_samba3_regconfig(struct torture_context *torture)
3283 struct srvsvc_NetShareInfo502 *i = NULL;
3284 const char *comment = "Dummer Kommentar";
3285 struct dcerpc_pipe *srvsvc_pipe, *winreg_pipe;
3287 torture_assert_ntstatus_ok(torture,
3288 torture_rpc_connection(torture, &srvsvc_pipe, &ndr_table_srvsvc),
3289 "failed to setup srvsvc");
3291 torture_assert_ntstatus_ok(torture,
3292 torture_rpc_connection(torture, &winreg_pipe, &ndr_table_winreg),
3293 "failed to setup winreg");
3295 torture_assert(torture,
3296 torture_samba3_createshare(torture, winreg_pipe->binding_handle, "blubber"),
3297 "torture_samba3_createshare failed");
3299 torture_assert(torture,
3300 torture_samba3_setconfig(torture, winreg_pipe->binding_handle, "blubber", "comment", comment),
3301 "torture_samba3_setconfig failed");
3303 torture_assert(torture,
3304 get_shareinfo(torture, srvsvc_pipe->binding_handle, dcerpc_server_name(srvsvc_pipe), "blubber", &i),
3305 "get_shareinfo failed");
3307 torture_assert_str_equal(torture, comment, i->comment,
3308 "got unexpected comment");
3310 torture_assert(torture,
3311 torture_samba3_deleteshare(torture, winreg_pipe->binding_handle, "blubber"),
3312 "torture_samba3_deleteshare failed");
3314 return true;
3318 * Test that even with a result of 0 rids the array is returned as a
3319 * non-NULL pointer. Yes, XP does notice.
3322 bool torture_samba3_getaliasmembership_0(struct torture_context *torture)
3324 struct dcerpc_pipe *p;
3325 struct dcerpc_binding_handle *b;
3326 struct samr_Connect2 c;
3327 struct samr_OpenDomain o;
3328 struct dom_sid sid;
3329 struct lsa_SidPtr ptr;
3330 struct lsa_SidArray sids;
3331 struct samr_GetAliasMembership g;
3332 struct samr_Ids rids;
3333 struct policy_handle samr, domain;
3335 torture_assert_ntstatus_ok(torture,
3336 torture_rpc_connection(torture, &p, &ndr_table_samr),
3337 "failed to setup samr");
3339 b = p->binding_handle;
3341 c.in.system_name = NULL;
3342 c.in.access_mask = SAMR_ACCESS_LOOKUP_DOMAIN;
3343 c.out.connect_handle = &samr;
3344 torture_assert_ntstatus_ok(torture,
3345 dcerpc_samr_Connect2_r(b, torture, &c),
3346 "");
3347 torture_assert_ntstatus_ok(torture, c.out.result,
3348 "");
3349 dom_sid_parse("S-1-5-32", &sid);
3350 o.in.connect_handle = &samr;
3351 o.in.access_mask = SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS;
3352 o.in.sid = &sid;
3353 o.out.domain_handle = &domain;
3354 torture_assert_ntstatus_ok(torture,
3355 dcerpc_samr_OpenDomain_r(b, torture, &o),
3356 "");
3357 torture_assert_ntstatus_ok(torture, o.out.result,
3358 "");
3359 dom_sid_parse("S-1-2-3-4-5", &sid);
3360 ptr.sid = &sid;
3361 sids.num_sids = 1;
3362 sids.sids = &ptr;
3363 g.in.domain_handle = &domain;
3364 g.in.sids = &sids;
3365 g.out.rids = &rids;
3366 torture_assert_ntstatus_ok(torture,
3367 dcerpc_samr_GetAliasMembership_r(b, torture, &g),
3368 "");
3369 torture_assert_ntstatus_ok(torture, g.out.result,
3370 "");
3371 if (rids.ids == NULL) {
3372 /* This is the piece to test here */
3373 torture_fail(torture,
3374 "torture_samba3_getaliasmembership_0: "
3375 "Server returns NULL rids array\n");
3378 return true;
3382 * Test smb reauthentication while rpc pipe is in use.
3384 static bool torture_rpc_smb_reauth1(struct torture_context *torture)
3386 TALLOC_CTX *mem_ctx;
3387 NTSTATUS status;
3388 bool ret = false;
3389 struct smbcli_state *cli;
3390 struct smbcli_options options;
3391 struct smbcli_session_options session_options;
3393 struct dcerpc_pipe *lsa_pipe;
3394 struct dcerpc_binding_handle *lsa_handle;
3395 struct lsa_GetUserName r;
3396 struct lsa_String *authority_name_p = NULL;
3397 char *authority_name_saved = NULL;
3398 struct lsa_String *account_name_p = NULL;
3399 char *account_name_saved = NULL;
3400 struct cli_credentials *anon_creds = NULL;
3401 struct smb_composite_sesssetup io;
3403 mem_ctx = talloc_init("torture_samba3_reauth");
3404 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
3406 lpcfg_smbcli_options(torture->lp_ctx, &options);
3407 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
3409 status = smbcli_full_connection(mem_ctx, &cli,
3410 torture_setting_string(torture, "host", NULL),
3411 lpcfg_smb_ports(torture->lp_ctx),
3412 "IPC$", NULL,
3413 lpcfg_socket_options(torture->lp_ctx),
3414 cmdline_credentials,
3415 lpcfg_resolve_context(torture->lp_ctx),
3416 torture->ev, &options, &session_options,
3417 lpcfg_gensec_settings(torture, torture->lp_ctx));
3418 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3419 "smbcli_full_connection failed");
3421 lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
3422 torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
3423 "dcerpc_pipe_init failed");
3424 lsa_handle = lsa_pipe->binding_handle;
3426 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
3427 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3428 "dcerpc_pipe_open failed");
3430 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
3431 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3432 "dcerpc_bind_auth_none failed");
3434 /* lsa getusername */
3436 ZERO_STRUCT(r);
3437 r.in.system_name = "\\";
3438 r.in.account_name = &account_name_p;
3439 r.in.authority_name = &authority_name_p;
3440 r.out.account_name = &account_name_p;
3442 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3444 authority_name_p = *r.out.authority_name;
3446 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3447 "GetUserName failed");
3448 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3449 "GetUserName failed");
3451 torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
3452 authority_name_p->string,
3453 account_name_p->string);
3455 account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
3456 torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
3457 "talloc failed");
3458 authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
3459 torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
3460 "talloc failed");
3462 /* smb re-authenticate as anonymous */
3464 anon_creds = cli_credentials_init_anon(mem_ctx);
3466 ZERO_STRUCT(io);
3467 io.in.sesskey = cli->transport->negotiate.sesskey;
3468 io.in.capabilities = cli->transport->negotiate.capabilities;
3469 io.in.credentials = anon_creds;
3470 io.in.workgroup = lpcfg_workgroup(torture->lp_ctx);
3471 io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
3473 status = smb_composite_sesssetup(cli->session, &io);
3474 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3475 "session reauth to anon failed");
3477 /* re-do lsa getusername after reauth */
3479 TALLOC_FREE(authority_name_p);
3480 TALLOC_FREE(account_name_p);
3481 ZERO_STRUCT(r);
3482 r.in.system_name = "\\";
3483 r.in.account_name = &account_name_p;
3484 r.in.authority_name = &authority_name_p;
3485 r.out.account_name = &account_name_p;
3487 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3489 authority_name_p = *r.out.authority_name;
3491 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3492 "GetUserName failed");
3493 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3494 "GetUserName failed");
3496 torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
3497 ret, done, "authority_name not equal after reauth to anon");
3498 torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
3499 ret, done, "account_name not equal after reauth to anon");
3501 /* smb re-auth again to the original user */
3503 ZERO_STRUCT(io);
3504 io.in.sesskey = cli->transport->negotiate.sesskey;
3505 io.in.capabilities = cli->transport->negotiate.capabilities;
3506 io.in.credentials = cmdline_credentials;
3507 io.in.workgroup = lpcfg_workgroup(torture->lp_ctx);
3508 io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
3510 status = smb_composite_sesssetup(cli->session, &io);
3511 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3512 "session reauth to anon failed");
3514 /* re-do lsa getusername */
3516 TALLOC_FREE(authority_name_p);
3517 TALLOC_FREE(account_name_p);
3518 ZERO_STRUCT(r);
3519 r.in.system_name = "\\";
3520 r.in.account_name = &account_name_p;
3521 r.in.authority_name = &authority_name_p;
3522 r.out.account_name = &account_name_p;
3524 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3526 authority_name_p = *r.out.authority_name;
3528 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3529 "GetUserName failed");
3530 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3531 "GetUserName failed");
3533 torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
3534 ret, done, "authority_name not equal after reauth to anon");
3535 torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
3536 ret, done, "account_name not equal after reauth to anon");
3538 ret = true;
3540 done:
3541 talloc_free(mem_ctx);
3542 return ret;
3546 * Test smb reauthentication while rpc pipe is in use.
3547 * Open a second lsa bind after reauth to anon.
3548 * Do lsa getusername on that second bind.
3550 static bool torture_rpc_smb_reauth2(struct torture_context *torture)
3552 TALLOC_CTX *mem_ctx;
3553 NTSTATUS status;
3554 bool ret = false;
3555 struct smbcli_state *cli;
3556 struct smbcli_options options;
3557 struct smbcli_session_options session_options;
3559 struct dcerpc_pipe *lsa_pipe;
3560 struct dcerpc_binding_handle *lsa_handle;
3561 struct lsa_GetUserName r;
3562 struct lsa_String *authority_name_p = NULL;
3563 char *authority_name_saved = NULL;
3564 struct lsa_String *account_name_p = NULL;
3565 char *account_name_saved = NULL;
3566 struct cli_credentials *anon_creds = NULL;
3567 struct smb_composite_sesssetup io;
3569 mem_ctx = talloc_init("torture_samba3_reauth");
3570 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
3572 lpcfg_smbcli_options(torture->lp_ctx, &options);
3573 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
3575 status = smbcli_full_connection(mem_ctx, &cli,
3576 torture_setting_string(torture, "host", NULL),
3577 lpcfg_smb_ports(torture->lp_ctx),
3578 "IPC$", NULL,
3579 lpcfg_socket_options(torture->lp_ctx),
3580 cmdline_credentials,
3581 lpcfg_resolve_context(torture->lp_ctx),
3582 torture->ev, &options, &session_options,
3583 lpcfg_gensec_settings(torture, torture->lp_ctx));
3584 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3585 "smbcli_full_connection failed");
3587 /* smb re-authenticate as anonymous */
3589 anon_creds = cli_credentials_init_anon(mem_ctx);
3591 ZERO_STRUCT(io);
3592 io.in.sesskey = cli->transport->negotiate.sesskey;
3593 io.in.capabilities = cli->transport->negotiate.capabilities;
3594 io.in.credentials = anon_creds;
3595 io.in.workgroup = lpcfg_workgroup(torture->lp_ctx);
3596 io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
3598 status = smb_composite_sesssetup(cli->session, &io);
3599 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3600 "session reauth to anon failed");
3602 /* open the lsa pipe */
3604 lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
3605 torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
3606 "dcerpc_pipe_init failed");
3607 lsa_handle = lsa_pipe->binding_handle;
3609 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
3610 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3611 "dcerpc_pipe_open failed");
3613 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
3614 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3615 "dcerpc_bind_auth_none failed");
3617 /* lsa getusername */
3619 ZERO_STRUCT(r);
3620 r.in.system_name = "\\";
3621 r.in.account_name = &account_name_p;
3622 r.in.authority_name = &authority_name_p;
3623 r.out.account_name = &account_name_p;
3625 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3627 authority_name_p = *r.out.authority_name;
3629 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3630 "GetUserName failed");
3631 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3632 "GetUserName failed");
3634 torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
3635 authority_name_p->string,
3636 account_name_p->string);
3638 account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
3639 torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
3640 "talloc failed");
3641 authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
3642 torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
3643 "talloc failed");
3645 /* smb re-auth again to the original user */
3647 ZERO_STRUCT(io);
3648 io.in.sesskey = cli->transport->negotiate.sesskey;
3649 io.in.capabilities = cli->transport->negotiate.capabilities;
3650 io.in.credentials = cmdline_credentials;
3651 io.in.workgroup = lpcfg_workgroup(torture->lp_ctx);
3652 io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
3654 status = smb_composite_sesssetup(cli->session, &io);
3655 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3656 "session reauth to anon failed");
3658 /* re-do lsa getusername after reauth */
3660 TALLOC_FREE(authority_name_p);
3661 TALLOC_FREE(account_name_p);
3662 ZERO_STRUCT(r);
3663 r.in.system_name = "\\";
3664 r.in.account_name = &account_name_p;
3665 r.in.authority_name = &authority_name_p;
3666 r.out.account_name = &account_name_p;
3668 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3670 authority_name_p = *r.out.authority_name;
3672 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3673 "GetUserName failed");
3674 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3675 "GetUserName failed");
3677 torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
3678 ret, done, "authority_name not equal after reauth to anon");
3679 torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
3680 ret, done, "account_name not equal after reauth to anon");
3682 ret = true;
3684 done:
3685 talloc_free(mem_ctx);
3686 return ret;
3690 * Test smb2 reauthentication while rpc pipe is in use.
3692 static bool torture_rpc_smb2_reauth1(struct torture_context *torture)
3694 TALLOC_CTX *mem_ctx;
3695 NTSTATUS status;
3696 bool ret = false;
3697 struct smbcli_options options;
3699 struct dcerpc_pipe *lsa_pipe;
3700 struct dcerpc_binding_handle *lsa_handle;
3701 struct lsa_GetUserName r;
3702 struct lsa_String *authority_name_p = NULL;
3703 char *authority_name_saved = NULL;
3704 struct lsa_String *account_name_p = NULL;
3705 char *account_name_saved = NULL;
3706 struct cli_credentials *anon_creds = NULL;
3707 const char *host = torture_setting_string(torture, "host", NULL);
3708 struct smb2_tree *tree;
3710 mem_ctx = talloc_init("torture_samba3_reauth");
3711 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
3713 lpcfg_smbcli_options(torture->lp_ctx, &options);
3715 status = smb2_connect(mem_ctx,
3716 host,
3717 lpcfg_smb_ports(torture->lp_ctx),
3718 "IPC$",
3719 lpcfg_resolve_context(torture->lp_ctx),
3720 cmdline_credentials,
3721 &tree,
3722 torture->ev,
3723 &options,
3724 lpcfg_socket_options(torture->lp_ctx),
3725 lpcfg_gensec_settings(torture, torture->lp_ctx)
3727 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3728 "smb2_connect failed");
3730 lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
3731 torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
3732 "dcerpc_pipe_init failed");
3733 lsa_handle = lsa_pipe->binding_handle;
3735 status = dcerpc_pipe_open_smb2(lsa_pipe, tree, "lsarpc");
3736 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3737 "dcerpc_pipe_open_smb2 failed");
3739 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
3740 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3741 "dcerpc_bind_auth_none failed");
3743 /* lsa getusername */
3745 ZERO_STRUCT(r);
3746 r.in.system_name = "\\";
3747 r.in.account_name = &account_name_p;
3748 r.in.authority_name = &authority_name_p;
3749 r.out.account_name = &account_name_p;
3751 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3753 authority_name_p = *r.out.authority_name;
3755 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3756 "GetUserName failed");
3757 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3758 "GetUserName failed");
3760 torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
3761 authority_name_p->string,
3762 account_name_p->string);
3764 account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
3765 torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
3766 "talloc failed");
3767 authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
3768 torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
3769 "talloc failed");
3771 /* smb re-authenticate as anonymous */
3773 anon_creds = cli_credentials_init_anon(mem_ctx);
3775 status = smb2_session_setup_spnego(tree->session,
3776 anon_creds,
3777 0 /* previous_session_id */);
3778 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3779 "session reauth to anon failed");
3781 /* re-do lsa getusername after reauth */
3783 TALLOC_FREE(authority_name_p);
3784 TALLOC_FREE(account_name_p);
3785 ZERO_STRUCT(r);
3786 r.in.system_name = "\\";
3787 r.in.account_name = &account_name_p;
3788 r.in.authority_name = &authority_name_p;
3789 r.out.account_name = &account_name_p;
3791 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3793 authority_name_p = *r.out.authority_name;
3795 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3796 "GetUserName failed");
3797 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3798 "GetUserName failed");
3800 torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
3801 ret, done, "authority_name not equal after reauth to anon");
3802 torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
3803 ret, done, "account_name not equal after reauth to anon");
3805 /* smb re-auth again to the original user */
3807 status = smb2_session_setup_spnego(tree->session,
3808 cmdline_credentials,
3809 0 /* previous_session_id */);
3810 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3811 "session reauth to anon failed");
3813 /* re-do lsa getusername */
3815 TALLOC_FREE(authority_name_p);
3816 TALLOC_FREE(account_name_p);
3817 ZERO_STRUCT(r);
3818 r.in.system_name = "\\";
3819 r.in.account_name = &account_name_p;
3820 r.in.authority_name = &authority_name_p;
3821 r.out.account_name = &account_name_p;
3823 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3825 authority_name_p = *r.out.authority_name;
3827 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3828 "GetUserName failed");
3829 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3830 "GetUserName failed");
3832 torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
3833 ret, done, "authority_name not equal after reauth to anon");
3834 torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
3835 ret, done, "account_name not equal after reauth to anon");
3837 ret = true;
3839 done:
3840 talloc_free(mem_ctx);
3841 return ret;
3845 * Test smb2 reauthentication while rpc pipe is in use.
3846 * Open a second lsa bind after reauth to anon.
3847 * Do lsa getusername on that second bind.
3849 static bool torture_rpc_smb2_reauth2(struct torture_context *torture)
3851 TALLOC_CTX *mem_ctx;
3852 NTSTATUS status;
3853 bool ret = false;
3854 struct smbcli_options options;
3856 struct dcerpc_pipe *lsa_pipe;
3857 struct dcerpc_binding_handle *lsa_handle;
3858 struct lsa_GetUserName r;
3859 struct lsa_String *authority_name_p = NULL;
3860 char *authority_name_saved = NULL;
3861 struct lsa_String *account_name_p = NULL;
3862 char *account_name_saved = NULL;
3863 struct cli_credentials *anon_creds = NULL;
3864 const char *host = torture_setting_string(torture, "host", NULL);
3865 struct smb2_tree *tree;
3867 mem_ctx = talloc_init("torture_samba3_reauth");
3868 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
3870 lpcfg_smbcli_options(torture->lp_ctx, &options);
3872 status = smb2_connect(mem_ctx,
3873 host,
3874 lpcfg_smb_ports(torture->lp_ctx),
3875 "IPC$",
3876 lpcfg_resolve_context(torture->lp_ctx),
3877 cmdline_credentials,
3878 &tree,
3879 torture->ev,
3880 &options,
3881 lpcfg_socket_options(torture->lp_ctx),
3882 lpcfg_gensec_settings(torture, torture->lp_ctx)
3884 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3885 "smb2_connect failed");
3887 /* smb re-authenticate as anonymous */
3889 anon_creds = cli_credentials_init_anon(mem_ctx);
3891 status = smb2_session_setup_spnego(tree->session,
3892 anon_creds,
3893 0 /* previous_session_id */);
3894 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3895 "session reauth to anon failed");
3897 /* open the lsa pipe */
3899 lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
3900 torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
3901 "dcerpc_pipe_init failed");
3902 lsa_handle = lsa_pipe->binding_handle;
3904 status = dcerpc_pipe_open_smb2(lsa_pipe, tree, "lsarpc");
3905 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3906 "dcerpc_pipe_open_smb2 failed");
3908 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
3909 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3910 "dcerpc_bind_auth_none failed");
3912 /* lsa getusername */
3914 ZERO_STRUCT(r);
3915 r.in.system_name = "\\";
3916 r.in.account_name = &account_name_p;
3917 r.in.authority_name = &authority_name_p;
3918 r.out.account_name = &account_name_p;
3920 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3922 authority_name_p = *r.out.authority_name;
3924 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3925 "GetUserName failed");
3926 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3927 "GetUserName failed");
3929 torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
3930 authority_name_p->string,
3931 account_name_p->string);
3933 account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
3934 torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
3935 "talloc failed");
3936 authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
3937 torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
3938 "talloc failed");
3940 /* smb re-auth again to the original user */
3942 status = smb2_session_setup_spnego(tree->session,
3943 cmdline_credentials,
3944 0 /* previous_session_id */);
3945 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3946 "session reauth to anon failed");
3948 /* re-do lsa getusername */
3950 TALLOC_FREE(authority_name_p);
3951 TALLOC_FREE(account_name_p);
3952 ZERO_STRUCT(r);
3953 r.in.system_name = "\\";
3954 r.in.account_name = &account_name_p;
3955 r.in.authority_name = &authority_name_p;
3956 r.out.account_name = &account_name_p;
3958 status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
3960 authority_name_p = *r.out.authority_name;
3962 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
3963 "GetUserName failed");
3964 torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
3965 "GetUserName failed");
3967 torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
3968 ret, done, "authority_name not equal after reauth to anon");
3969 torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
3970 ret, done, "account_name not equal after reauth to anon");
3972 ret = true;
3974 done:
3975 talloc_free(mem_ctx);
3976 return ret;
3979 static bool torture_rpc_smb1_pipe_name(struct torture_context *torture)
3981 TALLOC_CTX *mem_ctx;
3982 NTSTATUS status;
3983 bool ret = false;
3984 struct smbcli_state *cli;
3985 struct smbcli_options options;
3986 struct smbcli_session_options session_options;
3987 union smb_open io;
3988 union smb_close cl;
3989 uint16_t fnum;
3991 mem_ctx = talloc_init("torture_samba3_smb1_pipe_name");
3992 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
3994 lpcfg_smbcli_options(torture->lp_ctx, &options);
3995 lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
3997 status = smbcli_full_connection(mem_ctx, &cli,
3998 torture_setting_string(torture, "host", NULL),
3999 lpcfg_smb_ports(torture->lp_ctx),
4000 "IPC$", NULL,
4001 lpcfg_socket_options(torture->lp_ctx),
4002 cmdline_credentials,
4003 lpcfg_resolve_context(torture->lp_ctx),
4004 torture->ev, &options, &session_options,
4005 lpcfg_gensec_settings(torture, torture->lp_ctx));
4006 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4007 "smbcli_full_connection failed");
4009 ZERO_STRUCT(io);
4010 io.generic.level = RAW_OPEN_NTCREATEX;
4011 io.ntcreatex.in.access_mask = DESIRED_ACCESS_PIPE;
4012 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
4013 NTCREATEX_SHARE_ACCESS_WRITE;
4014 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
4015 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
4016 io.ntcreatex.in.security_flags = 0;
4018 io.ntcreatex.in.fname = "__none__";
4019 status = smb_raw_open(cli->tree, torture, &io);
4020 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4021 ret, done,
4022 "smb_raw_open for '__none__'");
4024 io.ntcreatex.in.fname = "pipe\\srvsvc";
4025 status = smb_raw_open(cli->tree, torture, &io);
4026 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4027 ret, done,
4028 "smb_raw_open for 'pipe\\srvsvc'");
4030 io.ntcreatex.in.fname = "\\pipe\\srvsvc";
4031 status = smb_raw_open(cli->tree, torture, &io);
4032 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4033 ret, done,
4034 "smb_raw_open for '\\pipe\\srvsvc'");
4036 io.ntcreatex.in.fname = "srvsvc";
4037 status = smb_raw_open(cli->tree, torture, &io);
4038 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4039 "smb2_create failed for 'srvsvc'");
4040 fnum = io.ntcreatex.out.file.fnum;
4041 ZERO_STRUCT(cl);
4042 cl.generic.level = RAW_CLOSE_CLOSE;
4043 cl.close.in.file.fnum = fnum;
4044 status = smb_raw_close(cli->tree, &cl);
4045 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4046 "smb_raw_close failed");
4048 io.ntcreatex.in.fname = "\\srvsvc";
4049 status = smb_raw_open(cli->tree, torture, &io);
4050 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4051 "smb2_create failed for '\\srvsvc'");
4052 fnum = io.ntcreatex.out.file.fnum;
4053 ZERO_STRUCT(cl);
4054 cl.generic.level = RAW_CLOSE_CLOSE;
4055 cl.close.in.file.fnum = fnum;
4056 status = smb_raw_close(cli->tree, &cl);
4057 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4058 "smb_raw_close failed");
4060 io.ntcreatex.in.fname = "\\\\\\\\\\srvsvc";
4061 status = smb_raw_open(cli->tree, torture, &io);
4062 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4063 "smb2_create failed for '\\\\\\\\\\srvsvc'");
4064 fnum = io.ntcreatex.out.file.fnum;
4065 ZERO_STRUCT(cl);
4066 cl.generic.level = RAW_CLOSE_CLOSE;
4067 cl.close.in.file.fnum = fnum;
4068 status = smb_raw_close(cli->tree, &cl);
4069 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4070 "smb_raw_close failed");
4072 ZERO_STRUCT(io);
4073 io.generic.level = RAW_OPEN_NTTRANS_CREATE;
4074 io.nttrans.in.access_mask = DESIRED_ACCESS_PIPE;
4075 io.nttrans.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
4076 NTCREATEX_SHARE_ACCESS_WRITE;
4077 io.nttrans.in.open_disposition = NTCREATEX_DISP_OPEN;
4078 io.nttrans.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
4079 io.nttrans.in.security_flags = 0;
4081 io.nttrans.in.fname = "__none__";
4082 status = smb_raw_open(cli->tree, torture, &io);
4083 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4084 ret, done,
4085 "smb_raw_open for '__none__'");
4087 io.nttrans.in.fname = "pipe\\srvsvc";
4088 status = smb_raw_open(cli->tree, torture, &io);
4089 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4090 ret, done,
4091 "smb_raw_open for 'pipe\\srvsvc'");
4093 io.nttrans.in.fname = "\\pipe\\srvsvc";
4094 status = smb_raw_open(cli->tree, torture, &io);
4095 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4096 ret, done,
4097 "smb_raw_open for '\\pipe\\srvsvc'");
4099 io.nttrans.in.fname = "srvsvc";
4100 status = smb_raw_open(cli->tree, torture, &io);
4101 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4102 "smb2_create failed for 'srvsvc'");
4103 fnum = io.nttrans.out.file.fnum;
4104 ZERO_STRUCT(cl);
4105 cl.generic.level = RAW_CLOSE_CLOSE;
4106 cl.close.in.file.fnum = fnum;
4107 status = smb_raw_close(cli->tree, &cl);
4108 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4109 "smb_raw_close failed");
4111 io.nttrans.in.fname = "\\srvsvc";
4112 status = smb_raw_open(cli->tree, torture, &io);
4113 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4114 "smb2_create failed for '\\srvsvc'");
4115 fnum = io.nttrans.out.file.fnum;
4116 ZERO_STRUCT(cl);
4117 cl.generic.level = RAW_CLOSE_CLOSE;
4118 cl.close.in.file.fnum = fnum;
4119 status = smb_raw_close(cli->tree, &cl);
4120 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4121 "smb_raw_close failed");
4123 io.nttrans.in.fname = "\\\\\\\\\\srvsvc";
4124 status = smb_raw_open(cli->tree, torture, &io);
4125 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4126 "smb2_create failed for '\\\\\\\\\\srvsvc'");
4127 fnum = io.nttrans.out.file.fnum;
4128 ZERO_STRUCT(cl);
4129 cl.generic.level = RAW_CLOSE_CLOSE;
4130 cl.close.in.file.fnum = fnum;
4131 status = smb_raw_close(cli->tree, &cl);
4132 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4133 "smb_raw_close failed");
4135 ZERO_STRUCT(io);
4136 io.generic.level = RAW_OPEN_OPENX;
4137 io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
4138 io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
4140 io.openx.in.fname = "__none__";
4141 status = smb_raw_open(cli->tree, torture, &io);
4142 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
4143 ret, done,
4144 "smb_raw_open for '__none__'");
4146 io.openx.in.fname = "srvsvc";
4147 status = smb_raw_open(cli->tree, torture, &io);
4148 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
4149 ret, done,
4150 "smb_raw_open for 'srvsvc'");
4152 io.openx.in.fname = "\\srvsvc";
4153 status = smb_raw_open(cli->tree, torture, &io);
4154 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
4155 ret, done,
4156 "smb_raw_open for '\\srvsvc'");
4158 io.openx.in.fname = "\\pipesrvsvc";
4159 status = smb_raw_open(cli->tree, torture, &io);
4160 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
4161 ret, done,
4162 "smb_raw_open for '\\pipesrvsvc'");
4164 io.openx.in.fname = "pipe\\__none__";
4165 status = smb_raw_open(cli->tree, torture, &io);
4166 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4167 ret, done,
4168 "smb_raw_open for 'pipe\\__none__'");
4170 io.openx.in.fname = "\\pipe\\__none__";
4171 status = smb_raw_open(cli->tree, torture, &io);
4172 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4173 ret, done,
4174 "smb_raw_open for '\\pipe\\__none__'");
4176 io.openx.in.fname = "pipe\\srvsvc";
4177 status = smb_raw_open(cli->tree, torture, &io);
4178 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4179 "smb2_create failed for 'pipe\\srvsvc'");
4180 fnum = io.openx.out.file.fnum;
4181 ZERO_STRUCT(cl);
4182 cl.generic.level = RAW_CLOSE_CLOSE;
4183 cl.close.in.file.fnum = fnum;
4184 status = smb_raw_close(cli->tree, &cl);
4185 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4186 "smb_raw_close failed");
4188 io.openx.in.fname = "\\pipe\\srvsvc";
4189 status = smb_raw_open(cli->tree, torture, &io);
4190 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4191 "smb2_create failed for '\\pipe\\srvsvc'");
4192 fnum = io.openx.out.file.fnum;
4193 ZERO_STRUCT(cl);
4194 cl.generic.level = RAW_CLOSE_CLOSE;
4195 cl.close.in.file.fnum = fnum;
4196 status = smb_raw_close(cli->tree, &cl);
4197 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4198 "smb_raw_close failed");
4200 io.openx.in.fname = "\\\\\\\\\\pipe\\srvsvc";
4201 status = smb_raw_open(cli->tree, torture, &io);
4202 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4203 "smb2_create failed for '\\\\\\\\\\pipe\\srvsvc'");
4204 fnum = io.openx.out.file.fnum;
4205 ZERO_STRUCT(cl);
4206 cl.generic.level = RAW_CLOSE_CLOSE;
4207 cl.close.in.file.fnum = fnum;
4208 status = smb_raw_close(cli->tree, &cl);
4209 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4210 "smb_raw_close failed");
4212 io.openx.in.fname = "\\\\\\\\\\pipe\\\\\\\\\\srvsvc";
4213 status = smb_raw_open(cli->tree, torture, &io);
4214 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4215 "smb2_create failed for '\\\\\\\\\\pipe\\\\\\\\\\srvsvc'");
4216 fnum = io.openx.out.file.fnum;
4217 ZERO_STRUCT(cl);
4218 cl.generic.level = RAW_CLOSE_CLOSE;
4219 cl.close.in.file.fnum = fnum;
4220 status = smb_raw_close(cli->tree, &cl);
4221 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4222 "smb_raw_close failed");
4223 ret = true;
4225 done:
4226 talloc_free(mem_ctx);
4227 return ret;
4230 static bool torture_rpc_smb2_pipe_name(struct torture_context *torture)
4232 TALLOC_CTX *mem_ctx;
4233 NTSTATUS status;
4234 bool ret = false;
4235 struct smbcli_options options;
4236 const char *host = torture_setting_string(torture, "host", NULL);
4237 struct smb2_tree *tree;
4238 struct smb2_handle h;
4239 struct smb2_create io;
4241 mem_ctx = talloc_init("torture_samba3_smb2_pipe_name");
4242 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
4244 lpcfg_smbcli_options(torture->lp_ctx, &options);
4246 status = smb2_connect(mem_ctx,
4247 host,
4248 lpcfg_smb_ports(torture->lp_ctx),
4249 "IPC$",
4250 lpcfg_resolve_context(torture->lp_ctx),
4251 cmdline_credentials,
4252 &tree,
4253 torture->ev,
4254 &options,
4255 lpcfg_socket_options(torture->lp_ctx),
4256 lpcfg_gensec_settings(torture, torture->lp_ctx)
4258 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4259 "smb2_connect failed");
4261 ZERO_STRUCT(io);
4262 io.in.oplock_level = 0;
4263 io.in.desired_access = DESIRED_ACCESS_PIPE;
4264 io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
4265 io.in.file_attributes = 0;
4266 io.in.create_disposition = NTCREATEX_DISP_OPEN;
4267 io.in.share_access =
4268 NTCREATEX_SHARE_ACCESS_READ|
4269 NTCREATEX_SHARE_ACCESS_WRITE;
4270 io.in.create_options = 0;
4272 io.in.fname = "__none__";
4273 status = smb2_create(tree, tree, &io);
4274 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4275 ret, done,
4276 "smb2_create for '__none__'");
4278 io.in.fname = "\\srvsvc";
4279 status = smb2_create(tree, tree, &io);
4280 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4281 ret, done,
4282 "smb2_create for '\\srvsvc'");
4284 io.in.fname = "\\pipe\\srvsvc";
4285 status = smb2_create(tree, tree, &io);
4286 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4287 ret, done,
4288 "smb2_create for '\\pipe\\srvsvc'");
4290 io.in.fname = "pipe\\srvsvc";
4291 status = smb2_create(tree, tree, &io);
4292 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
4293 ret, done,
4294 "smb2_create for 'pipe\\srvsvc'");
4296 io.in.fname = "srvsvc";
4297 status = smb2_create(tree, tree, &io);
4298 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4299 "smb2_create failed for 'srvsvc'");
4301 h = io.out.file.handle;
4303 status = smb2_util_close(tree, h);
4304 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4305 "smb2_util_close failed");
4307 ret = true;
4308 done:
4309 talloc_free(mem_ctx);
4310 return ret;
4314 * Test behaviour of a waiting read call on a pipe when
4315 * the pipe handle is closed:
4316 * - open a pipe via smb2
4317 * - trigger a read which hangs since there is nothing to read
4318 * - close the pipe file handle
4319 * - wait for the read to return and check the status
4320 * (STATUS_PIPE_BROKEN)
4322 static bool torture_rpc_smb2_pipe_read_close(struct torture_context *torture)
4324 TALLOC_CTX *mem_ctx;
4325 NTSTATUS status;
4326 bool ret = false;
4327 struct smbcli_options options;
4328 const char *host = torture_setting_string(torture, "host", NULL);
4329 struct smb2_tree *tree;
4330 struct smb2_handle h;
4331 struct smb2_request *smb2req;
4332 struct smb2_create io;
4333 struct smb2_read rd;
4335 mem_ctx = talloc_init("torture_samba3_pipe_read_close");
4336 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
4338 lpcfg_smbcli_options(torture->lp_ctx, &options);
4340 status = smb2_connect(mem_ctx,
4341 host,
4342 lpcfg_smb_ports(torture->lp_ctx),
4343 "IPC$",
4344 lpcfg_resolve_context(torture->lp_ctx),
4345 cmdline_credentials,
4346 &tree,
4347 torture->ev,
4348 &options,
4349 lpcfg_socket_options(torture->lp_ctx),
4350 lpcfg_gensec_settings(torture, torture->lp_ctx)
4352 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4353 "smb2_connect failed");
4355 ZERO_STRUCT(io);
4356 io.in.oplock_level = 0;
4357 io.in.desired_access = DESIRED_ACCESS_PIPE;
4358 io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
4359 io.in.file_attributes = 0;
4360 io.in.create_disposition = NTCREATEX_DISP_OPEN;
4361 io.in.share_access =
4362 NTCREATEX_SHARE_ACCESS_READ|
4363 NTCREATEX_SHARE_ACCESS_WRITE;
4364 io.in.create_options = 0;
4365 io.in.fname = "lsarpc";
4367 status = smb2_create(tree, tree, &io);
4368 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4369 "smb2_create failed for 'lsarpc'");
4371 h = io.out.file.handle;
4373 ZERO_STRUCT(rd);
4374 rd.in.file.handle = h;
4375 rd.in.length = 1024;
4376 rd.in.offset = 0;
4377 rd.in.min_count = 0;
4379 smb2req = smb2_read_send(tree, &rd);
4380 torture_assert_goto(torture, (smb2req != NULL), ret, done,
4381 "smb2_read_send failed");
4383 status = smb2_util_close(tree, h);
4384 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4385 "smb2_util_close failed");
4387 status = smb2_read_recv(smb2req, mem_ctx, &rd);
4388 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_PIPE_BROKEN, ret, done,
4389 "smb2_read_recv: unexpected return code");
4391 ret = true;
4392 done:
4393 talloc_free(mem_ctx);
4394 return ret;
4398 * Test behaviour of a waiting read call on a pipe when
4399 * the tree is disconnected.
4400 * - open a pipe via smb2
4401 * - trigger a read which hangs since there is nothing to read
4402 * - do a tree disconnect
4403 * - wait for the read to return and check the status
4404 * (STATUS_PIPE_BROKEN)
4406 static bool torture_rpc_smb2_pipe_read_tdis(struct torture_context *torture)
4408 TALLOC_CTX *mem_ctx;
4409 NTSTATUS status;
4410 bool ret = false;
4411 struct smbcli_options options;
4412 const char *host = torture_setting_string(torture, "host", NULL);
4413 struct smb2_tree *tree;
4414 struct smb2_handle h;
4415 struct smb2_request *smb2req;
4416 struct smb2_create io;
4417 struct smb2_read rd;
4419 mem_ctx = talloc_init("torture_samba3_pipe_read_tdis");
4420 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
4422 lpcfg_smbcli_options(torture->lp_ctx, &options);
4424 status = smb2_connect(mem_ctx,
4425 host,
4426 lpcfg_smb_ports(torture->lp_ctx),
4427 "IPC$",
4428 lpcfg_resolve_context(torture->lp_ctx),
4429 cmdline_credentials,
4430 &tree,
4431 torture->ev,
4432 &options,
4433 lpcfg_socket_options(torture->lp_ctx),
4434 lpcfg_gensec_settings(torture, torture->lp_ctx)
4436 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4437 "smb2_connect failed");
4439 ZERO_STRUCT(io);
4440 io.in.oplock_level = 0;
4441 io.in.desired_access = DESIRED_ACCESS_PIPE;
4442 io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
4443 io.in.file_attributes = 0;
4444 io.in.create_disposition = NTCREATEX_DISP_OPEN;
4445 io.in.share_access =
4446 NTCREATEX_SHARE_ACCESS_READ|
4447 NTCREATEX_SHARE_ACCESS_WRITE;
4448 io.in.create_options = 0;
4449 io.in.fname = "lsarpc";
4451 status = smb2_create(tree, tree, &io);
4452 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4453 "smb2_create failed for 'lsarpc'");
4455 h = io.out.file.handle;
4457 ZERO_STRUCT(rd);
4458 rd.in.file.handle = h;
4459 rd.in.length = 1024;
4460 rd.in.offset = 0;
4461 rd.in.min_count = 0;
4463 smb2req = smb2_read_send(tree, &rd);
4464 torture_assert_goto(torture, (smb2req != NULL), ret, done,
4465 "smb2_read_send failed");
4467 status = smb2_tdis(tree);
4468 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4469 "smb2_tdis failed");
4471 status = smb2_read_recv(smb2req, mem_ctx, &rd);
4472 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_PIPE_BROKEN, ret, done,
4473 "smb2_read_recv: unexpected return code");
4475 ret = true;
4476 done:
4477 talloc_free(mem_ctx);
4478 return ret;
4482 * Test behaviour of a waiting read call on a pipe when
4483 * the user logs off
4484 * - open a pipe via smb2
4485 * - trigger a read which hangs since there is nothing to read
4486 * - do a logoff
4487 * - wait for the read to return and check the status
4488 * (STATUS_PIPE_BROKEN)
4490 static bool torture_rpc_smb2_pipe_read_logoff(struct torture_context *torture)
4492 TALLOC_CTX *mem_ctx;
4493 NTSTATUS status;
4494 bool ret = false;
4495 struct smbcli_options options;
4496 const char *host = torture_setting_string(torture, "host", NULL);
4497 struct smb2_tree *tree;
4498 struct smb2_handle h;
4499 struct smb2_request *smb2req;
4500 struct smb2_create io;
4501 struct smb2_read rd;
4503 mem_ctx = talloc_init("torture_samba3_pipe_read_tdis");
4504 torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
4506 lpcfg_smbcli_options(torture->lp_ctx, &options);
4508 status = smb2_connect(mem_ctx,
4509 host,
4510 lpcfg_smb_ports(torture->lp_ctx),
4511 "IPC$",
4512 lpcfg_resolve_context(torture->lp_ctx),
4513 cmdline_credentials,
4514 &tree,
4515 torture->ev,
4516 &options,
4517 lpcfg_socket_options(torture->lp_ctx),
4518 lpcfg_gensec_settings(torture, torture->lp_ctx)
4520 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4521 "smb2_connect failed");
4523 ZERO_STRUCT(io);
4524 io.in.oplock_level = 0;
4525 io.in.desired_access = DESIRED_ACCESS_PIPE;
4526 io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
4527 io.in.file_attributes = 0;
4528 io.in.create_disposition = NTCREATEX_DISP_OPEN;
4529 io.in.share_access =
4530 NTCREATEX_SHARE_ACCESS_READ|
4531 NTCREATEX_SHARE_ACCESS_WRITE;
4532 io.in.create_options = 0;
4533 io.in.fname = "lsarpc";
4535 status = smb2_create(tree, tree, &io);
4536 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4537 "smb2_create failed for 'lsarpc'");
4539 h = io.out.file.handle;
4541 ZERO_STRUCT(rd);
4542 rd.in.file.handle = h;
4543 rd.in.length = 1024;
4544 rd.in.offset = 0;
4545 rd.in.min_count = 0;
4547 smb2req = smb2_read_send(tree, &rd);
4548 torture_assert_goto(torture, (smb2req != NULL), ret, done,
4549 "smb2_read_send failed");
4551 status = smb2_logoff(tree->session);
4552 torture_assert_ntstatus_ok_goto(torture, status, ret, done,
4553 "smb2_logoff failed");
4555 status = smb2_read_recv(smb2req, mem_ctx, &rd);
4556 torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_PIPE_BROKEN, ret, done,
4557 "smb2_read_recv: unexpected return code");
4559 ret = true;
4560 done:
4561 talloc_free(mem_ctx);
4562 return ret;
4566 struct torture_suite *torture_rpc_samba3(TALLOC_CTX *mem_ctx)
4568 struct torture_suite *suite = torture_suite_create(mem_ctx, "samba3");
4570 torture_suite_add_simple_test(suite, "bind", torture_bind_samba3);
4571 torture_suite_add_simple_test(suite, "netlogon", torture_netlogon_samba3);
4572 torture_suite_add_simple_test(suite, "sessionkey", torture_samba3_sessionkey);
4573 torture_suite_add_simple_test(suite, "srvsvc", torture_samba3_rpc_srvsvc);
4574 torture_suite_add_simple_test(suite, "sharesec", torture_samba3_rpc_sharesec);
4575 torture_suite_add_simple_test(suite, "getusername", torture_samba3_rpc_getusername);
4576 torture_suite_add_simple_test(suite, "randomauth2", torture_samba3_rpc_randomauth2);
4577 torture_suite_add_simple_test(suite, "lsa", torture_samba3_rpc_lsa);
4578 torture_suite_add_simple_test(suite, "spoolss", torture_samba3_rpc_spoolss);
4579 torture_suite_add_simple_test(suite, "wkssvc", torture_samba3_rpc_wkssvc);
4580 torture_suite_add_simple_test(suite, "winreg", torture_samba3_rpc_winreg);
4581 torture_suite_add_simple_test(suite, "getaliasmembership-0", torture_samba3_getaliasmembership_0);
4582 torture_suite_add_simple_test(suite, "regconfig", torture_samba3_regconfig);
4583 torture_suite_add_simple_test(suite, "smb-reauth1", torture_rpc_smb_reauth1);
4584 torture_suite_add_simple_test(suite, "smb-reauth2", torture_rpc_smb_reauth2);
4585 torture_suite_add_simple_test(suite, "smb2-reauth1", torture_rpc_smb2_reauth1);
4586 torture_suite_add_simple_test(suite, "smb2-reauth2", torture_rpc_smb2_reauth2);
4587 torture_suite_add_simple_test(suite, "smb1-pipe-name", torture_rpc_smb1_pipe_name);
4588 torture_suite_add_simple_test(suite, "smb2-pipe-name", torture_rpc_smb2_pipe_name);
4589 torture_suite_add_simple_test(suite, "smb2-pipe-read-close", torture_rpc_smb2_pipe_read_close);
4590 torture_suite_add_simple_test(suite, "smb2-pipe-read-tdis", torture_rpc_smb2_pipe_read_tdis);
4591 torture_suite_add_simple_test(suite, "smb2-pipe-read-logoff", torture_rpc_smb2_pipe_read_logoff);
4593 suite->description = talloc_strdup(suite, "samba3 DCERPC interface tests");
4595 return suite;