2 * Auditing VFS module for samba. Log selected file operations to syslog
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/>.
25 #include "system/filesys.h"
26 #include "system/syslog.h"
27 #include "smbd/smbd.h"
28 #include "lib/param/loadparm.h"
31 #define DBGC_CLASS DBGC_VFS
33 static int audit_syslog_facility(vfs_handle_struct
*handle
)
35 static const struct enum_list enum_log_facilities
[] = {
40 { LOG_AUTHPRIV
, "AUTHPRIV" },
43 { LOG_AUDIT
, "AUDIT" },
46 { LOG_CONSOLE
, "CONSOLE" },
52 { LOG_DAEMON
, "DAEMON" },
58 { LOG_INSTALL
, "INSTALL" },
64 { LOG_LAUNCHD
, "LAUNCHD" },
76 { LOG_MEGASAFE
, "MEGASAFE" },
79 { LOG_NETINFO
, "NETINFO" },
84 #ifdef LOG_NFACILITIES
85 { LOG_NFACILITIES
, "NFACILITIES" },
94 { LOG_REMOTEAUTH
, "REMOTEAUTH" },
97 { LOG_SECURITY
, "SECURITY" },
100 { LOG_SYSLOG
, "SYSLOG" },
103 { LOG_USER
, "USER" },
106 { LOG_UUCP
, "UUCP" },
108 { LOG_LOCAL0
, "LOCAL0" },
109 { LOG_LOCAL1
, "LOCAL1" },
110 { LOG_LOCAL2
, "LOCAL2" },
111 { LOG_LOCAL3
, "LOCAL3" },
112 { LOG_LOCAL4
, "LOCAL4" },
113 { LOG_LOCAL5
, "LOCAL5" },
114 { LOG_LOCAL6
, "LOCAL6" },
115 { LOG_LOCAL7
, "LOCAL7" },
121 facility
= lp_parm_enum(SNUM(handle
->conn
), "audit", "facility", enum_log_facilities
, LOG_USER
);
127 static int audit_syslog_priority(vfs_handle_struct
*handle
)
129 static const struct enum_list enum_log_priorities
[] = {
130 { LOG_EMERG
, "EMERG" },
131 { LOG_ALERT
, "ALERT" },
132 { LOG_CRIT
, "CRIT" },
134 { LOG_WARNING
, "WARNING" },
135 { LOG_NOTICE
, "NOTICE" },
136 { LOG_INFO
, "INFO" },
137 { LOG_DEBUG
, "DEBUG" },
143 priority
= lp_parm_enum(SNUM(handle
->conn
), "audit", "priority",
144 enum_log_priorities
, LOG_NOTICE
);
145 if (priority
== -1) {
146 priority
= LOG_WARNING
;
152 /* Implementation of vfs_ops. Pass everything on to the default
153 operation but log event first. */
155 static int audit_connect(vfs_handle_struct
*handle
, const char *svc
, const char *user
)
159 result
= SMB_VFS_NEXT_CONNECT(handle
, svc
, user
);
164 openlog("smbd_audit", LOG_PID
, audit_syslog_facility(handle
));
166 syslog(audit_syslog_priority(handle
), "connect to service %s by user %s\n",
172 static void audit_disconnect(vfs_handle_struct
*handle
)
174 syslog(audit_syslog_priority(handle
), "disconnected\n");
175 SMB_VFS_NEXT_DISCONNECT(handle
);
180 static DIR *audit_opendir(vfs_handle_struct
*handle
,
181 const struct smb_filename
*smb_fname
,
187 result
= SMB_VFS_NEXT_OPENDIR(handle
, smb_fname
, mask
, attr
);
189 syslog(audit_syslog_priority(handle
), "opendir %s %s%s\n",
190 smb_fname
->base_name
,
191 (result
== NULL
) ? "failed: " : "",
192 (result
== NULL
) ? strerror(errno
) : "");
197 static int audit_mkdir(vfs_handle_struct
*handle
,
198 const struct smb_filename
*smb_fname
,
203 result
= SMB_VFS_NEXT_MKDIR(handle
, smb_fname
, mode
);
205 syslog(audit_syslog_priority(handle
), "mkdir %s %s%s\n",
206 smb_fname
->base_name
,
207 (result
< 0) ? "failed: " : "",
208 (result
< 0) ? strerror(errno
) : "");
213 static int audit_rmdir(vfs_handle_struct
*handle
,
214 const struct smb_filename
*smb_fname
)
218 result
= SMB_VFS_NEXT_RMDIR(handle
, smb_fname
);
220 syslog(audit_syslog_priority(handle
), "rmdir %s %s%s\n",
221 smb_fname
->base_name
,
222 (result
< 0) ? "failed: " : "",
223 (result
< 0) ? strerror(errno
) : "");
228 static int audit_open(vfs_handle_struct
*handle
,
229 struct smb_filename
*smb_fname
, files_struct
*fsp
,
230 int flags
, mode_t mode
)
234 result
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
236 syslog(audit_syslog_priority(handle
), "open %s (fd %d) %s%s%s\n",
237 smb_fname
->base_name
, result
,
238 ((flags
& O_WRONLY
) || (flags
& O_RDWR
)) ? "for writing " : "",
239 (result
< 0) ? "failed: " : "",
240 (result
< 0) ? strerror(errno
) : "");
245 static int audit_close(vfs_handle_struct
*handle
, files_struct
*fsp
)
249 result
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
251 syslog(audit_syslog_priority(handle
), "close fd %d %s%s\n",
253 (result
< 0) ? "failed: " : "",
254 (result
< 0) ? strerror(errno
) : "");
259 static int audit_rename(vfs_handle_struct
*handle
,
260 const struct smb_filename
*smb_fname_src
,
261 const struct smb_filename
*smb_fname_dst
)
265 result
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
267 syslog(audit_syslog_priority(handle
), "rename %s -> %s %s%s\n",
268 smb_fname_src
->base_name
,
269 smb_fname_dst
->base_name
,
270 (result
< 0) ? "failed: " : "",
271 (result
< 0) ? strerror(errno
) : "");
276 static int audit_unlink(vfs_handle_struct
*handle
,
277 const struct smb_filename
*smb_fname
)
281 result
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
283 syslog(audit_syslog_priority(handle
), "unlink %s %s%s\n",
284 smb_fname
->base_name
,
285 (result
< 0) ? "failed: " : "",
286 (result
< 0) ? strerror(errno
) : "");
291 static int audit_chmod(vfs_handle_struct
*handle
,
292 const struct smb_filename
*smb_fname
,
297 result
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname
, mode
);
299 syslog(audit_syslog_priority(handle
), "chmod %s mode 0x%x %s%s\n",
300 smb_fname
->base_name
, mode
,
301 (result
< 0) ? "failed: " : "",
302 (result
< 0) ? strerror(errno
) : "");
307 static int audit_fchmod(vfs_handle_struct
*handle
, files_struct
*fsp
, mode_t mode
)
311 result
= SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
313 syslog(audit_syslog_priority(handle
), "fchmod %s mode 0x%x %s%s\n",
314 fsp
->fsp_name
->base_name
, mode
,
315 (result
< 0) ? "failed: " : "",
316 (result
< 0) ? strerror(errno
) : "");
321 static struct vfs_fn_pointers vfs_audit_fns
= {
322 .connect_fn
= audit_connect
,
323 .disconnect_fn
= audit_disconnect
,
324 .opendir_fn
= audit_opendir
,
325 .mkdir_fn
= audit_mkdir
,
326 .rmdir_fn
= audit_rmdir
,
327 .open_fn
= audit_open
,
328 .close_fn
= audit_close
,
329 .rename_fn
= audit_rename
,
330 .unlink_fn
= audit_unlink
,
331 .chmod_fn
= audit_chmod
,
332 .fchmod_fn
= audit_fchmod
,
336 NTSTATUS
vfs_audit_init(TALLOC_CTX
*ctx
)
338 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "audit",