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 int audit_mkdirat(vfs_handle_struct
*handle
,
191 struct files_struct
*dirfsp
,
192 const struct smb_filename
*smb_fname
,
195 struct smb_filename
*full_fname
= NULL
;
198 full_fname
= full_path_from_dirfsp_atname(talloc_tos(),
201 if (full_fname
== NULL
) {
206 result
= SMB_VFS_NEXT_MKDIRAT(handle
,
211 if (lp_syslog() > 0) {
212 syslog(audit_syslog_priority(handle
), "mkdirat %s %s%s\n",
213 full_fname
->base_name
,
214 (result
< 0) ? "failed: " : "",
215 (result
< 0) ? strerror(errno
) : "");
217 DEBUG(0, ("vfs_extd_audit: mkdirat %s %s %s\n",
218 full_fname
->base_name
,
219 (result
< 0) ? "failed: " : "",
220 (result
< 0) ? strerror(errno
) : ""));
222 TALLOC_FREE(full_fname
);
226 static int audit_openat(vfs_handle_struct
*handle
,
227 const struct files_struct
*dirfsp
,
228 const struct smb_filename
*smb_fname
,
235 ret
= SMB_VFS_NEXT_OPENAT(handle
, dirfsp
, smb_fname
, fsp
, flags
, mode
);
237 if (lp_syslog() > 0) {
238 syslog(audit_syslog_priority(handle
),
239 "openat %s/%s (fd %d) %s%s%s\n",
240 smb_fname_str_dbg(fsp
->fsp_name
),
241 smb_fname
->base_name
,
243 ((flags
& O_WRONLY
) || (flags
& O_RDWR
)) ?
245 (ret
< 0) ? "failed: " : "",
246 (ret
< 0) ? strerror(errno
) : "");
248 DEBUG(2, ("vfs_extd_audit: open %s/%s %s %s\n",
249 smb_fname_str_dbg(fsp
->fsp_name
),
250 smb_fname_str_dbg(smb_fname
),
251 (ret
< 0) ? "failed: " : "",
252 (ret
< 0) ? strerror(errno
) : ""));
257 static int audit_close(vfs_handle_struct
*handle
, files_struct
*fsp
)
261 result
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
263 if (lp_syslog() > 0) {
264 syslog(audit_syslog_priority(handle
), "close fd %d %s%s\n",
265 fsp_get_pathref_fd(fsp
),
266 (result
< 0) ? "failed: " : "",
267 (result
< 0) ? strerror(errno
) : "");
269 DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
270 fsp_get_pathref_fd(fsp
),
271 (result
< 0) ? "failed: " : "",
272 (result
< 0) ? strerror(errno
) : ""));
277 static int audit_renameat(vfs_handle_struct
*handle
,
278 files_struct
*srcfsp
,
279 const struct smb_filename
*smb_fname_src
,
280 files_struct
*dstfsp
,
281 const struct smb_filename
*smb_fname_dst
)
283 struct smb_filename
*full_fname_src
= NULL
;
284 struct smb_filename
*full_fname_dst
= NULL
;
288 full_fname_src
= full_path_from_dirfsp_atname(talloc_tos(),
291 if (full_fname_src
== NULL
) {
296 full_fname_dst
= full_path_from_dirfsp_atname(talloc_tos(),
299 if (full_fname_dst
== NULL
) {
300 TALLOC_FREE(full_fname_src
);
305 result
= SMB_VFS_NEXT_RENAMEAT(handle
,
313 if (lp_syslog() > 0) {
314 syslog(audit_syslog_priority(handle
), "renameat %s -> %s %s%s\n",
315 full_fname_src
->base_name
,
316 full_fname_dst
->base_name
,
317 (result
< 0) ? "failed: " : "",
318 (result
< 0) ? strerror(saved_errno
) : "");
320 DEBUG(1, ("vfs_extd_audit: renameat old: %s newname: %s %s %s\n",
321 smb_fname_str_dbg(full_fname_src
),
322 smb_fname_str_dbg(full_fname_dst
),
323 (result
< 0) ? "failed: " : "",
324 (result
< 0) ? strerror(saved_errno
) : ""));
326 TALLOC_FREE(full_fname_src
);
327 TALLOC_FREE(full_fname_dst
);
335 static int audit_unlinkat(vfs_handle_struct
*handle
,
336 struct files_struct
*dirfsp
,
337 const struct smb_filename
*smb_fname
,
340 struct smb_filename
*full_fname
= NULL
;
343 full_fname
= full_path_from_dirfsp_atname(talloc_tos(),
346 if (full_fname
== NULL
) {
350 result
= SMB_VFS_NEXT_UNLINKAT(handle
,
355 if (lp_syslog() > 0) {
356 syslog(audit_syslog_priority(handle
), "unlinkat %s %s%s\n",
357 full_fname
->base_name
,
358 (result
< 0) ? "failed: " : "",
359 (result
< 0) ? strerror(errno
) : "");
361 DBG_ERR("unlinkat %s %s %s\n",
362 smb_fname_str_dbg(full_fname
),
363 (result
< 0) ? "failed: " : "",
364 (result
< 0) ? strerror(errno
) : "");
366 TALLOC_FREE(full_fname
);
370 static int audit_fchmod(vfs_handle_struct
*handle
, files_struct
*fsp
, mode_t mode
)
374 result
= SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
376 if (lp_syslog() > 0) {
377 syslog(audit_syslog_priority(handle
), "fchmod %s mode 0x%x %s%s\n",
378 fsp
->fsp_name
->base_name
, mode
,
379 (result
< 0) ? "failed: " : "",
380 (result
< 0) ? strerror(errno
) : "");
382 DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
383 fsp_str_dbg(fsp
), (unsigned int)mode
,
384 (result
< 0) ? "failed: " : "",
385 (result
< 0) ? strerror(errno
) : ""));
390 static struct vfs_fn_pointers vfs_extd_audit_fns
= {
391 .connect_fn
= audit_connect
,
392 .disconnect_fn
= audit_disconnect
,
393 .mkdirat_fn
= audit_mkdirat
,
394 .openat_fn
= audit_openat
,
395 .close_fn
= audit_close
,
396 .renameat_fn
= audit_renameat
,
397 .unlinkat_fn
= audit_unlinkat
,
398 .fchmod_fn
= audit_fchmod
,
402 NTSTATUS
vfs_extd_audit_init(TALLOC_CTX
*ctx
)
404 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
405 "extd_audit", &vfs_extd_audit_fns
);
407 if (!NT_STATUS_IS_OK(ret
))
410 vfs_extd_audit_debug_level
= debug_add_class("extd_audit");
411 if (vfs_extd_audit_debug_level
== -1) {
412 vfs_extd_audit_debug_level
= DBGC_VFS
;
413 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
415 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level
));