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