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 if (!NT_STATUS_IS_OK(status)) {
99 d_printf("smbcli_full_connection failed: %s\n",
104 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
105 lp_iconv_convenience(torture->lp_ctx));
106 if (lsa_pipe == NULL) {
107 d_printf("dcerpc_pipe_init failed\n");
111 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
112 if (!NT_STATUS_IS_OK(status)) {
113 d_printf("dcerpc_pipe_open_smb failed: %s\n",
118 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
119 if (!NT_STATUS_IS_OK(status)) {
120 d_printf("dcerpc_bind_auth_none failed: %s\n",
125 openpolicy.in.system_name =talloc_asprintf(
126 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
127 ZERO_STRUCT(objectattr);
128 openpolicy.in.attr = &objectattr;
129 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
130 openpolicy.out.handle = &handle;
132 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
134 if (!NT_STATUS_IS_OK(status)) {
135 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
140 close_handle.in.handle = &handle;
141 close_handle.out.handle = &handle;
143 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
144 if (!NT_STATUS_IS_OK(status)) {
145 d_printf("dcerpc_lsa_Close failed: %s\n",
150 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
151 if (session2 == NULL) {
152 d_printf("smbcli_session_init failed\n");
156 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
157 d_printf("create_anon_creds failed\n");
161 setup.in.sesskey = cli->transport->negotiate.sesskey;
162 setup.in.capabilities = cli->transport->negotiate.capabilities;
163 setup.in.workgroup = "";
164 setup.in.credentials = anon_creds;
165 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
167 status = smb_composite_sesssetup(session2, &setup);
168 if (!NT_STATUS_IS_OK(status)) {
169 d_printf("anon session setup failed: %s\n",
173 session2->vuid = setup.out.vuid;
175 tmp = cli->tree->session;
176 cli->tree->session = session2;
178 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
180 cli->tree->session = tmp;
181 talloc_free(lsa_pipe);
184 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
185 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
186 "expected NT_STATUS_INVALID_HANDLE\n",
193 talloc_free(mem_ctx);
198 * Bind to lsa using a specific auth method
201 static bool bindtest(struct smbcli_state *cli,
202 struct cli_credentials *credentials,
203 struct loadparm_context *lp_ctx,
204 uint8_t auth_type, uint8_t auth_level)
210 struct dcerpc_pipe *lsa_pipe;
211 struct lsa_ObjectAttribute objectattr;
212 struct lsa_OpenPolicy2 openpolicy;
213 struct lsa_QueryInfoPolicy query;
214 union lsa_PolicyInformation *info = NULL;
215 struct policy_handle handle;
216 struct lsa_Close close_handle;
218 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
219 d_printf("talloc_init failed\n");
223 lsa_pipe = dcerpc_pipe_init(mem_ctx,
224 cli->transport->socket->event.ctx,
225 lp_iconv_convenience(lp_ctx));
226 if (lsa_pipe == NULL) {
227 d_printf("dcerpc_pipe_init failed\n");
231 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
232 if (!NT_STATUS_IS_OK(status)) {
233 d_printf("dcerpc_pipe_open_smb failed: %s\n",
238 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
239 credentials, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
241 if (!NT_STATUS_IS_OK(status)) {
242 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
246 openpolicy.in.system_name =talloc_asprintf(
247 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
248 ZERO_STRUCT(objectattr);
249 openpolicy.in.attr = &objectattr;
250 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
251 openpolicy.out.handle = &handle;
253 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
255 if (!NT_STATUS_IS_OK(status)) {
256 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
261 query.in.handle = &handle;
262 query.in.level = LSA_POLICY_INFO_DOMAIN;
263 query.out.info = &info;
265 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
266 if (!NT_STATUS_IS_OK(status)) {
267 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
272 close_handle.in.handle = &handle;
273 close_handle.out.handle = &handle;
275 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
276 if (!NT_STATUS_IS_OK(status)) {
277 d_printf("dcerpc_lsa_Close failed: %s\n",
284 talloc_free(mem_ctx);
289 * test authenticated RPC binds with the variants Samba3 does support
292 bool torture_bind_samba3(struct torture_context *torture)
297 struct smbcli_state *cli;
298 struct smbcli_options options;
299 struct smbcli_session_options session_options;
301 mem_ctx = talloc_init("torture_bind_authcontext");
303 if (mem_ctx == NULL) {
304 d_printf("talloc_init failed\n");
308 lp_smbcli_options(torture->lp_ctx, &options);
309 lp_smbcli_session_options(torture->lp_ctx, &session_options);
311 status = smbcli_full_connection(mem_ctx, &cli,
312 torture_setting_string(torture, "host", NULL),
313 lp_smb_ports(torture->lp_ctx),
315 lp_socket_options(torture->lp_ctx),
317 lp_resolve_context(torture->lp_ctx),
318 torture->ev, &options, &session_options,
319 lp_iconv_convenience(torture->lp_ctx));
320 if (!NT_STATUS_IS_OK(status)) {
321 d_printf("smbcli_full_connection failed: %s\n",
328 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
329 DCERPC_AUTH_LEVEL_INTEGRITY);
330 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
331 DCERPC_AUTH_LEVEL_PRIVACY);
332 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
333 DCERPC_AUTH_LEVEL_INTEGRITY);
334 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
335 DCERPC_AUTH_LEVEL_PRIVACY);
338 talloc_free(mem_ctx);
343 * Lookup or create a user and return all necessary info
346 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
348 struct loadparm_context *lp_ctx,
349 struct cli_credentials *admin_creds,
352 const char *username,
354 struct dcerpc_pipe **result_pipe,
355 struct policy_handle **result_handle,
356 struct dom_sid **sid)
358 struct dcerpc_pipe *samr_pipe;
360 struct policy_handle conn_handle;
361 struct policy_handle domain_handle;
362 struct policy_handle *user_handle;
363 struct samr_Connect2 conn;
364 struct samr_EnumDomains enumdom;
365 uint32_t resume_handle = 0;
366 struct samr_LookupDomain l;
368 struct lsa_String domain_name;
369 struct lsa_String user_name;
370 struct samr_OpenDomain o;
371 struct samr_CreateUser2 c;
372 uint32_t user_rid,access_granted;
374 samr_pipe = dcerpc_pipe_init(mem_ctx,
375 cli->transport->socket->event.ctx,
376 lp_iconv_convenience(lp_ctx));
377 if (samr_pipe == NULL) {
378 d_printf("dcerpc_pipe_init failed\n");
379 status = NT_STATUS_NO_MEMORY;
383 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
384 if (!NT_STATUS_IS_OK(status)) {
385 d_printf("dcerpc_pipe_open_smb failed: %s\n",
390 if (admin_creds != NULL) {
391 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
392 admin_creds, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
394 if (!NT_STATUS_IS_OK(status)) {
395 d_printf("dcerpc_bind_auth failed: %s\n",
400 /* We must have an authenticated SMB connection */
401 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
402 if (!NT_STATUS_IS_OK(status)) {
403 d_printf("dcerpc_bind_auth_none failed: %s\n",
409 conn.in.system_name = talloc_asprintf(
410 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
411 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
412 conn.out.connect_handle = &conn_handle;
414 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
415 if (!NT_STATUS_IS_OK(status)) {
416 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
420 enumdom.in.connect_handle = &conn_handle;
421 enumdom.in.resume_handle = &resume_handle;
422 enumdom.in.buf_size = (uint32_t)-1;
423 enumdom.out.resume_handle = &resume_handle;
425 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
426 if (!NT_STATUS_IS_OK(status)) {
427 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
431 if (enumdom.out.num_entries != 2) {
432 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
433 enumdom.out.num_entries);
434 status = NT_STATUS_UNSUCCESSFUL;
438 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
441 l.in.connect_handle = &conn_handle;
442 domain_name.string = enumdom.out.sam->entries[dom_idx].name.string;
443 *domain = talloc_strdup(mem_ctx, domain_name.string);
444 l.in.domain_name = &domain_name;
446 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
447 if (!NT_STATUS_IS_OK(status)) {
448 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
452 o.in.connect_handle = &conn_handle;
453 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
454 o.in.sid = l.out.sid;
455 o.out.domain_handle = &domain_handle;
457 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
458 if (!NT_STATUS_IS_OK(status)) {
459 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
463 c.in.domain_handle = &domain_handle;
464 user_name.string = username;
465 c.in.account_name = &user_name;
466 c.in.acct_flags = ACB_NORMAL;
467 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
468 user_handle = talloc(mem_ctx, struct policy_handle);
469 c.out.user_handle = user_handle;
470 c.out.access_granted = &access_granted;
471 c.out.rid = &user_rid;
473 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
475 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
476 struct samr_LookupNames ln;
477 struct samr_OpenUser ou;
479 ln.in.domain_handle = &domain_handle;
481 ln.in.names = &user_name;
483 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
484 if (!NT_STATUS_IS_OK(status)) {
485 d_printf("samr_LookupNames failed: %s\n",
490 ou.in.domain_handle = &domain_handle;
491 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
492 user_rid = ou.in.rid = ln.out.rids.ids[0];
493 ou.out.user_handle = user_handle;
495 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
496 if (!NT_STATUS_IS_OK(status)) {
497 d_printf("samr_OpenUser failed: %s\n",
503 if (!NT_STATUS_IS_OK(status)) {
504 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
508 *result_pipe = samr_pipe;
509 *result_handle = user_handle;
511 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
523 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
524 struct loadparm_context *lp_ctx,
525 struct cli_credentials *admin_creds,
526 const char *username, const char *password,
528 struct dom_sid **user_sid)
532 struct dcerpc_pipe *samr_pipe;
533 struct policy_handle *wks_handle;
536 if (!(tmp_ctx = talloc_new(mem_ctx))) {
537 d_printf("talloc_init failed\n");
541 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
542 DCERPC_AUTH_TYPE_NTLMSSP,
543 DCERPC_AUTH_LEVEL_INTEGRITY,
544 username, domain_name, &samr_pipe, &wks_handle,
546 if (!NT_STATUS_IS_OK(status)) {
547 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
552 struct samr_SetUserInfo2 sui2;
553 struct samr_SetUserInfo sui;
554 struct samr_QueryUserInfo qui;
555 union samr_UserInfo u_info;
556 DATA_BLOB session_key;
560 encode_pw_buffer(u_info.info23.password.data, password,
563 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
564 if (!NT_STATUS_IS_OK(status)) {
565 d_printf("dcerpc_fetch_session_key failed\n");
568 arcfour_crypt_blob(u_info.info23.password.data, 516,
570 u_info.info23.info.password_expired = 0;
571 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
572 SAMR_FIELD_PASSWORD2 |
573 SAMR_FIELD_EXPIRED_FLAG;
574 sui2.in.user_handle = wks_handle;
575 sui2.in.info = &u_info;
578 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
579 if (!NT_STATUS_IS_OK(status)) {
580 d_printf("samr_SetUserInfo(23) failed: %s\n",
585 u_info.info16.acct_flags = ACB_NORMAL;
586 sui.in.user_handle = wks_handle;
587 sui.in.info = &u_info;
590 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
591 if (!NT_STATUS_IS_OK(status)) {
592 d_printf("samr_SetUserInfo(16) failed\n");
596 qui.in.user_handle = wks_handle;
599 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
600 if (!NT_STATUS_IS_OK(status)) {
601 d_printf("samr_QueryUserInfo(21) failed\n");
605 qui.out.info->info21.allow_password_change = 0;
606 qui.out.info->info21.force_password_change = 0;
607 qui.out.info->info21.account_name.string = NULL;
608 qui.out.info->info21.rid = 0;
609 qui.out.info->info21.acct_expiry = 0;
610 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
612 u_info.info21 = qui.out.info->info21;
613 sui.in.user_handle = wks_handle;
614 sui.in.info = &u_info;
617 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
618 if (!NT_STATUS_IS_OK(status)) {
619 d_printf("samr_SetUserInfo(21) failed\n");
624 *domain_name= talloc_steal(mem_ctx, *domain_name);
625 *user_sid = talloc_steal(mem_ctx, *user_sid);
628 talloc_free(tmp_ctx);
636 static bool delete_user(struct smbcli_state *cli,
637 struct loadparm_context *lp_ctx,
638 struct cli_credentials *admin_creds,
639 const char *username)
644 struct dcerpc_pipe *samr_pipe;
645 struct policy_handle *user_handle;
648 if ((mem_ctx = talloc_init("leave")) == NULL) {
649 d_printf("talloc_init failed\n");
653 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
654 DCERPC_AUTH_TYPE_NTLMSSP,
655 DCERPC_AUTH_LEVEL_INTEGRITY,
656 username, &dom_name, &samr_pipe,
659 if (!NT_STATUS_IS_OK(status)) {
660 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
665 struct samr_DeleteUser d;
667 d.in.user_handle = user_handle;
668 d.out.user_handle = user_handle;
670 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
671 if (!NT_STATUS_IS_OK(status)) {
672 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
680 talloc_free(mem_ctx);
685 * Do a Samba3-style join
688 static bool join3(struct smbcli_state *cli,
689 struct loadparm_context *lp_ctx,
691 struct cli_credentials *admin_creds,
692 struct cli_credentials *wks_creds)
697 struct dcerpc_pipe *samr_pipe;
698 struct policy_handle *wks_handle;
700 NTTIME last_password_change;
702 if ((mem_ctx = talloc_init("join3")) == NULL) {
703 d_printf("talloc_init failed\n");
707 status = get_usr_handle(
708 cli, mem_ctx, lp_ctx, admin_creds,
709 DCERPC_AUTH_TYPE_NTLMSSP,
710 DCERPC_AUTH_LEVEL_PRIVACY,
711 talloc_asprintf(mem_ctx, "%s$",
712 cli_credentials_get_workstation(wks_creds)),
713 &dom_name, &samr_pipe, &wks_handle, NULL);
715 if (!NT_STATUS_IS_OK(status)) {
716 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
721 struct samr_QueryUserInfo q;
723 q.in.user_handle = wks_handle;
726 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
727 if (!NT_STATUS_IS_OK(status)) {
728 d_printf("(%s) QueryUserInfo failed: %s\n",
729 __location__, nt_errstr(status));
733 last_password_change = q.out.info->info21.last_password_change;
736 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
739 struct samr_SetUserInfo2 sui2;
740 union samr_UserInfo u_info;
741 struct samr_UserInfo21 *i21 = &u_info.info25.info;
742 DATA_BLOB session_key;
743 DATA_BLOB confounded_session_key = data_blob_talloc(
745 struct MD5Context ctx;
746 uint8_t confounder[16];
750 i21->full_name.string = talloc_asprintf(
752 cli_credentials_get_workstation(wks_creds));
753 i21->acct_flags = ACB_WSTRUST;
754 i21->fields_present = SAMR_FIELD_FULL_NAME |
755 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
757 encode_pw_buffer(u_info.info25.password.data,
758 cli_credentials_get_password(wks_creds),
760 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
761 if (!NT_STATUS_IS_OK(status)) {
762 d_printf("dcerpc_fetch_session_key failed: %s\n",
766 generate_random_buffer((uint8_t *)confounder, 16);
769 MD5Update(&ctx, confounder, 16);
770 MD5Update(&ctx, session_key.data, session_key.length);
771 MD5Final(confounded_session_key.data, &ctx);
773 arcfour_crypt_blob(u_info.info25.password.data, 516,
774 &confounded_session_key);
775 memcpy(&u_info.info25.password.data[516], confounder, 16);
777 sui2.in.user_handle = wks_handle;
779 sui2.in.info = &u_info;
781 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
782 if (!NT_STATUS_IS_OK(status)) {
783 d_printf("samr_SetUserInfo2(25) failed: %s\n",
788 struct samr_SetUserInfo2 sui2;
789 struct samr_SetUserInfo sui;
790 union samr_UserInfo u_info;
791 DATA_BLOB session_key;
793 encode_pw_buffer(u_info.info24.password.data,
794 cli_credentials_get_password(wks_creds),
796 u_info.info24.pw_len =
797 strlen_m(cli_credentials_get_password(wks_creds))*2;
799 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
800 if (!NT_STATUS_IS_OK(status)) {
801 d_printf("dcerpc_fetch_session_key failed\n");
804 arcfour_crypt_blob(u_info.info24.password.data, 516,
806 sui2.in.user_handle = wks_handle;
807 sui2.in.info = &u_info;
810 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
811 if (!NT_STATUS_IS_OK(status)) {
812 d_printf("samr_SetUserInfo(24) failed: %s\n",
817 u_info.info16.acct_flags = ACB_WSTRUST;
818 sui.in.user_handle = wks_handle;
819 sui.in.info = &u_info;
822 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
823 if (!NT_STATUS_IS_OK(status)) {
824 d_printf("samr_SetUserInfo(16) failed\n");
830 struct samr_QueryUserInfo q;
832 q.in.user_handle = wks_handle;
835 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
836 if (!NT_STATUS_IS_OK(status)) {
837 d_printf("(%s) QueryUserInfo failed: %s\n",
838 __location__, nt_errstr(status));
843 if (last_password_change
844 == q.out.info->info21.last_password_change) {
845 d_printf("(%s) last_password_change unchanged "
846 "during join, level25 must change "
847 "it\n", __location__);
852 if (last_password_change
853 != q.out.info->info21.last_password_change) {
854 d_printf("(%s) last_password_change changed "
855 "during join, level24 doesn't "
856 "change it\n", __location__);
865 talloc_free(mem_ctx);
870 * Do a ReqChallenge/Auth2 and get the wks creds
873 static bool auth2(struct smbcli_state *cli,
874 struct loadparm_context *lp_ctx,
875 struct cli_credentials *wks_cred)
878 struct dcerpc_pipe *net_pipe;
881 struct netr_ServerReqChallenge r;
882 struct netr_Credential netr_cli_creds;
883 struct netr_Credential netr_srv_creds;
884 uint32_t negotiate_flags;
885 struct netr_ServerAuthenticate2 a;
886 struct creds_CredentialState *creds_state;
887 struct netr_Credential netr_cred;
888 struct samr_Password mach_pw;
890 mem_ctx = talloc_new(NULL);
891 if (mem_ctx == NULL) {
892 d_printf("talloc_new failed\n");
896 net_pipe = dcerpc_pipe_init(mem_ctx,
897 cli->transport->socket->event.ctx,
898 lp_iconv_convenience(lp_ctx));
899 if (net_pipe == NULL) {
900 d_printf("dcerpc_pipe_init failed\n");
904 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
905 if (!NT_STATUS_IS_OK(status)) {
906 d_printf("dcerpc_pipe_open_smb failed: %s\n",
911 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
912 if (!NT_STATUS_IS_OK(status)) {
913 d_printf("dcerpc_bind_auth_none failed: %s\n",
918 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
919 r.in.server_name = talloc_asprintf(
920 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
921 if (r.in.server_name == NULL) {
922 d_printf("talloc_asprintf failed\n");
925 generate_random_buffer(netr_cli_creds.data,
926 sizeof(netr_cli_creds.data));
927 r.in.credentials = &netr_cli_creds;
928 r.out.return_credentials = &netr_srv_creds;
930 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
931 if (!NT_STATUS_IS_OK(status)) {
932 d_printf("netr_ServerReqChallenge failed: %s\n",
937 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
938 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
940 creds_state = talloc(mem_ctx, struct creds_CredentialState);
941 creds_client_init(creds_state, r.in.credentials,
942 r.out.return_credentials, &mach_pw,
943 &netr_cred, negotiate_flags);
945 a.in.server_name = talloc_asprintf(
946 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
947 a.in.account_name = talloc_asprintf(
948 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
949 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
950 a.in.secure_channel_type = SEC_CHAN_WKSTA;
951 a.in.negotiate_flags = &negotiate_flags;
952 a.out.negotiate_flags = &negotiate_flags;
953 a.in.credentials = &netr_cred;
954 a.out.return_credentials = &netr_cred;
956 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
957 if (!NT_STATUS_IS_OK(status)) {
958 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
963 if (!creds_client_check(creds_state, a.out.return_credentials)) {
964 d_printf("creds_client_check failed\n");
968 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
973 talloc_free(mem_ctx);
978 * Do a couple of schannel protected Netlogon ops: Interactive and Network
979 * login, and change the wks password
982 static bool schan(struct smbcli_state *cli,
983 struct loadparm_context *lp_ctx,
984 struct cli_credentials *wks_creds,
985 struct cli_credentials *user_creds)
990 struct dcerpc_pipe *net_pipe;
993 mem_ctx = talloc_new(NULL);
994 if (mem_ctx == NULL) {
995 d_printf("talloc_new failed\n");
999 net_pipe = dcerpc_pipe_init(mem_ctx,
1000 cli->transport->socket->event.ctx,
1001 lp_iconv_convenience(lp_ctx));
1002 if (net_pipe == NULL) {
1003 d_printf("dcerpc_pipe_init failed\n");
1007 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1008 if (!NT_STATUS_IS_OK(status)) {
1009 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1015 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1016 DCERPC_DEBUG_PRINT_OUT;
1019 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1020 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1021 wks_creds, lp_gensec_settings(lp_ctx, lp_ctx), DCERPC_AUTH_TYPE_SCHANNEL,
1022 DCERPC_AUTH_LEVEL_PRIVACY,
1025 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1033 for (i=2; i<4; i++) {
1035 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1036 struct creds_CredentialState *creds_state;
1037 struct netr_Authenticator netr_auth, netr_auth2;
1038 struct netr_NetworkInfo ninfo;
1039 struct netr_PasswordInfo pinfo;
1040 struct netr_LogonSamLogon r;
1041 union netr_LogonLevel logon;
1042 union netr_Validation validation;
1043 uint8_t authoritative;
1044 struct netr_Authenticator return_authenticator;
1046 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1047 CLI_CRED_NTLMv2_AUTH;
1049 chal = data_blob_talloc(mem_ctx, NULL, 8);
1050 if (chal.data == NULL) {
1051 d_printf("data_blob_talloc failed\n");
1055 generate_random_buffer(chal.data, chal.length);
1056 names_blob = NTLMv2_generate_names_blob(
1058 cli_credentials_get_workstation(user_creds),
1059 cli_credentials_get_domain(user_creds));
1060 status = cli_credentials_get_ntlm_response(
1061 user_creds, mem_ctx, &flags, chal, names_blob,
1062 &lm_resp, &nt_resp, NULL, NULL);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 d_printf("cli_credentials_get_ntlm_response failed:"
1065 " %s\n", nt_errstr(status));
1069 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1070 creds_client_authenticator(creds_state, &netr_auth);
1072 ninfo.identity_info.account_name.string =
1073 cli_credentials_get_username(user_creds);
1074 ninfo.identity_info.domain_name.string =
1075 cli_credentials_get_domain(user_creds);
1076 ninfo.identity_info.parameter_control = 0;
1077 ninfo.identity_info.logon_id_low = 0;
1078 ninfo.identity_info.logon_id_high = 0;
1079 ninfo.identity_info.workstation.string =
1080 cli_credentials_get_workstation(user_creds);
1081 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1082 ninfo.nt.length = nt_resp.length;
1083 ninfo.nt.data = nt_resp.data;
1084 ninfo.lm.length = lm_resp.length;
1085 ninfo.lm.data = lm_resp.data;
1087 logon.network = &ninfo;
1089 r.in.server_name = talloc_asprintf(
1090 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1091 ZERO_STRUCT(netr_auth2);
1092 r.in.computer_name =
1093 cli_credentials_get_workstation(wks_creds);
1094 r.in.credential = &netr_auth;
1095 r.in.return_authenticator = &netr_auth2;
1096 r.in.logon_level = 2;
1097 r.in.validation_level = i;
1098 r.in.logon = &logon;
1099 r.out.validation = &validation;
1100 r.out.authoritative = &authoritative;
1101 r.out.return_authenticator = &return_authenticator;
1103 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 d_printf("netr_LogonSamLogon failed: %s\n",
1110 if ((r.out.return_authenticator == NULL) ||
1111 (!creds_client_check(creds_state,
1112 &r.out.return_authenticator->cred))) {
1113 d_printf("Credentials check failed!\n");
1117 creds_client_authenticator(creds_state, &netr_auth);
1119 pinfo.identity_info = ninfo.identity_info;
1120 ZERO_STRUCT(pinfo.lmpassword.hash);
1121 E_md4hash(cli_credentials_get_password(user_creds),
1122 pinfo.ntpassword.hash);
1123 session_key = data_blob_talloc(mem_ctx,
1124 creds_state->session_key, 16);
1125 arcfour_crypt_blob(pinfo.ntpassword.hash,
1126 sizeof(pinfo.ntpassword.hash),
1129 logon.password = &pinfo;
1131 r.in.logon_level = 1;
1132 r.in.logon = &logon;
1133 r.out.return_authenticator = &return_authenticator;
1135 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1136 if (!NT_STATUS_IS_OK(status)) {
1137 d_printf("netr_LogonSamLogon failed: %s\n",
1142 if ((r.out.return_authenticator == NULL) ||
1143 (!creds_client_check(creds_state,
1144 &r.out.return_authenticator->cred))) {
1145 d_printf("Credentials check failed!\n");
1151 struct netr_ServerPasswordSet s;
1152 char *password = generate_random_str(wks_creds, 8);
1153 struct creds_CredentialState *creds_state;
1154 struct netr_Authenticator credential, return_authenticator;
1155 struct samr_Password new_password;
1157 s.in.server_name = talloc_asprintf(
1158 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1159 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1160 s.in.account_name = talloc_asprintf(
1161 mem_ctx, "%s$", s.in.computer_name);
1162 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1163 s.in.credential = &credential;
1164 s.in.new_password = &new_password;
1165 s.out.return_authenticator = &return_authenticator;
1167 E_md4hash(password, new_password.hash);
1169 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1170 creds_des_encrypt(creds_state, &new_password);
1171 creds_client_authenticator(creds_state, &credential);
1173 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1179 if (!creds_client_check(creds_state,
1180 &s.out.return_authenticator->cred)) {
1181 printf("Credential chaining failed\n");
1184 cli_credentials_set_password(wks_creds, password,
1190 talloc_free(mem_ctx);
1195 * Delete the wks account again
1198 static bool leave(struct smbcli_state *cli,
1199 struct loadparm_context *lp_ctx,
1200 struct cli_credentials *admin_creds,
1201 struct cli_credentials *wks_creds)
1203 char *wks_name = talloc_asprintf(
1204 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1207 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1208 talloc_free(wks_name);
1213 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1216 bool torture_netlogon_samba3(struct torture_context *torture)
1218 TALLOC_CTX *mem_ctx;
1221 struct smbcli_state *cli;
1222 struct cli_credentials *anon_creds;
1223 struct cli_credentials *wks_creds;
1224 const char *wks_name;
1226 struct smbcli_options options;
1227 struct smbcli_session_options session_options;
1229 wks_name = torture_setting_string(torture, "wksname", NULL);
1230 if (wks_name == NULL) {
1231 wks_name = get_myname();
1234 mem_ctx = talloc_init("torture_netlogon_samba3");
1236 if (mem_ctx == NULL) {
1237 d_printf("talloc_init failed\n");
1241 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1242 d_printf("create_anon_creds failed\n");
1246 lp_smbcli_options(torture->lp_ctx, &options);
1247 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1249 status = smbcli_full_connection(mem_ctx, &cli,
1250 torture_setting_string(torture, "host", NULL),
1251 lp_smb_ports(torture->lp_ctx),
1253 lp_socket_options(torture->lp_ctx),
1255 lp_resolve_context(torture->lp_ctx),
1256 torture->ev, &options, &session_options,
1257 lp_iconv_convenience(torture->lp_ctx));
1258 if (!NT_STATUS_IS_OK(status)) {
1259 d_printf("smbcli_full_connection failed: %s\n",
1264 wks_creds = cli_credentials_init(mem_ctx);
1265 if (wks_creds == NULL) {
1266 d_printf("cli_credentials_init failed\n");
1270 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1271 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1272 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1273 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1274 cli_credentials_set_password(wks_creds,
1275 generate_random_str(wks_creds, 8),
1278 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1279 d_printf("join failed\n");
1283 cli_credentials_set_domain(
1284 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1287 for (i=0; i<2; i++) {
1289 /* Do this more than once, the routine "schan" changes
1290 * the workstation password using the netlogon
1291 * password change routine */
1295 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1296 d_printf("auth2 failed\n");
1300 for (j=0; j<2; j++) {
1301 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1302 d_printf("schan failed\n");
1308 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1309 d_printf("leave failed\n");
1316 talloc_free(mem_ctx);
1321 * Do a simple join, testjoin and leave using specified smb and samr
1325 static bool test_join3(struct torture_context *tctx,
1327 struct cli_credentials *smb_creds,
1328 struct cli_credentials *samr_creds,
1329 const char *wks_name)
1333 struct smbcli_state *cli;
1334 struct cli_credentials *wks_creds;
1335 struct smbcli_options options;
1336 struct smbcli_session_options session_options;
1338 lp_smbcli_options(tctx->lp_ctx, &options);
1339 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1341 status = smbcli_full_connection(tctx, &cli,
1342 torture_setting_string(tctx, "host", NULL),
1343 lp_smb_ports(tctx->lp_ctx),
1344 "IPC$", NULL, lp_socket_options(tctx->lp_ctx),
1345 smb_creds, lp_resolve_context(tctx->lp_ctx),
1346 tctx->ev, &options, &session_options,
1347 lp_iconv_convenience(tctx->lp_ctx));
1348 if (!NT_STATUS_IS_OK(status)) {
1349 d_printf("smbcli_full_connection failed: %s\n",
1354 wks_creds = cli_credentials_init(cli);
1355 if (wks_creds == NULL) {
1356 d_printf("cli_credentials_init failed\n");
1360 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1361 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1362 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1363 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1364 cli_credentials_set_password(wks_creds,
1365 generate_random_str(wks_creds, 8),
1368 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1369 d_printf("join failed\n");
1373 cli_credentials_set_domain(
1374 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1377 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1378 d_printf("auth2 failed\n");
1382 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1383 d_printf("leave failed\n");
1396 * Test the different session key variants. Do it by joining, this uses the
1397 * session key in the setpassword routine. Test the join by doing the auth2.
1400 bool torture_samba3_sessionkey(struct torture_context *torture)
1403 struct cli_credentials *anon_creds;
1404 const char *wks_name;
1406 wks_name = torture_setting_string(torture, "wksname", get_myname());
1408 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1409 d_printf("create_anon_creds failed\n");
1413 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1417 if (!torture_setting_bool(torture, "samba3", false)) {
1419 /* Samba3 in the build farm right now does this happily. Need
1422 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1423 d_printf("join using anonymous bind on an anonymous smb "
1424 "connection succeeded -- HUH??\n");
1429 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1431 d_printf("join using ntlmssp bind on an anonymous smb "
1432 "connection failed\n");
1436 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1437 d_printf("join using anonymous bind on an authenticated smb "
1438 "connection failed\n");
1442 if (!test_join3(torture, false, cmdline_credentials,
1443 cmdline_credentials,
1445 d_printf("join using ntlmssp bind on an authenticated smb "
1446 "connection failed\n");
1451 * The following two are tests for setuserinfolevel 25
1454 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1456 d_printf("join using ntlmssp bind on an anonymous smb "
1457 "connection failed\n");
1461 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1462 d_printf("join using anonymous bind on an authenticated smb "
1463 "connection failed\n");
1473 * open pipe and bind, given an IPC$ context
1476 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1477 struct loadparm_context *lp_ctx,
1478 struct smbcli_tree *tree,
1479 const char *pipe_name,
1480 const struct ndr_interface_table *iface,
1481 struct dcerpc_pipe **p)
1483 struct dcerpc_pipe *result;
1486 if (!(result = dcerpc_pipe_init(
1487 mem_ctx, tree->session->transport->socket->event.ctx,
1488 lp_iconv_convenience(lp_ctx)))) {
1489 return NT_STATUS_NO_MEMORY;
1492 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1493 if (!NT_STATUS_IS_OK(status)) {
1494 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1496 talloc_free(result);
1500 status = dcerpc_bind_auth_none(result, iface);
1501 if (!NT_STATUS_IS_OK(status)) {
1502 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1503 talloc_free(result);
1508 return NT_STATUS_OK;
1512 * Sane wrapper around lsa_LookupNames
1515 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1516 struct dcerpc_pipe *p,
1520 struct lsa_ObjectAttribute attr;
1521 struct lsa_QosInfo qos;
1522 struct lsa_OpenPolicy2 r;
1525 struct policy_handle handle;
1526 struct lsa_LookupNames l;
1527 struct lsa_TransSidArray sids;
1528 struct lsa_RefDomainList *domains = NULL;
1529 struct lsa_String lsa_name;
1531 struct dom_sid *result;
1532 TALLOC_CTX *tmp_ctx;
1534 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1539 qos.impersonation_level = 2;
1540 qos.context_mode = 1;
1541 qos.effective_only = 0;
1544 attr.root_dir = NULL;
1545 attr.object_name = NULL;
1546 attr.attributes = 0;
1547 attr.sec_desc = NULL;
1548 attr.sec_qos = &qos;
1550 r.in.system_name = "\\";
1552 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1553 r.out.handle = &handle;
1555 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1556 if (!NT_STATUS_IS_OK(status)) {
1557 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1558 talloc_free(tmp_ctx);
1565 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1567 l.in.handle = &handle;
1569 l.in.names = &lsa_name;
1572 l.in.count = &count;
1573 l.out.count = &count;
1575 l.out.domains = &domains;
1577 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1578 if (!NT_STATUS_IS_OK(status)) {
1579 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1581 talloc_free(tmp_ctx);
1585 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1586 l.out.sids->sids[0].rid);
1588 c.in.handle = &handle;
1589 c.out.handle = &handle;
1591 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1592 if (!NT_STATUS_IS_OK(status)) {
1593 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1594 talloc_free(tmp_ctx);
1598 talloc_free(tmp_ctx);
1603 * Find out the user SID on this connection
1606 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1607 struct loadparm_context *lp_ctx,
1608 struct smbcli_tree *tree)
1610 struct dcerpc_pipe *lsa;
1611 struct lsa_GetUserName r;
1613 struct lsa_String *authority_name_p = NULL;
1614 struct lsa_String *account_name_p = NULL;
1615 struct dom_sid *result;
1617 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1618 &ndr_table_lsarpc, &lsa);
1619 if (!NT_STATUS_IS_OK(status)) {
1620 d_printf("(%s) Could not bind to LSA: %s\n",
1621 __location__, nt_errstr(status));
1625 r.in.system_name = "\\";
1626 r.in.account_name = &account_name_p;
1627 r.in.authority_name = &authority_name_p;
1628 r.out.account_name = &account_name_p;
1630 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1632 authority_name_p = *r.out.authority_name;
1634 if (!NT_STATUS_IS_OK(status)) {
1635 printf("(%s) GetUserName failed - %s\n",
1636 __location__, nt_errstr(status));
1641 result = name2sid(mem_ctx, lsa, account_name_p->string,
1642 authority_name_p->string);
1648 static int destroy_tree(struct smbcli_tree *tree)
1650 smb_tree_disconnect(tree);
1655 * Do a tcon, given a session
1658 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1659 struct smbcli_session *session,
1660 const char *sharename,
1661 struct smbcli_tree **res)
1663 struct smbcli_tree *result;
1664 TALLOC_CTX *tmp_ctx;
1665 union smb_tcon tcon;
1668 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1669 return NT_STATUS_NO_MEMORY;
1672 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1673 talloc_free(tmp_ctx);
1674 return NT_STATUS_NO_MEMORY;
1677 tcon.generic.level = RAW_TCON_TCONX;
1678 tcon.tconx.in.flags = 0;
1679 tcon.tconx.in.password = data_blob(NULL, 0);
1680 tcon.tconx.in.path = sharename;
1681 tcon.tconx.in.device = "?????";
1683 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1684 if (!NT_STATUS_IS_OK(status)) {
1685 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1687 talloc_free(tmp_ctx);
1691 result->tid = tcon.tconx.out.tid;
1692 result = talloc_steal(mem_ctx, result);
1693 talloc_set_destructor(result, destroy_tree);
1694 talloc_free(tmp_ctx);
1696 return NT_STATUS_OK;
1700 * Test the getusername behaviour
1703 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1706 struct smbcli_state *cli;
1707 TALLOC_CTX *mem_ctx;
1709 struct dom_sid *user_sid;
1710 struct dom_sid *created_sid;
1711 struct cli_credentials *anon_creds;
1712 struct cli_credentials *user_creds;
1714 struct smbcli_options options;
1715 struct smbcli_session_options session_options;
1717 if (!(mem_ctx = talloc_new(torture))) {
1721 lp_smbcli_options(torture->lp_ctx, &options);
1722 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1724 status = smbcli_full_connection(
1725 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1726 lp_smb_ports(torture->lp_ctx),
1727 "IPC$", NULL, lp_socket_options(torture->lp_ctx), cmdline_credentials,
1728 lp_resolve_context(torture->lp_ctx),
1729 torture->ev, &options, &session_options,
1730 lp_iconv_convenience(torture->lp_ctx));
1731 if (!NT_STATUS_IS_OK(status)) {
1732 d_printf("(%s) smbcli_full_connection failed: %s\n",
1733 __location__, nt_errstr(status));
1738 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1739 d_printf("(%s) whoami on auth'ed connection failed\n",
1746 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1747 d_printf("(%s) create_anon_creds failed\n", __location__);
1752 status = smbcli_full_connection(
1753 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1754 lp_smb_ports(torture->lp_ctx), "IPC$", NULL,
1755 lp_socket_options(torture->lp_ctx), anon_creds,
1756 lp_resolve_context(torture->lp_ctx),
1757 torture->ev, &options, &session_options,
1758 lp_iconv_convenience(torture->lp_ctx));
1759 if (!NT_STATUS_IS_OK(status)) {
1760 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1761 __location__, nt_errstr(status));
1766 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1767 d_printf("(%s) whoami on anon connection failed\n",
1773 if (!dom_sid_equal(user_sid,
1774 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1775 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1776 "S-1-5-7", __location__,
1777 dom_sid_string(mem_ctx, user_sid));
1781 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1782 d_printf("(%s) cli_credentials_init failed\n", __location__);
1787 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1788 cli_credentials_set_username(user_creds, "torture_username",
1790 cli_credentials_set_password(user_creds,
1791 generate_random_str(user_creds, 8),
1794 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1795 cli_credentials_get_username(user_creds),
1796 cli_credentials_get_password(user_creds),
1797 &domain_name, &created_sid)) {
1798 d_printf("(%s) create_user failed\n", __location__);
1803 cli_credentials_set_domain(user_creds, domain_name,
1807 struct smbcli_session *session2;
1808 struct smb_composite_sesssetup setup;
1809 struct smbcli_tree *tree;
1811 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1812 if (session2 == NULL) {
1813 d_printf("(%s) smbcli_session_init failed\n",
1818 setup.in.sesskey = cli->transport->negotiate.sesskey;
1819 setup.in.capabilities = cli->transport->negotiate.capabilities;
1820 setup.in.workgroup = "";
1821 setup.in.credentials = user_creds;
1822 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
1824 status = smb_composite_sesssetup(session2, &setup);
1825 if (!NT_STATUS_IS_OK(status)) {
1826 d_printf("(%s) session setup with new user failed: "
1827 "%s\n", __location__, nt_errstr(status));
1831 session2->vuid = setup.out.vuid;
1833 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1835 d_printf("(%s) secondary_tcon failed\n",
1841 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1842 d_printf("(%s) whoami on user connection failed\n",
1851 d_printf("Created %s, found %s\n",
1852 dom_sid_string(mem_ctx, created_sid),
1853 dom_sid_string(mem_ctx, user_sid));
1855 if (!dom_sid_equal(created_sid, user_sid)) {
1860 if (!delete_user(cli, torture->lp_ctx,
1861 cmdline_credentials,
1862 cli_credentials_get_username(user_creds))) {
1863 d_printf("(%s) delete_user failed\n", __location__);
1868 talloc_free(mem_ctx);
1872 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1873 const char *sharename)
1876 struct srvsvc_NetShareGetInfo r;
1877 union srvsvc_NetShareInfo info;
1878 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1882 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1883 dcerpc_server_name(p));
1884 r.in.share_name = sharename;
1887 for (i=0;i<ARRAY_SIZE(levels);i++) {
1888 r.in.level = levels[i];
1890 printf("testing NetShareGetInfo level %u on share '%s'\n",
1891 r.in.level, r.in.share_name);
1893 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1894 if (!NT_STATUS_IS_OK(status)) {
1895 printf("NetShareGetInfo level %u on share '%s' failed"
1896 " - %s\n", r.in.level, r.in.share_name,
1901 if (!W_ERROR_IS_OK(r.out.result)) {
1902 printf("NetShareGetInfo level %u on share '%s' failed "
1903 "- %s\n", r.in.level, r.in.share_name,
1904 win_errstr(r.out.result));
1913 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1914 const char **one_sharename)
1917 struct srvsvc_NetShareEnum r;
1918 struct srvsvc_NetShareInfoCtr info_ctr;
1919 struct srvsvc_NetShareCtr0 c0;
1920 struct srvsvc_NetShareCtr1 c1;
1921 struct srvsvc_NetShareCtr2 c2;
1922 struct srvsvc_NetShareCtr501 c501;
1923 struct srvsvc_NetShareCtr502 c502;
1924 struct srvsvc_NetShareCtr1004 c1004;
1925 struct srvsvc_NetShareCtr1005 c1005;
1926 struct srvsvc_NetShareCtr1006 c1006;
1927 struct srvsvc_NetShareCtr1007 c1007;
1928 uint32_t totalentries = 0;
1929 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1933 ZERO_STRUCT(info_ctr);
1935 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1936 r.in.info_ctr = &info_ctr;
1937 r.in.max_buffer = (uint32_t)-1;
1938 r.in.resume_handle = NULL;
1939 r.out.totalentries = &totalentries;
1940 r.out.info_ctr = &info_ctr;
1942 for (i=0;i<ARRAY_SIZE(levels);i++) {
1943 info_ctr.level = levels[i];
1945 switch (info_ctr.level) {
1948 info_ctr.ctr.ctr0 = &c0;
1952 info_ctr.ctr.ctr1 = &c1;
1956 info_ctr.ctr.ctr2 = &c2;
1960 info_ctr.ctr.ctr501 = &c501;
1964 info_ctr.ctr.ctr502 = &c502;
1968 info_ctr.ctr.ctr1004 = &c1004;
1972 info_ctr.ctr.ctr1005 = &c1005;
1976 info_ctr.ctr.ctr1006 = &c1006;
1980 info_ctr.ctr.ctr1007 = &c1007;
1984 printf("testing NetShareEnum level %u\n", info_ctr.level);
1985 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1986 if (!NT_STATUS_IS_OK(status)) {
1987 printf("NetShareEnum level %u failed - %s\n",
1988 info_ctr.level, nt_errstr(status));
1992 if (!W_ERROR_IS_OK(r.out.result)) {
1993 printf("NetShareEnum level %u failed - %s\n",
1994 info_ctr.level, win_errstr(r.out.result));
1997 if (info_ctr.level == 0) {
1998 struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
1999 if (ctr->count > 0) {
2000 *one_sharename = ctr->array[0].name;
2008 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
2010 struct dcerpc_pipe *p;
2011 TALLOC_CTX *mem_ctx;
2013 const char *sharename = NULL;
2014 struct smbcli_state *cli;
2017 if (!(mem_ctx = talloc_new(torture))) {
2021 if (!(torture_open_connection_share(
2022 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2023 "IPC$", torture->ev))) {
2024 talloc_free(mem_ctx);
2028 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
2029 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
2030 if (!NT_STATUS_IS_OK(status)) {
2031 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2032 __location__, nt_errstr(status));
2037 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
2038 if (sharename == NULL) {
2039 printf("did not get sharename\n");
2041 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
2045 talloc_free(mem_ctx);
2050 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
2051 * NT_STATUS_NO_SAM_ACCOUNT
2054 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
2056 TALLOC_CTX *mem_ctx;
2057 struct dcerpc_pipe *net_pipe;
2059 bool result = false;
2061 struct netr_ServerReqChallenge r;
2062 struct netr_Credential netr_cli_creds;
2063 struct netr_Credential netr_srv_creds;
2064 uint32_t negotiate_flags;
2065 struct netr_ServerAuthenticate2 a;
2066 struct creds_CredentialState *creds_state;
2067 struct netr_Credential netr_cred;
2068 struct samr_Password mach_pw;
2069 struct smbcli_state *cli;
2071 if (!(mem_ctx = talloc_new(torture))) {
2072 d_printf("talloc_new failed\n");
2076 if (!(wksname = generate_random_str_list(
2077 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2078 d_printf("generate_random_str_list failed\n");
2082 if (!(torture_open_connection_share(
2084 torture, torture_setting_string(torture, "host", NULL),
2085 "IPC$", torture->ev))) {
2086 d_printf("IPC$ connection failed\n");
2090 if (!(net_pipe = dcerpc_pipe_init(
2091 mem_ctx, cli->transport->socket->event.ctx,
2092 lp_iconv_convenience(torture->lp_ctx)))) {
2093 d_printf("dcerpc_pipe_init failed\n");
2097 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2098 if (!NT_STATUS_IS_OK(status)) {
2099 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2104 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 d_printf("dcerpc_bind_auth_none failed: %s\n",
2111 r.in.computer_name = wksname;
2112 r.in.server_name = talloc_asprintf(
2113 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2114 if (r.in.server_name == NULL) {
2115 d_printf("talloc_asprintf failed\n");
2118 generate_random_buffer(netr_cli_creds.data,
2119 sizeof(netr_cli_creds.data));
2120 r.in.credentials = &netr_cli_creds;
2121 r.out.return_credentials = &netr_srv_creds;
2123 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2124 if (!NT_STATUS_IS_OK(status)) {
2125 d_printf("netr_ServerReqChallenge failed: %s\n",
2130 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2131 E_md4hash("foobar", mach_pw.hash);
2133 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2134 creds_client_init(creds_state, r.in.credentials,
2135 r.out.return_credentials, &mach_pw,
2136 &netr_cred, negotiate_flags);
2138 a.in.server_name = talloc_asprintf(
2139 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2140 a.in.account_name = talloc_asprintf(
2141 mem_ctx, "%s$", wksname);
2142 a.in.computer_name = wksname;
2143 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2144 a.in.negotiate_flags = &negotiate_flags;
2145 a.out.negotiate_flags = &negotiate_flags;
2146 a.in.credentials = &netr_cred;
2147 a.out.return_credentials = &netr_cred;
2149 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2151 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2152 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2153 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2160 talloc_free(mem_ctx);
2164 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2165 struct loadparm_context *lp_ctx,
2166 struct smbcli_session *sess,
2167 const char *sharename)
2169 struct smbcli_tree *tree;
2170 TALLOC_CTX *tmp_ctx;
2171 struct dcerpc_pipe *p;
2173 struct srvsvc_NetShareGetInfo r;
2174 union srvsvc_NetShareInfo info;
2175 struct security_descriptor *result;
2177 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2178 d_printf("talloc_new failed\n");
2182 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2183 d_printf("secondary_tcon failed\n");
2184 talloc_free(tmp_ctx);
2188 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2189 &ndr_table_srvsvc, &p);
2190 if (!NT_STATUS_IS_OK(status)) {
2191 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2192 __location__, nt_errstr(status));
2193 talloc_free(tmp_ctx);
2198 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2201 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2202 dcerpc_server_name(p));
2203 r.in.share_name = sharename;
2207 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2208 if (!NT_STATUS_IS_OK(status)) {
2209 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2211 talloc_free(tmp_ctx);
2215 result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
2216 talloc_free(tmp_ctx);
2220 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2221 struct loadparm_context *lp_ctx,
2222 struct smbcli_session *sess,
2223 const char *sharename,
2224 struct security_descriptor *sd)
2226 struct smbcli_tree *tree;
2227 TALLOC_CTX *tmp_ctx;
2228 struct dcerpc_pipe *p;
2230 struct sec_desc_buf i;
2231 struct srvsvc_NetShareSetInfo r;
2232 union srvsvc_NetShareInfo info;
2235 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2236 d_printf("talloc_new failed\n");
2237 return NT_STATUS_NO_MEMORY;
2240 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2241 d_printf("secondary_tcon failed\n");
2242 talloc_free(tmp_ctx);
2243 return NT_STATUS_UNSUCCESSFUL;
2246 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2247 &ndr_table_srvsvc, &p);
2248 if (!NT_STATUS_IS_OK(status)) {
2249 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2250 __location__, nt_errstr(status));
2251 talloc_free(tmp_ctx);
2252 return NT_STATUS_UNSUCCESSFUL;
2256 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2259 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2260 dcerpc_server_name(p));
2261 r.in.share_name = sharename;
2266 r.in.parm_error = &error;
2268 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2269 if (!NT_STATUS_IS_OK(status)) {
2270 d_printf("srvsvc_NetShareSetInfo failed: %s\n",
2274 talloc_free(tmp_ctx);
2278 bool try_tcon(TALLOC_CTX *mem_ctx,
2279 struct loadparm_context *lp_ctx,
2280 struct security_descriptor *orig_sd,
2281 struct smbcli_session *session,
2282 const char *sharename, const struct dom_sid *user_sid,
2283 unsigned int access_mask, NTSTATUS expected_tcon,
2284 NTSTATUS expected_mkdir)
2286 TALLOC_CTX *tmp_ctx;
2287 struct smbcli_tree *rmdir_tree, *tree;
2288 struct dom_sid *domain_sid;
2290 struct security_descriptor *sd;
2294 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2295 d_printf("talloc_new failed\n");
2299 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2300 if (!NT_STATUS_IS_OK(status)) {
2301 d_printf("first tcon to delete dir failed\n");
2302 talloc_free(tmp_ctx);
2306 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2308 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2309 &domain_sid, &rid))) {
2310 d_printf("dom_sid_split_rid failed\n");
2311 talloc_free(tmp_ctx);
2315 sd = security_descriptor_dacl_create(
2316 tmp_ctx, 0, "S-1-5-32-544",
2317 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2319 dom_sid_string(mem_ctx, user_sid),
2320 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2322 d_printf("security_descriptor_dacl_create failed\n");
2323 talloc_free(tmp_ctx);
2327 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2328 if (!NT_STATUS_IS_OK(status)) {
2329 d_printf("custom set_sharesec failed: %s\n",
2331 talloc_free(tmp_ctx);
2335 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2336 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2337 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2343 if (!NT_STATUS_IS_OK(status)) {
2344 /* An expected non-access, no point in trying to write */
2348 status = smbcli_mkdir(tree, "sharesec_testdir");
2349 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2350 d_printf("(%s) Expected %s, got %s\n", __location__,
2351 nt_errstr(expected_mkdir), nt_errstr(status));
2356 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2358 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2359 if (!NT_STATUS_IS_OK(status)) {
2360 d_printf("custom set_sharesec failed: %s\n",
2362 talloc_free(tmp_ctx);
2366 talloc_free(tmp_ctx);
2370 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2372 TALLOC_CTX *mem_ctx;
2374 struct smbcli_state *cli;
2375 struct security_descriptor *sd;
2376 struct dom_sid *user_sid;
2378 if (!(mem_ctx = talloc_new(torture))) {
2382 if (!(torture_open_connection_share(
2383 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2384 "IPC$", torture->ev))) {
2385 d_printf("IPC$ connection failed\n");
2386 talloc_free(mem_ctx);
2390 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2391 d_printf("whoami failed\n");
2392 talloc_free(mem_ctx);
2396 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2397 torture_setting_string(torture, "share", NULL));
2399 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2400 torture_setting_string(torture, "share", NULL),
2401 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2403 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2404 torture_setting_string(torture, "share", NULL),
2405 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2406 NT_STATUS_MEDIA_WRITE_PROTECTED);
2408 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2409 torture_setting_string(torture, "share", NULL),
2410 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2412 talloc_free(mem_ctx);
2416 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2418 TALLOC_CTX *mem_ctx;
2420 struct smbcli_state *cli;
2421 struct dcerpc_pipe *p;
2422 struct policy_handle lsa_handle;
2424 struct dom_sid *domain_sid;
2426 if (!(mem_ctx = talloc_new(torture))) {
2430 if (!(torture_open_connection_share(
2431 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2432 "IPC$", torture->ev))) {
2433 d_printf("IPC$ connection failed\n");
2434 talloc_free(mem_ctx);
2438 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2439 &ndr_table_lsarpc, &p);
2440 if (!NT_STATUS_IS_OK(status)) {
2441 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2443 talloc_free(mem_ctx);
2448 struct lsa_ObjectAttribute attr;
2449 struct lsa_OpenPolicy2 o;
2450 o.in.system_name = talloc_asprintf(
2451 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2454 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2455 o.out.handle = &lsa_handle;
2456 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2457 if (!NT_STATUS_IS_OK(status)) {
2458 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2459 __location__, nt_errstr(status));
2460 talloc_free(mem_ctx);
2466 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2471 int levels[] = { 2,3,5,6 };
2473 for (i=0; i<ARRAY_SIZE(levels); i++) {
2474 struct lsa_QueryInfoPolicy r;
2475 union lsa_PolicyInformation *info = NULL;
2476 r.in.handle = &lsa_handle;
2477 r.in.level = levels[i];
2479 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2480 if (!NT_STATUS_IS_OK(status)) {
2481 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2482 "failed: %s\n", __location__,
2483 levels[i], nt_errstr(status));
2484 talloc_free(mem_ctx);
2487 if (levels[i] == 5) {
2488 domain_sid = info->account_domain.sid;
2496 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2497 struct smb_iconv_convenience *iconv_convenience,
2500 struct rap_WserverGetInfo r;
2502 char servername[17];
2505 r.in.bufsize = 0xffff;
2507 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2508 if (!NT_STATUS_IS_OK(status)) {
2512 memcpy(servername, r.out.info.info0.name, 16);
2513 servername[16] = '\0';
2515 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2516 return NT_STATUS_NO_MEMORY;
2519 return NT_STATUS_OK;
2523 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2524 struct smbcli_tree *tree,
2525 const char ***printers, int *num_printers)
2527 TALLOC_CTX *mem_ctx;
2529 struct dcerpc_pipe *p;
2530 struct srvsvc_NetShareEnum r;
2531 struct srvsvc_NetShareInfoCtr info_ctr;
2532 struct srvsvc_NetShareCtr1 c1_in;
2533 struct srvsvc_NetShareCtr1 *c1;
2534 uint32_t totalentries = 0;
2537 mem_ctx = talloc_new(ctx);
2538 if (mem_ctx == NULL) {
2539 return NT_STATUS_NO_MEMORY;
2542 status = pipe_bind_smb(mem_ctx, lp_ctx,
2543 tree, "\\srvsvc", &ndr_table_srvsvc,
2545 if (!NT_STATUS_IS_OK(status)) {
2546 d_printf("could not bind to srvsvc pipe\n");
2547 talloc_free(mem_ctx);
2553 info_ctr.ctr.ctr1 = &c1_in;
2555 r.in.server_unc = talloc_asprintf(
2556 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2557 r.in.info_ctr = &info_ctr;
2558 r.in.max_buffer = (uint32_t)-1;
2559 r.in.resume_handle = NULL;
2560 r.out.totalentries = &totalentries;
2561 r.out.info_ctr = &info_ctr;
2563 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2564 if (!NT_STATUS_IS_OK(status)) {
2565 d_printf("NetShareEnum level %u failed - %s\n",
2566 info_ctr.level, nt_errstr(status));
2567 talloc_free(mem_ctx);
2573 c1 = r.out.info_ctr->ctr.ctr1;
2574 for (i=0; i<c1->count; i++) {
2575 if (c1->array[i].type != STYPE_PRINTQ) {
2578 if (!add_string_to_array(ctx, c1->array[i].name,
2579 printers, num_printers)) {
2581 return NT_STATUS_NO_MEMORY;
2585 talloc_free(mem_ctx);
2586 return NT_STATUS_OK;
2589 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2590 const char *servername, int level, int *num_printers)
2592 struct spoolss_EnumPrinters r;
2596 r.in.flags = PRINTER_ENUM_LOCAL;
2597 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2602 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2603 if (!NT_STATUS_IS_OK(status)) {
2604 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2605 __location__, nt_errstr(status));
2609 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2610 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2611 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2612 win_errstr(r.out.result));
2616 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2617 if (blob.data == NULL) {
2618 d_printf("(%s) data_blob_talloc failed\n", __location__);
2622 r.in.buffer = &blob;
2623 r.in.offered = r.out.needed;
2625 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2626 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2627 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2628 "%s\n", __location__, nt_errstr(status),
2629 win_errstr(r.out.result));
2633 *num_printers = r.out.count;
2638 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2639 struct policy_handle *handle, int level,
2640 union spoolss_PrinterInfo **res)
2642 TALLOC_CTX *mem_ctx;
2643 struct spoolss_GetPrinter r;
2647 mem_ctx = talloc_new(ctx);
2648 if (mem_ctx == NULL) {
2649 return NT_STATUS_NO_MEMORY;
2652 r.in.handle = handle;
2657 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2658 if (!NT_STATUS_IS_OK(status)) {
2659 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2660 __location__, nt_errstr(status));
2661 talloc_free(mem_ctx);
2665 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2666 printf("GetPrinter unexpected return code %s, should "
2667 "be WERR_INSUFFICIENT_BUFFER\n",
2668 win_errstr(r.out.result));
2669 talloc_free(mem_ctx);
2670 return NT_STATUS_UNSUCCESSFUL;
2673 r.in.handle = handle;
2675 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2676 if (blob.data == NULL) {
2677 talloc_free(mem_ctx);
2678 return NT_STATUS_NO_MEMORY;
2680 memset(blob.data, 0, blob.length);
2681 r.in.buffer = &blob;
2682 r.in.offered = r.out.needed;
2684 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2685 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2686 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2687 "%s\n", __location__, nt_errstr(status),
2688 win_errstr(r.out.result));
2689 talloc_free(mem_ctx);
2690 return NT_STATUS_IS_OK(status) ?
2691 NT_STATUS_UNSUCCESSFUL : status;
2695 *res = talloc_steal(ctx, r.out.info);
2698 talloc_free(mem_ctx);
2699 return NT_STATUS_OK;
2702 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2704 TALLOC_CTX *mem_ctx;
2706 struct smbcli_state *cli;
2707 struct dcerpc_pipe *p;
2709 struct policy_handle server_handle, printer_handle;
2710 const char **printers;
2712 struct spoolss_UserLevel1 userlevel1;
2715 if (!(mem_ctx = talloc_new(torture))) {
2719 if (!(torture_open_connection_share(
2720 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2721 "IPC$", torture->ev))) {
2722 d_printf("IPC$ connection failed\n");
2723 talloc_free(mem_ctx);
2727 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2728 if (!NT_STATUS_IS_OK(status)) {
2729 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2730 __location__, nt_errstr(status));
2731 talloc_free(mem_ctx);
2735 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2736 &printers, &num_printers))) {
2737 talloc_free(mem_ctx);
2741 if (num_printers == 0) {
2742 d_printf("Did not find printers\n");
2743 talloc_free(mem_ctx);
2747 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2748 &ndr_table_spoolss, &p);
2749 if (!NT_STATUS_IS_OK(status)) {
2750 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2752 talloc_free(mem_ctx);
2756 ZERO_STRUCT(userlevel1);
2757 userlevel1.client = talloc_asprintf(
2758 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2759 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2760 userlevel1.build = 2600;
2761 userlevel1.major = 3;
2762 userlevel1.minor = 0;
2763 userlevel1.processor = 0;
2766 struct spoolss_OpenPrinterEx r;
2769 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2771 r.in.datatype = NULL;
2772 r.in.access_mask = 0;
2774 r.in.userlevel.level1 = &userlevel1;
2775 r.out.handle = &server_handle;
2777 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2778 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2779 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2780 "%s, %s\n", __location__, nt_errstr(status),
2781 win_errstr(r.out.result));
2782 talloc_free(mem_ctx);
2788 struct spoolss_ClosePrinter r;
2790 r.in.handle = &server_handle;
2791 r.out.handle = &server_handle;
2793 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2794 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2795 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2796 "%s, %s\n", __location__, nt_errstr(status),
2797 win_errstr(r.out.result));
2798 talloc_free(mem_ctx);
2804 struct spoolss_OpenPrinterEx r;
2807 r.in.printername = talloc_asprintf(
2808 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2809 r.in.datatype = NULL;
2810 r.in.access_mask = 0;
2812 r.in.userlevel.level1 = &userlevel1;
2813 r.out.handle = &printer_handle;
2815 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2816 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2817 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2818 "%s, %s\n", __location__, nt_errstr(status),
2819 win_errstr(r.out.result));
2820 talloc_free(mem_ctx);
2828 for (i=0; i<8; i++) {
2829 status = getprinterinfo(mem_ctx, p, &printer_handle,
2831 if (!NT_STATUS_IS_OK(status)) {
2832 d_printf("(%s) getprinterinfo %d failed: %s\n",
2833 __location__, i, nt_errstr(status));
2840 struct spoolss_ClosePrinter r;
2842 r.in.handle = &printer_handle;
2843 r.out.handle = &printer_handle;
2845 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2846 if (!NT_STATUS_IS_OK(status)) {
2847 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2848 "%s\n", __location__, nt_errstr(status));
2849 talloc_free(mem_ctx);
2856 if (!enumprinters(mem_ctx, p, servername, 1,
2858 d_printf("(%s) enumprinters failed\n", __location__);
2859 talloc_free(mem_ctx);
2862 if (num_printers != num_enumerated) {
2863 d_printf("(%s) netshareenum gave %d printers, "
2864 "enumprinters lvl 1 gave %d\n", __location__,
2865 num_printers, num_enumerated);
2866 talloc_free(mem_ctx);
2873 if (!enumprinters(mem_ctx, p, servername, 2,
2875 d_printf("(%s) enumprinters failed\n", __location__);
2876 talloc_free(mem_ctx);
2879 if (num_printers != num_enumerated) {
2880 d_printf("(%s) netshareenum gave %d printers, "
2881 "enumprinters lvl 2 gave %d\n", __location__,
2882 num_printers, num_enumerated);
2883 talloc_free(mem_ctx);
2888 talloc_free(mem_ctx);
2893 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2895 TALLOC_CTX *mem_ctx;
2896 struct smbcli_state *cli;
2897 struct dcerpc_pipe *p;
2901 if (!(mem_ctx = talloc_new(torture))) {
2905 if (!(torture_open_connection_share(
2906 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2907 "IPC$", torture->ev))) {
2908 d_printf("IPC$ connection failed\n");
2909 talloc_free(mem_ctx);
2913 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2914 if (!NT_STATUS_IS_OK(status)) {
2915 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2916 __location__, nt_errstr(status));
2917 talloc_free(mem_ctx);
2921 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2922 &ndr_table_wkssvc, &p);
2923 if (!NT_STATUS_IS_OK(status)) {
2924 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2926 talloc_free(mem_ctx);
2931 struct wkssvc_NetWkstaInfo100 wks100;
2932 union wkssvc_NetWkstaInfo info;
2933 struct wkssvc_NetWkstaGetInfo r;
2935 r.in.server_name = "\\foo";
2937 info.info100 = &wks100;
2940 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2941 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2942 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2943 "%s, %s\n", __location__, nt_errstr(status),
2944 win_errstr(r.out.result));
2945 talloc_free(mem_ctx);
2949 if (strcmp(servername,
2950 r.out.info->info100->server_name) != 0) {
2951 d_printf("(%s) servername inconsistency: RAP=%s, "
2952 "dcerpc_wkssvc_NetWksGetInfo=%s",
2953 __location__, servername,
2954 r.out.info->info100->server_name);
2955 talloc_free(mem_ctx);
2960 talloc_free(mem_ctx);
2964 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2965 struct policy_handle *handle)
2967 struct winreg_CloseKey c;
2969 TALLOC_CTX *mem_ctx;
2971 c.in.handle = c.out.handle = handle;
2973 if (!(mem_ctx = talloc_new(p))) {
2974 return NT_STATUS_NO_MEMORY;
2977 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2978 talloc_free(mem_ctx);
2980 if (!NT_STATUS_IS_OK(status)) {
2984 if (!W_ERROR_IS_OK(c.out.result)) {
2985 return werror_to_ntstatus(c.out.result);
2988 return NT_STATUS_OK;
2991 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2992 TALLOC_CTX *mem_ctx)
2994 uint32_t enum_index = 0;
2997 struct winreg_EnumValue r;
2998 struct winreg_StringBuf name;
2999 enum winreg_Type type = 0;
3002 uint32_t size, length;
3004 r.in.handle = handle;
3005 r.in.enum_index = enum_index;
3008 r.in.name = r.out.name = &name;
3014 r.in.length = &length;
3016 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
3017 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3018 return NT_STATUS_OK;
3024 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
3025 TALLOC_CTX *mem_ctx, int depth)
3027 struct winreg_EnumKey r;
3028 struct winreg_StringBuf class, name;
3033 return NT_STATUS_OK;
3039 r.in.handle = handle;
3040 r.in.enum_index = 0;
3042 r.in.keyclass = &class;
3044 r.in.last_changed_time = &t;
3047 TALLOC_CTX *tmp_ctx;
3048 struct winreg_OpenKey o;
3049 struct policy_handle key_handle;
3052 if (!(tmp_ctx = talloc_new(mem_ctx))) {
3053 return NT_STATUS_NO_MEMORY;
3059 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
3060 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3061 /* We're done enumerating */
3062 talloc_free(tmp_ctx);
3063 return NT_STATUS_OK;
3066 for (i=0; i<10-depth; i++)
3068 printf("%s\n", r.out.name->name);
3071 o.in.parent_handle = handle;
3072 o.in.keyname.name = r.out.name->name;
3074 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3075 o.out.handle = &key_handle;
3077 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
3078 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3079 enumkeys(p, &key_handle, tmp_ctx, depth-1);
3080 enumvalues(p, &key_handle, tmp_ctx);
3081 status = winreg_close(p, &key_handle);
3082 if (!NT_STATUS_IS_OK(status)) {
3087 talloc_free(tmp_ctx);
3089 r.in.enum_index += 1;
3092 return NT_STATUS_OK;
3095 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3097 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3098 const char *name, winreg_open_fn open_fn)
3100 struct policy_handle handle;
3101 struct winreg_OpenHKLM r;
3104 r.in.system_name = 0;
3105 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3106 r.out.handle = &handle;
3108 status = open_fn(p, mem_ctx, &r);
3109 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3110 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3111 nt_errstr(status), win_errstr(r.out.result));
3115 enumkeys(p, &handle, mem_ctx, 4);
3117 status = winreg_close(p, &handle);
3118 if (!NT_STATUS_IS_OK(status)) {
3119 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3120 __location__, nt_errstr(status));
3127 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3130 struct dcerpc_pipe *p;
3131 TALLOC_CTX *mem_ctx;
3137 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3138 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3139 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3140 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3141 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3146 mem_ctx = talloc_init("torture_rpc_winreg");
3148 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3150 if (!NT_STATUS_IS_OK(status)) {
3151 talloc_free(mem_ctx);
3156 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3158 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3159 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3164 talloc_free(mem_ctx);
3169 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3170 struct loadparm_context *lp_ctx,
3171 struct smbcli_state *cli,
3173 struct srvsvc_NetShareInfo502 **info502)
3175 struct smbcli_tree *ipc;
3176 struct dcerpc_pipe *p;
3177 struct srvsvc_NetShareGetInfo r;
3178 union srvsvc_NetShareInfo info;
3181 if (!(p = dcerpc_pipe_init(cli,
3182 cli->transport->socket->event.ctx,
3183 lp_iconv_convenience(lp_ctx)))) {
3184 status = NT_STATUS_NO_MEMORY;
3188 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3189 if (!NT_STATUS_IS_OK(status)) {
3193 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3194 if (!NT_STATUS_IS_OK(status)) {
3195 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3200 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3201 if (!NT_STATUS_IS_OK(status)) {
3202 d_printf("dcerpc_bind_auth_none failed: %s\n",
3207 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3208 dcerpc_server_name(p));
3209 r.in.share_name = share;
3213 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3214 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3215 d_printf("(%s) srvsvc_NetShareGetInfo failed: %s, %s\n", __location__,
3216 nt_errstr(status), win_errstr(r.out.result));
3220 *info502 = talloc_move(mem_ctx, &info.info502);
3221 return NT_STATUS_OK;
3229 * Get us a handle on HKLM\
3232 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3233 struct smbcli_state *cli,
3234 struct smb_iconv_convenience *iconv_convenience,
3235 struct dcerpc_pipe **pipe_p,
3236 struct policy_handle **handle)
3238 struct smbcli_tree *ipc;
3239 struct dcerpc_pipe *p;
3240 struct winreg_OpenHKLM r;
3242 struct policy_handle *result;
3244 result = talloc(mem_ctx, struct policy_handle);
3246 if (result == NULL) {
3247 return NT_STATUS_NO_MEMORY;
3250 if (!(p = dcerpc_pipe_init(result,
3251 cli->transport->socket->event.ctx,
3252 iconv_convenience))) {
3253 status = NT_STATUS_NO_MEMORY;
3257 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3258 if (!NT_STATUS_IS_OK(status)) {
3262 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3263 if (!NT_STATUS_IS_OK(status)) {
3264 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3269 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3270 if (!NT_STATUS_IS_OK(status)) {
3271 d_printf("dcerpc_bind_auth_none failed: %s\n",
3276 r.in.system_name = 0;
3277 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3278 r.out.handle = result;
3280 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3281 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3282 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3283 nt_errstr(status), win_errstr(r.out.result));
3289 return NT_STATUS_OK;
3292 talloc_free(result);
3296 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3297 struct smb_iconv_convenience *iconv_convenience,
3298 const char *sharename)
3300 struct dcerpc_pipe *p;
3301 struct policy_handle *hklm = NULL;
3302 struct policy_handle new_handle;
3303 struct winreg_CreateKey c;
3304 struct winreg_CloseKey cl;
3305 enum winreg_CreateAction action_taken;
3307 TALLOC_CTX *mem_ctx;
3309 mem_ctx = talloc_new(cli);
3310 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3312 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3313 if (!NT_STATUS_IS_OK(status)) {
3314 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3319 c.in.name.name = talloc_asprintf(
3320 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3321 if (c.in.name.name == NULL) {
3322 d_printf("talloc_asprintf failed\n");
3323 status = NT_STATUS_NO_MEMORY;
3326 c.in.keyclass.name = "";
3328 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3329 c.in.secdesc = NULL;
3330 c.in.action_taken = &action_taken;
3331 c.out.new_handle = &new_handle;
3332 c.out.action_taken = &action_taken;
3334 status = dcerpc_winreg_CreateKey(p, p, &c);
3335 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3336 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3337 nt_errstr(status), win_errstr(c.out.result));
3341 cl.in.handle = &new_handle;
3342 cl.out.handle = &new_handle;
3343 status = dcerpc_winreg_CloseKey(p, p, &cl);
3344 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3345 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3346 nt_errstr(status), win_errstr(cl.out.result));
3352 talloc_free(mem_ctx);
3356 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3357 struct smbcli_state *cli,
3358 const char *sharename)
3360 struct dcerpc_pipe *p;
3361 struct policy_handle *hklm = NULL;
3362 struct winreg_DeleteKey d;
3364 TALLOC_CTX *mem_ctx;
3366 mem_ctx = talloc_new(cli);
3367 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3369 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3371 if (!NT_STATUS_IS_OK(status)) {
3372 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3377 d.in.key.name = talloc_asprintf(
3378 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3379 if (d.in.key.name == NULL) {
3380 d_printf("talloc_asprintf failed\n");
3381 status = NT_STATUS_NO_MEMORY;
3384 status = dcerpc_winreg_DeleteKey(p, p, &d);
3385 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3386 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3387 nt_errstr(status), win_errstr(d.out.result));
3392 talloc_free(mem_ctx);
3396 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3397 struct loadparm_context *lp_ctx,
3398 const char *sharename,
3399 const char *parameter,
3402 struct dcerpc_pipe *p = NULL;
3403 struct policy_handle *hklm = NULL, key_handle;
3404 struct winreg_OpenKey o;
3405 struct winreg_SetValue s;
3410 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3411 if (!NT_STATUS_IS_OK(status)) {
3412 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3416 o.in.parent_handle = hklm;
3417 o.in.keyname.name = talloc_asprintf(
3418 hklm, "software\\samba\\smbconf\\%s", sharename);
3419 if (o.in.keyname.name == NULL) {
3420 d_printf("talloc_asprintf failed\n");
3421 status = NT_STATUS_NO_MEMORY;
3425 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3426 o.out.handle = &key_handle;
3428 status = dcerpc_winreg_OpenKey(p, p, &o);
3429 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3430 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3431 nt_errstr(status), win_errstr(o.out.result));
3435 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3436 value, &type, &val)) {
3437 d_printf("(%s) reg_string_to_val failed\n", __location__);
3441 s.in.handle = &key_handle;
3442 s.in.name.name = parameter;
3444 s.in.data = val.data;
3445 s.in.size = val.length;
3447 status = dcerpc_winreg_SetValue(p, p, &s);
3448 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3449 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3450 nt_errstr(status), win_errstr(s.out.result));
3459 bool torture_samba3_regconfig(struct torture_context *torture)
3461 struct smbcli_state *cli;
3462 struct srvsvc_NetShareInfo502 *i = NULL;
3465 const char *comment = "Dummer Kommentar";
3467 if (!(torture_open_connection(&cli, torture, 0))) {
3471 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3472 if (!NT_STATUS_IS_OK(status)) {
3473 torture_warning(torture, "torture_samba3_createshare failed: "
3474 "%s\n", nt_errstr(status));
3478 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3479 if (!NT_STATUS_IS_OK(status)) {
3480 torture_warning(torture, "torture_samba3_setconfig failed: "
3481 "%s\n", nt_errstr(status));
3485 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3486 if (!NT_STATUS_IS_OK(status)) {
3487 torture_warning(torture, "get_shareinfo failed: "
3488 "%s\n", nt_errstr(status));
3492 if (strcmp(comment, i->comment) != 0) {
3493 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3494 comment, i->comment);
3498 status = torture_samba3_deleteshare(torture, cli, "blubber");
3499 if (!NT_STATUS_IS_OK(status)) {
3500 torture_warning(torture, "torture_samba3_deleteshare failed: "
3501 "%s\n", nt_errstr(status));