samba3rpc.c

来自「samba最新软件」· C语言 代码 · 共 2,504 行 · 第 1/5 页

C
2,504
字号
/*    Unix SMB/CIFS implementation.   dcerpc torture tests, designed to walk Samba3 code paths   Copyright (C) Volker Lendecke 2006   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 3 of the License, or   (at your option) any later version.      This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program.  If not, see <http://www.gnu.org/licenses/>.*/#include "includes.h"#include "libcli/raw/libcliraw.h"#include "libcli/raw/raw_proto.h"#include "libcli/rap/rap.h"#include "torture/torture.h"#include "torture/util.h"#include "torture/rap/proto.h"#include "librpc/gen_ndr/ndr_lsa.h"#include "librpc/gen_ndr/ndr_lsa_c.h"#include "librpc/gen_ndr/ndr_samr.h"#include "librpc/gen_ndr/ndr_samr_c.h"#include "librpc/gen_ndr/ndr_netlogon.h"#include "librpc/gen_ndr/ndr_netlogon_c.h"#include "librpc/gen_ndr/ndr_srvsvc.h"#include "librpc/gen_ndr/ndr_srvsvc_c.h"#include "librpc/gen_ndr/ndr_spoolss.h"#include "librpc/gen_ndr/ndr_spoolss_c.h"#include "librpc/gen_ndr/ndr_winreg.h"#include "librpc/gen_ndr/ndr_winreg_c.h"#include "librpc/gen_ndr/ndr_wkssvc.h"#include "librpc/gen_ndr/ndr_wkssvc_c.h"#include "lib/cmdline/popt_common.h"#include "librpc/rpc/dcerpc.h"#include "torture/rpc/rpc.h"#include "libcli/libcli.h"#include "libcli/composite/composite.h"#include "libcli/smb_composite/smb_composite.h"#include "libcli/auth/libcli_auth.h"#include "lib/crypto/crypto.h"#include "auth/ntlmssp/ntlmssp.h"#include "libcli/security/proto.h"#include "param/param.h"#include "lib/registry/registry.h"#include "libcli/resolve/resolve.h"/* * This tests a RPC call using an invalid vuid */bool torture_bind_authcontext(struct torture_context *torture) {	TALLOC_CTX *mem_ctx;	NTSTATUS status;	bool ret = false;	struct lsa_ObjectAttribute objectattr;	struct lsa_OpenPolicy2 openpolicy;	struct policy_handle handle;	struct lsa_Close close_handle;	struct smbcli_session *tmp;	struct smbcli_session *session2;	struct smbcli_state *cli;	struct dcerpc_pipe *lsa_pipe;	struct cli_credentials *anon_creds;	struct smb_composite_sesssetup setup;	struct smbcli_options options;	mem_ctx = talloc_init("torture_bind_authcontext");	if (mem_ctx == NULL) {		d_printf("talloc_init failed\n");		return false;	}	lp_smbcli_options(torture->lp_ctx, &options);	status = smbcli_full_connection(mem_ctx, &cli,					torture_setting_string(torture, "host", NULL),					lp_smb_ports(torture->lp_ctx),					"IPC$", NULL, cmdline_credentials,					lp_resolve_context(torture->lp_ctx),					torture->ev, &options);	if (!NT_STATUS_IS_OK(status)) {		d_printf("smbcli_full_connection failed: %s\n",			 nt_errstr(status));		goto done;	}	lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,				    lp_iconv_convenience(torture->lp_ctx));	if (lsa_pipe == NULL) {		d_printf("dcerpc_pipe_init failed\n");		goto done;	}	status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_pipe_open_smb failed: %s\n",			 nt_errstr(status));		goto done;	}	status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_bind_auth_none failed: %s\n",			 nt_errstr(status));		goto done;	}	openpolicy.in.system_name =talloc_asprintf(		mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));	ZERO_STRUCT(objectattr);	openpolicy.in.attr = &objectattr;	openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;	openpolicy.out.handle = &handle;	status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",			 nt_errstr(status));		goto done;	}	close_handle.in.handle = &handle;	close_handle.out.handle = &handle;	status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_lsa_Close failed: %s\n",			 nt_errstr(status));		goto done;	}	session2 = smbcli_session_init(cli->transport, mem_ctx, false);	if (session2 == NULL) {		d_printf("smbcli_session_init failed\n");		goto done;	}	if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {		d_printf("create_anon_creds failed\n");		goto done;	}	setup.in.sesskey = cli->transport->negotiate.sesskey;	setup.in.capabilities = cli->transport->negotiate.capabilities;	setup.in.workgroup = "";	setup.in.credentials = anon_creds;	status = smb_composite_sesssetup(session2, &setup);	if (!NT_STATUS_IS_OK(status)) {		d_printf("anon session setup failed: %s\n",			 nt_errstr(status));		goto done;	}	session2->vuid = setup.out.vuid;	tmp = cli->tree->session;	cli->tree->session = session2;	status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);	cli->tree->session = tmp;	talloc_free(lsa_pipe);	lsa_pipe = NULL;	if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {		d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "			 "expected NT_STATUS_INVALID_HANDLE\n",			 nt_errstr(status));		goto done;	}	ret = true; done:	talloc_free(mem_ctx);	return ret;}/* * Bind to lsa using a specific auth method */static bool bindtest(struct smbcli_state *cli,		     struct cli_credentials *credentials,		     struct loadparm_context *lp_ctx,		     uint8_t auth_type, uint8_t auth_level){	TALLOC_CTX *mem_ctx;	bool ret = false;	NTSTATUS status;	struct dcerpc_pipe *lsa_pipe;	struct lsa_ObjectAttribute objectattr;	struct lsa_OpenPolicy2 openpolicy;	struct lsa_QueryInfoPolicy query;	struct policy_handle handle;	struct lsa_Close close_handle;	if ((mem_ctx = talloc_init("bindtest")) == NULL) {		d_printf("talloc_init failed\n");		return false;	}	lsa_pipe = dcerpc_pipe_init(mem_ctx,				    cli->transport->socket->event.ctx,				    lp_iconv_convenience(lp_ctx));	if (lsa_pipe == NULL) {		d_printf("dcerpc_pipe_init failed\n");		goto done;	}	status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_pipe_open_smb failed: %s\n",			 nt_errstr(status));		goto done;	}	status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,				  credentials, lp_ctx, auth_type, auth_level,				  NULL);	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));		goto done;	}	openpolicy.in.system_name =talloc_asprintf(		mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));	ZERO_STRUCT(objectattr);	openpolicy.in.attr = &objectattr;	openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;	openpolicy.out.handle = &handle;	status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",			 nt_errstr(status));		goto done;	}	query.in.handle = &handle;	query.in.level = LSA_POLICY_INFO_DOMAIN;	status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",			 nt_errstr(status));		goto done;	}	close_handle.in.handle = &handle;	close_handle.out.handle = &handle;	status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_lsa_Close failed: %s\n",			 nt_errstr(status));		goto done;	}	ret = true; done:	talloc_free(mem_ctx);	return ret;}/* * test authenticated RPC binds with the variants Samba3 does support */bool torture_bind_samba3(struct torture_context *torture) {	TALLOC_CTX *mem_ctx;	NTSTATUS status;	bool ret = false;	struct smbcli_state *cli;	struct smbcli_options options;	mem_ctx = talloc_init("torture_bind_authcontext");	if (mem_ctx == NULL) {		d_printf("talloc_init failed\n");		return false;	}	lp_smbcli_options(torture->lp_ctx, &options);	status = smbcli_full_connection(mem_ctx, &cli,					torture_setting_string(torture, "host", NULL),					lp_smb_ports(torture->lp_ctx),					"IPC$", NULL, cmdline_credentials,					lp_resolve_context(torture->lp_ctx),					torture->ev, &options);	if (!NT_STATUS_IS_OK(status)) {		d_printf("smbcli_full_connection failed: %s\n",			 nt_errstr(status));		goto done;	}	ret = true;	ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,			DCERPC_AUTH_LEVEL_INTEGRITY);	ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,			DCERPC_AUTH_LEVEL_PRIVACY);	ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,			DCERPC_AUTH_LEVEL_INTEGRITY);	ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,			DCERPC_AUTH_LEVEL_PRIVACY); done:	talloc_free(mem_ctx);	return ret;}/* * Lookup or create a user and return all necessary info */static NTSTATUS get_usr_handle(struct smbcli_state *cli,			       TALLOC_CTX *mem_ctx,			       struct loadparm_context *lp_ctx,			       struct cli_credentials *admin_creds,			       uint8_t auth_type,			       uint8_t auth_level,			       const char *username,			       char **domain,			       struct dcerpc_pipe **result_pipe,			       struct policy_handle **result_handle,			       struct dom_sid **sid){	struct dcerpc_pipe *samr_pipe;	NTSTATUS status;	struct policy_handle conn_handle;	struct policy_handle domain_handle;	struct policy_handle *user_handle;	struct samr_Connect2 conn;	struct samr_EnumDomains enumdom;	uint32_t resume_handle = 0;	struct samr_LookupDomain l;	int dom_idx;	struct lsa_String domain_name;	struct lsa_String user_name;	struct samr_OpenDomain o;	struct samr_CreateUser2 c;	uint32_t user_rid,access_granted;	samr_pipe = dcerpc_pipe_init(mem_ctx,				     cli->transport->socket->event.ctx,				     lp_iconv_convenience(lp_ctx));	if (samr_pipe == NULL) {		d_printf("dcerpc_pipe_init failed\n");		status = NT_STATUS_NO_MEMORY;		goto fail;	}	status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");	if (!NT_STATUS_IS_OK(status)) {		d_printf("dcerpc_pipe_open_smb failed: %s\n",			 nt_errstr(status));		goto fail;	}	if (admin_creds != NULL) {		status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,					  admin_creds, lp_ctx, auth_type, auth_level,					  NULL);		if (!NT_STATUS_IS_OK(status)) {			d_printf("dcerpc_bind_auth failed: %s\n",				 nt_errstr(status));			goto fail;		}	} else {		/* We must have an authenticated SMB connection */		status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);		if (!NT_STATUS_IS_OK(status)) {			d_printf("dcerpc_bind_auth_none failed: %s\n",				 nt_errstr(status));			goto fail;		}	}	conn.in.system_name = talloc_asprintf(		mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));	conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;	conn.out.connect_handle = &conn_handle;	status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);	if (!NT_STATUS_IS_OK(status)) {		d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));		goto fail;	}	enumdom.in.connect_handle = &conn_handle;	enumdom.in.resume_handle = &resume_handle;	enumdom.in.buf_size = (uint32_t)-1;	enumdom.out.resume_handle = &resume_handle;	status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);	if (!NT_STATUS_IS_OK(status)) {		d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));		goto fail;	}	if (enumdom.out.num_entries != 2) {		d_printf("samr_EnumDomains returned %d entries, expected 2\n",			 enumdom.out.num_entries);		status = NT_STATUS_UNSUCCESSFUL;		goto fail;	}	dom_idx = strequal(enumdom.out.sam->entries[0].name.string,			   "builtin") ? 1:0;	l.in.connect_handle = &conn_handle;	domain_name.string = enumdom.out.sam->entries[dom_idx].name.string;	*domain = talloc_strdup(mem_ctx, domain_name.string);	l.in.domain_name = &domain_name;	status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);	if (!NT_STATUS_IS_OK(status)) {		d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));		goto fail;	}	o.in.connect_handle = &conn_handle;	o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;	o.in.sid = l.out.sid;	o.out.domain_handle = &domain_handle;	status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);	if (!NT_STATUS_IS_OK(status)) {		d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));		goto fail;	}	c.in.domain_handle = &domain_handle;	user_name.string = username;	c.in.account_name = &user_name;	c.in.acct_flags = ACB_NORMAL;	c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;	user_handle = talloc(mem_ctx, struct policy_handle);	c.out.user_handle = user_handle;	c.out.access_granted = &access_granted;	c.out.rid = &user_rid;	status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);	if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {		struct samr_LookupNames ln;		struct samr_OpenUser ou;		ln.in.domain_handle = &domain_handle;		ln.in.num_names = 1;		ln.in.names = &user_name;		status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);		if (!NT_STATUS_IS_OK(status)) {			d_printf("samr_LookupNames failed: %s\n",				 nt_errstr(status));			goto fail;		}		ou.in.domain_handle = &domain_handle;		ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;		user_rid = ou.in.rid = ln.out.rids.ids[0];		ou.out.user_handle = user_handle;		status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);		if (!NT_STATUS_IS_OK(status)) {			d_printf("samr_OpenUser failed: %s\n",				 nt_errstr(status));			goto fail;		}	}	if (!NT_STATUS_IS_OK(status)) {		d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));		goto fail;	}	*result_pipe = samr_pipe;	*result_handle = user_handle;	if (sid != NULL) {		*sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);	}	return NT_STATUS_OK;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?