s3: VFS: vfs_full_audit: Remove chown_fn(). No longer used.
[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;
1879         struct file_id result;
1880
1881         ZERO_STRUCT(id_zero);
1882
1883         result = SMB_VFS_NEXT_FILE_ID_CREATE(handle, sbuf);
1884
1885         do_log(SMB_VFS_OP_FILE_ID_CREATE,
1886                !file_id_equal(&id_zero, &result),
1887                handle, "%s", file_id_string_tos(&result));
1888
1889         return result;
1890 }
1891
1892 static uint64_t smb_full_audit_fs_file_id(struct vfs_handle_struct *handle,
1893                                           const SMB_STRUCT_STAT *sbuf)
1894 {
1895         uint64_t result;
1896
1897         result = SMB_VFS_NEXT_FS_FILE_ID(handle, sbuf);
1898
1899         do_log(SMB_VFS_OP_FS_FILE_ID,
1900                result != 0,
1901                handle, "%" PRIu64, result);
1902
1903         return result;
1904 }
1905
1906 static NTSTATUS smb_full_audit_streaminfo(vfs_handle_struct *handle,
1907                                           struct files_struct *fsp,
1908                                           const struct smb_filename *smb_fname,
1909                                           TALLOC_CTX *mem_ctx,
1910                                           unsigned int *pnum_streams,
1911                                           struct stream_struct **pstreams)
1912 {
1913         NTSTATUS result;
1914
1915         result = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
1916                                          pnum_streams, pstreams);
1917
1918         do_log(SMB_VFS_OP_STREAMINFO,
1919                NT_STATUS_IS_OK(result),
1920                handle,
1921                "%s",
1922                smb_fname_str_do_log(handle->conn, smb_fname));
1923
1924         return result;
1925 }
1926
1927 static int smb_full_audit_get_real_filename(struct vfs_handle_struct *handle,
1928                                             const char *path,
1929                                             const char *name,
1930                                             TALLOC_CTX *mem_ctx,
1931                                             char **found_name)
1932 {
1933         int result;
1934
1935         result = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name, mem_ctx,
1936                                                 found_name);
1937
1938         do_log(SMB_VFS_OP_GET_REAL_FILENAME, (result == 0), handle,
1939                "%s/%s->%s", path, name, (result == 0) ? *found_name : "");
1940
1941         return result;
1942 }
1943
1944 static const char *smb_full_audit_connectpath(vfs_handle_struct *handle,
1945                                         const struct smb_filename *smb_fname)
1946 {
1947         const char *result;
1948
1949         result = SMB_VFS_NEXT_CONNECTPATH(handle, smb_fname);
1950
1951         do_log(SMB_VFS_OP_CONNECTPATH,
1952                result != NULL,
1953                handle,
1954                "%s",
1955                smb_fname_str_do_log(handle->conn, smb_fname));
1956
1957         return result;
1958 }
1959
1960 static NTSTATUS smb_full_audit_brl_lock_windows(struct vfs_handle_struct *handle,
1961                                                 struct byte_range_lock *br_lck,
1962                                                 struct lock_struct *plock)
1963 {
1964         NTSTATUS result;
1965
1966         result = SMB_VFS_NEXT_BRL_LOCK_WINDOWS(handle, br_lck, plock);
1967
1968         do_log(SMB_VFS_OP_BRL_LOCK_WINDOWS, NT_STATUS_IS_OK(result), handle,
1969             "%s:%llu-%llu. type=%d.",
1970                fsp_str_do_log(brl_fsp(br_lck)),
1971                (unsigned long long)plock->start,
1972                (unsigned long long)plock->size,
1973                plock->lock_type);
1974
1975         return result;
1976 }
1977
1978 static bool smb_full_audit_brl_unlock_windows(struct vfs_handle_struct *handle,
1979                                               struct byte_range_lock *br_lck,
1980                                               const struct lock_struct *plock)
1981 {
1982         bool result;
1983
1984         result = SMB_VFS_NEXT_BRL_UNLOCK_WINDOWS(handle, br_lck, plock);
1985
1986         do_log(SMB_VFS_OP_BRL_UNLOCK_WINDOWS, (result == 0), handle,
1987                "%s:%llu-%llu:%d", fsp_str_do_log(brl_fsp(br_lck)),
1988                (unsigned long long)plock->start,
1989                (unsigned long long)plock->size,
1990                plock->lock_type);
1991
1992         return result;
1993 }
1994
1995 static bool smb_full_audit_strict_lock_check(struct vfs_handle_struct *handle,
1996                                              struct files_struct *fsp,
1997                                              struct lock_struct *plock)
1998 {
1999         bool result;
2000
2001         result = SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
2002
2003         do_log(SMB_VFS_OP_STRICT_LOCK_CHECK, result, handle,
2004                "%s:%llu-%llu:%d", fsp_str_do_log(fsp),
2005                (unsigned long long)plock->start,
2006                (unsigned long long)plock->size,
2007                plock->lock_type);
2008
2009         return result;
2010 }
2011
2012 static NTSTATUS smb_full_audit_translate_name(struct vfs_handle_struct *handle,
2013                                               const char *name,
2014                                               enum vfs_translate_direction direction,
2015                                               TALLOC_CTX *mem_ctx,
2016                                               char **mapped_name)
2017 {
2018         NTSTATUS result;
2019
2020         result = SMB_VFS_NEXT_TRANSLATE_NAME(handle, name, direction, mem_ctx,
2021                                              mapped_name);
2022
2023         do_log(SMB_VFS_OP_TRANSLATE_NAME, NT_STATUS_IS_OK(result), handle, "");
2024
2025         return result;
2026 }
2027
2028 static NTSTATUS smb_full_audit_fsctl(struct vfs_handle_struct *handle,
2029                                 struct files_struct *fsp,
2030                                 TALLOC_CTX *ctx,
2031                                 uint32_t function,
2032                                 uint16_t req_flags,
2033                                 const uint8_t *_in_data,
2034                                 uint32_t in_len,
2035                                 uint8_t **_out_data,
2036                                 uint32_t max_out_len,
2037                                 uint32_t *out_len)
2038 {
2039         NTSTATUS result;
2040
2041         result = SMB_VFS_NEXT_FSCTL(handle,
2042                                 fsp,
2043                                 ctx,
2044                                 function,
2045                                 req_flags,
2046                                 _in_data,
2047                                 in_len,
2048                                 _out_data,
2049                                 max_out_len,
2050                                 out_len);
2051
2052         do_log(SMB_VFS_OP_FSCTL, NT_STATUS_IS_OK(result), handle, "");
2053
2054         return result;
2055 }
2056
2057 static struct tevent_req *smb_full_audit_offload_read_send(
2058         TALLOC_CTX *mem_ctx,
2059         struct tevent_context *ev,
2060         struct vfs_handle_struct *handle,
2061         struct files_struct *fsp,
2062         uint32_t fsctl,
2063         uint32_t ttl,
2064         off_t offset,
2065         size_t to_copy)
2066 {
2067         struct tevent_req *req = NULL;
2068
2069         req = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
2070                                              fsctl, ttl, offset, to_copy);
2071
2072         do_log(SMB_VFS_OP_OFFLOAD_READ_SEND, req, handle, "");
2073
2074         return req;
2075 }
2076
2077 static NTSTATUS smb_full_audit_offload_read_recv(
2078         struct tevent_req *req,
2079         struct vfs_handle_struct *handle,
2080         TALLOC_CTX *mem_ctx,
2081         DATA_BLOB *_token_blob)
2082 {
2083         NTSTATUS status;
2084
2085         status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(req, handle, mem_ctx,
2086                                                 _token_blob);
2087
2088         do_log(SMB_VFS_OP_OFFLOAD_READ_RECV, NT_STATUS_IS_OK(status), handle, "");
2089
2090         return status;
2091 }
2092
2093 static struct tevent_req *smb_full_audit_offload_write_send(struct vfs_handle_struct *handle,
2094                                                          TALLOC_CTX *mem_ctx,
2095                                                          struct tevent_context *ev,
2096                                                          uint32_t fsctl,
2097                                                          DATA_BLOB *token,
2098                                                          off_t transfer_offset,
2099                                                          struct files_struct *dest_fsp,
2100                                                          off_t dest_off,
2101                                                             off_t num)
2102 {
2103         struct tevent_req *req;
2104
2105         req = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle, mem_ctx, ev,
2106                                            fsctl, token, transfer_offset,
2107                                            dest_fsp, dest_off, num);
2108
2109         do_log(SMB_VFS_OP_OFFLOAD_WRITE_SEND, req, handle, "");
2110
2111         return req;
2112 }
2113
2114 static NTSTATUS smb_full_audit_offload_write_recv(struct vfs_handle_struct *handle,
2115                                                struct tevent_req *req,
2116                                                off_t *copied)
2117 {
2118         NTSTATUS result;
2119
2120         result = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(handle, req, copied);
2121
2122         do_log(SMB_VFS_OP_OFFLOAD_WRITE_RECV, NT_STATUS_IS_OK(result), handle, "");
2123
2124         return result;
2125 }
2126
2127 static NTSTATUS smb_full_audit_get_compression(vfs_handle_struct *handle,
2128                                                TALLOC_CTX *mem_ctx,
2129                                                struct files_struct *fsp,
2130                                                struct smb_filename *smb_fname,
2131                                                uint16_t *_compression_fmt)
2132 {
2133         NTSTATUS result;
2134
2135         result = SMB_VFS_NEXT_GET_COMPRESSION(handle, mem_ctx, fsp, smb_fname,
2136                                               _compression_fmt);
2137
2138         do_log(SMB_VFS_OP_GET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2139                "%s",
2140                (fsp ? fsp_str_do_log(fsp) :
2141                 smb_fname_str_do_log(handle->conn, smb_fname)));
2142
2143         return result;
2144 }
2145
2146 static NTSTATUS smb_full_audit_set_compression(vfs_handle_struct *handle,
2147                                                TALLOC_CTX *mem_ctx,
2148                                                struct files_struct *fsp,
2149                                                uint16_t compression_fmt)
2150 {
2151         NTSTATUS result;
2152
2153         result = SMB_VFS_NEXT_SET_COMPRESSION(handle, mem_ctx, fsp,
2154                                               compression_fmt);
2155
2156         do_log(SMB_VFS_OP_SET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2157                "%s", fsp_str_do_log(fsp));
2158
2159         return result;
2160 }
2161
2162 static NTSTATUS smb_full_audit_readdir_attr(struct vfs_handle_struct *handle,
2163                                             const struct smb_filename *fname,
2164                                             TALLOC_CTX *mem_ctx,
2165                                             struct readdir_attr_data **pattr_data)
2166 {
2167         NTSTATUS status;
2168
2169         status = SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
2170
2171         do_log(SMB_VFS_OP_READDIR_ATTR, NT_STATUS_IS_OK(status), handle, "%s",
2172                smb_fname_str_do_log(handle->conn, fname));
2173
2174         return status;
2175 }
2176
2177 static NTSTATUS smb_full_audit_get_dos_attributes(
2178                                 struct vfs_handle_struct *handle,
2179                                 struct smb_filename *smb_fname,
2180                                 uint32_t *dosmode)
2181 {
2182         NTSTATUS status;
2183
2184         status = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES(handle,
2185                                 smb_fname,
2186                                 dosmode);
2187
2188         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES,
2189                 NT_STATUS_IS_OK(status),
2190                 handle,
2191                 "%s",
2192                 smb_fname_str_do_log(handle->conn, smb_fname));
2193
2194         return status;
2195 }
2196
2197 struct smb_full_audit_get_dos_attributes_state {
2198         struct vfs_aio_state aio_state;
2199         vfs_handle_struct *handle;
2200         files_struct *dir_fsp;
2201         const struct smb_filename *smb_fname;
2202         uint32_t dosmode;
2203 };
2204
2205 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq);
2206
2207 static struct tevent_req *smb_full_audit_get_dos_attributes_send(
2208                 TALLOC_CTX *mem_ctx,
2209                 struct tevent_context *ev,
2210                 struct vfs_handle_struct *handle,
2211                 files_struct *dir_fsp,
2212                 struct smb_filename *smb_fname)
2213 {
2214         struct tevent_req *req = NULL;
2215         struct smb_full_audit_get_dos_attributes_state *state = NULL;
2216         struct tevent_req *subreq = NULL;
2217
2218         req = tevent_req_create(mem_ctx, &state,
2219                                 struct smb_full_audit_get_dos_attributes_state);
2220         if (req == NULL) {
2221                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2222                        false,
2223                        handle,
2224                        "%s/%s",
2225                        fsp_str_do_log(dir_fsp),
2226                        smb_fname->base_name);
2227                 return NULL;
2228         }
2229         *state = (struct smb_full_audit_get_dos_attributes_state) {
2230                 .handle = handle,
2231                 .dir_fsp = dir_fsp,
2232                 .smb_fname = smb_fname,
2233         };
2234
2235         subreq = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_SEND(mem_ctx,
2236                                                       ev,
2237                                                       handle,
2238                                                       dir_fsp,
2239                                                       smb_fname);
2240         if (tevent_req_nomem(subreq, req)) {
2241                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2242                        false,
2243                        handle,
2244                        "%s/%s",
2245                        fsp_str_do_log(dir_fsp),
2246                        smb_fname->base_name);
2247                 return tevent_req_post(req, ev);
2248         }
2249         tevent_req_set_callback(subreq,
2250                                 smb_full_audit_get_dos_attributes_done,
2251                                 req);
2252
2253         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2254                true,
2255                handle,
2256                "%s/%s",
2257                fsp_str_do_log(dir_fsp),
2258                smb_fname->base_name);
2259
2260         return req;
2261 }
2262
2263 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq)
2264 {
2265         struct tevent_req *req =
2266                 tevent_req_callback_data(subreq,
2267                 struct tevent_req);
2268         struct smb_full_audit_get_dos_attributes_state *state =
2269                 tevent_req_data(req,
2270                 struct smb_full_audit_get_dos_attributes_state);
2271         NTSTATUS status;
2272
2273         status = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_RECV(subreq,
2274                                                       &state->aio_state,
2275                                                       &state->dosmode);
2276         TALLOC_FREE(subreq);
2277         if (tevent_req_nterror(req, status)) {
2278                 return;
2279         }
2280
2281         tevent_req_done(req);
2282         return;
2283 }
2284
2285 static NTSTATUS smb_full_audit_get_dos_attributes_recv(struct tevent_req *req,
2286                                                 struct vfs_aio_state *aio_state,
2287                                                 uint32_t *dosmode)
2288 {
2289         struct smb_full_audit_get_dos_attributes_state *state =
2290                 tevent_req_data(req,
2291                 struct smb_full_audit_get_dos_attributes_state);
2292         NTSTATUS status;
2293
2294         if (tevent_req_is_nterror(req, &status)) {
2295                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2296                        false,
2297                        state->handle,
2298                        "%s/%s",
2299                        fsp_str_do_log(state->dir_fsp),
2300                        state->smb_fname->base_name);
2301                 tevent_req_received(req);
2302                 return status;
2303         }
2304
2305         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2306                true,
2307                state->handle,
2308                "%s/%s",
2309                fsp_str_do_log(state->dir_fsp),
2310                state->smb_fname->base_name);
2311
2312         *aio_state = state->aio_state;
2313         *dosmode = state->dosmode;
2314         tevent_req_received(req);
2315         return NT_STATUS_OK;
2316 }
2317
2318 static NTSTATUS smb_full_audit_fget_dos_attributes(
2319                                 struct vfs_handle_struct *handle,
2320                                 struct files_struct *fsp,
2321                                 uint32_t *dosmode)
2322 {
2323         NTSTATUS status;
2324
2325         status = SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle,
2326                                 fsp,
2327                                 dosmode);
2328
2329         do_log(SMB_VFS_OP_FGET_DOS_ATTRIBUTES,
2330                 NT_STATUS_IS_OK(status),
2331                 handle,
2332                 "%s",
2333                 fsp_str_do_log(fsp));
2334
2335         return status;
2336 }
2337
2338 static NTSTATUS smb_full_audit_set_dos_attributes(
2339                                 struct vfs_handle_struct *handle,
2340                                 const struct smb_filename *smb_fname,
2341                                 uint32_t dosmode)
2342 {
2343         NTSTATUS status;
2344
2345         status = SMB_VFS_NEXT_SET_DOS_ATTRIBUTES(handle,
2346                                 smb_fname,
2347                                 dosmode);
2348
2349         do_log(SMB_VFS_OP_SET_DOS_ATTRIBUTES,
2350                 NT_STATUS_IS_OK(status),
2351                 handle,
2352                 "%s",
2353                 smb_fname_str_do_log(handle->conn, smb_fname));
2354
2355         return status;
2356 }
2357
2358 static NTSTATUS smb_full_audit_fset_dos_attributes(
2359                                 struct vfs_handle_struct *handle,
2360                                 struct files_struct *fsp,
2361                                 uint32_t dosmode)
2362 {
2363         NTSTATUS status;
2364
2365         status = SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle,
2366                                 fsp,
2367                                 dosmode);
2368
2369         do_log(SMB_VFS_OP_FSET_DOS_ATTRIBUTES,
2370                 NT_STATUS_IS_OK(status),
2371                 handle,
2372                 "%s",
2373                 fsp_str_do_log(fsp));
2374
2375         return status;
2376 }
2377
2378 static NTSTATUS smb_full_audit_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2379                                            uint32_t security_info,
2380                                            TALLOC_CTX *mem_ctx,
2381                                            struct security_descriptor **ppdesc)
2382 {
2383         NTSTATUS result;
2384
2385         result = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2386                                           mem_ctx, ppdesc);
2387
2388         do_log(SMB_VFS_OP_FGET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2389                "%s", fsp_str_do_log(fsp));
2390
2391         return result;
2392 }
2393
2394 static NTSTATUS smb_full_audit_get_nt_acl(vfs_handle_struct *handle,
2395                                           const struct smb_filename *smb_fname,
2396                                           uint32_t security_info,
2397                                           TALLOC_CTX *mem_ctx,
2398                                           struct security_descriptor **ppdesc)
2399 {
2400         NTSTATUS result;
2401
2402         result = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2403                                          mem_ctx, ppdesc);
2404
2405         do_log(SMB_VFS_OP_GET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2406                "%s", smb_fname_str_do_log(handle->conn, smb_fname));
2407
2408         return result;
2409 }
2410
2411 static NTSTATUS smb_full_audit_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2412                               uint32_t security_info_sent,
2413                               const struct security_descriptor *psd)
2414 {
2415         struct vfs_full_audit_private_data *pd;
2416         NTSTATUS result;
2417         char *sd = NULL;
2418
2419         SMB_VFS_HANDLE_GET_DATA(handle, pd,
2420                                 struct vfs_full_audit_private_data,
2421                                 return NT_STATUS_INTERNAL_ERROR);
2422
2423         if (pd->log_secdesc) {
2424                 sd = sddl_encode(talloc_tos(), psd, get_global_sam_sid());
2425         }
2426
2427         result = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
2428
2429         do_log(SMB_VFS_OP_FSET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2430                "%s [%s]", fsp_str_do_log(fsp), sd ? sd : "");
2431
2432         TALLOC_FREE(sd);
2433
2434         return result;
2435 }
2436
2437 static NTSTATUS smb_full_audit_audit_file(struct vfs_handle_struct *handle,
2438                                 struct smb_filename *file,
2439                                 struct security_acl *sacl,
2440                                 uint32_t access_requested,
2441                                 uint32_t access_denied)
2442 {
2443         NTSTATUS result;
2444
2445         result = SMB_VFS_NEXT_AUDIT_FILE(handle,
2446                                         file,
2447                                         sacl,
2448                                         access_requested,
2449                                         access_denied);
2450
2451         do_log(SMB_VFS_OP_AUDIT_FILE, NT_STATUS_IS_OK(result), handle,
2452                         "%s",
2453                         smb_fname_str_do_log(handle->conn, file));
2454
2455         return result;
2456 }
2457
2458 static SMB_ACL_T smb_full_audit_sys_acl_get_file(vfs_handle_struct *handle,
2459                                 const struct smb_filename *smb_fname,
2460                                 SMB_ACL_TYPE_T type,
2461                                 TALLOC_CTX *mem_ctx)
2462 {
2463         SMB_ACL_T result;
2464
2465         result = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, smb_fname,
2466                                 type, mem_ctx);
2467
2468         do_log(SMB_VFS_OP_SYS_ACL_GET_FILE,
2469                (result != NULL),
2470                handle,
2471                "%s",
2472                smb_fname_str_do_log(handle->conn, smb_fname));
2473
2474         return result;
2475 }
2476
2477 static SMB_ACL_T smb_full_audit_sys_acl_get_fd(vfs_handle_struct *handle,
2478                                                files_struct *fsp, TALLOC_CTX *mem_ctx)
2479 {
2480         SMB_ACL_T result;
2481
2482         result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
2483
2484         do_log(SMB_VFS_OP_SYS_ACL_GET_FD, (result != NULL), handle,
2485                "%s", fsp_str_do_log(fsp));
2486
2487         return result;
2488 }
2489
2490 static int smb_full_audit_sys_acl_blob_get_file(vfs_handle_struct *handle,
2491                                 const struct smb_filename *smb_fname,
2492                                 TALLOC_CTX *mem_ctx,
2493                                 char **blob_description,
2494                                 DATA_BLOB *blob)
2495 {
2496         int result;
2497
2498         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, smb_fname,
2499                         mem_ctx, blob_description, blob);
2500
2501         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FILE,
2502                (result >= 0),
2503                handle,
2504                "%s",
2505                smb_fname_str_do_log(handle->conn, smb_fname));
2506
2507         return result;
2508 }
2509
2510 static int smb_full_audit_sys_acl_blob_get_fd(vfs_handle_struct *handle,
2511                                               files_struct *fsp,
2512                                               TALLOC_CTX *mem_ctx,
2513                                               char **blob_description,
2514                                               DATA_BLOB *blob)
2515 {
2516         int result;
2517
2518         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx, blob_description, blob);
2519
2520         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FD, (result >= 0), handle,
2521                "%s", fsp_str_do_log(fsp));
2522
2523         return result;
2524 }
2525
2526 static int smb_full_audit_sys_acl_set_file(vfs_handle_struct *handle,
2527                                 const struct smb_filename *smb_fname,
2528                                 SMB_ACL_TYPE_T acltype,
2529                                 SMB_ACL_T theacl)
2530 {
2531         int result;
2532
2533         result = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, smb_fname, acltype,
2534                                                theacl);
2535
2536         do_log(SMB_VFS_OP_SYS_ACL_SET_FILE,
2537                (result >= 0),
2538                handle,
2539                "%s",
2540                smb_fname_str_do_log(handle->conn, smb_fname));
2541
2542         return result;
2543 }
2544
2545 static int smb_full_audit_sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
2546                                 SMB_ACL_T theacl)
2547 {
2548         int result;
2549
2550         result = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
2551
2552         do_log(SMB_VFS_OP_SYS_ACL_SET_FD, (result >= 0), handle,
2553                "%s", fsp_str_do_log(fsp));
2554
2555         return result;
2556 }
2557
2558 static int smb_full_audit_sys_acl_delete_def_file(vfs_handle_struct *handle,
2559                                 const struct smb_filename *smb_fname)
2560 {
2561         int result;
2562
2563         result = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, smb_fname);
2564
2565         do_log(SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
2566                (result >= 0),
2567                handle,
2568                "%s",
2569                smb_fname_str_do_log(handle->conn, smb_fname));
2570
2571         return result;
2572 }
2573
2574 static ssize_t smb_full_audit_getxattr(struct vfs_handle_struct *handle,
2575                               const struct smb_filename *smb_fname,
2576                               const char *name, void *value, size_t size)
2577 {
2578         ssize_t result;
2579
2580         result = SMB_VFS_NEXT_GETXATTR(handle, smb_fname, name, value, size);
2581
2582         do_log(SMB_VFS_OP_GETXATTR,
2583                (result >= 0),
2584                handle,
2585                "%s|%s",
2586                smb_fname_str_do_log(handle->conn, smb_fname),
2587                name);
2588
2589         return result;
2590 }
2591
2592 struct smb_full_audit_getxattrat_state {
2593         struct vfs_aio_state aio_state;
2594         vfs_handle_struct *handle;
2595         files_struct *dir_fsp;
2596         const struct smb_filename *smb_fname;
2597         const char *xattr_name;
2598         ssize_t xattr_size;
2599         uint8_t *xattr_value;
2600 };
2601
2602 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq);
2603
2604 static struct tevent_req *smb_full_audit_getxattrat_send(
2605                         TALLOC_CTX *mem_ctx,
2606                         struct tevent_context *ev,
2607                         struct vfs_handle_struct *handle,
2608                         files_struct *dir_fsp,
2609                         const struct smb_filename *smb_fname,
2610                         const char *xattr_name,
2611                         size_t alloc_hint)
2612 {
2613         struct tevent_req *req = NULL;
2614         struct tevent_req *subreq = NULL;
2615         struct smb_full_audit_getxattrat_state *state = NULL;
2616
2617         req = tevent_req_create(mem_ctx, &state,
2618                                 struct smb_full_audit_getxattrat_state);
2619         if (req == NULL) {
2620                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2621                        false,
2622                        handle,
2623                        "%s/%s|%s",
2624                        fsp_str_do_log(dir_fsp),
2625                        smb_fname->base_name,
2626                        xattr_name);
2627                 return NULL;
2628         }
2629         *state = (struct smb_full_audit_getxattrat_state) {
2630                 .handle = handle,
2631                 .dir_fsp = dir_fsp,
2632                 .smb_fname = smb_fname,
2633                 .xattr_name = xattr_name,
2634         };
2635
2636         subreq = SMB_VFS_NEXT_GETXATTRAT_SEND(state,
2637                                               ev,
2638                                               handle,
2639                                               dir_fsp,
2640                                               smb_fname,
2641                                               xattr_name,
2642                                               alloc_hint);
2643         if (tevent_req_nomem(subreq, req)) {
2644                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2645                        false,
2646                        handle,
2647                        "%s/%s|%s",
2648                        fsp_str_do_log(dir_fsp),
2649                        smb_fname->base_name,
2650                        xattr_name);
2651                 return tevent_req_post(req, ev);
2652         }
2653         tevent_req_set_callback(subreq, smb_full_audit_getxattrat_done, req);
2654
2655         do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2656                true,
2657                handle,
2658                "%s/%s|%s",
2659                fsp_str_do_log(dir_fsp),
2660                smb_fname->base_name,
2661                xattr_name);
2662
2663         return req;
2664 }
2665
2666 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq)
2667 {
2668         struct tevent_req *req = tevent_req_callback_data(
2669                 subreq, struct tevent_req);
2670         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2671                 req, struct smb_full_audit_getxattrat_state);
2672
2673         state->xattr_size = SMB_VFS_NEXT_GETXATTRAT_RECV(subreq,
2674                                                          &state->aio_state,
2675                                                          state,
2676                                                          &state->xattr_value);
2677         TALLOC_FREE(subreq);
2678         if (state->xattr_size == -1) {
2679                 tevent_req_error(req, state->aio_state.error);
2680                 return;
2681         }
2682
2683         tevent_req_done(req);
2684 }
2685
2686 static ssize_t smb_full_audit_getxattrat_recv(struct tevent_req *req,
2687                                               struct vfs_aio_state *aio_state,
2688                                               TALLOC_CTX *mem_ctx,
2689                                               uint8_t **xattr_value)
2690 {
2691         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2692                 req, struct smb_full_audit_getxattrat_state);
2693         ssize_t xattr_size;
2694
2695         if (tevent_req_is_unix_error(req, &aio_state->error)) {
2696                 do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2697                        false,
2698                        state->handle,
2699                        "%s/%s|%s",
2700                        fsp_str_do_log(state->dir_fsp),
2701                        state->smb_fname->base_name,
2702                        state->xattr_name);
2703                 tevent_req_received(req);
2704                 return -1;
2705         }
2706
2707         do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2708                true,
2709                state->handle,
2710                "%s/%s|%s",
2711                fsp_str_do_log(state->dir_fsp),
2712                state->smb_fname->base_name,
2713                state->xattr_name);
2714
2715         *aio_state = state->aio_state;
2716         xattr_size = state->xattr_size;
2717         if (xattr_value != NULL) {
2718                 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
2719         }
2720
2721         tevent_req_received(req);
2722         return xattr_size;
2723 }
2724
2725 static ssize_t smb_full_audit_fgetxattr(struct vfs_handle_struct *handle,
2726                                struct files_struct *fsp,
2727                                const char *name, void *value, size_t size)
2728 {
2729         ssize_t result;
2730
2731         result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
2732
2733         do_log(SMB_VFS_OP_FGETXATTR, (result >= 0), handle,
2734                "%s|%s", fsp_str_do_log(fsp), name);
2735
2736         return result;
2737 }
2738
2739 static ssize_t smb_full_audit_listxattr(struct vfs_handle_struct *handle,
2740                                 const struct smb_filename *smb_fname,
2741                                 char *list,
2742                                 size_t size)
2743 {
2744         ssize_t result;
2745
2746         result = SMB_VFS_NEXT_LISTXATTR(handle, smb_fname, list, size);
2747
2748         do_log(SMB_VFS_OP_LISTXATTR,
2749                (result >= 0),
2750                handle,
2751                "%s",
2752                smb_fname_str_do_log(handle->conn, smb_fname));
2753
2754         return result;
2755 }
2756
2757 static ssize_t smb_full_audit_flistxattr(struct vfs_handle_struct *handle,
2758                                 struct files_struct *fsp, char *list,
2759                                 size_t size)
2760 {
2761         ssize_t result;
2762
2763         result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
2764
2765         do_log(SMB_VFS_OP_FLISTXATTR, (result >= 0), handle,
2766                "%s", fsp_str_do_log(fsp));
2767
2768         return result;
2769 }
2770
2771 static int smb_full_audit_removexattr(struct vfs_handle_struct *handle,
2772                              const struct smb_filename *smb_fname,
2773                              const char *name)
2774 {
2775         int result;
2776
2777         result = SMB_VFS_NEXT_REMOVEXATTR(handle, smb_fname, name);
2778
2779         do_log(SMB_VFS_OP_REMOVEXATTR,
2780                (result >= 0),
2781                handle,
2782                "%s|%s",
2783                smb_fname_str_do_log(handle->conn, smb_fname),
2784                name);
2785
2786         return result;
2787 }
2788
2789 static int smb_full_audit_fremovexattr(struct vfs_handle_struct *handle,
2790                               struct files_struct *fsp,
2791                               const char *name)
2792 {
2793         int result;
2794
2795         result = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
2796
2797         do_log(SMB_VFS_OP_FREMOVEXATTR, (result >= 0), handle,
2798                "%s|%s", fsp_str_do_log(fsp), name);
2799
2800         return result;
2801 }
2802
2803 static int smb_full_audit_setxattr(struct vfs_handle_struct *handle,
2804                           const struct smb_filename *smb_fname,
2805                           const char *name, const void *value, size_t size,
2806                           int flags)
2807 {
2808         int result;
2809
2810         result = SMB_VFS_NEXT_SETXATTR(handle, smb_fname, name, value, size,
2811                                        flags);
2812
2813         do_log(SMB_VFS_OP_SETXATTR,
2814                (result >= 0),
2815                handle,
2816                "%s|%s",
2817                smb_fname_str_do_log(handle->conn, smb_fname),
2818                name);
2819
2820         return result;
2821 }
2822
2823 static int smb_full_audit_fsetxattr(struct vfs_handle_struct *handle,
2824                            struct files_struct *fsp, const char *name,
2825                            const void *value, size_t size, int flags)
2826 {
2827         int result;
2828
2829         result = SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value, size, flags);
2830
2831         do_log(SMB_VFS_OP_FSETXATTR, (result >= 0), handle,
2832                "%s|%s", fsp_str_do_log(fsp), name);
2833
2834         return result;
2835 }
2836
2837 static bool smb_full_audit_aio_force(struct vfs_handle_struct *handle,
2838                                      struct files_struct *fsp)
2839 {
2840         bool result;
2841
2842         result = SMB_VFS_NEXT_AIO_FORCE(handle, fsp);
2843         do_log(SMB_VFS_OP_AIO_FORCE, result, handle,
2844                 "%s", fsp_str_do_log(fsp));
2845
2846         return result;
2847 }
2848
2849 static NTSTATUS smb_full_audit_durable_cookie(struct vfs_handle_struct *handle,
2850                                 struct files_struct *fsp,
2851                                 TALLOC_CTX *mem_ctx,
2852                                 DATA_BLOB *cookie)
2853 {
2854         NTSTATUS result;
2855
2856         result = SMB_VFS_NEXT_DURABLE_COOKIE(handle,
2857                                         fsp,
2858                                         mem_ctx,
2859                                         cookie);
2860
2861         do_log(SMB_VFS_OP_DURABLE_COOKIE, NT_STATUS_IS_OK(result), handle,
2862                         "%s", fsp_str_do_log(fsp));
2863
2864         return result;
2865 }
2866
2867 static NTSTATUS smb_full_audit_durable_disconnect(
2868                                 struct vfs_handle_struct *handle,
2869                                 struct files_struct *fsp,
2870                                 const DATA_BLOB old_cookie,
2871                                 TALLOC_CTX *mem_ctx,
2872                                 DATA_BLOB *new_cookie)
2873 {
2874         NTSTATUS result;
2875
2876         result = SMB_VFS_NEXT_DURABLE_DISCONNECT(handle,
2877                                         fsp,
2878                                         old_cookie,
2879                                         mem_ctx,
2880                                         new_cookie);
2881
2882         do_log(SMB_VFS_OP_DURABLE_DISCONNECT, NT_STATUS_IS_OK(result), handle,
2883                         "%s", fsp_str_do_log(fsp));
2884
2885         return result;
2886 }
2887
2888 static NTSTATUS smb_full_audit_durable_reconnect(
2889                                 struct vfs_handle_struct *handle,
2890                                 struct smb_request *smb1req,
2891                                 struct smbXsrv_open *op,
2892                                 const DATA_BLOB old_cookie,
2893                                 TALLOC_CTX *mem_ctx,
2894                                 struct files_struct **fsp,
2895                                 DATA_BLOB *new_cookie)
2896 {
2897         NTSTATUS result;
2898
2899         result = SMB_VFS_NEXT_DURABLE_RECONNECT(handle,
2900                                         smb1req,
2901                                         op,
2902                                         old_cookie,
2903                                         mem_ctx,
2904                                         fsp,
2905                                         new_cookie);
2906
2907         do_log(SMB_VFS_OP_DURABLE_RECONNECT,
2908                         NT_STATUS_IS_OK(result),
2909                         handle,
2910                         "");
2911
2912         return result;
2913 }
2914
2915 static struct vfs_fn_pointers vfs_full_audit_fns = {
2916
2917         /* Disk operations */
2918
2919         .connect_fn = smb_full_audit_connect,
2920         .disconnect_fn = smb_full_audit_disconnect,
2921         .disk_free_fn = smb_full_audit_disk_free,
2922         .get_quota_fn = smb_full_audit_get_quota,
2923         .set_quota_fn = smb_full_audit_set_quota,
2924         .get_shadow_copy_data_fn = smb_full_audit_get_shadow_copy_data,
2925         .statvfs_fn = smb_full_audit_statvfs,
2926         .fs_capabilities_fn = smb_full_audit_fs_capabilities,
2927         .get_dfs_referrals_fn = smb_full_audit_get_dfs_referrals,
2928         .opendir_fn = smb_full_audit_opendir,
2929         .fdopendir_fn = smb_full_audit_fdopendir,
2930         .readdir_fn = smb_full_audit_readdir,
2931         .seekdir_fn = smb_full_audit_seekdir,
2932         .telldir_fn = smb_full_audit_telldir,
2933         .rewind_dir_fn = smb_full_audit_rewinddir,
2934         .mkdirat_fn = smb_full_audit_mkdirat,
2935         .closedir_fn = smb_full_audit_closedir,
2936         .open_fn = smb_full_audit_open,
2937         .create_file_fn = smb_full_audit_create_file,
2938         .close_fn = smb_full_audit_close,
2939         .pread_fn = smb_full_audit_pread,
2940         .pread_send_fn = smb_full_audit_pread_send,
2941         .pread_recv_fn = smb_full_audit_pread_recv,
2942         .pwrite_fn = smb_full_audit_pwrite,
2943         .pwrite_send_fn = smb_full_audit_pwrite_send,
2944         .pwrite_recv_fn = smb_full_audit_pwrite_recv,
2945         .lseek_fn = smb_full_audit_lseek,
2946         .sendfile_fn = smb_full_audit_sendfile,
2947         .recvfile_fn = smb_full_audit_recvfile,
2948         .renameat_fn = smb_full_audit_renameat,
2949         .fsync_send_fn = smb_full_audit_fsync_send,
2950         .fsync_recv_fn = smb_full_audit_fsync_recv,
2951         .stat_fn = smb_full_audit_stat,
2952         .fstat_fn = smb_full_audit_fstat,
2953         .lstat_fn = smb_full_audit_lstat,
2954         .get_alloc_size_fn = smb_full_audit_get_alloc_size,
2955         .unlinkat_fn = smb_full_audit_unlinkat,
2956         .chmod_fn = smb_full_audit_chmod,
2957         .fchmod_fn = smb_full_audit_fchmod,
2958         .fchown_fn = smb_full_audit_fchown,
2959         .lchown_fn = smb_full_audit_lchown,
2960         .chdir_fn = smb_full_audit_chdir,
2961         .getwd_fn = smb_full_audit_getwd,
2962         .ntimes_fn = smb_full_audit_ntimes,
2963         .ftruncate_fn = smb_full_audit_ftruncate,
2964         .fallocate_fn = smb_full_audit_fallocate,
2965         .lock_fn = smb_full_audit_lock,
2966         .kernel_flock_fn = smb_full_audit_kernel_flock,
2967         .fcntl_fn = smb_full_audit_fcntl,
2968         .linux_setlease_fn = smb_full_audit_linux_setlease,
2969         .getlock_fn = smb_full_audit_getlock,
2970         .symlinkat_fn = smb_full_audit_symlinkat,
2971         .readlinkat_fn = smb_full_audit_readlinkat,
2972         .linkat_fn = smb_full_audit_linkat,
2973         .mknodat_fn = smb_full_audit_mknodat,
2974         .realpath_fn = smb_full_audit_realpath,
2975         .chflags_fn = smb_full_audit_chflags,
2976         .file_id_create_fn = smb_full_audit_file_id_create,
2977         .fs_file_id_fn = smb_full_audit_fs_file_id,
2978         .offload_read_send_fn = smb_full_audit_offload_read_send,
2979         .offload_read_recv_fn = smb_full_audit_offload_read_recv,
2980         .offload_write_send_fn = smb_full_audit_offload_write_send,
2981         .offload_write_recv_fn = smb_full_audit_offload_write_recv,
2982         .get_compression_fn = smb_full_audit_get_compression,
2983         .set_compression_fn = smb_full_audit_set_compression,
2984         .snap_check_path_fn =  smb_full_audit_snap_check_path,
2985         .snap_create_fn = smb_full_audit_snap_create,
2986         .snap_delete_fn = smb_full_audit_snap_delete,
2987         .streaminfo_fn = smb_full_audit_streaminfo,
2988         .get_real_filename_fn = smb_full_audit_get_real_filename,
2989         .connectpath_fn = smb_full_audit_connectpath,
2990         .brl_lock_windows_fn = smb_full_audit_brl_lock_windows,
2991         .brl_unlock_windows_fn = smb_full_audit_brl_unlock_windows,
2992         .strict_lock_check_fn = smb_full_audit_strict_lock_check,
2993         .translate_name_fn = smb_full_audit_translate_name,
2994         .fsctl_fn = smb_full_audit_fsctl,
2995         .get_dos_attributes_fn = smb_full_audit_get_dos_attributes,
2996         .get_dos_attributes_send_fn = smb_full_audit_get_dos_attributes_send,
2997         .get_dos_attributes_recv_fn = smb_full_audit_get_dos_attributes_recv,
2998         .fget_dos_attributes_fn = smb_full_audit_fget_dos_attributes,
2999         .set_dos_attributes_fn = smb_full_audit_set_dos_attributes,
3000         .fset_dos_attributes_fn = smb_full_audit_fset_dos_attributes,
3001         .fget_nt_acl_fn = smb_full_audit_fget_nt_acl,
3002         .get_nt_acl_fn = smb_full_audit_get_nt_acl,
3003         .fset_nt_acl_fn = smb_full_audit_fset_nt_acl,
3004         .audit_file_fn = smb_full_audit_audit_file,
3005         .sys_acl_get_file_fn = smb_full_audit_sys_acl_get_file,
3006         .sys_acl_get_fd_fn = smb_full_audit_sys_acl_get_fd,
3007         .sys_acl_blob_get_file_fn = smb_full_audit_sys_acl_blob_get_file,
3008         .sys_acl_blob_get_fd_fn = smb_full_audit_sys_acl_blob_get_fd,
3009         .sys_acl_set_file_fn = smb_full_audit_sys_acl_set_file,
3010         .sys_acl_set_fd_fn = smb_full_audit_sys_acl_set_fd,
3011         .sys_acl_delete_def_file_fn = smb_full_audit_sys_acl_delete_def_file,
3012         .getxattr_fn = smb_full_audit_getxattr,
3013         .getxattrat_send_fn = smb_full_audit_getxattrat_send,
3014         .getxattrat_recv_fn = smb_full_audit_getxattrat_recv,
3015         .fgetxattr_fn = smb_full_audit_fgetxattr,
3016         .listxattr_fn = smb_full_audit_listxattr,
3017         .flistxattr_fn = smb_full_audit_flistxattr,
3018         .removexattr_fn = smb_full_audit_removexattr,
3019         .fremovexattr_fn = smb_full_audit_fremovexattr,
3020         .setxattr_fn = smb_full_audit_setxattr,
3021         .fsetxattr_fn = smb_full_audit_fsetxattr,
3022         .aio_force_fn = smb_full_audit_aio_force,
3023         .durable_cookie_fn = smb_full_audit_durable_cookie,
3024         .durable_disconnect_fn = smb_full_audit_durable_disconnect,
3025         .durable_reconnect_fn = smb_full_audit_durable_reconnect,
3026         .readdir_attr_fn = smb_full_audit_readdir_attr
3027
3028 };
3029
3030 static_decl_vfs;
3031 NTSTATUS vfs_full_audit_init(TALLOC_CTX *ctx)
3032 {
3033         NTSTATUS ret;
3034
3035         smb_vfs_assert_all_fns(&vfs_full_audit_fns, "full_audit");
3036
3037         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "full_audit",
3038                                &vfs_full_audit_fns);
3039
3040         if (!NT_STATUS_IS_OK(ret))
3041                 return ret;
3042
3043         vfs_full_audit_debug_level = debug_add_class("full_audit");
3044         if (vfs_full_audit_debug_level == -1) {
3045                 vfs_full_audit_debug_level = DBGC_VFS;
3046                 DEBUG(0, ("vfs_full_audit: Couldn't register custom debugging "
3047                           "class!\n"));
3048         } else {
3049                 DEBUG(10, ("vfs_full_audit: Debug class number of "
3050                            "'full_audit': %d\n", vfs_full_audit_debug_level));
3051         }
3052         
3053         return ret;
3054 }