2 * Auditing VFS module for samba. Log selected file operations to syslog
5 * Copyright (C) Tim Potter, 1999-2000
6 * Copyright (C) Alexander Bokovoy, 2002
7 * Copyright (C) John H Terpstra, 2003
8 * Copyright (C) Stefan (metze) Metzmacher, 2003
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "system/filesys.h"
27 #include "system/syslog.h"
28 #include "smbd/smbd.h"
29 #include "lib/param/loadparm.h"
31 static int vfs_extd_audit_debug_level = DBGC_VFS;
34 #define DBGC_CLASS vfs_extd_audit_debug_level
36 static int audit_syslog_facility(vfs_handle_struct *handle)
38 static const struct enum_list enum_log_facilities[] = {
40 { LOG_LOCAL0, "LOCAL0" },
41 { LOG_LOCAL1, "LOCAL1" },
42 { LOG_LOCAL2, "LOCAL2" },
43 { LOG_LOCAL3, "LOCAL3" },
44 { LOG_LOCAL4, "LOCAL4" },
45 { LOG_LOCAL5, "LOCAL5" },
46 { LOG_LOCAL6, "LOCAL6" },
47 { LOG_LOCAL7, "LOCAL7" },
53 facility = lp_parm_enum(SNUM(handle->conn), "extd_audit", "facility", enum_log_facilities, LOG_USER);
59 static int audit_syslog_priority(vfs_handle_struct *handle)
61 static const struct enum_list enum_log_priorities[] = {
62 { LOG_EMERG, "EMERG" },
63 { LOG_ALERT, "ALERT" },
66 { LOG_WARNING, "WARNING" },
67 { LOG_NOTICE, "NOTICE" },
69 { LOG_DEBUG, "DEBUG" },
75 priority = lp_parm_enum(SNUM(handle->conn), "extd_audit", "priority",
76 enum_log_priorities, LOG_NOTICE);
78 priority = LOG_WARNING;
84 /* Implementation of vfs_ops. Pass everything on to the default
85 operation but log event first. */
87 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
89 int result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
95 openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
97 if (lp_syslog() > 0) {
98 syslog(audit_syslog_priority(handle),
99 "connect to service %s by user %s\n",
102 DEBUG(10, ("Connected to service %s as user %s\n",
108 static void audit_disconnect(vfs_handle_struct *handle)
110 if (lp_syslog() > 0) {
111 syslog(audit_syslog_priority(handle), "disconnected\n");
113 DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
114 SMB_VFS_NEXT_DISCONNECT(handle);
119 static DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32_t attr)
123 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
125 if (lp_syslog() > 0) {
126 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
128 (result == NULL) ? "failed: " : "",
129 (result == NULL) ? strerror(errno) : "");
131 DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
133 (result == NULL) ? "failed: " : "",
134 (result == NULL) ? strerror(errno) : ""));
139 static int audit_mkdir(vfs_handle_struct *handle,
140 const struct smb_filename *smb_fname,
145 result = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
147 if (lp_syslog() > 0) {
148 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
149 smb_fname->base_name,
150 (result < 0) ? "failed: " : "",
151 (result < 0) ? strerror(errno) : "");
153 DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
154 smb_fname->base_name,
155 (result < 0) ? "failed: " : "",
156 (result < 0) ? strerror(errno) : ""));
161 static int audit_rmdir(vfs_handle_struct *handle,
162 const struct smb_filename *smb_fname)
166 result = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
168 if (lp_syslog() > 0) {
169 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
170 smb_fname->base_name,
171 (result < 0) ? "failed: " : "",
172 (result < 0) ? strerror(errno) : "");
174 DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
175 smb_fname->base_name,
176 (result < 0) ? "failed: " : "",
177 (result < 0) ? strerror(errno) : ""));
182 static int audit_open(vfs_handle_struct *handle,
183 struct smb_filename *smb_fname, files_struct *fsp,
184 int flags, mode_t mode)
188 result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
190 if (lp_syslog() > 0) {
191 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
192 smb_fname->base_name, result,
193 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
194 (result < 0) ? "failed: " : "",
195 (result < 0) ? strerror(errno) : "");
197 DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
198 smb_fname_str_dbg(smb_fname),
199 (result < 0) ? "failed: " : "",
200 (result < 0) ? strerror(errno) : ""));
205 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
209 result = SMB_VFS_NEXT_CLOSE(handle, fsp);
211 if (lp_syslog() > 0) {
212 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
214 (result < 0) ? "failed: " : "",
215 (result < 0) ? strerror(errno) : "");
217 DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
219 (result < 0) ? "failed: " : "",
220 (result < 0) ? strerror(errno) : ""));
225 static int audit_rename(vfs_handle_struct *handle,
226 const struct smb_filename *smb_fname_src,
227 const struct smb_filename *smb_fname_dst)
231 result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
233 if (lp_syslog() > 0) {
234 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
235 smb_fname_src->base_name,
236 smb_fname_dst->base_name,
237 (result < 0) ? "failed: " : "",
238 (result < 0) ? strerror(errno) : "");
240 DEBUG(1, ("vfs_extd_audit: rename old: %s newname: %s %s %s\n",
241 smb_fname_str_dbg(smb_fname_src),
242 smb_fname_str_dbg(smb_fname_dst),
243 (result < 0) ? "failed: " : "",
244 (result < 0) ? strerror(errno) : ""));
249 static int audit_unlink(vfs_handle_struct *handle,
250 const struct smb_filename *smb_fname)
254 result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
256 if (lp_syslog() > 0) {
257 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
258 smb_fname->base_name,
259 (result < 0) ? "failed: " : "",
260 (result < 0) ? strerror(errno) : "");
262 DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
263 smb_fname_str_dbg(smb_fname),
264 (result < 0) ? "failed: " : "",
265 (result < 0) ? strerror(errno) : ""));
270 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
274 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
276 if (lp_syslog() > 0) {
277 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
279 (result < 0) ? "failed: " : "",
280 (result < 0) ? strerror(errno) : "");
282 DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
283 path, (unsigned int)mode,
284 (result < 0) ? "failed: " : "",
285 (result < 0) ? strerror(errno) : ""));
290 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
294 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
296 if (lp_syslog() > 0) {
297 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
299 (result < 0) ? "failed: " : "",
300 (result < 0) ? strerror(errno) : "");
302 DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
303 path, (unsigned int)mode,
304 (result < 0) ? "failed: " : "",
305 (result < 0) ? strerror(errno) : ""));
310 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
314 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
316 if (lp_syslog() > 0) {
317 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
318 fsp->fsp_name->base_name, mode,
319 (result < 0) ? "failed: " : "",
320 (result < 0) ? strerror(errno) : "");
322 DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
323 fsp_str_dbg(fsp), (unsigned int)mode,
324 (result < 0) ? "failed: " : "",
325 (result < 0) ? strerror(errno) : ""));
330 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
334 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
336 if (lp_syslog() > 0) {
337 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
338 fsp->fsp_name->base_name, mode,
339 (result < 0) ? "failed: " : "",
340 (result < 0) ? strerror(errno) : "");
342 DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
343 fsp_str_dbg(fsp), (unsigned int)mode,
344 (result < 0) ? "failed: " : "",
345 (result < 0) ? strerror(errno) : ""));
350 static struct vfs_fn_pointers vfs_extd_audit_fns = {
351 .connect_fn = audit_connect,
352 .disconnect_fn = audit_disconnect,
353 .opendir_fn = audit_opendir,
354 .mkdir_fn = audit_mkdir,
355 .rmdir_fn = audit_rmdir,
356 .open_fn = audit_open,
357 .close_fn = audit_close,
358 .rename_fn = audit_rename,
359 .unlink_fn = audit_unlink,
360 .chmod_fn = audit_chmod,
361 .fchmod_fn = audit_fchmod,
362 .chmod_acl_fn = audit_chmod_acl,
363 .fchmod_acl_fn = audit_fchmod_acl,
367 NTSTATUS vfs_extd_audit_init(void)
369 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
370 "extd_audit", &vfs_extd_audit_fns);
372 if (!NT_STATUS_IS_OK(ret))
375 vfs_extd_audit_debug_level = debug_add_class("extd_audit");
376 if (vfs_extd_audit_debug_level == -1) {
377 vfs_extd_audit_debug_level = DBGC_VFS;
378 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
380 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level));