test: Intercept open in vfs_error_inject
[samba.git] / source3 / modules / vfs_error_inject.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Samba VFS module for error injection in VFS calls
4  *  Copyright (C) Christof Schmitt 2017
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
25
26 struct unix_error_map {
27         const char *err_str;
28         int error;
29 } unix_error_map_array[] = {
30         {       "ESTALE",       ESTALE  },
31         {       "EBADF",        EBADF   },
32         {       "EINTR",        EINTR   },
33 };
34
35 static int find_unix_error_from_string(const char *err_str)
36 {
37         int i;
38
39         for (i = 0; i < ARRAY_SIZE(unix_error_map_array); i++) {
40                 struct unix_error_map *m = &unix_error_map_array[i];
41
42                 if (strequal(err_str, m->err_str)) {
43                         return m->error;
44                 }
45         }
46
47         return 0;
48 }
49
50 static int inject_unix_error(const char *vfs_func, vfs_handle_struct *handle)
51 {
52         const char *err_str;
53
54         err_str = lp_parm_const_string(SNUM(handle->conn),
55                                        "error_inject", vfs_func, NULL);
56
57         if (err_str != NULL) {
58                 int error;
59
60                 error = find_unix_error_from_string(err_str);
61                 if (error != 0) {
62                         DBG_WARNING("Returning error %s for VFS function %s\n",
63                                     err_str, vfs_func);
64                         return error;
65                 }
66
67                 if (strequal(err_str, "panic")) {
68                         DBG_ERR("Panic in VFS function %s\n", vfs_func);
69                         smb_panic("error_inject");
70                 }
71
72                 DBG_ERR("Unknown error inject %s requested "
73                         "for vfs function %s\n", err_str, vfs_func);
74         }
75
76         return 0;
77 }
78
79 static int vfs_error_inject_chdir(vfs_handle_struct *handle,
80                                   const struct smb_filename *smb_fname)
81 {
82         int error;
83
84         error = inject_unix_error("chdir", handle);
85         if (error != 0) {
86                 errno = error;
87                 return -1;
88         }
89
90         return SMB_VFS_NEXT_CHDIR(handle, smb_fname);
91 }
92
93 static ssize_t vfs_error_inject_pwrite(vfs_handle_struct *handle,
94                                        files_struct *fsp,
95                                        const void *data,
96                                        size_t n,
97                                        off_t offset)
98 {
99         int error;
100
101         error = inject_unix_error("pwrite", handle);
102         if (error != 0) {
103                 errno = error;
104                 return -1;
105         }
106
107         return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
108 }
109
110 static int vfs_error_inject_open(
111         struct vfs_handle_struct *handle,
112         struct smb_filename *smb_fname,
113         files_struct *fsp,
114         int flags,
115         mode_t mode)
116 {
117         int error = inject_unix_error("open", handle);
118         if (error != 0) {
119                 errno = error;
120                 return -1;
121         }
122         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
123 }
124
125 static struct vfs_fn_pointers vfs_error_inject_fns = {
126         .chdir_fn = vfs_error_inject_chdir,
127         .pwrite_fn = vfs_error_inject_pwrite,
128         .open_fn = vfs_error_inject_open,
129 };
130
131 static_decl_vfs;
132 NTSTATUS vfs_error_inject_init(TALLOC_CTX *ctx)
133 {
134         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "error_inject",
135                                 &vfs_error_inject_fns);
136 }