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) Stefan (metze) Metzmacher 2002
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "system/syslog.h"
28 #define DBGC_CLASS DBGC_VFS
30 static int audit_syslog_facility(vfs_handle_struct *handle)
32 static const struct enum_list enum_log_facilities[] = {
34 { LOG_LOCAL0, "LOCAL0" },
35 { LOG_LOCAL1, "LOCAL1" },
36 { LOG_LOCAL2, "LOCAL2" },
37 { LOG_LOCAL3, "LOCAL3" },
38 { LOG_LOCAL4, "LOCAL4" },
39 { LOG_LOCAL5, "LOCAL5" },
40 { LOG_LOCAL6, "LOCAL6" },
41 { LOG_LOCAL7, "LOCAL7" }
46 facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
52 static int audit_syslog_priority(vfs_handle_struct *handle)
54 static const struct enum_list enum_log_priorities[] = {
55 { LOG_EMERG, "EMERG" },
56 { LOG_ALERT, "ALERT" },
59 { LOG_WARNING, "WARNING" },
60 { LOG_NOTICE, "NOTICE" },
62 { LOG_DEBUG, "DEBUG" }
67 priority = lp_parm_enum(SNUM(handle->conn), "audit", "priority",
68 enum_log_priorities, LOG_NOTICE);
70 priority = LOG_WARNING;
76 /* Implementation of vfs_ops. Pass everything on to the default
77 operation but log event first. */
79 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
83 result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
88 openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
90 syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n",
96 static void audit_disconnect(vfs_handle_struct *handle)
98 syslog(audit_syslog_priority(handle), "disconnected\n");
99 SMB_VFS_NEXT_DISCONNECT(handle);
104 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
106 SMB_STRUCT_DIR *result;
108 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
110 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
112 (result == NULL) ? "failed: " : "",
113 (result == NULL) ? strerror(errno) : "");
118 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
122 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
124 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
126 (result < 0) ? "failed: " : "",
127 (result < 0) ? strerror(errno) : "");
132 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
136 result = SMB_VFS_NEXT_RMDIR(handle, path);
138 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
140 (result < 0) ? "failed: " : "",
141 (result < 0) ? strerror(errno) : "");
146 static int audit_open(vfs_handle_struct *handle,
147 struct smb_filename *smb_fname, files_struct *fsp,
148 int flags, mode_t mode)
152 result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
154 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
155 smb_fname->base_name, result,
156 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
157 (result < 0) ? "failed: " : "",
158 (result < 0) ? strerror(errno) : "");
163 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
167 result = SMB_VFS_NEXT_CLOSE(handle, fsp);
169 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
171 (result < 0) ? "failed: " : "",
172 (result < 0) ? strerror(errno) : "");
177 static int audit_rename(vfs_handle_struct *handle,
178 const struct smb_filename *smb_fname_src,
179 const struct smb_filename *smb_fname_dst)
183 result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
185 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
186 smb_fname_src->base_name,
187 smb_fname_dst->base_name,
188 (result < 0) ? "failed: " : "",
189 (result < 0) ? strerror(errno) : "");
194 static int audit_unlink(vfs_handle_struct *handle,
195 const struct smb_filename *smb_fname)
199 result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
201 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
202 smb_fname->base_name,
203 (result < 0) ? "failed: " : "",
204 (result < 0) ? strerror(errno) : "");
209 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
213 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
215 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
217 (result < 0) ? "failed: " : "",
218 (result < 0) ? strerror(errno) : "");
223 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
227 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
229 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
231 (result < 0) ? "failed: " : "",
232 (result < 0) ? strerror(errno) : "");
237 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
241 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
243 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
244 fsp->fsp_name->base_name, mode,
245 (result < 0) ? "failed: " : "",
246 (result < 0) ? strerror(errno) : "");
251 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
255 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
257 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
258 fsp->fsp_name->base_name, mode,
259 (result < 0) ? "failed: " : "",
260 (result < 0) ? strerror(errno) : "");
265 static struct vfs_fn_pointers vfs_audit_fns = {
266 .connect_fn = audit_connect,
267 .disconnect = audit_disconnect,
268 .opendir = audit_opendir,
269 .mkdir = audit_mkdir,
270 .rmdir = audit_rmdir,
272 .close_fn = audit_close,
273 .rename = audit_rename,
274 .unlink = audit_unlink,
275 .chmod = audit_chmod,
276 .fchmod = audit_fchmod,
277 .chmod_acl = audit_chmod_acl,
278 .fchmod_acl = audit_fchmod_acl
281 NTSTATUS vfs_audit_init(void)
283 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit",