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;
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;
1150 s.in.server_name = talloc_asprintf(
1151 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1152 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1153 s.in.account_name = talloc_asprintf(
1154 mem_ctx, "%s$", s.in.computer_name);
1155 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1156 E_md4hash(password, s.in.new_password.hash);
1158 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1159 creds_des_encrypt(creds_state, &s.in.new_password);
1160 creds_client_authenticator(creds_state, &s.in.credential);
1162 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1163 if (!NT_STATUS_IS_OK(status)) {
1164 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1168 if (!creds_client_check(creds_state,
1169 &s.out.return_authenticator.cred)) {
1170 printf("Credential chaining failed\n");
1173 cli_credentials_set_password(wks_creds, password,
1179 talloc_free(mem_ctx);
1184 * Delete the wks account again
1187 static bool leave(struct smbcli_state *cli,
1188 struct loadparm_context *lp_ctx,
1189 struct cli_credentials *admin_creds,
1190 struct cli_credentials *wks_creds)
1192 char *wks_name = talloc_asprintf(
1193 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1196 ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1197 talloc_free(wks_name);
1202 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1205 bool torture_netlogon_samba3(struct torture_context *torture)
1207 TALLOC_CTX *mem_ctx;
1210 struct smbcli_state *cli;
1211 struct cli_credentials *anon_creds;
1212 struct cli_credentials *wks_creds;
1213 const char *wks_name;
1215 struct smbcli_options options;
1216 struct smbcli_session_options session_options;
1218 wks_name = torture_setting_string(torture, "wksname", NULL);
1219 if (wks_name == NULL) {
1220 wks_name = get_myname();
1223 mem_ctx = talloc_init("torture_netlogon_samba3");
1225 if (mem_ctx == NULL) {
1226 d_printf("talloc_init failed\n");
1230 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1231 d_printf("create_anon_creds failed\n");
1235 lp_smbcli_options(torture->lp_ctx, &options);
1236 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1238 status = smbcli_full_connection(mem_ctx, &cli,
1239 torture_setting_string(torture, "host", NULL),
1240 lp_smb_ports(torture->lp_ctx),
1241 "IPC$", NULL, anon_creds,
1242 lp_resolve_context(torture->lp_ctx),
1243 torture->ev, &options, &session_options,
1244 lp_iconv_convenience(torture->lp_ctx));
1245 if (!NT_STATUS_IS_OK(status)) {
1246 d_printf("smbcli_full_connection failed: %s\n",
1251 wks_creds = cli_credentials_init(mem_ctx);
1252 if (wks_creds == NULL) {
1253 d_printf("cli_credentials_init failed\n");
1257 cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1258 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1259 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1260 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1261 cli_credentials_set_password(wks_creds,
1262 generate_random_str(wks_creds, 8),
1265 if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1266 d_printf("join failed\n");
1270 cli_credentials_set_domain(
1271 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1274 for (i=0; i<2; i++) {
1276 /* Do this more than once, the routine "schan" changes
1277 * the workstation password using the netlogon
1278 * password change routine */
1282 if (!auth2(cli, torture->lp_ctx, wks_creds)) {
1283 d_printf("auth2 failed\n");
1287 for (j=0; j<2; j++) {
1288 if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1289 d_printf("schan failed\n");
1295 if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1296 d_printf("leave failed\n");
1303 talloc_free(mem_ctx);
1308 * Do a simple join, testjoin and leave using specified smb and samr
1312 static bool test_join3(struct torture_context *tctx,
1314 struct cli_credentials *smb_creds,
1315 struct cli_credentials *samr_creds,
1316 const char *wks_name)
1320 struct smbcli_state *cli;
1321 struct cli_credentials *wks_creds;
1322 struct smbcli_options options;
1323 struct smbcli_session_options session_options;
1325 lp_smbcli_options(tctx->lp_ctx, &options);
1326 lp_smbcli_session_options(tctx->lp_ctx, &session_options);
1328 status = smbcli_full_connection(tctx, &cli,
1329 torture_setting_string(tctx, "host", NULL),
1330 lp_smb_ports(tctx->lp_ctx),
1331 "IPC$", NULL, smb_creds,
1332 lp_resolve_context(tctx->lp_ctx),
1333 tctx->ev, &options, &session_options,
1334 lp_iconv_convenience(tctx->lp_ctx));
1335 if (!NT_STATUS_IS_OK(status)) {
1336 d_printf("smbcli_full_connection failed: %s\n",
1341 wks_creds = cli_credentials_init(cli);
1342 if (wks_creds == NULL) {
1343 d_printf("cli_credentials_init failed\n");
1347 cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1348 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1349 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1350 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1351 cli_credentials_set_password(wks_creds,
1352 generate_random_str(wks_creds, 8),
1355 if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1356 d_printf("join failed\n");
1360 cli_credentials_set_domain(
1361 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1364 if (!auth2(cli, tctx->lp_ctx, wks_creds)) {
1365 d_printf("auth2 failed\n");
1369 if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1370 d_printf("leave failed\n");
1383 * Test the different session key variants. Do it by joining, this uses the
1384 * session key in the setpassword routine. Test the join by doing the auth2.
1387 bool torture_samba3_sessionkey(struct torture_context *torture)
1390 struct cli_credentials *anon_creds;
1391 const char *wks_name;
1393 wks_name = torture_setting_string(torture, "wksname", get_myname());
1395 if (!(anon_creds = cli_credentials_init_anon(torture))) {
1396 d_printf("create_anon_creds failed\n");
1400 cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);
1404 if (!torture_setting_bool(torture, "samba3", false)) {
1406 /* Samba3 in the build farm right now does this happily. Need
1409 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1410 d_printf("join using anonymous bind on an anonymous smb "
1411 "connection succeeded -- HUH??\n");
1416 if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1418 d_printf("join using ntlmssp bind on an anonymous smb "
1419 "connection failed\n");
1423 if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1424 d_printf("join using anonymous bind on an authenticated smb "
1425 "connection failed\n");
1429 if (!test_join3(torture, false, cmdline_credentials,
1430 cmdline_credentials,
1432 d_printf("join using ntlmssp bind on an authenticated smb "
1433 "connection failed\n");
1438 * The following two are tests for setuserinfolevel 25
1441 if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1443 d_printf("join using ntlmssp bind on an anonymous smb "
1444 "connection failed\n");
1448 if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1449 d_printf("join using anonymous bind on an authenticated smb "
1450 "connection failed\n");
1460 * open pipe and bind, given an IPC$ context
1463 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1464 struct loadparm_context *lp_ctx,
1465 struct smbcli_tree *tree,
1466 const char *pipe_name,
1467 const struct ndr_interface_table *iface,
1468 struct dcerpc_pipe **p)
1470 struct dcerpc_pipe *result;
1473 if (!(result = dcerpc_pipe_init(
1474 mem_ctx, tree->session->transport->socket->event.ctx,
1475 lp_iconv_convenience(lp_ctx)))) {
1476 return NT_STATUS_NO_MEMORY;
1479 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1480 if (!NT_STATUS_IS_OK(status)) {
1481 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1483 talloc_free(result);
1487 status = dcerpc_bind_auth_none(result, iface);
1488 if (!NT_STATUS_IS_OK(status)) {
1489 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1490 talloc_free(result);
1495 return NT_STATUS_OK;
1499 * Sane wrapper around lsa_LookupNames
1502 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1503 struct dcerpc_pipe *p,
1507 struct lsa_ObjectAttribute attr;
1508 struct lsa_QosInfo qos;
1509 struct lsa_OpenPolicy2 r;
1512 struct policy_handle handle;
1513 struct lsa_LookupNames l;
1514 struct lsa_TransSidArray sids;
1515 struct lsa_RefDomainList *domains = NULL;
1516 struct lsa_String lsa_name;
1518 struct dom_sid *result;
1519 TALLOC_CTX *tmp_ctx;
1521 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1526 qos.impersonation_level = 2;
1527 qos.context_mode = 1;
1528 qos.effective_only = 0;
1531 attr.root_dir = NULL;
1532 attr.object_name = NULL;
1533 attr.attributes = 0;
1534 attr.sec_desc = NULL;
1535 attr.sec_qos = &qos;
1537 r.in.system_name = "\\";
1539 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1540 r.out.handle = &handle;
1542 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1543 if (!NT_STATUS_IS_OK(status)) {
1544 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1545 talloc_free(tmp_ctx);
1552 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1554 l.in.handle = &handle;
1556 l.in.names = &lsa_name;
1559 l.in.count = &count;
1560 l.out.count = &count;
1562 l.out.domains = &domains;
1564 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1565 if (!NT_STATUS_IS_OK(status)) {
1566 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1568 talloc_free(tmp_ctx);
1572 result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
1573 l.out.sids->sids[0].rid);
1575 c.in.handle = &handle;
1576 c.out.handle = &handle;
1578 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1581 talloc_free(tmp_ctx);
1585 talloc_free(tmp_ctx);
1590 * Find out the user SID on this connection
1593 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx,
1594 struct loadparm_context *lp_ctx,
1595 struct smbcli_tree *tree)
1597 struct dcerpc_pipe *lsa;
1598 struct lsa_GetUserName r;
1600 struct lsa_String *authority_name_p = NULL;
1601 struct lsa_String *account_name_p = NULL;
1602 struct dom_sid *result;
1604 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\lsarpc",
1605 &ndr_table_lsarpc, &lsa);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 d_printf("(%s) Could not bind to LSA: %s\n",
1608 __location__, nt_errstr(status));
1612 r.in.system_name = "\\";
1613 r.in.account_name = &account_name_p;
1614 r.in.authority_name = &authority_name_p;
1615 r.out.account_name = &account_name_p;
1617 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1619 authority_name_p = *r.out.authority_name;
1621 if (!NT_STATUS_IS_OK(status)) {
1622 printf("(%s) GetUserName failed - %s\n",
1623 __location__, nt_errstr(status));
1628 result = name2sid(mem_ctx, lsa, account_name_p->string,
1629 authority_name_p->string);
1635 static int destroy_tree(struct smbcli_tree *tree)
1637 smb_tree_disconnect(tree);
1642 * Do a tcon, given a session
1645 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1646 struct smbcli_session *session,
1647 const char *sharename,
1648 struct smbcli_tree **res)
1650 struct smbcli_tree *result;
1651 TALLOC_CTX *tmp_ctx;
1652 union smb_tcon tcon;
1655 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1656 return NT_STATUS_NO_MEMORY;
1659 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1660 talloc_free(tmp_ctx);
1661 return NT_STATUS_NO_MEMORY;
1664 tcon.generic.level = RAW_TCON_TCONX;
1665 tcon.tconx.in.flags = 0;
1666 tcon.tconx.in.password = data_blob(NULL, 0);
1667 tcon.tconx.in.path = sharename;
1668 tcon.tconx.in.device = "?????";
1670 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1671 if (!NT_STATUS_IS_OK(status)) {
1672 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1674 talloc_free(tmp_ctx);
1678 result->tid = tcon.tconx.out.tid;
1679 result = talloc_steal(mem_ctx, result);
1680 talloc_set_destructor(result, destroy_tree);
1681 talloc_free(tmp_ctx);
1683 return NT_STATUS_OK;
1687 * Test the getusername behaviour
1690 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1693 struct smbcli_state *cli;
1694 TALLOC_CTX *mem_ctx;
1696 struct dom_sid *user_sid;
1697 struct dom_sid *created_sid;
1698 struct cli_credentials *anon_creds;
1699 struct cli_credentials *user_creds;
1701 struct smbcli_options options;
1702 struct smbcli_session_options session_options;
1704 if (!(mem_ctx = talloc_new(torture))) {
1708 lp_smbcli_options(torture->lp_ctx, &options);
1709 lp_smbcli_session_options(torture->lp_ctx, &session_options);
1711 status = smbcli_full_connection(
1712 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1713 lp_smb_ports(torture->lp_ctx),
1714 "IPC$", NULL, cmdline_credentials,
1715 lp_resolve_context(torture->lp_ctx),
1716 torture->ev, &options, &session_options,
1717 lp_iconv_convenience(torture->lp_ctx));
1718 if (!NT_STATUS_IS_OK(status)) {
1719 d_printf("(%s) smbcli_full_connection failed: %s\n",
1720 __location__, nt_errstr(status));
1725 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1726 d_printf("(%s) whoami on auth'ed connection failed\n",
1733 if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1734 d_printf("(%s) create_anon_creds failed\n", __location__);
1739 status = smbcli_full_connection(
1740 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1741 lp_smb_ports(torture->lp_ctx),
1742 "IPC$", NULL, anon_creds,
1743 lp_resolve_context(torture->lp_ctx),
1744 torture->ev, &options, &session_options,
1745 lp_iconv_convenience(torture->lp_ctx));
1746 if (!NT_STATUS_IS_OK(status)) {
1747 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1748 __location__, nt_errstr(status));
1753 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
1754 d_printf("(%s) whoami on anon connection failed\n",
1760 if (!dom_sid_equal(user_sid,
1761 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1762 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1763 "S-1-5-7", __location__,
1764 dom_sid_string(mem_ctx, user_sid));
1768 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1769 d_printf("(%s) cli_credentials_init failed\n", __location__);
1774 cli_credentials_set_conf(user_creds, torture->lp_ctx);
1775 cli_credentials_set_username(user_creds, "torture_username",
1777 cli_credentials_set_password(user_creds,
1778 generate_random_str(user_creds, 8),
1781 if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1782 cli_credentials_get_username(user_creds),
1783 cli_credentials_get_password(user_creds),
1784 &domain_name, &created_sid)) {
1785 d_printf("(%s) create_user failed\n", __location__);
1790 cli_credentials_set_domain(user_creds, domain_name,
1794 struct smbcli_session *session2;
1795 struct smb_composite_sesssetup setup;
1796 struct smbcli_tree *tree;
1798 session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
1799 if (session2 == NULL) {
1800 d_printf("(%s) smbcli_session_init failed\n",
1805 setup.in.sesskey = cli->transport->negotiate.sesskey;
1806 setup.in.capabilities = cli->transport->negotiate.capabilities;
1807 setup.in.workgroup = "";
1808 setup.in.credentials = user_creds;
1810 status = smb_composite_sesssetup(session2, &setup);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 d_printf("(%s) session setup with new user failed: "
1813 "%s\n", __location__, nt_errstr(status));
1817 session2->vuid = setup.out.vuid;
1819 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1821 d_printf("(%s) secondary_tcon failed\n",
1827 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, tree))) {
1828 d_printf("(%s) whoami on user connection failed\n",
1837 d_printf("Created %s, found %s\n",
1838 dom_sid_string(mem_ctx, created_sid),
1839 dom_sid_string(mem_ctx, user_sid));
1841 if (!dom_sid_equal(created_sid, user_sid)) {
1846 if (!delete_user(cli, torture->lp_ctx,
1847 cmdline_credentials,
1848 cli_credentials_get_username(user_creds))) {
1849 d_printf("(%s) delete_user failed\n", __location__);
1854 talloc_free(mem_ctx);
1858 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1859 const char *sharename)
1862 struct srvsvc_NetShareGetInfo r;
1863 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1867 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1868 dcerpc_server_name(p));
1869 r.in.share_name = sharename;
1871 for (i=0;i<ARRAY_SIZE(levels);i++) {
1872 r.in.level = levels[i];
1876 printf("testing NetShareGetInfo level %u on share '%s'\n",
1877 r.in.level, r.in.share_name);
1879 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1880 if (!NT_STATUS_IS_OK(status)) {
1881 printf("NetShareGetInfo level %u on share '%s' failed"
1882 " - %s\n", r.in.level, r.in.share_name,
1887 if (!W_ERROR_IS_OK(r.out.result)) {
1888 printf("NetShareGetInfo level %u on share '%s' failed "
1889 "- %s\n", r.in.level, r.in.share_name,
1890 win_errstr(r.out.result));
1899 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1900 const char **one_sharename)
1903 struct srvsvc_NetShareEnum r;
1904 struct srvsvc_NetShareCtr0 c0;
1905 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1909 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1910 r.in.ctr.ctr0 = &c0;
1911 r.in.ctr.ctr0->count = 0;
1912 r.in.ctr.ctr0->array = NULL;
1913 r.in.max_buffer = (uint32_t)-1;
1914 r.in.resume_handle = NULL;
1916 for (i=0;i<ARRAY_SIZE(levels);i++) {
1917 r.in.level = levels[i];
1921 printf("testing NetShareEnum level %u\n", r.in.level);
1922 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1923 if (!NT_STATUS_IS_OK(status)) {
1924 printf("NetShareEnum level %u failed - %s\n",
1925 r.in.level, nt_errstr(status));
1929 if (!W_ERROR_IS_OK(r.out.result)) {
1930 printf("NetShareEnum level %u failed - %s\n",
1931 r.in.level, win_errstr(r.out.result));
1934 if (r.in.level == 0) {
1935 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1936 if (ctr->count > 0) {
1937 *one_sharename = ctr->array[0].name;
1945 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
1947 struct dcerpc_pipe *p;
1948 TALLOC_CTX *mem_ctx;
1950 const char *sharename = NULL;
1951 struct smbcli_state *cli;
1954 if (!(mem_ctx = talloc_new(torture))) {
1958 if (!(torture_open_connection_share(
1959 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
1960 "IPC$", torture->ev))) {
1961 talloc_free(mem_ctx);
1965 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree,
1966 "\\pipe\\srvsvc", &ndr_table_srvsvc, &p);
1967 if (!NT_STATUS_IS_OK(status)) {
1968 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1969 __location__, nt_errstr(status));
1974 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1975 if (sharename == NULL) {
1976 printf("did not get sharename\n");
1978 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1982 talloc_free(mem_ctx);
1987 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1988 * NT_STATUS_NO_SAM_ACCOUNT
1991 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1993 TALLOC_CTX *mem_ctx;
1994 struct dcerpc_pipe *net_pipe;
1996 bool result = false;
1998 struct netr_ServerReqChallenge r;
1999 struct netr_Credential netr_cli_creds;
2000 struct netr_Credential netr_srv_creds;
2001 uint32_t negotiate_flags;
2002 struct netr_ServerAuthenticate2 a;
2003 struct creds_CredentialState *creds_state;
2004 struct netr_Credential netr_cred;
2005 struct samr_Password mach_pw;
2006 struct smbcli_state *cli;
2008 if (!(mem_ctx = talloc_new(torture))) {
2009 d_printf("talloc_new failed\n");
2013 if (!(wksname = generate_random_str_list(
2014 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
2015 d_printf("generate_random_str_list failed\n");
2019 if (!(torture_open_connection_share(
2021 torture, torture_setting_string(torture, "host", NULL),
2022 "IPC$", torture->ev))) {
2023 d_printf("IPC$ connection failed\n");
2027 if (!(net_pipe = dcerpc_pipe_init(
2028 mem_ctx, cli->transport->socket->event.ctx,
2029 lp_iconv_convenience(torture->lp_ctx)))) {
2030 d_printf("dcerpc_pipe_init failed\n");
2034 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
2035 if (!NT_STATUS_IS_OK(status)) {
2036 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2041 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
2042 if (!NT_STATUS_IS_OK(status)) {
2043 d_printf("dcerpc_bind_auth_none failed: %s\n",
2048 r.in.computer_name = wksname;
2049 r.in.server_name = talloc_asprintf(
2050 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2051 if (r.in.server_name == NULL) {
2052 d_printf("talloc_asprintf failed\n");
2055 generate_random_buffer(netr_cli_creds.data,
2056 sizeof(netr_cli_creds.data));
2057 r.in.credentials = &netr_cli_creds;
2058 r.out.credentials = &netr_srv_creds;
2060 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
2061 if (!NT_STATUS_IS_OK(status)) {
2062 d_printf("netr_ServerReqChallenge failed: %s\n",
2067 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
2068 E_md4hash("foobar", mach_pw.hash);
2070 creds_state = talloc(mem_ctx, struct creds_CredentialState);
2071 creds_client_init(creds_state, r.in.credentials,
2072 r.out.credentials, &mach_pw,
2073 &netr_cred, negotiate_flags);
2075 a.in.server_name = talloc_asprintf(
2076 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
2077 a.in.account_name = talloc_asprintf(
2078 mem_ctx, "%s$", wksname);
2079 a.in.computer_name = wksname;
2080 a.in.secure_channel_type = SEC_CHAN_WKSTA;
2081 a.in.negotiate_flags = &negotiate_flags;
2082 a.out.negotiate_flags = &negotiate_flags;
2083 a.in.credentials = &netr_cred;
2084 a.out.credentials = &netr_cred;
2086 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
2088 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
2089 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
2090 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
2097 talloc_free(mem_ctx);
2101 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
2102 struct loadparm_context *lp_ctx,
2103 struct smbcli_session *sess,
2104 const char *sharename)
2106 struct smbcli_tree *tree;
2107 TALLOC_CTX *tmp_ctx;
2108 struct dcerpc_pipe *p;
2110 struct srvsvc_NetShareGetInfo r;
2111 struct security_descriptor *result;
2113 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2114 d_printf("talloc_new failed\n");
2118 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2119 d_printf("secondary_tcon failed\n");
2120 talloc_free(tmp_ctx);
2124 status = pipe_bind_smb(mem_ctx, lp_ctx, tree, "\\pipe\\srvsvc",
2125 &ndr_table_srvsvc, &p);
2126 if (!NT_STATUS_IS_OK(status)) {
2127 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2128 __location__, nt_errstr(status));
2129 talloc_free(tmp_ctx);
2134 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2137 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2138 dcerpc_server_name(p));
2139 r.in.share_name = sharename;
2142 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2143 if (!NT_STATUS_IS_OK(status)) {
2144 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2146 talloc_free(tmp_ctx);
2150 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2151 talloc_free(tmp_ctx);
2155 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2156 struct loadparm_context *lp_ctx,
2157 struct smbcli_session *sess,
2158 const char *sharename,
2159 struct security_descriptor *sd)
2161 struct smbcli_tree *tree;
2162 TALLOC_CTX *tmp_ctx;
2163 struct dcerpc_pipe *p;
2165 struct sec_desc_buf i;
2166 struct srvsvc_NetShareSetInfo r;
2169 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2170 d_printf("talloc_new failed\n");
2171 return NT_STATUS_NO_MEMORY;
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);
2177 return NT_STATUS_UNSUCCESSFUL;
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);
2186 return NT_STATUS_UNSUCCESSFUL;
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;
2198 r.in.info.info1501 = &i;
2199 r.in.parm_error = &error;
2201 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2202 if (!NT_STATUS_IS_OK(status)) {
2203 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2207 talloc_free(tmp_ctx);
2211 bool try_tcon(TALLOC_CTX *mem_ctx,
2212 struct loadparm_context *lp_ctx,
2213 struct security_descriptor *orig_sd,
2214 struct smbcli_session *session,
2215 const char *sharename, const struct dom_sid *user_sid,
2216 unsigned int access_mask, NTSTATUS expected_tcon,
2217 NTSTATUS expected_mkdir)
2219 TALLOC_CTX *tmp_ctx;
2220 struct smbcli_tree *rmdir_tree, *tree;
2221 struct dom_sid *domain_sid;
2223 struct security_descriptor *sd;
2227 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2228 d_printf("talloc_new failed\n");
2232 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2233 if (!NT_STATUS_IS_OK(status)) {
2234 d_printf("first tcon to delete dir failed\n");
2235 talloc_free(tmp_ctx);
2239 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2241 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2242 &domain_sid, &rid))) {
2243 d_printf("dom_sid_split_rid failed\n");
2244 talloc_free(tmp_ctx);
2248 sd = security_descriptor_dacl_create(
2249 tmp_ctx, 0, "S-1-5-32-544",
2250 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2252 dom_sid_string(mem_ctx, user_sid),
2253 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2255 d_printf("security_descriptor_dacl_create failed\n");
2256 talloc_free(tmp_ctx);
2260 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, sd);
2261 if (!NT_STATUS_IS_OK(status)) {
2262 d_printf("custom set_sharesec failed: %s\n",
2264 talloc_free(tmp_ctx);
2268 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2269 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2270 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2276 if (!NT_STATUS_IS_OK(status)) {
2277 /* An expected non-access, no point in trying to write */
2281 status = smbcli_mkdir(tree, "sharesec_testdir");
2282 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2283 d_printf("(%s) Expected %s, got %s\n", __location__,
2284 nt_errstr(expected_mkdir), nt_errstr(status));
2289 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2291 status = set_sharesec(mem_ctx, lp_ctx, session, sharename, orig_sd);
2292 if (!NT_STATUS_IS_OK(status)) {
2293 d_printf("custom set_sharesec failed: %s\n",
2295 talloc_free(tmp_ctx);
2299 talloc_free(tmp_ctx);
2303 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2305 TALLOC_CTX *mem_ctx;
2307 struct smbcli_state *cli;
2308 struct security_descriptor *sd;
2309 struct dom_sid *user_sid;
2311 if (!(mem_ctx = talloc_new(torture))) {
2315 if (!(torture_open_connection_share(
2316 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2317 "IPC$", torture->ev))) {
2318 d_printf("IPC$ connection failed\n");
2319 talloc_free(mem_ctx);
2323 if (!(user_sid = whoami(mem_ctx, torture->lp_ctx, cli->tree))) {
2324 d_printf("whoami failed\n");
2325 talloc_free(mem_ctx);
2329 sd = get_sharesec(mem_ctx, torture->lp_ctx, cli->session,
2330 torture_setting_string(torture, "share", NULL));
2332 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2333 torture_setting_string(torture, "share", NULL),
2334 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2336 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2337 torture_setting_string(torture, "share", NULL),
2338 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2339 NT_STATUS_MEDIA_WRITE_PROTECTED);
2341 ret &= try_tcon(mem_ctx, torture->lp_ctx, sd, cli->session,
2342 torture_setting_string(torture, "share", NULL),
2343 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2345 talloc_free(mem_ctx);
2349 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2351 TALLOC_CTX *mem_ctx;
2353 struct smbcli_state *cli;
2354 struct dcerpc_pipe *p;
2355 struct policy_handle lsa_handle;
2357 struct dom_sid *domain_sid;
2359 if (!(mem_ctx = talloc_new(torture))) {
2363 if (!(torture_open_connection_share(
2364 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2365 "IPC$", torture->ev))) {
2366 d_printf("IPC$ connection failed\n");
2367 talloc_free(mem_ctx);
2371 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\lsarpc",
2372 &ndr_table_lsarpc, &p);
2373 if (!NT_STATUS_IS_OK(status)) {
2374 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2376 talloc_free(mem_ctx);
2381 struct lsa_ObjectAttribute attr;
2382 struct lsa_OpenPolicy2 o;
2383 o.in.system_name = talloc_asprintf(
2384 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2387 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2388 o.out.handle = &lsa_handle;
2389 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2390 if (!NT_STATUS_IS_OK(status)) {
2391 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2392 __location__, nt_errstr(status));
2393 talloc_free(mem_ctx);
2399 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2404 int levels[] = { 2,3,5,6 };
2406 for (i=0; i<ARRAY_SIZE(levels); i++) {
2407 struct lsa_QueryInfoPolicy r;
2408 union lsa_PolicyInformation *info = NULL;
2409 r.in.handle = &lsa_handle;
2410 r.in.level = levels[i];
2412 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2413 if (!NT_STATUS_IS_OK(status)) {
2414 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2415 "failed: %s\n", __location__,
2416 levels[i], nt_errstr(status));
2417 talloc_free(mem_ctx);
2420 if (levels[i] == 5) {
2421 domain_sid = info->account_domain.sid;
2429 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2430 struct smb_iconv_convenience *iconv_convenience,
2433 struct rap_WserverGetInfo r;
2435 char servername[17];
2438 r.in.bufsize = 0xffff;
2440 status = smbcli_rap_netservergetinfo(tree, iconv_convenience, mem_ctx, &r);
2441 if (!NT_STATUS_IS_OK(status)) {
2445 memcpy(servername, r.out.info.info0.name, 16);
2446 servername[16] = '\0';
2448 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2449 return NT_STATUS_NO_MEMORY;
2452 return NT_STATUS_OK;
2456 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
2457 struct smbcli_tree *tree,
2458 const char ***printers, int *num_printers)
2460 TALLOC_CTX *mem_ctx;
2462 struct dcerpc_pipe *p;
2463 struct srvsvc_NetShareEnum r;
2464 struct srvsvc_NetShareCtr1 c1_in;
2465 struct srvsvc_NetShareCtr1 *c1;
2468 mem_ctx = talloc_new(ctx);
2469 if (mem_ctx == NULL) {
2470 return NT_STATUS_NO_MEMORY;
2473 status = pipe_bind_smb(mem_ctx, lp_ctx,
2474 tree, "\\srvsvc", &ndr_table_srvsvc,
2476 if (!NT_STATUS_IS_OK(status)) {
2477 d_printf("could not bind to srvsvc pipe\n");
2478 talloc_free(mem_ctx);
2482 r.in.server_unc = talloc_asprintf(
2483 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2486 r.in.ctr.ctr1 = &c1_in;
2487 r.in.max_buffer = (uint32_t)-1;
2488 r.in.resume_handle = NULL;
2490 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2491 if (!NT_STATUS_IS_OK(status)) {
2492 d_printf("NetShareEnum level %u failed - %s\n",
2493 r.in.level, nt_errstr(status));
2494 talloc_free(mem_ctx);
2500 c1 = r.out.ctr.ctr1;
2501 for (i=0; i<c1->count; i++) {
2502 if (c1->array[i].type != STYPE_PRINTQ) {
2505 if (!add_string_to_array(ctx, c1->array[i].name,
2506 printers, num_printers)) {
2508 return NT_STATUS_NO_MEMORY;
2512 talloc_free(mem_ctx);
2513 return NT_STATUS_OK;
2516 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2517 const char *servername, int level, int *num_printers)
2519 struct spoolss_EnumPrinters r;
2523 r.in.flags = PRINTER_ENUM_LOCAL;
2524 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2529 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2530 if (!NT_STATUS_IS_OK(status)) {
2531 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2532 __location__, nt_errstr(status));
2536 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2537 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2538 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2539 win_errstr(r.out.result));
2543 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2544 if (blob.data == NULL) {
2545 d_printf("(%s) data_blob_talloc failed\n", __location__);
2549 r.in.buffer = &blob;
2550 r.in.offered = r.out.needed;
2552 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2553 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2554 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2555 "%s\n", __location__, nt_errstr(status),
2556 win_errstr(r.out.result));
2560 *num_printers = r.out.count;
2565 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2566 struct policy_handle *handle, int level,
2567 union spoolss_PrinterInfo **res)
2569 TALLOC_CTX *mem_ctx;
2570 struct spoolss_GetPrinter r;
2574 mem_ctx = talloc_new(ctx);
2575 if (mem_ctx == NULL) {
2576 return NT_STATUS_NO_MEMORY;
2579 r.in.handle = handle;
2584 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2585 if (!NT_STATUS_IS_OK(status)) {
2586 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2587 __location__, nt_errstr(status));
2588 talloc_free(mem_ctx);
2592 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2593 printf("GetPrinter unexpected return code %s, should "
2594 "be WERR_INSUFFICIENT_BUFFER\n",
2595 win_errstr(r.out.result));
2596 talloc_free(mem_ctx);
2597 return NT_STATUS_UNSUCCESSFUL;
2600 r.in.handle = handle;
2602 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2603 if (blob.data == NULL) {
2604 talloc_free(mem_ctx);
2605 return NT_STATUS_NO_MEMORY;
2607 memset(blob.data, 0, blob.length);
2608 r.in.buffer = &blob;
2609 r.in.offered = r.out.needed;
2611 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2612 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2613 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2614 "%s\n", __location__, nt_errstr(status),
2615 win_errstr(r.out.result));
2616 talloc_free(mem_ctx);
2617 return NT_STATUS_IS_OK(status) ?
2618 NT_STATUS_UNSUCCESSFUL : status;
2622 *res = talloc_steal(ctx, r.out.info);
2625 talloc_free(mem_ctx);
2626 return NT_STATUS_OK;
2629 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2631 TALLOC_CTX *mem_ctx;
2633 struct smbcli_state *cli;
2634 struct dcerpc_pipe *p;
2636 struct policy_handle server_handle, printer_handle;
2637 const char **printers;
2639 struct spoolss_UserLevel1 userlevel1;
2642 if (!(mem_ctx = talloc_new(torture))) {
2646 if (!(torture_open_connection_share(
2647 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2648 "IPC$", torture->ev))) {
2649 d_printf("IPC$ connection failed\n");
2650 talloc_free(mem_ctx);
2654 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2655 if (!NT_STATUS_IS_OK(status)) {
2656 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2657 __location__, nt_errstr(status));
2658 talloc_free(mem_ctx);
2662 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, torture->lp_ctx, cli->tree,
2663 &printers, &num_printers))) {
2664 talloc_free(mem_ctx);
2668 if (num_printers == 0) {
2669 d_printf("Did not find printers\n");
2670 talloc_free(mem_ctx);
2674 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\spoolss",
2675 &ndr_table_spoolss, &p);
2676 if (!NT_STATUS_IS_OK(status)) {
2677 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2679 talloc_free(mem_ctx);
2683 ZERO_STRUCT(userlevel1);
2684 userlevel1.client = talloc_asprintf(
2685 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2686 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2687 userlevel1.build = 2600;
2688 userlevel1.major = 3;
2689 userlevel1.minor = 0;
2690 userlevel1.processor = 0;
2693 struct spoolss_OpenPrinterEx r;
2696 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2698 r.in.datatype = NULL;
2699 r.in.access_mask = 0;
2701 r.in.userlevel.level1 = &userlevel1;
2702 r.out.handle = &server_handle;
2704 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2705 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2706 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2707 "%s, %s\n", __location__, nt_errstr(status),
2708 win_errstr(r.out.result));
2709 talloc_free(mem_ctx);
2715 struct spoolss_ClosePrinter r;
2717 r.in.handle = &server_handle;
2718 r.out.handle = &server_handle;
2720 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2721 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2722 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2723 "%s, %s\n", __location__, nt_errstr(status),
2724 win_errstr(r.out.result));
2725 talloc_free(mem_ctx);
2731 struct spoolss_OpenPrinterEx r;
2734 r.in.printername = talloc_asprintf(
2735 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2736 r.in.datatype = NULL;
2737 r.in.access_mask = 0;
2739 r.in.userlevel.level1 = &userlevel1;
2740 r.out.handle = &printer_handle;
2742 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2743 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2744 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2745 "%s, %s\n", __location__, nt_errstr(status),
2746 win_errstr(r.out.result));
2747 talloc_free(mem_ctx);
2755 for (i=0; i<8; i++) {
2756 status = getprinterinfo(mem_ctx, p, &printer_handle,
2758 if (!NT_STATUS_IS_OK(status)) {
2759 d_printf("(%s) getprinterinfo %d failed: %s\n",
2760 __location__, i, nt_errstr(status));
2767 struct spoolss_ClosePrinter r;
2769 r.in.handle = &printer_handle;
2770 r.out.handle = &printer_handle;
2772 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2773 if (!NT_STATUS_IS_OK(status)) {
2774 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2775 "%s\n", __location__, nt_errstr(status));
2776 talloc_free(mem_ctx);
2783 if (!enumprinters(mem_ctx, p, servername, 1,
2785 d_printf("(%s) enumprinters failed\n", __location__);
2786 talloc_free(mem_ctx);
2789 if (num_printers != num_enumerated) {
2790 d_printf("(%s) netshareenum gave %d printers, "
2791 "enumprinters lvl 1 gave %d\n", __location__,
2792 num_printers, num_enumerated);
2793 talloc_free(mem_ctx);
2800 if (!enumprinters(mem_ctx, p, servername, 2,
2802 d_printf("(%s) enumprinters failed\n", __location__);
2803 talloc_free(mem_ctx);
2806 if (num_printers != num_enumerated) {
2807 d_printf("(%s) netshareenum gave %d printers, "
2808 "enumprinters lvl 2 gave %d\n", __location__,
2809 num_printers, num_enumerated);
2810 talloc_free(mem_ctx);
2815 talloc_free(mem_ctx);
2820 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2822 TALLOC_CTX *mem_ctx;
2823 struct smbcli_state *cli;
2824 struct dcerpc_pipe *p;
2828 if (!(mem_ctx = talloc_new(torture))) {
2832 if (!(torture_open_connection_share(
2833 mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2834 "IPC$", torture->ev))) {
2835 d_printf("IPC$ connection failed\n");
2836 talloc_free(mem_ctx);
2840 status = get_servername(mem_ctx, cli->tree, lp_iconv_convenience(torture->lp_ctx), &servername);
2841 if (!NT_STATUS_IS_OK(status)) {
2842 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2843 __location__, nt_errstr(status));
2844 talloc_free(mem_ctx);
2848 status = pipe_bind_smb(mem_ctx, torture->lp_ctx, cli->tree, "\\wkssvc",
2849 &ndr_table_wkssvc, &p);
2850 if (!NT_STATUS_IS_OK(status)) {
2851 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2853 talloc_free(mem_ctx);
2858 struct wkssvc_NetWkstaInfo100 wks100;
2859 union wkssvc_NetWkstaInfo info;
2860 struct wkssvc_NetWkstaGetInfo r;
2862 r.in.server_name = "\\foo";
2864 info.info100 = &wks100;
2867 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2868 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2869 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2870 "%s, %s\n", __location__, nt_errstr(status),
2871 win_errstr(r.out.result));
2872 talloc_free(mem_ctx);
2876 if (strcmp(servername,
2877 r.out.info->info100->server_name) != 0) {
2878 d_printf("(%s) servername inconsistency: RAP=%s, "
2879 "dcerpc_wkssvc_NetWksGetInfo=%s",
2880 __location__, servername,
2881 r.out.info->info100->server_name);
2882 talloc_free(mem_ctx);
2887 talloc_free(mem_ctx);
2891 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2892 struct policy_handle *handle)
2894 struct winreg_CloseKey c;
2896 TALLOC_CTX *mem_ctx;
2898 c.in.handle = c.out.handle = handle;
2900 if (!(mem_ctx = talloc_new(p))) {
2901 return NT_STATUS_NO_MEMORY;
2904 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2905 talloc_free(mem_ctx);
2907 if (!NT_STATUS_IS_OK(status)) {
2911 if (!W_ERROR_IS_OK(c.out.result)) {
2912 return werror_to_ntstatus(c.out.result);
2915 return NT_STATUS_OK;
2918 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2919 TALLOC_CTX *mem_ctx)
2921 uint32_t enum_index = 0;
2924 struct winreg_EnumValue r;
2925 struct winreg_StringBuf name;
2926 enum winreg_Type type = 0;
2929 uint32_t size, length;
2931 r.in.handle = handle;
2932 r.in.enum_index = enum_index;
2935 r.in.name = r.out.name = &name;
2941 r.in.length = &length;
2943 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2944 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2945 return NT_STATUS_OK;
2951 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
2952 TALLOC_CTX *mem_ctx, int depth)
2954 struct winreg_EnumKey r;
2955 struct winreg_StringBuf class, name;
2960 return NT_STATUS_OK;
2966 r.in.handle = handle;
2967 r.in.enum_index = 0;
2969 r.in.keyclass = &class;
2971 r.in.last_changed_time = &t;
2974 TALLOC_CTX *tmp_ctx;
2975 struct winreg_OpenKey o;
2976 struct policy_handle key_handle;
2979 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2980 return NT_STATUS_NO_MEMORY;
2986 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2987 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2988 /* We're done enumerating */
2989 talloc_free(tmp_ctx);
2990 return NT_STATUS_OK;
2993 for (i=0; i<10-depth; i++)
2995 printf("%s\n", r.out.name->name);
2998 o.in.parent_handle = handle;
2999 o.in.keyname.name = r.out.name->name;
3001 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3002 o.out.handle = &key_handle;
3004 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
3005 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
3006 enumkeys(p, &key_handle, tmp_ctx, depth-1);
3007 enumvalues(p, &key_handle, tmp_ctx);
3008 status = winreg_close(p, &key_handle);
3009 if (!NT_STATUS_IS_OK(status)) {
3014 talloc_free(tmp_ctx);
3016 r.in.enum_index += 1;
3019 return NT_STATUS_OK;
3022 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
3024 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
3025 const char *name, winreg_open_fn open_fn)
3027 struct policy_handle handle;
3028 struct winreg_OpenHKLM r;
3031 r.in.system_name = 0;
3032 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3033 r.out.handle = &handle;
3035 status = open_fn(p, mem_ctx, &r);
3036 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3037 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
3038 nt_errstr(status), win_errstr(r.out.result));
3042 enumkeys(p, &handle, mem_ctx, 4);
3044 status = winreg_close(p, &handle);
3045 if (!NT_STATUS_IS_OK(status)) {
3046 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
3047 __location__, nt_errstr(status));
3054 bool torture_samba3_rpc_winreg(struct torture_context *torture)
3057 struct dcerpc_pipe *p;
3058 TALLOC_CTX *mem_ctx;
3064 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
3065 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
3066 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
3067 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
3068 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
3073 mem_ctx = talloc_init("torture_rpc_winreg");
3075 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
3077 if (!NT_STATUS_IS_OK(status)) {
3078 talloc_free(mem_ctx);
3083 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
3085 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
3086 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
3091 talloc_free(mem_ctx);
3096 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
3097 struct loadparm_context *lp_ctx,
3098 struct smbcli_state *cli,
3100 struct srvsvc_NetShareInfo502 **info)
3102 struct smbcli_tree *ipc;
3103 struct dcerpc_pipe *p;
3104 struct srvsvc_NetShareGetInfo r;
3107 if (!(p = dcerpc_pipe_init(cli,
3108 cli->transport->socket->event.ctx,
3109 lp_iconv_convenience(lp_ctx)))) {
3110 status = NT_STATUS_NO_MEMORY;
3114 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3115 if (!NT_STATUS_IS_OK(status)) {
3119 status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
3120 if (!NT_STATUS_IS_OK(status)) {
3121 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3126 status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3127 if (!NT_STATUS_IS_OK(status)) {
3128 d_printf("dcerpc_bind_auth_none failed: %s\n",
3133 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
3134 dcerpc_server_name(p));
3135 r.in.share_name = share;
3138 status = dcerpc_srvsvc_NetShareGetInfo(p, p, &r);
3139 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3140 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3141 nt_errstr(status), win_errstr(r.out.result));
3145 *info = talloc_move(mem_ctx, &r.out.info.info502);
3146 return NT_STATUS_OK;
3154 * Get us a handle on HKLM\
3157 static NTSTATUS get_hklm_handle(TALLOC_CTX *mem_ctx,
3158 struct smbcli_state *cli,
3159 struct smb_iconv_convenience *iconv_convenience,
3160 struct dcerpc_pipe **pipe_p,
3161 struct policy_handle **handle)
3163 struct smbcli_tree *ipc;
3164 struct dcerpc_pipe *p;
3165 struct winreg_OpenHKLM r;
3167 struct policy_handle *result;
3169 result = talloc(mem_ctx, struct policy_handle);
3171 if (result == NULL) {
3172 return NT_STATUS_NO_MEMORY;
3175 if (!(p = dcerpc_pipe_init(result,
3176 cli->transport->socket->event.ctx,
3177 iconv_convenience))) {
3178 status = NT_STATUS_NO_MEMORY;
3182 status = secondary_tcon(p, cli->session, "IPC$", &ipc);
3183 if (!NT_STATUS_IS_OK(status)) {
3187 status = dcerpc_pipe_open_smb(p, ipc, "\\winreg");
3188 if (!NT_STATUS_IS_OK(status)) {
3189 d_printf("dcerpc_pipe_open_smb failed: %s\n",
3194 status = dcerpc_bind_auth_none(p, &ndr_table_winreg);
3195 if (!NT_STATUS_IS_OK(status)) {
3196 d_printf("dcerpc_bind_auth_none failed: %s\n",
3201 r.in.system_name = 0;
3202 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3203 r.out.handle = result;
3205 status = dcerpc_winreg_OpenHKLM(p, p, &r);
3206 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
3207 d_printf("(%s) OpenHKLM failed: %s, %s\n", __location__,
3208 nt_errstr(status), win_errstr(r.out.result));
3214 return NT_STATUS_OK;
3217 talloc_free(result);
3221 static NTSTATUS torture_samba3_createshare(struct smbcli_state *cli,
3222 struct smb_iconv_convenience *iconv_convenience,
3223 const char *sharename)
3225 struct dcerpc_pipe *p;
3226 struct policy_handle *hklm = NULL;
3227 struct policy_handle new_handle;
3228 struct winreg_CreateKey c;
3229 struct winreg_CloseKey cl;
3230 enum winreg_CreateAction action_taken;
3232 TALLOC_CTX *mem_ctx;
3234 mem_ctx = talloc_new(cli);
3235 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3237 status = get_hklm_handle(mem_ctx, cli, iconv_convenience, &p, &hklm);
3238 if (!NT_STATUS_IS_OK(status)) {
3239 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3244 c.in.name.name = talloc_asprintf(
3245 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3246 if (c.in.name.name == NULL) {
3247 d_printf("talloc_asprintf failed\n");
3248 status = NT_STATUS_NO_MEMORY;
3251 c.in.keyclass.name = "";
3253 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3254 c.in.secdesc = NULL;
3255 c.in.action_taken = &action_taken;
3256 c.out.new_handle = &new_handle;
3257 c.out.action_taken = &action_taken;
3259 status = dcerpc_winreg_CreateKey(p, p, &c);
3260 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(c.out.result)) {
3261 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3262 nt_errstr(status), win_errstr(c.out.result));
3266 cl.in.handle = &new_handle;
3267 cl.out.handle = &new_handle;
3268 status = dcerpc_winreg_CloseKey(p, p, &cl);
3269 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(cl.out.result)) {
3270 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3271 nt_errstr(status), win_errstr(cl.out.result));
3277 talloc_free(mem_ctx);
3281 static NTSTATUS torture_samba3_deleteshare(struct torture_context *torture,
3282 struct smbcli_state *cli,
3283 const char *sharename)
3285 struct dcerpc_pipe *p;
3286 struct policy_handle *hklm = NULL;
3287 struct winreg_DeleteKey d;
3289 TALLOC_CTX *mem_ctx;
3291 mem_ctx = talloc_new(cli);
3292 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
3294 status = get_hklm_handle(cli, cli, lp_iconv_convenience(torture->lp_ctx),
3296 if (!NT_STATUS_IS_OK(status)) {
3297 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3302 d.in.key.name = talloc_asprintf(
3303 mem_ctx, "software\\samba\\smbconf\\%s", sharename);
3304 if (d.in.key.name == NULL) {
3305 d_printf("talloc_asprintf failed\n");
3306 status = NT_STATUS_NO_MEMORY;
3309 status = dcerpc_winreg_DeleteKey(p, p, &d);
3310 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(d.out.result)) {
3311 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3312 nt_errstr(status), win_errstr(d.out.result));
3317 talloc_free(mem_ctx);
3321 static NTSTATUS torture_samba3_setconfig(struct smbcli_state *cli,
3322 struct loadparm_context *lp_ctx,
3323 const char *sharename,
3324 const char *parameter,
3327 struct dcerpc_pipe *p = NULL;
3328 struct policy_handle *hklm = NULL, key_handle;
3329 struct winreg_OpenKey o;
3330 struct winreg_SetValue s;
3335 status = get_hklm_handle(cli, cli, lp_iconv_convenience(lp_ctx), &p, &hklm);
3336 if (!NT_STATUS_IS_OK(status)) {
3337 d_printf("get_hklm_handle failed: %s\n", nt_errstr(status));
3341 o.in.parent_handle = hklm;
3342 o.in.keyname.name = talloc_asprintf(
3343 hklm, "software\\samba\\smbconf\\%s", sharename);
3344 if (o.in.keyname.name == NULL) {
3345 d_printf("talloc_asprintf failed\n");
3346 status = NT_STATUS_NO_MEMORY;
3350 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3351 o.out.handle = &key_handle;
3353 status = dcerpc_winreg_OpenKey(p, p, &o);
3354 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(o.out.result)) {
3355 d_printf("(%s) OpenKey failed: %s, %s\n", __location__,
3356 nt_errstr(status), win_errstr(o.out.result));
3360 if (!reg_string_to_val(hklm, lp_iconv_convenience(lp_ctx), "REG_SZ",
3361 value, &type, &val)) {
3362 d_printf("(%s) reg_string_to_val failed\n", __location__);
3366 s.in.handle = &key_handle;
3367 s.in.name.name = parameter;
3369 s.in.data = val.data;
3370 s.in.size = val.length;
3372 status = dcerpc_winreg_SetValue(p, p, &s);
3373 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(s.out.result)) {
3374 d_printf("(%s) SetValue failed: %s, %s\n", __location__,
3375 nt_errstr(status), win_errstr(s.out.result));
3384 bool torture_samba3_regconfig(struct torture_context *torture)
3386 struct smbcli_state *cli;
3387 struct srvsvc_NetShareInfo502 *i = NULL;
3390 const char *comment = "Dummer Kommentar";
3392 if (!(torture_open_connection(&cli, torture, 0))) {
3396 status = torture_samba3_createshare(cli, lp_iconv_convenience(torture->lp_ctx), "blubber");
3397 if (!NT_STATUS_IS_OK(status)) {
3398 torture_warning(torture, "torture_samba3_createshare failed: "
3399 "%s\n", nt_errstr(status));
3403 status = torture_samba3_setconfig(cli, torture->lp_ctx, "blubber", "comment", comment);
3404 if (!NT_STATUS_IS_OK(status)) {
3405 torture_warning(torture, "torture_samba3_setconfig failed: "
3406 "%s\n", nt_errstr(status));
3410 status = get_shareinfo(torture, torture->lp_ctx, cli, "blubber", &i);
3411 if (!NT_STATUS_IS_OK(status)) {
3412 torture_warning(torture, "get_shareinfo failed: "
3413 "%s\n", nt_errstr(status));
3417 if (strcmp(comment, i->comment) != 0) {
3418 torture_warning(torture, "Expected comment [%s], got [%s]\n",
3419 comment, i->comment);
3423 status = torture_samba3_deleteshare(torture, cli, "blubber");
3424 if (!NT_STATUS_IS_OK(status)) {
3425 torture_warning(torture, "torture_samba3_deleteshare failed: "
3426 "%s\n", nt_errstr(status));