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/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/rap/rap.h"
26 #include "torture/torture.h"
27 #include "torture/util.h"
28 #include "torture/rap/proto.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "librpc/gen_ndr/ndr_lsa_c.h"
31 #include "librpc/gen_ndr/ndr_samr.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "librpc/gen_ndr/ndr_netlogon.h"
34 #include "librpc/gen_ndr/ndr_netlogon_c.h"
35 #include "librpc/gen_ndr/ndr_srvsvc.h"
36 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
37 #include "librpc/gen_ndr/ndr_spoolss.h"
38 #include "librpc/gen_ndr/ndr_spoolss_c.h"
39 #include "librpc/gen_ndr/ndr_winreg.h"
40 #include "librpc/gen_ndr/ndr_winreg_c.h"
41 #include "librpc/gen_ndr/ndr_wkssvc.h"
42 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
43 #include "lib/cmdline/popt_common.h"
44 #include "librpc/rpc/dcerpc.h"
45 #include "torture/rpc/rpc.h"
46 #include "libcli/libcli.h"
47 #include "libcli/composite/composite.h"
48 #include "libcli/smb_composite/smb_composite.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "../lib/crypto/crypto.h"
51 #include "auth/ntlmssp/ntlmssp.h"
52 #include "libcli/security/proto.h"
53 #include "param/param.h"
54 #include "lib/registry/registry.h"
55 #include "libcli/resolve/resolve.h"
58 * This tests a RPC call using an invalid vuid
61 bool torture_bind_authcontext(struct torture_context *torture)
66 struct lsa_ObjectAttribute objectattr;
67 struct lsa_OpenPolicy2 openpolicy;
68 struct policy_handle handle;
69 struct lsa_Close close_handle;
70 struct smbcli_session *tmp;
71 struct smbcli_session *session2;
72 struct smbcli_state *cli;
73 struct dcerpc_pipe *lsa_pipe;
74 struct cli_credentials *anon_creds;
75 struct smb_composite_sesssetup setup;
76 struct smbcli_options options;
77 struct smbcli_session_options session_options;
79 mem_ctx = talloc_init("torture_bind_authcontext");
81 if (mem_ctx == NULL) {
82 d_printf("talloc_init failed\n");
86 lp_smbcli_options(torture->lp_ctx, &options);
87 lp_smbcli_session_options(torture->lp_ctx, &session_options);
89 status = smbcli_full_connection(mem_ctx, &cli,
90 torture_setting_string(torture, "host", NULL),
91 lp_smb_ports(torture->lp_ctx),
93 lp_socket_options(torture->lp_ctx),
95 lp_resolve_context(torture->lp_ctx),
96 torture->ev, &options, &session_options,
97 lp_iconv_convenience(torture->lp_ctx),
98 lp_gensec_settings(torture, torture->lp_ctx));
99 if (!NT_STATUS_IS_OK(status)) {
100 d_printf("smbcli_full_connection failed: %s\n",
105 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
106 lp_iconv_convenience(torture->lp_ctx));
107 if (lsa_pipe == NULL) {
108 d_printf("dcerpc_pipe_init failed\n");
112 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
113 if (!NT_STATUS_IS_OK(status)) {
114 d_printf("dcerpc_pipe_open_smb failed: %s\n",
119 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
120 if (!NT_STATUS_IS_OK(status)) {
121 d_printf("dcerpc_bind_auth_none failed: %s\n",
126 openpolicy.in.system_name =talloc_asprintf(
127 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
128 ZERO_STRUCT(objectattr);
129 openpolicy.in.attr = &objectattr;
130 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
131 openpolicy.out.handle = &handle;
133 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
135 if (!NT_STATUS_IS_OK(status)) {
136 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
141 close_handle.in.handle = &handle;
142 close_handle.out.handle = &handle;
144 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
145 if (!NT_STATUS_IS_OK(status)) {
146 d_printf("dcerpc_lsa_Close failed: %s\n",
151 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
152 if (session2 == NULL) {
153 d_printf("smbcli_session_init failed\n");
157 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
158 d_printf("create_anon_creds failed\n");
162 setup.in.sesskey = cli->transport->negotiate.sesskey;
163 setup.in.capabilities = cli->transport->negotiate.capabilities;
164 setup.in.workgroup = "";
165 setup.in.credentials = anon_creds;
166 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
168 status = smb_composite_sesssetup(session2, &setup);
169 if (!NT_STATUS_IS_OK(status)) {
170 d_printf("anon session setup failed: %s\n",
174 session2->vuid = setup.out.vuid;
176 tmp = cli->tree->session;
177 cli->tree->session = session2;
179 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
181 cli->tree->session = tmp;
182 talloc_free(lsa_pipe);
185 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
186 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
187 "expected NT_STATUS_INVALID_HANDLE\n",
194 talloc_free(mem_ctx);
199 * Bind to lsa using a specific auth method
202 static bool bindtest(struct smbcli_state *cli,
203 struct cli_credentials *credentials,
204 struct loadparm_context *lp_ctx,
205 uint8_t auth_type, uint8_t auth_level)
211 struct dcerpc_pipe *lsa_pipe;
212 struct lsa_ObjectAttribute objectattr;
213 struct lsa_OpenPolicy2 openpolicy;
214 struct lsa_QueryInfoPolicy query;
215 union lsa_PolicyInformation *info = NULL;
216 struct policy_handle handle;
217 struct lsa_Close close_handle;
219 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
220 d_printf("talloc_init failed\n");
224 lsa_pipe = dcerpc_pipe_init(mem_ctx,
225 cli->transport->socket->event.ctx,
226 lp_iconv_convenience(lp_ctx));
227 if (lsa_pipe == NULL) {
228 d_printf("dcerpc_pipe_init failed\n");
232 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
233 if (!NT_STATUS_IS_OK(status)) {
234 d_printf("dcerpc_pipe_open_smb failed: %s\n",
239 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
240 credentials, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
242 if (!NT_STATUS_IS_OK(status)) {
243 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
247 openpolicy.in.system_name =talloc_asprintf(
248 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
249 ZERO_STRUCT(objectattr);
250 openpolicy.in.attr = &objectattr;
251 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
252 openpolicy.out.handle = &handle;
254 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
256 if (!NT_STATUS_IS_OK(status)) {
257 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
262 query.in.handle = &handle;
263 query.in.level = LSA_POLICY_INFO_DOMAIN;
264 query.out.info = &info;
266 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
267 if (!NT_STATUS_IS_OK(status)) {
268 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
273 close_handle.in.handle = &handle;
274 close_handle.out.handle = &handle;
276 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
277 if (!NT_STATUS_IS_OK(status)) {
278 d_printf("dcerpc_lsa_Close failed: %s\n",
285 talloc_free(mem_ctx);
290 * test authenticated RPC binds with the variants Samba3 does support
293 bool torture_bind_samba3(struct torture_context *torture)
298 struct smbcli_state *cli;
299 struct smbcli_options options;
300 struct smbcli_session_options session_options;
302 mem_ctx = talloc_init("torture_bind_authcontext");
304 if (mem_ctx == NULL) {
305 d_printf("talloc_init failed\n");
309 lp_smbcli_options(torture->lp_ctx, &options);
310 lp_smbcli_session_options(torture->lp_ctx, &session_options);
312 status = smbcli_full_connection(mem_ctx, &cli,
313 torture_setting_string(torture, "host", NULL),
314 lp_smb_ports(torture->lp_ctx),
316 lp_socket_options(torture->lp_ctx),
318 lp_resolve_context(torture->lp_ctx),
319 torture->ev, &options, &session_options,
320 lp_iconv_convenience(torture->lp_ctx),
321 lp_gensec_settings(torture, torture->lp_ctx));
322 if (!NT_STATUS_IS_OK(status)) {
323 d_printf("smbcli_full_connection failed: %s\n",
330 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
331 DCERPC_AUTH_LEVEL_INTEGRITY);
332 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
333 DCERPC_AUTH_LEVEL_PRIVACY);
334 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
335 DCERPC_AUTH_LEVEL_INTEGRITY);
336 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
337 DCERPC_AUTH_LEVEL_PRIVACY);
340 talloc_free(mem_ctx);
345 * Lookup or create a user and return all necessary info
348 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
350 struct loadparm_context *lp_ctx,
351 struct cli_credentials *admin_creds,
354 const char *username,
356 struct dcerpc_pipe **result_pipe,
357 struct policy_handle **result_handle,
358 struct dom_sid **sid)
360 struct dcerpc_pipe *samr_pipe;
362 struct policy_handle conn_handle;
363 struct policy_handle domain_handle;
364 struct policy_handle *user_handle;
365 struct samr_Connect2 conn;
366 struct samr_EnumDomains enumdom;
367 uint32_t resume_handle = 0;
368 struct samr_LookupDomain l;
370 struct lsa_String domain_name;
371 struct lsa_String user_name;
372 struct samr_OpenDomain o;
373 struct samr_CreateUser2 c;
374 uint32_t user_rid,access_granted;
376 samr_pipe = dcerpc_pipe_init(mem_ctx,
377 cli->transport->socket->event.ctx,
378 lp_iconv_convenience(lp_ctx));
379 if (samr_pipe == NULL) {
380 d_printf("dcerpc_pipe_init failed\n");
381 status = NT_STATUS_NO_MEMORY;
385 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
386 if (!NT_STATUS_IS_OK(status)) {
387 d_printf("dcerpc_pipe_open_smb failed: %s\n",
392 if (admin_creds != NULL) {
393 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
394 admin_creds, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
396 if (!NT_STATUS_IS_OK(status)) {
397 d_printf("dcerpc_bind_auth failed: %s\n",
402 /* We must have an authenticated SMB connection */
403 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
404 if (!NT_STATUS_IS_OK(status)) {
405 d_printf("dcerpc_bind_auth_none failed: %s\n",
411 conn.in.system_name = talloc_asprintf(
412 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
413 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
414 conn.out.connect_handle = &conn_handle;
416 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
417 if (!NT_STATUS_IS_OK(status)) {
418 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
422 enumdom.in.connect_handle = &conn_handle;
423 enumdom.in.resume_handle = &resume_handle;
424 enumdom.in.buf_size = (uint32_t)-1;
425 enumdom.out.resume_handle = &resume_handle;
427 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
428 if (!NT_STATUS_IS_OK(status)) {
429 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
433 if (enumdom.out.num_entries != 2) {
434 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
435 enumdom.out.num_entries);
436 status = NT_STATUS_UNSUCCESSFUL;
440 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
443 l.in.connect_handle = &conn_handle;
444 domain_name.string = enumdom.out.sam->entries[dom_idx].name.string;
445 *domain = talloc_strdup(mem_ctx, domain_name.string);
446 l.in.domain_name = &domain_name;
448 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
449 if (!NT_STATUS_IS_OK(status)) {
450 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
454 o.in.connect_handle = &conn_handle;
455 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
456 o.in.sid = l.out.sid;
457 o.out.domain_handle = &domain_handle;
459 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
460 if (!NT_STATUS_IS_OK(status)) {
461 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
465 c.in.domain_handle = &domain_handle;
466 user_name.string = username;
467 c.in.account_name = &user_name;
468 c.in.acct_flags = ACB_NORMAL;
469 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
470 user_handle = talloc(mem_ctx, struct policy_handle);
471 c.out.user_handle = user_handle;
472 c.out.access_granted = &access_granted;
473 c.out.rid = &user_rid;
475 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
477 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
478 struct samr_LookupNames ln;
479 struct samr_OpenUser ou;
481 ln.in.domain_handle = &domain_handle;
483 ln.in.names = &user_name;
485 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
486 if (!NT_STATUS_IS_OK(status)) {
487 d_printf("samr_LookupNames failed: %s\n",
492 ou.in.domain_handle = &domain_handle;
493 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
494 user_rid = ou.in.rid = ln.out.rids.ids[0];
495 ou.out.user_handle = user_handle;
497 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
498 if (!NT_STATUS_IS_OK(status)) {
499 d_printf("samr_OpenUser failed: %s\n",
505 if (!NT_STATUS_IS_OK(status)) {
506 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
510 *result_pipe = samr_pipe;
511 *result_handle = user_handle;
513 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
525 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
526 struct loadparm_context *lp_ctx,
527 struct cli_credentials *admin_creds,
528 const char *username, const char *password,
530 struct dom_sid **user_sid)
534 struct dcerpc_pipe *samr_pipe;
535 struct policy_handle *wks_handle;
538 if (!(tmp_ctx = talloc_new(mem_ctx))) {
539 d_printf("talloc_init failed\n");
543 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
544 DCERPC_AUTH_TYPE_NTLMSSP,
545 DCERPC_AUTH_LEVEL_INTEGRITY,
546 username, domain_name, &samr_pipe, &wks_handle,
548 if (!NT_STATUS_IS_OK(status)) {
549 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
554 struct samr_SetUserInfo2 sui2;
555 struct samr_SetUserInfo sui;
556 struct samr_QueryUserInfo qui;
557 union samr_UserInfo u_info;
558 DATA_BLOB session_key;
562 encode_pw_buffer(u_info.info23.password.data, password,
565 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
566 if (!NT_STATUS_IS_OK(status)) {
567 d_printf("dcerpc_fetch_session_key failed\n");
570 arcfour_crypt_blob(u_info.info23.password.data, 516,
572 u_info.info23.info.password_expired = 0;
573 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
574 SAMR_FIELD_PASSWORD2 |
575 SAMR_FIELD_EXPIRED_FLAG;
576 sui2.in.user_handle = wks_handle;
577 sui2.in.info = &u_info;
580 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
581 if (!NT_STATUS_IS_OK(status)) {
582 d_printf("samr_SetUserInfo(23) failed: %s\n",
587 u_info.info16.acct_flags = ACB_NORMAL;
588 sui.in.user_handle = wks_handle;
589 sui.in.info = &u_info;
592 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
593 if (!NT_STATUS_IS_OK(status)) {
594 d_printf("samr_SetUserInfo(16) failed\n");
598 qui.in.user_handle = wks_handle;
601 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
602 if (!NT_STATUS_IS_OK(status)) {
603 d_printf("samr_QueryUserInfo(21) failed\n");
607 qui.out.info->info21.allow_password_change = 0;
608 qui.out.info->info21.force_password_change = 0;
609 qui.out.info->info21.account_name.string = NULL;
610 qui.out.info->info21.rid = 0;
611 qui.out.info->info21.acct_expiry = 0;
612 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
614 u_info.info21 = qui.out.info->info21;
615 sui.in.user_handle = wks_handle;
616 sui.in.info = &u_info;
619 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
620 if (!NT_STATUS_IS_OK(status)) {
621 d_printf("samr_SetUserInfo(21) failed\n");
626 *domain_name= talloc_steal(mem_ctx, *domain_name);
627 *user_sid = talloc_steal(mem_ctx, *user_sid);
630 talloc_free(tmp_ctx);
638 static bool delete_user(struct smbcli_state *cli,
639 struct loadparm_context *lp_ctx,
640 struct cli_credentials *admin_creds,
641 const char *username)
646 struct dcerpc_pipe *samr_pipe;
647 struct policy_handle *user_handle;
650 if ((mem_ctx = talloc_init("leave")) == NULL) {
651 d_printf("talloc_init failed\n");
655 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
656 DCERPC_AUTH_TYPE_NTLMSSP,
657 DCERPC_AUTH_LEVEL_INTEGRITY,
658 username, &dom_name, &samr_pipe,
661 if (!NT_STATUS_IS_OK(status)) {
662 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
667 struct samr_DeleteUser d;
669 d.in.user_handle = user_handle;
670 d.out.user_handle = user_handle;
672 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
673 if (!NT_STATUS_IS_OK(status)) {
674 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
682 talloc_free(mem_ctx);
687 * Do a Samba3-style join
690 static bool join3(struct smbcli_state *cli,
691 struct loadparm_context *lp_ctx,
693 struct cli_credentials *admin_creds,
694 struct cli_credentials *wks_creds)
699 struct dcerpc_pipe *samr_pipe;
700 struct policy_handle *wks_handle;
702 NTTIME last_password_change;
704 if ((mem_ctx = talloc_init("join3")) == NULL) {
705 d_printf("talloc_init failed\n");
709 status = get_usr_handle(
710 cli, mem_ctx, lp_ctx, admin_creds,
711 DCERPC_AUTH_TYPE_NTLMSSP,
712 DCERPC_AUTH_LEVEL_PRIVACY,
713 talloc_asprintf(mem_ctx, "%s$",
714 cli_credentials_get_workstation(wks_creds)),
715 &dom_name, &samr_pipe, &wks_handle, NULL);
717 if (!NT_STATUS_IS_OK(status)) {
718 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
723 struct samr_QueryUserInfo q;
725 q.in.user_handle = wks_handle;
728 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
729 if (!NT_STATUS_IS_OK(status)) {
730 d_printf("(%s) QueryUserInfo failed: %s\n",
731 __location__, nt_errstr(status));
735 last_password_change = q.out.info->info21.last_password_change;
738 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
741 struct samr_SetUserInfo2 sui2;
742 union samr_UserInfo u_info;
743 struct samr_UserInfo21 *i21 = &u_info.info25.info;
744 DATA_BLOB session_key;
745 DATA_BLOB confounded_session_key = data_blob_talloc(
747 struct MD5Context ctx;
748 uint8_t confounder[16];
752 i21->full_name.string = talloc_asprintf(
754 cli_credentials_get_workstation(wks_creds));
755 i21->acct_flags = ACB_WSTRUST;
756 i21->fields_present = SAMR_FIELD_FULL_NAME |
757 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
759 encode_pw_buffer(u_info.info25.password.data,
760 cli_credentials_get_password(wks_creds),
762 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
763 if (!NT_STATUS_IS_OK(status)) {
764 d_printf("dcerpc_fetch_session_key failed: %s\n",
768 generate_random_buffer((uint8_t *)confounder, 16);
771 MD5Update(&ctx, confounder, 16);
772 MD5Update(&ctx, session_key.data, session_key.length);
773 MD5Final(confounded_session_key.data, &ctx);
775 arcfour_crypt_blob(u_info.info25.password.data, 516,
776 &confounded_session_key);
777 memcpy(&u_info.info25.password.data[516], confounder, 16);
779 sui2.in.user_handle = wks_handle;
781 sui2.in.info = &u_info;
783 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
784 if (!NT_STATUS_IS_OK(status)) {
785 d_printf("samr_SetUserInfo2(25) failed: %s\n",
790 struct samr_SetUserInfo2 sui2;
791 struct samr_SetUserInfo sui;
792 union samr_UserInfo u_info;
793 DATA_BLOB session_key;
795 encode_pw_buffer(u_info.info24.password.data,
796 cli_credentials_get_password(wks_creds),
798 u_info.info24.pw_len =
799 strlen_m(cli_credentials_get_password(wks_creds))*2;
801 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
802 if (!NT_STATUS_IS_OK(status)) {
803 d_printf("dcerpc_fetch_session_key failed\n");
806 arcfour_crypt_blob(u_info.info24.password.data, 516,
808 sui2.in.user_handle = wks_handle;
809 sui2.in.info = &u_info;
812 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
813 if (!NT_STATUS_IS_OK(status)) {
814 d_printf("samr_SetUserInfo(24) failed: %s\n",
819 u_info.info16.acct_flags = ACB_WSTRUST;
820 sui.in.user_handle = wks_handle;
821 sui.in.info = &u_info;
824 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
825 if (!NT_STATUS_IS_OK(status)) {
826 d_printf("samr_SetUserInfo(16) failed\n");
832 struct samr_QueryUserInfo q;
834 q.in.user_handle = wks_handle;
837 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
838 if (!NT_STATUS_IS_OK(status)) {
839 d_printf("(%s) QueryUserInfo failed: %s\n",
840 __location__, nt_errstr(status));
845 if (last_password_change
846 == q.out.info->info21.last_password_change) {
847 d_printf("(%s) last_password_change unchanged "
848 "during join, level25 must change "
849 "it\n", __location__);
854 if (last_password_change
855 != q.out.info->info21.last_password_change) {
856 d_printf("(%s) last_password_change changed "
857 "during join, level24 doesn't "
858 "change it\n", __location__);
867 talloc_free(mem_ctx);
872 * Do a ReqChallenge/Auth2 and get the wks creds
875 static bool auth2(struct smbcli_state *cli,
876 struct loadparm_context *lp_ctx,
877 struct cli_credentials *wks_cred)
880 struct dcerpc_pipe *net_pipe;
883 struct netr_ServerReqChallenge r;
884 struct netr_Credential netr_cli_creds;
885 struct netr_Credential netr_srv_creds;
886 uint32_t negotiate_flags;
887 struct netr_ServerAuthenticate2 a;
888 struct creds_CredentialState *creds_state;
889 struct netr_Credential netr_cred;
890 struct samr_Password mach_pw;
892 mem_ctx = talloc_new(NULL);
893 if (mem_ctx == NULL) {
894 d_printf("talloc_new failed\n");
898 net_pipe = dcerpc_pipe_init(mem_ctx,
899 cli->transport->socket->event.ctx,
900 lp_iconv_convenience(lp_ctx));
901 if (net_pipe == NULL) {
902 d_printf("dcerpc_pipe_init failed\n");
906 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
907 if (!NT_STATUS_IS_OK(status)) {
908 d_printf("dcerpc_pipe_open_smb failed: %s\n",
913 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
914 if (!NT_STATUS_IS_OK(status)) {
915 d_printf("dcerpc_bind_auth_none failed: %s\n",
920 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
921 r.in.server_name = talloc_asprintf(
922 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
923 if (r.in.server_name == NULL) {
924 d_printf("talloc_asprintf failed\n");
927 generate_random_buffer(netr_cli_creds.data,
928 sizeof(netr_cli_creds.data));
929 r.in.credentials = &netr_cli_creds;
930 r.out.return_credentials = &netr_srv_creds;
932 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
933 if (!NT_STATUS_IS_OK(status)) {
934 d_printf("netr_ServerReqChallenge failed: %s\n",
939 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
940 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
942 creds_state = talloc(mem_ctx, struct creds_CredentialState);
943 creds_client_init(creds_state, r.in.credentials,
944 r.out.return_credentials, &mach_pw,
945 &netr_cred, negotiate_flags);
947 a.in.server_name = talloc_asprintf(
948 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
949 a.in.account_name = talloc_asprintf(
950 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
951 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
952 a.in.secure_channel_type = SEC_CHAN_WKSTA;
953 a.in.negotiate_flags = &negotiate_flags;
954 a.out.negotiate_flags = &negotiate_flags;
955 a.in.credentials = &netr_cred;
956 a.out.return_credentials = &netr_cred;
958 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
959 if (!NT_STATUS_IS_OK(status)) {
960 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
965 if (!creds_client_check(creds_state, a.out.return_credentials)) {
966 d_printf("creds_client_check failed\n");
970 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
975 talloc_free(mem_ctx);
980 * Do a couple of schannel protected Netlogon ops: Interactive and Network
981 * login, and change the wks password
984 static bool schan(struct smbcli_state *cli,
985 struct loadparm_context *lp_ctx,
986 struct cli_credentials *wks_creds,
987 struct cli_credentials *user_creds)
992 struct dcerpc_pipe *net_pipe;
995 mem_ctx = talloc_new(NULL);
996 if (mem_ctx == NULL) {
997 d_printf("talloc_new failed\n");
1001 net_pipe = dcerpc_pipe_init(mem_ctx,
1002 cli->transport->socket->event.ctx,
1003 lp_iconv_convenience(lp_ctx));
1004 if (net_pipe == NULL) {
1005 d_printf("dcerpc_pipe_init failed\n");
1009 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1010 if (!NT_STATUS_IS_OK(status)) {
1011 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1017 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1018 DCERPC_DEBUG_PRINT_OUT;
1021 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1022 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1023 wks_creds, lp_gensec_settings(lp_ctx, lp_ctx), DCERPC_AUTH_TYPE_SCHANNEL,
1024 DCERPC_AUTH_LEVEL_PRIVACY,
1027 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1029 if (!NT_STATUS_IS_OK(status)) {
1030 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1035 for (i=2; i<4; i++) {
1037 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1038 struct creds_CredentialState *creds_state;
1039 struct netr_Authenticator netr_auth, netr_auth2;
1040 struct netr_NetworkInfo ninfo;
1041 struct netr_PasswordInfo pinfo;
1042 struct netr_LogonSamLogon r;
1043 union netr_LogonLevel logon;
1044 union netr_Validation validation;
1045 uint8_t authoritative;
1046 struct netr_Authenticator return_authenticator;
1048 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1049 CLI_CRED_NTLMv2_AUTH;
1051 chal = data_blob_talloc(mem_ctx, NULL, 8);
1052 if (chal.data == NULL) {
1053 d_printf("data_blob_talloc failed\n");
1057 generate_random_buffer(chal.data, chal.length);
1058 names_blob = NTLMv2_generate_names_blob(
1060 cli_credentials_get_workstation(user_creds),
1061 cli_credentials_get_domain(user_creds));
1062 status = cli_credentials_get_ntlm_response(
1063 user_creds, mem_ctx, &flags, chal, names_blob,
1064 &lm_resp, &nt_resp, NULL, NULL);
1065 if (!NT_STATUS_IS_OK(status)) {
1066 d_printf("cli_credentials_get_ntlm_response failed:"
1067 " %s\n", nt_errstr(status));
1071 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1072 creds_client_authenticator(creds_state, &netr_auth);
1074 ninfo.identity_info.account_name.string =
1075 cli_credentials_get_username(user_creds);
1076 ninfo.identity_info.domain_name.string =
1077 cli_credentials_get_domain(user_creds);
1078 ninfo.identity_info.parameter_control = 0;
1079 ninfo.identity_info.logon_id_low = 0;
1080 ninfo.identity_info.logon_id_high = 0;
1081 ninfo.identity_info.workstation.string =
1082 cli_credentials_get_workstation(user_creds);
1083 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1084 ninfo.nt.length = nt_resp.length;
1085 ninfo.nt.data = nt_resp.data;
1086 ninfo.lm.length = lm_resp.length;
1087 ninfo.lm.data = lm_resp.data;
1089 logon.network = &ninfo;
1091 r.in.server_name = talloc_asprintf(
1092 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1093 ZERO_STRUCT(netr_auth2);
1094 r.in.computer_name =
1095 cli_credentials_get_workstation(wks_creds);
1096 r.in.credential = &netr_auth;
1097 r.in.return_authenticator = &netr_auth2;
1098 r.in.logon_level = 2;
1099 r.in.validation_level = i;
1100 r.in.logon = &logon;
1101 r.out.validation = &validation;
1102 r.out.authoritative = &authoritative;
1103 r.out.return_authenticator = &return_authenticator;
1105 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1106 if (!NT_STATUS_IS_OK(status)) {
1107 d_printf("netr_LogonSamLogon failed: %s\n",
1112 if ((r.out.return_authenticator == NULL) ||
1113 (!creds_client_check(creds_state,
1114 &r.out.return_authenticator->cred))) {
1115 d_printf("Credentials check failed!\n");
1119 creds_client_authenticator(creds_state, &netr_auth);
1121 pinfo.identity_info = ninfo.identity_info;
1122 ZERO_STRUCT(pinfo.lmpassword.hash);
1123 E_md4hash(cli_credentials_get_password(user_creds),
1124 pinfo.ntpassword.hash);
1125 session_key = data_blob_talloc(mem_ctx,
1126 creds_state->session_key, 16);
1127 arcfour_crypt_blob(pinfo.ntpassword.hash,
1128 sizeof(pinfo.ntpassword.hash),
1131 logon.password = &pinfo;
1133 r.in.logon_level = 1;
1134 r.in.logon = &logon;
1135 r.out.return_authenticator = &return_authenticator;
1137 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1138 if (!NT_STATUS_IS_OK(status)) {
1139 d_printf("netr_LogonSamLogon failed: %s\n",
1144 if ((r.out.return_authenticator == NULL) ||
1145 (!creds_client_check(creds_state,
1146 &r.out.return_authenticator->cred))) {
1147 d_printf("Credentials check failed!\n");
1153 struct netr_ServerPasswordSet s;
1154 char *password = generate_random_str(wks_creds, 8);
1155 struct creds_CredentialState *creds_state;
1156 struct netr_Authenticator credential, return_authenticator;
1157 struct samr_Password new_password;
1159 s.in.server_name = talloc_asprintf(
1160 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1161 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1162 s.in.account_name = talloc_asprintf(
1163 mem_ctx, "%s$", s.in.computer_name);
1164 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1165 s.in.credential = &credential;
1166 s.in.new_password = &new_password;
1167 s.out.return_authenticator = &return_authenticator;
1169 E_md4hash(password, new_password.hash);
1171 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1172 creds_des_encrypt(creds_state, &new_password);
1173 creds_client_authenticator(creds_state, &credential);
1175 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1176 if (!NT_STATUS_IS_OK(status)) {
1177 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1181 if (!creds_client_check(creds_state,
1182 &s.out.return_authenticator->cred)) {
1183 printf("Credential chaining failed\n");
1186 cli_credentials_set_password(wks_creds, password,
1192 talloc_free(mem_ctx);
1197 * Delete the wks account again
1200 static bool leave(struct smbcli_state *cli,
1201 struct loadparm_context *lp_ctx,
1202 struct cli_credentials *admin_creds,
1203 struct cli_credentials *wks_creds)
1205 char *wks_name = talloc_asprintf(
1206 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1209 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1210 talloc_free(wks_name);
1215 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1218 bool torture_netlogon_samba3(struct torture_context *torture)
1220 TALLOC_CTX *mem_ctx;
1223 struct smbcli_state *cli;
1224 struct cli_credentials *anon_creds;
1225 struct cli_credentials *wks_creds;
1226 const char *wks_name;
1228 struct smbcli_options options;
1229 struct smbcli_session_options session_options;
1231 wks_name = torture_setting_string(torture, "wksname", NULL);
1232 if (wks_name == NULL) {
1233 wks_name = get_myname();
1236 mem_ctx = talloc_init("torture_netlogon_samba3");
1238 if (mem_ctx == NULL) {
1239 d_printf("talloc_init failed\n");
1243 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1244 d_printf("create_anon_creds failed\n");
1248 lp_smbcli_options(torture->lp_ctx, &options);
1249 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1251 status = smbcli_full_connection(mem_ctx, &cli,
1252 torture_setting_string(torture, "host", NULL),
1253 lp_smb_ports(torture->lp_ctx),
1255 lp_socket_options(torture->lp_ctx),
1257 lp_resolve_context(torture->lp_ctx),
1258 torture->ev, &options, &session_options,
1259 lp_iconv_convenience(torture->lp_ctx),
1260 lp_gensec_settings(torture, torture->lp_ctx));
1261 if (!NT_STATUS_IS_OK(status)) {
1262 d_printf("smbcli_full_connection failed: %s\n",
1267 wks_creds = cli_credentials_init(mem_ctx);
1268 if (wks_creds == NULL) {
1269 d_printf("cli_credentials_init failed\n");
1273 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1274 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1275 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1276 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1277 cli_credentials_set_password(wks_creds,
1278 generate_random_str(wks_creds, 8),
1281 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1282 d_printf("join failed\n");
1286 cli_credentials_set_domain(
1287 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1290 for (i=0; i<2; i++) {
1292 /* Do this more than once, the routine "schan" changes
1293 * the workstation password using the netlogon
1294 * password change routine */
1298 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1299 d_printf("auth2 failed\n");
1303 for (j=0; j<2; j++) {
1304 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1305 d_printf("schan failed\n");
1311 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1312 d_printf("leave failed\n");
1319 talloc_free(mem_ctx);
1324 * Do a simple join, testjoin and leave using specified smb and samr
1328 static bool test_join3(struct torture_context *tctx,
1330 struct cli_credentials *smb_creds,
1331 struct cli_credentials *samr_creds,
1332 const char *wks_name)
1336 struct smbcli_state *cli;
1337 struct cli_credentials *wks_creds;
1338 struct smbcli_options options;
1339 struct smbcli_session_options session_options;
1341 lp_smbcli_options(tctx->lp_ctx, &options);
1342 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1344 status = smbcli_full_connection(tctx, &cli,
1345 torture_setting_string(tctx, "host", NULL),
1346 lp_smb_ports(tctx->lp_ctx),
1347 "IPC$", NULL, lp_socket_options(tctx->lp_ctx),
1348 smb_creds, lp_resolve_context(tctx->lp_ctx),
1349 tctx->ev, &options, &session_options,
1350 lp_iconv_convenience(tctx->lp_ctx),
1351 lp_gensec_settings(tctx, tctx->lp_ctx));
1352 if (!NT_STATUS_IS_OK(status)) {
1353 d_printf("smbcli_full_connection failed: %s\n",
1358 wks_creds = cli_credentials_init(cli);
1359 if (wks_creds == NULL) {
1360 d_printf("cli_credentials_init failed\n");
1364 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1365 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1366 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1367 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1368 cli_credentials_set_password(wks_creds,
1369 generate_random_str(wks_creds, 8),
1372 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1373 d_printf("join failed\n");
1377 cli_credentials_set_domain(
1378 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1381 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1382 d_printf("auth2 failed\n");
1386 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1387 d_printf("leave failed\n");
1400 * Test the different session key variants. Do it by joining, this uses the
1401 * session key in the setpassword routine. Test the join by doing the auth2.
1404 bool torture_samba3_sessionkey(struct torture_context *torture)
1407 struct cli_credentials *anon_creds;
1408 const char *wks_name;
1410 wks_name = torture_setting_string(torture, "wksname", get_myname());
1412 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1413 d_printf("create_anon_creds failed\n");
1417 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1421 if (!torture_setting_bool(torture, "samba3", false)) {
1423 /* Samba3 in the build farm right now does this happily. Need
1426 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1427 d_printf("join using anonymous bind on an anonymous smb "
1428 "connection succeeded -- HUH??\n");
1433 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1435 d_printf("join using ntlmssp bind on an anonymous smb "
1436 "connection failed\n");
1440 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1441 d_printf("join using anonymous bind on an authenticated smb "
1442 "connection failed\n");
1446 if (!test_join3(torture, false, cmdline_credentials,
1447 cmdline_credentials,
1449 d_printf("join using ntlmssp bind on an authenticated smb "
1450 "connection failed\n");
1455 * The following two are tests for setuserinfolevel 25
1458 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1460 d_printf("join using ntlmssp bind on an anonymous smb "
1461 "connection failed\n");
1465 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1466 d_printf("join using anonymous bind on an authenticated smb "
1467 "connection failed\n");
1477 * open pipe and bind, given an IPC$ context
1480 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1481 struct loadparm_context *lp_ctx,
1482 struct smbcli_tree *tree,
1483 const char *pipe_name,
1484 const struct ndr_interface_table *iface,
1485 struct dcerpc_pipe **p)
1487 struct dcerpc_pipe *result;
1490 if (!(result = dcerpc_pipe_init(
1491 mem_ctx, tree->session->transport->socket->event.ctx,
1492 lp_iconv_convenience(lp_ctx)))) {
1493 return NT_STATUS_NO_MEMORY;
1496 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1497 if (!NT_STATUS_IS_OK(status)) {
1498 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1500 talloc_free(result);
1504 status = dcerpc_bind_auth_none(result, iface);
1505 if (!NT_STATUS_IS_OK(status)) {
1506 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1507 talloc_free(result);
1512 return NT_STATUS_OK;
1516 * Sane wrapper around lsa_LookupNames
1519 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1520 struct dcerpc_pipe *p,
1524 struct lsa_ObjectAttribute attr;
1525 struct lsa_QosInfo qos;
1526 struct lsa_OpenPolicy2 r;
1529 struct policy_handle handle;
1530 struct lsa_LookupNames l;
1531 struct lsa_TransSidArray sids;
1532 struct lsa_RefDomainList *domains = NULL;
1533 struct lsa_String lsa_name;
1535 struct dom_sid *result;
1536 TALLOC_CTX *tmp_ctx;
1538 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1543 qos.impersonation_level = 2;
1544 qos.context_mode = 1;
1545 qos.effective_only = 0;
1548 attr.root_dir = NULL;
1549 attr.object_name = NULL;
1550 attr.attributes = 0;
1551 attr.sec_desc = NULL;
1552 attr.sec_qos = &qos;
1554 r.in.system_name = "\\";
1556 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1557 r.out.handle = &handle;
1559 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1560 if (!NT_STATUS_IS_OK(status)) {
1561 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1562 talloc_free(tmp_ctx);
1569 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1571 l.in.handle = &handle;
1573 l.in.names = &lsa_name;
1576 l.in.count = &count;
1577 l.out.count = &count;
1579 l.out.domains = &domains;
1581 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1582 if (!NT_STATUS_IS_OK(status)) {
1583 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1585 talloc_free(tmp_ctx);
1589 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1590 l.out.sids->sids[0].rid);
1592 c.in.handle = &handle;
1593 c.out.handle = &handle;
1595 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1596 if (!NT_STATUS_IS_OK(status)) {
1597 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1598 talloc_free(tmp_ctx);
1602 talloc_free(tmp_ctx);
1607 * Find out the user SID on this connection
1610 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1611 struct loadparm_context *lp_ctx,
1612 struct smbcli_tree *tree)
1614 struct dcerpc_pipe *lsa;
1615 struct lsa_GetUserName r;
1617 struct lsa_String *authority_name_p = NULL;
1618 struct lsa_String *account_name_p = NULL;
1619 struct dom_sid *result;
1621 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1622 &ndr_table_lsarpc, &lsa);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 d_printf("(%s) Could not bind to LSA: %s\n",
1625 __location__, nt_errstr(status));
1629 r.in.system_name = "\\";
1630 r.in.account_name = &account_name_p;
1631 r.in.authority_name = &authority_name_p;
1632 r.out.account_name = &account_name_p;
1634 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1636 authority_name_p = *r.out.authority_name;
1638 if (!NT_STATUS_IS_OK(status)) {
1639 printf("(%s) GetUserName failed - %s\n",
1640 __location__, nt_errstr(status));
1645 result = name2sid(mem_ctx, lsa, account_name_p->string,
1646 authority_name_p->string);
1652 static int destroy_tree(struct smbcli_tree *tree)
1654 smb_tree_disconnect(tree);
1659 * Do a tcon, given a session
1662 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1663 struct smbcli_session *session,
1664 const char *sharename,
1665 struct smbcli_tree **res)
1667 struct smbcli_tree *result;
1668 TALLOC_CTX *tmp_ctx;
1669 union smb_tcon tcon;
1672 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1673 return NT_STATUS_NO_MEMORY;
1676 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1677 talloc_free(tmp_ctx);
1678 return NT_STATUS_NO_MEMORY;
1681 tcon.generic.level = RAW_TCON_TCONX;
1682 tcon.tconx.in.flags = 0;
1683 tcon.tconx.in.password = data_blob(NULL, 0);
1684 tcon.tconx.in.path = sharename;
1685 tcon.tconx.in.device = "?????";
1687 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1688 if (!NT_STATUS_IS_OK(status)) {
1689 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1691 talloc_free(tmp_ctx);
1695 result->tid = tcon.tconx.out.tid;
1696 result = talloc_steal(mem_ctx, result);
1697 talloc_set_destructor(result, destroy_tree);
1698 talloc_free(tmp_ctx);
1700 return NT_STATUS_OK;
1704 * Test the getusername behaviour
1707 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1710 struct smbcli_state *cli;
1711 TALLOC_CTX *mem_ctx;
1713 struct dom_sid *user_sid;
1714 struct dom_sid *created_sid;
1715 struct cli_credentials *anon_creds;
1716 struct cli_credentials *user_creds;
1718 struct smbcli_options options;
1719 struct smbcli_session_options session_options;
1721 if (!(mem_ctx = talloc_new(torture))) {
1725 lp_smbcli_options(torture->lp_ctx, &options);
1726 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1728 status = smbcli_full_connection(
1729 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1730 lp_smb_ports(torture->lp_ctx),
1731 "IPC$", NULL, lp_socket_options(torture->lp_ctx), cmdline_credentials,
1732 lp_resolve_context(torture->lp_ctx),
1733 torture->ev, &options, &session_options,
1734 lp_iconv_convenience(torture->lp_ctx),
1735 lp_gensec_settings(torture, torture->lp_ctx));
1736 if (!NT_STATUS_IS_OK(status)) {
1737 d_printf("(%s) smbcli_full_connection failed: %s\n",
1738 __location__, nt_errstr(status));
1743 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1744 d_printf("(%s) whoami on auth'ed connection failed\n",
1751 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1752 d_printf("(%s) create_anon_creds failed\n", __location__);
1757 status = smbcli_full_connection(
1758 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1759 lp_smb_ports(torture->lp_ctx), "IPC$", NULL,
1760 lp_socket_options(torture->lp_ctx), anon_creds,
1761 lp_resolve_context(torture->lp_ctx),
1762 torture->ev, &options, &session_options,
1763 lp_iconv_convenience(torture->lp_ctx),
1764 lp_gensec_settings(torture, torture->lp_ctx));
1765 if (!NT_STATUS_IS_OK(status)) {
1766 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1767 __location__, nt_errstr(status));
1772 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1773 d_printf("(%s) whoami on anon connection failed\n",
1779 if (!dom_sid_equal(user_sid,
1780 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1781 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1782 "S-1-5-7", __location__,
1783 dom_sid_string(mem_ctx, user_sid));
1787 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1788 d_printf("(%s) cli_credentials_init failed\n", __location__);
1793 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1794 cli_credentials_set_username(user_creds, "torture_username",
1796 cli_credentials_set_password(user_creds,
1797 generate_random_str(user_creds, 8),
1800 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1801 cli_credentials_get_username(user_creds),
1802 cli_credentials_get_password(user_creds),
1803 &domain_name, &created_sid)) {
1804 d_printf("(%s) create_user failed\n", __location__);
1809 cli_credentials_set_domain(user_creds, domain_name,
1813 struct smbcli_session *session2;
1814 struct smb_composite_sesssetup setup;
1815 struct smbcli_tree *tree;
1817 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1818 if (session2 == NULL) {
1819 d_printf("(%s) smbcli_session_init failed\n",
1824 setup.in.sesskey = cli->transport->negotiate.sesskey;
1825 setup.in.capabilities = cli->transport->negotiate.capabilities;
1826 setup.in.workgroup = "";
1827 setup.in.credentials = user_creds;
1828 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
1830 status = smb_composite_sesssetup(session2, &setup);
1831 if (!NT_STATUS_IS_OK(status)) {
1832 d_printf("(%s) session setup with new user failed: "
1833 "%s\n", __location__, nt_errstr(status));
1837 session2->vuid = setup.out.vuid;
1839 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1841 d_printf("(%s) secondary_tcon failed\n",
1847 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1848 d_printf("(%s) whoami on user connection failed\n",
1857 d_printf("Created %s, found %s\n",
1858 dom_sid_string(mem_ctx, created_sid),
1859 dom_sid_string(mem_ctx, user_sid));
1861 if (!dom_sid_equal(created_sid, user_sid)) {
1866 if (!delete_user(cli, torture->lp_ctx,
1867 cmdline_credentials,
1868 cli_credentials_get_username(user_creds))) {
1869 d_printf("(%s) delete_user failed\n", __location__);
1874 talloc_free(mem_ctx);
1878 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1879 const char *sharename)
1882 struct srvsvc_NetShareGetInfo r;
1883 union srvsvc_NetShareInfo info;
1884 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1888 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1889 dcerpc_server_name(p));
1890 r.in.share_name = sharename;
1893 for (i=0;i<ARRAY_SIZE(levels);i++) {
1894 r.in.level = levels[i];
1896 printf("testing NetShareGetInfo level %u on share '%s'\n",
1897 r.in.level, r.in.share_name);
1899 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1900 if (!NT_STATUS_IS_OK(status)) {
1901 printf("NetShareGetInfo level %u on share '%s' failed"
1902 " - %s\n", r.in.level, r.in.share_name,
1907 if (!W_ERROR_IS_OK(r.out.result)) {
1908 printf("NetShareGetInfo level %u on share '%s' failed "
1909 "- %s\n", r.in.level, r.in.share_name,
1910 win_errstr(r.out.result));
1919 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1920 const char **one_sharename)
1923 struct srvsvc_NetShareEnum r;
1924 struct srvsvc_NetShareInfoCtr info_ctr;
1925 struct srvsvc_NetShareCtr0 c0;
1926 struct srvsvc_NetShareCtr1 c1;
1927 struct srvsvc_NetShareCtr2 c2;
1928 struct srvsvc_NetShareCtr501 c501;
1929 struct srvsvc_NetShareCtr502 c502;
1930 struct srvsvc_NetShareCtr1004 c1004;
1931 struct srvsvc_NetShareCtr1005 c1005;
1932 struct srvsvc_NetShareCtr1006 c1006;
1933 struct srvsvc_NetShareCtr1007 c1007;
1934 uint32_t totalentries = 0;
1935 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1939 ZERO_STRUCT(info_ctr);
1941 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1942 r.in.info_ctr = &info_ctr;
1943 r.in.max_buffer = (uint32_t)-1;
1944 r.in.resume_handle = NULL;
1945 r.out.totalentries = &totalentries;
1946 r.out.info_ctr = &info_ctr;
1948 for (i=0;i<ARRAY_SIZE(levels);i++) {
1949 info_ctr.level = levels[i];
1951 switch (info_ctr.level) {
1954 info_ctr.ctr.ctr0 = &c0;
1958 info_ctr.ctr.ctr1 = &c1;
1962 info_ctr.ctr.ctr2 = &c2;
1966 info_ctr.ctr.ctr501 = &c501;
1970 info_ctr.ctr.ctr502 = &c502;
1974 info_ctr.ctr.ctr1004 = &c1004;
1978 info_ctr.ctr.ctr1005 = &c1005;
1982 info_ctr.ctr.ctr1006 = &c1006;
1986 info_ctr.ctr.ctr1007 = &c1007;
1990 printf("testing NetShareEnum level %u\n", info_ctr.level);
1991 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1992 if (!NT_STATUS_IS_OK(status)) {
1993 printf("NetShareEnum level %u failed - %s\n",
1994 info_ctr.level, nt_errstr(status));
1998 if (!W_ERROR_IS_OK(r.out.result)) {
1999 printf("NetShareEnum level %u failed - %s\n",
2000 info_ctr.level, win_errstr(r.out.result));
2003 if (info_ctr.level == 0) {
2004 struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
2005 if (ctr->count > 0) {
2006 *one_sharename = ctr->array[0].name;
2014 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
2016 struct dcerpc_pipe *p;
2017 TALLOC_CTX *mem_ctx;
2019 const char *sharename = NULL;
2020 struct smbcli_state *cli;
2023 if (!(mem_ctx = talloc_new(torture))) {
2027 if (!(torture_open_connection_share(
2028 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2029 "IPC$", torture->ev))) {
2030 talloc_free(mem_ctx);
2034 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
2035 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
2036 if (!NT_STATUS_IS_OK(status)) {
2037 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2038 __location__, nt_errstr(status));
2043 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
2044 if (sharename == NULL) {
2045 printf("did not get sharename\n");
2047 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
2051 talloc_free(mem_ctx);
2056 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
2057 * NT_STATUS_NO_SAM_ACCOUNT
2060 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
2062 TALLOC_CTX *mem_ctx;
2063 struct dcerpc_pipe *net_pipe;
2065 bool result = false;
2067 struct netr_ServerReqChallenge r;
2068 struct netr_Credential netr_cli_creds;
2069 struct netr_Credential netr_srv_creds;
2070 uint32_t negotiate_flags;
2071 struct netr_ServerAuthenticate2 a;
2072 struct creds_CredentialState *creds_state;
2073 struct netr_Credential netr_cred;
2074 struct samr_Password mach_pw;
2075 struct smbcli_state *cli;
2077 if (!(mem_ctx = talloc_new(torture))) {
2078 d_printf("talloc_new failed\n");
2082 if (!(wksname = generate_random_str_list(
2083 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2084 d_printf("generate_random_str_list failed\n");
2088 if (!(torture_open_connection_share(
2090 torture, torture_setting_string(torture, "host", NULL),
2091 "IPC$", torture->ev))) {
2092 d_printf("IPC$ connection failed\n");
2096 if (!(net_pipe = dcerpc_pipe_init(
2097 mem_ctx, cli->transport->socket->event.ctx,
2098 lp_iconv_convenience(torture->lp_ctx)))) {
2099 d_printf("dcerpc_pipe_init failed\n");
2103 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2104 if (!NT_STATUS_IS_OK(status)) {
2105 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2110 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2111 if (!NT_STATUS_IS_OK(status)) {
2112 d_printf("dcerpc_bind_auth_none failed: %s\n",
2117 r.in.computer_name = wksname;
2118 r.in.server_name = talloc_asprintf(
2119 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2120 if (r.in.server_name == NULL) {
2121 d_printf("talloc_asprintf failed\n");
2124 generate_random_buffer(netr_cli_creds.data,
2125 sizeof(netr_cli_creds.data));
2126 r.in.credentials = &netr_cli_creds;
2127 r.out.return_credentials = &netr_srv_creds;
2129 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2130 if (!NT_STATUS_IS_OK(status)) {
2131 d_printf("netr_ServerReqChallenge failed: %s\n",
2136 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2137 E_md4hash("foobar", mach_pw.hash);
2139 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2140 creds_client_init(creds_state, r.in.credentials,
2141 r.out.return_credentials, &mach_pw,
2142 &netr_cred, negotiate_flags);
2144 a.in.server_name = talloc_asprintf(
2145 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2146 a.in.account_name = talloc_asprintf(
2147 mem_ctx, "%s$", wksname);
2148 a.in.computer_name = wksname;
2149 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2150 a.in.negotiate_flags = &negotiate_flags;
2151 a.out.negotiate_flags = &negotiate_flags;
2152 a.in.credentials = &netr_cred;
2153 a.out.return_credentials = &netr_cred;
2155 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2157 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2158 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2159 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2166 talloc_free(mem_ctx);
2170 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2171 struct loadparm_context *lp_ctx,
2172 struct smbcli_session *sess,
2173 const char *sharename)
2175 struct smbcli_tree *tree;
2176 TALLOC_CTX *tmp_ctx;
2177 struct dcerpc_pipe *p;
2179 struct srvsvc_NetShareGetInfo r;
2180 union srvsvc_NetShareInfo info;
2181 struct security_descriptor *result;
2183 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2184 d_printf("talloc_new failed\n");
2188 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2189 d_printf("secondary_tcon failed\n");
2190 talloc_free(tmp_ctx);
2194 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2195 &ndr_table_srvsvc, &p);
2196 if (!NT_STATUS_IS_OK(status)) {
2197 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2198 __location__, nt_errstr(status));
2199 talloc_free(tmp_ctx);
2204 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2207 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2208 dcerpc_server_name(p));
2209 r.in.share_name = sharename;
2213 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2214 if (!NT_STATUS_IS_OK(status)) {
2215 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2217 talloc_free(tmp_ctx);
2221 result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
2222 talloc_free(tmp_ctx);
2226 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2227 struct loadparm_context *lp_ctx,
2228 struct smbcli_session *sess,
2229 const char *sharename,
2230 struct security_descriptor *sd)
2232 struct smbcli_tree *tree;
2233 TALLOC_CTX *tmp_ctx;
2234 struct dcerpc_pipe *p;
2236 struct sec_desc_buf i;
2237 struct srvsvc_NetShareSetInfo r;
2238 union srvsvc_NetShareInfo info;
2241 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2242 d_printf("talloc_new failed\n");
2243 return NT_STATUS_NO_MEMORY;
2246 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2247 d_printf("secondary_tcon failed\n");
2248 talloc_free(tmp_ctx);
2249 return NT_STATUS_UNSUCCESSFUL;
2252 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2253 &ndr_table_srvsvc, &p);
2254 if (!NT_STATUS_IS_OK(status)) {
2255 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2256 __location__, nt_errstr(status));
2257 talloc_free(tmp_ctx);
2258 return NT_STATUS_UNSUCCESSFUL;
2262 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2265 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2266 dcerpc_server_name(p));
2267 r.in.share_name = sharename;
2272 r.in.parm_error = &error;
2274 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2275 if (!NT_STATUS_IS_OK(status)) {
2276 d_printf("srvsvc_NetShareSetInfo failed: %s\n",
2280 talloc_free(tmp_ctx);
2284 bool try_tcon(TALLOC_CTX *mem_ctx,
2285 struct loadparm_context *lp_ctx,
2286 struct security_descriptor *orig_sd,
2287 struct smbcli_session *session,
2288 const char *sharename, const struct dom_sid *user_sid,
2289 unsigned int access_mask, NTSTATUS expected_tcon,
2290 NTSTATUS expected_mkdir)
2292 TALLOC_CTX *tmp_ctx;
2293 struct smbcli_tree *rmdir_tree, *tree;
2294 struct dom_sid *domain_sid;
2296 struct security_descriptor *sd;
2300 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2301 d_printf("talloc_new failed\n");
2305 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2306 if (!NT_STATUS_IS_OK(status)) {
2307 d_printf("first tcon to delete dir failed\n");
2308 talloc_free(tmp_ctx);
2312 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2314 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2315 &domain_sid, &rid))) {
2316 d_printf("dom_sid_split_rid failed\n");
2317 talloc_free(tmp_ctx);
2321 sd = security_descriptor_dacl_create(
2322 tmp_ctx, 0, "S-1-5-32-544",
2323 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2325 dom_sid_string(mem_ctx, user_sid),
2326 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2328 d_printf("security_descriptor_dacl_create failed\n");
2329 talloc_free(tmp_ctx);
2333 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2334 if (!NT_STATUS_IS_OK(status)) {
2335 d_printf("custom set_sharesec failed: %s\n",
2337 talloc_free(tmp_ctx);
2341 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2342 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2343 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2349 if (!NT_STATUS_IS_OK(status)) {
2350 /* An expected non-access, no point in trying to write */
2354 status = smbcli_mkdir(tree, "sharesec_testdir");
2355 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2356 d_printf("(%s) Expected %s, got %s\n", __location__,
2357 nt_errstr(expected_mkdir), nt_errstr(status));
2362 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2364 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2365 if (!NT_STATUS_IS_OK(status)) {
2366 d_printf("custom set_sharesec failed: %s\n",
2368 talloc_free(tmp_ctx);
2372 talloc_free(tmp_ctx);
2376 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2378 TALLOC_CTX *mem_ctx;
2380 struct smbcli_state *cli;
2381 struct security_descriptor *sd;
2382 struct dom_sid *user_sid;
2384 if (!(mem_ctx = talloc_new(torture))) {
2388 if (!(torture_open_connection_share(
2389 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2390 "IPC$", torture->ev))) {
2391 d_printf("IPC$ connection failed\n");
2392 talloc_free(mem_ctx);
2396 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2397 d_printf("whoami failed\n");
2398 talloc_free(mem_ctx);
2402 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2403 torture_setting_string(torture, "share", NULL));
2405 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2406 torture_setting_string(torture, "share", NULL),
2407 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2409 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2410 torture_setting_string(torture, "share", NULL),
2411 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2412 NT_STATUS_MEDIA_WRITE_PROTECTED);
2414 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2415 torture_setting_string(torture, "share", NULL),
2416 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2418 talloc_free(mem_ctx);
2422 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2424 TALLOC_CTX *mem_ctx;
2426 struct smbcli_state *cli;
2427 struct dcerpc_pipe *p;
2428 struct policy_handle lsa_handle;
2430 struct dom_sid *domain_sid;
2432 if (!(mem_ctx = talloc_new(torture))) {
2436 if (!(torture_open_connection_share(
2437 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2438 "IPC$", torture->ev))) {
2439 d_printf("IPC$ connection failed\n");
2440 talloc_free(mem_ctx);
2444 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2445 &ndr_table_lsarpc, &p);
2446 if (!NT_STATUS_IS_OK(status)) {
2447 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2449 talloc_free(mem_ctx);
2454 struct lsa_ObjectAttribute attr;
2455 struct lsa_OpenPolicy2 o;
2456 o.in.system_name = talloc_asprintf(
2457 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2460 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2461 o.out.handle = &lsa_handle;
2462 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2463 if (!NT_STATUS_IS_OK(status)) {
2464 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2465 __location__, nt_errstr(status));
2466 talloc_free(mem_ctx);
2472 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2477 int levels[] = { 2,3,5,6 };
2479 for (i=0; i<ARRAY_SIZE(levels); i++) {
2480 struct lsa_QueryInfoPolicy r;
2481 union lsa_PolicyInformation *info = NULL;
2482 r.in.handle = &lsa_handle;
2483 r.in.level = levels[i];
2485 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2486 if (!NT_STATUS_IS_OK(status)) {
2487 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2488 "failed: %s\n", __location__,
2489 levels[i], nt_errstr(status));
2490 talloc_free(mem_ctx);
2493 if (levels[i] == 5) {
2494 domain_sid = info->account_domain.sid;
2502 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2503 struct smb_iconv_convenience *iconv_convenience,
2506 struct rap_WserverGetInfo r;
2508 char servername[17];
2511 r.in.bufsize = 0xffff;
2513 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2514 if (!NT_STATUS_IS_OK(status)) {
2518 memcpy(servername, r.out.info.info0.name, 16);
2519 servername[16] = '\0';
2521 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2522 return NT_STATUS_NO_MEMORY;
2525 return NT_STATUS_OK;
2529 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2530 struct smbcli_tree *tree,
2531 const char ***printers, int *num_printers)
2533 TALLOC_CTX *mem_ctx;
2535 struct dcerpc_pipe *p;
2536 struct srvsvc_NetShareEnum r;
2537 struct srvsvc_NetShareInfoCtr info_ctr;
2538 struct srvsvc_NetShareCtr1 c1_in;
2539 struct srvsvc_NetShareCtr1 *c1;
2540 uint32_t totalentries = 0;
2543 mem_ctx = talloc_new(ctx);
2544 if (mem_ctx == NULL) {
2545 return NT_STATUS_NO_MEMORY;
2548 status = pipe_bind_smb(mem_ctx, lp_ctx,
2549 tree, "\\srvsvc", &ndr_table_srvsvc,
2551 if (!NT_STATUS_IS_OK(status)) {
2552 d_printf("could not bind to srvsvc pipe\n");
2553 talloc_free(mem_ctx);
2559 info_ctr.ctr.ctr1 = &c1_in;
2561 r.in.server_unc = talloc_asprintf(
2562 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2563 r.in.info_ctr = &info_ctr;
2564 r.in.max_buffer = (uint32_t)-1;
2565 r.in.resume_handle = NULL;
2566 r.out.totalentries = &totalentries;
2567 r.out.info_ctr = &info_ctr;
2569 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2570 if (!NT_STATUS_IS_OK(status)) {
2571 d_printf("NetShareEnum level %u failed - %s\n",
2572 info_ctr.level, nt_errstr(status));
2573 talloc_free(mem_ctx);
2579 c1 = r.out.info_ctr->ctr.ctr1;
2580 for (i=0; i<c1->count; i++) {
2581 if (c1->array[i].type != STYPE_PRINTQ) {
2584 if (!add_string_to_array(ctx, c1->array[i].name,
2585 printers, num_printers)) {
2587 return NT_STATUS_NO_MEMORY;
2591 talloc_free(mem_ctx);
2592 return NT_STATUS_OK;
2595 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2596 const char *servername, int level, int *num_printers)
2598 struct spoolss_EnumPrinters r;
2602 r.in.flags = PRINTER_ENUM_LOCAL;
2603 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2608 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2609 if (!NT_STATUS_IS_OK(status)) {
2610 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2611 __location__, nt_errstr(status));
2615 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2616 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2617 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2618 win_errstr(r.out.result));
2622 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2623 if (blob.data == NULL) {
2624 d_printf("(%s) data_blob_talloc failed\n", __location__);
2628 r.in.buffer = &blob;
2629 r.in.offered = r.out.needed;
2631 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2632 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2633 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2634 "%s\n", __location__, nt_errstr(status),
2635 win_errstr(r.out.result));
2639 *num_printers = r.out.count;
2644 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2645 struct policy_handle *handle, int level,
2646 union spoolss_PrinterInfo **res)
2648 TALLOC_CTX *mem_ctx;
2649 struct spoolss_GetPrinter r;
2653 mem_ctx = talloc_new(ctx);
2654 if (mem_ctx == NULL) {
2655 return NT_STATUS_NO_MEMORY;
2658 r.in.handle = handle;
2663 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2664 if (!NT_STATUS_IS_OK(status)) {
2665 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2666 __location__, nt_errstr(status));
2667 talloc_free(mem_ctx);
2671 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2672 printf("GetPrinter unexpected return code %s, should "
2673 "be WERR_INSUFFICIENT_BUFFER\n",
2674 win_errstr(r.out.result));
2675 talloc_free(mem_ctx);
2676 return NT_STATUS_UNSUCCESSFUL;
2679 r.in.handle = handle;
2681 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2682 if (blob.data == NULL) {
2683 talloc_free(mem_ctx);
2684 return NT_STATUS_NO_MEMORY;
2686 memset(blob.data, 0, blob.length);
2687 r.in.buffer = &blob;
2688 r.in.offered = r.out.needed;
2690 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2691 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2692 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2693 "%s\n", __location__, nt_errstr(status),
2694 win_errstr(r.out.result));
2695 talloc_free(mem_ctx);
2696 return NT_STATUS_IS_OK(status) ?
2697 NT_STATUS_UNSUCCESSFUL : status;
2701 *res = talloc_steal(ctx, r.out.info);
2704 talloc_free(mem_ctx);
2705 return NT_STATUS_OK;
2708 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2710 TALLOC_CTX *mem_ctx;
2712 struct smbcli_state *cli;
2713 struct dcerpc_pipe *p;
2715 struct policy_handle server_handle, printer_handle;
2716 const char **printers;
2718 struct spoolss_UserLevel1 userlevel1;
2721 if (!(mem_ctx = talloc_new(torture))) {
2725 if (!(torture_open_connection_share(
2726 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2727 "IPC$", torture->ev))) {
2728 d_printf("IPC$ connection failed\n");
2729 talloc_free(mem_ctx);
2733 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2734 if (!NT_STATUS_IS_OK(status)) {
2735 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2736 __location__, nt_errstr(status));
2737 talloc_free(mem_ctx);
2741 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2742 &printers, &num_printers))) {
2743 talloc_free(mem_ctx);
2747 if (num_printers == 0) {
2748 d_printf("Did not find printers\n");
2749 talloc_free(mem_ctx);
2753 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2754 &ndr_table_spoolss, &p);
2755 if (!NT_STATUS_IS_OK(status)) {
2756 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2758 talloc_free(mem_ctx);
2762 ZERO_STRUCT(userlevel1);
2763 userlevel1.client = talloc_asprintf(
2764 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2765 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2766 userlevel1.build = 2600;
2767 userlevel1.major = 3;
2768 userlevel1.minor = 0;
2769 userlevel1.processor = 0;
2772 struct spoolss_OpenPrinterEx r;
2775 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2777 r.in.datatype = NULL;
2778 r.in.access_mask = 0;
2780 r.in.userlevel.level1 = &userlevel1;
2781 r.out.handle = &server_handle;
2783 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2784 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2785 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2786 "%s, %s\n", __location__, nt_errstr(status),
2787 win_errstr(r.out.result));
2788 talloc_free(mem_ctx);
2794 struct spoolss_ClosePrinter r;
2796 r.in.handle = &server_handle;
2797 r.out.handle = &server_handle;
2799 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2800 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2801 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2802 "%s, %s\n", __location__, nt_errstr(status),
2803 win_errstr(r.out.result));
2804 talloc_free(mem_ctx);
2810 struct spoolss_OpenPrinterEx r;
2813 r.in.printername = talloc_asprintf(
2814 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2815 r.in.datatype = NULL;
2816 r.in.access_mask = 0;
2818 r.in.userlevel.level1 = &userlevel1;
2819 r.out.handle = &printer_handle;
2821 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2822 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2823 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2824 "%s, %s\n", __location__, nt_errstr(status),
2825 win_errstr(r.out.result));
2826 talloc_free(mem_ctx);
2834 for (i=0; i<8; i++) {
2835 status = getprinterinfo(mem_ctx, p, &printer_handle,
2837 if (!NT_STATUS_IS_OK(status)) {
2838 d_printf("(%s) getprinterinfo %d failed: %s\n",
2839 __location__, i, nt_errstr(status));
2846 struct spoolss_ClosePrinter r;
2848 r.in.handle = &printer_handle;
2849 r.out.handle = &printer_handle;
2851 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2852 if (!NT_STATUS_IS_OK(status)) {
2853 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2854 "%s\n", __location__, nt_errstr(status));
2855 talloc_free(mem_ctx);
2862 if (!enumprinters(mem_ctx, p, servername, 1,
2864 d_printf("(%s) enumprinters failed\n", __location__);
2865 talloc_free(mem_ctx);
2868 if (num_printers != num_enumerated) {
2869 d_printf("(%s) netshareenum gave %d printers, "
2870 "enumprinters lvl 1 gave %d\n", __location__,
2871 num_printers, num_enumerated);
2872 talloc_free(mem_ctx);
2879 if (!enumprinters(mem_ctx, p, servername, 2,
2881 d_printf("(%s) enumprinters failed\n", __location__);
2882 talloc_free(mem_ctx);
2885 if (num_printers != num_enumerated) {
2886 d_printf("(%s) netshareenum gave %d printers, "
2887 "enumprinters lvl 2 gave %d\n", __location__,
2888 num_printers, num_enumerated);
2889 talloc_free(mem_ctx);
2894 talloc_free(mem_ctx);
2899 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2901 TALLOC_CTX *mem_ctx;
2902 struct smbcli_state *cli;
2903 struct dcerpc_pipe *p;
2907 if (!(mem_ctx = talloc_new(torture))) {
2911 if (!(torture_open_connection_share(
2912 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2913 "IPC$", torture->ev))) {
2914 d_printf("IPC$ connection failed\n");
2915 talloc_free(mem_ctx);
2919 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2920 if (!NT_STATUS_IS_OK(status)) {
2921 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2922 __location__, nt_errstr(status));
2923 talloc_free(mem_ctx);
2927 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2928 &ndr_table_wkssvc, &p);
2929 if (!NT_STATUS_IS_OK(status)) {
2930 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2932 talloc_free(mem_ctx);
2937 struct wkssvc_NetWkstaInfo100 wks100;
2938 union wkssvc_NetWkstaInfo info;
2939 struct wkssvc_NetWkstaGetInfo r;
2941 r.in.server_name = "\\foo";
2943 info.info100 = &wks100;
2946 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2947 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2948 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2949 "%s, %s\n", __location__, nt_errstr(status),
2950 win_errstr(r.out.result));
2951 talloc_free(mem_ctx);
2955 if (strcmp(servername,
2956 r.out.info->info100->server_name) != 0) {
2957 d_printf("(%s) servername inconsistency: RAP=%s, "
2958 "dcerpc_wkssvc_NetWksGetInfo=%s",
2959 __location__, servername,
2960 r.out.info->info100->server_name);
2961 talloc_free(mem_ctx);
2966 talloc_free(mem_ctx);
2970 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2971 struct policy_handle *handle)
2973 struct winreg_CloseKey c;
2975 TALLOC_CTX *mem_ctx;
2977 c.in.handle = c.out.handle = handle;
2979 if (!(mem_ctx = talloc_new(p))) {
2980 return NT_STATUS_NO_MEMORY;
2983 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2984 talloc_free(mem_ctx);
2986 if (!NT_STATUS_IS_OK(status)) {
2990 if (!W_ERROR_IS_OK(c.out.result)) {
2991 return werror_to_ntstatus(c.out.result);
2994 return NT_STATUS_OK;
2997 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2998 TALLOC_CTX *mem_ctx)
3000 uint32_t enum_index = 0;
3003 struct winreg_EnumValue r;
3004 struct winreg_StringBuf name;
3005 enum winreg_Type type = 0;
3008 uint32_t size, length;
3010 r.in.handle = handle;
3011 r.in.enum_index = enum_index;
3014 r.in.name = r.out.name = &name;
3020 r.in.length = &length;
3022 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
3023 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3024 return NT_STATUS_OK;
3030 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
3031 TALLOC_CTX *mem_ctx, int depth)
3033 struct winreg_EnumKey r;
3034 struct winreg_StringBuf class, name;
3039 return NT_STATUS_OK;
3045 r.in.handle = handle;
3046 r.in.enum_index = 0;
3048 r.in.keyclass = &class;
3050 r.in.last_changed_time = &t;
3053 TALLOC_CTX *tmp_ctx;
3054 struct winreg_OpenKey o;
3055 struct policy_handle key_handle;
3058 if (!(tmp_ctx = talloc_new(mem_ctx))) {
3059 return NT_STATUS_NO_MEMORY;
3065 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
3066 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3067 /* We're done enumerating */
3068 talloc_free(tmp_ctx);
3069 return NT_STATUS_OK;
3072 for (i=0; i<10-depth; i++)
3074 printf("%s\n", r.out.name->name);
3077 o.in.parent_handle = handle;
3078 o.in.keyname.name = r.out.name->name;
3080 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3081 o.out.handle = &key_handle;
3083 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
3084 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3085 enumkeys(p, &key_handle, tmp_ctx, depth-1);
3086 enumvalues(p, &key_handle, tmp_ctx);
3087 status = winreg_close(p, &key_handle);
3088 if (!NT_STATUS_IS_OK(status)) {
3093 talloc_free(tmp_ctx);
3095 r.in.enum_index += 1;
3098 return NT_STATUS_OK;
3101 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3103 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3104 const char *name, winreg_open_fn open_fn)
3106 struct policy_handle handle;
3107 struct winreg_OpenHKLM r;
3110 r.in.system_name = 0;
3111 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3112 r.out.handle = &handle;
3114 status = open_fn(p, mem_ctx, &r);
3115 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3116 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3117 nt_errstr(status), win_errstr(r.out.result));
3121 enumkeys(p, &handle, mem_ctx, 4);
3123 status = winreg_close(p, &handle);
3124 if (!NT_STATUS_IS_OK(status)) {
3125 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3126 __location__, nt_errstr(status));
3133 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3136 struct dcerpc_pipe *p;
3137 TALLOC_CTX *mem_ctx;
3143 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3144 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3145 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3146 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3147 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3152 mem_ctx = talloc_init("torture_rpc_winreg");
3154 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3156 if (!NT_STATUS_IS_OK(status)) {
3157 talloc_free(mem_ctx);
3162 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3164 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3165 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3170 talloc_free(mem_ctx);
3175 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3176 struct loadparm_context *lp_ctx,
3177 struct smbcli_state *cli,
3179 struct srvsvc_NetShareInfo502 **info502)
3181 struct smbcli_tree *ipc;
3182 struct dcerpc_pipe *p;
3183 struct srvsvc_NetShareGetInfo r;
3184 union srvsvc_NetShareInfo info;
3187 if (!(p = dcerpc_pipe_init(cli,
3188 cli->transport->socket->event.ctx,
3189 lp_iconv_convenience(lp_ctx)))) {
3190 status = NT_STATUS_NO_MEMORY;
3194 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3195 if (!NT_STATUS_IS_OK(status)) {
3199 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3200 if (!NT_STATUS_IS_OK(status)) {
3201 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3206 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3207 if (!NT_STATUS_IS_OK(status)) {
3208 d_printf("dcerpc_bind_auth_none failed: %s\n",
3213 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3214 dcerpc_server_name(p));
3215 r.in.share_name = share;
3219 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3220 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3221 d_printf("(%s) srvsvc_NetShareGetInfo failed: %s, %s\n", __location__,
3222 nt_errstr(status), win_errstr(r.out.result));
3226 *info502 = talloc_move(mem_ctx, &info.info502);
3227 return NT_STATUS_OK;
3235 * Get us a handle on HKLM\
3238 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3239 struct smbcli_state *cli,
3240 struct smb_iconv_convenience *iconv_convenience,
3241 struct dcerpc_pipe **pipe_p,
3242 struct policy_handle **handle)
3244 struct smbcli_tree *ipc;
3245 struct dcerpc_pipe *p;
3246 struct winreg_OpenHKLM r;
3248 struct policy_handle *result;
3250 result = talloc(mem_ctx, struct policy_handle);
3252 if (result == NULL) {
3253 return NT_STATUS_NO_MEMORY;
3256 if (!(p = dcerpc_pipe_init(result,
3257 cli->transport->socket->event.ctx,
3258 iconv_convenience))) {
3259 status = NT_STATUS_NO_MEMORY;
3263 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3264 if (!NT_STATUS_IS_OK(status)) {
3268 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3269 if (!NT_STATUS_IS_OK(status)) {
3270 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3275 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3276 if (!NT_STATUS_IS_OK(status)) {
3277 d_printf("dcerpc_bind_auth_none failed: %s\n",
3282 r.in.system_name = 0;
3283 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3284 r.out.handle = result;
3286 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3287 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3288 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3289 nt_errstr(status), win_errstr(r.out.result));
3295 return NT_STATUS_OK;
3298 talloc_free(result);
3302 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3303 struct smb_iconv_convenience *iconv_convenience,
3304 const char *sharename)
3306 struct dcerpc_pipe *p;
3307 struct policy_handle *hklm = NULL;
3308 struct policy_handle new_handle;
3309 struct winreg_CreateKey c;
3310 struct winreg_CloseKey cl;
3311 enum winreg_CreateAction action_taken;
3313 TALLOC_CTX *mem_ctx;
3315 mem_ctx = talloc_new(cli);
3316 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3318 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3319 if (!NT_STATUS_IS_OK(status)) {
3320 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3325 c.in.name.name = talloc_asprintf(
3326 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3327 if (c.in.name.name == NULL) {
3328 d_printf("talloc_asprintf failed\n");
3329 status = NT_STATUS_NO_MEMORY;
3332 c.in.keyclass.name = "";
3334 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3335 c.in.secdesc = NULL;
3336 c.in.action_taken = &action_taken;
3337 c.out.new_handle = &new_handle;
3338 c.out.action_taken = &action_taken;
3340 status = dcerpc_winreg_CreateKey(p, p, &c);
3341 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3342 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3343 nt_errstr(status), win_errstr(c.out.result));
3347 cl.in.handle = &new_handle;
3348 cl.out.handle = &new_handle;
3349 status = dcerpc_winreg_CloseKey(p, p, &cl);
3350 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3351 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3352 nt_errstr(status), win_errstr(cl.out.result));
3358 talloc_free(mem_ctx);
3362 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3363 struct smbcli_state *cli,
3364 const char *sharename)
3366 struct dcerpc_pipe *p;
3367 struct policy_handle *hklm = NULL;
3368 struct winreg_DeleteKey d;
3370 TALLOC_CTX *mem_ctx;
3372 mem_ctx = talloc_new(cli);
3373 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3375 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3377 if (!NT_STATUS_IS_OK(status)) {
3378 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3383 d.in.key.name = talloc_asprintf(
3384 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3385 if (d.in.key.name == NULL) {
3386 d_printf("talloc_asprintf failed\n");
3387 status = NT_STATUS_NO_MEMORY;
3390 status = dcerpc_winreg_DeleteKey(p, p, &d);
3391 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3392 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3393 nt_errstr(status), win_errstr(d.out.result));
3398 talloc_free(mem_ctx);
3402 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3403 struct loadparm_context *lp_ctx,
3404 const char *sharename,
3405 const char *parameter,
3408 struct dcerpc_pipe *p = NULL;
3409 struct policy_handle *hklm = NULL, key_handle;
3410 struct winreg_OpenKey o;
3411 struct winreg_SetValue s;
3416 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3417 if (!NT_STATUS_IS_OK(status)) {
3418 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3422 o.in.parent_handle = hklm;
3423 o.in.keyname.name = talloc_asprintf(
3424 hklm, "software\\samba\\smbconf\\%s", sharename);
3425 if (o.in.keyname.name == NULL) {
3426 d_printf("talloc_asprintf failed\n");
3427 status = NT_STATUS_NO_MEMORY;
3431 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3432 o.out.handle = &key_handle;
3434 status = dcerpc_winreg_OpenKey(p, p, &o);
3435 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3436 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3437 nt_errstr(status), win_errstr(o.out.result));
3441 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3442 value, &type, &val)) {
3443 d_printf("(%s) reg_string_to_val failed\n", __location__);
3447 s.in.handle = &key_handle;
3448 s.in.name.name = parameter;
3450 s.in.data = val.data;
3451 s.in.size = val.length;
3453 status = dcerpc_winreg_SetValue(p, p, &s);
3454 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3455 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3456 nt_errstr(status), win_errstr(s.out.result));
3465 bool torture_samba3_regconfig(struct torture_context *torture)
3467 struct smbcli_state *cli;
3468 struct srvsvc_NetShareInfo502 *i = NULL;
3471 const char *comment = "Dummer Kommentar";
3473 if (!(torture_open_connection(&cli, torture, 0))) {
3477 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3478 if (!NT_STATUS_IS_OK(status)) {
3479 torture_warning(torture, "torture_samba3_createshare failed: "
3480 "%s\n", nt_errstr(status));
3484 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3485 if (!NT_STATUS_IS_OK(status)) {
3486 torture_warning(torture, "torture_samba3_setconfig failed: "
3487 "%s\n", nt_errstr(status));
3491 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3492 if (!NT_STATUS_IS_OK(status)) {
3493 torture_warning(torture, "get_shareinfo failed: "
3494 "%s\n", nt_errstr(status));
3498 if (strcmp(comment, i->comment) != 0) {
3499 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3500 comment, i->comment);
3504 status = torture_samba3_deleteshare(torture, cli, "blubber");
3505 if (!NT_STATUS_IS_OK(status)) {
3506 torture_warning(torture, "torture_samba3_deleteshare failed: "
3507 "%s\n", nt_errstr(status));