⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delete.c

📁 samba最新软件
💻 C
📖 第 1 页 / 共 4 页
字号:
/*    Unix SMB/CIFS implementation.   delete on close testing   Copyright (C) Andrew Tridgell 2003      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/libcli.h"#include "torture/torture.h"#include "torture/util.h"#include "system/filesys.h"#include "libcli/raw/libcliraw.h"#include "libcli/raw/raw_proto.h"#include "torture/raw/proto.h"static bool check_delete_on_close(struct torture_context *tctx, 								  struct smbcli_state *cli, int fnum,								  const char *fname, bool expect_it, 								  const char *where){	union smb_search_data data;	NTSTATUS status;	time_t c_time, a_time, m_time;	size_t size;	uint16_t mode;	status = torture_single_search(cli, tctx,				       fname,				       RAW_SEARCH_TRANS2,				       RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,				       FILE_ATTRIBUTE_DIRECTORY,				       &data);	torture_assert_ntstatus_ok(tctx, status, 		talloc_asprintf(tctx, "single_search failed (%s)", where));	if (fnum != -1) {		union smb_fileinfo io;		int nlink = expect_it ? 0 : 1;		io.all_info.level = RAW_FILEINFO_ALL_INFO;		io.all_info.in.file.fnum = fnum;		status = smb_raw_fileinfo(cli->tree, tctx, &io);		torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, 					"qfileinfo failed (%s)", where));		torture_assert(tctx, expect_it == io.all_info.out.delete_pending, 			talloc_asprintf(tctx, 			"%s - Expected del_on_close flag %d, qfileinfo/all_info gave %d",			       where, expect_it, io.all_info.out.delete_pending));		torture_assert(tctx, nlink == io.all_info.out.nlink, 			talloc_asprintf(tctx, 				"%s - Expected nlink %d, qfileinfo/all_info gave %d",			       where, nlink, io.all_info.out.nlink));		io.standard_info.level = RAW_FILEINFO_STANDARD_INFO;		io.standard_info.in.file.fnum = fnum;		status = smb_raw_fileinfo(cli->tree, tctx, &io);		torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "qpathinfo failed (%s)", where));		torture_assert(tctx, expect_it == io.standard_info.out.delete_pending,			talloc_asprintf(tctx, "%s - Expected del_on_close flag %d, qfileinfo/standard_info gave %d\n",			       where, expect_it, io.standard_info.out.delete_pending));		torture_assert(tctx, nlink == io.standard_info.out.nlink,			talloc_asprintf(tctx, "%s - Expected nlink %d, qfileinfo/standard_info gave %d",			       where, nlink, io.all_info.out.nlink));	}	status = smbcli_qpathinfo(cli->tree, fname,				  &c_time, &a_time, &m_time,				  &size, &mode);	if (expect_it) {		torture_assert_ntstatus_equal(tctx, status, NT_STATUS_DELETE_PENDING,			"qpathinfo did not give correct error code");	} else {		torture_assert_ntstatus_ok(tctx, status, 			talloc_asprintf(tctx, "qpathinfo failed (%s)", where));	}	return true;}#define CHECK_STATUS(_cli, _expected) \	torture_assert_ntstatus_equal(tctx, _cli->tree->session->transport->error.e.nt_status, _expected, \		 "Incorrect status")static const char *fname = "\\delete.file";static const char *fname_new = "\\delete.new";static const char *dname = "\\delete.dir";static void del_clean_area(struct smbcli_state *cli1, struct smbcli_state *cli2){	smb_raw_exit(cli1->session);	smb_raw_exit(cli2->session);	smbcli_deltree(cli1->tree, dname);	smbcli_setatr(cli1->tree, fname, 0, 0);	smbcli_unlink(cli1->tree, fname);	smbcli_setatr(cli1->tree, fname_new, 0, 0);	smbcli_unlink(cli1->tree, fname_new);}/* Test 1 - this should delete the file on close. */static bool deltest1(struct torture_context *tctx, struct smbcli_state *cli1, struct smbcli_state *cli2){	int fnum1 = -1;	del_clean_area(cli1, cli2);	fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_RIGHTS_FILE_ALL,				      FILE_ATTRIBUTE_NORMAL,				      NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 				      NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);		torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "open of %s failed (%s)", 		       fname, smbcli_errstr(cli1->tree)));		torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1), 		talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(cli1->tree)));	fnum1 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_NONE);	torture_assert(tctx, fnum1 == -1, talloc_asprintf(tctx, "open of %s succeeded (should fail)", 		       fname));	return true;}/* Test 2 - this should delete the file on close. */static bool deltest2(struct torture_context *tctx, struct smbcli_state *cli1, struct smbcli_state *cli2){	int fnum1 = -1;	del_clean_area(cli1, cli2);	fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_RIGHTS_FILE_ALL,				      FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_NONE, 				      NTCREATEX_DISP_OVERWRITE_IF, 0, 0);		torture_assert(tctx, fnum1 != -1, 		talloc_asprintf(tctx, "open of %s failed (%s)", 		       fname, smbcli_errstr(cli1->tree)));		torture_assert_ntstatus_ok(tctx, smbcli_nt_delete_on_close(cli1->tree, fnum1, true), 		talloc_asprintf(tctx, "setting delete_on_close failed (%s)", 		       smbcli_errstr(cli1->tree)));		torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1), 		talloc_asprintf(tctx, "close failed (%s)", 		       smbcli_errstr(cli1->tree)));		fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);	if (fnum1 != -1) {		printf("(%s) open of %s succeeded should have been deleted on close !\n", 		       __location__, fname);		if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {			printf("(%s) close failed (%s)\n", 			       __location__, smbcli_errstr(cli1->tree));			return false;		}		smbcli_unlink(cli1->tree, fname);	}	return true;}/* Test 3 - ... */static bool deltest3(struct torture_context *tctx, struct smbcli_state *cli1, struct smbcli_state *cli2){	int fnum1 = -1;	int fnum2 = -1;	del_clean_area(cli1, cli2);	fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_RIGHTS_FILE_ALL,				      FILE_ATTRIBUTE_NORMAL,				      NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 				      NTCREATEX_DISP_OVERWRITE_IF, 0, 0);	torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "open - 1 of %s failed (%s)", 		        fname, smbcli_errstr(cli1->tree)));	/* This should fail with a sharing violation - open for delete is only compatible	   with SHARE_DELETE. */	fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_RIGHTS_FILE_READ, 				      FILE_ATTRIBUTE_NORMAL,				      NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 				      NTCREATEX_DISP_OPEN, 0, 0);	torture_assert(tctx, fnum2 == -1, 		talloc_asprintf(tctx, "open  - 2 of %s succeeded - should have failed.", 		       fname));	/* This should succeed. */	fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_RIGHTS_FILE_READ, 				      FILE_ATTRIBUTE_NORMAL,				      NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, 				      NTCREATEX_DISP_OPEN, 0, 0);	torture_assert(tctx, fnum2 != -1, talloc_asprintf(tctx, "open  - 2 of %s failed (%s)", 		       fname, smbcli_errstr(cli1->tree)));	torture_assert_ntstatus_ok(tctx, 							   smbcli_nt_delete_on_close(cli1->tree, fnum1, true),							   talloc_asprintf(tctx, "setting delete_on_close failed (%s)", 		       				   smbcli_errstr(cli1->tree)));		torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1),		talloc_asprintf(tctx, "close 1 failed (%s)", 		       smbcli_errstr(cli1->tree)));		torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum2),		talloc_asprintf(tctx, "close 2 failed (%s)", 		       smbcli_errstr(cli1->tree)));		/* This should fail - file should no longer be there. */	fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);	if (fnum1 != -1) {		printf("(%s) open of %s succeeded should have been deleted on close !\n", 		       __location__, fname);		if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {			printf("(%s) close failed (%s)\n", 			       __location__, smbcli_errstr(cli1->tree));		}		smbcli_unlink(cli1->tree, fname);		return false;	}	return true;}/* Test 4 ... */static bool deltest4(struct torture_context *tctx, struct smbcli_state *cli1, struct smbcli_state *cli2){	int fnum1 = -1;	int fnum2 = -1;	bool correct = true;	del_clean_area(cli1, cli2);	fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_FILE_READ_DATA  | 				      SEC_FILE_WRITE_DATA |				      SEC_STD_DELETE,				      FILE_ATTRIBUTE_NORMAL, 				      NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 				      NTCREATEX_DISP_OVERWRITE_IF, 0, 0);									torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "open of %s failed (%s)", 		       fname, smbcli_errstr(cli1->tree)));	/* This should succeed. */	fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_RIGHTS_FILE_READ,				      FILE_ATTRIBUTE_NORMAL, 				      NTCREATEX_SHARE_ACCESS_READ  | 				      NTCREATEX_SHARE_ACCESS_WRITE |				      NTCREATEX_SHARE_ACCESS_DELETE, 				      NTCREATEX_DISP_OPEN, 0, 0);	torture_assert(tctx, fnum2 != -1, talloc_asprintf(tctx, "open  - 2 of %s failed (%s)", 		       fname, smbcli_errstr(cli1->tree)));		torture_assert_ntstatus_ok(tctx, 					smbcli_close(cli1->tree, fnum2),		 			talloc_asprintf(tctx, "close - 1 failed (%s)", 					smbcli_errstr(cli1->tree)));		torture_assert_ntstatus_ok(tctx, 				smbcli_nt_delete_on_close(cli1->tree, fnum1, true), 		 		talloc_asprintf(tctx, "setting delete_on_close failed (%s)", 		        smbcli_errstr(cli1->tree)));	/* This should fail - no more opens once delete on close set. */	fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_RIGHTS_FILE_READ,				      FILE_ATTRIBUTE_NORMAL, 				      NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,				      NTCREATEX_DISP_OPEN, 0, 0);	torture_assert(tctx, fnum2 == -1, 		 talloc_asprintf(tctx, "open  - 3 of %s succeeded ! Should have failed.",		       fname ));	CHECK_STATUS(cli1, NT_STATUS_DELETE_PENDING);	torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1), 		 talloc_asprintf(tctx, "close - 2 failed (%s)", 		       smbcli_errstr(cli1->tree)));		return correct;}/* Test 5 ... */static bool deltest5(struct torture_context *tctx, struct smbcli_state *cli1, struct smbcli_state *cli2){	int fnum1 = -1;	del_clean_area(cli1, cli2);	fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);	torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "open of %s failed (%s)", 		       fname, smbcli_errstr(cli1->tree)));		/* This should fail - only allowed on NT opens with DELETE access. */	torture_assert(tctx, !NT_STATUS_IS_OK(smbcli_nt_delete_on_close(cli1->tree, fnum1, true)),		 "setting delete_on_close on OpenX file succeeded - should fail !");	torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1),		talloc_asprintf(tctx, "close - 2 failed (%s)", smbcli_errstr(cli1->tree)));	return true;}/* Test 6 ... */static bool deltest6(struct torture_context *tctx, struct smbcli_state *cli1, struct smbcli_state *cli2){	int fnum1 = -1;	del_clean_area(cli1, cli2);	fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 				   SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA,				   FILE_ATTRIBUTE_NORMAL, 				   NTCREATEX_SHARE_ACCESS_READ  |				   NTCREATEX_SHARE_ACCESS_WRITE |				   NTCREATEX_SHARE_ACCESS_DELETE,				   NTCREATEX_DISP_OVERWRITE_IF, 0, 0);		torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "open of %s failed (%s)", 		       fname, smbcli_errstr(cli1->tree)));		/* This should fail - only allowed on NT opens with DELETE access. */		torture_assert(tctx, 		!NT_STATUS_IS_OK(smbcli_nt_delete_on_close(cli1->tree, fnum1, true)),		"setting delete_on_close on file with no delete access succeeded - should fail !");	torture_assert_ntstatus_ok(tctx, 							   smbcli_close(cli1->tree, fnum1),		talloc_asprintf(tctx, "close - 2 failed (%s)", 		       smbcli_errstr(cli1->tree)));	return true;}/* Test 7 ... */static bool deltest7(struct torture_context *tctx, struct smbcli_state *cli1, struct smbcli_state *cli2){	int fnum1 = -1;	bool correct = true;	del_clean_area(cli1, cli2);	fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 				      SEC_FILE_READ_DATA  | 				      SEC_FILE_WRITE_DATA |				      SEC_STD_DELETE,				      FILE_ATTRIBUTE_NORMAL, 0, 				      NTCREATEX_DISP_OVERWRITE_IF, 0, 0);								

⌨️ 快捷键说明

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