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/rap/rap.h"
25 #include "torture/torture.h"
26 #include "torture/util.h"
27 #include "torture/rap/proto.h"
28 #include "librpc/gen_ndr/ndr_lsa.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_samr.h"
31 #include "librpc/gen_ndr/ndr_samr_c.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #include "librpc/gen_ndr/ndr_netlogon_c.h"
34 #include "librpc/gen_ndr/ndr_srvsvc.h"
35 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
36 #include "librpc/gen_ndr/ndr_spoolss.h"
37 #include "librpc/gen_ndr/ndr_spoolss_c.h"
38 #include "librpc/gen_ndr/ndr_winreg.h"
39 #include "librpc/gen_ndr/ndr_winreg_c.h"
40 #include "librpc/gen_ndr/ndr_wkssvc.h"
41 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
42 #include "lib/cmdline/popt_common.h"
43 #include "librpc/rpc/dcerpc.h"
44 #include "torture/rpc/rpc.h"
45 #include "libcli/libcli.h"
46 #include "libcli/composite/composite.h"
47 #include "libcli/smb_composite/smb_composite.h"
48 #include "libcli/auth/libcli_auth.h"
49 #include "lib/crypto/crypto.h"
50 #include "libcli/security/proto.h"
51 #include "param/param.h"
52 #include "lib/registry/registry.h"
53 #include "libcli/resolve/resolve.h"
56 * This tests a RPC call using an invalid vuid
59 bool torture_bind_authcontext(struct torture_context *torture)
64 struct lsa_ObjectAttribute objectattr;
65 struct lsa_OpenPolicy2 openpolicy;
66 struct policy_handle handle;
67 struct lsa_Close close_handle;
68 struct smbcli_session *tmp;
69 struct smbcli_session *session2;
70 struct smbcli_state *cli;
71 struct dcerpc_pipe *lsa_pipe;
72 struct cli_credentials *anon_creds;
73 struct smb_composite_sesssetup setup;
74 struct smbcli_options options;
76 mem_ctx = talloc_init("torture_bind_authcontext");
78 if (mem_ctx == NULL) {
79 d_printf("talloc_init failed\n");
83 lp_smbcli_options(torture->lp_ctx, &options);
85 status = smbcli_full_connection(mem_ctx, &cli,
86 torture_setting_string(torture, "host", NULL),
87 lp_smb_ports(torture->lp_ctx),
88 "IPC$", NULL, cmdline_credentials,
89 lp_resolve_context(torture->lp_ctx),
91 if (!NT_STATUS_IS_OK(status)) {
92 d_printf("smbcli_full_connection failed: %s\n",
97 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
98 lp_iconv_convenience(torture->lp_ctx));
99 if (lsa_pipe == NULL) {
100 d_printf("dcerpc_pipe_init failed\n");
104 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
105 if (!NT_STATUS_IS_OK(status)) {
106 d_printf("dcerpc_pipe_open_smb failed: %s\n",
111 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
112 if (!NT_STATUS_IS_OK(status)) {
113 d_printf("dcerpc_bind_auth_none failed: %s\n",
118 openpolicy.in.system_name =talloc_asprintf(
119 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
120 ZERO_STRUCT(objectattr);
121 openpolicy.in.attr = &objectattr;
122 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
123 openpolicy.out.handle = &handle;
125 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
127 if (!NT_STATUS_IS_OK(status)) {
128 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
133 close_handle.in.handle = &handle;
134 close_handle.out.handle = &handle;
136 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
137 if (!NT_STATUS_IS_OK(status)) {
138 d_printf("dcerpc_lsa_Close failed: %s\n",
143 session2 = smbcli_session_init(cli->transport, mem_ctx, false);
144 if (session2 == NULL) {
145 d_printf("smbcli_session_init failed\n");
149 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
150 d_printf("create_anon_creds failed\n");
154 setup.in.sesskey = cli->transport->negotiate.sesskey;
155 setup.in.capabilities = cli->transport->negotiate.capabilities;
156 setup.in.workgroup = "";
157 setup.in.credentials = anon_creds;
159 status = smb_composite_sesssetup(session2, &setup);
160 if (!NT_STATUS_IS_OK(status)) {
161 d_printf("anon session setup failed: %s\n",
165 session2->vuid = setup.out.vuid;
167 tmp = cli->tree->session;
168 cli->tree->session = session2;
170 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
172 cli->tree->session = tmp;
173 talloc_free(lsa_pipe);
176 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
177 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
178 "expected NT_STATUS_INVALID_HANDLE\n",
185 talloc_free(mem_ctx);
190 * Bind to lsa using a specific auth method
193 static bool bindtest(struct smbcli_state *cli,
194 struct cli_credentials *credentials,
195 struct loadparm_context *lp_ctx,
196 uint8_t auth_type, uint8_t auth_level)
202 struct dcerpc_pipe *lsa_pipe;
203 struct lsa_ObjectAttribute objectattr;
204 struct lsa_OpenPolicy2 openpolicy;
205 struct lsa_QueryInfoPolicy query;
206 struct policy_handle handle;
207 struct lsa_Close close_handle;
209 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
210 d_printf("talloc_init failed\n");
214 lsa_pipe = dcerpc_pipe_init(mem_ctx,
215 cli->transport->socket->event.ctx,
216 lp_iconv_convenience(lp_ctx));
217 if (lsa_pipe == NULL) {
218 d_printf("dcerpc_pipe_init failed\n");
222 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
223 if (!NT_STATUS_IS_OK(status)) {
224 d_printf("dcerpc_pipe_open_smb failed: %s\n",
229 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
230 credentials, lp_ctx, auth_type, auth_level,
232 if (!NT_STATUS_IS_OK(status)) {
233 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
237 openpolicy.in.system_name =talloc_asprintf(
238 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
239 ZERO_STRUCT(objectattr);
240 openpolicy.in.attr = &objectattr;
241 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
242 openpolicy.out.handle = &handle;
244 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
246 if (!NT_STATUS_IS_OK(status)) {
247 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
252 query.in.handle = &handle;
253 query.in.level = LSA_POLICY_INFO_DOMAIN;
255 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
256 if (!NT_STATUS_IS_OK(status)) {
257 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
262 close_handle.in.handle = &handle;
263 close_handle.out.handle = &handle;
265 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
266 if (!NT_STATUS_IS_OK(status)) {
267 d_printf("dcerpc_lsa_Close failed: %s\n",
274 talloc_free(mem_ctx);
279 * test authenticated RPC binds with the variants Samba3 does support
282 bool torture_bind_samba3(struct torture_context *torture)
287 struct smbcli_state *cli;
288 struct smbcli_options options;
290 mem_ctx = talloc_init("torture_bind_authcontext");
292 if (mem_ctx == NULL) {
293 d_printf("talloc_init failed\n");
297 lp_smbcli_options(torture->lp_ctx, &options);
299 status = smbcli_full_connection(mem_ctx, &cli,
300 torture_setting_string(torture, "host", NULL),
301 lp_smb_ports(torture->lp_ctx),
302 "IPC$", NULL, cmdline_credentials,
303 lp_resolve_context(torture->lp_ctx),
305 if (!NT_STATUS_IS_OK(status)) {
306 d_printf("smbcli_full_connection failed: %s\n",
313 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
314 DCERPC_AUTH_LEVEL_INTEGRITY);
315 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
316 DCERPC_AUTH_LEVEL_PRIVACY);
317 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
318 DCERPC_AUTH_LEVEL_INTEGRITY);
319 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
320 DCERPC_AUTH_LEVEL_PRIVACY);
323 talloc_free(mem_ctx);
328 * Lookup or create a user and return all necessary info
331 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
333 struct loadparm_context *lp_ctx,
334 struct cli_credentials *admin_creds,
337 const char *username,
339 struct dcerpc_pipe **result_pipe,
340 struct policy_handle **result_handle,
341 struct dom_sid **sid)
343 struct dcerpc_pipe *samr_pipe;
345 struct policy_handle conn_handle;
346 struct policy_handle domain_handle;
347 struct policy_handle *user_handle;
348 struct samr_Connect2 conn;
349 struct samr_EnumDomains enumdom;
350 uint32_t resume_handle = 0;
351 struct samr_LookupDomain l;
353 struct lsa_String domain_name;
354 struct lsa_String user_name;
355 struct samr_OpenDomain o;
356 struct samr_CreateUser2 c;
357 uint32_t user_rid,access_granted;
359 samr_pipe = dcerpc_pipe_init(mem_ctx,
360 cli->transport->socket->event.ctx,
361 lp_iconv_convenience(lp_ctx));
362 if (samr_pipe == NULL) {
363 d_printf("dcerpc_pipe_init failed\n");
364 status = NT_STATUS_NO_MEMORY;
368 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
369 if (!NT_STATUS_IS_OK(status)) {
370 d_printf("dcerpc_pipe_open_smb failed: %s\n",
375 if (admin_creds != NULL) {
376 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
377 admin_creds, lp_ctx, auth_type, auth_level,
379 if (!NT_STATUS_IS_OK(status)) {
380 d_printf("dcerpc_bind_auth failed: %s\n",
385 /* We must have an authenticated SMB connection */
386 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
387 if (!NT_STATUS_IS_OK(status)) {
388 d_printf("dcerpc_bind_auth_none failed: %s\n",
394 conn.in.system_name = talloc_asprintf(
395 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
396 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
397 conn.out.connect_handle = &conn_handle;
399 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
400 if (!NT_STATUS_IS_OK(status)) {
401 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
405 enumdom.in.connect_handle = &conn_handle;
406 enumdom.in.resume_handle = &resume_handle;
407 enumdom.in.buf_size = (uint32_t)-1;
408 enumdom.out.resume_handle = &resume_handle;
410 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
411 if (!NT_STATUS_IS_OK(status)) {
412 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
416 if (enumdom.out.num_entries != 2) {
417 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
418 enumdom.out.num_entries);
419 status = NT_STATUS_UNSUCCESSFUL;
423 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
426 l.in.connect_handle = &conn_handle;
427 domain_name.string = enumdom.out.sam->entries[0].name.string;
428 *domain = talloc_strdup(mem_ctx, domain_name.string);
429 l.in.domain_name = &domain_name;
431 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
432 if (!NT_STATUS_IS_OK(status)) {
433 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
437 o.in.connect_handle = &conn_handle;
438 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
439 o.in.sid = l.out.sid;
440 o.out.domain_handle = &domain_handle;
442 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
443 if (!NT_STATUS_IS_OK(status)) {
444 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
448 c.in.domain_handle = &domain_handle;
449 user_name.string = username;
450 c.in.account_name = &user_name;
451 c.in.acct_flags = ACB_NORMAL;
452 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
453 user_handle = talloc(mem_ctx, struct policy_handle);
454 c.out.user_handle = user_handle;
455 c.out.access_granted = &access_granted;
456 c.out.rid = &user_rid;
458 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
460 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
461 struct samr_LookupNames ln;
462 struct samr_OpenUser ou;
464 ln.in.domain_handle = &domain_handle;
466 ln.in.names = &user_name;
468 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
469 if (!NT_STATUS_IS_OK(status)) {
470 d_printf("samr_LookupNames failed: %s\n",
475 ou.in.domain_handle = &domain_handle;
476 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
477 user_rid = ou.in.rid = ln.out.rids.ids[0];
478 ou.out.user_handle = user_handle;
480 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
481 if (!NT_STATUS_IS_OK(status)) {
482 d_printf("samr_OpenUser failed: %s\n",
488 if (!NT_STATUS_IS_OK(status)) {
489 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
493 *result_pipe = samr_pipe;
494 *result_handle = user_handle;
496 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
508 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
509 struct loadparm_context *lp_ctx,
510 struct cli_credentials *admin_creds,
511 const char *username, const char *password,
513 struct dom_sid **user_sid)
517 struct dcerpc_pipe *samr_pipe;
518 struct policy_handle *wks_handle;
521 if (!(tmp_ctx = talloc_new(mem_ctx))) {
522 d_printf("talloc_init failed\n");
526 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
527 DCERPC_AUTH_TYPE_NTLMSSP,
528 DCERPC_AUTH_LEVEL_INTEGRITY,
529 username, domain_name, &samr_pipe, &wks_handle,
531 if (!NT_STATUS_IS_OK(status)) {
532 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
537 struct samr_SetUserInfo2 sui2;
538 struct samr_SetUserInfo sui;
539 struct samr_QueryUserInfo qui;
540 union samr_UserInfo u_info;
541 DATA_BLOB session_key;
545 encode_pw_buffer(u_info.info23.password.data, password,
548 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
549 if (!NT_STATUS_IS_OK(status)) {
550 d_printf("dcerpc_fetch_session_key failed\n");
553 arcfour_crypt_blob(u_info.info23.password.data, 516,
555 u_info.info23.info.password_expired = 0;
556 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
557 SAMR_FIELD_PASSWORD2 |
558 SAMR_FIELD_EXPIRED_FLAG;
559 sui2.in.user_handle = wks_handle;
560 sui2.in.info = &u_info;
563 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
564 if (!NT_STATUS_IS_OK(status)) {
565 d_printf("samr_SetUserInfo(23) failed: %s\n",
570 u_info.info16.acct_flags = ACB_NORMAL;
571 sui.in.user_handle = wks_handle;
572 sui.in.info = &u_info;
575 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
576 if (!NT_STATUS_IS_OK(status)) {
577 d_printf("samr_SetUserInfo(16) failed\n");
581 qui.in.user_handle = wks_handle;
584 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
585 if (!NT_STATUS_IS_OK(status)) {
586 d_printf("samr_QueryUserInfo(21) failed\n");
590 qui.out.info->info21.allow_password_change = 0;
591 qui.out.info->info21.force_password_change = 0;
592 qui.out.info->info21.account_name.string = NULL;
593 qui.out.info->info21.rid = 0;
594 qui.out.info->info21.acct_expiry = 0;
595 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
597 u_info.info21 = qui.out.info->info21;
598 sui.in.user_handle = wks_handle;
599 sui.in.info = &u_info;
602 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
603 if (!NT_STATUS_IS_OK(status)) {
604 d_printf("samr_SetUserInfo(21) failed\n");
609 *domain_name= talloc_steal(mem_ctx, *domain_name);
610 *user_sid = talloc_steal(mem_ctx, *user_sid);
613 talloc_free(tmp_ctx);
621 static bool delete_user(struct smbcli_state *cli,
622 struct loadparm_context *lp_ctx,
623 struct cli_credentials *admin_creds,
624 const char *username)
629 struct dcerpc_pipe *samr_pipe;
630 struct policy_handle *user_handle;
633 if ((mem_ctx = talloc_init("leave")) == NULL) {
634 d_printf("talloc_init failed\n");
638 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
639 DCERPC_AUTH_TYPE_NTLMSSP,
640 DCERPC_AUTH_LEVEL_INTEGRITY,
641 username, &dom_name, &samr_pipe,
644 if (!NT_STATUS_IS_OK(status)) {
645 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
650 struct samr_DeleteUser d;
652 d.in.user_handle = user_handle;
653 d.out.user_handle = user_handle;
655 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
656 if (!NT_STATUS_IS_OK(status)) {
657 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
665 talloc_free(mem_ctx);
670 * Do a Samba3-style join
673 static bool join3(struct smbcli_state *cli,
674 struct loadparm_context *lp_ctx,
676 struct cli_credentials *admin_creds,
677 struct cli_credentials *wks_creds)
682 struct dcerpc_pipe *samr_pipe;
683 struct policy_handle *wks_handle;
686 if ((mem_ctx = talloc_init("join3")) == NULL) {
687 d_printf("talloc_init failed\n");
691 status = get_usr_handle(
692 cli, mem_ctx, lp_ctx, admin_creds,
693 DCERPC_AUTH_TYPE_NTLMSSP,
694 DCERPC_AUTH_LEVEL_PRIVACY,
695 talloc_asprintf(mem_ctx, "%s$",
696 cli_credentials_get_workstation(wks_creds)),
697 &dom_name, &samr_pipe, &wks_handle, NULL);
699 if (!NT_STATUS_IS_OK(status)) {
700 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
704 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
707 struct samr_SetUserInfo2 sui2;
708 union samr_UserInfo u_info;
709 struct samr_UserInfo21 *i21 = &u_info.info25.info;
710 DATA_BLOB session_key;
711 DATA_BLOB confounded_session_key = data_blob_talloc(
713 struct MD5Context ctx;
714 uint8_t confounder[16];
718 i21->full_name.string = talloc_asprintf(
720 cli_credentials_get_workstation(wks_creds));
721 i21->acct_flags = ACB_WSTRUST;
722 i21->fields_present = SAMR_FIELD_FULL_NAME |
723 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
725 encode_pw_buffer(u_info.info25.password.data,
726 cli_credentials_get_password(wks_creds),
728 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
729 if (!NT_STATUS_IS_OK(status)) {
730 d_printf("dcerpc_fetch_session_key failed: %s\n",
734 generate_random_buffer((uint8_t *)confounder, 16);
737 MD5Update(&ctx, confounder, 16);
738 MD5Update(&ctx, session_key.data, session_key.length);
739 MD5Final(confounded_session_key.data, &ctx);
741 arcfour_crypt_blob(u_info.info25.password.data, 516,
742 &confounded_session_key);
743 memcpy(&u_info.info25.password.data[516], confounder, 16);
745 sui2.in.user_handle = wks_handle;
747 sui2.in.info = &u_info;
749 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
750 if (!NT_STATUS_IS_OK(status)) {
751 d_printf("samr_SetUserInfo2(25) failed: %s\n",
756 struct samr_SetUserInfo2 sui2;
757 struct samr_SetUserInfo sui;
758 union samr_UserInfo u_info;
759 DATA_BLOB session_key;
761 encode_pw_buffer(u_info.info24.password.data,
762 cli_credentials_get_password(wks_creds),
764 u_info.info24.pw_len =
765 strlen_m(cli_credentials_get_password(wks_creds))*2;
767 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
768 if (!NT_STATUS_IS_OK(status)) {
769 d_printf("dcerpc_fetch_session_key failed\n");
772 arcfour_crypt_blob(u_info.info24.password.data, 516,
774 sui2.in.user_handle = wks_handle;
775 sui2.in.info = &u_info;
778 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
779 if (!NT_STATUS_IS_OK(status)) {
780 d_printf("samr_SetUserInfo(24) failed: %s\n",
785 u_info.info16.acct_flags = ACB_WSTRUST;
786 sui.in.user_handle = wks_handle;
787 sui.in.info = &u_info;
790 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
791 if (!NT_STATUS_IS_OK(status)) {
792 d_printf("samr_SetUserInfo(16) failed\n");
800 talloc_free(mem_ctx);
805 * Do a ReqChallenge/Auth2 and get the wks creds
808 static bool auth2(struct smbcli_state *cli,
809 struct cli_credentials *wks_cred)
812 struct dcerpc_pipe *net_pipe;
815 struct netr_ServerReqChallenge r;
816 struct netr_Credential netr_cli_creds;
817 struct netr_Credential netr_srv_creds;
818 uint32_t negotiate_flags;
819 struct netr_ServerAuthenticate2 a;
820 struct creds_CredentialState *creds_state;
821 struct netr_Credential netr_cred;
822 struct samr_Password mach_pw;
824 mem_ctx = talloc_new(NULL);
825 if (mem_ctx == NULL) {
826 d_printf("talloc_new failed\n");
830 net_pipe = dcerpc_pipe_init(mem_ctx,
831 cli->transport->socket->event.ctx,
832 lp_iconv_convenience(global_loadparm));
833 if (net_pipe == NULL) {
834 d_printf("dcerpc_pipe_init failed\n");
838 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
839 if (!NT_STATUS_IS_OK(status)) {
840 d_printf("dcerpc_pipe_open_smb failed: %s\n",
845 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
846 if (!NT_STATUS_IS_OK(status)) {
847 d_printf("dcerpc_bind_auth_none failed: %s\n",
852 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
853 r.in.server_name = talloc_asprintf(
854 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
855 if (r.in.server_name == NULL) {
856 d_printf("talloc_asprintf failed\n");
859 generate_random_buffer(netr_cli_creds.data,
860 sizeof(netr_cli_creds.data));
861 r.in.credentials = &netr_cli_creds;
862 r.out.credentials = &netr_srv_creds;
864 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
865 if (!NT_STATUS_IS_OK(status)) {
866 d_printf("netr_ServerReqChallenge failed: %s\n",
871 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
872 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
874 creds_state = talloc(mem_ctx, struct creds_CredentialState);
875 creds_client_init(creds_state, r.in.credentials,
876 r.out.credentials, &mach_pw,
877 &netr_cred, negotiate_flags);
879 a.in.server_name = talloc_asprintf(
880 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
881 a.in.account_name = talloc_asprintf(
882 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
883 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
884 a.in.secure_channel_type = SEC_CHAN_WKSTA;
885 a.in.negotiate_flags = &negotiate_flags;
886 a.out.negotiate_flags = &negotiate_flags;
887 a.in.credentials = &netr_cred;
888 a.out.credentials = &netr_cred;
890 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
891 if (!NT_STATUS_IS_OK(status)) {
892 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
897 if (!creds_client_check(creds_state, a.out.credentials)) {
898 d_printf("creds_client_check failed\n");
902 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
907 talloc_free(mem_ctx);
912 * Do a couple of schannel protected Netlogon ops: Interactive and Network
913 * login, and change the wks password
916 static bool schan(struct smbcli_state *cli,
917 struct loadparm_context *lp_ctx,
918 struct cli_credentials *wks_creds,
919 struct cli_credentials *user_creds)
924 struct dcerpc_pipe *net_pipe;
927 mem_ctx = talloc_new(NULL);
928 if (mem_ctx == NULL) {
929 d_printf("talloc_new failed\n");
933 net_pipe = dcerpc_pipe_init(mem_ctx,
934 cli->transport->socket->event.ctx,
935 lp_iconv_convenience(lp_ctx));
936 if (net_pipe == NULL) {
937 d_printf("dcerpc_pipe_init failed\n");
941 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
942 if (!NT_STATUS_IS_OK(status)) {
943 d_printf("dcerpc_pipe_open_smb failed: %s\n",
949 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
950 DCERPC_DEBUG_PRINT_OUT;
953 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
954 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
955 wks_creds, lp_ctx, DCERPC_AUTH_TYPE_SCHANNEL,
956 DCERPC_AUTH_LEVEL_PRIVACY,
959 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
961 if (!NT_STATUS_IS_OK(status)) {
962 d_printf("schannel bind failed: %s\n", nt_errstr(status));
967 for (i=2; i<4; i++) {
969 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
970 struct creds_CredentialState *creds_state;
971 struct netr_Authenticator netr_auth, netr_auth2;
972 struct netr_NetworkInfo ninfo;
973 struct netr_PasswordInfo pinfo;
974 struct netr_LogonSamLogon r;
976 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
977 CLI_CRED_NTLMv2_AUTH;
979 chal = data_blob_talloc(mem_ctx, NULL, 8);
980 if (chal.data == NULL) {
981 d_printf("data_blob_talloc failed\n");
985 generate_random_buffer(chal.data, chal.length);
986 names_blob = NTLMv2_generate_names_blob(
987 mem_ctx, cli_credentials_get_workstation(user_creds),
988 cli_credentials_get_domain(user_creds));
989 status = cli_credentials_get_ntlm_response(
990 user_creds, mem_ctx, &flags, chal, names_blob,
991 &lm_resp, &nt_resp, NULL, NULL);
992 if (!NT_STATUS_IS_OK(status)) {
993 d_printf("cli_credentials_get_ntlm_response failed:"
994 " %s\n", nt_errstr(status));
998 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
999 creds_client_authenticator(creds_state, &netr_auth);
1001 ninfo.identity_info.account_name.string =
1002 cli_credentials_get_username(user_creds);
1003 ninfo.identity_info.domain_name.string =
1004 cli_credentials_get_domain(user_creds);
1005 ninfo.identity_info.parameter_control = 0;
1006 ninfo.identity_info.logon_id_low = 0;
1007 ninfo.identity_info.logon_id_high = 0;
1008 ninfo.identity_info.workstation.string =
1009 cli_credentials_get_workstation(user_creds);
1010 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1011 ninfo.nt.length = nt_resp.length;
1012 ninfo.nt.data = nt_resp.data;
1013 ninfo.lm.length = lm_resp.length;
1014 ninfo.lm.data = lm_resp.data;
1016 r.in.server_name = talloc_asprintf(
1017 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1018 ZERO_STRUCT(netr_auth2);
1019 r.in.computer_name =
1020 cli_credentials_get_workstation(wks_creds);
1021 r.in.credential = &netr_auth;
1022 r.in.return_authenticator = &netr_auth2;
1023 r.in.logon_level = 2;
1024 r.in.validation_level = i;
1025 r.in.logon.network = &ninfo;
1026 r.out.return_authenticator = NULL;
1028 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1029 if (!NT_STATUS_IS_OK(status)) {
1030 d_printf("netr_LogonSamLogon failed: %s\n",
1035 if ((r.out.return_authenticator == NULL) ||
1036 (!creds_client_check(creds_state,
1037 &r.out.return_authenticator->cred))) {
1038 d_printf("Credentials check failed!\n");
1042 creds_client_authenticator(creds_state, &netr_auth);
1044 pinfo.identity_info = ninfo.identity_info;
1045 ZERO_STRUCT(pinfo.lmpassword.hash);
1046 E_md4hash(cli_credentials_get_password(user_creds),
1047 pinfo.ntpassword.hash);
1048 session_key = data_blob_talloc(mem_ctx,
1049 creds_state->session_key, 16);
1050 arcfour_crypt_blob(pinfo.ntpassword.hash,
1051 sizeof(pinfo.ntpassword.hash),
1054 r.in.logon_level = 1;
1055 r.in.logon.password = &pinfo;
1056 r.out.return_authenticator = NULL;
1058 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1059 if (!NT_STATUS_IS_OK(status)) {
1060 d_printf("netr_LogonSamLogon failed: %s\n",
1065 if ((r.out.return_authenticator == NULL) ||
1066 (!creds_client_check(creds_state,
1067 &r.out.return_authenticator->cred))) {
1068 d_printf("Credentials check failed!\n");
1074 struct netr_ServerPasswordSet s;
1075 char *password = generate_random_str(wks_creds, 8);
1076 struct creds_CredentialState *creds_state;
1078 s.in.server_name = talloc_asprintf(
1079 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1080 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1081 s.in.account_name = talloc_asprintf(
1082 mem_ctx, "%s$", s.in.computer_name);
1083 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1084 E_md4hash(password, s.in.new_password.hash);
1086 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1087 creds_des_encrypt(creds_state, &s.in.new_password);
1088 creds_client_authenticator(creds_state, &s.in.credential);
1090 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1091 if (!NT_STATUS_IS_OK(status)) {
1092 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1096 if (!creds_client_check(creds_state,
1097 &s.out.return_authenticator.cred)) {
1098 printf("Credential chaining failed\n");
1101 cli_credentials_set_password(wks_creds, password,
1107 talloc_free(mem_ctx);
1112 * Delete the wks account again
1115 static bool leave(struct smbcli_state *cli,
1116 struct loadparm_context *lp_ctx,
1117 struct cli_credentials *admin_creds,
1118 struct cli_credentials *wks_creds)
1120 char *wks_name = talloc_asprintf(
1121 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1124 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1125 talloc_free(wks_name);
1130 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1133 bool torture_netlogon_samba3(struct torture_context *torture)
1135 TALLOC_CTX *mem_ctx;
1138 struct smbcli_state *cli;
1139 struct cli_credentials *anon_creds;
1140 struct cli_credentials *wks_creds;
1141 const char *wks_name;
1143 struct smbcli_options options;
1145 wks_name = torture_setting_string(torture, "wksname", NULL);
1146 if (wks_name == NULL) {
1147 wks_name = get_myname();
1150 mem_ctx = talloc_init("torture_netlogon_samba3");
1152 if (mem_ctx == NULL) {
1153 d_printf("talloc_init failed\n");
1157 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1158 d_printf("create_anon_creds failed\n");
1162 lp_smbcli_options(torture->lp_ctx, &options);
1164 status = smbcli_full_connection(mem_ctx, &cli,
1165 torture_setting_string(torture, "host", NULL),
1166 lp_smb_ports(torture->lp_ctx),
1167 "IPC$", NULL, anon_creds,
1168 lp_resolve_context(torture->lp_ctx),
1170 if (!NT_STATUS_IS_OK(status)) {
1171 d_printf("smbcli_full_connection failed: %s\n",
1176 wks_creds = cli_credentials_init(mem_ctx);
1177 if (wks_creds == NULL) {
1178 d_printf("cli_credentials_init failed\n");
1182 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1183 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1184 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1185 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1186 cli_credentials_set_password(wks_creds,
1187 generate_random_str(wks_creds, 8),
1190 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1191 d_printf("join failed\n");
1195 cli_credentials_set_domain(
1196 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1199 for (i=0; i<2; i++) {
1201 /* Do this more than once, the routine "schan" changes
1202 * the workstation password using the netlogon
1203 * password change routine */
1207 if (!auth2(cli, wks_creds)) {
1208 d_printf("auth2 failed\n");
1212 for (j=0; j<2; j++) {
1213 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1214 d_printf("schan failed\n");
1220 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1221 d_printf("leave failed\n");
1228 talloc_free(mem_ctx);
1233 * Do a simple join, testjoin and leave using specified smb and samr
1237 static bool test_join3(struct torture_context *tctx,
1239 struct cli_credentials *smb_creds,
1240 struct cli_credentials *samr_creds,
1241 const char *wks_name)
1245 struct smbcli_state *cli;
1246 struct cli_credentials *wks_creds;
1247 struct smbcli_options options;
1249 lp_smbcli_options(tctx->lp_ctx, &options);
1251 status = smbcli_full_connection(tctx, &cli,
1252 torture_setting_string(tctx, "host", NULL),
1253 lp_smb_ports(tctx->lp_ctx),
1254 "IPC$", NULL, smb_creds,
1255 lp_resolve_context(tctx->lp_ctx),
1257 if (!NT_STATUS_IS_OK(status)) {
1258 d_printf("smbcli_full_connection failed: %s\n",
1263 wks_creds = cli_credentials_init(cli);
1264 if (wks_creds == NULL) {
1265 d_printf("cli_credentials_init failed\n");
1269 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1270 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1271 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1272 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1273 cli_credentials_set_password(wks_creds,
1274 generate_random_str(wks_creds, 8),
1277 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1278 d_printf("join failed\n");
1282 cli_credentials_set_domain(
1283 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1286 if (!auth2(cli, wks_creds)) {
1287 d_printf("auth2 failed\n");
1291 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1292 d_printf("leave failed\n");
1305 * Test the different session key variants. Do it by joining, this uses the
1306 * session key in the setpassword routine. Test the join by doing the auth2.
1309 bool torture_samba3_sessionkey(struct torture_context *torture)
1312 struct cli_credentials *anon_creds;
1313 const char *wks_name;
1315 wks_name = torture_setting_string(torture, "wksname", get_myname());
1317 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1318 d_printf("create_anon_creds failed\n");
1324 if (!torture_setting_bool(torture, "samba3", false)) {
1326 /* Samba3 in the build farm right now does this happily. Need
1329 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1330 d_printf("join using anonymous bind on an anonymous smb "
1331 "connection succeeded -- HUH??\n");
1336 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1338 d_printf("join using ntlmssp bind on an anonymous smb "
1339 "connection failed\n");
1343 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1344 d_printf("join using anonymous bind on an authenticated smb "
1345 "connection failed\n");
1349 if (!test_join3(torture, false, cmdline_credentials,
1350 cmdline_credentials,
1352 d_printf("join using ntlmssp bind on an authenticated smb "
1353 "connection failed\n");
1358 * The following two are tests for setuserinfolevel 25
1361 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1363 d_printf("join using ntlmssp bind on an anonymous smb "
1364 "connection failed\n");
1368 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1369 d_printf("join using anonymous bind on an authenticated smb "
1370 "connection failed\n");
1380 * open pipe and bind, given an IPC$ context
1383 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1384 struct smbcli_tree *tree,
1385 const char *pipe_name,
1386 const struct ndr_interface_table *iface,
1387 struct dcerpc_pipe **p)
1389 struct dcerpc_pipe *result;
1392 if (!(result = dcerpc_pipe_init(
1393 mem_ctx, tree->session->transport->socket->event.ctx,
1394 lp_iconv_convenience(global_loadparm)))) {
1395 return NT_STATUS_NO_MEMORY;
1398 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1399 if (!NT_STATUS_IS_OK(status)) {
1400 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1402 talloc_free(result);
1406 status = dcerpc_bind_auth_none(result, iface);
1407 if (!NT_STATUS_IS_OK(status)) {
1408 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1409 talloc_free(result);
1414 return NT_STATUS_OK;
1418 * Sane wrapper around lsa_LookupNames
1421 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1422 struct dcerpc_pipe *p,
1426 struct lsa_ObjectAttribute attr;
1427 struct lsa_QosInfo qos;
1428 struct lsa_OpenPolicy2 r;
1431 struct policy_handle handle;
1432 struct lsa_LookupNames l;
1433 struct lsa_TransSidArray sids;
1434 struct lsa_String lsa_name;
1436 struct dom_sid *result;
1437 TALLOC_CTX *tmp_ctx;
1439 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1444 qos.impersonation_level = 2;
1445 qos.context_mode = 1;
1446 qos.effective_only = 0;
1449 attr.root_dir = NULL;
1450 attr.object_name = NULL;
1451 attr.attributes = 0;
1452 attr.sec_desc = NULL;
1453 attr.sec_qos = &qos;
1455 r.in.system_name = "\\";
1457 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1458 r.out.handle = &handle;
1460 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1461 if (!NT_STATUS_IS_OK(status)) {
1462 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1463 talloc_free(tmp_ctx);
1470 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1472 l.in.handle = &handle;
1474 l.in.names = &lsa_name;
1477 l.in.count = &count;
1478 l.out.count = &count;
1481 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1482 if (!NT_STATUS_IS_OK(status)) {
1483 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1485 talloc_free(tmp_ctx);
1489 result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1490 l.out.sids->sids[0].rid);
1492 c.in.handle = &handle;
1493 c.out.handle = &handle;
1495 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1498 talloc_free(tmp_ctx);
1502 talloc_free(tmp_ctx);
1507 * Find out the user SID on this connection
1510 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1512 struct dcerpc_pipe *lsa;
1513 struct lsa_GetUserName r;
1515 struct lsa_StringPointer authority_name_p;
1516 struct dom_sid *result;
1518 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1519 &ndr_table_lsarpc, &lsa);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 d_printf("(%s) Could not bind to LSA: %s\n",
1522 __location__, nt_errstr(status));
1526 r.in.system_name = "\\";
1527 r.in.account_name = NULL;
1528 authority_name_p.string = NULL;
1529 r.in.authority_name = &authority_name_p;
1531 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1533 if (!NT_STATUS_IS_OK(status)) {
1534 printf("(%s) GetUserName failed - %s\n",
1535 __location__, nt_errstr(status));
1540 result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1541 r.out.authority_name->string->string);
1547 static int destroy_tree(struct smbcli_tree *tree)
1549 smb_tree_disconnect(tree);
1554 * Do a tcon, given a session
1557 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1558 struct smbcli_session *session,
1559 const char *sharename,
1560 struct smbcli_tree **res)
1562 struct smbcli_tree *result;
1563 TALLOC_CTX *tmp_ctx;
1564 union smb_tcon tcon;
1567 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1568 return NT_STATUS_NO_MEMORY;
1571 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1572 talloc_free(tmp_ctx);
1573 return NT_STATUS_NO_MEMORY;
1576 tcon.generic.level = RAW_TCON_TCONX;
1577 tcon.tconx.in.flags = 0;
1578 tcon.tconx.in.password = data_blob(NULL, 0);
1579 tcon.tconx.in.path = sharename;
1580 tcon.tconx.in.device = "?????";
1582 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1583 if (!NT_STATUS_IS_OK(status)) {
1584 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1586 talloc_free(tmp_ctx);
1590 result->tid = tcon.tconx.out.tid;
1591 result = talloc_steal(mem_ctx, result);
1592 talloc_set_destructor(result, destroy_tree);
1593 talloc_free(tmp_ctx);
1595 return NT_STATUS_OK;
1599 * Test the getusername behaviour
1602 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1605 struct smbcli_state *cli;
1606 TALLOC_CTX *mem_ctx;
1608 struct dom_sid *user_sid;
1609 struct dom_sid *created_sid;
1610 struct cli_credentials *anon_creds;
1611 struct cli_credentials *user_creds;
1613 struct smbcli_options options;
1615 if (!(mem_ctx = talloc_new(torture))) {
1619 lp_smbcli_options(torture->lp_ctx, &options);
1621 status = smbcli_full_connection(
1622 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1623 lp_smb_ports(torture->lp_ctx),
1624 "IPC$", NULL, cmdline_credentials,
1625 lp_resolve_context(torture->lp_ctx),
1627 if (!NT_STATUS_IS_OK(status)) {
1628 d_printf("(%s) smbcli_full_connection failed: %s\n",
1629 __location__, nt_errstr(status));
1634 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1635 d_printf("(%s) whoami on auth'ed connection failed\n",
1642 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1643 d_printf("(%s) create_anon_creds failed\n", __location__);
1648 status = smbcli_full_connection(
1649 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1650 lp_smb_ports(torture->lp_ctx),
1651 "IPC$", NULL, anon_creds,
1652 lp_resolve_context(torture->lp_ctx),
1654 if (!NT_STATUS_IS_OK(status)) {
1655 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1656 __location__, nt_errstr(status));
1661 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1662 d_printf("(%s) whoami on anon connection failed\n",
1668 if (!dom_sid_equal(user_sid,
1669 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1670 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1671 "S-1-5-7", __location__,
1672 dom_sid_string(mem_ctx, user_sid));
1676 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1677 d_printf("(%s) cli_credentials_init failed\n", __location__);
1682 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1683 cli_credentials_set_username(user_creds, "torture_username",
1685 cli_credentials_set_password(user_creds,
1686 generate_random_str(user_creds, 8),
1689 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1690 cli_credentials_get_username(user_creds),
1691 cli_credentials_get_password(user_creds),
1692 &domain_name, &created_sid)) {
1693 d_printf("(%s) create_user failed\n", __location__);
1698 cli_credentials_set_domain(user_creds, domain_name,
1702 struct smbcli_session *session2;
1703 struct smb_composite_sesssetup setup;
1704 struct smbcli_tree *tree;
1706 session2 = smbcli_session_init(cli->transport, mem_ctx, false);
1707 if (session2 == NULL) {
1708 d_printf("(%s) smbcli_session_init failed\n",
1713 setup.in.sesskey = cli->transport->negotiate.sesskey;
1714 setup.in.capabilities = cli->transport->negotiate.capabilities;
1715 setup.in.workgroup = "";
1716 setup.in.credentials = user_creds;
1718 status = smb_composite_sesssetup(session2, &setup);
1719 if (!NT_STATUS_IS_OK(status)) {
1720 d_printf("(%s) session setup with new user failed: "
1721 "%s\n", __location__, nt_errstr(status));
1725 session2->vuid = setup.out.vuid;
1727 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1729 d_printf("(%s) secondary_tcon failed\n",
1735 if (!(user_sid = whoami(mem_ctx, tree))) {
1736 d_printf("(%s) whoami on user connection failed\n",
1745 d_printf("Created %s, found %s\n",
1746 dom_sid_string(mem_ctx, created_sid),
1747 dom_sid_string(mem_ctx, user_sid));
1749 if (!dom_sid_equal(created_sid, user_sid)) {
1754 if (!delete_user(cli, torture->lp_ctx,
1755 cmdline_credentials,
1756 cli_credentials_get_username(user_creds))) {
1757 d_printf("(%s) delete_user failed\n", __location__);
1762 talloc_free(mem_ctx);
1766 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1767 const char *sharename)
1770 struct srvsvc_NetShareGetInfo r;
1771 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1775 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1776 dcerpc_server_name(p));
1777 r.in.share_name = sharename;
1779 for (i=0;i<ARRAY_SIZE(levels);i++) {
1780 r.in.level = levels[i];
1784 printf("testing NetShareGetInfo level %u on share '%s'\n",
1785 r.in.level, r.in.share_name);
1787 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1788 if (!NT_STATUS_IS_OK(status)) {
1789 printf("NetShareGetInfo level %u on share '%s' failed"
1790 " - %s\n", r.in.level, r.in.share_name,
1795 if (!W_ERROR_IS_OK(r.out.result)) {
1796 printf("NetShareGetInfo level %u on share '%s' failed "
1797 "- %s\n", r.in.level, r.in.share_name,
1798 win_errstr(r.out.result));
1807 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1808 const char **one_sharename)
1811 struct srvsvc_NetShareEnum r;
1812 struct srvsvc_NetShareCtr0 c0;
1813 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1817 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1818 r.in.ctr.ctr0 = &c0;
1819 r.in.ctr.ctr0->count = 0;
1820 r.in.ctr.ctr0->array = NULL;
1821 r.in.max_buffer = (uint32_t)-1;
1822 r.in.resume_handle = NULL;
1824 for (i=0;i<ARRAY_SIZE(levels);i++) {
1825 r.in.level = levels[i];
1829 printf("testing NetShareEnum level %u\n", r.in.level);
1830 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1831 if (!NT_STATUS_IS_OK(status)) {
1832 printf("NetShareEnum level %u failed - %s\n",
1833 r.in.level, nt_errstr(status));
1837 if (!W_ERROR_IS_OK(r.out.result)) {
1838 printf("NetShareEnum level %u failed - %s\n",
1839 r.in.level, win_errstr(r.out.result));
1842 if (r.in.level == 0) {
1843 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1844 if (ctr->count > 0) {
1845 *one_sharename = ctr->array[0].name;
1853 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
1855 struct dcerpc_pipe *p;
1856 TALLOC_CTX *mem_ctx;
1858 const char *sharename = NULL;
1859 struct smbcli_state *cli;
1862 if (!(mem_ctx = talloc_new(torture))) {
1866 if (!(torture_open_connection_share(
1867 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
1869 talloc_free(mem_ctx);
1873 status = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1874 &ndr_table_srvsvc, &p);
1875 if (!NT_STATUS_IS_OK(status)) {
1876 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1877 __location__, nt_errstr(status));
1882 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1883 if (sharename == NULL) {
1884 printf("did not get sharename\n");
1886 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1890 talloc_free(mem_ctx);
1895 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1896 * NT_STATUS_NO_SAM_ACCOUNT
1899 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1901 TALLOC_CTX *mem_ctx;
1902 struct dcerpc_pipe *net_pipe;
1904 bool result = false;
1906 struct netr_ServerReqChallenge r;
1907 struct netr_Credential netr_cli_creds;
1908 struct netr_Credential netr_srv_creds;
1909 uint32_t negotiate_flags;
1910 struct netr_ServerAuthenticate2 a;
1911 struct creds_CredentialState *creds_state;
1912 struct netr_Credential netr_cred;
1913 struct samr_Password mach_pw;
1914 struct smbcli_state *cli;
1916 if (!(mem_ctx = talloc_new(torture))) {
1917 d_printf("talloc_new failed\n");
1921 if (!(wksname = generate_random_str_list(
1922 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
1923 d_printf("generate_random_str_list failed\n");
1927 if (!(torture_open_connection_share(
1929 torture, torture_setting_string(torture, "host", NULL),
1931 d_printf("IPC$ connection failed\n");
1935 if (!(net_pipe = dcerpc_pipe_init(
1936 mem_ctx, cli->transport->socket->event.ctx,
1937 lp_iconv_convenience(torture->lp_ctx)))) {
1938 d_printf("dcerpc_pipe_init failed\n");
1942 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1943 if (!NT_STATUS_IS_OK(status)) {
1944 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1949 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1950 if (!NT_STATUS_IS_OK(status)) {
1951 d_printf("dcerpc_bind_auth_none failed: %s\n",
1956 r.in.computer_name = wksname;
1957 r.in.server_name = talloc_asprintf(
1958 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1959 if (r.in.server_name == NULL) {
1960 d_printf("talloc_asprintf failed\n");
1963 generate_random_buffer(netr_cli_creds.data,
1964 sizeof(netr_cli_creds.data));
1965 r.in.credentials = &netr_cli_creds;
1966 r.out.credentials = &netr_srv_creds;
1968 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
1969 if (!NT_STATUS_IS_OK(status)) {
1970 d_printf("netr_ServerReqChallenge failed: %s\n",
1975 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
1976 E_md4hash("foobar", mach_pw.hash);
1978 creds_state = talloc(mem_ctx, struct creds_CredentialState);
1979 creds_client_init(creds_state, r.in.credentials,
1980 r.out.credentials, &mach_pw,
1981 &netr_cred, negotiate_flags);
1983 a.in.server_name = talloc_asprintf(
1984 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1985 a.in.account_name = talloc_asprintf(
1986 mem_ctx, "%s$", wksname);
1987 a.in.computer_name = wksname;
1988 a.in.secure_channel_type = SEC_CHAN_WKSTA;
1989 a.in.negotiate_flags = &negotiate_flags;
1990 a.out.negotiate_flags = &negotiate_flags;
1991 a.in.credentials = &netr_cred;
1992 a.out.credentials = &netr_cred;
1994 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
1996 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
1997 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
1998 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2005 talloc_free(mem_ctx);
2009 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2010 struct smbcli_session *sess,
2011 const char *sharename)
2013 struct smbcli_tree *tree;
2014 TALLOC_CTX *tmp_ctx;
2015 struct dcerpc_pipe *p;
2017 struct srvsvc_NetShareGetInfo r;
2018 struct security_descriptor *result;
2020 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2021 d_printf("talloc_new failed\n");
2025 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2026 d_printf("secondary_tcon failed\n");
2027 talloc_free(tmp_ctx);
2031 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
2032 &ndr_table_srvsvc, &p);
2033 if (!NT_STATUS_IS_OK(status)) {
2034 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2035 __location__, nt_errstr(status));
2036 talloc_free(tmp_ctx);
2041 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2044 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2045 dcerpc_server_name(p));
2046 r.in.share_name = sharename;
2049 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2050 if (!NT_STATUS_IS_OK(status)) {
2051 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2053 talloc_free(tmp_ctx);
2057 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2058 talloc_free(tmp_ctx);
2062 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2063 struct smbcli_session *sess,
2064 const char *sharename,
2065 struct security_descriptor *sd)
2067 struct smbcli_tree *tree;
2068 TALLOC_CTX *tmp_ctx;
2069 struct dcerpc_pipe *p;
2071 struct sec_desc_buf i;
2072 struct srvsvc_NetShareSetInfo r;
2075 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2076 d_printf("talloc_new failed\n");
2077 return NT_STATUS_NO_MEMORY;
2080 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2081 d_printf("secondary_tcon failed\n");
2082 talloc_free(tmp_ctx);
2083 return NT_STATUS_UNSUCCESSFUL;
2086 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
2087 &ndr_table_srvsvc, &p);
2088 if (!NT_STATUS_IS_OK(status)) {
2089 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2090 __location__, nt_errstr(status));
2091 talloc_free(tmp_ctx);
2092 return NT_STATUS_UNSUCCESSFUL;
2096 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2099 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2100 dcerpc_server_name(p));
2101 r.in.share_name = sharename;
2104 r.in.info.info1501 = &i;
2105 r.in.parm_error = &error;
2107 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2108 if (!NT_STATUS_IS_OK(status)) {
2109 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2113 talloc_free(tmp_ctx);
2117 bool try_tcon(TALLOC_CTX *mem_ctx,
2118 struct security_descriptor *orig_sd,
2119 struct smbcli_session *session,
2120 const char *sharename, const struct dom_sid *user_sid,
2121 unsigned int access_mask, NTSTATUS expected_tcon,
2122 NTSTATUS expected_mkdir)
2124 TALLOC_CTX *tmp_ctx;
2125 struct smbcli_tree *rmdir_tree, *tree;
2126 struct dom_sid *domain_sid;
2128 struct security_descriptor *sd;
2132 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2133 d_printf("talloc_new failed\n");
2137 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2138 if (!NT_STATUS_IS_OK(status)) {
2139 d_printf("first tcon to delete dir failed\n");
2140 talloc_free(tmp_ctx);
2144 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2146 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2147 &domain_sid, &rid))) {
2148 d_printf("dom_sid_split_rid failed\n");
2149 talloc_free(tmp_ctx);
2153 sd = security_descriptor_dacl_create(
2154 tmp_ctx, 0, "S-1-5-32-544",
2155 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2157 dom_sid_string(mem_ctx, user_sid),
2158 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2160 d_printf("security_descriptor_dacl_create failed\n");
2161 talloc_free(tmp_ctx);
2165 status = set_sharesec(mem_ctx, session, sharename, sd);
2166 if (!NT_STATUS_IS_OK(status)) {
2167 d_printf("custom set_sharesec failed: %s\n",
2169 talloc_free(tmp_ctx);
2173 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2174 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2175 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2181 if (!NT_STATUS_IS_OK(status)) {
2182 /* An expected non-access, no point in trying to write */
2186 status = smbcli_mkdir(tree, "sharesec_testdir");
2187 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2188 d_printf("(%s) Expected %s, got %s\n", __location__,
2189 nt_errstr(expected_mkdir), nt_errstr(status));
2194 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2196 status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2197 if (!NT_STATUS_IS_OK(status)) {
2198 d_printf("custom set_sharesec failed: %s\n",
2200 talloc_free(tmp_ctx);
2204 talloc_free(tmp_ctx);
2208 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2210 TALLOC_CTX *mem_ctx;
2212 struct smbcli_state *cli;
2213 struct security_descriptor *sd;
2214 struct dom_sid *user_sid;
2216 if (!(mem_ctx = talloc_new(torture))) {
2220 if (!(torture_open_connection_share(
2221 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2223 d_printf("IPC$ connection failed\n");
2224 talloc_free(mem_ctx);
2228 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2229 d_printf("whoami failed\n");
2230 talloc_free(mem_ctx);
2234 sd = get_sharesec(mem_ctx, cli->session, torture_setting_string(torture,
2237 ret &= try_tcon(mem_ctx, sd, cli->session,
2238 torture_setting_string(torture, "share", NULL),
2239 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2241 ret &= try_tcon(mem_ctx, sd, cli->session,
2242 torture_setting_string(torture, "share", NULL),
2243 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2244 NT_STATUS_MEDIA_WRITE_PROTECTED);
2246 ret &= try_tcon(mem_ctx, sd, cli->session,
2247 torture_setting_string(torture, "share", NULL),
2248 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2250 talloc_free(mem_ctx);
2254 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2256 TALLOC_CTX *mem_ctx;
2258 struct smbcli_state *cli;
2259 struct dcerpc_pipe *p;
2260 struct policy_handle lsa_handle;
2262 struct dom_sid *domain_sid;
2264 if (!(mem_ctx = talloc_new(torture))) {
2268 if (!(torture_open_connection_share(
2269 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2271 d_printf("IPC$ connection failed\n");
2272 talloc_free(mem_ctx);
2276 status = pipe_bind_smb(mem_ctx, cli->tree, "\\lsarpc",
2277 &ndr_table_lsarpc, &p);
2278 if (!NT_STATUS_IS_OK(status)) {
2279 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2281 talloc_free(mem_ctx);
2286 struct lsa_ObjectAttribute attr;
2287 struct lsa_OpenPolicy2 o;
2288 o.in.system_name = talloc_asprintf(
2289 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2292 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2293 o.out.handle = &lsa_handle;
2294 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2295 if (!NT_STATUS_IS_OK(status)) {
2296 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2297 __location__, nt_errstr(status));
2298 talloc_free(mem_ctx);
2304 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2309 int levels[] = { 2,3,5,6 };
2311 for (i=0; i<ARRAY_SIZE(levels); i++) {
2312 struct lsa_QueryInfoPolicy r;
2313 r.in.handle = &lsa_handle;
2314 r.in.level = levels[i];
2315 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2316 if (!NT_STATUS_IS_OK(status)) {
2317 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2318 "failed: %s\n", __location__,
2319 levels[i], nt_errstr(status));
2320 talloc_free(mem_ctx);
2323 if (levels[i] == 5) {
2324 domain_sid = r.out.info->account_domain.sid;
2332 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2335 struct rap_WserverGetInfo r;
2337 char servername[17];
2340 r.in.bufsize = 0xffff;
2342 status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
2343 if (!NT_STATUS_IS_OK(status)) {
2347 memcpy(servername, r.out.info.info0.name, 16);
2348 servername[16] = '\0';
2350 if (pull_ascii_talloc(mem_ctx, lp_iconv_convenience(global_loadparm),
2351 name, servername) < 0) {
2352 return NT_STATUS_NO_MEMORY;
2355 return NT_STATUS_OK;
2359 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct smbcli_tree *tree,
2360 const char ***printers, int *num_printers)
2362 TALLOC_CTX *mem_ctx;
2364 struct dcerpc_pipe *p;
2365 struct srvsvc_NetShareEnum r;
2366 struct srvsvc_NetShareCtr1 c1_in;
2367 struct srvsvc_NetShareCtr1 *c1;
2370 mem_ctx = talloc_new(ctx);
2371 if (mem_ctx == NULL) {
2372 return NT_STATUS_NO_MEMORY;
2375 status = pipe_bind_smb(mem_ctx, tree, "\\srvsvc", &ndr_table_srvsvc,
2377 if (!NT_STATUS_IS_OK(status)) {
2378 d_printf("could not bind to srvsvc pipe\n");
2379 talloc_free(mem_ctx);
2383 r.in.server_unc = talloc_asprintf(
2384 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2387 r.in.ctr.ctr1 = &c1_in;
2388 r.in.max_buffer = (uint32_t)-1;
2389 r.in.resume_handle = NULL;
2391 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2392 if (!NT_STATUS_IS_OK(status)) {
2393 d_printf("NetShareEnum level %u failed - %s\n",
2394 r.in.level, nt_errstr(status));
2395 talloc_free(mem_ctx);
2401 c1 = r.out.ctr.ctr1;
2402 for (i=0; i<c1->count; i++) {
2403 if (c1->array[i].type != STYPE_PRINTQ) {
2406 if (!add_string_to_array(ctx, c1->array[i].name,
2407 printers, num_printers)) {
2409 return NT_STATUS_NO_MEMORY;
2413 talloc_free(mem_ctx);
2414 return NT_STATUS_OK;
2417 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2418 const char *servername, int level, int *num_printers)
2420 struct spoolss_EnumPrinters r;
2424 r.in.flags = PRINTER_ENUM_LOCAL;
2425 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2430 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2431 if (!NT_STATUS_IS_OK(status)) {
2432 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2433 __location__, nt_errstr(status));
2437 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2438 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2439 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2440 win_errstr(r.out.result));
2444 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2445 if (blob.data == NULL) {
2446 d_printf("(%s) data_blob_talloc failed\n", __location__);
2450 r.in.buffer = &blob;
2451 r.in.offered = r.out.needed;
2453 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2454 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2455 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2456 "%s\n", __location__, nt_errstr(status),
2457 win_errstr(r.out.result));
2461 *num_printers = r.out.count;
2466 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2467 struct policy_handle *handle, int level,
2468 union spoolss_PrinterInfo **res)
2470 TALLOC_CTX *mem_ctx;
2471 struct spoolss_GetPrinter r;
2475 mem_ctx = talloc_new(ctx);
2476 if (mem_ctx == NULL) {
2477 return NT_STATUS_NO_MEMORY;
2480 r.in.handle = handle;
2485 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2486 if (!NT_STATUS_IS_OK(status)) {
2487 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2488 __location__, nt_errstr(status));
2489 talloc_free(mem_ctx);
2493 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2494 printf("GetPrinter unexpected return code %s, should "
2495 "be WERR_INSUFFICIENT_BUFFER\n",
2496 win_errstr(r.out.result));
2497 talloc_free(mem_ctx);
2498 return NT_STATUS_UNSUCCESSFUL;
2501 r.in.handle = handle;
2503 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2504 if (blob.data == NULL) {
2505 talloc_free(mem_ctx);
2506 return NT_STATUS_NO_MEMORY;
2508 memset(blob.data, 0, blob.length);
2509 r.in.buffer = &blob;
2510 r.in.offered = r.out.needed;
2512 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2513 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2514 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2515 "%s\n", __location__, nt_errstr(status),
2516 win_errstr(r.out.result));
2517 talloc_free(mem_ctx);
2518 return NT_STATUS_IS_OK(status) ?
2519 NT_STATUS_UNSUCCESSFUL : status;
2523 *res = talloc_steal(ctx, r.out.info);
2526 talloc_free(mem_ctx);
2527 return NT_STATUS_OK;
2530 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2532 TALLOC_CTX *mem_ctx;
2534 struct smbcli_state *cli;
2535 struct dcerpc_pipe *p;
2537 struct policy_handle server_handle, printer_handle;
2538 const char **printers;
2540 struct spoolss_UserLevel1 userlevel1;
2543 if (!(mem_ctx = talloc_new(torture))) {
2547 if (!(torture_open_connection_share(
2548 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2550 d_printf("IPC$ connection failed\n");
2551 talloc_free(mem_ctx);
2555 status = get_servername(mem_ctx, cli->tree, &servername);
2556 if (!NT_STATUS_IS_OK(status)) {
2557 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2558 __location__, nt_errstr(status));
2559 talloc_free(mem_ctx);
2563 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, cli->tree,
2564 &printers, &num_printers))) {
2565 talloc_free(mem_ctx);
2569 if (num_printers == 0) {
2570 d_printf("Did not find printers\n");
2571 talloc_free(mem_ctx);
2575 status = pipe_bind_smb(mem_ctx, cli->tree, "\\spoolss",
2576 &ndr_table_spoolss, &p);
2577 if (!NT_STATUS_IS_OK(status)) {
2578 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2580 talloc_free(mem_ctx);
2584 ZERO_STRUCT(userlevel1);
2585 userlevel1.client = talloc_asprintf(
2586 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2587 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2588 userlevel1.build = 2600;
2589 userlevel1.major = 3;
2590 userlevel1.minor = 0;
2591 userlevel1.processor = 0;
2594 struct spoolss_OpenPrinterEx r;
2597 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2599 r.in.datatype = NULL;
2600 r.in.access_mask = 0;
2602 r.in.userlevel.level1 = &userlevel1;
2603 r.out.handle = &server_handle;
2605 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2606 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2607 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2608 "%s, %s\n", __location__, nt_errstr(status),
2609 win_errstr(r.out.result));
2610 talloc_free(mem_ctx);
2616 struct spoolss_ClosePrinter r;
2618 r.in.handle = &server_handle;
2619 r.out.handle = &server_handle;
2621 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2622 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2623 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2624 "%s, %s\n", __location__, nt_errstr(status),
2625 win_errstr(r.out.result));
2626 talloc_free(mem_ctx);
2632 struct spoolss_OpenPrinterEx r;
2635 r.in.printername = talloc_asprintf(
2636 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2637 r.in.datatype = NULL;
2638 r.in.access_mask = 0;
2640 r.in.userlevel.level1 = &userlevel1;
2641 r.out.handle = &printer_handle;
2643 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2644 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2645 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2646 "%s, %s\n", __location__, nt_errstr(status),
2647 win_errstr(r.out.result));
2648 talloc_free(mem_ctx);
2656 for (i=0; i<8; i++) {
2657 status = getprinterinfo(mem_ctx, p, &printer_handle,
2659 if (!NT_STATUS_IS_OK(status)) {
2660 d_printf("(%s) getprinterinfo %d failed: %s\n",
2661 __location__, i, nt_errstr(status));
2668 struct spoolss_ClosePrinter r;
2670 r.in.handle = &printer_handle;
2671 r.out.handle = &printer_handle;
2673 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2674 if (!NT_STATUS_IS_OK(status)) {
2675 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2676 "%s\n", __location__, nt_errstr(status));
2677 talloc_free(mem_ctx);
2684 if (!enumprinters(mem_ctx, p, servername, 1,
2686 d_printf("(%s) enumprinters failed\n", __location__);
2687 talloc_free(mem_ctx);
2690 if (num_printers != num_enumerated) {
2691 d_printf("(%s) netshareenum gave %d printers, "
2692 "enumprinters lvl 1 gave %d\n", __location__,
2693 num_printers, num_enumerated);
2694 talloc_free(mem_ctx);
2701 if (!enumprinters(mem_ctx, p, servername, 2,
2703 d_printf("(%s) enumprinters failed\n", __location__);
2704 talloc_free(mem_ctx);
2707 if (num_printers != num_enumerated) {
2708 d_printf("(%s) netshareenum gave %d printers, "
2709 "enumprinters lvl 2 gave %d\n", __location__,
2710 num_printers, num_enumerated);
2711 talloc_free(mem_ctx);
2716 talloc_free(mem_ctx);
2721 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2723 TALLOC_CTX *mem_ctx;
2724 struct smbcli_state *cli;
2725 struct dcerpc_pipe *p;
2729 if (!(mem_ctx = talloc_new(torture))) {
2733 if (!(torture_open_connection_share(
2734 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2736 d_printf("IPC$ connection failed\n");
2737 talloc_free(mem_ctx);
2741 status = get_servername(mem_ctx, cli->tree, &servername);
2742 if (!NT_STATUS_IS_OK(status)) {
2743 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2744 __location__, nt_errstr(status));
2745 talloc_free(mem_ctx);
2749 status = pipe_bind_smb(mem_ctx, cli->tree, "\\wkssvc",
2750 &ndr_table_wkssvc, &p);
2751 if (!NT_STATUS_IS_OK(status)) {
2752 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2754 talloc_free(mem_ctx);
2759 struct wkssvc_NetWkstaInfo100 wks100;
2760 union wkssvc_NetWkstaInfo info;
2761 struct wkssvc_NetWkstaGetInfo r;
2763 r.in.server_name = "\\foo";
2765 info.info100 = &wks100;
2768 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2769 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2770 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2771 "%s, %s\n", __location__, nt_errstr(status),
2772 win_errstr(r.out.result));
2773 talloc_free(mem_ctx);
2777 if (strcmp(servername,
2778 r.out.info->info100->server_name) != 0) {
2779 d_printf("(%s) servername inconsistency: RAP=%s, "
2780 "dcerpc_wkssvc_NetWksGetInfo=%s",
2781 __location__, servername,
2782 r.out.info->info100->server_name);
2783 talloc_free(mem_ctx);
2788 talloc_free(mem_ctx);
2792 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2793 struct policy_handle *handle)
2795 struct winreg_CloseKey c;
2797 TALLOC_CTX *mem_ctx;
2799 c.in.handle = c.out.handle = handle;
2801 if (!(mem_ctx = talloc_new(p))) {
2802 return NT_STATUS_NO_MEMORY;
2805 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2806 talloc_free(mem_ctx);
2808 if (!NT_STATUS_IS_OK(status)) {
2812 if (!W_ERROR_IS_OK(c.out.result)) {
2813 return werror_to_ntstatus(c.out.result);
2816 return NT_STATUS_OK;
2819 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2820 TALLOC_CTX *mem_ctx)
2822 uint32_t enum_index = 0;
2825 struct winreg_EnumValue r;
2826 struct winreg_StringBuf name;
2827 enum winreg_Type type = 0;
2830 uint32_t size, length;
2832 r.in.handle = handle;
2833 r.in.enum_index = enum_index;
2836 r.in.name = r.out.name = &name;
2842 r.in.length = &length;
2844 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2845 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2846 return NT_STATUS_OK;
2852 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
2853 TALLOC_CTX *mem_ctx, int depth)
2855 struct winreg_EnumKey r;
2856 struct winreg_StringBuf class, name;
2861 return NT_STATUS_OK;
2867 r.in.handle = handle;
2868 r.in.enum_index = 0;
2870 r.in.keyclass = &class;
2872 r.in.last_changed_time = &t;
2875 TALLOC_CTX *tmp_ctx;
2876 struct winreg_OpenKey o;
2877 struct policy_handle key_handle;
2880 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2881 return NT_STATUS_NO_MEMORY;
2887 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2888 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2889 /* We're done enumerating */
2890 talloc_free(tmp_ctx);
2891 return NT_STATUS_OK;
2894 for (i=0; i<10-depth; i++)
2896 printf("%s\n", r.out.name->name);
2899 o.in.parent_handle = handle;
2900 o.in.keyname.name = r.out.name->name;
2902 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2903 o.out.handle = &key_handle;
2905 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2906 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2907 enumkeys(p, &key_handle, tmp_ctx, depth-1);
2908 enumvalues(p, &key_handle, tmp_ctx);
2909 status = winreg_close(p, &key_handle);
2910 if (!NT_STATUS_IS_OK(status)) {
2915 talloc_free(tmp_ctx);
2917 r.in.enum_index += 1;
2920 return NT_STATUS_OK;
2923 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
2925 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
2926 const char *name, winreg_open_fn open_fn)
2928 struct policy_handle handle;
2929 struct winreg_OpenHKLM r;
2932 r.in.system_name = 0;
2933 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2934 r.out.handle = &handle;
2936 status = open_fn(p, mem_ctx, &r);
2937 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2938 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
2939 nt_errstr(status), win_errstr(r.out.result));
2943 enumkeys(p, &handle, mem_ctx, 4);
2945 status = winreg_close(p, &handle);
2946 if (!NT_STATUS_IS_OK(status)) {
2947 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
2948 __location__, nt_errstr(status));
2955 bool torture_samba3_rpc_winreg(struct torture_context *torture)
2958 struct dcerpc_pipe *p;
2959 TALLOC_CTX *mem_ctx;
2965 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
2966 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
2967 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
2968 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
2969 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
2974 mem_ctx = talloc_init("torture_rpc_winreg");
2976 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
2978 if (!NT_STATUS_IS_OK(status)) {
2979 talloc_free(mem_ctx);
2984 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
2986 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
2987 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
2992 talloc_free(mem_ctx);
2997 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
2998 struct smbcli_state *cli,
3000 struct srvsvc_NetShareInfo502 **info)
3002 struct smbcli_tree *ipc;
3003 struct dcerpc_pipe *p;
3004 struct srvsvc_NetShareGetInfo r;
3007 if (!(p = dcerpc_pipe_init(cli,
3008 cli->transport->socket->event.ctx,
3009 lp_iconv_convenience(global_loadparm)))) {
3010 status = NT_STATUS_NO_MEMORY;
3014 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3015 if (!NT_STATUS_IS_OK(status)) {
3019 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3020 if (!NT_STATUS_IS_OK(status)) {
3021 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3026 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3027 if (!NT_STATUS_IS_OK(status)) {
3028 d_printf("dcerpc_bind_auth_none failed: %s\n",
3033 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3034 dcerpc_server_name(p));
3035 r.in.share_name = share;
3038 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3039 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3040 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3041 nt_errstr(status), win_errstr(r.out.result));
3045 *info = talloc_move(mem_ctx, &r.out.info.info502);
3046 return NT_STATUS_OK;
3054 * Get us a handle on HKLM\
3057 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3058 struct smbcli_state *cli,
3059 struct dcerpc_pipe **pipe_p,
3060 struct policy_handle **handle)
3062 struct smbcli_tree *ipc;
3063 struct dcerpc_pipe *p;
3064 struct winreg_OpenHKLM r;
3066 struct policy_handle *result;
3068 result = talloc(mem_ctx, struct policy_handle);
3070 if (result == NULL) {
3071 return NT_STATUS_NO_MEMORY;
3074 if (!(p = dcerpc_pipe_init(result,
3075 cli->transport->socket->event.ctx,
3076 lp_iconv_convenience(global_loadparm)))) {
3077 status = NT_STATUS_NO_MEMORY;
3081 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3082 if (!NT_STATUS_IS_OK(status)) {
3086 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3087 if (!NT_STATUS_IS_OK(status)) {
3088 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3093 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3094 if (!NT_STATUS_IS_OK(status)) {
3095 d_printf("dcerpc_bind_auth_none failed: %s\n",
3100 r.in.system_name = 0;
3101 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3102 r.out.handle = result;
3104 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3105 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3106 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3107 nt_errstr(status), win_errstr(r.out.result));
3113 return NT_STATUS_OK;
3116 talloc_free(result);
3120 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3121 const char *sharename)
3123 struct dcerpc_pipe *p;
3124 struct policy_handle *hklm = NULL;
3125 struct policy_handle new_handle;
3126 struct winreg_CreateKey c;
3127 struct winreg_CloseKey cl;
3128 enum winreg_CreateAction action_taken;
3130 TALLOC_CTX *mem_ctx;
3132 mem_ctx = talloc_new(cli);
3133 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3135 status = get_hklm_handle(mem_ctx, cli, &p, &hklm);
3136 if (!NT_STATUS_IS_OK(status)) {
3137 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3142 c.in.name.name = talloc_asprintf(
3143 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3144 if (c.in.name.name == NULL) {
3145 d_printf("talloc_asprintf failed\n");
3146 status = NT_STATUS_NO_MEMORY;
3149 c.in.keyclass.name = "";
3151 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3152 c.in.secdesc = NULL;
3153 c.in.action_taken = &action_taken;
3154 c.out.new_handle = &new_handle;
3155 c.out.action_taken = &action_taken;
3157 status = dcerpc_winreg_CreateKey(p, p, &c);
3158 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3159 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3160 nt_errstr(status), win_errstr(c.out.result));
3164 cl.in.handle = &new_handle;
3165 cl.out.handle = &new_handle;
3166 status = dcerpc_winreg_CloseKey(p, p, &cl);
3167 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3168 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3169 nt_errstr(status), win_errstr(cl.out.result));
3175 talloc_free(mem_ctx);
3179 static NTSTATUS torture_samba3_deleteshare(struct smbcli_state *cli,
3180 const char *sharename)
3182 struct dcerpc_pipe *p;
3183 struct policy_handle *hklm = NULL;
3184 struct winreg_DeleteKey d;
3186 TALLOC_CTX *mem_ctx;
3188 mem_ctx = talloc_new(cli);
3189 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3191 status = get_hklm_handle(cli, cli, &p, &hklm);
3192 if (!NT_STATUS_IS_OK(status)) {
3193 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3198 d.in.key.name = talloc_asprintf(
3199 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3200 if (d.in.key.name == NULL) {
3201 d_printf("talloc_asprintf failed\n");
3202 status = NT_STATUS_NO_MEMORY;
3205 status = dcerpc_winreg_DeleteKey(p, p, &d);
3206 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3207 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3208 nt_errstr(status), win_errstr(d.out.result));
3213 talloc_free(mem_ctx);
3217 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3218 const char *sharename,
3219 const char *parameter,
3222 struct dcerpc_pipe *p = NULL;
3223 struct policy_handle *hklm = NULL, key_handle;
3224 struct winreg_OpenKey o;
3225 struct winreg_SetValue s;
3230 status = get_hklm_handle(cli, cli, &p, &hklm);
3231 if (!NT_STATUS_IS_OK(status)) {
3232 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3236 o.in.parent_handle = hklm;
3237 o.in.keyname.name = talloc_asprintf(
3238 hklm, "software\\samba\\smbconf\\%s", sharename);
3239 if (o.in.keyname.name == NULL) {
3240 d_printf("talloc_asprintf failed\n");
3241 status = NT_STATUS_NO_MEMORY;
3245 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3246 o.out.handle = &key_handle;
3248 status = dcerpc_winreg_OpenKey(p, p, &o);
3249 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3250 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3251 nt_errstr(status), win_errstr(o.out.result));
3255 if (!reg_string_to_val(hklm, "REG_SZ", value, &type, &val)) {
3256 d_printf("(%s) reg_string_to_val failed\n", __location__);
3260 s.in.handle = &key_handle;
3261 s.in.name.name = parameter;
3263 s.in.data = val.data;
3264 s.in.size = val.length;
3266 status = dcerpc_winreg_SetValue(p, p, &s);
3267 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3268 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3269 nt_errstr(status), win_errstr(s.out.result));
3278 bool torture_samba3_regconfig(struct torture_context *torture)
3280 struct smbcli_state *cli;
3281 struct srvsvc_NetShareInfo502 *i = NULL;
3284 const char *comment = "Dummer Kommentar";
3286 if (!(torture_open_connection(&cli, torture, 0))) {
3290 status = torture_samba3_createshare(cli, "blubber");
3291 if (!NT_STATUS_IS_OK(status)) {
3292 torture_warning(torture, "torture_samba3_createshare failed: "
3293 "%s\n", nt_errstr(status));
3297 status = torture_samba3_setconfig(cli, "blubber", "comment", comment);
3298 if (!NT_STATUS_IS_OK(status)) {
3299 torture_warning(torture, "torture_samba3_setconfig failed: "
3300 "%s\n", nt_errstr(status));
3304 status = get_shareinfo(torture, cli, "blubber", &i);
3305 if (!NT_STATUS_IS_OK(status)) {
3306 torture_warning(torture, "get_shareinfo failed: "
3307 "%s\n", nt_errstr(status));
3311 if (strcmp(comment, i->comment) != 0) {
3312 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3313 comment, i->comment);
3317 status = torture_samba3_deleteshare(cli, "blubber");
3318 if (!NT_STATUS_IS_OK(status)) {
3319 torture_warning(torture, "torture_samba3_deleteshare failed: "
3320 "%s\n", nt_errstr(status));