2 Unix SMB/CIFS implementation.
4 dcerpc torture tests, designed to walk Samba3 code paths
6 Copyright (C) Volker Lendecke 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/rap/rap.h"
26 #include "torture/torture.h"
27 #include "torture/util.h"
28 #include "torture/rap/proto.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "librpc/gen_ndr/ndr_lsa_c.h"
31 #include "librpc/gen_ndr/ndr_samr.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "librpc/gen_ndr/ndr_netlogon.h"
34 #include "librpc/gen_ndr/ndr_netlogon_c.h"
35 #include "librpc/gen_ndr/ndr_srvsvc.h"
36 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
37 #include "librpc/gen_ndr/ndr_spoolss.h"
38 #include "librpc/gen_ndr/ndr_spoolss_c.h"
39 #include "librpc/gen_ndr/ndr_winreg.h"
40 #include "librpc/gen_ndr/ndr_winreg_c.h"
41 #include "librpc/gen_ndr/ndr_wkssvc.h"
42 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
43 #include "lib/cmdline/popt_common.h"
44 #include "librpc/rpc/dcerpc.h"
45 #include "torture/rpc/rpc.h"
46 #include "libcli/libcli.h"
47 #include "libcli/composite/composite.h"
48 #include "libcli/smb_composite/smb_composite.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "../lib/crypto/crypto.h"
51 #include "auth/ntlmssp/ntlmssp.h"
52 #include "libcli/security/proto.h"
53 #include "param/param.h"
54 #include "lib/registry/registry.h"
55 #include "libcli/resolve/resolve.h"
58 * This tests a RPC call using an invalid vuid
61 bool torture_bind_authcontext(struct torture_context *torture)
66 struct lsa_ObjectAttribute objectattr;
67 struct lsa_OpenPolicy2 openpolicy;
68 struct policy_handle handle;
69 struct lsa_Close close_handle;
70 struct smbcli_session *tmp;
71 struct smbcli_session *session2;
72 struct smbcli_state *cli;
73 struct dcerpc_pipe *lsa_pipe;
74 struct cli_credentials *anon_creds;
75 struct smb_composite_sesssetup setup;
76 struct smbcli_options options;
77 struct smbcli_session_options session_options;
79 mem_ctx = talloc_init("torture_bind_authcontext");
81 if (mem_ctx == NULL) {
82 d_printf("talloc_init failed\n");
86 lp_smbcli_options(torture->lp_ctx, &options);
87 lp_smbcli_session_options(torture->lp_ctx, &session_options);
89 status = smbcli_full_connection(mem_ctx, &cli,
90 torture_setting_string(torture, "host", NULL),
91 lp_smb_ports(torture->lp_ctx),
92 "IPC$", NULL, cmdline_credentials,
93 lp_resolve_context(torture->lp_ctx),
94 torture->ev, &options, &session_options);
95 if (!NT_STATUS_IS_OK(status)) {
96 d_printf("smbcli_full_connection failed: %s\n",
101 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
102 lp_iconv_convenience(torture->lp_ctx));
103 if (lsa_pipe == NULL) {
104 d_printf("dcerpc_pipe_init failed\n");
108 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
109 if (!NT_STATUS_IS_OK(status)) {
110 d_printf("dcerpc_pipe_open_smb failed: %s\n",
115 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
116 if (!NT_STATUS_IS_OK(status)) {
117 d_printf("dcerpc_bind_auth_none failed: %s\n",
122 openpolicy.in.system_name =talloc_asprintf(
123 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
124 ZERO_STRUCT(objectattr);
125 openpolicy.in.attr = &objectattr;
126 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
127 openpolicy.out.handle = &handle;
129 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
131 if (!NT_STATUS_IS_OK(status)) {
132 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
137 close_handle.in.handle = &handle;
138 close_handle.out.handle = &handle;
140 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
141 if (!NT_STATUS_IS_OK(status)) {
142 d_printf("dcerpc_lsa_Close failed: %s\n",
147 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
148 if (session2 == NULL) {
149 d_printf("smbcli_session_init failed\n");
153 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
154 d_printf("create_anon_creds failed\n");
158 setup.in.sesskey = cli->transport->negotiate.sesskey;
159 setup.in.capabilities = cli->transport->negotiate.capabilities;
160 setup.in.workgroup = "";
161 setup.in.credentials = anon_creds;
163 status = smb_composite_sesssetup(session2, &setup);
164 if (!NT_STATUS_IS_OK(status)) {
165 d_printf("anon session setup failed: %s\n",
169 session2->vuid = setup.out.vuid;
171 tmp = cli->tree->session;
172 cli->tree->session = session2;
174 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
176 cli->tree->session = tmp;
177 talloc_free(lsa_pipe);
180 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
181 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
182 "expected NT_STATUS_INVALID_HANDLE\n",
189 talloc_free(mem_ctx);
194 * Bind to lsa using a specific auth method
197 static bool bindtest(struct smbcli_state *cli,
198 struct cli_credentials *credentials,
199 struct loadparm_context *lp_ctx,
200 uint8_t auth_type, uint8_t auth_level)
206 struct dcerpc_pipe *lsa_pipe;
207 struct lsa_ObjectAttribute objectattr;
208 struct lsa_OpenPolicy2 openpolicy;
209 struct lsa_QueryInfoPolicy query;
210 struct policy_handle handle;
211 struct lsa_Close close_handle;
213 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
214 d_printf("talloc_init failed\n");
218 lsa_pipe = dcerpc_pipe_init(mem_ctx,
219 cli->transport->socket->event.ctx,
220 lp_iconv_convenience(lp_ctx));
221 if (lsa_pipe == NULL) {
222 d_printf("dcerpc_pipe_init failed\n");
226 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
227 if (!NT_STATUS_IS_OK(status)) {
228 d_printf("dcerpc_pipe_open_smb failed: %s\n",
233 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
234 credentials, lp_ctx, auth_type, auth_level,
236 if (!NT_STATUS_IS_OK(status)) {
237 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
241 openpolicy.in.system_name =talloc_asprintf(
242 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
243 ZERO_STRUCT(objectattr);
244 openpolicy.in.attr = &objectattr;
245 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
246 openpolicy.out.handle = &handle;
248 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
250 if (!NT_STATUS_IS_OK(status)) {
251 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
256 query.in.handle = &handle;
257 query.in.level = LSA_POLICY_INFO_DOMAIN;
259 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
260 if (!NT_STATUS_IS_OK(status)) {
261 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
266 close_handle.in.handle = &handle;
267 close_handle.out.handle = &handle;
269 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
270 if (!NT_STATUS_IS_OK(status)) {
271 d_printf("dcerpc_lsa_Close failed: %s\n",
278 talloc_free(mem_ctx);
283 * test authenticated RPC binds with the variants Samba3 does support
286 bool torture_bind_samba3(struct torture_context *torture)
291 struct smbcli_state *cli;
292 struct smbcli_options options;
293 struct smbcli_session_options session_options;
295 mem_ctx = talloc_init("torture_bind_authcontext");
297 if (mem_ctx == NULL) {
298 d_printf("talloc_init failed\n");
302 lp_smbcli_options(torture->lp_ctx, &options);
303 lp_smbcli_session_options(torture->lp_ctx, &session_options);
305 status = smbcli_full_connection(mem_ctx, &cli,
306 torture_setting_string(torture, "host", NULL),
307 lp_smb_ports(torture->lp_ctx),
308 "IPC$", NULL, cmdline_credentials,
309 lp_resolve_context(torture->lp_ctx),
310 torture->ev, &options, &session_options);
311 if (!NT_STATUS_IS_OK(status)) {
312 d_printf("smbcli_full_connection failed: %s\n",
319 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
320 DCERPC_AUTH_LEVEL_INTEGRITY);
321 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
322 DCERPC_AUTH_LEVEL_PRIVACY);
323 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
324 DCERPC_AUTH_LEVEL_INTEGRITY);
325 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
326 DCERPC_AUTH_LEVEL_PRIVACY);
329 talloc_free(mem_ctx);
334 * Lookup or create a user and return all necessary info
337 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
339 struct loadparm_context *lp_ctx,
340 struct cli_credentials *admin_creds,
343 const char *username,
345 struct dcerpc_pipe **result_pipe,
346 struct policy_handle **result_handle,
347 struct dom_sid **sid)
349 struct dcerpc_pipe *samr_pipe;
351 struct policy_handle conn_handle;
352 struct policy_handle domain_handle;
353 struct policy_handle *user_handle;
354 struct samr_Connect2 conn;
355 struct samr_EnumDomains enumdom;
356 uint32_t resume_handle = 0;
357 struct samr_LookupDomain l;
359 struct lsa_String domain_name;
360 struct lsa_String user_name;
361 struct samr_OpenDomain o;
362 struct samr_CreateUser2 c;
363 uint32_t user_rid,access_granted;
365 samr_pipe = dcerpc_pipe_init(mem_ctx,
366 cli->transport->socket->event.ctx,
367 lp_iconv_convenience(lp_ctx));
368 if (samr_pipe == NULL) {
369 d_printf("dcerpc_pipe_init failed\n");
370 status = NT_STATUS_NO_MEMORY;
374 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
375 if (!NT_STATUS_IS_OK(status)) {
376 d_printf("dcerpc_pipe_open_smb failed: %s\n",
381 if (admin_creds != NULL) {
382 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
383 admin_creds, lp_ctx, auth_type, auth_level,
385 if (!NT_STATUS_IS_OK(status)) {
386 d_printf("dcerpc_bind_auth failed: %s\n",
391 /* We must have an authenticated SMB connection */
392 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
393 if (!NT_STATUS_IS_OK(status)) {
394 d_printf("dcerpc_bind_auth_none failed: %s\n",
400 conn.in.system_name = talloc_asprintf(
401 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
402 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
403 conn.out.connect_handle = &conn_handle;
405 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
406 if (!NT_STATUS_IS_OK(status)) {
407 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
411 enumdom.in.connect_handle = &conn_handle;
412 enumdom.in.resume_handle = &resume_handle;
413 enumdom.in.buf_size = (uint32_t)-1;
414 enumdom.out.resume_handle = &resume_handle;
416 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
417 if (!NT_STATUS_IS_OK(status)) {
418 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
422 if (enumdom.out.num_entries != 2) {
423 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
424 enumdom.out.num_entries);
425 status = NT_STATUS_UNSUCCESSFUL;
429 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
432 l.in.connect_handle = &conn_handle;
433 domain_name.string = enumdom.out.sam->entries[dom_idx].name.string;
434 *domain = talloc_strdup(mem_ctx, domain_name.string);
435 l.in.domain_name = &domain_name;
437 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
438 if (!NT_STATUS_IS_OK(status)) {
439 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
443 o.in.connect_handle = &conn_handle;
444 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
445 o.in.sid = l.out.sid;
446 o.out.domain_handle = &domain_handle;
448 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
449 if (!NT_STATUS_IS_OK(status)) {
450 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
454 c.in.domain_handle = &domain_handle;
455 user_name.string = username;
456 c.in.account_name = &user_name;
457 c.in.acct_flags = ACB_NORMAL;
458 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
459 user_handle = talloc(mem_ctx, struct policy_handle);
460 c.out.user_handle = user_handle;
461 c.out.access_granted = &access_granted;
462 c.out.rid = &user_rid;
464 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
466 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
467 struct samr_LookupNames ln;
468 struct samr_OpenUser ou;
470 ln.in.domain_handle = &domain_handle;
472 ln.in.names = &user_name;
474 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
475 if (!NT_STATUS_IS_OK(status)) {
476 d_printf("samr_LookupNames failed: %s\n",
481 ou.in.domain_handle = &domain_handle;
482 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
483 user_rid = ou.in.rid = ln.out.rids.ids[0];
484 ou.out.user_handle = user_handle;
486 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
487 if (!NT_STATUS_IS_OK(status)) {
488 d_printf("samr_OpenUser failed: %s\n",
494 if (!NT_STATUS_IS_OK(status)) {
495 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
499 *result_pipe = samr_pipe;
500 *result_handle = user_handle;
502 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
514 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
515 struct loadparm_context *lp_ctx,
516 struct cli_credentials *admin_creds,
517 const char *username, const char *password,
519 struct dom_sid **user_sid)
523 struct dcerpc_pipe *samr_pipe;
524 struct policy_handle *wks_handle;
527 if (!(tmp_ctx = talloc_new(mem_ctx))) {
528 d_printf("talloc_init failed\n");
532 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
533 DCERPC_AUTH_TYPE_NTLMSSP,
534 DCERPC_AUTH_LEVEL_INTEGRITY,
535 username, domain_name, &samr_pipe, &wks_handle,
537 if (!NT_STATUS_IS_OK(status)) {
538 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
543 struct samr_SetUserInfo2 sui2;
544 struct samr_SetUserInfo sui;
545 struct samr_QueryUserInfo qui;
546 union samr_UserInfo u_info;
547 DATA_BLOB session_key;
551 encode_pw_buffer(u_info.info23.password.data, password,
554 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
555 if (!NT_STATUS_IS_OK(status)) {
556 d_printf("dcerpc_fetch_session_key failed\n");
559 arcfour_crypt_blob(u_info.info23.password.data, 516,
561 u_info.info23.info.password_expired = 0;
562 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
563 SAMR_FIELD_PASSWORD2 |
564 SAMR_FIELD_EXPIRED_FLAG;
565 sui2.in.user_handle = wks_handle;
566 sui2.in.info = &u_info;
569 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
570 if (!NT_STATUS_IS_OK(status)) {
571 d_printf("samr_SetUserInfo(23) failed: %s\n",
576 u_info.info16.acct_flags = ACB_NORMAL;
577 sui.in.user_handle = wks_handle;
578 sui.in.info = &u_info;
581 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
582 if (!NT_STATUS_IS_OK(status)) {
583 d_printf("samr_SetUserInfo(16) failed\n");
587 qui.in.user_handle = wks_handle;
590 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
591 if (!NT_STATUS_IS_OK(status)) {
592 d_printf("samr_QueryUserInfo(21) failed\n");
596 qui.out.info->info21.allow_password_change = 0;
597 qui.out.info->info21.force_password_change = 0;
598 qui.out.info->info21.account_name.string = NULL;
599 qui.out.info->info21.rid = 0;
600 qui.out.info->info21.acct_expiry = 0;
601 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
603 u_info.info21 = qui.out.info->info21;
604 sui.in.user_handle = wks_handle;
605 sui.in.info = &u_info;
608 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
609 if (!NT_STATUS_IS_OK(status)) {
610 d_printf("samr_SetUserInfo(21) failed\n");
615 *domain_name= talloc_steal(mem_ctx, *domain_name);
616 *user_sid = talloc_steal(mem_ctx, *user_sid);
619 talloc_free(tmp_ctx);
627 static bool delete_user(struct smbcli_state *cli,
628 struct loadparm_context *lp_ctx,
629 struct cli_credentials *admin_creds,
630 const char *username)
635 struct dcerpc_pipe *samr_pipe;
636 struct policy_handle *user_handle;
639 if ((mem_ctx = talloc_init("leave")) == NULL) {
640 d_printf("talloc_init failed\n");
644 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
645 DCERPC_AUTH_TYPE_NTLMSSP,
646 DCERPC_AUTH_LEVEL_INTEGRITY,
647 username, &dom_name, &samr_pipe,
650 if (!NT_STATUS_IS_OK(status)) {
651 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
656 struct samr_DeleteUser d;
658 d.in.user_handle = user_handle;
659 d.out.user_handle = user_handle;
661 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
662 if (!NT_STATUS_IS_OK(status)) {
663 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
671 talloc_free(mem_ctx);
676 * Do a Samba3-style join
679 static bool join3(struct smbcli_state *cli,
680 struct loadparm_context *lp_ctx,
682 struct cli_credentials *admin_creds,
683 struct cli_credentials *wks_creds)
688 struct dcerpc_pipe *samr_pipe;
689 struct policy_handle *wks_handle;
691 NTTIME last_password_change;
693 if ((mem_ctx = talloc_init("join3")) == NULL) {
694 d_printf("talloc_init failed\n");
698 status = get_usr_handle(
699 cli, mem_ctx, lp_ctx, admin_creds,
700 DCERPC_AUTH_TYPE_NTLMSSP,
701 DCERPC_AUTH_LEVEL_PRIVACY,
702 talloc_asprintf(mem_ctx, "%s$",
703 cli_credentials_get_workstation(wks_creds)),
704 &dom_name, &samr_pipe, &wks_handle, NULL);
706 if (!NT_STATUS_IS_OK(status)) {
707 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
712 struct samr_QueryUserInfo q;
714 q.in.user_handle = wks_handle;
717 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
718 if (!NT_STATUS_IS_OK(status)) {
719 d_printf("(%s) QueryUserInfo failed: %s\n",
720 __location__, nt_errstr(status));
724 last_password_change = q.out.info->info21.last_password_change;
727 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
730 struct samr_SetUserInfo2 sui2;
731 union samr_UserInfo u_info;
732 struct samr_UserInfo21 *i21 = &u_info.info25.info;
733 DATA_BLOB session_key;
734 DATA_BLOB confounded_session_key = data_blob_talloc(
736 struct MD5Context ctx;
737 uint8_t confounder[16];
741 i21->full_name.string = talloc_asprintf(
743 cli_credentials_get_workstation(wks_creds));
744 i21->acct_flags = ACB_WSTRUST;
745 i21->fields_present = SAMR_FIELD_FULL_NAME |
746 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
748 encode_pw_buffer(u_info.info25.password.data,
749 cli_credentials_get_password(wks_creds),
751 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
752 if (!NT_STATUS_IS_OK(status)) {
753 d_printf("dcerpc_fetch_session_key failed: %s\n",
757 generate_random_buffer((uint8_t *)confounder, 16);
760 MD5Update(&ctx, confounder, 16);
761 MD5Update(&ctx, session_key.data, session_key.length);
762 MD5Final(confounded_session_key.data, &ctx);
764 arcfour_crypt_blob(u_info.info25.password.data, 516,
765 &confounded_session_key);
766 memcpy(&u_info.info25.password.data[516], confounder, 16);
768 sui2.in.user_handle = wks_handle;
770 sui2.in.info = &u_info;
772 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
773 if (!NT_STATUS_IS_OK(status)) {
774 d_printf("samr_SetUserInfo2(25) failed: %s\n",
779 struct samr_SetUserInfo2 sui2;
780 struct samr_SetUserInfo sui;
781 union samr_UserInfo u_info;
782 DATA_BLOB session_key;
784 encode_pw_buffer(u_info.info24.password.data,
785 cli_credentials_get_password(wks_creds),
787 u_info.info24.pw_len =
788 strlen_m(cli_credentials_get_password(wks_creds))*2;
790 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
791 if (!NT_STATUS_IS_OK(status)) {
792 d_printf("dcerpc_fetch_session_key failed\n");
795 arcfour_crypt_blob(u_info.info24.password.data, 516,
797 sui2.in.user_handle = wks_handle;
798 sui2.in.info = &u_info;
801 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
802 if (!NT_STATUS_IS_OK(status)) {
803 d_printf("samr_SetUserInfo(24) failed: %s\n",
808 u_info.info16.acct_flags = ACB_WSTRUST;
809 sui.in.user_handle = wks_handle;
810 sui.in.info = &u_info;
813 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
814 if (!NT_STATUS_IS_OK(status)) {
815 d_printf("samr_SetUserInfo(16) failed\n");
821 struct samr_QueryUserInfo q;
823 q.in.user_handle = wks_handle;
826 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
827 if (!NT_STATUS_IS_OK(status)) {
828 d_printf("(%s) QueryUserInfo failed: %s\n",
829 __location__, nt_errstr(status));
834 if (last_password_change
835 == q.out.info->info21.last_password_change) {
836 d_printf("(%s) last_password_change unchanged "
837 "during join, level25 must change "
838 "it\n", __location__);
843 if (last_password_change
844 != q.out.info->info21.last_password_change) {
845 d_printf("(%s) last_password_change changed "
846 "during join, level24 doesn't "
847 "change it\n", __location__);
856 talloc_free(mem_ctx);
861 * Do a ReqChallenge/Auth2 and get the wks creds
864 static bool auth2(struct smbcli_state *cli,
865 struct loadparm_context *lp_ctx,
866 struct cli_credentials *wks_cred)
869 struct dcerpc_pipe *net_pipe;
872 struct netr_ServerReqChallenge r;
873 struct netr_Credential netr_cli_creds;
874 struct netr_Credential netr_srv_creds;
875 uint32_t negotiate_flags;
876 struct netr_ServerAuthenticate2 a;
877 struct creds_CredentialState *creds_state;
878 struct netr_Credential netr_cred;
879 struct samr_Password mach_pw;
881 mem_ctx = talloc_new(NULL);
882 if (mem_ctx == NULL) {
883 d_printf("talloc_new failed\n");
887 net_pipe = dcerpc_pipe_init(mem_ctx,
888 cli->transport->socket->event.ctx,
889 lp_iconv_convenience(lp_ctx));
890 if (net_pipe == NULL) {
891 d_printf("dcerpc_pipe_init failed\n");
895 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
896 if (!NT_STATUS_IS_OK(status)) {
897 d_printf("dcerpc_pipe_open_smb failed: %s\n",
902 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
903 if (!NT_STATUS_IS_OK(status)) {
904 d_printf("dcerpc_bind_auth_none failed: %s\n",
909 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
910 r.in.server_name = talloc_asprintf(
911 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
912 if (r.in.server_name == NULL) {
913 d_printf("talloc_asprintf failed\n");
916 generate_random_buffer(netr_cli_creds.data,
917 sizeof(netr_cli_creds.data));
918 r.in.credentials = &netr_cli_creds;
919 r.out.credentials = &netr_srv_creds;
921 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
922 if (!NT_STATUS_IS_OK(status)) {
923 d_printf("netr_ServerReqChallenge failed: %s\n",
928 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
929 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
931 creds_state = talloc(mem_ctx, struct creds_CredentialState);
932 creds_client_init(creds_state, r.in.credentials,
933 r.out.credentials, &mach_pw,
934 &netr_cred, negotiate_flags);
936 a.in.server_name = talloc_asprintf(
937 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
938 a.in.account_name = talloc_asprintf(
939 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
940 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
941 a.in.secure_channel_type = SEC_CHAN_WKSTA;
942 a.in.negotiate_flags = &negotiate_flags;
943 a.out.negotiate_flags = &negotiate_flags;
944 a.in.credentials = &netr_cred;
945 a.out.credentials = &netr_cred;
947 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
948 if (!NT_STATUS_IS_OK(status)) {
949 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
954 if (!creds_client_check(creds_state, a.out.credentials)) {
955 d_printf("creds_client_check failed\n");
959 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
964 talloc_free(mem_ctx);
969 * Do a couple of schannel protected Netlogon ops: Interactive and Network
970 * login, and change the wks password
973 static bool schan(struct smbcli_state *cli,
974 struct loadparm_context *lp_ctx,
975 struct cli_credentials *wks_creds,
976 struct cli_credentials *user_creds)
981 struct dcerpc_pipe *net_pipe;
984 mem_ctx = talloc_new(NULL);
985 if (mem_ctx == NULL) {
986 d_printf("talloc_new failed\n");
990 net_pipe = dcerpc_pipe_init(mem_ctx,
991 cli->transport->socket->event.ctx,
992 lp_iconv_convenience(lp_ctx));
993 if (net_pipe == NULL) {
994 d_printf("dcerpc_pipe_init failed\n");
998 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
999 if (!NT_STATUS_IS_OK(status)) {
1000 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1006 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1007 DCERPC_DEBUG_PRINT_OUT;
1010 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1011 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1012 wks_creds, lp_ctx, DCERPC_AUTH_TYPE_SCHANNEL,
1013 DCERPC_AUTH_LEVEL_PRIVACY,
1016 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1024 for (i=2; i<4; i++) {
1026 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1027 struct creds_CredentialState *creds_state;
1028 struct netr_Authenticator netr_auth, netr_auth2;
1029 struct netr_NetworkInfo ninfo;
1030 struct netr_PasswordInfo pinfo;
1031 struct netr_LogonSamLogon r;
1033 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1034 CLI_CRED_NTLMv2_AUTH;
1036 chal = data_blob_talloc(mem_ctx, NULL, 8);
1037 if (chal.data == NULL) {
1038 d_printf("data_blob_talloc failed\n");
1042 generate_random_buffer(chal.data, chal.length);
1043 names_blob = NTLMv2_generate_names_blob(
1044 mem_ctx, lp_iconv_convenience(lp_ctx),
1045 cli_credentials_get_workstation(user_creds),
1046 cli_credentials_get_domain(user_creds));
1047 status = cli_credentials_get_ntlm_response(
1048 user_creds, mem_ctx, &flags, chal, names_blob,
1049 &lm_resp, &nt_resp, NULL, NULL);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 d_printf("cli_credentials_get_ntlm_response failed:"
1052 " %s\n", nt_errstr(status));
1056 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1057 creds_client_authenticator(creds_state, &netr_auth);
1059 ninfo.identity_info.account_name.string =
1060 cli_credentials_get_username(user_creds);
1061 ninfo.identity_info.domain_name.string =
1062 cli_credentials_get_domain(user_creds);
1063 ninfo.identity_info.parameter_control = 0;
1064 ninfo.identity_info.logon_id_low = 0;
1065 ninfo.identity_info.logon_id_high = 0;
1066 ninfo.identity_info.workstation.string =
1067 cli_credentials_get_workstation(user_creds);
1068 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1069 ninfo.nt.length = nt_resp.length;
1070 ninfo.nt.data = nt_resp.data;
1071 ninfo.lm.length = lm_resp.length;
1072 ninfo.lm.data = lm_resp.data;
1074 r.in.server_name = talloc_asprintf(
1075 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1076 ZERO_STRUCT(netr_auth2);
1077 r.in.computer_name =
1078 cli_credentials_get_workstation(wks_creds);
1079 r.in.credential = &netr_auth;
1080 r.in.return_authenticator = &netr_auth2;
1081 r.in.logon_level = 2;
1082 r.in.validation_level = i;
1083 r.in.logon.network = &ninfo;
1084 r.out.return_authenticator = NULL;
1086 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1087 if (!NT_STATUS_IS_OK(status)) {
1088 d_printf("netr_LogonSamLogon failed: %s\n",
1093 if ((r.out.return_authenticator == NULL) ||
1094 (!creds_client_check(creds_state,
1095 &r.out.return_authenticator->cred))) {
1096 d_printf("Credentials check failed!\n");
1100 creds_client_authenticator(creds_state, &netr_auth);
1102 pinfo.identity_info = ninfo.identity_info;
1103 ZERO_STRUCT(pinfo.lmpassword.hash);
1104 E_md4hash(cli_credentials_get_password(user_creds),
1105 pinfo.ntpassword.hash);
1106 session_key = data_blob_talloc(mem_ctx,
1107 creds_state->session_key, 16);
1108 arcfour_crypt_blob(pinfo.ntpassword.hash,
1109 sizeof(pinfo.ntpassword.hash),
1112 r.in.logon_level = 1;
1113 r.in.logon.password = &pinfo;
1114 r.out.return_authenticator = NULL;
1116 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1117 if (!NT_STATUS_IS_OK(status)) {
1118 d_printf("netr_LogonSamLogon failed: %s\n",
1123 if ((r.out.return_authenticator == NULL) ||
1124 (!creds_client_check(creds_state,
1125 &r.out.return_authenticator->cred))) {
1126 d_printf("Credentials check failed!\n");
1132 struct netr_ServerPasswordSet s;
1133 char *password = generate_random_str(wks_creds, 8);
1134 struct creds_CredentialState *creds_state;
1136 s.in.server_name = talloc_asprintf(
1137 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1138 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1139 s.in.account_name = talloc_asprintf(
1140 mem_ctx, "%s$", s.in.computer_name);
1141 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1142 E_md4hash(password, s.in.new_password.hash);
1144 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1145 creds_des_encrypt(creds_state, &s.in.new_password);
1146 creds_client_authenticator(creds_state, &s.in.credential);
1148 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1149 if (!NT_STATUS_IS_OK(status)) {
1150 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1154 if (!creds_client_check(creds_state,
1155 &s.out.return_authenticator.cred)) {
1156 printf("Credential chaining failed\n");
1159 cli_credentials_set_password(wks_creds, password,
1165 talloc_free(mem_ctx);
1170 * Delete the wks account again
1173 static bool leave(struct smbcli_state *cli,
1174 struct loadparm_context *lp_ctx,
1175 struct cli_credentials *admin_creds,
1176 struct cli_credentials *wks_creds)
1178 char *wks_name = talloc_asprintf(
1179 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1182 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1183 talloc_free(wks_name);
1188 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1191 bool torture_netlogon_samba3(struct torture_context *torture)
1193 TALLOC_CTX *mem_ctx;
1196 struct smbcli_state *cli;
1197 struct cli_credentials *anon_creds;
1198 struct cli_credentials *wks_creds;
1199 const char *wks_name;
1201 struct smbcli_options options;
1202 struct smbcli_session_options session_options;
1204 wks_name = torture_setting_string(torture, "wksname", NULL);
1205 if (wks_name == NULL) {
1206 wks_name = get_myname();
1209 mem_ctx = talloc_init("torture_netlogon_samba3");
1211 if (mem_ctx == NULL) {
1212 d_printf("talloc_init failed\n");
1216 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1217 d_printf("create_anon_creds failed\n");
1221 lp_smbcli_options(torture->lp_ctx, &options);
1222 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1224 status = smbcli_full_connection(mem_ctx, &cli,
1225 torture_setting_string(torture, "host", NULL),
1226 lp_smb_ports(torture->lp_ctx),
1227 "IPC$", NULL, anon_creds,
1228 lp_resolve_context(torture->lp_ctx),
1229 torture->ev, &options, &session_options);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 d_printf("smbcli_full_connection failed: %s\n",
1236 wks_creds = cli_credentials_init(mem_ctx);
1237 if (wks_creds == NULL) {
1238 d_printf("cli_credentials_init failed\n");
1242 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1243 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1244 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1245 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1246 cli_credentials_set_password(wks_creds,
1247 generate_random_str(wks_creds, 8),
1250 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1251 d_printf("join failed\n");
1255 cli_credentials_set_domain(
1256 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1259 for (i=0; i<2; i++) {
1261 /* Do this more than once, the routine "schan" changes
1262 * the workstation password using the netlogon
1263 * password change routine */
1267 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1268 d_printf("auth2 failed\n");
1272 for (j=0; j<2; j++) {
1273 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1274 d_printf("schan failed\n");
1280 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1281 d_printf("leave failed\n");
1288 talloc_free(mem_ctx);
1293 * Do a simple join, testjoin and leave using specified smb and samr
1297 static bool test_join3(struct torture_context *tctx,
1299 struct cli_credentials *smb_creds,
1300 struct cli_credentials *samr_creds,
1301 const char *wks_name)
1305 struct smbcli_state *cli;
1306 struct cli_credentials *wks_creds;
1307 struct smbcli_options options;
1308 struct smbcli_session_options session_options;
1310 lp_smbcli_options(tctx->lp_ctx, &options);
1311 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1313 status = smbcli_full_connection(tctx, &cli,
1314 torture_setting_string(tctx, "host", NULL),
1315 lp_smb_ports(tctx->lp_ctx),
1316 "IPC$", NULL, smb_creds,
1317 lp_resolve_context(tctx->lp_ctx),
1318 tctx->ev, &options, &session_options);
1319 if (!NT_STATUS_IS_OK(status)) {
1320 d_printf("smbcli_full_connection failed: %s\n",
1325 wks_creds = cli_credentials_init(cli);
1326 if (wks_creds == NULL) {
1327 d_printf("cli_credentials_init failed\n");
1331 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1332 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1333 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1334 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1335 cli_credentials_set_password(wks_creds,
1336 generate_random_str(wks_creds, 8),
1339 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1340 d_printf("join failed\n");
1344 cli_credentials_set_domain(
1345 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1348 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1349 d_printf("auth2 failed\n");
1353 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1354 d_printf("leave failed\n");
1367 * Test the different session key variants. Do it by joining, this uses the
1368 * session key in the setpassword routine. Test the join by doing the auth2.
1371 bool torture_samba3_sessionkey(struct torture_context *torture)
1374 struct cli_credentials *anon_creds;
1375 const char *wks_name;
1377 wks_name = torture_setting_string(torture, "wksname", get_myname());
1379 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1380 d_printf("create_anon_creds failed\n");
1384 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1388 if (!torture_setting_bool(torture, "samba3", false)) {
1390 /* Samba3 in the build farm right now does this happily. Need
1393 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1394 d_printf("join using anonymous bind on an anonymous smb "
1395 "connection succeeded -- HUH??\n");
1400 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1402 d_printf("join using ntlmssp bind on an anonymous smb "
1403 "connection failed\n");
1407 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1408 d_printf("join using anonymous bind on an authenticated smb "
1409 "connection failed\n");
1413 if (!test_join3(torture, false, cmdline_credentials,
1414 cmdline_credentials,
1416 d_printf("join using ntlmssp bind on an authenticated smb "
1417 "connection failed\n");
1422 * The following two are tests for setuserinfolevel 25
1425 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1427 d_printf("join using ntlmssp bind on an anonymous smb "
1428 "connection failed\n");
1432 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1433 d_printf("join using anonymous bind on an authenticated smb "
1434 "connection failed\n");
1444 * open pipe and bind, given an IPC$ context
1447 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1448 struct loadparm_context *lp_ctx,
1449 struct smbcli_tree *tree,
1450 const char *pipe_name,
1451 const struct ndr_interface_table *iface,
1452 struct dcerpc_pipe **p)
1454 struct dcerpc_pipe *result;
1457 if (!(result = dcerpc_pipe_init(
1458 mem_ctx, tree->session->transport->socket->event.ctx,
1459 lp_iconv_convenience(lp_ctx)))) {
1460 return NT_STATUS_NO_MEMORY;
1463 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1464 if (!NT_STATUS_IS_OK(status)) {
1465 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1467 talloc_free(result);
1471 status = dcerpc_bind_auth_none(result, iface);
1472 if (!NT_STATUS_IS_OK(status)) {
1473 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1474 talloc_free(result);
1479 return NT_STATUS_OK;
1483 * Sane wrapper around lsa_LookupNames
1486 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1487 struct dcerpc_pipe *p,
1491 struct lsa_ObjectAttribute attr;
1492 struct lsa_QosInfo qos;
1493 struct lsa_OpenPolicy2 r;
1496 struct policy_handle handle;
1497 struct lsa_LookupNames l;
1498 struct lsa_TransSidArray sids;
1499 struct lsa_String lsa_name;
1501 struct dom_sid *result;
1502 TALLOC_CTX *tmp_ctx;
1504 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1509 qos.impersonation_level = 2;
1510 qos.context_mode = 1;
1511 qos.effective_only = 0;
1514 attr.root_dir = NULL;
1515 attr.object_name = NULL;
1516 attr.attributes = 0;
1517 attr.sec_desc = NULL;
1518 attr.sec_qos = &qos;
1520 r.in.system_name = "\\";
1522 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1523 r.out.handle = &handle;
1525 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1526 if (!NT_STATUS_IS_OK(status)) {
1527 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1528 talloc_free(tmp_ctx);
1535 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1537 l.in.handle = &handle;
1539 l.in.names = &lsa_name;
1542 l.in.count = &count;
1543 l.out.count = &count;
1546 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1547 if (!NT_STATUS_IS_OK(status)) {
1548 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1550 talloc_free(tmp_ctx);
1554 result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1555 l.out.sids->sids[0].rid);
1557 c.in.handle = &handle;
1558 c.out.handle = &handle;
1560 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1561 if (!NT_STATUS_IS_OK(status)) {
1562 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1563 talloc_free(tmp_ctx);
1567 talloc_free(tmp_ctx);
1572 * Find out the user SID on this connection
1575 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1576 struct loadparm_context *lp_ctx,
1577 struct smbcli_tree *tree)
1579 struct dcerpc_pipe *lsa;
1580 struct lsa_GetUserName r;
1582 struct lsa_StringPointer authority_name_p;
1583 struct dom_sid *result;
1585 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1586 &ndr_table_lsarpc, &lsa);
1587 if (!NT_STATUS_IS_OK(status)) {
1588 d_printf("(%s) Could not bind to LSA: %s\n",
1589 __location__, nt_errstr(status));
1593 r.in.system_name = "\\";
1594 r.in.account_name = NULL;
1595 authority_name_p.string = NULL;
1596 r.in.authority_name = &authority_name_p;
1598 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1600 if (!NT_STATUS_IS_OK(status)) {
1601 printf("(%s) GetUserName failed - %s\n",
1602 __location__, nt_errstr(status));
1607 result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1608 r.out.authority_name->string->string);
1614 static int destroy_tree(struct smbcli_tree *tree)
1616 smb_tree_disconnect(tree);
1621 * Do a tcon, given a session
1624 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1625 struct smbcli_session *session,
1626 const char *sharename,
1627 struct smbcli_tree **res)
1629 struct smbcli_tree *result;
1630 TALLOC_CTX *tmp_ctx;
1631 union smb_tcon tcon;
1634 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1635 return NT_STATUS_NO_MEMORY;
1638 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1639 talloc_free(tmp_ctx);
1640 return NT_STATUS_NO_MEMORY;
1643 tcon.generic.level = RAW_TCON_TCONX;
1644 tcon.tconx.in.flags = 0;
1645 tcon.tconx.in.password = data_blob(NULL, 0);
1646 tcon.tconx.in.path = sharename;
1647 tcon.tconx.in.device = "?????";
1649 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1650 if (!NT_STATUS_IS_OK(status)) {
1651 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1653 talloc_free(tmp_ctx);
1657 result->tid = tcon.tconx.out.tid;
1658 result = talloc_steal(mem_ctx, result);
1659 talloc_set_destructor(result, destroy_tree);
1660 talloc_free(tmp_ctx);
1662 return NT_STATUS_OK;
1666 * Test the getusername behaviour
1669 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1672 struct smbcli_state *cli;
1673 TALLOC_CTX *mem_ctx;
1675 struct dom_sid *user_sid;
1676 struct dom_sid *created_sid;
1677 struct cli_credentials *anon_creds;
1678 struct cli_credentials *user_creds;
1680 struct smbcli_options options;
1681 struct smbcli_session_options session_options;
1683 if (!(mem_ctx = talloc_new(torture))) {
1687 lp_smbcli_options(torture->lp_ctx, &options);
1688 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1690 status = smbcli_full_connection(
1691 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1692 lp_smb_ports(torture->lp_ctx),
1693 "IPC$", NULL, cmdline_credentials,
1694 lp_resolve_context(torture->lp_ctx),
1695 torture->ev, &options, &session_options);
1696 if (!NT_STATUS_IS_OK(status)) {
1697 d_printf("(%s) smbcli_full_connection failed: %s\n",
1698 __location__, nt_errstr(status));
1703 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1704 d_printf("(%s) whoami on auth'ed connection failed\n",
1711 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1712 d_printf("(%s) create_anon_creds failed\n", __location__);
1717 status = smbcli_full_connection(
1718 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1719 lp_smb_ports(torture->lp_ctx),
1720 "IPC$", NULL, anon_creds,
1721 lp_resolve_context(torture->lp_ctx),
1722 torture->ev, &options, &session_options);
1723 if (!NT_STATUS_IS_OK(status)) {
1724 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1725 __location__, nt_errstr(status));
1730 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1731 d_printf("(%s) whoami on anon connection failed\n",
1737 if (!dom_sid_equal(user_sid,
1738 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1739 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1740 "S-1-5-7", __location__,
1741 dom_sid_string(mem_ctx, user_sid));
1745 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1746 d_printf("(%s) cli_credentials_init failed\n", __location__);
1751 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1752 cli_credentials_set_username(user_creds, "torture_username",
1754 cli_credentials_set_password(user_creds,
1755 generate_random_str(user_creds, 8),
1758 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1759 cli_credentials_get_username(user_creds),
1760 cli_credentials_get_password(user_creds),
1761 &domain_name, &created_sid)) {
1762 d_printf("(%s) create_user failed\n", __location__);
1767 cli_credentials_set_domain(user_creds, domain_name,
1771 struct smbcli_session *session2;
1772 struct smb_composite_sesssetup setup;
1773 struct smbcli_tree *tree;
1775 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1776 if (session2 == NULL) {
1777 d_printf("(%s) smbcli_session_init failed\n",
1782 setup.in.sesskey = cli->transport->negotiate.sesskey;
1783 setup.in.capabilities = cli->transport->negotiate.capabilities;
1784 setup.in.workgroup = "";
1785 setup.in.credentials = user_creds;
1787 status = smb_composite_sesssetup(session2, &setup);
1788 if (!NT_STATUS_IS_OK(status)) {
1789 d_printf("(%s) session setup with new user failed: "
1790 "%s\n", __location__, nt_errstr(status));
1794 session2->vuid = setup.out.vuid;
1796 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1798 d_printf("(%s) secondary_tcon failed\n",
1804 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1805 d_printf("(%s) whoami on user connection failed\n",
1814 d_printf("Created %s, found %s\n",
1815 dom_sid_string(mem_ctx, created_sid),
1816 dom_sid_string(mem_ctx, user_sid));
1818 if (!dom_sid_equal(created_sid, user_sid)) {
1823 if (!delete_user(cli, torture->lp_ctx,
1824 cmdline_credentials,
1825 cli_credentials_get_username(user_creds))) {
1826 d_printf("(%s) delete_user failed\n", __location__);
1831 talloc_free(mem_ctx);
1835 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1836 const char *sharename)
1839 struct srvsvc_NetShareGetInfo r;
1840 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1844 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1845 dcerpc_server_name(p));
1846 r.in.share_name = sharename;
1848 for (i=0;i<ARRAY_SIZE(levels);i++) {
1849 r.in.level = levels[i];
1853 printf("testing NetShareGetInfo level %u on share '%s'\n",
1854 r.in.level, r.in.share_name);
1856 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1857 if (!NT_STATUS_IS_OK(status)) {
1858 printf("NetShareGetInfo level %u on share '%s' failed"
1859 " - %s\n", r.in.level, r.in.share_name,
1864 if (!W_ERROR_IS_OK(r.out.result)) {
1865 printf("NetShareGetInfo level %u on share '%s' failed "
1866 "- %s\n", r.in.level, r.in.share_name,
1867 win_errstr(r.out.result));
1876 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1877 const char **one_sharename)
1880 struct srvsvc_NetShareEnum r;
1881 struct srvsvc_NetShareCtr0 c0;
1882 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1886 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1887 r.in.ctr.ctr0 = &c0;
1888 r.in.ctr.ctr0->count = 0;
1889 r.in.ctr.ctr0->array = NULL;
1890 r.in.max_buffer = (uint32_t)-1;
1891 r.in.resume_handle = NULL;
1893 for (i=0;i<ARRAY_SIZE(levels);i++) {
1894 r.in.level = levels[i];
1898 printf("testing NetShareEnum level %u\n", r.in.level);
1899 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1900 if (!NT_STATUS_IS_OK(status)) {
1901 printf("NetShareEnum level %u failed - %s\n",
1902 r.in.level, nt_errstr(status));
1906 if (!W_ERROR_IS_OK(r.out.result)) {
1907 printf("NetShareEnum level %u failed - %s\n",
1908 r.in.level, win_errstr(r.out.result));
1911 if (r.in.level == 0) {
1912 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1913 if (ctr->count > 0) {
1914 *one_sharename = ctr->array[0].name;
1922 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
1924 struct dcerpc_pipe *p;
1925 TALLOC_CTX *mem_ctx;
1927 const char *sharename = NULL;
1928 struct smbcli_state *cli;
1931 if (!(mem_ctx = talloc_new(torture))) {
1935 if (!(torture_open_connection_share(
1936 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
1937 "IPC$", torture->ev))) {
1938 talloc_free(mem_ctx);
1942 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
1943 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
1944 if (!NT_STATUS_IS_OK(status)) {
1945 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1946 __location__, nt_errstr(status));
1951 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1952 if (sharename == NULL) {
1953 printf("did not get sharename\n");
1955 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1959 talloc_free(mem_ctx);
1964 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1965 * NT_STATUS_NO_SAM_ACCOUNT
1968 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1970 TALLOC_CTX *mem_ctx;
1971 struct dcerpc_pipe *net_pipe;
1973 bool result = false;
1975 struct netr_ServerReqChallenge r;
1976 struct netr_Credential netr_cli_creds;
1977 struct netr_Credential netr_srv_creds;
1978 uint32_t negotiate_flags;
1979 struct netr_ServerAuthenticate2 a;
1980 struct creds_CredentialState *creds_state;
1981 struct netr_Credential netr_cred;
1982 struct samr_Password mach_pw;
1983 struct smbcli_state *cli;
1985 if (!(mem_ctx = talloc_new(torture))) {
1986 d_printf("talloc_new failed\n");
1990 if (!(wksname = generate_random_str_list(
1991 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
1992 d_printf("generate_random_str_list failed\n");
1996 if (!(torture_open_connection_share(
1998 torture, torture_setting_string(torture, "host", NULL),
1999 "IPC$", torture->ev))) {
2000 d_printf("IPC$ connection failed\n");
2004 if (!(net_pipe = dcerpc_pipe_init(
2005 mem_ctx, cli->transport->socket->event.ctx,
2006 lp_iconv_convenience(torture->lp_ctx)))) {
2007 d_printf("dcerpc_pipe_init failed\n");
2011 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2012 if (!NT_STATUS_IS_OK(status)) {
2013 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2018 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2019 if (!NT_STATUS_IS_OK(status)) {
2020 d_printf("dcerpc_bind_auth_none failed: %s\n",
2025 r.in.computer_name = wksname;
2026 r.in.server_name = talloc_asprintf(
2027 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2028 if (r.in.server_name == NULL) {
2029 d_printf("talloc_asprintf failed\n");
2032 generate_random_buffer(netr_cli_creds.data,
2033 sizeof(netr_cli_creds.data));
2034 r.in.credentials = &netr_cli_creds;
2035 r.out.credentials = &netr_srv_creds;
2037 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2038 if (!NT_STATUS_IS_OK(status)) {
2039 d_printf("netr_ServerReqChallenge failed: %s\n",
2044 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2045 E_md4hash("foobar", mach_pw.hash);
2047 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2048 creds_client_init(creds_state, r.in.credentials,
2049 r.out.credentials, &mach_pw,
2050 &netr_cred, negotiate_flags);
2052 a.in.server_name = talloc_asprintf(
2053 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2054 a.in.account_name = talloc_asprintf(
2055 mem_ctx, "%s$", wksname);
2056 a.in.computer_name = wksname;
2057 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2058 a.in.negotiate_flags = &negotiate_flags;
2059 a.out.negotiate_flags = &negotiate_flags;
2060 a.in.credentials = &netr_cred;
2061 a.out.credentials = &netr_cred;
2063 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2065 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2066 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2067 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2074 talloc_free(mem_ctx);
2078 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2079 struct loadparm_context *lp_ctx,
2080 struct smbcli_session *sess,
2081 const char *sharename)
2083 struct smbcli_tree *tree;
2084 TALLOC_CTX *tmp_ctx;
2085 struct dcerpc_pipe *p;
2087 struct srvsvc_NetShareGetInfo r;
2088 struct security_descriptor *result;
2090 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2091 d_printf("talloc_new failed\n");
2095 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2096 d_printf("secondary_tcon failed\n");
2097 talloc_free(tmp_ctx);
2101 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2102 &ndr_table_srvsvc, &p);
2103 if (!NT_STATUS_IS_OK(status)) {
2104 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2105 __location__, nt_errstr(status));
2106 talloc_free(tmp_ctx);
2111 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2114 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2115 dcerpc_server_name(p));
2116 r.in.share_name = sharename;
2119 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2120 if (!NT_STATUS_IS_OK(status)) {
2121 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2123 talloc_free(tmp_ctx);
2127 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2128 talloc_free(tmp_ctx);
2132 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2133 struct loadparm_context *lp_ctx,
2134 struct smbcli_session *sess,
2135 const char *sharename,
2136 struct security_descriptor *sd)
2138 struct smbcli_tree *tree;
2139 TALLOC_CTX *tmp_ctx;
2140 struct dcerpc_pipe *p;
2142 struct sec_desc_buf i;
2143 struct srvsvc_NetShareSetInfo r;
2146 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2147 d_printf("talloc_new failed\n");
2148 return NT_STATUS_NO_MEMORY;
2151 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2152 d_printf("secondary_tcon failed\n");
2153 talloc_free(tmp_ctx);
2154 return NT_STATUS_UNSUCCESSFUL;
2157 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2158 &ndr_table_srvsvc, &p);
2159 if (!NT_STATUS_IS_OK(status)) {
2160 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2161 __location__, nt_errstr(status));
2162 talloc_free(tmp_ctx);
2163 return NT_STATUS_UNSUCCESSFUL;
2167 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2170 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2171 dcerpc_server_name(p));
2172 r.in.share_name = sharename;
2175 r.in.info.info1501 = &i;
2176 r.in.parm_error = &error;
2178 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2179 if (!NT_STATUS_IS_OK(status)) {
2180 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2184 talloc_free(tmp_ctx);
2188 bool try_tcon(TALLOC_CTX *mem_ctx,
2189 struct loadparm_context *lp_ctx,
2190 struct security_descriptor *orig_sd,
2191 struct smbcli_session *session,
2192 const char *sharename, const struct dom_sid *user_sid,
2193 unsigned int access_mask, NTSTATUS expected_tcon,
2194 NTSTATUS expected_mkdir)
2196 TALLOC_CTX *tmp_ctx;
2197 struct smbcli_tree *rmdir_tree, *tree;
2198 struct dom_sid *domain_sid;
2200 struct security_descriptor *sd;
2204 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2205 d_printf("talloc_new failed\n");
2209 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2210 if (!NT_STATUS_IS_OK(status)) {
2211 d_printf("first tcon to delete dir failed\n");
2212 talloc_free(tmp_ctx);
2216 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2218 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2219 &domain_sid, &rid))) {
2220 d_printf("dom_sid_split_rid failed\n");
2221 talloc_free(tmp_ctx);
2225 sd = security_descriptor_dacl_create(
2226 tmp_ctx, 0, "S-1-5-32-544",
2227 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2229 dom_sid_string(mem_ctx, user_sid),
2230 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2232 d_printf("security_descriptor_dacl_create failed\n");
2233 talloc_free(tmp_ctx);
2237 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2238 if (!NT_STATUS_IS_OK(status)) {
2239 d_printf("custom set_sharesec failed: %s\n",
2241 talloc_free(tmp_ctx);
2245 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2246 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2247 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2253 if (!NT_STATUS_IS_OK(status)) {
2254 /* An expected non-access, no point in trying to write */
2258 status = smbcli_mkdir(tree, "sharesec_testdir");
2259 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2260 d_printf("(%s) Expected %s, got %s\n", __location__,
2261 nt_errstr(expected_mkdir), nt_errstr(status));
2266 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2268 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2269 if (!NT_STATUS_IS_OK(status)) {
2270 d_printf("custom set_sharesec failed: %s\n",
2272 talloc_free(tmp_ctx);
2276 talloc_free(tmp_ctx);
2280 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2282 TALLOC_CTX *mem_ctx;
2284 struct smbcli_state *cli;
2285 struct security_descriptor *sd;
2286 struct dom_sid *user_sid;
2288 if (!(mem_ctx = talloc_new(torture))) {
2292 if (!(torture_open_connection_share(
2293 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2294 "IPC$", torture->ev))) {
2295 d_printf("IPC$ connection failed\n");
2296 talloc_free(mem_ctx);
2300 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2301 d_printf("whoami failed\n");
2302 talloc_free(mem_ctx);
2306 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2307 torture_setting_string(torture, "share", NULL));
2309 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2310 torture_setting_string(torture, "share", NULL),
2311 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2313 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2314 torture_setting_string(torture, "share", NULL),
2315 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2316 NT_STATUS_MEDIA_WRITE_PROTECTED);
2318 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2319 torture_setting_string(torture, "share", NULL),
2320 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2322 talloc_free(mem_ctx);
2326 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2328 TALLOC_CTX *mem_ctx;
2330 struct smbcli_state *cli;
2331 struct dcerpc_pipe *p;
2332 struct policy_handle lsa_handle;
2334 struct dom_sid *domain_sid;
2336 if (!(mem_ctx = talloc_new(torture))) {
2340 if (!(torture_open_connection_share(
2341 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2342 "IPC$", torture->ev))) {
2343 d_printf("IPC$ connection failed\n");
2344 talloc_free(mem_ctx);
2348 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2349 &ndr_table_lsarpc, &p);
2350 if (!NT_STATUS_IS_OK(status)) {
2351 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2353 talloc_free(mem_ctx);
2358 struct lsa_ObjectAttribute attr;
2359 struct lsa_OpenPolicy2 o;
2360 o.in.system_name = talloc_asprintf(
2361 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2364 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2365 o.out.handle = &lsa_handle;
2366 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2367 if (!NT_STATUS_IS_OK(status)) {
2368 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2369 __location__, nt_errstr(status));
2370 talloc_free(mem_ctx);
2376 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2381 int levels[] = { 2,3,5,6 };
2383 for (i=0; i<ARRAY_SIZE(levels); i++) {
2384 struct lsa_QueryInfoPolicy r;
2385 r.in.handle = &lsa_handle;
2386 r.in.level = levels[i];
2387 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2388 if (!NT_STATUS_IS_OK(status)) {
2389 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2390 "failed: %s\n", __location__,
2391 levels[i], nt_errstr(status));
2392 talloc_free(mem_ctx);
2395 if (levels[i] == 5) {
2396 domain_sid = r.out.info->account_domain.sid;
2404 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2405 struct smb_iconv_convenience *iconv_convenience,
2408 struct rap_WserverGetInfo r;
2410 char servername[17];
2413 r.in.bufsize = 0xffff;
2415 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2416 if (!NT_STATUS_IS_OK(status)) {
2420 memcpy(servername, r.out.info.info0.name, 16);
2421 servername[16] = '\0';
2423 if (pull_ascii_talloc(mem_ctx, iconv_convenience,
2424 name, servername) < 0) {
2425 return NT_STATUS_NO_MEMORY;
2428 return NT_STATUS_OK;
2432 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2433 struct smbcli_tree *tree,
2434 const char ***printers, int *num_printers)
2436 TALLOC_CTX *mem_ctx;
2438 struct dcerpc_pipe *p;
2439 struct srvsvc_NetShareEnum r;
2440 struct srvsvc_NetShareCtr1 c1_in;
2441 struct srvsvc_NetShareCtr1 *c1;
2444 mem_ctx = talloc_new(ctx);
2445 if (mem_ctx == NULL) {
2446 return NT_STATUS_NO_MEMORY;
2449 status = pipe_bind_smb(mem_ctx, lp_ctx,
2450 tree, "\\srvsvc", &ndr_table_srvsvc,
2452 if (!NT_STATUS_IS_OK(status)) {
2453 d_printf("could not bind to srvsvc pipe\n");
2454 talloc_free(mem_ctx);
2458 r.in.server_unc = talloc_asprintf(
2459 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2462 r.in.ctr.ctr1 = &c1_in;
2463 r.in.max_buffer = (uint32_t)-1;
2464 r.in.resume_handle = NULL;
2466 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2467 if (!NT_STATUS_IS_OK(status)) {
2468 d_printf("NetShareEnum level %u failed - %s\n",
2469 r.in.level, nt_errstr(status));
2470 talloc_free(mem_ctx);
2476 c1 = r.out.ctr.ctr1;
2477 for (i=0; i<c1->count; i++) {
2478 if (c1->array[i].type != STYPE_PRINTQ) {
2481 if (!add_string_to_array(ctx, c1->array[i].name,
2482 printers, num_printers)) {
2484 return NT_STATUS_NO_MEMORY;
2488 talloc_free(mem_ctx);
2489 return NT_STATUS_OK;
2492 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2493 const char *servername, int level, int *num_printers)
2495 struct spoolss_EnumPrinters r;
2499 r.in.flags = PRINTER_ENUM_LOCAL;
2500 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2505 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2506 if (!NT_STATUS_IS_OK(status)) {
2507 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2508 __location__, nt_errstr(status));
2512 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2513 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2514 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2515 win_errstr(r.out.result));
2519 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2520 if (blob.data == NULL) {
2521 d_printf("(%s) data_blob_talloc failed\n", __location__);
2525 r.in.buffer = &blob;
2526 r.in.offered = r.out.needed;
2528 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2529 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2530 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2531 "%s\n", __location__, nt_errstr(status),
2532 win_errstr(r.out.result));
2536 *num_printers = r.out.count;
2541 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2542 struct policy_handle *handle, int level,
2543 union spoolss_PrinterInfo **res)
2545 TALLOC_CTX *mem_ctx;
2546 struct spoolss_GetPrinter r;
2550 mem_ctx = talloc_new(ctx);
2551 if (mem_ctx == NULL) {
2552 return NT_STATUS_NO_MEMORY;
2555 r.in.handle = handle;
2560 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2561 if (!NT_STATUS_IS_OK(status)) {
2562 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2563 __location__, nt_errstr(status));
2564 talloc_free(mem_ctx);
2568 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2569 printf("GetPrinter unexpected return code %s, should "
2570 "be WERR_INSUFFICIENT_BUFFER\n",
2571 win_errstr(r.out.result));
2572 talloc_free(mem_ctx);
2573 return NT_STATUS_UNSUCCESSFUL;
2576 r.in.handle = handle;
2578 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2579 if (blob.data == NULL) {
2580 talloc_free(mem_ctx);
2581 return NT_STATUS_NO_MEMORY;
2583 memset(blob.data, 0, blob.length);
2584 r.in.buffer = &blob;
2585 r.in.offered = r.out.needed;
2587 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2588 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2589 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2590 "%s\n", __location__, nt_errstr(status),
2591 win_errstr(r.out.result));
2592 talloc_free(mem_ctx);
2593 return NT_STATUS_IS_OK(status) ?
2594 NT_STATUS_UNSUCCESSFUL : status;
2598 *res = talloc_steal(ctx, r.out.info);
2601 talloc_free(mem_ctx);
2602 return NT_STATUS_OK;
2605 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2607 TALLOC_CTX *mem_ctx;
2609 struct smbcli_state *cli;
2610 struct dcerpc_pipe *p;
2612 struct policy_handle server_handle, printer_handle;
2613 const char **printers;
2615 struct spoolss_UserLevel1 userlevel1;
2618 if (!(mem_ctx = talloc_new(torture))) {
2622 if (!(torture_open_connection_share(
2623 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2624 "IPC$", torture->ev))) {
2625 d_printf("IPC$ connection failed\n");
2626 talloc_free(mem_ctx);
2630 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2631 if (!NT_STATUS_IS_OK(status)) {
2632 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2633 __location__, nt_errstr(status));
2634 talloc_free(mem_ctx);
2638 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2639 &printers, &num_printers))) {
2640 talloc_free(mem_ctx);
2644 if (num_printers == 0) {
2645 d_printf("Did not find printers\n");
2646 talloc_free(mem_ctx);
2650 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2651 &ndr_table_spoolss, &p);
2652 if (!NT_STATUS_IS_OK(status)) {
2653 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2655 talloc_free(mem_ctx);
2659 ZERO_STRUCT(userlevel1);
2660 userlevel1.client = talloc_asprintf(
2661 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2662 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2663 userlevel1.build = 2600;
2664 userlevel1.major = 3;
2665 userlevel1.minor = 0;
2666 userlevel1.processor = 0;
2669 struct spoolss_OpenPrinterEx r;
2672 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2674 r.in.datatype = NULL;
2675 r.in.access_mask = 0;
2677 r.in.userlevel.level1 = &userlevel1;
2678 r.out.handle = &server_handle;
2680 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2681 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2682 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2683 "%s, %s\n", __location__, nt_errstr(status),
2684 win_errstr(r.out.result));
2685 talloc_free(mem_ctx);
2691 struct spoolss_ClosePrinter r;
2693 r.in.handle = &server_handle;
2694 r.out.handle = &server_handle;
2696 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2697 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2698 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2699 "%s, %s\n", __location__, nt_errstr(status),
2700 win_errstr(r.out.result));
2701 talloc_free(mem_ctx);
2707 struct spoolss_OpenPrinterEx r;
2710 r.in.printername = talloc_asprintf(
2711 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2712 r.in.datatype = NULL;
2713 r.in.access_mask = 0;
2715 r.in.userlevel.level1 = &userlevel1;
2716 r.out.handle = &printer_handle;
2718 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2719 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2720 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2721 "%s, %s\n", __location__, nt_errstr(status),
2722 win_errstr(r.out.result));
2723 talloc_free(mem_ctx);
2731 for (i=0; i<8; i++) {
2732 status = getprinterinfo(mem_ctx, p, &printer_handle,
2734 if (!NT_STATUS_IS_OK(status)) {
2735 d_printf("(%s) getprinterinfo %d failed: %s\n",
2736 __location__, i, nt_errstr(status));
2743 struct spoolss_ClosePrinter r;
2745 r.in.handle = &printer_handle;
2746 r.out.handle = &printer_handle;
2748 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2749 if (!NT_STATUS_IS_OK(status)) {
2750 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2751 "%s\n", __location__, nt_errstr(status));
2752 talloc_free(mem_ctx);
2759 if (!enumprinters(mem_ctx, p, servername, 1,
2761 d_printf("(%s) enumprinters failed\n", __location__);
2762 talloc_free(mem_ctx);
2765 if (num_printers != num_enumerated) {
2766 d_printf("(%s) netshareenum gave %d printers, "
2767 "enumprinters lvl 1 gave %d\n", __location__,
2768 num_printers, num_enumerated);
2769 talloc_free(mem_ctx);
2776 if (!enumprinters(mem_ctx, p, servername, 2,
2778 d_printf("(%s) enumprinters failed\n", __location__);
2779 talloc_free(mem_ctx);
2782 if (num_printers != num_enumerated) {
2783 d_printf("(%s) netshareenum gave %d printers, "
2784 "enumprinters lvl 2 gave %d\n", __location__,
2785 num_printers, num_enumerated);
2786 talloc_free(mem_ctx);
2791 talloc_free(mem_ctx);
2796 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2798 TALLOC_CTX *mem_ctx;
2799 struct smbcli_state *cli;
2800 struct dcerpc_pipe *p;
2804 if (!(mem_ctx = talloc_new(torture))) {
2808 if (!(torture_open_connection_share(
2809 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2810 "IPC$", torture->ev))) {
2811 d_printf("IPC$ connection failed\n");
2812 talloc_free(mem_ctx);
2816 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2817 if (!NT_STATUS_IS_OK(status)) {
2818 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2819 __location__, nt_errstr(status));
2820 talloc_free(mem_ctx);
2824 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2825 &ndr_table_wkssvc, &p);
2826 if (!NT_STATUS_IS_OK(status)) {
2827 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2829 talloc_free(mem_ctx);
2834 struct wkssvc_NetWkstaInfo100 wks100;
2835 union wkssvc_NetWkstaInfo info;
2836 struct wkssvc_NetWkstaGetInfo r;
2838 r.in.server_name = "\\foo";
2840 info.info100 = &wks100;
2843 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2844 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2845 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2846 "%s, %s\n", __location__, nt_errstr(status),
2847 win_errstr(r.out.result));
2848 talloc_free(mem_ctx);
2852 if (strcmp(servername,
2853 r.out.info->info100->server_name) != 0) {
2854 d_printf("(%s) servername inconsistency: RAP=%s, "
2855 "dcerpc_wkssvc_NetWksGetInfo=%s",
2856 __location__, servername,
2857 r.out.info->info100->server_name);
2858 talloc_free(mem_ctx);
2863 talloc_free(mem_ctx);
2867 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2868 struct policy_handle *handle)
2870 struct winreg_CloseKey c;
2872 TALLOC_CTX *mem_ctx;
2874 c.in.handle = c.out.handle = handle;
2876 if (!(mem_ctx = talloc_new(p))) {
2877 return NT_STATUS_NO_MEMORY;
2880 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2881 talloc_free(mem_ctx);
2883 if (!NT_STATUS_IS_OK(status)) {
2887 if (!W_ERROR_IS_OK(c.out.result)) {
2888 return werror_to_ntstatus(c.out.result);
2891 return NT_STATUS_OK;
2894 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2895 TALLOC_CTX *mem_ctx)
2897 uint32_t enum_index = 0;
2900 struct winreg_EnumValue r;
2901 struct winreg_StringBuf name;
2902 enum winreg_Type type = 0;
2905 uint32_t size, length;
2907 r.in.handle = handle;
2908 r.in.enum_index = enum_index;
2911 r.in.name = r.out.name = &name;
2917 r.in.length = &length;
2919 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2920 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2921 return NT_STATUS_OK;
2927 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
2928 TALLOC_CTX *mem_ctx, int depth)
2930 struct winreg_EnumKey r;
2931 struct winreg_StringBuf class, name;
2936 return NT_STATUS_OK;
2942 r.in.handle = handle;
2943 r.in.enum_index = 0;
2945 r.in.keyclass = &class;
2947 r.in.last_changed_time = &t;
2950 TALLOC_CTX *tmp_ctx;
2951 struct winreg_OpenKey o;
2952 struct policy_handle key_handle;
2955 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2956 return NT_STATUS_NO_MEMORY;
2962 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2963 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2964 /* We're done enumerating */
2965 talloc_free(tmp_ctx);
2966 return NT_STATUS_OK;
2969 for (i=0; i<10-depth; i++)
2971 printf("%s\n", r.out.name->name);
2974 o.in.parent_handle = handle;
2975 o.in.keyname.name = r.out.name->name;
2977 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2978 o.out.handle = &key_handle;
2980 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2981 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2982 enumkeys(p, &key_handle, tmp_ctx, depth-1);
2983 enumvalues(p, &key_handle, tmp_ctx);
2984 status = winreg_close(p, &key_handle);
2985 if (!NT_STATUS_IS_OK(status)) {
2990 talloc_free(tmp_ctx);
2992 r.in.enum_index += 1;
2995 return NT_STATUS_OK;
2998 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3000 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3001 const char *name, winreg_open_fn open_fn)
3003 struct policy_handle handle;
3004 struct winreg_OpenHKLM r;
3007 r.in.system_name = 0;
3008 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3009 r.out.handle = &handle;
3011 status = open_fn(p, mem_ctx, &r);
3012 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3013 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3014 nt_errstr(status), win_errstr(r.out.result));
3018 enumkeys(p, &handle, mem_ctx, 4);
3020 status = winreg_close(p, &handle);
3021 if (!NT_STATUS_IS_OK(status)) {
3022 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3023 __location__, nt_errstr(status));
3030 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3033 struct dcerpc_pipe *p;
3034 TALLOC_CTX *mem_ctx;
3040 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3041 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3042 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3043 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3044 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3049 mem_ctx = talloc_init("torture_rpc_winreg");
3051 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3053 if (!NT_STATUS_IS_OK(status)) {
3054 talloc_free(mem_ctx);
3059 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3061 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3062 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3067 talloc_free(mem_ctx);
3072 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3073 struct loadparm_context *lp_ctx,
3074 struct smbcli_state *cli,
3076 struct srvsvc_NetShareInfo502 **info)
3078 struct smbcli_tree *ipc;
3079 struct dcerpc_pipe *p;
3080 struct srvsvc_NetShareGetInfo r;
3083 if (!(p = dcerpc_pipe_init(cli,
3084 cli->transport->socket->event.ctx,
3085 lp_iconv_convenience(lp_ctx)))) {
3086 status = NT_STATUS_NO_MEMORY;
3090 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3091 if (!NT_STATUS_IS_OK(status)) {
3095 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3096 if (!NT_STATUS_IS_OK(status)) {
3097 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3102 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3103 if (!NT_STATUS_IS_OK(status)) {
3104 d_printf("dcerpc_bind_auth_none failed: %s\n",
3109 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3110 dcerpc_server_name(p));
3111 r.in.share_name = share;
3114 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3115 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3116 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3117 nt_errstr(status), win_errstr(r.out.result));
3121 *info = talloc_move(mem_ctx, &r.out.info.info502);
3122 return NT_STATUS_OK;
3130 * Get us a handle on HKLM\
3133 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3134 struct smbcli_state *cli,
3135 struct smb_iconv_convenience *iconv_convenience,
3136 struct dcerpc_pipe **pipe_p,
3137 struct policy_handle **handle)
3139 struct smbcli_tree *ipc;
3140 struct dcerpc_pipe *p;
3141 struct winreg_OpenHKLM r;
3143 struct policy_handle *result;
3145 result = talloc(mem_ctx, struct policy_handle);
3147 if (result == NULL) {
3148 return NT_STATUS_NO_MEMORY;
3151 if (!(p = dcerpc_pipe_init(result,
3152 cli->transport->socket->event.ctx,
3153 iconv_convenience))) {
3154 status = NT_STATUS_NO_MEMORY;
3158 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3159 if (!NT_STATUS_IS_OK(status)) {
3163 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3164 if (!NT_STATUS_IS_OK(status)) {
3165 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3170 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3171 if (!NT_STATUS_IS_OK(status)) {
3172 d_printf("dcerpc_bind_auth_none failed: %s\n",
3177 r.in.system_name = 0;
3178 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3179 r.out.handle = result;
3181 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3182 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3183 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3184 nt_errstr(status), win_errstr(r.out.result));
3190 return NT_STATUS_OK;
3193 talloc_free(result);
3197 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3198 struct smb_iconv_convenience *iconv_convenience,
3199 const char *sharename)
3201 struct dcerpc_pipe *p;
3202 struct policy_handle *hklm = NULL;
3203 struct policy_handle new_handle;
3204 struct winreg_CreateKey c;
3205 struct winreg_CloseKey cl;
3206 enum winreg_CreateAction action_taken;
3208 TALLOC_CTX *mem_ctx;
3210 mem_ctx = talloc_new(cli);
3211 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3213 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3214 if (!NT_STATUS_IS_OK(status)) {
3215 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3220 c.in.name.name = talloc_asprintf(
3221 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3222 if (c.in.name.name == NULL) {
3223 d_printf("talloc_asprintf failed\n");
3224 status = NT_STATUS_NO_MEMORY;
3227 c.in.keyclass.name = "";
3229 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3230 c.in.secdesc = NULL;
3231 c.in.action_taken = &action_taken;
3232 c.out.new_handle = &new_handle;
3233 c.out.action_taken = &action_taken;
3235 status = dcerpc_winreg_CreateKey(p, p, &c);
3236 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3237 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3238 nt_errstr(status), win_errstr(c.out.result));
3242 cl.in.handle = &new_handle;
3243 cl.out.handle = &new_handle;
3244 status = dcerpc_winreg_CloseKey(p, p, &cl);
3245 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3246 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3247 nt_errstr(status), win_errstr(cl.out.result));
3253 talloc_free(mem_ctx);
3257 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3258 struct smbcli_state *cli,
3259 const char *sharename)
3261 struct dcerpc_pipe *p;
3262 struct policy_handle *hklm = NULL;
3263 struct winreg_DeleteKey d;
3265 TALLOC_CTX *mem_ctx;
3267 mem_ctx = talloc_new(cli);
3268 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3270 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3272 if (!NT_STATUS_IS_OK(status)) {
3273 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3278 d.in.key.name = talloc_asprintf(
3279 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3280 if (d.in.key.name == NULL) {
3281 d_printf("talloc_asprintf failed\n");
3282 status = NT_STATUS_NO_MEMORY;
3285 status = dcerpc_winreg_DeleteKey(p, p, &d);
3286 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3287 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3288 nt_errstr(status), win_errstr(d.out.result));
3293 talloc_free(mem_ctx);
3297 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3298 struct loadparm_context *lp_ctx,
3299 const char *sharename,
3300 const char *parameter,
3303 struct dcerpc_pipe *p = NULL;
3304 struct policy_handle *hklm = NULL, key_handle;
3305 struct winreg_OpenKey o;
3306 struct winreg_SetValue s;
3311 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3312 if (!NT_STATUS_IS_OK(status)) {
3313 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3317 o.in.parent_handle = hklm;
3318 o.in.keyname.name = talloc_asprintf(
3319 hklm, "software\\samba\\smbconf\\%s", sharename);
3320 if (o.in.keyname.name == NULL) {
3321 d_printf("talloc_asprintf failed\n");
3322 status = NT_STATUS_NO_MEMORY;
3326 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3327 o.out.handle = &key_handle;
3329 status = dcerpc_winreg_OpenKey(p, p, &o);
3330 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3331 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3332 nt_errstr(status), win_errstr(o.out.result));
3336 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3337 value, &type, &val)) {
3338 d_printf("(%s) reg_string_to_val failed\n", __location__);
3342 s.in.handle = &key_handle;
3343 s.in.name.name = parameter;
3345 s.in.data = val.data;
3346 s.in.size = val.length;
3348 status = dcerpc_winreg_SetValue(p, p, &s);
3349 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3350 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3351 nt_errstr(status), win_errstr(s.out.result));
3360 bool torture_samba3_regconfig(struct torture_context *torture)
3362 struct smbcli_state *cli;
3363 struct srvsvc_NetShareInfo502 *i = NULL;
3366 const char *comment = "Dummer Kommentar";
3368 if (!(torture_open_connection(&cli, torture, 0))) {
3372 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3373 if (!NT_STATUS_IS_OK(status)) {
3374 torture_warning(torture, "torture_samba3_createshare failed: "
3375 "%s\n", nt_errstr(status));
3379 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3380 if (!NT_STATUS_IS_OK(status)) {
3381 torture_warning(torture, "torture_samba3_setconfig failed: "
3382 "%s\n", nt_errstr(status));
3386 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3387 if (!NT_STATUS_IS_OK(status)) {
3388 torture_warning(torture, "get_shareinfo failed: "
3389 "%s\n", nt_errstr(status));
3393 if (strcmp(comment, i->comment) != 0) {
3394 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3395 comment, i->comment);
3399 status = torture_samba3_deleteshare(torture, cli, "blubber");
3400 if (!NT_STATUS_IS_OK(status)) {
3401 torture_warning(torture, "torture_samba3_deleteshare failed: "
3402 "%s\n", nt_errstr(status));