2 Unix SMB/CIFS implementation.
4 dcerpc torture tests, designed to walk Samba3 code paths
6 Copyright (C) Volker Lendecke 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/rap/rap.h"
26 #include "torture/torture.h"
27 #include "torture/util.h"
28 #include "torture/rap/proto.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "librpc/gen_ndr/ndr_lsa_c.h"
31 #include "librpc/gen_ndr/ndr_samr.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "librpc/gen_ndr/ndr_netlogon.h"
34 #include "librpc/gen_ndr/ndr_netlogon_c.h"
35 #include "librpc/gen_ndr/ndr_srvsvc.h"
36 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
37 #include "librpc/gen_ndr/ndr_spoolss.h"
38 #include "librpc/gen_ndr/ndr_spoolss_c.h"
39 #include "librpc/gen_ndr/ndr_winreg.h"
40 #include "librpc/gen_ndr/ndr_winreg_c.h"
41 #include "librpc/gen_ndr/ndr_wkssvc.h"
42 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
43 #include "lib/cmdline/popt_common.h"
44 #include "librpc/rpc/dcerpc.h"
45 #include "torture/rpc/rpc.h"
46 #include "libcli/libcli.h"
47 #include "libcli/composite/composite.h"
48 #include "libcli/smb_composite/smb_composite.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "../lib/crypto/crypto.h"
51 #include "auth/ntlmssp/ntlmssp.h"
52 #include "libcli/security/proto.h"
53 #include "param/param.h"
54 #include "lib/registry/registry.h"
55 #include "libcli/resolve/resolve.h"
58 * This tests a RPC call using an invalid vuid
61 bool torture_bind_authcontext(struct torture_context *torture)
66 struct lsa_ObjectAttribute objectattr;
67 struct lsa_OpenPolicy2 openpolicy;
68 struct policy_handle handle;
69 struct lsa_Close close_handle;
70 struct smbcli_session *tmp;
71 struct smbcli_session *session2;
72 struct smbcli_state *cli;
73 struct dcerpc_pipe *lsa_pipe;
74 struct cli_credentials *anon_creds;
75 struct smb_composite_sesssetup setup;
76 struct smbcli_options options;
77 struct smbcli_session_options session_options;
79 mem_ctx = talloc_init("torture_bind_authcontext");
81 if (mem_ctx == NULL) {
82 d_printf("talloc_init failed\n");
86 lp_smbcli_options(torture->lp_ctx, &options);
87 lp_smbcli_session_options(torture->lp_ctx, &session_options);
89 status = smbcli_full_connection(mem_ctx, &cli,
90 torture_setting_string(torture, "host", NULL),
91 lp_smb_ports(torture->lp_ctx),
93 lp_socket_options(torture->lp_ctx),
95 lp_resolve_context(torture->lp_ctx),
96 torture->ev, &options, &session_options,
97 lp_iconv_convenience(torture->lp_ctx),
98 lp_gensec_settings(torture, torture->lp_ctx));
99 if (!NT_STATUS_IS_OK(status)) {
100 d_printf("smbcli_full_connection failed: %s\n",
105 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
106 lp_iconv_convenience(torture->lp_ctx));
107 if (lsa_pipe == NULL) {
108 d_printf("dcerpc_pipe_init failed\n");
112 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
113 if (!NT_STATUS_IS_OK(status)) {
114 d_printf("dcerpc_pipe_open_smb failed: %s\n",
119 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
120 if (!NT_STATUS_IS_OK(status)) {
121 d_printf("dcerpc_bind_auth_none failed: %s\n",
126 openpolicy.in.system_name =talloc_asprintf(
127 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
128 ZERO_STRUCT(objectattr);
129 openpolicy.in.attr = &objectattr;
130 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
131 openpolicy.out.handle = &handle;
133 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
135 if (!NT_STATUS_IS_OK(status)) {
136 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
141 close_handle.in.handle = &handle;
142 close_handle.out.handle = &handle;
144 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
145 if (!NT_STATUS_IS_OK(status)) {
146 d_printf("dcerpc_lsa_Close failed: %s\n",
151 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
152 if (session2 == NULL) {
153 d_printf("smbcli_session_init failed\n");
157 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
158 d_printf("create_anon_creds failed\n");
162 setup.in.sesskey = cli->transport->negotiate.sesskey;
163 setup.in.capabilities = cli->transport->negotiate.capabilities;
164 setup.in.workgroup = "";
165 setup.in.credentials = anon_creds;
166 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
168 status = smb_composite_sesssetup(session2, &setup);
169 if (!NT_STATUS_IS_OK(status)) {
170 d_printf("anon session setup failed: %s\n",
174 session2->vuid = setup.out.vuid;
176 tmp = cli->tree->session;
177 cli->tree->session = session2;
179 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
181 cli->tree->session = tmp;
182 talloc_free(lsa_pipe);
185 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
186 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
187 "expected NT_STATUS_INVALID_HANDLE\n",
194 talloc_free(mem_ctx);
199 * Bind to lsa using a specific auth method
202 static bool bindtest(struct smbcli_state *cli,
203 struct cli_credentials *credentials,
204 struct loadparm_context *lp_ctx,
205 uint8_t auth_type, uint8_t auth_level)
211 struct dcerpc_pipe *lsa_pipe;
212 struct lsa_ObjectAttribute objectattr;
213 struct lsa_OpenPolicy2 openpolicy;
214 struct lsa_QueryInfoPolicy query;
215 union lsa_PolicyInformation *info = NULL;
216 struct policy_handle handle;
217 struct lsa_Close close_handle;
219 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
220 d_printf("talloc_init failed\n");
224 lsa_pipe = dcerpc_pipe_init(mem_ctx,
225 cli->transport->socket->event.ctx,
226 lp_iconv_convenience(lp_ctx));
227 if (lsa_pipe == NULL) {
228 d_printf("dcerpc_pipe_init failed\n");
232 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
233 if (!NT_STATUS_IS_OK(status)) {
234 d_printf("dcerpc_pipe_open_smb failed: %s\n",
239 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
240 credentials, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
242 if (!NT_STATUS_IS_OK(status)) {
243 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
247 openpolicy.in.system_name =talloc_asprintf(
248 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
249 ZERO_STRUCT(objectattr);
250 openpolicy.in.attr = &objectattr;
251 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
252 openpolicy.out.handle = &handle;
254 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
256 if (!NT_STATUS_IS_OK(status)) {
257 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
262 query.in.handle = &handle;
263 query.in.level = LSA_POLICY_INFO_DOMAIN;
264 query.out.info = &info;
266 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
267 if (!NT_STATUS_IS_OK(status)) {
268 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
273 close_handle.in.handle = &handle;
274 close_handle.out.handle = &handle;
276 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
277 if (!NT_STATUS_IS_OK(status)) {
278 d_printf("dcerpc_lsa_Close failed: %s\n",
285 talloc_free(mem_ctx);
290 * test authenticated RPC binds with the variants Samba3 does support
293 bool torture_bind_samba3(struct torture_context *torture)
298 struct smbcli_state *cli;
299 struct smbcli_options options;
300 struct smbcli_session_options session_options;
302 mem_ctx = talloc_init("torture_bind_authcontext");
304 if (mem_ctx == NULL) {
305 d_printf("talloc_init failed\n");
309 lp_smbcli_options(torture->lp_ctx, &options);
310 lp_smbcli_session_options(torture->lp_ctx, &session_options);
312 status = smbcli_full_connection(mem_ctx, &cli,
313 torture_setting_string(torture, "host", NULL),
314 lp_smb_ports(torture->lp_ctx),
316 lp_socket_options(torture->lp_ctx),
318 lp_resolve_context(torture->lp_ctx),
319 torture->ev, &options, &session_options,
320 lp_iconv_convenience(torture->lp_ctx),
321 lp_gensec_settings(torture, torture->lp_ctx));
322 if (!NT_STATUS_IS_OK(status)) {
323 d_printf("smbcli_full_connection failed: %s\n",
330 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
331 DCERPC_AUTH_LEVEL_INTEGRITY);
332 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
333 DCERPC_AUTH_LEVEL_PRIVACY);
334 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
335 DCERPC_AUTH_LEVEL_INTEGRITY);
336 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
337 DCERPC_AUTH_LEVEL_PRIVACY);
340 talloc_free(mem_ctx);
345 * Lookup or create a user and return all necessary info
348 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
350 struct loadparm_context *lp_ctx,
351 struct cli_credentials *admin_creds,
354 const char *username,
356 struct dcerpc_pipe **result_pipe,
357 struct policy_handle **result_handle,
358 struct dom_sid **sid_p)
360 struct dcerpc_pipe *samr_pipe;
362 struct policy_handle conn_handle;
363 struct policy_handle domain_handle;
364 struct policy_handle *user_handle;
365 struct samr_Connect2 conn;
366 struct samr_EnumDomains enumdom;
367 uint32_t resume_handle = 0;
368 uint32_t num_entries = 0;
369 struct samr_SamArray *sam = NULL;
370 struct samr_LookupDomain l;
371 struct dom_sid2 *sid = NULL;
373 struct lsa_String domain_name;
374 struct lsa_String user_name;
375 struct samr_OpenDomain o;
376 struct samr_CreateUser2 c;
377 uint32_t user_rid,access_granted;
379 samr_pipe = dcerpc_pipe_init(mem_ctx,
380 cli->transport->socket->event.ctx,
381 lp_iconv_convenience(lp_ctx));
382 if (samr_pipe == NULL) {
383 d_printf("dcerpc_pipe_init failed\n");
384 status = NT_STATUS_NO_MEMORY;
388 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
389 if (!NT_STATUS_IS_OK(status)) {
390 d_printf("dcerpc_pipe_open_smb failed: %s\n",
395 if (admin_creds != NULL) {
396 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
397 admin_creds, lp_gensec_settings(lp_ctx, lp_ctx), auth_type, auth_level,
399 if (!NT_STATUS_IS_OK(status)) {
400 d_printf("dcerpc_bind_auth failed: %s\n",
405 /* We must have an authenticated SMB connection */
406 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
407 if (!NT_STATUS_IS_OK(status)) {
408 d_printf("dcerpc_bind_auth_none failed: %s\n",
414 conn.in.system_name = talloc_asprintf(
415 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
416 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
417 conn.out.connect_handle = &conn_handle;
419 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
420 if (!NT_STATUS_IS_OK(status)) {
421 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
425 enumdom.in.connect_handle = &conn_handle;
426 enumdom.in.resume_handle = &resume_handle;
427 enumdom.in.buf_size = (uint32_t)-1;
428 enumdom.out.resume_handle = &resume_handle;
429 enumdom.out.num_entries = &num_entries;
430 enumdom.out.sam = &sam;
432 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
433 if (!NT_STATUS_IS_OK(status)) {
434 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
438 if (*enumdom.out.num_entries != 2) {
439 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
440 *enumdom.out.num_entries);
441 status = NT_STATUS_UNSUCCESSFUL;
445 dom_idx = strequal(sam->entries[0].name.string,
448 l.in.connect_handle = &conn_handle;
449 domain_name.string = sam->entries[dom_idx].name.string;
450 *domain = talloc_strdup(mem_ctx, domain_name.string);
451 l.in.domain_name = &domain_name;
454 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
455 if (!NT_STATUS_IS_OK(status)) {
456 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
460 o.in.connect_handle = &conn_handle;
461 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
462 o.in.sid = *l.out.sid;
463 o.out.domain_handle = &domain_handle;
465 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
466 if (!NT_STATUS_IS_OK(status)) {
467 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
471 c.in.domain_handle = &domain_handle;
472 user_name.string = username;
473 c.in.account_name = &user_name;
474 c.in.acct_flags = ACB_NORMAL;
475 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
476 user_handle = talloc(mem_ctx, struct policy_handle);
477 c.out.user_handle = user_handle;
478 c.out.access_granted = &access_granted;
479 c.out.rid = &user_rid;
481 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
483 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
484 struct samr_LookupNames ln;
485 struct samr_OpenUser ou;
486 struct samr_Ids rids, types;
488 ln.in.domain_handle = &domain_handle;
490 ln.in.names = &user_name;
492 ln.out.types = &types;
494 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
495 if (!NT_STATUS_IS_OK(status)) {
496 d_printf("samr_LookupNames failed: %s\n",
501 ou.in.domain_handle = &domain_handle;
502 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
503 user_rid = ou.in.rid = ln.out.rids->ids[0];
504 ou.out.user_handle = user_handle;
506 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
507 if (!NT_STATUS_IS_OK(status)) {
508 d_printf("samr_OpenUser failed: %s\n",
514 if (!NT_STATUS_IS_OK(status)) {
515 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
519 *result_pipe = samr_pipe;
520 *result_handle = user_handle;
522 *sid_p = dom_sid_add_rid(mem_ctx, *l.out.sid, user_rid);
534 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
535 struct loadparm_context *lp_ctx,
536 struct cli_credentials *admin_creds,
537 const char *username, const char *password,
539 struct dom_sid **user_sid)
543 struct dcerpc_pipe *samr_pipe;
544 struct policy_handle *wks_handle;
547 if (!(tmp_ctx = talloc_new(mem_ctx))) {
548 d_printf("talloc_init failed\n");
552 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
553 DCERPC_AUTH_TYPE_NTLMSSP,
554 DCERPC_AUTH_LEVEL_INTEGRITY,
555 username, domain_name, &samr_pipe, &wks_handle,
557 if (!NT_STATUS_IS_OK(status)) {
558 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
563 struct samr_SetUserInfo2 sui2;
564 struct samr_SetUserInfo sui;
565 struct samr_QueryUserInfo qui;
566 union samr_UserInfo u_info;
567 union samr_UserInfo *info;
568 DATA_BLOB session_key;
572 encode_pw_buffer(u_info.info23.password.data, password,
575 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
576 if (!NT_STATUS_IS_OK(status)) {
577 d_printf("dcerpc_fetch_session_key failed\n");
580 arcfour_crypt_blob(u_info.info23.password.data, 516,
582 u_info.info23.info.password_expired = 0;
583 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
584 SAMR_FIELD_PASSWORD2 |
585 SAMR_FIELD_EXPIRED_FLAG;
586 sui2.in.user_handle = wks_handle;
587 sui2.in.info = &u_info;
590 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
591 if (!NT_STATUS_IS_OK(status)) {
592 d_printf("samr_SetUserInfo(23) failed: %s\n",
597 u_info.info16.acct_flags = ACB_NORMAL;
598 sui.in.user_handle = wks_handle;
599 sui.in.info = &u_info;
602 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
603 if (!NT_STATUS_IS_OK(status)) {
604 d_printf("samr_SetUserInfo(16) failed\n");
608 qui.in.user_handle = wks_handle;
610 qui.out.info = &info;
612 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
613 if (!NT_STATUS_IS_OK(status)) {
614 d_printf("samr_QueryUserInfo(21) failed\n");
618 info->info21.allow_password_change = 0;
619 info->info21.force_password_change = 0;
620 info->info21.account_name.string = NULL;
621 info->info21.rid = 0;
622 info->info21.acct_expiry = 0;
623 info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
625 u_info.info21 = info->info21;
626 sui.in.user_handle = wks_handle;
627 sui.in.info = &u_info;
630 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
631 if (!NT_STATUS_IS_OK(status)) {
632 d_printf("samr_SetUserInfo(21) failed\n");
637 *domain_name= talloc_steal(mem_ctx, *domain_name);
638 *user_sid = talloc_steal(mem_ctx, *user_sid);
641 talloc_free(tmp_ctx);
649 static bool delete_user(struct smbcli_state *cli,
650 struct loadparm_context *lp_ctx,
651 struct cli_credentials *admin_creds,
652 const char *username)
657 struct dcerpc_pipe *samr_pipe;
658 struct policy_handle *user_handle;
661 if ((mem_ctx = talloc_init("leave")) == NULL) {
662 d_printf("talloc_init failed\n");
666 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
667 DCERPC_AUTH_TYPE_NTLMSSP,
668 DCERPC_AUTH_LEVEL_INTEGRITY,
669 username, &dom_name, &samr_pipe,
672 if (!NT_STATUS_IS_OK(status)) {
673 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
678 struct samr_DeleteUser d;
680 d.in.user_handle = user_handle;
681 d.out.user_handle = user_handle;
683 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
684 if (!NT_STATUS_IS_OK(status)) {
685 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
693 talloc_free(mem_ctx);
698 * Do a Samba3-style join
701 static bool join3(struct smbcli_state *cli,
702 struct loadparm_context *lp_ctx,
704 struct cli_credentials *admin_creds,
705 struct cli_credentials *wks_creds)
710 struct dcerpc_pipe *samr_pipe;
711 struct policy_handle *wks_handle;
713 NTTIME last_password_change;
715 if ((mem_ctx = talloc_init("join3")) == NULL) {
716 d_printf("talloc_init failed\n");
720 status = get_usr_handle(
721 cli, mem_ctx, lp_ctx, admin_creds,
722 DCERPC_AUTH_TYPE_NTLMSSP,
723 DCERPC_AUTH_LEVEL_PRIVACY,
724 talloc_asprintf(mem_ctx, "%s$",
725 cli_credentials_get_workstation(wks_creds)),
726 &dom_name, &samr_pipe, &wks_handle, NULL);
728 if (!NT_STATUS_IS_OK(status)) {
729 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
734 struct samr_QueryUserInfo q;
735 union samr_UserInfo *info;
737 q.in.user_handle = wks_handle;
741 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
742 if (!NT_STATUS_IS_OK(status)) {
743 d_printf("(%s) QueryUserInfo failed: %s\n",
744 __location__, nt_errstr(status));
748 last_password_change = info->info21.last_password_change;
751 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
754 struct samr_SetUserInfo2 sui2;
755 union samr_UserInfo u_info;
756 struct samr_UserInfo21 *i21 = &u_info.info25.info;
757 DATA_BLOB session_key;
758 DATA_BLOB confounded_session_key = data_blob_talloc(
760 struct MD5Context ctx;
761 uint8_t confounder[16];
765 i21->full_name.string = talloc_asprintf(
767 cli_credentials_get_workstation(wks_creds));
768 i21->acct_flags = ACB_WSTRUST;
769 i21->fields_present = SAMR_FIELD_FULL_NAME |
770 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
771 /* this would break the test result expectations
772 i21->fields_present |= SAMR_FIELD_EXPIRED_FLAG;
773 i21->password_expired = 1;
776 encode_pw_buffer(u_info.info25.password.data,
777 cli_credentials_get_password(wks_creds),
779 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
780 if (!NT_STATUS_IS_OK(status)) {
781 d_printf("dcerpc_fetch_session_key failed: %s\n",
785 generate_random_buffer((uint8_t *)confounder, 16);
788 MD5Update(&ctx, confounder, 16);
789 MD5Update(&ctx, session_key.data, session_key.length);
790 MD5Final(confounded_session_key.data, &ctx);
792 arcfour_crypt_blob(u_info.info25.password.data, 516,
793 &confounded_session_key);
794 memcpy(&u_info.info25.password.data[516], confounder, 16);
796 sui2.in.user_handle = wks_handle;
798 sui2.in.info = &u_info;
800 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
801 if (!NT_STATUS_IS_OK(status)) {
802 d_printf("samr_SetUserInfo2(25) failed: %s\n",
807 struct samr_SetUserInfo2 sui2;
808 struct samr_SetUserInfo sui;
809 union samr_UserInfo u_info;
810 DATA_BLOB session_key;
812 encode_pw_buffer(u_info.info24.password.data,
813 cli_credentials_get_password(wks_creds),
815 /* just to make this test pass */
816 u_info.info24.password_expired = 1;
818 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
819 if (!NT_STATUS_IS_OK(status)) {
820 d_printf("dcerpc_fetch_session_key failed\n");
823 arcfour_crypt_blob(u_info.info24.password.data, 516,
825 sui2.in.user_handle = wks_handle;
826 sui2.in.info = &u_info;
829 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
830 if (!NT_STATUS_IS_OK(status)) {
831 d_printf("samr_SetUserInfo(24) failed: %s\n",
836 u_info.info16.acct_flags = ACB_WSTRUST;
837 sui.in.user_handle = wks_handle;
838 sui.in.info = &u_info;
841 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
842 if (!NT_STATUS_IS_OK(status)) {
843 d_printf("samr_SetUserInfo(16) failed\n");
849 struct samr_QueryUserInfo q;
850 union samr_UserInfo *info;
852 q.in.user_handle = wks_handle;
856 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
857 if (!NT_STATUS_IS_OK(status)) {
858 d_printf("(%s) QueryUserInfo failed: %s\n",
859 __location__, nt_errstr(status));
864 if (last_password_change
865 == info->info21.last_password_change) {
866 d_printf("(%s) last_password_change unchanged "
867 "during join, level25 must change "
868 "it\n", __location__);
873 if (last_password_change
874 != info->info21.last_password_change) {
875 d_printf("(%s) last_password_change changed "
876 "during join, level24 doesn't "
877 "change it\n", __location__);
886 talloc_free(mem_ctx);
891 * Do a ReqChallenge/Auth2 and get the wks creds
894 static bool auth2(struct smbcli_state *cli,
895 struct loadparm_context *lp_ctx,
896 struct cli_credentials *wks_cred)
899 struct dcerpc_pipe *net_pipe;
902 struct netr_ServerReqChallenge r;
903 struct netr_Credential netr_cli_creds;
904 struct netr_Credential netr_srv_creds;
905 uint32_t negotiate_flags;
906 struct netr_ServerAuthenticate2 a;
907 struct creds_CredentialState *creds_state;
908 struct netr_Credential netr_cred;
909 struct samr_Password mach_pw;
911 mem_ctx = talloc_new(NULL);
912 if (mem_ctx == NULL) {
913 d_printf("talloc_new failed\n");
917 net_pipe = dcerpc_pipe_init(mem_ctx,
918 cli->transport->socket->event.ctx,
919 lp_iconv_convenience(lp_ctx));
920 if (net_pipe == NULL) {
921 d_printf("dcerpc_pipe_init failed\n");
925 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
926 if (!NT_STATUS_IS_OK(status)) {
927 d_printf("dcerpc_pipe_open_smb failed: %s\n",
932 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
933 if (!NT_STATUS_IS_OK(status)) {
934 d_printf("dcerpc_bind_auth_none failed: %s\n",
939 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
940 r.in.server_name = talloc_asprintf(
941 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
942 if (r.in.server_name == NULL) {
943 d_printf("talloc_asprintf failed\n");
946 generate_random_buffer(netr_cli_creds.data,
947 sizeof(netr_cli_creds.data));
948 r.in.credentials = &netr_cli_creds;
949 r.out.return_credentials = &netr_srv_creds;
951 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
952 if (!NT_STATUS_IS_OK(status)) {
953 d_printf("netr_ServerReqChallenge failed: %s\n",
958 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
959 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
961 creds_state = talloc(mem_ctx, struct creds_CredentialState);
962 creds_client_init(creds_state, r.in.credentials,
963 r.out.return_credentials, &mach_pw,
964 &netr_cred, negotiate_flags);
966 a.in.server_name = talloc_asprintf(
967 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
968 a.in.account_name = talloc_asprintf(
969 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
970 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
971 a.in.secure_channel_type = SEC_CHAN_WKSTA;
972 a.in.negotiate_flags = &negotiate_flags;
973 a.out.negotiate_flags = &negotiate_flags;
974 a.in.credentials = &netr_cred;
975 a.out.return_credentials = &netr_cred;
977 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
978 if (!NT_STATUS_IS_OK(status)) {
979 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
984 if (!creds_client_check(creds_state, a.out.return_credentials)) {
985 d_printf("creds_client_check failed\n");
989 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
994 talloc_free(mem_ctx);
999 * Do a couple of schannel protected Netlogon ops: Interactive and Network
1000 * login, and change the wks password
1003 static bool schan(struct smbcli_state *cli,
1004 struct loadparm_context *lp_ctx,
1005 struct cli_credentials *wks_creds,
1006 struct cli_credentials *user_creds)
1008 TALLOC_CTX *mem_ctx;
1011 struct dcerpc_pipe *net_pipe;
1014 mem_ctx = talloc_new(NULL);
1015 if (mem_ctx == NULL) {
1016 d_printf("talloc_new failed\n");
1020 net_pipe = dcerpc_pipe_init(mem_ctx,
1021 cli->transport->socket->event.ctx,
1022 lp_iconv_convenience(lp_ctx));
1023 if (net_pipe == NULL) {
1024 d_printf("dcerpc_pipe_init failed\n");
1028 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1029 if (!NT_STATUS_IS_OK(status)) {
1030 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1036 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1037 DCERPC_DEBUG_PRINT_OUT;
1040 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1041 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1042 wks_creds, lp_gensec_settings(lp_ctx, lp_ctx), DCERPC_AUTH_TYPE_SCHANNEL,
1043 DCERPC_AUTH_LEVEL_PRIVACY,
1046 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1048 if (!NT_STATUS_IS_OK(status)) {
1049 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1054 for (i=2; i<4; i++) {
1056 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1057 struct creds_CredentialState *creds_state;
1058 struct netr_Authenticator netr_auth, netr_auth2;
1059 struct netr_NetworkInfo ninfo;
1060 struct netr_PasswordInfo pinfo;
1061 struct netr_LogonSamLogon r;
1062 union netr_LogonLevel logon;
1063 union netr_Validation validation;
1064 uint8_t authoritative;
1065 struct netr_Authenticator return_authenticator;
1067 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1068 CLI_CRED_NTLMv2_AUTH;
1070 chal = data_blob_talloc(mem_ctx, NULL, 8);
1071 if (chal.data == NULL) {
1072 d_printf("data_blob_talloc failed\n");
1076 generate_random_buffer(chal.data, chal.length);
1077 names_blob = NTLMv2_generate_names_blob(
1079 cli_credentials_get_workstation(user_creds),
1080 cli_credentials_get_domain(user_creds));
1081 status = cli_credentials_get_ntlm_response(
1082 user_creds, mem_ctx, &flags, chal, names_blob,
1083 &lm_resp, &nt_resp, NULL, NULL);
1084 if (!NT_STATUS_IS_OK(status)) {
1085 d_printf("cli_credentials_get_ntlm_response failed:"
1086 " %s\n", nt_errstr(status));
1090 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1091 creds_client_authenticator(creds_state, &netr_auth);
1093 ninfo.identity_info.account_name.string =
1094 cli_credentials_get_username(user_creds);
1095 ninfo.identity_info.domain_name.string =
1096 cli_credentials_get_domain(user_creds);
1097 ninfo.identity_info.parameter_control = 0;
1098 ninfo.identity_info.logon_id_low = 0;
1099 ninfo.identity_info.logon_id_high = 0;
1100 ninfo.identity_info.workstation.string =
1101 cli_credentials_get_workstation(user_creds);
1102 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1103 ninfo.nt.length = nt_resp.length;
1104 ninfo.nt.data = nt_resp.data;
1105 ninfo.lm.length = lm_resp.length;
1106 ninfo.lm.data = lm_resp.data;
1108 logon.network = &ninfo;
1110 r.in.server_name = talloc_asprintf(
1111 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1112 ZERO_STRUCT(netr_auth2);
1113 r.in.computer_name =
1114 cli_credentials_get_workstation(wks_creds);
1115 r.in.credential = &netr_auth;
1116 r.in.return_authenticator = &netr_auth2;
1117 r.in.logon_level = 2;
1118 r.in.validation_level = i;
1119 r.in.logon = &logon;
1120 r.out.validation = &validation;
1121 r.out.authoritative = &authoritative;
1122 r.out.return_authenticator = &return_authenticator;
1124 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 d_printf("netr_LogonSamLogon failed: %s\n",
1131 if ((r.out.return_authenticator == NULL) ||
1132 (!creds_client_check(creds_state,
1133 &r.out.return_authenticator->cred))) {
1134 d_printf("Credentials check failed!\n");
1138 creds_client_authenticator(creds_state, &netr_auth);
1140 pinfo.identity_info = ninfo.identity_info;
1141 ZERO_STRUCT(pinfo.lmpassword.hash);
1142 E_md4hash(cli_credentials_get_password(user_creds),
1143 pinfo.ntpassword.hash);
1144 session_key = data_blob_talloc(mem_ctx,
1145 creds_state->session_key, 16);
1146 arcfour_crypt_blob(pinfo.ntpassword.hash,
1147 sizeof(pinfo.ntpassword.hash),
1150 logon.password = &pinfo;
1152 r.in.logon_level = 1;
1153 r.in.logon = &logon;
1154 r.out.return_authenticator = &return_authenticator;
1156 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1157 if (!NT_STATUS_IS_OK(status)) {
1158 d_printf("netr_LogonSamLogon failed: %s\n",
1163 if ((r.out.return_authenticator == NULL) ||
1164 (!creds_client_check(creds_state,
1165 &r.out.return_authenticator->cred))) {
1166 d_printf("Credentials check failed!\n");
1172 struct netr_ServerPasswordSet s;
1173 char *password = generate_random_str(wks_creds, 8);
1174 struct creds_CredentialState *creds_state;
1175 struct netr_Authenticator credential, return_authenticator;
1176 struct samr_Password new_password;
1178 s.in.server_name = talloc_asprintf(
1179 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1180 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1181 s.in.account_name = talloc_asprintf(
1182 mem_ctx, "%s$", s.in.computer_name);
1183 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1184 s.in.credential = &credential;
1185 s.in.new_password = &new_password;
1186 s.out.return_authenticator = &return_authenticator;
1188 E_md4hash(password, new_password.hash);
1190 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1191 creds_des_encrypt(creds_state, &new_password);
1192 creds_client_authenticator(creds_state, &credential);
1194 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1195 if (!NT_STATUS_IS_OK(status)) {
1196 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1200 if (!creds_client_check(creds_state,
1201 &s.out.return_authenticator->cred)) {
1202 printf("Credential chaining failed\n");
1205 cli_credentials_set_password(wks_creds, password,
1211 talloc_free(mem_ctx);
1216 * Delete the wks account again
1219 static bool leave(struct smbcli_state *cli,
1220 struct loadparm_context *lp_ctx,
1221 struct cli_credentials *admin_creds,
1222 struct cli_credentials *wks_creds)
1224 char *wks_name = talloc_asprintf(
1225 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1228 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1229 talloc_free(wks_name);
1234 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1237 bool torture_netlogon_samba3(struct torture_context *torture)
1239 TALLOC_CTX *mem_ctx;
1242 struct smbcli_state *cli;
1243 struct cli_credentials *anon_creds;
1244 struct cli_credentials *wks_creds;
1245 const char *wks_name;
1247 struct smbcli_options options;
1248 struct smbcli_session_options session_options;
1250 wks_name = torture_setting_string(torture, "wksname", NULL);
1251 if (wks_name == NULL) {
1252 wks_name = get_myname();
1255 mem_ctx = talloc_init("torture_netlogon_samba3");
1257 if (mem_ctx == NULL) {
1258 d_printf("talloc_init failed\n");
1262 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1263 d_printf("create_anon_creds failed\n");
1267 lp_smbcli_options(torture->lp_ctx, &options);
1268 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1270 status = smbcli_full_connection(mem_ctx, &cli,
1271 torture_setting_string(torture, "host", NULL),
1272 lp_smb_ports(torture->lp_ctx),
1274 lp_socket_options(torture->lp_ctx),
1276 lp_resolve_context(torture->lp_ctx),
1277 torture->ev, &options, &session_options,
1278 lp_iconv_convenience(torture->lp_ctx),
1279 lp_gensec_settings(torture, torture->lp_ctx));
1280 if (!NT_STATUS_IS_OK(status)) {
1281 d_printf("smbcli_full_connection failed: %s\n",
1286 wks_creds = cli_credentials_init(mem_ctx);
1287 if (wks_creds == NULL) {
1288 d_printf("cli_credentials_init failed\n");
1292 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1293 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1294 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1295 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1296 cli_credentials_set_password(wks_creds,
1297 generate_random_str(wks_creds, 8),
1300 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1301 d_printf("join failed\n");
1305 cli_credentials_set_domain(
1306 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1309 for (i=0; i<2; i++) {
1311 /* Do this more than once, the routine "schan" changes
1312 * the workstation password using the netlogon
1313 * password change routine */
1317 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1318 d_printf("auth2 failed\n");
1322 for (j=0; j<2; j++) {
1323 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1324 d_printf("schan failed\n");
1330 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1331 d_printf("leave failed\n");
1338 talloc_free(mem_ctx);
1343 * Do a simple join, testjoin and leave using specified smb and samr
1347 static bool test_join3(struct torture_context *tctx,
1349 struct cli_credentials *smb_creds,
1350 struct cli_credentials *samr_creds,
1351 const char *wks_name)
1355 struct smbcli_state *cli;
1356 struct cli_credentials *wks_creds;
1357 struct smbcli_options options;
1358 struct smbcli_session_options session_options;
1360 lp_smbcli_options(tctx->lp_ctx, &options);
1361 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1363 status = smbcli_full_connection(tctx, &cli,
1364 torture_setting_string(tctx, "host", NULL),
1365 lp_smb_ports(tctx->lp_ctx),
1366 "IPC$", NULL, lp_socket_options(tctx->lp_ctx),
1367 smb_creds, lp_resolve_context(tctx->lp_ctx),
1368 tctx->ev, &options, &session_options,
1369 lp_iconv_convenience(tctx->lp_ctx),
1370 lp_gensec_settings(tctx, tctx->lp_ctx));
1371 if (!NT_STATUS_IS_OK(status)) {
1372 d_printf("smbcli_full_connection failed: %s\n",
1377 wks_creds = cli_credentials_init(cli);
1378 if (wks_creds == NULL) {
1379 d_printf("cli_credentials_init failed\n");
1383 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1384 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1385 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1386 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1387 cli_credentials_set_password(wks_creds,
1388 generate_random_str(wks_creds, 8),
1391 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1392 d_printf("join failed\n");
1396 cli_credentials_set_domain(
1397 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1400 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1401 d_printf("auth2 failed\n");
1405 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1406 d_printf("leave failed\n");
1419 * Test the different session key variants. Do it by joining, this uses the
1420 * session key in the setpassword routine. Test the join by doing the auth2.
1423 bool torture_samba3_sessionkey(struct torture_context *torture)
1426 struct cli_credentials *anon_creds;
1427 const char *wks_name;
1429 wks_name = torture_setting_string(torture, "wksname", get_myname());
1431 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1432 d_printf("create_anon_creds failed\n");
1436 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1440 if (!torture_setting_bool(torture, "samba3", false)) {
1442 /* Samba3 in the build farm right now does this happily. Need
1445 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1446 d_printf("join using anonymous bind on an anonymous smb "
1447 "connection succeeded -- HUH??\n");
1452 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1454 d_printf("join using ntlmssp bind on an anonymous smb "
1455 "connection failed\n");
1459 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1460 d_printf("join using anonymous bind on an authenticated smb "
1461 "connection failed\n");
1465 if (!test_join3(torture, false, cmdline_credentials,
1466 cmdline_credentials,
1468 d_printf("join using ntlmssp bind on an authenticated smb "
1469 "connection failed\n");
1474 * The following two are tests for setuserinfolevel 25
1477 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1479 d_printf("join using ntlmssp bind on an anonymous smb "
1480 "connection failed\n");
1484 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1485 d_printf("join using anonymous bind on an authenticated smb "
1486 "connection failed\n");
1496 * open pipe and bind, given an IPC$ context
1499 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1500 struct loadparm_context *lp_ctx,
1501 struct smbcli_tree *tree,
1502 const char *pipe_name,
1503 const struct ndr_interface_table *iface,
1504 struct dcerpc_pipe **p)
1506 struct dcerpc_pipe *result;
1509 if (!(result = dcerpc_pipe_init(
1510 mem_ctx, tree->session->transport->socket->event.ctx,
1511 lp_iconv_convenience(lp_ctx)))) {
1512 return NT_STATUS_NO_MEMORY;
1515 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1519 talloc_free(result);
1523 status = dcerpc_bind_auth_none(result, iface);
1524 if (!NT_STATUS_IS_OK(status)) {
1525 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1526 talloc_free(result);
1531 return NT_STATUS_OK;
1535 * Sane wrapper around lsa_LookupNames
1538 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1539 struct dcerpc_pipe *p,
1543 struct lsa_ObjectAttribute attr;
1544 struct lsa_QosInfo qos;
1545 struct lsa_OpenPolicy2 r;
1548 struct policy_handle handle;
1549 struct lsa_LookupNames l;
1550 struct lsa_TransSidArray sids;
1551 struct lsa_RefDomainList *domains = NULL;
1552 struct lsa_String lsa_name;
1554 struct dom_sid *result;
1555 TALLOC_CTX *tmp_ctx;
1557 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1562 qos.impersonation_level = 2;
1563 qos.context_mode = 1;
1564 qos.effective_only = 0;
1567 attr.root_dir = NULL;
1568 attr.object_name = NULL;
1569 attr.attributes = 0;
1570 attr.sec_desc = NULL;
1571 attr.sec_qos = &qos;
1573 r.in.system_name = "\\";
1575 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1576 r.out.handle = &handle;
1578 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1581 talloc_free(tmp_ctx);
1588 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1590 l.in.handle = &handle;
1592 l.in.names = &lsa_name;
1595 l.in.count = &count;
1596 l.out.count = &count;
1598 l.out.domains = &domains;
1600 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1601 if (!NT_STATUS_IS_OK(status)) {
1602 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1604 talloc_free(tmp_ctx);
1608 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1609 l.out.sids->sids[0].rid);
1611 c.in.handle = &handle;
1612 c.out.handle = &handle;
1614 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1615 if (!NT_STATUS_IS_OK(status)) {
1616 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1617 talloc_free(tmp_ctx);
1621 talloc_free(tmp_ctx);
1626 * Find out the user SID on this connection
1629 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1630 struct loadparm_context *lp_ctx,
1631 struct smbcli_tree *tree)
1633 struct dcerpc_pipe *lsa;
1634 struct lsa_GetUserName r;
1636 struct lsa_String *authority_name_p = NULL;
1637 struct lsa_String *account_name_p = NULL;
1638 struct dom_sid *result;
1640 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1641 &ndr_table_lsarpc, &lsa);
1642 if (!NT_STATUS_IS_OK(status)) {
1643 d_printf("(%s) Could not bind to LSA: %s\n",
1644 __location__, nt_errstr(status));
1648 r.in.system_name = "\\";
1649 r.in.account_name = &account_name_p;
1650 r.in.authority_name = &authority_name_p;
1651 r.out.account_name = &account_name_p;
1653 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1655 authority_name_p = *r.out.authority_name;
1657 if (!NT_STATUS_IS_OK(status)) {
1658 printf("(%s) GetUserName failed - %s\n",
1659 __location__, nt_errstr(status));
1664 result = name2sid(mem_ctx, lsa, account_name_p->string,
1665 authority_name_p->string);
1671 static int destroy_tree(struct smbcli_tree *tree)
1673 smb_tree_disconnect(tree);
1678 * Do a tcon, given a session
1681 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1682 struct smbcli_session *session,
1683 const char *sharename,
1684 struct smbcli_tree **res)
1686 struct smbcli_tree *result;
1687 TALLOC_CTX *tmp_ctx;
1688 union smb_tcon tcon;
1691 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1692 return NT_STATUS_NO_MEMORY;
1695 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1696 talloc_free(tmp_ctx);
1697 return NT_STATUS_NO_MEMORY;
1700 tcon.generic.level = RAW_TCON_TCONX;
1701 tcon.tconx.in.flags = 0;
1702 tcon.tconx.in.password = data_blob(NULL, 0);
1703 tcon.tconx.in.path = sharename;
1704 tcon.tconx.in.device = "?????";
1706 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1707 if (!NT_STATUS_IS_OK(status)) {
1708 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1710 talloc_free(tmp_ctx);
1714 result->tid = tcon.tconx.out.tid;
1715 result = talloc_steal(mem_ctx, result);
1716 talloc_set_destructor(result, destroy_tree);
1717 talloc_free(tmp_ctx);
1719 return NT_STATUS_OK;
1723 * Test the getusername behaviour
1726 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1729 struct smbcli_state *cli;
1730 TALLOC_CTX *mem_ctx;
1732 struct dom_sid *user_sid;
1733 struct dom_sid *created_sid;
1734 struct cli_credentials *anon_creds;
1735 struct cli_credentials *user_creds;
1737 struct smbcli_options options;
1738 struct smbcli_session_options session_options;
1740 if (!(mem_ctx = talloc_new(torture))) {
1744 lp_smbcli_options(torture->lp_ctx, &options);
1745 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1747 status = smbcli_full_connection(
1748 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1749 lp_smb_ports(torture->lp_ctx),
1750 "IPC$", NULL, lp_socket_options(torture->lp_ctx), cmdline_credentials,
1751 lp_resolve_context(torture->lp_ctx),
1752 torture->ev, &options, &session_options,
1753 lp_iconv_convenience(torture->lp_ctx),
1754 lp_gensec_settings(torture, torture->lp_ctx));
1755 if (!NT_STATUS_IS_OK(status)) {
1756 d_printf("(%s) smbcli_full_connection failed: %s\n",
1757 __location__, nt_errstr(status));
1762 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1763 d_printf("(%s) whoami on auth'ed connection failed\n",
1770 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1771 d_printf("(%s) create_anon_creds failed\n", __location__);
1776 status = smbcli_full_connection(
1777 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1778 lp_smb_ports(torture->lp_ctx), "IPC$", NULL,
1779 lp_socket_options(torture->lp_ctx), anon_creds,
1780 lp_resolve_context(torture->lp_ctx),
1781 torture->ev, &options, &session_options,
1782 lp_iconv_convenience(torture->lp_ctx),
1783 lp_gensec_settings(torture, torture->lp_ctx));
1784 if (!NT_STATUS_IS_OK(status)) {
1785 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1786 __location__, nt_errstr(status));
1791 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1792 d_printf("(%s) whoami on anon connection failed\n",
1798 if (!dom_sid_equal(user_sid,
1799 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1800 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1801 "S-1-5-7", __location__,
1802 dom_sid_string(mem_ctx, user_sid));
1806 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1807 d_printf("(%s) cli_credentials_init failed\n", __location__);
1812 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1813 cli_credentials_set_username(user_creds, "torture_username",
1815 cli_credentials_set_password(user_creds,
1816 generate_random_str(user_creds, 8),
1819 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1820 cli_credentials_get_username(user_creds),
1821 cli_credentials_get_password(user_creds),
1822 &domain_name, &created_sid)) {
1823 d_printf("(%s) create_user failed\n", __location__);
1828 cli_credentials_set_domain(user_creds, domain_name,
1832 struct smbcli_session *session2;
1833 struct smb_composite_sesssetup setup;
1834 struct smbcli_tree *tree;
1836 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1837 if (session2 == NULL) {
1838 d_printf("(%s) smbcli_session_init failed\n",
1843 setup.in.sesskey = cli->transport->negotiate.sesskey;
1844 setup.in.capabilities = cli->transport->negotiate.capabilities;
1845 setup.in.workgroup = "";
1846 setup.in.credentials = user_creds;
1847 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
1849 status = smb_composite_sesssetup(session2, &setup);
1850 if (!NT_STATUS_IS_OK(status)) {
1851 d_printf("(%s) session setup with new user failed: "
1852 "%s\n", __location__, nt_errstr(status));
1856 session2->vuid = setup.out.vuid;
1858 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1860 d_printf("(%s) secondary_tcon failed\n",
1866 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1867 d_printf("(%s) whoami on user connection failed\n",
1876 d_printf("Created %s, found %s\n",
1877 dom_sid_string(mem_ctx, created_sid),
1878 dom_sid_string(mem_ctx, user_sid));
1880 if (!dom_sid_equal(created_sid, user_sid)) {
1885 if (!delete_user(cli, torture->lp_ctx,
1886 cmdline_credentials,
1887 cli_credentials_get_username(user_creds))) {
1888 d_printf("(%s) delete_user failed\n", __location__);
1893 talloc_free(mem_ctx);
1897 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1898 const char *sharename)
1901 struct srvsvc_NetShareGetInfo r;
1902 union srvsvc_NetShareInfo info;
1903 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1907 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1908 dcerpc_server_name(p));
1909 r.in.share_name = sharename;
1912 for (i=0;i<ARRAY_SIZE(levels);i++) {
1913 r.in.level = levels[i];
1915 printf("testing NetShareGetInfo level %u on share '%s'\n",
1916 r.in.level, r.in.share_name);
1918 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1919 if (!NT_STATUS_IS_OK(status)) {
1920 printf("NetShareGetInfo level %u on share '%s' failed"
1921 " - %s\n", r.in.level, r.in.share_name,
1926 if (!W_ERROR_IS_OK(r.out.result)) {
1927 printf("NetShareGetInfo level %u on share '%s' failed "
1928 "- %s\n", r.in.level, r.in.share_name,
1929 win_errstr(r.out.result));
1938 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1939 const char **one_sharename)
1942 struct srvsvc_NetShareEnum r;
1943 struct srvsvc_NetShareInfoCtr info_ctr;
1944 struct srvsvc_NetShareCtr0 c0;
1945 struct srvsvc_NetShareCtr1 c1;
1946 struct srvsvc_NetShareCtr2 c2;
1947 struct srvsvc_NetShareCtr501 c501;
1948 struct srvsvc_NetShareCtr502 c502;
1949 struct srvsvc_NetShareCtr1004 c1004;
1950 struct srvsvc_NetShareCtr1005 c1005;
1951 struct srvsvc_NetShareCtr1006 c1006;
1952 struct srvsvc_NetShareCtr1007 c1007;
1953 uint32_t totalentries = 0;
1954 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1958 ZERO_STRUCT(info_ctr);
1960 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1961 r.in.info_ctr = &info_ctr;
1962 r.in.max_buffer = (uint32_t)-1;
1963 r.in.resume_handle = NULL;
1964 r.out.totalentries = &totalentries;
1965 r.out.info_ctr = &info_ctr;
1967 for (i=0;i<ARRAY_SIZE(levels);i++) {
1968 info_ctr.level = levels[i];
1970 switch (info_ctr.level) {
1973 info_ctr.ctr.ctr0 = &c0;
1977 info_ctr.ctr.ctr1 = &c1;
1981 info_ctr.ctr.ctr2 = &c2;
1985 info_ctr.ctr.ctr501 = &c501;
1989 info_ctr.ctr.ctr502 = &c502;
1993 info_ctr.ctr.ctr1004 = &c1004;
1997 info_ctr.ctr.ctr1005 = &c1005;
2001 info_ctr.ctr.ctr1006 = &c1006;
2005 info_ctr.ctr.ctr1007 = &c1007;
2009 printf("testing NetShareEnum level %u\n", info_ctr.level);
2010 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2011 if (!NT_STATUS_IS_OK(status)) {
2012 printf("NetShareEnum level %u failed - %s\n",
2013 info_ctr.level, nt_errstr(status));
2017 if (!W_ERROR_IS_OK(r.out.result)) {
2018 printf("NetShareEnum level %u failed - %s\n",
2019 info_ctr.level, win_errstr(r.out.result));
2022 if (info_ctr.level == 0) {
2023 struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
2024 if (ctr->count > 0) {
2025 *one_sharename = ctr->array[0].name;
2033 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
2035 struct dcerpc_pipe *p;
2036 TALLOC_CTX *mem_ctx;
2038 const char *sharename = NULL;
2039 struct smbcli_state *cli;
2042 if (!(mem_ctx = talloc_new(torture))) {
2046 if (!(torture_open_connection_share(
2047 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2048 "IPC$", torture->ev))) {
2049 talloc_free(mem_ctx);
2053 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
2054 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
2055 if (!NT_STATUS_IS_OK(status)) {
2056 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2057 __location__, nt_errstr(status));
2062 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
2063 if (sharename == NULL) {
2064 printf("did not get sharename\n");
2066 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
2070 talloc_free(mem_ctx);
2075 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
2076 * NT_STATUS_NO_SAM_ACCOUNT
2079 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
2081 TALLOC_CTX *mem_ctx;
2082 struct dcerpc_pipe *net_pipe;
2084 bool result = false;
2086 struct netr_ServerReqChallenge r;
2087 struct netr_Credential netr_cli_creds;
2088 struct netr_Credential netr_srv_creds;
2089 uint32_t negotiate_flags;
2090 struct netr_ServerAuthenticate2 a;
2091 struct creds_CredentialState *creds_state;
2092 struct netr_Credential netr_cred;
2093 struct samr_Password mach_pw;
2094 struct smbcli_state *cli;
2096 if (!(mem_ctx = talloc_new(torture))) {
2097 d_printf("talloc_new failed\n");
2101 if (!(wksname = generate_random_str_list(
2102 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2103 d_printf("generate_random_str_list failed\n");
2107 if (!(torture_open_connection_share(
2109 torture, torture_setting_string(torture, "host", NULL),
2110 "IPC$", torture->ev))) {
2111 d_printf("IPC$ connection failed\n");
2115 if (!(net_pipe = dcerpc_pipe_init(
2116 mem_ctx, cli->transport->socket->event.ctx,
2117 lp_iconv_convenience(torture->lp_ctx)))) {
2118 d_printf("dcerpc_pipe_init failed\n");
2122 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2123 if (!NT_STATUS_IS_OK(status)) {
2124 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2129 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2130 if (!NT_STATUS_IS_OK(status)) {
2131 d_printf("dcerpc_bind_auth_none failed: %s\n",
2136 r.in.computer_name = wksname;
2137 r.in.server_name = talloc_asprintf(
2138 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2139 if (r.in.server_name == NULL) {
2140 d_printf("talloc_asprintf failed\n");
2143 generate_random_buffer(netr_cli_creds.data,
2144 sizeof(netr_cli_creds.data));
2145 r.in.credentials = &netr_cli_creds;
2146 r.out.return_credentials = &netr_srv_creds;
2148 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2149 if (!NT_STATUS_IS_OK(status)) {
2150 d_printf("netr_ServerReqChallenge failed: %s\n",
2155 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2156 E_md4hash("foobar", mach_pw.hash);
2158 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2159 creds_client_init(creds_state, r.in.credentials,
2160 r.out.return_credentials, &mach_pw,
2161 &netr_cred, negotiate_flags);
2163 a.in.server_name = talloc_asprintf(
2164 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2165 a.in.account_name = talloc_asprintf(
2166 mem_ctx, "%s$", wksname);
2167 a.in.computer_name = wksname;
2168 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2169 a.in.negotiate_flags = &negotiate_flags;
2170 a.out.negotiate_flags = &negotiate_flags;
2171 a.in.credentials = &netr_cred;
2172 a.out.return_credentials = &netr_cred;
2174 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2176 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2177 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2178 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2185 talloc_free(mem_ctx);
2189 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2190 struct loadparm_context *lp_ctx,
2191 struct smbcli_session *sess,
2192 const char *sharename)
2194 struct smbcli_tree *tree;
2195 TALLOC_CTX *tmp_ctx;
2196 struct dcerpc_pipe *p;
2198 struct srvsvc_NetShareGetInfo r;
2199 union srvsvc_NetShareInfo info;
2200 struct security_descriptor *result;
2202 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2203 d_printf("talloc_new failed\n");
2207 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2208 d_printf("secondary_tcon failed\n");
2209 talloc_free(tmp_ctx);
2213 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2214 &ndr_table_srvsvc, &p);
2215 if (!NT_STATUS_IS_OK(status)) {
2216 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2217 __location__, nt_errstr(status));
2218 talloc_free(tmp_ctx);
2223 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2226 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2227 dcerpc_server_name(p));
2228 r.in.share_name = sharename;
2232 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2233 if (!NT_STATUS_IS_OK(status)) {
2234 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2236 talloc_free(tmp_ctx);
2240 result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
2241 talloc_free(tmp_ctx);
2245 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2246 struct loadparm_context *lp_ctx,
2247 struct smbcli_session *sess,
2248 const char *sharename,
2249 struct security_descriptor *sd)
2251 struct smbcli_tree *tree;
2252 TALLOC_CTX *tmp_ctx;
2253 struct dcerpc_pipe *p;
2255 struct sec_desc_buf i;
2256 struct srvsvc_NetShareSetInfo r;
2257 union srvsvc_NetShareInfo info;
2260 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2261 d_printf("talloc_new failed\n");
2262 return NT_STATUS_NO_MEMORY;
2265 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2266 d_printf("secondary_tcon failed\n");
2267 talloc_free(tmp_ctx);
2268 return NT_STATUS_UNSUCCESSFUL;
2271 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2272 &ndr_table_srvsvc, &p);
2273 if (!NT_STATUS_IS_OK(status)) {
2274 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2275 __location__, nt_errstr(status));
2276 talloc_free(tmp_ctx);
2277 return NT_STATUS_UNSUCCESSFUL;
2281 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2284 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2285 dcerpc_server_name(p));
2286 r.in.share_name = sharename;
2291 r.in.parm_error = &error;
2293 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2294 if (!NT_STATUS_IS_OK(status)) {
2295 d_printf("srvsvc_NetShareSetInfo failed: %s\n",
2299 talloc_free(tmp_ctx);
2303 bool try_tcon(TALLOC_CTX *mem_ctx,
2304 struct loadparm_context *lp_ctx,
2305 struct security_descriptor *orig_sd,
2306 struct smbcli_session *session,
2307 const char *sharename, const struct dom_sid *user_sid,
2308 unsigned int access_mask, NTSTATUS expected_tcon,
2309 NTSTATUS expected_mkdir)
2311 TALLOC_CTX *tmp_ctx;
2312 struct smbcli_tree *rmdir_tree, *tree;
2313 struct dom_sid *domain_sid;
2315 struct security_descriptor *sd;
2319 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2320 d_printf("talloc_new failed\n");
2324 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2325 if (!NT_STATUS_IS_OK(status)) {
2326 d_printf("first tcon to delete dir failed\n");
2327 talloc_free(tmp_ctx);
2331 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2333 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2334 &domain_sid, &rid))) {
2335 d_printf("dom_sid_split_rid failed\n");
2336 talloc_free(tmp_ctx);
2340 sd = security_descriptor_dacl_create(
2341 tmp_ctx, 0, "S-1-5-32-544",
2342 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2344 dom_sid_string(mem_ctx, user_sid),
2345 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2347 d_printf("security_descriptor_dacl_create failed\n");
2348 talloc_free(tmp_ctx);
2352 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2353 if (!NT_STATUS_IS_OK(status)) {
2354 d_printf("custom set_sharesec failed: %s\n",
2356 talloc_free(tmp_ctx);
2360 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2361 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2362 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2368 if (!NT_STATUS_IS_OK(status)) {
2369 /* An expected non-access, no point in trying to write */
2373 status = smbcli_mkdir(tree, "sharesec_testdir");
2374 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2375 d_printf("(%s) Expected %s, got %s\n", __location__,
2376 nt_errstr(expected_mkdir), nt_errstr(status));
2381 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2383 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2384 if (!NT_STATUS_IS_OK(status)) {
2385 d_printf("custom set_sharesec failed: %s\n",
2387 talloc_free(tmp_ctx);
2391 talloc_free(tmp_ctx);
2395 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2397 TALLOC_CTX *mem_ctx;
2399 struct smbcli_state *cli;
2400 struct security_descriptor *sd;
2401 struct dom_sid *user_sid;
2403 if (!(mem_ctx = talloc_new(torture))) {
2407 if (!(torture_open_connection_share(
2408 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2409 "IPC$", torture->ev))) {
2410 d_printf("IPC$ connection failed\n");
2411 talloc_free(mem_ctx);
2415 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2416 d_printf("whoami failed\n");
2417 talloc_free(mem_ctx);
2421 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2422 torture_setting_string(torture, "share", NULL));
2424 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2425 torture_setting_string(torture, "share", NULL),
2426 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2428 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2429 torture_setting_string(torture, "share", NULL),
2430 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2431 NT_STATUS_MEDIA_WRITE_PROTECTED);
2433 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2434 torture_setting_string(torture, "share", NULL),
2435 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2437 talloc_free(mem_ctx);
2441 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2443 TALLOC_CTX *mem_ctx;
2445 struct smbcli_state *cli;
2446 struct dcerpc_pipe *p;
2447 struct policy_handle lsa_handle;
2449 struct dom_sid *domain_sid;
2451 if (!(mem_ctx = talloc_new(torture))) {
2455 if (!(torture_open_connection_share(
2456 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2457 "IPC$", torture->ev))) {
2458 d_printf("IPC$ connection failed\n");
2459 talloc_free(mem_ctx);
2463 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2464 &ndr_table_lsarpc, &p);
2465 if (!NT_STATUS_IS_OK(status)) {
2466 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2468 talloc_free(mem_ctx);
2473 struct lsa_ObjectAttribute attr;
2474 struct lsa_OpenPolicy2 o;
2475 o.in.system_name = talloc_asprintf(
2476 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2479 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2480 o.out.handle = &lsa_handle;
2481 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2482 if (!NT_STATUS_IS_OK(status)) {
2483 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2484 __location__, nt_errstr(status));
2485 talloc_free(mem_ctx);
2491 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2496 int levels[] = { 2,3,5,6 };
2498 for (i=0; i<ARRAY_SIZE(levels); i++) {
2499 struct lsa_QueryInfoPolicy r;
2500 union lsa_PolicyInformation *info = NULL;
2501 r.in.handle = &lsa_handle;
2502 r.in.level = levels[i];
2504 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2505 if (!NT_STATUS_IS_OK(status)) {
2506 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2507 "failed: %s\n", __location__,
2508 levels[i], nt_errstr(status));
2509 talloc_free(mem_ctx);
2512 if (levels[i] == 5) {
2513 domain_sid = info->account_domain.sid;
2521 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2522 struct smb_iconv_convenience *iconv_convenience,
2525 struct rap_WserverGetInfo r;
2527 char servername[17];
2530 r.in.bufsize = 0xffff;
2532 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2533 if (!NT_STATUS_IS_OK(status)) {
2537 memcpy(servername, r.out.info.info0.name, 16);
2538 servername[16] = '\0';
2540 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2541 return NT_STATUS_NO_MEMORY;
2544 return NT_STATUS_OK;
2548 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2549 struct smbcli_tree *tree,
2550 const char ***printers, int *num_printers)
2552 TALLOC_CTX *mem_ctx;
2554 struct dcerpc_pipe *p;
2555 struct srvsvc_NetShareEnum r;
2556 struct srvsvc_NetShareInfoCtr info_ctr;
2557 struct srvsvc_NetShareCtr1 c1_in;
2558 struct srvsvc_NetShareCtr1 *c1;
2559 uint32_t totalentries = 0;
2562 mem_ctx = talloc_new(ctx);
2563 if (mem_ctx == NULL) {
2564 return NT_STATUS_NO_MEMORY;
2567 status = pipe_bind_smb(mem_ctx, lp_ctx,
2568 tree, "\\srvsvc", &ndr_table_srvsvc,
2570 if (!NT_STATUS_IS_OK(status)) {
2571 d_printf("could not bind to srvsvc pipe\n");
2572 talloc_free(mem_ctx);
2578 info_ctr.ctr.ctr1 = &c1_in;
2580 r.in.server_unc = talloc_asprintf(
2581 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2582 r.in.info_ctr = &info_ctr;
2583 r.in.max_buffer = (uint32_t)-1;
2584 r.in.resume_handle = NULL;
2585 r.out.totalentries = &totalentries;
2586 r.out.info_ctr = &info_ctr;
2588 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2589 if (!NT_STATUS_IS_OK(status)) {
2590 d_printf("NetShareEnum level %u failed - %s\n",
2591 info_ctr.level, nt_errstr(status));
2592 talloc_free(mem_ctx);
2598 c1 = r.out.info_ctr->ctr.ctr1;
2599 for (i=0; i<c1->count; i++) {
2600 if (c1->array[i].type != STYPE_PRINTQ) {
2603 if (!add_string_to_array(ctx, c1->array[i].name,
2604 printers, num_printers)) {
2606 return NT_STATUS_NO_MEMORY;
2610 talloc_free(mem_ctx);
2611 return NT_STATUS_OK;
2614 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2615 const char *servername, int level, int *num_printers)
2617 struct spoolss_EnumPrinters r;
2621 r.in.flags = PRINTER_ENUM_LOCAL;
2622 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2627 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2628 if (!NT_STATUS_IS_OK(status)) {
2629 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2630 __location__, nt_errstr(status));
2634 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2635 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2636 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2637 win_errstr(r.out.result));
2641 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2642 if (blob.data == NULL) {
2643 d_printf("(%s) data_blob_talloc failed\n", __location__);
2647 r.in.buffer = &blob;
2648 r.in.offered = r.out.needed;
2650 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2651 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2652 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2653 "%s\n", __location__, nt_errstr(status),
2654 win_errstr(r.out.result));
2658 *num_printers = r.out.count;
2663 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2664 struct policy_handle *handle, int level,
2665 union spoolss_PrinterInfo **res)
2667 TALLOC_CTX *mem_ctx;
2668 struct spoolss_GetPrinter r;
2672 mem_ctx = talloc_new(ctx);
2673 if (mem_ctx == NULL) {
2674 return NT_STATUS_NO_MEMORY;
2677 r.in.handle = handle;
2682 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2683 if (!NT_STATUS_IS_OK(status)) {
2684 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2685 __location__, nt_errstr(status));
2686 talloc_free(mem_ctx);
2690 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2691 printf("GetPrinter unexpected return code %s, should "
2692 "be WERR_INSUFFICIENT_BUFFER\n",
2693 win_errstr(r.out.result));
2694 talloc_free(mem_ctx);
2695 return NT_STATUS_UNSUCCESSFUL;
2698 r.in.handle = handle;
2700 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2701 if (blob.data == NULL) {
2702 talloc_free(mem_ctx);
2703 return NT_STATUS_NO_MEMORY;
2705 memset(blob.data, 0, blob.length);
2706 r.in.buffer = &blob;
2707 r.in.offered = r.out.needed;
2709 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2710 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2711 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2712 "%s\n", __location__, nt_errstr(status),
2713 win_errstr(r.out.result));
2714 talloc_free(mem_ctx);
2715 return NT_STATUS_IS_OK(status) ?
2716 NT_STATUS_UNSUCCESSFUL : status;
2720 *res = talloc_steal(ctx, r.out.info);
2723 talloc_free(mem_ctx);
2724 return NT_STATUS_OK;
2727 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2729 TALLOC_CTX *mem_ctx;
2731 struct smbcli_state *cli;
2732 struct dcerpc_pipe *p;
2734 struct policy_handle server_handle, printer_handle;
2735 const char **printers;
2737 struct spoolss_UserLevel1 userlevel1;
2740 if (!(mem_ctx = talloc_new(torture))) {
2744 if (!(torture_open_connection_share(
2745 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2746 "IPC$", torture->ev))) {
2747 d_printf("IPC$ connection failed\n");
2748 talloc_free(mem_ctx);
2752 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2753 if (!NT_STATUS_IS_OK(status)) {
2754 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2755 __location__, nt_errstr(status));
2756 talloc_free(mem_ctx);
2760 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2761 &printers, &num_printers))) {
2762 talloc_free(mem_ctx);
2766 if (num_printers == 0) {
2767 d_printf("Did not find printers\n");
2768 talloc_free(mem_ctx);
2772 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2773 &ndr_table_spoolss, &p);
2774 if (!NT_STATUS_IS_OK(status)) {
2775 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2777 talloc_free(mem_ctx);
2781 ZERO_STRUCT(userlevel1);
2782 userlevel1.client = talloc_asprintf(
2783 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2784 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2785 userlevel1.build = 2600;
2786 userlevel1.major = 3;
2787 userlevel1.minor = 0;
2788 userlevel1.processor = 0;
2791 struct spoolss_OpenPrinterEx r;
2794 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2796 r.in.datatype = NULL;
2797 r.in.access_mask = 0;
2799 r.in.userlevel.level1 = &userlevel1;
2800 r.out.handle = &server_handle;
2802 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2803 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2804 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2805 "%s, %s\n", __location__, nt_errstr(status),
2806 win_errstr(r.out.result));
2807 talloc_free(mem_ctx);
2813 struct spoolss_ClosePrinter r;
2815 r.in.handle = &server_handle;
2816 r.out.handle = &server_handle;
2818 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2819 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2820 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2821 "%s, %s\n", __location__, nt_errstr(status),
2822 win_errstr(r.out.result));
2823 talloc_free(mem_ctx);
2829 struct spoolss_OpenPrinterEx r;
2832 r.in.printername = talloc_asprintf(
2833 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2834 r.in.datatype = NULL;
2835 r.in.access_mask = 0;
2837 r.in.userlevel.level1 = &userlevel1;
2838 r.out.handle = &printer_handle;
2840 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2841 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2842 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2843 "%s, %s\n", __location__, nt_errstr(status),
2844 win_errstr(r.out.result));
2845 talloc_free(mem_ctx);
2853 for (i=0; i<8; i++) {
2854 status = getprinterinfo(mem_ctx, p, &printer_handle,
2856 if (!NT_STATUS_IS_OK(status)) {
2857 d_printf("(%s) getprinterinfo %d failed: %s\n",
2858 __location__, i, nt_errstr(status));
2865 struct spoolss_ClosePrinter r;
2867 r.in.handle = &printer_handle;
2868 r.out.handle = &printer_handle;
2870 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2871 if (!NT_STATUS_IS_OK(status)) {
2872 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2873 "%s\n", __location__, nt_errstr(status));
2874 talloc_free(mem_ctx);
2881 if (!enumprinters(mem_ctx, p, servername, 1,
2883 d_printf("(%s) enumprinters failed\n", __location__);
2884 talloc_free(mem_ctx);
2887 if (num_printers != num_enumerated) {
2888 d_printf("(%s) netshareenum gave %d printers, "
2889 "enumprinters lvl 1 gave %d\n", __location__,
2890 num_printers, num_enumerated);
2891 talloc_free(mem_ctx);
2898 if (!enumprinters(mem_ctx, p, servername, 2,
2900 d_printf("(%s) enumprinters failed\n", __location__);
2901 talloc_free(mem_ctx);
2904 if (num_printers != num_enumerated) {
2905 d_printf("(%s) netshareenum gave %d printers, "
2906 "enumprinters lvl 2 gave %d\n", __location__,
2907 num_printers, num_enumerated);
2908 talloc_free(mem_ctx);
2913 talloc_free(mem_ctx);
2918 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2920 TALLOC_CTX *mem_ctx;
2921 struct smbcli_state *cli;
2922 struct dcerpc_pipe *p;
2926 if (!(mem_ctx = talloc_new(torture))) {
2930 if (!(torture_open_connection_share(
2931 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2932 "IPC$", torture->ev))) {
2933 d_printf("IPC$ connection failed\n");
2934 talloc_free(mem_ctx);
2938 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2939 if (!NT_STATUS_IS_OK(status)) {
2940 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2941 __location__, nt_errstr(status));
2942 talloc_free(mem_ctx);
2946 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2947 &ndr_table_wkssvc, &p);
2948 if (!NT_STATUS_IS_OK(status)) {
2949 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2951 talloc_free(mem_ctx);
2956 struct wkssvc_NetWkstaInfo100 wks100;
2957 union wkssvc_NetWkstaInfo info;
2958 struct wkssvc_NetWkstaGetInfo r;
2960 r.in.server_name = "\\foo";
2962 info.info100 = &wks100;
2965 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2966 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2967 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2968 "%s, %s\n", __location__, nt_errstr(status),
2969 win_errstr(r.out.result));
2970 talloc_free(mem_ctx);
2974 if (strcmp(servername,
2975 r.out.info->info100->server_name) != 0) {
2976 d_printf("(%s) servername inconsistency: RAP=%s, "
2977 "dcerpc_wkssvc_NetWksGetInfo=%s",
2978 __location__, servername,
2979 r.out.info->info100->server_name);
2980 talloc_free(mem_ctx);
2985 talloc_free(mem_ctx);
2989 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2990 struct policy_handle *handle)
2992 struct winreg_CloseKey c;
2994 TALLOC_CTX *mem_ctx;
2996 c.in.handle = c.out.handle = handle;
2998 if (!(mem_ctx = talloc_new(p))) {
2999 return NT_STATUS_NO_MEMORY;
3002 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
3003 talloc_free(mem_ctx);
3005 if (!NT_STATUS_IS_OK(status)) {
3009 if (!W_ERROR_IS_OK(c.out.result)) {
3010 return werror_to_ntstatus(c.out.result);
3013 return NT_STATUS_OK;
3016 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
3017 TALLOC_CTX *mem_ctx)
3019 uint32_t enum_index = 0;
3022 struct winreg_EnumValue r;
3023 struct winreg_StringBuf name;
3024 enum winreg_Type type = 0;
3027 uint32_t size, length;
3029 r.in.handle = handle;
3030 r.in.enum_index = enum_index;
3033 r.in.name = r.out.name = &name;
3039 r.in.length = &length;
3041 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
3042 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3043 return NT_STATUS_OK;
3049 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
3050 TALLOC_CTX *mem_ctx, int depth)
3052 struct winreg_EnumKey r;
3053 struct winreg_StringBuf class, name;
3058 return NT_STATUS_OK;
3064 r.in.handle = handle;
3065 r.in.enum_index = 0;
3067 r.in.keyclass = &class;
3069 r.in.last_changed_time = &t;
3072 TALLOC_CTX *tmp_ctx;
3073 struct winreg_OpenKey o;
3074 struct policy_handle key_handle;
3077 if (!(tmp_ctx = talloc_new(mem_ctx))) {
3078 return NT_STATUS_NO_MEMORY;
3084 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
3085 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3086 /* We're done enumerating */
3087 talloc_free(tmp_ctx);
3088 return NT_STATUS_OK;
3091 for (i=0; i<10-depth; i++)
3093 printf("%s\n", r.out.name->name);
3096 o.in.parent_handle = handle;
3097 o.in.keyname.name = r.out.name->name;
3099 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3100 o.out.handle = &key_handle;
3102 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
3103 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3104 enumkeys(p, &key_handle, tmp_ctx, depth-1);
3105 enumvalues(p, &key_handle, tmp_ctx);
3106 status = winreg_close(p, &key_handle);
3107 if (!NT_STATUS_IS_OK(status)) {
3112 talloc_free(tmp_ctx);
3114 r.in.enum_index += 1;
3117 return NT_STATUS_OK;
3120 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3122 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3123 const char *name, winreg_open_fn open_fn)
3125 struct policy_handle handle;
3126 struct winreg_OpenHKLM r;
3129 r.in.system_name = 0;
3130 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3131 r.out.handle = &handle;
3133 status = open_fn(p, mem_ctx, &r);
3134 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3135 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3136 nt_errstr(status), win_errstr(r.out.result));
3140 enumkeys(p, &handle, mem_ctx, 4);
3142 status = winreg_close(p, &handle);
3143 if (!NT_STATUS_IS_OK(status)) {
3144 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3145 __location__, nt_errstr(status));
3152 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3155 struct dcerpc_pipe *p;
3156 TALLOC_CTX *mem_ctx;
3162 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3163 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3164 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3165 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3166 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3171 mem_ctx = talloc_init("torture_rpc_winreg");
3173 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3175 if (!NT_STATUS_IS_OK(status)) {
3176 talloc_free(mem_ctx);
3181 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3183 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3184 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3189 talloc_free(mem_ctx);
3194 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3195 struct loadparm_context *lp_ctx,
3196 struct smbcli_state *cli,
3198 struct srvsvc_NetShareInfo502 **info502)
3200 struct smbcli_tree *ipc;
3201 struct dcerpc_pipe *p;
3202 struct srvsvc_NetShareGetInfo r;
3203 union srvsvc_NetShareInfo info;
3206 if (!(p = dcerpc_pipe_init(cli,
3207 cli->transport->socket->event.ctx,
3208 lp_iconv_convenience(lp_ctx)))) {
3209 status = NT_STATUS_NO_MEMORY;
3213 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3214 if (!NT_STATUS_IS_OK(status)) {
3218 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3219 if (!NT_STATUS_IS_OK(status)) {
3220 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3225 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3226 if (!NT_STATUS_IS_OK(status)) {
3227 d_printf("dcerpc_bind_auth_none failed: %s\n",
3232 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3233 dcerpc_server_name(p));
3234 r.in.share_name = share;
3238 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3239 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3240 d_printf("(%s) srvsvc_NetShareGetInfo failed: %s, %s\n", __location__,
3241 nt_errstr(status), win_errstr(r.out.result));
3245 *info502 = talloc_move(mem_ctx, &info.info502);
3246 return NT_STATUS_OK;
3254 * Get us a handle on HKLM\
3257 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3258 struct smbcli_state *cli,
3259 struct smb_iconv_convenience *iconv_convenience,
3260 struct dcerpc_pipe **pipe_p,
3261 struct policy_handle **handle)
3263 struct smbcli_tree *ipc;
3264 struct dcerpc_pipe *p;
3265 struct winreg_OpenHKLM r;
3267 struct policy_handle *result;
3269 result = talloc(mem_ctx, struct policy_handle);
3271 if (result == NULL) {
3272 return NT_STATUS_NO_MEMORY;
3275 if (!(p = dcerpc_pipe_init(result,
3276 cli->transport->socket->event.ctx,
3277 iconv_convenience))) {
3278 status = NT_STATUS_NO_MEMORY;
3282 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3283 if (!NT_STATUS_IS_OK(status)) {
3287 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3288 if (!NT_STATUS_IS_OK(status)) {
3289 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3294 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3295 if (!NT_STATUS_IS_OK(status)) {
3296 d_printf("dcerpc_bind_auth_none failed: %s\n",
3301 r.in.system_name = 0;
3302 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3303 r.out.handle = result;
3305 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3306 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3307 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3308 nt_errstr(status), win_errstr(r.out.result));
3314 return NT_STATUS_OK;
3317 talloc_free(result);
3321 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3322 struct smb_iconv_convenience *iconv_convenience,
3323 const char *sharename)
3325 struct dcerpc_pipe *p;
3326 struct policy_handle *hklm = NULL;
3327 struct policy_handle new_handle;
3328 struct winreg_CreateKey c;
3329 struct winreg_CloseKey cl;
3330 enum winreg_CreateAction action_taken;
3332 TALLOC_CTX *mem_ctx;
3334 mem_ctx = talloc_new(cli);
3335 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3337 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3338 if (!NT_STATUS_IS_OK(status)) {
3339 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3344 c.in.name.name = talloc_asprintf(
3345 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3346 if (c.in.name.name == NULL) {
3347 d_printf("talloc_asprintf failed\n");
3348 status = NT_STATUS_NO_MEMORY;
3351 c.in.keyclass.name = "";
3353 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3354 c.in.secdesc = NULL;
3355 c.in.action_taken = &action_taken;
3356 c.out.new_handle = &new_handle;
3357 c.out.action_taken = &action_taken;
3359 status = dcerpc_winreg_CreateKey(p, p, &c);
3360 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3361 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3362 nt_errstr(status), win_errstr(c.out.result));
3366 cl.in.handle = &new_handle;
3367 cl.out.handle = &new_handle;
3368 status = dcerpc_winreg_CloseKey(p, p, &cl);
3369 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3370 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3371 nt_errstr(status), win_errstr(cl.out.result));
3377 talloc_free(mem_ctx);
3381 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3382 struct smbcli_state *cli,
3383 const char *sharename)
3385 struct dcerpc_pipe *p;
3386 struct policy_handle *hklm = NULL;
3387 struct winreg_DeleteKey d;
3389 TALLOC_CTX *mem_ctx;
3391 mem_ctx = talloc_new(cli);
3392 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3394 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3396 if (!NT_STATUS_IS_OK(status)) {
3397 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3402 d.in.key.name = talloc_asprintf(
3403 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3404 if (d.in.key.name == NULL) {
3405 d_printf("talloc_asprintf failed\n");
3406 status = NT_STATUS_NO_MEMORY;
3409 status = dcerpc_winreg_DeleteKey(p, p, &d);
3410 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3411 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3412 nt_errstr(status), win_errstr(d.out.result));
3417 talloc_free(mem_ctx);
3421 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3422 struct loadparm_context *lp_ctx,
3423 const char *sharename,
3424 const char *parameter,
3427 struct dcerpc_pipe *p = NULL;
3428 struct policy_handle *hklm = NULL, key_handle;
3429 struct winreg_OpenKey o;
3430 struct winreg_SetValue s;
3435 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3436 if (!NT_STATUS_IS_OK(status)) {
3437 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3441 o.in.parent_handle = hklm;
3442 o.in.keyname.name = talloc_asprintf(
3443 hklm, "software\\samba\\smbconf\\%s", sharename);
3444 if (o.in.keyname.name == NULL) {
3445 d_printf("talloc_asprintf failed\n");
3446 status = NT_STATUS_NO_MEMORY;
3450 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3451 o.out.handle = &key_handle;
3453 status = dcerpc_winreg_OpenKey(p, p, &o);
3454 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3455 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3456 nt_errstr(status), win_errstr(o.out.result));
3460 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3461 value, &type, &val)) {
3462 d_printf("(%s) reg_string_to_val failed\n", __location__);
3466 s.in.handle = &key_handle;
3467 s.in.name.name = parameter;
3469 s.in.data = val.data;
3470 s.in.size = val.length;
3472 status = dcerpc_winreg_SetValue(p, p, &s);
3473 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3474 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3475 nt_errstr(status), win_errstr(s.out.result));
3484 bool torture_samba3_regconfig(struct torture_context *torture)
3486 struct smbcli_state *cli;
3487 struct srvsvc_NetShareInfo502 *i = NULL;
3490 const char *comment = "Dummer Kommentar";
3492 if (!(torture_open_connection(&cli, torture, 0))) {
3496 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3497 if (!NT_STATUS_IS_OK(status)) {
3498 torture_warning(torture, "torture_samba3_createshare failed: "
3499 "%s\n", nt_errstr(status));
3503 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3504 if (!NT_STATUS_IS_OK(status)) {
3505 torture_warning(torture, "torture_samba3_setconfig failed: "
3506 "%s\n", nt_errstr(status));
3510 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3511 if (!NT_STATUS_IS_OK(status)) {
3512 torture_warning(torture, "get_shareinfo failed: "
3513 "%s\n", nt_errstr(status));
3517 if (strcmp(comment, i->comment) != 0) {
3518 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3519 comment, i->comment);
3523 status = torture_samba3_deleteshare(torture, cli, "blubber");
3524 if (!NT_STATUS_IS_OK(status)) {
3525 torture_warning(torture, "torture_samba3_deleteshare failed: "
3526 "%s\n", nt_errstr(status));