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/syslog.h"
28 static int vfs_extd_audit_debug_level = DBGC_VFS;
31 #define DBGC_CLASS vfs_extd_audit_debug_level
33 static int audit_syslog_facility(vfs_handle_struct *handle)
35 static const struct enum_list enum_log_facilities[] = {
37 { LOG_LOCAL0, "LOCAL0" },
38 { LOG_LOCAL1, "LOCAL1" },
39 { LOG_LOCAL2, "LOCAL2" },
40 { LOG_LOCAL3, "LOCAL3" },
41 { LOG_LOCAL4, "LOCAL4" },
42 { LOG_LOCAL5, "LOCAL5" },
43 { LOG_LOCAL6, "LOCAL6" },
44 { LOG_LOCAL7, "LOCAL7" }
49 facility = lp_parm_enum(SNUM(handle->conn), "extd_audit", "facility", enum_log_facilities, LOG_USER);
55 static int audit_syslog_priority(vfs_handle_struct *handle)
57 static const struct enum_list enum_log_priorities[] = {
58 { LOG_EMERG, "EMERG" },
59 { LOG_ALERT, "ALERT" },
62 { LOG_WARNING, "WARNING" },
63 { LOG_NOTICE, "NOTICE" },
65 { LOG_DEBUG, "DEBUG" }
70 priority = lp_parm_enum(SNUM(handle->conn), "extd_audit", "priority",
71 enum_log_priorities, LOG_NOTICE);
73 priority = LOG_WARNING;
79 /* Implementation of vfs_ops. Pass everything on to the default
80 operation but log event first. */
82 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
84 int result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
90 openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
92 if (lp_syslog() > 0) {
93 syslog(audit_syslog_priority(handle),
94 "connect to service %s by user %s\n",
97 DEBUG(10, ("Connected to service %s as user %s\n",
103 static void audit_disconnect(vfs_handle_struct *handle)
105 if (lp_syslog() > 0) {
106 syslog(audit_syslog_priority(handle), "disconnected\n");
108 DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
109 SMB_VFS_NEXT_DISCONNECT(handle);
114 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
116 SMB_STRUCT_DIR *result;
118 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
120 if (lp_syslog() > 0) {
121 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
123 (result == NULL) ? "failed: " : "",
124 (result == NULL) ? strerror(errno) : "");
126 DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
128 (result == NULL) ? "failed: " : "",
129 (result == NULL) ? strerror(errno) : ""));
134 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
138 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
140 if (lp_syslog() > 0) {
141 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
143 (result < 0) ? "failed: " : "",
144 (result < 0) ? strerror(errno) : "");
146 DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
148 (result < 0) ? "failed: " : "",
149 (result < 0) ? strerror(errno) : ""));
154 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
158 result = SMB_VFS_NEXT_RMDIR(handle, path);
160 if (lp_syslog() > 0) {
161 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
163 (result < 0) ? "failed: " : "",
164 (result < 0) ? strerror(errno) : "");
166 DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
168 (result < 0) ? "failed: " : "",
169 (result < 0) ? strerror(errno) : ""));
174 static int audit_open(vfs_handle_struct *handle,
175 struct smb_filename *smb_fname, files_struct *fsp,
176 int flags, mode_t mode)
180 result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
182 if (lp_syslog() > 0) {
183 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
184 smb_fname->base_name, result,
185 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
186 (result < 0) ? "failed: " : "",
187 (result < 0) ? strerror(errno) : "");
189 DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
190 smb_fname_str_dbg(smb_fname),
191 (result < 0) ? "failed: " : "",
192 (result < 0) ? strerror(errno) : ""));
197 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
201 result = SMB_VFS_NEXT_CLOSE(handle, fsp);
203 if (lp_syslog() > 0) {
204 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
206 (result < 0) ? "failed: " : "",
207 (result < 0) ? strerror(errno) : "");
209 DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
211 (result < 0) ? "failed: " : "",
212 (result < 0) ? strerror(errno) : ""));
217 static int audit_rename(vfs_handle_struct *handle,
218 const struct smb_filename *smb_fname_src,
219 const struct smb_filename *smb_fname_dst)
223 result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
225 if (lp_syslog() > 0) {
226 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
227 smb_fname_src->base_name,
228 smb_fname_dst->base_name,
229 (result < 0) ? "failed: " : "",
230 (result < 0) ? strerror(errno) : "");
232 DEBUG(1, ("vfs_extd_audit: rename old: %s newname: %s %s %s\n",
233 smb_fname_str_dbg(smb_fname_src),
234 smb_fname_str_dbg(smb_fname_dst),
235 (result < 0) ? "failed: " : "",
236 (result < 0) ? strerror(errno) : ""));
241 static int audit_unlink(vfs_handle_struct *handle,
242 const struct smb_filename *smb_fname)
246 result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
248 if (lp_syslog() > 0) {
249 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
250 smb_fname->base_name,
251 (result < 0) ? "failed: " : "",
252 (result < 0) ? strerror(errno) : "");
254 DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
255 smb_fname_str_dbg(smb_fname),
256 (result < 0) ? "failed: " : "",
257 (result < 0) ? strerror(errno) : ""));
262 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
266 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
268 if (lp_syslog() > 0) {
269 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
271 (result < 0) ? "failed: " : "",
272 (result < 0) ? strerror(errno) : "");
274 DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
275 path, (unsigned int)mode,
276 (result < 0) ? "failed: " : "",
277 (result < 0) ? strerror(errno) : ""));
282 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
286 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
288 if (lp_syslog() > 0) {
289 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
291 (result < 0) ? "failed: " : "",
292 (result < 0) ? strerror(errno) : "");
294 DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
295 path, (unsigned int)mode,
296 (result < 0) ? "failed: " : "",
297 (result < 0) ? strerror(errno) : ""));
302 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
306 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
308 if (lp_syslog() > 0) {
309 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
310 fsp->fsp_name->base_name, mode,
311 (result < 0) ? "failed: " : "",
312 (result < 0) ? strerror(errno) : "");
314 DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
315 fsp_str_dbg(fsp), (unsigned int)mode,
316 (result < 0) ? "failed: " : "",
317 (result < 0) ? strerror(errno) : ""));
322 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
326 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
328 if (lp_syslog() > 0) {
329 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
330 fsp->fsp_name->base_name, mode,
331 (result < 0) ? "failed: " : "",
332 (result < 0) ? strerror(errno) : "");
334 DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
335 fsp_str_dbg(fsp), (unsigned int)mode,
336 (result < 0) ? "failed: " : "",
337 (result < 0) ? strerror(errno) : ""));
342 static struct vfs_fn_pointers vfs_extd_audit_fns = {
343 .connect_fn = audit_connect,
344 .disconnect = audit_disconnect,
345 .opendir = audit_opendir,
346 .mkdir = audit_mkdir,
347 .rmdir = audit_rmdir,
349 .close_fn = audit_close,
350 .rename = audit_rename,
351 .unlink = audit_unlink,
352 .chmod = audit_chmod,
353 .fchmod = audit_fchmod,
354 .chmod_acl = audit_chmod_acl,
355 .fchmod_acl = audit_fchmod_acl,
358 NTSTATUS vfs_extd_audit_init(void)
360 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
361 "extd_audit", &vfs_extd_audit_fns);
363 if (!NT_STATUS_IS_OK(ret))
366 vfs_extd_audit_debug_level = debug_add_class("extd_audit");
367 if (vfs_extd_audit_debug_level == -1) {
368 vfs_extd_audit_debug_level = DBGC_VFS;
369 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
371 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level));