vfs: add acl type arg to SMB_VFS_SYS_ACL_SET_FD()
[samba.git] / source3 / modules / vfs_full_audit.c
1 /* 
2  * Auditing VFS module for samba.  Log selected file operations to syslog
3  * facility.
4  *
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
9  * Copyright (C) Volker Lendecke, 2004
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *  
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *  
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, see <http://www.gnu.org/licenses/>.
23  */
24
25 /*
26  * This module implements parseable logging for all Samba VFS operations.
27  *
28  * You use it as follows:
29  *
30  * [tmp]
31  * path = /tmp
32  * vfs objects = full_audit
33  * full_audit:prefix = %u|%I
34  * full_audit:success = open opendir create_file
35  * full_audit:failure = all
36  *
37  * vfs op can be "all" which means log all operations.
38  * vfs op can be "none" which means no logging.
39  *
40  * This leads to syslog entries of the form:
41  * smbd_audit: nobody|192.168.234.1|opendir|ok|/tmp
42  * smbd_audit: nobody|192.168.234.1|create_file|fail (No such file or directory)|0x1|file|open|/ts/doesNotExist
43  * smbd_audit: nobody|192.168.234.1|open|ok|w|/tmp/file.txt
44  * smbd_audit: nobody|192.168.234.1|create_file|ok|0x3|file|open|/tmp/file.txt
45  *
46  * where "nobody" is the connected username and "192.168.234.1" is the
47  * client's IP address. 
48  *
49  * Options:
50  *
51  * prefix: A macro expansion template prepended to the syslog entry.
52  *
53  * success: A list of VFS operations for which a successful completion should
54  * be logged. Defaults to no logging at all. The special operation "all" logs
55  * - you guessed it - everything.
56  *
57  * failure: A list of VFS operations for which failure to complete should be
58  * logged. Defaults to logging everything.
59  */
60
61
62 #include "includes.h"
63 #include "system/filesys.h"
64 #include "system/syslog.h"
65 #include "smbd/smbd.h"
66 #include "../librpc/gen_ndr/ndr_netlogon.h"
67 #include "auth.h"
68 #include "ntioctl.h"
69 #include "lib/param/loadparm.h"
70 #include "lib/util/bitmap.h"
71 #include "lib/util/tevent_unix.h"
72 #include "libcli/security/sddl.h"
73 #include "passdb/machine_sid.h"
74 #include "lib/util/tevent_ntstatus.h"
75 #include "lib/util/string_wrappers.h"
76
77 static int vfs_full_audit_debug_level = DBGC_VFS;
78
79 struct vfs_full_audit_private_data {
80         struct bitmap *success_ops;
81         struct bitmap *failure_ops;
82         int syslog_facility;
83         int syslog_priority;
84         bool log_secdesc;
85         bool do_syslog;
86 };
87
88 #undef DBGC_CLASS
89 #define DBGC_CLASS vfs_full_audit_debug_level
90
91 typedef enum _vfs_op_type {
92         SMB_VFS_OP_NOOP = -1,
93
94         /* Disk operations */
95
96         SMB_VFS_OP_CONNECT = 0,
97         SMB_VFS_OP_DISCONNECT,
98         SMB_VFS_OP_DISK_FREE,
99         SMB_VFS_OP_GET_QUOTA,
100         SMB_VFS_OP_SET_QUOTA,
101         SMB_VFS_OP_GET_SHADOW_COPY_DATA,
102         SMB_VFS_OP_STATVFS,
103         SMB_VFS_OP_FS_CAPABILITIES,
104         SMB_VFS_OP_GET_DFS_REFERRALS,
105         SMB_VFS_OP_CREATE_DFS_PATHAT,
106         SMB_VFS_OP_READ_DFS_PATHAT,
107
108         /* Directory operations */
109
110         SMB_VFS_OP_FDOPENDIR,
111         SMB_VFS_OP_READDIR,
112         SMB_VFS_OP_SEEKDIR,
113         SMB_VFS_OP_TELLDIR,
114         SMB_VFS_OP_REWINDDIR,
115         SMB_VFS_OP_MKDIRAT,
116         SMB_VFS_OP_CLOSEDIR,
117
118         /* File operations */
119
120         SMB_VFS_OP_OPEN,
121         SMB_VFS_OP_OPENAT,
122         SMB_VFS_OP_CREATE_FILE,
123         SMB_VFS_OP_CLOSE,
124         SMB_VFS_OP_READ,
125         SMB_VFS_OP_PREAD,
126         SMB_VFS_OP_PREAD_SEND,
127         SMB_VFS_OP_PREAD_RECV,
128         SMB_VFS_OP_WRITE,
129         SMB_VFS_OP_PWRITE,
130         SMB_VFS_OP_PWRITE_SEND,
131         SMB_VFS_OP_PWRITE_RECV,
132         SMB_VFS_OP_LSEEK,
133         SMB_VFS_OP_SENDFILE,
134         SMB_VFS_OP_RECVFILE,
135         SMB_VFS_OP_RENAMEAT,
136         SMB_VFS_OP_FSYNC,
137         SMB_VFS_OP_FSYNC_SEND,
138         SMB_VFS_OP_FSYNC_RECV,
139         SMB_VFS_OP_STAT,
140         SMB_VFS_OP_FSTAT,
141         SMB_VFS_OP_LSTAT,
142         SMB_VFS_OP_GET_ALLOC_SIZE,
143         SMB_VFS_OP_UNLINKAT,
144         SMB_VFS_OP_CHMOD,
145         SMB_VFS_OP_FCHMOD,
146         SMB_VFS_OP_FCHOWN,
147         SMB_VFS_OP_LCHOWN,
148         SMB_VFS_OP_CHDIR,
149         SMB_VFS_OP_GETWD,
150         SMB_VFS_OP_NTIMES,
151         SMB_VFS_OP_FTRUNCATE,
152         SMB_VFS_OP_FALLOCATE,
153         SMB_VFS_OP_LOCK,
154         SMB_VFS_OP_KERNEL_FLOCK,
155         SMB_VFS_OP_FCNTL,
156         SMB_VFS_OP_LINUX_SETLEASE,
157         SMB_VFS_OP_GETLOCK,
158         SMB_VFS_OP_SYMLINKAT,
159         SMB_VFS_OP_READLINKAT,
160         SMB_VFS_OP_LINKAT,
161         SMB_VFS_OP_MKNODAT,
162         SMB_VFS_OP_REALPATH,
163         SMB_VFS_OP_CHFLAGS,
164         SMB_VFS_OP_FILE_ID_CREATE,
165         SMB_VFS_OP_FS_FILE_ID,
166         SMB_VFS_OP_STREAMINFO,
167         SMB_VFS_OP_GET_REAL_FILENAME,
168         SMB_VFS_OP_CONNECTPATH,
169         SMB_VFS_OP_BRL_LOCK_WINDOWS,
170         SMB_VFS_OP_BRL_UNLOCK_WINDOWS,
171         SMB_VFS_OP_STRICT_LOCK_CHECK,
172         SMB_VFS_OP_TRANSLATE_NAME,
173         SMB_VFS_OP_FSCTL,
174         SMB_VFS_OP_OFFLOAD_READ_SEND,
175         SMB_VFS_OP_OFFLOAD_READ_RECV,
176         SMB_VFS_OP_OFFLOAD_WRITE_SEND,
177         SMB_VFS_OP_OFFLOAD_WRITE_RECV,
178         SMB_VFS_OP_FGET_COMPRESSION,
179         SMB_VFS_OP_SET_COMPRESSION,
180         SMB_VFS_OP_SNAP_CHECK_PATH,
181         SMB_VFS_OP_SNAP_CREATE,
182         SMB_VFS_OP_SNAP_DELETE,
183
184         /* DOS attribute operations. */
185         SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
186         SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
187         SMB_VFS_OP_FGET_DOS_ATTRIBUTES,
188         SMB_VFS_OP_SET_DOS_ATTRIBUTES,
189         SMB_VFS_OP_FSET_DOS_ATTRIBUTES,
190
191         /* NT ACL operations. */
192
193         SMB_VFS_OP_FGET_NT_ACL,
194         SMB_VFS_OP_GET_NT_ACL_AT,
195         SMB_VFS_OP_FSET_NT_ACL,
196         SMB_VFS_OP_AUDIT_FILE,
197
198         /* POSIX ACL operations. */
199
200         SMB_VFS_OP_SYS_ACL_GET_FILE,
201         SMB_VFS_OP_SYS_ACL_GET_FD,
202         SMB_VFS_OP_SYS_ACL_BLOB_GET_FILE,
203         SMB_VFS_OP_SYS_ACL_BLOB_GET_FD,
204         SMB_VFS_OP_SYS_ACL_SET_FILE,
205         SMB_VFS_OP_SYS_ACL_SET_FD,
206         SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
207
208         /* EA operations. */
209         SMB_VFS_OP_GETXATTR,
210         SMB_VFS_OP_GETXATTRAT_SEND,
211         SMB_VFS_OP_GETXATTRAT_RECV,
212         SMB_VFS_OP_FGETXATTR,
213         SMB_VFS_OP_LISTXATTR,
214         SMB_VFS_OP_FLISTXATTR,
215         SMB_VFS_OP_REMOVEXATTR,
216         SMB_VFS_OP_FREMOVEXATTR,
217         SMB_VFS_OP_SETXATTR,
218         SMB_VFS_OP_FSETXATTR,
219
220         /* aio operations */
221         SMB_VFS_OP_AIO_FORCE,
222
223         /* offline operations */
224         SMB_VFS_OP_IS_OFFLINE,
225         SMB_VFS_OP_SET_OFFLINE,
226
227         /* Durable handle operations. */
228         SMB_VFS_OP_DURABLE_COOKIE,
229         SMB_VFS_OP_DURABLE_DISCONNECT,
230         SMB_VFS_OP_DURABLE_RECONNECT,
231
232         SMB_VFS_OP_READDIR_ATTR,
233
234         /* This should always be last enum value */
235
236         SMB_VFS_OP_LAST
237 } vfs_op_type;
238
239 /* The following array *must* be in the same order as defined in vfs_op_type */
240
241 static struct {
242         vfs_op_type type;
243         const char *name;
244 } vfs_op_names[] = {
245         { SMB_VFS_OP_CONNECT,   "connect" },
246         { SMB_VFS_OP_DISCONNECT,        "disconnect" },
247         { SMB_VFS_OP_DISK_FREE, "disk_free" },
248         { SMB_VFS_OP_GET_QUOTA, "get_quota" },
249         { SMB_VFS_OP_SET_QUOTA, "set_quota" },
250         { SMB_VFS_OP_GET_SHADOW_COPY_DATA,      "get_shadow_copy_data" },
251         { SMB_VFS_OP_STATVFS,   "statvfs" },
252         { SMB_VFS_OP_FS_CAPABILITIES,   "fs_capabilities" },
253         { SMB_VFS_OP_GET_DFS_REFERRALS, "get_dfs_referrals" },
254         { SMB_VFS_OP_CREATE_DFS_PATHAT, "create_dfs_pathat" },
255         { SMB_VFS_OP_READ_DFS_PATHAT,   "read_dfs_pathat" },
256         { SMB_VFS_OP_FDOPENDIR, "fdopendir" },
257         { SMB_VFS_OP_READDIR,   "readdir" },
258         { SMB_VFS_OP_SEEKDIR,   "seekdir" },
259         { SMB_VFS_OP_TELLDIR,   "telldir" },
260         { SMB_VFS_OP_REWINDDIR, "rewinddir" },
261         { SMB_VFS_OP_MKDIRAT,   "mkdirat" },
262         { SMB_VFS_OP_CLOSEDIR,  "closedir" },
263         { SMB_VFS_OP_OPEN,      "open" },
264         { SMB_VFS_OP_OPENAT,    "openat" },
265         { SMB_VFS_OP_CREATE_FILE, "create_file" },
266         { SMB_VFS_OP_CLOSE,     "close" },
267         { SMB_VFS_OP_READ,      "read" },
268         { SMB_VFS_OP_PREAD,     "pread" },
269         { SMB_VFS_OP_PREAD_SEND,        "pread_send" },
270         { SMB_VFS_OP_PREAD_RECV,        "pread_recv" },
271         { SMB_VFS_OP_WRITE,     "write" },
272         { SMB_VFS_OP_PWRITE,    "pwrite" },
273         { SMB_VFS_OP_PWRITE_SEND,       "pwrite_send" },
274         { SMB_VFS_OP_PWRITE_RECV,       "pwrite_recv" },
275         { SMB_VFS_OP_LSEEK,     "lseek" },
276         { SMB_VFS_OP_SENDFILE,  "sendfile" },
277         { SMB_VFS_OP_RECVFILE,  "recvfile" },
278         { SMB_VFS_OP_RENAMEAT,  "renameat" },
279         { SMB_VFS_OP_FSYNC,     "fsync" },
280         { SMB_VFS_OP_FSYNC_SEND,        "fsync_send" },
281         { SMB_VFS_OP_FSYNC_RECV,        "fsync_recv" },
282         { SMB_VFS_OP_STAT,      "stat" },
283         { SMB_VFS_OP_FSTAT,     "fstat" },
284         { SMB_VFS_OP_LSTAT,     "lstat" },
285         { SMB_VFS_OP_GET_ALLOC_SIZE,    "get_alloc_size" },
286         { SMB_VFS_OP_UNLINKAT,  "unlinkat" },
287         { SMB_VFS_OP_CHMOD,     "chmod" },
288         { SMB_VFS_OP_FCHMOD,    "fchmod" },
289         { SMB_VFS_OP_FCHOWN,    "fchown" },
290         { SMB_VFS_OP_LCHOWN,    "lchown" },
291         { SMB_VFS_OP_CHDIR,     "chdir" },
292         { SMB_VFS_OP_GETWD,     "getwd" },
293         { SMB_VFS_OP_NTIMES,    "ntimes" },
294         { SMB_VFS_OP_FTRUNCATE, "ftruncate" },
295         { SMB_VFS_OP_FALLOCATE,"fallocate" },
296         { SMB_VFS_OP_LOCK,      "lock" },
297         { SMB_VFS_OP_KERNEL_FLOCK,      "kernel_flock" },
298         { SMB_VFS_OP_FCNTL,     "fcntl" },
299         { SMB_VFS_OP_LINUX_SETLEASE, "linux_setlease" },
300         { SMB_VFS_OP_GETLOCK,   "getlock" },
301         { SMB_VFS_OP_SYMLINKAT, "symlinkat" },
302         { SMB_VFS_OP_READLINKAT,"readlinkat" },
303         { SMB_VFS_OP_LINKAT,    "linkat" },
304         { SMB_VFS_OP_MKNODAT,   "mknodat" },
305         { SMB_VFS_OP_REALPATH,  "realpath" },
306         { SMB_VFS_OP_CHFLAGS,   "chflags" },
307         { SMB_VFS_OP_FILE_ID_CREATE,    "file_id_create" },
308         { SMB_VFS_OP_FS_FILE_ID,        "fs_file_id" },
309         { SMB_VFS_OP_STREAMINFO,        "streaminfo" },
310         { SMB_VFS_OP_GET_REAL_FILENAME, "get_real_filename" },
311         { SMB_VFS_OP_CONNECTPATH,       "connectpath" },
312         { SMB_VFS_OP_BRL_LOCK_WINDOWS,  "brl_lock_windows" },
313         { SMB_VFS_OP_BRL_UNLOCK_WINDOWS, "brl_unlock_windows" },
314         { SMB_VFS_OP_STRICT_LOCK_CHECK, "strict_lock_check" },
315         { SMB_VFS_OP_TRANSLATE_NAME,    "translate_name" },
316         { SMB_VFS_OP_FSCTL,             "fsctl" },
317         { SMB_VFS_OP_OFFLOAD_READ_SEND, "offload_read_send" },
318         { SMB_VFS_OP_OFFLOAD_READ_RECV, "offload_read_recv" },
319         { SMB_VFS_OP_OFFLOAD_WRITE_SEND,        "offload_write_send" },
320         { SMB_VFS_OP_OFFLOAD_WRITE_RECV,        "offload_write_recv" },
321         { SMB_VFS_OP_FGET_COMPRESSION,  "fget_compression" },
322         { SMB_VFS_OP_SET_COMPRESSION,   "set_compression" },
323         { SMB_VFS_OP_SNAP_CHECK_PATH, "snap_check_path" },
324         { SMB_VFS_OP_SNAP_CREATE, "snap_create" },
325         { SMB_VFS_OP_SNAP_DELETE, "snap_delete" },
326         { SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND, "get_dos_attributes_send" },
327         { SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV, "get_dos_attributes_recv" },
328         { SMB_VFS_OP_FGET_DOS_ATTRIBUTES, "fget_dos_attributes" },
329         { SMB_VFS_OP_SET_DOS_ATTRIBUTES, "set_dos_attributes" },
330         { SMB_VFS_OP_FSET_DOS_ATTRIBUTES, "fset_dos_attributes" },
331         { SMB_VFS_OP_FGET_NT_ACL,       "fget_nt_acl" },
332         { SMB_VFS_OP_GET_NT_ACL_AT,     "get_nt_acl_at" },
333         { SMB_VFS_OP_FSET_NT_ACL,       "fset_nt_acl" },
334         { SMB_VFS_OP_AUDIT_FILE,        "audit_file" },
335         { SMB_VFS_OP_SYS_ACL_GET_FILE,  "sys_acl_get_file" },
336         { SMB_VFS_OP_SYS_ACL_GET_FD,    "sys_acl_get_fd" },
337         { SMB_VFS_OP_SYS_ACL_BLOB_GET_FILE,     "sys_acl_blob_get_file" },
338         { SMB_VFS_OP_SYS_ACL_BLOB_GET_FD,       "sys_acl_blob_get_fd" },
339         { SMB_VFS_OP_SYS_ACL_SET_FILE,  "sys_acl_set_file" },
340         { SMB_VFS_OP_SYS_ACL_SET_FD,    "sys_acl_set_fd" },
341         { SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,   "sys_acl_delete_def_file" },
342         { SMB_VFS_OP_GETXATTR,  "getxattr" },
343         { SMB_VFS_OP_GETXATTRAT_SEND, "getxattrat_send" },
344         { SMB_VFS_OP_GETXATTRAT_RECV, "getxattrat_recv" },
345         { SMB_VFS_OP_FGETXATTR, "fgetxattr" },
346         { SMB_VFS_OP_LISTXATTR, "listxattr" },
347         { SMB_VFS_OP_FLISTXATTR,        "flistxattr" },
348         { SMB_VFS_OP_REMOVEXATTR,       "removexattr" },
349         { SMB_VFS_OP_FREMOVEXATTR,      "fremovexattr" },
350         { SMB_VFS_OP_SETXATTR,  "setxattr" },
351         { SMB_VFS_OP_FSETXATTR, "fsetxattr" },
352         { SMB_VFS_OP_AIO_FORCE, "aio_force" },
353         { SMB_VFS_OP_IS_OFFLINE, "is_offline" },
354         { SMB_VFS_OP_SET_OFFLINE, "set_offline" },
355         { SMB_VFS_OP_DURABLE_COOKIE, "durable_cookie" },
356         { SMB_VFS_OP_DURABLE_DISCONNECT, "durable_disconnect" },
357         { SMB_VFS_OP_DURABLE_RECONNECT, "durable_reconnect" },
358         { SMB_VFS_OP_READDIR_ATTR,      "readdir_attr" },
359         { SMB_VFS_OP_LAST, NULL }
360 };
361
362 static int audit_syslog_facility(vfs_handle_struct *handle)
363 {
364         static const struct enum_list enum_log_facilities[] = {
365 #ifdef LOG_AUTH
366                 { LOG_AUTH,             "AUTH" },
367 #endif
368 #ifdef LOG_AUTHPRIV
369                 { LOG_AUTHPRIV,         "AUTHPRIV" },
370 #endif
371 #ifdef LOG_AUDIT
372                 { LOG_AUDIT,            "AUDIT" },
373 #endif
374 #ifdef LOG_CONSOLE
375                 { LOG_CONSOLE,          "CONSOLE" },
376 #endif
377 #ifdef LOG_CRON
378                 { LOG_CRON,             "CRON" },
379 #endif
380 #ifdef LOG_DAEMON
381                 { LOG_DAEMON,           "DAEMON" },
382 #endif
383 #ifdef LOG_FTP
384                 { LOG_FTP,              "FTP" },
385 #endif
386 #ifdef LOG_INSTALL
387                 { LOG_INSTALL,          "INSTALL" },
388 #endif
389 #ifdef LOG_KERN
390                 { LOG_KERN,             "KERN" },
391 #endif
392 #ifdef LOG_LAUNCHD
393                 { LOG_LAUNCHD,          "LAUNCHD" },
394 #endif
395 #ifdef LOG_LFMT
396                 { LOG_LFMT,             "LFMT" },
397 #endif
398 #ifdef LOG_LPR
399                 { LOG_LPR,              "LPR" },
400 #endif
401 #ifdef LOG_MAIL
402                 { LOG_MAIL,             "MAIL" },
403 #endif
404 #ifdef LOG_MEGASAFE
405                 { LOG_MEGASAFE,         "MEGASAFE" },
406 #endif
407 #ifdef LOG_NETINFO
408                 { LOG_NETINFO,          "NETINFO" },
409 #endif
410 #ifdef LOG_NEWS
411                 { LOG_NEWS,             "NEWS" },
412 #endif
413 #ifdef LOG_NFACILITIES
414                 { LOG_NFACILITIES,      "NFACILITIES" },
415 #endif
416 #ifdef LOG_NTP
417                 { LOG_NTP,              "NTP" },
418 #endif
419 #ifdef LOG_RAS
420                 { LOG_RAS,              "RAS" },
421 #endif
422 #ifdef LOG_REMOTEAUTH
423                 { LOG_REMOTEAUTH,       "REMOTEAUTH" },
424 #endif
425 #ifdef LOG_SECURITY
426                 { LOG_SECURITY,         "SECURITY" },
427 #endif
428 #ifdef LOG_SYSLOG
429                 { LOG_SYSLOG,           "SYSLOG" },
430 #endif
431 #ifdef LOG_USER
432                 { LOG_USER,             "USER" },
433 #endif
434 #ifdef LOG_UUCP
435                 { LOG_UUCP,             "UUCP" },
436 #endif
437                 { LOG_LOCAL0,           "LOCAL0" },
438                 { LOG_LOCAL1,           "LOCAL1" },
439                 { LOG_LOCAL2,           "LOCAL2" },
440                 { LOG_LOCAL3,           "LOCAL3" },
441                 { LOG_LOCAL4,           "LOCAL4" },
442                 { LOG_LOCAL5,           "LOCAL5" },
443                 { LOG_LOCAL6,           "LOCAL6" },
444                 { LOG_LOCAL7,           "LOCAL7" },
445                 { -1,                   NULL }
446         };
447
448         int facility;
449
450         facility = lp_parm_enum(SNUM(handle->conn), "full_audit", "facility", enum_log_facilities, LOG_USER);
451
452         return facility;
453 }
454
455 static int audit_syslog_priority(vfs_handle_struct *handle)
456 {
457         static const struct enum_list enum_log_priorities[] = {
458                 { LOG_EMERG, "EMERG" },
459                 { LOG_ALERT, "ALERT" },
460                 { LOG_CRIT, "CRIT" },
461                 { LOG_ERR, "ERR" },
462                 { LOG_WARNING, "WARNING" },
463                 { LOG_NOTICE, "NOTICE" },
464                 { LOG_INFO, "INFO" },
465                 { LOG_DEBUG, "DEBUG" },
466                 { -1, NULL }
467         };
468
469         int priority;
470
471         priority = lp_parm_enum(SNUM(handle->conn), "full_audit", "priority",
472                                 enum_log_priorities, LOG_NOTICE);
473         if (priority == -1) {
474                 priority = LOG_WARNING;
475         }
476
477         return priority;
478 }
479
480 static char *audit_prefix(TALLOC_CTX *ctx, connection_struct *conn)
481 {
482         const struct loadparm_substitution *lp_sub =
483                 loadparm_s3_global_substitution();
484         char *prefix = NULL;
485         char *result;
486
487         prefix = talloc_strdup(ctx,
488                         lp_parm_const_string(SNUM(conn), "full_audit",
489                                              "prefix", "%u|%I"));
490         if (!prefix) {
491                 return NULL;
492         }
493         result = talloc_sub_full(ctx,
494                         lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
495                         conn->session_info->unix_info->unix_name,
496                         conn->connectpath,
497                         conn->session_info->unix_token->gid,
498                         conn->session_info->unix_info->sanitized_username,
499                         conn->session_info->info->domain_name,
500                         prefix);
501         TALLOC_FREE(prefix);
502         return result;
503 }
504
505 static bool log_success(struct vfs_full_audit_private_data *pd, vfs_op_type op)
506 {
507         if (pd->success_ops == NULL) {
508                 return True;
509         }
510
511         return bitmap_query(pd->success_ops, op);
512 }
513
514 static bool log_failure(struct vfs_full_audit_private_data *pd, vfs_op_type op)
515 {
516         if (pd->failure_ops == NULL)
517                 return True;
518
519         return bitmap_query(pd->failure_ops, op);
520 }
521
522 static struct bitmap *init_bitmap(TALLOC_CTX *mem_ctx, const char **ops)
523 {
524         struct bitmap *bm;
525
526         if (ops == NULL) {
527                 return NULL;
528         }
529
530         bm = bitmap_talloc(mem_ctx, SMB_VFS_OP_LAST);
531         if (bm == NULL) {
532                 DEBUG(0, ("Could not alloc bitmap -- "
533                           "defaulting to logging everything\n"));
534                 return NULL;
535         }
536
537         for (; *ops != NULL; ops += 1) {
538                 int i;
539                 bool neg = false;
540                 const char *op;
541
542                 if (strequal(*ops, "all")) {
543                         for (i=0; i<SMB_VFS_OP_LAST; i++) {
544                                 bitmap_set(bm, i);
545                         }
546                         continue;
547                 }
548
549                 if (strequal(*ops, "none")) {
550                         break;
551                 }
552
553                 op = ops[0];
554                 if (op[0] == '!') {
555                         neg = true;
556                         op += 1;
557                 }
558
559                 for (i=0; i<SMB_VFS_OP_LAST; i++) {
560                         if ((vfs_op_names[i].name == NULL)
561                          || (vfs_op_names[i].type != i)) {
562                                 smb_panic("vfs_full_audit.c: name table not "
563                                           "in sync with vfs_op_type enums\n");
564                         }
565                         if (strequal(op, vfs_op_names[i].name)) {
566                                 if (neg) {
567                                         bitmap_clear(bm, i);
568                                 } else {
569                                         bitmap_set(bm, i);
570                                 }
571                                 break;
572                         }
573                 }
574                 if (i == SMB_VFS_OP_LAST) {
575                         DEBUG(0, ("Could not find opname %s, logging all\n",
576                                   *ops));
577                         TALLOC_FREE(bm);
578                         return NULL;
579                 }
580         }
581         return bm;
582 }
583
584 static const char *audit_opname(vfs_op_type op)
585 {
586         if (op >= SMB_VFS_OP_LAST)
587                 return "INVALID VFS OP";
588         return vfs_op_names[op].name;
589 }
590
591 static TALLOC_CTX *tmp_do_log_ctx;
592 /*
593  * Get us a temporary talloc context usable just for DEBUG arguments
594  */
595 static TALLOC_CTX *do_log_ctx(void)
596 {
597         if (tmp_do_log_ctx == NULL) {
598                 tmp_do_log_ctx = talloc_named_const(NULL, 0, "do_log_ctx");
599         }
600         return tmp_do_log_ctx;
601 }
602
603 static void do_log(vfs_op_type op, bool success, vfs_handle_struct *handle,
604                    const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
605
606 static void do_log(vfs_op_type op, bool success, vfs_handle_struct *handle,
607                    const char *format, ...)
608 {
609         struct vfs_full_audit_private_data *pd;
610         fstring err_msg;
611         char *audit_pre = NULL;
612         va_list ap;
613         char *op_msg = NULL;
614
615         SMB_VFS_HANDLE_GET_DATA(handle, pd,
616                                 struct vfs_full_audit_private_data,
617                                 return;);
618
619         if (success && (!log_success(pd, op)))
620                 goto out;
621
622         if (!success && (!log_failure(pd, op)))
623                 goto out;
624
625         if (success)
626                 fstrcpy(err_msg, "ok");
627         else
628                 fstr_sprintf(err_msg, "fail (%s)", strerror(errno));
629
630         va_start(ap, format);
631         op_msg = talloc_vasprintf(talloc_tos(), format, ap);
632         va_end(ap);
633
634         if (!op_msg) {
635                 goto out;
636         }
637
638         audit_pre = audit_prefix(talloc_tos(), handle->conn);
639
640         if (pd->do_syslog) {
641                 int priority;
642
643                 /*
644                  * Specify the facility to interoperate with other syslog
645                  * callers (smbd for example).
646                  */
647                 priority = pd->syslog_priority | pd->syslog_facility;
648
649                 syslog(priority, "%s|%s|%s|%s\n",
650                        audit_pre ? audit_pre : "",
651                        audit_opname(op), err_msg, op_msg);
652         } else {
653                 DEBUG(1, ("%s|%s|%s|%s\n",
654                           audit_pre ? audit_pre : "",
655                           audit_opname(op), err_msg, op_msg));
656         }
657  out:
658         TALLOC_FREE(audit_pre);
659         TALLOC_FREE(op_msg);
660         TALLOC_FREE(tmp_do_log_ctx);
661 }
662
663 /**
664  * Return a string using the do_log_ctx()
665  */
666 static const char *smb_fname_str_do_log(struct connection_struct *conn,
667                                 const struct smb_filename *smb_fname)
668 {
669         char *fname = NULL;
670         NTSTATUS status;
671
672         if (smb_fname == NULL) {
673                 return "";
674         }
675
676         if (smb_fname->base_name[0] != '/') {
677                 char *abs_name = NULL;
678                 struct smb_filename *fname_copy = cp_smb_filename(
679                                                         do_log_ctx(),
680                                                         smb_fname);
681                 if (fname_copy == NULL) {
682                         return "";
683                 }
684
685                 if (!ISDOT(smb_fname->base_name)) {
686                         abs_name = talloc_asprintf(do_log_ctx(),
687                                         "%s/%s",
688                                         conn->cwd_fsp->fsp_name->base_name,
689                                         smb_fname->base_name);
690                 } else {
691                         abs_name = talloc_strdup(do_log_ctx(),
692                                         conn->cwd_fsp->fsp_name->base_name);
693                 }
694                 if (abs_name == NULL) {
695                         return "";
696                 }
697                 fname_copy->base_name = abs_name;
698                 smb_fname = fname_copy;
699         }
700
701         status = get_full_smb_filename(do_log_ctx(), smb_fname, &fname);
702         if (!NT_STATUS_IS_OK(status)) {
703                 return "";
704         }
705         return fname;
706 }
707
708 /**
709  * Return an fsp debug string using the do_log_ctx()
710  */
711 static const char *fsp_str_do_log(const struct files_struct *fsp)
712 {
713         return smb_fname_str_do_log(fsp->conn, fsp->fsp_name);
714 }
715
716 /* Implementation of vfs_ops.  Pass everything on to the default
717    operation but log event first. */
718
719 static int smb_full_audit_connect(vfs_handle_struct *handle,
720                          const char *svc, const char *user)
721 {
722         int result;
723         const char *none[] = { "none" };
724         struct vfs_full_audit_private_data *pd = NULL;
725
726         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
727         if (result < 0) {
728                 return result;
729         }
730
731         pd = talloc_zero(handle, struct vfs_full_audit_private_data);
732         if (!pd) {
733                 SMB_VFS_NEXT_DISCONNECT(handle);
734                 return -1;
735         }
736
737         pd->syslog_facility = audit_syslog_facility(handle);
738         if (pd->syslog_facility == -1) {
739                 DEBUG(1, ("%s: Unknown facility %s\n", __func__,
740                           lp_parm_const_string(SNUM(handle->conn),
741                                                "full_audit", "facility",
742                                                "USER")));
743                 SMB_VFS_NEXT_DISCONNECT(handle);
744                 return -1;
745         }
746
747         pd->syslog_priority = audit_syslog_priority(handle);
748
749         pd->log_secdesc = lp_parm_bool(SNUM(handle->conn),
750                                        "full_audit", "log_secdesc", false);
751
752         pd->do_syslog = lp_parm_bool(SNUM(handle->conn),
753                                      "full_audit", "syslog", true);
754
755 #ifdef WITH_SYSLOG
756         if (pd->do_syslog) {
757                 openlog("smbd_audit", 0, pd->syslog_facility);
758         }
759 #endif
760
761         pd->success_ops = init_bitmap(
762                 pd, lp_parm_string_list(SNUM(handle->conn), "full_audit",
763                                         "success", none));
764         pd->failure_ops = init_bitmap(
765                 pd, lp_parm_string_list(SNUM(handle->conn), "full_audit",
766                                         "failure", none));
767
768         /* Store the private data. */
769         SMB_VFS_HANDLE_SET_DATA(handle, pd, NULL,
770                                 struct vfs_full_audit_private_data, return -1);
771
772         do_log(SMB_VFS_OP_CONNECT, True, handle,
773                "%s", svc);
774
775         return 0;
776 }
777
778 static void smb_full_audit_disconnect(vfs_handle_struct *handle)
779 {
780         const struct loadparm_substitution *lp_sub =
781                 loadparm_s3_global_substitution();
782
783         SMB_VFS_NEXT_DISCONNECT(handle);
784
785         do_log(SMB_VFS_OP_DISCONNECT, True, handle,
786                "%s", lp_servicename(talloc_tos(), lp_sub, SNUM(handle->conn)));
787
788         /* The bitmaps will be disconnected when the private
789            data is deleted. */
790 }
791
792 static uint64_t smb_full_audit_disk_free(vfs_handle_struct *handle,
793                                 const struct smb_filename *smb_fname,
794                                 uint64_t *bsize,
795                                 uint64_t *dfree,
796                                 uint64_t *dsize)
797 {
798         uint64_t result;
799
800         result = SMB_VFS_NEXT_DISK_FREE(handle, smb_fname, bsize, dfree, dsize);
801
802         /* Don't have a reasonable notion of failure here */
803
804         do_log(SMB_VFS_OP_DISK_FREE,
805                True,
806                handle,
807                "%s",
808                smb_fname_str_do_log(handle->conn, smb_fname));
809
810         return result;
811 }
812
813 static int smb_full_audit_get_quota(struct vfs_handle_struct *handle,
814                                 const struct smb_filename *smb_fname,
815                                 enum SMB_QUOTA_TYPE qtype,
816                                 unid_t id,
817                                 SMB_DISK_QUOTA *qt)
818 {
819         int result;
820
821         result = SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, qt);
822
823         do_log(SMB_VFS_OP_GET_QUOTA,
824                (result >= 0),
825                handle,
826                "%s",
827                smb_fname_str_do_log(handle->conn, smb_fname));
828
829         return result;
830 }
831
832 static int smb_full_audit_set_quota(struct vfs_handle_struct *handle,
833                            enum SMB_QUOTA_TYPE qtype, unid_t id,
834                            SMB_DISK_QUOTA *qt)
835 {
836         int result;
837
838         result = SMB_VFS_NEXT_SET_QUOTA(handle, qtype, id, qt);
839
840         do_log(SMB_VFS_OP_SET_QUOTA, (result >= 0), handle, "");
841
842         return result;
843 }
844
845 static int smb_full_audit_get_shadow_copy_data(struct vfs_handle_struct *handle,
846                                 struct files_struct *fsp,
847                                 struct shadow_copy_data *shadow_copy_data,
848                                 bool labels)
849 {
850         int result;
851
852         result = SMB_VFS_NEXT_GET_SHADOW_COPY_DATA(handle, fsp, shadow_copy_data, labels);
853
854         do_log(SMB_VFS_OP_GET_SHADOW_COPY_DATA, (result >= 0), handle, "");
855
856         return result;
857 }
858
859 static int smb_full_audit_statvfs(struct vfs_handle_struct *handle,
860                                 const struct smb_filename *smb_fname,
861                                 struct vfs_statvfs_struct *statbuf)
862 {
863         int result;
864
865         result = SMB_VFS_NEXT_STATVFS(handle, smb_fname, statbuf);
866
867         do_log(SMB_VFS_OP_STATVFS, (result >= 0), handle, "");
868
869         return result;
870 }
871
872 static uint32_t smb_full_audit_fs_capabilities(struct vfs_handle_struct *handle, enum timestamp_set_resolution *p_ts_res)
873 {
874         int result;
875
876         result = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
877
878         do_log(SMB_VFS_OP_FS_CAPABILITIES, true, handle, "");
879
880         return result;
881 }
882
883 static NTSTATUS smb_full_audit_get_dfs_referrals(
884                                 struct vfs_handle_struct *handle,
885                                 struct dfs_GetDFSReferral *r)
886 {
887         NTSTATUS status;
888
889         status = SMB_VFS_NEXT_GET_DFS_REFERRALS(handle, r);
890
891         do_log(SMB_VFS_OP_GET_DFS_REFERRALS, NT_STATUS_IS_OK(status),
892                handle, "");
893
894         return status;
895 }
896
897 static NTSTATUS smb_full_audit_create_dfs_pathat(struct vfs_handle_struct *handle,
898                                 struct files_struct *dirfsp,
899                                 const struct smb_filename *smb_fname,
900                                 const struct referral *reflist,
901                                 size_t referral_count)
902 {
903         NTSTATUS status;
904
905         status = SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
906                         dirfsp,
907                         smb_fname,
908                         reflist,
909                         referral_count);
910
911         do_log(SMB_VFS_OP_CREATE_DFS_PATHAT,
912                 NT_STATUS_IS_OK(status),
913                 handle,
914                 "%s",
915                 smb_fname_str_do_log(handle->conn, smb_fname));
916
917         return status;
918 }
919
920 static NTSTATUS smb_full_audit_read_dfs_pathat(struct vfs_handle_struct *handle,
921                         TALLOC_CTX *mem_ctx,
922                         struct files_struct *dirfsp,
923                         struct smb_filename *smb_fname,
924                         struct referral **ppreflist,
925                         size_t *preferral_count)
926 {
927         NTSTATUS status;
928
929         status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
930                         mem_ctx,
931                         dirfsp,
932                         smb_fname,
933                         ppreflist,
934                         preferral_count);
935
936         do_log(SMB_VFS_OP_READ_DFS_PATHAT,
937                 NT_STATUS_IS_OK(status),
938                 handle,
939                 "%s",
940                 smb_fname_str_do_log(handle->conn, smb_fname));
941
942         return status;
943 }
944
945 static NTSTATUS smb_full_audit_snap_check_path(struct vfs_handle_struct *handle,
946                                                TALLOC_CTX *mem_ctx,
947                                                const char *service_path,
948                                                char **base_volume)
949 {
950         NTSTATUS status;
951
952         status = SMB_VFS_NEXT_SNAP_CHECK_PATH(handle, mem_ctx, service_path,
953                                               base_volume);
954         do_log(SMB_VFS_OP_SNAP_CHECK_PATH, NT_STATUS_IS_OK(status),
955                handle, "");
956
957         return status;
958 }
959
960 static NTSTATUS smb_full_audit_snap_create(struct vfs_handle_struct *handle,
961                                            TALLOC_CTX *mem_ctx,
962                                            const char *base_volume,
963                                            time_t *tstamp,
964                                            bool rw,
965                                            char **base_path,
966                                            char **snap_path)
967 {
968         NTSTATUS status;
969
970         status = SMB_VFS_NEXT_SNAP_CREATE(handle, mem_ctx, base_volume, tstamp,
971                                           rw, base_path, snap_path);
972         do_log(SMB_VFS_OP_SNAP_CREATE, NT_STATUS_IS_OK(status), handle, "");
973
974         return status;
975 }
976
977 static NTSTATUS smb_full_audit_snap_delete(struct vfs_handle_struct *handle,
978                                            TALLOC_CTX *mem_ctx,
979                                            char *base_path,
980                                            char *snap_path)
981 {
982         NTSTATUS status;
983
984         status = SMB_VFS_NEXT_SNAP_DELETE(handle, mem_ctx, base_path,
985                                           snap_path);
986         do_log(SMB_VFS_OP_SNAP_DELETE, NT_STATUS_IS_OK(status), handle, "");
987
988         return status;
989 }
990
991 static DIR *smb_full_audit_fdopendir(vfs_handle_struct *handle,
992                           files_struct *fsp, const char *mask, uint32_t attr)
993 {
994         DIR *result;
995
996         result = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
997
998         do_log(SMB_VFS_OP_FDOPENDIR, (result != NULL), handle, "%s",
999                         fsp_str_do_log(fsp));
1000
1001         return result;
1002 }
1003
1004 static struct dirent *smb_full_audit_readdir(vfs_handle_struct *handle,
1005                                              struct files_struct *dirfsp,
1006                                              DIR *dirp,
1007                                              SMB_STRUCT_STAT *sbuf)
1008 {
1009         struct dirent *result;
1010
1011         result = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirp, sbuf);
1012
1013         /* This operation has no reasonable error condition
1014          * (End of dir is also failure), so always succeed.
1015          */
1016         do_log(SMB_VFS_OP_READDIR, True, handle, "");
1017
1018         return result;
1019 }
1020
1021 static void smb_full_audit_seekdir(vfs_handle_struct *handle,
1022                         DIR *dirp, long offset)
1023 {
1024         SMB_VFS_NEXT_SEEKDIR(handle, dirp, offset);
1025
1026         do_log(SMB_VFS_OP_SEEKDIR, True, handle, "");
1027 }
1028
1029 static long smb_full_audit_telldir(vfs_handle_struct *handle,
1030                         DIR *dirp)
1031 {
1032         long result;
1033
1034         result = SMB_VFS_NEXT_TELLDIR(handle, dirp);
1035
1036         do_log(SMB_VFS_OP_TELLDIR, True, handle, "");
1037
1038         return result;
1039 }
1040
1041 static void smb_full_audit_rewinddir(vfs_handle_struct *handle,
1042                         DIR *dirp)
1043 {
1044         SMB_VFS_NEXT_REWINDDIR(handle, dirp);
1045
1046         do_log(SMB_VFS_OP_REWINDDIR, True, handle, "");
1047 }
1048
1049 static int smb_full_audit_mkdirat(vfs_handle_struct *handle,
1050                         struct files_struct *dirfsp,
1051                         const struct smb_filename *smb_fname,
1052                         mode_t mode)
1053 {
1054         int result;
1055
1056         result = SMB_VFS_NEXT_MKDIRAT(handle,
1057                         dirfsp,
1058                         smb_fname,
1059                         mode);
1060
1061         do_log(SMB_VFS_OP_MKDIRAT,
1062                (result >= 0),
1063                handle,
1064                "%s",
1065                smb_fname_str_do_log(handle->conn, smb_fname));
1066
1067         return result;
1068 }
1069
1070 static int smb_full_audit_closedir(vfs_handle_struct *handle,
1071                           DIR *dirp)
1072 {
1073         int result;
1074
1075         result = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
1076         
1077         do_log(SMB_VFS_OP_CLOSEDIR, (result >= 0), handle, "");
1078
1079         return result;
1080 }
1081
1082 static int smb_full_audit_openat(vfs_handle_struct *handle,
1083                                  const struct files_struct *dirfsp,
1084                                  const struct smb_filename *smb_fname,
1085                                  struct files_struct *fsp,
1086                                  int flags,
1087                                  mode_t mode)
1088 {
1089         int result;
1090
1091         result = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
1092
1093         do_log(SMB_VFS_OP_OPENAT, (result >= 0), handle, "%s|%s",
1094                ((flags & O_WRONLY) || (flags & O_RDWR))?"w":"r",
1095                fsp_str_do_log(fsp));
1096
1097         return result;
1098 }
1099
1100 static NTSTATUS smb_full_audit_create_file(vfs_handle_struct *handle,
1101                                       struct smb_request *req,
1102                                       struct smb_filename *smb_fname,
1103                                       uint32_t access_mask,
1104                                       uint32_t share_access,
1105                                       uint32_t create_disposition,
1106                                       uint32_t create_options,
1107                                       uint32_t file_attributes,
1108                                       uint32_t oplock_request,
1109                                       const struct smb2_lease *lease,
1110                                       uint64_t allocation_size,
1111                                       uint32_t private_flags,
1112                                       struct security_descriptor *sd,
1113                                       struct ea_list *ea_list,
1114                                       files_struct **result_fsp,
1115                                       int *pinfo,
1116                                       const struct smb2_create_blobs *in_context_blobs,
1117                                       struct smb2_create_blobs *out_context_blobs)
1118 {
1119         NTSTATUS result;
1120         const char* str_create_disposition;
1121
1122         switch (create_disposition) {
1123         case FILE_SUPERSEDE:
1124                 str_create_disposition = "supersede";
1125                 break;
1126         case FILE_OVERWRITE_IF:
1127                 str_create_disposition = "overwrite_if";
1128                 break;
1129         case FILE_OPEN:
1130                 str_create_disposition = "open";
1131                 break;
1132         case FILE_OVERWRITE:
1133                 str_create_disposition = "overwrite";
1134                 break;
1135         case FILE_CREATE:
1136                 str_create_disposition = "create";
1137                 break;
1138         case FILE_OPEN_IF:
1139                 str_create_disposition = "open_if";
1140                 break;
1141         default:
1142                 str_create_disposition = "unknown";
1143         }
1144
1145         result = SMB_VFS_NEXT_CREATE_FILE(
1146                 handle,                                 /* handle */
1147                 req,                                    /* req */
1148                 smb_fname,                              /* fname */
1149                 access_mask,                            /* access_mask */
1150                 share_access,                           /* share_access */
1151                 create_disposition,                     /* create_disposition*/
1152                 create_options,                         /* create_options */
1153                 file_attributes,                        /* file_attributes */
1154                 oplock_request,                         /* oplock_request */
1155                 lease,                                  /* lease */
1156                 allocation_size,                        /* allocation_size */
1157                 private_flags,
1158                 sd,                                     /* sd */
1159                 ea_list,                                /* ea_list */
1160                 result_fsp,                             /* result */
1161                 pinfo,                                  /* pinfo */
1162                 in_context_blobs, out_context_blobs);   /* create context */
1163
1164         do_log(SMB_VFS_OP_CREATE_FILE, (NT_STATUS_IS_OK(result)), handle,
1165                "0x%x|%s|%s|%s", access_mask,
1166                create_options & FILE_DIRECTORY_FILE ? "dir" : "file",
1167                str_create_disposition,
1168                 smb_fname_str_do_log(handle->conn, smb_fname));
1169
1170         return result;
1171 }
1172
1173 static int smb_full_audit_close(vfs_handle_struct *handle, files_struct *fsp)
1174 {
1175         int result;
1176         
1177         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
1178
1179         do_log(SMB_VFS_OP_CLOSE, (result >= 0), handle, "%s",
1180                fsp_str_do_log(fsp));
1181
1182         return result;
1183 }
1184
1185 static ssize_t smb_full_audit_pread(vfs_handle_struct *handle, files_struct *fsp,
1186                            void *data, size_t n, off_t offset)
1187 {
1188         ssize_t result;
1189
1190         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1191
1192         do_log(SMB_VFS_OP_PREAD, (result >= 0), handle, "%s",
1193                fsp_str_do_log(fsp));
1194
1195         return result;
1196 }
1197
1198 struct smb_full_audit_pread_state {
1199         vfs_handle_struct *handle;
1200         files_struct *fsp;
1201         ssize_t ret;
1202         struct vfs_aio_state vfs_aio_state;
1203 };
1204
1205 static void smb_full_audit_pread_done(struct tevent_req *subreq);
1206
1207 static struct tevent_req *smb_full_audit_pread_send(
1208         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1209         struct tevent_context *ev, struct files_struct *fsp,
1210         void *data, size_t n, off_t offset)
1211 {
1212         struct tevent_req *req, *subreq;
1213         struct smb_full_audit_pread_state *state;
1214
1215         req = tevent_req_create(mem_ctx, &state,
1216                                 struct smb_full_audit_pread_state);
1217         if (req == NULL) {
1218                 do_log(SMB_VFS_OP_PREAD_SEND, false, handle, "%s",
1219                        fsp_str_do_log(fsp));
1220                 return NULL;
1221         }
1222         state->handle = handle;
1223         state->fsp = fsp;
1224
1225         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
1226                                          n, offset);
1227         if (tevent_req_nomem(subreq, req)) {
1228                 do_log(SMB_VFS_OP_PREAD_SEND, false, handle, "%s",
1229                        fsp_str_do_log(fsp));
1230                 return tevent_req_post(req, ev);
1231         }
1232         tevent_req_set_callback(subreq, smb_full_audit_pread_done, req);
1233
1234         do_log(SMB_VFS_OP_PREAD_SEND, true, handle, "%s", fsp_str_do_log(fsp));
1235         return req;
1236 }
1237
1238 static void smb_full_audit_pread_done(struct tevent_req *subreq)
1239 {
1240         struct tevent_req *req = tevent_req_callback_data(
1241                 subreq, struct tevent_req);
1242         struct smb_full_audit_pread_state *state = tevent_req_data(
1243                 req, struct smb_full_audit_pread_state);
1244
1245         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1246         TALLOC_FREE(subreq);
1247         tevent_req_done(req);
1248 }
1249
1250 static ssize_t smb_full_audit_pread_recv(struct tevent_req *req,
1251                                          struct vfs_aio_state *vfs_aio_state)
1252 {
1253         struct smb_full_audit_pread_state *state = tevent_req_data(
1254                 req, struct smb_full_audit_pread_state);
1255
1256         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1257                 do_log(SMB_VFS_OP_PREAD_RECV, false, state->handle, "%s",
1258                        fsp_str_do_log(state->fsp));
1259                 return -1;
1260         }
1261
1262         do_log(SMB_VFS_OP_PREAD_RECV, (state->ret >= 0), state->handle, "%s",
1263                fsp_str_do_log(state->fsp));
1264
1265         *vfs_aio_state = state->vfs_aio_state;
1266         return state->ret;
1267 }
1268
1269 static ssize_t smb_full_audit_pwrite(vfs_handle_struct *handle, files_struct *fsp,
1270                             const void *data, size_t n,
1271                             off_t offset)
1272 {
1273         ssize_t result;
1274
1275         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
1276
1277         do_log(SMB_VFS_OP_PWRITE, (result >= 0), handle, "%s",
1278                fsp_str_do_log(fsp));
1279
1280         return result;
1281 }
1282
1283 struct smb_full_audit_pwrite_state {
1284         vfs_handle_struct *handle;
1285         files_struct *fsp;
1286         ssize_t ret;
1287         struct vfs_aio_state vfs_aio_state;
1288 };
1289
1290 static void smb_full_audit_pwrite_done(struct tevent_req *subreq);
1291
1292 static struct tevent_req *smb_full_audit_pwrite_send(
1293         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1294         struct tevent_context *ev, struct files_struct *fsp,
1295         const void *data, size_t n, off_t offset)
1296 {
1297         struct tevent_req *req, *subreq;
1298         struct smb_full_audit_pwrite_state *state;
1299
1300         req = tevent_req_create(mem_ctx, &state,
1301                                 struct smb_full_audit_pwrite_state);
1302         if (req == NULL) {
1303                 do_log(SMB_VFS_OP_PWRITE_SEND, false, handle, "%s",
1304                        fsp_str_do_log(fsp));
1305                 return NULL;
1306         }
1307         state->handle = handle;
1308         state->fsp = fsp;
1309
1310         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
1311                                          n, offset);
1312         if (tevent_req_nomem(subreq, req)) {
1313                 do_log(SMB_VFS_OP_PWRITE_SEND, false, handle, "%s",
1314                        fsp_str_do_log(fsp));
1315                 return tevent_req_post(req, ev);
1316         }
1317         tevent_req_set_callback(subreq, smb_full_audit_pwrite_done, req);
1318
1319         do_log(SMB_VFS_OP_PWRITE_SEND, true, handle, "%s",
1320                fsp_str_do_log(fsp));
1321         return req;
1322 }
1323
1324 static void smb_full_audit_pwrite_done(struct tevent_req *subreq)
1325 {
1326         struct tevent_req *req = tevent_req_callback_data(
1327                 subreq, struct tevent_req);
1328         struct smb_full_audit_pwrite_state *state = tevent_req_data(
1329                 req, struct smb_full_audit_pwrite_state);
1330
1331         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1332         TALLOC_FREE(subreq);
1333         tevent_req_done(req);
1334 }
1335
1336 static ssize_t smb_full_audit_pwrite_recv(struct tevent_req *req,
1337                                           struct vfs_aio_state *vfs_aio_state)
1338 {
1339         struct smb_full_audit_pwrite_state *state = tevent_req_data(
1340                 req, struct smb_full_audit_pwrite_state);
1341
1342         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1343                 do_log(SMB_VFS_OP_PWRITE_RECV, false, state->handle, "%s",
1344                        fsp_str_do_log(state->fsp));
1345                 return -1;
1346         }
1347
1348         do_log(SMB_VFS_OP_PWRITE_RECV, (state->ret >= 0), state->handle, "%s",
1349                fsp_str_do_log(state->fsp));
1350
1351         *vfs_aio_state = state->vfs_aio_state;
1352         return state->ret;
1353 }
1354
1355 static off_t smb_full_audit_lseek(vfs_handle_struct *handle, files_struct *fsp,
1356                              off_t offset, int whence)
1357 {
1358         ssize_t result;
1359
1360         result = SMB_VFS_NEXT_LSEEK(handle, fsp, offset, whence);
1361
1362         do_log(SMB_VFS_OP_LSEEK, (result != (ssize_t)-1), handle,
1363                "%s", fsp_str_do_log(fsp));
1364
1365         return result;
1366 }
1367
1368 static ssize_t smb_full_audit_sendfile(vfs_handle_struct *handle, int tofd,
1369                               files_struct *fromfsp,
1370                               const DATA_BLOB *hdr, off_t offset,
1371                               size_t n)
1372 {
1373         ssize_t result;
1374
1375         result = SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp, hdr, offset, n);
1376
1377         do_log(SMB_VFS_OP_SENDFILE, (result >= 0), handle,
1378                "%s", fsp_str_do_log(fromfsp));
1379
1380         return result;
1381 }
1382
1383 static ssize_t smb_full_audit_recvfile(vfs_handle_struct *handle, int fromfd,
1384                       files_struct *tofsp,
1385                               off_t offset,
1386                               size_t n)
1387 {
1388         ssize_t result;
1389
1390         result = SMB_VFS_NEXT_RECVFILE(handle, fromfd, tofsp, offset, n);
1391
1392         do_log(SMB_VFS_OP_RECVFILE, (result >= 0), handle,
1393                "%s", fsp_str_do_log(tofsp));
1394
1395         return result;
1396 }
1397
1398 static int smb_full_audit_renameat(vfs_handle_struct *handle,
1399                                 files_struct *srcfsp,
1400                                 const struct smb_filename *smb_fname_src,
1401                                 files_struct *dstfsp,
1402                                 const struct smb_filename *smb_fname_dst)
1403 {
1404         int result;
1405
1406         result = SMB_VFS_NEXT_RENAMEAT(handle,
1407                                 srcfsp,
1408                                 smb_fname_src,
1409                                 dstfsp,
1410                                 smb_fname_dst);
1411
1412         do_log(SMB_VFS_OP_RENAMEAT, (result >= 0), handle, "%s|%s",
1413                smb_fname_str_do_log(handle->conn, smb_fname_src),
1414                smb_fname_str_do_log(handle->conn, smb_fname_dst));
1415
1416         return result;
1417 }
1418
1419 struct smb_full_audit_fsync_state {
1420         vfs_handle_struct *handle;
1421         files_struct *fsp;
1422         int ret;
1423         struct vfs_aio_state vfs_aio_state;
1424 };
1425
1426 static void smb_full_audit_fsync_done(struct tevent_req *subreq);
1427
1428 static struct tevent_req *smb_full_audit_fsync_send(
1429         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1430         struct tevent_context *ev, struct files_struct *fsp)
1431 {
1432         struct tevent_req *req, *subreq;
1433         struct smb_full_audit_fsync_state *state;
1434
1435         req = tevent_req_create(mem_ctx, &state,
1436                                 struct smb_full_audit_fsync_state);
1437         if (req == NULL) {
1438                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1439                        fsp_str_do_log(fsp));
1440                 return NULL;
1441         }
1442         state->handle = handle;
1443         state->fsp = fsp;
1444
1445         subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1446         if (tevent_req_nomem(subreq, req)) {
1447                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1448                        fsp_str_do_log(fsp));
1449                 return tevent_req_post(req, ev);
1450         }
1451         tevent_req_set_callback(subreq, smb_full_audit_fsync_done, req);
1452
1453         do_log(SMB_VFS_OP_FSYNC_SEND, true, handle, "%s", fsp_str_do_log(fsp));
1454         return req;
1455 }
1456
1457 static void smb_full_audit_fsync_done(struct tevent_req *subreq)
1458 {
1459         struct tevent_req *req = tevent_req_callback_data(
1460                 subreq, struct tevent_req);
1461         struct smb_full_audit_fsync_state *state = tevent_req_data(
1462                 req, struct smb_full_audit_fsync_state);
1463
1464         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1465         TALLOC_FREE(subreq);
1466         tevent_req_done(req);
1467 }
1468
1469 static int smb_full_audit_fsync_recv(struct tevent_req *req,
1470                                      struct vfs_aio_state *vfs_aio_state)
1471 {
1472         struct smb_full_audit_fsync_state *state = tevent_req_data(
1473                 req, struct smb_full_audit_fsync_state);
1474
1475         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1476                 do_log(SMB_VFS_OP_FSYNC_RECV, false, state->handle, "%s",
1477                        fsp_str_do_log(state->fsp));
1478                 return -1;
1479         }
1480
1481         do_log(SMB_VFS_OP_FSYNC_RECV, (state->ret >= 0), state->handle, "%s",
1482                fsp_str_do_log(state->fsp));
1483
1484         *vfs_aio_state = state->vfs_aio_state;
1485         return state->ret;
1486 }
1487
1488 static int smb_full_audit_stat(vfs_handle_struct *handle,
1489                                struct smb_filename *smb_fname)
1490 {
1491         int result;
1492         
1493         result = SMB_VFS_NEXT_STAT(handle, smb_fname);
1494
1495         do_log(SMB_VFS_OP_STAT, (result >= 0), handle, "%s",
1496                smb_fname_str_do_log(handle->conn, smb_fname));
1497
1498         return result;    
1499 }
1500
1501 static int smb_full_audit_fstat(vfs_handle_struct *handle, files_struct *fsp,
1502                        SMB_STRUCT_STAT *sbuf)
1503 {
1504         int result;
1505         
1506         result = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1507
1508         do_log(SMB_VFS_OP_FSTAT, (result >= 0), handle, "%s",
1509                fsp_str_do_log(fsp));
1510
1511         return result;
1512 }
1513
1514 static int smb_full_audit_lstat(vfs_handle_struct *handle,
1515                                 struct smb_filename *smb_fname)
1516 {
1517         int result;
1518         
1519         result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1520
1521         do_log(SMB_VFS_OP_LSTAT, (result >= 0), handle, "%s",
1522                smb_fname_str_do_log(handle->conn, smb_fname));
1523
1524         return result;    
1525 }
1526
1527 static uint64_t smb_full_audit_get_alloc_size(vfs_handle_struct *handle,
1528                        files_struct *fsp, const SMB_STRUCT_STAT *sbuf)
1529 {
1530         uint64_t result;
1531
1532         result = SMB_VFS_NEXT_GET_ALLOC_SIZE(handle, fsp, sbuf);
1533
1534         do_log(SMB_VFS_OP_GET_ALLOC_SIZE, (result != (uint64_t)-1), handle,
1535                         "%llu", (unsigned long long)result);
1536
1537         return result;
1538 }
1539
1540 static int smb_full_audit_unlinkat(vfs_handle_struct *handle,
1541                         struct files_struct *dirfsp,
1542                         const struct smb_filename *smb_fname,
1543                         int flags)
1544 {
1545         int result;
1546
1547         result = SMB_VFS_NEXT_UNLINKAT(handle,
1548                         dirfsp,
1549                         smb_fname,
1550                         flags);
1551
1552         do_log(SMB_VFS_OP_UNLINKAT, (result >= 0), handle, "%s",
1553                smb_fname_str_do_log(handle->conn, smb_fname));
1554
1555         return result;
1556 }
1557
1558 static int smb_full_audit_chmod(vfs_handle_struct *handle,
1559                                 const struct smb_filename *smb_fname,
1560                                 mode_t mode)
1561 {
1562         int result;
1563
1564         result = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1565
1566         do_log(SMB_VFS_OP_CHMOD,
1567                (result >= 0),
1568                handle,
1569                "%s|%o",
1570                smb_fname_str_do_log(handle->conn, smb_fname),
1571                mode);
1572
1573         return result;
1574 }
1575
1576 static int smb_full_audit_fchmod(vfs_handle_struct *handle, files_struct *fsp,
1577                         mode_t mode)
1578 {
1579         int result;
1580         
1581         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1582
1583         do_log(SMB_VFS_OP_FCHMOD, (result >= 0), handle,
1584                "%s|%o", fsp_str_do_log(fsp), mode);
1585
1586         return result;
1587 }
1588
1589 static int smb_full_audit_fchown(vfs_handle_struct *handle, files_struct *fsp,
1590                         uid_t uid, gid_t gid)
1591 {
1592         int result;
1593
1594         result = SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1595
1596         do_log(SMB_VFS_OP_FCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1597                fsp_str_do_log(fsp), (long int)uid, (long int)gid);
1598
1599         return result;
1600 }
1601
1602 static int smb_full_audit_lchown(vfs_handle_struct *handle,
1603                         const struct smb_filename *smb_fname,
1604                         uid_t uid,
1605                         gid_t gid)
1606 {
1607         int result;
1608
1609         result = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
1610
1611         do_log(SMB_VFS_OP_LCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1612                smb_fname->base_name, (long int)uid, (long int)gid);
1613
1614         return result;
1615 }
1616
1617 static int smb_full_audit_chdir(vfs_handle_struct *handle,
1618                         const struct smb_filename *smb_fname)
1619 {
1620         int result;
1621
1622         result = SMB_VFS_NEXT_CHDIR(handle, smb_fname);
1623
1624         do_log(SMB_VFS_OP_CHDIR,
1625                (result >= 0),
1626                handle,
1627                "chdir|%s",
1628                smb_fname_str_do_log(handle->conn, smb_fname));
1629
1630         return result;
1631 }
1632
1633 static struct smb_filename *smb_full_audit_getwd(vfs_handle_struct *handle,
1634                                 TALLOC_CTX *ctx)
1635 {
1636         struct smb_filename *result;
1637
1638         result = SMB_VFS_NEXT_GETWD(handle, ctx);
1639         
1640         do_log(SMB_VFS_OP_GETWD, (result != NULL), handle, "%s",
1641                 result == NULL? "" : result->base_name);
1642
1643         return result;
1644 }
1645
1646 static int smb_full_audit_ntimes(vfs_handle_struct *handle,
1647                                  const struct smb_filename *smb_fname,
1648                                  struct smb_file_time *ft)
1649 {
1650         int result;
1651         time_t create_time = convert_timespec_to_time_t(ft->create_time);
1652         time_t atime = convert_timespec_to_time_t(ft->atime);
1653         time_t mtime = convert_timespec_to_time_t(ft->mtime);
1654         time_t ctime = convert_timespec_to_time_t(ft->ctime);
1655         const char *create_time_str = "";
1656         const char *atime_str = "";
1657         const char *mtime_str = "";
1658         const char *ctime_str = "";
1659         TALLOC_CTX *frame = talloc_stackframe();
1660
1661         result = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1662
1663         if (create_time > 0) {
1664                 create_time_str = timestring(frame, create_time);
1665         }
1666         if (atime > 0) {
1667                 atime_str = timestring(frame, atime);
1668         }
1669         if (mtime > 0) {
1670                 mtime_str = timestring(frame, mtime);
1671         }
1672         if (ctime > 0) {
1673                 ctime_str = timestring(frame, ctime);
1674         }
1675
1676         do_log(SMB_VFS_OP_NTIMES,
1677                (result >= 0),
1678                handle,
1679                "%s|%s|%s|%s|%s",
1680                smb_fname_str_do_log(handle->conn, smb_fname),
1681                create_time_str,
1682                atime_str,
1683                mtime_str,
1684                ctime_str);
1685
1686         TALLOC_FREE(frame);
1687
1688         return result;
1689 }
1690
1691 static int smb_full_audit_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1692                            off_t len)
1693 {
1694         int result;
1695
1696         result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1697
1698         do_log(SMB_VFS_OP_FTRUNCATE, (result >= 0), handle,
1699                "%s", fsp_str_do_log(fsp));
1700
1701         return result;
1702 }
1703
1704 static int smb_full_audit_fallocate(vfs_handle_struct *handle, files_struct *fsp,
1705                            uint32_t mode,
1706                            off_t offset,
1707                            off_t len)
1708 {
1709         int result;
1710
1711         result = SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1712
1713         do_log(SMB_VFS_OP_FALLOCATE, (result >= 0), handle,
1714                "%s", fsp_str_do_log(fsp));
1715
1716         return result;
1717 }
1718
1719 static bool smb_full_audit_lock(vfs_handle_struct *handle, files_struct *fsp,
1720                        int op, off_t offset, off_t count, int type)
1721 {
1722         bool result;
1723
1724         result = SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1725
1726         do_log(SMB_VFS_OP_LOCK, result, handle, "%s", fsp_str_do_log(fsp));
1727
1728         return result;
1729 }
1730
1731 static int smb_full_audit_kernel_flock(struct vfs_handle_struct *handle,
1732                                        struct files_struct *fsp,
1733                                        uint32_t share_access,
1734                                        uint32_t access_mask)
1735 {
1736         int result;
1737
1738         result = SMB_VFS_NEXT_KERNEL_FLOCK(handle,
1739                                            fsp,
1740                                            share_access,
1741                                            access_mask);
1742
1743         do_log(SMB_VFS_OP_KERNEL_FLOCK, (result >= 0), handle, "%s",
1744                fsp_str_do_log(fsp));
1745
1746         return result;
1747 }
1748
1749 static int smb_full_audit_fcntl(struct vfs_handle_struct *handle,
1750                                 struct files_struct *fsp,
1751                                 int cmd, va_list cmd_arg)
1752 {
1753         void *arg;
1754         va_list dup_cmd_arg;
1755         int result;
1756
1757         va_copy(dup_cmd_arg, cmd_arg);
1758         arg = va_arg(dup_cmd_arg, void *);
1759         result = SMB_VFS_NEXT_FCNTL(handle, fsp, cmd, arg);
1760         va_end(dup_cmd_arg);
1761
1762         do_log(SMB_VFS_OP_FCNTL, (result >= 0), handle, "%s",
1763                fsp_str_do_log(fsp));
1764
1765         return result;
1766 }
1767
1768 static int smb_full_audit_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1769                                  int leasetype)
1770 {
1771         int result;
1772
1773         result = SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1774
1775         do_log(SMB_VFS_OP_LINUX_SETLEASE, (result >= 0), handle, "%s",
1776                fsp_str_do_log(fsp));
1777
1778         return result;
1779 }
1780
1781 static bool smb_full_audit_getlock(vfs_handle_struct *handle, files_struct *fsp,
1782                        off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1783 {
1784         bool result;
1785
1786         result = SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset, pcount, ptype, ppid);
1787
1788         do_log(SMB_VFS_OP_GETLOCK, result, handle, "%s", fsp_str_do_log(fsp));
1789
1790         return result;
1791 }
1792
1793 static int smb_full_audit_symlinkat(vfs_handle_struct *handle,
1794                         const struct smb_filename *link_contents,
1795                         struct files_struct *dirfsp,
1796                         const struct smb_filename *new_smb_fname)
1797 {
1798         int result;
1799
1800         result = SMB_VFS_NEXT_SYMLINKAT(handle,
1801                                 link_contents,
1802                                 dirfsp,
1803                                 new_smb_fname);
1804
1805         do_log(SMB_VFS_OP_SYMLINKAT,
1806                (result >= 0),
1807                handle,
1808                "%s|%s",
1809                link_contents->base_name,
1810                smb_fname_str_do_log(handle->conn, new_smb_fname));
1811
1812         return result;
1813 }
1814
1815 static int smb_full_audit_readlinkat(vfs_handle_struct *handle,
1816                         const struct files_struct *dirfsp,
1817                         const struct smb_filename *smb_fname,
1818                         char *buf,
1819                         size_t bufsiz)
1820 {
1821         int result;
1822
1823         result = SMB_VFS_NEXT_READLINKAT(handle,
1824                         dirfsp,
1825                         smb_fname,
1826                         buf,
1827                         bufsiz);
1828
1829         do_log(SMB_VFS_OP_READLINKAT,
1830                (result >= 0),
1831                handle,
1832                "%s",
1833                smb_fname_str_do_log(handle->conn, smb_fname));
1834
1835         return result;
1836 }
1837
1838 static int smb_full_audit_linkat(vfs_handle_struct *handle,
1839                         files_struct *srcfsp,
1840                         const struct smb_filename *old_smb_fname,
1841                         files_struct *dstfsp,
1842                         const struct smb_filename *new_smb_fname,
1843                         int flags)
1844 {
1845         int result;
1846
1847         result = SMB_VFS_NEXT_LINKAT(handle,
1848                         srcfsp,
1849                         old_smb_fname,
1850                         dstfsp,
1851                         new_smb_fname,
1852                         flags);
1853
1854         do_log(SMB_VFS_OP_LINKAT,
1855                (result >= 0),
1856                handle,
1857                "%s|%s",
1858                smb_fname_str_do_log(handle->conn, old_smb_fname),
1859                smb_fname_str_do_log(handle->conn, new_smb_fname));
1860
1861         return result;
1862 }
1863
1864 static int smb_full_audit_mknodat(vfs_handle_struct *handle,
1865                         files_struct *dirfsp,
1866                         const struct smb_filename *smb_fname,
1867                         mode_t mode,
1868                         SMB_DEV_T dev)
1869 {
1870         int result;
1871
1872         result = SMB_VFS_NEXT_MKNODAT(handle,
1873                                 dirfsp,
1874                                 smb_fname,
1875                                 mode,
1876                                 dev);
1877
1878         do_log(SMB_VFS_OP_MKNODAT,
1879                (result >= 0),
1880                handle,
1881                "%s",
1882                smb_fname_str_do_log(handle->conn, smb_fname));
1883
1884         return result;
1885 }
1886
1887 static struct smb_filename *smb_full_audit_realpath(vfs_handle_struct *handle,
1888                                 TALLOC_CTX *ctx,
1889                                 const struct smb_filename *smb_fname)
1890 {
1891         struct smb_filename *result_fname = NULL;
1892
1893         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1894
1895         do_log(SMB_VFS_OP_REALPATH,
1896                (result_fname != NULL),
1897                handle,
1898                "%s",
1899                smb_fname_str_do_log(handle->conn, smb_fname));
1900
1901         return result_fname;
1902 }
1903
1904 static int smb_full_audit_chflags(vfs_handle_struct *handle,
1905                         const struct smb_filename *smb_fname,
1906                         unsigned int flags)
1907 {
1908         int result;
1909
1910         result = SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
1911
1912         do_log(SMB_VFS_OP_CHFLAGS,
1913                (result != 0),
1914                handle,
1915                "%s",
1916                smb_fname_str_do_log(handle->conn, smb_fname));
1917
1918         return result;
1919 }
1920
1921 static struct file_id smb_full_audit_file_id_create(struct vfs_handle_struct *handle,
1922                                                     const SMB_STRUCT_STAT *sbuf)
1923 {
1924         struct file_id id_zero = { 0 };
1925         struct file_id result;
1926         struct file_id_buf idbuf;
1927
1928         result = SMB_VFS_NEXT_FILE_ID_CREATE(handle, sbuf);
1929
1930         do_log(SMB_VFS_OP_FILE_ID_CREATE,
1931                !file_id_equal(&id_zero, &result),
1932                handle,
1933                "%s",
1934                file_id_str_buf(result, &idbuf));
1935
1936         return result;
1937 }
1938
1939 static uint64_t smb_full_audit_fs_file_id(struct vfs_handle_struct *handle,
1940                                           const SMB_STRUCT_STAT *sbuf)
1941 {
1942         uint64_t result;
1943
1944         result = SMB_VFS_NEXT_FS_FILE_ID(handle, sbuf);
1945
1946         do_log(SMB_VFS_OP_FS_FILE_ID,
1947                result != 0,
1948                handle, "%" PRIu64, result);
1949
1950         return result;
1951 }
1952
1953 static NTSTATUS smb_full_audit_streaminfo(vfs_handle_struct *handle,
1954                                           struct files_struct *fsp,
1955                                           const struct smb_filename *smb_fname,
1956                                           TALLOC_CTX *mem_ctx,
1957                                           unsigned int *pnum_streams,
1958                                           struct stream_struct **pstreams)
1959 {
1960         NTSTATUS result;
1961
1962         result = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
1963                                          pnum_streams, pstreams);
1964
1965         do_log(SMB_VFS_OP_STREAMINFO,
1966                NT_STATUS_IS_OK(result),
1967                handle,
1968                "%s",
1969                smb_fname_str_do_log(handle->conn, smb_fname));
1970
1971         return result;
1972 }
1973
1974 static int smb_full_audit_get_real_filename(struct vfs_handle_struct *handle,
1975                                             const struct smb_filename *path,
1976                                             const char *name,
1977                                             TALLOC_CTX *mem_ctx,
1978                                             char **found_name)
1979 {
1980         int result;
1981
1982         result = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name, mem_ctx,
1983                                                 found_name);
1984
1985         do_log(SMB_VFS_OP_GET_REAL_FILENAME, (result == 0), handle,
1986                "%s/%s->%s",
1987                path->base_name, name, (result == 0) ? *found_name : "");
1988
1989         return result;
1990 }
1991
1992 static const char *smb_full_audit_connectpath(vfs_handle_struct *handle,
1993                                         const struct smb_filename *smb_fname)
1994 {
1995         const char *result;
1996
1997         result = SMB_VFS_NEXT_CONNECTPATH(handle, smb_fname);
1998
1999         do_log(SMB_VFS_OP_CONNECTPATH,
2000                result != NULL,
2001                handle,
2002                "%s",
2003                smb_fname_str_do_log(handle->conn, smb_fname));
2004
2005         return result;
2006 }
2007
2008 static NTSTATUS smb_full_audit_brl_lock_windows(struct vfs_handle_struct *handle,
2009                                                 struct byte_range_lock *br_lck,
2010                                                 struct lock_struct *plock)
2011 {
2012         NTSTATUS result;
2013
2014         result = SMB_VFS_NEXT_BRL_LOCK_WINDOWS(handle, br_lck, plock);
2015
2016         do_log(SMB_VFS_OP_BRL_LOCK_WINDOWS, NT_STATUS_IS_OK(result), handle,
2017             "%s:%llu-%llu. type=%d.",
2018                fsp_str_do_log(brl_fsp(br_lck)),
2019                (unsigned long long)plock->start,
2020                (unsigned long long)plock->size,
2021                plock->lock_type);
2022
2023         return result;
2024 }
2025
2026 static bool smb_full_audit_brl_unlock_windows(struct vfs_handle_struct *handle,
2027                                               struct byte_range_lock *br_lck,
2028                                               const struct lock_struct *plock)
2029 {
2030         bool result;
2031
2032         result = SMB_VFS_NEXT_BRL_UNLOCK_WINDOWS(handle, br_lck, plock);
2033
2034         do_log(SMB_VFS_OP_BRL_UNLOCK_WINDOWS, (result == 0), handle,
2035                "%s:%llu-%llu:%d", fsp_str_do_log(brl_fsp(br_lck)),
2036                (unsigned long long)plock->start,
2037                (unsigned long long)plock->size,
2038                plock->lock_type);
2039
2040         return result;
2041 }
2042
2043 static bool smb_full_audit_strict_lock_check(struct vfs_handle_struct *handle,
2044                                              struct files_struct *fsp,
2045                                              struct lock_struct *plock)
2046 {
2047         bool result;
2048
2049         result = SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
2050
2051         do_log(SMB_VFS_OP_STRICT_LOCK_CHECK, result, handle,
2052                "%s:%llu-%llu:%d", fsp_str_do_log(fsp),
2053                (unsigned long long)plock->start,
2054                (unsigned long long)plock->size,
2055                plock->lock_type);
2056
2057         return result;
2058 }
2059
2060 static NTSTATUS smb_full_audit_translate_name(struct vfs_handle_struct *handle,
2061                                               const char *name,
2062                                               enum vfs_translate_direction direction,
2063                                               TALLOC_CTX *mem_ctx,
2064                                               char **mapped_name)
2065 {
2066         NTSTATUS result;
2067
2068         result = SMB_VFS_NEXT_TRANSLATE_NAME(handle, name, direction, mem_ctx,
2069                                              mapped_name);
2070
2071         do_log(SMB_VFS_OP_TRANSLATE_NAME, NT_STATUS_IS_OK(result), handle, "");
2072
2073         return result;
2074 }
2075
2076 static NTSTATUS smb_full_audit_fsctl(struct vfs_handle_struct *handle,
2077                                 struct files_struct *fsp,
2078                                 TALLOC_CTX *ctx,
2079                                 uint32_t function,
2080                                 uint16_t req_flags,
2081                                 const uint8_t *_in_data,
2082                                 uint32_t in_len,
2083                                 uint8_t **_out_data,
2084                                 uint32_t max_out_len,
2085                                 uint32_t *out_len)
2086 {
2087         NTSTATUS result;
2088
2089         result = SMB_VFS_NEXT_FSCTL(handle,
2090                                 fsp,
2091                                 ctx,
2092                                 function,
2093                                 req_flags,
2094                                 _in_data,
2095                                 in_len,
2096                                 _out_data,
2097                                 max_out_len,
2098                                 out_len);
2099
2100         do_log(SMB_VFS_OP_FSCTL, NT_STATUS_IS_OK(result), handle, "");
2101
2102         return result;
2103 }
2104
2105 static struct tevent_req *smb_full_audit_offload_read_send(
2106         TALLOC_CTX *mem_ctx,
2107         struct tevent_context *ev,
2108         struct vfs_handle_struct *handle,
2109         struct files_struct *fsp,
2110         uint32_t fsctl,
2111         uint32_t ttl,
2112         off_t offset,
2113         size_t to_copy)
2114 {
2115         struct tevent_req *req = NULL;
2116
2117         req = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
2118                                              fsctl, ttl, offset, to_copy);
2119
2120         do_log(SMB_VFS_OP_OFFLOAD_READ_SEND, req, handle, "");
2121
2122         return req;
2123 }
2124
2125 static NTSTATUS smb_full_audit_offload_read_recv(
2126         struct tevent_req *req,
2127         struct vfs_handle_struct *handle,
2128         TALLOC_CTX *mem_ctx,
2129         DATA_BLOB *_token_blob)
2130 {
2131         NTSTATUS status;
2132
2133         status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(req, handle, mem_ctx,
2134                                                 _token_blob);
2135
2136         do_log(SMB_VFS_OP_OFFLOAD_READ_RECV, NT_STATUS_IS_OK(status), handle, "");
2137
2138         return status;
2139 }
2140
2141 static struct tevent_req *smb_full_audit_offload_write_send(struct vfs_handle_struct *handle,
2142                                                          TALLOC_CTX *mem_ctx,
2143                                                          struct tevent_context *ev,
2144                                                          uint32_t fsctl,
2145                                                          DATA_BLOB *token,
2146                                                          off_t transfer_offset,
2147                                                          struct files_struct *dest_fsp,
2148                                                          off_t dest_off,
2149                                                             off_t num)
2150 {
2151         struct tevent_req *req;
2152
2153         req = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle, mem_ctx, ev,
2154                                            fsctl, token, transfer_offset,
2155                                            dest_fsp, dest_off, num);
2156
2157         do_log(SMB_VFS_OP_OFFLOAD_WRITE_SEND, req, handle, "");
2158
2159         return req;
2160 }
2161
2162 static NTSTATUS smb_full_audit_offload_write_recv(struct vfs_handle_struct *handle,
2163                                                struct tevent_req *req,
2164                                                off_t *copied)
2165 {
2166         NTSTATUS result;
2167
2168         result = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(handle, req, copied);
2169
2170         do_log(SMB_VFS_OP_OFFLOAD_WRITE_RECV, NT_STATUS_IS_OK(result), handle, "");
2171
2172         return result;
2173 }
2174
2175 static NTSTATUS smb_full_audit_fget_compression(vfs_handle_struct *handle,
2176                                                TALLOC_CTX *mem_ctx,
2177                                                struct files_struct *fsp,
2178                                                uint16_t *_compression_fmt)
2179 {
2180         NTSTATUS result;
2181
2182         result = SMB_VFS_NEXT_FGET_COMPRESSION(handle, mem_ctx, fsp,
2183                                               _compression_fmt);
2184
2185         do_log(SMB_VFS_OP_FGET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2186                "%s",
2187                fsp_str_do_log(fsp));
2188
2189         return result;
2190 }
2191
2192 static NTSTATUS smb_full_audit_set_compression(vfs_handle_struct *handle,
2193                                                TALLOC_CTX *mem_ctx,
2194                                                struct files_struct *fsp,
2195                                                uint16_t compression_fmt)
2196 {
2197         NTSTATUS result;
2198
2199         result = SMB_VFS_NEXT_SET_COMPRESSION(handle, mem_ctx, fsp,
2200                                               compression_fmt);
2201
2202         do_log(SMB_VFS_OP_SET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2203                "%s", fsp_str_do_log(fsp));
2204
2205         return result;
2206 }
2207
2208 static NTSTATUS smb_full_audit_readdir_attr(struct vfs_handle_struct *handle,
2209                                             const struct smb_filename *fname,
2210                                             TALLOC_CTX *mem_ctx,
2211                                             struct readdir_attr_data **pattr_data)
2212 {
2213         NTSTATUS status;
2214
2215         status = SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
2216
2217         do_log(SMB_VFS_OP_READDIR_ATTR, NT_STATUS_IS_OK(status), handle, "%s",
2218                smb_fname_str_do_log(handle->conn, fname));
2219
2220         return status;
2221 }
2222
2223 struct smb_full_audit_get_dos_attributes_state {
2224         struct vfs_aio_state aio_state;
2225         vfs_handle_struct *handle;
2226         files_struct *dir_fsp;
2227         const struct smb_filename *smb_fname;
2228         uint32_t dosmode;
2229 };
2230
2231 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq);
2232
2233 static struct tevent_req *smb_full_audit_get_dos_attributes_send(
2234                 TALLOC_CTX *mem_ctx,
2235                 struct tevent_context *ev,
2236                 struct vfs_handle_struct *handle,
2237                 files_struct *dir_fsp,
2238                 struct smb_filename *smb_fname)
2239 {
2240         struct tevent_req *req = NULL;
2241         struct smb_full_audit_get_dos_attributes_state *state = NULL;
2242         struct tevent_req *subreq = NULL;
2243
2244         req = tevent_req_create(mem_ctx, &state,
2245                                 struct smb_full_audit_get_dos_attributes_state);
2246         if (req == NULL) {
2247                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2248                        false,
2249                        handle,
2250                        "%s/%s",
2251                        fsp_str_do_log(dir_fsp),
2252                        smb_fname->base_name);
2253                 return NULL;
2254         }
2255         *state = (struct smb_full_audit_get_dos_attributes_state) {
2256                 .handle = handle,
2257                 .dir_fsp = dir_fsp,
2258                 .smb_fname = smb_fname,
2259         };
2260
2261         subreq = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_SEND(mem_ctx,
2262                                                       ev,
2263                                                       handle,
2264                                                       dir_fsp,
2265                                                       smb_fname);
2266         if (tevent_req_nomem(subreq, req)) {
2267                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2268                        false,
2269                        handle,
2270                        "%s/%s",
2271                        fsp_str_do_log(dir_fsp),
2272                        smb_fname->base_name);
2273                 return tevent_req_post(req, ev);
2274         }
2275         tevent_req_set_callback(subreq,
2276                                 smb_full_audit_get_dos_attributes_done,
2277                                 req);
2278
2279         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2280                true,
2281                handle,
2282                "%s/%s",
2283                fsp_str_do_log(dir_fsp),
2284                smb_fname->base_name);
2285
2286         return req;
2287 }
2288
2289 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq)
2290 {
2291         struct tevent_req *req =
2292                 tevent_req_callback_data(subreq,
2293                 struct tevent_req);
2294         struct smb_full_audit_get_dos_attributes_state *state =
2295                 tevent_req_data(req,
2296                 struct smb_full_audit_get_dos_attributes_state);
2297         NTSTATUS status;
2298
2299         status = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_RECV(subreq,
2300                                                       &state->aio_state,
2301                                                       &state->dosmode);
2302         TALLOC_FREE(subreq);
2303         if (tevent_req_nterror(req, status)) {
2304                 return;
2305         }
2306
2307         tevent_req_done(req);
2308         return;
2309 }
2310
2311 static NTSTATUS smb_full_audit_get_dos_attributes_recv(struct tevent_req *req,
2312                                                 struct vfs_aio_state *aio_state,
2313                                                 uint32_t *dosmode)
2314 {
2315         struct smb_full_audit_get_dos_attributes_state *state =
2316                 tevent_req_data(req,
2317                 struct smb_full_audit_get_dos_attributes_state);
2318         NTSTATUS status;
2319
2320         if (tevent_req_is_nterror(req, &status)) {
2321                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2322                        false,
2323                        state->handle,
2324                        "%s/%s",
2325                        fsp_str_do_log(state->dir_fsp),
2326                        state->smb_fname->base_name);
2327                 tevent_req_received(req);
2328                 return status;
2329         }
2330
2331         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2332                true,
2333                state->handle,
2334                "%s/%s",
2335                fsp_str_do_log(state->dir_fsp),
2336                state->smb_fname->base_name);
2337
2338         *aio_state = state->aio_state;
2339         *dosmode = state->dosmode;
2340         tevent_req_received(req);
2341         return NT_STATUS_OK;
2342 }
2343
2344 static NTSTATUS smb_full_audit_fget_dos_attributes(
2345                                 struct vfs_handle_struct *handle,
2346                                 struct files_struct *fsp,
2347                                 uint32_t *dosmode)
2348 {
2349         NTSTATUS status;
2350
2351         status = SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle,
2352                                 fsp,
2353                                 dosmode);
2354
2355         do_log(SMB_VFS_OP_FGET_DOS_ATTRIBUTES,
2356                 NT_STATUS_IS_OK(status),
2357                 handle,
2358                 "%s",
2359                 fsp_str_do_log(fsp));
2360
2361         return status;
2362 }
2363
2364 static NTSTATUS smb_full_audit_set_dos_attributes(
2365                                 struct vfs_handle_struct *handle,
2366                                 const struct smb_filename *smb_fname,
2367                                 uint32_t dosmode)
2368 {
2369         NTSTATUS status;
2370
2371         status = SMB_VFS_NEXT_SET_DOS_ATTRIBUTES(handle,
2372                                 smb_fname,
2373                                 dosmode);
2374
2375         do_log(SMB_VFS_OP_SET_DOS_ATTRIBUTES,
2376                 NT_STATUS_IS_OK(status),
2377                 handle,
2378                 "%s",
2379                 smb_fname_str_do_log(handle->conn, smb_fname));
2380
2381         return status;
2382 }
2383
2384 static NTSTATUS smb_full_audit_fset_dos_attributes(
2385                                 struct vfs_handle_struct *handle,
2386                                 struct files_struct *fsp,
2387                                 uint32_t dosmode)
2388 {
2389         NTSTATUS status;
2390
2391         status = SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle,
2392                                 fsp,
2393                                 dosmode);
2394
2395         do_log(SMB_VFS_OP_FSET_DOS_ATTRIBUTES,
2396                 NT_STATUS_IS_OK(status),
2397                 handle,
2398                 "%s",
2399                 fsp_str_do_log(fsp));
2400
2401         return status;
2402 }
2403
2404 static NTSTATUS smb_full_audit_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2405                                            uint32_t security_info,
2406                                            TALLOC_CTX *mem_ctx,
2407                                            struct security_descriptor **ppdesc)
2408 {
2409         NTSTATUS result;
2410
2411         result = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2412                                           mem_ctx, ppdesc);
2413
2414         do_log(SMB_VFS_OP_FGET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2415                "%s", fsp_str_do_log(fsp));
2416
2417         return result;
2418 }
2419
2420 static NTSTATUS smb_full_audit_get_nt_acl_at(vfs_handle_struct *handle,
2421                                 struct files_struct *dirfsp,
2422                                 const struct smb_filename *smb_fname,
2423                                 uint32_t security_info,
2424                                 TALLOC_CTX *mem_ctx,
2425                                 struct security_descriptor **ppdesc)
2426 {
2427         NTSTATUS result;
2428
2429         result = SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
2430                                 dirfsp,
2431                                 smb_fname,
2432                                 security_info,
2433                                 mem_ctx,
2434                                 ppdesc);
2435
2436         do_log(SMB_VFS_OP_GET_NT_ACL_AT,
2437                 NT_STATUS_IS_OK(result),
2438                 handle,
2439                "%s",
2440                 smb_fname_str_do_log(handle->conn, smb_fname));
2441
2442         return result;
2443 }
2444
2445 static NTSTATUS smb_full_audit_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2446                               uint32_t security_info_sent,
2447                               const struct security_descriptor *psd)
2448 {
2449         struct vfs_full_audit_private_data *pd;
2450         NTSTATUS result;
2451         char *sd = NULL;
2452
2453         SMB_VFS_HANDLE_GET_DATA(handle, pd,
2454                                 struct vfs_full_audit_private_data,
2455                                 return NT_STATUS_INTERNAL_ERROR);
2456
2457         if (pd->log_secdesc) {
2458                 sd = sddl_encode(talloc_tos(), psd, get_global_sam_sid());
2459         }
2460
2461         result = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
2462
2463         do_log(SMB_VFS_OP_FSET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2464                "%s [%s]", fsp_str_do_log(fsp), sd ? sd : "");
2465
2466         TALLOC_FREE(sd);
2467
2468         return result;
2469 }
2470
2471 static NTSTATUS smb_full_audit_audit_file(struct vfs_handle_struct *handle,
2472                                 struct smb_filename *file,
2473                                 struct security_acl *sacl,
2474                                 uint32_t access_requested,
2475                                 uint32_t access_denied)
2476 {
2477         NTSTATUS result;
2478
2479         result = SMB_VFS_NEXT_AUDIT_FILE(handle,
2480                                         file,
2481                                         sacl,
2482                                         access_requested,
2483                                         access_denied);
2484
2485         do_log(SMB_VFS_OP_AUDIT_FILE, NT_STATUS_IS_OK(result), handle,
2486                         "%s",
2487                         smb_fname_str_do_log(handle->conn, file));
2488
2489         return result;
2490 }
2491
2492 static SMB_ACL_T smb_full_audit_sys_acl_get_file(vfs_handle_struct *handle,
2493                                 const struct smb_filename *smb_fname,
2494                                 SMB_ACL_TYPE_T type,
2495                                 TALLOC_CTX *mem_ctx)
2496 {
2497         SMB_ACL_T result;
2498
2499         result = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, smb_fname,
2500                                 type, mem_ctx);
2501
2502         do_log(SMB_VFS_OP_SYS_ACL_GET_FILE,
2503                (result != NULL),
2504                handle,
2505                "%s",
2506                smb_fname_str_do_log(handle->conn, smb_fname));
2507
2508         return result;
2509 }
2510
2511 static SMB_ACL_T smb_full_audit_sys_acl_get_fd(vfs_handle_struct *handle,
2512                                                files_struct *fsp, TALLOC_CTX *mem_ctx)
2513 {
2514         SMB_ACL_T result;
2515
2516         result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
2517
2518         do_log(SMB_VFS_OP_SYS_ACL_GET_FD, (result != NULL), handle,
2519                "%s", fsp_str_do_log(fsp));
2520
2521         return result;
2522 }
2523
2524 static int smb_full_audit_sys_acl_blob_get_file(vfs_handle_struct *handle,
2525                                 const struct smb_filename *smb_fname,
2526                                 TALLOC_CTX *mem_ctx,
2527                                 char **blob_description,
2528                                 DATA_BLOB *blob)
2529 {
2530         int result;
2531
2532         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, smb_fname,
2533                         mem_ctx, blob_description, blob);
2534
2535         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FILE,
2536                (result >= 0),
2537                handle,
2538                "%s",
2539                smb_fname_str_do_log(handle->conn, smb_fname));
2540
2541         return result;
2542 }
2543
2544 static int smb_full_audit_sys_acl_blob_get_fd(vfs_handle_struct *handle,
2545                                               files_struct *fsp,
2546                                               TALLOC_CTX *mem_ctx,
2547                                               char **blob_description,
2548                                               DATA_BLOB *blob)
2549 {
2550         int result;
2551
2552         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx, blob_description, blob);
2553
2554         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FD, (result >= 0), handle,
2555                "%s", fsp_str_do_log(fsp));
2556
2557         return result;
2558 }
2559
2560 static int smb_full_audit_sys_acl_set_file(vfs_handle_struct *handle,
2561                                 const struct smb_filename *smb_fname,
2562                                 SMB_ACL_TYPE_T acltype,
2563                                 SMB_ACL_T theacl)
2564 {
2565         int result;
2566
2567         result = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, smb_fname, acltype,
2568                                                theacl);
2569
2570         do_log(SMB_VFS_OP_SYS_ACL_SET_FILE,
2571                (result >= 0),
2572                handle,
2573                "%s",
2574                smb_fname_str_do_log(handle->conn, smb_fname));
2575
2576         return result;
2577 }
2578
2579 static int smb_full_audit_sys_acl_set_fd(vfs_handle_struct *handle,
2580                                          struct files_struct *fsp,
2581                                          SMB_ACL_TYPE_T type,
2582                                          SMB_ACL_T theacl)
2583 {
2584         int result;
2585
2586         result = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, type, theacl);
2587
2588         do_log(SMB_VFS_OP_SYS_ACL_SET_FD, (result >= 0), handle,
2589                "%s", fsp_str_do_log(fsp));
2590
2591         return result;
2592 }
2593
2594 static int smb_full_audit_sys_acl_delete_def_file(vfs_handle_struct *handle,
2595                                 const struct smb_filename *smb_fname)
2596 {
2597         int result;
2598
2599         result = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, smb_fname);
2600
2601         do_log(SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
2602                (result >= 0),
2603                handle,
2604                "%s",
2605                smb_fname_str_do_log(handle->conn, smb_fname));
2606
2607         return result;
2608 }
2609
2610 static ssize_t smb_full_audit_getxattr(struct vfs_handle_struct *handle,
2611                               const struct smb_filename *smb_fname,
2612                               const char *name, void *value, size_t size)
2613 {
2614         ssize_t result;
2615
2616         result = SMB_VFS_NEXT_GETXATTR(handle, smb_fname, name, value, size);
2617
2618         do_log(SMB_VFS_OP_GETXATTR,
2619                (result >= 0),
2620                handle,
2621                "%s|%s",
2622                smb_fname_str_do_log(handle->conn, smb_fname),
2623                name);
2624
2625         return result;
2626 }
2627
2628 struct smb_full_audit_getxattrat_state {
2629         struct vfs_aio_state aio_state;
2630         vfs_handle_struct *handle;
2631         files_struct *dir_fsp;
2632         const struct smb_filename *smb_fname;
2633         const char *xattr_name;
2634         ssize_t xattr_size;
2635         uint8_t *xattr_value;
2636 };
2637
2638 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq);
2639
2640 static struct tevent_req *smb_full_audit_getxattrat_send(
2641                         TALLOC_CTX *mem_ctx,
2642                         struct tevent_context *ev,
2643                         struct vfs_handle_struct *handle,
2644                         files_struct *dir_fsp,
2645                         const struct smb_filename *smb_fname,
2646                         const char *xattr_name,
2647                         size_t alloc_hint)
2648 {
2649         struct tevent_req *req = NULL;
2650         struct tevent_req *subreq = NULL;
2651         struct smb_full_audit_getxattrat_state *state = NULL;
2652
2653         req = tevent_req_create(mem_ctx, &state,
2654                                 struct smb_full_audit_getxattrat_state);
2655         if (req == NULL) {
2656                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2657                        false,
2658                        handle,
2659                        "%s/%s|%s",
2660                        fsp_str_do_log(dir_fsp),
2661                        smb_fname->base_name,
2662                        xattr_name);
2663                 return NULL;
2664         }
2665         *state = (struct smb_full_audit_getxattrat_state) {
2666                 .handle = handle,
2667                 .dir_fsp = dir_fsp,
2668                 .smb_fname = smb_fname,
2669                 .xattr_name = xattr_name,
2670         };
2671
2672         subreq = SMB_VFS_NEXT_GETXATTRAT_SEND(state,
2673                                               ev,
2674                                               handle,
2675                                               dir_fsp,
2676                                               smb_fname,
2677                                               xattr_name,
2678                                               alloc_hint);
2679         if (tevent_req_nomem(subreq, req)) {
2680                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2681                        false,
2682                        handle,
2683                        "%s/%s|%s",
2684                        fsp_str_do_log(dir_fsp),
2685                        smb_fname->base_name,
2686                        xattr_name);
2687                 return tevent_req_post(req, ev);
2688         }
2689         tevent_req_set_callback(subreq, smb_full_audit_getxattrat_done, req);
2690
2691         do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2692                true,
2693                handle,
2694                "%s/%s|%s",
2695                fsp_str_do_log(dir_fsp),
2696                smb_fname->base_name,
2697                xattr_name);
2698
2699         return req;
2700 }
2701
2702 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq)
2703 {
2704         struct tevent_req *req = tevent_req_callback_data(
2705                 subreq, struct tevent_req);
2706         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2707                 req, struct smb_full_audit_getxattrat_state);
2708
2709         state->xattr_size = SMB_VFS_NEXT_GETXATTRAT_RECV(subreq,
2710                                                          &state->aio_state,
2711                                                          state,
2712                                                          &state->xattr_value);
2713         TALLOC_FREE(subreq);
2714         if (state->xattr_size == -1) {
2715                 tevent_req_error(req, state->aio_state.error);
2716                 return;
2717         }
2718
2719         tevent_req_done(req);
2720 }
2721
2722 static ssize_t smb_full_audit_getxattrat_recv(struct tevent_req *req,
2723                                               struct vfs_aio_state *aio_state,
2724                                               TALLOC_CTX *mem_ctx,
2725                                               uint8_t **xattr_value)
2726 {
2727         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2728                 req, struct smb_full_audit_getxattrat_state);
2729         ssize_t xattr_size;
2730
2731         if (tevent_req_is_unix_error(req, &aio_state->error)) {
2732                 do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2733                        false,
2734                        state->handle,
2735                        "%s/%s|%s",
2736                        fsp_str_do_log(state->dir_fsp),
2737                        state->smb_fname->base_name,
2738                        state->xattr_name);
2739                 tevent_req_received(req);
2740                 return -1;
2741         }
2742
2743         do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2744                true,
2745                state->handle,
2746                "%s/%s|%s",
2747                fsp_str_do_log(state->dir_fsp),
2748                state->smb_fname->base_name,
2749                state->xattr_name);
2750
2751         *aio_state = state->aio_state;
2752         xattr_size = state->xattr_size;
2753         if (xattr_value != NULL) {
2754                 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
2755         }
2756
2757         tevent_req_received(req);
2758         return xattr_size;
2759 }
2760
2761 static ssize_t smb_full_audit_fgetxattr(struct vfs_handle_struct *handle,
2762                                struct files_struct *fsp,
2763                                const char *name, void *value, size_t size)
2764 {
2765         ssize_t result;
2766
2767         result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
2768
2769         do_log(SMB_VFS_OP_FGETXATTR, (result >= 0), handle,
2770                "%s|%s", fsp_str_do_log(fsp), name);
2771
2772         return result;
2773 }
2774
2775 static ssize_t smb_full_audit_listxattr(struct vfs_handle_struct *handle,
2776                                 const struct smb_filename *smb_fname,
2777                                 char *list,
2778                                 size_t size)
2779 {
2780         ssize_t result;
2781
2782         result = SMB_VFS_NEXT_LISTXATTR(handle, smb_fname, list, size);
2783
2784         do_log(SMB_VFS_OP_LISTXATTR,
2785                (result >= 0),
2786                handle,
2787                "%s",
2788                smb_fname_str_do_log(handle->conn, smb_fname));
2789
2790         return result;
2791 }
2792
2793 static ssize_t smb_full_audit_flistxattr(struct vfs_handle_struct *handle,
2794                                 struct files_struct *fsp, char *list,
2795                                 size_t size)
2796 {
2797         ssize_t result;
2798
2799         result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
2800
2801         do_log(SMB_VFS_OP_FLISTXATTR, (result >= 0), handle,
2802                "%s", fsp_str_do_log(fsp));
2803
2804         return result;
2805 }
2806
2807 static int smb_full_audit_removexattr(struct vfs_handle_struct *handle,
2808                              const struct smb_filename *smb_fname,
2809                              const char *name)
2810 {
2811         int result;
2812
2813         result = SMB_VFS_NEXT_REMOVEXATTR(handle, smb_fname, name);
2814
2815         do_log(SMB_VFS_OP_REMOVEXATTR,
2816                (result >= 0),
2817                handle,
2818                "%s|%s",
2819                smb_fname_str_do_log(handle->conn, smb_fname),
2820                name);
2821
2822         return result;
2823 }
2824
2825 static int smb_full_audit_fremovexattr(struct vfs_handle_struct *handle,
2826                               struct files_struct *fsp,
2827                               const char *name)
2828 {
2829         int result;
2830
2831         result = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
2832
2833         do_log(SMB_VFS_OP_FREMOVEXATTR, (result >= 0), handle,
2834                "%s|%s", fsp_str_do_log(fsp), name);
2835
2836         return result;
2837 }
2838
2839 static int smb_full_audit_setxattr(struct vfs_handle_struct *handle,
2840                           const struct smb_filename *smb_fname,
2841                           const char *name, const void *value, size_t size,
2842                           int flags)
2843 {
2844         int result;
2845
2846         result = SMB_VFS_NEXT_SETXATTR(handle, smb_fname, name, value, size,
2847                                        flags);
2848
2849         do_log(SMB_VFS_OP_SETXATTR,
2850                (result >= 0),
2851                handle,
2852                "%s|%s",
2853                smb_fname_str_do_log(handle->conn, smb_fname),
2854                name);
2855
2856         return result;
2857 }
2858
2859 static int smb_full_audit_fsetxattr(struct vfs_handle_struct *handle,
2860                            struct files_struct *fsp, const char *name,
2861                            const void *value, size_t size, int flags)
2862 {
2863         int result;
2864
2865         result = SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value, size, flags);
2866
2867         do_log(SMB_VFS_OP_FSETXATTR, (result >= 0), handle,
2868                "%s|%s", fsp_str_do_log(fsp), name);
2869
2870         return result;
2871 }
2872
2873 static bool smb_full_audit_aio_force(struct vfs_handle_struct *handle,
2874                                      struct files_struct *fsp)
2875 {
2876         bool result;
2877
2878         result = SMB_VFS_NEXT_AIO_FORCE(handle, fsp);
2879         do_log(SMB_VFS_OP_AIO_FORCE, result, handle,
2880                 "%s", fsp_str_do_log(fsp));
2881
2882         return result;
2883 }
2884
2885 static NTSTATUS smb_full_audit_durable_cookie(struct vfs_handle_struct *handle,
2886                                 struct files_struct *fsp,
2887                                 TALLOC_CTX *mem_ctx,
2888                                 DATA_BLOB *cookie)
2889 {
2890         NTSTATUS result;
2891
2892         result = SMB_VFS_NEXT_DURABLE_COOKIE(handle,
2893                                         fsp,
2894                                         mem_ctx,
2895                                         cookie);
2896
2897         do_log(SMB_VFS_OP_DURABLE_COOKIE, NT_STATUS_IS_OK(result), handle,
2898                         "%s", fsp_str_do_log(fsp));
2899
2900         return result;
2901 }
2902
2903 static NTSTATUS smb_full_audit_durable_disconnect(
2904                                 struct vfs_handle_struct *handle,
2905                                 struct files_struct *fsp,
2906                                 const DATA_BLOB old_cookie,
2907                                 TALLOC_CTX *mem_ctx,
2908                                 DATA_BLOB *new_cookie)
2909 {
2910         NTSTATUS result;
2911
2912         result = SMB_VFS_NEXT_DURABLE_DISCONNECT(handle,
2913                                         fsp,
2914                                         old_cookie,
2915                                         mem_ctx,
2916                                         new_cookie);
2917
2918         do_log(SMB_VFS_OP_DURABLE_DISCONNECT, NT_STATUS_IS_OK(result), handle,
2919                         "%s", fsp_str_do_log(fsp));
2920
2921         return result;
2922 }
2923
2924 static NTSTATUS smb_full_audit_durable_reconnect(
2925                                 struct vfs_handle_struct *handle,
2926                                 struct smb_request *smb1req,
2927                                 struct smbXsrv_open *op,
2928                                 const DATA_BLOB old_cookie,
2929                                 TALLOC_CTX *mem_ctx,
2930                                 struct files_struct **fsp,
2931                                 DATA_BLOB *new_cookie)
2932 {
2933         NTSTATUS result;
2934
2935         result = SMB_VFS_NEXT_DURABLE_RECONNECT(handle,
2936                                         smb1req,
2937                                         op,
2938                                         old_cookie,
2939                                         mem_ctx,
2940                                         fsp,
2941                                         new_cookie);
2942
2943         do_log(SMB_VFS_OP_DURABLE_RECONNECT,
2944                         NT_STATUS_IS_OK(result),
2945                         handle,
2946                         "");
2947
2948         return result;
2949 }
2950
2951 static struct vfs_fn_pointers vfs_full_audit_fns = {
2952
2953         /* Disk operations */
2954
2955         .connect_fn = smb_full_audit_connect,
2956         .disconnect_fn = smb_full_audit_disconnect,
2957         .disk_free_fn = smb_full_audit_disk_free,
2958         .get_quota_fn = smb_full_audit_get_quota,
2959         .set_quota_fn = smb_full_audit_set_quota,
2960         .get_shadow_copy_data_fn = smb_full_audit_get_shadow_copy_data,
2961         .statvfs_fn = smb_full_audit_statvfs,
2962         .fs_capabilities_fn = smb_full_audit_fs_capabilities,
2963         .get_dfs_referrals_fn = smb_full_audit_get_dfs_referrals,
2964         .create_dfs_pathat_fn = smb_full_audit_create_dfs_pathat,
2965         .read_dfs_pathat_fn = smb_full_audit_read_dfs_pathat,
2966         .fdopendir_fn = smb_full_audit_fdopendir,
2967         .readdir_fn = smb_full_audit_readdir,
2968         .seekdir_fn = smb_full_audit_seekdir,
2969         .telldir_fn = smb_full_audit_telldir,
2970         .rewind_dir_fn = smb_full_audit_rewinddir,
2971         .mkdirat_fn = smb_full_audit_mkdirat,
2972         .closedir_fn = smb_full_audit_closedir,
2973         .openat_fn = smb_full_audit_openat,
2974         .create_file_fn = smb_full_audit_create_file,
2975         .close_fn = smb_full_audit_close,
2976         .pread_fn = smb_full_audit_pread,
2977         .pread_send_fn = smb_full_audit_pread_send,
2978         .pread_recv_fn = smb_full_audit_pread_recv,
2979         .pwrite_fn = smb_full_audit_pwrite,
2980         .pwrite_send_fn = smb_full_audit_pwrite_send,
2981         .pwrite_recv_fn = smb_full_audit_pwrite_recv,
2982         .lseek_fn = smb_full_audit_lseek,
2983         .sendfile_fn = smb_full_audit_sendfile,
2984         .recvfile_fn = smb_full_audit_recvfile,
2985         .renameat_fn = smb_full_audit_renameat,
2986         .fsync_send_fn = smb_full_audit_fsync_send,
2987         .fsync_recv_fn = smb_full_audit_fsync_recv,
2988         .stat_fn = smb_full_audit_stat,
2989         .fstat_fn = smb_full_audit_fstat,
2990         .lstat_fn = smb_full_audit_lstat,
2991         .get_alloc_size_fn = smb_full_audit_get_alloc_size,
2992         .unlinkat_fn = smb_full_audit_unlinkat,
2993         .chmod_fn = smb_full_audit_chmod,
2994         .fchmod_fn = smb_full_audit_fchmod,
2995         .fchown_fn = smb_full_audit_fchown,
2996         .lchown_fn = smb_full_audit_lchown,
2997         .chdir_fn = smb_full_audit_chdir,
2998         .getwd_fn = smb_full_audit_getwd,
2999         .ntimes_fn = smb_full_audit_ntimes,
3000         .ftruncate_fn = smb_full_audit_ftruncate,
3001         .fallocate_fn = smb_full_audit_fallocate,
3002         .lock_fn = smb_full_audit_lock,
3003         .kernel_flock_fn = smb_full_audit_kernel_flock,
3004         .fcntl_fn = smb_full_audit_fcntl,
3005         .linux_setlease_fn = smb_full_audit_linux_setlease,
3006         .getlock_fn = smb_full_audit_getlock,
3007         .symlinkat_fn = smb_full_audit_symlinkat,
3008         .readlinkat_fn = smb_full_audit_readlinkat,
3009         .linkat_fn = smb_full_audit_linkat,
3010         .mknodat_fn = smb_full_audit_mknodat,
3011         .realpath_fn = smb_full_audit_realpath,
3012         .chflags_fn = smb_full_audit_chflags,
3013         .file_id_create_fn = smb_full_audit_file_id_create,
3014         .fs_file_id_fn = smb_full_audit_fs_file_id,
3015         .offload_read_send_fn = smb_full_audit_offload_read_send,
3016         .offload_read_recv_fn = smb_full_audit_offload_read_recv,
3017         .offload_write_send_fn = smb_full_audit_offload_write_send,
3018         .offload_write_recv_fn = smb_full_audit_offload_write_recv,
3019         .fget_compression_fn = smb_full_audit_fget_compression,
3020         .set_compression_fn = smb_full_audit_set_compression,
3021         .snap_check_path_fn =  smb_full_audit_snap_check_path,
3022         .snap_create_fn = smb_full_audit_snap_create,
3023         .snap_delete_fn = smb_full_audit_snap_delete,
3024         .streaminfo_fn = smb_full_audit_streaminfo,
3025         .get_real_filename_fn = smb_full_audit_get_real_filename,
3026         .connectpath_fn = smb_full_audit_connectpath,
3027         .brl_lock_windows_fn = smb_full_audit_brl_lock_windows,
3028         .brl_unlock_windows_fn = smb_full_audit_brl_unlock_windows,
3029         .strict_lock_check_fn = smb_full_audit_strict_lock_check,
3030         .translate_name_fn = smb_full_audit_translate_name,
3031         .fsctl_fn = smb_full_audit_fsctl,
3032         .get_dos_attributes_send_fn = smb_full_audit_get_dos_attributes_send,
3033         .get_dos_attributes_recv_fn = smb_full_audit_get_dos_attributes_recv,
3034         .fget_dos_attributes_fn = smb_full_audit_fget_dos_attributes,
3035         .set_dos_attributes_fn = smb_full_audit_set_dos_attributes,
3036         .fset_dos_attributes_fn = smb_full_audit_fset_dos_attributes,
3037         .fget_nt_acl_fn = smb_full_audit_fget_nt_acl,
3038         .get_nt_acl_at_fn = smb_full_audit_get_nt_acl_at,
3039         .fset_nt_acl_fn = smb_full_audit_fset_nt_acl,
3040         .audit_file_fn = smb_full_audit_audit_file,
3041         .sys_acl_get_file_fn = smb_full_audit_sys_acl_get_file,
3042         .sys_acl_get_fd_fn = smb_full_audit_sys_acl_get_fd,
3043         .sys_acl_blob_get_file_fn = smb_full_audit_sys_acl_blob_get_file,
3044         .sys_acl_blob_get_fd_fn = smb_full_audit_sys_acl_blob_get_fd,
3045         .sys_acl_set_file_fn = smb_full_audit_sys_acl_set_file,
3046         .sys_acl_set_fd_fn = smb_full_audit_sys_acl_set_fd,
3047         .sys_acl_delete_def_file_fn = smb_full_audit_sys_acl_delete_def_file,
3048         .getxattr_fn = smb_full_audit_getxattr,
3049         .getxattrat_send_fn = smb_full_audit_getxattrat_send,
3050         .getxattrat_recv_fn = smb_full_audit_getxattrat_recv,
3051         .fgetxattr_fn = smb_full_audit_fgetxattr,
3052         .listxattr_fn = smb_full_audit_listxattr,
3053         .flistxattr_fn = smb_full_audit_flistxattr,
3054         .removexattr_fn = smb_full_audit_removexattr,
3055         .fremovexattr_fn = smb_full_audit_fremovexattr,
3056         .setxattr_fn = smb_full_audit_setxattr,
3057         .fsetxattr_fn = smb_full_audit_fsetxattr,
3058         .aio_force_fn = smb_full_audit_aio_force,
3059         .durable_cookie_fn = smb_full_audit_durable_cookie,
3060         .durable_disconnect_fn = smb_full_audit_durable_disconnect,
3061         .durable_reconnect_fn = smb_full_audit_durable_reconnect,
3062         .readdir_attr_fn = smb_full_audit_readdir_attr
3063
3064 };
3065
3066 static_decl_vfs;
3067 NTSTATUS vfs_full_audit_init(TALLOC_CTX *ctx)
3068 {
3069         NTSTATUS ret;
3070
3071         smb_vfs_assert_all_fns(&vfs_full_audit_fns, "full_audit");
3072
3073         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "full_audit",
3074                                &vfs_full_audit_fns);
3075
3076         if (!NT_STATUS_IS_OK(ret))
3077                 return ret;
3078
3079         vfs_full_audit_debug_level = debug_add_class("full_audit");
3080         if (vfs_full_audit_debug_level == -1) {
3081                 vfs_full_audit_debug_level = DBGC_VFS;
3082                 DEBUG(0, ("vfs_full_audit: Couldn't register custom debugging "
3083                           "class!\n"));
3084         } else {
3085                 DEBUG(10, ("vfs_full_audit: Debug class number of "
3086                            "'full_audit': %d\n", vfs_full_audit_debug_level));
3087         }
3088         
3089         return ret;
3090 }