s3: We only need base_name in map_open_params_to_ntcreate
[Samba.git] / source3 / modules / vfs_audit.c
blob349600fc43f5ef2e1f82bd1bbad773e68789e929
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) 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/>.
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "system/syslog.h"
27 #include "smbd/smbd.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 static int audit_syslog_facility(vfs_handle_struct *handle)
34 static const struct enum_list enum_log_facilities[] = {
35 { LOG_USER, "USER" },
36 { LOG_LOCAL0, "LOCAL0" },
37 { LOG_LOCAL1, "LOCAL1" },
38 { LOG_LOCAL2, "LOCAL2" },
39 { LOG_LOCAL3, "LOCAL3" },
40 { LOG_LOCAL4, "LOCAL4" },
41 { LOG_LOCAL5, "LOCAL5" },
42 { LOG_LOCAL6, "LOCAL6" },
43 { LOG_LOCAL7, "LOCAL7" }
46 int facility;
48 facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
50 return facility;
54 static int audit_syslog_priority(vfs_handle_struct *handle)
56 static const struct enum_list enum_log_priorities[] = {
57 { LOG_EMERG, "EMERG" },
58 { LOG_ALERT, "ALERT" },
59 { LOG_CRIT, "CRIT" },
60 { LOG_ERR, "ERR" },
61 { LOG_WARNING, "WARNING" },
62 { LOG_NOTICE, "NOTICE" },
63 { LOG_INFO, "INFO" },
64 { LOG_DEBUG, "DEBUG" }
67 int priority;
69 priority = lp_parm_enum(SNUM(handle->conn), "audit", "priority",
70 enum_log_priorities, LOG_NOTICE);
71 if (priority == -1) {
72 priority = LOG_WARNING;
75 return priority;
78 /* Implementation of vfs_ops. Pass everything on to the default
79 operation but log event first. */
81 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
83 int result;
85 result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
86 if (result < 0) {
87 return result;
90 openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
92 syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n",
93 svc, user);
95 return 0;
98 static void audit_disconnect(vfs_handle_struct *handle)
100 syslog(audit_syslog_priority(handle), "disconnected\n");
101 SMB_VFS_NEXT_DISCONNECT(handle);
103 return;
106 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
108 SMB_STRUCT_DIR *result;
110 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
112 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
113 fname,
114 (result == NULL) ? "failed: " : "",
115 (result == NULL) ? strerror(errno) : "");
117 return result;
120 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
122 int result;
124 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
126 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
127 path,
128 (result < 0) ? "failed: " : "",
129 (result < 0) ? strerror(errno) : "");
131 return result;
134 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
136 int result;
138 result = SMB_VFS_NEXT_RMDIR(handle, path);
140 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
141 path,
142 (result < 0) ? "failed: " : "",
143 (result < 0) ? strerror(errno) : "");
145 return result;
148 static int audit_open(vfs_handle_struct *handle,
149 struct smb_filename *smb_fname, files_struct *fsp,
150 int flags, mode_t mode)
152 int result;
154 result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
156 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
157 smb_fname->base_name, result,
158 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
159 (result < 0) ? "failed: " : "",
160 (result < 0) ? strerror(errno) : "");
162 return result;
165 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
167 int result;
169 result = SMB_VFS_NEXT_CLOSE(handle, fsp);
171 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
172 fsp->fh->fd,
173 (result < 0) ? "failed: " : "",
174 (result < 0) ? strerror(errno) : "");
176 return result;
179 static int audit_rename(vfs_handle_struct *handle,
180 const struct smb_filename *smb_fname_src,
181 const struct smb_filename *smb_fname_dst)
183 int result;
185 result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
187 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
188 smb_fname_src->base_name,
189 smb_fname_dst->base_name,
190 (result < 0) ? "failed: " : "",
191 (result < 0) ? strerror(errno) : "");
193 return result;
196 static int audit_unlink(vfs_handle_struct *handle,
197 const struct smb_filename *smb_fname)
199 int result;
201 result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
203 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
204 smb_fname->base_name,
205 (result < 0) ? "failed: " : "",
206 (result < 0) ? strerror(errno) : "");
208 return result;
211 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
213 int result;
215 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
217 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
218 path, mode,
219 (result < 0) ? "failed: " : "",
220 (result < 0) ? strerror(errno) : "");
222 return result;
225 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
227 int result;
229 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
231 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
232 path, mode,
233 (result < 0) ? "failed: " : "",
234 (result < 0) ? strerror(errno) : "");
236 return result;
239 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
241 int result;
243 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
245 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
246 fsp->fsp_name->base_name, mode,
247 (result < 0) ? "failed: " : "",
248 (result < 0) ? strerror(errno) : "");
250 return result;
253 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
255 int result;
257 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
259 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
260 fsp->fsp_name->base_name, mode,
261 (result < 0) ? "failed: " : "",
262 (result < 0) ? strerror(errno) : "");
264 return result;
267 static struct vfs_fn_pointers vfs_audit_fns = {
268 .connect_fn = audit_connect,
269 .disconnect = audit_disconnect,
270 .opendir = audit_opendir,
271 .mkdir = audit_mkdir,
272 .rmdir = audit_rmdir,
273 .open_fn = audit_open,
274 .close_fn = audit_close,
275 .rename = audit_rename,
276 .unlink = audit_unlink,
277 .chmod = audit_chmod,
278 .fchmod = audit_fchmod,
279 .chmod_acl = audit_chmod_acl,
280 .fchmod_acl = audit_fchmod_acl
283 NTSTATUS vfs_audit_init(void)
285 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit",
286 &vfs_audit_fns);