Mark smbc_option_{get,set} as deprecated
[Samba/gbeck.git] / source3 / modules / vfs_extd_audit.c
bloba21e281e2ca6b09cd10f228837c81bc016a0a9a4
1 /*
2 * Auditing VFS module for samba. Log selected file operations to syslog
3 * facility.
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/>.
25 #include "includes.h"
27 static int vfs_extd_audit_debug_level = DBGC_VFS;
29 #undef DBGC_CLASS
30 #define DBGC_CLASS vfs_extd_audit_debug_level
32 /* Function prototypes */
34 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user);
35 static void audit_disconnect(vfs_handle_struct *handle);
36 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr);
37 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode);
38 static int audit_rmdir(vfs_handle_struct *handle, const char *path);
39 static int audit_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode);
40 static int audit_close(vfs_handle_struct *handle, files_struct *fsp, int fd);
41 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname);
42 static int audit_unlink(vfs_handle_struct *handle, const char *path);
43 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode);
44 static int audit_chmod_acl(vfs_handle_struct *handle, const char *name, mode_t mode);
45 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode);
46 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode);
48 /* VFS operations */
50 static vfs_op_tuple audit_op_tuples[] = {
52 /* Disk operations */
54 {SMB_VFS_OP(audit_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_LOGGER},
55 {SMB_VFS_OP(audit_disconnect), SMB_VFS_OP_DISCONNECT, SMB_VFS_LAYER_LOGGER},
57 /* Directory operations */
59 {SMB_VFS_OP(audit_opendir), SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_LOGGER},
60 {SMB_VFS_OP(audit_mkdir), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_LOGGER},
61 {SMB_VFS_OP(audit_rmdir), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_LOGGER},
63 /* File operations */
65 {SMB_VFS_OP(audit_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_LOGGER},
66 {SMB_VFS_OP(audit_close), SMB_VFS_OP_CLOSE, SMB_VFS_LAYER_LOGGER},
67 {SMB_VFS_OP(audit_rename), SMB_VFS_OP_RENAME, SMB_VFS_LAYER_LOGGER},
68 {SMB_VFS_OP(audit_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_LOGGER},
69 {SMB_VFS_OP(audit_chmod), SMB_VFS_OP_CHMOD, SMB_VFS_LAYER_LOGGER},
70 {SMB_VFS_OP(audit_fchmod), SMB_VFS_OP_FCHMOD, SMB_VFS_LAYER_LOGGER},
71 {SMB_VFS_OP(audit_chmod_acl), SMB_VFS_OP_CHMOD_ACL, SMB_VFS_LAYER_LOGGER},
72 {SMB_VFS_OP(audit_fchmod_acl), SMB_VFS_OP_FCHMOD_ACL, SMB_VFS_LAYER_LOGGER},
74 /* Finish VFS operations definition */
76 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
80 static int audit_syslog_facility(vfs_handle_struct *handle)
82 static const struct enum_list enum_log_facilities[] = {
83 { LOG_USER, "USER" },
84 { LOG_LOCAL0, "LOCAL0" },
85 { LOG_LOCAL1, "LOCAL1" },
86 { LOG_LOCAL2, "LOCAL2" },
87 { LOG_LOCAL3, "LOCAL3" },
88 { LOG_LOCAL4, "LOCAL4" },
89 { LOG_LOCAL5, "LOCAL5" },
90 { LOG_LOCAL6, "LOCAL6" },
91 { LOG_LOCAL7, "LOCAL7" }
94 int facility;
96 facility = lp_parm_enum(SNUM(handle->conn), "extd_audit", "facility", enum_log_facilities, LOG_USER);
98 return facility;
102 static int audit_syslog_priority(vfs_handle_struct *handle)
104 static const struct enum_list enum_log_priorities[] = {
105 { LOG_EMERG, "EMERG" },
106 { LOG_ALERT, "ALERT" },
107 { LOG_CRIT, "CRIT" },
108 { LOG_ERR, "ERR" },
109 { LOG_WARNING, "WARNING" },
110 { LOG_NOTICE, "NOTICE" },
111 { LOG_INFO, "INFO" },
112 { LOG_DEBUG, "DEBUG" }
115 int priority;
117 priority = lp_parm_enum(SNUM(handle->conn), "extd_audit", "priority", enum_log_priorities, LOG_NOTICE);
119 return priority;
122 /* Implementation of vfs_ops. Pass everything on to the default
123 operation but log event first. */
125 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
127 int result;
129 openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
131 syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n",
132 svc, user);
133 DEBUG(10, ("Connected to service %s as user %s\n",
134 svc, user));
136 result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
138 return result;
141 static void audit_disconnect(vfs_handle_struct *handle)
143 syslog(audit_syslog_priority(handle), "disconnected\n");
144 DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
145 SMB_VFS_NEXT_DISCONNECT(handle);
147 return;
150 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
152 SMB_STRUCT_DIR *result;
154 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
156 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
157 fname,
158 (result == NULL) ? "failed: " : "",
159 (result == NULL) ? strerror(errno) : "");
160 DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
161 fname,
162 (result == NULL) ? "failed: " : "",
163 (result == NULL) ? strerror(errno) : ""));
165 return result;
168 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
170 int result;
172 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
174 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
175 path,
176 (result < 0) ? "failed: " : "",
177 (result < 0) ? strerror(errno) : "");
178 DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
179 path,
180 (result < 0) ? "failed: " : "",
181 (result < 0) ? strerror(errno) : ""));
183 return result;
186 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
188 int result;
190 result = SMB_VFS_NEXT_RMDIR(handle, path);
192 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
193 path,
194 (result < 0) ? "failed: " : "",
195 (result < 0) ? strerror(errno) : "");
196 DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
197 path,
198 (result < 0) ? "failed: " : "",
199 (result < 0) ? strerror(errno) : ""));
201 return result;
204 static int audit_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode)
206 int result;
208 result = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
210 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
211 fname, result,
212 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
213 (result < 0) ? "failed: " : "",
214 (result < 0) ? strerror(errno) : "");
215 DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
216 fname,
217 (result < 0) ? "failed: " : "",
218 (result < 0) ? strerror(errno) : ""));
220 return result;
223 static int audit_close(vfs_handle_struct *handle, files_struct *fsp, int fd)
225 int result;
227 result = SMB_VFS_NEXT_CLOSE(handle, fsp, fd);
229 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
231 (result < 0) ? "failed: " : "",
232 (result < 0) ? strerror(errno) : "");
233 DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
235 (result < 0) ? "failed: " : "",
236 (result < 0) ? strerror(errno) : ""));
238 return result;
241 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
243 int result;
245 result = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
247 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
248 oldname, newname,
249 (result < 0) ? "failed: " : "",
250 (result < 0) ? strerror(errno) : "");
251 DEBUG(1, ("vfs_extd_audit: rename old: %s newname: %s %s %s\n",
252 oldname, newname,
253 (result < 0) ? "failed: " : "",
254 (result < 0) ? strerror(errno) : ""));
256 return result;
259 static int audit_unlink(vfs_handle_struct *handle, const char *path)
261 int result;
263 result = SMB_VFS_NEXT_UNLINK(handle, path);
265 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
266 path,
267 (result < 0) ? "failed: " : "",
268 (result < 0) ? strerror(errno) : "");
269 DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
270 path,
271 (result < 0) ? "failed: " : "",
272 (result < 0) ? strerror(errno) : ""));
274 return result;
277 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
279 int result;
281 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
283 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
284 path, mode,
285 (result < 0) ? "failed: " : "",
286 (result < 0) ? strerror(errno) : "");
287 DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
288 path, mode,
289 (result < 0) ? "failed: " : "",
290 (result < 0) ? strerror(errno) : ""));
292 return result;
295 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
297 int result;
299 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
301 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
302 path, mode,
303 (result < 0) ? "failed: " : "",
304 (result < 0) ? strerror(errno) : "");
305 DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
306 path, mode,
307 (result < 0) ? "failed: " : "",
308 (result < 0) ? strerror(errno) : ""));
310 return result;
313 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
315 int result;
317 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
319 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
320 fsp->fsp_name, mode,
321 (result < 0) ? "failed: " : "",
322 (result < 0) ? strerror(errno) : "");
323 DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
324 fsp->fsp_name, mode,
325 (result < 0) ? "failed: " : "",
326 (result < 0) ? strerror(errno) : ""));
328 return result;
331 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
333 int result;
335 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
337 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
338 fsp->fsp_name, mode,
339 (result < 0) ? "failed: " : "",
340 (result < 0) ? strerror(errno) : "");
341 DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
342 fsp->fsp_name, mode,
343 (result < 0) ? "failed: " : "",
344 (result < 0) ? strerror(errno) : ""));
346 return result;
349 NTSTATUS vfs_extd_audit_init(void);
350 NTSTATUS vfs_extd_audit_init(void)
352 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "extd_audit", audit_op_tuples);
354 if (!NT_STATUS_IS_OK(ret))
355 return ret;
357 vfs_extd_audit_debug_level = debug_add_class("extd_audit");
358 if (vfs_extd_audit_debug_level == -1) {
359 vfs_extd_audit_debug_level = DBGC_VFS;
360 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
361 } else {
362 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level));
365 return ret;