s3:smbd: make use of smbXsrv_tcon and smbXsrv_session for smb2
[Samba/gbeck.git] / source3 / modules / vfs_extd_audit.c
blobfc23ea91488b71367be372d7004494dd430d7cc5
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"
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;
33 #undef DBGC_CLASS
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[] = {
39 { LOG_USER, "USER" },
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" },
48 { -1, NULL}
51 int facility;
53 facility = lp_parm_enum(SNUM(handle->conn), "extd_audit", "facility", enum_log_facilities, LOG_USER);
55 return facility;
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" },
64 { LOG_CRIT, "CRIT" },
65 { LOG_ERR, "ERR" },
66 { LOG_WARNING, "WARNING" },
67 { LOG_NOTICE, "NOTICE" },
68 { LOG_INFO, "INFO" },
69 { LOG_DEBUG, "DEBUG" },
70 { -1, NULL}
73 int priority;
75 priority = lp_parm_enum(SNUM(handle->conn), "extd_audit", "priority",
76 enum_log_priorities, LOG_NOTICE);
77 if (priority == -1) {
78 priority = LOG_WARNING;
81 return priority;
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);
91 if (result < 0) {
92 return result;
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",
100 svc, user);
102 DEBUG(10, ("Connected to service %s as user %s\n",
103 svc, user));
105 return 0;
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);
116 return;
119 static DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
121 DIR *result;
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",
127 fname,
128 (result == NULL) ? "failed: " : "",
129 (result == NULL) ? strerror(errno) : "");
131 DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
132 fname,
133 (result == NULL) ? "failed: " : "",
134 (result == NULL) ? strerror(errno) : ""));
136 return result;
139 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
141 int result;
143 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
145 if (lp_syslog() > 0) {
146 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
147 path,
148 (result < 0) ? "failed: " : "",
149 (result < 0) ? strerror(errno) : "");
151 DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
152 path,
153 (result < 0) ? "failed: " : "",
154 (result < 0) ? strerror(errno) : ""));
156 return result;
159 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
161 int result;
163 result = SMB_VFS_NEXT_RMDIR(handle, path);
165 if (lp_syslog() > 0) {
166 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
167 path,
168 (result < 0) ? "failed: " : "",
169 (result < 0) ? strerror(errno) : "");
171 DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
172 path,
173 (result < 0) ? "failed: " : "",
174 (result < 0) ? strerror(errno) : ""));
176 return result;
179 static int audit_open(vfs_handle_struct *handle,
180 struct smb_filename *smb_fname, files_struct *fsp,
181 int flags, mode_t mode)
183 int result;
185 result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
187 if (lp_syslog() > 0) {
188 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
189 smb_fname->base_name, result,
190 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
191 (result < 0) ? "failed: " : "",
192 (result < 0) ? strerror(errno) : "");
194 DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
195 smb_fname_str_dbg(smb_fname),
196 (result < 0) ? "failed: " : "",
197 (result < 0) ? strerror(errno) : ""));
199 return result;
202 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
204 int result;
206 result = SMB_VFS_NEXT_CLOSE(handle, fsp);
208 if (lp_syslog() > 0) {
209 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
210 fsp->fh->fd,
211 (result < 0) ? "failed: " : "",
212 (result < 0) ? strerror(errno) : "");
214 DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
215 fsp->fh->fd,
216 (result < 0) ? "failed: " : "",
217 (result < 0) ? strerror(errno) : ""));
219 return result;
222 static int audit_rename(vfs_handle_struct *handle,
223 const struct smb_filename *smb_fname_src,
224 const struct smb_filename *smb_fname_dst)
226 int result;
228 result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
230 if (lp_syslog() > 0) {
231 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
232 smb_fname_src->base_name,
233 smb_fname_dst->base_name,
234 (result < 0) ? "failed: " : "",
235 (result < 0) ? strerror(errno) : "");
237 DEBUG(1, ("vfs_extd_audit: rename old: %s newname: %s %s %s\n",
238 smb_fname_str_dbg(smb_fname_src),
239 smb_fname_str_dbg(smb_fname_dst),
240 (result < 0) ? "failed: " : "",
241 (result < 0) ? strerror(errno) : ""));
243 return result;
246 static int audit_unlink(vfs_handle_struct *handle,
247 const struct smb_filename *smb_fname)
249 int result;
251 result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
253 if (lp_syslog() > 0) {
254 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
255 smb_fname->base_name,
256 (result < 0) ? "failed: " : "",
257 (result < 0) ? strerror(errno) : "");
259 DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
260 smb_fname_str_dbg(smb_fname),
261 (result < 0) ? "failed: " : "",
262 (result < 0) ? strerror(errno) : ""));
264 return result;
267 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
269 int result;
271 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
273 if (lp_syslog() > 0) {
274 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
275 path, mode,
276 (result < 0) ? "failed: " : "",
277 (result < 0) ? strerror(errno) : "");
279 DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
280 path, (unsigned int)mode,
281 (result < 0) ? "failed: " : "",
282 (result < 0) ? strerror(errno) : ""));
284 return result;
287 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
289 int result;
291 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
293 if (lp_syslog() > 0) {
294 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
295 path, mode,
296 (result < 0) ? "failed: " : "",
297 (result < 0) ? strerror(errno) : "");
299 DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
300 path, (unsigned int)mode,
301 (result < 0) ? "failed: " : "",
302 (result < 0) ? strerror(errno) : ""));
304 return result;
307 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
309 int result;
311 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
313 if (lp_syslog() > 0) {
314 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
315 fsp->fsp_name->base_name, mode,
316 (result < 0) ? "failed: " : "",
317 (result < 0) ? strerror(errno) : "");
319 DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
320 fsp_str_dbg(fsp), (unsigned int)mode,
321 (result < 0) ? "failed: " : "",
322 (result < 0) ? strerror(errno) : ""));
324 return result;
327 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
329 int result;
331 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
333 if (lp_syslog() > 0) {
334 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
335 fsp->fsp_name->base_name, mode,
336 (result < 0) ? "failed: " : "",
337 (result < 0) ? strerror(errno) : "");
339 DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
340 fsp_str_dbg(fsp), (unsigned int)mode,
341 (result < 0) ? "failed: " : "",
342 (result < 0) ? strerror(errno) : ""));
344 return result;
347 static struct vfs_fn_pointers vfs_extd_audit_fns = {
348 .connect_fn = audit_connect,
349 .disconnect_fn = audit_disconnect,
350 .opendir_fn = audit_opendir,
351 .mkdir_fn = audit_mkdir,
352 .rmdir_fn = audit_rmdir,
353 .open_fn = audit_open,
354 .close_fn = audit_close,
355 .rename_fn = audit_rename,
356 .unlink_fn = audit_unlink,
357 .chmod_fn = audit_chmod,
358 .fchmod_fn = audit_fchmod,
359 .chmod_acl_fn = audit_chmod_acl,
360 .fchmod_acl_fn = audit_fchmod_acl,
363 NTSTATUS vfs_extd_audit_init(void)
365 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
366 "extd_audit", &vfs_extd_audit_fns);
368 if (!NT_STATUS_IS_OK(ret))
369 return ret;
371 vfs_extd_audit_debug_level = debug_add_class("extd_audit");
372 if (vfs_extd_audit_debug_level == -1) {
373 vfs_extd_audit_debug_level = DBGC_VFS;
374 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
375 } else {
376 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level));
379 return ret;