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),
92 "IPC$", NULL, cmdline_credentials,
93 lp_resolve_context(torture->lp_ctx),
94 torture->ev, &options, &session_options,
95 lp_iconv_convenience(torture->lp_ctx));
96 if (!NT_STATUS_IS_OK(status)) {
97 d_printf("smbcli_full_connection failed: %s\n",
102 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
103 lp_iconv_convenience(torture->lp_ctx));
104 if (lsa_pipe == NULL) {
105 d_printf("dcerpc_pipe_init failed\n");
109 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
110 if (!NT_STATUS_IS_OK(status)) {
111 d_printf("dcerpc_pipe_open_smb failed: %s\n",
116 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
117 if (!NT_STATUS_IS_OK(status)) {
118 d_printf("dcerpc_bind_auth_none failed: %s\n",
123 openpolicy.in.system_name =talloc_asprintf(
124 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
125 ZERO_STRUCT(objectattr);
126 openpolicy.in.attr = &objectattr;
127 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
128 openpolicy.out.handle = &handle;
130 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
132 if (!NT_STATUS_IS_OK(status)) {
133 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
138 close_handle.in.handle = &handle;
139 close_handle.out.handle = &handle;
141 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
142 if (!NT_STATUS_IS_OK(status)) {
143 d_printf("dcerpc_lsa_Close failed: %s\n",
148 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
149 if (session2 == NULL) {
150 d_printf("smbcli_session_init failed\n");
154 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
155 d_printf("create_anon_creds failed\n");
159 setup.in.sesskey = cli->transport->negotiate.sesskey;
160 setup.in.capabilities = cli->transport->negotiate.capabilities;
161 setup.in.workgroup = "";
162 setup.in.credentials = anon_creds;
164 status = smb_composite_sesssetup(session2, &setup);
165 if (!NT_STATUS_IS_OK(status)) {
166 d_printf("anon session setup failed: %s\n",
170 session2->vuid = setup.out.vuid;
172 tmp = cli->tree->session;
173 cli->tree->session = session2;
175 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
177 cli->tree->session = tmp;
178 talloc_free(lsa_pipe);
181 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
182 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
183 "expected NT_STATUS_INVALID_HANDLE\n",
190 talloc_free(mem_ctx);
195 * Bind to lsa using a specific auth method
198 static bool bindtest(struct smbcli_state *cli,
199 struct cli_credentials *credentials,
200 struct loadparm_context *lp_ctx,
201 uint8_t auth_type, uint8_t auth_level)
207 struct dcerpc_pipe *lsa_pipe;
208 struct lsa_ObjectAttribute objectattr;
209 struct lsa_OpenPolicy2 openpolicy;
210 struct lsa_QueryInfoPolicy query;
211 struct policy_handle handle;
212 struct lsa_Close close_handle;
214 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
215 d_printf("talloc_init failed\n");
219 lsa_pipe = dcerpc_pipe_init(mem_ctx,
220 cli->transport->socket->event.ctx,
221 lp_iconv_convenience(lp_ctx));
222 if (lsa_pipe == NULL) {
223 d_printf("dcerpc_pipe_init failed\n");
227 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
228 if (!NT_STATUS_IS_OK(status)) {
229 d_printf("dcerpc_pipe_open_smb failed: %s\n",
234 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
235 credentials, lp_ctx, auth_type, auth_level,
237 if (!NT_STATUS_IS_OK(status)) {
238 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
242 openpolicy.in.system_name =talloc_asprintf(
243 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
244 ZERO_STRUCT(objectattr);
245 openpolicy.in.attr = &objectattr;
246 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
247 openpolicy.out.handle = &handle;
249 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
251 if (!NT_STATUS_IS_OK(status)) {
252 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
257 query.in.handle = &handle;
258 query.in.level = LSA_POLICY_INFO_DOMAIN;
260 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
261 if (!NT_STATUS_IS_OK(status)) {
262 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
267 close_handle.in.handle = &handle;
268 close_handle.out.handle = &handle;
270 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
271 if (!NT_STATUS_IS_OK(status)) {
272 d_printf("dcerpc_lsa_Close failed: %s\n",
279 talloc_free(mem_ctx);
284 * test authenticated RPC binds with the variants Samba3 does support
287 bool torture_bind_samba3(struct torture_context *torture)
292 struct smbcli_state *cli;
293 struct smbcli_options options;
294 struct smbcli_session_options session_options;
296 mem_ctx = talloc_init("torture_bind_authcontext");
298 if (mem_ctx == NULL) {
299 d_printf("talloc_init failed\n");
303 lp_smbcli_options(torture->lp_ctx, &options);
304 lp_smbcli_session_options(torture->lp_ctx, &session_options);
306 status = smbcli_full_connection(mem_ctx, &cli,
307 torture_setting_string(torture, "host", NULL),
308 lp_smb_ports(torture->lp_ctx),
309 "IPC$", NULL, cmdline_credentials,
310 lp_resolve_context(torture->lp_ctx),
311 torture->ev, &options, &session_options,
312 lp_iconv_convenience(torture->lp_ctx));
313 if (!NT_STATUS_IS_OK(status)) {
314 d_printf("smbcli_full_connection failed: %s\n",
321 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
322 DCERPC_AUTH_LEVEL_INTEGRITY);
323 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
324 DCERPC_AUTH_LEVEL_PRIVACY);
325 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
326 DCERPC_AUTH_LEVEL_INTEGRITY);
327 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
328 DCERPC_AUTH_LEVEL_PRIVACY);
331 talloc_free(mem_ctx);
336 * Lookup or create a user and return all necessary info
339 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
341 struct loadparm_context *lp_ctx,
342 struct cli_credentials *admin_creds,
345 const char *username,
347 struct dcerpc_pipe **result_pipe,
348 struct policy_handle **result_handle,
349 struct dom_sid **sid)
351 struct dcerpc_pipe *samr_pipe;
353 struct policy_handle conn_handle;
354 struct policy_handle domain_handle;
355 struct policy_handle *user_handle;
356 struct samr_Connect2 conn;
357 struct samr_EnumDomains enumdom;
358 uint32_t resume_handle = 0;
359 struct samr_LookupDomain l;
361 struct lsa_String domain_name;
362 struct lsa_String user_name;
363 struct samr_OpenDomain o;
364 struct samr_CreateUser2 c;
365 uint32_t user_rid,access_granted;
367 samr_pipe = dcerpc_pipe_init(mem_ctx,
368 cli->transport->socket->event.ctx,
369 lp_iconv_convenience(lp_ctx));
370 if (samr_pipe == NULL) {
371 d_printf("dcerpc_pipe_init failed\n");
372 status = NT_STATUS_NO_MEMORY;
376 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
377 if (!NT_STATUS_IS_OK(status)) {
378 d_printf("dcerpc_pipe_open_smb failed: %s\n",
383 if (admin_creds != NULL) {
384 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
385 admin_creds, lp_ctx, auth_type, auth_level,
387 if (!NT_STATUS_IS_OK(status)) {
388 d_printf("dcerpc_bind_auth failed: %s\n",
393 /* We must have an authenticated SMB connection */
394 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
395 if (!NT_STATUS_IS_OK(status)) {
396 d_printf("dcerpc_bind_auth_none failed: %s\n",
402 conn.in.system_name = talloc_asprintf(
403 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
404 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
405 conn.out.connect_handle = &conn_handle;
407 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
408 if (!NT_STATUS_IS_OK(status)) {
409 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
413 enumdom.in.connect_handle = &conn_handle;
414 enumdom.in.resume_handle = &resume_handle;
415 enumdom.in.buf_size = (uint32_t)-1;
416 enumdom.out.resume_handle = &resume_handle;
418 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
419 if (!NT_STATUS_IS_OK(status)) {
420 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
424 if (enumdom.out.num_entries != 2) {
425 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
426 enumdom.out.num_entries);
427 status = NT_STATUS_UNSUCCESSFUL;
431 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
434 l.in.connect_handle = &conn_handle;
435 domain_name.string = enumdom.out.sam->entries[dom_idx].name.string;
436 *domain = talloc_strdup(mem_ctx, domain_name.string);
437 l.in.domain_name = &domain_name;
439 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
440 if (!NT_STATUS_IS_OK(status)) {
441 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
445 o.in.connect_handle = &conn_handle;
446 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
447 o.in.sid = l.out.sid;
448 o.out.domain_handle = &domain_handle;
450 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
451 if (!NT_STATUS_IS_OK(status)) {
452 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
456 c.in.domain_handle = &domain_handle;
457 user_name.string = username;
458 c.in.account_name = &user_name;
459 c.in.acct_flags = ACB_NORMAL;
460 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
461 user_handle = talloc(mem_ctx, struct policy_handle);
462 c.out.user_handle = user_handle;
463 c.out.access_granted = &access_granted;
464 c.out.rid = &user_rid;
466 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
468 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
469 struct samr_LookupNames ln;
470 struct samr_OpenUser ou;
472 ln.in.domain_handle = &domain_handle;
474 ln.in.names = &user_name;
476 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
477 if (!NT_STATUS_IS_OK(status)) {
478 d_printf("samr_LookupNames failed: %s\n",
483 ou.in.domain_handle = &domain_handle;
484 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
485 user_rid = ou.in.rid = ln.out.rids.ids[0];
486 ou.out.user_handle = user_handle;
488 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
489 if (!NT_STATUS_IS_OK(status)) {
490 d_printf("samr_OpenUser failed: %s\n",
496 if (!NT_STATUS_IS_OK(status)) {
497 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
501 *result_pipe = samr_pipe;
502 *result_handle = user_handle;
504 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
516 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
517 struct loadparm_context *lp_ctx,
518 struct cli_credentials *admin_creds,
519 const char *username, const char *password,
521 struct dom_sid **user_sid)
525 struct dcerpc_pipe *samr_pipe;
526 struct policy_handle *wks_handle;
529 if (!(tmp_ctx = talloc_new(mem_ctx))) {
530 d_printf("talloc_init failed\n");
534 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
535 DCERPC_AUTH_TYPE_NTLMSSP,
536 DCERPC_AUTH_LEVEL_INTEGRITY,
537 username, domain_name, &samr_pipe, &wks_handle,
539 if (!NT_STATUS_IS_OK(status)) {
540 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
545 struct samr_SetUserInfo2 sui2;
546 struct samr_SetUserInfo sui;
547 struct samr_QueryUserInfo qui;
548 union samr_UserInfo u_info;
549 DATA_BLOB session_key;
553 encode_pw_buffer(u_info.info23.password.data, password,
556 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
557 if (!NT_STATUS_IS_OK(status)) {
558 d_printf("dcerpc_fetch_session_key failed\n");
561 arcfour_crypt_blob(u_info.info23.password.data, 516,
563 u_info.info23.info.password_expired = 0;
564 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
565 SAMR_FIELD_PASSWORD2 |
566 SAMR_FIELD_EXPIRED_FLAG;
567 sui2.in.user_handle = wks_handle;
568 sui2.in.info = &u_info;
571 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
572 if (!NT_STATUS_IS_OK(status)) {
573 d_printf("samr_SetUserInfo(23) failed: %s\n",
578 u_info.info16.acct_flags = ACB_NORMAL;
579 sui.in.user_handle = wks_handle;
580 sui.in.info = &u_info;
583 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
584 if (!NT_STATUS_IS_OK(status)) {
585 d_printf("samr_SetUserInfo(16) failed\n");
589 qui.in.user_handle = wks_handle;
592 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
593 if (!NT_STATUS_IS_OK(status)) {
594 d_printf("samr_QueryUserInfo(21) failed\n");
598 qui.out.info->info21.allow_password_change = 0;
599 qui.out.info->info21.force_password_change = 0;
600 qui.out.info->info21.account_name.string = NULL;
601 qui.out.info->info21.rid = 0;
602 qui.out.info->info21.acct_expiry = 0;
603 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
605 u_info.info21 = qui.out.info->info21;
606 sui.in.user_handle = wks_handle;
607 sui.in.info = &u_info;
610 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
611 if (!NT_STATUS_IS_OK(status)) {
612 d_printf("samr_SetUserInfo(21) failed\n");
617 *domain_name= talloc_steal(mem_ctx, *domain_name);
618 *user_sid = talloc_steal(mem_ctx, *user_sid);
621 talloc_free(tmp_ctx);
629 static bool delete_user(struct smbcli_state *cli,
630 struct loadparm_context *lp_ctx,
631 struct cli_credentials *admin_creds,
632 const char *username)
637 struct dcerpc_pipe *samr_pipe;
638 struct policy_handle *user_handle;
641 if ((mem_ctx = talloc_init("leave")) == NULL) {
642 d_printf("talloc_init failed\n");
646 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
647 DCERPC_AUTH_TYPE_NTLMSSP,
648 DCERPC_AUTH_LEVEL_INTEGRITY,
649 username, &dom_name, &samr_pipe,
652 if (!NT_STATUS_IS_OK(status)) {
653 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
658 struct samr_DeleteUser d;
660 d.in.user_handle = user_handle;
661 d.out.user_handle = user_handle;
663 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
664 if (!NT_STATUS_IS_OK(status)) {
665 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
673 talloc_free(mem_ctx);
678 * Do a Samba3-style join
681 static bool join3(struct smbcli_state *cli,
682 struct loadparm_context *lp_ctx,
684 struct cli_credentials *admin_creds,
685 struct cli_credentials *wks_creds)
690 struct dcerpc_pipe *samr_pipe;
691 struct policy_handle *wks_handle;
693 NTTIME last_password_change;
695 if ((mem_ctx = talloc_init("join3")) == NULL) {
696 d_printf("talloc_init failed\n");
700 status = get_usr_handle(
701 cli, mem_ctx, lp_ctx, admin_creds,
702 DCERPC_AUTH_TYPE_NTLMSSP,
703 DCERPC_AUTH_LEVEL_PRIVACY,
704 talloc_asprintf(mem_ctx, "%s$",
705 cli_credentials_get_workstation(wks_creds)),
706 &dom_name, &samr_pipe, &wks_handle, NULL);
708 if (!NT_STATUS_IS_OK(status)) {
709 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
714 struct samr_QueryUserInfo q;
716 q.in.user_handle = wks_handle;
719 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
720 if (!NT_STATUS_IS_OK(status)) {
721 d_printf("(%s) QueryUserInfo failed: %s\n",
722 __location__, nt_errstr(status));
726 last_password_change = q.out.info->info21.last_password_change;
729 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
732 struct samr_SetUserInfo2 sui2;
733 union samr_UserInfo u_info;
734 struct samr_UserInfo21 *i21 = &u_info.info25.info;
735 DATA_BLOB session_key;
736 DATA_BLOB confounded_session_key = data_blob_talloc(
738 struct MD5Context ctx;
739 uint8_t confounder[16];
743 i21->full_name.string = talloc_asprintf(
745 cli_credentials_get_workstation(wks_creds));
746 i21->acct_flags = ACB_WSTRUST;
747 i21->fields_present = SAMR_FIELD_FULL_NAME |
748 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
750 encode_pw_buffer(u_info.info25.password.data,
751 cli_credentials_get_password(wks_creds),
753 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
754 if (!NT_STATUS_IS_OK(status)) {
755 d_printf("dcerpc_fetch_session_key failed: %s\n",
759 generate_random_buffer((uint8_t *)confounder, 16);
762 MD5Update(&ctx, confounder, 16);
763 MD5Update(&ctx, session_key.data, session_key.length);
764 MD5Final(confounded_session_key.data, &ctx);
766 arcfour_crypt_blob(u_info.info25.password.data, 516,
767 &confounded_session_key);
768 memcpy(&u_info.info25.password.data[516], confounder, 16);
770 sui2.in.user_handle = wks_handle;
772 sui2.in.info = &u_info;
774 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
775 if (!NT_STATUS_IS_OK(status)) {
776 d_printf("samr_SetUserInfo2(25) failed: %s\n",
781 struct samr_SetUserInfo2 sui2;
782 struct samr_SetUserInfo sui;
783 union samr_UserInfo u_info;
784 DATA_BLOB session_key;
786 encode_pw_buffer(u_info.info24.password.data,
787 cli_credentials_get_password(wks_creds),
789 u_info.info24.pw_len =
790 strlen_m(cli_credentials_get_password(wks_creds))*2;
792 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
793 if (!NT_STATUS_IS_OK(status)) {
794 d_printf("dcerpc_fetch_session_key failed\n");
797 arcfour_crypt_blob(u_info.info24.password.data, 516,
799 sui2.in.user_handle = wks_handle;
800 sui2.in.info = &u_info;
803 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
804 if (!NT_STATUS_IS_OK(status)) {
805 d_printf("samr_SetUserInfo(24) failed: %s\n",
810 u_info.info16.acct_flags = ACB_WSTRUST;
811 sui.in.user_handle = wks_handle;
812 sui.in.info = &u_info;
815 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
816 if (!NT_STATUS_IS_OK(status)) {
817 d_printf("samr_SetUserInfo(16) failed\n");
823 struct samr_QueryUserInfo q;
825 q.in.user_handle = wks_handle;
828 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
829 if (!NT_STATUS_IS_OK(status)) {
830 d_printf("(%s) QueryUserInfo failed: %s\n",
831 __location__, nt_errstr(status));
836 if (last_password_change
837 == q.out.info->info21.last_password_change) {
838 d_printf("(%s) last_password_change unchanged "
839 "during join, level25 must change "
840 "it\n", __location__);
845 if (last_password_change
846 != q.out.info->info21.last_password_change) {
847 d_printf("(%s) last_password_change changed "
848 "during join, level24 doesn't "
849 "change it\n", __location__);
858 talloc_free(mem_ctx);
863 * Do a ReqChallenge/Auth2 and get the wks creds
866 static bool auth2(struct smbcli_state *cli,
867 struct loadparm_context *lp_ctx,
868 struct cli_credentials *wks_cred)
871 struct dcerpc_pipe *net_pipe;
874 struct netr_ServerReqChallenge r;
875 struct netr_Credential netr_cli_creds;
876 struct netr_Credential netr_srv_creds;
877 uint32_t negotiate_flags;
878 struct netr_ServerAuthenticate2 a;
879 struct creds_CredentialState *creds_state;
880 struct netr_Credential netr_cred;
881 struct samr_Password mach_pw;
883 mem_ctx = talloc_new(NULL);
884 if (mem_ctx == NULL) {
885 d_printf("talloc_new failed\n");
889 net_pipe = dcerpc_pipe_init(mem_ctx,
890 cli->transport->socket->event.ctx,
891 lp_iconv_convenience(lp_ctx));
892 if (net_pipe == NULL) {
893 d_printf("dcerpc_pipe_init failed\n");
897 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
898 if (!NT_STATUS_IS_OK(status)) {
899 d_printf("dcerpc_pipe_open_smb failed: %s\n",
904 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
905 if (!NT_STATUS_IS_OK(status)) {
906 d_printf("dcerpc_bind_auth_none failed: %s\n",
911 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
912 r.in.server_name = talloc_asprintf(
913 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
914 if (r.in.server_name == NULL) {
915 d_printf("talloc_asprintf failed\n");
918 generate_random_buffer(netr_cli_creds.data,
919 sizeof(netr_cli_creds.data));
920 r.in.credentials = &netr_cli_creds;
921 r.out.credentials = &netr_srv_creds;
923 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
924 if (!NT_STATUS_IS_OK(status)) {
925 d_printf("netr_ServerReqChallenge failed: %s\n",
930 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
931 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
933 creds_state = talloc(mem_ctx, struct creds_CredentialState);
934 creds_client_init(creds_state, r.in.credentials,
935 r.out.credentials, &mach_pw,
936 &netr_cred, negotiate_flags);
938 a.in.server_name = talloc_asprintf(
939 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
940 a.in.account_name = talloc_asprintf(
941 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
942 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
943 a.in.secure_channel_type = SEC_CHAN_WKSTA;
944 a.in.negotiate_flags = &negotiate_flags;
945 a.out.negotiate_flags = &negotiate_flags;
946 a.in.credentials = &netr_cred;
947 a.out.credentials = &netr_cred;
949 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
950 if (!NT_STATUS_IS_OK(status)) {
951 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
956 if (!creds_client_check(creds_state, a.out.credentials)) {
957 d_printf("creds_client_check failed\n");
961 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
966 talloc_free(mem_ctx);
971 * Do a couple of schannel protected Netlogon ops: Interactive and Network
972 * login, and change the wks password
975 static bool schan(struct smbcli_state *cli,
976 struct loadparm_context *lp_ctx,
977 struct cli_credentials *wks_creds,
978 struct cli_credentials *user_creds)
983 struct dcerpc_pipe *net_pipe;
986 mem_ctx = talloc_new(NULL);
987 if (mem_ctx == NULL) {
988 d_printf("talloc_new failed\n");
992 net_pipe = dcerpc_pipe_init(mem_ctx,
993 cli->transport->socket->event.ctx,
994 lp_iconv_convenience(lp_ctx));
995 if (net_pipe == NULL) {
996 d_printf("dcerpc_pipe_init failed\n");
1000 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1001 if (!NT_STATUS_IS_OK(status)) {
1002 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1008 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1009 DCERPC_DEBUG_PRINT_OUT;
1012 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1013 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1014 wks_creds, lp_ctx, DCERPC_AUTH_TYPE_SCHANNEL,
1015 DCERPC_AUTH_LEVEL_PRIVACY,
1018 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1026 for (i=2; i<4; i++) {
1028 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1029 struct creds_CredentialState *creds_state;
1030 struct netr_Authenticator netr_auth, netr_auth2;
1031 struct netr_NetworkInfo ninfo;
1032 struct netr_PasswordInfo pinfo;
1033 struct netr_LogonSamLogon r;
1035 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1036 CLI_CRED_NTLMv2_AUTH;
1038 chal = data_blob_talloc(mem_ctx, NULL, 8);
1039 if (chal.data == NULL) {
1040 d_printf("data_blob_talloc failed\n");
1044 generate_random_buffer(chal.data, chal.length);
1045 names_blob = NTLMv2_generate_names_blob(
1047 cli_credentials_get_workstation(user_creds),
1048 cli_credentials_get_domain(user_creds));
1049 status = cli_credentials_get_ntlm_response(
1050 user_creds, mem_ctx, &flags, chal, names_blob,
1051 &lm_resp, &nt_resp, NULL, NULL);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 d_printf("cli_credentials_get_ntlm_response failed:"
1054 " %s\n", nt_errstr(status));
1058 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1059 creds_client_authenticator(creds_state, &netr_auth);
1061 ninfo.identity_info.account_name.string =
1062 cli_credentials_get_username(user_creds);
1063 ninfo.identity_info.domain_name.string =
1064 cli_credentials_get_domain(user_creds);
1065 ninfo.identity_info.parameter_control = 0;
1066 ninfo.identity_info.logon_id_low = 0;
1067 ninfo.identity_info.logon_id_high = 0;
1068 ninfo.identity_info.workstation.string =
1069 cli_credentials_get_workstation(user_creds);
1070 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1071 ninfo.nt.length = nt_resp.length;
1072 ninfo.nt.data = nt_resp.data;
1073 ninfo.lm.length = lm_resp.length;
1074 ninfo.lm.data = lm_resp.data;
1076 r.in.server_name = talloc_asprintf(
1077 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1078 ZERO_STRUCT(netr_auth2);
1079 r.in.computer_name =
1080 cli_credentials_get_workstation(wks_creds);
1081 r.in.credential = &netr_auth;
1082 r.in.return_authenticator = &netr_auth2;
1083 r.in.logon_level = 2;
1084 r.in.validation_level = i;
1085 r.in.logon.network = &ninfo;
1086 r.out.return_authenticator = NULL;
1088 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 d_printf("netr_LogonSamLogon failed: %s\n",
1095 if ((r.out.return_authenticator == NULL) ||
1096 (!creds_client_check(creds_state,
1097 &r.out.return_authenticator->cred))) {
1098 d_printf("Credentials check failed!\n");
1102 creds_client_authenticator(creds_state, &netr_auth);
1104 pinfo.identity_info = ninfo.identity_info;
1105 ZERO_STRUCT(pinfo.lmpassword.hash);
1106 E_md4hash(cli_credentials_get_password(user_creds),
1107 pinfo.ntpassword.hash);
1108 session_key = data_blob_talloc(mem_ctx,
1109 creds_state->session_key, 16);
1110 arcfour_crypt_blob(pinfo.ntpassword.hash,
1111 sizeof(pinfo.ntpassword.hash),
1114 r.in.logon_level = 1;
1115 r.in.logon.password = &pinfo;
1116 r.out.return_authenticator = NULL;
1118 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1119 if (!NT_STATUS_IS_OK(status)) {
1120 d_printf("netr_LogonSamLogon failed: %s\n",
1125 if ((r.out.return_authenticator == NULL) ||
1126 (!creds_client_check(creds_state,
1127 &r.out.return_authenticator->cred))) {
1128 d_printf("Credentials check failed!\n");
1134 struct netr_ServerPasswordSet s;
1135 char *password = generate_random_str(wks_creds, 8);
1136 struct creds_CredentialState *creds_state;
1138 s.in.server_name = talloc_asprintf(
1139 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1140 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1141 s.in.account_name = talloc_asprintf(
1142 mem_ctx, "%s$", s.in.computer_name);
1143 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1144 E_md4hash(password, s.in.new_password.hash);
1146 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1147 creds_des_encrypt(creds_state, &s.in.new_password);
1148 creds_client_authenticator(creds_state, &s.in.credential);
1150 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1151 if (!NT_STATUS_IS_OK(status)) {
1152 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1156 if (!creds_client_check(creds_state,
1157 &s.out.return_authenticator.cred)) {
1158 printf("Credential chaining failed\n");
1161 cli_credentials_set_password(wks_creds, password,
1167 talloc_free(mem_ctx);
1172 * Delete the wks account again
1175 static bool leave(struct smbcli_state *cli,
1176 struct loadparm_context *lp_ctx,
1177 struct cli_credentials *admin_creds,
1178 struct cli_credentials *wks_creds)
1180 char *wks_name = talloc_asprintf(
1181 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1184 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1185 talloc_free(wks_name);
1190 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1193 bool torture_netlogon_samba3(struct torture_context *torture)
1195 TALLOC_CTX *mem_ctx;
1198 struct smbcli_state *cli;
1199 struct cli_credentials *anon_creds;
1200 struct cli_credentials *wks_creds;
1201 const char *wks_name;
1203 struct smbcli_options options;
1204 struct smbcli_session_options session_options;
1206 wks_name = torture_setting_string(torture, "wksname", NULL);
1207 if (wks_name == NULL) {
1208 wks_name = get_myname();
1211 mem_ctx = talloc_init("torture_netlogon_samba3");
1213 if (mem_ctx == NULL) {
1214 d_printf("talloc_init failed\n");
1218 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1219 d_printf("create_anon_creds failed\n");
1223 lp_smbcli_options(torture->lp_ctx, &options);
1224 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1226 status = smbcli_full_connection(mem_ctx, &cli,
1227 torture_setting_string(torture, "host", NULL),
1228 lp_smb_ports(torture->lp_ctx),
1229 "IPC$", NULL, anon_creds,
1230 lp_resolve_context(torture->lp_ctx),
1231 torture->ev, &options, &session_options,
1232 lp_iconv_convenience(torture->lp_ctx));
1233 if (!NT_STATUS_IS_OK(status)) {
1234 d_printf("smbcli_full_connection failed: %s\n",
1239 wks_creds = cli_credentials_init(mem_ctx);
1240 if (wks_creds == NULL) {
1241 d_printf("cli_credentials_init failed\n");
1245 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1246 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1247 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1248 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1249 cli_credentials_set_password(wks_creds,
1250 generate_random_str(wks_creds, 8),
1253 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1254 d_printf("join failed\n");
1258 cli_credentials_set_domain(
1259 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1262 for (i=0; i<2; i++) {
1264 /* Do this more than once, the routine "schan" changes
1265 * the workstation password using the netlogon
1266 * password change routine */
1270 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1271 d_printf("auth2 failed\n");
1275 for (j=0; j<2; j++) {
1276 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1277 d_printf("schan failed\n");
1283 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1284 d_printf("leave failed\n");
1291 talloc_free(mem_ctx);
1296 * Do a simple join, testjoin and leave using specified smb and samr
1300 static bool test_join3(struct torture_context *tctx,
1302 struct cli_credentials *smb_creds,
1303 struct cli_credentials *samr_creds,
1304 const char *wks_name)
1308 struct smbcli_state *cli;
1309 struct cli_credentials *wks_creds;
1310 struct smbcli_options options;
1311 struct smbcli_session_options session_options;
1313 lp_smbcli_options(tctx->lp_ctx, &options);
1314 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1316 status = smbcli_full_connection(tctx, &cli,
1317 torture_setting_string(tctx, "host", NULL),
1318 lp_smb_ports(tctx->lp_ctx),
1319 "IPC$", NULL, smb_creds,
1320 lp_resolve_context(tctx->lp_ctx),
1321 tctx->ev, &options, &session_options,
1322 lp_iconv_convenience(tctx->lp_ctx));
1323 if (!NT_STATUS_IS_OK(status)) {
1324 d_printf("smbcli_full_connection failed: %s\n",
1329 wks_creds = cli_credentials_init(cli);
1330 if (wks_creds == NULL) {
1331 d_printf("cli_credentials_init failed\n");
1335 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1336 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1337 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1338 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1339 cli_credentials_set_password(wks_creds,
1340 generate_random_str(wks_creds, 8),
1343 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1344 d_printf("join failed\n");
1348 cli_credentials_set_domain(
1349 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1352 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1353 d_printf("auth2 failed\n");
1357 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1358 d_printf("leave failed\n");
1371 * Test the different session key variants. Do it by joining, this uses the
1372 * session key in the setpassword routine. Test the join by doing the auth2.
1375 bool torture_samba3_sessionkey(struct torture_context *torture)
1378 struct cli_credentials *anon_creds;
1379 const char *wks_name;
1381 wks_name = torture_setting_string(torture, "wksname", get_myname());
1383 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1384 d_printf("create_anon_creds failed\n");
1388 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1392 if (!torture_setting_bool(torture, "samba3", false)) {
1394 /* Samba3 in the build farm right now does this happily. Need
1397 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1398 d_printf("join using anonymous bind on an anonymous smb "
1399 "connection succeeded -- HUH??\n");
1404 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1406 d_printf("join using ntlmssp bind on an anonymous smb "
1407 "connection failed\n");
1411 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1412 d_printf("join using anonymous bind on an authenticated smb "
1413 "connection failed\n");
1417 if (!test_join3(torture, false, cmdline_credentials,
1418 cmdline_credentials,
1420 d_printf("join using ntlmssp bind on an authenticated smb "
1421 "connection failed\n");
1426 * The following two are tests for setuserinfolevel 25
1429 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1431 d_printf("join using ntlmssp bind on an anonymous smb "
1432 "connection failed\n");
1436 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1437 d_printf("join using anonymous bind on an authenticated smb "
1438 "connection failed\n");
1448 * open pipe and bind, given an IPC$ context
1451 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1452 struct loadparm_context *lp_ctx,
1453 struct smbcli_tree *tree,
1454 const char *pipe_name,
1455 const struct ndr_interface_table *iface,
1456 struct dcerpc_pipe **p)
1458 struct dcerpc_pipe *result;
1461 if (!(result = dcerpc_pipe_init(
1462 mem_ctx, tree->session->transport->socket->event.ctx,
1463 lp_iconv_convenience(lp_ctx)))) {
1464 return NT_STATUS_NO_MEMORY;
1467 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1468 if (!NT_STATUS_IS_OK(status)) {
1469 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1471 talloc_free(result);
1475 status = dcerpc_bind_auth_none(result, iface);
1476 if (!NT_STATUS_IS_OK(status)) {
1477 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1478 talloc_free(result);
1483 return NT_STATUS_OK;
1487 * Sane wrapper around lsa_LookupNames
1490 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1491 struct dcerpc_pipe *p,
1495 struct lsa_ObjectAttribute attr;
1496 struct lsa_QosInfo qos;
1497 struct lsa_OpenPolicy2 r;
1500 struct policy_handle handle;
1501 struct lsa_LookupNames l;
1502 struct lsa_TransSidArray sids;
1503 struct lsa_String lsa_name;
1505 struct dom_sid *result;
1506 TALLOC_CTX *tmp_ctx;
1508 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1513 qos.impersonation_level = 2;
1514 qos.context_mode = 1;
1515 qos.effective_only = 0;
1518 attr.root_dir = NULL;
1519 attr.object_name = NULL;
1520 attr.attributes = 0;
1521 attr.sec_desc = NULL;
1522 attr.sec_qos = &qos;
1524 r.in.system_name = "\\";
1526 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1527 r.out.handle = &handle;
1529 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1530 if (!NT_STATUS_IS_OK(status)) {
1531 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1532 talloc_free(tmp_ctx);
1539 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1541 l.in.handle = &handle;
1543 l.in.names = &lsa_name;
1546 l.in.count = &count;
1547 l.out.count = &count;
1550 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1551 if (!NT_STATUS_IS_OK(status)) {
1552 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1554 talloc_free(tmp_ctx);
1558 result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1559 l.out.sids->sids[0].rid);
1561 c.in.handle = &handle;
1562 c.out.handle = &handle;
1564 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1565 if (!NT_STATUS_IS_OK(status)) {
1566 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1567 talloc_free(tmp_ctx);
1571 talloc_free(tmp_ctx);
1576 * Find out the user SID on this connection
1579 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1580 struct loadparm_context *lp_ctx,
1581 struct smbcli_tree *tree)
1583 struct dcerpc_pipe *lsa;
1584 struct lsa_GetUserName r;
1586 struct lsa_StringPointer authority_name_p;
1587 struct dom_sid *result;
1589 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1590 &ndr_table_lsarpc, &lsa);
1591 if (!NT_STATUS_IS_OK(status)) {
1592 d_printf("(%s) Could not bind to LSA: %s\n",
1593 __location__, nt_errstr(status));
1597 r.in.system_name = "\\";
1598 r.in.account_name = NULL;
1599 authority_name_p.string = NULL;
1600 r.in.authority_name = &authority_name_p;
1602 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1604 if (!NT_STATUS_IS_OK(status)) {
1605 printf("(%s) GetUserName failed - %s\n",
1606 __location__, nt_errstr(status));
1611 result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1612 r.out.authority_name->string->string);
1618 static int destroy_tree(struct smbcli_tree *tree)
1620 smb_tree_disconnect(tree);
1625 * Do a tcon, given a session
1628 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1629 struct smbcli_session *session,
1630 const char *sharename,
1631 struct smbcli_tree **res)
1633 struct smbcli_tree *result;
1634 TALLOC_CTX *tmp_ctx;
1635 union smb_tcon tcon;
1638 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1639 return NT_STATUS_NO_MEMORY;
1642 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1643 talloc_free(tmp_ctx);
1644 return NT_STATUS_NO_MEMORY;
1647 tcon.generic.level = RAW_TCON_TCONX;
1648 tcon.tconx.in.flags = 0;
1649 tcon.tconx.in.password = data_blob(NULL, 0);
1650 tcon.tconx.in.path = sharename;
1651 tcon.tconx.in.device = "?????";
1653 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1654 if (!NT_STATUS_IS_OK(status)) {
1655 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1657 talloc_free(tmp_ctx);
1661 result->tid = tcon.tconx.out.tid;
1662 result = talloc_steal(mem_ctx, result);
1663 talloc_set_destructor(result, destroy_tree);
1664 talloc_free(tmp_ctx);
1666 return NT_STATUS_OK;
1670 * Test the getusername behaviour
1673 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1676 struct smbcli_state *cli;
1677 TALLOC_CTX *mem_ctx;
1679 struct dom_sid *user_sid;
1680 struct dom_sid *created_sid;
1681 struct cli_credentials *anon_creds;
1682 struct cli_credentials *user_creds;
1684 struct smbcli_options options;
1685 struct smbcli_session_options session_options;
1687 if (!(mem_ctx = talloc_new(torture))) {
1691 lp_smbcli_options(torture->lp_ctx, &options);
1692 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1694 status = smbcli_full_connection(
1695 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1696 lp_smb_ports(torture->lp_ctx),
1697 "IPC$", NULL, cmdline_credentials,
1698 lp_resolve_context(torture->lp_ctx),
1699 torture->ev, &options, &session_options,
1700 lp_iconv_convenience(torture->lp_ctx));
1701 if (!NT_STATUS_IS_OK(status)) {
1702 d_printf("(%s) smbcli_full_connection failed: %s\n",
1703 __location__, nt_errstr(status));
1708 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1709 d_printf("(%s) whoami on auth'ed connection failed\n",
1716 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1717 d_printf("(%s) create_anon_creds failed\n", __location__);
1722 status = smbcli_full_connection(
1723 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1724 lp_smb_ports(torture->lp_ctx),
1725 "IPC$", NULL, anon_creds,
1726 lp_resolve_context(torture->lp_ctx),
1727 torture->ev, &options, &session_options,
1728 lp_iconv_convenience(torture->lp_ctx));
1729 if (!NT_STATUS_IS_OK(status)) {
1730 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1731 __location__, nt_errstr(status));
1736 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1737 d_printf("(%s) whoami on anon connection failed\n",
1743 if (!dom_sid_equal(user_sid,
1744 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1745 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1746 "S-1-5-7", __location__,
1747 dom_sid_string(mem_ctx, user_sid));
1751 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1752 d_printf("(%s) cli_credentials_init failed\n", __location__);
1757 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1758 cli_credentials_set_username(user_creds, "torture_username",
1760 cli_credentials_set_password(user_creds,
1761 generate_random_str(user_creds, 8),
1764 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1765 cli_credentials_get_username(user_creds),
1766 cli_credentials_get_password(user_creds),
1767 &domain_name, &created_sid)) {
1768 d_printf("(%s) create_user failed\n", __location__);
1773 cli_credentials_set_domain(user_creds, domain_name,
1777 struct smbcli_session *session2;
1778 struct smb_composite_sesssetup setup;
1779 struct smbcli_tree *tree;
1781 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1782 if (session2 == NULL) {
1783 d_printf("(%s) smbcli_session_init failed\n",
1788 setup.in.sesskey = cli->transport->negotiate.sesskey;
1789 setup.in.capabilities = cli->transport->negotiate.capabilities;
1790 setup.in.workgroup = "";
1791 setup.in.credentials = user_creds;
1793 status = smb_composite_sesssetup(session2, &setup);
1794 if (!NT_STATUS_IS_OK(status)) {
1795 d_printf("(%s) session setup with new user failed: "
1796 "%s\n", __location__, nt_errstr(status));
1800 session2->vuid = setup.out.vuid;
1802 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1804 d_printf("(%s) secondary_tcon failed\n",
1810 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1811 d_printf("(%s) whoami on user connection failed\n",
1820 d_printf("Created %s, found %s\n",
1821 dom_sid_string(mem_ctx, created_sid),
1822 dom_sid_string(mem_ctx, user_sid));
1824 if (!dom_sid_equal(created_sid, user_sid)) {
1829 if (!delete_user(cli, torture->lp_ctx,
1830 cmdline_credentials,
1831 cli_credentials_get_username(user_creds))) {
1832 d_printf("(%s) delete_user failed\n", __location__);
1837 talloc_free(mem_ctx);
1841 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1842 const char *sharename)
1845 struct srvsvc_NetShareGetInfo r;
1846 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1850 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1851 dcerpc_server_name(p));
1852 r.in.share_name = sharename;
1854 for (i=0;i<ARRAY_SIZE(levels);i++) {
1855 r.in.level = levels[i];
1859 printf("testing NetShareGetInfo level %u on share '%s'\n",
1860 r.in.level, r.in.share_name);
1862 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1863 if (!NT_STATUS_IS_OK(status)) {
1864 printf("NetShareGetInfo level %u on share '%s' failed"
1865 " - %s\n", r.in.level, r.in.share_name,
1870 if (!W_ERROR_IS_OK(r.out.result)) {
1871 printf("NetShareGetInfo level %u on share '%s' failed "
1872 "- %s\n", r.in.level, r.in.share_name,
1873 win_errstr(r.out.result));
1882 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1883 const char **one_sharename)
1886 struct srvsvc_NetShareEnum r;
1887 struct srvsvc_NetShareCtr0 c0;
1888 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1892 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1893 r.in.ctr.ctr0 = &c0;
1894 r.in.ctr.ctr0->count = 0;
1895 r.in.ctr.ctr0->array = NULL;
1896 r.in.max_buffer = (uint32_t)-1;
1897 r.in.resume_handle = NULL;
1899 for (i=0;i<ARRAY_SIZE(levels);i++) {
1900 r.in.level = levels[i];
1904 printf("testing NetShareEnum level %u\n", r.in.level);
1905 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1906 if (!NT_STATUS_IS_OK(status)) {
1907 printf("NetShareEnum level %u failed - %s\n",
1908 r.in.level, nt_errstr(status));
1912 if (!W_ERROR_IS_OK(r.out.result)) {
1913 printf("NetShareEnum level %u failed - %s\n",
1914 r.in.level, win_errstr(r.out.result));
1917 if (r.in.level == 0) {
1918 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1919 if (ctr->count > 0) {
1920 *one_sharename = ctr->array[0].name;
1928 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
1930 struct dcerpc_pipe *p;
1931 TALLOC_CTX *mem_ctx;
1933 const char *sharename = NULL;
1934 struct smbcli_state *cli;
1937 if (!(mem_ctx = talloc_new(torture))) {
1941 if (!(torture_open_connection_share(
1942 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
1943 "IPC$", torture->ev))) {
1944 talloc_free(mem_ctx);
1948 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
1949 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
1950 if (!NT_STATUS_IS_OK(status)) {
1951 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1952 __location__, nt_errstr(status));
1957 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1958 if (sharename == NULL) {
1959 printf("did not get sharename\n");
1961 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1965 talloc_free(mem_ctx);
1970 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1971 * NT_STATUS_NO_SAM_ACCOUNT
1974 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1976 TALLOC_CTX *mem_ctx;
1977 struct dcerpc_pipe *net_pipe;
1979 bool result = false;
1981 struct netr_ServerReqChallenge r;
1982 struct netr_Credential netr_cli_creds;
1983 struct netr_Credential netr_srv_creds;
1984 uint32_t negotiate_flags;
1985 struct netr_ServerAuthenticate2 a;
1986 struct creds_CredentialState *creds_state;
1987 struct netr_Credential netr_cred;
1988 struct samr_Password mach_pw;
1989 struct smbcli_state *cli;
1991 if (!(mem_ctx = talloc_new(torture))) {
1992 d_printf("talloc_new failed\n");
1996 if (!(wksname = generate_random_str_list(
1997 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
1998 d_printf("generate_random_str_list failed\n");
2002 if (!(torture_open_connection_share(
2004 torture, torture_setting_string(torture, "host", NULL),
2005 "IPC$", torture->ev))) {
2006 d_printf("IPC$ connection failed\n");
2010 if (!(net_pipe = dcerpc_pipe_init(
2011 mem_ctx, cli->transport->socket->event.ctx,
2012 lp_iconv_convenience(torture->lp_ctx)))) {
2013 d_printf("dcerpc_pipe_init failed\n");
2017 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2018 if (!NT_STATUS_IS_OK(status)) {
2019 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2024 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2025 if (!NT_STATUS_IS_OK(status)) {
2026 d_printf("dcerpc_bind_auth_none failed: %s\n",
2031 r.in.computer_name = wksname;
2032 r.in.server_name = talloc_asprintf(
2033 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2034 if (r.in.server_name == NULL) {
2035 d_printf("talloc_asprintf failed\n");
2038 generate_random_buffer(netr_cli_creds.data,
2039 sizeof(netr_cli_creds.data));
2040 r.in.credentials = &netr_cli_creds;
2041 r.out.credentials = &netr_srv_creds;
2043 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2044 if (!NT_STATUS_IS_OK(status)) {
2045 d_printf("netr_ServerReqChallenge failed: %s\n",
2050 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2051 E_md4hash("foobar", mach_pw.hash);
2053 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2054 creds_client_init(creds_state, r.in.credentials,
2055 r.out.credentials, &mach_pw,
2056 &netr_cred, negotiate_flags);
2058 a.in.server_name = talloc_asprintf(
2059 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2060 a.in.account_name = talloc_asprintf(
2061 mem_ctx, "%s$", wksname);
2062 a.in.computer_name = wksname;
2063 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2064 a.in.negotiate_flags = &negotiate_flags;
2065 a.out.negotiate_flags = &negotiate_flags;
2066 a.in.credentials = &netr_cred;
2067 a.out.credentials = &netr_cred;
2069 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2071 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2072 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2073 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2080 talloc_free(mem_ctx);
2084 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2085 struct loadparm_context *lp_ctx,
2086 struct smbcli_session *sess,
2087 const char *sharename)
2089 struct smbcli_tree *tree;
2090 TALLOC_CTX *tmp_ctx;
2091 struct dcerpc_pipe *p;
2093 struct srvsvc_NetShareGetInfo r;
2094 struct security_descriptor *result;
2096 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2097 d_printf("talloc_new failed\n");
2101 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2102 d_printf("secondary_tcon failed\n");
2103 talloc_free(tmp_ctx);
2107 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2108 &ndr_table_srvsvc, &p);
2109 if (!NT_STATUS_IS_OK(status)) {
2110 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2111 __location__, nt_errstr(status));
2112 talloc_free(tmp_ctx);
2117 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2120 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2121 dcerpc_server_name(p));
2122 r.in.share_name = sharename;
2125 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2126 if (!NT_STATUS_IS_OK(status)) {
2127 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2129 talloc_free(tmp_ctx);
2133 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2134 talloc_free(tmp_ctx);
2138 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2139 struct loadparm_context *lp_ctx,
2140 struct smbcli_session *sess,
2141 const char *sharename,
2142 struct security_descriptor *sd)
2144 struct smbcli_tree *tree;
2145 TALLOC_CTX *tmp_ctx;
2146 struct dcerpc_pipe *p;
2148 struct sec_desc_buf i;
2149 struct srvsvc_NetShareSetInfo r;
2152 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2153 d_printf("talloc_new failed\n");
2154 return NT_STATUS_NO_MEMORY;
2157 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2158 d_printf("secondary_tcon failed\n");
2159 talloc_free(tmp_ctx);
2160 return NT_STATUS_UNSUCCESSFUL;
2163 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2164 &ndr_table_srvsvc, &p);
2165 if (!NT_STATUS_IS_OK(status)) {
2166 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2167 __location__, nt_errstr(status));
2168 talloc_free(tmp_ctx);
2169 return NT_STATUS_UNSUCCESSFUL;
2173 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2176 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2177 dcerpc_server_name(p));
2178 r.in.share_name = sharename;
2181 r.in.info.info1501 = &i;
2182 r.in.parm_error = &error;
2184 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2185 if (!NT_STATUS_IS_OK(status)) {
2186 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2190 talloc_free(tmp_ctx);
2194 bool try_tcon(TALLOC_CTX *mem_ctx,
2195 struct loadparm_context *lp_ctx,
2196 struct security_descriptor *orig_sd,
2197 struct smbcli_session *session,
2198 const char *sharename, const struct dom_sid *user_sid,
2199 unsigned int access_mask, NTSTATUS expected_tcon,
2200 NTSTATUS expected_mkdir)
2202 TALLOC_CTX *tmp_ctx;
2203 struct smbcli_tree *rmdir_tree, *tree;
2204 struct dom_sid *domain_sid;
2206 struct security_descriptor *sd;
2210 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2211 d_printf("talloc_new failed\n");
2215 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2216 if (!NT_STATUS_IS_OK(status)) {
2217 d_printf("first tcon to delete dir failed\n");
2218 talloc_free(tmp_ctx);
2222 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2224 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2225 &domain_sid, &rid))) {
2226 d_printf("dom_sid_split_rid failed\n");
2227 talloc_free(tmp_ctx);
2231 sd = security_descriptor_dacl_create(
2232 tmp_ctx, 0, "S-1-5-32-544",
2233 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2235 dom_sid_string(mem_ctx, user_sid),
2236 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2238 d_printf("security_descriptor_dacl_create failed\n");
2239 talloc_free(tmp_ctx);
2243 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2244 if (!NT_STATUS_IS_OK(status)) {
2245 d_printf("custom set_sharesec failed: %s\n",
2247 talloc_free(tmp_ctx);
2251 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2252 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2253 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2259 if (!NT_STATUS_IS_OK(status)) {
2260 /* An expected non-access, no point in trying to write */
2264 status = smbcli_mkdir(tree, "sharesec_testdir");
2265 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2266 d_printf("(%s) Expected %s, got %s\n", __location__,
2267 nt_errstr(expected_mkdir), nt_errstr(status));
2272 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2274 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2275 if (!NT_STATUS_IS_OK(status)) {
2276 d_printf("custom set_sharesec failed: %s\n",
2278 talloc_free(tmp_ctx);
2282 talloc_free(tmp_ctx);
2286 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2288 TALLOC_CTX *mem_ctx;
2290 struct smbcli_state *cli;
2291 struct security_descriptor *sd;
2292 struct dom_sid *user_sid;
2294 if (!(mem_ctx = talloc_new(torture))) {
2298 if (!(torture_open_connection_share(
2299 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2300 "IPC$", torture->ev))) {
2301 d_printf("IPC$ connection failed\n");
2302 talloc_free(mem_ctx);
2306 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2307 d_printf("whoami failed\n");
2308 talloc_free(mem_ctx);
2312 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2313 torture_setting_string(torture, "share", NULL));
2315 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2316 torture_setting_string(torture, "share", NULL),
2317 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2319 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2320 torture_setting_string(torture, "share", NULL),
2321 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2322 NT_STATUS_MEDIA_WRITE_PROTECTED);
2324 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2325 torture_setting_string(torture, "share", NULL),
2326 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2328 talloc_free(mem_ctx);
2332 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2334 TALLOC_CTX *mem_ctx;
2336 struct smbcli_state *cli;
2337 struct dcerpc_pipe *p;
2338 struct policy_handle lsa_handle;
2340 struct dom_sid *domain_sid;
2342 if (!(mem_ctx = talloc_new(torture))) {
2346 if (!(torture_open_connection_share(
2347 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2348 "IPC$", torture->ev))) {
2349 d_printf("IPC$ connection failed\n");
2350 talloc_free(mem_ctx);
2354 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2355 &ndr_table_lsarpc, &p);
2356 if (!NT_STATUS_IS_OK(status)) {
2357 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2359 talloc_free(mem_ctx);
2364 struct lsa_ObjectAttribute attr;
2365 struct lsa_OpenPolicy2 o;
2366 o.in.system_name = talloc_asprintf(
2367 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2370 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2371 o.out.handle = &lsa_handle;
2372 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2373 if (!NT_STATUS_IS_OK(status)) {
2374 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2375 __location__, nt_errstr(status));
2376 talloc_free(mem_ctx);
2382 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2387 int levels[] = { 2,3,5,6 };
2389 for (i=0; i<ARRAY_SIZE(levels); i++) {
2390 struct lsa_QueryInfoPolicy r;
2391 r.in.handle = &lsa_handle;
2392 r.in.level = levels[i];
2393 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2394 if (!NT_STATUS_IS_OK(status)) {
2395 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2396 "failed: %s\n", __location__,
2397 levels[i], nt_errstr(status));
2398 talloc_free(mem_ctx);
2401 if (levels[i] == 5) {
2402 domain_sid = r.out.info->account_domain.sid;
2410 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2411 struct smb_iconv_convenience *iconv_convenience,
2414 struct rap_WserverGetInfo r;
2416 char servername[17];
2419 r.in.bufsize = 0xffff;
2421 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2422 if (!NT_STATUS_IS_OK(status)) {
2426 memcpy(servername, r.out.info.info0.name, 16);
2427 servername[16] = '\0';
2429 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2430 return NT_STATUS_NO_MEMORY;
2433 return NT_STATUS_OK;
2437 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2438 struct smbcli_tree *tree,
2439 const char ***printers, int *num_printers)
2441 TALLOC_CTX *mem_ctx;
2443 struct dcerpc_pipe *p;
2444 struct srvsvc_NetShareEnum r;
2445 struct srvsvc_NetShareCtr1 c1_in;
2446 struct srvsvc_NetShareCtr1 *c1;
2449 mem_ctx = talloc_new(ctx);
2450 if (mem_ctx == NULL) {
2451 return NT_STATUS_NO_MEMORY;
2454 status = pipe_bind_smb(mem_ctx, lp_ctx,
2455 tree, "\\srvsvc", &ndr_table_srvsvc,
2457 if (!NT_STATUS_IS_OK(status)) {
2458 d_printf("could not bind to srvsvc pipe\n");
2459 talloc_free(mem_ctx);
2463 r.in.server_unc = talloc_asprintf(
2464 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2467 r.in.ctr.ctr1 = &c1_in;
2468 r.in.max_buffer = (uint32_t)-1;
2469 r.in.resume_handle = NULL;
2471 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2472 if (!NT_STATUS_IS_OK(status)) {
2473 d_printf("NetShareEnum level %u failed - %s\n",
2474 r.in.level, nt_errstr(status));
2475 talloc_free(mem_ctx);
2481 c1 = r.out.ctr.ctr1;
2482 for (i=0; i<c1->count; i++) {
2483 if (c1->array[i].type != STYPE_PRINTQ) {
2486 if (!add_string_to_array(ctx, c1->array[i].name,
2487 printers, num_printers)) {
2489 return NT_STATUS_NO_MEMORY;
2493 talloc_free(mem_ctx);
2494 return NT_STATUS_OK;
2497 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2498 const char *servername, int level, int *num_printers)
2500 struct spoolss_EnumPrinters r;
2504 r.in.flags = PRINTER_ENUM_LOCAL;
2505 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2510 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2511 if (!NT_STATUS_IS_OK(status)) {
2512 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2513 __location__, nt_errstr(status));
2517 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2518 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2519 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2520 win_errstr(r.out.result));
2524 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2525 if (blob.data == NULL) {
2526 d_printf("(%s) data_blob_talloc failed\n", __location__);
2530 r.in.buffer = &blob;
2531 r.in.offered = r.out.needed;
2533 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2534 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2535 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2536 "%s\n", __location__, nt_errstr(status),
2537 win_errstr(r.out.result));
2541 *num_printers = r.out.count;
2546 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2547 struct policy_handle *handle, int level,
2548 union spoolss_PrinterInfo **res)
2550 TALLOC_CTX *mem_ctx;
2551 struct spoolss_GetPrinter r;
2555 mem_ctx = talloc_new(ctx);
2556 if (mem_ctx == NULL) {
2557 return NT_STATUS_NO_MEMORY;
2560 r.in.handle = handle;
2565 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2566 if (!NT_STATUS_IS_OK(status)) {
2567 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2568 __location__, nt_errstr(status));
2569 talloc_free(mem_ctx);
2573 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2574 printf("GetPrinter unexpected return code %s, should "
2575 "be WERR_INSUFFICIENT_BUFFER\n",
2576 win_errstr(r.out.result));
2577 talloc_free(mem_ctx);
2578 return NT_STATUS_UNSUCCESSFUL;
2581 r.in.handle = handle;
2583 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2584 if (blob.data == NULL) {
2585 talloc_free(mem_ctx);
2586 return NT_STATUS_NO_MEMORY;
2588 memset(blob.data, 0, blob.length);
2589 r.in.buffer = &blob;
2590 r.in.offered = r.out.needed;
2592 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2593 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2594 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2595 "%s\n", __location__, nt_errstr(status),
2596 win_errstr(r.out.result));
2597 talloc_free(mem_ctx);
2598 return NT_STATUS_IS_OK(status) ?
2599 NT_STATUS_UNSUCCESSFUL : status;
2603 *res = talloc_steal(ctx, r.out.info);
2606 talloc_free(mem_ctx);
2607 return NT_STATUS_OK;
2610 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2612 TALLOC_CTX *mem_ctx;
2614 struct smbcli_state *cli;
2615 struct dcerpc_pipe *p;
2617 struct policy_handle server_handle, printer_handle;
2618 const char **printers;
2620 struct spoolss_UserLevel1 userlevel1;
2623 if (!(mem_ctx = talloc_new(torture))) {
2627 if (!(torture_open_connection_share(
2628 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2629 "IPC$", torture->ev))) {
2630 d_printf("IPC$ connection failed\n");
2631 talloc_free(mem_ctx);
2635 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2636 if (!NT_STATUS_IS_OK(status)) {
2637 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2638 __location__, nt_errstr(status));
2639 talloc_free(mem_ctx);
2643 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2644 &printers, &num_printers))) {
2645 talloc_free(mem_ctx);
2649 if (num_printers == 0) {
2650 d_printf("Did not find printers\n");
2651 talloc_free(mem_ctx);
2655 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2656 &ndr_table_spoolss, &p);
2657 if (!NT_STATUS_IS_OK(status)) {
2658 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2660 talloc_free(mem_ctx);
2664 ZERO_STRUCT(userlevel1);
2665 userlevel1.client = talloc_asprintf(
2666 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2667 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2668 userlevel1.build = 2600;
2669 userlevel1.major = 3;
2670 userlevel1.minor = 0;
2671 userlevel1.processor = 0;
2674 struct spoolss_OpenPrinterEx r;
2677 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2679 r.in.datatype = NULL;
2680 r.in.access_mask = 0;
2682 r.in.userlevel.level1 = &userlevel1;
2683 r.out.handle = &server_handle;
2685 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2686 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2687 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2688 "%s, %s\n", __location__, nt_errstr(status),
2689 win_errstr(r.out.result));
2690 talloc_free(mem_ctx);
2696 struct spoolss_ClosePrinter r;
2698 r.in.handle = &server_handle;
2699 r.out.handle = &server_handle;
2701 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2702 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2703 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2704 "%s, %s\n", __location__, nt_errstr(status),
2705 win_errstr(r.out.result));
2706 talloc_free(mem_ctx);
2712 struct spoolss_OpenPrinterEx r;
2715 r.in.printername = talloc_asprintf(
2716 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2717 r.in.datatype = NULL;
2718 r.in.access_mask = 0;
2720 r.in.userlevel.level1 = &userlevel1;
2721 r.out.handle = &printer_handle;
2723 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2724 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2725 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2726 "%s, %s\n", __location__, nt_errstr(status),
2727 win_errstr(r.out.result));
2728 talloc_free(mem_ctx);
2736 for (i=0; i<8; i++) {
2737 status = getprinterinfo(mem_ctx, p, &printer_handle,
2739 if (!NT_STATUS_IS_OK(status)) {
2740 d_printf("(%s) getprinterinfo %d failed: %s\n",
2741 __location__, i, nt_errstr(status));
2748 struct spoolss_ClosePrinter r;
2750 r.in.handle = &printer_handle;
2751 r.out.handle = &printer_handle;
2753 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2754 if (!NT_STATUS_IS_OK(status)) {
2755 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2756 "%s\n", __location__, nt_errstr(status));
2757 talloc_free(mem_ctx);
2764 if (!enumprinters(mem_ctx, p, servername, 1,
2766 d_printf("(%s) enumprinters failed\n", __location__);
2767 talloc_free(mem_ctx);
2770 if (num_printers != num_enumerated) {
2771 d_printf("(%s) netshareenum gave %d printers, "
2772 "enumprinters lvl 1 gave %d\n", __location__,
2773 num_printers, num_enumerated);
2774 talloc_free(mem_ctx);
2781 if (!enumprinters(mem_ctx, p, servername, 2,
2783 d_printf("(%s) enumprinters failed\n", __location__);
2784 talloc_free(mem_ctx);
2787 if (num_printers != num_enumerated) {
2788 d_printf("(%s) netshareenum gave %d printers, "
2789 "enumprinters lvl 2 gave %d\n", __location__,
2790 num_printers, num_enumerated);
2791 talloc_free(mem_ctx);
2796 talloc_free(mem_ctx);
2801 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2803 TALLOC_CTX *mem_ctx;
2804 struct smbcli_state *cli;
2805 struct dcerpc_pipe *p;
2809 if (!(mem_ctx = talloc_new(torture))) {
2813 if (!(torture_open_connection_share(
2814 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2815 "IPC$", torture->ev))) {
2816 d_printf("IPC$ connection failed\n");
2817 talloc_free(mem_ctx);
2821 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2822 if (!NT_STATUS_IS_OK(status)) {
2823 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2824 __location__, nt_errstr(status));
2825 talloc_free(mem_ctx);
2829 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2830 &ndr_table_wkssvc, &p);
2831 if (!NT_STATUS_IS_OK(status)) {
2832 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2834 talloc_free(mem_ctx);
2839 struct wkssvc_NetWkstaInfo100 wks100;
2840 union wkssvc_NetWkstaInfo info;
2841 struct wkssvc_NetWkstaGetInfo r;
2843 r.in.server_name = "\\foo";
2845 info.info100 = &wks100;
2848 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2849 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2850 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2851 "%s, %s\n", __location__, nt_errstr(status),
2852 win_errstr(r.out.result));
2853 talloc_free(mem_ctx);
2857 if (strcmp(servername,
2858 r.out.info->info100->server_name) != 0) {
2859 d_printf("(%s) servername inconsistency: RAP=%s, "
2860 "dcerpc_wkssvc_NetWksGetInfo=%s",
2861 __location__, servername,
2862 r.out.info->info100->server_name);
2863 talloc_free(mem_ctx);
2868 talloc_free(mem_ctx);
2872 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2873 struct policy_handle *handle)
2875 struct winreg_CloseKey c;
2877 TALLOC_CTX *mem_ctx;
2879 c.in.handle = c.out.handle = handle;
2881 if (!(mem_ctx = talloc_new(p))) {
2882 return NT_STATUS_NO_MEMORY;
2885 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2886 talloc_free(mem_ctx);
2888 if (!NT_STATUS_IS_OK(status)) {
2892 if (!W_ERROR_IS_OK(c.out.result)) {
2893 return werror_to_ntstatus(c.out.result);
2896 return NT_STATUS_OK;
2899 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2900 TALLOC_CTX *mem_ctx)
2902 uint32_t enum_index = 0;
2905 struct winreg_EnumValue r;
2906 struct winreg_StringBuf name;
2907 enum winreg_Type type = 0;
2910 uint32_t size, length;
2912 r.in.handle = handle;
2913 r.in.enum_index = enum_index;
2916 r.in.name = r.out.name = &name;
2922 r.in.length = &length;
2924 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2925 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2926 return NT_STATUS_OK;
2932 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
2933 TALLOC_CTX *mem_ctx, int depth)
2935 struct winreg_EnumKey r;
2936 struct winreg_StringBuf class, name;
2941 return NT_STATUS_OK;
2947 r.in.handle = handle;
2948 r.in.enum_index = 0;
2950 r.in.keyclass = &class;
2952 r.in.last_changed_time = &t;
2955 TALLOC_CTX *tmp_ctx;
2956 struct winreg_OpenKey o;
2957 struct policy_handle key_handle;
2960 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2961 return NT_STATUS_NO_MEMORY;
2967 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2968 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2969 /* We're done enumerating */
2970 talloc_free(tmp_ctx);
2971 return NT_STATUS_OK;
2974 for (i=0; i<10-depth; i++)
2976 printf("%s\n", r.out.name->name);
2979 o.in.parent_handle = handle;
2980 o.in.keyname.name = r.out.name->name;
2982 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2983 o.out.handle = &key_handle;
2985 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2986 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2987 enumkeys(p, &key_handle, tmp_ctx, depth-1);
2988 enumvalues(p, &key_handle, tmp_ctx);
2989 status = winreg_close(p, &key_handle);
2990 if (!NT_STATUS_IS_OK(status)) {
2995 talloc_free(tmp_ctx);
2997 r.in.enum_index += 1;
3000 return NT_STATUS_OK;
3003 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3005 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3006 const char *name, winreg_open_fn open_fn)
3008 struct policy_handle handle;
3009 struct winreg_OpenHKLM r;
3012 r.in.system_name = 0;
3013 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3014 r.out.handle = &handle;
3016 status = open_fn(p, mem_ctx, &r);
3017 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3018 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3019 nt_errstr(status), win_errstr(r.out.result));
3023 enumkeys(p, &handle, mem_ctx, 4);
3025 status = winreg_close(p, &handle);
3026 if (!NT_STATUS_IS_OK(status)) {
3027 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3028 __location__, nt_errstr(status));
3035 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3038 struct dcerpc_pipe *p;
3039 TALLOC_CTX *mem_ctx;
3045 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3046 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3047 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3048 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3049 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3054 mem_ctx = talloc_init("torture_rpc_winreg");
3056 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3058 if (!NT_STATUS_IS_OK(status)) {
3059 talloc_free(mem_ctx);
3064 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3066 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3067 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3072 talloc_free(mem_ctx);
3077 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3078 struct loadparm_context *lp_ctx,
3079 struct smbcli_state *cli,
3081 struct srvsvc_NetShareInfo502 **info)
3083 struct smbcli_tree *ipc;
3084 struct dcerpc_pipe *p;
3085 struct srvsvc_NetShareGetInfo r;
3088 if (!(p = dcerpc_pipe_init(cli,
3089 cli->transport->socket->event.ctx,
3090 lp_iconv_convenience(lp_ctx)))) {
3091 status = NT_STATUS_NO_MEMORY;
3095 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3096 if (!NT_STATUS_IS_OK(status)) {
3100 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3101 if (!NT_STATUS_IS_OK(status)) {
3102 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3107 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3108 if (!NT_STATUS_IS_OK(status)) {
3109 d_printf("dcerpc_bind_auth_none failed: %s\n",
3114 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3115 dcerpc_server_name(p));
3116 r.in.share_name = share;
3119 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3120 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3121 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3122 nt_errstr(status), win_errstr(r.out.result));
3126 *info = talloc_move(mem_ctx, &r.out.info.info502);
3127 return NT_STATUS_OK;
3135 * Get us a handle on HKLM\
3138 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3139 struct smbcli_state *cli,
3140 struct smb_iconv_convenience *iconv_convenience,
3141 struct dcerpc_pipe **pipe_p,
3142 struct policy_handle **handle)
3144 struct smbcli_tree *ipc;
3145 struct dcerpc_pipe *p;
3146 struct winreg_OpenHKLM r;
3148 struct policy_handle *result;
3150 result = talloc(mem_ctx, struct policy_handle);
3152 if (result == NULL) {
3153 return NT_STATUS_NO_MEMORY;
3156 if (!(p = dcerpc_pipe_init(result,
3157 cli->transport->socket->event.ctx,
3158 iconv_convenience))) {
3159 status = NT_STATUS_NO_MEMORY;
3163 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3164 if (!NT_STATUS_IS_OK(status)) {
3168 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3169 if (!NT_STATUS_IS_OK(status)) {
3170 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3175 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3176 if (!NT_STATUS_IS_OK(status)) {
3177 d_printf("dcerpc_bind_auth_none failed: %s\n",
3182 r.in.system_name = 0;
3183 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3184 r.out.handle = result;
3186 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3187 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3188 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3189 nt_errstr(status), win_errstr(r.out.result));
3195 return NT_STATUS_OK;
3198 talloc_free(result);
3202 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3203 struct smb_iconv_convenience *iconv_convenience,
3204 const char *sharename)
3206 struct dcerpc_pipe *p;
3207 struct policy_handle *hklm = NULL;
3208 struct policy_handle new_handle;
3209 struct winreg_CreateKey c;
3210 struct winreg_CloseKey cl;
3211 enum winreg_CreateAction action_taken;
3213 TALLOC_CTX *mem_ctx;
3215 mem_ctx = talloc_new(cli);
3216 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3218 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3219 if (!NT_STATUS_IS_OK(status)) {
3220 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3225 c.in.name.name = talloc_asprintf(
3226 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3227 if (c.in.name.name == NULL) {
3228 d_printf("talloc_asprintf failed\n");
3229 status = NT_STATUS_NO_MEMORY;
3232 c.in.keyclass.name = "";
3234 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3235 c.in.secdesc = NULL;
3236 c.in.action_taken = &action_taken;
3237 c.out.new_handle = &new_handle;
3238 c.out.action_taken = &action_taken;
3240 status = dcerpc_winreg_CreateKey(p, p, &c);
3241 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3242 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3243 nt_errstr(status), win_errstr(c.out.result));
3247 cl.in.handle = &new_handle;
3248 cl.out.handle = &new_handle;
3249 status = dcerpc_winreg_CloseKey(p, p, &cl);
3250 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3251 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3252 nt_errstr(status), win_errstr(cl.out.result));
3258 talloc_free(mem_ctx);
3262 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3263 struct smbcli_state *cli,
3264 const char *sharename)
3266 struct dcerpc_pipe *p;
3267 struct policy_handle *hklm = NULL;
3268 struct winreg_DeleteKey d;
3270 TALLOC_CTX *mem_ctx;
3272 mem_ctx = talloc_new(cli);
3273 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3275 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3277 if (!NT_STATUS_IS_OK(status)) {
3278 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3283 d.in.key.name = talloc_asprintf(
3284 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3285 if (d.in.key.name == NULL) {
3286 d_printf("talloc_asprintf failed\n");
3287 status = NT_STATUS_NO_MEMORY;
3290 status = dcerpc_winreg_DeleteKey(p, p, &d);
3291 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3292 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3293 nt_errstr(status), win_errstr(d.out.result));
3298 talloc_free(mem_ctx);
3302 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3303 struct loadparm_context *lp_ctx,
3304 const char *sharename,
3305 const char *parameter,
3308 struct dcerpc_pipe *p = NULL;
3309 struct policy_handle *hklm = NULL, key_handle;
3310 struct winreg_OpenKey o;
3311 struct winreg_SetValue s;
3316 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3317 if (!NT_STATUS_IS_OK(status)) {
3318 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3322 o.in.parent_handle = hklm;
3323 o.in.keyname.name = talloc_asprintf(
3324 hklm, "software\\samba\\smbconf\\%s", sharename);
3325 if (o.in.keyname.name == NULL) {
3326 d_printf("talloc_asprintf failed\n");
3327 status = NT_STATUS_NO_MEMORY;
3331 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3332 o.out.handle = &key_handle;
3334 status = dcerpc_winreg_OpenKey(p, p, &o);
3335 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3336 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3337 nt_errstr(status), win_errstr(o.out.result));
3341 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3342 value, &type, &val)) {
3343 d_printf("(%s) reg_string_to_val failed\n", __location__);
3347 s.in.handle = &key_handle;
3348 s.in.name.name = parameter;
3350 s.in.data = val.data;
3351 s.in.size = val.length;
3353 status = dcerpc_winreg_SetValue(p, p, &s);
3354 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3355 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3356 nt_errstr(status), win_errstr(s.out.result));
3365 bool torture_samba3_regconfig(struct torture_context *torture)
3367 struct smbcli_state *cli;
3368 struct srvsvc_NetShareInfo502 *i = NULL;
3371 const char *comment = "Dummer Kommentar";
3373 if (!(torture_open_connection(&cli, torture, 0))) {
3377 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3378 if (!NT_STATUS_IS_OK(status)) {
3379 torture_warning(torture, "torture_samba3_createshare failed: "
3380 "%s\n", nt_errstr(status));
3384 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3385 if (!NT_STATUS_IS_OK(status)) {
3386 torture_warning(torture, "torture_samba3_setconfig failed: "
3387 "%s\n", nt_errstr(status));
3391 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3392 if (!NT_STATUS_IS_OK(status)) {
3393 torture_warning(torture, "get_shareinfo failed: "
3394 "%s\n", nt_errstr(status));
3398 if (strcmp(comment, i->comment) != 0) {
3399 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3400 comment, i->comment);
3404 status = torture_samba3_deleteshare(torture, cli, "blubber");
3405 if (!NT_STATUS_IS_OK(status)) {
3406 torture_warning(torture, "torture_samba3_deleteshare failed: "
3407 "%s\n", nt_errstr(status));