s4:upgradeprovision Rework script, and reset machine account pw
[Samba/cd1.git] / source3 / modules / vfs_audit.c
blob258246e42db2178db08b0091945c86bdc8283a31
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 static int audit_syslog_facility(vfs_handle_struct *handle)
31 static const struct enum_list enum_log_facilities[] = {
32 { LOG_USER, "USER" },
33 { LOG_LOCAL0, "LOCAL0" },
34 { LOG_LOCAL1, "LOCAL1" },
35 { LOG_LOCAL2, "LOCAL2" },
36 { LOG_LOCAL3, "LOCAL3" },
37 { LOG_LOCAL4, "LOCAL4" },
38 { LOG_LOCAL5, "LOCAL5" },
39 { LOG_LOCAL6, "LOCAL6" },
40 { LOG_LOCAL7, "LOCAL7" }
43 int facility;
45 facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
47 return facility;
51 static int audit_syslog_priority(vfs_handle_struct *handle)
53 static const struct enum_list enum_log_priorities[] = {
54 { LOG_EMERG, "EMERG" },
55 { LOG_ALERT, "ALERT" },
56 { LOG_CRIT, "CRIT" },
57 { LOG_ERR, "ERR" },
58 { LOG_WARNING, "WARNING" },
59 { LOG_NOTICE, "NOTICE" },
60 { LOG_INFO, "INFO" },
61 { LOG_DEBUG, "DEBUG" }
64 int priority;
66 priority = lp_parm_enum(SNUM(handle->conn), "audit", "priority",
67 enum_log_priorities, LOG_NOTICE);
68 if (priority == -1) {
69 priority = LOG_WARNING;
72 return priority;
75 /* Implementation of vfs_ops. Pass everything on to the default
76 operation but log event first. */
78 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
80 int result;
82 openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
84 syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n",
85 svc, user);
87 result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
89 return result;
92 static void audit_disconnect(vfs_handle_struct *handle)
94 syslog(audit_syslog_priority(handle), "disconnected\n");
95 SMB_VFS_NEXT_DISCONNECT(handle);
97 return;
100 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
102 SMB_STRUCT_DIR *result;
104 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
106 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
107 fname,
108 (result == NULL) ? "failed: " : "",
109 (result == NULL) ? strerror(errno) : "");
111 return result;
114 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
116 int result;
118 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
120 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
121 path,
122 (result < 0) ? "failed: " : "",
123 (result < 0) ? strerror(errno) : "");
125 return result;
128 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
130 int result;
132 result = SMB_VFS_NEXT_RMDIR(handle, path);
134 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
135 path,
136 (result < 0) ? "failed: " : "",
137 (result < 0) ? strerror(errno) : "");
139 return result;
142 static int audit_open(vfs_handle_struct *handle,
143 struct smb_filename *smb_fname, files_struct *fsp,
144 int flags, mode_t mode)
146 int result;
148 result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
150 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
151 smb_fname->base_name, result,
152 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
153 (result < 0) ? "failed: " : "",
154 (result < 0) ? strerror(errno) : "");
156 return result;
159 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
161 int result;
163 result = SMB_VFS_NEXT_CLOSE(handle, fsp);
165 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
166 fsp->fh->fd,
167 (result < 0) ? "failed: " : "",
168 (result < 0) ? strerror(errno) : "");
170 return result;
173 static int audit_rename(vfs_handle_struct *handle,
174 const struct smb_filename *smb_fname_src,
175 const struct smb_filename *smb_fname_dst)
177 int result;
179 result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
181 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
182 smb_fname_src->base_name,
183 smb_fname_dst->base_name,
184 (result < 0) ? "failed: " : "",
185 (result < 0) ? strerror(errno) : "");
187 return result;
190 static int audit_unlink(vfs_handle_struct *handle,
191 const struct smb_filename *smb_fname)
193 int result;
195 result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
197 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
198 smb_fname->base_name,
199 (result < 0) ? "failed: " : "",
200 (result < 0) ? strerror(errno) : "");
202 return result;
205 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
207 int result;
209 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
211 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
212 path, mode,
213 (result < 0) ? "failed: " : "",
214 (result < 0) ? strerror(errno) : "");
216 return result;
219 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
221 int result;
223 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
225 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
226 path, mode,
227 (result < 0) ? "failed: " : "",
228 (result < 0) ? strerror(errno) : "");
230 return result;
233 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
235 int result;
237 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
239 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
240 fsp->fsp_name->base_name, mode,
241 (result < 0) ? "failed: " : "",
242 (result < 0) ? strerror(errno) : "");
244 return result;
247 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
249 int result;
251 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
253 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
254 fsp->fsp_name->base_name, mode,
255 (result < 0) ? "failed: " : "",
256 (result < 0) ? strerror(errno) : "");
258 return result;
261 static struct vfs_fn_pointers vfs_audit_fns = {
262 .connect_fn = audit_connect,
263 .disconnect = audit_disconnect,
264 .opendir = audit_opendir,
265 .mkdir = audit_mkdir,
266 .rmdir = audit_rmdir,
267 .open = audit_open,
268 .close_fn = audit_close,
269 .rename = audit_rename,
270 .unlink = audit_unlink,
271 .chmod = audit_chmod,
272 .fchmod = audit_fchmod,
273 .chmod_acl = audit_chmod_acl,
274 .fchmod_acl = audit_fchmod_acl
277 NTSTATUS vfs_audit_init(void)
279 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit",
280 &vfs_audit_fns);