2 Unix SMB/CIFS implementation.
4 dcerpc torture tests, designed to walk Samba3 code paths
6 Copyright (C) Volker Lendecke 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/rap/rap.h"
26 #include "torture/torture.h"
27 #include "torture/util.h"
28 #include "torture/rap/proto.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "librpc/gen_ndr/ndr_lsa_c.h"
31 #include "librpc/gen_ndr/ndr_samr.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "librpc/gen_ndr/ndr_netlogon.h"
34 #include "librpc/gen_ndr/ndr_netlogon_c.h"
35 #include "librpc/gen_ndr/ndr_srvsvc.h"
36 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
37 #include "librpc/gen_ndr/ndr_spoolss.h"
38 #include "librpc/gen_ndr/ndr_spoolss_c.h"
39 #include "librpc/gen_ndr/ndr_winreg.h"
40 #include "librpc/gen_ndr/ndr_winreg_c.h"
41 #include "librpc/gen_ndr/ndr_wkssvc.h"
42 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
43 #include "lib/cmdline/popt_common.h"
44 #include "librpc/rpc/dcerpc.h"
45 #include "torture/rpc/rpc.h"
46 #include "libcli/libcli.h"
47 #include "libcli/composite/composite.h"
48 #include "libcli/smb_composite/smb_composite.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "../lib/crypto/crypto.h"
51 #include "auth/ntlmssp/ntlmssp.h"
52 #include "libcli/security/proto.h"
53 #include "param/param.h"
54 #include "lib/registry/registry.h"
55 #include "libcli/resolve/resolve.h"
58 * This tests a RPC call using an invalid vuid
61 bool torture_bind_authcontext(struct torture_context *torture)
66 struct lsa_ObjectAttribute objectattr;
67 struct lsa_OpenPolicy2 openpolicy;
68 struct policy_handle handle;
69 struct lsa_Close close_handle;
70 struct smbcli_session *tmp;
71 struct smbcli_session *session2;
72 struct smbcli_state *cli;
73 struct dcerpc_pipe *lsa_pipe;
74 struct cli_credentials *anon_creds;
75 struct smb_composite_sesssetup setup;
76 struct smbcli_options options;
77 struct smbcli_session_options session_options;
79 mem_ctx = talloc_init("torture_bind_authcontext");
81 if (mem_ctx == NULL) {
82 d_printf("talloc_init failed\n");
86 lp_smbcli_options(torture->lp_ctx, &options);
87 lp_smbcli_session_options(torture->lp_ctx, &session_options);
89 status = smbcli_full_connection(mem_ctx, &cli,
90 torture_setting_string(torture, "host", NULL),
91 lp_smb_ports(torture->lp_ctx),
92 "IPC$", NULL, cmdline_credentials,
93 lp_resolve_context(torture->lp_ctx),
94 torture->ev, &options, &session_options,
95 lp_iconv_convenience(torture->lp_ctx));
96 if (!NT_STATUS_IS_OK(status)) {
97 d_printf("smbcli_full_connection failed: %s\n",
102 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
103 lp_iconv_convenience(torture->lp_ctx));
104 if (lsa_pipe == NULL) {
105 d_printf("dcerpc_pipe_init failed\n");
109 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
110 if (!NT_STATUS_IS_OK(status)) {
111 d_printf("dcerpc_pipe_open_smb failed: %s\n",
116 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
117 if (!NT_STATUS_IS_OK(status)) {
118 d_printf("dcerpc_bind_auth_none failed: %s\n",
123 openpolicy.in.system_name =talloc_asprintf(
124 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
125 ZERO_STRUCT(objectattr);
126 openpolicy.in.attr = &objectattr;
127 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
128 openpolicy.out.handle = &handle;
130 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
132 if (!NT_STATUS_IS_OK(status)) {
133 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
138 close_handle.in.handle = &handle;
139 close_handle.out.handle = &handle;
141 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
142 if (!NT_STATUS_IS_OK(status)) {
143 d_printf("dcerpc_lsa_Close failed: %s\n",
148 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
149 if (session2 == NULL) {
150 d_printf("smbcli_session_init failed\n");
154 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
155 d_printf("create_anon_creds failed\n");
159 setup.in.sesskey = cli->transport->negotiate.sesskey;
160 setup.in.capabilities = cli->transport->negotiate.capabilities;
161 setup.in.workgroup = "";
162 setup.in.credentials = anon_creds;
164 status = smb_composite_sesssetup(session2, &setup);
165 if (!NT_STATUS_IS_OK(status)) {
166 d_printf("anon session setup failed: %s\n",
170 session2->vuid = setup.out.vuid;
172 tmp = cli->tree->session;
173 cli->tree->session = session2;
175 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
177 cli->tree->session = tmp;
178 talloc_free(lsa_pipe);
181 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
182 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
183 "expected NT_STATUS_INVALID_HANDLE\n",
190 talloc_free(mem_ctx);
195 * Bind to lsa using a specific auth method
198 static bool bindtest(struct smbcli_state *cli,
199 struct cli_credentials *credentials,
200 struct loadparm_context *lp_ctx,
201 uint8_t auth_type, uint8_t auth_level)
207 struct dcerpc_pipe *lsa_pipe;
208 struct lsa_ObjectAttribute objectattr;
209 struct lsa_OpenPolicy2 openpolicy;
210 struct lsa_QueryInfoPolicy query;
211 union lsa_PolicyInformation *info = NULL;
212 struct policy_handle handle;
213 struct lsa_Close close_handle;
215 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
216 d_printf("talloc_init failed\n");
220 lsa_pipe = dcerpc_pipe_init(mem_ctx,
221 cli->transport->socket->event.ctx,
222 lp_iconv_convenience(lp_ctx));
223 if (lsa_pipe == NULL) {
224 d_printf("dcerpc_pipe_init failed\n");
228 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
229 if (!NT_STATUS_IS_OK(status)) {
230 d_printf("dcerpc_pipe_open_smb failed: %s\n",
235 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
236 credentials, lp_ctx, auth_type, auth_level,
238 if (!NT_STATUS_IS_OK(status)) {
239 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
243 openpolicy.in.system_name =talloc_asprintf(
244 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
245 ZERO_STRUCT(objectattr);
246 openpolicy.in.attr = &objectattr;
247 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
248 openpolicy.out.handle = &handle;
250 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
252 if (!NT_STATUS_IS_OK(status)) {
253 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
258 query.in.handle = &handle;
259 query.in.level = LSA_POLICY_INFO_DOMAIN;
260 query.out.info = &info;
262 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
263 if (!NT_STATUS_IS_OK(status)) {
264 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
269 close_handle.in.handle = &handle;
270 close_handle.out.handle = &handle;
272 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
273 if (!NT_STATUS_IS_OK(status)) {
274 d_printf("dcerpc_lsa_Close failed: %s\n",
281 talloc_free(mem_ctx);
286 * test authenticated RPC binds with the variants Samba3 does support
289 bool torture_bind_samba3(struct torture_context *torture)
294 struct smbcli_state *cli;
295 struct smbcli_options options;
296 struct smbcli_session_options session_options;
298 mem_ctx = talloc_init("torture_bind_authcontext");
300 if (mem_ctx == NULL) {
301 d_printf("talloc_init failed\n");
305 lp_smbcli_options(torture->lp_ctx, &options);
306 lp_smbcli_session_options(torture->lp_ctx, &session_options);
308 status = smbcli_full_connection(mem_ctx, &cli,
309 torture_setting_string(torture, "host", NULL),
310 lp_smb_ports(torture->lp_ctx),
311 "IPC$", NULL, cmdline_credentials,
312 lp_resolve_context(torture->lp_ctx),
313 torture->ev, &options, &session_options,
314 lp_iconv_convenience(torture->lp_ctx));
315 if (!NT_STATUS_IS_OK(status)) {
316 d_printf("smbcli_full_connection failed: %s\n",
323 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
324 DCERPC_AUTH_LEVEL_INTEGRITY);
325 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
326 DCERPC_AUTH_LEVEL_PRIVACY);
327 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
328 DCERPC_AUTH_LEVEL_INTEGRITY);
329 ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
330 DCERPC_AUTH_LEVEL_PRIVACY);
333 talloc_free(mem_ctx);
338 * Lookup or create a user and return all necessary info
341 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
343 struct loadparm_context *lp_ctx,
344 struct cli_credentials *admin_creds,
347 const char *username,
349 struct dcerpc_pipe **result_pipe,
350 struct policy_handle **result_handle,
351 struct dom_sid **sid)
353 struct dcerpc_pipe *samr_pipe;
355 struct policy_handle conn_handle;
356 struct policy_handle domain_handle;
357 struct policy_handle *user_handle;
358 struct samr_Connect2 conn;
359 struct samr_EnumDomains enumdom;
360 uint32_t resume_handle = 0;
361 struct samr_LookupDomain l;
363 struct lsa_String domain_name;
364 struct lsa_String user_name;
365 struct samr_OpenDomain o;
366 struct samr_CreateUser2 c;
367 uint32_t user_rid,access_granted;
369 samr_pipe = dcerpc_pipe_init(mem_ctx,
370 cli->transport->socket->event.ctx,
371 lp_iconv_convenience(lp_ctx));
372 if (samr_pipe == NULL) {
373 d_printf("dcerpc_pipe_init failed\n");
374 status = NT_STATUS_NO_MEMORY;
378 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
379 if (!NT_STATUS_IS_OK(status)) {
380 d_printf("dcerpc_pipe_open_smb failed: %s\n",
385 if (admin_creds != NULL) {
386 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
387 admin_creds, lp_ctx, auth_type, auth_level,
389 if (!NT_STATUS_IS_OK(status)) {
390 d_printf("dcerpc_bind_auth failed: %s\n",
395 /* We must have an authenticated SMB connection */
396 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
397 if (!NT_STATUS_IS_OK(status)) {
398 d_printf("dcerpc_bind_auth_none failed: %s\n",
404 conn.in.system_name = talloc_asprintf(
405 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
406 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
407 conn.out.connect_handle = &conn_handle;
409 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
410 if (!NT_STATUS_IS_OK(status)) {
411 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
415 enumdom.in.connect_handle = &conn_handle;
416 enumdom.in.resume_handle = &resume_handle;
417 enumdom.in.buf_size = (uint32_t)-1;
418 enumdom.out.resume_handle = &resume_handle;
420 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
421 if (!NT_STATUS_IS_OK(status)) {
422 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
426 if (enumdom.out.num_entries != 2) {
427 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
428 enumdom.out.num_entries);
429 status = NT_STATUS_UNSUCCESSFUL;
433 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
436 l.in.connect_handle = &conn_handle;
437 domain_name.string = enumdom.out.sam->entries[dom_idx].name.string;
438 *domain = talloc_strdup(mem_ctx, domain_name.string);
439 l.in.domain_name = &domain_name;
441 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
442 if (!NT_STATUS_IS_OK(status)) {
443 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
447 o.in.connect_handle = &conn_handle;
448 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
449 o.in.sid = l.out.sid;
450 o.out.domain_handle = &domain_handle;
452 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
453 if (!NT_STATUS_IS_OK(status)) {
454 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
458 c.in.domain_handle = &domain_handle;
459 user_name.string = username;
460 c.in.account_name = &user_name;
461 c.in.acct_flags = ACB_NORMAL;
462 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
463 user_handle = talloc(mem_ctx, struct policy_handle);
464 c.out.user_handle = user_handle;
465 c.out.access_granted = &access_granted;
466 c.out.rid = &user_rid;
468 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
470 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
471 struct samr_LookupNames ln;
472 struct samr_OpenUser ou;
474 ln.in.domain_handle = &domain_handle;
476 ln.in.names = &user_name;
478 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
479 if (!NT_STATUS_IS_OK(status)) {
480 d_printf("samr_LookupNames failed: %s\n",
485 ou.in.domain_handle = &domain_handle;
486 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
487 user_rid = ou.in.rid = ln.out.rids.ids[0];
488 ou.out.user_handle = user_handle;
490 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
491 if (!NT_STATUS_IS_OK(status)) {
492 d_printf("samr_OpenUser failed: %s\n",
498 if (!NT_STATUS_IS_OK(status)) {
499 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
503 *result_pipe = samr_pipe;
504 *result_handle = user_handle;
506 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
518 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
519 struct loadparm_context *lp_ctx,
520 struct cli_credentials *admin_creds,
521 const char *username, const char *password,
523 struct dom_sid **user_sid)
527 struct dcerpc_pipe *samr_pipe;
528 struct policy_handle *wks_handle;
531 if (!(tmp_ctx = talloc_new(mem_ctx))) {
532 d_printf("talloc_init failed\n");
536 status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
537 DCERPC_AUTH_TYPE_NTLMSSP,
538 DCERPC_AUTH_LEVEL_INTEGRITY,
539 username, domain_name, &samr_pipe, &wks_handle,
541 if (!NT_STATUS_IS_OK(status)) {
542 d_printf("get_usr_handle failed: %s\n", nt_errstr(status));
547 struct samr_SetUserInfo2 sui2;
548 struct samr_SetUserInfo sui;
549 struct samr_QueryUserInfo qui;
550 union samr_UserInfo u_info;
551 DATA_BLOB session_key;
555 encode_pw_buffer(u_info.info23.password.data, password,
558 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
559 if (!NT_STATUS_IS_OK(status)) {
560 d_printf("dcerpc_fetch_session_key failed\n");
563 arcfour_crypt_blob(u_info.info23.password.data, 516,
565 u_info.info23.info.password_expired = 0;
566 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
567 SAMR_FIELD_PASSWORD2 |
568 SAMR_FIELD_EXPIRED_FLAG;
569 sui2.in.user_handle = wks_handle;
570 sui2.in.info = &u_info;
573 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
574 if (!NT_STATUS_IS_OK(status)) {
575 d_printf("samr_SetUserInfo(23) failed: %s\n",
580 u_info.info16.acct_flags = ACB_NORMAL;
581 sui.in.user_handle = wks_handle;
582 sui.in.info = &u_info;
585 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
586 if (!NT_STATUS_IS_OK(status)) {
587 d_printf("samr_SetUserInfo(16) failed\n");
591 qui.in.user_handle = wks_handle;
594 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
595 if (!NT_STATUS_IS_OK(status)) {
596 d_printf("samr_QueryUserInfo(21) failed\n");
600 qui.out.info->info21.allow_password_change = 0;
601 qui.out.info->info21.force_password_change = 0;
602 qui.out.info->info21.account_name.string = NULL;
603 qui.out.info->info21.rid = 0;
604 qui.out.info->info21.acct_expiry = 0;
605 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
607 u_info.info21 = qui.out.info->info21;
608 sui.in.user_handle = wks_handle;
609 sui.in.info = &u_info;
612 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
613 if (!NT_STATUS_IS_OK(status)) {
614 d_printf("samr_SetUserInfo(21) failed\n");
619 *domain_name= talloc_steal(mem_ctx, *domain_name);
620 *user_sid = talloc_steal(mem_ctx, *user_sid);
623 talloc_free(tmp_ctx);
631 static bool delete_user(struct smbcli_state *cli,
632 struct loadparm_context *lp_ctx,
633 struct cli_credentials *admin_creds,
634 const char *username)
639 struct dcerpc_pipe *samr_pipe;
640 struct policy_handle *user_handle;
643 if ((mem_ctx = talloc_init("leave")) == NULL) {
644 d_printf("talloc_init failed\n");
648 status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
649 DCERPC_AUTH_TYPE_NTLMSSP,
650 DCERPC_AUTH_LEVEL_INTEGRITY,
651 username, &dom_name, &samr_pipe,
654 if (!NT_STATUS_IS_OK(status)) {
655 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
660 struct samr_DeleteUser d;
662 d.in.user_handle = user_handle;
663 d.out.user_handle = user_handle;
665 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
666 if (!NT_STATUS_IS_OK(status)) {
667 d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
675 talloc_free(mem_ctx);
680 * Do a Samba3-style join
683 static bool join3(struct smbcli_state *cli,
684 struct loadparm_context *lp_ctx,
686 struct cli_credentials *admin_creds,
687 struct cli_credentials *wks_creds)
692 struct dcerpc_pipe *samr_pipe;
693 struct policy_handle *wks_handle;
695 NTTIME last_password_change;
697 if ((mem_ctx = talloc_init("join3")) == NULL) {
698 d_printf("talloc_init failed\n");
702 status = get_usr_handle(
703 cli, mem_ctx, lp_ctx, admin_creds,
704 DCERPC_AUTH_TYPE_NTLMSSP,
705 DCERPC_AUTH_LEVEL_PRIVACY,
706 talloc_asprintf(mem_ctx, "%s$",
707 cli_credentials_get_workstation(wks_creds)),
708 &dom_name, &samr_pipe, &wks_handle, NULL);
710 if (!NT_STATUS_IS_OK(status)) {
711 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
716 struct samr_QueryUserInfo q;
718 q.in.user_handle = wks_handle;
721 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
722 if (!NT_STATUS_IS_OK(status)) {
723 d_printf("(%s) QueryUserInfo failed: %s\n",
724 __location__, nt_errstr(status));
728 last_password_change = q.out.info->info21.last_password_change;
731 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
734 struct samr_SetUserInfo2 sui2;
735 union samr_UserInfo u_info;
736 struct samr_UserInfo21 *i21 = &u_info.info25.info;
737 DATA_BLOB session_key;
738 DATA_BLOB confounded_session_key = data_blob_talloc(
740 struct MD5Context ctx;
741 uint8_t confounder[16];
745 i21->full_name.string = talloc_asprintf(
747 cli_credentials_get_workstation(wks_creds));
748 i21->acct_flags = ACB_WSTRUST;
749 i21->fields_present = SAMR_FIELD_FULL_NAME |
750 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
752 encode_pw_buffer(u_info.info25.password.data,
753 cli_credentials_get_password(wks_creds),
755 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
756 if (!NT_STATUS_IS_OK(status)) {
757 d_printf("dcerpc_fetch_session_key failed: %s\n",
761 generate_random_buffer((uint8_t *)confounder, 16);
764 MD5Update(&ctx, confounder, 16);
765 MD5Update(&ctx, session_key.data, session_key.length);
766 MD5Final(confounded_session_key.data, &ctx);
768 arcfour_crypt_blob(u_info.info25.password.data, 516,
769 &confounded_session_key);
770 memcpy(&u_info.info25.password.data[516], confounder, 16);
772 sui2.in.user_handle = wks_handle;
774 sui2.in.info = &u_info;
776 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
777 if (!NT_STATUS_IS_OK(status)) {
778 d_printf("samr_SetUserInfo2(25) failed: %s\n",
783 struct samr_SetUserInfo2 sui2;
784 struct samr_SetUserInfo sui;
785 union samr_UserInfo u_info;
786 DATA_BLOB session_key;
788 encode_pw_buffer(u_info.info24.password.data,
789 cli_credentials_get_password(wks_creds),
791 u_info.info24.pw_len =
792 strlen_m(cli_credentials_get_password(wks_creds))*2;
794 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
795 if (!NT_STATUS_IS_OK(status)) {
796 d_printf("dcerpc_fetch_session_key failed\n");
799 arcfour_crypt_blob(u_info.info24.password.data, 516,
801 sui2.in.user_handle = wks_handle;
802 sui2.in.info = &u_info;
805 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
806 if (!NT_STATUS_IS_OK(status)) {
807 d_printf("samr_SetUserInfo(24) failed: %s\n",
812 u_info.info16.acct_flags = ACB_WSTRUST;
813 sui.in.user_handle = wks_handle;
814 sui.in.info = &u_info;
817 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
818 if (!NT_STATUS_IS_OK(status)) {
819 d_printf("samr_SetUserInfo(16) failed\n");
825 struct samr_QueryUserInfo q;
827 q.in.user_handle = wks_handle;
830 status = dcerpc_samr_QueryUserInfo(samr_pipe, mem_ctx, &q);
831 if (!NT_STATUS_IS_OK(status)) {
832 d_printf("(%s) QueryUserInfo failed: %s\n",
833 __location__, nt_errstr(status));
838 if (last_password_change
839 == q.out.info->info21.last_password_change) {
840 d_printf("(%s) last_password_change unchanged "
841 "during join, level25 must change "
842 "it\n", __location__);
847 if (last_password_change
848 != q.out.info->info21.last_password_change) {
849 d_printf("(%s) last_password_change changed "
850 "during join, level24 doesn't "
851 "change it\n", __location__);
860 talloc_free(mem_ctx);
865 * Do a ReqChallenge/Auth2 and get the wks creds
868 static bool auth2(struct smbcli_state *cli,
869 struct loadparm_context *lp_ctx,
870 struct cli_credentials *wks_cred)
873 struct dcerpc_pipe *net_pipe;
876 struct netr_ServerReqChallenge r;
877 struct netr_Credential netr_cli_creds;
878 struct netr_Credential netr_srv_creds;
879 uint32_t negotiate_flags;
880 struct netr_ServerAuthenticate2 a;
881 struct creds_CredentialState *creds_state;
882 struct netr_Credential netr_cred;
883 struct samr_Password mach_pw;
885 mem_ctx = talloc_new(NULL);
886 if (mem_ctx == NULL) {
887 d_printf("talloc_new failed\n");
891 net_pipe = dcerpc_pipe_init(mem_ctx,
892 cli->transport->socket->event.ctx,
893 lp_iconv_convenience(lp_ctx));
894 if (net_pipe == NULL) {
895 d_printf("dcerpc_pipe_init failed\n");
899 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
900 if (!NT_STATUS_IS_OK(status)) {
901 d_printf("dcerpc_pipe_open_smb failed: %s\n",
906 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
907 if (!NT_STATUS_IS_OK(status)) {
908 d_printf("dcerpc_bind_auth_none failed: %s\n",
913 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
914 r.in.server_name = talloc_asprintf(
915 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
916 if (r.in.server_name == NULL) {
917 d_printf("talloc_asprintf failed\n");
920 generate_random_buffer(netr_cli_creds.data,
921 sizeof(netr_cli_creds.data));
922 r.in.credentials = &netr_cli_creds;
923 r.out.return_credentials = &netr_srv_creds;
925 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
926 if (!NT_STATUS_IS_OK(status)) {
927 d_printf("netr_ServerReqChallenge failed: %s\n",
932 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
933 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
935 creds_state = talloc(mem_ctx, struct creds_CredentialState);
936 creds_client_init(creds_state, r.in.credentials,
937 r.out.return_credentials, &mach_pw,
938 &netr_cred, negotiate_flags);
940 a.in.server_name = talloc_asprintf(
941 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
942 a.in.account_name = talloc_asprintf(
943 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
944 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
945 a.in.secure_channel_type = SEC_CHAN_WKSTA;
946 a.in.negotiate_flags = &negotiate_flags;
947 a.out.negotiate_flags = &negotiate_flags;
948 a.in.credentials = &netr_cred;
949 a.out.return_credentials = &netr_cred;
951 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
952 if (!NT_STATUS_IS_OK(status)) {
953 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
958 if (!creds_client_check(creds_state, a.out.return_credentials)) {
959 d_printf("creds_client_check failed\n");
963 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
968 talloc_free(mem_ctx);
973 * Do a couple of schannel protected Netlogon ops: Interactive and Network
974 * login, and change the wks password
977 static bool schan(struct smbcli_state *cli,
978 struct loadparm_context *lp_ctx,
979 struct cli_credentials *wks_creds,
980 struct cli_credentials *user_creds)
985 struct dcerpc_pipe *net_pipe;
988 mem_ctx = talloc_new(NULL);
989 if (mem_ctx == NULL) {
990 d_printf("talloc_new failed\n");
994 net_pipe = dcerpc_pipe_init(mem_ctx,
995 cli->transport->socket->event.ctx,
996 lp_iconv_convenience(lp_ctx));
997 if (net_pipe == NULL) {
998 d_printf("dcerpc_pipe_init failed\n");
1002 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1003 if (!NT_STATUS_IS_OK(status)) {
1004 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1010 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
1011 DCERPC_DEBUG_PRINT_OUT;
1014 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
1015 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
1016 wks_creds, lp_ctx, DCERPC_AUTH_TYPE_SCHANNEL,
1017 DCERPC_AUTH_LEVEL_PRIVACY,
1020 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1028 for (i=2; i<4; i++) {
1030 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
1031 struct creds_CredentialState *creds_state;
1032 struct netr_Authenticator netr_auth, netr_auth2;
1033 struct netr_NetworkInfo ninfo;
1034 struct netr_PasswordInfo pinfo;
1035 struct netr_LogonSamLogon r;
1036 union netr_LogonLevel logon;
1037 union netr_Validation validation;
1038 uint8_t authoritative;
1039 struct netr_Authenticator return_authenticator;
1041 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1042 CLI_CRED_NTLMv2_AUTH;
1044 chal = data_blob_talloc(mem_ctx, NULL, 8);
1045 if (chal.data == NULL) {
1046 d_printf("data_blob_talloc failed\n");
1050 generate_random_buffer(chal.data, chal.length);
1051 names_blob = NTLMv2_generate_names_blob(
1053 cli_credentials_get_workstation(user_creds),
1054 cli_credentials_get_domain(user_creds));
1055 status = cli_credentials_get_ntlm_response(
1056 user_creds, mem_ctx, &flags, chal, names_blob,
1057 &lm_resp, &nt_resp, NULL, NULL);
1058 if (!NT_STATUS_IS_OK(status)) {
1059 d_printf("cli_credentials_get_ntlm_response failed:"
1060 " %s\n", nt_errstr(status));
1064 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1065 creds_client_authenticator(creds_state, &netr_auth);
1067 ninfo.identity_info.account_name.string =
1068 cli_credentials_get_username(user_creds);
1069 ninfo.identity_info.domain_name.string =
1070 cli_credentials_get_domain(user_creds);
1071 ninfo.identity_info.parameter_control = 0;
1072 ninfo.identity_info.logon_id_low = 0;
1073 ninfo.identity_info.logon_id_high = 0;
1074 ninfo.identity_info.workstation.string =
1075 cli_credentials_get_workstation(user_creds);
1076 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1077 ninfo.nt.length = nt_resp.length;
1078 ninfo.nt.data = nt_resp.data;
1079 ninfo.lm.length = lm_resp.length;
1080 ninfo.lm.data = lm_resp.data;
1082 logon.network = &ninfo;
1084 r.in.server_name = talloc_asprintf(
1085 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1086 ZERO_STRUCT(netr_auth2);
1087 r.in.computer_name =
1088 cli_credentials_get_workstation(wks_creds);
1089 r.in.credential = &netr_auth;
1090 r.in.return_authenticator = &netr_auth2;
1091 r.in.logon_level = 2;
1092 r.in.validation_level = i;
1093 r.in.logon = &logon;
1094 r.out.validation = &validation;
1095 r.out.authoritative = &authoritative;
1096 r.out.return_authenticator = &return_authenticator;
1098 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 d_printf("netr_LogonSamLogon failed: %s\n",
1105 if ((r.out.return_authenticator == NULL) ||
1106 (!creds_client_check(creds_state,
1107 &r.out.return_authenticator->cred))) {
1108 d_printf("Credentials check failed!\n");
1112 creds_client_authenticator(creds_state, &netr_auth);
1114 pinfo.identity_info = ninfo.identity_info;
1115 ZERO_STRUCT(pinfo.lmpassword.hash);
1116 E_md4hash(cli_credentials_get_password(user_creds),
1117 pinfo.ntpassword.hash);
1118 session_key = data_blob_talloc(mem_ctx,
1119 creds_state->session_key, 16);
1120 arcfour_crypt_blob(pinfo.ntpassword.hash,
1121 sizeof(pinfo.ntpassword.hash),
1124 logon.password = &pinfo;
1126 r.in.logon_level = 1;
1127 r.in.logon = &logon;
1128 r.out.return_authenticator = &return_authenticator;
1130 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1131 if (!NT_STATUS_IS_OK(status)) {
1132 d_printf("netr_LogonSamLogon failed: %s\n",
1137 if ((r.out.return_authenticator == NULL) ||
1138 (!creds_client_check(creds_state,
1139 &r.out.return_authenticator->cred))) {
1140 d_printf("Credentials check failed!\n");
1146 struct netr_ServerPasswordSet s;
1147 char *password = generate_random_str(wks_creds, 8);
1148 struct creds_CredentialState *creds_state;
1149 struct netr_Authenticator credential, return_authenticator;
1150 struct samr_Password new_password;
1152 s.in.server_name = talloc_asprintf(
1153 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1154 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1155 s.in.account_name = talloc_asprintf(
1156 mem_ctx, "%s$", s.in.computer_name);
1157 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1158 s.in.credential = &credential;
1159 s.in.new_password = &new_password;
1160 s.out.return_authenticator = &return_authenticator;
1162 E_md4hash(password, new_password.hash);
1164 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1165 creds_des_encrypt(creds_state, &new_password);
1166 creds_client_authenticator(creds_state, &credential);
1168 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1169 if (!NT_STATUS_IS_OK(status)) {
1170 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1174 if (!creds_client_check(creds_state,
1175 &s.out.return_authenticator->cred)) {
1176 printf("Credential chaining failed\n");
1179 cli_credentials_set_password(wks_creds, password,
1185 talloc_free(mem_ctx);
1190 * Delete the wks account again
1193 static bool leave(struct smbcli_state *cli,
1194 struct loadparm_context *lp_ctx,
1195 struct cli_credentials *admin_creds,
1196 struct cli_credentials *wks_creds)
1198 char *wks_name = talloc_asprintf(
1199 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1202 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1203 talloc_free(wks_name);
1208 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1211 bool torture_netlogon_samba3(struct torture_context *torture)
1213 TALLOC_CTX *mem_ctx;
1216 struct smbcli_state *cli;
1217 struct cli_credentials *anon_creds;
1218 struct cli_credentials *wks_creds;
1219 const char *wks_name;
1221 struct smbcli_options options;
1222 struct smbcli_session_options session_options;
1224 wks_name = torture_setting_string(torture, "wksname", NULL);
1225 if (wks_name == NULL) {
1226 wks_name = get_myname();
1229 mem_ctx = talloc_init("torture_netlogon_samba3");
1231 if (mem_ctx == NULL) {
1232 d_printf("talloc_init failed\n");
1236 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1237 d_printf("create_anon_creds failed\n");
1241 lp_smbcli_options(torture->lp_ctx, &options);
1242 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1244 status = smbcli_full_connection(mem_ctx, &cli,
1245 torture_setting_string(torture, "host", NULL),
1246 lp_smb_ports(torture->lp_ctx),
1247 "IPC$", NULL, anon_creds,
1248 lp_resolve_context(torture->lp_ctx),
1249 torture->ev, &options, &session_options,
1250 lp_iconv_convenience(torture->lp_ctx));
1251 if (!NT_STATUS_IS_OK(status)) {
1252 d_printf("smbcli_full_connection failed: %s\n",
1257 wks_creds = cli_credentials_init(mem_ctx);
1258 if (wks_creds == NULL) {
1259 d_printf("cli_credentials_init failed\n");
1263 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1264 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1265 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1266 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1267 cli_credentials_set_password(wks_creds,
1268 generate_random_str(wks_creds, 8),
1271 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1272 d_printf("join failed\n");
1276 cli_credentials_set_domain(
1277 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1280 for (i=0; i<2; i++) {
1282 /* Do this more than once, the routine "schan" changes
1283 * the workstation password using the netlogon
1284 * password change routine */
1288 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1289 d_printf("auth2 failed\n");
1293 for (j=0; j<2; j++) {
1294 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1295 d_printf("schan failed\n");
1301 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1302 d_printf("leave failed\n");
1309 talloc_free(mem_ctx);
1314 * Do a simple join, testjoin and leave using specified smb and samr
1318 static bool test_join3(struct torture_context *tctx,
1320 struct cli_credentials *smb_creds,
1321 struct cli_credentials *samr_creds,
1322 const char *wks_name)
1326 struct smbcli_state *cli;
1327 struct cli_credentials *wks_creds;
1328 struct smbcli_options options;
1329 struct smbcli_session_options session_options;
1331 lp_smbcli_options(tctx->lp_ctx, &options);
1332 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1334 status = smbcli_full_connection(tctx, &cli,
1335 torture_setting_string(tctx, "host", NULL),
1336 lp_smb_ports(tctx->lp_ctx),
1337 "IPC$", NULL, smb_creds,
1338 lp_resolve_context(tctx->lp_ctx),
1339 tctx->ev, &options, &session_options,
1340 lp_iconv_convenience(tctx->lp_ctx));
1341 if (!NT_STATUS_IS_OK(status)) {
1342 d_printf("smbcli_full_connection failed: %s\n",
1347 wks_creds = cli_credentials_init(cli);
1348 if (wks_creds == NULL) {
1349 d_printf("cli_credentials_init failed\n");
1353 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1354 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1355 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1356 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1357 cli_credentials_set_password(wks_creds,
1358 generate_random_str(wks_creds, 8),
1361 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1362 d_printf("join failed\n");
1366 cli_credentials_set_domain(
1367 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1370 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1371 d_printf("auth2 failed\n");
1375 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1376 d_printf("leave failed\n");
1389 * Test the different session key variants. Do it by joining, this uses the
1390 * session key in the setpassword routine. Test the join by doing the auth2.
1393 bool torture_samba3_sessionkey(struct torture_context *torture)
1396 struct cli_credentials *anon_creds;
1397 const char *wks_name;
1399 wks_name = torture_setting_string(torture, "wksname", get_myname());
1401 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1402 d_printf("create_anon_creds failed\n");
1406 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1410 if (!torture_setting_bool(torture, "samba3", false)) {
1412 /* Samba3 in the build farm right now does this happily. Need
1415 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1416 d_printf("join using anonymous bind on an anonymous smb "
1417 "connection succeeded -- HUH??\n");
1422 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1424 d_printf("join using ntlmssp bind on an anonymous smb "
1425 "connection failed\n");
1429 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1430 d_printf("join using anonymous bind on an authenticated smb "
1431 "connection failed\n");
1435 if (!test_join3(torture, false, cmdline_credentials,
1436 cmdline_credentials,
1438 d_printf("join using ntlmssp bind on an authenticated smb "
1439 "connection failed\n");
1444 * The following two are tests for setuserinfolevel 25
1447 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1449 d_printf("join using ntlmssp bind on an anonymous smb "
1450 "connection failed\n");
1454 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1455 d_printf("join using anonymous bind on an authenticated smb "
1456 "connection failed\n");
1466 * open pipe and bind, given an IPC$ context
1469 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1470 struct loadparm_context *lp_ctx,
1471 struct smbcli_tree *tree,
1472 const char *pipe_name,
1473 const struct ndr_interface_table *iface,
1474 struct dcerpc_pipe **p)
1476 struct dcerpc_pipe *result;
1479 if (!(result = dcerpc_pipe_init(
1480 mem_ctx, tree->session->transport->socket->event.ctx,
1481 lp_iconv_convenience(lp_ctx)))) {
1482 return NT_STATUS_NO_MEMORY;
1485 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1486 if (!NT_STATUS_IS_OK(status)) {
1487 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1489 talloc_free(result);
1493 status = dcerpc_bind_auth_none(result, iface);
1494 if (!NT_STATUS_IS_OK(status)) {
1495 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1496 talloc_free(result);
1501 return NT_STATUS_OK;
1505 * Sane wrapper around lsa_LookupNames
1508 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1509 struct dcerpc_pipe *p,
1513 struct lsa_ObjectAttribute attr;
1514 struct lsa_QosInfo qos;
1515 struct lsa_OpenPolicy2 r;
1518 struct policy_handle handle;
1519 struct lsa_LookupNames l;
1520 struct lsa_TransSidArray sids;
1521 struct lsa_RefDomainList *domains = NULL;
1522 struct lsa_String lsa_name;
1524 struct dom_sid *result;
1525 TALLOC_CTX *tmp_ctx;
1527 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1532 qos.impersonation_level = 2;
1533 qos.context_mode = 1;
1534 qos.effective_only = 0;
1537 attr.root_dir = NULL;
1538 attr.object_name = NULL;
1539 attr.attributes = 0;
1540 attr.sec_desc = NULL;
1541 attr.sec_qos = &qos;
1543 r.in.system_name = "\\";
1545 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1546 r.out.handle = &handle;
1548 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1549 if (!NT_STATUS_IS_OK(status)) {
1550 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1551 talloc_free(tmp_ctx);
1558 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1560 l.in.handle = &handle;
1562 l.in.names = &lsa_name;
1565 l.in.count = &count;
1566 l.out.count = &count;
1568 l.out.domains = &domains;
1570 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1571 if (!NT_STATUS_IS_OK(status)) {
1572 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1574 talloc_free(tmp_ctx);
1578 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1579 l.out.sids->sids[0].rid);
1581 c.in.handle = &handle;
1582 c.out.handle = &handle;
1584 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1585 if (!NT_STATUS_IS_OK(status)) {
1586 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1587 talloc_free(tmp_ctx);
1591 talloc_free(tmp_ctx);
1596 * Find out the user SID on this connection
1599 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1600 struct loadparm_context *lp_ctx,
1601 struct smbcli_tree *tree)
1603 struct dcerpc_pipe *lsa;
1604 struct lsa_GetUserName r;
1606 struct lsa_String *authority_name_p = NULL;
1607 struct lsa_String *account_name_p = NULL;
1608 struct dom_sid *result;
1610 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1611 &ndr_table_lsarpc, &lsa);
1612 if (!NT_STATUS_IS_OK(status)) {
1613 d_printf("(%s) Could not bind to LSA: %s\n",
1614 __location__, nt_errstr(status));
1618 r.in.system_name = "\\";
1619 r.in.account_name = &account_name_p;
1620 r.in.authority_name = &authority_name_p;
1621 r.out.account_name = &account_name_p;
1623 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1625 authority_name_p = *r.out.authority_name;
1627 if (!NT_STATUS_IS_OK(status)) {
1628 printf("(%s) GetUserName failed - %s\n",
1629 __location__, nt_errstr(status));
1634 result = name2sid(mem_ctx, lsa, account_name_p->string,
1635 authority_name_p->string);
1641 static int destroy_tree(struct smbcli_tree *tree)
1643 smb_tree_disconnect(tree);
1648 * Do a tcon, given a session
1651 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1652 struct smbcli_session *session,
1653 const char *sharename,
1654 struct smbcli_tree **res)
1656 struct smbcli_tree *result;
1657 TALLOC_CTX *tmp_ctx;
1658 union smb_tcon tcon;
1661 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1662 return NT_STATUS_NO_MEMORY;
1665 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1666 talloc_free(tmp_ctx);
1667 return NT_STATUS_NO_MEMORY;
1670 tcon.generic.level = RAW_TCON_TCONX;
1671 tcon.tconx.in.flags = 0;
1672 tcon.tconx.in.password = data_blob(NULL, 0);
1673 tcon.tconx.in.path = sharename;
1674 tcon.tconx.in.device = "?????";
1676 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1677 if (!NT_STATUS_IS_OK(status)) {
1678 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1680 talloc_free(tmp_ctx);
1684 result->tid = tcon.tconx.out.tid;
1685 result = talloc_steal(mem_ctx, result);
1686 talloc_set_destructor(result, destroy_tree);
1687 talloc_free(tmp_ctx);
1689 return NT_STATUS_OK;
1693 * Test the getusername behaviour
1696 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1699 struct smbcli_state *cli;
1700 TALLOC_CTX *mem_ctx;
1702 struct dom_sid *user_sid;
1703 struct dom_sid *created_sid;
1704 struct cli_credentials *anon_creds;
1705 struct cli_credentials *user_creds;
1707 struct smbcli_options options;
1708 struct smbcli_session_options session_options;
1710 if (!(mem_ctx = talloc_new(torture))) {
1714 lp_smbcli_options(torture->lp_ctx, &options);
1715 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1717 status = smbcli_full_connection(
1718 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1719 lp_smb_ports(torture->lp_ctx),
1720 "IPC$", NULL, cmdline_credentials,
1721 lp_resolve_context(torture->lp_ctx),
1722 torture->ev, &options, &session_options,
1723 lp_iconv_convenience(torture->lp_ctx));
1724 if (!NT_STATUS_IS_OK(status)) {
1725 d_printf("(%s) smbcli_full_connection failed: %s\n",
1726 __location__, nt_errstr(status));
1731 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1732 d_printf("(%s) whoami on auth'ed connection failed\n",
1739 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1740 d_printf("(%s) create_anon_creds failed\n", __location__);
1745 status = smbcli_full_connection(
1746 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1747 lp_smb_ports(torture->lp_ctx),
1748 "IPC$", NULL, anon_creds,
1749 lp_resolve_context(torture->lp_ctx),
1750 torture->ev, &options, &session_options,
1751 lp_iconv_convenience(torture->lp_ctx));
1752 if (!NT_STATUS_IS_OK(status)) {
1753 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1754 __location__, nt_errstr(status));
1759 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1760 d_printf("(%s) whoami on anon connection failed\n",
1766 if (!dom_sid_equal(user_sid,
1767 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1768 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1769 "S-1-5-7", __location__,
1770 dom_sid_string(mem_ctx, user_sid));
1774 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1775 d_printf("(%s) cli_credentials_init failed\n", __location__);
1780 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1781 cli_credentials_set_username(user_creds, "torture_username",
1783 cli_credentials_set_password(user_creds,
1784 generate_random_str(user_creds, 8),
1787 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1788 cli_credentials_get_username(user_creds),
1789 cli_credentials_get_password(user_creds),
1790 &domain_name, &created_sid)) {
1791 d_printf("(%s) create_user failed\n", __location__);
1796 cli_credentials_set_domain(user_creds, domain_name,
1800 struct smbcli_session *session2;
1801 struct smb_composite_sesssetup setup;
1802 struct smbcli_tree *tree;
1804 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1805 if (session2 == NULL) {
1806 d_printf("(%s) smbcli_session_init failed\n",
1811 setup.in.sesskey = cli->transport->negotiate.sesskey;
1812 setup.in.capabilities = cli->transport->negotiate.capabilities;
1813 setup.in.workgroup = "";
1814 setup.in.credentials = user_creds;
1816 status = smb_composite_sesssetup(session2, &setup);
1817 if (!NT_STATUS_IS_OK(status)) {
1818 d_printf("(%s) session setup with new user failed: "
1819 "%s\n", __location__, nt_errstr(status));
1823 session2->vuid = setup.out.vuid;
1825 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1827 d_printf("(%s) secondary_tcon failed\n",
1833 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1834 d_printf("(%s) whoami on user connection failed\n",
1843 d_printf("Created %s, found %s\n",
1844 dom_sid_string(mem_ctx, created_sid),
1845 dom_sid_string(mem_ctx, user_sid));
1847 if (!dom_sid_equal(created_sid, user_sid)) {
1852 if (!delete_user(cli, torture->lp_ctx,
1853 cmdline_credentials,
1854 cli_credentials_get_username(user_creds))) {
1855 d_printf("(%s) delete_user failed\n", __location__);
1860 talloc_free(mem_ctx);
1864 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1865 const char *sharename)
1868 struct srvsvc_NetShareGetInfo r;
1869 union srvsvc_NetShareInfo info;
1870 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1874 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1875 dcerpc_server_name(p));
1876 r.in.share_name = sharename;
1879 for (i=0;i<ARRAY_SIZE(levels);i++) {
1880 r.in.level = levels[i];
1882 printf("testing NetShareGetInfo level %u on share '%s'\n",
1883 r.in.level, r.in.share_name);
1885 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1886 if (!NT_STATUS_IS_OK(status)) {
1887 printf("NetShareGetInfo level %u on share '%s' failed"
1888 " - %s\n", r.in.level, r.in.share_name,
1893 if (!W_ERROR_IS_OK(r.out.result)) {
1894 printf("NetShareGetInfo level %u on share '%s' failed "
1895 "- %s\n", r.in.level, r.in.share_name,
1896 win_errstr(r.out.result));
1905 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1906 const char **one_sharename)
1909 struct srvsvc_NetShareEnum r;
1910 struct srvsvc_NetShareInfoCtr info_ctr;
1911 struct srvsvc_NetShareCtr0 c0;
1912 struct srvsvc_NetShareCtr1 c1;
1913 struct srvsvc_NetShareCtr2 c2;
1914 struct srvsvc_NetShareCtr501 c501;
1915 struct srvsvc_NetShareCtr502 c502;
1916 struct srvsvc_NetShareCtr1004 c1004;
1917 struct srvsvc_NetShareCtr1005 c1005;
1918 struct srvsvc_NetShareCtr1006 c1006;
1919 struct srvsvc_NetShareCtr1007 c1007;
1920 uint32_t totalentries = 0;
1921 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1925 ZERO_STRUCT(info_ctr);
1927 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1928 r.in.info_ctr = &info_ctr;
1929 r.in.max_buffer = (uint32_t)-1;
1930 r.in.resume_handle = NULL;
1931 r.out.totalentries = &totalentries;
1932 r.out.info_ctr = &info_ctr;
1934 for (i=0;i<ARRAY_SIZE(levels);i++) {
1935 info_ctr.level = levels[i];
1937 switch (info_ctr.level) {
1940 info_ctr.ctr.ctr0 = &c0;
1944 info_ctr.ctr.ctr1 = &c1;
1948 info_ctr.ctr.ctr2 = &c2;
1952 info_ctr.ctr.ctr501 = &c501;
1956 info_ctr.ctr.ctr502 = &c502;
1960 info_ctr.ctr.ctr1004 = &c1004;
1964 info_ctr.ctr.ctr1005 = &c1005;
1968 info_ctr.ctr.ctr1006 = &c1006;
1972 info_ctr.ctr.ctr1007 = &c1007;
1976 printf("testing NetShareEnum level %u\n", info_ctr.level);
1977 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1978 if (!NT_STATUS_IS_OK(status)) {
1979 printf("NetShareEnum level %u failed - %s\n",
1980 info_ctr.level, nt_errstr(status));
1984 if (!W_ERROR_IS_OK(r.out.result)) {
1985 printf("NetShareEnum level %u failed - %s\n",
1986 info_ctr.level, win_errstr(r.out.result));
1989 if (info_ctr.level == 0) {
1990 struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
1991 if (ctr->count > 0) {
1992 *one_sharename = ctr->array[0].name;
2000 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
2002 struct dcerpc_pipe *p;
2003 TALLOC_CTX *mem_ctx;
2005 const char *sharename = NULL;
2006 struct smbcli_state *cli;
2009 if (!(mem_ctx = talloc_new(torture))) {
2013 if (!(torture_open_connection_share(
2014 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2015 "IPC$", torture->ev))) {
2016 talloc_free(mem_ctx);
2020 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
2021 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
2022 if (!NT_STATUS_IS_OK(status)) {
2023 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2024 __location__, nt_errstr(status));
2029 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
2030 if (sharename == NULL) {
2031 printf("did not get sharename\n");
2033 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
2037 talloc_free(mem_ctx);
2042 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
2043 * NT_STATUS_NO_SAM_ACCOUNT
2046 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
2048 TALLOC_CTX *mem_ctx;
2049 struct dcerpc_pipe *net_pipe;
2051 bool result = false;
2053 struct netr_ServerReqChallenge r;
2054 struct netr_Credential netr_cli_creds;
2055 struct netr_Credential netr_srv_creds;
2056 uint32_t negotiate_flags;
2057 struct netr_ServerAuthenticate2 a;
2058 struct creds_CredentialState *creds_state;
2059 struct netr_Credential netr_cred;
2060 struct samr_Password mach_pw;
2061 struct smbcli_state *cli;
2063 if (!(mem_ctx = talloc_new(torture))) {
2064 d_printf("talloc_new failed\n");
2068 if (!(wksname = generate_random_str_list(
2069 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2070 d_printf("generate_random_str_list failed\n");
2074 if (!(torture_open_connection_share(
2076 torture, torture_setting_string(torture, "host", NULL),
2077 "IPC$", torture->ev))) {
2078 d_printf("IPC$ connection failed\n");
2082 if (!(net_pipe = dcerpc_pipe_init(
2083 mem_ctx, cli->transport->socket->event.ctx,
2084 lp_iconv_convenience(torture->lp_ctx)))) {
2085 d_printf("dcerpc_pipe_init failed\n");
2089 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2090 if (!NT_STATUS_IS_OK(status)) {
2091 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2096 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2097 if (!NT_STATUS_IS_OK(status)) {
2098 d_printf("dcerpc_bind_auth_none failed: %s\n",
2103 r.in.computer_name = wksname;
2104 r.in.server_name = talloc_asprintf(
2105 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2106 if (r.in.server_name == NULL) {
2107 d_printf("talloc_asprintf failed\n");
2110 generate_random_buffer(netr_cli_creds.data,
2111 sizeof(netr_cli_creds.data));
2112 r.in.credentials = &netr_cli_creds;
2113 r.out.return_credentials = &netr_srv_creds;
2115 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2116 if (!NT_STATUS_IS_OK(status)) {
2117 d_printf("netr_ServerReqChallenge failed: %s\n",
2122 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2123 E_md4hash("foobar", mach_pw.hash);
2125 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2126 creds_client_init(creds_state, r.in.credentials,
2127 r.out.return_credentials, &mach_pw,
2128 &netr_cred, negotiate_flags);
2130 a.in.server_name = talloc_asprintf(
2131 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2132 a.in.account_name = talloc_asprintf(
2133 mem_ctx, "%s$", wksname);
2134 a.in.computer_name = wksname;
2135 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2136 a.in.negotiate_flags = &negotiate_flags;
2137 a.out.negotiate_flags = &negotiate_flags;
2138 a.in.credentials = &netr_cred;
2139 a.out.return_credentials = &netr_cred;
2141 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2143 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2144 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2145 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2152 talloc_free(mem_ctx);
2156 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2157 struct loadparm_context *lp_ctx,
2158 struct smbcli_session *sess,
2159 const char *sharename)
2161 struct smbcli_tree *tree;
2162 TALLOC_CTX *tmp_ctx;
2163 struct dcerpc_pipe *p;
2165 struct srvsvc_NetShareGetInfo r;
2166 union srvsvc_NetShareInfo info;
2167 struct security_descriptor *result;
2169 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2170 d_printf("talloc_new failed\n");
2174 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2175 d_printf("secondary_tcon failed\n");
2176 talloc_free(tmp_ctx);
2180 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2181 &ndr_table_srvsvc, &p);
2182 if (!NT_STATUS_IS_OK(status)) {
2183 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2184 __location__, nt_errstr(status));
2185 talloc_free(tmp_ctx);
2190 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2193 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2194 dcerpc_server_name(p));
2195 r.in.share_name = sharename;
2199 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2200 if (!NT_STATUS_IS_OK(status)) {
2201 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2203 talloc_free(tmp_ctx);
2207 result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
2208 talloc_free(tmp_ctx);
2212 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2213 struct loadparm_context *lp_ctx,
2214 struct smbcli_session *sess,
2215 const char *sharename,
2216 struct security_descriptor *sd)
2218 struct smbcli_tree *tree;
2219 TALLOC_CTX *tmp_ctx;
2220 struct dcerpc_pipe *p;
2222 struct sec_desc_buf i;
2223 struct srvsvc_NetShareSetInfo r;
2224 union srvsvc_NetShareInfo info;
2227 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2228 d_printf("talloc_new failed\n");
2229 return NT_STATUS_NO_MEMORY;
2232 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2233 d_printf("secondary_tcon failed\n");
2234 talloc_free(tmp_ctx);
2235 return NT_STATUS_UNSUCCESSFUL;
2238 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2239 &ndr_table_srvsvc, &p);
2240 if (!NT_STATUS_IS_OK(status)) {
2241 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2242 __location__, nt_errstr(status));
2243 talloc_free(tmp_ctx);
2244 return NT_STATUS_UNSUCCESSFUL;
2248 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2251 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2252 dcerpc_server_name(p));
2253 r.in.share_name = sharename;
2258 r.in.parm_error = &error;
2260 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2261 if (!NT_STATUS_IS_OK(status)) {
2262 d_printf("srvsvc_NetShareSetInfo failed: %s\n",
2266 talloc_free(tmp_ctx);
2270 bool try_tcon(TALLOC_CTX *mem_ctx,
2271 struct loadparm_context *lp_ctx,
2272 struct security_descriptor *orig_sd,
2273 struct smbcli_session *session,
2274 const char *sharename, const struct dom_sid *user_sid,
2275 unsigned int access_mask, NTSTATUS expected_tcon,
2276 NTSTATUS expected_mkdir)
2278 TALLOC_CTX *tmp_ctx;
2279 struct smbcli_tree *rmdir_tree, *tree;
2280 struct dom_sid *domain_sid;
2282 struct security_descriptor *sd;
2286 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2287 d_printf("talloc_new failed\n");
2291 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2292 if (!NT_STATUS_IS_OK(status)) {
2293 d_printf("first tcon to delete dir failed\n");
2294 talloc_free(tmp_ctx);
2298 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2300 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2301 &domain_sid, &rid))) {
2302 d_printf("dom_sid_split_rid failed\n");
2303 talloc_free(tmp_ctx);
2307 sd = security_descriptor_dacl_create(
2308 tmp_ctx, 0, "S-1-5-32-544",
2309 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2311 dom_sid_string(mem_ctx, user_sid),
2312 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2314 d_printf("security_descriptor_dacl_create failed\n");
2315 talloc_free(tmp_ctx);
2319 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2320 if (!NT_STATUS_IS_OK(status)) {
2321 d_printf("custom set_sharesec failed: %s\n",
2323 talloc_free(tmp_ctx);
2327 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2328 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2329 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2335 if (!NT_STATUS_IS_OK(status)) {
2336 /* An expected non-access, no point in trying to write */
2340 status = smbcli_mkdir(tree, "sharesec_testdir");
2341 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2342 d_printf("(%s) Expected %s, got %s\n", __location__,
2343 nt_errstr(expected_mkdir), nt_errstr(status));
2348 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2350 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2351 if (!NT_STATUS_IS_OK(status)) {
2352 d_printf("custom set_sharesec failed: %s\n",
2354 talloc_free(tmp_ctx);
2358 talloc_free(tmp_ctx);
2362 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2364 TALLOC_CTX *mem_ctx;
2366 struct smbcli_state *cli;
2367 struct security_descriptor *sd;
2368 struct dom_sid *user_sid;
2370 if (!(mem_ctx = talloc_new(torture))) {
2374 if (!(torture_open_connection_share(
2375 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2376 "IPC$", torture->ev))) {
2377 d_printf("IPC$ connection failed\n");
2378 talloc_free(mem_ctx);
2382 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2383 d_printf("whoami failed\n");
2384 talloc_free(mem_ctx);
2388 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2389 torture_setting_string(torture, "share", NULL));
2391 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2392 torture_setting_string(torture, "share", NULL),
2393 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2395 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2396 torture_setting_string(torture, "share", NULL),
2397 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2398 NT_STATUS_MEDIA_WRITE_PROTECTED);
2400 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2401 torture_setting_string(torture, "share", NULL),
2402 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2404 talloc_free(mem_ctx);
2408 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2410 TALLOC_CTX *mem_ctx;
2412 struct smbcli_state *cli;
2413 struct dcerpc_pipe *p;
2414 struct policy_handle lsa_handle;
2416 struct dom_sid *domain_sid;
2418 if (!(mem_ctx = talloc_new(torture))) {
2422 if (!(torture_open_connection_share(
2423 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2424 "IPC$", torture->ev))) {
2425 d_printf("IPC$ connection failed\n");
2426 talloc_free(mem_ctx);
2430 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2431 &ndr_table_lsarpc, &p);
2432 if (!NT_STATUS_IS_OK(status)) {
2433 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2435 talloc_free(mem_ctx);
2440 struct lsa_ObjectAttribute attr;
2441 struct lsa_OpenPolicy2 o;
2442 o.in.system_name = talloc_asprintf(
2443 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2446 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2447 o.out.handle = &lsa_handle;
2448 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2449 if (!NT_STATUS_IS_OK(status)) {
2450 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2451 __location__, nt_errstr(status));
2452 talloc_free(mem_ctx);
2458 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2463 int levels[] = { 2,3,5,6 };
2465 for (i=0; i<ARRAY_SIZE(levels); i++) {
2466 struct lsa_QueryInfoPolicy r;
2467 union lsa_PolicyInformation *info = NULL;
2468 r.in.handle = &lsa_handle;
2469 r.in.level = levels[i];
2471 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2472 if (!NT_STATUS_IS_OK(status)) {
2473 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2474 "failed: %s\n", __location__,
2475 levels[i], nt_errstr(status));
2476 talloc_free(mem_ctx);
2479 if (levels[i] == 5) {
2480 domain_sid = info->account_domain.sid;
2488 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2489 struct smb_iconv_convenience *iconv_convenience,
2492 struct rap_WserverGetInfo r;
2494 char servername[17];
2497 r.in.bufsize = 0xffff;
2499 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2500 if (!NT_STATUS_IS_OK(status)) {
2504 memcpy(servername, r.out.info.info0.name, 16);
2505 servername[16] = '\0';
2507 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2508 return NT_STATUS_NO_MEMORY;
2511 return NT_STATUS_OK;
2515 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2516 struct smbcli_tree *tree,
2517 const char ***printers, int *num_printers)
2519 TALLOC_CTX *mem_ctx;
2521 struct dcerpc_pipe *p;
2522 struct srvsvc_NetShareEnum r;
2523 struct srvsvc_NetShareInfoCtr info_ctr;
2524 struct srvsvc_NetShareCtr1 c1_in;
2525 struct srvsvc_NetShareCtr1 *c1;
2526 uint32_t totalentries = 0;
2529 mem_ctx = talloc_new(ctx);
2530 if (mem_ctx == NULL) {
2531 return NT_STATUS_NO_MEMORY;
2534 status = pipe_bind_smb(mem_ctx, lp_ctx,
2535 tree, "\\srvsvc", &ndr_table_srvsvc,
2537 if (!NT_STATUS_IS_OK(status)) {
2538 d_printf("could not bind to srvsvc pipe\n");
2539 talloc_free(mem_ctx);
2545 info_ctr.ctr.ctr1 = &c1_in;
2547 r.in.server_unc = talloc_asprintf(
2548 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2549 r.in.info_ctr = &info_ctr;
2550 r.in.max_buffer = (uint32_t)-1;
2551 r.in.resume_handle = NULL;
2552 r.out.totalentries = &totalentries;
2553 r.out.info_ctr = &info_ctr;
2555 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2556 if (!NT_STATUS_IS_OK(status)) {
2557 d_printf("NetShareEnum level %u failed - %s\n",
2558 info_ctr.level, nt_errstr(status));
2559 talloc_free(mem_ctx);
2565 c1 = r.out.info_ctr->ctr.ctr1;
2566 for (i=0; i<c1->count; i++) {
2567 if (c1->array[i].type != STYPE_PRINTQ) {
2570 if (!add_string_to_array(ctx, c1->array[i].name,
2571 printers, num_printers)) {
2573 return NT_STATUS_NO_MEMORY;
2577 talloc_free(mem_ctx);
2578 return NT_STATUS_OK;
2581 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2582 const char *servername, int level, int *num_printers)
2584 struct spoolss_EnumPrinters r;
2588 r.in.flags = PRINTER_ENUM_LOCAL;
2589 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2594 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2595 if (!NT_STATUS_IS_OK(status)) {
2596 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2597 __location__, nt_errstr(status));
2601 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2602 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2603 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2604 win_errstr(r.out.result));
2608 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2609 if (blob.data == NULL) {
2610 d_printf("(%s) data_blob_talloc failed\n", __location__);
2614 r.in.buffer = &blob;
2615 r.in.offered = r.out.needed;
2617 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2618 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2619 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2620 "%s\n", __location__, nt_errstr(status),
2621 win_errstr(r.out.result));
2625 *num_printers = r.out.count;
2630 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2631 struct policy_handle *handle, int level,
2632 union spoolss_PrinterInfo **res)
2634 TALLOC_CTX *mem_ctx;
2635 struct spoolss_GetPrinter r;
2639 mem_ctx = talloc_new(ctx);
2640 if (mem_ctx == NULL) {
2641 return NT_STATUS_NO_MEMORY;
2644 r.in.handle = handle;
2649 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2650 if (!NT_STATUS_IS_OK(status)) {
2651 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2652 __location__, nt_errstr(status));
2653 talloc_free(mem_ctx);
2657 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2658 printf("GetPrinter unexpected return code %s, should "
2659 "be WERR_INSUFFICIENT_BUFFER\n",
2660 win_errstr(r.out.result));
2661 talloc_free(mem_ctx);
2662 return NT_STATUS_UNSUCCESSFUL;
2665 r.in.handle = handle;
2667 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2668 if (blob.data == NULL) {
2669 talloc_free(mem_ctx);
2670 return NT_STATUS_NO_MEMORY;
2672 memset(blob.data, 0, blob.length);
2673 r.in.buffer = &blob;
2674 r.in.offered = r.out.needed;
2676 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2677 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2678 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2679 "%s\n", __location__, nt_errstr(status),
2680 win_errstr(r.out.result));
2681 talloc_free(mem_ctx);
2682 return NT_STATUS_IS_OK(status) ?
2683 NT_STATUS_UNSUCCESSFUL : status;
2687 *res = talloc_steal(ctx, r.out.info);
2690 talloc_free(mem_ctx);
2691 return NT_STATUS_OK;
2694 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2696 TALLOC_CTX *mem_ctx;
2698 struct smbcli_state *cli;
2699 struct dcerpc_pipe *p;
2701 struct policy_handle server_handle, printer_handle;
2702 const char **printers;
2704 struct spoolss_UserLevel1 userlevel1;
2707 if (!(mem_ctx = talloc_new(torture))) {
2711 if (!(torture_open_connection_share(
2712 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2713 "IPC$", torture->ev))) {
2714 d_printf("IPC$ connection failed\n");
2715 talloc_free(mem_ctx);
2719 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2720 if (!NT_STATUS_IS_OK(status)) {
2721 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2722 __location__, nt_errstr(status));
2723 talloc_free(mem_ctx);
2727 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2728 &printers, &num_printers))) {
2729 talloc_free(mem_ctx);
2733 if (num_printers == 0) {
2734 d_printf("Did not find printers\n");
2735 talloc_free(mem_ctx);
2739 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2740 &ndr_table_spoolss, &p);
2741 if (!NT_STATUS_IS_OK(status)) {
2742 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2744 talloc_free(mem_ctx);
2748 ZERO_STRUCT(userlevel1);
2749 userlevel1.client = talloc_asprintf(
2750 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2751 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2752 userlevel1.build = 2600;
2753 userlevel1.major = 3;
2754 userlevel1.minor = 0;
2755 userlevel1.processor = 0;
2758 struct spoolss_OpenPrinterEx r;
2761 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2763 r.in.datatype = NULL;
2764 r.in.access_mask = 0;
2766 r.in.userlevel.level1 = &userlevel1;
2767 r.out.handle = &server_handle;
2769 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2770 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2771 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2772 "%s, %s\n", __location__, nt_errstr(status),
2773 win_errstr(r.out.result));
2774 talloc_free(mem_ctx);
2780 struct spoolss_ClosePrinter r;
2782 r.in.handle = &server_handle;
2783 r.out.handle = &server_handle;
2785 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2786 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2787 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2788 "%s, %s\n", __location__, nt_errstr(status),
2789 win_errstr(r.out.result));
2790 talloc_free(mem_ctx);
2796 struct spoolss_OpenPrinterEx r;
2799 r.in.printername = talloc_asprintf(
2800 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2801 r.in.datatype = NULL;
2802 r.in.access_mask = 0;
2804 r.in.userlevel.level1 = &userlevel1;
2805 r.out.handle = &printer_handle;
2807 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2808 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2809 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2810 "%s, %s\n", __location__, nt_errstr(status),
2811 win_errstr(r.out.result));
2812 talloc_free(mem_ctx);
2820 for (i=0; i<8; i++) {
2821 status = getprinterinfo(mem_ctx, p, &printer_handle,
2823 if (!NT_STATUS_IS_OK(status)) {
2824 d_printf("(%s) getprinterinfo %d failed: %s\n",
2825 __location__, i, nt_errstr(status));
2832 struct spoolss_ClosePrinter r;
2834 r.in.handle = &printer_handle;
2835 r.out.handle = &printer_handle;
2837 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2838 if (!NT_STATUS_IS_OK(status)) {
2839 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2840 "%s\n", __location__, nt_errstr(status));
2841 talloc_free(mem_ctx);
2848 if (!enumprinters(mem_ctx, p, servername, 1,
2850 d_printf("(%s) enumprinters failed\n", __location__);
2851 talloc_free(mem_ctx);
2854 if (num_printers != num_enumerated) {
2855 d_printf("(%s) netshareenum gave %d printers, "
2856 "enumprinters lvl 1 gave %d\n", __location__,
2857 num_printers, num_enumerated);
2858 talloc_free(mem_ctx);
2865 if (!enumprinters(mem_ctx, p, servername, 2,
2867 d_printf("(%s) enumprinters failed\n", __location__);
2868 talloc_free(mem_ctx);
2871 if (num_printers != num_enumerated) {
2872 d_printf("(%s) netshareenum gave %d printers, "
2873 "enumprinters lvl 2 gave %d\n", __location__,
2874 num_printers, num_enumerated);
2875 talloc_free(mem_ctx);
2880 talloc_free(mem_ctx);
2885 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2887 TALLOC_CTX *mem_ctx;
2888 struct smbcli_state *cli;
2889 struct dcerpc_pipe *p;
2893 if (!(mem_ctx = talloc_new(torture))) {
2897 if (!(torture_open_connection_share(
2898 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2899 "IPC$", torture->ev))) {
2900 d_printf("IPC$ connection failed\n");
2901 talloc_free(mem_ctx);
2905 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2906 if (!NT_STATUS_IS_OK(status)) {
2907 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2908 __location__, nt_errstr(status));
2909 talloc_free(mem_ctx);
2913 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2914 &ndr_table_wkssvc, &p);
2915 if (!NT_STATUS_IS_OK(status)) {
2916 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2918 talloc_free(mem_ctx);
2923 struct wkssvc_NetWkstaInfo100 wks100;
2924 union wkssvc_NetWkstaInfo info;
2925 struct wkssvc_NetWkstaGetInfo r;
2927 r.in.server_name = "\\foo";
2929 info.info100 = &wks100;
2932 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2933 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2934 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2935 "%s, %s\n", __location__, nt_errstr(status),
2936 win_errstr(r.out.result));
2937 talloc_free(mem_ctx);
2941 if (strcmp(servername,
2942 r.out.info->info100->server_name) != 0) {
2943 d_printf("(%s) servername inconsistency: RAP=%s, "
2944 "dcerpc_wkssvc_NetWksGetInfo=%s",
2945 __location__, servername,
2946 r.out.info->info100->server_name);
2947 talloc_free(mem_ctx);
2952 talloc_free(mem_ctx);
2956 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2957 struct policy_handle *handle)
2959 struct winreg_CloseKey c;
2961 TALLOC_CTX *mem_ctx;
2963 c.in.handle = c.out.handle = handle;
2965 if (!(mem_ctx = talloc_new(p))) {
2966 return NT_STATUS_NO_MEMORY;
2969 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2970 talloc_free(mem_ctx);
2972 if (!NT_STATUS_IS_OK(status)) {
2976 if (!W_ERROR_IS_OK(c.out.result)) {
2977 return werror_to_ntstatus(c.out.result);
2980 return NT_STATUS_OK;
2983 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2984 TALLOC_CTX *mem_ctx)
2986 uint32_t enum_index = 0;
2989 struct winreg_EnumValue r;
2990 struct winreg_StringBuf name;
2991 enum winreg_Type type = 0;
2994 uint32_t size, length;
2996 r.in.handle = handle;
2997 r.in.enum_index = enum_index;
3000 r.in.name = r.out.name = &name;
3006 r.in.length = &length;
3008 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
3009 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3010 return NT_STATUS_OK;
3016 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
3017 TALLOC_CTX *mem_ctx, int depth)
3019 struct winreg_EnumKey r;
3020 struct winreg_StringBuf class, name;
3025 return NT_STATUS_OK;
3031 r.in.handle = handle;
3032 r.in.enum_index = 0;
3034 r.in.keyclass = &class;
3036 r.in.last_changed_time = &t;
3039 TALLOC_CTX *tmp_ctx;
3040 struct winreg_OpenKey o;
3041 struct policy_handle key_handle;
3044 if (!(tmp_ctx = talloc_new(mem_ctx))) {
3045 return NT_STATUS_NO_MEMORY;
3051 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
3052 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3053 /* We're done enumerating */
3054 talloc_free(tmp_ctx);
3055 return NT_STATUS_OK;
3058 for (i=0; i<10-depth; i++)
3060 printf("%s\n", r.out.name->name);
3063 o.in.parent_handle = handle;
3064 o.in.keyname.name = r.out.name->name;
3066 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3067 o.out.handle = &key_handle;
3069 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
3070 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3071 enumkeys(p, &key_handle, tmp_ctx, depth-1);
3072 enumvalues(p, &key_handle, tmp_ctx);
3073 status = winreg_close(p, &key_handle);
3074 if (!NT_STATUS_IS_OK(status)) {
3079 talloc_free(tmp_ctx);
3081 r.in.enum_index += 1;
3084 return NT_STATUS_OK;
3087 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3089 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3090 const char *name, winreg_open_fn open_fn)
3092 struct policy_handle handle;
3093 struct winreg_OpenHKLM r;
3096 r.in.system_name = 0;
3097 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3098 r.out.handle = &handle;
3100 status = open_fn(p, mem_ctx, &r);
3101 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3102 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3103 nt_errstr(status), win_errstr(r.out.result));
3107 enumkeys(p, &handle, mem_ctx, 4);
3109 status = winreg_close(p, &handle);
3110 if (!NT_STATUS_IS_OK(status)) {
3111 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3112 __location__, nt_errstr(status));
3119 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3122 struct dcerpc_pipe *p;
3123 TALLOC_CTX *mem_ctx;
3129 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3130 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3131 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3132 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3133 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3138 mem_ctx = talloc_init("torture_rpc_winreg");
3140 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3142 if (!NT_STATUS_IS_OK(status)) {
3143 talloc_free(mem_ctx);
3148 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3150 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3151 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3156 talloc_free(mem_ctx);
3161 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3162 struct loadparm_context *lp_ctx,
3163 struct smbcli_state *cli,
3165 struct srvsvc_NetShareInfo502 **info502)
3167 struct smbcli_tree *ipc;
3168 struct dcerpc_pipe *p;
3169 struct srvsvc_NetShareGetInfo r;
3170 union srvsvc_NetShareInfo info;
3173 if (!(p = dcerpc_pipe_init(cli,
3174 cli->transport->socket->event.ctx,
3175 lp_iconv_convenience(lp_ctx)))) {
3176 status = NT_STATUS_NO_MEMORY;
3180 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3181 if (!NT_STATUS_IS_OK(status)) {
3185 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3186 if (!NT_STATUS_IS_OK(status)) {
3187 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3192 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3193 if (!NT_STATUS_IS_OK(status)) {
3194 d_printf("dcerpc_bind_auth_none failed: %s\n",
3199 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3200 dcerpc_server_name(p));
3201 r.in.share_name = share;
3205 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3206 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3207 d_printf("(%s) srvsvc_NetShareGetInfo failed: %s, %s\n", __location__,
3208 nt_errstr(status), win_errstr(r.out.result));
3212 *info502 = talloc_move(mem_ctx, &info.info502);
3213 return NT_STATUS_OK;
3221 * Get us a handle on HKLM\
3224 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3225 struct smbcli_state *cli,
3226 struct smb_iconv_convenience *iconv_convenience,
3227 struct dcerpc_pipe **pipe_p,
3228 struct policy_handle **handle)
3230 struct smbcli_tree *ipc;
3231 struct dcerpc_pipe *p;
3232 struct winreg_OpenHKLM r;
3234 struct policy_handle *result;
3236 result = talloc(mem_ctx, struct policy_handle);
3238 if (result == NULL) {
3239 return NT_STATUS_NO_MEMORY;
3242 if (!(p = dcerpc_pipe_init(result,
3243 cli->transport->socket->event.ctx,
3244 iconv_convenience))) {
3245 status = NT_STATUS_NO_MEMORY;
3249 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3250 if (!NT_STATUS_IS_OK(status)) {
3254 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3255 if (!NT_STATUS_IS_OK(status)) {
3256 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3261 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3262 if (!NT_STATUS_IS_OK(status)) {
3263 d_printf("dcerpc_bind_auth_none failed: %s\n",
3268 r.in.system_name = 0;
3269 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3270 r.out.handle = result;
3272 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3273 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3274 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3275 nt_errstr(status), win_errstr(r.out.result));
3281 return NT_STATUS_OK;
3284 talloc_free(result);
3288 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3289 struct smb_iconv_convenience *iconv_convenience,
3290 const char *sharename)
3292 struct dcerpc_pipe *p;
3293 struct policy_handle *hklm = NULL;
3294 struct policy_handle new_handle;
3295 struct winreg_CreateKey c;
3296 struct winreg_CloseKey cl;
3297 enum winreg_CreateAction action_taken;
3299 TALLOC_CTX *mem_ctx;
3301 mem_ctx = talloc_new(cli);
3302 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3304 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3305 if (!NT_STATUS_IS_OK(status)) {
3306 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3311 c.in.name.name = talloc_asprintf(
3312 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3313 if (c.in.name.name == NULL) {
3314 d_printf("talloc_asprintf failed\n");
3315 status = NT_STATUS_NO_MEMORY;
3318 c.in.keyclass.name = "";
3320 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3321 c.in.secdesc = NULL;
3322 c.in.action_taken = &action_taken;
3323 c.out.new_handle = &new_handle;
3324 c.out.action_taken = &action_taken;
3326 status = dcerpc_winreg_CreateKey(p, p, &c);
3327 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3328 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3329 nt_errstr(status), win_errstr(c.out.result));
3333 cl.in.handle = &new_handle;
3334 cl.out.handle = &new_handle;
3335 status = dcerpc_winreg_CloseKey(p, p, &cl);
3336 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3337 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3338 nt_errstr(status), win_errstr(cl.out.result));
3344 talloc_free(mem_ctx);
3348 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3349 struct smbcli_state *cli,
3350 const char *sharename)
3352 struct dcerpc_pipe *p;
3353 struct policy_handle *hklm = NULL;
3354 struct winreg_DeleteKey d;
3356 TALLOC_CTX *mem_ctx;
3358 mem_ctx = talloc_new(cli);
3359 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3361 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3363 if (!NT_STATUS_IS_OK(status)) {
3364 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3369 d.in.key.name = talloc_asprintf(
3370 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3371 if (d.in.key.name == NULL) {
3372 d_printf("talloc_asprintf failed\n");
3373 status = NT_STATUS_NO_MEMORY;
3376 status = dcerpc_winreg_DeleteKey(p, p, &d);
3377 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3378 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3379 nt_errstr(status), win_errstr(d.out.result));
3384 talloc_free(mem_ctx);
3388 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3389 struct loadparm_context *lp_ctx,
3390 const char *sharename,
3391 const char *parameter,
3394 struct dcerpc_pipe *p = NULL;
3395 struct policy_handle *hklm = NULL, key_handle;
3396 struct winreg_OpenKey o;
3397 struct winreg_SetValue s;
3402 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3403 if (!NT_STATUS_IS_OK(status)) {
3404 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3408 o.in.parent_handle = hklm;
3409 o.in.keyname.name = talloc_asprintf(
3410 hklm, "software\\samba\\smbconf\\%s", sharename);
3411 if (o.in.keyname.name == NULL) {
3412 d_printf("talloc_asprintf failed\n");
3413 status = NT_STATUS_NO_MEMORY;
3417 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3418 o.out.handle = &key_handle;
3420 status = dcerpc_winreg_OpenKey(p, p, &o);
3421 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3422 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3423 nt_errstr(status), win_errstr(o.out.result));
3427 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3428 value, &type, &val)) {
3429 d_printf("(%s) reg_string_to_val failed\n", __location__);
3433 s.in.handle = &key_handle;
3434 s.in.name.name = parameter;
3436 s.in.data = val.data;
3437 s.in.size = val.length;
3439 status = dcerpc_winreg_SetValue(p, p, &s);
3440 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3441 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3442 nt_errstr(status), win_errstr(s.out.result));
3451 bool torture_samba3_regconfig(struct torture_context *torture)
3453 struct smbcli_state *cli;
3454 struct srvsvc_NetShareInfo502 *i = NULL;
3457 const char *comment = "Dummer Kommentar";
3459 if (!(torture_open_connection(&cli, torture, 0))) {
3463 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3464 if (!NT_STATUS_IS_OK(status)) {
3465 torture_warning(torture, "torture_samba3_createshare failed: "
3466 "%s\n", nt_errstr(status));
3470 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3471 if (!NT_STATUS_IS_OK(status)) {
3472 torture_warning(torture, "torture_samba3_setconfig failed: "
3473 "%s\n", nt_errstr(status));
3477 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3478 if (!NT_STATUS_IS_OK(status)) {
3479 torture_warning(torture, "get_shareinfo failed: "
3480 "%s\n", nt_errstr(status));
3484 if (strcmp(comment, i->comment) != 0) {
3485 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3486 comment, i->comment);
3490 status = torture_samba3_deleteshare(torture, cli, "blubber");
3491 if (!NT_STATUS_IS_OK(status)) {
3492 torture_warning(torture, "torture_samba3_deleteshare failed: "
3493 "%s\n", nt_errstr(status));