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.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.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.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.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;
1037 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
1038 CLI_CRED_NTLMv2_AUTH;
1040 chal = data_blob_talloc(mem_ctx, NULL, 8);
1041 if (chal.data == NULL) {
1042 d_printf("data_blob_talloc failed\n");
1046 generate_random_buffer(chal.data, chal.length);
1047 names_blob = NTLMv2_generate_names_blob(
1049 cli_credentials_get_workstation(user_creds),
1050 cli_credentials_get_domain(user_creds));
1051 status = cli_credentials_get_ntlm_response(
1052 user_creds, mem_ctx, &flags, chal, names_blob,
1053 &lm_resp, &nt_resp, NULL, NULL);
1054 if (!NT_STATUS_IS_OK(status)) {
1055 d_printf("cli_credentials_get_ntlm_response failed:"
1056 " %s\n", nt_errstr(status));
1060 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1061 creds_client_authenticator(creds_state, &netr_auth);
1063 ninfo.identity_info.account_name.string =
1064 cli_credentials_get_username(user_creds);
1065 ninfo.identity_info.domain_name.string =
1066 cli_credentials_get_domain(user_creds);
1067 ninfo.identity_info.parameter_control = 0;
1068 ninfo.identity_info.logon_id_low = 0;
1069 ninfo.identity_info.logon_id_high = 0;
1070 ninfo.identity_info.workstation.string =
1071 cli_credentials_get_workstation(user_creds);
1072 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1073 ninfo.nt.length = nt_resp.length;
1074 ninfo.nt.data = nt_resp.data;
1075 ninfo.lm.length = lm_resp.length;
1076 ninfo.lm.data = lm_resp.data;
1078 r.in.server_name = talloc_asprintf(
1079 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1080 ZERO_STRUCT(netr_auth2);
1081 r.in.computer_name =
1082 cli_credentials_get_workstation(wks_creds);
1083 r.in.credential = &netr_auth;
1084 r.in.return_authenticator = &netr_auth2;
1085 r.in.logon_level = 2;
1086 r.in.validation_level = i;
1087 r.in.logon.network = &ninfo;
1088 r.out.return_authenticator = NULL;
1090 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1091 if (!NT_STATUS_IS_OK(status)) {
1092 d_printf("netr_LogonSamLogon failed: %s\n",
1097 if ((r.out.return_authenticator == NULL) ||
1098 (!creds_client_check(creds_state,
1099 &r.out.return_authenticator->cred))) {
1100 d_printf("Credentials check failed!\n");
1104 creds_client_authenticator(creds_state, &netr_auth);
1106 pinfo.identity_info = ninfo.identity_info;
1107 ZERO_STRUCT(pinfo.lmpassword.hash);
1108 E_md4hash(cli_credentials_get_password(user_creds),
1109 pinfo.ntpassword.hash);
1110 session_key = data_blob_talloc(mem_ctx,
1111 creds_state->session_key, 16);
1112 arcfour_crypt_blob(pinfo.ntpassword.hash,
1113 sizeof(pinfo.ntpassword.hash),
1116 r.in.logon_level = 1;
1117 r.in.logon.password = &pinfo;
1118 r.out.return_authenticator = NULL;
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");
1136 struct netr_ServerPasswordSet s;
1137 char *password = generate_random_str(wks_creds, 8);
1138 struct creds_CredentialState *creds_state;
1140 s.in.server_name = talloc_asprintf(
1141 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1142 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1143 s.in.account_name = talloc_asprintf(
1144 mem_ctx, "%s$", s.in.computer_name);
1145 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1146 E_md4hash(password, s.in.new_password.hash);
1148 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1149 creds_des_encrypt(creds_state, &s.in.new_password);
1150 creds_client_authenticator(creds_state, &s.in.credential);
1152 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1153 if (!NT_STATUS_IS_OK(status)) {
1154 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1158 if (!creds_client_check(creds_state,
1159 &s.out.return_authenticator.cred)) {
1160 printf("Credential chaining failed\n");
1163 cli_credentials_set_password(wks_creds, password,
1169 talloc_free(mem_ctx);
1174 * Delete the wks account again
1177 static bool leave(struct smbcli_state *cli,
1178 struct loadparm_context *lp_ctx,
1179 struct cli_credentials *admin_creds,
1180 struct cli_credentials *wks_creds)
1182 char *wks_name = talloc_asprintf(
1183 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1186 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1187 talloc_free(wks_name);
1192 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1195 bool torture_netlogon_samba3(struct torture_context *torture)
1197 TALLOC_CTX *mem_ctx;
1200 struct smbcli_state *cli;
1201 struct cli_credentials *anon_creds;
1202 struct cli_credentials *wks_creds;
1203 const char *wks_name;
1205 struct smbcli_options options;
1206 struct smbcli_session_options session_options;
1208 wks_name = torture_setting_string(torture, "wksname", NULL);
1209 if (wks_name == NULL) {
1210 wks_name = get_myname();
1213 mem_ctx = talloc_init("torture_netlogon_samba3");
1215 if (mem_ctx == NULL) {
1216 d_printf("talloc_init failed\n");
1220 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1221 d_printf("create_anon_creds failed\n");
1225 lp_smbcli_options(torture->lp_ctx, &options);
1226 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1228 status = smbcli_full_connection(mem_ctx, &cli,
1229 torture_setting_string(torture, "host", NULL),
1230 lp_smb_ports(torture->lp_ctx),
1231 "IPC$", NULL, anon_creds,
1232 lp_resolve_context(torture->lp_ctx),
1233 torture->ev, &options, &session_options,
1234 lp_iconv_convenience(torture->lp_ctx));
1235 if (!NT_STATUS_IS_OK(status)) {
1236 d_printf("smbcli_full_connection failed: %s\n",
1241 wks_creds = cli_credentials_init(mem_ctx);
1242 if (wks_creds == NULL) {
1243 d_printf("cli_credentials_init failed\n");
1247 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1248 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1249 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1250 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1251 cli_credentials_set_password(wks_creds,
1252 generate_random_str(wks_creds, 8),
1255 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1256 d_printf("join failed\n");
1260 cli_credentials_set_domain(
1261 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1264 for (i=0; i<2; i++) {
1266 /* Do this more than once, the routine "schan" changes
1267 * the workstation password using the netlogon
1268 * password change routine */
1272 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1273 d_printf("auth2 failed\n");
1277 for (j=0; j<2; j++) {
1278 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1279 d_printf("schan failed\n");
1285 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1286 d_printf("leave failed\n");
1293 talloc_free(mem_ctx);
1298 * Do a simple join, testjoin and leave using specified smb and samr
1302 static bool test_join3(struct torture_context *tctx,
1304 struct cli_credentials *smb_creds,
1305 struct cli_credentials *samr_creds,
1306 const char *wks_name)
1310 struct smbcli_state *cli;
1311 struct cli_credentials *wks_creds;
1312 struct smbcli_options options;
1313 struct smbcli_session_options session_options;
1315 lp_smbcli_options(tctx->lp_ctx, &options);
1316 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1318 status = smbcli_full_connection(tctx, &cli,
1319 torture_setting_string(tctx, "host", NULL),
1320 lp_smb_ports(tctx->lp_ctx),
1321 "IPC$", NULL, smb_creds,
1322 lp_resolve_context(tctx->lp_ctx),
1323 tctx->ev, &options, &session_options,
1324 lp_iconv_convenience(tctx->lp_ctx));
1325 if (!NT_STATUS_IS_OK(status)) {
1326 d_printf("smbcli_full_connection failed: %s\n",
1331 wks_creds = cli_credentials_init(cli);
1332 if (wks_creds == NULL) {
1333 d_printf("cli_credentials_init failed\n");
1337 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1338 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1339 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1340 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1341 cli_credentials_set_password(wks_creds,
1342 generate_random_str(wks_creds, 8),
1345 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1346 d_printf("join failed\n");
1350 cli_credentials_set_domain(
1351 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1354 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1355 d_printf("auth2 failed\n");
1359 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1360 d_printf("leave failed\n");
1373 * Test the different session key variants. Do it by joining, this uses the
1374 * session key in the setpassword routine. Test the join by doing the auth2.
1377 bool torture_samba3_sessionkey(struct torture_context *torture)
1380 struct cli_credentials *anon_creds;
1381 const char *wks_name;
1383 wks_name = torture_setting_string(torture, "wksname", get_myname());
1385 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1386 d_printf("create_anon_creds failed\n");
1390 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1394 if (!torture_setting_bool(torture, "samba3", false)) {
1396 /* Samba3 in the build farm right now does this happily. Need
1399 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1400 d_printf("join using anonymous bind on an anonymous smb "
1401 "connection succeeded -- HUH??\n");
1406 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1408 d_printf("join using ntlmssp bind on an anonymous smb "
1409 "connection failed\n");
1413 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1414 d_printf("join using anonymous bind on an authenticated smb "
1415 "connection failed\n");
1419 if (!test_join3(torture, false, cmdline_credentials,
1420 cmdline_credentials,
1422 d_printf("join using ntlmssp bind on an authenticated smb "
1423 "connection failed\n");
1428 * The following two are tests for setuserinfolevel 25
1431 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1433 d_printf("join using ntlmssp bind on an anonymous smb "
1434 "connection failed\n");
1438 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1439 d_printf("join using anonymous bind on an authenticated smb "
1440 "connection failed\n");
1450 * open pipe and bind, given an IPC$ context
1453 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1454 struct loadparm_context *lp_ctx,
1455 struct smbcli_tree *tree,
1456 const char *pipe_name,
1457 const struct ndr_interface_table *iface,
1458 struct dcerpc_pipe **p)
1460 struct dcerpc_pipe *result;
1463 if (!(result = dcerpc_pipe_init(
1464 mem_ctx, tree->session->transport->socket->event.ctx,
1465 lp_iconv_convenience(lp_ctx)))) {
1466 return NT_STATUS_NO_MEMORY;
1469 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1473 talloc_free(result);
1477 status = dcerpc_bind_auth_none(result, iface);
1478 if (!NT_STATUS_IS_OK(status)) {
1479 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1480 talloc_free(result);
1485 return NT_STATUS_OK;
1489 * Sane wrapper around lsa_LookupNames
1492 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1493 struct dcerpc_pipe *p,
1497 struct lsa_ObjectAttribute attr;
1498 struct lsa_QosInfo qos;
1499 struct lsa_OpenPolicy2 r;
1502 struct policy_handle handle;
1503 struct lsa_LookupNames l;
1504 struct lsa_TransSidArray sids;
1505 struct lsa_RefDomainList *domains = NULL;
1506 struct lsa_String lsa_name;
1508 struct dom_sid *result;
1509 TALLOC_CTX *tmp_ctx;
1511 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1516 qos.impersonation_level = 2;
1517 qos.context_mode = 1;
1518 qos.effective_only = 0;
1521 attr.root_dir = NULL;
1522 attr.object_name = NULL;
1523 attr.attributes = 0;
1524 attr.sec_desc = NULL;
1525 attr.sec_qos = &qos;
1527 r.in.system_name = "\\";
1529 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1530 r.out.handle = &handle;
1532 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1533 if (!NT_STATUS_IS_OK(status)) {
1534 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1535 talloc_free(tmp_ctx);
1542 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1544 l.in.handle = &handle;
1546 l.in.names = &lsa_name;
1549 l.in.count = &count;
1550 l.out.count = &count;
1552 l.out.domains = &domains;
1554 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1558 talloc_free(tmp_ctx);
1562 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1563 l.out.sids->sids[0].rid);
1565 c.in.handle = &handle;
1566 c.out.handle = &handle;
1568 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1569 if (!NT_STATUS_IS_OK(status)) {
1570 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1571 talloc_free(tmp_ctx);
1575 talloc_free(tmp_ctx);
1580 * Find out the user SID on this connection
1583 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1584 struct loadparm_context *lp_ctx,
1585 struct smbcli_tree *tree)
1587 struct dcerpc_pipe *lsa;
1588 struct lsa_GetUserName r;
1590 struct lsa_String *authority_name_p = NULL;
1591 struct lsa_String *account_name_p = NULL;
1592 struct dom_sid *result;
1594 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1595 &ndr_table_lsarpc, &lsa);
1596 if (!NT_STATUS_IS_OK(status)) {
1597 d_printf("(%s) Could not bind to LSA: %s\n",
1598 __location__, nt_errstr(status));
1602 r.in.system_name = "\\";
1603 r.in.account_name = &account_name_p;
1604 r.in.authority_name = &authority_name_p;
1605 r.out.account_name = &account_name_p;
1607 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1609 authority_name_p = *r.out.authority_name;
1611 if (!NT_STATUS_IS_OK(status)) {
1612 printf("(%s) GetUserName failed - %s\n",
1613 __location__, nt_errstr(status));
1618 result = name2sid(mem_ctx, lsa, account_name_p->string,
1619 authority_name_p->string);
1625 static int destroy_tree(struct smbcli_tree *tree)
1627 smb_tree_disconnect(tree);
1632 * Do a tcon, given a session
1635 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1636 struct smbcli_session *session,
1637 const char *sharename,
1638 struct smbcli_tree **res)
1640 struct smbcli_tree *result;
1641 TALLOC_CTX *tmp_ctx;
1642 union smb_tcon tcon;
1645 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1646 return NT_STATUS_NO_MEMORY;
1649 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1650 talloc_free(tmp_ctx);
1651 return NT_STATUS_NO_MEMORY;
1654 tcon.generic.level = RAW_TCON_TCONX;
1655 tcon.tconx.in.flags = 0;
1656 tcon.tconx.in.password = data_blob(NULL, 0);
1657 tcon.tconx.in.path = sharename;
1658 tcon.tconx.in.device = "?????";
1660 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1661 if (!NT_STATUS_IS_OK(status)) {
1662 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1664 talloc_free(tmp_ctx);
1668 result->tid = tcon.tconx.out.tid;
1669 result = talloc_steal(mem_ctx, result);
1670 talloc_set_destructor(result, destroy_tree);
1671 talloc_free(tmp_ctx);
1673 return NT_STATUS_OK;
1677 * Test the getusername behaviour
1680 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1683 struct smbcli_state *cli;
1684 TALLOC_CTX *mem_ctx;
1686 struct dom_sid *user_sid;
1687 struct dom_sid *created_sid;
1688 struct cli_credentials *anon_creds;
1689 struct cli_credentials *user_creds;
1691 struct smbcli_options options;
1692 struct smbcli_session_options session_options;
1694 if (!(mem_ctx = talloc_new(torture))) {
1698 lp_smbcli_options(torture->lp_ctx, &options);
1699 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1701 status = smbcli_full_connection(
1702 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1703 lp_smb_ports(torture->lp_ctx),
1704 "IPC$", NULL, cmdline_credentials,
1705 lp_resolve_context(torture->lp_ctx),
1706 torture->ev, &options, &session_options,
1707 lp_iconv_convenience(torture->lp_ctx));
1708 if (!NT_STATUS_IS_OK(status)) {
1709 d_printf("(%s) smbcli_full_connection failed: %s\n",
1710 __location__, nt_errstr(status));
1715 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1716 d_printf("(%s) whoami on auth'ed connection failed\n",
1723 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1724 d_printf("(%s) create_anon_creds failed\n", __location__);
1729 status = smbcli_full_connection(
1730 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1731 lp_smb_ports(torture->lp_ctx),
1732 "IPC$", NULL, anon_creds,
1733 lp_resolve_context(torture->lp_ctx),
1734 torture->ev, &options, &session_options,
1735 lp_iconv_convenience(torture->lp_ctx));
1736 if (!NT_STATUS_IS_OK(status)) {
1737 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1738 __location__, nt_errstr(status));
1743 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1744 d_printf("(%s) whoami on anon connection failed\n",
1750 if (!dom_sid_equal(user_sid,
1751 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1752 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1753 "S-1-5-7", __location__,
1754 dom_sid_string(mem_ctx, user_sid));
1758 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1759 d_printf("(%s) cli_credentials_init failed\n", __location__);
1764 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1765 cli_credentials_set_username(user_creds, "torture_username",
1767 cli_credentials_set_password(user_creds,
1768 generate_random_str(user_creds, 8),
1771 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1772 cli_credentials_get_username(user_creds),
1773 cli_credentials_get_password(user_creds),
1774 &domain_name, &created_sid)) {
1775 d_printf("(%s) create_user failed\n", __location__);
1780 cli_credentials_set_domain(user_creds, domain_name,
1784 struct smbcli_session *session2;
1785 struct smb_composite_sesssetup setup;
1786 struct smbcli_tree *tree;
1788 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1789 if (session2 == NULL) {
1790 d_printf("(%s) smbcli_session_init failed\n",
1795 setup.in.sesskey = cli->transport->negotiate.sesskey;
1796 setup.in.capabilities = cli->transport->negotiate.capabilities;
1797 setup.in.workgroup = "";
1798 setup.in.credentials = user_creds;
1800 status = smb_composite_sesssetup(session2, &setup);
1801 if (!NT_STATUS_IS_OK(status)) {
1802 d_printf("(%s) session setup with new user failed: "
1803 "%s\n", __location__, nt_errstr(status));
1807 session2->vuid = setup.out.vuid;
1809 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1811 d_printf("(%s) secondary_tcon failed\n",
1817 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1818 d_printf("(%s) whoami on user connection failed\n",
1827 d_printf("Created %s, found %s\n",
1828 dom_sid_string(mem_ctx, created_sid),
1829 dom_sid_string(mem_ctx, user_sid));
1831 if (!dom_sid_equal(created_sid, user_sid)) {
1836 if (!delete_user(cli, torture->lp_ctx,
1837 cmdline_credentials,
1838 cli_credentials_get_username(user_creds))) {
1839 d_printf("(%s) delete_user failed\n", __location__);
1844 talloc_free(mem_ctx);
1848 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1849 const char *sharename)
1852 struct srvsvc_NetShareGetInfo r;
1853 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1857 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1858 dcerpc_server_name(p));
1859 r.in.share_name = sharename;
1861 for (i=0;i<ARRAY_SIZE(levels);i++) {
1862 r.in.level = levels[i];
1866 printf("testing NetShareGetInfo level %u on share '%s'\n",
1867 r.in.level, r.in.share_name);
1869 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1870 if (!NT_STATUS_IS_OK(status)) {
1871 printf("NetShareGetInfo level %u on share '%s' failed"
1872 " - %s\n", r.in.level, r.in.share_name,
1877 if (!W_ERROR_IS_OK(r.out.result)) {
1878 printf("NetShareGetInfo level %u on share '%s' failed "
1879 "- %s\n", r.in.level, r.in.share_name,
1880 win_errstr(r.out.result));
1889 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1890 const char **one_sharename)
1893 struct srvsvc_NetShareEnum r;
1894 struct srvsvc_NetShareCtr0 c0;
1895 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1899 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1900 r.in.ctr.ctr0 = &c0;
1901 r.in.ctr.ctr0->count = 0;
1902 r.in.ctr.ctr0->array = NULL;
1903 r.in.max_buffer = (uint32_t)-1;
1904 r.in.resume_handle = NULL;
1906 for (i=0;i<ARRAY_SIZE(levels);i++) {
1907 r.in.level = levels[i];
1911 printf("testing NetShareEnum level %u\n", r.in.level);
1912 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1913 if (!NT_STATUS_IS_OK(status)) {
1914 printf("NetShareEnum level %u failed - %s\n",
1915 r.in.level, nt_errstr(status));
1919 if (!W_ERROR_IS_OK(r.out.result)) {
1920 printf("NetShareEnum level %u failed - %s\n",
1921 r.in.level, win_errstr(r.out.result));
1924 if (r.in.level == 0) {
1925 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1926 if (ctr->count > 0) {
1927 *one_sharename = ctr->array[0].name;
1935 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
1937 struct dcerpc_pipe *p;
1938 TALLOC_CTX *mem_ctx;
1940 const char *sharename = NULL;
1941 struct smbcli_state *cli;
1944 if (!(mem_ctx = talloc_new(torture))) {
1948 if (!(torture_open_connection_share(
1949 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
1950 "IPC$", torture->ev))) {
1951 talloc_free(mem_ctx);
1955 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
1956 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
1957 if (!NT_STATUS_IS_OK(status)) {
1958 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1959 __location__, nt_errstr(status));
1964 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1965 if (sharename == NULL) {
1966 printf("did not get sharename\n");
1968 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1972 talloc_free(mem_ctx);
1977 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1978 * NT_STATUS_NO_SAM_ACCOUNT
1981 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1983 TALLOC_CTX *mem_ctx;
1984 struct dcerpc_pipe *net_pipe;
1986 bool result = false;
1988 struct netr_ServerReqChallenge r;
1989 struct netr_Credential netr_cli_creds;
1990 struct netr_Credential netr_srv_creds;
1991 uint32_t negotiate_flags;
1992 struct netr_ServerAuthenticate2 a;
1993 struct creds_CredentialState *creds_state;
1994 struct netr_Credential netr_cred;
1995 struct samr_Password mach_pw;
1996 struct smbcli_state *cli;
1998 if (!(mem_ctx = talloc_new(torture))) {
1999 d_printf("talloc_new failed\n");
2003 if (!(wksname = generate_random_str_list(
2004 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2005 d_printf("generate_random_str_list failed\n");
2009 if (!(torture_open_connection_share(
2011 torture, torture_setting_string(torture, "host", NULL),
2012 "IPC$", torture->ev))) {
2013 d_printf("IPC$ connection failed\n");
2017 if (!(net_pipe = dcerpc_pipe_init(
2018 mem_ctx, cli->transport->socket->event.ctx,
2019 lp_iconv_convenience(torture->lp_ctx)))) {
2020 d_printf("dcerpc_pipe_init failed\n");
2024 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2025 if (!NT_STATUS_IS_OK(status)) {
2026 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2031 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2032 if (!NT_STATUS_IS_OK(status)) {
2033 d_printf("dcerpc_bind_auth_none failed: %s\n",
2038 r.in.computer_name = wksname;
2039 r.in.server_name = talloc_asprintf(
2040 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2041 if (r.in.server_name == NULL) {
2042 d_printf("talloc_asprintf failed\n");
2045 generate_random_buffer(netr_cli_creds.data,
2046 sizeof(netr_cli_creds.data));
2047 r.in.credentials = &netr_cli_creds;
2048 r.out.credentials = &netr_srv_creds;
2050 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2051 if (!NT_STATUS_IS_OK(status)) {
2052 d_printf("netr_ServerReqChallenge failed: %s\n",
2057 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2058 E_md4hash("foobar", mach_pw.hash);
2060 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2061 creds_client_init(creds_state, r.in.credentials,
2062 r.out.credentials, &mach_pw,
2063 &netr_cred, negotiate_flags);
2065 a.in.server_name = talloc_asprintf(
2066 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2067 a.in.account_name = talloc_asprintf(
2068 mem_ctx, "%s$", wksname);
2069 a.in.computer_name = wksname;
2070 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2071 a.in.negotiate_flags = &negotiate_flags;
2072 a.out.negotiate_flags = &negotiate_flags;
2073 a.in.credentials = &netr_cred;
2074 a.out.credentials = &netr_cred;
2076 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2078 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2079 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2080 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2087 talloc_free(mem_ctx);
2091 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2092 struct loadparm_context *lp_ctx,
2093 struct smbcli_session *sess,
2094 const char *sharename)
2096 struct smbcli_tree *tree;
2097 TALLOC_CTX *tmp_ctx;
2098 struct dcerpc_pipe *p;
2100 struct srvsvc_NetShareGetInfo r;
2101 struct security_descriptor *result;
2103 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2104 d_printf("talloc_new failed\n");
2108 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2109 d_printf("secondary_tcon failed\n");
2110 talloc_free(tmp_ctx);
2114 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2115 &ndr_table_srvsvc, &p);
2116 if (!NT_STATUS_IS_OK(status)) {
2117 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2118 __location__, nt_errstr(status));
2119 talloc_free(tmp_ctx);
2124 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2127 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2128 dcerpc_server_name(p));
2129 r.in.share_name = sharename;
2132 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2133 if (!NT_STATUS_IS_OK(status)) {
2134 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2136 talloc_free(tmp_ctx);
2140 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2141 talloc_free(tmp_ctx);
2145 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2146 struct loadparm_context *lp_ctx,
2147 struct smbcli_session *sess,
2148 const char *sharename,
2149 struct security_descriptor *sd)
2151 struct smbcli_tree *tree;
2152 TALLOC_CTX *tmp_ctx;
2153 struct dcerpc_pipe *p;
2155 struct sec_desc_buf i;
2156 struct srvsvc_NetShareSetInfo r;
2159 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2160 d_printf("talloc_new failed\n");
2161 return NT_STATUS_NO_MEMORY;
2164 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2165 d_printf("secondary_tcon failed\n");
2166 talloc_free(tmp_ctx);
2167 return NT_STATUS_UNSUCCESSFUL;
2170 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2171 &ndr_table_srvsvc, &p);
2172 if (!NT_STATUS_IS_OK(status)) {
2173 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2174 __location__, nt_errstr(status));
2175 talloc_free(tmp_ctx);
2176 return NT_STATUS_UNSUCCESSFUL;
2180 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2183 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2184 dcerpc_server_name(p));
2185 r.in.share_name = sharename;
2188 r.in.info.info1501 = &i;
2189 r.in.parm_error = &error;
2191 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2192 if (!NT_STATUS_IS_OK(status)) {
2193 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2197 talloc_free(tmp_ctx);
2201 bool try_tcon(TALLOC_CTX *mem_ctx,
2202 struct loadparm_context *lp_ctx,
2203 struct security_descriptor *orig_sd,
2204 struct smbcli_session *session,
2205 const char *sharename, const struct dom_sid *user_sid,
2206 unsigned int access_mask, NTSTATUS expected_tcon,
2207 NTSTATUS expected_mkdir)
2209 TALLOC_CTX *tmp_ctx;
2210 struct smbcli_tree *rmdir_tree, *tree;
2211 struct dom_sid *domain_sid;
2213 struct security_descriptor *sd;
2217 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2218 d_printf("talloc_new failed\n");
2222 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2223 if (!NT_STATUS_IS_OK(status)) {
2224 d_printf("first tcon to delete dir failed\n");
2225 talloc_free(tmp_ctx);
2229 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2231 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2232 &domain_sid, &rid))) {
2233 d_printf("dom_sid_split_rid failed\n");
2234 talloc_free(tmp_ctx);
2238 sd = security_descriptor_dacl_create(
2239 tmp_ctx, 0, "S-1-5-32-544",
2240 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2242 dom_sid_string(mem_ctx, user_sid),
2243 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2245 d_printf("security_descriptor_dacl_create failed\n");
2246 talloc_free(tmp_ctx);
2250 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2251 if (!NT_STATUS_IS_OK(status)) {
2252 d_printf("custom set_sharesec failed: %s\n",
2254 talloc_free(tmp_ctx);
2258 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2259 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2260 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2266 if (!NT_STATUS_IS_OK(status)) {
2267 /* An expected non-access, no point in trying to write */
2271 status = smbcli_mkdir(tree, "sharesec_testdir");
2272 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2273 d_printf("(%s) Expected %s, got %s\n", __location__,
2274 nt_errstr(expected_mkdir), nt_errstr(status));
2279 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2281 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2282 if (!NT_STATUS_IS_OK(status)) {
2283 d_printf("custom set_sharesec failed: %s\n",
2285 talloc_free(tmp_ctx);
2289 talloc_free(tmp_ctx);
2293 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2295 TALLOC_CTX *mem_ctx;
2297 struct smbcli_state *cli;
2298 struct security_descriptor *sd;
2299 struct dom_sid *user_sid;
2301 if (!(mem_ctx = talloc_new(torture))) {
2305 if (!(torture_open_connection_share(
2306 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2307 "IPC$", torture->ev))) {
2308 d_printf("IPC$ connection failed\n");
2309 talloc_free(mem_ctx);
2313 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2314 d_printf("whoami failed\n");
2315 talloc_free(mem_ctx);
2319 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2320 torture_setting_string(torture, "share", NULL));
2322 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2323 torture_setting_string(torture, "share", NULL),
2324 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2326 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2327 torture_setting_string(torture, "share", NULL),
2328 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2329 NT_STATUS_MEDIA_WRITE_PROTECTED);
2331 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2332 torture_setting_string(torture, "share", NULL),
2333 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2335 talloc_free(mem_ctx);
2339 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2341 TALLOC_CTX *mem_ctx;
2343 struct smbcli_state *cli;
2344 struct dcerpc_pipe *p;
2345 struct policy_handle lsa_handle;
2347 struct dom_sid *domain_sid;
2349 if (!(mem_ctx = talloc_new(torture))) {
2353 if (!(torture_open_connection_share(
2354 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2355 "IPC$", torture->ev))) {
2356 d_printf("IPC$ connection failed\n");
2357 talloc_free(mem_ctx);
2361 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2362 &ndr_table_lsarpc, &p);
2363 if (!NT_STATUS_IS_OK(status)) {
2364 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2366 talloc_free(mem_ctx);
2371 struct lsa_ObjectAttribute attr;
2372 struct lsa_OpenPolicy2 o;
2373 o.in.system_name = talloc_asprintf(
2374 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2377 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2378 o.out.handle = &lsa_handle;
2379 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2380 if (!NT_STATUS_IS_OK(status)) {
2381 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2382 __location__, nt_errstr(status));
2383 talloc_free(mem_ctx);
2389 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2394 int levels[] = { 2,3,5,6 };
2396 for (i=0; i<ARRAY_SIZE(levels); i++) {
2397 struct lsa_QueryInfoPolicy r;
2398 union lsa_PolicyInformation *info = NULL;
2399 r.in.handle = &lsa_handle;
2400 r.in.level = levels[i];
2402 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2403 if (!NT_STATUS_IS_OK(status)) {
2404 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2405 "failed: %s\n", __location__,
2406 levels[i], nt_errstr(status));
2407 talloc_free(mem_ctx);
2410 if (levels[i] == 5) {
2411 domain_sid = info->account_domain.sid;
2419 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2420 struct smb_iconv_convenience *iconv_convenience,
2423 struct rap_WserverGetInfo r;
2425 char servername[17];
2428 r.in.bufsize = 0xffff;
2430 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2431 if (!NT_STATUS_IS_OK(status)) {
2435 memcpy(servername, r.out.info.info0.name, 16);
2436 servername[16] = '\0';
2438 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2439 return NT_STATUS_NO_MEMORY;
2442 return NT_STATUS_OK;
2446 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2447 struct smbcli_tree *tree,
2448 const char ***printers, int *num_printers)
2450 TALLOC_CTX *mem_ctx;
2452 struct dcerpc_pipe *p;
2453 struct srvsvc_NetShareEnum r;
2454 struct srvsvc_NetShareCtr1 c1_in;
2455 struct srvsvc_NetShareCtr1 *c1;
2458 mem_ctx = talloc_new(ctx);
2459 if (mem_ctx == NULL) {
2460 return NT_STATUS_NO_MEMORY;
2463 status = pipe_bind_smb(mem_ctx, lp_ctx,
2464 tree, "\\srvsvc", &ndr_table_srvsvc,
2466 if (!NT_STATUS_IS_OK(status)) {
2467 d_printf("could not bind to srvsvc pipe\n");
2468 talloc_free(mem_ctx);
2472 r.in.server_unc = talloc_asprintf(
2473 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2476 r.in.ctr.ctr1 = &c1_in;
2477 r.in.max_buffer = (uint32_t)-1;
2478 r.in.resume_handle = NULL;
2480 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2481 if (!NT_STATUS_IS_OK(status)) {
2482 d_printf("NetShareEnum level %u failed - %s\n",
2483 r.in.level, nt_errstr(status));
2484 talloc_free(mem_ctx);
2490 c1 = r.out.ctr.ctr1;
2491 for (i=0; i<c1->count; i++) {
2492 if (c1->array[i].type != STYPE_PRINTQ) {
2495 if (!add_string_to_array(ctx, c1->array[i].name,
2496 printers, num_printers)) {
2498 return NT_STATUS_NO_MEMORY;
2502 talloc_free(mem_ctx);
2503 return NT_STATUS_OK;
2506 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2507 const char *servername, int level, int *num_printers)
2509 struct spoolss_EnumPrinters r;
2513 r.in.flags = PRINTER_ENUM_LOCAL;
2514 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2519 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2520 if (!NT_STATUS_IS_OK(status)) {
2521 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2522 __location__, nt_errstr(status));
2526 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2527 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2528 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2529 win_errstr(r.out.result));
2533 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2534 if (blob.data == NULL) {
2535 d_printf("(%s) data_blob_talloc failed\n", __location__);
2539 r.in.buffer = &blob;
2540 r.in.offered = r.out.needed;
2542 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2543 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2544 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2545 "%s\n", __location__, nt_errstr(status),
2546 win_errstr(r.out.result));
2550 *num_printers = r.out.count;
2555 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2556 struct policy_handle *handle, int level,
2557 union spoolss_PrinterInfo **res)
2559 TALLOC_CTX *mem_ctx;
2560 struct spoolss_GetPrinter r;
2564 mem_ctx = talloc_new(ctx);
2565 if (mem_ctx == NULL) {
2566 return NT_STATUS_NO_MEMORY;
2569 r.in.handle = handle;
2574 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2575 if (!NT_STATUS_IS_OK(status)) {
2576 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2577 __location__, nt_errstr(status));
2578 talloc_free(mem_ctx);
2582 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2583 printf("GetPrinter unexpected return code %s, should "
2584 "be WERR_INSUFFICIENT_BUFFER\n",
2585 win_errstr(r.out.result));
2586 talloc_free(mem_ctx);
2587 return NT_STATUS_UNSUCCESSFUL;
2590 r.in.handle = handle;
2592 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2593 if (blob.data == NULL) {
2594 talloc_free(mem_ctx);
2595 return NT_STATUS_NO_MEMORY;
2597 memset(blob.data, 0, blob.length);
2598 r.in.buffer = &blob;
2599 r.in.offered = r.out.needed;
2601 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2602 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2603 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2604 "%s\n", __location__, nt_errstr(status),
2605 win_errstr(r.out.result));
2606 talloc_free(mem_ctx);
2607 return NT_STATUS_IS_OK(status) ?
2608 NT_STATUS_UNSUCCESSFUL : status;
2612 *res = talloc_steal(ctx, r.out.info);
2615 talloc_free(mem_ctx);
2616 return NT_STATUS_OK;
2619 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2621 TALLOC_CTX *mem_ctx;
2623 struct smbcli_state *cli;
2624 struct dcerpc_pipe *p;
2626 struct policy_handle server_handle, printer_handle;
2627 const char **printers;
2629 struct spoolss_UserLevel1 userlevel1;
2632 if (!(mem_ctx = talloc_new(torture))) {
2636 if (!(torture_open_connection_share(
2637 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2638 "IPC$", torture->ev))) {
2639 d_printf("IPC$ connection failed\n");
2640 talloc_free(mem_ctx);
2644 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2645 if (!NT_STATUS_IS_OK(status)) {
2646 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2647 __location__, nt_errstr(status));
2648 talloc_free(mem_ctx);
2652 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2653 &printers, &num_printers))) {
2654 talloc_free(mem_ctx);
2658 if (num_printers == 0) {
2659 d_printf("Did not find printers\n");
2660 talloc_free(mem_ctx);
2664 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2665 &ndr_table_spoolss, &p);
2666 if (!NT_STATUS_IS_OK(status)) {
2667 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2669 talloc_free(mem_ctx);
2673 ZERO_STRUCT(userlevel1);
2674 userlevel1.client = talloc_asprintf(
2675 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2676 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2677 userlevel1.build = 2600;
2678 userlevel1.major = 3;
2679 userlevel1.minor = 0;
2680 userlevel1.processor = 0;
2683 struct spoolss_OpenPrinterEx r;
2686 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2688 r.in.datatype = NULL;
2689 r.in.access_mask = 0;
2691 r.in.userlevel.level1 = &userlevel1;
2692 r.out.handle = &server_handle;
2694 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2695 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2696 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2697 "%s, %s\n", __location__, nt_errstr(status),
2698 win_errstr(r.out.result));
2699 talloc_free(mem_ctx);
2705 struct spoolss_ClosePrinter r;
2707 r.in.handle = &server_handle;
2708 r.out.handle = &server_handle;
2710 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2711 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2712 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2713 "%s, %s\n", __location__, nt_errstr(status),
2714 win_errstr(r.out.result));
2715 talloc_free(mem_ctx);
2721 struct spoolss_OpenPrinterEx r;
2724 r.in.printername = talloc_asprintf(
2725 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2726 r.in.datatype = NULL;
2727 r.in.access_mask = 0;
2729 r.in.userlevel.level1 = &userlevel1;
2730 r.out.handle = &printer_handle;
2732 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2733 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2734 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2735 "%s, %s\n", __location__, nt_errstr(status),
2736 win_errstr(r.out.result));
2737 talloc_free(mem_ctx);
2745 for (i=0; i<8; i++) {
2746 status = getprinterinfo(mem_ctx, p, &printer_handle,
2748 if (!NT_STATUS_IS_OK(status)) {
2749 d_printf("(%s) getprinterinfo %d failed: %s\n",
2750 __location__, i, nt_errstr(status));
2757 struct spoolss_ClosePrinter r;
2759 r.in.handle = &printer_handle;
2760 r.out.handle = &printer_handle;
2762 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2763 if (!NT_STATUS_IS_OK(status)) {
2764 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2765 "%s\n", __location__, nt_errstr(status));
2766 talloc_free(mem_ctx);
2773 if (!enumprinters(mem_ctx, p, servername, 1,
2775 d_printf("(%s) enumprinters failed\n", __location__);
2776 talloc_free(mem_ctx);
2779 if (num_printers != num_enumerated) {
2780 d_printf("(%s) netshareenum gave %d printers, "
2781 "enumprinters lvl 1 gave %d\n", __location__,
2782 num_printers, num_enumerated);
2783 talloc_free(mem_ctx);
2790 if (!enumprinters(mem_ctx, p, servername, 2,
2792 d_printf("(%s) enumprinters failed\n", __location__);
2793 talloc_free(mem_ctx);
2796 if (num_printers != num_enumerated) {
2797 d_printf("(%s) netshareenum gave %d printers, "
2798 "enumprinters lvl 2 gave %d\n", __location__,
2799 num_printers, num_enumerated);
2800 talloc_free(mem_ctx);
2805 talloc_free(mem_ctx);
2810 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2812 TALLOC_CTX *mem_ctx;
2813 struct smbcli_state *cli;
2814 struct dcerpc_pipe *p;
2818 if (!(mem_ctx = talloc_new(torture))) {
2822 if (!(torture_open_connection_share(
2823 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2824 "IPC$", torture->ev))) {
2825 d_printf("IPC$ connection failed\n");
2826 talloc_free(mem_ctx);
2830 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2831 if (!NT_STATUS_IS_OK(status)) {
2832 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2833 __location__, nt_errstr(status));
2834 talloc_free(mem_ctx);
2838 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2839 &ndr_table_wkssvc, &p);
2840 if (!NT_STATUS_IS_OK(status)) {
2841 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2843 talloc_free(mem_ctx);
2848 struct wkssvc_NetWkstaInfo100 wks100;
2849 union wkssvc_NetWkstaInfo info;
2850 struct wkssvc_NetWkstaGetInfo r;
2852 r.in.server_name = "\\foo";
2854 info.info100 = &wks100;
2857 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2858 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2859 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2860 "%s, %s\n", __location__, nt_errstr(status),
2861 win_errstr(r.out.result));
2862 talloc_free(mem_ctx);
2866 if (strcmp(servername,
2867 r.out.info->info100->server_name) != 0) {
2868 d_printf("(%s) servername inconsistency: RAP=%s, "
2869 "dcerpc_wkssvc_NetWksGetInfo=%s",
2870 __location__, servername,
2871 r.out.info->info100->server_name);
2872 talloc_free(mem_ctx);
2877 talloc_free(mem_ctx);
2881 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2882 struct policy_handle *handle)
2884 struct winreg_CloseKey c;
2886 TALLOC_CTX *mem_ctx;
2888 c.in.handle = c.out.handle = handle;
2890 if (!(mem_ctx = talloc_new(p))) {
2891 return NT_STATUS_NO_MEMORY;
2894 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2895 talloc_free(mem_ctx);
2897 if (!NT_STATUS_IS_OK(status)) {
2901 if (!W_ERROR_IS_OK(c.out.result)) {
2902 return werror_to_ntstatus(c.out.result);
2905 return NT_STATUS_OK;
2908 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2909 TALLOC_CTX *mem_ctx)
2911 uint32_t enum_index = 0;
2914 struct winreg_EnumValue r;
2915 struct winreg_StringBuf name;
2916 enum winreg_Type type = 0;
2919 uint32_t size, length;
2921 r.in.handle = handle;
2922 r.in.enum_index = enum_index;
2925 r.in.name = r.out.name = &name;
2931 r.in.length = &length;
2933 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2934 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2935 return NT_STATUS_OK;
2941 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
2942 TALLOC_CTX *mem_ctx, int depth)
2944 struct winreg_EnumKey r;
2945 struct winreg_StringBuf class, name;
2950 return NT_STATUS_OK;
2956 r.in.handle = handle;
2957 r.in.enum_index = 0;
2959 r.in.keyclass = &class;
2961 r.in.last_changed_time = &t;
2964 TALLOC_CTX *tmp_ctx;
2965 struct winreg_OpenKey o;
2966 struct policy_handle key_handle;
2969 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2970 return NT_STATUS_NO_MEMORY;
2976 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2977 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2978 /* We're done enumerating */
2979 talloc_free(tmp_ctx);
2980 return NT_STATUS_OK;
2983 for (i=0; i<10-depth; i++)
2985 printf("%s\n", r.out.name->name);
2988 o.in.parent_handle = handle;
2989 o.in.keyname.name = r.out.name->name;
2991 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2992 o.out.handle = &key_handle;
2994 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2995 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2996 enumkeys(p, &key_handle, tmp_ctx, depth-1);
2997 enumvalues(p, &key_handle, tmp_ctx);
2998 status = winreg_close(p, &key_handle);
2999 if (!NT_STATUS_IS_OK(status)) {
3004 talloc_free(tmp_ctx);
3006 r.in.enum_index += 1;
3009 return NT_STATUS_OK;
3012 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3014 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3015 const char *name, winreg_open_fn open_fn)
3017 struct policy_handle handle;
3018 struct winreg_OpenHKLM r;
3021 r.in.system_name = 0;
3022 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3023 r.out.handle = &handle;
3025 status = open_fn(p, mem_ctx, &r);
3026 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3027 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3028 nt_errstr(status), win_errstr(r.out.result));
3032 enumkeys(p, &handle, mem_ctx, 4);
3034 status = winreg_close(p, &handle);
3035 if (!NT_STATUS_IS_OK(status)) {
3036 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3037 __location__, nt_errstr(status));
3044 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3047 struct dcerpc_pipe *p;
3048 TALLOC_CTX *mem_ctx;
3054 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3055 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3056 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3057 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3058 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3063 mem_ctx = talloc_init("torture_rpc_winreg");
3065 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3067 if (!NT_STATUS_IS_OK(status)) {
3068 talloc_free(mem_ctx);
3073 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3075 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3076 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3081 talloc_free(mem_ctx);
3086 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3087 struct loadparm_context *lp_ctx,
3088 struct smbcli_state *cli,
3090 struct srvsvc_NetShareInfo502 **info)
3092 struct smbcli_tree *ipc;
3093 struct dcerpc_pipe *p;
3094 struct srvsvc_NetShareGetInfo r;
3097 if (!(p = dcerpc_pipe_init(cli,
3098 cli->transport->socket->event.ctx,
3099 lp_iconv_convenience(lp_ctx)))) {
3100 status = NT_STATUS_NO_MEMORY;
3104 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3105 if (!NT_STATUS_IS_OK(status)) {
3109 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3110 if (!NT_STATUS_IS_OK(status)) {
3111 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3116 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3117 if (!NT_STATUS_IS_OK(status)) {
3118 d_printf("dcerpc_bind_auth_none failed: %s\n",
3123 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3124 dcerpc_server_name(p));
3125 r.in.share_name = share;
3128 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3129 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3130 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3131 nt_errstr(status), win_errstr(r.out.result));
3135 *info = talloc_move(mem_ctx, &r.out.info.info502);
3136 return NT_STATUS_OK;
3144 * Get us a handle on HKLM\
3147 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3148 struct smbcli_state *cli,
3149 struct smb_iconv_convenience *iconv_convenience,
3150 struct dcerpc_pipe **pipe_p,
3151 struct policy_handle **handle)
3153 struct smbcli_tree *ipc;
3154 struct dcerpc_pipe *p;
3155 struct winreg_OpenHKLM r;
3157 struct policy_handle *result;
3159 result = talloc(mem_ctx, struct policy_handle);
3161 if (result == NULL) {
3162 return NT_STATUS_NO_MEMORY;
3165 if (!(p = dcerpc_pipe_init(result,
3166 cli->transport->socket->event.ctx,
3167 iconv_convenience))) {
3168 status = NT_STATUS_NO_MEMORY;
3172 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3173 if (!NT_STATUS_IS_OK(status)) {
3177 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3178 if (!NT_STATUS_IS_OK(status)) {
3179 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3184 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3185 if (!NT_STATUS_IS_OK(status)) {
3186 d_printf("dcerpc_bind_auth_none failed: %s\n",
3191 r.in.system_name = 0;
3192 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3193 r.out.handle = result;
3195 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3196 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3197 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3198 nt_errstr(status), win_errstr(r.out.result));
3204 return NT_STATUS_OK;
3207 talloc_free(result);
3211 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3212 struct smb_iconv_convenience *iconv_convenience,
3213 const char *sharename)
3215 struct dcerpc_pipe *p;
3216 struct policy_handle *hklm = NULL;
3217 struct policy_handle new_handle;
3218 struct winreg_CreateKey c;
3219 struct winreg_CloseKey cl;
3220 enum winreg_CreateAction action_taken;
3222 TALLOC_CTX *mem_ctx;
3224 mem_ctx = talloc_new(cli);
3225 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3227 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3228 if (!NT_STATUS_IS_OK(status)) {
3229 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3234 c.in.name.name = talloc_asprintf(
3235 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3236 if (c.in.name.name == NULL) {
3237 d_printf("talloc_asprintf failed\n");
3238 status = NT_STATUS_NO_MEMORY;
3241 c.in.keyclass.name = "";
3243 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3244 c.in.secdesc = NULL;
3245 c.in.action_taken = &action_taken;
3246 c.out.new_handle = &new_handle;
3247 c.out.action_taken = &action_taken;
3249 status = dcerpc_winreg_CreateKey(p, p, &c);
3250 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3251 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3252 nt_errstr(status), win_errstr(c.out.result));
3256 cl.in.handle = &new_handle;
3257 cl.out.handle = &new_handle;
3258 status = dcerpc_winreg_CloseKey(p, p, &cl);
3259 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3260 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3261 nt_errstr(status), win_errstr(cl.out.result));
3267 talloc_free(mem_ctx);
3271 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3272 struct smbcli_state *cli,
3273 const char *sharename)
3275 struct dcerpc_pipe *p;
3276 struct policy_handle *hklm = NULL;
3277 struct winreg_DeleteKey d;
3279 TALLOC_CTX *mem_ctx;
3281 mem_ctx = talloc_new(cli);
3282 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3284 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3286 if (!NT_STATUS_IS_OK(status)) {
3287 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3292 d.in.key.name = talloc_asprintf(
3293 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3294 if (d.in.key.name == NULL) {
3295 d_printf("talloc_asprintf failed\n");
3296 status = NT_STATUS_NO_MEMORY;
3299 status = dcerpc_winreg_DeleteKey(p, p, &d);
3300 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3301 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3302 nt_errstr(status), win_errstr(d.out.result));
3307 talloc_free(mem_ctx);
3311 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3312 struct loadparm_context *lp_ctx,
3313 const char *sharename,
3314 const char *parameter,
3317 struct dcerpc_pipe *p = NULL;
3318 struct policy_handle *hklm = NULL, key_handle;
3319 struct winreg_OpenKey o;
3320 struct winreg_SetValue s;
3325 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3326 if (!NT_STATUS_IS_OK(status)) {
3327 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3331 o.in.parent_handle = hklm;
3332 o.in.keyname.name = talloc_asprintf(
3333 hklm, "software\\samba\\smbconf\\%s", sharename);
3334 if (o.in.keyname.name == NULL) {
3335 d_printf("talloc_asprintf failed\n");
3336 status = NT_STATUS_NO_MEMORY;
3340 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3341 o.out.handle = &key_handle;
3343 status = dcerpc_winreg_OpenKey(p, p, &o);
3344 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3345 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3346 nt_errstr(status), win_errstr(o.out.result));
3350 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3351 value, &type, &val)) {
3352 d_printf("(%s) reg_string_to_val failed\n", __location__);
3356 s.in.handle = &key_handle;
3357 s.in.name.name = parameter;
3359 s.in.data = val.data;
3360 s.in.size = val.length;
3362 status = dcerpc_winreg_SetValue(p, p, &s);
3363 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3364 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3365 nt_errstr(status), win_errstr(s.out.result));
3374 bool torture_samba3_regconfig(struct torture_context *torture)
3376 struct smbcli_state *cli;
3377 struct srvsvc_NetShareInfo502 *i = NULL;
3380 const char *comment = "Dummer Kommentar";
3382 if (!(torture_open_connection(&cli, torture, 0))) {
3386 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3387 if (!NT_STATUS_IS_OK(status)) {
3388 torture_warning(torture, "torture_samba3_createshare failed: "
3389 "%s\n", nt_errstr(status));
3393 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3394 if (!NT_STATUS_IS_OK(status)) {
3395 torture_warning(torture, "torture_samba3_setconfig failed: "
3396 "%s\n", nt_errstr(status));
3400 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3401 if (!NT_STATUS_IS_OK(status)) {
3402 torture_warning(torture, "get_shareinfo failed: "
3403 "%s\n", nt_errstr(status));
3407 if (strcmp(comment, i->comment) != 0) {
3408 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3409 comment, i->comment);
3413 status = torture_samba3_deleteshare(torture, cli, "blubber");
3414 if (!NT_STATUS_IS_OK(status)) {
3415 torture_warning(torture, "torture_samba3_deleteshare failed: "
3416 "%s\n", nt_errstr(status));