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