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;
772 encode_pw_buffer(u_info.info25.password.data,
773 cli_credentials_get_password(wks_creds),
775 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
776 if (!NT_STATUS_IS_OK(status)) {
777 d_printf("dcerpc_fetch_session_key failed: %s\n",
781 generate_random_buffer((uint8_t *)confounder, 16);
784 MD5Update(&ctx, confounder, 16);
785 MD5Update(&ctx, session_key.data, session_key.length);
786 MD5Final(confounded_session_key.data, &ctx);
788 arcfour_crypt_blob(u_info.info25.password.data, 516,
789 &confounded_session_key);
790 memcpy(&u_info.info25.password.data[516], confounder, 16);
792 sui2.in.user_handle = wks_handle;
794 sui2.in.info = &u_info;
796 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
797 if (!NT_STATUS_IS_OK(status)) {
798 d_printf("samr_SetUserInfo2(25) failed: %s\n",
803 struct samr_SetUserInfo2 sui2;
804 struct samr_SetUserInfo sui;
805 union samr_UserInfo u_info;
806 DATA_BLOB session_key;
808 encode_pw_buffer(u_info.info24.password.data,
809 cli_credentials_get_password(wks_creds),
811 u_info.info24.pw_len =
812 strlen_m(cli_credentials_get_password(wks_creds))*2;
814 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
815 if (!NT_STATUS_IS_OK(status)) {
816 d_printf("dcerpc_fetch_session_key failed\n");
819 arcfour_crypt_blob(u_info.info24.password.data, 516,
821 sui2.in.user_handle = wks_handle;
822 sui2.in.info = &u_info;
825 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
826 if (!NT_STATUS_IS_OK(status)) {
827 d_printf("samr_SetUserInfo(24) failed: %s\n",
832 u_info.info16.acct_flags = ACB_WSTRUST;
833 sui.in.user_handle = wks_handle;
834 sui.in.info = &u_info;
837 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
838 if (!NT_STATUS_IS_OK(status)) {
839 d_printf("samr_SetUserInfo(16) failed\n");
845 struct samr_QueryUserInfo q;
846 union samr_UserInfo *info;
848 q.in.user_handle = wks_handle;
852 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
853 if (!NT_STATUS_IS_OK(status)) {
854 d_printf("(%s) QueryUserInfo failed: %s\n",
855 __location__, nt_errstr(status));
860 if (last_password_change
861 == info->info21.last_password_change) {
862 d_printf("(%s) last_password_change unchanged "
863 "during join, level25 must change "
864 "it\n", __location__);
869 if (last_password_change
870 != info->info21.last_password_change) {
871 d_printf("(%s) last_password_change changed "
872 "during join, level24 doesn't "
873 "change it\n", __location__);
882 talloc_free(mem_ctx);
887 * Do a ReqChallenge/Auth2 and get the wks creds
890 static bool auth2(struct smbcli_state *cli,
891 struct loadparm_context *lp_ctx,
892 struct cli_credentials *wks_cred)
895 struct dcerpc_pipe *net_pipe;
898 struct netr_ServerReqChallenge r;
899 struct netr_Credential netr_cli_creds;
900 struct netr_Credential netr_srv_creds;
901 uint32_t negotiate_flags;
902 struct netr_ServerAuthenticate2 a;
903 struct creds_CredentialState *creds_state;
904 struct netr_Credential netr_cred;
905 struct samr_Password mach_pw;
907 mem_ctx = talloc_new(NULL);
908 if (mem_ctx == NULL) {
909 d_printf("talloc_new failed\n");
913 net_pipe = dcerpc_pipe_init(mem_ctx,
914 cli->transport->socket->event.ctx,
915 lp_iconv_convenience(lp_ctx));
916 if (net_pipe == NULL) {
917 d_printf("dcerpc_pipe_init failed\n");
921 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
922 if (!NT_STATUS_IS_OK(status)) {
923 d_printf("dcerpc_pipe_open_smb failed: %s\n",
928 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
929 if (!NT_STATUS_IS_OK(status)) {
930 d_printf("dcerpc_bind_auth_none failed: %s\n",
935 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
936 r.in.server_name = talloc_asprintf(
937 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
938 if (r.in.server_name == NULL) {
939 d_printf("talloc_asprintf failed\n");
942 generate_random_buffer(netr_cli_creds.data,
943 sizeof(netr_cli_creds.data));
944 r.in.credentials = &netr_cli_creds;
945 r.out.return_credentials = &netr_srv_creds;
947 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
948 if (!NT_STATUS_IS_OK(status)) {
949 d_printf("netr_ServerReqChallenge failed: %s\n",
954 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
955 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
957 creds_state = talloc(mem_ctx, struct creds_CredentialState);
958 creds_client_init(creds_state, r.in.credentials,
959 r.out.return_credentials, &mach_pw,
960 &netr_cred, negotiate_flags);
962 a.in.server_name = talloc_asprintf(
963 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
964 a.in.account_name = talloc_asprintf(
965 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
966 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
967 a.in.secure_channel_type = SEC_CHAN_WKSTA;
968 a.in.negotiate_flags = &negotiate_flags;
969 a.out.negotiate_flags = &negotiate_flags;
970 a.in.credentials = &netr_cred;
971 a.out.return_credentials = &netr_cred;
973 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
974 if (!NT_STATUS_IS_OK(status)) {
975 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
980 if (!creds_client_check(creds_state, a.out.return_credentials)) {
981 d_printf("creds_client_check failed\n");
985 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
990 talloc_free(mem_ctx);
995 * Do a couple of schannel protected Netlogon ops: Interactive and Network
996 * login, and change the wks password
999 static bool schan(struct smbcli_state *cli,
1000 struct loadparm_context *lp_ctx,
1001 struct cli_credentials *wks_creds,
1002 struct cli_credentials *user_creds)
1004 TALLOC_CTX *mem_ctx;
1007 struct dcerpc_pipe *net_pipe;
1010 mem_ctx = talloc_new(NULL);
1011 if (mem_ctx == NULL) {
1012 d_printf("talloc_new failed\n");
1016 net_pipe = dcerpc_pipe_init(mem_ctx,
1017 cli->transport->socket->event.ctx,
1018 lp_iconv_convenience(lp_ctx));
1019 if (net_pipe == NULL) {
1020 d_printf("dcerpc_pipe_init failed\n");
1024 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1025 if (!NT_STATUS_IS_OK(status)) {
1026 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1032 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1033 DCERPC_DEBUG_PRINT_OUT;
1036 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1037 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1038 wks_creds, lp_gensec_settings(lp_ctx, lp_ctx), DCERPC_AUTH_TYPE_SCHANNEL,
1039 DCERPC_AUTH_LEVEL_PRIVACY,
1042 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1044 if (!NT_STATUS_IS_OK(status)) {
1045 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1050 for (i=2; i<4; i++) {
1052 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1053 struct creds_CredentialState *creds_state;
1054 struct netr_Authenticator netr_auth, netr_auth2;
1055 struct netr_NetworkInfo ninfo;
1056 struct netr_PasswordInfo pinfo;
1057 struct netr_LogonSamLogon r;
1058 union netr_LogonLevel logon;
1059 union netr_Validation validation;
1060 uint8_t authoritative;
1061 struct netr_Authenticator return_authenticator;
1063 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1064 CLI_CRED_NTLMv2_AUTH;
1066 chal = data_blob_talloc(mem_ctx, NULL, 8);
1067 if (chal.data == NULL) {
1068 d_printf("data_blob_talloc failed\n");
1072 generate_random_buffer(chal.data, chal.length);
1073 names_blob = NTLMv2_generate_names_blob(
1075 cli_credentials_get_workstation(user_creds),
1076 cli_credentials_get_domain(user_creds));
1077 status = cli_credentials_get_ntlm_response(
1078 user_creds, mem_ctx, &flags, chal, names_blob,
1079 &lm_resp, &nt_resp, NULL, NULL);
1080 if (!NT_STATUS_IS_OK(status)) {
1081 d_printf("cli_credentials_get_ntlm_response failed:"
1082 " %s\n", nt_errstr(status));
1086 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1087 creds_client_authenticator(creds_state, &netr_auth);
1089 ninfo.identity_info.account_name.string =
1090 cli_credentials_get_username(user_creds);
1091 ninfo.identity_info.domain_name.string =
1092 cli_credentials_get_domain(user_creds);
1093 ninfo.identity_info.parameter_control = 0;
1094 ninfo.identity_info.logon_id_low = 0;
1095 ninfo.identity_info.logon_id_high = 0;
1096 ninfo.identity_info.workstation.string =
1097 cli_credentials_get_workstation(user_creds);
1098 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1099 ninfo.nt.length = nt_resp.length;
1100 ninfo.nt.data = nt_resp.data;
1101 ninfo.lm.length = lm_resp.length;
1102 ninfo.lm.data = lm_resp.data;
1104 logon.network = &ninfo;
1106 r.in.server_name = talloc_asprintf(
1107 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1108 ZERO_STRUCT(netr_auth2);
1109 r.in.computer_name =
1110 cli_credentials_get_workstation(wks_creds);
1111 r.in.credential = &netr_auth;
1112 r.in.return_authenticator = &netr_auth2;
1113 r.in.logon_level = 2;
1114 r.in.validation_level = i;
1115 r.in.logon = &logon;
1116 r.out.validation = &validation;
1117 r.out.authoritative = &authoritative;
1118 r.out.return_authenticator = &return_authenticator;
1120 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1121 if (!NT_STATUS_IS_OK(status)) {
1122 d_printf("netr_LogonSamLogon failed: %s\n",
1127 if ((r.out.return_authenticator == NULL) ||
1128 (!creds_client_check(creds_state,
1129 &r.out.return_authenticator->cred))) {
1130 d_printf("Credentials check failed!\n");
1134 creds_client_authenticator(creds_state, &netr_auth);
1136 pinfo.identity_info = ninfo.identity_info;
1137 ZERO_STRUCT(pinfo.lmpassword.hash);
1138 E_md4hash(cli_credentials_get_password(user_creds),
1139 pinfo.ntpassword.hash);
1140 session_key = data_blob_talloc(mem_ctx,
1141 creds_state->session_key, 16);
1142 arcfour_crypt_blob(pinfo.ntpassword.hash,
1143 sizeof(pinfo.ntpassword.hash),
1146 logon.password = &pinfo;
1148 r.in.logon_level = 1;
1149 r.in.logon = &logon;
1150 r.out.return_authenticator = &return_authenticator;
1152 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1153 if (!NT_STATUS_IS_OK(status)) {
1154 d_printf("netr_LogonSamLogon failed: %s\n",
1159 if ((r.out.return_authenticator == NULL) ||
1160 (!creds_client_check(creds_state,
1161 &r.out.return_authenticator->cred))) {
1162 d_printf("Credentials check failed!\n");
1168 struct netr_ServerPasswordSet s;
1169 char *password = generate_random_str(wks_creds, 8);
1170 struct creds_CredentialState *creds_state;
1171 struct netr_Authenticator credential, return_authenticator;
1172 struct samr_Password new_password;
1174 s.in.server_name = talloc_asprintf(
1175 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1176 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1177 s.in.account_name = talloc_asprintf(
1178 mem_ctx, "%s$", s.in.computer_name);
1179 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1180 s.in.credential = &credential;
1181 s.in.new_password = &new_password;
1182 s.out.return_authenticator = &return_authenticator;
1184 E_md4hash(password, new_password.hash);
1186 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1187 creds_des_encrypt(creds_state, &new_password);
1188 creds_client_authenticator(creds_state, &credential);
1190 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1191 if (!NT_STATUS_IS_OK(status)) {
1192 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1196 if (!creds_client_check(creds_state,
1197 &s.out.return_authenticator->cred)) {
1198 printf("Credential chaining failed\n");
1201 cli_credentials_set_password(wks_creds, password,
1207 talloc_free(mem_ctx);
1212 * Delete the wks account again
1215 static bool leave(struct smbcli_state *cli,
1216 struct loadparm_context *lp_ctx,
1217 struct cli_credentials *admin_creds,
1218 struct cli_credentials *wks_creds)
1220 char *wks_name = talloc_asprintf(
1221 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1224 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1225 talloc_free(wks_name);
1230 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1233 bool torture_netlogon_samba3(struct torture_context *torture)
1235 TALLOC_CTX *mem_ctx;
1238 struct smbcli_state *cli;
1239 struct cli_credentials *anon_creds;
1240 struct cli_credentials *wks_creds;
1241 const char *wks_name;
1243 struct smbcli_options options;
1244 struct smbcli_session_options session_options;
1246 wks_name = torture_setting_string(torture, "wksname", NULL);
1247 if (wks_name == NULL) {
1248 wks_name = get_myname();
1251 mem_ctx = talloc_init("torture_netlogon_samba3");
1253 if (mem_ctx == NULL) {
1254 d_printf("talloc_init failed\n");
1258 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1259 d_printf("create_anon_creds failed\n");
1263 lp_smbcli_options(torture->lp_ctx, &options);
1264 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1266 status = smbcli_full_connection(mem_ctx, &cli,
1267 torture_setting_string(torture, "host", NULL),
1268 lp_smb_ports(torture->lp_ctx),
1270 lp_socket_options(torture->lp_ctx),
1272 lp_resolve_context(torture->lp_ctx),
1273 torture->ev, &options, &session_options,
1274 lp_iconv_convenience(torture->lp_ctx),
1275 lp_gensec_settings(torture, torture->lp_ctx));
1276 if (!NT_STATUS_IS_OK(status)) {
1277 d_printf("smbcli_full_connection failed: %s\n",
1282 wks_creds = cli_credentials_init(mem_ctx);
1283 if (wks_creds == NULL) {
1284 d_printf("cli_credentials_init failed\n");
1288 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1289 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1290 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1291 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1292 cli_credentials_set_password(wks_creds,
1293 generate_random_str(wks_creds, 8),
1296 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1297 d_printf("join failed\n");
1301 cli_credentials_set_domain(
1302 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1305 for (i=0; i<2; i++) {
1307 /* Do this more than once, the routine "schan" changes
1308 * the workstation password using the netlogon
1309 * password change routine */
1313 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1314 d_printf("auth2 failed\n");
1318 for (j=0; j<2; j++) {
1319 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1320 d_printf("schan failed\n");
1326 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1327 d_printf("leave failed\n");
1334 talloc_free(mem_ctx);
1339 * Do a simple join, testjoin and leave using specified smb and samr
1343 static bool test_join3(struct torture_context *tctx,
1345 struct cli_credentials *smb_creds,
1346 struct cli_credentials *samr_creds,
1347 const char *wks_name)
1351 struct smbcli_state *cli;
1352 struct cli_credentials *wks_creds;
1353 struct smbcli_options options;
1354 struct smbcli_session_options session_options;
1356 lp_smbcli_options(tctx->lp_ctx, &options);
1357 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1359 status = smbcli_full_connection(tctx, &cli,
1360 torture_setting_string(tctx, "host", NULL),
1361 lp_smb_ports(tctx->lp_ctx),
1362 "IPC$", NULL, lp_socket_options(tctx->lp_ctx),
1363 smb_creds, lp_resolve_context(tctx->lp_ctx),
1364 tctx->ev, &options, &session_options,
1365 lp_iconv_convenience(tctx->lp_ctx),
1366 lp_gensec_settings(tctx, tctx->lp_ctx));
1367 if (!NT_STATUS_IS_OK(status)) {
1368 d_printf("smbcli_full_connection failed: %s\n",
1373 wks_creds = cli_credentials_init(cli);
1374 if (wks_creds == NULL) {
1375 d_printf("cli_credentials_init failed\n");
1379 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1380 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1381 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1382 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1383 cli_credentials_set_password(wks_creds,
1384 generate_random_str(wks_creds, 8),
1387 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1388 d_printf("join failed\n");
1392 cli_credentials_set_domain(
1393 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1396 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1397 d_printf("auth2 failed\n");
1401 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1402 d_printf("leave failed\n");
1415 * Test the different session key variants. Do it by joining, this uses the
1416 * session key in the setpassword routine. Test the join by doing the auth2.
1419 bool torture_samba3_sessionkey(struct torture_context *torture)
1422 struct cli_credentials *anon_creds;
1423 const char *wks_name;
1425 wks_name = torture_setting_string(torture, "wksname", get_myname());
1427 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1428 d_printf("create_anon_creds failed\n");
1432 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1436 if (!torture_setting_bool(torture, "samba3", false)) {
1438 /* Samba3 in the build farm right now does this happily. Need
1441 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1442 d_printf("join using anonymous bind on an anonymous smb "
1443 "connection succeeded -- HUH??\n");
1448 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1450 d_printf("join using ntlmssp bind on an anonymous smb "
1451 "connection failed\n");
1455 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1456 d_printf("join using anonymous bind on an authenticated smb "
1457 "connection failed\n");
1461 if (!test_join3(torture, false, cmdline_credentials,
1462 cmdline_credentials,
1464 d_printf("join using ntlmssp bind on an authenticated smb "
1465 "connection failed\n");
1470 * The following two are tests for setuserinfolevel 25
1473 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1475 d_printf("join using ntlmssp bind on an anonymous smb "
1476 "connection failed\n");
1480 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1481 d_printf("join using anonymous bind on an authenticated smb "
1482 "connection failed\n");
1492 * open pipe and bind, given an IPC$ context
1495 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1496 struct loadparm_context *lp_ctx,
1497 struct smbcli_tree *tree,
1498 const char *pipe_name,
1499 const struct ndr_interface_table *iface,
1500 struct dcerpc_pipe **p)
1502 struct dcerpc_pipe *result;
1505 if (!(result = dcerpc_pipe_init(
1506 mem_ctx, tree->session->transport->socket->event.ctx,
1507 lp_iconv_convenience(lp_ctx)))) {
1508 return NT_STATUS_NO_MEMORY;
1511 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1512 if (!NT_STATUS_IS_OK(status)) {
1513 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1515 talloc_free(result);
1519 status = dcerpc_bind_auth_none(result, iface);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1522 talloc_free(result);
1527 return NT_STATUS_OK;
1531 * Sane wrapper around lsa_LookupNames
1534 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1535 struct dcerpc_pipe *p,
1539 struct lsa_ObjectAttribute attr;
1540 struct lsa_QosInfo qos;
1541 struct lsa_OpenPolicy2 r;
1544 struct policy_handle handle;
1545 struct lsa_LookupNames l;
1546 struct lsa_TransSidArray sids;
1547 struct lsa_RefDomainList *domains = NULL;
1548 struct lsa_String lsa_name;
1550 struct dom_sid *result;
1551 TALLOC_CTX *tmp_ctx;
1553 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1558 qos.impersonation_level = 2;
1559 qos.context_mode = 1;
1560 qos.effective_only = 0;
1563 attr.root_dir = NULL;
1564 attr.object_name = NULL;
1565 attr.attributes = 0;
1566 attr.sec_desc = NULL;
1567 attr.sec_qos = &qos;
1569 r.in.system_name = "\\";
1571 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1572 r.out.handle = &handle;
1574 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1575 if (!NT_STATUS_IS_OK(status)) {
1576 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1577 talloc_free(tmp_ctx);
1584 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1586 l.in.handle = &handle;
1588 l.in.names = &lsa_name;
1591 l.in.count = &count;
1592 l.out.count = &count;
1594 l.out.domains = &domains;
1596 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1597 if (!NT_STATUS_IS_OK(status)) {
1598 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1600 talloc_free(tmp_ctx);
1604 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1605 l.out.sids->sids[0].rid);
1607 c.in.handle = &handle;
1608 c.out.handle = &handle;
1610 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1611 if (!NT_STATUS_IS_OK(status)) {
1612 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1613 talloc_free(tmp_ctx);
1617 talloc_free(tmp_ctx);
1622 * Find out the user SID on this connection
1625 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1626 struct loadparm_context *lp_ctx,
1627 struct smbcli_tree *tree)
1629 struct dcerpc_pipe *lsa;
1630 struct lsa_GetUserName r;
1632 struct lsa_String *authority_name_p = NULL;
1633 struct lsa_String *account_name_p = NULL;
1634 struct dom_sid *result;
1636 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1637 &ndr_table_lsarpc, &lsa);
1638 if (!NT_STATUS_IS_OK(status)) {
1639 d_printf("(%s) Could not bind to LSA: %s\n",
1640 __location__, nt_errstr(status));
1644 r.in.system_name = "\\";
1645 r.in.account_name = &account_name_p;
1646 r.in.authority_name = &authority_name_p;
1647 r.out.account_name = &account_name_p;
1649 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1651 authority_name_p = *r.out.authority_name;
1653 if (!NT_STATUS_IS_OK(status)) {
1654 printf("(%s) GetUserName failed - %s\n",
1655 __location__, nt_errstr(status));
1660 result = name2sid(mem_ctx, lsa, account_name_p->string,
1661 authority_name_p->string);
1667 static int destroy_tree(struct smbcli_tree *tree)
1669 smb_tree_disconnect(tree);
1674 * Do a tcon, given a session
1677 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1678 struct smbcli_session *session,
1679 const char *sharename,
1680 struct smbcli_tree **res)
1682 struct smbcli_tree *result;
1683 TALLOC_CTX *tmp_ctx;
1684 union smb_tcon tcon;
1687 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1688 return NT_STATUS_NO_MEMORY;
1691 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1692 talloc_free(tmp_ctx);
1693 return NT_STATUS_NO_MEMORY;
1696 tcon.generic.level = RAW_TCON_TCONX;
1697 tcon.tconx.in.flags = 0;
1698 tcon.tconx.in.password = data_blob(NULL, 0);
1699 tcon.tconx.in.path = sharename;
1700 tcon.tconx.in.device = "?????";
1702 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1703 if (!NT_STATUS_IS_OK(status)) {
1704 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1706 talloc_free(tmp_ctx);
1710 result->tid = tcon.tconx.out.tid;
1711 result = talloc_steal(mem_ctx, result);
1712 talloc_set_destructor(result, destroy_tree);
1713 talloc_free(tmp_ctx);
1715 return NT_STATUS_OK;
1719 * Test the getusername behaviour
1722 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1725 struct smbcli_state *cli;
1726 TALLOC_CTX *mem_ctx;
1728 struct dom_sid *user_sid;
1729 struct dom_sid *created_sid;
1730 struct cli_credentials *anon_creds;
1731 struct cli_credentials *user_creds;
1733 struct smbcli_options options;
1734 struct smbcli_session_options session_options;
1736 if (!(mem_ctx = talloc_new(torture))) {
1740 lp_smbcli_options(torture->lp_ctx, &options);
1741 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1743 status = smbcli_full_connection(
1744 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1745 lp_smb_ports(torture->lp_ctx),
1746 "IPC$", NULL, lp_socket_options(torture->lp_ctx), cmdline_credentials,
1747 lp_resolve_context(torture->lp_ctx),
1748 torture->ev, &options, &session_options,
1749 lp_iconv_convenience(torture->lp_ctx),
1750 lp_gensec_settings(torture, torture->lp_ctx));
1751 if (!NT_STATUS_IS_OK(status)) {
1752 d_printf("(%s) smbcli_full_connection failed: %s\n",
1753 __location__, nt_errstr(status));
1758 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1759 d_printf("(%s) whoami on auth'ed connection failed\n",
1766 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1767 d_printf("(%s) create_anon_creds failed\n", __location__);
1772 status = smbcli_full_connection(
1773 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1774 lp_smb_ports(torture->lp_ctx), "IPC$", NULL,
1775 lp_socket_options(torture->lp_ctx), anon_creds,
1776 lp_resolve_context(torture->lp_ctx),
1777 torture->ev, &options, &session_options,
1778 lp_iconv_convenience(torture->lp_ctx),
1779 lp_gensec_settings(torture, torture->lp_ctx));
1780 if (!NT_STATUS_IS_OK(status)) {
1781 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1782 __location__, nt_errstr(status));
1787 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1788 d_printf("(%s) whoami on anon connection failed\n",
1794 if (!dom_sid_equal(user_sid,
1795 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1796 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1797 "S-1-5-7", __location__,
1798 dom_sid_string(mem_ctx, user_sid));
1802 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1803 d_printf("(%s) cli_credentials_init failed\n", __location__);
1808 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1809 cli_credentials_set_username(user_creds, "torture_username",
1811 cli_credentials_set_password(user_creds,
1812 generate_random_str(user_creds, 8),
1815 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1816 cli_credentials_get_username(user_creds),
1817 cli_credentials_get_password(user_creds),
1818 &domain_name, &created_sid)) {
1819 d_printf("(%s) create_user failed\n", __location__);
1824 cli_credentials_set_domain(user_creds, domain_name,
1828 struct smbcli_session *session2;
1829 struct smb_composite_sesssetup setup;
1830 struct smbcli_tree *tree;
1832 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1833 if (session2 == NULL) {
1834 d_printf("(%s) smbcli_session_init failed\n",
1839 setup.in.sesskey = cli->transport->negotiate.sesskey;
1840 setup.in.capabilities = cli->transport->negotiate.capabilities;
1841 setup.in.workgroup = "";
1842 setup.in.credentials = user_creds;
1843 setup.in.gensec_settings = lp_gensec_settings(torture, torture->lp_ctx);
1845 status = smb_composite_sesssetup(session2, &setup);
1846 if (!NT_STATUS_IS_OK(status)) {
1847 d_printf("(%s) session setup with new user failed: "
1848 "%s\n", __location__, nt_errstr(status));
1852 session2->vuid = setup.out.vuid;
1854 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1856 d_printf("(%s) secondary_tcon failed\n",
1862 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1863 d_printf("(%s) whoami on user connection failed\n",
1872 d_printf("Created %s, found %s\n",
1873 dom_sid_string(mem_ctx, created_sid),
1874 dom_sid_string(mem_ctx, user_sid));
1876 if (!dom_sid_equal(created_sid, user_sid)) {
1881 if (!delete_user(cli, torture->lp_ctx,
1882 cmdline_credentials,
1883 cli_credentials_get_username(user_creds))) {
1884 d_printf("(%s) delete_user failed\n", __location__);
1889 talloc_free(mem_ctx);
1893 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1894 const char *sharename)
1897 struct srvsvc_NetShareGetInfo r;
1898 union srvsvc_NetShareInfo info;
1899 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1903 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1904 dcerpc_server_name(p));
1905 r.in.share_name = sharename;
1908 for (i=0;i<ARRAY_SIZE(levels);i++) {
1909 r.in.level = levels[i];
1911 printf("testing NetShareGetInfo level %u on share '%s'\n",
1912 r.in.level, r.in.share_name);
1914 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1915 if (!NT_STATUS_IS_OK(status)) {
1916 printf("NetShareGetInfo level %u on share '%s' failed"
1917 " - %s\n", r.in.level, r.in.share_name,
1922 if (!W_ERROR_IS_OK(r.out.result)) {
1923 printf("NetShareGetInfo level %u on share '%s' failed "
1924 "- %s\n", r.in.level, r.in.share_name,
1925 win_errstr(r.out.result));
1934 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1935 const char **one_sharename)
1938 struct srvsvc_NetShareEnum r;
1939 struct srvsvc_NetShareInfoCtr info_ctr;
1940 struct srvsvc_NetShareCtr0 c0;
1941 struct srvsvc_NetShareCtr1 c1;
1942 struct srvsvc_NetShareCtr2 c2;
1943 struct srvsvc_NetShareCtr501 c501;
1944 struct srvsvc_NetShareCtr502 c502;
1945 struct srvsvc_NetShareCtr1004 c1004;
1946 struct srvsvc_NetShareCtr1005 c1005;
1947 struct srvsvc_NetShareCtr1006 c1006;
1948 struct srvsvc_NetShareCtr1007 c1007;
1949 uint32_t totalentries = 0;
1950 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1954 ZERO_STRUCT(info_ctr);
1956 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1957 r.in.info_ctr = &info_ctr;
1958 r.in.max_buffer = (uint32_t)-1;
1959 r.in.resume_handle = NULL;
1960 r.out.totalentries = &totalentries;
1961 r.out.info_ctr = &info_ctr;
1963 for (i=0;i<ARRAY_SIZE(levels);i++) {
1964 info_ctr.level = levels[i];
1966 switch (info_ctr.level) {
1969 info_ctr.ctr.ctr0 = &c0;
1973 info_ctr.ctr.ctr1 = &c1;
1977 info_ctr.ctr.ctr2 = &c2;
1981 info_ctr.ctr.ctr501 = &c501;
1985 info_ctr.ctr.ctr502 = &c502;
1989 info_ctr.ctr.ctr1004 = &c1004;
1993 info_ctr.ctr.ctr1005 = &c1005;
1997 info_ctr.ctr.ctr1006 = &c1006;
2001 info_ctr.ctr.ctr1007 = &c1007;
2005 printf("testing NetShareEnum level %u\n", info_ctr.level);
2006 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2007 if (!NT_STATUS_IS_OK(status)) {
2008 printf("NetShareEnum level %u failed - %s\n",
2009 info_ctr.level, nt_errstr(status));
2013 if (!W_ERROR_IS_OK(r.out.result)) {
2014 printf("NetShareEnum level %u failed - %s\n",
2015 info_ctr.level, win_errstr(r.out.result));
2018 if (info_ctr.level == 0) {
2019 struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
2020 if (ctr->count > 0) {
2021 *one_sharename = ctr->array[0].name;
2029 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
2031 struct dcerpc_pipe *p;
2032 TALLOC_CTX *mem_ctx;
2034 const char *sharename = NULL;
2035 struct smbcli_state *cli;
2038 if (!(mem_ctx = talloc_new(torture))) {
2042 if (!(torture_open_connection_share(
2043 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2044 "IPC$", torture->ev))) {
2045 talloc_free(mem_ctx);
2049 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
2050 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
2051 if (!NT_STATUS_IS_OK(status)) {
2052 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2053 __location__, nt_errstr(status));
2058 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
2059 if (sharename == NULL) {
2060 printf("did not get sharename\n");
2062 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
2066 talloc_free(mem_ctx);
2071 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
2072 * NT_STATUS_NO_SAM_ACCOUNT
2075 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
2077 TALLOC_CTX *mem_ctx;
2078 struct dcerpc_pipe *net_pipe;
2080 bool result = false;
2082 struct netr_ServerReqChallenge r;
2083 struct netr_Credential netr_cli_creds;
2084 struct netr_Credential netr_srv_creds;
2085 uint32_t negotiate_flags;
2086 struct netr_ServerAuthenticate2 a;
2087 struct creds_CredentialState *creds_state;
2088 struct netr_Credential netr_cred;
2089 struct samr_Password mach_pw;
2090 struct smbcli_state *cli;
2092 if (!(mem_ctx = talloc_new(torture))) {
2093 d_printf("talloc_new failed\n");
2097 if (!(wksname = generate_random_str_list(
2098 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2099 d_printf("generate_random_str_list failed\n");
2103 if (!(torture_open_connection_share(
2105 torture, torture_setting_string(torture, "host", NULL),
2106 "IPC$", torture->ev))) {
2107 d_printf("IPC$ connection failed\n");
2111 if (!(net_pipe = dcerpc_pipe_init(
2112 mem_ctx, cli->transport->socket->event.ctx,
2113 lp_iconv_convenience(torture->lp_ctx)))) {
2114 d_printf("dcerpc_pipe_init failed\n");
2118 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2119 if (!NT_STATUS_IS_OK(status)) {
2120 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2125 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2126 if (!NT_STATUS_IS_OK(status)) {
2127 d_printf("dcerpc_bind_auth_none failed: %s\n",
2132 r.in.computer_name = wksname;
2133 r.in.server_name = talloc_asprintf(
2134 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2135 if (r.in.server_name == NULL) {
2136 d_printf("talloc_asprintf failed\n");
2139 generate_random_buffer(netr_cli_creds.data,
2140 sizeof(netr_cli_creds.data));
2141 r.in.credentials = &netr_cli_creds;
2142 r.out.return_credentials = &netr_srv_creds;
2144 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2145 if (!NT_STATUS_IS_OK(status)) {
2146 d_printf("netr_ServerReqChallenge failed: %s\n",
2151 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2152 E_md4hash("foobar", mach_pw.hash);
2154 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2155 creds_client_init(creds_state, r.in.credentials,
2156 r.out.return_credentials, &mach_pw,
2157 &netr_cred, negotiate_flags);
2159 a.in.server_name = talloc_asprintf(
2160 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2161 a.in.account_name = talloc_asprintf(
2162 mem_ctx, "%s$", wksname);
2163 a.in.computer_name = wksname;
2164 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2165 a.in.negotiate_flags = &negotiate_flags;
2166 a.out.negotiate_flags = &negotiate_flags;
2167 a.in.credentials = &netr_cred;
2168 a.out.return_credentials = &netr_cred;
2170 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2172 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2173 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2174 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2181 talloc_free(mem_ctx);
2185 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2186 struct loadparm_context *lp_ctx,
2187 struct smbcli_session *sess,
2188 const char *sharename)
2190 struct smbcli_tree *tree;
2191 TALLOC_CTX *tmp_ctx;
2192 struct dcerpc_pipe *p;
2194 struct srvsvc_NetShareGetInfo r;
2195 union srvsvc_NetShareInfo info;
2196 struct security_descriptor *result;
2198 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2199 d_printf("talloc_new failed\n");
2203 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2204 d_printf("secondary_tcon failed\n");
2205 talloc_free(tmp_ctx);
2209 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2210 &ndr_table_srvsvc, &p);
2211 if (!NT_STATUS_IS_OK(status)) {
2212 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2213 __location__, nt_errstr(status));
2214 talloc_free(tmp_ctx);
2219 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2222 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2223 dcerpc_server_name(p));
2224 r.in.share_name = sharename;
2228 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2229 if (!NT_STATUS_IS_OK(status)) {
2230 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2232 talloc_free(tmp_ctx);
2236 result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
2237 talloc_free(tmp_ctx);
2241 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2242 struct loadparm_context *lp_ctx,
2243 struct smbcli_session *sess,
2244 const char *sharename,
2245 struct security_descriptor *sd)
2247 struct smbcli_tree *tree;
2248 TALLOC_CTX *tmp_ctx;
2249 struct dcerpc_pipe *p;
2251 struct sec_desc_buf i;
2252 struct srvsvc_NetShareSetInfo r;
2253 union srvsvc_NetShareInfo info;
2256 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2257 d_printf("talloc_new failed\n");
2258 return NT_STATUS_NO_MEMORY;
2261 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2262 d_printf("secondary_tcon failed\n");
2263 talloc_free(tmp_ctx);
2264 return NT_STATUS_UNSUCCESSFUL;
2267 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2268 &ndr_table_srvsvc, &p);
2269 if (!NT_STATUS_IS_OK(status)) {
2270 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2271 __location__, nt_errstr(status));
2272 talloc_free(tmp_ctx);
2273 return NT_STATUS_UNSUCCESSFUL;
2277 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2280 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2281 dcerpc_server_name(p));
2282 r.in.share_name = sharename;
2287 r.in.parm_error = &error;
2289 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2290 if (!NT_STATUS_IS_OK(status)) {
2291 d_printf("srvsvc_NetShareSetInfo failed: %s\n",
2295 talloc_free(tmp_ctx);
2299 bool try_tcon(TALLOC_CTX *mem_ctx,
2300 struct loadparm_context *lp_ctx,
2301 struct security_descriptor *orig_sd,
2302 struct smbcli_session *session,
2303 const char *sharename, const struct dom_sid *user_sid,
2304 unsigned int access_mask, NTSTATUS expected_tcon,
2305 NTSTATUS expected_mkdir)
2307 TALLOC_CTX *tmp_ctx;
2308 struct smbcli_tree *rmdir_tree, *tree;
2309 struct dom_sid *domain_sid;
2311 struct security_descriptor *sd;
2315 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2316 d_printf("talloc_new failed\n");
2320 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2321 if (!NT_STATUS_IS_OK(status)) {
2322 d_printf("first tcon to delete dir failed\n");
2323 talloc_free(tmp_ctx);
2327 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2329 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2330 &domain_sid, &rid))) {
2331 d_printf("dom_sid_split_rid failed\n");
2332 talloc_free(tmp_ctx);
2336 sd = security_descriptor_dacl_create(
2337 tmp_ctx, 0, "S-1-5-32-544",
2338 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2340 dom_sid_string(mem_ctx, user_sid),
2341 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2343 d_printf("security_descriptor_dacl_create failed\n");
2344 talloc_free(tmp_ctx);
2348 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2349 if (!NT_STATUS_IS_OK(status)) {
2350 d_printf("custom set_sharesec failed: %s\n",
2352 talloc_free(tmp_ctx);
2356 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2357 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2358 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2364 if (!NT_STATUS_IS_OK(status)) {
2365 /* An expected non-access, no point in trying to write */
2369 status = smbcli_mkdir(tree, "sharesec_testdir");
2370 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2371 d_printf("(%s) Expected %s, got %s\n", __location__,
2372 nt_errstr(expected_mkdir), nt_errstr(status));
2377 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2379 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2380 if (!NT_STATUS_IS_OK(status)) {
2381 d_printf("custom set_sharesec failed: %s\n",
2383 talloc_free(tmp_ctx);
2387 talloc_free(tmp_ctx);
2391 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2393 TALLOC_CTX *mem_ctx;
2395 struct smbcli_state *cli;
2396 struct security_descriptor *sd;
2397 struct dom_sid *user_sid;
2399 if (!(mem_ctx = talloc_new(torture))) {
2403 if (!(torture_open_connection_share(
2404 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2405 "IPC$", torture->ev))) {
2406 d_printf("IPC$ connection failed\n");
2407 talloc_free(mem_ctx);
2411 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2412 d_printf("whoami failed\n");
2413 talloc_free(mem_ctx);
2417 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2418 torture_setting_string(torture, "share", NULL));
2420 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2421 torture_setting_string(torture, "share", NULL),
2422 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2424 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2425 torture_setting_string(torture, "share", NULL),
2426 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2427 NT_STATUS_MEDIA_WRITE_PROTECTED);
2429 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2430 torture_setting_string(torture, "share", NULL),
2431 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2433 talloc_free(mem_ctx);
2437 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2439 TALLOC_CTX *mem_ctx;
2441 struct smbcli_state *cli;
2442 struct dcerpc_pipe *p;
2443 struct policy_handle lsa_handle;
2445 struct dom_sid *domain_sid;
2447 if (!(mem_ctx = talloc_new(torture))) {
2451 if (!(torture_open_connection_share(
2452 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2453 "IPC$", torture->ev))) {
2454 d_printf("IPC$ connection failed\n");
2455 talloc_free(mem_ctx);
2459 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2460 &ndr_table_lsarpc, &p);
2461 if (!NT_STATUS_IS_OK(status)) {
2462 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2464 talloc_free(mem_ctx);
2469 struct lsa_ObjectAttribute attr;
2470 struct lsa_OpenPolicy2 o;
2471 o.in.system_name = talloc_asprintf(
2472 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2475 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2476 o.out.handle = &lsa_handle;
2477 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2478 if (!NT_STATUS_IS_OK(status)) {
2479 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2480 __location__, nt_errstr(status));
2481 talloc_free(mem_ctx);
2487 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2492 int levels[] = { 2,3,5,6 };
2494 for (i=0; i<ARRAY_SIZE(levels); i++) {
2495 struct lsa_QueryInfoPolicy r;
2496 union lsa_PolicyInformation *info = NULL;
2497 r.in.handle = &lsa_handle;
2498 r.in.level = levels[i];
2500 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2501 if (!NT_STATUS_IS_OK(status)) {
2502 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2503 "failed: %s\n", __location__,
2504 levels[i], nt_errstr(status));
2505 talloc_free(mem_ctx);
2508 if (levels[i] == 5) {
2509 domain_sid = info->account_domain.sid;
2517 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2518 struct smb_iconv_convenience *iconv_convenience,
2521 struct rap_WserverGetInfo r;
2523 char servername[17];
2526 r.in.bufsize = 0xffff;
2528 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2529 if (!NT_STATUS_IS_OK(status)) {
2533 memcpy(servername, r.out.info.info0.name, 16);
2534 servername[16] = '\0';
2536 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2537 return NT_STATUS_NO_MEMORY;
2540 return NT_STATUS_OK;
2544 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2545 struct smbcli_tree *tree,
2546 const char ***printers, int *num_printers)
2548 TALLOC_CTX *mem_ctx;
2550 struct dcerpc_pipe *p;
2551 struct srvsvc_NetShareEnum r;
2552 struct srvsvc_NetShareInfoCtr info_ctr;
2553 struct srvsvc_NetShareCtr1 c1_in;
2554 struct srvsvc_NetShareCtr1 *c1;
2555 uint32_t totalentries = 0;
2558 mem_ctx = talloc_new(ctx);
2559 if (mem_ctx == NULL) {
2560 return NT_STATUS_NO_MEMORY;
2563 status = pipe_bind_smb(mem_ctx, lp_ctx,
2564 tree, "\\srvsvc", &ndr_table_srvsvc,
2566 if (!NT_STATUS_IS_OK(status)) {
2567 d_printf("could not bind to srvsvc pipe\n");
2568 talloc_free(mem_ctx);
2574 info_ctr.ctr.ctr1 = &c1_in;
2576 r.in.server_unc = talloc_asprintf(
2577 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2578 r.in.info_ctr = &info_ctr;
2579 r.in.max_buffer = (uint32_t)-1;
2580 r.in.resume_handle = NULL;
2581 r.out.totalentries = &totalentries;
2582 r.out.info_ctr = &info_ctr;
2584 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2585 if (!NT_STATUS_IS_OK(status)) {
2586 d_printf("NetShareEnum level %u failed - %s\n",
2587 info_ctr.level, nt_errstr(status));
2588 talloc_free(mem_ctx);
2594 c1 = r.out.info_ctr->ctr.ctr1;
2595 for (i=0; i<c1->count; i++) {
2596 if (c1->array[i].type != STYPE_PRINTQ) {
2599 if (!add_string_to_array(ctx, c1->array[i].name,
2600 printers, num_printers)) {
2602 return NT_STATUS_NO_MEMORY;
2606 talloc_free(mem_ctx);
2607 return NT_STATUS_OK;
2610 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2611 const char *servername, int level, int *num_printers)
2613 struct spoolss_EnumPrinters r;
2617 r.in.flags = PRINTER_ENUM_LOCAL;
2618 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2623 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2624 if (!NT_STATUS_IS_OK(status)) {
2625 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2626 __location__, nt_errstr(status));
2630 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2631 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2632 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2633 win_errstr(r.out.result));
2637 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2638 if (blob.data == NULL) {
2639 d_printf("(%s) data_blob_talloc failed\n", __location__);
2643 r.in.buffer = &blob;
2644 r.in.offered = r.out.needed;
2646 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2647 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2648 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2649 "%s\n", __location__, nt_errstr(status),
2650 win_errstr(r.out.result));
2654 *num_printers = r.out.count;
2659 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2660 struct policy_handle *handle, int level,
2661 union spoolss_PrinterInfo **res)
2663 TALLOC_CTX *mem_ctx;
2664 struct spoolss_GetPrinter r;
2668 mem_ctx = talloc_new(ctx);
2669 if (mem_ctx == NULL) {
2670 return NT_STATUS_NO_MEMORY;
2673 r.in.handle = handle;
2678 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2679 if (!NT_STATUS_IS_OK(status)) {
2680 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2681 __location__, nt_errstr(status));
2682 talloc_free(mem_ctx);
2686 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2687 printf("GetPrinter unexpected return code %s, should "
2688 "be WERR_INSUFFICIENT_BUFFER\n",
2689 win_errstr(r.out.result));
2690 talloc_free(mem_ctx);
2691 return NT_STATUS_UNSUCCESSFUL;
2694 r.in.handle = handle;
2696 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2697 if (blob.data == NULL) {
2698 talloc_free(mem_ctx);
2699 return NT_STATUS_NO_MEMORY;
2701 memset(blob.data, 0, blob.length);
2702 r.in.buffer = &blob;
2703 r.in.offered = r.out.needed;
2705 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2706 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2707 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2708 "%s\n", __location__, nt_errstr(status),
2709 win_errstr(r.out.result));
2710 talloc_free(mem_ctx);
2711 return NT_STATUS_IS_OK(status) ?
2712 NT_STATUS_UNSUCCESSFUL : status;
2716 *res = talloc_steal(ctx, r.out.info);
2719 talloc_free(mem_ctx);
2720 return NT_STATUS_OK;
2723 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2725 TALLOC_CTX *mem_ctx;
2727 struct smbcli_state *cli;
2728 struct dcerpc_pipe *p;
2730 struct policy_handle server_handle, printer_handle;
2731 const char **printers;
2733 struct spoolss_UserLevel1 userlevel1;
2736 if (!(mem_ctx = talloc_new(torture))) {
2740 if (!(torture_open_connection_share(
2741 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2742 "IPC$", torture->ev))) {
2743 d_printf("IPC$ connection failed\n");
2744 talloc_free(mem_ctx);
2748 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2749 if (!NT_STATUS_IS_OK(status)) {
2750 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2751 __location__, nt_errstr(status));
2752 talloc_free(mem_ctx);
2756 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2757 &printers, &num_printers))) {
2758 talloc_free(mem_ctx);
2762 if (num_printers == 0) {
2763 d_printf("Did not find printers\n");
2764 talloc_free(mem_ctx);
2768 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2769 &ndr_table_spoolss, &p);
2770 if (!NT_STATUS_IS_OK(status)) {
2771 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2773 talloc_free(mem_ctx);
2777 ZERO_STRUCT(userlevel1);
2778 userlevel1.client = talloc_asprintf(
2779 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2780 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2781 userlevel1.build = 2600;
2782 userlevel1.major = 3;
2783 userlevel1.minor = 0;
2784 userlevel1.processor = 0;
2787 struct spoolss_OpenPrinterEx r;
2790 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2792 r.in.datatype = NULL;
2793 r.in.access_mask = 0;
2795 r.in.userlevel.level1 = &userlevel1;
2796 r.out.handle = &server_handle;
2798 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2799 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2800 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2801 "%s, %s\n", __location__, nt_errstr(status),
2802 win_errstr(r.out.result));
2803 talloc_free(mem_ctx);
2809 struct spoolss_ClosePrinter r;
2811 r.in.handle = &server_handle;
2812 r.out.handle = &server_handle;
2814 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2815 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2816 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2817 "%s, %s\n", __location__, nt_errstr(status),
2818 win_errstr(r.out.result));
2819 talloc_free(mem_ctx);
2825 struct spoolss_OpenPrinterEx r;
2828 r.in.printername = talloc_asprintf(
2829 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2830 r.in.datatype = NULL;
2831 r.in.access_mask = 0;
2833 r.in.userlevel.level1 = &userlevel1;
2834 r.out.handle = &printer_handle;
2836 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2837 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2838 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2839 "%s, %s\n", __location__, nt_errstr(status),
2840 win_errstr(r.out.result));
2841 talloc_free(mem_ctx);
2849 for (i=0; i<8; i++) {
2850 status = getprinterinfo(mem_ctx, p, &printer_handle,
2852 if (!NT_STATUS_IS_OK(status)) {
2853 d_printf("(%s) getprinterinfo %d failed: %s\n",
2854 __location__, i, nt_errstr(status));
2861 struct spoolss_ClosePrinter r;
2863 r.in.handle = &printer_handle;
2864 r.out.handle = &printer_handle;
2866 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2867 if (!NT_STATUS_IS_OK(status)) {
2868 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2869 "%s\n", __location__, nt_errstr(status));
2870 talloc_free(mem_ctx);
2877 if (!enumprinters(mem_ctx, p, servername, 1,
2879 d_printf("(%s) enumprinters failed\n", __location__);
2880 talloc_free(mem_ctx);
2883 if (num_printers != num_enumerated) {
2884 d_printf("(%s) netshareenum gave %d printers, "
2885 "enumprinters lvl 1 gave %d\n", __location__,
2886 num_printers, num_enumerated);
2887 talloc_free(mem_ctx);
2894 if (!enumprinters(mem_ctx, p, servername, 2,
2896 d_printf("(%s) enumprinters failed\n", __location__);
2897 talloc_free(mem_ctx);
2900 if (num_printers != num_enumerated) {
2901 d_printf("(%s) netshareenum gave %d printers, "
2902 "enumprinters lvl 2 gave %d\n", __location__,
2903 num_printers, num_enumerated);
2904 talloc_free(mem_ctx);
2909 talloc_free(mem_ctx);
2914 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2916 TALLOC_CTX *mem_ctx;
2917 struct smbcli_state *cli;
2918 struct dcerpc_pipe *p;
2922 if (!(mem_ctx = talloc_new(torture))) {
2926 if (!(torture_open_connection_share(
2927 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2928 "IPC$", torture->ev))) {
2929 d_printf("IPC$ connection failed\n");
2930 talloc_free(mem_ctx);
2934 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2935 if (!NT_STATUS_IS_OK(status)) {
2936 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2937 __location__, nt_errstr(status));
2938 talloc_free(mem_ctx);
2942 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2943 &ndr_table_wkssvc, &p);
2944 if (!NT_STATUS_IS_OK(status)) {
2945 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2947 talloc_free(mem_ctx);
2952 struct wkssvc_NetWkstaInfo100 wks100;
2953 union wkssvc_NetWkstaInfo info;
2954 struct wkssvc_NetWkstaGetInfo r;
2956 r.in.server_name = "\\foo";
2958 info.info100 = &wks100;
2961 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2962 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2963 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2964 "%s, %s\n", __location__, nt_errstr(status),
2965 win_errstr(r.out.result));
2966 talloc_free(mem_ctx);
2970 if (strcmp(servername,
2971 r.out.info->info100->server_name) != 0) {
2972 d_printf("(%s) servername inconsistency: RAP=%s, "
2973 "dcerpc_wkssvc_NetWksGetInfo=%s",
2974 __location__, servername,
2975 r.out.info->info100->server_name);
2976 talloc_free(mem_ctx);
2981 talloc_free(mem_ctx);
2985 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2986 struct policy_handle *handle)
2988 struct winreg_CloseKey c;
2990 TALLOC_CTX *mem_ctx;
2992 c.in.handle = c.out.handle = handle;
2994 if (!(mem_ctx = talloc_new(p))) {
2995 return NT_STATUS_NO_MEMORY;
2998 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2999 talloc_free(mem_ctx);
3001 if (!NT_STATUS_IS_OK(status)) {
3005 if (!W_ERROR_IS_OK(c.out.result)) {
3006 return werror_to_ntstatus(c.out.result);
3009 return NT_STATUS_OK;
3012 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
3013 TALLOC_CTX *mem_ctx)
3015 uint32_t enum_index = 0;
3018 struct winreg_EnumValue r;
3019 struct winreg_StringBuf name;
3020 enum winreg_Type type = 0;
3023 uint32_t size, length;
3025 r.in.handle = handle;
3026 r.in.enum_index = enum_index;
3029 r.in.name = r.out.name = &name;
3035 r.in.length = &length;
3037 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
3038 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3039 return NT_STATUS_OK;
3045 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
3046 TALLOC_CTX *mem_ctx, int depth)
3048 struct winreg_EnumKey r;
3049 struct winreg_StringBuf class, name;
3054 return NT_STATUS_OK;
3060 r.in.handle = handle;
3061 r.in.enum_index = 0;
3063 r.in.keyclass = &class;
3065 r.in.last_changed_time = &t;
3068 TALLOC_CTX *tmp_ctx;
3069 struct winreg_OpenKey o;
3070 struct policy_handle key_handle;
3073 if (!(tmp_ctx = talloc_new(mem_ctx))) {
3074 return NT_STATUS_NO_MEMORY;
3080 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
3081 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3082 /* We're done enumerating */
3083 talloc_free(tmp_ctx);
3084 return NT_STATUS_OK;
3087 for (i=0; i<10-depth; i++)
3089 printf("%s\n", r.out.name->name);
3092 o.in.parent_handle = handle;
3093 o.in.keyname.name = r.out.name->name;
3095 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3096 o.out.handle = &key_handle;
3098 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
3099 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3100 enumkeys(p, &key_handle, tmp_ctx, depth-1);
3101 enumvalues(p, &key_handle, tmp_ctx);
3102 status = winreg_close(p, &key_handle);
3103 if (!NT_STATUS_IS_OK(status)) {
3108 talloc_free(tmp_ctx);
3110 r.in.enum_index += 1;
3113 return NT_STATUS_OK;
3116 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3118 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3119 const char *name, winreg_open_fn open_fn)
3121 struct policy_handle handle;
3122 struct winreg_OpenHKLM r;
3125 r.in.system_name = 0;
3126 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3127 r.out.handle = &handle;
3129 status = open_fn(p, mem_ctx, &r);
3130 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3131 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3132 nt_errstr(status), win_errstr(r.out.result));
3136 enumkeys(p, &handle, mem_ctx, 4);
3138 status = winreg_close(p, &handle);
3139 if (!NT_STATUS_IS_OK(status)) {
3140 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3141 __location__, nt_errstr(status));
3148 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3151 struct dcerpc_pipe *p;
3152 TALLOC_CTX *mem_ctx;
3158 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3159 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3160 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3161 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3162 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3167 mem_ctx = talloc_init("torture_rpc_winreg");
3169 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3171 if (!NT_STATUS_IS_OK(status)) {
3172 talloc_free(mem_ctx);
3177 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3179 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3180 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3185 talloc_free(mem_ctx);
3190 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3191 struct loadparm_context *lp_ctx,
3192 struct smbcli_state *cli,
3194 struct srvsvc_NetShareInfo502 **info502)
3196 struct smbcli_tree *ipc;
3197 struct dcerpc_pipe *p;
3198 struct srvsvc_NetShareGetInfo r;
3199 union srvsvc_NetShareInfo info;
3202 if (!(p = dcerpc_pipe_init(cli,
3203 cli->transport->socket->event.ctx,
3204 lp_iconv_convenience(lp_ctx)))) {
3205 status = NT_STATUS_NO_MEMORY;
3209 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3210 if (!NT_STATUS_IS_OK(status)) {
3214 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3215 if (!NT_STATUS_IS_OK(status)) {
3216 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3221 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3222 if (!NT_STATUS_IS_OK(status)) {
3223 d_printf("dcerpc_bind_auth_none failed: %s\n",
3228 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3229 dcerpc_server_name(p));
3230 r.in.share_name = share;
3234 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3235 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3236 d_printf("(%s) srvsvc_NetShareGetInfo failed: %s, %s\n", __location__,
3237 nt_errstr(status), win_errstr(r.out.result));
3241 *info502 = talloc_move(mem_ctx, &info.info502);
3242 return NT_STATUS_OK;
3250 * Get us a handle on HKLM\
3253 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3254 struct smbcli_state *cli,
3255 struct smb_iconv_convenience *iconv_convenience,
3256 struct dcerpc_pipe **pipe_p,
3257 struct policy_handle **handle)
3259 struct smbcli_tree *ipc;
3260 struct dcerpc_pipe *p;
3261 struct winreg_OpenHKLM r;
3263 struct policy_handle *result;
3265 result = talloc(mem_ctx, struct policy_handle);
3267 if (result == NULL) {
3268 return NT_STATUS_NO_MEMORY;
3271 if (!(p = dcerpc_pipe_init(result,
3272 cli->transport->socket->event.ctx,
3273 iconv_convenience))) {
3274 status = NT_STATUS_NO_MEMORY;
3278 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3279 if (!NT_STATUS_IS_OK(status)) {
3283 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3284 if (!NT_STATUS_IS_OK(status)) {
3285 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3290 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3291 if (!NT_STATUS_IS_OK(status)) {
3292 d_printf("dcerpc_bind_auth_none failed: %s\n",
3297 r.in.system_name = 0;
3298 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3299 r.out.handle = result;
3301 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3302 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3303 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3304 nt_errstr(status), win_errstr(r.out.result));
3310 return NT_STATUS_OK;
3313 talloc_free(result);
3317 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3318 struct smb_iconv_convenience *iconv_convenience,
3319 const char *sharename)
3321 struct dcerpc_pipe *p;
3322 struct policy_handle *hklm = NULL;
3323 struct policy_handle new_handle;
3324 struct winreg_CreateKey c;
3325 struct winreg_CloseKey cl;
3326 enum winreg_CreateAction action_taken;
3328 TALLOC_CTX *mem_ctx;
3330 mem_ctx = talloc_new(cli);
3331 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3333 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3334 if (!NT_STATUS_IS_OK(status)) {
3335 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3340 c.in.name.name = talloc_asprintf(
3341 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3342 if (c.in.name.name == NULL) {
3343 d_printf("talloc_asprintf failed\n");
3344 status = NT_STATUS_NO_MEMORY;
3347 c.in.keyclass.name = "";
3349 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3350 c.in.secdesc = NULL;
3351 c.in.action_taken = &action_taken;
3352 c.out.new_handle = &new_handle;
3353 c.out.action_taken = &action_taken;
3355 status = dcerpc_winreg_CreateKey(p, p, &c);
3356 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3357 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3358 nt_errstr(status), win_errstr(c.out.result));
3362 cl.in.handle = &new_handle;
3363 cl.out.handle = &new_handle;
3364 status = dcerpc_winreg_CloseKey(p, p, &cl);
3365 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3366 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3367 nt_errstr(status), win_errstr(cl.out.result));
3373 talloc_free(mem_ctx);
3377 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3378 struct smbcli_state *cli,
3379 const char *sharename)
3381 struct dcerpc_pipe *p;
3382 struct policy_handle *hklm = NULL;
3383 struct winreg_DeleteKey d;
3385 TALLOC_CTX *mem_ctx;
3387 mem_ctx = talloc_new(cli);
3388 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3390 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3392 if (!NT_STATUS_IS_OK(status)) {
3393 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3398 d.in.key.name = talloc_asprintf(
3399 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3400 if (d.in.key.name == NULL) {
3401 d_printf("talloc_asprintf failed\n");
3402 status = NT_STATUS_NO_MEMORY;
3405 status = dcerpc_winreg_DeleteKey(p, p, &d);
3406 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3407 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3408 nt_errstr(status), win_errstr(d.out.result));
3413 talloc_free(mem_ctx);
3417 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3418 struct loadparm_context *lp_ctx,
3419 const char *sharename,
3420 const char *parameter,
3423 struct dcerpc_pipe *p = NULL;
3424 struct policy_handle *hklm = NULL, key_handle;
3425 struct winreg_OpenKey o;
3426 struct winreg_SetValue s;
3431 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3432 if (!NT_STATUS_IS_OK(status)) {
3433 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3437 o.in.parent_handle = hklm;
3438 o.in.keyname.name = talloc_asprintf(
3439 hklm, "software\\samba\\smbconf\\%s", sharename);
3440 if (o.in.keyname.name == NULL) {
3441 d_printf("talloc_asprintf failed\n");
3442 status = NT_STATUS_NO_MEMORY;
3446 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3447 o.out.handle = &key_handle;
3449 status = dcerpc_winreg_OpenKey(p, p, &o);
3450 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3451 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3452 nt_errstr(status), win_errstr(o.out.result));
3456 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3457 value, &type, &val)) {
3458 d_printf("(%s) reg_string_to_val failed\n", __location__);
3462 s.in.handle = &key_handle;
3463 s.in.name.name = parameter;
3465 s.in.data = val.data;
3466 s.in.size = val.length;
3468 status = dcerpc_winreg_SetValue(p, p, &s);
3469 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3470 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3471 nt_errstr(status), win_errstr(s.out.result));
3480 bool torture_samba3_regconfig(struct torture_context *torture)
3482 struct smbcli_state *cli;
3483 struct srvsvc_NetShareInfo502 *i = NULL;
3486 const char *comment = "Dummer Kommentar";
3488 if (!(torture_open_connection(&cli, torture, 0))) {
3492 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3493 if (!NT_STATUS_IS_OK(status)) {
3494 torture_warning(torture, "torture_samba3_createshare failed: "
3495 "%s\n", nt_errstr(status));
3499 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3500 if (!NT_STATUS_IS_OK(status)) {
3501 torture_warning(torture, "torture_samba3_setconfig failed: "
3502 "%s\n", nt_errstr(status));
3506 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3507 if (!NT_STATUS_IS_OK(status)) {
3508 torture_warning(torture, "get_shareinfo failed: "
3509 "%s\n", nt_errstr(status));
3513 if (strcmp(comment, i->comment) != 0) {
3514 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3515 comment, i->comment);
3519 status = torture_samba3_deleteshare(torture, cli, "blubber");
3520 if (!NT_STATUS_IS_OK(status)) {
3521 torture_warning(torture, "torture_samba3_deleteshare failed: "
3522 "%s\n", nt_errstr(status));