summaryrefslogtreecommitdiff
path: root/source3/modules/vfs_error_inject.c
blob: c7a99370b2a5ce3ccb08fe99283551fa75645034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 *  Unix SMB/CIFS implementation.
 *  Samba VFS module for error injection in VFS calls
 *  Copyright (C) Christof Schmitt 2017
 *
 *  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 "smbd/smbd.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_VFS

struct unix_error_map {
	const char *err_str;
	int error;
} unix_error_map_array[] = {
	{	"ESTALE",	ESTALE	},
	{	"EBADF",	EBADF	},
	{	"EINTR",	EINTR	},
};

static int find_unix_error_from_string(const char *err_str)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(unix_error_map_array); i++) {
		struct unix_error_map *m = &unix_error_map_array[i];

		if (strequal(err_str, m->err_str)) {
			return m->error;
		}
	}

	return 0;
}

static int inject_unix_error(const char *vfs_func, vfs_handle_struct *handle)
{
	const char *err_str;

	err_str = lp_parm_const_string(SNUM(handle->conn),
				       "error_inject", vfs_func, NULL);

	if (err_str != NULL) {
		int error;

		error = find_unix_error_from_string(err_str);
		if (error != 0) {
			DBG_WARNING("Returning error %s for VFS function %s\n",
				    err_str, vfs_func);
			return error;
		}

		if (strequal(err_str, "panic")) {
			DBG_ERR("Panic in VFS function %s\n", vfs_func);
			smb_panic("error_inject");
		}

		DBG_ERR("Unknown error inject %s requested "
			"for vfs function %s\n", err_str, vfs_func);
	}

	return 0;
}

static int vfs_error_inject_chdir(vfs_handle_struct *handle,
				  const struct smb_filename *smb_fname)
{
	int error;

	error = inject_unix_error("chdir", handle);
	if (error != 0) {
		errno = error;
		return -1;
	}

	return SMB_VFS_NEXT_CHDIR(handle, smb_fname);
}

static ssize_t vfs_error_inject_pwrite(vfs_handle_struct *handle,
				       files_struct *fsp,
				       const void *data,
				       size_t n,
				       off_t offset)
{
	int error;

	error = inject_unix_error("pwrite", handle);
	if (error != 0) {
		errno = error;
		return -1;
	}

	return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
}

static int vfs_error_inject_open(
	struct vfs_handle_struct *handle,
	struct smb_filename *smb_fname,
	files_struct *fsp,
	int flags,
	mode_t mode)
{
	int error = inject_unix_error("open", handle);
	if (error != 0) {
		errno = error;
		return -1;
	}
	return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
}

static struct vfs_fn_pointers vfs_error_inject_fns = {
	.chdir_fn = vfs_error_inject_chdir,
	.pwrite_fn = vfs_error_inject_pwrite,
	.open_fn = vfs_error_inject_open,
};

static_decl_vfs;
NTSTATUS vfs_error_inject_init(TALLOC_CTX *ctx)
{
	return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "error_inject",
				&vfs_error_inject_fns);
}