2 Unix SMB/CIFS implementation.
4 dcerpc torture tests, designed to walk Samba3 code paths
6 Copyright (C) Volker Lendecke 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/rap/rap.h"
26 #include "torture/torture.h"
27 #include "torture/util.h"
28 #include "torture/rap/proto.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "librpc/gen_ndr/ndr_lsa_c.h"
31 #include "librpc/gen_ndr/ndr_samr.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "librpc/gen_ndr/ndr_netlogon.h"
34 #include "librpc/gen_ndr/ndr_netlogon_c.h"
35 #include "librpc/gen_ndr/ndr_srvsvc.h"
36 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
37 #include "librpc/gen_ndr/ndr_spoolss.h"
38 #include "librpc/gen_ndr/ndr_spoolss_c.h"
39 #include "librpc/gen_ndr/ndr_winreg.h"
40 #include "librpc/gen_ndr/ndr_winreg_c.h"
41 #include "librpc/gen_ndr/ndr_wkssvc.h"
42 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
43 #include "lib/cmdline/popt_common.h"
44 #include "librpc/rpc/dcerpc.h"
45 #include "torture/rpc/rpc.h"
46 #include "libcli/libcli.h"
47 #include "libcli/composite/composite.h"
48 #include "libcli/smb_composite/smb_composite.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "../lib/crypto/crypto.h"
51 #include "auth/ntlmssp/ntlmssp.h"
52 #include "libcli/security/proto.h"
53 #include "param/param.h"
54 #include "lib/registry/registry.h"
55 #include "libcli/resolve/resolve.h"
58 * This tests a RPC call using an invalid vuid
61 bool torture_bind_authcontext(struct torture_context *torture)
66 struct lsa_ObjectAttribute objectattr;
67 struct lsa_OpenPolicy2 openpolicy;
68 struct policy_handle handle;
69 struct lsa_Close close_handle;
70 struct smbcli_session *tmp;
71 struct smbcli_session *session2;
72 struct smbcli_state *cli;
73 struct dcerpc_pipe *lsa_pipe;
74 struct cli_credentials *anon_creds;
75 struct smb_composite_sesssetup setup;
76 struct smbcli_options options;
77 struct smbcli_session_options session_options;
79 mem_ctx = talloc_init("torture_bind_authcontext");
81 if (mem_ctx == NULL) {
82 d_printf("talloc_init failed\n");
86 lp_smbcli_options(torture->lp_ctx, &options);
87 lp_smbcli_session_options(torture->lp_ctx, &session_options);
89 status = smbcli_full_connection(mem_ctx, &cli,
90 torture_setting_string(torture, "host", NULL),
91 lp_smb_ports(torture->lp_ctx),
92 "IPC$", NULL, cmdline_credentials,
93 lp_resolve_context(torture->lp_ctx),
94 torture->ev, &options, &session_options,
95 lp_iconv_convenience(torture->lp_ctx));
96 if (!NT_STATUS_IS_OK(status)) {
97 d_printf("smbcli_full_connection failed: %s\n",
102 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
103 lp_iconv_convenience(torture->lp_ctx));
104 if (lsa_pipe == NULL) {
105 d_printf("dcerpc_pipe_init failed\n");
109 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
110 if (!NT_STATUS_IS_OK(status)) {
111 d_printf("dcerpc_pipe_open_smb failed: %s\n",
116 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
117 if (!NT_STATUS_IS_OK(status)) {
118 d_printf("dcerpc_bind_auth_none failed: %s\n",
123 openpolicy.in.system_name =talloc_asprintf(
124 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
125 ZERO_STRUCT(objectattr);
126 openpolicy.in.attr = &objectattr;
127 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
128 openpolicy.out.handle = &handle;
130 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
132 if (!NT_STATUS_IS_OK(status)) {
133 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
138 close_handle.in.handle = &handle;
139 close_handle.out.handle = &handle;
141 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
142 if (!NT_STATUS_IS_OK(status)) {
143 d_printf("dcerpc_lsa_Close failed: %s\n",
148 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
149 if (session2 == NULL) {
150 d_printf("smbcli_session_init failed\n");
154 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
155 d_printf("create_anon_creds failed\n");
159 setup.in.sesskey = cli->transport->negotiate.sesskey;
160 setup.in.capabilities = cli->transport->negotiate.capabilities;
161 setup.in.workgroup = "";
162 setup.in.credentials = anon_creds;
163 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
165 status = smb_composite_sesssetup(session2, &setup);
166 if (!NT_STATUS_IS_OK(status)) {
167 d_printf("anon session setup failed: %s\n",
171 session2->vuid = setup.out.vuid;
173 tmp = cli->tree->session;
174 cli->tree->session = session2;
176 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
178 cli->tree->session = tmp;
179 talloc_free(lsa_pipe);
182 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
183 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
184 "expected NT_STATUS_INVALID_HANDLE\n",
191 talloc_free(mem_ctx);
196 * Bind to lsa using a specific auth method
199 static bool bindtest(struct smbcli_state *cli,
200 struct cli_credentials *credentials,
201 struct loadparm_context *lp_ctx,
202 uint8_t auth_type, uint8_t auth_level)
208 struct dcerpc_pipe *lsa_pipe;
209 struct lsa_ObjectAttribute objectattr;
210 struct lsa_OpenPolicy2 openpolicy;
211 struct lsa_QueryInfoPolicy query;
212 union lsa_PolicyInformation *info = NULL;
213 struct policy_handle handle;
214 struct lsa_Close close_handle;
216 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
217 d_printf("talloc_init failed\n");
221 lsa_pipe = dcerpc_pipe_init(mem_ctx,
222 cli->transport->socket->event.ctx,
223 lp_iconv_convenience(lp_ctx));
224 if (lsa_pipe == NULL) {
225 d_printf("dcerpc_pipe_init failed\n");
229 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
230 if (!NT_STATUS_IS_OK(status)) {
231 d_printf("dcerpc_pipe_open_smb failed: %s\n",
236 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
237 credentials, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
239 if (!NT_STATUS_IS_OK(status)) {
240 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
244 openpolicy.in.system_name =talloc_asprintf(
245 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
246 ZERO_STRUCT(objectattr);
247 openpolicy.in.attr = &objectattr;
248 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
249 openpolicy.out.handle = &handle;
251 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
253 if (!NT_STATUS_IS_OK(status)) {
254 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
259 query.in.handle = &handle;
260 query.in.level = LSA_POLICY_INFO_DOMAIN;
261 query.out.info = &info;
263 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
264 if (!NT_STATUS_IS_OK(status)) {
265 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
270 close_handle.in.handle = &handle;
271 close_handle.out.handle = &handle;
273 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
274 if (!NT_STATUS_IS_OK(status)) {
275 d_printf("dcerpc_lsa_Close failed: %s\n",
282 talloc_free(mem_ctx);
287 * test authenticated RPC binds with the variants Samba3 does support
290 bool torture_bind_samba3(struct torture_context *torture)
295 struct smbcli_state *cli;
296 struct smbcli_options options;
297 struct smbcli_session_options session_options;
299 mem_ctx = talloc_init("torture_bind_authcontext");
301 if (mem_ctx == NULL) {
302 d_printf("talloc_init failed\n");
306 lp_smbcli_options(torture->lp_ctx, &options);
307 lp_smbcli_session_options(torture->lp_ctx, &session_options);
309 status = smbcli_full_connection(mem_ctx, &cli,
310 torture_setting_string(torture, "host", NULL),
311 lp_smb_ports(torture->lp_ctx),
312 "IPC$", NULL, cmdline_credentials,
313 lp_resolve_context(torture->lp_ctx),
314 torture->ev, &options, &session_options,
315 lp_iconv_convenience(torture->lp_ctx));
316 if (!NT_STATUS_IS_OK(status)) {
317 d_printf("smbcli_full_connection failed: %s\n",
324 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
325 DCERPC_AUTH_LEVEL_INTEGRITY);
326 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
327 DCERPC_AUTH_LEVEL_PRIVACY);
328 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
329 DCERPC_AUTH_LEVEL_INTEGRITY);
330 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
331 DCERPC_AUTH_LEVEL_PRIVACY);
334 talloc_free(mem_ctx);
339 * Lookup or create a user and return all necessary info
342 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
344 struct loadparm_context *lp_ctx,
345 struct cli_credentials *admin_creds,
348 const char *username,
350 struct dcerpc_pipe **result_pipe,
351 struct policy_handle **result_handle,
352 struct dom_sid **sid)
354 struct dcerpc_pipe *samr_pipe;
356 struct policy_handle conn_handle;
357 struct policy_handle domain_handle;
358 struct policy_handle *user_handle;
359 struct samr_Connect2 conn;
360 struct samr_EnumDomains enumdom;
361 uint32_t resume_handle = 0;
362 struct samr_LookupDomain l;
364 struct lsa_String domain_name;
365 struct lsa_String user_name;
366 struct samr_OpenDomain o;
367 struct samr_CreateUser2 c;
368 uint32_t user_rid,access_granted;
370 samr_pipe = dcerpc_pipe_init(mem_ctx,
371 cli->transport->socket->event.ctx,
372 lp_iconv_convenience(lp_ctx));
373 if (samr_pipe == NULL) {
374 d_printf("dcerpc_pipe_init failed\n");
375 status = NT_STATUS_NO_MEMORY;
379 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
380 if (!NT_STATUS_IS_OK(status)) {
381 d_printf("dcerpc_pipe_open_smb failed: %s\n",
386 if (admin_creds != NULL) {
387 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
388 admin_creds, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
390 if (!NT_STATUS_IS_OK(status)) {
391 d_printf("dcerpc_bind_auth failed: %s\n",
396 /* We must have an authenticated SMB connection */
397 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
398 if (!NT_STATUS_IS_OK(status)) {
399 d_printf("dcerpc_bind_auth_none failed: %s\n",
405 conn.in.system_name = talloc_asprintf(
406 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
407 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
408 conn.out.connect_handle = &conn_handle;
410 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
411 if (!NT_STATUS_IS_OK(status)) {
412 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
416 enumdom.in.connect_handle = &conn_handle;
417 enumdom.in.resume_handle = &resume_handle;
418 enumdom.in.buf_size = (uint32_t)-1;
419 enumdom.out.resume_handle = &resume_handle;
421 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
422 if (!NT_STATUS_IS_OK(status)) {
423 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
427 if (enumdom.out.num_entries != 2) {
428 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
429 enumdom.out.num_entries);
430 status = NT_STATUS_UNSUCCESSFUL;
434 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
437 l.in.connect_handle = &conn_handle;
438 domain_name.string = enumdom.out.sam->entries[dom_idx].name.string;
439 *domain = talloc_strdup(mem_ctx, domain_name.string);
440 l.in.domain_name = &domain_name;
442 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
443 if (!NT_STATUS_IS_OK(status)) {
444 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
448 o.in.connect_handle = &conn_handle;
449 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
450 o.in.sid = l.out.sid;
451 o.out.domain_handle = &domain_handle;
453 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
454 if (!NT_STATUS_IS_OK(status)) {
455 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
459 c.in.domain_handle = &domain_handle;
460 user_name.string = username;
461 c.in.account_name = &user_name;
462 c.in.acct_flags = ACB_NORMAL;
463 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
464 user_handle = talloc(mem_ctx, struct policy_handle);
465 c.out.user_handle = user_handle;
466 c.out.access_granted = &access_granted;
467 c.out.rid = &user_rid;
469 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
471 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
472 struct samr_LookupNames ln;
473 struct samr_OpenUser ou;
475 ln.in.domain_handle = &domain_handle;
477 ln.in.names = &user_name;
479 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
480 if (!NT_STATUS_IS_OK(status)) {
481 d_printf("samr_LookupNames failed: %s\n",
486 ou.in.domain_handle = &domain_handle;
487 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
488 user_rid = ou.in.rid = ln.out.rids.ids[0];
489 ou.out.user_handle = user_handle;
491 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
492 if (!NT_STATUS_IS_OK(status)) {
493 d_printf("samr_OpenUser failed: %s\n",
499 if (!NT_STATUS_IS_OK(status)) {
500 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
504 *result_pipe = samr_pipe;
505 *result_handle = user_handle;
507 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
519 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
520 struct loadparm_context *lp_ctx,
521 struct cli_credentials *admin_creds,
522 const char *username, const char *password,
524 struct dom_sid **user_sid)
528 struct dcerpc_pipe *samr_pipe;
529 struct policy_handle *wks_handle;
532 if (!(tmp_ctx = talloc_new(mem_ctx))) {
533 d_printf("talloc_init failed\n");
537 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
538 DCERPC_AUTH_TYPE_NTLMSSP,
539 DCERPC_AUTH_LEVEL_INTEGRITY,
540 username, domain_name, &samr_pipe, &wks_handle,
542 if (!NT_STATUS_IS_OK(status)) {
543 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
548 struct samr_SetUserInfo2 sui2;
549 struct samr_SetUserInfo sui;
550 struct samr_QueryUserInfo qui;
551 union samr_UserInfo u_info;
552 DATA_BLOB session_key;
556 encode_pw_buffer(u_info.info23.password.data, password,
559 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
560 if (!NT_STATUS_IS_OK(status)) {
561 d_printf("dcerpc_fetch_session_key failed\n");
564 arcfour_crypt_blob(u_info.info23.password.data, 516,
566 u_info.info23.info.password_expired = 0;
567 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
568 SAMR_FIELD_PASSWORD2 |
569 SAMR_FIELD_EXPIRED_FLAG;
570 sui2.in.user_handle = wks_handle;
571 sui2.in.info = &u_info;
574 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
575 if (!NT_STATUS_IS_OK(status)) {
576 d_printf("samr_SetUserInfo(23) failed: %s\n",
581 u_info.info16.acct_flags = ACB_NORMAL;
582 sui.in.user_handle = wks_handle;
583 sui.in.info = &u_info;
586 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
587 if (!NT_STATUS_IS_OK(status)) {
588 d_printf("samr_SetUserInfo(16) failed\n");
592 qui.in.user_handle = wks_handle;
595 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
596 if (!NT_STATUS_IS_OK(status)) {
597 d_printf("samr_QueryUserInfo(21) failed\n");
601 qui.out.info->info21.allow_password_change = 0;
602 qui.out.info->info21.force_password_change = 0;
603 qui.out.info->info21.account_name.string = NULL;
604 qui.out.info->info21.rid = 0;
605 qui.out.info->info21.acct_expiry = 0;
606 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
608 u_info.info21 = qui.out.info->info21;
609 sui.in.user_handle = wks_handle;
610 sui.in.info = &u_info;
613 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
614 if (!NT_STATUS_IS_OK(status)) {
615 d_printf("samr_SetUserInfo(21) failed\n");
620 *domain_name= talloc_steal(mem_ctx, *domain_name);
621 *user_sid = talloc_steal(mem_ctx, *user_sid);
624 talloc_free(tmp_ctx);
632 static bool delete_user(struct smbcli_state *cli,
633 struct loadparm_context *lp_ctx,
634 struct cli_credentials *admin_creds,
635 const char *username)
640 struct dcerpc_pipe *samr_pipe;
641 struct policy_handle *user_handle;
644 if ((mem_ctx = talloc_init("leave")) == NULL) {
645 d_printf("talloc_init failed\n");
649 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
650 DCERPC_AUTH_TYPE_NTLMSSP,
651 DCERPC_AUTH_LEVEL_INTEGRITY,
652 username, &dom_name, &samr_pipe,
655 if (!NT_STATUS_IS_OK(status)) {
656 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
661 struct samr_DeleteUser d;
663 d.in.user_handle = user_handle;
664 d.out.user_handle = user_handle;
666 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
667 if (!NT_STATUS_IS_OK(status)) {
668 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
676 talloc_free(mem_ctx);
681 * Do a Samba3-style join
684 static bool join3(struct smbcli_state *cli,
685 struct loadparm_context *lp_ctx,
687 struct cli_credentials *admin_creds,
688 struct cli_credentials *wks_creds)
693 struct dcerpc_pipe *samr_pipe;
694 struct policy_handle *wks_handle;
696 NTTIME last_password_change;
698 if ((mem_ctx = talloc_init("join3")) == NULL) {
699 d_printf("talloc_init failed\n");
703 status = get_usr_handle(
704 cli, mem_ctx, lp_ctx, admin_creds,
705 DCERPC_AUTH_TYPE_NTLMSSP,
706 DCERPC_AUTH_LEVEL_PRIVACY,
707 talloc_asprintf(mem_ctx, "%s$",
708 cli_credentials_get_workstation(wks_creds)),
709 &dom_name, &samr_pipe, &wks_handle, NULL);
711 if (!NT_STATUS_IS_OK(status)) {
712 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
717 struct samr_QueryUserInfo q;
719 q.in.user_handle = wks_handle;
722 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
723 if (!NT_STATUS_IS_OK(status)) {
724 d_printf("(%s) QueryUserInfo failed: %s\n",
725 __location__, nt_errstr(status));
729 last_password_change = q.out.info->info21.last_password_change;
732 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
735 struct samr_SetUserInfo2 sui2;
736 union samr_UserInfo u_info;
737 struct samr_UserInfo21 *i21 = &u_info.info25.info;
738 DATA_BLOB session_key;
739 DATA_BLOB confounded_session_key = data_blob_talloc(
741 struct MD5Context ctx;
742 uint8_t confounder[16];
746 i21->full_name.string = talloc_asprintf(
748 cli_credentials_get_workstation(wks_creds));
749 i21->acct_flags = ACB_WSTRUST;
750 i21->fields_present = SAMR_FIELD_FULL_NAME |
751 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
753 encode_pw_buffer(u_info.info25.password.data,
754 cli_credentials_get_password(wks_creds),
756 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
757 if (!NT_STATUS_IS_OK(status)) {
758 d_printf("dcerpc_fetch_session_key failed: %s\n",
762 generate_random_buffer((uint8_t *)confounder, 16);
765 MD5Update(&ctx, confounder, 16);
766 MD5Update(&ctx, session_key.data, session_key.length);
767 MD5Final(confounded_session_key.data, &ctx);
769 arcfour_crypt_blob(u_info.info25.password.data, 516,
770 &confounded_session_key);
771 memcpy(&u_info.info25.password.data[516], confounder, 16);
773 sui2.in.user_handle = wks_handle;
775 sui2.in.info = &u_info;
777 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
778 if (!NT_STATUS_IS_OK(status)) {
779 d_printf("samr_SetUserInfo2(25) failed: %s\n",
784 struct samr_SetUserInfo2 sui2;
785 struct samr_SetUserInfo sui;
786 union samr_UserInfo u_info;
787 DATA_BLOB session_key;
789 encode_pw_buffer(u_info.info24.password.data,
790 cli_credentials_get_password(wks_creds),
792 u_info.info24.pw_len =
793 strlen_m(cli_credentials_get_password(wks_creds))*2;
795 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
796 if (!NT_STATUS_IS_OK(status)) {
797 d_printf("dcerpc_fetch_session_key failed\n");
800 arcfour_crypt_blob(u_info.info24.password.data, 516,
802 sui2.in.user_handle = wks_handle;
803 sui2.in.info = &u_info;
806 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
807 if (!NT_STATUS_IS_OK(status)) {
808 d_printf("samr_SetUserInfo(24) failed: %s\n",
813 u_info.info16.acct_flags = ACB_WSTRUST;
814 sui.in.user_handle = wks_handle;
815 sui.in.info = &u_info;
818 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
819 if (!NT_STATUS_IS_OK(status)) {
820 d_printf("samr_SetUserInfo(16) failed\n");
826 struct samr_QueryUserInfo q;
828 q.in.user_handle = wks_handle;
831 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
832 if (!NT_STATUS_IS_OK(status)) {
833 d_printf("(%s) QueryUserInfo failed: %s\n",
834 __location__, nt_errstr(status));
839 if (last_password_change
840 == q.out.info->info21.last_password_change) {
841 d_printf("(%s) last_password_change unchanged "
842 "during join, level25 must change "
843 "it\n", __location__);
848 if (last_password_change
849 != q.out.info->info21.last_password_change) {
850 d_printf("(%s) last_password_change changed "
851 "during join, level24 doesn't "
852 "change it\n", __location__);
861 talloc_free(mem_ctx);
866 * Do a ReqChallenge/Auth2 and get the wks creds
869 static bool auth2(struct smbcli_state *cli,
870 struct loadparm_context *lp_ctx,
871 struct cli_credentials *wks_cred)
874 struct dcerpc_pipe *net_pipe;
877 struct netr_ServerReqChallenge r;
878 struct netr_Credential netr_cli_creds;
879 struct netr_Credential netr_srv_creds;
880 uint32_t negotiate_flags;
881 struct netr_ServerAuthenticate2 a;
882 struct creds_CredentialState *creds_state;
883 struct netr_Credential netr_cred;
884 struct samr_Password mach_pw;
886 mem_ctx = talloc_new(NULL);
887 if (mem_ctx == NULL) {
888 d_printf("talloc_new failed\n");
892 net_pipe = dcerpc_pipe_init(mem_ctx,
893 cli->transport->socket->event.ctx,
894 lp_iconv_convenience(lp_ctx));
895 if (net_pipe == NULL) {
896 d_printf("dcerpc_pipe_init failed\n");
900 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
901 if (!NT_STATUS_IS_OK(status)) {
902 d_printf("dcerpc_pipe_open_smb failed: %s\n",
907 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
908 if (!NT_STATUS_IS_OK(status)) {
909 d_printf("dcerpc_bind_auth_none failed: %s\n",
914 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
915 r.in.server_name = talloc_asprintf(
916 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
917 if (r.in.server_name == NULL) {
918 d_printf("talloc_asprintf failed\n");
921 generate_random_buffer(netr_cli_creds.data,
922 sizeof(netr_cli_creds.data));
923 r.in.credentials = &netr_cli_creds;
924 r.out.return_credentials = &netr_srv_creds;
926 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
927 if (!NT_STATUS_IS_OK(status)) {
928 d_printf("netr_ServerReqChallenge failed: %s\n",
933 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
934 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
936 creds_state = talloc(mem_ctx, struct creds_CredentialState);
937 creds_client_init(creds_state, r.in.credentials,
938 r.out.return_credentials, &mach_pw,
939 &netr_cred, negotiate_flags);
941 a.in.server_name = talloc_asprintf(
942 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
943 a.in.account_name = talloc_asprintf(
944 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
945 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
946 a.in.secure_channel_type = SEC_CHAN_WKSTA;
947 a.in.negotiate_flags = &negotiate_flags;
948 a.out.negotiate_flags = &negotiate_flags;
949 a.in.credentials = &netr_cred;
950 a.out.return_credentials = &netr_cred;
952 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
953 if (!NT_STATUS_IS_OK(status)) {
954 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
959 if (!creds_client_check(creds_state, a.out.return_credentials)) {
960 d_printf("creds_client_check failed\n");
964 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
969 talloc_free(mem_ctx);
974 * Do a couple of schannel protected Netlogon ops: Interactive and Network
975 * login, and change the wks password
978 static bool schan(struct smbcli_state *cli,
979 struct loadparm_context *lp_ctx,
980 struct cli_credentials *wks_creds,
981 struct cli_credentials *user_creds)
986 struct dcerpc_pipe *net_pipe;
989 mem_ctx = talloc_new(NULL);
990 if (mem_ctx == NULL) {
991 d_printf("talloc_new failed\n");
995 net_pipe = dcerpc_pipe_init(mem_ctx,
996 cli->transport->socket->event.ctx,
997 lp_iconv_convenience(lp_ctx));
998 if (net_pipe == NULL) {
999 d_printf("dcerpc_pipe_init failed\n");
1003 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1004 if (!NT_STATUS_IS_OK(status)) {
1005 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1011 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1012 DCERPC_DEBUG_PRINT_OUT;
1015 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1016 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1017 wks_creds, lp_gensec_settings(lp_ctx, lp_ctx), DCERPC_AUTH_TYPE_SCHANNEL,
1018 DCERPC_AUTH_LEVEL_PRIVACY,
1021 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1023 if (!NT_STATUS_IS_OK(status)) {
1024 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1029 for (i=2; i<4; i++) {
1031 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1032 struct creds_CredentialState *creds_state;
1033 struct netr_Authenticator netr_auth, netr_auth2;
1034 struct netr_NetworkInfo ninfo;
1035 struct netr_PasswordInfo pinfo;
1036 struct netr_LogonSamLogon r;
1037 union netr_LogonLevel logon;
1038 union netr_Validation validation;
1039 uint8_t authoritative;
1040 struct netr_Authenticator return_authenticator;
1042 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1043 CLI_CRED_NTLMv2_AUTH;
1045 chal = data_blob_talloc(mem_ctx, NULL, 8);
1046 if (chal.data == NULL) {
1047 d_printf("data_blob_talloc failed\n");
1051 generate_random_buffer(chal.data, chal.length);
1052 names_blob = NTLMv2_generate_names_blob(
1054 cli_credentials_get_workstation(user_creds),
1055 cli_credentials_get_domain(user_creds));
1056 status = cli_credentials_get_ntlm_response(
1057 user_creds, mem_ctx, &flags, chal, names_blob,
1058 &lm_resp, &nt_resp, NULL, NULL);
1059 if (!NT_STATUS_IS_OK(status)) {
1060 d_printf("cli_credentials_get_ntlm_response failed:"
1061 " %s\n", nt_errstr(status));
1065 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1066 creds_client_authenticator(creds_state, &netr_auth);
1068 ninfo.identity_info.account_name.string =
1069 cli_credentials_get_username(user_creds);
1070 ninfo.identity_info.domain_name.string =
1071 cli_credentials_get_domain(user_creds);
1072 ninfo.identity_info.parameter_control = 0;
1073 ninfo.identity_info.logon_id_low = 0;
1074 ninfo.identity_info.logon_id_high = 0;
1075 ninfo.identity_info.workstation.string =
1076 cli_credentials_get_workstation(user_creds);
1077 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1078 ninfo.nt.length = nt_resp.length;
1079 ninfo.nt.data = nt_resp.data;
1080 ninfo.lm.length = lm_resp.length;
1081 ninfo.lm.data = lm_resp.data;
1083 logon.network = &ninfo;
1085 r.in.server_name = talloc_asprintf(
1086 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1087 ZERO_STRUCT(netr_auth2);
1088 r.in.computer_name =
1089 cli_credentials_get_workstation(wks_creds);
1090 r.in.credential = &netr_auth;
1091 r.in.return_authenticator = &netr_auth2;
1092 r.in.logon_level = 2;
1093 r.in.validation_level = i;
1094 r.in.logon = &logon;
1095 r.out.validation = &validation;
1096 r.out.authoritative = &authoritative;
1097 r.out.return_authenticator = &return_authenticator;
1099 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1100 if (!NT_STATUS_IS_OK(status)) {
1101 d_printf("netr_LogonSamLogon failed: %s\n",
1106 if ((r.out.return_authenticator == NULL) ||
1107 (!creds_client_check(creds_state,
1108 &r.out.return_authenticator->cred))) {
1109 d_printf("Credentials check failed!\n");
1113 creds_client_authenticator(creds_state, &netr_auth);
1115 pinfo.identity_info = ninfo.identity_info;
1116 ZERO_STRUCT(pinfo.lmpassword.hash);
1117 E_md4hash(cli_credentials_get_password(user_creds),
1118 pinfo.ntpassword.hash);
1119 session_key = data_blob_talloc(mem_ctx,
1120 creds_state->session_key, 16);
1121 arcfour_crypt_blob(pinfo.ntpassword.hash,
1122 sizeof(pinfo.ntpassword.hash),
1125 logon.password = &pinfo;
1127 r.in.logon_level = 1;
1128 r.in.logon = &logon;
1129 r.out.return_authenticator = &return_authenticator;
1131 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1132 if (!NT_STATUS_IS_OK(status)) {
1133 d_printf("netr_LogonSamLogon failed: %s\n",
1138 if ((r.out.return_authenticator == NULL) ||
1139 (!creds_client_check(creds_state,
1140 &r.out.return_authenticator->cred))) {
1141 d_printf("Credentials check failed!\n");
1147 struct netr_ServerPasswordSet s;
1148 char *password = generate_random_str(wks_creds, 8);
1149 struct creds_CredentialState *creds_state;
1150 struct netr_Authenticator credential, return_authenticator;
1151 struct samr_Password new_password;
1153 s.in.server_name = talloc_asprintf(
1154 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1155 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1156 s.in.account_name = talloc_asprintf(
1157 mem_ctx, "%s$", s.in.computer_name);
1158 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1159 s.in.credential = &credential;
1160 s.in.new_password = &new_password;
1161 s.out.return_authenticator = &return_authenticator;
1163 E_md4hash(password, new_password.hash);
1165 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1166 creds_des_encrypt(creds_state, &new_password);
1167 creds_client_authenticator(creds_state, &credential);
1169 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1170 if (!NT_STATUS_IS_OK(status)) {
1171 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1175 if (!creds_client_check(creds_state,
1176 &s.out.return_authenticator->cred)) {
1177 printf("Credential chaining failed\n");
1180 cli_credentials_set_password(wks_creds, password,
1186 talloc_free(mem_ctx);
1191 * Delete the wks account again
1194 static bool leave(struct smbcli_state *cli,
1195 struct loadparm_context *lp_ctx,
1196 struct cli_credentials *admin_creds,
1197 struct cli_credentials *wks_creds)
1199 char *wks_name = talloc_asprintf(
1200 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1203 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1204 talloc_free(wks_name);
1209 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1212 bool torture_netlogon_samba3(struct torture_context *torture)
1214 TALLOC_CTX *mem_ctx;
1217 struct smbcli_state *cli;
1218 struct cli_credentials *anon_creds;
1219 struct cli_credentials *wks_creds;
1220 const char *wks_name;
1222 struct smbcli_options options;
1223 struct smbcli_session_options session_options;
1225 wks_name = torture_setting_string(torture, "wksname", NULL);
1226 if (wks_name == NULL) {
1227 wks_name = get_myname();
1230 mem_ctx = talloc_init("torture_netlogon_samba3");
1232 if (mem_ctx == NULL) {
1233 d_printf("talloc_init failed\n");
1237 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1238 d_printf("create_anon_creds failed\n");
1242 lp_smbcli_options(torture->lp_ctx, &options);
1243 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1245 status = smbcli_full_connection(mem_ctx, &cli,
1246 torture_setting_string(torture, "host", NULL),
1247 lp_smb_ports(torture->lp_ctx),
1248 "IPC$", NULL, anon_creds,
1249 lp_resolve_context(torture->lp_ctx),
1250 torture->ev, &options, &session_options,
1251 lp_iconv_convenience(torture->lp_ctx));
1252 if (!NT_STATUS_IS_OK(status)) {
1253 d_printf("smbcli_full_connection failed: %s\n",
1258 wks_creds = cli_credentials_init(mem_ctx);
1259 if (wks_creds == NULL) {
1260 d_printf("cli_credentials_init failed\n");
1264 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1265 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1266 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1267 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1268 cli_credentials_set_password(wks_creds,
1269 generate_random_str(wks_creds, 8),
1272 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1273 d_printf("join failed\n");
1277 cli_credentials_set_domain(
1278 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1281 for (i=0; i<2; i++) {
1283 /* Do this more than once, the routine "schan" changes
1284 * the workstation password using the netlogon
1285 * password change routine */
1289 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1290 d_printf("auth2 failed\n");
1294 for (j=0; j<2; j++) {
1295 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1296 d_printf("schan failed\n");
1302 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1303 d_printf("leave failed\n");
1310 talloc_free(mem_ctx);
1315 * Do a simple join, testjoin and leave using specified smb and samr
1319 static bool test_join3(struct torture_context *tctx,
1321 struct cli_credentials *smb_creds,
1322 struct cli_credentials *samr_creds,
1323 const char *wks_name)
1327 struct smbcli_state *cli;
1328 struct cli_credentials *wks_creds;
1329 struct smbcli_options options;
1330 struct smbcli_session_options session_options;
1332 lp_smbcli_options(tctx->lp_ctx, &options);
1333 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1335 status = smbcli_full_connection(tctx, &cli,
1336 torture_setting_string(tctx, "host", NULL),
1337 lp_smb_ports(tctx->lp_ctx),
1338 "IPC$", NULL, smb_creds,
1339 lp_resolve_context(tctx->lp_ctx),
1340 tctx->ev, &options, &session_options,
1341 lp_iconv_convenience(tctx->lp_ctx));
1342 if (!NT_STATUS_IS_OK(status)) {
1343 d_printf("smbcli_full_connection failed: %s\n",
1348 wks_creds = cli_credentials_init(cli);
1349 if (wks_creds == NULL) {
1350 d_printf("cli_credentials_init failed\n");
1354 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1355 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1356 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1357 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1358 cli_credentials_set_password(wks_creds,
1359 generate_random_str(wks_creds, 8),
1362 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1363 d_printf("join failed\n");
1367 cli_credentials_set_domain(
1368 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1371 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1372 d_printf("auth2 failed\n");
1376 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1377 d_printf("leave failed\n");
1390 * Test the different session key variants. Do it by joining, this uses the
1391 * session key in the setpassword routine. Test the join by doing the auth2.
1394 bool torture_samba3_sessionkey(struct torture_context *torture)
1397 struct cli_credentials *anon_creds;
1398 const char *wks_name;
1400 wks_name = torture_setting_string(torture, "wksname", get_myname());
1402 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1403 d_printf("create_anon_creds failed\n");
1407 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1411 if (!torture_setting_bool(torture, "samba3", false)) {
1413 /* Samba3 in the build farm right now does this happily. Need
1416 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1417 d_printf("join using anonymous bind on an anonymous smb "
1418 "connection succeeded -- HUH??\n");
1423 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1425 d_printf("join using ntlmssp bind on an anonymous smb "
1426 "connection failed\n");
1430 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1431 d_printf("join using anonymous bind on an authenticated smb "
1432 "connection failed\n");
1436 if (!test_join3(torture, false, cmdline_credentials,
1437 cmdline_credentials,
1439 d_printf("join using ntlmssp bind on an authenticated smb "
1440 "connection failed\n");
1445 * The following two are tests for setuserinfolevel 25
1448 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1450 d_printf("join using ntlmssp bind on an anonymous smb "
1451 "connection failed\n");
1455 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1456 d_printf("join using anonymous bind on an authenticated smb "
1457 "connection failed\n");
1467 * open pipe and bind, given an IPC$ context
1470 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1471 struct loadparm_context *lp_ctx,
1472 struct smbcli_tree *tree,
1473 const char *pipe_name,
1474 const struct ndr_interface_table *iface,
1475 struct dcerpc_pipe **p)
1477 struct dcerpc_pipe *result;
1480 if (!(result = dcerpc_pipe_init(
1481 mem_ctx, tree->session->transport->socket->event.ctx,
1482 lp_iconv_convenience(lp_ctx)))) {
1483 return NT_STATUS_NO_MEMORY;
1486 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1487 if (!NT_STATUS_IS_OK(status)) {
1488 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1490 talloc_free(result);
1494 status = dcerpc_bind_auth_none(result, iface);
1495 if (!NT_STATUS_IS_OK(status)) {
1496 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1497 talloc_free(result);
1502 return NT_STATUS_OK;
1506 * Sane wrapper around lsa_LookupNames
1509 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1510 struct dcerpc_pipe *p,
1514 struct lsa_ObjectAttribute attr;
1515 struct lsa_QosInfo qos;
1516 struct lsa_OpenPolicy2 r;
1519 struct policy_handle handle;
1520 struct lsa_LookupNames l;
1521 struct lsa_TransSidArray sids;
1522 struct lsa_RefDomainList *domains = NULL;
1523 struct lsa_String lsa_name;
1525 struct dom_sid *result;
1526 TALLOC_CTX *tmp_ctx;
1528 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1533 qos.impersonation_level = 2;
1534 qos.context_mode = 1;
1535 qos.effective_only = 0;
1538 attr.root_dir = NULL;
1539 attr.object_name = NULL;
1540 attr.attributes = 0;
1541 attr.sec_desc = NULL;
1542 attr.sec_qos = &qos;
1544 r.in.system_name = "\\";
1546 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1547 r.out.handle = &handle;
1549 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1550 if (!NT_STATUS_IS_OK(status)) {
1551 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1552 talloc_free(tmp_ctx);
1559 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1561 l.in.handle = &handle;
1563 l.in.names = &lsa_name;
1566 l.in.count = &count;
1567 l.out.count = &count;
1569 l.out.domains = &domains;
1571 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1572 if (!NT_STATUS_IS_OK(status)) {
1573 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1575 talloc_free(tmp_ctx);
1579 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1580 l.out.sids->sids[0].rid);
1582 c.in.handle = &handle;
1583 c.out.handle = &handle;
1585 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1586 if (!NT_STATUS_IS_OK(status)) {
1587 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1588 talloc_free(tmp_ctx);
1592 talloc_free(tmp_ctx);
1597 * Find out the user SID on this connection
1600 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1601 struct loadparm_context *lp_ctx,
1602 struct smbcli_tree *tree)
1604 struct dcerpc_pipe *lsa;
1605 struct lsa_GetUserName r;
1607 struct lsa_String *authority_name_p = NULL;
1608 struct lsa_String *account_name_p = NULL;
1609 struct dom_sid *result;
1611 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1612 &ndr_table_lsarpc, &lsa);
1613 if (!NT_STATUS_IS_OK(status)) {
1614 d_printf("(%s) Could not bind to LSA: %s\n",
1615 __location__, nt_errstr(status));
1619 r.in.system_name = "\\";
1620 r.in.account_name = &account_name_p;
1621 r.in.authority_name = &authority_name_p;
1622 r.out.account_name = &account_name_p;
1624 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1626 authority_name_p = *r.out.authority_name;
1628 if (!NT_STATUS_IS_OK(status)) {
1629 printf("(%s) GetUserName failed - %s\n",
1630 __location__, nt_errstr(status));
1635 result = name2sid(mem_ctx, lsa, account_name_p->string,
1636 authority_name_p->string);
1642 static int destroy_tree(struct smbcli_tree *tree)
1644 smb_tree_disconnect(tree);
1649 * Do a tcon, given a session
1652 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1653 struct smbcli_session *session,
1654 const char *sharename,
1655 struct smbcli_tree **res)
1657 struct smbcli_tree *result;
1658 TALLOC_CTX *tmp_ctx;
1659 union smb_tcon tcon;
1662 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1663 return NT_STATUS_NO_MEMORY;
1666 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1667 talloc_free(tmp_ctx);
1668 return NT_STATUS_NO_MEMORY;
1671 tcon.generic.level = RAW_TCON_TCONX;
1672 tcon.tconx.in.flags = 0;
1673 tcon.tconx.in.password = data_blob(NULL, 0);
1674 tcon.tconx.in.path = sharename;
1675 tcon.tconx.in.device = "?????";
1677 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1678 if (!NT_STATUS_IS_OK(status)) {
1679 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1681 talloc_free(tmp_ctx);
1685 result->tid = tcon.tconx.out.tid;
1686 result = talloc_steal(mem_ctx, result);
1687 talloc_set_destructor(result, destroy_tree);
1688 talloc_free(tmp_ctx);
1690 return NT_STATUS_OK;
1694 * Test the getusername behaviour
1697 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1700 struct smbcli_state *cli;
1701 TALLOC_CTX *mem_ctx;
1703 struct dom_sid *user_sid;
1704 struct dom_sid *created_sid;
1705 struct cli_credentials *anon_creds;
1706 struct cli_credentials *user_creds;
1708 struct smbcli_options options;
1709 struct smbcli_session_options session_options;
1711 if (!(mem_ctx = talloc_new(torture))) {
1715 lp_smbcli_options(torture->lp_ctx, &options);
1716 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1718 status = smbcli_full_connection(
1719 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1720 lp_smb_ports(torture->lp_ctx),
1721 "IPC$", NULL, cmdline_credentials,
1722 lp_resolve_context(torture->lp_ctx),
1723 torture->ev, &options, &session_options,
1724 lp_iconv_convenience(torture->lp_ctx));
1725 if (!NT_STATUS_IS_OK(status)) {
1726 d_printf("(%s) smbcli_full_connection failed: %s\n",
1727 __location__, nt_errstr(status));
1732 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1733 d_printf("(%s) whoami on auth'ed connection failed\n",
1740 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1741 d_printf("(%s) create_anon_creds failed\n", __location__);
1746 status = smbcli_full_connection(
1747 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1748 lp_smb_ports(torture->lp_ctx),
1749 "IPC$", NULL, anon_creds,
1750 lp_resolve_context(torture->lp_ctx),
1751 torture->ev, &options, &session_options,
1752 lp_iconv_convenience(torture->lp_ctx));
1753 if (!NT_STATUS_IS_OK(status)) {
1754 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1755 __location__, nt_errstr(status));
1760 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1761 d_printf("(%s) whoami on anon connection failed\n",
1767 if (!dom_sid_equal(user_sid,
1768 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1769 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1770 "S-1-5-7", __location__,
1771 dom_sid_string(mem_ctx, user_sid));
1775 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1776 d_printf("(%s) cli_credentials_init failed\n", __location__);
1781 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1782 cli_credentials_set_username(user_creds, "torture_username",
1784 cli_credentials_set_password(user_creds,
1785 generate_random_str(user_creds, 8),
1788 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1789 cli_credentials_get_username(user_creds),
1790 cli_credentials_get_password(user_creds),
1791 &domain_name, &created_sid)) {
1792 d_printf("(%s) create_user failed\n", __location__);
1797 cli_credentials_set_domain(user_creds, domain_name,
1801 struct smbcli_session *session2;
1802 struct smb_composite_sesssetup setup;
1803 struct smbcli_tree *tree;
1805 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1806 if (session2 == NULL) {
1807 d_printf("(%s) smbcli_session_init failed\n",
1812 setup.in.sesskey = cli->transport->negotiate.sesskey;
1813 setup.in.capabilities = cli->transport->negotiate.capabilities;
1814 setup.in.workgroup = "";
1815 setup.in.credentials = user_creds;
1816 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
1818 status = smb_composite_sesssetup(session2, &setup);
1819 if (!NT_STATUS_IS_OK(status)) {
1820 d_printf("(%s) session setup with new user failed: "
1821 "%s\n", __location__, nt_errstr(status));
1825 session2->vuid = setup.out.vuid;
1827 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1829 d_printf("(%s) secondary_tcon failed\n",
1835 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1836 d_printf("(%s) whoami on user connection failed\n",
1845 d_printf("Created %s, found %s\n",
1846 dom_sid_string(mem_ctx, created_sid),
1847 dom_sid_string(mem_ctx, user_sid));
1849 if (!dom_sid_equal(created_sid, user_sid)) {
1854 if (!delete_user(cli, torture->lp_ctx,
1855 cmdline_credentials,
1856 cli_credentials_get_username(user_creds))) {
1857 d_printf("(%s) delete_user failed\n", __location__);
1862 talloc_free(mem_ctx);
1866 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1867 const char *sharename)
1870 struct srvsvc_NetShareGetInfo r;
1871 union srvsvc_NetShareInfo info;
1872 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1876 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1877 dcerpc_server_name(p));
1878 r.in.share_name = sharename;
1881 for (i=0;i<ARRAY_SIZE(levels);i++) {
1882 r.in.level = levels[i];
1884 printf("testing NetShareGetInfo level %u on share '%s'\n",
1885 r.in.level, r.in.share_name);
1887 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1888 if (!NT_STATUS_IS_OK(status)) {
1889 printf("NetShareGetInfo level %u on share '%s' failed"
1890 " - %s\n", r.in.level, r.in.share_name,
1895 if (!W_ERROR_IS_OK(r.out.result)) {
1896 printf("NetShareGetInfo level %u on share '%s' failed "
1897 "- %s\n", r.in.level, r.in.share_name,
1898 win_errstr(r.out.result));
1907 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1908 const char **one_sharename)
1911 struct srvsvc_NetShareEnum r;
1912 struct srvsvc_NetShareInfoCtr info_ctr;
1913 struct srvsvc_NetShareCtr0 c0;
1914 struct srvsvc_NetShareCtr1 c1;
1915 struct srvsvc_NetShareCtr2 c2;
1916 struct srvsvc_NetShareCtr501 c501;
1917 struct srvsvc_NetShareCtr502 c502;
1918 struct srvsvc_NetShareCtr1004 c1004;
1919 struct srvsvc_NetShareCtr1005 c1005;
1920 struct srvsvc_NetShareCtr1006 c1006;
1921 struct srvsvc_NetShareCtr1007 c1007;
1922 uint32_t totalentries = 0;
1923 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1927 ZERO_STRUCT(info_ctr);
1929 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1930 r.in.info_ctr = &info_ctr;
1931 r.in.max_buffer = (uint32_t)-1;
1932 r.in.resume_handle = NULL;
1933 r.out.totalentries = &totalentries;
1934 r.out.info_ctr = &info_ctr;
1936 for (i=0;i<ARRAY_SIZE(levels);i++) {
1937 info_ctr.level = levels[i];
1939 switch (info_ctr.level) {
1942 info_ctr.ctr.ctr0 = &c0;
1946 info_ctr.ctr.ctr1 = &c1;
1950 info_ctr.ctr.ctr2 = &c2;
1954 info_ctr.ctr.ctr501 = &c501;
1958 info_ctr.ctr.ctr502 = &c502;
1962 info_ctr.ctr.ctr1004 = &c1004;
1966 info_ctr.ctr.ctr1005 = &c1005;
1970 info_ctr.ctr.ctr1006 = &c1006;
1974 info_ctr.ctr.ctr1007 = &c1007;
1978 printf("testing NetShareEnum level %u\n", info_ctr.level);
1979 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1980 if (!NT_STATUS_IS_OK(status)) {
1981 printf("NetShareEnum level %u failed - %s\n",
1982 info_ctr.level, nt_errstr(status));
1986 if (!W_ERROR_IS_OK(r.out.result)) {
1987 printf("NetShareEnum level %u failed - %s\n",
1988 info_ctr.level, win_errstr(r.out.result));
1991 if (info_ctr.level == 0) {
1992 struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
1993 if (ctr->count > 0) {
1994 *one_sharename = ctr->array[0].name;
2002 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
2004 struct dcerpc_pipe *p;
2005 TALLOC_CTX *mem_ctx;
2007 const char *sharename = NULL;
2008 struct smbcli_state *cli;
2011 if (!(mem_ctx = talloc_new(torture))) {
2015 if (!(torture_open_connection_share(
2016 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2017 "IPC$", torture->ev))) {
2018 talloc_free(mem_ctx);
2022 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
2023 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
2024 if (!NT_STATUS_IS_OK(status)) {
2025 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2026 __location__, nt_errstr(status));
2031 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
2032 if (sharename == NULL) {
2033 printf("did not get sharename\n");
2035 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
2039 talloc_free(mem_ctx);
2044 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
2045 * NT_STATUS_NO_SAM_ACCOUNT
2048 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
2050 TALLOC_CTX *mem_ctx;
2051 struct dcerpc_pipe *net_pipe;
2053 bool result = false;
2055 struct netr_ServerReqChallenge r;
2056 struct netr_Credential netr_cli_creds;
2057 struct netr_Credential netr_srv_creds;
2058 uint32_t negotiate_flags;
2059 struct netr_ServerAuthenticate2 a;
2060 struct creds_CredentialState *creds_state;
2061 struct netr_Credential netr_cred;
2062 struct samr_Password mach_pw;
2063 struct smbcli_state *cli;
2065 if (!(mem_ctx = talloc_new(torture))) {
2066 d_printf("talloc_new failed\n");
2070 if (!(wksname = generate_random_str_list(
2071 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2072 d_printf("generate_random_str_list failed\n");
2076 if (!(torture_open_connection_share(
2078 torture, torture_setting_string(torture, "host", NULL),
2079 "IPC$", torture->ev))) {
2080 d_printf("IPC$ connection failed\n");
2084 if (!(net_pipe = dcerpc_pipe_init(
2085 mem_ctx, cli->transport->socket->event.ctx,
2086 lp_iconv_convenience(torture->lp_ctx)))) {
2087 d_printf("dcerpc_pipe_init failed\n");
2091 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2092 if (!NT_STATUS_IS_OK(status)) {
2093 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2098 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2099 if (!NT_STATUS_IS_OK(status)) {
2100 d_printf("dcerpc_bind_auth_none failed: %s\n",
2105 r.in.computer_name = wksname;
2106 r.in.server_name = talloc_asprintf(
2107 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2108 if (r.in.server_name == NULL) {
2109 d_printf("talloc_asprintf failed\n");
2112 generate_random_buffer(netr_cli_creds.data,
2113 sizeof(netr_cli_creds.data));
2114 r.in.credentials = &netr_cli_creds;
2115 r.out.return_credentials = &netr_srv_creds;
2117 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2118 if (!NT_STATUS_IS_OK(status)) {
2119 d_printf("netr_ServerReqChallenge failed: %s\n",
2124 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2125 E_md4hash("foobar", mach_pw.hash);
2127 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2128 creds_client_init(creds_state, r.in.credentials,
2129 r.out.return_credentials, &mach_pw,
2130 &netr_cred, negotiate_flags);
2132 a.in.server_name = talloc_asprintf(
2133 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2134 a.in.account_name = talloc_asprintf(
2135 mem_ctx, "%s$", wksname);
2136 a.in.computer_name = wksname;
2137 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2138 a.in.negotiate_flags = &negotiate_flags;
2139 a.out.negotiate_flags = &negotiate_flags;
2140 a.in.credentials = &netr_cred;
2141 a.out.return_credentials = &netr_cred;
2143 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2145 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2146 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2147 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2154 talloc_free(mem_ctx);
2158 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2159 struct loadparm_context *lp_ctx,
2160 struct smbcli_session *sess,
2161 const char *sharename)
2163 struct smbcli_tree *tree;
2164 TALLOC_CTX *tmp_ctx;
2165 struct dcerpc_pipe *p;
2167 struct srvsvc_NetShareGetInfo r;
2168 union srvsvc_NetShareInfo info;
2169 struct security_descriptor *result;
2171 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2172 d_printf("talloc_new failed\n");
2176 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2177 d_printf("secondary_tcon failed\n");
2178 talloc_free(tmp_ctx);
2182 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2183 &ndr_table_srvsvc, &p);
2184 if (!NT_STATUS_IS_OK(status)) {
2185 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2186 __location__, nt_errstr(status));
2187 talloc_free(tmp_ctx);
2192 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2195 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2196 dcerpc_server_name(p));
2197 r.in.share_name = sharename;
2201 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2202 if (!NT_STATUS_IS_OK(status)) {
2203 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2205 talloc_free(tmp_ctx);
2209 result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
2210 talloc_free(tmp_ctx);
2214 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2215 struct loadparm_context *lp_ctx,
2216 struct smbcli_session *sess,
2217 const char *sharename,
2218 struct security_descriptor *sd)
2220 struct smbcli_tree *tree;
2221 TALLOC_CTX *tmp_ctx;
2222 struct dcerpc_pipe *p;
2224 struct sec_desc_buf i;
2225 struct srvsvc_NetShareSetInfo r;
2226 union srvsvc_NetShareInfo info;
2229 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2230 d_printf("talloc_new failed\n");
2231 return NT_STATUS_NO_MEMORY;
2234 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2235 d_printf("secondary_tcon failed\n");
2236 talloc_free(tmp_ctx);
2237 return NT_STATUS_UNSUCCESSFUL;
2240 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2241 &ndr_table_srvsvc, &p);
2242 if (!NT_STATUS_IS_OK(status)) {
2243 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2244 __location__, nt_errstr(status));
2245 talloc_free(tmp_ctx);
2246 return NT_STATUS_UNSUCCESSFUL;
2250 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2253 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2254 dcerpc_server_name(p));
2255 r.in.share_name = sharename;
2260 r.in.parm_error = &error;
2262 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2263 if (!NT_STATUS_IS_OK(status)) {
2264 d_printf("srvsvc_NetShareSetInfo failed: %s\n",
2268 talloc_free(tmp_ctx);
2272 bool try_tcon(TALLOC_CTX *mem_ctx,
2273 struct loadparm_context *lp_ctx,
2274 struct security_descriptor *orig_sd,
2275 struct smbcli_session *session,
2276 const char *sharename, const struct dom_sid *user_sid,
2277 unsigned int access_mask, NTSTATUS expected_tcon,
2278 NTSTATUS expected_mkdir)
2280 TALLOC_CTX *tmp_ctx;
2281 struct smbcli_tree *rmdir_tree, *tree;
2282 struct dom_sid *domain_sid;
2284 struct security_descriptor *sd;
2288 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2289 d_printf("talloc_new failed\n");
2293 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2294 if (!NT_STATUS_IS_OK(status)) {
2295 d_printf("first tcon to delete dir failed\n");
2296 talloc_free(tmp_ctx);
2300 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2302 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2303 &domain_sid, &rid))) {
2304 d_printf("dom_sid_split_rid failed\n");
2305 talloc_free(tmp_ctx);
2309 sd = security_descriptor_dacl_create(
2310 tmp_ctx, 0, "S-1-5-32-544",
2311 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2313 dom_sid_string(mem_ctx, user_sid),
2314 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2316 d_printf("security_descriptor_dacl_create failed\n");
2317 talloc_free(tmp_ctx);
2321 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2322 if (!NT_STATUS_IS_OK(status)) {
2323 d_printf("custom set_sharesec failed: %s\n",
2325 talloc_free(tmp_ctx);
2329 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2330 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2331 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2337 if (!NT_STATUS_IS_OK(status)) {
2338 /* An expected non-access, no point in trying to write */
2342 status = smbcli_mkdir(tree, "sharesec_testdir");
2343 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2344 d_printf("(%s) Expected %s, got %s\n", __location__,
2345 nt_errstr(expected_mkdir), nt_errstr(status));
2350 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2352 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2353 if (!NT_STATUS_IS_OK(status)) {
2354 d_printf("custom set_sharesec failed: %s\n",
2356 talloc_free(tmp_ctx);
2360 talloc_free(tmp_ctx);
2364 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2366 TALLOC_CTX *mem_ctx;
2368 struct smbcli_state *cli;
2369 struct security_descriptor *sd;
2370 struct dom_sid *user_sid;
2372 if (!(mem_ctx = talloc_new(torture))) {
2376 if (!(torture_open_connection_share(
2377 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2378 "IPC$", torture->ev))) {
2379 d_printf("IPC$ connection failed\n");
2380 talloc_free(mem_ctx);
2384 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2385 d_printf("whoami failed\n");
2386 talloc_free(mem_ctx);
2390 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2391 torture_setting_string(torture, "share", NULL));
2393 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2394 torture_setting_string(torture, "share", NULL),
2395 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2397 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2398 torture_setting_string(torture, "share", NULL),
2399 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2400 NT_STATUS_MEDIA_WRITE_PROTECTED);
2402 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2403 torture_setting_string(torture, "share", NULL),
2404 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2406 talloc_free(mem_ctx);
2410 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2412 TALLOC_CTX *mem_ctx;
2414 struct smbcli_state *cli;
2415 struct dcerpc_pipe *p;
2416 struct policy_handle lsa_handle;
2418 struct dom_sid *domain_sid;
2420 if (!(mem_ctx = talloc_new(torture))) {
2424 if (!(torture_open_connection_share(
2425 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2426 "IPC$", torture->ev))) {
2427 d_printf("IPC$ connection failed\n");
2428 talloc_free(mem_ctx);
2432 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2433 &ndr_table_lsarpc, &p);
2434 if (!NT_STATUS_IS_OK(status)) {
2435 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2437 talloc_free(mem_ctx);
2442 struct lsa_ObjectAttribute attr;
2443 struct lsa_OpenPolicy2 o;
2444 o.in.system_name = talloc_asprintf(
2445 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2448 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2449 o.out.handle = &lsa_handle;
2450 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2451 if (!NT_STATUS_IS_OK(status)) {
2452 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2453 __location__, nt_errstr(status));
2454 talloc_free(mem_ctx);
2460 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2465 int levels[] = { 2,3,5,6 };
2467 for (i=0; i<ARRAY_SIZE(levels); i++) {
2468 struct lsa_QueryInfoPolicy r;
2469 union lsa_PolicyInformation *info = NULL;
2470 r.in.handle = &lsa_handle;
2471 r.in.level = levels[i];
2473 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2474 if (!NT_STATUS_IS_OK(status)) {
2475 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2476 "failed: %s\n", __location__,
2477 levels[i], nt_errstr(status));
2478 talloc_free(mem_ctx);
2481 if (levels[i] == 5) {
2482 domain_sid = info->account_domain.sid;
2490 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2491 struct smb_iconv_convenience *iconv_convenience,
2494 struct rap_WserverGetInfo r;
2496 char servername[17];
2499 r.in.bufsize = 0xffff;
2501 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2502 if (!NT_STATUS_IS_OK(status)) {
2506 memcpy(servername, r.out.info.info0.name, 16);
2507 servername[16] = '\0';
2509 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2510 return NT_STATUS_NO_MEMORY;
2513 return NT_STATUS_OK;
2517 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2518 struct smbcli_tree *tree,
2519 const char ***printers, int *num_printers)
2521 TALLOC_CTX *mem_ctx;
2523 struct dcerpc_pipe *p;
2524 struct srvsvc_NetShareEnum r;
2525 struct srvsvc_NetShareInfoCtr info_ctr;
2526 struct srvsvc_NetShareCtr1 c1_in;
2527 struct srvsvc_NetShareCtr1 *c1;
2528 uint32_t totalentries = 0;
2531 mem_ctx = talloc_new(ctx);
2532 if (mem_ctx == NULL) {
2533 return NT_STATUS_NO_MEMORY;
2536 status = pipe_bind_smb(mem_ctx, lp_ctx,
2537 tree, "\\srvsvc", &ndr_table_srvsvc,
2539 if (!NT_STATUS_IS_OK(status)) {
2540 d_printf("could not bind to srvsvc pipe\n");
2541 talloc_free(mem_ctx);
2547 info_ctr.ctr.ctr1 = &c1_in;
2549 r.in.server_unc = talloc_asprintf(
2550 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2551 r.in.info_ctr = &info_ctr;
2552 r.in.max_buffer = (uint32_t)-1;
2553 r.in.resume_handle = NULL;
2554 r.out.totalentries = &totalentries;
2555 r.out.info_ctr = &info_ctr;
2557 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2558 if (!NT_STATUS_IS_OK(status)) {
2559 d_printf("NetShareEnum level %u failed - %s\n",
2560 info_ctr.level, nt_errstr(status));
2561 talloc_free(mem_ctx);
2567 c1 = r.out.info_ctr->ctr.ctr1;
2568 for (i=0; i<c1->count; i++) {
2569 if (c1->array[i].type != STYPE_PRINTQ) {
2572 if (!add_string_to_array(ctx, c1->array[i].name,
2573 printers, num_printers)) {
2575 return NT_STATUS_NO_MEMORY;
2579 talloc_free(mem_ctx);
2580 return NT_STATUS_OK;
2583 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2584 const char *servername, int level, int *num_printers)
2586 struct spoolss_EnumPrinters r;
2590 r.in.flags = PRINTER_ENUM_LOCAL;
2591 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2596 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2597 if (!NT_STATUS_IS_OK(status)) {
2598 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2599 __location__, nt_errstr(status));
2603 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2604 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2605 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2606 win_errstr(r.out.result));
2610 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2611 if (blob.data == NULL) {
2612 d_printf("(%s) data_blob_talloc failed\n", __location__);
2616 r.in.buffer = &blob;
2617 r.in.offered = r.out.needed;
2619 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2620 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2621 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2622 "%s\n", __location__, nt_errstr(status),
2623 win_errstr(r.out.result));
2627 *num_printers = r.out.count;
2632 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2633 struct policy_handle *handle, int level,
2634 union spoolss_PrinterInfo **res)
2636 TALLOC_CTX *mem_ctx;
2637 struct spoolss_GetPrinter r;
2641 mem_ctx = talloc_new(ctx);
2642 if (mem_ctx == NULL) {
2643 return NT_STATUS_NO_MEMORY;
2646 r.in.handle = handle;
2651 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2652 if (!NT_STATUS_IS_OK(status)) {
2653 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2654 __location__, nt_errstr(status));
2655 talloc_free(mem_ctx);
2659 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2660 printf("GetPrinter unexpected return code %s, should "
2661 "be WERR_INSUFFICIENT_BUFFER\n",
2662 win_errstr(r.out.result));
2663 talloc_free(mem_ctx);
2664 return NT_STATUS_UNSUCCESSFUL;
2667 r.in.handle = handle;
2669 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2670 if (blob.data == NULL) {
2671 talloc_free(mem_ctx);
2672 return NT_STATUS_NO_MEMORY;
2674 memset(blob.data, 0, blob.length);
2675 r.in.buffer = &blob;
2676 r.in.offered = r.out.needed;
2678 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2679 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2680 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2681 "%s\n", __location__, nt_errstr(status),
2682 win_errstr(r.out.result));
2683 talloc_free(mem_ctx);
2684 return NT_STATUS_IS_OK(status) ?
2685 NT_STATUS_UNSUCCESSFUL : status;
2689 *res = talloc_steal(ctx, r.out.info);
2692 talloc_free(mem_ctx);
2693 return NT_STATUS_OK;
2696 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2698 TALLOC_CTX *mem_ctx;
2700 struct smbcli_state *cli;
2701 struct dcerpc_pipe *p;
2703 struct policy_handle server_handle, printer_handle;
2704 const char **printers;
2706 struct spoolss_UserLevel1 userlevel1;
2709 if (!(mem_ctx = talloc_new(torture))) {
2713 if (!(torture_open_connection_share(
2714 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2715 "IPC$", torture->ev))) {
2716 d_printf("IPC$ connection failed\n");
2717 talloc_free(mem_ctx);
2721 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2722 if (!NT_STATUS_IS_OK(status)) {
2723 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2724 __location__, nt_errstr(status));
2725 talloc_free(mem_ctx);
2729 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2730 &printers, &num_printers))) {
2731 talloc_free(mem_ctx);
2735 if (num_printers == 0) {
2736 d_printf("Did not find printers\n");
2737 talloc_free(mem_ctx);
2741 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2742 &ndr_table_spoolss, &p);
2743 if (!NT_STATUS_IS_OK(status)) {
2744 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2746 talloc_free(mem_ctx);
2750 ZERO_STRUCT(userlevel1);
2751 userlevel1.client = talloc_asprintf(
2752 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2753 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2754 userlevel1.build = 2600;
2755 userlevel1.major = 3;
2756 userlevel1.minor = 0;
2757 userlevel1.processor = 0;
2760 struct spoolss_OpenPrinterEx r;
2763 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2765 r.in.datatype = NULL;
2766 r.in.access_mask = 0;
2768 r.in.userlevel.level1 = &userlevel1;
2769 r.out.handle = &server_handle;
2771 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2772 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2773 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2774 "%s, %s\n", __location__, nt_errstr(status),
2775 win_errstr(r.out.result));
2776 talloc_free(mem_ctx);
2782 struct spoolss_ClosePrinter r;
2784 r.in.handle = &server_handle;
2785 r.out.handle = &server_handle;
2787 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2788 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2789 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2790 "%s, %s\n", __location__, nt_errstr(status),
2791 win_errstr(r.out.result));
2792 talloc_free(mem_ctx);
2798 struct spoolss_OpenPrinterEx r;
2801 r.in.printername = talloc_asprintf(
2802 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2803 r.in.datatype = NULL;
2804 r.in.access_mask = 0;
2806 r.in.userlevel.level1 = &userlevel1;
2807 r.out.handle = &printer_handle;
2809 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2810 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2811 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2812 "%s, %s\n", __location__, nt_errstr(status),
2813 win_errstr(r.out.result));
2814 talloc_free(mem_ctx);
2822 for (i=0; i<8; i++) {
2823 status = getprinterinfo(mem_ctx, p, &printer_handle,
2825 if (!NT_STATUS_IS_OK(status)) {
2826 d_printf("(%s) getprinterinfo %d failed: %s\n",
2827 __location__, i, nt_errstr(status));
2834 struct spoolss_ClosePrinter r;
2836 r.in.handle = &printer_handle;
2837 r.out.handle = &printer_handle;
2839 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2840 if (!NT_STATUS_IS_OK(status)) {
2841 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2842 "%s\n", __location__, nt_errstr(status));
2843 talloc_free(mem_ctx);
2850 if (!enumprinters(mem_ctx, p, servername, 1,
2852 d_printf("(%s) enumprinters failed\n", __location__);
2853 talloc_free(mem_ctx);
2856 if (num_printers != num_enumerated) {
2857 d_printf("(%s) netshareenum gave %d printers, "
2858 "enumprinters lvl 1 gave %d\n", __location__,
2859 num_printers, num_enumerated);
2860 talloc_free(mem_ctx);
2867 if (!enumprinters(mem_ctx, p, servername, 2,
2869 d_printf("(%s) enumprinters failed\n", __location__);
2870 talloc_free(mem_ctx);
2873 if (num_printers != num_enumerated) {
2874 d_printf("(%s) netshareenum gave %d printers, "
2875 "enumprinters lvl 2 gave %d\n", __location__,
2876 num_printers, num_enumerated);
2877 talloc_free(mem_ctx);
2882 talloc_free(mem_ctx);
2887 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2889 TALLOC_CTX *mem_ctx;
2890 struct smbcli_state *cli;
2891 struct dcerpc_pipe *p;
2895 if (!(mem_ctx = talloc_new(torture))) {
2899 if (!(torture_open_connection_share(
2900 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2901 "IPC$", torture->ev))) {
2902 d_printf("IPC$ connection failed\n");
2903 talloc_free(mem_ctx);
2907 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2908 if (!NT_STATUS_IS_OK(status)) {
2909 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2910 __location__, nt_errstr(status));
2911 talloc_free(mem_ctx);
2915 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2916 &ndr_table_wkssvc, &p);
2917 if (!NT_STATUS_IS_OK(status)) {
2918 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2920 talloc_free(mem_ctx);
2925 struct wkssvc_NetWkstaInfo100 wks100;
2926 union wkssvc_NetWkstaInfo info;
2927 struct wkssvc_NetWkstaGetInfo r;
2929 r.in.server_name = "\\foo";
2931 info.info100 = &wks100;
2934 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2935 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2936 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2937 "%s, %s\n", __location__, nt_errstr(status),
2938 win_errstr(r.out.result));
2939 talloc_free(mem_ctx);
2943 if (strcmp(servername,
2944 r.out.info->info100->server_name) != 0) {
2945 d_printf("(%s) servername inconsistency: RAP=%s, "
2946 "dcerpc_wkssvc_NetWksGetInfo=%s",
2947 __location__, servername,
2948 r.out.info->info100->server_name);
2949 talloc_free(mem_ctx);
2954 talloc_free(mem_ctx);
2958 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2959 struct policy_handle *handle)
2961 struct winreg_CloseKey c;
2963 TALLOC_CTX *mem_ctx;
2965 c.in.handle = c.out.handle = handle;
2967 if (!(mem_ctx = talloc_new(p))) {
2968 return NT_STATUS_NO_MEMORY;
2971 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2972 talloc_free(mem_ctx);
2974 if (!NT_STATUS_IS_OK(status)) {
2978 if (!W_ERROR_IS_OK(c.out.result)) {
2979 return werror_to_ntstatus(c.out.result);
2982 return NT_STATUS_OK;
2985 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2986 TALLOC_CTX *mem_ctx)
2988 uint32_t enum_index = 0;
2991 struct winreg_EnumValue r;
2992 struct winreg_StringBuf name;
2993 enum winreg_Type type = 0;
2996 uint32_t size, length;
2998 r.in.handle = handle;
2999 r.in.enum_index = enum_index;
3002 r.in.name = r.out.name = &name;
3008 r.in.length = &length;
3010 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
3011 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3012 return NT_STATUS_OK;
3018 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
3019 TALLOC_CTX *mem_ctx, int depth)
3021 struct winreg_EnumKey r;
3022 struct winreg_StringBuf class, name;
3027 return NT_STATUS_OK;
3033 r.in.handle = handle;
3034 r.in.enum_index = 0;
3036 r.in.keyclass = &class;
3038 r.in.last_changed_time = &t;
3041 TALLOC_CTX *tmp_ctx;
3042 struct winreg_OpenKey o;
3043 struct policy_handle key_handle;
3046 if (!(tmp_ctx = talloc_new(mem_ctx))) {
3047 return NT_STATUS_NO_MEMORY;
3053 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
3054 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3055 /* We're done enumerating */
3056 talloc_free(tmp_ctx);
3057 return NT_STATUS_OK;
3060 for (i=0; i<10-depth; i++)
3062 printf("%s\n", r.out.name->name);
3065 o.in.parent_handle = handle;
3066 o.in.keyname.name = r.out.name->name;
3068 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3069 o.out.handle = &key_handle;
3071 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
3072 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3073 enumkeys(p, &key_handle, tmp_ctx, depth-1);
3074 enumvalues(p, &key_handle, tmp_ctx);
3075 status = winreg_close(p, &key_handle);
3076 if (!NT_STATUS_IS_OK(status)) {
3081 talloc_free(tmp_ctx);
3083 r.in.enum_index += 1;
3086 return NT_STATUS_OK;
3089 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3091 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3092 const char *name, winreg_open_fn open_fn)
3094 struct policy_handle handle;
3095 struct winreg_OpenHKLM r;
3098 r.in.system_name = 0;
3099 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3100 r.out.handle = &handle;
3102 status = open_fn(p, mem_ctx, &r);
3103 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3104 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3105 nt_errstr(status), win_errstr(r.out.result));
3109 enumkeys(p, &handle, mem_ctx, 4);
3111 status = winreg_close(p, &handle);
3112 if (!NT_STATUS_IS_OK(status)) {
3113 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3114 __location__, nt_errstr(status));
3121 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3124 struct dcerpc_pipe *p;
3125 TALLOC_CTX *mem_ctx;
3131 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3132 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3133 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3134 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3135 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3140 mem_ctx = talloc_init("torture_rpc_winreg");
3142 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3144 if (!NT_STATUS_IS_OK(status)) {
3145 talloc_free(mem_ctx);
3150 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3152 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3153 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3158 talloc_free(mem_ctx);
3163 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3164 struct loadparm_context *lp_ctx,
3165 struct smbcli_state *cli,
3167 struct srvsvc_NetShareInfo502 **info502)
3169 struct smbcli_tree *ipc;
3170 struct dcerpc_pipe *p;
3171 struct srvsvc_NetShareGetInfo r;
3172 union srvsvc_NetShareInfo info;
3175 if (!(p = dcerpc_pipe_init(cli,
3176 cli->transport->socket->event.ctx,
3177 lp_iconv_convenience(lp_ctx)))) {
3178 status = NT_STATUS_NO_MEMORY;
3182 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3183 if (!NT_STATUS_IS_OK(status)) {
3187 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3188 if (!NT_STATUS_IS_OK(status)) {
3189 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3194 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3195 if (!NT_STATUS_IS_OK(status)) {
3196 d_printf("dcerpc_bind_auth_none failed: %s\n",
3201 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3202 dcerpc_server_name(p));
3203 r.in.share_name = share;
3207 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3208 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3209 d_printf("(%s) srvsvc_NetShareGetInfo failed: %s, %s\n", __location__,
3210 nt_errstr(status), win_errstr(r.out.result));
3214 *info502 = talloc_move(mem_ctx, &info.info502);
3215 return NT_STATUS_OK;
3223 * Get us a handle on HKLM\
3226 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3227 struct smbcli_state *cli,
3228 struct smb_iconv_convenience *iconv_convenience,
3229 struct dcerpc_pipe **pipe_p,
3230 struct policy_handle **handle)
3232 struct smbcli_tree *ipc;
3233 struct dcerpc_pipe *p;
3234 struct winreg_OpenHKLM r;
3236 struct policy_handle *result;
3238 result = talloc(mem_ctx, struct policy_handle);
3240 if (result == NULL) {
3241 return NT_STATUS_NO_MEMORY;
3244 if (!(p = dcerpc_pipe_init(result,
3245 cli->transport->socket->event.ctx,
3246 iconv_convenience))) {
3247 status = NT_STATUS_NO_MEMORY;
3251 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3252 if (!NT_STATUS_IS_OK(status)) {
3256 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3257 if (!NT_STATUS_IS_OK(status)) {
3258 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3263 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3264 if (!NT_STATUS_IS_OK(status)) {
3265 d_printf("dcerpc_bind_auth_none failed: %s\n",
3270 r.in.system_name = 0;
3271 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3272 r.out.handle = result;
3274 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3275 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3276 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3277 nt_errstr(status), win_errstr(r.out.result));
3283 return NT_STATUS_OK;
3286 talloc_free(result);
3290 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3291 struct smb_iconv_convenience *iconv_convenience,
3292 const char *sharename)
3294 struct dcerpc_pipe *p;
3295 struct policy_handle *hklm = NULL;
3296 struct policy_handle new_handle;
3297 struct winreg_CreateKey c;
3298 struct winreg_CloseKey cl;
3299 enum winreg_CreateAction action_taken;
3301 TALLOC_CTX *mem_ctx;
3303 mem_ctx = talloc_new(cli);
3304 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3306 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3307 if (!NT_STATUS_IS_OK(status)) {
3308 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3313 c.in.name.name = talloc_asprintf(
3314 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3315 if (c.in.name.name == NULL) {
3316 d_printf("talloc_asprintf failed\n");
3317 status = NT_STATUS_NO_MEMORY;
3320 c.in.keyclass.name = "";
3322 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3323 c.in.secdesc = NULL;
3324 c.in.action_taken = &action_taken;
3325 c.out.new_handle = &new_handle;
3326 c.out.action_taken = &action_taken;
3328 status = dcerpc_winreg_CreateKey(p, p, &c);
3329 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3330 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3331 nt_errstr(status), win_errstr(c.out.result));
3335 cl.in.handle = &new_handle;
3336 cl.out.handle = &new_handle;
3337 status = dcerpc_winreg_CloseKey(p, p, &cl);
3338 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3339 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3340 nt_errstr(status), win_errstr(cl.out.result));
3346 talloc_free(mem_ctx);
3350 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3351 struct smbcli_state *cli,
3352 const char *sharename)
3354 struct dcerpc_pipe *p;
3355 struct policy_handle *hklm = NULL;
3356 struct winreg_DeleteKey d;
3358 TALLOC_CTX *mem_ctx;
3360 mem_ctx = talloc_new(cli);
3361 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3363 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3365 if (!NT_STATUS_IS_OK(status)) {
3366 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3371 d.in.key.name = talloc_asprintf(
3372 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3373 if (d.in.key.name == NULL) {
3374 d_printf("talloc_asprintf failed\n");
3375 status = NT_STATUS_NO_MEMORY;
3378 status = dcerpc_winreg_DeleteKey(p, p, &d);
3379 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3380 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3381 nt_errstr(status), win_errstr(d.out.result));
3386 talloc_free(mem_ctx);
3390 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3391 struct loadparm_context *lp_ctx,
3392 const char *sharename,
3393 const char *parameter,
3396 struct dcerpc_pipe *p = NULL;
3397 struct policy_handle *hklm = NULL, key_handle;
3398 struct winreg_OpenKey o;
3399 struct winreg_SetValue s;
3404 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3405 if (!NT_STATUS_IS_OK(status)) {
3406 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3410 o.in.parent_handle = hklm;
3411 o.in.keyname.name = talloc_asprintf(
3412 hklm, "software\\samba\\smbconf\\%s", sharename);
3413 if (o.in.keyname.name == NULL) {
3414 d_printf("talloc_asprintf failed\n");
3415 status = NT_STATUS_NO_MEMORY;
3419 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3420 o.out.handle = &key_handle;
3422 status = dcerpc_winreg_OpenKey(p, p, &o);
3423 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3424 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3425 nt_errstr(status), win_errstr(o.out.result));
3429 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3430 value, &type, &val)) {
3431 d_printf("(%s) reg_string_to_val failed\n", __location__);
3435 s.in.handle = &key_handle;
3436 s.in.name.name = parameter;
3438 s.in.data = val.data;
3439 s.in.size = val.length;
3441 status = dcerpc_winreg_SetValue(p, p, &s);
3442 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3443 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3444 nt_errstr(status), win_errstr(s.out.result));
3453 bool torture_samba3_regconfig(struct torture_context *torture)
3455 struct smbcli_state *cli;
3456 struct srvsvc_NetShareInfo502 *i = NULL;
3459 const char *comment = "Dummer Kommentar";
3461 if (!(torture_open_connection(&cli, torture, 0))) {
3465 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3466 if (!NT_STATUS_IS_OK(status)) {
3467 torture_warning(torture, "torture_samba3_createshare failed: "
3468 "%s\n", nt_errstr(status));
3472 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3473 if (!NT_STATUS_IS_OK(status)) {
3474 torture_warning(torture, "torture_samba3_setconfig failed: "
3475 "%s\n", nt_errstr(status));
3479 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3480 if (!NT_STATUS_IS_OK(status)) {
3481 torture_warning(torture, "get_shareinfo failed: "
3482 "%s\n", nt_errstr(status));
3486 if (strcmp(comment, i->comment) != 0) {
3487 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3488 comment, i->comment);
3492 status = torture_samba3_deleteshare(torture, cli, "blubber");
3493 if (!NT_STATUS_IS_OK(status)) {
3494 torture_warning(torture, "torture_samba3_deleteshare failed: "
3495 "%s\n", nt_errstr(status));