2 * Unix SMB/CIFS implementation.
3 * Samba VFS module for error injection in VFS calls
4 * Copyright (C) Christof Schmitt 2017
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "smbd/smbd.h"
24 #define DBGC_CLASS DBGC_VFS
26 struct unix_error_map
{
29 } unix_error_map_array
[] = {
37 static int find_unix_error_from_string(const char *err_str
)
41 for (i
= 0; i
< ARRAY_SIZE(unix_error_map_array
); i
++) {
42 struct unix_error_map
*m
= &unix_error_map_array
[i
];
44 if (strequal(err_str
, m
->err_str
)) {
52 static int inject_unix_error(const char *vfs_func
, vfs_handle_struct
*handle
)
57 err_str
= lp_parm_const_string(SNUM(handle
->conn
),
58 "error_inject", vfs_func
, NULL
);
59 if (err_str
== NULL
) {
63 error
= find_unix_error_from_string(err_str
);
65 DBG_WARNING("Returning error %s for VFS function %s\n",
70 if (strequal(err_str
, "panic")) {
71 DBG_ERR("Panic in VFS function %s\n", vfs_func
);
72 smb_panic("error_inject");
75 DBG_ERR("Unknown error inject %s requested "
76 "for vfs function %s\n", err_str
, vfs_func
);
81 static int vfs_error_inject_chdir(vfs_handle_struct
*handle
,
82 const struct smb_filename
*smb_fname
)
86 error
= inject_unix_error("chdir", handle
);
92 return SMB_VFS_NEXT_CHDIR(handle
, smb_fname
);
95 static ssize_t
vfs_error_inject_pwrite(vfs_handle_struct
*handle
,
103 error
= inject_unix_error("pwrite", handle
);
109 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
112 static int vfs_error_inject_openat(struct vfs_handle_struct
*handle
,
113 const struct files_struct
*dirfsp
,
114 const struct smb_filename
*smb_fname
,
116 const struct vfs_open_how
*how
)
118 int error
= inject_unix_error("openat", handle
);
119 int create_error
= inject_unix_error("openat_create", handle
);
120 int dirfsp_flags
= (O_NOFOLLOW
|O_DIRECTORY
);
124 dirfsp_flags
|= O_PATH
;
127 dirfsp_flags
|= O_SEARCH
;
131 if ((create_error
!= 0) && (how
->flags
& O_CREAT
)) {
132 struct stat_ex st
= {
137 ret
= SMB_VFS_FSTATAT(handle
->conn
,
141 AT_SYMLINK_NOFOLLOW
);
143 if ((ret
== -1) && (errno
== ENOENT
)) {
144 errno
= create_error
;
149 return_error
= (error
!= 0);
150 return_error
&= !fsp
->fsp_flags
.is_pathref
;
151 return_error
&= ((how
->flags
& dirfsp_flags
) != dirfsp_flags
);
157 return SMB_VFS_NEXT_OPENAT(handle
, dirfsp
, smb_fname
, fsp
, how
);
160 static int vfs_error_inject_unlinkat(struct vfs_handle_struct
*handle
,
161 struct files_struct
*dirfsp
,
162 const struct smb_filename
*smb_fname
,
165 struct smb_filename
*full_fname
= NULL
;
166 struct smb_filename
*parent_fname
= NULL
;
167 int error
= inject_unix_error("unlinkat", handle
);
172 return SMB_VFS_NEXT_UNLINKAT(handle
, dirfsp
, smb_fname
, flags
);
175 full_fname
= full_path_from_dirfsp_atname(talloc_tos(),
178 if (full_fname
== NULL
) {
182 status
= SMB_VFS_PARENT_PATHNAME(handle
->conn
,
183 full_fname
, /* TALLOC_CTX. */
187 if (!NT_STATUS_IS_OK(status
)) {
188 TALLOC_FREE(full_fname
);
189 errno
= map_errno_from_nt_status(status
);
193 ret
= SMB_VFS_STAT(handle
->conn
, parent_fname
);
195 TALLOC_FREE(full_fname
);
199 if (parent_fname
->st
.st_ex_uid
== get_current_uid(dirfsp
->conn
)) {
200 return SMB_VFS_NEXT_UNLINKAT(handle
, dirfsp
, smb_fname
, flags
);
207 static struct vfs_fn_pointers vfs_error_inject_fns
= {
208 .chdir_fn
= vfs_error_inject_chdir
,
209 .pwrite_fn
= vfs_error_inject_pwrite
,
210 .openat_fn
= vfs_error_inject_openat
,
211 .unlinkat_fn
= vfs_error_inject_unlinkat
,
215 NTSTATUS
vfs_error_inject_init(TALLOC_CTX
*ctx
)
217 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "error_inject",
218 &vfs_error_inject_fns
);