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
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 2 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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 #ifndef SYSLOG_FACILITY
43 #define SYSLOG_FACILITY LOG_USER
46 #ifndef SYSLOG_PRIORITY
47 #define SYSLOG_PRIORITY LOG_NOTICE
50 /* Function prototypes */
52 static int audit_connect(struct connection_struct *conn, const char *svc, const char *user);
53 static void audit_disconnect(struct connection_struct *conn);
54 static DIR *audit_opendir(struct connection_struct *conn, const char *fname);
55 static int audit_mkdir(struct connection_struct *conn, const char *path, mode_t mode);
56 static int audit_rmdir(struct connection_struct *conn, const char *path);
57 static int audit_open(struct connection_struct *conn, const char *fname, int flags, mode_t mode);
58 static int audit_close(struct files_struct *fsp, int fd);
59 static int audit_rename(struct connection_struct *conn, const char *old, const char *new);
60 static int audit_unlink(struct connection_struct *conn, const char *path);
61 static int audit_chmod(struct connection_struct *conn, const char *path, mode_t mode);
62 static int audit_chmod_acl(struct connection_struct *conn, const char *name, mode_t mode);
63 static int audit_fchmod(struct files_struct *fsp, int fd, mode_t mode);
64 static int audit_fchmod_acl(struct files_struct *fsp, int fd, mode_t mode);
68 static struct vfs_ops default_vfs_ops; /* For passthrough operation */
69 static struct smb_vfs_handle_struct *audit_handle;
71 static vfs_op_tuple audit_ops[] = {
75 {audit_connect, SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_LOGGER},
76 {audit_disconnect, SMB_VFS_OP_DISCONNECT, SMB_VFS_LAYER_LOGGER},
78 /* Directory operations */
80 {audit_opendir, SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_LOGGER},
81 {audit_mkdir, SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_LOGGER},
82 {audit_rmdir, SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_LOGGER},
86 {audit_open, SMB_VFS_OP_OPEN, SMB_VFS_LAYER_LOGGER},
87 {audit_close, SMB_VFS_OP_CLOSE, SMB_VFS_LAYER_LOGGER},
88 {audit_rename, SMB_VFS_OP_RENAME, SMB_VFS_LAYER_LOGGER},
89 {audit_unlink, SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_LOGGER},
90 {audit_chmod, SMB_VFS_OP_CHMOD, SMB_VFS_LAYER_LOGGER},
91 {audit_fchmod, SMB_VFS_OP_FCHMOD, SMB_VFS_LAYER_LOGGER},
92 {audit_chmod_acl, SMB_VFS_OP_CHMOD_ACL, SMB_VFS_LAYER_LOGGER},
93 {audit_fchmod_acl, SMB_VFS_OP_FCHMOD_ACL, SMB_VFS_LAYER_LOGGER},
95 /* Finish VFS operations definition */
97 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
100 /* VFS initialisation function. Return vfs_op_tuple array back to SAMBA. */
102 vfs_op_tuple *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops,
103 struct smb_vfs_handle_struct *vfs_handle)
105 *vfs_version = SMB_VFS_INTERFACE_VERSION;
106 memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
108 audit_handle = vfs_handle;
110 openlog("smbd_audit", LOG_PID, SYSLOG_FACILITY);
111 syslog(SYSLOG_PRIORITY, "VFS_INIT: vfs_ops loaded\n");
116 /* VFS finalization function. */
118 void vfs_done(connection_struct *conn)
120 syslog(SYSLOG_PRIORITY, "VFS_DONE: vfs module unloaded\n");
123 /* Implementation of vfs_ops. Pass everything on to the default
124 operation but log event first. */
126 static int audit_connect(struct connection_struct *conn, const char *svc, const char *user)
128 syslog(SYSLOG_PRIORITY, "connect to service %s by user %s\n",
130 DEBUG(10, ("Connected to service %s as user %s\n",
133 return default_vfs_ops.connect(conn, svc, user);
136 static void audit_disconnect(struct connection_struct *conn)
138 syslog(SYSLOG_PRIORITY, "disconnected\n");
139 DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
141 default_vfs_ops.disconnect(conn);
144 static DIR *audit_opendir(struct connection_struct *conn, const char *fname)
146 DIR *result = default_vfs_ops.opendir(conn, fname);
148 syslog(SYSLOG_PRIORITY, "opendir %s %s%s\n",
150 (result == NULL) ? "failed: " : "",
151 (result == NULL) ? strerror(errno) : "");
152 DEBUG(1, ("vfs_extd_audit: opendir %s %s %s",
154 (result == NULL) ? "failed: " : "",
155 (result == NULL) ? strerror(errno) : ""));
160 static int audit_mkdir(struct connection_struct *conn, const char *path, mode_t mode)
162 int result = default_vfs_ops.mkdir(conn, path, mode);
164 syslog(SYSLOG_PRIORITY, "mkdir %s %s%s\n",
166 (result < 0) ? "failed: " : "",
167 (result < 0) ? strerror(errno) : "");
168 DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
170 (result < 0) ? "failed: " : "",
171 (result < 0) ? strerror(errno) : ""));
176 static int audit_rmdir(struct connection_struct *conn, const char *path)
178 int result = default_vfs_ops.rmdir(conn, path);
180 syslog(SYSLOG_PRIORITY, "rmdir %s %s%s\n",
182 (result < 0) ? "failed: " : "",
183 (result < 0) ? strerror(errno) : "");
184 DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
186 (result < 0) ? "failed: " : "",
187 (result < 0) ? strerror(errno) : ""));
192 static int audit_open(struct connection_struct *conn, const char *fname, int flags, mode_t mode)
194 int result = default_vfs_ops.open(conn, fname, flags, mode);
196 syslog(SYSLOG_PRIORITY, "open %s (fd %d) %s%s%s\n",
198 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
199 (result < 0) ? "failed: " : "",
200 (result < 0) ? strerror(errno) : "");
201 DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
203 (result < 0) ? "failed: " : "",
204 (result < 0) ? strerror(errno) : ""));
209 static int audit_close(struct files_struct *fsp, int fd)
211 int result = default_vfs_ops.close(fsp, fd);
213 syslog(SYSLOG_PRIORITY, "close fd %d %s%s\n",
215 (result < 0) ? "failed: " : "",
216 (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(struct connection_struct *conn, const char *old, const char *new)
227 int result = default_vfs_ops.rename(conn, old, new);
229 syslog(SYSLOG_PRIORITY, "rename %s -> %s %s%s\n",
231 (result < 0) ? "failed: " : "",
232 (result < 0) ? strerror(errno) : "");
233 DEBUG(1, ("vfs_extd_audit: rename old: %s new: %s %s %s\n",
235 (result < 0) ? "failed: " : "",
236 (result < 0) ? strerror(errno) : ""));
241 static int audit_unlink(struct connection_struct *conn, const char *path)
243 int result = default_vfs_ops.unlink(conn, path);
245 syslog(SYSLOG_PRIORITY, "unlink %s %s%s\n",
247 (result < 0) ? "failed: " : "",
248 (result < 0) ? strerror(errno) : "");
249 DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
251 (result < 0) ? "failed: " : "",
252 (result < 0) ? strerror(errno) : ""));
257 static int audit_chmod(struct connection_struct *conn, const char *path, mode_t mode)
259 int result = default_vfs_ops.chmod(conn, path, mode);
261 syslog(SYSLOG_PRIORITY, "chmod %s mode 0x%x %s%s\n",
263 (result < 0) ? "failed: " : "",
264 (result < 0) ? strerror(errno) : "");
265 DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
267 (result < 0) ? "failed: " : "",
268 (result < 0) ? strerror(errno) : ""));
273 static int audit_chmod_acl(struct connection_struct *conn, const char *path, mode_t mode)
275 int result = default_vfs_ops.chmod_acl(conn, path, mode);
277 syslog(SYSLOG_PRIORITY, "chmod_acl %s mode 0x%x %s%s\n",
279 (result < 0) ? "failed: " : "",
280 (result < 0) ? strerror(errno) : "");
281 DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
283 (result < 0) ? "failed: " : "",
284 (result < 0) ? strerror(errno) : ""));
289 static int audit_fchmod(struct files_struct *fsp, int fd, mode_t mode)
291 int result = default_vfs_ops.fchmod(fsp, fd, mode);
293 syslog(SYSLOG_PRIORITY, "fchmod %s mode 0x%x %s%s\n",
295 (result < 0) ? "failed: " : "",
296 (result < 0) ? strerror(errno) : "");
297 DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
299 (result < 0) ? "failed: " : "",
300 (result < 0) ? strerror(errno) : ""));
305 static int audit_fchmod_acl(struct files_struct *fsp, int fd, mode_t mode)
307 int result = default_vfs_ops.fchmod_acl(fsp, fd, mode);
309 syslog(SYSLOG_PRIORITY, "fchmod_acl %s mode 0x%x %s%s\n",
311 (result < 0) ? "failed: " : "",
312 (result < 0) ? strerror(errno) : "");
313 DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
315 (result < 0) ? "failed: " : "",
316 (result < 0) ? strerror(errno) : ""));