Fix IDL for lsa_LookupSids3.
[Samba/gebeck_regimport.git] / source3 / modules / vfs_audit.c
bloba63bf4f67261e50ed551c439ec4748d98ada45d2
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"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
29 /* Function prototypes */
31 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user);
32 static void audit_disconnect(vfs_handle_struct *handle);
33 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr);
34 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode);
35 static int audit_rmdir(vfs_handle_struct *handle, const char *path);
36 static int audit_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode);
37 static int audit_close(vfs_handle_struct *handle, files_struct *fsp, int fd);
38 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname);
39 static int audit_unlink(vfs_handle_struct *handle, const char *path);
40 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode);
41 static int audit_chmod_acl(vfs_handle_struct *handle, const char *name, mode_t mode);
42 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode);
43 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode);
45 /* VFS operations */
47 static vfs_op_tuple audit_op_tuples[] = {
49 /* Disk operations */
51 {SMB_VFS_OP(audit_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_LOGGER},
52 {SMB_VFS_OP(audit_disconnect), SMB_VFS_OP_DISCONNECT, SMB_VFS_LAYER_LOGGER},
54 /* Directory operations */
56 {SMB_VFS_OP(audit_opendir), SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_LOGGER},
57 {SMB_VFS_OP(audit_mkdir), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_LOGGER},
58 {SMB_VFS_OP(audit_rmdir), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_LOGGER},
60 /* File operations */
62 {SMB_VFS_OP(audit_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_LOGGER},
63 {SMB_VFS_OP(audit_close), SMB_VFS_OP_CLOSE, SMB_VFS_LAYER_LOGGER},
64 {SMB_VFS_OP(audit_rename), SMB_VFS_OP_RENAME, SMB_VFS_LAYER_LOGGER},
65 {SMB_VFS_OP(audit_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_LOGGER},
66 {SMB_VFS_OP(audit_chmod), SMB_VFS_OP_CHMOD, SMB_VFS_LAYER_LOGGER},
67 {SMB_VFS_OP(audit_fchmod), SMB_VFS_OP_FCHMOD, SMB_VFS_LAYER_LOGGER},
68 {SMB_VFS_OP(audit_chmod_acl), SMB_VFS_OP_CHMOD_ACL, SMB_VFS_LAYER_LOGGER},
69 {SMB_VFS_OP(audit_fchmod_acl), SMB_VFS_OP_FCHMOD_ACL, SMB_VFS_LAYER_LOGGER},
71 /* Finish VFS operations definition */
73 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
77 static int audit_syslog_facility(vfs_handle_struct *handle)
79 static const struct enum_list enum_log_facilities[] = {
80 { LOG_USER, "USER" },
81 { LOG_LOCAL0, "LOCAL0" },
82 { LOG_LOCAL1, "LOCAL1" },
83 { LOG_LOCAL2, "LOCAL2" },
84 { LOG_LOCAL3, "LOCAL3" },
85 { LOG_LOCAL4, "LOCAL4" },
86 { LOG_LOCAL5, "LOCAL5" },
87 { LOG_LOCAL6, "LOCAL6" },
88 { LOG_LOCAL7, "LOCAL7" }
91 int facility;
93 facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
95 return facility;
99 static int audit_syslog_priority(vfs_handle_struct *handle)
101 static const struct enum_list enum_log_priorities[] = {
102 { LOG_EMERG, "EMERG" },
103 { LOG_ALERT, "ALERT" },
104 { LOG_CRIT, "CRIT" },
105 { LOG_ERR, "ERR" },
106 { LOG_WARNING, "WARNING" },
107 { LOG_NOTICE, "NOTICE" },
108 { LOG_INFO, "INFO" },
109 { LOG_DEBUG, "DEBUG" }
112 int priority;
114 priority = lp_parm_enum(SNUM(handle->conn), "audit", "priority", enum_log_priorities, LOG_NOTICE);
116 return priority;
119 /* Implementation of vfs_ops. Pass everything on to the default
120 operation but log event first. */
122 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
124 int result;
126 openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
128 syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n",
129 svc, user);
131 result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
133 return result;
136 static void audit_disconnect(vfs_handle_struct *handle)
138 syslog(audit_syslog_priority(handle), "disconnected\n");
139 SMB_VFS_NEXT_DISCONNECT(handle);
141 return;
144 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
146 SMB_STRUCT_DIR *result;
148 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
150 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
151 fname,
152 (result == NULL) ? "failed: " : "",
153 (result == NULL) ? strerror(errno) : "");
155 return result;
158 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
160 int result;
162 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
164 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
165 path,
166 (result < 0) ? "failed: " : "",
167 (result < 0) ? strerror(errno) : "");
169 return result;
172 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
174 int result;
176 result = SMB_VFS_NEXT_RMDIR(handle, path);
178 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
179 path,
180 (result < 0) ? "failed: " : "",
181 (result < 0) ? strerror(errno) : "");
183 return result;
186 static int audit_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode)
188 int result;
190 result = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
192 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
193 fname, result,
194 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
195 (result < 0) ? "failed: " : "",
196 (result < 0) ? strerror(errno) : "");
198 return result;
201 static int audit_close(vfs_handle_struct *handle, files_struct *fsp, int fd)
203 int result;
205 result = SMB_VFS_NEXT_CLOSE(handle, fsp, fd);
207 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
209 (result < 0) ? "failed: " : "",
210 (result < 0) ? strerror(errno) : "");
212 return result;
215 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
217 int result;
219 result = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
221 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
222 oldname, newname,
223 (result < 0) ? "failed: " : "",
224 (result < 0) ? strerror(errno) : "");
226 return result;
229 static int audit_unlink(vfs_handle_struct *handle, const char *path)
231 int result;
233 result = SMB_VFS_NEXT_UNLINK(handle, path);
235 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
236 path,
237 (result < 0) ? "failed: " : "",
238 (result < 0) ? strerror(errno) : "");
240 return result;
243 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
245 int result;
247 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
249 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
250 path, mode,
251 (result < 0) ? "failed: " : "",
252 (result < 0) ? strerror(errno) : "");
254 return result;
257 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
259 int result;
261 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
263 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
264 path, mode,
265 (result < 0) ? "failed: " : "",
266 (result < 0) ? strerror(errno) : "");
268 return result;
271 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
273 int result;
275 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
277 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
278 fsp->fsp_name, mode,
279 (result < 0) ? "failed: " : "",
280 (result < 0) ? strerror(errno) : "");
282 return result;
285 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
287 int result;
289 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
291 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
292 fsp->fsp_name, mode,
293 (result < 0) ? "failed: " : "",
294 (result < 0) ? strerror(errno) : "");
296 return result;
299 NTSTATUS vfs_audit_init(void);
300 NTSTATUS vfs_audit_init(void)
302 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit", audit_op_tuples);