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) 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/>.
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
;
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
[] = {
43 { LOG_AUTHPRIV
, "AUTHPRIV" },
46 { LOG_AUDIT
, "AUDIT" },
49 { LOG_CONSOLE
, "CONSOLE" },
55 { LOG_DAEMON
, "DAEMON" },
61 { LOG_INSTALL
, "INSTALL" },
67 { LOG_LAUNCHD
, "LAUNCHD" },
79 { LOG_MEGASAFE
, "MEGASAFE" },
82 { LOG_NETINFO
, "NETINFO" },
87 #ifdef LOG_NFACILITIES
88 { LOG_NFACILITIES
, "NFACILITIES" },
97 { LOG_REMOTEAUTH
, "REMOTEAUTH" },
100 { LOG_SECURITY
, "SECURITY" },
103 { LOG_SYSLOG
, "SYSLOG" },
106 { LOG_USER
, "USER" },
109 { LOG_UUCP
, "UUCP" },
111 { LOG_LOCAL0
, "LOCAL0" },
112 { LOG_LOCAL1
, "LOCAL1" },
113 { LOG_LOCAL2
, "LOCAL2" },
114 { LOG_LOCAL3
, "LOCAL3" },
115 { LOG_LOCAL4
, "LOCAL4" },
116 { LOG_LOCAL5
, "LOCAL5" },
117 { LOG_LOCAL6
, "LOCAL6" },
118 { LOG_LOCAL7
, "LOCAL7" },
124 facility
= lp_parm_enum(SNUM(handle
->conn
), "extd_audit", "facility", enum_log_facilities
, LOG_USER
);
130 static int audit_syslog_priority(vfs_handle_struct
*handle
)
132 static const struct enum_list enum_log_priorities
[] = {
133 { LOG_EMERG
, "EMERG" },
134 { LOG_ALERT
, "ALERT" },
135 { LOG_CRIT
, "CRIT" },
137 { LOG_WARNING
, "WARNING" },
138 { LOG_NOTICE
, "NOTICE" },
139 { LOG_INFO
, "INFO" },
140 { LOG_DEBUG
, "DEBUG" },
146 priority
= lp_parm_enum(SNUM(handle
->conn
), "extd_audit", "priority",
147 enum_log_priorities
, LOG_NOTICE
);
148 if (priority
== -1) {
149 priority
= LOG_WARNING
;
155 /* Implementation of vfs_ops. Pass everything on to the default
156 operation but log event first. */
158 static int audit_connect(vfs_handle_struct
*handle
, const char *svc
, const char *user
)
160 int result
= SMB_VFS_NEXT_CONNECT(handle
, svc
, user
);
166 openlog("smbd_audit", LOG_PID
, audit_syslog_facility(handle
));
168 if (lp_syslog() > 0) {
169 syslog(audit_syslog_priority(handle
),
170 "connect to service %s by user %s\n",
173 DEBUG(10, ("Connected to service %s as user %s\n",
179 static void audit_disconnect(vfs_handle_struct
*handle
)
181 if (lp_syslog() > 0) {
182 syslog(audit_syslog_priority(handle
), "disconnected\n");
184 DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
185 SMB_VFS_NEXT_DISCONNECT(handle
);
190 static DIR *audit_opendir(vfs_handle_struct
*handle
,
191 const struct smb_filename
*smb_fname
,
197 result
= SMB_VFS_NEXT_OPENDIR(handle
, smb_fname
, mask
, attr
);
199 if (lp_syslog() > 0) {
200 syslog(audit_syslog_priority(handle
), "opendir %s %s%s\n",
201 smb_fname
->base_name
,
202 (result
== NULL
) ? "failed: " : "",
203 (result
== NULL
) ? strerror(errno
) : "");
205 DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
206 smb_fname
->base_name
,
207 (result
== NULL
) ? "failed: " : "",
208 (result
== NULL
) ? strerror(errno
) : ""));
213 static int audit_mkdir(vfs_handle_struct
*handle
,
214 const struct smb_filename
*smb_fname
,
219 result
= SMB_VFS_NEXT_MKDIR(handle
, smb_fname
, mode
);
221 if (lp_syslog() > 0) {
222 syslog(audit_syslog_priority(handle
), "mkdir %s %s%s\n",
223 smb_fname
->base_name
,
224 (result
< 0) ? "failed: " : "",
225 (result
< 0) ? strerror(errno
) : "");
227 DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
228 smb_fname
->base_name
,
229 (result
< 0) ? "failed: " : "",
230 (result
< 0) ? strerror(errno
) : ""));
235 static int audit_rmdir(vfs_handle_struct
*handle
,
236 const struct smb_filename
*smb_fname
)
240 result
= SMB_VFS_NEXT_RMDIR(handle
, smb_fname
);
242 if (lp_syslog() > 0) {
243 syslog(audit_syslog_priority(handle
), "rmdir %s %s%s\n",
244 smb_fname
->base_name
,
245 (result
< 0) ? "failed: " : "",
246 (result
< 0) ? strerror(errno
) : "");
248 DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
249 smb_fname
->base_name
,
250 (result
< 0) ? "failed: " : "",
251 (result
< 0) ? strerror(errno
) : ""));
256 static int audit_open(vfs_handle_struct
*handle
,
257 struct smb_filename
*smb_fname
, files_struct
*fsp
,
258 int flags
, mode_t mode
)
262 result
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
264 if (lp_syslog() > 0) {
265 syslog(audit_syslog_priority(handle
), "open %s (fd %d) %s%s%s\n",
266 smb_fname
->base_name
, result
,
267 ((flags
& O_WRONLY
) || (flags
& O_RDWR
)) ? "for writing " : "",
268 (result
< 0) ? "failed: " : "",
269 (result
< 0) ? strerror(errno
) : "");
271 DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
272 smb_fname_str_dbg(smb_fname
),
273 (result
< 0) ? "failed: " : "",
274 (result
< 0) ? strerror(errno
) : ""));
279 static int audit_close(vfs_handle_struct
*handle
, files_struct
*fsp
)
283 result
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
285 if (lp_syslog() > 0) {
286 syslog(audit_syslog_priority(handle
), "close fd %d %s%s\n",
288 (result
< 0) ? "failed: " : "",
289 (result
< 0) ? strerror(errno
) : "");
291 DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
293 (result
< 0) ? "failed: " : "",
294 (result
< 0) ? strerror(errno
) : ""));
299 static int audit_rename(vfs_handle_struct
*handle
,
300 const struct smb_filename
*smb_fname_src
,
301 const struct smb_filename
*smb_fname_dst
)
305 result
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
307 if (lp_syslog() > 0) {
308 syslog(audit_syslog_priority(handle
), "rename %s -> %s %s%s\n",
309 smb_fname_src
->base_name
,
310 smb_fname_dst
->base_name
,
311 (result
< 0) ? "failed: " : "",
312 (result
< 0) ? strerror(errno
) : "");
314 DEBUG(1, ("vfs_extd_audit: rename old: %s newname: %s %s %s\n",
315 smb_fname_str_dbg(smb_fname_src
),
316 smb_fname_str_dbg(smb_fname_dst
),
317 (result
< 0) ? "failed: " : "",
318 (result
< 0) ? strerror(errno
) : ""));
323 static int audit_unlink(vfs_handle_struct
*handle
,
324 const struct smb_filename
*smb_fname
)
328 result
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
330 if (lp_syslog() > 0) {
331 syslog(audit_syslog_priority(handle
), "unlink %s %s%s\n",
332 smb_fname
->base_name
,
333 (result
< 0) ? "failed: " : "",
334 (result
< 0) ? strerror(errno
) : "");
336 DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
337 smb_fname_str_dbg(smb_fname
),
338 (result
< 0) ? "failed: " : "",
339 (result
< 0) ? strerror(errno
) : ""));
344 static int audit_chmod(vfs_handle_struct
*handle
,
345 const struct smb_filename
*smb_fname
,
350 result
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname
, mode
);
352 if (lp_syslog() > 0) {
353 syslog(audit_syslog_priority(handle
), "chmod %s mode 0x%x %s%s\n",
354 smb_fname
->base_name
, mode
,
355 (result
< 0) ? "failed: " : "",
356 (result
< 0) ? strerror(errno
) : "");
358 DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
359 smb_fname
->base_name
, (unsigned int)mode
,
360 (result
< 0) ? "failed: " : "",
361 (result
< 0) ? strerror(errno
) : ""));
366 static int audit_fchmod(vfs_handle_struct
*handle
, files_struct
*fsp
, mode_t mode
)
370 result
= SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
372 if (lp_syslog() > 0) {
373 syslog(audit_syslog_priority(handle
), "fchmod %s mode 0x%x %s%s\n",
374 fsp
->fsp_name
->base_name
, mode
,
375 (result
< 0) ? "failed: " : "",
376 (result
< 0) ? strerror(errno
) : "");
378 DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
379 fsp_str_dbg(fsp
), (unsigned int)mode
,
380 (result
< 0) ? "failed: " : "",
381 (result
< 0) ? strerror(errno
) : ""));
386 static struct vfs_fn_pointers vfs_extd_audit_fns
= {
387 .connect_fn
= audit_connect
,
388 .disconnect_fn
= audit_disconnect
,
389 .opendir_fn
= audit_opendir
,
390 .mkdir_fn
= audit_mkdir
,
391 .rmdir_fn
= audit_rmdir
,
392 .open_fn
= audit_open
,
393 .close_fn
= audit_close
,
394 .rename_fn
= audit_rename
,
395 .unlink_fn
= audit_unlink
,
396 .chmod_fn
= audit_chmod
,
397 .fchmod_fn
= audit_fchmod
,
401 NTSTATUS
vfs_extd_audit_init(TALLOC_CTX
*ctx
)
403 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
404 "extd_audit", &vfs_extd_audit_fns
);
406 if (!NT_STATUS_IS_OK(ret
))
409 vfs_extd_audit_debug_level
= debug_add_class("extd_audit");
410 if (vfs_extd_audit_debug_level
== -1) {
411 vfs_extd_audit_debug_level
= DBGC_VFS
;
412 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
414 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level
));